Print this page
6785 nfs4_attr_cache deadlock
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/fs/nfs/nfs4_vnops.c
+++ new/usr/src/uts/common/fs/nfs/nfs4_vnops.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
24 24 */
25 25
26 26 /*
27 27 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
28 28 * Use is subject to license terms.
29 29 */
30 30
31 31 /*
32 32 * Copyright 1983,1984,1985,1986,1987,1988,1989 AT&T.
33 33 * All Rights Reserved
34 34 */
35 35
36 36 /*
37 37 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
38 38 */
39 39
40 40 #include <sys/param.h>
41 41 #include <sys/types.h>
42 42 #include <sys/systm.h>
43 43 #include <sys/cred.h>
44 44 #include <sys/time.h>
45 45 #include <sys/vnode.h>
46 46 #include <sys/vfs.h>
47 47 #include <sys/vfs_opreg.h>
48 48 #include <sys/file.h>
49 49 #include <sys/filio.h>
50 50 #include <sys/uio.h>
51 51 #include <sys/buf.h>
52 52 #include <sys/mman.h>
53 53 #include <sys/pathname.h>
54 54 #include <sys/dirent.h>
55 55 #include <sys/debug.h>
56 56 #include <sys/vmsystm.h>
57 57 #include <sys/fcntl.h>
58 58 #include <sys/flock.h>
59 59 #include <sys/swap.h>
60 60 #include <sys/errno.h>
61 61 #include <sys/strsubr.h>
62 62 #include <sys/sysmacros.h>
63 63 #include <sys/kmem.h>
64 64 #include <sys/cmn_err.h>
65 65 #include <sys/pathconf.h>
66 66 #include <sys/utsname.h>
67 67 #include <sys/dnlc.h>
68 68 #include <sys/acl.h>
69 69 #include <sys/systeminfo.h>
70 70 #include <sys/policy.h>
71 71 #include <sys/sdt.h>
72 72 #include <sys/list.h>
73 73 #include <sys/stat.h>
74 74 #include <sys/zone.h>
75 75
76 76 #include <rpc/types.h>
77 77 #include <rpc/auth.h>
78 78 #include <rpc/clnt.h>
79 79
80 80 #include <nfs/nfs.h>
81 81 #include <nfs/nfs_clnt.h>
82 82 #include <nfs/nfs_acl.h>
83 83 #include <nfs/lm.h>
84 84 #include <nfs/nfs4.h>
85 85 #include <nfs/nfs4_kprot.h>
86 86 #include <nfs/rnode4.h>
87 87 #include <nfs/nfs4_clnt.h>
88 88
89 89 #include <vm/hat.h>
90 90 #include <vm/as.h>
91 91 #include <vm/page.h>
92 92 #include <vm/pvn.h>
93 93 #include <vm/seg.h>
94 94 #include <vm/seg_map.h>
95 95 #include <vm/seg_kpm.h>
96 96 #include <vm/seg_vn.h>
97 97
98 98 #include <fs/fs_subr.h>
99 99
100 100 #include <sys/ddi.h>
101 101 #include <sys/int_fmtio.h>
102 102 #include <sys/fs/autofs.h>
103 103
104 104 typedef struct {
105 105 nfs4_ga_res_t *di_garp;
106 106 cred_t *di_cred;
107 107 hrtime_t di_time_call;
108 108 } dirattr_info_t;
109 109
110 110 typedef enum nfs4_acl_op {
111 111 NFS4_ACL_GET,
112 112 NFS4_ACL_SET
113 113 } nfs4_acl_op_t;
114 114
115 115 static struct lm_sysid *nfs4_find_sysid(mntinfo4_t *mi);
116 116
117 117 static void nfs4_update_dircaches(change_info4 *, vnode_t *, vnode_t *,
118 118 char *, dirattr_info_t *);
119 119
120 120 static void nfs4close_otw(rnode4_t *, cred_t *, nfs4_open_owner_t *,
121 121 nfs4_open_stream_t *, int *, int *, nfs4_close_type_t,
122 122 nfs4_error_t *, int *);
123 123 static int nfs4_rdwrlbn(vnode_t *, page_t *, u_offset_t, size_t, int,
124 124 cred_t *);
125 125 static int nfs4write(vnode_t *, caddr_t, u_offset_t, int, cred_t *,
126 126 stable_how4 *);
127 127 static int nfs4read(vnode_t *, caddr_t, offset_t, int, size_t *,
128 128 cred_t *, bool_t, struct uio *);
129 129 static int nfs4setattr(vnode_t *, struct vattr *, int, cred_t *,
130 130 vsecattr_t *);
131 131 static int nfs4openattr(vnode_t *, vnode_t **, int, cred_t *);
132 132 static int nfs4lookup(vnode_t *, char *, vnode_t **, cred_t *, int);
133 133 static int nfs4lookup_xattr(vnode_t *, char *, vnode_t **, int, cred_t *);
134 134 static int nfs4lookupvalidate_otw(vnode_t *, char *, vnode_t **, cred_t *);
135 135 static int nfs4lookupnew_otw(vnode_t *, char *, vnode_t **, cred_t *);
136 136 static int nfs4mknod(vnode_t *, char *, struct vattr *, enum vcexcl,
137 137 int, vnode_t **, cred_t *);
138 138 static int nfs4open_otw(vnode_t *, char *, struct vattr *, vnode_t **,
139 139 cred_t *, int, int, enum createmode4, int);
140 140 static int nfs4rename(vnode_t *, char *, vnode_t *, char *, cred_t *,
141 141 caller_context_t *);
142 142 static int nfs4rename_persistent_fh(vnode_t *, char *, vnode_t *,
143 143 vnode_t *, char *, cred_t *, nfsstat4 *);
144 144 static int nfs4rename_volatile_fh(vnode_t *, char *, vnode_t *,
145 145 vnode_t *, char *, cred_t *, nfsstat4 *);
146 146 static int do_nfs4readdir(vnode_t *, rddir4_cache *, cred_t *);
147 147 static void nfs4readdir(vnode_t *, rddir4_cache *, cred_t *);
148 148 static int nfs4_bio(struct buf *, stable_how4 *, cred_t *, bool_t);
149 149 static int nfs4_getapage(vnode_t *, u_offset_t, size_t, uint_t *,
150 150 page_t *[], size_t, struct seg *, caddr_t,
151 151 enum seg_rw, cred_t *);
152 152 static void nfs4_readahead(vnode_t *, u_offset_t, caddr_t, struct seg *,
153 153 cred_t *);
154 154 static int nfs4_sync_putapage(vnode_t *, page_t *, u_offset_t, size_t,
155 155 int, cred_t *);
156 156 static int nfs4_sync_pageio(vnode_t *, page_t *, u_offset_t, size_t,
157 157 int, cred_t *);
158 158 static int nfs4_commit(vnode_t *, offset4, count4, cred_t *);
159 159 static void nfs4_set_mod(vnode_t *);
160 160 static void nfs4_get_commit(vnode_t *);
161 161 static void nfs4_get_commit_range(vnode_t *, u_offset_t, size_t);
162 162 static int nfs4_putpage_commit(vnode_t *, offset_t, size_t, cred_t *);
163 163 static int nfs4_commit_vp(vnode_t *, u_offset_t, size_t, cred_t *, int);
164 164 static int nfs4_sync_commit(vnode_t *, page_t *, offset3, count3,
165 165 cred_t *);
166 166 static void do_nfs4_async_commit(vnode_t *, page_t *, offset3, count3,
167 167 cred_t *);
168 168 static int nfs4_update_attrcache(nfsstat4, nfs4_ga_res_t *,
169 169 hrtime_t, vnode_t *, cred_t *);
170 170 static int nfs4_open_non_reg_file(vnode_t **, int, cred_t *);
171 171 static int nfs4_safelock(vnode_t *, const struct flock64 *, cred_t *);
172 172 static void nfs4_register_lock_locally(vnode_t *, struct flock64 *, int,
173 173 u_offset_t);
174 174 static int nfs4_lockrelease(vnode_t *, int, offset_t, cred_t *);
175 175 static int nfs4_block_and_wait(clock_t *, rnode4_t *);
176 176 static cred_t *state_to_cred(nfs4_open_stream_t *);
177 177 static void denied_to_flk(LOCK4denied *, flock64_t *, LOCKT4args *);
178 178 static pid_t lo_to_pid(lock_owner4 *);
179 179 static void nfs4_reinstitute_local_lock_state(vnode_t *, flock64_t *,
180 180 cred_t *, nfs4_lock_owner_t *);
181 181 static void push_reinstate(vnode_t *, int, flock64_t *, cred_t *,
182 182 nfs4_lock_owner_t *);
183 183 static int open_and_get_osp(vnode_t *, cred_t *, nfs4_open_stream_t **);
184 184 static void nfs4_delmap_callback(struct as *, void *, uint_t);
185 185 static void nfs4_free_delmapcall(nfs4_delmapcall_t *);
186 186 static nfs4_delmapcall_t *nfs4_init_delmapcall();
187 187 static int nfs4_find_and_delete_delmapcall(rnode4_t *, int *);
188 188 static int nfs4_is_acl_mask_valid(uint_t, nfs4_acl_op_t);
189 189 static int nfs4_create_getsecattr_return(vsecattr_t *, vsecattr_t *,
190 190 uid_t, gid_t, int);
191 191
192 192 /*
193 193 * Routines that implement the setting of v4 args for the misc. ops
194 194 */
195 195 static void nfs4args_lock_free(nfs_argop4 *);
196 196 static void nfs4args_lockt_free(nfs_argop4 *);
197 197 static void nfs4args_setattr(nfs_argop4 *, vattr_t *, vsecattr_t *,
198 198 int, rnode4_t *, cred_t *, bitmap4, int *,
199 199 nfs4_stateid_types_t *);
200 200 static void nfs4args_setattr_free(nfs_argop4 *);
201 201 static int nfs4args_verify(nfs_argop4 *, vattr_t *, enum nfs_opnum4,
202 202 bitmap4);
203 203 static void nfs4args_verify_free(nfs_argop4 *);
204 204 static void nfs4args_write(nfs_argop4 *, stable_how4, rnode4_t *, cred_t *,
205 205 WRITE4args **, nfs4_stateid_types_t *);
206 206
207 207 /*
208 208 * These are the vnode ops functions that implement the vnode interface to
209 209 * the networked file system. See more comments below at nfs4_vnodeops.
210 210 */
211 211 static int nfs4_open(vnode_t **, int, cred_t *, caller_context_t *);
212 212 static int nfs4_close(vnode_t *, int, int, offset_t, cred_t *,
213 213 caller_context_t *);
214 214 static int nfs4_read(vnode_t *, struct uio *, int, cred_t *,
215 215 caller_context_t *);
216 216 static int nfs4_write(vnode_t *, struct uio *, int, cred_t *,
217 217 caller_context_t *);
218 218 static int nfs4_ioctl(vnode_t *, int, intptr_t, int, cred_t *, int *,
219 219 caller_context_t *);
220 220 static int nfs4_setattr(vnode_t *, struct vattr *, int, cred_t *,
221 221 caller_context_t *);
222 222 static int nfs4_access(vnode_t *, int, int, cred_t *, caller_context_t *);
223 223 static int nfs4_readlink(vnode_t *, struct uio *, cred_t *,
224 224 caller_context_t *);
225 225 static int nfs4_fsync(vnode_t *, int, cred_t *, caller_context_t *);
226 226 static int nfs4_create(vnode_t *, char *, struct vattr *, enum vcexcl,
227 227 int, vnode_t **, cred_t *, int, caller_context_t *,
228 228 vsecattr_t *);
229 229 static int nfs4_remove(vnode_t *, char *, cred_t *, caller_context_t *,
230 230 int);
231 231 static int nfs4_link(vnode_t *, vnode_t *, char *, cred_t *,
232 232 caller_context_t *, int);
233 233 static int nfs4_rename(vnode_t *, char *, vnode_t *, char *, cred_t *,
234 234 caller_context_t *, int);
235 235 static int nfs4_mkdir(vnode_t *, char *, struct vattr *, vnode_t **,
236 236 cred_t *, caller_context_t *, int, vsecattr_t *);
237 237 static int nfs4_rmdir(vnode_t *, char *, vnode_t *, cred_t *,
238 238 caller_context_t *, int);
239 239 static int nfs4_symlink(vnode_t *, char *, struct vattr *, char *,
240 240 cred_t *, caller_context_t *, int);
241 241 static int nfs4_readdir(vnode_t *, struct uio *, cred_t *, int *,
242 242 caller_context_t *, int);
243 243 static int nfs4_seek(vnode_t *, offset_t, offset_t *, caller_context_t *);
244 244 static int nfs4_getpage(vnode_t *, offset_t, size_t, uint_t *,
245 245 page_t *[], size_t, struct seg *, caddr_t,
246 246 enum seg_rw, cred_t *, caller_context_t *);
247 247 static int nfs4_putpage(vnode_t *, offset_t, size_t, int, cred_t *,
248 248 caller_context_t *);
249 249 static int nfs4_map(vnode_t *, offset_t, struct as *, caddr_t *, size_t,
250 250 uchar_t, uchar_t, uint_t, cred_t *, caller_context_t *);
251 251 static int nfs4_addmap(vnode_t *, offset_t, struct as *, caddr_t, size_t,
252 252 uchar_t, uchar_t, uint_t, cred_t *, caller_context_t *);
253 253 static int nfs4_cmp(vnode_t *, vnode_t *, caller_context_t *);
254 254 static int nfs4_frlock(vnode_t *, int, struct flock64 *, int, offset_t,
255 255 struct flk_callback *, cred_t *, caller_context_t *);
256 256 static int nfs4_space(vnode_t *, int, struct flock64 *, int, offset_t,
257 257 cred_t *, caller_context_t *);
258 258 static int nfs4_delmap(vnode_t *, offset_t, struct as *, caddr_t, size_t,
259 259 uint_t, uint_t, uint_t, cred_t *, caller_context_t *);
260 260 static int nfs4_pageio(vnode_t *, page_t *, u_offset_t, size_t, int,
261 261 cred_t *, caller_context_t *);
262 262 static void nfs4_dispose(vnode_t *, page_t *, int, int, cred_t *,
263 263 caller_context_t *);
264 264 static int nfs4_setsecattr(vnode_t *, vsecattr_t *, int, cred_t *,
265 265 caller_context_t *);
266 266 /*
267 267 * These vnode ops are required to be called from outside this source file,
268 268 * e.g. by ephemeral mount stub vnode ops, and so may not be declared
269 269 * as static.
270 270 */
271 271 int nfs4_getattr(vnode_t *, struct vattr *, int, cred_t *,
272 272 caller_context_t *);
273 273 void nfs4_inactive(vnode_t *, cred_t *, caller_context_t *);
274 274 int nfs4_lookup(vnode_t *, char *, vnode_t **,
275 275 struct pathname *, int, vnode_t *, cred_t *,
276 276 caller_context_t *, int *, pathname_t *);
277 277 int nfs4_fid(vnode_t *, fid_t *, caller_context_t *);
278 278 int nfs4_rwlock(vnode_t *, int, caller_context_t *);
279 279 void nfs4_rwunlock(vnode_t *, int, caller_context_t *);
280 280 int nfs4_realvp(vnode_t *, vnode_t **, caller_context_t *);
281 281 int nfs4_pathconf(vnode_t *, int, ulong_t *, cred_t *,
282 282 caller_context_t *);
283 283 int nfs4_getsecattr(vnode_t *, vsecattr_t *, int, cred_t *,
284 284 caller_context_t *);
285 285 int nfs4_shrlock(vnode_t *, int, struct shrlock *, int, cred_t *,
286 286 caller_context_t *);
287 287
288 288 /*
289 289 * Used for nfs4_commit_vp() to indicate if we should
290 290 * wait on pending writes.
291 291 */
292 292 #define NFS4_WRITE_NOWAIT 0
293 293 #define NFS4_WRITE_WAIT 1
294 294
295 295 #define NFS4_BASE_WAIT_TIME 1 /* 1 second */
296 296
297 297 /*
298 298 * Error flags used to pass information about certain special errors
299 299 * which need to be handled specially.
300 300 */
301 301 #define NFS_EOF -98
302 302 #define NFS_VERF_MISMATCH -97
303 303
304 304 /*
305 305 * Flags used to differentiate between which operation drove the
306 306 * potential CLOSE OTW. (see nfs4_close_otw_if_necessary)
307 307 */
308 308 #define NFS4_CLOSE_OP 0x1
309 309 #define NFS4_DELMAP_OP 0x2
310 310 #define NFS4_INACTIVE_OP 0x3
311 311
312 312 #define ISVDEV(t) ((t == VBLK) || (t == VCHR) || (t == VFIFO))
313 313
314 314 /* ALIGN64 aligns the given buffer and adjust buffer size to 64 bit */
315 315 #define ALIGN64(x, ptr, sz) \
316 316 x = ((uintptr_t)(ptr)) & (sizeof (uint64_t) - 1); \
317 317 if (x) { \
318 318 x = sizeof (uint64_t) - (x); \
319 319 sz -= (x); \
320 320 ptr += (x); \
321 321 }
322 322
323 323 #ifdef DEBUG
324 324 int nfs4_client_attr_debug = 0;
325 325 int nfs4_client_state_debug = 0;
326 326 int nfs4_client_shadow_debug = 0;
327 327 int nfs4_client_lock_debug = 0;
328 328 int nfs4_seqid_sync = 0;
329 329 int nfs4_client_map_debug = 0;
330 330 static int nfs4_pageio_debug = 0;
331 331 int nfs4_client_inactive_debug = 0;
332 332 int nfs4_client_recov_debug = 0;
333 333 int nfs4_client_failover_debug = 0;
334 334 int nfs4_client_call_debug = 0;
335 335 int nfs4_client_lookup_debug = 0;
336 336 int nfs4_client_zone_debug = 0;
337 337 int nfs4_lost_rqst_debug = 0;
338 338 int nfs4_rdattrerr_debug = 0;
339 339 int nfs4_open_stream_debug = 0;
340 340
341 341 int nfs4read_error_inject;
342 342
343 343 static int nfs4_create_misses = 0;
344 344
345 345 static int nfs4_readdir_cache_shorts = 0;
346 346 static int nfs4_readdir_readahead = 0;
347 347
348 348 static int nfs4_bio_do_stop = 0;
349 349
350 350 static int nfs4_lostpage = 0; /* number of times we lost original page */
351 351
352 352 int nfs4_mmap_debug = 0;
353 353
354 354 static int nfs4_pathconf_cache_hits = 0;
355 355 static int nfs4_pathconf_cache_misses = 0;
356 356
357 357 int nfs4close_all_cnt;
358 358 int nfs4close_one_debug = 0;
359 359 int nfs4close_notw_debug = 0;
360 360
361 361 int denied_to_flk_debug = 0;
362 362 void *lockt_denied_debug;
363 363
364 364 #endif
365 365
366 366 /*
367 367 * How long to wait before trying again if OPEN_CONFIRM gets ETIMEDOUT
368 368 * or NFS4ERR_RESOURCE.
369 369 */
370 370 static int confirm_retry_sec = 30;
371 371
372 372 static int nfs4_lookup_neg_cache = 1;
373 373
374 374 /*
375 375 * number of pages to read ahead
376 376 * optimized for 100 base-T.
377 377 */
378 378 static int nfs4_nra = 4;
379 379
380 380 static int nfs4_do_symlink_cache = 1;
381 381
382 382 static int nfs4_pathconf_disable_cache = 0;
383 383
384 384 /*
385 385 * These are the vnode ops routines which implement the vnode interface to
386 386 * the networked file system. These routines just take their parameters,
387 387 * make them look networkish by putting the right info into interface structs,
388 388 * and then calling the appropriate remote routine(s) to do the work.
389 389 *
390 390 * Note on directory name lookup cacheing: If we detect a stale fhandle,
391 391 * we purge the directory cache relative to that vnode. This way, the
392 392 * user won't get burned by the cache repeatedly. See <nfs/rnode4.h> for
393 393 * more details on rnode locking.
394 394 */
395 395
396 396 struct vnodeops *nfs4_vnodeops;
397 397
398 398 const fs_operation_def_t nfs4_vnodeops_template[] = {
399 399 VOPNAME_OPEN, { .vop_open = nfs4_open },
400 400 VOPNAME_CLOSE, { .vop_close = nfs4_close },
401 401 VOPNAME_READ, { .vop_read = nfs4_read },
402 402 VOPNAME_WRITE, { .vop_write = nfs4_write },
403 403 VOPNAME_IOCTL, { .vop_ioctl = nfs4_ioctl },
404 404 VOPNAME_GETATTR, { .vop_getattr = nfs4_getattr },
405 405 VOPNAME_SETATTR, { .vop_setattr = nfs4_setattr },
406 406 VOPNAME_ACCESS, { .vop_access = nfs4_access },
407 407 VOPNAME_LOOKUP, { .vop_lookup = nfs4_lookup },
408 408 VOPNAME_CREATE, { .vop_create = nfs4_create },
409 409 VOPNAME_REMOVE, { .vop_remove = nfs4_remove },
410 410 VOPNAME_LINK, { .vop_link = nfs4_link },
411 411 VOPNAME_RENAME, { .vop_rename = nfs4_rename },
412 412 VOPNAME_MKDIR, { .vop_mkdir = nfs4_mkdir },
413 413 VOPNAME_RMDIR, { .vop_rmdir = nfs4_rmdir },
414 414 VOPNAME_READDIR, { .vop_readdir = nfs4_readdir },
415 415 VOPNAME_SYMLINK, { .vop_symlink = nfs4_symlink },
416 416 VOPNAME_READLINK, { .vop_readlink = nfs4_readlink },
417 417 VOPNAME_FSYNC, { .vop_fsync = nfs4_fsync },
418 418 VOPNAME_INACTIVE, { .vop_inactive = nfs4_inactive },
419 419 VOPNAME_FID, { .vop_fid = nfs4_fid },
420 420 VOPNAME_RWLOCK, { .vop_rwlock = nfs4_rwlock },
421 421 VOPNAME_RWUNLOCK, { .vop_rwunlock = nfs4_rwunlock },
422 422 VOPNAME_SEEK, { .vop_seek = nfs4_seek },
423 423 VOPNAME_FRLOCK, { .vop_frlock = nfs4_frlock },
424 424 VOPNAME_SPACE, { .vop_space = nfs4_space },
425 425 VOPNAME_REALVP, { .vop_realvp = nfs4_realvp },
426 426 VOPNAME_GETPAGE, { .vop_getpage = nfs4_getpage },
427 427 VOPNAME_PUTPAGE, { .vop_putpage = nfs4_putpage },
428 428 VOPNAME_MAP, { .vop_map = nfs4_map },
429 429 VOPNAME_ADDMAP, { .vop_addmap = nfs4_addmap },
430 430 VOPNAME_DELMAP, { .vop_delmap = nfs4_delmap },
431 431 /* no separate nfs4_dump */
432 432 VOPNAME_DUMP, { .vop_dump = nfs_dump },
433 433 VOPNAME_PATHCONF, { .vop_pathconf = nfs4_pathconf },
434 434 VOPNAME_PAGEIO, { .vop_pageio = nfs4_pageio },
435 435 VOPNAME_DISPOSE, { .vop_dispose = nfs4_dispose },
436 436 VOPNAME_SETSECATTR, { .vop_setsecattr = nfs4_setsecattr },
437 437 VOPNAME_GETSECATTR, { .vop_getsecattr = nfs4_getsecattr },
438 438 VOPNAME_SHRLOCK, { .vop_shrlock = nfs4_shrlock },
439 439 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
440 440 NULL, NULL
441 441 };
442 442
443 443 /*
444 444 * The following are subroutines and definitions to set args or get res
445 445 * for the different nfsv4 ops
446 446 */
447 447
448 448 void
449 449 nfs4args_lookup_free(nfs_argop4 *argop, int arglen)
450 450 {
451 451 int i;
452 452
453 453 for (i = 0; i < arglen; i++) {
454 454 if (argop[i].argop == OP_LOOKUP) {
455 455 kmem_free(
456 456 argop[i].nfs_argop4_u.oplookup.
457 457 objname.utf8string_val,
458 458 argop[i].nfs_argop4_u.oplookup.
459 459 objname.utf8string_len);
460 460 }
461 461 }
462 462 }
463 463
464 464 static void
465 465 nfs4args_lock_free(nfs_argop4 *argop)
466 466 {
467 467 locker4 *locker = &argop->nfs_argop4_u.oplock.locker;
468 468
469 469 if (locker->new_lock_owner == TRUE) {
470 470 open_to_lock_owner4 *open_owner;
471 471
472 472 open_owner = &locker->locker4_u.open_owner;
473 473 if (open_owner->lock_owner.owner_val != NULL) {
474 474 kmem_free(open_owner->lock_owner.owner_val,
475 475 open_owner->lock_owner.owner_len);
476 476 }
477 477 }
478 478 }
479 479
480 480 static void
481 481 nfs4args_lockt_free(nfs_argop4 *argop)
482 482 {
483 483 lock_owner4 *lowner = &argop->nfs_argop4_u.oplockt.owner;
484 484
485 485 if (lowner->owner_val != NULL) {
486 486 kmem_free(lowner->owner_val, lowner->owner_len);
487 487 }
488 488 }
489 489
490 490 static void
491 491 nfs4args_setattr(nfs_argop4 *argop, vattr_t *vap, vsecattr_t *vsap, int flags,
492 492 rnode4_t *rp, cred_t *cr, bitmap4 supp, int *error,
493 493 nfs4_stateid_types_t *sid_types)
494 494 {
495 495 fattr4 *attr = &argop->nfs_argop4_u.opsetattr.obj_attributes;
496 496 mntinfo4_t *mi;
497 497
498 498 argop->argop = OP_SETATTR;
499 499 /*
500 500 * The stateid is set to 0 if client is not modifying the size
501 501 * and otherwise to whatever nfs4_get_stateid() returns.
502 502 *
503 503 * XXX Note: nfs4_get_stateid() returns 0 if no lockowner and/or no
504 504 * state struct could be found for the process/file pair. We may
505 505 * want to change this in the future (by OPENing the file). See
506 506 * bug # 4474852.
507 507 */
508 508 if (vap->va_mask & AT_SIZE) {
509 509
510 510 ASSERT(rp != NULL);
511 511 mi = VTOMI4(RTOV4(rp));
512 512
513 513 argop->nfs_argop4_u.opsetattr.stateid =
514 514 nfs4_get_stateid(cr, rp, curproc->p_pidp->pid_id, mi,
515 515 OP_SETATTR, sid_types, FALSE);
516 516 } else {
517 517 bzero(&argop->nfs_argop4_u.opsetattr.stateid,
518 518 sizeof (stateid4));
519 519 }
520 520
521 521 *error = vattr_to_fattr4(vap, vsap, attr, flags, OP_SETATTR, supp);
522 522 if (*error)
523 523 bzero(attr, sizeof (*attr));
524 524 }
525 525
526 526 static void
527 527 nfs4args_setattr_free(nfs_argop4 *argop)
528 528 {
529 529 nfs4_fattr4_free(&argop->nfs_argop4_u.opsetattr.obj_attributes);
530 530 }
531 531
532 532 static int
533 533 nfs4args_verify(nfs_argop4 *argop, vattr_t *vap, enum nfs_opnum4 op,
534 534 bitmap4 supp)
535 535 {
536 536 fattr4 *attr;
537 537 int error = 0;
538 538
539 539 argop->argop = op;
540 540 switch (op) {
541 541 case OP_VERIFY:
542 542 attr = &argop->nfs_argop4_u.opverify.obj_attributes;
543 543 break;
544 544 case OP_NVERIFY:
545 545 attr = &argop->nfs_argop4_u.opnverify.obj_attributes;
546 546 break;
547 547 default:
548 548 return (EINVAL);
549 549 }
550 550 if (!error)
551 551 error = vattr_to_fattr4(vap, NULL, attr, 0, op, supp);
552 552 if (error)
553 553 bzero(attr, sizeof (*attr));
554 554 return (error);
555 555 }
556 556
557 557 static void
558 558 nfs4args_verify_free(nfs_argop4 *argop)
559 559 {
560 560 switch (argop->argop) {
561 561 case OP_VERIFY:
562 562 nfs4_fattr4_free(&argop->nfs_argop4_u.opverify.obj_attributes);
563 563 break;
564 564 case OP_NVERIFY:
565 565 nfs4_fattr4_free(&argop->nfs_argop4_u.opnverify.obj_attributes);
566 566 break;
567 567 default:
568 568 break;
569 569 }
570 570 }
571 571
572 572 static void
573 573 nfs4args_write(nfs_argop4 *argop, stable_how4 stable, rnode4_t *rp, cred_t *cr,
574 574 WRITE4args **wargs_pp, nfs4_stateid_types_t *sid_tp)
575 575 {
576 576 WRITE4args *wargs = &argop->nfs_argop4_u.opwrite;
577 577 mntinfo4_t *mi = VTOMI4(RTOV4(rp));
578 578
579 579 argop->argop = OP_WRITE;
580 580 wargs->stable = stable;
581 581 wargs->stateid = nfs4_get_w_stateid(cr, rp, curproc->p_pidp->pid_id,
582 582 mi, OP_WRITE, sid_tp);
583 583 wargs->mblk = NULL;
584 584 *wargs_pp = wargs;
585 585 }
586 586
587 587 void
588 588 nfs4args_copen_free(OPEN4cargs *open_args)
589 589 {
590 590 if (open_args->owner.owner_val) {
591 591 kmem_free(open_args->owner.owner_val,
592 592 open_args->owner.owner_len);
593 593 }
594 594 if ((open_args->opentype == OPEN4_CREATE) &&
595 595 (open_args->mode != EXCLUSIVE4)) {
596 596 nfs4_fattr4_free(&open_args->createhow4_u.createattrs);
597 597 }
598 598 }
599 599
600 600 /*
601 601 * XXX: This is referenced in modstubs.s
602 602 */
603 603 struct vnodeops *
604 604 nfs4_getvnodeops(void)
605 605 {
606 606 return (nfs4_vnodeops);
607 607 }
608 608
609 609 /*
610 610 * The OPEN operation opens a regular file.
611 611 */
612 612 /*ARGSUSED3*/
613 613 static int
614 614 nfs4_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
615 615 {
616 616 vnode_t *dvp = NULL;
617 617 rnode4_t *rp, *drp;
618 618 int error;
619 619 int just_been_created;
620 620 char fn[MAXNAMELEN];
621 621
622 622 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4_open: "));
623 623 if (nfs_zone() != VTOMI4(*vpp)->mi_zone)
624 624 return (EIO);
625 625 rp = VTOR4(*vpp);
626 626
627 627 /*
628 628 * Check to see if opening something besides a regular file;
629 629 * if so skip the OTW call
630 630 */
631 631 if ((*vpp)->v_type != VREG) {
632 632 error = nfs4_open_non_reg_file(vpp, flag, cr);
633 633 return (error);
634 634 }
635 635
636 636 /*
637 637 * XXX - would like a check right here to know if the file is
638 638 * executable or not, so as to skip OTW
639 639 */
640 640
641 641 if ((error = vtodv(*vpp, &dvp, cr, TRUE)) != 0)
642 642 return (error);
643 643
644 644 drp = VTOR4(dvp);
645 645 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp)))
646 646 return (EINTR);
647 647
648 648 if ((error = vtoname(*vpp, fn, MAXNAMELEN)) != 0) {
649 649 nfs_rw_exit(&drp->r_rwlock);
650 650 return (error);
651 651 }
652 652
653 653 /*
654 654 * See if this file has just been CREATEd.
655 655 * If so, clear the flag and update the dnlc, which was previously
656 656 * skipped in nfs4_create.
657 657 * XXX need better serilization on this.
658 658 * XXX move this into the nf4open_otw call, after we have
659 659 * XXX acquired the open owner seqid sync.
660 660 */
661 661 mutex_enter(&rp->r_statev4_lock);
662 662 if (rp->created_v4) {
663 663 rp->created_v4 = 0;
664 664 mutex_exit(&rp->r_statev4_lock);
665 665
666 666 dnlc_update(dvp, fn, *vpp);
667 667 /* This is needed so we don't bump the open ref count */
668 668 just_been_created = 1;
669 669 } else {
670 670 mutex_exit(&rp->r_statev4_lock);
671 671 just_been_created = 0;
672 672 }
673 673
674 674 /*
675 675 * If caller specified O_TRUNC/FTRUNC, then be sure to set
676 676 * FWRITE (to drive successful setattr(size=0) after open)
677 677 */
678 678 if (flag & FTRUNC)
679 679 flag |= FWRITE;
680 680
681 681 error = nfs4open_otw(dvp, fn, NULL, vpp, cr, 0, flag, 0,
682 682 just_been_created);
683 683
684 684 if (!error && !((*vpp)->v_flag & VROOT))
685 685 dnlc_update(dvp, fn, *vpp);
686 686
687 687 nfs_rw_exit(&drp->r_rwlock);
688 688
689 689 /* release the hold from vtodv */
690 690 VN_RELE(dvp);
691 691
692 692 /* exchange the shadow for the master vnode, if needed */
693 693
694 694 if (error == 0 && IS_SHADOW(*vpp, rp))
695 695 sv_exchange(vpp);
696 696
697 697 return (error);
698 698 }
699 699
700 700 /*
701 701 * See if there's a "lost open" request to be saved and recovered.
702 702 */
703 703 static void
704 704 nfs4open_save_lost_rqst(int error, nfs4_lost_rqst_t *lost_rqstp,
705 705 nfs4_open_owner_t *oop, cred_t *cr, vnode_t *vp,
706 706 vnode_t *dvp, OPEN4cargs *open_args)
707 707 {
708 708 vfs_t *vfsp;
709 709 char *srccfp;
710 710
711 711 vfsp = (dvp ? dvp->v_vfsp : vp->v_vfsp);
712 712
713 713 if (error != ETIMEDOUT && error != EINTR &&
714 714 !NFS4_FRC_UNMT_ERR(error, vfsp)) {
715 715 lost_rqstp->lr_op = 0;
716 716 return;
717 717 }
718 718
719 719 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
720 720 "nfs4open_save_lost_rqst: error %d", error));
721 721
722 722 lost_rqstp->lr_op = OP_OPEN;
723 723
724 724 /*
725 725 * The vp (if it is not NULL) and dvp are held and rele'd via
726 726 * the recovery code. See nfs4_save_lost_rqst.
727 727 */
728 728 lost_rqstp->lr_vp = vp;
729 729 lost_rqstp->lr_dvp = dvp;
730 730 lost_rqstp->lr_oop = oop;
731 731 lost_rqstp->lr_osp = NULL;
732 732 lost_rqstp->lr_lop = NULL;
733 733 lost_rqstp->lr_cr = cr;
734 734 lost_rqstp->lr_flk = NULL;
735 735 lost_rqstp->lr_oacc = open_args->share_access;
736 736 lost_rqstp->lr_odeny = open_args->share_deny;
737 737 lost_rqstp->lr_oclaim = open_args->claim;
738 738 if (open_args->claim == CLAIM_DELEGATE_CUR) {
739 739 lost_rqstp->lr_ostateid =
740 740 open_args->open_claim4_u.delegate_cur_info.delegate_stateid;
741 741 srccfp = open_args->open_claim4_u.delegate_cur_info.cfile;
742 742 } else {
743 743 srccfp = open_args->open_claim4_u.cfile;
744 744 }
745 745 lost_rqstp->lr_ofile.utf8string_len = 0;
746 746 lost_rqstp->lr_ofile.utf8string_val = NULL;
747 747 (void) str_to_utf8(srccfp, &lost_rqstp->lr_ofile);
748 748 lost_rqstp->lr_putfirst = FALSE;
749 749 }
750 750
751 751 struct nfs4_excl_time {
752 752 uint32 seconds;
753 753 uint32 nseconds;
754 754 };
755 755
756 756 /*
757 757 * The OPEN operation creates and/or opens a regular file
758 758 *
759 759 * ARGSUSED
760 760 */
761 761 static int
762 762 nfs4open_otw(vnode_t *dvp, char *file_name, struct vattr *in_va,
763 763 vnode_t **vpp, cred_t *cr, int create_flag, int open_flag,
764 764 enum createmode4 createmode, int file_just_been_created)
765 765 {
766 766 rnode4_t *rp;
767 767 rnode4_t *drp = VTOR4(dvp);
768 768 vnode_t *vp = NULL;
769 769 vnode_t *vpi = *vpp;
770 770 bool_t needrecov = FALSE;
771 771
772 772 int doqueue = 1;
773 773
774 774 COMPOUND4args_clnt args;
775 775 COMPOUND4res_clnt res;
776 776 nfs_argop4 *argop;
777 777 nfs_resop4 *resop;
778 778 int argoplist_size;
779 779 int idx_open, idx_fattr;
780 780
781 781 GETFH4res *gf_res = NULL;
782 782 OPEN4res *op_res = NULL;
783 783 nfs4_ga_res_t *garp;
784 784 fattr4 *attr = NULL;
785 785 struct nfs4_excl_time verf;
786 786 bool_t did_excl_setup = FALSE;
787 787 int created_osp;
788 788
789 789 OPEN4cargs *open_args;
790 790 nfs4_open_owner_t *oop = NULL;
791 791 nfs4_open_stream_t *osp = NULL;
792 792 seqid4 seqid = 0;
793 793 bool_t retry_open = FALSE;
794 794 nfs4_recov_state_t recov_state;
795 795 nfs4_lost_rqst_t lost_rqst;
796 796 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
797 797 hrtime_t t;
798 798 int acc = 0;
799 799 cred_t *cred_otw = NULL; /* cred used to do the RPC call */
800 800 cred_t *ncr = NULL;
801 801
802 802 nfs4_sharedfh_t *otw_sfh;
803 803 nfs4_sharedfh_t *orig_sfh;
804 804 int fh_differs = 0;
805 805 int numops, setgid_flag;
806 806 int num_bseqid_retry = NFS4_NUM_RETRY_BAD_SEQID + 1;
807 807
808 808 /*
809 809 * Make sure we properly deal with setting the right gid on
810 810 * a newly created file to reflect the parent's setgid bit
811 811 */
812 812 setgid_flag = 0;
813 813 if (create_flag && in_va) {
814 814
815 815 /*
816 816 * If there is grpid mount flag used or
817 817 * the parent's directory has the setgid bit set
818 818 * _and_ the client was able to get a valid mapping
819 819 * for the parent dir's owner_group, we want to
820 820 * append NVERIFY(owner_group == dva.va_gid) and
821 821 * SETATTR to the CREATE compound.
822 822 */
823 823 mutex_enter(&drp->r_statelock);
824 824 if ((VTOMI4(dvp)->mi_flags & MI4_GRPID ||
825 825 drp->r_attr.va_mode & VSGID) &&
826 826 drp->r_attr.va_gid != GID_NOBODY) {
827 827 in_va->va_mask |= AT_GID;
828 828 in_va->va_gid = drp->r_attr.va_gid;
829 829 setgid_flag = 1;
830 830 }
831 831 mutex_exit(&drp->r_statelock);
832 832 }
833 833
834 834 /*
835 835 * Normal/non-create compound:
836 836 * PUTFH(dfh) + OPEN(create) + GETFH + GETATTR(new)
837 837 *
838 838 * Open(create) compound no setgid:
839 839 * PUTFH(dfh) + SAVEFH + OPEN(create) + GETFH + GETATTR(new) +
840 840 * RESTOREFH + GETATTR
841 841 *
842 842 * Open(create) setgid:
843 843 * PUTFH(dfh) + OPEN(create) + GETFH + GETATTR(new) +
844 844 * SAVEFH + PUTFH(dfh) + GETATTR(dvp) + RESTOREFH +
845 845 * NVERIFY(grp) + SETATTR
846 846 */
847 847 if (setgid_flag) {
848 848 numops = 10;
849 849 idx_open = 1;
850 850 idx_fattr = 3;
851 851 } else if (create_flag) {
852 852 numops = 7;
853 853 idx_open = 2;
854 854 idx_fattr = 4;
855 855 } else {
856 856 numops = 4;
857 857 idx_open = 1;
858 858 idx_fattr = 3;
859 859 }
860 860
861 861 args.array_len = numops;
862 862 argoplist_size = numops * sizeof (nfs_argop4);
863 863 argop = kmem_alloc(argoplist_size, KM_SLEEP);
864 864
865 865 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4open_otw: "
866 866 "open %s open flag 0x%x cred %p", file_name, open_flag,
867 867 (void *)cr));
868 868
869 869 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
870 870 if (create_flag) {
871 871 /*
872 872 * We are to create a file. Initialize the passed in vnode
873 873 * pointer.
874 874 */
875 875 vpi = NULL;
876 876 } else {
877 877 /*
878 878 * Check to see if the client owns a read delegation and is
879 879 * trying to open for write. If so, then return the delegation
880 880 * to avoid the server doing a cb_recall and returning DELAY.
881 881 * NB - we don't use the statev4_lock here because we'd have
882 882 * to drop the lock anyway and the result would be stale.
883 883 */
884 884 if ((open_flag & FWRITE) &&
885 885 VTOR4(vpi)->r_deleg_type == OPEN_DELEGATE_READ)
886 886 (void) nfs4delegreturn(VTOR4(vpi), NFS4_DR_REOPEN);
887 887
888 888 /*
889 889 * If the file has a delegation, then do an access check up
890 890 * front. This avoids having to an access check later after
891 891 * we've already done start_op, which could deadlock.
892 892 */
893 893 if (VTOR4(vpi)->r_deleg_type != OPEN_DELEGATE_NONE) {
894 894 if (open_flag & FREAD &&
895 895 nfs4_access(vpi, VREAD, 0, cr, NULL) == 0)
896 896 acc |= VREAD;
897 897 if (open_flag & FWRITE &&
898 898 nfs4_access(vpi, VWRITE, 0, cr, NULL) == 0)
899 899 acc |= VWRITE;
900 900 }
901 901 }
902 902
903 903 drp = VTOR4(dvp);
904 904
905 905 recov_state.rs_flags = 0;
906 906 recov_state.rs_num_retry_despite_err = 0;
907 907 cred_otw = cr;
908 908
909 909 recov_retry:
910 910 fh_differs = 0;
911 911 nfs4_error_zinit(&e);
912 912
913 913 e.error = nfs4_start_op(VTOMI4(dvp), dvp, vpi, &recov_state);
914 914 if (e.error) {
915 915 if (ncr != NULL)
916 916 crfree(ncr);
917 917 kmem_free(argop, argoplist_size);
918 918 return (e.error);
919 919 }
920 920
921 921 args.ctag = TAG_OPEN;
922 922 args.array_len = numops;
923 923 args.array = argop;
924 924
925 925 /* putfh directory fh */
926 926 argop[0].argop = OP_CPUTFH;
927 927 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
928 928
929 929 /* OPEN: either op 1 or op 2 depending upon create/setgid flags */
930 930 argop[idx_open].argop = OP_COPEN;
931 931 open_args = &argop[idx_open].nfs_argop4_u.opcopen;
932 932 open_args->claim = CLAIM_NULL;
933 933
934 934 /* name of file */
935 935 open_args->open_claim4_u.cfile = file_name;
936 936 open_args->owner.owner_len = 0;
937 937 open_args->owner.owner_val = NULL;
938 938
939 939 if (create_flag) {
940 940 /* CREATE a file */
941 941 open_args->opentype = OPEN4_CREATE;
942 942 open_args->mode = createmode;
943 943 if (createmode == EXCLUSIVE4) {
944 944 if (did_excl_setup == FALSE) {
945 945 verf.seconds = zone_get_hostid(NULL);
946 946 if (verf.seconds != 0)
947 947 verf.nseconds = newnum();
948 948 else {
949 949 timestruc_t now;
950 950
951 951 gethrestime(&now);
952 952 verf.seconds = now.tv_sec;
953 953 verf.nseconds = now.tv_nsec;
954 954 }
955 955 /*
956 956 * Since the server will use this value for the
957 957 * mtime, make sure that it can't overflow. Zero
958 958 * out the MSB. The actual value does not matter
959 959 * here, only its uniqeness.
960 960 */
961 961 verf.seconds &= INT32_MAX;
962 962 did_excl_setup = TRUE;
963 963 }
964 964
965 965 /* Now copy over verifier to OPEN4args. */
966 966 open_args->createhow4_u.createverf = *(uint64_t *)&verf;
967 967 } else {
968 968 int v_error;
969 969 bitmap4 supp_attrs;
970 970 servinfo4_t *svp;
971 971
972 972 attr = &open_args->createhow4_u.createattrs;
973 973
974 974 svp = drp->r_server;
975 975 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
976 976 supp_attrs = svp->sv_supp_attrs;
977 977 nfs_rw_exit(&svp->sv_lock);
978 978
979 979 /* GUARDED4 or UNCHECKED4 */
980 980 v_error = vattr_to_fattr4(in_va, NULL, attr, 0, OP_OPEN,
981 981 supp_attrs);
982 982 if (v_error) {
983 983 bzero(attr, sizeof (*attr));
984 984 nfs4args_copen_free(open_args);
985 985 nfs4_end_op(VTOMI4(dvp), dvp, vpi,
986 986 &recov_state, FALSE);
987 987 if (ncr != NULL)
988 988 crfree(ncr);
989 989 kmem_free(argop, argoplist_size);
990 990 return (v_error);
991 991 }
992 992 }
993 993 } else {
994 994 /* NO CREATE */
995 995 open_args->opentype = OPEN4_NOCREATE;
996 996 }
997 997
998 998 if (recov_state.rs_sp != NULL) {
999 999 mutex_enter(&recov_state.rs_sp->s_lock);
1000 1000 open_args->owner.clientid = recov_state.rs_sp->clientid;
1001 1001 mutex_exit(&recov_state.rs_sp->s_lock);
1002 1002 } else {
1003 1003 /* XXX should we just fail here? */
1004 1004 open_args->owner.clientid = 0;
1005 1005 }
1006 1006
1007 1007 /*
1008 1008 * This increments oop's ref count or creates a temporary 'just_created'
1009 1009 * open owner that will become valid when this OPEN/OPEN_CONFIRM call
1010 1010 * completes.
1011 1011 */
1012 1012 mutex_enter(&VTOMI4(dvp)->mi_lock);
1013 1013
1014 1014 /* See if a permanent or just created open owner exists */
1015 1015 oop = find_open_owner_nolock(cr, NFS4_JUST_CREATED, VTOMI4(dvp));
1016 1016 if (!oop) {
1017 1017 /*
1018 1018 * This open owner does not exist so create a temporary
1019 1019 * just created one.
1020 1020 */
1021 1021 oop = create_open_owner(cr, VTOMI4(dvp));
1022 1022 ASSERT(oop != NULL);
1023 1023 }
1024 1024 mutex_exit(&VTOMI4(dvp)->mi_lock);
1025 1025
1026 1026 /* this length never changes, do alloc before seqid sync */
1027 1027 open_args->owner.owner_len = sizeof (oop->oo_name);
1028 1028 open_args->owner.owner_val =
1029 1029 kmem_alloc(open_args->owner.owner_len, KM_SLEEP);
1030 1030
1031 1031 e.error = nfs4_start_open_seqid_sync(oop, VTOMI4(dvp));
1032 1032 if (e.error == EAGAIN) {
1033 1033 open_owner_rele(oop);
1034 1034 nfs4args_copen_free(open_args);
1035 1035 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, TRUE);
1036 1036 if (ncr != NULL) {
1037 1037 crfree(ncr);
1038 1038 ncr = NULL;
1039 1039 }
1040 1040 goto recov_retry;
1041 1041 }
1042 1042
1043 1043 /* Check to see if we need to do the OTW call */
1044 1044 if (!create_flag) {
1045 1045 if (!nfs4_is_otw_open_necessary(oop, open_flag, vpi,
1046 1046 file_just_been_created, &e.error, acc, &recov_state)) {
1047 1047
1048 1048 /*
1049 1049 * The OTW open is not necessary. Either
1050 1050 * the open can succeed without it (eg.
1051 1051 * delegation, error == 0) or the open
1052 1052 * must fail due to an access failure
1053 1053 * (error != 0). In either case, tidy
1054 1054 * up and return.
1055 1055 */
1056 1056
1057 1057 nfs4_end_open_seqid_sync(oop);
1058 1058 open_owner_rele(oop);
1059 1059 nfs4args_copen_free(open_args);
1060 1060 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, FALSE);
1061 1061 if (ncr != NULL)
1062 1062 crfree(ncr);
1063 1063 kmem_free(argop, argoplist_size);
1064 1064 return (e.error);
1065 1065 }
1066 1066 }
1067 1067
1068 1068 bcopy(&oop->oo_name, open_args->owner.owner_val,
1069 1069 open_args->owner.owner_len);
1070 1070
1071 1071 seqid = nfs4_get_open_seqid(oop) + 1;
1072 1072 open_args->seqid = seqid;
1073 1073 open_args->share_access = 0;
1074 1074 if (open_flag & FREAD)
1075 1075 open_args->share_access |= OPEN4_SHARE_ACCESS_READ;
1076 1076 if (open_flag & FWRITE)
1077 1077 open_args->share_access |= OPEN4_SHARE_ACCESS_WRITE;
1078 1078 open_args->share_deny = OPEN4_SHARE_DENY_NONE;
1079 1079
1080 1080
1081 1081
1082 1082 /*
1083 1083 * getfh w/sanity check for idx_open/idx_fattr
1084 1084 */
1085 1085 ASSERT((idx_open + 1) == (idx_fattr - 1));
1086 1086 argop[idx_open + 1].argop = OP_GETFH;
1087 1087
1088 1088 /* getattr */
1089 1089 argop[idx_fattr].argop = OP_GETATTR;
1090 1090 argop[idx_fattr].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
1091 1091 argop[idx_fattr].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
1092 1092
1093 1093 if (setgid_flag) {
1094 1094 vattr_t _v;
1095 1095 servinfo4_t *svp;
1096 1096 bitmap4 supp_attrs;
1097 1097
1098 1098 svp = drp->r_server;
1099 1099 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
1100 1100 supp_attrs = svp->sv_supp_attrs;
1101 1101 nfs_rw_exit(&svp->sv_lock);
1102 1102
1103 1103 /*
1104 1104 * For setgid case, we need to:
1105 1105 * 4:savefh(new) 5:putfh(dir) 6:getattr(dir) 7:restorefh(new)
1106 1106 */
1107 1107 argop[4].argop = OP_SAVEFH;
1108 1108
1109 1109 argop[5].argop = OP_CPUTFH;
1110 1110 argop[5].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
1111 1111
1112 1112 argop[6].argop = OP_GETATTR;
1113 1113 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
1114 1114 argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
1115 1115
1116 1116 argop[7].argop = OP_RESTOREFH;
1117 1117
1118 1118 /*
1119 1119 * nverify
1120 1120 */
1121 1121 _v.va_mask = AT_GID;
1122 1122 _v.va_gid = in_va->va_gid;
1123 1123 if (!(e.error = nfs4args_verify(&argop[8], &_v, OP_NVERIFY,
1124 1124 supp_attrs))) {
1125 1125
1126 1126 /*
1127 1127 * setattr
1128 1128 *
1129 1129 * We _know_ we're not messing with AT_SIZE or
1130 1130 * AT_XTIME, so no need for stateid or flags.
1131 1131 * Also we specify NULL rp since we're only
1132 1132 * interested in setting owner_group attributes.
1133 1133 */
1134 1134 nfs4args_setattr(&argop[9], &_v, NULL, 0, NULL, cr,
1135 1135 supp_attrs, &e.error, 0);
1136 1136 if (e.error)
1137 1137 nfs4args_verify_free(&argop[8]);
1138 1138 }
1139 1139
1140 1140 if (e.error) {
1141 1141 /*
1142 1142 * XXX - Revisit the last argument to nfs4_end_op()
1143 1143 * once 5020486 is fixed.
1144 1144 */
1145 1145 nfs4_end_open_seqid_sync(oop);
1146 1146 open_owner_rele(oop);
1147 1147 nfs4args_copen_free(open_args);
1148 1148 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, TRUE);
1149 1149 if (ncr != NULL)
1150 1150 crfree(ncr);
1151 1151 kmem_free(argop, argoplist_size);
1152 1152 return (e.error);
1153 1153 }
1154 1154 } else if (create_flag) {
1155 1155 argop[1].argop = OP_SAVEFH;
1156 1156
1157 1157 argop[5].argop = OP_RESTOREFH;
1158 1158
1159 1159 argop[6].argop = OP_GETATTR;
1160 1160 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
1161 1161 argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
1162 1162 }
1163 1163
1164 1164 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE,
1165 1165 "nfs4open_otw: %s call, nm %s, rp %s",
1166 1166 needrecov ? "recov" : "first", file_name,
1167 1167 rnode4info(VTOR4(dvp))));
1168 1168
1169 1169 t = gethrtime();
1170 1170
1171 1171 rfs4call(VTOMI4(dvp), &args, &res, cred_otw, &doqueue, 0, &e);
1172 1172
1173 1173 if (!e.error && nfs4_need_to_bump_seqid(&res))
1174 1174 nfs4_set_open_seqid(seqid, oop, args.ctag);
1175 1175
1176 1176 needrecov = nfs4_needs_recovery(&e, TRUE, dvp->v_vfsp);
1177 1177
1178 1178 if (e.error || needrecov) {
1179 1179 bool_t abort = FALSE;
1180 1180
1181 1181 if (needrecov) {
1182 1182 nfs4_bseqid_entry_t *bsep = NULL;
1183 1183
1184 1184 nfs4open_save_lost_rqst(e.error, &lost_rqst, oop,
1185 1185 cred_otw, vpi, dvp, open_args);
1186 1186
1187 1187 if (!e.error && res.status == NFS4ERR_BAD_SEQID) {
1188 1188 bsep = nfs4_create_bseqid_entry(oop, NULL,
1189 1189 vpi, 0, args.ctag, open_args->seqid);
1190 1190 num_bseqid_retry--;
1191 1191 }
1192 1192
1193 1193 abort = nfs4_start_recovery(&e, VTOMI4(dvp), dvp, vpi,
1194 1194 NULL, lost_rqst.lr_op == OP_OPEN ?
1195 1195 &lost_rqst : NULL, OP_OPEN, bsep, NULL, NULL);
1196 1196
1197 1197 if (bsep)
1198 1198 kmem_free(bsep, sizeof (*bsep));
1199 1199 /* give up if we keep getting BAD_SEQID */
1200 1200 if (num_bseqid_retry == 0)
1201 1201 abort = TRUE;
1202 1202 if (abort == TRUE && e.error == 0)
1203 1203 e.error = geterrno4(res.status);
1204 1204 }
1205 1205 nfs4_end_open_seqid_sync(oop);
1206 1206 open_owner_rele(oop);
1207 1207 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov);
1208 1208 nfs4args_copen_free(open_args);
1209 1209 if (setgid_flag) {
1210 1210 nfs4args_verify_free(&argop[8]);
1211 1211 nfs4args_setattr_free(&argop[9]);
1212 1212 }
1213 1213 if (!e.error)
1214 1214 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1215 1215 if (ncr != NULL) {
1216 1216 crfree(ncr);
1217 1217 ncr = NULL;
1218 1218 }
1219 1219 if (!needrecov || abort == TRUE || e.error == EINTR ||
1220 1220 NFS4_FRC_UNMT_ERR(e.error, dvp->v_vfsp)) {
1221 1221 kmem_free(argop, argoplist_size);
1222 1222 return (e.error);
1223 1223 }
1224 1224 goto recov_retry;
1225 1225 }
1226 1226
1227 1227 /*
1228 1228 * Will check and update lease after checking the rflag for
1229 1229 * OPEN_CONFIRM in the successful OPEN call.
1230 1230 */
1231 1231 if (res.status != NFS4_OK && res.array_len <= idx_fattr + 1) {
1232 1232
1233 1233 /*
1234 1234 * XXX what if we're crossing mount points from server1:/drp
1235 1235 * to server2:/drp/rp.
1236 1236 */
1237 1237
1238 1238 /* Signal our end of use of the open seqid */
1239 1239 nfs4_end_open_seqid_sync(oop);
1240 1240
1241 1241 /*
1242 1242 * This will destroy the open owner if it was just created,
1243 1243 * and no one else has put a reference on it.
1244 1244 */
1245 1245 open_owner_rele(oop);
1246 1246 if (create_flag && (createmode != EXCLUSIVE4) &&
1247 1247 res.status == NFS4ERR_BADOWNER)
1248 1248 nfs4_log_badowner(VTOMI4(dvp), OP_OPEN);
1249 1249
1250 1250 e.error = geterrno4(res.status);
1251 1251 nfs4args_copen_free(open_args);
1252 1252 if (setgid_flag) {
1253 1253 nfs4args_verify_free(&argop[8]);
1254 1254 nfs4args_setattr_free(&argop[9]);
1255 1255 }
1256 1256 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1257 1257 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov);
1258 1258 /*
1259 1259 * If the reply is NFS4ERR_ACCESS, it may be because
1260 1260 * we are root (no root net access). If the real uid
1261 1261 * is not root, then retry with the real uid instead.
1262 1262 */
1263 1263 if (ncr != NULL) {
1264 1264 crfree(ncr);
1265 1265 ncr = NULL;
1266 1266 }
1267 1267 if (res.status == NFS4ERR_ACCESS &&
1268 1268 (ncr = crnetadjust(cred_otw)) != NULL) {
1269 1269 cred_otw = ncr;
1270 1270 goto recov_retry;
1271 1271 }
1272 1272 kmem_free(argop, argoplist_size);
1273 1273 return (e.error);
1274 1274 }
1275 1275
1276 1276 resop = &res.array[idx_open]; /* open res */
1277 1277 op_res = &resop->nfs_resop4_u.opopen;
1278 1278
1279 1279 #ifdef DEBUG
1280 1280 /*
1281 1281 * verify attrset bitmap
1282 1282 */
1283 1283 if (create_flag &&
1284 1284 (createmode == UNCHECKED4 || createmode == GUARDED4)) {
1285 1285 /* make sure attrset returned is what we asked for */
1286 1286 /* XXX Ignore this 'error' for now */
1287 1287 if (attr->attrmask != op_res->attrset)
1288 1288 /* EMPTY */;
1289 1289 }
1290 1290 #endif
1291 1291
1292 1292 if (op_res->rflags & OPEN4_RESULT_LOCKTYPE_POSIX) {
1293 1293 mutex_enter(&VTOMI4(dvp)->mi_lock);
1294 1294 VTOMI4(dvp)->mi_flags |= MI4_POSIX_LOCK;
1295 1295 mutex_exit(&VTOMI4(dvp)->mi_lock);
1296 1296 }
1297 1297
1298 1298 resop = &res.array[idx_open + 1]; /* getfh res */
1299 1299 gf_res = &resop->nfs_resop4_u.opgetfh;
1300 1300
1301 1301 otw_sfh = sfh4_get(&gf_res->object, VTOMI4(dvp));
1302 1302
1303 1303 /*
1304 1304 * The open stateid has been updated on the server but not
1305 1305 * on the client yet. There is a path: makenfs4node->nfs4_attr_cache->
1306 1306 * flush_pages->VOP_PUTPAGE->...->nfs4write where we will issue an OTW
1307 1307 * WRITE call. That, however, will use the old stateid, so go ahead
1308 1308 * and upate the open stateid now, before any call to makenfs4node.
1309 1309 */
1310 1310 if (vpi) {
1311 1311 nfs4_open_stream_t *tmp_osp;
1312 1312 rnode4_t *tmp_rp = VTOR4(vpi);
1313 1313
1314 1314 tmp_osp = find_open_stream(oop, tmp_rp);
1315 1315 if (tmp_osp) {
1316 1316 tmp_osp->open_stateid = op_res->stateid;
1317 1317 mutex_exit(&tmp_osp->os_sync_lock);
1318 1318 open_stream_rele(tmp_osp, tmp_rp);
1319 1319 }
1320 1320
1321 1321 /*
1322 1322 * We must determine if the file handle given by the otw open
1323 1323 * is the same as the file handle which was passed in with
1324 1324 * *vpp. This case can be reached if the file we are trying
1325 1325 * to open has been removed and another file has been created
1326 1326 * having the same file name. The passed in vnode is released
1327 1327 * later.
1328 1328 */
1329 1329 orig_sfh = VTOR4(vpi)->r_fh;
1330 1330 fh_differs = nfs4cmpfh(&orig_sfh->sfh_fh, &otw_sfh->sfh_fh);
1331 1331 }
1332 1332
1333 1333 garp = &res.array[idx_fattr].nfs_resop4_u.opgetattr.ga_res;
1334 1334
1335 1335 if (create_flag || fh_differs) {
1336 1336 int rnode_err = 0;
1337 1337
1338 1338 vp = makenfs4node(otw_sfh, garp, dvp->v_vfsp, t, cr,
1339 1339 dvp, fn_get(VTOSV(dvp)->sv_name, file_name, otw_sfh));
1340 1340
1341 1341 if (e.error)
1342 1342 PURGE_ATTRCACHE4(vp);
1343 1343 /*
1344 1344 * For the newly created vp case, make sure the rnode
1345 1345 * isn't bad before using it.
1346 1346 */
1347 1347 mutex_enter(&(VTOR4(vp))->r_statelock);
1348 1348 if (VTOR4(vp)->r_flags & R4RECOVERR)
1349 1349 rnode_err = EIO;
1350 1350 mutex_exit(&(VTOR4(vp))->r_statelock);
1351 1351
1352 1352 if (rnode_err) {
1353 1353 nfs4_end_open_seqid_sync(oop);
1354 1354 nfs4args_copen_free(open_args);
1355 1355 if (setgid_flag) {
1356 1356 nfs4args_verify_free(&argop[8]);
1357 1357 nfs4args_setattr_free(&argop[9]);
1358 1358 }
1359 1359 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1360 1360 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state,
1361 1361 needrecov);
1362 1362 open_owner_rele(oop);
1363 1363 VN_RELE(vp);
1364 1364 if (ncr != NULL)
1365 1365 crfree(ncr);
1366 1366 sfh4_rele(&otw_sfh);
1367 1367 kmem_free(argop, argoplist_size);
1368 1368 return (EIO);
1369 1369 }
1370 1370 } else {
1371 1371 vp = vpi;
1372 1372 }
1373 1373 sfh4_rele(&otw_sfh);
1374 1374
1375 1375 /*
1376 1376 * It seems odd to get a full set of attrs and then not update
1377 1377 * the object's attrcache in the non-create case. Create case uses
1378 1378 * the attrs since makenfs4node checks to see if the attrs need to
1379 1379 * be updated (and then updates them). The non-create case should
1380 1380 * update attrs also.
1381 1381 */
1382 1382 if (! create_flag && ! fh_differs && !e.error) {
1383 1383 nfs4_attr_cache(vp, garp, t, cr, TRUE, NULL);
1384 1384 }
1385 1385
1386 1386 nfs4_error_zinit(&e);
1387 1387 if (op_res->rflags & OPEN4_RESULT_CONFIRM) {
1388 1388 /* This does not do recovery for vp explicitly. */
1389 1389 nfs4open_confirm(vp, &seqid, &op_res->stateid, cred_otw, FALSE,
1390 1390 &retry_open, oop, FALSE, &e, &num_bseqid_retry);
1391 1391
1392 1392 if (e.error || e.stat) {
1393 1393 nfs4_end_open_seqid_sync(oop);
1394 1394 nfs4args_copen_free(open_args);
1395 1395 if (setgid_flag) {
1396 1396 nfs4args_verify_free(&argop[8]);
1397 1397 nfs4args_setattr_free(&argop[9]);
1398 1398 }
1399 1399 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1400 1400 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state,
1401 1401 needrecov);
1402 1402 open_owner_rele(oop);
1403 1403 if (create_flag || fh_differs) {
1404 1404 /* rele the makenfs4node */
1405 1405 VN_RELE(vp);
1406 1406 }
1407 1407 if (ncr != NULL) {
1408 1408 crfree(ncr);
1409 1409 ncr = NULL;
1410 1410 }
1411 1411 if (retry_open == TRUE) {
1412 1412 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
1413 1413 "nfs4open_otw: retry the open since OPEN "
1414 1414 "CONFIRM failed with error %d stat %d",
1415 1415 e.error, e.stat));
1416 1416 if (create_flag && createmode == GUARDED4) {
1417 1417 NFS4_DEBUG(nfs4_client_recov_debug,
1418 1418 (CE_NOTE, "nfs4open_otw: switch "
1419 1419 "createmode from GUARDED4 to "
1420 1420 "UNCHECKED4"));
1421 1421 createmode = UNCHECKED4;
1422 1422 }
1423 1423 goto recov_retry;
1424 1424 }
1425 1425 if (!e.error) {
1426 1426 if (create_flag && (createmode != EXCLUSIVE4) &&
1427 1427 e.stat == NFS4ERR_BADOWNER)
1428 1428 nfs4_log_badowner(VTOMI4(dvp), OP_OPEN);
1429 1429
1430 1430 e.error = geterrno4(e.stat);
1431 1431 }
1432 1432 kmem_free(argop, argoplist_size);
1433 1433 return (e.error);
1434 1434 }
1435 1435 }
1436 1436
1437 1437 rp = VTOR4(vp);
1438 1438
1439 1439 mutex_enter(&rp->r_statev4_lock);
1440 1440 if (create_flag)
1441 1441 rp->created_v4 = 1;
1442 1442 mutex_exit(&rp->r_statev4_lock);
1443 1443
1444 1444 mutex_enter(&oop->oo_lock);
1445 1445 /* Doesn't matter if 'oo_just_created' already was set as this */
1446 1446 oop->oo_just_created = NFS4_PERM_CREATED;
1447 1447 if (oop->oo_cred_otw)
1448 1448 crfree(oop->oo_cred_otw);
1449 1449 oop->oo_cred_otw = cred_otw;
1450 1450 crhold(oop->oo_cred_otw);
1451 1451 mutex_exit(&oop->oo_lock);
1452 1452
1453 1453 /* returns with 'os_sync_lock' held */
1454 1454 osp = find_or_create_open_stream(oop, rp, &created_osp);
1455 1455 if (!osp) {
1456 1456 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE,
1457 1457 "nfs4open_otw: failed to create an open stream"));
1458 1458 NFS4_DEBUG(nfs4_seqid_sync, (CE_NOTE, "nfs4open_otw: "
1459 1459 "signal our end of use of the open seqid"));
1460 1460
1461 1461 nfs4_end_open_seqid_sync(oop);
1462 1462 open_owner_rele(oop);
1463 1463 nfs4args_copen_free(open_args);
1464 1464 if (setgid_flag) {
1465 1465 nfs4args_verify_free(&argop[8]);
1466 1466 nfs4args_setattr_free(&argop[9]);
1467 1467 }
1468 1468 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1469 1469 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov);
1470 1470 if (create_flag || fh_differs)
1471 1471 VN_RELE(vp);
1472 1472 if (ncr != NULL)
1473 1473 crfree(ncr);
1474 1474
1475 1475 kmem_free(argop, argoplist_size);
1476 1476 return (EINVAL);
1477 1477
1478 1478 }
1479 1479
1480 1480 osp->open_stateid = op_res->stateid;
1481 1481
1482 1482 if (open_flag & FREAD)
1483 1483 osp->os_share_acc_read++;
1484 1484 if (open_flag & FWRITE)
1485 1485 osp->os_share_acc_write++;
1486 1486 osp->os_share_deny_none++;
1487 1487
1488 1488 /*
1489 1489 * Need to reset this bitfield for the possible case where we were
1490 1490 * going to OTW CLOSE the file, got a non-recoverable error, and before
1491 1491 * we could retry the CLOSE, OPENed the file again.
1492 1492 */
1493 1493 ASSERT(osp->os_open_owner->oo_seqid_inuse);
1494 1494 osp->os_final_close = 0;
1495 1495 osp->os_force_close = 0;
1496 1496 #ifdef DEBUG
1497 1497 if (osp->os_failed_reopen)
1498 1498 NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE, "nfs4open_otw:"
1499 1499 " clearing os_failed_reopen for osp %p, cr %p, rp %s",
1500 1500 (void *)osp, (void *)cr, rnode4info(rp)));
1501 1501 #endif
1502 1502 osp->os_failed_reopen = 0;
1503 1503
1504 1504 mutex_exit(&osp->os_sync_lock);
1505 1505
1506 1506 nfs4_end_open_seqid_sync(oop);
1507 1507
1508 1508 if (created_osp && recov_state.rs_sp != NULL) {
1509 1509 mutex_enter(&recov_state.rs_sp->s_lock);
1510 1510 nfs4_inc_state_ref_count_nolock(recov_state.rs_sp, VTOMI4(dvp));
1511 1511 mutex_exit(&recov_state.rs_sp->s_lock);
1512 1512 }
1513 1513
1514 1514 /* get rid of our reference to find oop */
1515 1515 open_owner_rele(oop);
1516 1516
1517 1517 open_stream_rele(osp, rp);
1518 1518
1519 1519 /* accept delegation, if any */
1520 1520 nfs4_delegation_accept(rp, CLAIM_NULL, op_res, garp, cred_otw);
1521 1521
1522 1522 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov);
1523 1523
1524 1524 if (createmode == EXCLUSIVE4 &&
1525 1525 (in_va->va_mask & ~(AT_GID | AT_SIZE))) {
1526 1526 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4open_otw:"
1527 1527 " EXCLUSIVE4: sending a SETATTR"));
1528 1528 /*
1529 1529 * If doing an exclusive create, then generate
1530 1530 * a SETATTR to set the initial attributes.
1531 1531 * Try to set the mtime and the atime to the
1532 1532 * server's current time. It is somewhat
1533 1533 * expected that these fields will be used to
1534 1534 * store the exclusive create cookie. If not,
1535 1535 * server implementors will need to know that
1536 1536 * a SETATTR will follow an exclusive create
1537 1537 * and the cookie should be destroyed if
1538 1538 * appropriate.
1539 1539 *
1540 1540 * The AT_GID and AT_SIZE bits are turned off
1541 1541 * so that the SETATTR request will not attempt
1542 1542 * to process these. The gid will be set
1543 1543 * separately if appropriate. The size is turned
1544 1544 * off because it is assumed that a new file will
1545 1545 * be created empty and if the file wasn't empty,
1546 1546 * then the exclusive create will have failed
1547 1547 * because the file must have existed already.
1548 1548 * Therefore, no truncate operation is needed.
1549 1549 */
1550 1550 in_va->va_mask &= ~(AT_GID | AT_SIZE);
1551 1551 in_va->va_mask |= (AT_MTIME | AT_ATIME);
1552 1552
1553 1553 e.error = nfs4setattr(vp, in_va, 0, cr, NULL);
1554 1554 if (e.error) {
1555 1555 /*
1556 1556 * Couldn't correct the attributes of
1557 1557 * the newly created file and the
1558 1558 * attributes are wrong. Remove the
1559 1559 * file and return an error to the
1560 1560 * application.
1561 1561 */
1562 1562 /* XXX will this take care of client state ? */
1563 1563 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE,
1564 1564 "nfs4open_otw: EXCLUSIVE4: error %d on SETATTR:"
1565 1565 " remove file", e.error));
1566 1566 VN_RELE(vp);
1567 1567 (void) nfs4_remove(dvp, file_name, cr, NULL, 0);
1568 1568 /*
1569 1569 * Since we've reled the vnode and removed
1570 1570 * the file we now need to return the error.
1571 1571 * At this point we don't want to update the
1572 1572 * dircaches, call nfs4_waitfor_purge_complete
1573 1573 * or set vpp to vp so we need to skip these
1574 1574 * as well.
1575 1575 */
1576 1576 goto skip_update_dircaches;
1577 1577 }
1578 1578 }
1579 1579
1580 1580 /*
1581 1581 * If we created or found the correct vnode, due to create_flag or
1582 1582 * fh_differs being set, then update directory cache attribute, readdir
1583 1583 * and dnlc caches.
1584 1584 */
1585 1585 if (create_flag || fh_differs) {
1586 1586 dirattr_info_t dinfo, *dinfop;
1587 1587
1588 1588 /*
1589 1589 * Make sure getattr succeeded before using results.
1590 1590 * note: op 7 is getattr(dir) for both flavors of
1591 1591 * open(create).
1592 1592 */
1593 1593 if (create_flag && res.status == NFS4_OK) {
1594 1594 dinfo.di_time_call = t;
1595 1595 dinfo.di_cred = cr;
1596 1596 dinfo.di_garp =
1597 1597 &res.array[6].nfs_resop4_u.opgetattr.ga_res;
1598 1598 dinfop = &dinfo;
1599 1599 } else {
1600 1600 dinfop = NULL;
1601 1601 }
1602 1602
1603 1603 nfs4_update_dircaches(&op_res->cinfo, dvp, vp, file_name,
1604 1604 dinfop);
1605 1605 }
1606 1606
1607 1607 /*
1608 1608 * If the page cache for this file was flushed from actions
1609 1609 * above, it was done asynchronously and if that is true,
1610 1610 * there is a need to wait here for it to complete. This must
1611 1611 * be done outside of start_fop/end_fop.
1612 1612 */
1613 1613 (void) nfs4_waitfor_purge_complete(vp);
1614 1614
1615 1615 /*
1616 1616 * It is implicit that we are in the open case (create_flag == 0) since
1617 1617 * fh_differs can only be set to a non-zero value in the open case.
1618 1618 */
1619 1619 if (fh_differs != 0 && vpi != NULL)
1620 1620 VN_RELE(vpi);
1621 1621
1622 1622 /*
1623 1623 * Be sure to set *vpp to the correct value before returning.
1624 1624 */
1625 1625 *vpp = vp;
1626 1626
1627 1627 skip_update_dircaches:
1628 1628
1629 1629 nfs4args_copen_free(open_args);
1630 1630 if (setgid_flag) {
1631 1631 nfs4args_verify_free(&argop[8]);
1632 1632 nfs4args_setattr_free(&argop[9]);
1633 1633 }
1634 1634 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1635 1635
1636 1636 if (ncr)
1637 1637 crfree(ncr);
1638 1638 kmem_free(argop, argoplist_size);
1639 1639 return (e.error);
1640 1640 }
1641 1641
1642 1642 /*
1643 1643 * Reopen an open instance. cf. nfs4open_otw().
1644 1644 *
1645 1645 * Errors are returned by the nfs4_error_t parameter.
1646 1646 * - ep->error contains an errno value or zero.
1647 1647 * - if it is zero, ep->stat is set to an NFS status code, if any.
1648 1648 * If the file could not be reopened, but the caller should continue, the
1649 1649 * file is marked dead and no error values are returned. If the caller
1650 1650 * should stop recovering open files and start over, either the ep->error
1651 1651 * value or ep->stat will indicate an error (either something that requires
1652 1652 * recovery or EAGAIN). Note that some recovery (e.g., expired volatile
1653 1653 * filehandles) may be handled silently by this routine.
1654 1654 * - if it is EINTR, ETIMEDOUT, or NFS4_FRC_UNMT_ERR, recovery for lost state
1655 1655 * will be started, so the caller should not do it.
1656 1656 *
1657 1657 * Gotos:
1658 1658 * - kill_file : reopen failed in such a fashion to constitute marking the
1659 1659 * file dead and setting the open stream's 'os_failed_reopen' as 1. This
1660 1660 * is for cases where recovery is not possible.
1661 1661 * - failed_reopen : same as above, except that the file has already been
1662 1662 * marked dead, so no need to do it again.
1663 1663 * - bailout : reopen failed but we are able to recover and retry the reopen -
1664 1664 * either within this function immediately or via the calling function.
1665 1665 */
1666 1666
1667 1667 void
1668 1668 nfs4_reopen(vnode_t *vp, nfs4_open_stream_t *osp, nfs4_error_t *ep,
1669 1669 open_claim_type4 claim, bool_t frc_use_claim_previous,
1670 1670 bool_t is_recov)
1671 1671 {
1672 1672 COMPOUND4args_clnt args;
1673 1673 COMPOUND4res_clnt res;
1674 1674 nfs_argop4 argop[4];
1675 1675 nfs_resop4 *resop;
1676 1676 OPEN4res *op_res = NULL;
1677 1677 OPEN4cargs *open_args;
1678 1678 GETFH4res *gf_res;
1679 1679 rnode4_t *rp = VTOR4(vp);
1680 1680 int doqueue = 1;
1681 1681 cred_t *cr = NULL, *cred_otw = NULL;
1682 1682 nfs4_open_owner_t *oop = NULL;
1683 1683 seqid4 seqid;
1684 1684 nfs4_ga_res_t *garp;
1685 1685 char fn[MAXNAMELEN];
1686 1686 nfs4_recov_state_t recov = {NULL, 0};
1687 1687 nfs4_lost_rqst_t lost_rqst;
1688 1688 mntinfo4_t *mi = VTOMI4(vp);
1689 1689 bool_t abort;
1690 1690 char *failed_msg = "";
1691 1691 int fh_different;
1692 1692 hrtime_t t;
1693 1693 nfs4_bseqid_entry_t *bsep = NULL;
1694 1694
1695 1695 ASSERT(nfs4_consistent_type(vp));
1696 1696 ASSERT(nfs_zone() == mi->mi_zone);
1697 1697
1698 1698 nfs4_error_zinit(ep);
1699 1699
1700 1700 /* this is the cred used to find the open owner */
1701 1701 cr = state_to_cred(osp);
1702 1702 if (cr == NULL) {
1703 1703 failed_msg = "Couldn't reopen: no cred";
1704 1704 goto kill_file;
1705 1705 }
1706 1706 /* use this cred for OTW operations */
1707 1707 cred_otw = nfs4_get_otw_cred(cr, mi, osp->os_open_owner);
1708 1708
1709 1709 top:
1710 1710 nfs4_error_zinit(ep);
1711 1711
1712 1712 if (mi->mi_vfsp->vfs_flag & VFS_UNMOUNTED) {
1713 1713 /* File system has been unmounted, quit */
1714 1714 ep->error = EIO;
1715 1715 failed_msg = "Couldn't reopen: file system has been unmounted";
1716 1716 goto kill_file;
1717 1717 }
1718 1718
1719 1719 oop = osp->os_open_owner;
1720 1720
1721 1721 ASSERT(oop != NULL);
1722 1722 if (oop == NULL) { /* be defensive in non-DEBUG */
1723 1723 failed_msg = "can't reopen: no open owner";
1724 1724 goto kill_file;
1725 1725 }
1726 1726 open_owner_hold(oop);
1727 1727
1728 1728 ep->error = nfs4_start_open_seqid_sync(oop, mi);
1729 1729 if (ep->error) {
1730 1730 open_owner_rele(oop);
1731 1731 oop = NULL;
1732 1732 goto bailout;
1733 1733 }
1734 1734
1735 1735 /*
1736 1736 * If the rnode has a delegation and the delegation has been
1737 1737 * recovered and the server didn't request a recall and the caller
1738 1738 * didn't specifically ask for CLAIM_PREVIOUS (nfs4frlock during
1739 1739 * recovery) and the rnode hasn't been marked dead, then install
1740 1740 * the delegation stateid in the open stream. Otherwise, proceed
1741 1741 * with a CLAIM_PREVIOUS or CLAIM_NULL OPEN.
1742 1742 */
1743 1743 mutex_enter(&rp->r_statev4_lock);
1744 1744 if (rp->r_deleg_type != OPEN_DELEGATE_NONE &&
1745 1745 !rp->r_deleg_return_pending &&
1746 1746 (rp->r_deleg_needs_recovery == OPEN_DELEGATE_NONE) &&
1747 1747 !rp->r_deleg_needs_recall &&
1748 1748 claim != CLAIM_DELEGATE_CUR && !frc_use_claim_previous &&
1749 1749 !(rp->r_flags & R4RECOVERR)) {
1750 1750 mutex_enter(&osp->os_sync_lock);
1751 1751 osp->os_delegation = 1;
1752 1752 osp->open_stateid = rp->r_deleg_stateid;
1753 1753 mutex_exit(&osp->os_sync_lock);
1754 1754 mutex_exit(&rp->r_statev4_lock);
1755 1755 goto bailout;
1756 1756 }
1757 1757 mutex_exit(&rp->r_statev4_lock);
1758 1758
1759 1759 /*
1760 1760 * If the file failed recovery, just quit. This failure need not
1761 1761 * affect other reopens, so don't return an error.
1762 1762 */
1763 1763 mutex_enter(&rp->r_statelock);
1764 1764 if (rp->r_flags & R4RECOVERR) {
1765 1765 mutex_exit(&rp->r_statelock);
1766 1766 ep->error = 0;
1767 1767 goto failed_reopen;
1768 1768 }
1769 1769 mutex_exit(&rp->r_statelock);
1770 1770
1771 1771 /*
1772 1772 * argop is empty here
1773 1773 *
1774 1774 * PUTFH, OPEN, GETATTR
1775 1775 */
1776 1776 args.ctag = TAG_REOPEN;
1777 1777 args.array_len = 4;
1778 1778 args.array = argop;
1779 1779
1780 1780 NFS4_DEBUG(nfs4_client_failover_debug, (CE_NOTE,
1781 1781 "nfs4_reopen: file is type %d, id %s",
1782 1782 vp->v_type, rnode4info(VTOR4(vp))));
1783 1783
1784 1784 argop[0].argop = OP_CPUTFH;
1785 1785
1786 1786 if (claim != CLAIM_PREVIOUS) {
1787 1787 /*
1788 1788 * if this is a file mount then
1789 1789 * use the mntinfo parentfh
1790 1790 */
1791 1791 argop[0].nfs_argop4_u.opcputfh.sfh =
1792 1792 (vp->v_flag & VROOT) ? mi->mi_srvparentfh :
1793 1793 VTOSV(vp)->sv_dfh;
1794 1794 } else {
1795 1795 /* putfh fh to reopen */
1796 1796 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
1797 1797 }
1798 1798
1799 1799 argop[1].argop = OP_COPEN;
1800 1800 open_args = &argop[1].nfs_argop4_u.opcopen;
1801 1801 open_args->claim = claim;
1802 1802
1803 1803 if (claim == CLAIM_NULL) {
1804 1804
1805 1805 if ((ep->error = vtoname(vp, fn, MAXNAMELEN)) != 0) {
1806 1806 nfs_cmn_err(ep->error, CE_WARN, "nfs4_reopen: vtoname "
1807 1807 "failed for vp 0x%p for CLAIM_NULL with %m",
1808 1808 (void *)vp);
1809 1809 failed_msg = "Couldn't reopen: vtoname failed for "
1810 1810 "CLAIM_NULL";
1811 1811 /* nothing allocated yet */
1812 1812 goto kill_file;
1813 1813 }
1814 1814
1815 1815 open_args->open_claim4_u.cfile = fn;
1816 1816 } else if (claim == CLAIM_PREVIOUS) {
1817 1817
1818 1818 /*
1819 1819 * We have two cases to deal with here:
1820 1820 * 1) We're being called to reopen files in order to satisfy
1821 1821 * a lock operation request which requires us to explicitly
1822 1822 * reopen files which were opened under a delegation. If
1823 1823 * we're in recovery, we *must* use CLAIM_PREVIOUS. In
1824 1824 * that case, frc_use_claim_previous is TRUE and we must
1825 1825 * use the rnode's current delegation type (r_deleg_type).
1826 1826 * 2) We're reopening files during some form of recovery.
1827 1827 * In this case, frc_use_claim_previous is FALSE and we
1828 1828 * use the delegation type appropriate for recovery
1829 1829 * (r_deleg_needs_recovery).
1830 1830 */
1831 1831 mutex_enter(&rp->r_statev4_lock);
1832 1832 open_args->open_claim4_u.delegate_type =
1833 1833 frc_use_claim_previous ?
1834 1834 rp->r_deleg_type :
1835 1835 rp->r_deleg_needs_recovery;
1836 1836 mutex_exit(&rp->r_statev4_lock);
1837 1837
1838 1838 } else if (claim == CLAIM_DELEGATE_CUR) {
1839 1839
1840 1840 if ((ep->error = vtoname(vp, fn, MAXNAMELEN)) != 0) {
1841 1841 nfs_cmn_err(ep->error, CE_WARN, "nfs4_reopen: vtoname "
1842 1842 "failed for vp 0x%p for CLAIM_DELEGATE_CUR "
1843 1843 "with %m", (void *)vp);
1844 1844 failed_msg = "Couldn't reopen: vtoname failed for "
1845 1845 "CLAIM_DELEGATE_CUR";
1846 1846 /* nothing allocated yet */
1847 1847 goto kill_file;
1848 1848 }
1849 1849
1850 1850 mutex_enter(&rp->r_statev4_lock);
1851 1851 open_args->open_claim4_u.delegate_cur_info.delegate_stateid =
1852 1852 rp->r_deleg_stateid;
1853 1853 mutex_exit(&rp->r_statev4_lock);
1854 1854
1855 1855 open_args->open_claim4_u.delegate_cur_info.cfile = fn;
1856 1856 }
1857 1857 open_args->opentype = OPEN4_NOCREATE;
1858 1858 open_args->owner.clientid = mi2clientid(mi);
1859 1859 open_args->owner.owner_len = sizeof (oop->oo_name);
1860 1860 open_args->owner.owner_val =
1861 1861 kmem_alloc(open_args->owner.owner_len, KM_SLEEP);
1862 1862 bcopy(&oop->oo_name, open_args->owner.owner_val,
1863 1863 open_args->owner.owner_len);
1864 1864 open_args->share_access = 0;
1865 1865 open_args->share_deny = 0;
1866 1866
1867 1867 mutex_enter(&osp->os_sync_lock);
1868 1868 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, "nfs4_reopen: osp %p rp "
1869 1869 "%p: read acc %"PRIu64" write acc %"PRIu64": open ref count %d: "
1870 1870 "mmap read %"PRIu64" mmap write %"PRIu64" claim %d ",
1871 1871 (void *)osp, (void *)rp, osp->os_share_acc_read,
1872 1872 osp->os_share_acc_write, osp->os_open_ref_count,
1873 1873 osp->os_mmap_read, osp->os_mmap_write, claim));
1874 1874
1875 1875 if (osp->os_share_acc_read || osp->os_mmap_read)
1876 1876 open_args->share_access |= OPEN4_SHARE_ACCESS_READ;
1877 1877 if (osp->os_share_acc_write || osp->os_mmap_write)
1878 1878 open_args->share_access |= OPEN4_SHARE_ACCESS_WRITE;
1879 1879 if (osp->os_share_deny_read)
1880 1880 open_args->share_deny |= OPEN4_SHARE_DENY_READ;
1881 1881 if (osp->os_share_deny_write)
1882 1882 open_args->share_deny |= OPEN4_SHARE_DENY_WRITE;
1883 1883 mutex_exit(&osp->os_sync_lock);
1884 1884
1885 1885 seqid = nfs4_get_open_seqid(oop) + 1;
1886 1886 open_args->seqid = seqid;
1887 1887
1888 1888 /* Construct the getfh part of the compound */
1889 1889 argop[2].argop = OP_GETFH;
1890 1890
1891 1891 /* Construct the getattr part of the compound */
1892 1892 argop[3].argop = OP_GETATTR;
1893 1893 argop[3].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
1894 1894 argop[3].nfs_argop4_u.opgetattr.mi = mi;
1895 1895
1896 1896 t = gethrtime();
1897 1897
1898 1898 rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, ep);
1899 1899
1900 1900 if (ep->error) {
1901 1901 if (!is_recov && !frc_use_claim_previous &&
1902 1902 (ep->error == EINTR || ep->error == ETIMEDOUT ||
1903 1903 NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp))) {
1904 1904 nfs4open_save_lost_rqst(ep->error, &lost_rqst, oop,
1905 1905 cred_otw, vp, NULL, open_args);
1906 1906 abort = nfs4_start_recovery(ep,
1907 1907 VTOMI4(vp), vp, NULL, NULL,
1908 1908 lost_rqst.lr_op == OP_OPEN ?
1909 1909 &lost_rqst : NULL, OP_OPEN, NULL, NULL, NULL);
1910 1910 nfs4args_copen_free(open_args);
1911 1911 goto bailout;
1912 1912 }
1913 1913
1914 1914 nfs4args_copen_free(open_args);
1915 1915
1916 1916 if (ep->error == EACCES && cred_otw != cr) {
1917 1917 crfree(cred_otw);
1918 1918 cred_otw = cr;
1919 1919 crhold(cred_otw);
1920 1920 nfs4_end_open_seqid_sync(oop);
1921 1921 open_owner_rele(oop);
1922 1922 oop = NULL;
1923 1923 goto top;
1924 1924 }
1925 1925 if (ep->error == ETIMEDOUT)
1926 1926 goto bailout;
1927 1927 failed_msg = "Couldn't reopen: rpc error";
1928 1928 goto kill_file;
1929 1929 }
1930 1930
1931 1931 if (nfs4_need_to_bump_seqid(&res))
1932 1932 nfs4_set_open_seqid(seqid, oop, args.ctag);
1933 1933
1934 1934 switch (res.status) {
1935 1935 case NFS4_OK:
1936 1936 if (recov.rs_flags & NFS4_RS_DELAY_MSG) {
1937 1937 mutex_enter(&rp->r_statelock);
1938 1938 rp->r_delay_interval = 0;
1939 1939 mutex_exit(&rp->r_statelock);
1940 1940 }
1941 1941 break;
1942 1942 case NFS4ERR_BAD_SEQID:
1943 1943 bsep = nfs4_create_bseqid_entry(oop, NULL, vp, 0,
1944 1944 args.ctag, open_args->seqid);
1945 1945
1946 1946 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL,
1947 1947 NULL, lost_rqst.lr_op == OP_OPEN ? &lost_rqst :
1948 1948 NULL, OP_OPEN, bsep, NULL, NULL);
1949 1949
1950 1950 nfs4args_copen_free(open_args);
1951 1951 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1952 1952 nfs4_end_open_seqid_sync(oop);
1953 1953 open_owner_rele(oop);
1954 1954 oop = NULL;
1955 1955 kmem_free(bsep, sizeof (*bsep));
1956 1956
1957 1957 goto kill_file;
1958 1958 case NFS4ERR_NO_GRACE:
1959 1959 nfs4args_copen_free(open_args);
1960 1960 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1961 1961 nfs4_end_open_seqid_sync(oop);
1962 1962 open_owner_rele(oop);
1963 1963 oop = NULL;
1964 1964 if (claim == CLAIM_PREVIOUS) {
1965 1965 /*
1966 1966 * Retry as a plain open. We don't need to worry about
1967 1967 * checking the changeinfo: it is acceptable for a
1968 1968 * client to re-open a file and continue processing
1969 1969 * (in the absence of locks).
1970 1970 */
1971 1971 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
1972 1972 "nfs4_reopen: CLAIM_PREVIOUS: NFS4ERR_NO_GRACE; "
1973 1973 "will retry as CLAIM_NULL"));
1974 1974 claim = CLAIM_NULL;
1975 1975 nfs4_mi_kstat_inc_no_grace(mi);
1976 1976 goto top;
1977 1977 }
1978 1978 failed_msg =
1979 1979 "Couldn't reopen: tried reclaim outside grace period. ";
1980 1980 goto kill_file;
1981 1981 case NFS4ERR_GRACE:
1982 1982 nfs4_set_grace_wait(mi);
1983 1983 nfs4args_copen_free(open_args);
1984 1984 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1985 1985 nfs4_end_open_seqid_sync(oop);
1986 1986 open_owner_rele(oop);
1987 1987 oop = NULL;
1988 1988 ep->error = nfs4_wait_for_grace(mi, &recov);
1989 1989 if (ep->error != 0)
1990 1990 goto bailout;
1991 1991 goto top;
1992 1992 case NFS4ERR_DELAY:
1993 1993 nfs4_set_delay_wait(vp);
1994 1994 nfs4args_copen_free(open_args);
1995 1995 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1996 1996 nfs4_end_open_seqid_sync(oop);
1997 1997 open_owner_rele(oop);
1998 1998 oop = NULL;
1999 1999 ep->error = nfs4_wait_for_delay(vp, &recov);
2000 2000 nfs4_mi_kstat_inc_delay(mi);
2001 2001 if (ep->error != 0)
2002 2002 goto bailout;
2003 2003 goto top;
2004 2004 case NFS4ERR_FHEXPIRED:
2005 2005 /* recover filehandle and retry */
2006 2006 abort = nfs4_start_recovery(ep,
2007 2007 mi, vp, NULL, NULL, NULL, OP_OPEN, NULL, NULL, NULL);
2008 2008 nfs4args_copen_free(open_args);
2009 2009 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2010 2010 nfs4_end_open_seqid_sync(oop);
2011 2011 open_owner_rele(oop);
2012 2012 oop = NULL;
2013 2013 if (abort == FALSE)
2014 2014 goto top;
2015 2015 failed_msg = "Couldn't reopen: recovery aborted";
2016 2016 goto kill_file;
2017 2017 case NFS4ERR_RESOURCE:
2018 2018 case NFS4ERR_STALE_CLIENTID:
2019 2019 case NFS4ERR_WRONGSEC:
2020 2020 case NFS4ERR_EXPIRED:
2021 2021 /*
2022 2022 * Do not mark the file dead and let the calling
2023 2023 * function initiate recovery.
2024 2024 */
2025 2025 nfs4args_copen_free(open_args);
2026 2026 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2027 2027 nfs4_end_open_seqid_sync(oop);
2028 2028 open_owner_rele(oop);
2029 2029 oop = NULL;
2030 2030 goto bailout;
2031 2031 case NFS4ERR_ACCESS:
2032 2032 if (cred_otw != cr) {
2033 2033 crfree(cred_otw);
2034 2034 cred_otw = cr;
2035 2035 crhold(cred_otw);
2036 2036 nfs4args_copen_free(open_args);
2037 2037 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2038 2038 nfs4_end_open_seqid_sync(oop);
2039 2039 open_owner_rele(oop);
2040 2040 oop = NULL;
2041 2041 goto top;
2042 2042 }
2043 2043 /* fall through */
2044 2044 default:
2045 2045 NFS4_DEBUG(nfs4_client_failover_debug, (CE_NOTE,
2046 2046 "nfs4_reopen: r_server 0x%p, mi_curr_serv 0x%p, rnode %s",
2047 2047 (void*)VTOR4(vp)->r_server, (void*)mi->mi_curr_serv,
2048 2048 rnode4info(VTOR4(vp))));
2049 2049 failed_msg = "Couldn't reopen: NFSv4 error";
2050 2050 nfs4args_copen_free(open_args);
2051 2051 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2052 2052 goto kill_file;
2053 2053 }
2054 2054
2055 2055 resop = &res.array[1]; /* open res */
2056 2056 op_res = &resop->nfs_resop4_u.opopen;
2057 2057
2058 2058 garp = &res.array[3].nfs_resop4_u.opgetattr.ga_res;
2059 2059
2060 2060 /*
2061 2061 * Check if the path we reopened really is the same
2062 2062 * file. We could end up in a situation where the file
2063 2063 * was removed and a new file created with the same name.
2064 2064 */
2065 2065 resop = &res.array[2];
2066 2066 gf_res = &resop->nfs_resop4_u.opgetfh;
2067 2067 (void) nfs_rw_enter_sig(&mi->mi_fh_lock, RW_READER, 0);
2068 2068 fh_different = (nfs4cmpfh(&rp->r_fh->sfh_fh, &gf_res->object) != 0);
2069 2069 if (fh_different) {
2070 2070 if (mi->mi_fh_expire_type == FH4_PERSISTENT ||
2071 2071 mi->mi_fh_expire_type & FH4_NOEXPIRE_WITH_OPEN) {
2072 2072 /* Oops, we don't have the same file */
2073 2073 if (mi->mi_fh_expire_type == FH4_PERSISTENT)
2074 2074 failed_msg = "Couldn't reopen: Persistent "
2075 2075 "file handle changed";
2076 2076 else
2077 2077 failed_msg = "Couldn't reopen: Volatile "
2078 2078 "(no expire on open) file handle changed";
2079 2079
2080 2080 nfs4args_copen_free(open_args);
2081 2081 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2082 2082 nfs_rw_exit(&mi->mi_fh_lock);
2083 2083 goto kill_file;
2084 2084
2085 2085 } else {
2086 2086 /*
2087 2087 * We have volatile file handles that don't compare.
2088 2088 * If the fids are the same then we assume that the
2089 2089 * file handle expired but the rnode still refers to
2090 2090 * the same file object.
2091 2091 *
2092 2092 * First check that we have fids or not.
2093 2093 * If we don't we have a dumb server so we will
2094 2094 * just assume every thing is ok for now.
2095 2095 */
2096 2096 if (!ep->error && garp->n4g_va.va_mask & AT_NODEID &&
2097 2097 rp->r_attr.va_mask & AT_NODEID &&
2098 2098 rp->r_attr.va_nodeid != garp->n4g_va.va_nodeid) {
2099 2099 /*
2100 2100 * We have fids, but they don't
2101 2101 * compare. So kill the file.
2102 2102 */
2103 2103 failed_msg =
2104 2104 "Couldn't reopen: file handle changed"
2105 2105 " due to mismatched fids";
2106 2106 nfs4args_copen_free(open_args);
2107 2107 (void) xdr_free(xdr_COMPOUND4res_clnt,
2108 2108 (caddr_t)&res);
2109 2109 nfs_rw_exit(&mi->mi_fh_lock);
2110 2110 goto kill_file;
2111 2111 } else {
2112 2112 /*
2113 2113 * We have volatile file handles that refers
2114 2114 * to the same file (at least they have the
2115 2115 * same fid) or we don't have fids so we
2116 2116 * can't tell. :(. We'll be a kind and accepting
2117 2117 * client so we'll update the rnode's file
2118 2118 * handle with the otw handle.
2119 2119 *
2120 2120 * We need to drop mi->mi_fh_lock since
2121 2121 * sh4_update acquires it. Since there is
2122 2122 * only one recovery thread there is no
2123 2123 * race.
2124 2124 */
2125 2125 nfs_rw_exit(&mi->mi_fh_lock);
2126 2126 sfh4_update(rp->r_fh, &gf_res->object);
2127 2127 }
2128 2128 }
2129 2129 } else {
2130 2130 nfs_rw_exit(&mi->mi_fh_lock);
2131 2131 }
2132 2132
2133 2133 ASSERT(nfs4_consistent_type(vp));
2134 2134
2135 2135 /*
2136 2136 * If the server wanted an OPEN_CONFIRM but that fails, just start
2137 2137 * over. Presumably if there is a persistent error it will show up
2138 2138 * when we resend the OPEN.
2139 2139 */
2140 2140 if (op_res->rflags & OPEN4_RESULT_CONFIRM) {
2141 2141 bool_t retry_open = FALSE;
2142 2142
2143 2143 nfs4open_confirm(vp, &seqid, &op_res->stateid,
2144 2144 cred_otw, is_recov, &retry_open,
2145 2145 oop, FALSE, ep, NULL);
2146 2146 if (ep->error || ep->stat) {
2147 2147 nfs4args_copen_free(open_args);
2148 2148 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2149 2149 nfs4_end_open_seqid_sync(oop);
2150 2150 open_owner_rele(oop);
2151 2151 oop = NULL;
2152 2152 goto top;
2153 2153 }
2154 2154 }
2155 2155
2156 2156 mutex_enter(&osp->os_sync_lock);
2157 2157 osp->open_stateid = op_res->stateid;
2158 2158 osp->os_delegation = 0;
2159 2159 /*
2160 2160 * Need to reset this bitfield for the possible case where we were
2161 2161 * going to OTW CLOSE the file, got a non-recoverable error, and before
2162 2162 * we could retry the CLOSE, OPENed the file again.
2163 2163 */
2164 2164 ASSERT(osp->os_open_owner->oo_seqid_inuse);
2165 2165 osp->os_final_close = 0;
2166 2166 osp->os_force_close = 0;
2167 2167 if (claim == CLAIM_DELEGATE_CUR || claim == CLAIM_PREVIOUS)
2168 2168 osp->os_dc_openacc = open_args->share_access;
2169 2169 mutex_exit(&osp->os_sync_lock);
2170 2170
2171 2171 nfs4_end_open_seqid_sync(oop);
2172 2172
2173 2173 /* accept delegation, if any */
2174 2174 nfs4_delegation_accept(rp, claim, op_res, garp, cred_otw);
2175 2175
2176 2176 nfs4args_copen_free(open_args);
2177 2177
2178 2178 nfs4_attr_cache(vp, garp, t, cr, TRUE, NULL);
2179 2179
2180 2180 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2181 2181
2182 2182 ASSERT(nfs4_consistent_type(vp));
2183 2183
2184 2184 open_owner_rele(oop);
2185 2185 crfree(cr);
2186 2186 crfree(cred_otw);
2187 2187 return;
2188 2188
2189 2189 kill_file:
2190 2190 nfs4_fail_recov(vp, failed_msg, ep->error, ep->stat);
2191 2191 failed_reopen:
2192 2192 NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE,
2193 2193 "nfs4_reopen: setting os_failed_reopen for osp %p, cr %p, rp %s",
2194 2194 (void *)osp, (void *)cr, rnode4info(rp)));
2195 2195 mutex_enter(&osp->os_sync_lock);
2196 2196 osp->os_failed_reopen = 1;
2197 2197 mutex_exit(&osp->os_sync_lock);
2198 2198 bailout:
2199 2199 if (oop != NULL) {
2200 2200 nfs4_end_open_seqid_sync(oop);
2201 2201 open_owner_rele(oop);
2202 2202 }
2203 2203 if (cr != NULL)
2204 2204 crfree(cr);
2205 2205 if (cred_otw != NULL)
2206 2206 crfree(cred_otw);
2207 2207 }
2208 2208
2209 2209 /* for . and .. OPENs */
2210 2210 /* ARGSUSED */
2211 2211 static int
2212 2212 nfs4_open_non_reg_file(vnode_t **vpp, int flag, cred_t *cr)
2213 2213 {
2214 2214 rnode4_t *rp;
2215 2215 nfs4_ga_res_t gar;
2216 2216
2217 2217 ASSERT(nfs_zone() == VTOMI4(*vpp)->mi_zone);
2218 2218
2219 2219 /*
2220 2220 * If close-to-open consistency checking is turned off or
2221 2221 * if there is no cached data, we can avoid
2222 2222 * the over the wire getattr. Otherwise, force a
2223 2223 * call to the server to get fresh attributes and to
2224 2224 * check caches. This is required for close-to-open
2225 2225 * consistency.
2226 2226 */
2227 2227 rp = VTOR4(*vpp);
2228 2228 if (VTOMI4(*vpp)->mi_flags & MI4_NOCTO ||
2229 2229 (rp->r_dir == NULL && !nfs4_has_pages(*vpp)))
2230 2230 return (0);
2231 2231
2232 2232 gar.n4g_va.va_mask = AT_ALL;
2233 2233 return (nfs4_getattr_otw(*vpp, &gar, cr, 0));
2234 2234 }
2235 2235
2236 2236 /*
2237 2237 * CLOSE a file
2238 2238 */
2239 2239 /* ARGSUSED */
2240 2240 static int
2241 2241 nfs4_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr,
2242 2242 caller_context_t *ct)
2243 2243 {
2244 2244 rnode4_t *rp;
2245 2245 int error = 0;
2246 2246 int r_error = 0;
2247 2247 int n4error = 0;
2248 2248 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
2249 2249
2250 2250 /*
2251 2251 * Remove client state for this (lockowner, file) pair.
2252 2252 * Issue otw v4 call to have the server do the same.
2253 2253 */
2254 2254
2255 2255 rp = VTOR4(vp);
2256 2256
2257 2257 /*
2258 2258 * zone_enter(2) prevents processes from changing zones with NFS files
2259 2259 * open; if we happen to get here from the wrong zone we can't do
2260 2260 * anything over the wire.
2261 2261 */
2262 2262 if (VTOMI4(vp)->mi_zone != nfs_zone()) {
2263 2263 /*
2264 2264 * We could attempt to clean up locks, except we're sure
2265 2265 * that the current process didn't acquire any locks on
2266 2266 * the file: any attempt to lock a file belong to another zone
2267 2267 * will fail, and one can't lock an NFS file and then change
2268 2268 * zones, as that fails too.
2269 2269 *
2270 2270 * Returning an error here is the sane thing to do. A
2271 2271 * subsequent call to VN_RELE() which translates to a
2272 2272 * nfs4_inactive() will clean up state: if the zone of the
2273 2273 * vnode's origin is still alive and kicking, the inactive
2274 2274 * thread will handle the request (from the correct zone), and
2275 2275 * everything (minus the OTW close call) should be OK. If the
2276 2276 * zone is going away nfs4_async_inactive() will throw away
2277 2277 * delegations, open streams and cached pages inline.
2278 2278 */
2279 2279 return (EIO);
2280 2280 }
2281 2281
2282 2282 /*
2283 2283 * If we are using local locking for this filesystem, then
2284 2284 * release all of the SYSV style record locks. Otherwise,
2285 2285 * we are doing network locking and we need to release all
2286 2286 * of the network locks. All of the locks held by this
2287 2287 * process on this file are released no matter what the
2288 2288 * incoming reference count is.
2289 2289 */
2290 2290 if (VTOMI4(vp)->mi_flags & MI4_LLOCK) {
2291 2291 cleanlocks(vp, ttoproc(curthread)->p_pid, 0);
2292 2292 cleanshares(vp, ttoproc(curthread)->p_pid);
2293 2293 } else
2294 2294 e.error = nfs4_lockrelease(vp, flag, offset, cr);
2295 2295
2296 2296 if (e.error) {
2297 2297 struct lm_sysid *lmsid;
2298 2298 lmsid = nfs4_find_sysid(VTOMI4(vp));
2299 2299 if (lmsid == NULL) {
2300 2300 DTRACE_PROBE2(unknown__sysid, int, e.error,
2301 2301 vnode_t *, vp);
2302 2302 } else {
2303 2303 cleanlocks(vp, ttoproc(curthread)->p_pid,
2304 2304 (lm_sysidt(lmsid) | LM_SYSID_CLIENT));
2305 2305
2306 2306 lm_rel_sysid(lmsid);
2307 2307 }
2308 2308 return (e.error);
2309 2309 }
2310 2310
2311 2311 if (count > 1)
2312 2312 return (0);
2313 2313
2314 2314 /*
2315 2315 * If the file has been `unlinked', then purge the
2316 2316 * DNLC so that this vnode will get reycled quicker
2317 2317 * and the .nfs* file on the server will get removed.
2318 2318 */
2319 2319 if (rp->r_unldvp != NULL)
2320 2320 dnlc_purge_vp(vp);
2321 2321
2322 2322 /*
2323 2323 * If the file was open for write and there are pages,
2324 2324 * do a synchronous flush and commit of all of the
2325 2325 * dirty and uncommitted pages.
2326 2326 */
2327 2327 ASSERT(!e.error);
2328 2328 if ((flag & FWRITE) && nfs4_has_pages(vp))
2329 2329 error = nfs4_putpage_commit(vp, 0, 0, cr);
2330 2330
2331 2331 mutex_enter(&rp->r_statelock);
2332 2332 r_error = rp->r_error;
2333 2333 rp->r_error = 0;
2334 2334 mutex_exit(&rp->r_statelock);
2335 2335
2336 2336 /*
2337 2337 * If this file type is one for which no explicit 'open' was
2338 2338 * done, then bail now (ie. no need for protocol 'close'). If
2339 2339 * there was an error w/the vm subsystem, return _that_ error,
2340 2340 * otherwise, return any errors that may've been reported via
2341 2341 * the rnode.
2342 2342 */
2343 2343 if (vp->v_type != VREG)
2344 2344 return (error ? error : r_error);
2345 2345
2346 2346 /*
2347 2347 * The sync putpage commit may have failed above, but since
2348 2348 * we're working w/a regular file, we need to do the protocol
2349 2349 * 'close' (nfs4close_one will figure out if an otw close is
2350 2350 * needed or not). Report any errors _after_ doing the protocol
2351 2351 * 'close'.
2352 2352 */
2353 2353 nfs4close_one(vp, NULL, cr, flag, NULL, &e, CLOSE_NORM, 0, 0, 0);
2354 2354 n4error = e.error ? e.error : geterrno4(e.stat);
2355 2355
2356 2356 /*
2357 2357 * Error reporting prio (Hi -> Lo)
2358 2358 *
2359 2359 * i) nfs4_putpage_commit (error)
2360 2360 * ii) rnode's (r_error)
2361 2361 * iii) nfs4close_one (n4error)
2362 2362 */
2363 2363 return (error ? error : (r_error ? r_error : n4error));
2364 2364 }
2365 2365
2366 2366 /*
2367 2367 * Initialize *lost_rqstp.
2368 2368 */
2369 2369
2370 2370 static void
2371 2371 nfs4close_save_lost_rqst(int error, nfs4_lost_rqst_t *lost_rqstp,
2372 2372 nfs4_open_owner_t *oop, nfs4_open_stream_t *osp, cred_t *cr,
2373 2373 vnode_t *vp)
2374 2374 {
2375 2375 if (error != ETIMEDOUT && error != EINTR &&
2376 2376 !NFS4_FRC_UNMT_ERR(error, vp->v_vfsp)) {
2377 2377 lost_rqstp->lr_op = 0;
2378 2378 return;
2379 2379 }
2380 2380
2381 2381 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
2382 2382 "nfs4close_save_lost_rqst: error %d", error));
2383 2383
2384 2384 lost_rqstp->lr_op = OP_CLOSE;
2385 2385 /*
2386 2386 * The vp is held and rele'd via the recovery code.
2387 2387 * See nfs4_save_lost_rqst.
2388 2388 */
2389 2389 lost_rqstp->lr_vp = vp;
2390 2390 lost_rqstp->lr_dvp = NULL;
2391 2391 lost_rqstp->lr_oop = oop;
2392 2392 lost_rqstp->lr_osp = osp;
2393 2393 ASSERT(osp != NULL);
2394 2394 ASSERT(mutex_owned(&osp->os_sync_lock));
2395 2395 osp->os_pending_close = 1;
2396 2396 lost_rqstp->lr_lop = NULL;
2397 2397 lost_rqstp->lr_cr = cr;
2398 2398 lost_rqstp->lr_flk = NULL;
2399 2399 lost_rqstp->lr_putfirst = FALSE;
2400 2400 }
2401 2401
2402 2402 /*
2403 2403 * Assumes you already have the open seqid sync grabbed as well as the
2404 2404 * 'os_sync_lock'. Note: this will release the open seqid sync and
2405 2405 * 'os_sync_lock' if client recovery starts. Calling functions have to
2406 2406 * be prepared to handle this.
2407 2407 *
2408 2408 * 'recov' is returned as 1 if the CLOSE operation detected client recovery
2409 2409 * was needed and was started, and that the calling function should retry
2410 2410 * this function; otherwise it is returned as 0.
2411 2411 *
2412 2412 * Errors are returned via the nfs4_error_t parameter.
2413 2413 */
2414 2414 static void
2415 2415 nfs4close_otw(rnode4_t *rp, cred_t *cred_otw, nfs4_open_owner_t *oop,
2416 2416 nfs4_open_stream_t *osp, int *recov, int *did_start_seqid_syncp,
2417 2417 nfs4_close_type_t close_type, nfs4_error_t *ep, int *have_sync_lockp)
2418 2418 {
2419 2419 COMPOUND4args_clnt args;
2420 2420 COMPOUND4res_clnt res;
2421 2421 CLOSE4args *close_args;
2422 2422 nfs_resop4 *resop;
2423 2423 nfs_argop4 argop[3];
2424 2424 int doqueue = 1;
2425 2425 mntinfo4_t *mi;
2426 2426 seqid4 seqid;
2427 2427 vnode_t *vp;
2428 2428 bool_t needrecov = FALSE;
2429 2429 nfs4_lost_rqst_t lost_rqst;
2430 2430 hrtime_t t;
2431 2431
2432 2432 ASSERT(nfs_zone() == VTOMI4(RTOV4(rp))->mi_zone);
2433 2433
2434 2434 ASSERT(MUTEX_HELD(&osp->os_sync_lock));
2435 2435
2436 2436 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4close_otw"));
2437 2437
2438 2438 /* Only set this to 1 if recovery is started */
2439 2439 *recov = 0;
2440 2440
2441 2441 /* do the OTW call to close the file */
2442 2442
2443 2443 if (close_type == CLOSE_RESEND)
2444 2444 args.ctag = TAG_CLOSE_LOST;
2445 2445 else if (close_type == CLOSE_AFTER_RESEND)
2446 2446 args.ctag = TAG_CLOSE_UNDO;
2447 2447 else
2448 2448 args.ctag = TAG_CLOSE;
2449 2449
2450 2450 args.array_len = 3;
2451 2451 args.array = argop;
2452 2452
2453 2453 vp = RTOV4(rp);
2454 2454
2455 2455 mi = VTOMI4(vp);
2456 2456
2457 2457 /* putfh target fh */
2458 2458 argop[0].argop = OP_CPUTFH;
2459 2459 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
2460 2460
2461 2461 argop[1].argop = OP_GETATTR;
2462 2462 argop[1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
2463 2463 argop[1].nfs_argop4_u.opgetattr.mi = mi;
2464 2464
2465 2465 argop[2].argop = OP_CLOSE;
2466 2466 close_args = &argop[2].nfs_argop4_u.opclose;
2467 2467
2468 2468 seqid = nfs4_get_open_seqid(oop) + 1;
2469 2469
2470 2470 close_args->seqid = seqid;
2471 2471 close_args->open_stateid = osp->open_stateid;
2472 2472
2473 2473 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE,
2474 2474 "nfs4close_otw: %s call, rp %s", needrecov ? "recov" : "first",
2475 2475 rnode4info(rp)));
2476 2476
2477 2477 t = gethrtime();
2478 2478
2479 2479 rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, ep);
2480 2480
2481 2481 if (!ep->error && nfs4_need_to_bump_seqid(&res)) {
2482 2482 nfs4_set_open_seqid(seqid, oop, args.ctag);
2483 2483 }
2484 2484
2485 2485 needrecov = nfs4_needs_recovery(ep, TRUE, mi->mi_vfsp);
2486 2486 if (ep->error && !needrecov) {
2487 2487 /*
2488 2488 * if there was an error and no recovery is to be done
2489 2489 * then then set up the file to flush its cache if
2490 2490 * needed for the next caller.
2491 2491 */
2492 2492 mutex_enter(&rp->r_statelock);
2493 2493 PURGE_ATTRCACHE4_LOCKED(rp);
2494 2494 rp->r_flags &= ~R4WRITEMODIFIED;
2495 2495 mutex_exit(&rp->r_statelock);
2496 2496 return;
2497 2497 }
2498 2498
2499 2499 if (needrecov) {
2500 2500 bool_t abort;
2501 2501 nfs4_bseqid_entry_t *bsep = NULL;
2502 2502
2503 2503 if (close_type != CLOSE_RESEND)
2504 2504 nfs4close_save_lost_rqst(ep->error, &lost_rqst, oop,
2505 2505 osp, cred_otw, vp);
2506 2506
2507 2507 if (!ep->error && res.status == NFS4ERR_BAD_SEQID)
2508 2508 bsep = nfs4_create_bseqid_entry(oop, NULL, vp,
2509 2509 0, args.ctag, close_args->seqid);
2510 2510
2511 2511 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
2512 2512 "nfs4close_otw: initiating recovery. error %d "
2513 2513 "res.status %d", ep->error, res.status));
2514 2514
2515 2515 /*
2516 2516 * Drop the 'os_sync_lock' here so we don't hit
2517 2517 * a potential recursive mutex_enter via an
2518 2518 * 'open_stream_hold()'.
2519 2519 */
2520 2520 mutex_exit(&osp->os_sync_lock);
2521 2521 *have_sync_lockp = 0;
2522 2522 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL, NULL,
2523 2523 (close_type != CLOSE_RESEND &&
2524 2524 lost_rqst.lr_op == OP_CLOSE) ? &lost_rqst : NULL,
2525 2525 OP_CLOSE, bsep, NULL, NULL);
2526 2526
2527 2527 /* drop open seq sync, and let the calling function regrab it */
2528 2528 nfs4_end_open_seqid_sync(oop);
2529 2529 *did_start_seqid_syncp = 0;
2530 2530
2531 2531 if (bsep)
2532 2532 kmem_free(bsep, sizeof (*bsep));
2533 2533 /*
2534 2534 * For signals, the caller wants to quit, so don't say to
2535 2535 * retry. For forced unmount, if it's a user thread, it
2536 2536 * wants to quit. If it's a recovery thread, the retry
2537 2537 * will happen higher-up on the call stack. Either way,
2538 2538 * don't say to retry.
2539 2539 */
2540 2540 if (abort == FALSE && ep->error != EINTR &&
2541 2541 !NFS4_FRC_UNMT_ERR(ep->error, mi->mi_vfsp) &&
2542 2542 close_type != CLOSE_RESEND &&
2543 2543 close_type != CLOSE_AFTER_RESEND)
2544 2544 *recov = 1;
2545 2545 else
2546 2546 *recov = 0;
2547 2547
2548 2548 if (!ep->error)
2549 2549 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2550 2550 return;
2551 2551 }
2552 2552
2553 2553 if (res.status) {
2554 2554 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2555 2555 return;
2556 2556 }
2557 2557
2558 2558 mutex_enter(&rp->r_statev4_lock);
2559 2559 rp->created_v4 = 0;
2560 2560 mutex_exit(&rp->r_statev4_lock);
2561 2561
2562 2562 resop = &res.array[2];
2563 2563 osp->open_stateid = resop->nfs_resop4_u.opclose.open_stateid;
2564 2564 osp->os_valid = 0;
2565 2565
2566 2566 /*
2567 2567 * This removes the reference obtained at OPEN; ie, when the
↓ open down ↓ |
2567 lines elided |
↑ open up ↑ |
2568 2568 * open stream structure was created.
2569 2569 *
2570 2570 * We don't have to worry about calling 'open_stream_rele'
2571 2571 * since we our currently holding a reference to the open
2572 2572 * stream which means the count cannot go to 0 with this
2573 2573 * decrement.
2574 2574 */
2575 2575 ASSERT(osp->os_ref_count >= 2);
2576 2576 osp->os_ref_count--;
2577 2577
2578 - if (!ep->error)
2578 + if (ep->error == 0) {
2579 + /*
2580 + * Avoid a deadlock with the r_serial thread waiting for
2581 + * os_sync_lock in nfs4_get_otw_cred_by_osp() which might be
2582 + * held by us. We will wait in nfs4_attr_cache() for the
2583 + * completion of the r_serial thread.
2584 + */
2585 + mutex_exit(&osp->os_sync_lock);
2586 + *have_sync_lockp = 0;
2587 +
2579 2588 nfs4_attr_cache(vp,
2580 2589 &res.array[1].nfs_resop4_u.opgetattr.ga_res,
2581 2590 t, cred_otw, TRUE, NULL);
2591 + }
2582 2592
2583 2593 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4close_otw:"
2584 2594 " returning %d", ep->error));
2585 2595
2586 2596 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2587 2597 }
2588 2598
2589 2599 /* ARGSUSED */
2590 2600 static int
2591 2601 nfs4_read(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr,
2592 2602 caller_context_t *ct)
2593 2603 {
2594 2604 rnode4_t *rp;
2595 2605 u_offset_t off;
2596 2606 offset_t diff;
2597 2607 uint_t on;
2598 2608 uint_t n;
2599 2609 caddr_t base;
2600 2610 uint_t flags;
2601 2611 int error;
2602 2612 mntinfo4_t *mi;
2603 2613
2604 2614 rp = VTOR4(vp);
2605 2615
2606 2616 ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER));
2607 2617
2608 2618 if (IS_SHADOW(vp, rp))
2609 2619 vp = RTOV4(rp);
2610 2620
2611 2621 if (vp->v_type != VREG)
2612 2622 return (EISDIR);
2613 2623
2614 2624 mi = VTOMI4(vp);
2615 2625
2616 2626 if (nfs_zone() != mi->mi_zone)
2617 2627 return (EIO);
2618 2628
2619 2629 if (uiop->uio_resid == 0)
2620 2630 return (0);
2621 2631
2622 2632 if (uiop->uio_loffset < 0 || uiop->uio_loffset + uiop->uio_resid < 0)
2623 2633 return (EINVAL);
2624 2634
2625 2635 mutex_enter(&rp->r_statelock);
2626 2636 if (rp->r_flags & R4RECOVERRP)
2627 2637 error = (rp->r_error ? rp->r_error : EIO);
2628 2638 else
2629 2639 error = 0;
2630 2640 mutex_exit(&rp->r_statelock);
2631 2641 if (error)
2632 2642 return (error);
2633 2643
2634 2644 /*
2635 2645 * Bypass VM if caching has been disabled (e.g., locking) or if
2636 2646 * using client-side direct I/O and the file is not mmap'd and
2637 2647 * there are no cached pages.
2638 2648 */
2639 2649 if ((vp->v_flag & VNOCACHE) ||
2640 2650 (((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO)) &&
2641 2651 rp->r_mapcnt == 0 && rp->r_inmap == 0 && !nfs4_has_pages(vp))) {
2642 2652 size_t resid = 0;
2643 2653
2644 2654 return (nfs4read(vp, NULL, uiop->uio_loffset,
2645 2655 uiop->uio_resid, &resid, cr, FALSE, uiop));
2646 2656 }
2647 2657
2648 2658 error = 0;
2649 2659
2650 2660 do {
2651 2661 off = uiop->uio_loffset & MAXBMASK; /* mapping offset */
2652 2662 on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */
2653 2663 n = MIN(MAXBSIZE - on, uiop->uio_resid);
2654 2664
2655 2665 if (error = nfs4_validate_caches(vp, cr))
2656 2666 break;
2657 2667
2658 2668 mutex_enter(&rp->r_statelock);
2659 2669 while (rp->r_flags & R4INCACHEPURGE) {
2660 2670 if (!cv_wait_sig(&rp->r_cv, &rp->r_statelock)) {
2661 2671 mutex_exit(&rp->r_statelock);
2662 2672 return (EINTR);
2663 2673 }
2664 2674 }
2665 2675 diff = rp->r_size - uiop->uio_loffset;
2666 2676 mutex_exit(&rp->r_statelock);
2667 2677 if (diff <= 0)
2668 2678 break;
2669 2679 if (diff < n)
2670 2680 n = (uint_t)diff;
2671 2681
2672 2682 if (vpm_enable) {
2673 2683 /*
2674 2684 * Copy data.
2675 2685 */
2676 2686 error = vpm_data_copy(vp, off + on, n, uiop,
2677 2687 1, NULL, 0, S_READ);
2678 2688 } else {
2679 2689 base = segmap_getmapflt(segkmap, vp, off + on, n, 1,
2680 2690 S_READ);
2681 2691
2682 2692 error = uiomove(base + on, n, UIO_READ, uiop);
2683 2693 }
2684 2694
2685 2695 if (!error) {
2686 2696 /*
2687 2697 * If read a whole block or read to eof,
2688 2698 * won't need this buffer again soon.
2689 2699 */
2690 2700 mutex_enter(&rp->r_statelock);
2691 2701 if (n + on == MAXBSIZE ||
2692 2702 uiop->uio_loffset == rp->r_size)
2693 2703 flags = SM_DONTNEED;
2694 2704 else
2695 2705 flags = 0;
2696 2706 mutex_exit(&rp->r_statelock);
2697 2707 if (vpm_enable) {
2698 2708 error = vpm_sync_pages(vp, off, n, flags);
2699 2709 } else {
2700 2710 error = segmap_release(segkmap, base, flags);
2701 2711 }
2702 2712 } else {
2703 2713 if (vpm_enable) {
2704 2714 (void) vpm_sync_pages(vp, off, n, 0);
2705 2715 } else {
2706 2716 (void) segmap_release(segkmap, base, 0);
2707 2717 }
2708 2718 }
2709 2719 } while (!error && uiop->uio_resid > 0);
2710 2720
2711 2721 return (error);
2712 2722 }
2713 2723
2714 2724 /* ARGSUSED */
2715 2725 static int
2716 2726 nfs4_write(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr,
2717 2727 caller_context_t *ct)
2718 2728 {
2719 2729 rlim64_t limit = uiop->uio_llimit;
2720 2730 rnode4_t *rp;
2721 2731 u_offset_t off;
2722 2732 caddr_t base;
2723 2733 uint_t flags;
2724 2734 int remainder;
2725 2735 size_t n;
2726 2736 int on;
2727 2737 int error;
2728 2738 int resid;
2729 2739 u_offset_t offset;
2730 2740 mntinfo4_t *mi;
2731 2741 uint_t bsize;
2732 2742
2733 2743 rp = VTOR4(vp);
2734 2744
2735 2745 if (IS_SHADOW(vp, rp))
2736 2746 vp = RTOV4(rp);
2737 2747
2738 2748 if (vp->v_type != VREG)
2739 2749 return (EISDIR);
2740 2750
2741 2751 mi = VTOMI4(vp);
2742 2752
2743 2753 if (nfs_zone() != mi->mi_zone)
2744 2754 return (EIO);
2745 2755
2746 2756 if (uiop->uio_resid == 0)
2747 2757 return (0);
2748 2758
2749 2759 mutex_enter(&rp->r_statelock);
2750 2760 if (rp->r_flags & R4RECOVERRP)
2751 2761 error = (rp->r_error ? rp->r_error : EIO);
2752 2762 else
2753 2763 error = 0;
2754 2764 mutex_exit(&rp->r_statelock);
2755 2765 if (error)
2756 2766 return (error);
2757 2767
2758 2768 if (ioflag & FAPPEND) {
2759 2769 struct vattr va;
2760 2770
2761 2771 /*
2762 2772 * Must serialize if appending.
2763 2773 */
2764 2774 if (nfs_rw_lock_held(&rp->r_rwlock, RW_READER)) {
2765 2775 nfs_rw_exit(&rp->r_rwlock);
2766 2776 if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER,
2767 2777 INTR4(vp)))
2768 2778 return (EINTR);
2769 2779 }
2770 2780
2771 2781 va.va_mask = AT_SIZE;
2772 2782 error = nfs4getattr(vp, &va, cr);
2773 2783 if (error)
2774 2784 return (error);
2775 2785 uiop->uio_loffset = va.va_size;
2776 2786 }
2777 2787
2778 2788 offset = uiop->uio_loffset + uiop->uio_resid;
2779 2789
2780 2790 if (uiop->uio_loffset < (offset_t)0 || offset < 0)
2781 2791 return (EINVAL);
2782 2792
2783 2793 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
2784 2794 limit = MAXOFFSET_T;
2785 2795
2786 2796 /*
2787 2797 * Check to make sure that the process will not exceed
2788 2798 * its limit on file size. It is okay to write up to
2789 2799 * the limit, but not beyond. Thus, the write which
2790 2800 * reaches the limit will be short and the next write
2791 2801 * will return an error.
2792 2802 */
2793 2803 remainder = 0;
2794 2804 if (offset > uiop->uio_llimit) {
2795 2805 remainder = offset - uiop->uio_llimit;
2796 2806 uiop->uio_resid = uiop->uio_llimit - uiop->uio_loffset;
2797 2807 if (uiop->uio_resid <= 0) {
2798 2808 proc_t *p = ttoproc(curthread);
2799 2809
2800 2810 uiop->uio_resid += remainder;
2801 2811 mutex_enter(&p->p_lock);
2802 2812 (void) rctl_action(rctlproc_legacy[RLIMIT_FSIZE],
2803 2813 p->p_rctls, p, RCA_UNSAFE_SIGINFO);
2804 2814 mutex_exit(&p->p_lock);
2805 2815 return (EFBIG);
2806 2816 }
2807 2817 }
2808 2818
2809 2819 /* update the change attribute, if we have a write delegation */
2810 2820
2811 2821 mutex_enter(&rp->r_statev4_lock);
2812 2822 if (rp->r_deleg_type == OPEN_DELEGATE_WRITE)
2813 2823 rp->r_deleg_change++;
2814 2824
2815 2825 mutex_exit(&rp->r_statev4_lock);
2816 2826
2817 2827 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR4(vp)))
2818 2828 return (EINTR);
2819 2829
2820 2830 /*
2821 2831 * Bypass VM if caching has been disabled (e.g., locking) or if
2822 2832 * using client-side direct I/O and the file is not mmap'd and
2823 2833 * there are no cached pages.
2824 2834 */
2825 2835 if ((vp->v_flag & VNOCACHE) ||
2826 2836 (((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO)) &&
2827 2837 rp->r_mapcnt == 0 && rp->r_inmap == 0 && !nfs4_has_pages(vp))) {
2828 2838 size_t bufsize;
2829 2839 int count;
2830 2840 u_offset_t org_offset;
2831 2841 stable_how4 stab_comm;
2832 2842 nfs4_fwrite:
2833 2843 if (rp->r_flags & R4STALE) {
2834 2844 resid = uiop->uio_resid;
2835 2845 offset = uiop->uio_loffset;
2836 2846 error = rp->r_error;
2837 2847 /*
2838 2848 * A close may have cleared r_error, if so,
2839 2849 * propagate ESTALE error return properly
2840 2850 */
2841 2851 if (error == 0)
2842 2852 error = ESTALE;
2843 2853 goto bottom;
2844 2854 }
2845 2855
2846 2856 bufsize = MIN(uiop->uio_resid, mi->mi_stsize);
2847 2857 base = kmem_alloc(bufsize, KM_SLEEP);
2848 2858 do {
2849 2859 if (ioflag & FDSYNC)
2850 2860 stab_comm = DATA_SYNC4;
2851 2861 else
2852 2862 stab_comm = FILE_SYNC4;
2853 2863 resid = uiop->uio_resid;
2854 2864 offset = uiop->uio_loffset;
2855 2865 count = MIN(uiop->uio_resid, bufsize);
2856 2866 org_offset = uiop->uio_loffset;
2857 2867 error = uiomove(base, count, UIO_WRITE, uiop);
2858 2868 if (!error) {
2859 2869 error = nfs4write(vp, base, org_offset,
2860 2870 count, cr, &stab_comm);
2861 2871 if (!error) {
2862 2872 mutex_enter(&rp->r_statelock);
2863 2873 if (rp->r_size < uiop->uio_loffset)
2864 2874 rp->r_size = uiop->uio_loffset;
2865 2875 mutex_exit(&rp->r_statelock);
2866 2876 }
2867 2877 }
2868 2878 } while (!error && uiop->uio_resid > 0);
2869 2879 kmem_free(base, bufsize);
2870 2880 goto bottom;
2871 2881 }
2872 2882
2873 2883 bsize = vp->v_vfsp->vfs_bsize;
2874 2884
2875 2885 do {
2876 2886 off = uiop->uio_loffset & MAXBMASK; /* mapping offset */
2877 2887 on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */
2878 2888 n = MIN(MAXBSIZE - on, uiop->uio_resid);
2879 2889
2880 2890 resid = uiop->uio_resid;
2881 2891 offset = uiop->uio_loffset;
2882 2892
2883 2893 if (rp->r_flags & R4STALE) {
2884 2894 error = rp->r_error;
2885 2895 /*
2886 2896 * A close may have cleared r_error, if so,
2887 2897 * propagate ESTALE error return properly
2888 2898 */
2889 2899 if (error == 0)
2890 2900 error = ESTALE;
2891 2901 break;
2892 2902 }
2893 2903
2894 2904 /*
2895 2905 * Don't create dirty pages faster than they
2896 2906 * can be cleaned so that the system doesn't
2897 2907 * get imbalanced. If the async queue is
2898 2908 * maxed out, then wait for it to drain before
2899 2909 * creating more dirty pages. Also, wait for
2900 2910 * any threads doing pagewalks in the vop_getattr
2901 2911 * entry points so that they don't block for
2902 2912 * long periods.
2903 2913 */
2904 2914 mutex_enter(&rp->r_statelock);
2905 2915 while ((mi->mi_max_threads != 0 &&
2906 2916 rp->r_awcount > 2 * mi->mi_max_threads) ||
2907 2917 rp->r_gcount > 0) {
2908 2918 if (INTR4(vp)) {
2909 2919 klwp_t *lwp = ttolwp(curthread);
2910 2920
2911 2921 if (lwp != NULL)
2912 2922 lwp->lwp_nostop++;
2913 2923 if (!cv_wait_sig(&rp->r_cv, &rp->r_statelock)) {
2914 2924 mutex_exit(&rp->r_statelock);
2915 2925 if (lwp != NULL)
2916 2926 lwp->lwp_nostop--;
2917 2927 error = EINTR;
2918 2928 goto bottom;
2919 2929 }
2920 2930 if (lwp != NULL)
2921 2931 lwp->lwp_nostop--;
2922 2932 } else
2923 2933 cv_wait(&rp->r_cv, &rp->r_statelock);
2924 2934 }
2925 2935 mutex_exit(&rp->r_statelock);
2926 2936
2927 2937 /*
2928 2938 * Touch the page and fault it in if it is not in core
2929 2939 * before segmap_getmapflt or vpm_data_copy can lock it.
2930 2940 * This is to avoid the deadlock if the buffer is mapped
2931 2941 * to the same file through mmap which we want to write.
2932 2942 */
2933 2943 uio_prefaultpages((long)n, uiop);
2934 2944
2935 2945 if (vpm_enable) {
2936 2946 /*
2937 2947 * It will use kpm mappings, so no need to
2938 2948 * pass an address.
2939 2949 */
2940 2950 error = writerp4(rp, NULL, n, uiop, 0);
2941 2951 } else {
2942 2952 if (segmap_kpm) {
2943 2953 int pon = uiop->uio_loffset & PAGEOFFSET;
2944 2954 size_t pn = MIN(PAGESIZE - pon,
2945 2955 uiop->uio_resid);
2946 2956 int pagecreate;
2947 2957
2948 2958 mutex_enter(&rp->r_statelock);
2949 2959 pagecreate = (pon == 0) && (pn == PAGESIZE ||
2950 2960 uiop->uio_loffset + pn >= rp->r_size);
2951 2961 mutex_exit(&rp->r_statelock);
2952 2962
2953 2963 base = segmap_getmapflt(segkmap, vp, off + on,
2954 2964 pn, !pagecreate, S_WRITE);
2955 2965
2956 2966 error = writerp4(rp, base + pon, n, uiop,
2957 2967 pagecreate);
2958 2968
2959 2969 } else {
2960 2970 base = segmap_getmapflt(segkmap, vp, off + on,
2961 2971 n, 0, S_READ);
2962 2972 error = writerp4(rp, base + on, n, uiop, 0);
2963 2973 }
2964 2974 }
2965 2975
2966 2976 if (!error) {
2967 2977 if (mi->mi_flags & MI4_NOAC)
2968 2978 flags = SM_WRITE;
2969 2979 else if ((uiop->uio_loffset % bsize) == 0 ||
2970 2980 IS_SWAPVP(vp)) {
2971 2981 /*
2972 2982 * Have written a whole block.
2973 2983 * Start an asynchronous write
2974 2984 * and mark the buffer to
2975 2985 * indicate that it won't be
2976 2986 * needed again soon.
2977 2987 */
2978 2988 flags = SM_WRITE | SM_ASYNC | SM_DONTNEED;
2979 2989 } else
2980 2990 flags = 0;
2981 2991 if ((ioflag & (FSYNC|FDSYNC)) ||
2982 2992 (rp->r_flags & R4OUTOFSPACE)) {
2983 2993 flags &= ~SM_ASYNC;
2984 2994 flags |= SM_WRITE;
2985 2995 }
2986 2996 if (vpm_enable) {
2987 2997 error = vpm_sync_pages(vp, off, n, flags);
2988 2998 } else {
2989 2999 error = segmap_release(segkmap, base, flags);
2990 3000 }
2991 3001 } else {
2992 3002 if (vpm_enable) {
2993 3003 (void) vpm_sync_pages(vp, off, n, 0);
2994 3004 } else {
2995 3005 (void) segmap_release(segkmap, base, 0);
2996 3006 }
2997 3007 /*
2998 3008 * In the event that we got an access error while
2999 3009 * faulting in a page for a write-only file just
3000 3010 * force a write.
3001 3011 */
3002 3012 if (error == EACCES)
3003 3013 goto nfs4_fwrite;
3004 3014 }
3005 3015 } while (!error && uiop->uio_resid > 0);
3006 3016
3007 3017 bottom:
3008 3018 if (error) {
3009 3019 uiop->uio_resid = resid + remainder;
3010 3020 uiop->uio_loffset = offset;
3011 3021 } else {
3012 3022 uiop->uio_resid += remainder;
3013 3023
3014 3024 mutex_enter(&rp->r_statev4_lock);
3015 3025 if (rp->r_deleg_type == OPEN_DELEGATE_WRITE) {
3016 3026 gethrestime(&rp->r_attr.va_mtime);
3017 3027 rp->r_attr.va_ctime = rp->r_attr.va_mtime;
3018 3028 }
3019 3029 mutex_exit(&rp->r_statev4_lock);
3020 3030 }
3021 3031
3022 3032 nfs_rw_exit(&rp->r_lkserlock);
3023 3033
3024 3034 return (error);
3025 3035 }
3026 3036
3027 3037 /*
3028 3038 * Flags are composed of {B_ASYNC, B_INVAL, B_FREE, B_DONTNEED}
3029 3039 */
3030 3040 static int
3031 3041 nfs4_rdwrlbn(vnode_t *vp, page_t *pp, u_offset_t off, size_t len,
3032 3042 int flags, cred_t *cr)
3033 3043 {
3034 3044 struct buf *bp;
3035 3045 int error;
3036 3046 page_t *savepp;
3037 3047 uchar_t fsdata;
3038 3048 stable_how4 stab_comm;
3039 3049
3040 3050 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
3041 3051 bp = pageio_setup(pp, len, vp, flags);
3042 3052 ASSERT(bp != NULL);
3043 3053
3044 3054 /*
3045 3055 * pageio_setup should have set b_addr to 0. This
3046 3056 * is correct since we want to do I/O on a page
3047 3057 * boundary. bp_mapin will use this addr to calculate
3048 3058 * an offset, and then set b_addr to the kernel virtual
3049 3059 * address it allocated for us.
3050 3060 */
3051 3061 ASSERT(bp->b_un.b_addr == 0);
3052 3062
3053 3063 bp->b_edev = 0;
3054 3064 bp->b_dev = 0;
3055 3065 bp->b_lblkno = lbtodb(off);
3056 3066 bp->b_file = vp;
3057 3067 bp->b_offset = (offset_t)off;
3058 3068 bp_mapin(bp);
3059 3069
3060 3070 if ((flags & (B_WRITE|B_ASYNC)) == (B_WRITE|B_ASYNC) &&
3061 3071 freemem > desfree)
3062 3072 stab_comm = UNSTABLE4;
3063 3073 else
3064 3074 stab_comm = FILE_SYNC4;
3065 3075
3066 3076 error = nfs4_bio(bp, &stab_comm, cr, FALSE);
3067 3077
3068 3078 bp_mapout(bp);
3069 3079 pageio_done(bp);
3070 3080
3071 3081 if (stab_comm == UNSTABLE4)
3072 3082 fsdata = C_DELAYCOMMIT;
3073 3083 else
3074 3084 fsdata = C_NOCOMMIT;
3075 3085
3076 3086 savepp = pp;
3077 3087 do {
3078 3088 pp->p_fsdata = fsdata;
3079 3089 } while ((pp = pp->p_next) != savepp);
3080 3090
3081 3091 return (error);
3082 3092 }
3083 3093
3084 3094 /*
3085 3095 */
3086 3096 static int
3087 3097 nfs4rdwr_check_osid(vnode_t *vp, nfs4_error_t *ep, cred_t *cr)
3088 3098 {
3089 3099 nfs4_open_owner_t *oop;
3090 3100 nfs4_open_stream_t *osp;
3091 3101 rnode4_t *rp = VTOR4(vp);
3092 3102 mntinfo4_t *mi = VTOMI4(vp);
3093 3103 int reopen_needed;
3094 3104
3095 3105 ASSERT(nfs_zone() == mi->mi_zone);
3096 3106
3097 3107
3098 3108 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi);
3099 3109 if (!oop)
3100 3110 return (EIO);
3101 3111
3102 3112 /* returns with 'os_sync_lock' held */
3103 3113 osp = find_open_stream(oop, rp);
3104 3114 if (!osp) {
3105 3115 open_owner_rele(oop);
3106 3116 return (EIO);
3107 3117 }
3108 3118
3109 3119 if (osp->os_failed_reopen) {
3110 3120 mutex_exit(&osp->os_sync_lock);
3111 3121 open_stream_rele(osp, rp);
3112 3122 open_owner_rele(oop);
3113 3123 return (EIO);
3114 3124 }
3115 3125
3116 3126 /*
3117 3127 * Determine whether a reopen is needed. If this
3118 3128 * is a delegation open stream, then the os_delegation bit
3119 3129 * should be set.
3120 3130 */
3121 3131
3122 3132 reopen_needed = osp->os_delegation;
3123 3133
3124 3134 mutex_exit(&osp->os_sync_lock);
3125 3135 open_owner_rele(oop);
3126 3136
3127 3137 if (reopen_needed) {
3128 3138 nfs4_error_zinit(ep);
3129 3139 nfs4_reopen(vp, osp, ep, CLAIM_NULL, FALSE, FALSE);
3130 3140 mutex_enter(&osp->os_sync_lock);
3131 3141 if (ep->error || ep->stat || osp->os_failed_reopen) {
3132 3142 mutex_exit(&osp->os_sync_lock);
3133 3143 open_stream_rele(osp, rp);
3134 3144 return (EIO);
3135 3145 }
3136 3146 mutex_exit(&osp->os_sync_lock);
3137 3147 }
3138 3148 open_stream_rele(osp, rp);
3139 3149
3140 3150 return (0);
3141 3151 }
3142 3152
3143 3153 /*
3144 3154 * Write to file. Writes to remote server in largest size
3145 3155 * chunks that the server can handle. Write is synchronous.
3146 3156 */
3147 3157 static int
3148 3158 nfs4write(vnode_t *vp, caddr_t base, u_offset_t offset, int count, cred_t *cr,
3149 3159 stable_how4 *stab_comm)
3150 3160 {
3151 3161 mntinfo4_t *mi;
3152 3162 COMPOUND4args_clnt args;
3153 3163 COMPOUND4res_clnt res;
3154 3164 WRITE4args *wargs;
3155 3165 WRITE4res *wres;
3156 3166 nfs_argop4 argop[2];
3157 3167 nfs_resop4 *resop;
3158 3168 int tsize;
3159 3169 stable_how4 stable;
3160 3170 rnode4_t *rp;
3161 3171 int doqueue = 1;
3162 3172 bool_t needrecov;
3163 3173 nfs4_recov_state_t recov_state;
3164 3174 nfs4_stateid_types_t sid_types;
3165 3175 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
3166 3176 int recov;
3167 3177
3168 3178 rp = VTOR4(vp);
3169 3179 mi = VTOMI4(vp);
3170 3180
3171 3181 ASSERT(nfs_zone() == mi->mi_zone);
3172 3182
3173 3183 stable = *stab_comm;
3174 3184 *stab_comm = FILE_SYNC4;
3175 3185
3176 3186 needrecov = FALSE;
3177 3187 recov_state.rs_flags = 0;
3178 3188 recov_state.rs_num_retry_despite_err = 0;
3179 3189 nfs4_init_stateid_types(&sid_types);
3180 3190
3181 3191 /* Is curthread the recovery thread? */
3182 3192 mutex_enter(&mi->mi_lock);
3183 3193 recov = (mi->mi_recovthread == curthread);
3184 3194 mutex_exit(&mi->mi_lock);
3185 3195
3186 3196 recov_retry:
3187 3197 args.ctag = TAG_WRITE;
3188 3198 args.array_len = 2;
3189 3199 args.array = argop;
3190 3200
3191 3201 if (!recov) {
3192 3202 e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_WRITE,
3193 3203 &recov_state, NULL);
3194 3204 if (e.error)
3195 3205 return (e.error);
3196 3206 }
3197 3207
3198 3208 /* 0. putfh target fh */
3199 3209 argop[0].argop = OP_CPUTFH;
3200 3210 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
3201 3211
3202 3212 /* 1. write */
3203 3213 nfs4args_write(&argop[1], stable, rp, cr, &wargs, &sid_types);
3204 3214
3205 3215 do {
3206 3216
3207 3217 wargs->offset = (offset4)offset;
3208 3218 wargs->data_val = base;
3209 3219
3210 3220 if (mi->mi_io_kstats) {
3211 3221 mutex_enter(&mi->mi_lock);
3212 3222 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
3213 3223 mutex_exit(&mi->mi_lock);
3214 3224 }
3215 3225
3216 3226 if ((vp->v_flag & VNOCACHE) ||
3217 3227 (rp->r_flags & R4DIRECTIO) ||
3218 3228 (mi->mi_flags & MI4_DIRECTIO))
3219 3229 tsize = MIN(mi->mi_stsize, count);
3220 3230 else
3221 3231 tsize = MIN(mi->mi_curwrite, count);
3222 3232 wargs->data_len = (uint_t)tsize;
3223 3233 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
3224 3234
3225 3235 if (mi->mi_io_kstats) {
3226 3236 mutex_enter(&mi->mi_lock);
3227 3237 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
3228 3238 mutex_exit(&mi->mi_lock);
3229 3239 }
3230 3240
3231 3241 if (!recov) {
3232 3242 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
3233 3243 if (e.error && !needrecov) {
3234 3244 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE,
3235 3245 &recov_state, needrecov);
3236 3246 return (e.error);
3237 3247 }
3238 3248 } else {
3239 3249 if (e.error)
3240 3250 return (e.error);
3241 3251 }
3242 3252
3243 3253 /*
3244 3254 * Do handling of OLD_STATEID outside
3245 3255 * of the normal recovery framework.
3246 3256 *
3247 3257 * If write receives a BAD stateid error while using a
3248 3258 * delegation stateid, retry using the open stateid (if it
3249 3259 * exists). If it doesn't have an open stateid, reopen the
3250 3260 * file first, then retry.
3251 3261 */
3252 3262 if (!e.error && res.status == NFS4ERR_OLD_STATEID &&
3253 3263 sid_types.cur_sid_type != SPEC_SID) {
3254 3264 nfs4_save_stateid(&wargs->stateid, &sid_types);
3255 3265 if (!recov)
3256 3266 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE,
3257 3267 &recov_state, needrecov);
3258 3268 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3259 3269 goto recov_retry;
3260 3270 } else if (e.error == 0 && res.status == NFS4ERR_BAD_STATEID &&
3261 3271 sid_types.cur_sid_type == DEL_SID) {
3262 3272 nfs4_save_stateid(&wargs->stateid, &sid_types);
3263 3273 mutex_enter(&rp->r_statev4_lock);
3264 3274 rp->r_deleg_return_pending = TRUE;
3265 3275 mutex_exit(&rp->r_statev4_lock);
3266 3276 if (nfs4rdwr_check_osid(vp, &e, cr)) {
3267 3277 if (!recov)
3268 3278 nfs4_end_fop(mi, vp, NULL, OH_WRITE,
3269 3279 &recov_state, needrecov);
3270 3280 (void) xdr_free(xdr_COMPOUND4res_clnt,
3271 3281 (caddr_t)&res);
3272 3282 return (EIO);
3273 3283 }
3274 3284 if (!recov)
3275 3285 nfs4_end_fop(mi, vp, NULL, OH_WRITE,
3276 3286 &recov_state, needrecov);
3277 3287 /* hold needed for nfs4delegreturn_thread */
3278 3288 VN_HOLD(vp);
3279 3289 nfs4delegreturn_async(rp, (NFS4_DR_PUSH|NFS4_DR_REOPEN|
3280 3290 NFS4_DR_DISCARD), FALSE);
3281 3291 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3282 3292 goto recov_retry;
3283 3293 }
3284 3294
3285 3295 if (needrecov) {
3286 3296 bool_t abort;
3287 3297
3288 3298 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
3289 3299 "nfs4write: client got error %d, res.status %d"
3290 3300 ", so start recovery", e.error, res.status));
3291 3301
3292 3302 abort = nfs4_start_recovery(&e,
3293 3303 VTOMI4(vp), vp, NULL, &wargs->stateid,
3294 3304 NULL, OP_WRITE, NULL, NULL, NULL);
3295 3305 if (!e.error) {
3296 3306 e.error = geterrno4(res.status);
3297 3307 (void) xdr_free(xdr_COMPOUND4res_clnt,
3298 3308 (caddr_t)&res);
3299 3309 }
3300 3310 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE,
3301 3311 &recov_state, needrecov);
3302 3312 if (abort == FALSE)
3303 3313 goto recov_retry;
3304 3314 return (e.error);
3305 3315 }
3306 3316
3307 3317 if (res.status) {
3308 3318 e.error = geterrno4(res.status);
3309 3319 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3310 3320 if (!recov)
3311 3321 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE,
3312 3322 &recov_state, needrecov);
3313 3323 return (e.error);
3314 3324 }
3315 3325
3316 3326 resop = &res.array[1]; /* write res */
3317 3327 wres = &resop->nfs_resop4_u.opwrite;
3318 3328
3319 3329 if ((int)wres->count > tsize) {
3320 3330 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3321 3331
3322 3332 zcmn_err(getzoneid(), CE_WARN,
3323 3333 "nfs4write: server wrote %u, requested was %u",
3324 3334 (int)wres->count, tsize);
3325 3335 if (!recov)
3326 3336 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE,
3327 3337 &recov_state, needrecov);
3328 3338 return (EIO);
3329 3339 }
3330 3340 if (wres->committed == UNSTABLE4) {
3331 3341 *stab_comm = UNSTABLE4;
3332 3342 if (wargs->stable == DATA_SYNC4 ||
3333 3343 wargs->stable == FILE_SYNC4) {
3334 3344 (void) xdr_free(xdr_COMPOUND4res_clnt,
3335 3345 (caddr_t)&res);
3336 3346 zcmn_err(getzoneid(), CE_WARN,
3337 3347 "nfs4write: server %s did not commit "
3338 3348 "to stable storage",
3339 3349 rp->r_server->sv_hostname);
3340 3350 if (!recov)
3341 3351 nfs4_end_fop(VTOMI4(vp), vp, NULL,
3342 3352 OH_WRITE, &recov_state, needrecov);
3343 3353 return (EIO);
3344 3354 }
3345 3355 }
3346 3356
3347 3357 tsize = (int)wres->count;
3348 3358 count -= tsize;
3349 3359 base += tsize;
3350 3360 offset += tsize;
3351 3361 if (mi->mi_io_kstats) {
3352 3362 mutex_enter(&mi->mi_lock);
3353 3363 KSTAT_IO_PTR(mi->mi_io_kstats)->writes++;
3354 3364 KSTAT_IO_PTR(mi->mi_io_kstats)->nwritten +=
3355 3365 tsize;
3356 3366 mutex_exit(&mi->mi_lock);
3357 3367 }
3358 3368 lwp_stat_update(LWP_STAT_OUBLK, 1);
3359 3369 mutex_enter(&rp->r_statelock);
3360 3370 if (rp->r_flags & R4HAVEVERF) {
3361 3371 if (rp->r_writeverf != wres->writeverf) {
3362 3372 nfs4_set_mod(vp);
3363 3373 rp->r_writeverf = wres->writeverf;
3364 3374 }
3365 3375 } else {
3366 3376 rp->r_writeverf = wres->writeverf;
3367 3377 rp->r_flags |= R4HAVEVERF;
3368 3378 }
3369 3379 PURGE_ATTRCACHE4_LOCKED(rp);
3370 3380 rp->r_flags |= R4WRITEMODIFIED;
3371 3381 gethrestime(&rp->r_attr.va_mtime);
3372 3382 rp->r_attr.va_ctime = rp->r_attr.va_mtime;
3373 3383 mutex_exit(&rp->r_statelock);
3374 3384 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3375 3385 } while (count);
3376 3386
3377 3387 if (!recov)
3378 3388 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, &recov_state,
3379 3389 needrecov);
3380 3390
3381 3391 return (e.error);
3382 3392 }
3383 3393
3384 3394 /*
3385 3395 * Read from a file. Reads data in largest chunks our interface can handle.
3386 3396 */
3387 3397 static int
3388 3398 nfs4read(vnode_t *vp, caddr_t base, offset_t offset, int count,
3389 3399 size_t *residp, cred_t *cr, bool_t async, struct uio *uiop)
3390 3400 {
3391 3401 mntinfo4_t *mi;
3392 3402 COMPOUND4args_clnt args;
3393 3403 COMPOUND4res_clnt res;
3394 3404 READ4args *rargs;
3395 3405 nfs_argop4 argop[2];
3396 3406 int tsize;
3397 3407 int doqueue;
3398 3408 rnode4_t *rp;
3399 3409 int data_len;
3400 3410 bool_t is_eof;
3401 3411 bool_t needrecov = FALSE;
3402 3412 nfs4_recov_state_t recov_state;
3403 3413 nfs4_stateid_types_t sid_types;
3404 3414 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
3405 3415
3406 3416 rp = VTOR4(vp);
3407 3417 mi = VTOMI4(vp);
3408 3418 doqueue = 1;
3409 3419
3410 3420 ASSERT(nfs_zone() == mi->mi_zone);
3411 3421
3412 3422 args.ctag = async ? TAG_READAHEAD : TAG_READ;
3413 3423
3414 3424 args.array_len = 2;
3415 3425 args.array = argop;
3416 3426
3417 3427 nfs4_init_stateid_types(&sid_types);
3418 3428
3419 3429 recov_state.rs_flags = 0;
3420 3430 recov_state.rs_num_retry_despite_err = 0;
3421 3431
3422 3432 recov_retry:
3423 3433 e.error = nfs4_start_fop(mi, vp, NULL, OH_READ,
3424 3434 &recov_state, NULL);
3425 3435 if (e.error)
3426 3436 return (e.error);
3427 3437
3428 3438 /* putfh target fh */
3429 3439 argop[0].argop = OP_CPUTFH;
3430 3440 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
3431 3441
3432 3442 /* read */
3433 3443 argop[1].argop = OP_READ;
3434 3444 rargs = &argop[1].nfs_argop4_u.opread;
3435 3445 rargs->stateid = nfs4_get_stateid(cr, rp, curproc->p_pidp->pid_id, mi,
3436 3446 OP_READ, &sid_types, async);
3437 3447
3438 3448 do {
3439 3449 if (mi->mi_io_kstats) {
3440 3450 mutex_enter(&mi->mi_lock);
3441 3451 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
3442 3452 mutex_exit(&mi->mi_lock);
3443 3453 }
3444 3454
3445 3455 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE,
3446 3456 "nfs4read: %s call, rp %s",
3447 3457 needrecov ? "recov" : "first",
3448 3458 rnode4info(rp)));
3449 3459
3450 3460 if ((vp->v_flag & VNOCACHE) ||
3451 3461 (rp->r_flags & R4DIRECTIO) ||
3452 3462 (mi->mi_flags & MI4_DIRECTIO))
3453 3463 tsize = MIN(mi->mi_tsize, count);
3454 3464 else
3455 3465 tsize = MIN(mi->mi_curread, count);
3456 3466
3457 3467 rargs->offset = (offset4)offset;
3458 3468 rargs->count = (count4)tsize;
3459 3469 rargs->res_data_val_alt = NULL;
3460 3470 rargs->res_mblk = NULL;
3461 3471 rargs->res_uiop = NULL;
3462 3472 rargs->res_maxsize = 0;
3463 3473 rargs->wlist = NULL;
3464 3474
3465 3475 if (uiop)
3466 3476 rargs->res_uiop = uiop;
3467 3477 else
3468 3478 rargs->res_data_val_alt = base;
3469 3479 rargs->res_maxsize = tsize;
3470 3480
3471 3481 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
3472 3482 #ifdef DEBUG
3473 3483 if (nfs4read_error_inject) {
3474 3484 res.status = nfs4read_error_inject;
3475 3485 nfs4read_error_inject = 0;
3476 3486 }
3477 3487 #endif
3478 3488
3479 3489 if (mi->mi_io_kstats) {
3480 3490 mutex_enter(&mi->mi_lock);
3481 3491 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
3482 3492 mutex_exit(&mi->mi_lock);
3483 3493 }
3484 3494
3485 3495 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
3486 3496 if (e.error != 0 && !needrecov) {
3487 3497 nfs4_end_fop(mi, vp, NULL, OH_READ,
3488 3498 &recov_state, needrecov);
3489 3499 return (e.error);
3490 3500 }
3491 3501
3492 3502 /*
3493 3503 * Do proper retry for OLD and BAD stateid errors outside
3494 3504 * of the normal recovery framework. There are two differences
3495 3505 * between async and sync reads. The first is that we allow
3496 3506 * retry on BAD_STATEID for async reads, but not sync reads.
3497 3507 * The second is that we mark the file dead for a failed
3498 3508 * attempt with a special stateid for sync reads, but just
3499 3509 * return EIO for async reads.
3500 3510 *
3501 3511 * If a sync read receives a BAD stateid error while using a
3502 3512 * delegation stateid, retry using the open stateid (if it
3503 3513 * exists). If it doesn't have an open stateid, reopen the
3504 3514 * file first, then retry.
3505 3515 */
3506 3516 if (e.error == 0 && (res.status == NFS4ERR_OLD_STATEID ||
3507 3517 res.status == NFS4ERR_BAD_STATEID) && async) {
3508 3518 nfs4_end_fop(mi, vp, NULL, OH_READ,
3509 3519 &recov_state, needrecov);
3510 3520 if (sid_types.cur_sid_type == SPEC_SID) {
3511 3521 (void) xdr_free(xdr_COMPOUND4res_clnt,
3512 3522 (caddr_t)&res);
3513 3523 return (EIO);
3514 3524 }
3515 3525 nfs4_save_stateid(&rargs->stateid, &sid_types);
3516 3526 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3517 3527 goto recov_retry;
3518 3528 } else if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID &&
3519 3529 !async && sid_types.cur_sid_type != SPEC_SID) {
3520 3530 nfs4_save_stateid(&rargs->stateid, &sid_types);
3521 3531 nfs4_end_fop(mi, vp, NULL, OH_READ,
3522 3532 &recov_state, needrecov);
3523 3533 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3524 3534 goto recov_retry;
3525 3535 } else if (e.error == 0 && res.status == NFS4ERR_BAD_STATEID &&
3526 3536 sid_types.cur_sid_type == DEL_SID) {
3527 3537 nfs4_save_stateid(&rargs->stateid, &sid_types);
3528 3538 mutex_enter(&rp->r_statev4_lock);
3529 3539 rp->r_deleg_return_pending = TRUE;
3530 3540 mutex_exit(&rp->r_statev4_lock);
3531 3541 if (nfs4rdwr_check_osid(vp, &e, cr)) {
3532 3542 nfs4_end_fop(mi, vp, NULL, OH_READ,
3533 3543 &recov_state, needrecov);
3534 3544 (void) xdr_free(xdr_COMPOUND4res_clnt,
3535 3545 (caddr_t)&res);
3536 3546 return (EIO);
3537 3547 }
3538 3548 nfs4_end_fop(mi, vp, NULL, OH_READ,
3539 3549 &recov_state, needrecov);
3540 3550 /* hold needed for nfs4delegreturn_thread */
3541 3551 VN_HOLD(vp);
3542 3552 nfs4delegreturn_async(rp, (NFS4_DR_PUSH|NFS4_DR_REOPEN|
3543 3553 NFS4_DR_DISCARD), FALSE);
3544 3554 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3545 3555 goto recov_retry;
3546 3556 }
3547 3557 if (needrecov) {
3548 3558 bool_t abort;
3549 3559
3550 3560 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
3551 3561 "nfs4read: initiating recovery\n"));
3552 3562 abort = nfs4_start_recovery(&e,
3553 3563 mi, vp, NULL, &rargs->stateid,
3554 3564 NULL, OP_READ, NULL, NULL, NULL);
3555 3565 nfs4_end_fop(mi, vp, NULL, OH_READ,
3556 3566 &recov_state, needrecov);
3557 3567 /*
3558 3568 * Do not retry if we got OLD_STATEID using a special
3559 3569 * stateid. This avoids looping with a broken server.
3560 3570 */
3561 3571 if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID &&
3562 3572 sid_types.cur_sid_type == SPEC_SID)
3563 3573 abort = TRUE;
3564 3574
3565 3575 if (abort == FALSE) {
3566 3576 /*
3567 3577 * Need to retry all possible stateids in
3568 3578 * case the recovery error wasn't stateid
3569 3579 * related or the stateids have become
3570 3580 * stale (server reboot).
3571 3581 */
3572 3582 nfs4_init_stateid_types(&sid_types);
3573 3583 (void) xdr_free(xdr_COMPOUND4res_clnt,
3574 3584 (caddr_t)&res);
3575 3585 goto recov_retry;
3576 3586 }
3577 3587
3578 3588 if (!e.error) {
3579 3589 e.error = geterrno4(res.status);
3580 3590 (void) xdr_free(xdr_COMPOUND4res_clnt,
3581 3591 (caddr_t)&res);
3582 3592 }
3583 3593 return (e.error);
3584 3594 }
3585 3595
3586 3596 if (res.status) {
3587 3597 e.error = geterrno4(res.status);
3588 3598 nfs4_end_fop(mi, vp, NULL, OH_READ,
3589 3599 &recov_state, needrecov);
3590 3600 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3591 3601 return (e.error);
3592 3602 }
3593 3603
3594 3604 data_len = res.array[1].nfs_resop4_u.opread.data_len;
3595 3605 count -= data_len;
3596 3606 if (base)
3597 3607 base += data_len;
3598 3608 offset += data_len;
3599 3609 if (mi->mi_io_kstats) {
3600 3610 mutex_enter(&mi->mi_lock);
3601 3611 KSTAT_IO_PTR(mi->mi_io_kstats)->reads++;
3602 3612 KSTAT_IO_PTR(mi->mi_io_kstats)->nread += data_len;
3603 3613 mutex_exit(&mi->mi_lock);
3604 3614 }
3605 3615 lwp_stat_update(LWP_STAT_INBLK, 1);
3606 3616 is_eof = res.array[1].nfs_resop4_u.opread.eof;
3607 3617 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3608 3618
3609 3619 } while (count && !is_eof);
3610 3620
3611 3621 *residp = count;
3612 3622
3613 3623 nfs4_end_fop(mi, vp, NULL, OH_READ, &recov_state, needrecov);
3614 3624
3615 3625 return (e.error);
3616 3626 }
3617 3627
3618 3628 /* ARGSUSED */
3619 3629 static int
3620 3630 nfs4_ioctl(vnode_t *vp, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp,
3621 3631 caller_context_t *ct)
3622 3632 {
3623 3633 if (nfs_zone() != VTOMI4(vp)->mi_zone)
3624 3634 return (EIO);
3625 3635 switch (cmd) {
3626 3636 case _FIODIRECTIO:
3627 3637 return (nfs4_directio(vp, (int)arg, cr));
3628 3638 default:
3629 3639 return (ENOTTY);
3630 3640 }
3631 3641 }
3632 3642
3633 3643 /* ARGSUSED */
3634 3644 int
3635 3645 nfs4_getattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr,
3636 3646 caller_context_t *ct)
3637 3647 {
3638 3648 int error;
3639 3649 rnode4_t *rp = VTOR4(vp);
3640 3650
3641 3651 if (nfs_zone() != VTOMI4(vp)->mi_zone)
3642 3652 return (EIO);
3643 3653 /*
3644 3654 * If it has been specified that the return value will
3645 3655 * just be used as a hint, and we are only being asked
3646 3656 * for size, fsid or rdevid, then return the client's
3647 3657 * notion of these values without checking to make sure
3648 3658 * that the attribute cache is up to date.
3649 3659 * The whole point is to avoid an over the wire GETATTR
3650 3660 * call.
3651 3661 */
3652 3662 if (flags & ATTR_HINT) {
3653 3663 if (!(vap->va_mask & ~(AT_SIZE | AT_FSID | AT_RDEV))) {
3654 3664 mutex_enter(&rp->r_statelock);
3655 3665 if (vap->va_mask & AT_SIZE)
3656 3666 vap->va_size = rp->r_size;
3657 3667 if (vap->va_mask & AT_FSID)
3658 3668 vap->va_fsid = rp->r_attr.va_fsid;
3659 3669 if (vap->va_mask & AT_RDEV)
3660 3670 vap->va_rdev = rp->r_attr.va_rdev;
3661 3671 mutex_exit(&rp->r_statelock);
3662 3672 return (0);
3663 3673 }
3664 3674 }
3665 3675
3666 3676 /*
3667 3677 * Only need to flush pages if asking for the mtime
3668 3678 * and if there any dirty pages or any outstanding
3669 3679 * asynchronous (write) requests for this file.
3670 3680 */
3671 3681 if (vap->va_mask & AT_MTIME) {
3672 3682 rp = VTOR4(vp);
3673 3683 if (nfs4_has_pages(vp)) {
3674 3684 mutex_enter(&rp->r_statev4_lock);
3675 3685 if (rp->r_deleg_type != OPEN_DELEGATE_WRITE) {
3676 3686 mutex_exit(&rp->r_statev4_lock);
3677 3687 if (rp->r_flags & R4DIRTY ||
3678 3688 rp->r_awcount > 0) {
3679 3689 mutex_enter(&rp->r_statelock);
3680 3690 rp->r_gcount++;
3681 3691 mutex_exit(&rp->r_statelock);
3682 3692 error =
3683 3693 nfs4_putpage(vp, (u_offset_t)0,
3684 3694 0, 0, cr, NULL);
3685 3695 mutex_enter(&rp->r_statelock);
3686 3696 if (error && (error == ENOSPC ||
3687 3697 error == EDQUOT)) {
3688 3698 if (!rp->r_error)
3689 3699 rp->r_error = error;
3690 3700 }
3691 3701 if (--rp->r_gcount == 0)
3692 3702 cv_broadcast(&rp->r_cv);
3693 3703 mutex_exit(&rp->r_statelock);
3694 3704 }
3695 3705 } else {
3696 3706 mutex_exit(&rp->r_statev4_lock);
3697 3707 }
3698 3708 }
3699 3709 }
3700 3710 return (nfs4getattr(vp, vap, cr));
3701 3711 }
3702 3712
3703 3713 int
3704 3714 nfs4_compare_modes(mode_t from_server, mode_t on_client)
3705 3715 {
3706 3716 /*
3707 3717 * If these are the only two bits cleared
3708 3718 * on the server then return 0 (OK) else
3709 3719 * return 1 (BAD).
3710 3720 */
3711 3721 on_client &= ~(S_ISUID|S_ISGID);
3712 3722 if (on_client == from_server)
3713 3723 return (0);
3714 3724 else
3715 3725 return (1);
3716 3726 }
3717 3727
3718 3728 /*ARGSUSED4*/
3719 3729 static int
3720 3730 nfs4_setattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr,
3721 3731 caller_context_t *ct)
3722 3732 {
3723 3733 int error;
3724 3734
3725 3735 if (vap->va_mask & AT_NOSET)
3726 3736 return (EINVAL);
3727 3737
3728 3738 if (nfs_zone() != VTOMI4(vp)->mi_zone)
3729 3739 return (EIO);
3730 3740
3731 3741 /*
3732 3742 * Don't call secpolicy_vnode_setattr, the client cannot
3733 3743 * use its cached attributes to make security decisions
3734 3744 * as the server may be faking mode bits or mapping uid/gid.
3735 3745 * Always just let the server to the checking.
3736 3746 * If we provide the ability to remove basic priviledges
3737 3747 * to setattr (e.g. basic without chmod) then we will
3738 3748 * need to add a check here before calling the server.
3739 3749 */
3740 3750 error = nfs4setattr(vp, vap, flags, cr, NULL);
3741 3751
3742 3752 if (error == 0 && (vap->va_mask & AT_SIZE) && vap->va_size == 0)
3743 3753 vnevent_truncate(vp, ct);
3744 3754
3745 3755 return (error);
3746 3756 }
3747 3757
3748 3758 /*
3749 3759 * To replace the "guarded" version 3 setattr, we use two types of compound
3750 3760 * setattr requests:
3751 3761 * 1. The "normal" setattr, used when the size of the file isn't being
3752 3762 * changed - { Putfh <fh>; Setattr; Getattr }/
3753 3763 * 2. If the size is changed, precede Setattr with: Getattr; Verify
3754 3764 * with only ctime as the argument. If the server ctime differs from
3755 3765 * what is cached on the client, the verify will fail, but we would
3756 3766 * already have the ctime from the preceding getattr, so just set it
3757 3767 * and retry. Thus the compound here is - { Putfh <fh>; Getattr; Verify;
3758 3768 * Setattr; Getattr }.
3759 3769 *
3760 3770 * The vsecattr_t * input parameter will be non-NULL if ACLs are being set in
3761 3771 * this setattr and NULL if they are not.
3762 3772 */
3763 3773 static int
3764 3774 nfs4setattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr,
3765 3775 vsecattr_t *vsap)
3766 3776 {
3767 3777 COMPOUND4args_clnt args;
3768 3778 COMPOUND4res_clnt res, *resp = NULL;
3769 3779 nfs4_ga_res_t *garp = NULL;
3770 3780 int numops = 3; /* { Putfh; Setattr; Getattr } */
3771 3781 nfs_argop4 argop[5];
3772 3782 int verify_argop = -1;
3773 3783 int setattr_argop = 1;
3774 3784 nfs_resop4 *resop;
3775 3785 vattr_t va;
3776 3786 rnode4_t *rp;
3777 3787 int doqueue = 1;
3778 3788 uint_t mask = vap->va_mask;
3779 3789 mode_t omode;
3780 3790 vsecattr_t *vsp;
3781 3791 timestruc_t ctime;
3782 3792 bool_t needrecov = FALSE;
3783 3793 nfs4_recov_state_t recov_state;
3784 3794 nfs4_stateid_types_t sid_types;
3785 3795 stateid4 stateid;
3786 3796 hrtime_t t;
3787 3797 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
3788 3798 servinfo4_t *svp;
3789 3799 bitmap4 supp_attrs;
3790 3800
3791 3801 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
3792 3802 rp = VTOR4(vp);
3793 3803 nfs4_init_stateid_types(&sid_types);
3794 3804
3795 3805 /*
3796 3806 * Only need to flush pages if there are any pages and
3797 3807 * if the file is marked as dirty in some fashion. The
3798 3808 * file must be flushed so that we can accurately
3799 3809 * determine the size of the file and the cached data
3800 3810 * after the SETATTR returns. A file is considered to
3801 3811 * be dirty if it is either marked with R4DIRTY, has
3802 3812 * outstanding i/o's active, or is mmap'd. In this
3803 3813 * last case, we can't tell whether there are dirty
3804 3814 * pages, so we flush just to be sure.
3805 3815 */
3806 3816 if (nfs4_has_pages(vp) &&
3807 3817 ((rp->r_flags & R4DIRTY) ||
3808 3818 rp->r_count > 0 ||
3809 3819 rp->r_mapcnt > 0)) {
3810 3820 ASSERT(vp->v_type != VCHR);
3811 3821 e.error = nfs4_putpage(vp, (offset_t)0, 0, 0, cr, NULL);
3812 3822 if (e.error && (e.error == ENOSPC || e.error == EDQUOT)) {
3813 3823 mutex_enter(&rp->r_statelock);
3814 3824 if (!rp->r_error)
3815 3825 rp->r_error = e.error;
3816 3826 mutex_exit(&rp->r_statelock);
3817 3827 }
3818 3828 }
3819 3829
3820 3830 if (mask & AT_SIZE) {
3821 3831 /*
3822 3832 * Verification setattr compound for non-deleg AT_SIZE:
3823 3833 * { Putfh; Getattr; Verify; Setattr; Getattr }
3824 3834 * Set ctime local here (outside the do_again label)
3825 3835 * so that subsequent retries (after failed VERIFY)
3826 3836 * will use ctime from GETATTR results (from failed
3827 3837 * verify compound) as VERIFY arg.
3828 3838 * If file has delegation, then VERIFY(time_metadata)
3829 3839 * is of little added value, so don't bother.
3830 3840 */
3831 3841 mutex_enter(&rp->r_statev4_lock);
3832 3842 if (rp->r_deleg_type == OPEN_DELEGATE_NONE ||
3833 3843 rp->r_deleg_return_pending) {
3834 3844 numops = 5;
3835 3845 ctime = rp->r_attr.va_ctime;
3836 3846 }
3837 3847 mutex_exit(&rp->r_statev4_lock);
3838 3848 }
3839 3849
3840 3850 recov_state.rs_flags = 0;
3841 3851 recov_state.rs_num_retry_despite_err = 0;
3842 3852
3843 3853 args.ctag = TAG_SETATTR;
3844 3854 do_again:
3845 3855 recov_retry:
3846 3856 setattr_argop = numops - 2;
3847 3857
3848 3858 args.array = argop;
3849 3859 args.array_len = numops;
3850 3860
3851 3861 e.error = nfs4_start_op(VTOMI4(vp), vp, NULL, &recov_state);
3852 3862 if (e.error)
3853 3863 return (e.error);
3854 3864
3855 3865
3856 3866 /* putfh target fh */
3857 3867 argop[0].argop = OP_CPUTFH;
3858 3868 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
3859 3869
3860 3870 if (numops == 5) {
3861 3871 /*
3862 3872 * We only care about the ctime, but need to get mtime
3863 3873 * and size for proper cache update.
3864 3874 */
3865 3875 /* getattr */
3866 3876 argop[1].argop = OP_GETATTR;
3867 3877 argop[1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
3868 3878 argop[1].nfs_argop4_u.opgetattr.mi = VTOMI4(vp);
3869 3879
3870 3880 /* verify - set later in loop */
3871 3881 verify_argop = 2;
3872 3882 }
3873 3883
3874 3884 /* setattr */
3875 3885 svp = rp->r_server;
3876 3886 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
3877 3887 supp_attrs = svp->sv_supp_attrs;
3878 3888 nfs_rw_exit(&svp->sv_lock);
3879 3889
3880 3890 nfs4args_setattr(&argop[setattr_argop], vap, vsap, flags, rp, cr,
3881 3891 supp_attrs, &e.error, &sid_types);
3882 3892 stateid = argop[setattr_argop].nfs_argop4_u.opsetattr.stateid;
3883 3893 if (e.error) {
3884 3894 /* req time field(s) overflow - return immediately */
3885 3895 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov);
3886 3896 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u.
3887 3897 opsetattr.obj_attributes);
3888 3898 return (e.error);
3889 3899 }
3890 3900 omode = rp->r_attr.va_mode;
3891 3901
3892 3902 /* getattr */
3893 3903 argop[numops-1].argop = OP_GETATTR;
3894 3904 argop[numops-1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
3895 3905 /*
3896 3906 * If we are setting the ACL (indicated only by vsap != NULL), request
3897 3907 * the ACL in this getattr. The ACL returned from this getattr will be
3898 3908 * used in updating the ACL cache.
3899 3909 */
3900 3910 if (vsap != NULL)
3901 3911 argop[numops-1].nfs_argop4_u.opgetattr.attr_request |=
3902 3912 FATTR4_ACL_MASK;
3903 3913 argop[numops-1].nfs_argop4_u.opgetattr.mi = VTOMI4(vp);
3904 3914
3905 3915 /*
3906 3916 * setattr iterates if the object size is set and the cached ctime
3907 3917 * does not match the file ctime. In that case, verify the ctime first.
3908 3918 */
3909 3919
3910 3920 do {
3911 3921 if (verify_argop != -1) {
3912 3922 /*
3913 3923 * Verify that the ctime match before doing setattr.
3914 3924 */
3915 3925 va.va_mask = AT_CTIME;
3916 3926 va.va_ctime = ctime;
3917 3927 svp = rp->r_server;
3918 3928 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
3919 3929 supp_attrs = svp->sv_supp_attrs;
3920 3930 nfs_rw_exit(&svp->sv_lock);
3921 3931 e.error = nfs4args_verify(&argop[verify_argop], &va,
3922 3932 OP_VERIFY, supp_attrs);
3923 3933 if (e.error) {
3924 3934 /* req time field(s) overflow - return */
3925 3935 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state,
3926 3936 needrecov);
3927 3937 break;
3928 3938 }
3929 3939 }
3930 3940
3931 3941 doqueue = 1;
3932 3942
3933 3943 t = gethrtime();
3934 3944
3935 3945 rfs4call(VTOMI4(vp), &args, &res, cr, &doqueue, 0, &e);
3936 3946
3937 3947 /*
3938 3948 * Purge the access cache and ACL cache if changing either the
3939 3949 * owner of the file, the group owner, or the mode. These may
3940 3950 * change the access permissions of the file, so purge old
3941 3951 * information and start over again.
3942 3952 */
3943 3953 if (mask & (AT_UID | AT_GID | AT_MODE)) {
3944 3954 (void) nfs4_access_purge_rp(rp);
3945 3955 if (rp->r_secattr != NULL) {
3946 3956 mutex_enter(&rp->r_statelock);
3947 3957 vsp = rp->r_secattr;
3948 3958 rp->r_secattr = NULL;
3949 3959 mutex_exit(&rp->r_statelock);
3950 3960 if (vsp != NULL)
3951 3961 nfs4_acl_free_cache(vsp);
3952 3962 }
3953 3963 }
3954 3964
3955 3965 /*
3956 3966 * If res.array_len == numops, then everything succeeded,
3957 3967 * except for possibly the final getattr. If only the
3958 3968 * last getattr failed, give up, and don't try recovery.
3959 3969 */
3960 3970 if (res.array_len == numops) {
3961 3971 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state,
3962 3972 needrecov);
3963 3973 if (! e.error)
3964 3974 resp = &res;
3965 3975 break;
3966 3976 }
3967 3977
3968 3978 /*
3969 3979 * if either rpc call failed or completely succeeded - done
3970 3980 */
3971 3981 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp);
3972 3982 if (e.error) {
3973 3983 PURGE_ATTRCACHE4(vp);
3974 3984 if (!needrecov) {
3975 3985 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state,
3976 3986 needrecov);
3977 3987 break;
3978 3988 }
3979 3989 }
3980 3990
3981 3991 /*
3982 3992 * Do proper retry for OLD_STATEID outside of the normal
3983 3993 * recovery framework.
3984 3994 */
3985 3995 if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID &&
3986 3996 sid_types.cur_sid_type != SPEC_SID &&
3987 3997 sid_types.cur_sid_type != NO_SID) {
3988 3998 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state,
3989 3999 needrecov);
3990 4000 nfs4_save_stateid(&stateid, &sid_types);
3991 4001 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u.
3992 4002 opsetattr.obj_attributes);
3993 4003 if (verify_argop != -1) {
3994 4004 nfs4args_verify_free(&argop[verify_argop]);
3995 4005 verify_argop = -1;
3996 4006 }
3997 4007 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3998 4008 goto recov_retry;
3999 4009 }
4000 4010
4001 4011 if (needrecov) {
4002 4012 bool_t abort;
4003 4013
4004 4014 abort = nfs4_start_recovery(&e,
4005 4015 VTOMI4(vp), vp, NULL, NULL, NULL,
4006 4016 OP_SETATTR, NULL, NULL, NULL);
4007 4017 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state,
4008 4018 needrecov);
4009 4019 /*
4010 4020 * Do not retry if we failed with OLD_STATEID using
4011 4021 * a special stateid. This is done to avoid looping
4012 4022 * with a broken server.
4013 4023 */
4014 4024 if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID &&
4015 4025 (sid_types.cur_sid_type == SPEC_SID ||
4016 4026 sid_types.cur_sid_type == NO_SID))
4017 4027 abort = TRUE;
4018 4028 if (!e.error) {
4019 4029 if (res.status == NFS4ERR_BADOWNER)
4020 4030 nfs4_log_badowner(VTOMI4(vp),
4021 4031 OP_SETATTR);
4022 4032
4023 4033 e.error = geterrno4(res.status);
4024 4034 (void) xdr_free(xdr_COMPOUND4res_clnt,
4025 4035 (caddr_t)&res);
4026 4036 }
4027 4037 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u.
4028 4038 opsetattr.obj_attributes);
4029 4039 if (verify_argop != -1) {
4030 4040 nfs4args_verify_free(&argop[verify_argop]);
4031 4041 verify_argop = -1;
4032 4042 }
4033 4043 if (abort == FALSE) {
4034 4044 /*
4035 4045 * Need to retry all possible stateids in
4036 4046 * case the recovery error wasn't stateid
4037 4047 * related or the stateids have become
4038 4048 * stale (server reboot).
4039 4049 */
4040 4050 nfs4_init_stateid_types(&sid_types);
4041 4051 goto recov_retry;
4042 4052 }
4043 4053 return (e.error);
4044 4054 }
4045 4055
4046 4056 /*
4047 4057 * Need to call nfs4_end_op before nfs4getattr to
4048 4058 * avoid potential nfs4_start_op deadlock. See RFE
4049 4059 * 4777612. Calls to nfs4_invalidate_pages() and
4050 4060 * nfs4_purge_stale_fh() might also generate over the
4051 4061 * wire calls which my cause nfs4_start_op() deadlock.
4052 4062 */
4053 4063 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov);
4054 4064
4055 4065 /*
4056 4066 * Check to update lease.
4057 4067 */
4058 4068 resp = &res;
4059 4069 if (res.status == NFS4_OK) {
4060 4070 break;
4061 4071 }
4062 4072
4063 4073 /*
4064 4074 * Check if verify failed to see if try again
4065 4075 */
4066 4076 if ((verify_argop == -1) || (res.array_len != 3)) {
4067 4077 /*
4068 4078 * can't continue...
4069 4079 */
4070 4080 if (res.status == NFS4ERR_BADOWNER)
4071 4081 nfs4_log_badowner(VTOMI4(vp), OP_SETATTR);
4072 4082
4073 4083 e.error = geterrno4(res.status);
4074 4084 } else {
4075 4085 /*
4076 4086 * When the verify request fails, the client ctime is
4077 4087 * not in sync with the server. This is the same as
4078 4088 * the version 3 "not synchronized" error, and we
4079 4089 * handle it in a similar manner (XXX do we need to???).
4080 4090 * Use the ctime returned in the first getattr for
4081 4091 * the input to the next verify.
4082 4092 * If we couldn't get the attributes, then we give up
4083 4093 * because we can't complete the operation as required.
4084 4094 */
4085 4095 garp = &res.array[1].nfs_resop4_u.opgetattr.ga_res;
4086 4096 }
4087 4097 if (e.error) {
4088 4098 PURGE_ATTRCACHE4(vp);
4089 4099 nfs4_purge_stale_fh(e.error, vp, cr);
4090 4100 } else {
4091 4101 /*
4092 4102 * retry with a new verify value
4093 4103 */
4094 4104 ctime = garp->n4g_va.va_ctime;
4095 4105 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4096 4106 resp = NULL;
4097 4107 }
4098 4108 if (!e.error) {
4099 4109 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u.
4100 4110 opsetattr.obj_attributes);
4101 4111 if (verify_argop != -1) {
4102 4112 nfs4args_verify_free(&argop[verify_argop]);
4103 4113 verify_argop = -1;
4104 4114 }
4105 4115 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4106 4116 goto do_again;
4107 4117 }
4108 4118 } while (!e.error);
4109 4119
4110 4120 if (e.error) {
4111 4121 /*
4112 4122 * If we are here, rfs4call has an irrecoverable error - return
4113 4123 */
4114 4124 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u.
4115 4125 opsetattr.obj_attributes);
4116 4126 if (verify_argop != -1) {
4117 4127 nfs4args_verify_free(&argop[verify_argop]);
4118 4128 verify_argop = -1;
4119 4129 }
4120 4130 if (resp)
4121 4131 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
4122 4132 return (e.error);
4123 4133 }
4124 4134
4125 4135
4126 4136
4127 4137 /*
4128 4138 * If changing the size of the file, invalidate
4129 4139 * any local cached data which is no longer part
4130 4140 * of the file. We also possibly invalidate the
4131 4141 * last page in the file. We could use
4132 4142 * pvn_vpzero(), but this would mark the page as
4133 4143 * modified and require it to be written back to
4134 4144 * the server for no particularly good reason.
4135 4145 * This way, if we access it, then we bring it
4136 4146 * back in. A read should be cheaper than a
4137 4147 * write.
4138 4148 */
4139 4149 if (mask & AT_SIZE) {
4140 4150 nfs4_invalidate_pages(vp, (vap->va_size & PAGEMASK), cr);
4141 4151 }
4142 4152
4143 4153 /* either no error or one of the postop getattr failed */
4144 4154
4145 4155 /*
4146 4156 * XXX Perform a simplified version of wcc checking. Instead of
4147 4157 * have another getattr to get pre-op, just purge cache if
4148 4158 * any of the ops prior to and including the getattr failed.
4149 4159 * If the getattr succeeded then update the attrcache accordingly.
4150 4160 */
4151 4161
4152 4162 garp = NULL;
4153 4163 if (res.status == NFS4_OK) {
4154 4164 /*
4155 4165 * Last getattr
4156 4166 */
4157 4167 resop = &res.array[numops - 1];
4158 4168 garp = &resop->nfs_resop4_u.opgetattr.ga_res;
4159 4169 }
4160 4170 /*
4161 4171 * In certain cases, nfs4_update_attrcache() will purge the attrcache,
4162 4172 * rather than filling it. See the function itself for details.
4163 4173 */
4164 4174 e.error = nfs4_update_attrcache(res.status, garp, t, vp, cr);
4165 4175 if (garp != NULL) {
4166 4176 if (garp->n4g_resbmap & FATTR4_ACL_MASK) {
4167 4177 nfs4_acl_fill_cache(rp, &garp->n4g_vsa);
4168 4178 vs_ace4_destroy(&garp->n4g_vsa);
4169 4179 } else {
4170 4180 if (vsap != NULL) {
4171 4181 /*
4172 4182 * The ACL was supposed to be set and to be
4173 4183 * returned in the last getattr of this
4174 4184 * compound, but for some reason the getattr
4175 4185 * result doesn't contain the ACL. In this
4176 4186 * case, purge the ACL cache.
4177 4187 */
4178 4188 if (rp->r_secattr != NULL) {
4179 4189 mutex_enter(&rp->r_statelock);
4180 4190 vsp = rp->r_secattr;
4181 4191 rp->r_secattr = NULL;
4182 4192 mutex_exit(&rp->r_statelock);
4183 4193 if (vsp != NULL)
4184 4194 nfs4_acl_free_cache(vsp);
4185 4195 }
4186 4196 }
4187 4197 }
4188 4198 }
4189 4199
4190 4200 if (res.status == NFS4_OK && (mask & AT_SIZE)) {
4191 4201 /*
4192 4202 * Set the size, rather than relying on getting it updated
4193 4203 * via a GETATTR. With delegations the client tries to
4194 4204 * suppress GETATTR calls.
4195 4205 */
4196 4206 mutex_enter(&rp->r_statelock);
4197 4207 rp->r_size = vap->va_size;
4198 4208 mutex_exit(&rp->r_statelock);
4199 4209 }
4200 4210
4201 4211 /*
4202 4212 * Can free up request args and res
4203 4213 */
4204 4214 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u.
4205 4215 opsetattr.obj_attributes);
4206 4216 if (verify_argop != -1) {
4207 4217 nfs4args_verify_free(&argop[verify_argop]);
4208 4218 verify_argop = -1;
4209 4219 }
4210 4220 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4211 4221
4212 4222 /*
4213 4223 * Some servers will change the mode to clear the setuid
4214 4224 * and setgid bits when changing the uid or gid. The
4215 4225 * client needs to compensate appropriately.
4216 4226 */
4217 4227 if (mask & (AT_UID | AT_GID)) {
4218 4228 int terror, do_setattr;
4219 4229
4220 4230 do_setattr = 0;
4221 4231 va.va_mask = AT_MODE;
4222 4232 terror = nfs4getattr(vp, &va, cr);
4223 4233 if (!terror &&
4224 4234 (((mask & AT_MODE) && va.va_mode != vap->va_mode) ||
4225 4235 (!(mask & AT_MODE) && va.va_mode != omode))) {
4226 4236 va.va_mask = AT_MODE;
4227 4237 if (mask & AT_MODE) {
4228 4238 /*
4229 4239 * We asked the mode to be changed and what
4230 4240 * we just got from the server in getattr is
4231 4241 * not what we wanted it to be, so set it now.
4232 4242 */
4233 4243 va.va_mode = vap->va_mode;
4234 4244 do_setattr = 1;
4235 4245 } else {
4236 4246 /*
4237 4247 * We did not ask the mode to be changed,
4238 4248 * Check to see that the server just cleared
4239 4249 * I_SUID and I_GUID from it. If not then
4240 4250 * set mode to omode with UID/GID cleared.
4241 4251 */
4242 4252 if (nfs4_compare_modes(va.va_mode, omode)) {
4243 4253 omode &= ~(S_ISUID|S_ISGID);
4244 4254 va.va_mode = omode;
4245 4255 do_setattr = 1;
4246 4256 }
4247 4257 }
4248 4258
4249 4259 if (do_setattr)
4250 4260 (void) nfs4setattr(vp, &va, 0, cr, NULL);
4251 4261 }
4252 4262 }
4253 4263
4254 4264 return (e.error);
4255 4265 }
4256 4266
4257 4267 /* ARGSUSED */
4258 4268 static int
4259 4269 nfs4_access(vnode_t *vp, int mode, int flags, cred_t *cr, caller_context_t *ct)
4260 4270 {
4261 4271 COMPOUND4args_clnt args;
4262 4272 COMPOUND4res_clnt res;
4263 4273 int doqueue;
4264 4274 uint32_t acc, resacc, argacc;
4265 4275 rnode4_t *rp;
4266 4276 cred_t *cred, *ncr, *ncrfree = NULL;
4267 4277 nfs4_access_type_t cacc;
4268 4278 int num_ops;
4269 4279 nfs_argop4 argop[3];
4270 4280 nfs_resop4 *resop;
4271 4281 bool_t needrecov = FALSE, do_getattr;
4272 4282 nfs4_recov_state_t recov_state;
4273 4283 int rpc_error;
4274 4284 hrtime_t t;
4275 4285 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
4276 4286 mntinfo4_t *mi = VTOMI4(vp);
4277 4287
4278 4288 if (nfs_zone() != mi->mi_zone)
4279 4289 return (EIO);
4280 4290
4281 4291 acc = 0;
4282 4292 if (mode & VREAD)
4283 4293 acc |= ACCESS4_READ;
4284 4294 if (mode & VWRITE) {
4285 4295 if ((vp->v_vfsp->vfs_flag & VFS_RDONLY) && !ISVDEV(vp->v_type))
4286 4296 return (EROFS);
4287 4297 if (vp->v_type == VDIR)
4288 4298 acc |= ACCESS4_DELETE;
4289 4299 acc |= ACCESS4_MODIFY | ACCESS4_EXTEND;
4290 4300 }
4291 4301 if (mode & VEXEC) {
4292 4302 if (vp->v_type == VDIR)
4293 4303 acc |= ACCESS4_LOOKUP;
4294 4304 else
4295 4305 acc |= ACCESS4_EXECUTE;
4296 4306 }
4297 4307
4298 4308 if (VTOR4(vp)->r_acache != NULL) {
4299 4309 e.error = nfs4_validate_caches(vp, cr);
4300 4310 if (e.error)
4301 4311 return (e.error);
4302 4312 }
4303 4313
4304 4314 rp = VTOR4(vp);
4305 4315 if (vp->v_type == VDIR)
4306 4316 argacc = ACCESS4_READ | ACCESS4_DELETE | ACCESS4_MODIFY |
4307 4317 ACCESS4_EXTEND | ACCESS4_LOOKUP;
4308 4318 else
4309 4319 argacc = ACCESS4_READ | ACCESS4_MODIFY | ACCESS4_EXTEND |
4310 4320 ACCESS4_EXECUTE;
4311 4321 recov_state.rs_flags = 0;
4312 4322 recov_state.rs_num_retry_despite_err = 0;
4313 4323
4314 4324 cred = cr;
4315 4325 /*
4316 4326 * ncr and ncrfree both initially
4317 4327 * point to the memory area returned
4318 4328 * by crnetadjust();
4319 4329 * ncrfree not NULL when exiting means
4320 4330 * that we need to release it
4321 4331 */
4322 4332 ncr = crnetadjust(cred);
4323 4333 ncrfree = ncr;
4324 4334
4325 4335 tryagain:
4326 4336 cacc = nfs4_access_check(rp, acc, cred);
4327 4337 if (cacc == NFS4_ACCESS_ALLOWED) {
4328 4338 if (ncrfree != NULL)
4329 4339 crfree(ncrfree);
4330 4340 return (0);
4331 4341 }
4332 4342 if (cacc == NFS4_ACCESS_DENIED) {
4333 4343 /*
4334 4344 * If the cred can be adjusted, try again
4335 4345 * with the new cred.
4336 4346 */
4337 4347 if (ncr != NULL) {
4338 4348 cred = ncr;
4339 4349 ncr = NULL;
4340 4350 goto tryagain;
4341 4351 }
4342 4352 if (ncrfree != NULL)
4343 4353 crfree(ncrfree);
4344 4354 return (EACCES);
4345 4355 }
4346 4356
4347 4357 recov_retry:
4348 4358 /*
4349 4359 * Don't take with r_statev4_lock here. r_deleg_type could
4350 4360 * change as soon as lock is released. Since it is an int,
4351 4361 * there is no atomicity issue.
4352 4362 */
4353 4363 do_getattr = (rp->r_deleg_type == OPEN_DELEGATE_NONE);
4354 4364 num_ops = do_getattr ? 3 : 2;
4355 4365
4356 4366 args.ctag = TAG_ACCESS;
4357 4367
4358 4368 args.array_len = num_ops;
4359 4369 args.array = argop;
4360 4370
4361 4371 if (e.error = nfs4_start_fop(mi, vp, NULL, OH_ACCESS,
4362 4372 &recov_state, NULL)) {
4363 4373 if (ncrfree != NULL)
4364 4374 crfree(ncrfree);
4365 4375 return (e.error);
4366 4376 }
4367 4377
4368 4378 /* putfh target fh */
4369 4379 argop[0].argop = OP_CPUTFH;
4370 4380 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh;
4371 4381
4372 4382 /* access */
4373 4383 argop[1].argop = OP_ACCESS;
4374 4384 argop[1].nfs_argop4_u.opaccess.access = argacc;
4375 4385
4376 4386 /* getattr */
4377 4387 if (do_getattr) {
4378 4388 argop[2].argop = OP_GETATTR;
4379 4389 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
4380 4390 argop[2].nfs_argop4_u.opgetattr.mi = mi;
4381 4391 }
4382 4392
4383 4393 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE,
4384 4394 "nfs4_access: %s call, rp %s", needrecov ? "recov" : "first",
4385 4395 rnode4info(VTOR4(vp))));
4386 4396
4387 4397 doqueue = 1;
4388 4398 t = gethrtime();
4389 4399 rfs4call(VTOMI4(vp), &args, &res, cred, &doqueue, 0, &e);
4390 4400 rpc_error = e.error;
4391 4401
4392 4402 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp);
4393 4403 if (needrecov) {
4394 4404 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
4395 4405 "nfs4_access: initiating recovery\n"));
4396 4406
4397 4407 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL,
4398 4408 NULL, OP_ACCESS, NULL, NULL, NULL) == FALSE) {
4399 4409 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_ACCESS,
4400 4410 &recov_state, needrecov);
4401 4411 if (!e.error)
4402 4412 (void) xdr_free(xdr_COMPOUND4res_clnt,
4403 4413 (caddr_t)&res);
4404 4414 goto recov_retry;
4405 4415 }
4406 4416 }
4407 4417 nfs4_end_fop(mi, vp, NULL, OH_ACCESS, &recov_state, needrecov);
4408 4418
4409 4419 if (e.error)
4410 4420 goto out;
4411 4421
4412 4422 if (res.status) {
4413 4423 e.error = geterrno4(res.status);
4414 4424 /*
4415 4425 * This might generate over the wire calls throught
4416 4426 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op()
4417 4427 * here to avoid a deadlock.
4418 4428 */
4419 4429 nfs4_purge_stale_fh(e.error, vp, cr);
4420 4430 goto out;
4421 4431 }
4422 4432 resop = &res.array[1]; /* access res */
4423 4433
4424 4434 resacc = resop->nfs_resop4_u.opaccess.access;
4425 4435
4426 4436 if (do_getattr) {
4427 4437 resop++; /* getattr res */
4428 4438 nfs4_attr_cache(vp, &resop->nfs_resop4_u.opgetattr.ga_res,
4429 4439 t, cr, FALSE, NULL);
4430 4440 }
4431 4441
4432 4442 if (!e.error) {
4433 4443 nfs4_access_cache(rp, argacc, resacc, cred);
4434 4444 /*
4435 4445 * we just cached results with cred; if cred is the
4436 4446 * adjusted credentials from crnetadjust, we do not want
4437 4447 * to release them before exiting: hence setting ncrfree
4438 4448 * to NULL
4439 4449 */
4440 4450 if (cred != cr)
4441 4451 ncrfree = NULL;
4442 4452 /* XXX check the supported bits too? */
4443 4453 if ((acc & resacc) != acc) {
4444 4454 /*
4445 4455 * The following code implements the semantic
4446 4456 * that a setuid root program has *at least* the
4447 4457 * permissions of the user that is running the
4448 4458 * program. See rfs3call() for more portions
4449 4459 * of the implementation of this functionality.
4450 4460 */
4451 4461 /* XXX-LP */
4452 4462 if (ncr != NULL) {
4453 4463 (void) xdr_free(xdr_COMPOUND4res_clnt,
4454 4464 (caddr_t)&res);
4455 4465 cred = ncr;
4456 4466 ncr = NULL;
4457 4467 goto tryagain;
4458 4468 }
4459 4469 e.error = EACCES;
4460 4470 }
4461 4471 }
4462 4472
4463 4473 out:
4464 4474 if (!rpc_error)
4465 4475 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4466 4476
4467 4477 if (ncrfree != NULL)
4468 4478 crfree(ncrfree);
4469 4479
4470 4480 return (e.error);
4471 4481 }
4472 4482
4473 4483 /* ARGSUSED */
4474 4484 static int
4475 4485 nfs4_readlink(vnode_t *vp, struct uio *uiop, cred_t *cr, caller_context_t *ct)
4476 4486 {
4477 4487 COMPOUND4args_clnt args;
4478 4488 COMPOUND4res_clnt res;
4479 4489 int doqueue;
4480 4490 rnode4_t *rp;
4481 4491 nfs_argop4 argop[3];
4482 4492 nfs_resop4 *resop;
4483 4493 READLINK4res *lr_res;
4484 4494 nfs4_ga_res_t *garp;
4485 4495 uint_t len;
4486 4496 char *linkdata;
4487 4497 bool_t needrecov = FALSE;
4488 4498 nfs4_recov_state_t recov_state;
4489 4499 hrtime_t t;
4490 4500 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
4491 4501
4492 4502 if (nfs_zone() != VTOMI4(vp)->mi_zone)
4493 4503 return (EIO);
4494 4504 /*
4495 4505 * Can't readlink anything other than a symbolic link.
4496 4506 */
4497 4507 if (vp->v_type != VLNK)
4498 4508 return (EINVAL);
4499 4509
4500 4510 rp = VTOR4(vp);
4501 4511 if (nfs4_do_symlink_cache && rp->r_symlink.contents != NULL) {
4502 4512 e.error = nfs4_validate_caches(vp, cr);
4503 4513 if (e.error)
4504 4514 return (e.error);
4505 4515 mutex_enter(&rp->r_statelock);
4506 4516 if (rp->r_symlink.contents != NULL) {
4507 4517 e.error = uiomove(rp->r_symlink.contents,
4508 4518 rp->r_symlink.len, UIO_READ, uiop);
4509 4519 mutex_exit(&rp->r_statelock);
4510 4520 return (e.error);
4511 4521 }
4512 4522 mutex_exit(&rp->r_statelock);
4513 4523 }
4514 4524 recov_state.rs_flags = 0;
4515 4525 recov_state.rs_num_retry_despite_err = 0;
4516 4526
4517 4527 recov_retry:
4518 4528 args.array_len = 3;
4519 4529 args.array = argop;
4520 4530 args.ctag = TAG_READLINK;
4521 4531
4522 4532 e.error = nfs4_start_op(VTOMI4(vp), vp, NULL, &recov_state);
4523 4533 if (e.error) {
4524 4534 return (e.error);
4525 4535 }
4526 4536
4527 4537 /* 0. putfh symlink fh */
4528 4538 argop[0].argop = OP_CPUTFH;
4529 4539 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh;
4530 4540
4531 4541 /* 1. readlink */
4532 4542 argop[1].argop = OP_READLINK;
4533 4543
4534 4544 /* 2. getattr */
4535 4545 argop[2].argop = OP_GETATTR;
4536 4546 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
4537 4547 argop[2].nfs_argop4_u.opgetattr.mi = VTOMI4(vp);
4538 4548
4539 4549 doqueue = 1;
4540 4550
4541 4551 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE,
4542 4552 "nfs4_readlink: %s call, rp %s", needrecov ? "recov" : "first",
4543 4553 rnode4info(VTOR4(vp))));
4544 4554
4545 4555 t = gethrtime();
4546 4556
4547 4557 rfs4call(VTOMI4(vp), &args, &res, cr, &doqueue, 0, &e);
4548 4558
4549 4559 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp);
4550 4560 if (needrecov) {
4551 4561 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
4552 4562 "nfs4_readlink: initiating recovery\n"));
4553 4563
4554 4564 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL,
4555 4565 NULL, OP_READLINK, NULL, NULL, NULL) == FALSE) {
4556 4566 if (!e.error)
4557 4567 (void) xdr_free(xdr_COMPOUND4res_clnt,
4558 4568 (caddr_t)&res);
4559 4569
4560 4570 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state,
4561 4571 needrecov);
4562 4572 goto recov_retry;
4563 4573 }
4564 4574 }
4565 4575
4566 4576 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov);
4567 4577
4568 4578 if (e.error)
4569 4579 return (e.error);
4570 4580
4571 4581 /*
4572 4582 * There is an path in the code below which calls
4573 4583 * nfs4_purge_stale_fh(), which may generate otw calls through
4574 4584 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op()
4575 4585 * here to avoid nfs4_start_op() deadlock.
4576 4586 */
4577 4587
4578 4588 if (res.status && (res.array_len < args.array_len)) {
4579 4589 /*
4580 4590 * either Putfh or Link failed
4581 4591 */
4582 4592 e.error = geterrno4(res.status);
4583 4593 nfs4_purge_stale_fh(e.error, vp, cr);
4584 4594 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4585 4595 return (e.error);
4586 4596 }
4587 4597
4588 4598 resop = &res.array[1]; /* readlink res */
4589 4599 lr_res = &resop->nfs_resop4_u.opreadlink;
4590 4600
4591 4601 /*
4592 4602 * treat symlink names as data
4593 4603 */
4594 4604 linkdata = utf8_to_str((utf8string *)&lr_res->link, &len, NULL);
4595 4605 if (linkdata != NULL) {
4596 4606 int uio_len = len - 1;
4597 4607 /* len includes null byte, which we won't uiomove */
4598 4608 e.error = uiomove(linkdata, uio_len, UIO_READ, uiop);
4599 4609 if (nfs4_do_symlink_cache && rp->r_symlink.contents == NULL) {
4600 4610 mutex_enter(&rp->r_statelock);
4601 4611 if (rp->r_symlink.contents == NULL) {
4602 4612 rp->r_symlink.contents = linkdata;
4603 4613 rp->r_symlink.len = uio_len;
4604 4614 rp->r_symlink.size = len;
4605 4615 mutex_exit(&rp->r_statelock);
4606 4616 } else {
4607 4617 mutex_exit(&rp->r_statelock);
4608 4618 kmem_free(linkdata, len);
4609 4619 }
4610 4620 } else {
4611 4621 kmem_free(linkdata, len);
4612 4622 }
4613 4623 }
4614 4624 if (res.status == NFS4_OK) {
4615 4625 resop++; /* getattr res */
4616 4626 garp = &resop->nfs_resop4_u.opgetattr.ga_res;
4617 4627 }
4618 4628 e.error = nfs4_update_attrcache(res.status, garp, t, vp, cr);
4619 4629
4620 4630 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4621 4631
4622 4632 /*
4623 4633 * The over the wire error for attempting to readlink something
4624 4634 * other than a symbolic link is ENXIO. However, we need to
4625 4635 * return EINVAL instead of ENXIO, so we map it here.
4626 4636 */
4627 4637 return (e.error == ENXIO ? EINVAL : e.error);
4628 4638 }
4629 4639
4630 4640 /*
4631 4641 * Flush local dirty pages to stable storage on the server.
4632 4642 *
4633 4643 * If FNODSYNC is specified, then there is nothing to do because
4634 4644 * metadata changes are not cached on the client before being
4635 4645 * sent to the server.
4636 4646 */
4637 4647 /* ARGSUSED */
4638 4648 static int
4639 4649 nfs4_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
4640 4650 {
4641 4651 int error;
4642 4652
4643 4653 if ((syncflag & FNODSYNC) || IS_SWAPVP(vp))
4644 4654 return (0);
4645 4655 if (nfs_zone() != VTOMI4(vp)->mi_zone)
4646 4656 return (EIO);
4647 4657 error = nfs4_putpage_commit(vp, (offset_t)0, 0, cr);
4648 4658 if (!error)
4649 4659 error = VTOR4(vp)->r_error;
4650 4660 return (error);
4651 4661 }
4652 4662
4653 4663 /*
4654 4664 * Weirdness: if the file was removed or the target of a rename
4655 4665 * operation while it was open, it got renamed instead. Here we
4656 4666 * remove the renamed file.
4657 4667 */
4658 4668 /* ARGSUSED */
4659 4669 void
4660 4670 nfs4_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
4661 4671 {
4662 4672 rnode4_t *rp;
4663 4673
4664 4674 ASSERT(vp != DNLC_NO_VNODE);
4665 4675
4666 4676 rp = VTOR4(vp);
4667 4677
4668 4678 if (IS_SHADOW(vp, rp)) {
4669 4679 sv_inactive(vp);
4670 4680 return;
4671 4681 }
4672 4682
4673 4683 /*
4674 4684 * If this is coming from the wrong zone, we let someone in the right
4675 4685 * zone take care of it asynchronously. We can get here due to
4676 4686 * VN_RELE() being called from pageout() or fsflush(). This call may
4677 4687 * potentially turn into an expensive no-op if, for instance, v_count
4678 4688 * gets incremented in the meantime, but it's still correct.
4679 4689 */
4680 4690 if (nfs_zone() != VTOMI4(vp)->mi_zone) {
4681 4691 nfs4_async_inactive(vp, cr);
4682 4692 return;
4683 4693 }
4684 4694
4685 4695 /*
4686 4696 * Some of the cleanup steps might require over-the-wire
4687 4697 * operations. Since VOP_INACTIVE can get called as a result of
4688 4698 * other over-the-wire operations (e.g., an attribute cache update
4689 4699 * can lead to a DNLC purge), doing those steps now would lead to a
4690 4700 * nested call to the recovery framework, which can deadlock. So
4691 4701 * do any over-the-wire cleanups asynchronously, in a separate
4692 4702 * thread.
4693 4703 */
4694 4704
4695 4705 mutex_enter(&rp->r_os_lock);
4696 4706 mutex_enter(&rp->r_statelock);
4697 4707 mutex_enter(&rp->r_statev4_lock);
4698 4708
4699 4709 if (vp->v_type == VREG && list_head(&rp->r_open_streams) != NULL) {
4700 4710 mutex_exit(&rp->r_statev4_lock);
4701 4711 mutex_exit(&rp->r_statelock);
4702 4712 mutex_exit(&rp->r_os_lock);
4703 4713 nfs4_async_inactive(vp, cr);
4704 4714 return;
4705 4715 }
4706 4716
4707 4717 if (rp->r_deleg_type == OPEN_DELEGATE_READ ||
4708 4718 rp->r_deleg_type == OPEN_DELEGATE_WRITE) {
4709 4719 mutex_exit(&rp->r_statev4_lock);
4710 4720 mutex_exit(&rp->r_statelock);
4711 4721 mutex_exit(&rp->r_os_lock);
4712 4722 nfs4_async_inactive(vp, cr);
4713 4723 return;
4714 4724 }
4715 4725
4716 4726 if (rp->r_unldvp != NULL) {
4717 4727 mutex_exit(&rp->r_statev4_lock);
4718 4728 mutex_exit(&rp->r_statelock);
4719 4729 mutex_exit(&rp->r_os_lock);
4720 4730 nfs4_async_inactive(vp, cr);
4721 4731 return;
4722 4732 }
4723 4733 mutex_exit(&rp->r_statev4_lock);
4724 4734 mutex_exit(&rp->r_statelock);
4725 4735 mutex_exit(&rp->r_os_lock);
4726 4736
4727 4737 rp4_addfree(rp, cr);
4728 4738 }
4729 4739
4730 4740 /*
4731 4741 * nfs4_inactive_otw - nfs4_inactive, plus over-the-wire calls to free up
4732 4742 * various bits of state. The caller must not refer to vp after this call.
4733 4743 */
4734 4744
4735 4745 void
4736 4746 nfs4_inactive_otw(vnode_t *vp, cred_t *cr)
4737 4747 {
4738 4748 rnode4_t *rp = VTOR4(vp);
4739 4749 nfs4_recov_state_t recov_state;
4740 4750 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
4741 4751 vnode_t *unldvp;
4742 4752 char *unlname;
4743 4753 cred_t *unlcred;
4744 4754 COMPOUND4args_clnt args;
4745 4755 COMPOUND4res_clnt res, *resp;
4746 4756 nfs_argop4 argop[2];
4747 4757 int doqueue;
4748 4758 #ifdef DEBUG
4749 4759 char *name;
4750 4760 #endif
4751 4761
4752 4762 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
4753 4763 ASSERT(!IS_SHADOW(vp, rp));
4754 4764
4755 4765 #ifdef DEBUG
4756 4766 name = fn_name(VTOSV(vp)->sv_name);
4757 4767 NFS4_DEBUG(nfs4_client_inactive_debug, (CE_NOTE, "nfs4_inactive_otw: "
4758 4768 "release vnode %s", name));
4759 4769 kmem_free(name, MAXNAMELEN);
4760 4770 #endif
4761 4771
4762 4772 if (vp->v_type == VREG) {
4763 4773 bool_t recov_failed = FALSE;
4764 4774
4765 4775 e.error = nfs4close_all(vp, cr);
4766 4776 if (e.error) {
4767 4777 /* Check to see if recovery failed */
4768 4778 mutex_enter(&(VTOMI4(vp)->mi_lock));
4769 4779 if (VTOMI4(vp)->mi_flags & MI4_RECOV_FAIL)
4770 4780 recov_failed = TRUE;
4771 4781 mutex_exit(&(VTOMI4(vp)->mi_lock));
4772 4782 if (!recov_failed) {
4773 4783 mutex_enter(&rp->r_statelock);
4774 4784 if (rp->r_flags & R4RECOVERR)
4775 4785 recov_failed = TRUE;
4776 4786 mutex_exit(&rp->r_statelock);
4777 4787 }
4778 4788 if (recov_failed) {
4779 4789 NFS4_DEBUG(nfs4_client_recov_debug,
4780 4790 (CE_NOTE, "nfs4_inactive_otw: "
4781 4791 "close failed (recovery failure)"));
4782 4792 }
4783 4793 }
4784 4794 }
4785 4795
4786 4796 redo:
4787 4797 if (rp->r_unldvp == NULL) {
4788 4798 rp4_addfree(rp, cr);
4789 4799 return;
4790 4800 }
4791 4801
4792 4802 /*
4793 4803 * Save the vnode pointer for the directory where the
4794 4804 * unlinked-open file got renamed, then set it to NULL
4795 4805 * to prevent another thread from getting here before
4796 4806 * we're done with the remove. While we have the
4797 4807 * statelock, make local copies of the pertinent rnode
4798 4808 * fields. If we weren't to do this in an atomic way, the
4799 4809 * the unl* fields could become inconsistent with respect
4800 4810 * to each other due to a race condition between this
4801 4811 * code and nfs_remove(). See bug report 1034328.
4802 4812 */
4803 4813 mutex_enter(&rp->r_statelock);
4804 4814 if (rp->r_unldvp == NULL) {
4805 4815 mutex_exit(&rp->r_statelock);
4806 4816 rp4_addfree(rp, cr);
4807 4817 return;
4808 4818 }
4809 4819
4810 4820 unldvp = rp->r_unldvp;
4811 4821 rp->r_unldvp = NULL;
4812 4822 unlname = rp->r_unlname;
4813 4823 rp->r_unlname = NULL;
4814 4824 unlcred = rp->r_unlcred;
4815 4825 rp->r_unlcred = NULL;
4816 4826 mutex_exit(&rp->r_statelock);
4817 4827
4818 4828 /*
4819 4829 * If there are any dirty pages left, then flush
4820 4830 * them. This is unfortunate because they just
4821 4831 * may get thrown away during the remove operation,
4822 4832 * but we have to do this for correctness.
4823 4833 */
4824 4834 if (nfs4_has_pages(vp) &&
4825 4835 ((rp->r_flags & R4DIRTY) || rp->r_count > 0)) {
4826 4836 ASSERT(vp->v_type != VCHR);
4827 4837 e.error = nfs4_putpage(vp, (u_offset_t)0, 0, 0, cr, NULL);
4828 4838 if (e.error) {
4829 4839 mutex_enter(&rp->r_statelock);
4830 4840 if (!rp->r_error)
4831 4841 rp->r_error = e.error;
4832 4842 mutex_exit(&rp->r_statelock);
4833 4843 }
4834 4844 }
4835 4845
4836 4846 recov_state.rs_flags = 0;
4837 4847 recov_state.rs_num_retry_despite_err = 0;
4838 4848 recov_retry_remove:
4839 4849 /*
4840 4850 * Do the remove operation on the renamed file
4841 4851 */
4842 4852 args.ctag = TAG_INACTIVE;
4843 4853
4844 4854 /*
4845 4855 * Remove ops: putfh dir; remove
4846 4856 */
4847 4857 args.array_len = 2;
4848 4858 args.array = argop;
4849 4859
4850 4860 e.error = nfs4_start_op(VTOMI4(unldvp), unldvp, NULL, &recov_state);
4851 4861 if (e.error) {
4852 4862 kmem_free(unlname, MAXNAMELEN);
4853 4863 crfree(unlcred);
4854 4864 VN_RELE(unldvp);
4855 4865 /*
4856 4866 * Try again; this time around r_unldvp will be NULL, so we'll
4857 4867 * just call rp4_addfree() and return.
4858 4868 */
4859 4869 goto redo;
4860 4870 }
4861 4871
4862 4872 /* putfh directory */
4863 4873 argop[0].argop = OP_CPUTFH;
4864 4874 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(unldvp)->r_fh;
4865 4875
4866 4876 /* remove */
4867 4877 argop[1].argop = OP_CREMOVE;
4868 4878 argop[1].nfs_argop4_u.opcremove.ctarget = unlname;
4869 4879
4870 4880 doqueue = 1;
4871 4881 resp = &res;
4872 4882
4873 4883 #if 0 /* notyet */
4874 4884 /*
4875 4885 * Can't do this yet. We may be being called from
4876 4886 * dnlc_purge_XXX while that routine is holding a
4877 4887 * mutex lock to the nc_rele list. The calls to
4878 4888 * nfs3_cache_wcc_data may result in calls to
4879 4889 * dnlc_purge_XXX. This will result in a deadlock.
4880 4890 */
4881 4891 rfs4call(VTOMI4(unldvp), &args, &res, unlcred, &doqueue, 0, &e);
4882 4892 if (e.error) {
4883 4893 PURGE_ATTRCACHE4(unldvp);
4884 4894 resp = NULL;
4885 4895 } else if (res.status) {
4886 4896 e.error = geterrno4(res.status);
4887 4897 PURGE_ATTRCACHE4(unldvp);
4888 4898 /*
4889 4899 * This code is inactive right now
4890 4900 * but if made active there should
4891 4901 * be a nfs4_end_op() call before
4892 4902 * nfs4_purge_stale_fh to avoid start_op()
4893 4903 * deadlock. See BugId: 4948726
4894 4904 */
4895 4905 nfs4_purge_stale_fh(error, unldvp, cr);
4896 4906 } else {
4897 4907 nfs_resop4 *resop;
4898 4908 REMOVE4res *rm_res;
4899 4909
4900 4910 resop = &res.array[1];
4901 4911 rm_res = &resop->nfs_resop4_u.opremove;
4902 4912 /*
4903 4913 * Update directory cache attribute,
4904 4914 * readdir and dnlc caches.
4905 4915 */
4906 4916 nfs4_update_dircaches(&rm_res->cinfo, unldvp, NULL, NULL, NULL);
4907 4917 }
4908 4918 #else
4909 4919 rfs4call(VTOMI4(unldvp), &args, &res, unlcred, &doqueue, 0, &e);
4910 4920
4911 4921 PURGE_ATTRCACHE4(unldvp);
4912 4922 #endif
4913 4923
4914 4924 if (nfs4_needs_recovery(&e, FALSE, unldvp->v_vfsp)) {
4915 4925 if (nfs4_start_recovery(&e, VTOMI4(unldvp), unldvp, NULL,
4916 4926 NULL, NULL, OP_REMOVE, NULL, NULL, NULL) == FALSE) {
4917 4927 if (!e.error)
4918 4928 (void) xdr_free(xdr_COMPOUND4res_clnt,
4919 4929 (caddr_t)&res);
4920 4930 nfs4_end_op(VTOMI4(unldvp), unldvp, NULL,
4921 4931 &recov_state, TRUE);
4922 4932 goto recov_retry_remove;
4923 4933 }
4924 4934 }
4925 4935 nfs4_end_op(VTOMI4(unldvp), unldvp, NULL, &recov_state, FALSE);
4926 4936
4927 4937 /*
4928 4938 * Release stuff held for the remove
4929 4939 */
4930 4940 VN_RELE(unldvp);
4931 4941 if (!e.error && resp)
4932 4942 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
4933 4943
4934 4944 kmem_free(unlname, MAXNAMELEN);
4935 4945 crfree(unlcred);
4936 4946 goto redo;
4937 4947 }
4938 4948
4939 4949 /*
4940 4950 * Remote file system operations having to do with directory manipulation.
4941 4951 */
4942 4952 /* ARGSUSED3 */
4943 4953 int
4944 4954 nfs4_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
4945 4955 int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
4946 4956 int *direntflags, pathname_t *realpnp)
4947 4957 {
4948 4958 int error;
4949 4959 vnode_t *vp, *avp = NULL;
4950 4960 rnode4_t *drp;
4951 4961
4952 4962 *vpp = NULL;
4953 4963 if (nfs_zone() != VTOMI4(dvp)->mi_zone)
4954 4964 return (EPERM);
4955 4965 /*
4956 4966 * if LOOKUP_XATTR, must replace dvp (object) with
4957 4967 * object's attrdir before continuing with lookup
4958 4968 */
4959 4969 if (flags & LOOKUP_XATTR) {
4960 4970 error = nfs4lookup_xattr(dvp, nm, &avp, flags, cr);
4961 4971 if (error)
4962 4972 return (error);
4963 4973
4964 4974 dvp = avp;
4965 4975
4966 4976 /*
4967 4977 * If lookup is for "", just return dvp now. The attrdir
4968 4978 * has already been activated (from nfs4lookup_xattr), and
4969 4979 * the caller will RELE the original dvp -- not
4970 4980 * the attrdir. So, set vpp and return.
4971 4981 * Currently, when the LOOKUP_XATTR flag is
4972 4982 * passed to VOP_LOOKUP, the name is always empty, and
4973 4983 * shortcircuiting here avoids 3 unneeded lock/unlock
4974 4984 * pairs.
4975 4985 *
4976 4986 * If a non-empty name was provided, then it is the
4977 4987 * attribute name, and it will be looked up below.
4978 4988 */
4979 4989 if (*nm == '\0') {
4980 4990 *vpp = dvp;
4981 4991 return (0);
4982 4992 }
4983 4993
4984 4994 /*
4985 4995 * The vfs layer never sends a name when asking for the
4986 4996 * attrdir, so we should never get here (unless of course
4987 4997 * name is passed at some time in future -- at which time
4988 4998 * we'll blow up here).
4989 4999 */
4990 5000 ASSERT(0);
4991 5001 }
4992 5002
4993 5003 drp = VTOR4(dvp);
4994 5004 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp)))
4995 5005 return (EINTR);
4996 5006
4997 5007 error = nfs4lookup(dvp, nm, vpp, cr, 0);
4998 5008 nfs_rw_exit(&drp->r_rwlock);
4999 5009
5000 5010 /*
5001 5011 * If vnode is a device, create special vnode.
5002 5012 */
5003 5013 if (!error && ISVDEV((*vpp)->v_type)) {
5004 5014 vp = *vpp;
5005 5015 *vpp = specvp(vp, vp->v_rdev, vp->v_type, cr);
5006 5016 VN_RELE(vp);
5007 5017 }
5008 5018
5009 5019 return (error);
5010 5020 }
5011 5021
5012 5022 /* ARGSUSED */
5013 5023 static int
5014 5024 nfs4lookup_xattr(vnode_t *dvp, char *nm, vnode_t **vpp, int flags, cred_t *cr)
5015 5025 {
5016 5026 int error;
5017 5027 rnode4_t *drp;
5018 5028 int cflag = ((flags & CREATE_XATTR_DIR) != 0);
5019 5029 mntinfo4_t *mi;
5020 5030
5021 5031 mi = VTOMI4(dvp);
5022 5032 if (!(mi->mi_vfsp->vfs_flag & VFS_XATTR) &&
5023 5033 !vfs_has_feature(mi->mi_vfsp, VFSFT_SYSATTR_VIEWS))
5024 5034 return (EINVAL);
5025 5035
5026 5036 drp = VTOR4(dvp);
5027 5037 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp)))
5028 5038 return (EINTR);
5029 5039
5030 5040 mutex_enter(&drp->r_statelock);
5031 5041 /*
5032 5042 * If the server doesn't support xattrs just return EINVAL
5033 5043 */
5034 5044 if (drp->r_xattr_dir == NFS4_XATTR_DIR_NOTSUPP) {
5035 5045 mutex_exit(&drp->r_statelock);
5036 5046 nfs_rw_exit(&drp->r_rwlock);
5037 5047 return (EINVAL);
5038 5048 }
5039 5049
5040 5050 /*
5041 5051 * If there is a cached xattr directory entry,
5042 5052 * use it as long as the attributes are valid. If the
5043 5053 * attributes are not valid, take the simple approach and
5044 5054 * free the cached value and re-fetch a new value.
5045 5055 *
5046 5056 * We don't negative entry cache for now, if we did we
5047 5057 * would need to check if the file has changed on every
5048 5058 * lookup. But xattrs don't exist very often and failing
5049 5059 * an openattr is not much more expensive than and NVERIFY or GETATTR
5050 5060 * so do an openattr over the wire for now.
5051 5061 */
5052 5062 if (drp->r_xattr_dir != NULL) {
5053 5063 if (ATTRCACHE4_VALID(dvp)) {
5054 5064 VN_HOLD(drp->r_xattr_dir);
5055 5065 *vpp = drp->r_xattr_dir;
5056 5066 mutex_exit(&drp->r_statelock);
5057 5067 nfs_rw_exit(&drp->r_rwlock);
5058 5068 return (0);
5059 5069 }
5060 5070 VN_RELE(drp->r_xattr_dir);
5061 5071 drp->r_xattr_dir = NULL;
5062 5072 }
5063 5073 mutex_exit(&drp->r_statelock);
5064 5074
5065 5075 error = nfs4openattr(dvp, vpp, cflag, cr);
5066 5076
5067 5077 nfs_rw_exit(&drp->r_rwlock);
5068 5078
5069 5079 return (error);
5070 5080 }
5071 5081
5072 5082 static int
5073 5083 nfs4lookup(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr, int skipdnlc)
5074 5084 {
5075 5085 int error;
5076 5086 rnode4_t *drp;
5077 5087
5078 5088 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
5079 5089
5080 5090 /*
5081 5091 * If lookup is for "", just return dvp. Don't need
5082 5092 * to send it over the wire, look it up in the dnlc,
5083 5093 * or perform any access checks.
5084 5094 */
5085 5095 if (*nm == '\0') {
5086 5096 VN_HOLD(dvp);
5087 5097 *vpp = dvp;
5088 5098 return (0);
5089 5099 }
5090 5100
5091 5101 /*
5092 5102 * Can't do lookups in non-directories.
5093 5103 */
5094 5104 if (dvp->v_type != VDIR)
5095 5105 return (ENOTDIR);
5096 5106
5097 5107 /*
5098 5108 * If lookup is for ".", just return dvp. Don't need
5099 5109 * to send it over the wire or look it up in the dnlc,
5100 5110 * just need to check access.
5101 5111 */
5102 5112 if (nm[0] == '.' && nm[1] == '\0') {
5103 5113 error = nfs4_access(dvp, VEXEC, 0, cr, NULL);
5104 5114 if (error)
5105 5115 return (error);
5106 5116 VN_HOLD(dvp);
5107 5117 *vpp = dvp;
5108 5118 return (0);
5109 5119 }
5110 5120
5111 5121 drp = VTOR4(dvp);
5112 5122 if (!(drp->r_flags & R4LOOKUP)) {
5113 5123 mutex_enter(&drp->r_statelock);
5114 5124 drp->r_flags |= R4LOOKUP;
5115 5125 mutex_exit(&drp->r_statelock);
5116 5126 }
5117 5127
5118 5128 *vpp = NULL;
5119 5129 /*
5120 5130 * Lookup this name in the DNLC. If there is no entry
5121 5131 * lookup over the wire.
5122 5132 */
5123 5133 if (!skipdnlc)
5124 5134 *vpp = dnlc_lookup(dvp, nm);
5125 5135 if (*vpp == NULL) {
5126 5136 /*
5127 5137 * We need to go over the wire to lookup the name.
5128 5138 */
5129 5139 return (nfs4lookupnew_otw(dvp, nm, vpp, cr));
5130 5140 }
5131 5141
5132 5142 /*
5133 5143 * We hit on the dnlc
5134 5144 */
5135 5145 if (*vpp != DNLC_NO_VNODE ||
5136 5146 (dvp->v_vfsp->vfs_flag & VFS_RDONLY)) {
5137 5147 /*
5138 5148 * But our attrs may not be valid.
5139 5149 */
5140 5150 if (ATTRCACHE4_VALID(dvp)) {
5141 5151 error = nfs4_waitfor_purge_complete(dvp);
5142 5152 if (error) {
5143 5153 VN_RELE(*vpp);
5144 5154 *vpp = NULL;
5145 5155 return (error);
5146 5156 }
5147 5157
5148 5158 /*
5149 5159 * If after the purge completes, check to make sure
5150 5160 * our attrs are still valid.
5151 5161 */
5152 5162 if (ATTRCACHE4_VALID(dvp)) {
5153 5163 /*
5154 5164 * If we waited for a purge we may have
5155 5165 * lost our vnode so look it up again.
5156 5166 */
5157 5167 VN_RELE(*vpp);
5158 5168 *vpp = dnlc_lookup(dvp, nm);
5159 5169 if (*vpp == NULL)
5160 5170 return (nfs4lookupnew_otw(dvp,
5161 5171 nm, vpp, cr));
5162 5172
5163 5173 /*
5164 5174 * The access cache should almost always hit
5165 5175 */
5166 5176 error = nfs4_access(dvp, VEXEC, 0, cr, NULL);
5167 5177
5168 5178 if (error) {
5169 5179 VN_RELE(*vpp);
5170 5180 *vpp = NULL;
5171 5181 return (error);
5172 5182 }
5173 5183 if (*vpp == DNLC_NO_VNODE) {
5174 5184 VN_RELE(*vpp);
5175 5185 *vpp = NULL;
5176 5186 return (ENOENT);
5177 5187 }
5178 5188 return (0);
5179 5189 }
5180 5190 }
5181 5191 }
5182 5192
5183 5193 ASSERT(*vpp != NULL);
5184 5194
5185 5195 /*
5186 5196 * We may have gotten here we have one of the following cases:
5187 5197 * 1) vpp != DNLC_NO_VNODE, our attrs have timed out so we
5188 5198 * need to validate them.
5189 5199 * 2) vpp == DNLC_NO_VNODE, a negative entry that we always
5190 5200 * must validate.
5191 5201 *
5192 5202 * Go to the server and check if the directory has changed, if
5193 5203 * it hasn't we are done and can use the dnlc entry.
5194 5204 */
5195 5205 return (nfs4lookupvalidate_otw(dvp, nm, vpp, cr));
5196 5206 }
5197 5207
5198 5208 /*
5199 5209 * Go to the server and check if the directory has changed, if
5200 5210 * it hasn't we are done and can use the dnlc entry. If it
5201 5211 * has changed we get a new copy of its attributes and check
5202 5212 * the access for VEXEC, then relookup the filename and
5203 5213 * get its filehandle and attributes.
5204 5214 *
5205 5215 * PUTFH dfh NVERIFY GETATTR ACCESS LOOKUP GETFH GETATTR
5206 5216 * if the NVERIFY failed we must
5207 5217 * purge the caches
5208 5218 * cache new attributes (will set r_time_attr_inval)
5209 5219 * cache new access
5210 5220 * recheck VEXEC access
5211 5221 * add name to dnlc, possibly negative
5212 5222 * if LOOKUP succeeded
5213 5223 * cache new attributes
5214 5224 * else
5215 5225 * set a new r_time_attr_inval for dvp
5216 5226 * check to make sure we have access
5217 5227 *
5218 5228 * The vpp returned is the vnode passed in if the directory is valid,
5219 5229 * a new vnode if successful lookup, or NULL on error.
5220 5230 */
5221 5231 static int
5222 5232 nfs4lookupvalidate_otw(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr)
5223 5233 {
5224 5234 COMPOUND4args_clnt args;
5225 5235 COMPOUND4res_clnt res;
5226 5236 fattr4 *ver_fattr;
5227 5237 fattr4_change dchange;
5228 5238 int32_t *ptr;
5229 5239 int argoplist_size = 7 * sizeof (nfs_argop4);
5230 5240 nfs_argop4 *argop;
5231 5241 int doqueue;
5232 5242 mntinfo4_t *mi;
5233 5243 nfs4_recov_state_t recov_state;
5234 5244 hrtime_t t;
5235 5245 int isdotdot;
5236 5246 vnode_t *nvp;
5237 5247 nfs_fh4 *fhp;
5238 5248 nfs4_sharedfh_t *sfhp;
5239 5249 nfs4_access_type_t cacc;
5240 5250 rnode4_t *nrp;
5241 5251 rnode4_t *drp = VTOR4(dvp);
5242 5252 nfs4_ga_res_t *garp = NULL;
5243 5253 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
5244 5254
5245 5255 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
5246 5256 ASSERT(nm != NULL);
5247 5257 ASSERT(nm[0] != '\0');
5248 5258 ASSERT(dvp->v_type == VDIR);
5249 5259 ASSERT(nm[0] != '.' || nm[1] != '\0');
5250 5260 ASSERT(*vpp != NULL);
5251 5261
5252 5262 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') {
5253 5263 isdotdot = 1;
5254 5264 args.ctag = TAG_LOOKUP_VPARENT;
5255 5265 } else {
5256 5266 /*
5257 5267 * If dvp were a stub, it should have triggered and caused
5258 5268 * a mount for us to get this far.
5259 5269 */
5260 5270 ASSERT(!RP_ISSTUB(VTOR4(dvp)));
5261 5271
5262 5272 isdotdot = 0;
5263 5273 args.ctag = TAG_LOOKUP_VALID;
5264 5274 }
5265 5275
5266 5276 mi = VTOMI4(dvp);
5267 5277 recov_state.rs_flags = 0;
5268 5278 recov_state.rs_num_retry_despite_err = 0;
5269 5279
5270 5280 nvp = NULL;
5271 5281
5272 5282 /* Save the original mount point security information */
5273 5283 (void) save_mnt_secinfo(mi->mi_curr_serv);
5274 5284
5275 5285 recov_retry:
5276 5286 e.error = nfs4_start_fop(mi, dvp, NULL, OH_LOOKUP,
5277 5287 &recov_state, NULL);
5278 5288 if (e.error) {
5279 5289 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5280 5290 VN_RELE(*vpp);
5281 5291 *vpp = NULL;
5282 5292 return (e.error);
5283 5293 }
5284 5294
5285 5295 argop = kmem_alloc(argoplist_size, KM_SLEEP);
5286 5296
5287 5297 /* PUTFH dfh NVERIFY GETATTR ACCESS LOOKUP GETFH GETATTR */
5288 5298 args.array_len = 7;
5289 5299 args.array = argop;
5290 5300
5291 5301 /* 0. putfh file */
5292 5302 argop[0].argop = OP_CPUTFH;
5293 5303 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(dvp)->r_fh;
5294 5304
5295 5305 /* 1. nverify the change info */
5296 5306 argop[1].argop = OP_NVERIFY;
5297 5307 ver_fattr = &argop[1].nfs_argop4_u.opnverify.obj_attributes;
5298 5308 ver_fattr->attrmask = FATTR4_CHANGE_MASK;
5299 5309 ver_fattr->attrlist4 = (char *)&dchange;
5300 5310 ptr = (int32_t *)&dchange;
5301 5311 IXDR_PUT_HYPER(ptr, VTOR4(dvp)->r_change);
5302 5312 ver_fattr->attrlist4_len = sizeof (fattr4_change);
5303 5313
5304 5314 /* 2. getattr directory */
5305 5315 argop[2].argop = OP_GETATTR;
5306 5316 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
5307 5317 argop[2].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
5308 5318
5309 5319 /* 3. access directory */
5310 5320 argop[3].argop = OP_ACCESS;
5311 5321 argop[3].nfs_argop4_u.opaccess.access = ACCESS4_READ | ACCESS4_DELETE |
5312 5322 ACCESS4_MODIFY | ACCESS4_EXTEND | ACCESS4_LOOKUP;
5313 5323
5314 5324 /* 4. lookup name */
5315 5325 if (isdotdot) {
5316 5326 argop[4].argop = OP_LOOKUPP;
5317 5327 } else {
5318 5328 argop[4].argop = OP_CLOOKUP;
5319 5329 argop[4].nfs_argop4_u.opclookup.cname = nm;
5320 5330 }
5321 5331
5322 5332 /* 5. resulting file handle */
5323 5333 argop[5].argop = OP_GETFH;
5324 5334
5325 5335 /* 6. resulting file attributes */
5326 5336 argop[6].argop = OP_GETATTR;
5327 5337 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
5328 5338 argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
5329 5339
5330 5340 doqueue = 1;
5331 5341 t = gethrtime();
5332 5342
5333 5343 rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e);
5334 5344
5335 5345 if (!isdotdot && res.status == NFS4ERR_MOVED) {
5336 5346 e.error = nfs4_setup_referral(dvp, nm, vpp, cr);
5337 5347 if (e.error != 0 && *vpp != NULL)
5338 5348 VN_RELE(*vpp);
5339 5349 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5340 5350 &recov_state, FALSE);
5341 5351 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5342 5352 kmem_free(argop, argoplist_size);
5343 5353 return (e.error);
5344 5354 }
5345 5355
5346 5356 if (nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp)) {
5347 5357 /*
5348 5358 * For WRONGSEC of a non-dotdot case, send secinfo directly
5349 5359 * from this thread, do not go thru the recovery thread since
5350 5360 * we need the nm information.
5351 5361 *
5352 5362 * Not doing dotdot case because there is no specification
5353 5363 * for (PUTFH, SECINFO "..") yet.
5354 5364 */
5355 5365 if (!isdotdot && res.status == NFS4ERR_WRONGSEC) {
5356 5366 if ((e.error = nfs4_secinfo_vnode_otw(dvp, nm, cr)))
5357 5367 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5358 5368 &recov_state, FALSE);
5359 5369 else
5360 5370 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5361 5371 &recov_state, TRUE);
5362 5372 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5363 5373 kmem_free(argop, argoplist_size);
5364 5374 if (!e.error)
5365 5375 goto recov_retry;
5366 5376 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5367 5377 VN_RELE(*vpp);
5368 5378 *vpp = NULL;
5369 5379 return (e.error);
5370 5380 }
5371 5381
5372 5382 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL,
5373 5383 OP_LOOKUP, NULL, NULL, NULL) == FALSE) {
5374 5384 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5375 5385 &recov_state, TRUE);
5376 5386
5377 5387 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5378 5388 kmem_free(argop, argoplist_size);
5379 5389 goto recov_retry;
5380 5390 }
5381 5391 }
5382 5392
5383 5393 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, &recov_state, FALSE);
5384 5394
5385 5395 if (e.error || res.array_len == 0) {
5386 5396 /*
5387 5397 * If e.error isn't set, then reply has no ops (or we couldn't
5388 5398 * be here). The only legal way to reply without an op array
5389 5399 * is via NFS4ERR_MINOR_VERS_MISMATCH. An ops array should
5390 5400 * be in the reply for all other status values.
5391 5401 *
5392 5402 * For valid replies without an ops array, return ENOTSUP
5393 5403 * (geterrno4 xlation of VERS_MISMATCH). For illegal replies,
5394 5404 * return EIO -- don't trust status.
5395 5405 */
5396 5406 if (e.error == 0)
5397 5407 e.error = (res.status == NFS4ERR_MINOR_VERS_MISMATCH) ?
5398 5408 ENOTSUP : EIO;
5399 5409 VN_RELE(*vpp);
5400 5410 *vpp = NULL;
5401 5411 kmem_free(argop, argoplist_size);
5402 5412 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5403 5413 return (e.error);
5404 5414 }
5405 5415
5406 5416 if (res.status != NFS4ERR_SAME) {
5407 5417 e.error = geterrno4(res.status);
5408 5418
5409 5419 /*
5410 5420 * The NVERIFY "failed" so the directory has changed
5411 5421 * First make sure PUTFH succeeded and NVERIFY "failed"
5412 5422 * cleanly.
5413 5423 */
5414 5424 if ((res.array[0].nfs_resop4_u.opputfh.status != NFS4_OK) ||
5415 5425 (res.array[1].nfs_resop4_u.opnverify.status != NFS4_OK)) {
5416 5426 nfs4_purge_stale_fh(e.error, dvp, cr);
5417 5427 VN_RELE(*vpp);
5418 5428 *vpp = NULL;
5419 5429 goto exit;
5420 5430 }
5421 5431
5422 5432 /*
5423 5433 * We know the NVERIFY "failed" so we must:
5424 5434 * purge the caches (access and indirectly dnlc if needed)
5425 5435 */
5426 5436 nfs4_purge_caches(dvp, NFS4_NOPURGE_DNLC, cr, TRUE);
5427 5437
5428 5438 if (res.array[2].nfs_resop4_u.opgetattr.status != NFS4_OK) {
5429 5439 nfs4_purge_stale_fh(e.error, dvp, cr);
5430 5440 VN_RELE(*vpp);
5431 5441 *vpp = NULL;
5432 5442 goto exit;
5433 5443 }
5434 5444
5435 5445 /*
5436 5446 * Install new cached attributes for the directory
5437 5447 */
5438 5448 nfs4_attr_cache(dvp,
5439 5449 &res.array[2].nfs_resop4_u.opgetattr.ga_res,
5440 5450 t, cr, FALSE, NULL);
5441 5451
5442 5452 if (res.array[3].nfs_resop4_u.opaccess.status != NFS4_OK) {
5443 5453 nfs4_purge_stale_fh(e.error, dvp, cr);
5444 5454 VN_RELE(*vpp);
5445 5455 *vpp = NULL;
5446 5456 e.error = geterrno4(res.status);
5447 5457 goto exit;
5448 5458 }
5449 5459
5450 5460 /*
5451 5461 * Now we know the directory is valid,
5452 5462 * cache new directory access
5453 5463 */
5454 5464 nfs4_access_cache(drp,
5455 5465 args.array[3].nfs_argop4_u.opaccess.access,
5456 5466 res.array[3].nfs_resop4_u.opaccess.access, cr);
5457 5467
5458 5468 /*
5459 5469 * recheck VEXEC access
5460 5470 */
5461 5471 cacc = nfs4_access_check(drp, ACCESS4_LOOKUP, cr);
5462 5472 if (cacc != NFS4_ACCESS_ALLOWED) {
5463 5473 /*
5464 5474 * Directory permissions might have been revoked
5465 5475 */
5466 5476 if (cacc == NFS4_ACCESS_DENIED) {
5467 5477 e.error = EACCES;
5468 5478 VN_RELE(*vpp);
5469 5479 *vpp = NULL;
5470 5480 goto exit;
5471 5481 }
5472 5482
5473 5483 /*
5474 5484 * Somehow we must not have asked for enough
5475 5485 * so try a singleton ACCESS, should never happen.
5476 5486 */
5477 5487 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL);
5478 5488 if (e.error) {
5479 5489 VN_RELE(*vpp);
5480 5490 *vpp = NULL;
5481 5491 goto exit;
5482 5492 }
5483 5493 }
5484 5494
5485 5495 e.error = geterrno4(res.status);
5486 5496 if (res.array[4].nfs_resop4_u.oplookup.status != NFS4_OK) {
5487 5497 /*
5488 5498 * The lookup failed, probably no entry
5489 5499 */
5490 5500 if (e.error == ENOENT && nfs4_lookup_neg_cache) {
5491 5501 dnlc_update(dvp, nm, DNLC_NO_VNODE);
5492 5502 } else {
5493 5503 /*
5494 5504 * Might be some other error, so remove
5495 5505 * the dnlc entry to make sure we start all
5496 5506 * over again, next time.
5497 5507 */
5498 5508 dnlc_remove(dvp, nm);
5499 5509 }
5500 5510 VN_RELE(*vpp);
5501 5511 *vpp = NULL;
5502 5512 goto exit;
5503 5513 }
5504 5514
5505 5515 if (res.array[5].nfs_resop4_u.opgetfh.status != NFS4_OK) {
5506 5516 /*
5507 5517 * The file exists but we can't get its fh for
5508 5518 * some unknown reason. Remove it from the dnlc
5509 5519 * and error out to be safe.
5510 5520 */
5511 5521 dnlc_remove(dvp, nm);
5512 5522 VN_RELE(*vpp);
5513 5523 *vpp = NULL;
5514 5524 goto exit;
5515 5525 }
5516 5526 fhp = &res.array[5].nfs_resop4_u.opgetfh.object;
5517 5527 if (fhp->nfs_fh4_len == 0) {
5518 5528 /*
5519 5529 * The file exists but a bogus fh
5520 5530 * some unknown reason. Remove it from the dnlc
5521 5531 * and error out to be safe.
5522 5532 */
5523 5533 e.error = ENOENT;
5524 5534 dnlc_remove(dvp, nm);
5525 5535 VN_RELE(*vpp);
5526 5536 *vpp = NULL;
5527 5537 goto exit;
5528 5538 }
5529 5539 sfhp = sfh4_get(fhp, mi);
5530 5540
5531 5541 if (res.array[6].nfs_resop4_u.opgetattr.status == NFS4_OK)
5532 5542 garp = &res.array[6].nfs_resop4_u.opgetattr.ga_res;
5533 5543
5534 5544 /*
5535 5545 * Make the new rnode
5536 5546 */
5537 5547 if (isdotdot) {
5538 5548 e.error = nfs4_make_dotdot(sfhp, t, dvp, cr, &nvp, 1);
5539 5549 if (e.error) {
5540 5550 sfh4_rele(&sfhp);
5541 5551 VN_RELE(*vpp);
5542 5552 *vpp = NULL;
5543 5553 goto exit;
5544 5554 }
5545 5555 /*
5546 5556 * XXX if nfs4_make_dotdot uses an existing rnode
5547 5557 * XXX it doesn't update the attributes.
5548 5558 * XXX for now just save them again to save an OTW
5549 5559 */
5550 5560 nfs4_attr_cache(nvp, garp, t, cr, FALSE, NULL);
5551 5561 } else {
5552 5562 nvp = makenfs4node(sfhp, garp, dvp->v_vfsp, t, cr,
5553 5563 dvp, fn_get(VTOSV(dvp)->sv_name, nm, sfhp));
5554 5564 /*
5555 5565 * If v_type == VNON, then garp was NULL because
5556 5566 * the last op in the compound failed and makenfs4node
5557 5567 * could not find the vnode for sfhp. It created
5558 5568 * a new vnode, so we have nothing to purge here.
5559 5569 */
5560 5570 if (nvp->v_type == VNON) {
5561 5571 vattr_t vattr;
5562 5572
5563 5573 vattr.va_mask = AT_TYPE;
5564 5574 /*
5565 5575 * N.B. We've already called nfs4_end_fop above.
5566 5576 */
5567 5577 e.error = nfs4getattr(nvp, &vattr, cr);
5568 5578 if (e.error) {
5569 5579 sfh4_rele(&sfhp);
5570 5580 VN_RELE(*vpp);
5571 5581 *vpp = NULL;
5572 5582 VN_RELE(nvp);
5573 5583 goto exit;
5574 5584 }
5575 5585 nvp->v_type = vattr.va_type;
5576 5586 }
5577 5587 }
5578 5588 sfh4_rele(&sfhp);
5579 5589
5580 5590 nrp = VTOR4(nvp);
5581 5591 mutex_enter(&nrp->r_statev4_lock);
5582 5592 if (!nrp->created_v4) {
5583 5593 mutex_exit(&nrp->r_statev4_lock);
5584 5594 dnlc_update(dvp, nm, nvp);
5585 5595 } else
5586 5596 mutex_exit(&nrp->r_statev4_lock);
5587 5597
5588 5598 VN_RELE(*vpp);
5589 5599 *vpp = nvp;
5590 5600 } else {
5591 5601 hrtime_t now;
5592 5602 hrtime_t delta = 0;
5593 5603
5594 5604 e.error = 0;
5595 5605
5596 5606 /*
5597 5607 * Because the NVERIFY "succeeded" we know that the
5598 5608 * directory attributes are still valid
5599 5609 * so update r_time_attr_inval
5600 5610 */
5601 5611 now = gethrtime();
5602 5612 mutex_enter(&drp->r_statelock);
5603 5613 if (!(mi->mi_flags & MI4_NOAC) && !(dvp->v_flag & VNOCACHE)) {
5604 5614 delta = now - drp->r_time_attr_saved;
5605 5615 if (delta < mi->mi_acdirmin)
5606 5616 delta = mi->mi_acdirmin;
5607 5617 else if (delta > mi->mi_acdirmax)
5608 5618 delta = mi->mi_acdirmax;
5609 5619 }
5610 5620 drp->r_time_attr_inval = now + delta;
5611 5621 mutex_exit(&drp->r_statelock);
5612 5622 dnlc_update(dvp, nm, *vpp);
5613 5623
5614 5624 /*
5615 5625 * Even though we have a valid directory attr cache
5616 5626 * and dnlc entry, we may not have access.
5617 5627 * This should almost always hit the cache.
5618 5628 */
5619 5629 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL);
5620 5630 if (e.error) {
5621 5631 VN_RELE(*vpp);
5622 5632 *vpp = NULL;
5623 5633 }
5624 5634
5625 5635 if (*vpp == DNLC_NO_VNODE) {
5626 5636 VN_RELE(*vpp);
5627 5637 *vpp = NULL;
5628 5638 e.error = ENOENT;
5629 5639 }
5630 5640 }
5631 5641
5632 5642 exit:
5633 5643 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5634 5644 kmem_free(argop, argoplist_size);
5635 5645 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5636 5646 return (e.error);
5637 5647 }
5638 5648
5639 5649 /*
5640 5650 * We need to go over the wire to lookup the name, but
5641 5651 * while we are there verify the directory has not
5642 5652 * changed but if it has, get new attributes and check access
5643 5653 *
5644 5654 * PUTFH dfh SAVEFH LOOKUP nm GETFH GETATTR RESTOREFH
5645 5655 * NVERIFY GETATTR ACCESS
5646 5656 *
5647 5657 * With the results:
5648 5658 * if the NVERIFY failed we must purge the caches, add new attributes,
5649 5659 * and cache new access.
5650 5660 * set a new r_time_attr_inval
5651 5661 * add name to dnlc, possibly negative
5652 5662 * if LOOKUP succeeded
5653 5663 * cache new attributes
5654 5664 */
5655 5665 static int
5656 5666 nfs4lookupnew_otw(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr)
5657 5667 {
5658 5668 COMPOUND4args_clnt args;
5659 5669 COMPOUND4res_clnt res;
5660 5670 fattr4 *ver_fattr;
5661 5671 fattr4_change dchange;
5662 5672 int32_t *ptr;
5663 5673 nfs4_ga_res_t *garp = NULL;
5664 5674 int argoplist_size = 9 * sizeof (nfs_argop4);
5665 5675 nfs_argop4 *argop;
5666 5676 int doqueue;
5667 5677 mntinfo4_t *mi;
5668 5678 nfs4_recov_state_t recov_state;
5669 5679 hrtime_t t;
5670 5680 int isdotdot;
5671 5681 vnode_t *nvp;
5672 5682 nfs_fh4 *fhp;
5673 5683 nfs4_sharedfh_t *sfhp;
5674 5684 nfs4_access_type_t cacc;
5675 5685 rnode4_t *nrp;
5676 5686 rnode4_t *drp = VTOR4(dvp);
5677 5687 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
5678 5688
5679 5689 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
5680 5690 ASSERT(nm != NULL);
5681 5691 ASSERT(nm[0] != '\0');
5682 5692 ASSERT(dvp->v_type == VDIR);
5683 5693 ASSERT(nm[0] != '.' || nm[1] != '\0');
5684 5694 ASSERT(*vpp == NULL);
5685 5695
5686 5696 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') {
5687 5697 isdotdot = 1;
5688 5698 args.ctag = TAG_LOOKUP_PARENT;
5689 5699 } else {
5690 5700 /*
5691 5701 * If dvp were a stub, it should have triggered and caused
5692 5702 * a mount for us to get this far.
5693 5703 */
5694 5704 ASSERT(!RP_ISSTUB(VTOR4(dvp)));
5695 5705
5696 5706 isdotdot = 0;
5697 5707 args.ctag = TAG_LOOKUP;
5698 5708 }
5699 5709
5700 5710 mi = VTOMI4(dvp);
5701 5711 recov_state.rs_flags = 0;
5702 5712 recov_state.rs_num_retry_despite_err = 0;
5703 5713
5704 5714 nvp = NULL;
5705 5715
5706 5716 /* Save the original mount point security information */
5707 5717 (void) save_mnt_secinfo(mi->mi_curr_serv);
5708 5718
5709 5719 recov_retry:
5710 5720 e.error = nfs4_start_fop(mi, dvp, NULL, OH_LOOKUP,
5711 5721 &recov_state, NULL);
5712 5722 if (e.error) {
5713 5723 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5714 5724 return (e.error);
5715 5725 }
5716 5726
5717 5727 argop = kmem_alloc(argoplist_size, KM_SLEEP);
5718 5728
5719 5729 /* PUTFH SAVEFH LOOKUP GETFH GETATTR RESTOREFH NVERIFY GETATTR ACCESS */
5720 5730 args.array_len = 9;
5721 5731 args.array = argop;
5722 5732
5723 5733 /* 0. putfh file */
5724 5734 argop[0].argop = OP_CPUTFH;
5725 5735 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(dvp)->r_fh;
5726 5736
5727 5737 /* 1. savefh for the nverify */
5728 5738 argop[1].argop = OP_SAVEFH;
5729 5739
5730 5740 /* 2. lookup name */
5731 5741 if (isdotdot) {
5732 5742 argop[2].argop = OP_LOOKUPP;
5733 5743 } else {
5734 5744 argop[2].argop = OP_CLOOKUP;
5735 5745 argop[2].nfs_argop4_u.opclookup.cname = nm;
5736 5746 }
5737 5747
5738 5748 /* 3. resulting file handle */
5739 5749 argop[3].argop = OP_GETFH;
5740 5750
5741 5751 /* 4. resulting file attributes */
5742 5752 argop[4].argop = OP_GETATTR;
5743 5753 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
5744 5754 argop[4].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
5745 5755
5746 5756 /* 5. restorefh back the directory for the nverify */
5747 5757 argop[5].argop = OP_RESTOREFH;
5748 5758
5749 5759 /* 6. nverify the change info */
5750 5760 argop[6].argop = OP_NVERIFY;
5751 5761 ver_fattr = &argop[6].nfs_argop4_u.opnverify.obj_attributes;
5752 5762 ver_fattr->attrmask = FATTR4_CHANGE_MASK;
5753 5763 ver_fattr->attrlist4 = (char *)&dchange;
5754 5764 ptr = (int32_t *)&dchange;
5755 5765 IXDR_PUT_HYPER(ptr, VTOR4(dvp)->r_change);
5756 5766 ver_fattr->attrlist4_len = sizeof (fattr4_change);
5757 5767
5758 5768 /* 7. getattr directory */
5759 5769 argop[7].argop = OP_GETATTR;
5760 5770 argop[7].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
5761 5771 argop[7].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
5762 5772
5763 5773 /* 8. access directory */
5764 5774 argop[8].argop = OP_ACCESS;
5765 5775 argop[8].nfs_argop4_u.opaccess.access = ACCESS4_READ | ACCESS4_DELETE |
5766 5776 ACCESS4_MODIFY | ACCESS4_EXTEND | ACCESS4_LOOKUP;
5767 5777
5768 5778 doqueue = 1;
5769 5779 t = gethrtime();
5770 5780
5771 5781 rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e);
5772 5782
5773 5783 if (!isdotdot && res.status == NFS4ERR_MOVED) {
5774 5784 e.error = nfs4_setup_referral(dvp, nm, vpp, cr);
5775 5785 if (e.error != 0 && *vpp != NULL)
5776 5786 VN_RELE(*vpp);
5777 5787 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5778 5788 &recov_state, FALSE);
5779 5789 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5780 5790 kmem_free(argop, argoplist_size);
5781 5791 return (e.error);
5782 5792 }
5783 5793
5784 5794 if (nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp)) {
5785 5795 /*
5786 5796 * For WRONGSEC of a non-dotdot case, send secinfo directly
5787 5797 * from this thread, do not go thru the recovery thread since
5788 5798 * we need the nm information.
5789 5799 *
5790 5800 * Not doing dotdot case because there is no specification
5791 5801 * for (PUTFH, SECINFO "..") yet.
5792 5802 */
5793 5803 if (!isdotdot && res.status == NFS4ERR_WRONGSEC) {
5794 5804 if ((e.error = nfs4_secinfo_vnode_otw(dvp, nm, cr)))
5795 5805 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5796 5806 &recov_state, FALSE);
5797 5807 else
5798 5808 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5799 5809 &recov_state, TRUE);
5800 5810 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5801 5811 kmem_free(argop, argoplist_size);
5802 5812 if (!e.error)
5803 5813 goto recov_retry;
5804 5814 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5805 5815 return (e.error);
5806 5816 }
5807 5817
5808 5818 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL,
5809 5819 OP_LOOKUP, NULL, NULL, NULL) == FALSE) {
5810 5820 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5811 5821 &recov_state, TRUE);
5812 5822
5813 5823 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5814 5824 kmem_free(argop, argoplist_size);
5815 5825 goto recov_retry;
5816 5826 }
5817 5827 }
5818 5828
5819 5829 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, &recov_state, FALSE);
5820 5830
5821 5831 if (e.error || res.array_len == 0) {
5822 5832 /*
5823 5833 * If e.error isn't set, then reply has no ops (or we couldn't
5824 5834 * be here). The only legal way to reply without an op array
5825 5835 * is via NFS4ERR_MINOR_VERS_MISMATCH. An ops array should
5826 5836 * be in the reply for all other status values.
5827 5837 *
5828 5838 * For valid replies without an ops array, return ENOTSUP
5829 5839 * (geterrno4 xlation of VERS_MISMATCH). For illegal replies,
5830 5840 * return EIO -- don't trust status.
5831 5841 */
5832 5842 if (e.error == 0)
5833 5843 e.error = (res.status == NFS4ERR_MINOR_VERS_MISMATCH) ?
5834 5844 ENOTSUP : EIO;
5835 5845
5836 5846 kmem_free(argop, argoplist_size);
5837 5847 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5838 5848 return (e.error);
5839 5849 }
5840 5850
5841 5851 e.error = geterrno4(res.status);
5842 5852
5843 5853 /*
5844 5854 * The PUTFH and SAVEFH may have failed.
5845 5855 */
5846 5856 if ((res.array[0].nfs_resop4_u.opputfh.status != NFS4_OK) ||
5847 5857 (res.array[1].nfs_resop4_u.opsavefh.status != NFS4_OK)) {
5848 5858 nfs4_purge_stale_fh(e.error, dvp, cr);
5849 5859 goto exit;
5850 5860 }
5851 5861
5852 5862 /*
5853 5863 * Check if the file exists, if it does delay entering
5854 5864 * into the dnlc until after we update the directory
5855 5865 * attributes so we don't cause it to get purged immediately.
5856 5866 */
5857 5867 if (res.array[2].nfs_resop4_u.oplookup.status != NFS4_OK) {
5858 5868 /*
5859 5869 * The lookup failed, probably no entry
5860 5870 */
5861 5871 if (e.error == ENOENT && nfs4_lookup_neg_cache)
5862 5872 dnlc_update(dvp, nm, DNLC_NO_VNODE);
5863 5873 goto exit;
5864 5874 }
5865 5875
5866 5876 if (res.array[3].nfs_resop4_u.opgetfh.status != NFS4_OK) {
5867 5877 /*
5868 5878 * The file exists but we can't get its fh for
5869 5879 * some unknown reason. Error out to be safe.
5870 5880 */
5871 5881 goto exit;
5872 5882 }
5873 5883
5874 5884 fhp = &res.array[3].nfs_resop4_u.opgetfh.object;
5875 5885 if (fhp->nfs_fh4_len == 0) {
5876 5886 /*
5877 5887 * The file exists but a bogus fh
5878 5888 * some unknown reason. Error out to be safe.
5879 5889 */
5880 5890 e.error = EIO;
5881 5891 goto exit;
5882 5892 }
5883 5893 sfhp = sfh4_get(fhp, mi);
5884 5894
5885 5895 if (res.array[4].nfs_resop4_u.opgetattr.status != NFS4_OK) {
5886 5896 sfh4_rele(&sfhp);
5887 5897 goto exit;
5888 5898 }
5889 5899 garp = &res.array[4].nfs_resop4_u.opgetattr.ga_res;
5890 5900
5891 5901 /*
5892 5902 * The RESTOREFH may have failed
5893 5903 */
5894 5904 if (res.array[5].nfs_resop4_u.oprestorefh.status != NFS4_OK) {
5895 5905 sfh4_rele(&sfhp);
5896 5906 e.error = EIO;
5897 5907 goto exit;
5898 5908 }
5899 5909
5900 5910 if (res.array[6].nfs_resop4_u.opnverify.status != NFS4ERR_SAME) {
5901 5911 /*
5902 5912 * First make sure the NVERIFY failed as we expected,
5903 5913 * if it didn't then be conservative and error out
5904 5914 * as we can't trust the directory.
5905 5915 */
5906 5916 if (res.array[6].nfs_resop4_u.opnverify.status != NFS4_OK) {
5907 5917 sfh4_rele(&sfhp);
5908 5918 e.error = EIO;
5909 5919 goto exit;
5910 5920 }
5911 5921
5912 5922 /*
5913 5923 * We know the NVERIFY "failed" so the directory has changed,
5914 5924 * so we must:
5915 5925 * purge the caches (access and indirectly dnlc if needed)
5916 5926 */
5917 5927 nfs4_purge_caches(dvp, NFS4_NOPURGE_DNLC, cr, TRUE);
5918 5928
5919 5929 if (res.array[7].nfs_resop4_u.opgetattr.status != NFS4_OK) {
5920 5930 sfh4_rele(&sfhp);
5921 5931 goto exit;
5922 5932 }
5923 5933 nfs4_attr_cache(dvp,
5924 5934 &res.array[7].nfs_resop4_u.opgetattr.ga_res,
5925 5935 t, cr, FALSE, NULL);
5926 5936
5927 5937 if (res.array[8].nfs_resop4_u.opaccess.status != NFS4_OK) {
5928 5938 nfs4_purge_stale_fh(e.error, dvp, cr);
5929 5939 sfh4_rele(&sfhp);
5930 5940 e.error = geterrno4(res.status);
5931 5941 goto exit;
5932 5942 }
5933 5943
5934 5944 /*
5935 5945 * Now we know the directory is valid,
5936 5946 * cache new directory access
5937 5947 */
5938 5948 nfs4_access_cache(drp,
5939 5949 args.array[8].nfs_argop4_u.opaccess.access,
5940 5950 res.array[8].nfs_resop4_u.opaccess.access, cr);
5941 5951
5942 5952 /*
5943 5953 * recheck VEXEC access
5944 5954 */
5945 5955 cacc = nfs4_access_check(drp, ACCESS4_LOOKUP, cr);
5946 5956 if (cacc != NFS4_ACCESS_ALLOWED) {
5947 5957 /*
5948 5958 * Directory permissions might have been revoked
5949 5959 */
5950 5960 if (cacc == NFS4_ACCESS_DENIED) {
5951 5961 sfh4_rele(&sfhp);
5952 5962 e.error = EACCES;
5953 5963 goto exit;
5954 5964 }
5955 5965
5956 5966 /*
5957 5967 * Somehow we must not have asked for enough
5958 5968 * so try a singleton ACCESS should never happen
5959 5969 */
5960 5970 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL);
5961 5971 if (e.error) {
5962 5972 sfh4_rele(&sfhp);
5963 5973 goto exit;
5964 5974 }
5965 5975 }
5966 5976
5967 5977 e.error = geterrno4(res.status);
5968 5978 } else {
5969 5979 hrtime_t now;
5970 5980 hrtime_t delta = 0;
5971 5981
5972 5982 e.error = 0;
5973 5983
5974 5984 /*
5975 5985 * Because the NVERIFY "succeeded" we know that the
5976 5986 * directory attributes are still valid
5977 5987 * so update r_time_attr_inval
5978 5988 */
5979 5989 now = gethrtime();
5980 5990 mutex_enter(&drp->r_statelock);
5981 5991 if (!(mi->mi_flags & MI4_NOAC) && !(dvp->v_flag & VNOCACHE)) {
5982 5992 delta = now - drp->r_time_attr_saved;
5983 5993 if (delta < mi->mi_acdirmin)
5984 5994 delta = mi->mi_acdirmin;
5985 5995 else if (delta > mi->mi_acdirmax)
5986 5996 delta = mi->mi_acdirmax;
5987 5997 }
5988 5998 drp->r_time_attr_inval = now + delta;
5989 5999 mutex_exit(&drp->r_statelock);
5990 6000
5991 6001 /*
5992 6002 * Even though we have a valid directory attr cache,
5993 6003 * we may not have access.
5994 6004 * This should almost always hit the cache.
5995 6005 */
5996 6006 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL);
5997 6007 if (e.error) {
5998 6008 sfh4_rele(&sfhp);
5999 6009 goto exit;
6000 6010 }
6001 6011 }
6002 6012
6003 6013 /*
6004 6014 * Now we have successfully completed the lookup, if the
6005 6015 * directory has changed we now have the valid attributes.
6006 6016 * We also know we have directory access.
6007 6017 * Create the new rnode and insert it in the dnlc.
6008 6018 */
6009 6019 if (isdotdot) {
6010 6020 e.error = nfs4_make_dotdot(sfhp, t, dvp, cr, &nvp, 1);
6011 6021 if (e.error) {
6012 6022 sfh4_rele(&sfhp);
6013 6023 goto exit;
6014 6024 }
6015 6025 /*
6016 6026 * XXX if nfs4_make_dotdot uses an existing rnode
6017 6027 * XXX it doesn't update the attributes.
6018 6028 * XXX for now just save them again to save an OTW
6019 6029 */
6020 6030 nfs4_attr_cache(nvp, garp, t, cr, FALSE, NULL);
6021 6031 } else {
6022 6032 nvp = makenfs4node(sfhp, garp, dvp->v_vfsp, t, cr,
6023 6033 dvp, fn_get(VTOSV(dvp)->sv_name, nm, sfhp));
6024 6034 }
6025 6035 sfh4_rele(&sfhp);
6026 6036
6027 6037 nrp = VTOR4(nvp);
6028 6038 mutex_enter(&nrp->r_statev4_lock);
6029 6039 if (!nrp->created_v4) {
6030 6040 mutex_exit(&nrp->r_statev4_lock);
6031 6041 dnlc_update(dvp, nm, nvp);
6032 6042 } else
6033 6043 mutex_exit(&nrp->r_statev4_lock);
6034 6044
6035 6045 *vpp = nvp;
6036 6046
6037 6047 exit:
6038 6048 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
6039 6049 kmem_free(argop, argoplist_size);
6040 6050 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
6041 6051 return (e.error);
6042 6052 }
6043 6053
6044 6054 #ifdef DEBUG
6045 6055 void
6046 6056 nfs4lookup_dump_compound(char *where, nfs_argop4 *argbase, int argcnt)
6047 6057 {
6048 6058 uint_t i, len;
6049 6059 zoneid_t zoneid = getzoneid();
6050 6060 char *s;
6051 6061
6052 6062 zcmn_err(zoneid, CE_NOTE, "%s: dumping cmpd", where);
6053 6063 for (i = 0; i < argcnt; i++) {
6054 6064 nfs_argop4 *op = &argbase[i];
6055 6065 switch (op->argop) {
6056 6066 case OP_CPUTFH:
6057 6067 case OP_PUTFH:
6058 6068 zcmn_err(zoneid, CE_NOTE, "\t op %d, putfh", i);
6059 6069 break;
6060 6070 case OP_PUTROOTFH:
6061 6071 zcmn_err(zoneid, CE_NOTE, "\t op %d, putrootfh", i);
6062 6072 break;
6063 6073 case OP_CLOOKUP:
6064 6074 s = op->nfs_argop4_u.opclookup.cname;
6065 6075 zcmn_err(zoneid, CE_NOTE, "\t op %d, lookup %s", i, s);
6066 6076 break;
6067 6077 case OP_LOOKUP:
6068 6078 s = utf8_to_str(&op->nfs_argop4_u.oplookup.objname,
6069 6079 &len, NULL);
6070 6080 zcmn_err(zoneid, CE_NOTE, "\t op %d, lookup %s", i, s);
6071 6081 kmem_free(s, len);
6072 6082 break;
6073 6083 case OP_LOOKUPP:
6074 6084 zcmn_err(zoneid, CE_NOTE, "\t op %d, lookupp ..", i);
6075 6085 break;
6076 6086 case OP_GETFH:
6077 6087 zcmn_err(zoneid, CE_NOTE, "\t op %d, getfh", i);
6078 6088 break;
6079 6089 case OP_GETATTR:
6080 6090 zcmn_err(zoneid, CE_NOTE, "\t op %d, getattr", i);
6081 6091 break;
6082 6092 case OP_OPENATTR:
6083 6093 zcmn_err(zoneid, CE_NOTE, "\t op %d, openattr", i);
6084 6094 break;
6085 6095 default:
6086 6096 zcmn_err(zoneid, CE_NOTE, "\t op %d, opcode %d", i,
6087 6097 op->argop);
6088 6098 break;
6089 6099 }
6090 6100 }
6091 6101 }
6092 6102 #endif
6093 6103
6094 6104 /*
6095 6105 * nfs4lookup_setup - constructs a multi-lookup compound request.
6096 6106 *
6097 6107 * Given the path "nm1/nm2/.../nmn", the following compound requests
6098 6108 * may be created:
6099 6109 *
6100 6110 * Note: Getfh is not be needed because filehandle attr is mandatory, but it
6101 6111 * is faster, for now.
6102 6112 *
6103 6113 * l4_getattrs indicates the type of compound requested.
6104 6114 *
6105 6115 * LKP4_NO_ATTRIBUTE - no attributes (used by secinfo):
6106 6116 *
6107 6117 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Lookup {nmn} }
6108 6118 *
6109 6119 * total number of ops is n + 1.
6110 6120 *
6111 6121 * LKP4_LAST_NAMED_ATTR - multi-component path for a named
6112 6122 * attribute: create lookups plus one OPENATTR/GETFH/GETATTR
6113 6123 * before the last component, and only get attributes
6114 6124 * for the last component. Note that the second-to-last
6115 6125 * pathname component is XATTR_RPATH, which does NOT go
6116 6126 * over-the-wire as a lookup.
6117 6127 *
6118 6128 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Lookup {nmn-2};
6119 6129 * Openattr; Getfh; Getattr; Lookup {nmn}; Getfh; Getattr }
6120 6130 *
6121 6131 * and total number of ops is n + 5.
6122 6132 *
6123 6133 * LKP4_LAST_ATTRDIR - multi-component path for the hidden named
6124 6134 * attribute directory: create lookups plus an OPENATTR
6125 6135 * replacing the last lookup. Note that the last pathname
6126 6136 * component is XATTR_RPATH, which does NOT go over-the-wire
6127 6137 * as a lookup.
6128 6138 *
6129 6139 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Getfh; Getattr;
6130 6140 * Openattr; Getfh; Getattr }
6131 6141 *
6132 6142 * and total number of ops is n + 5.
6133 6143 *
6134 6144 * LKP4_ALL_ATTRIBUTES - create lookups and get attributes for intermediate
6135 6145 * nodes too.
6136 6146 *
6137 6147 * compound { Put*fh; Lookup {nm1}; Getfh; Getattr;
6138 6148 * Lookup {nm2}; ... Lookup {nmn}; Getfh; Getattr }
6139 6149 *
6140 6150 * and total number of ops is 3*n + 1.
6141 6151 *
6142 6152 * All cases: returns the index in the arg array of the final LOOKUP op, or
6143 6153 * -1 if no LOOKUPs were used.
6144 6154 */
6145 6155 int
6146 6156 nfs4lookup_setup(char *nm, lookup4_param_t *lookupargp, int needgetfh)
6147 6157 {
6148 6158 enum lkp4_attr_setup l4_getattrs = lookupargp->l4_getattrs;
6149 6159 nfs_argop4 *argbase, *argop;
6150 6160 int arglen, argcnt;
6151 6161 int n = 1; /* number of components */
6152 6162 int nga = 1; /* number of Getattr's in request */
6153 6163 char c = '\0', *s, *p;
6154 6164 int lookup_idx = -1;
6155 6165 int argoplist_size;
6156 6166
6157 6167 /* set lookuparg response result to 0 */
6158 6168 lookupargp->resp->status = NFS4_OK;
6159 6169
6160 6170 /* skip leading "/" or "." e.g. ".//./" if there is */
6161 6171 for (; ; nm++) {
6162 6172 if (*nm != '/' && *nm != '.')
6163 6173 break;
6164 6174
6165 6175 /* ".." is counted as 1 component */
6166 6176 if (*nm == '.' && *(nm + 1) != '/')
6167 6177 break;
6168 6178 }
6169 6179
6170 6180 /*
6171 6181 * Find n = number of components - nm must be null terminated
6172 6182 * Skip "." components.
6173 6183 */
6174 6184 if (*nm != '\0')
6175 6185 for (n = 1, s = nm; *s != '\0'; s++) {
6176 6186 if ((*s == '/') && (*(s + 1) != '/') &&
6177 6187 (*(s + 1) != '\0') &&
6178 6188 !(*(s + 1) == '.' && (*(s + 2) == '/' ||
6179 6189 *(s + 2) == '\0')))
6180 6190 n++;
6181 6191 }
6182 6192 else
6183 6193 n = 0;
6184 6194
6185 6195 /*
6186 6196 * nga is number of components that need Getfh+Getattr
6187 6197 */
6188 6198 switch (l4_getattrs) {
6189 6199 case LKP4_NO_ATTRIBUTES:
6190 6200 nga = 0;
6191 6201 break;
6192 6202 case LKP4_ALL_ATTRIBUTES:
6193 6203 nga = n;
6194 6204 /*
6195 6205 * Always have at least 1 getfh, getattr pair
6196 6206 */
6197 6207 if (nga == 0)
6198 6208 nga++;
6199 6209 break;
6200 6210 case LKP4_LAST_ATTRDIR:
6201 6211 case LKP4_LAST_NAMED_ATTR:
6202 6212 nga = n+1;
6203 6213 break;
6204 6214 }
6205 6215
6206 6216 /*
6207 6217 * If change to use the filehandle attr instead of getfh
6208 6218 * the following line can be deleted.
6209 6219 */
6210 6220 nga *= 2;
6211 6221
6212 6222 /*
6213 6223 * calculate number of ops in request as
6214 6224 * header + trailer + lookups + getattrs
6215 6225 */
6216 6226 arglen = lookupargp->header_len + lookupargp->trailer_len + n + nga;
6217 6227
6218 6228 argoplist_size = arglen * sizeof (nfs_argop4);
6219 6229 argop = argbase = kmem_alloc(argoplist_size, KM_SLEEP);
6220 6230 lookupargp->argsp->array = argop;
6221 6231
6222 6232 argcnt = lookupargp->header_len;
6223 6233 argop += argcnt;
6224 6234
6225 6235 /*
6226 6236 * loop and create a lookup op and possibly getattr/getfh for
6227 6237 * each component. Skip "." components.
6228 6238 */
6229 6239 for (s = nm; *s != '\0'; s = p) {
6230 6240 /*
6231 6241 * Set up a pathname struct for each component if needed
6232 6242 */
6233 6243 while (*s == '/')
6234 6244 s++;
6235 6245 if (*s == '\0')
6236 6246 break;
6237 6247
6238 6248 for (p = s; (*p != '/') && (*p != '\0'); p++)
6239 6249 ;
6240 6250 c = *p;
6241 6251 *p = '\0';
6242 6252
6243 6253 if (s[0] == '.' && s[1] == '\0') {
6244 6254 *p = c;
6245 6255 continue;
6246 6256 }
6247 6257 if (l4_getattrs == LKP4_LAST_ATTRDIR &&
6248 6258 strcmp(s, XATTR_RPATH) == 0) {
6249 6259 /* getfh XXX may not be needed in future */
6250 6260 argop->argop = OP_GETFH;
6251 6261 argop++;
6252 6262 argcnt++;
6253 6263
6254 6264 /* getattr */
6255 6265 argop->argop = OP_GETATTR;
6256 6266 argop->nfs_argop4_u.opgetattr.attr_request =
6257 6267 lookupargp->ga_bits;
6258 6268 argop->nfs_argop4_u.opgetattr.mi =
6259 6269 lookupargp->mi;
6260 6270 argop++;
6261 6271 argcnt++;
6262 6272
6263 6273 /* openattr */
6264 6274 argop->argop = OP_OPENATTR;
6265 6275 } else if (l4_getattrs == LKP4_LAST_NAMED_ATTR &&
6266 6276 strcmp(s, XATTR_RPATH) == 0) {
6267 6277 /* openattr */
6268 6278 argop->argop = OP_OPENATTR;
6269 6279 argop++;
6270 6280 argcnt++;
6271 6281
6272 6282 /* getfh XXX may not be needed in future */
6273 6283 argop->argop = OP_GETFH;
6274 6284 argop++;
6275 6285 argcnt++;
6276 6286
6277 6287 /* getattr */
6278 6288 argop->argop = OP_GETATTR;
6279 6289 argop->nfs_argop4_u.opgetattr.attr_request =
6280 6290 lookupargp->ga_bits;
6281 6291 argop->nfs_argop4_u.opgetattr.mi =
6282 6292 lookupargp->mi;
6283 6293 argop++;
6284 6294 argcnt++;
6285 6295 *p = c;
6286 6296 continue;
6287 6297 } else if (s[0] == '.' && s[1] == '.' && s[2] == '\0') {
6288 6298 /* lookupp */
6289 6299 argop->argop = OP_LOOKUPP;
6290 6300 } else {
6291 6301 /* lookup */
6292 6302 argop->argop = OP_LOOKUP;
6293 6303 (void) str_to_utf8(s,
6294 6304 &argop->nfs_argop4_u.oplookup.objname);
6295 6305 }
6296 6306 lookup_idx = argcnt;
6297 6307 argop++;
6298 6308 argcnt++;
6299 6309
6300 6310 *p = c;
6301 6311
6302 6312 if (l4_getattrs == LKP4_ALL_ATTRIBUTES) {
6303 6313 /* getfh XXX may not be needed in future */
6304 6314 argop->argop = OP_GETFH;
6305 6315 argop++;
6306 6316 argcnt++;
6307 6317
6308 6318 /* getattr */
6309 6319 argop->argop = OP_GETATTR;
6310 6320 argop->nfs_argop4_u.opgetattr.attr_request =
6311 6321 lookupargp->ga_bits;
6312 6322 argop->nfs_argop4_u.opgetattr.mi =
6313 6323 lookupargp->mi;
6314 6324 argop++;
6315 6325 argcnt++;
6316 6326 }
6317 6327 }
6318 6328
6319 6329 if ((l4_getattrs != LKP4_NO_ATTRIBUTES) &&
6320 6330 ((l4_getattrs != LKP4_ALL_ATTRIBUTES) || (lookup_idx < 0))) {
6321 6331 if (needgetfh) {
6322 6332 /* stick in a post-lookup getfh */
6323 6333 argop->argop = OP_GETFH;
6324 6334 argcnt++;
6325 6335 argop++;
6326 6336 }
6327 6337 /* post-lookup getattr */
6328 6338 argop->argop = OP_GETATTR;
6329 6339 argop->nfs_argop4_u.opgetattr.attr_request =
6330 6340 lookupargp->ga_bits;
6331 6341 argop->nfs_argop4_u.opgetattr.mi = lookupargp->mi;
6332 6342 argcnt++;
6333 6343 }
6334 6344 argcnt += lookupargp->trailer_len; /* actual op count */
6335 6345 lookupargp->argsp->array_len = argcnt;
6336 6346 lookupargp->arglen = arglen;
6337 6347
6338 6348 #ifdef DEBUG
6339 6349 if (nfs4_client_lookup_debug)
6340 6350 nfs4lookup_dump_compound("nfs4lookup_setup", argbase, argcnt);
6341 6351 #endif
6342 6352
6343 6353 return (lookup_idx);
6344 6354 }
6345 6355
6346 6356 static int
6347 6357 nfs4openattr(vnode_t *dvp, vnode_t **avp, int cflag, cred_t *cr)
6348 6358 {
6349 6359 COMPOUND4args_clnt args;
6350 6360 COMPOUND4res_clnt res;
6351 6361 GETFH4res *gf_res = NULL;
6352 6362 nfs_argop4 argop[4];
6353 6363 nfs_resop4 *resop = NULL;
6354 6364 nfs4_sharedfh_t *sfhp;
6355 6365 hrtime_t t;
6356 6366 nfs4_error_t e;
6357 6367
6358 6368 rnode4_t *drp;
6359 6369 int doqueue = 1;
6360 6370 vnode_t *vp;
6361 6371 int needrecov = 0;
6362 6372 nfs4_recov_state_t recov_state;
6363 6373
6364 6374 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
6365 6375
6366 6376 *avp = NULL;
6367 6377 recov_state.rs_flags = 0;
6368 6378 recov_state.rs_num_retry_despite_err = 0;
6369 6379
6370 6380 recov_retry:
6371 6381 /* COMPOUND: putfh, openattr, getfh, getattr */
6372 6382 args.array_len = 4;
6373 6383 args.array = argop;
6374 6384 args.ctag = TAG_OPENATTR;
6375 6385
6376 6386 e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state);
6377 6387 if (e.error)
6378 6388 return (e.error);
6379 6389
6380 6390 drp = VTOR4(dvp);
6381 6391
6382 6392 /* putfh */
6383 6393 argop[0].argop = OP_CPUTFH;
6384 6394 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
6385 6395
6386 6396 /* openattr */
6387 6397 argop[1].argop = OP_OPENATTR;
6388 6398 argop[1].nfs_argop4_u.opopenattr.createdir = (cflag ? TRUE : FALSE);
6389 6399
6390 6400 /* getfh */
6391 6401 argop[2].argop = OP_GETFH;
6392 6402
6393 6403 /* getattr */
6394 6404 argop[3].argop = OP_GETATTR;
6395 6405 argop[3].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
6396 6406 argop[3].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
6397 6407
6398 6408 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE,
6399 6409 "nfs4openattr: %s call, drp %s", needrecov ? "recov" : "first",
6400 6410 rnode4info(drp)));
6401 6411
6402 6412 t = gethrtime();
6403 6413
6404 6414 rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e);
6405 6415
6406 6416 needrecov = nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp);
6407 6417 if (needrecov) {
6408 6418 bool_t abort;
6409 6419
6410 6420 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
6411 6421 "nfs4openattr: initiating recovery\n"));
6412 6422
6413 6423 abort = nfs4_start_recovery(&e,
6414 6424 VTOMI4(dvp), dvp, NULL, NULL, NULL,
6415 6425 OP_OPENATTR, NULL, NULL, NULL);
6416 6426 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov);
6417 6427 if (!e.error) {
6418 6428 e.error = geterrno4(res.status);
6419 6429 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
6420 6430 }
6421 6431 if (abort == FALSE)
6422 6432 goto recov_retry;
6423 6433 return (e.error);
6424 6434 }
6425 6435
6426 6436 if (e.error) {
6427 6437 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov);
6428 6438 return (e.error);
6429 6439 }
6430 6440
6431 6441 if (res.status) {
6432 6442 /*
6433 6443 * If OTW errro is NOTSUPP, then it should be
6434 6444 * translated to EINVAL. All Solaris file system
6435 6445 * implementations return EINVAL to the syscall layer
6436 6446 * when the attrdir cannot be created due to an
6437 6447 * implementation restriction or noxattr mount option.
6438 6448 */
6439 6449 if (res.status == NFS4ERR_NOTSUPP) {
6440 6450 mutex_enter(&drp->r_statelock);
6441 6451 if (drp->r_xattr_dir)
6442 6452 VN_RELE(drp->r_xattr_dir);
6443 6453 VN_HOLD(NFS4_XATTR_DIR_NOTSUPP);
6444 6454 drp->r_xattr_dir = NFS4_XATTR_DIR_NOTSUPP;
6445 6455 mutex_exit(&drp->r_statelock);
6446 6456
6447 6457 e.error = EINVAL;
6448 6458 } else {
6449 6459 e.error = geterrno4(res.status);
6450 6460 }
6451 6461
6452 6462 if (e.error) {
6453 6463 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
6454 6464 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state,
6455 6465 needrecov);
6456 6466 return (e.error);
6457 6467 }
6458 6468 }
6459 6469
6460 6470 resop = &res.array[0]; /* putfh res */
6461 6471 ASSERT(resop->nfs_resop4_u.opgetfh.status == NFS4_OK);
6462 6472
6463 6473 resop = &res.array[1]; /* openattr res */
6464 6474 ASSERT(resop->nfs_resop4_u.opopenattr.status == NFS4_OK);
6465 6475
6466 6476 resop = &res.array[2]; /* getfh res */
6467 6477 gf_res = &resop->nfs_resop4_u.opgetfh;
6468 6478 if (gf_res->object.nfs_fh4_len == 0) {
6469 6479 *avp = NULL;
6470 6480 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
6471 6481 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov);
6472 6482 return (ENOENT);
6473 6483 }
6474 6484
6475 6485 sfhp = sfh4_get(&gf_res->object, VTOMI4(dvp));
6476 6486 vp = makenfs4node(sfhp, &res.array[3].nfs_resop4_u.opgetattr.ga_res,
6477 6487 dvp->v_vfsp, t, cr, dvp,
6478 6488 fn_get(VTOSV(dvp)->sv_name, XATTR_RPATH, sfhp));
6479 6489 sfh4_rele(&sfhp);
6480 6490
6481 6491 if (e.error)
6482 6492 PURGE_ATTRCACHE4(vp);
6483 6493
6484 6494 mutex_enter(&vp->v_lock);
6485 6495 vp->v_flag |= V_XATTRDIR;
6486 6496 mutex_exit(&vp->v_lock);
6487 6497
6488 6498 *avp = vp;
6489 6499
6490 6500 mutex_enter(&drp->r_statelock);
6491 6501 if (drp->r_xattr_dir)
6492 6502 VN_RELE(drp->r_xattr_dir);
6493 6503 VN_HOLD(vp);
6494 6504 drp->r_xattr_dir = vp;
6495 6505
6496 6506 /*
6497 6507 * Invalidate pathconf4 cache because r_xattr_dir is no longer
6498 6508 * NULL. xattrs could be created at any time, and we have no
6499 6509 * way to update pc4_xattr_exists in the base object if/when
6500 6510 * it happens.
6501 6511 */
6502 6512 drp->r_pathconf.pc4_xattr_valid = 0;
6503 6513
6504 6514 mutex_exit(&drp->r_statelock);
6505 6515
6506 6516 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov);
6507 6517
6508 6518 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
6509 6519
6510 6520 return (0);
6511 6521 }
6512 6522
6513 6523 /* ARGSUSED */
6514 6524 static int
6515 6525 nfs4_create(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive,
6516 6526 int mode, vnode_t **vpp, cred_t *cr, int flags, caller_context_t *ct,
6517 6527 vsecattr_t *vsecp)
6518 6528 {
6519 6529 int error;
6520 6530 vnode_t *vp = NULL;
6521 6531 rnode4_t *rp;
6522 6532 struct vattr vattr;
6523 6533 rnode4_t *drp;
6524 6534 vnode_t *tempvp;
6525 6535 enum createmode4 createmode;
6526 6536 bool_t must_trunc = FALSE;
6527 6537 int truncating = 0;
6528 6538
6529 6539 if (nfs_zone() != VTOMI4(dvp)->mi_zone)
6530 6540 return (EPERM);
6531 6541 if (exclusive == EXCL && (dvp->v_flag & V_XATTRDIR)) {
6532 6542 return (EINVAL);
6533 6543 }
6534 6544
6535 6545 /* . and .. have special meaning in the protocol, reject them. */
6536 6546
6537 6547 if (nm[0] == '.' && (nm[1] == '\0' || (nm[1] == '.' && nm[2] == '\0')))
6538 6548 return (EISDIR);
6539 6549
6540 6550 drp = VTOR4(dvp);
6541 6551
6542 6552 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp)))
6543 6553 return (EINTR);
6544 6554
6545 6555 top:
6546 6556 /*
6547 6557 * We make a copy of the attributes because the caller does not
6548 6558 * expect us to change what va points to.
6549 6559 */
6550 6560 vattr = *va;
6551 6561
6552 6562 /*
6553 6563 * If the pathname is "", then dvp is the root vnode of
6554 6564 * a remote file mounted over a local directory.
6555 6565 * All that needs to be done is access
6556 6566 * checking and truncation. Note that we avoid doing
6557 6567 * open w/ create because the parent directory might
6558 6568 * be in pseudo-fs and the open would fail.
6559 6569 */
6560 6570 if (*nm == '\0') {
6561 6571 error = 0;
6562 6572 VN_HOLD(dvp);
6563 6573 vp = dvp;
6564 6574 must_trunc = TRUE;
6565 6575 } else {
6566 6576 /*
6567 6577 * We need to go over the wire, just to be sure whether the
6568 6578 * file exists or not. Using the DNLC can be dangerous in
6569 6579 * this case when making a decision regarding existence.
6570 6580 */
6571 6581 error = nfs4lookup(dvp, nm, &vp, cr, 1);
6572 6582 }
6573 6583
6574 6584 if (exclusive)
6575 6585 createmode = EXCLUSIVE4;
6576 6586 else
6577 6587 createmode = GUARDED4;
6578 6588
6579 6589 /*
6580 6590 * error would be set if the file does not exist on the
6581 6591 * server, so lets go create it.
6582 6592 */
6583 6593 if (error) {
6584 6594 goto create_otw;
6585 6595 }
6586 6596
6587 6597 /*
6588 6598 * File does exist on the server
6589 6599 */
6590 6600 if (exclusive == EXCL)
6591 6601 error = EEXIST;
6592 6602 else if (vp->v_type == VDIR && (mode & VWRITE))
6593 6603 error = EISDIR;
6594 6604 else {
6595 6605 /*
6596 6606 * If vnode is a device, create special vnode.
6597 6607 */
6598 6608 if (ISVDEV(vp->v_type)) {
6599 6609 tempvp = vp;
6600 6610 vp = specvp(vp, vp->v_rdev, vp->v_type, cr);
6601 6611 VN_RELE(tempvp);
6602 6612 }
6603 6613 if (!(error = VOP_ACCESS(vp, mode, 0, cr, ct))) {
6604 6614 if ((vattr.va_mask & AT_SIZE) &&
6605 6615 vp->v_type == VREG) {
6606 6616 rp = VTOR4(vp);
6607 6617 /*
6608 6618 * Check here for large file handled
6609 6619 * by LF-unaware process (as
6610 6620 * ufs_create() does)
6611 6621 */
6612 6622 if (!(flags & FOFFMAX)) {
6613 6623 mutex_enter(&rp->r_statelock);
6614 6624 if (rp->r_size > MAXOFF32_T)
6615 6625 error = EOVERFLOW;
6616 6626 mutex_exit(&rp->r_statelock);
6617 6627 }
6618 6628
6619 6629 /* if error is set then we need to return */
6620 6630 if (error) {
6621 6631 nfs_rw_exit(&drp->r_rwlock);
6622 6632 VN_RELE(vp);
6623 6633 return (error);
6624 6634 }
6625 6635
6626 6636 if (must_trunc) {
6627 6637 vattr.va_mask = AT_SIZE;
6628 6638 error = nfs4setattr(vp, &vattr, 0, cr,
6629 6639 NULL);
6630 6640 } else {
6631 6641 /*
6632 6642 * we know we have a regular file that already
6633 6643 * exists and we may end up truncating the file
6634 6644 * as a result of the open_otw, so flush out
6635 6645 * any dirty pages for this file first.
6636 6646 */
6637 6647 if (nfs4_has_pages(vp) &&
6638 6648 ((rp->r_flags & R4DIRTY) ||
6639 6649 rp->r_count > 0 ||
6640 6650 rp->r_mapcnt > 0)) {
6641 6651 error = nfs4_putpage(vp,
6642 6652 (offset_t)0, 0, 0, cr, ct);
6643 6653 if (error && (error == ENOSPC ||
6644 6654 error == EDQUOT)) {
6645 6655 mutex_enter(
6646 6656 &rp->r_statelock);
6647 6657 if (!rp->r_error)
6648 6658 rp->r_error =
6649 6659 error;
6650 6660 mutex_exit(
6651 6661 &rp->r_statelock);
6652 6662 }
6653 6663 }
6654 6664 vattr.va_mask = (AT_SIZE |
6655 6665 AT_TYPE | AT_MODE);
6656 6666 vattr.va_type = VREG;
6657 6667 createmode = UNCHECKED4;
6658 6668 truncating = 1;
6659 6669 goto create_otw;
6660 6670 }
6661 6671 }
6662 6672 }
6663 6673 }
6664 6674 nfs_rw_exit(&drp->r_rwlock);
6665 6675 if (error) {
6666 6676 VN_RELE(vp);
6667 6677 } else {
6668 6678 vnode_t *tvp;
6669 6679 rnode4_t *trp;
6670 6680 tvp = vp;
6671 6681 if (vp->v_type == VREG) {
6672 6682 trp = VTOR4(vp);
6673 6683 if (IS_SHADOW(vp, trp))
6674 6684 tvp = RTOV4(trp);
6675 6685 }
6676 6686
6677 6687 if (must_trunc) {
6678 6688 /*
6679 6689 * existing file got truncated, notify.
6680 6690 */
6681 6691 vnevent_create(tvp, ct);
6682 6692 }
6683 6693
6684 6694 *vpp = vp;
6685 6695 }
6686 6696 return (error);
6687 6697
6688 6698 create_otw:
6689 6699 dnlc_remove(dvp, nm);
6690 6700
6691 6701 ASSERT(vattr.va_mask & AT_TYPE);
6692 6702
6693 6703 /*
6694 6704 * If not a regular file let nfs4mknod() handle it.
6695 6705 */
6696 6706 if (vattr.va_type != VREG) {
6697 6707 error = nfs4mknod(dvp, nm, &vattr, exclusive, mode, vpp, cr);
6698 6708 nfs_rw_exit(&drp->r_rwlock);
6699 6709 return (error);
6700 6710 }
6701 6711
6702 6712 /*
6703 6713 * It _is_ a regular file.
6704 6714 */
6705 6715 ASSERT(vattr.va_mask & AT_MODE);
6706 6716 if (MANDMODE(vattr.va_mode)) {
6707 6717 nfs_rw_exit(&drp->r_rwlock);
6708 6718 return (EACCES);
6709 6719 }
6710 6720
6711 6721 /*
6712 6722 * If this happens to be a mknod of a regular file, then flags will
6713 6723 * have neither FREAD or FWRITE. However, we must set at least one
6714 6724 * for the call to nfs4open_otw. If it's open(O_CREAT) driving
6715 6725 * nfs4_create, then either FREAD, FWRITE, or FRDWR has already been
6716 6726 * set (based on openmode specified by app).
6717 6727 */
6718 6728 if ((flags & (FREAD|FWRITE)) == 0)
6719 6729 flags |= (FREAD|FWRITE);
6720 6730
6721 6731 error = nfs4open_otw(dvp, nm, &vattr, vpp, cr, 1, flags, createmode, 0);
6722 6732
6723 6733 if (vp != NULL) {
6724 6734 /* if create was successful, throw away the file's pages */
6725 6735 if (!error && (vattr.va_mask & AT_SIZE))
6726 6736 nfs4_invalidate_pages(vp, (vattr.va_size & PAGEMASK),
6727 6737 cr);
6728 6738 /* release the lookup hold */
6729 6739 VN_RELE(vp);
6730 6740 vp = NULL;
6731 6741 }
6732 6742
6733 6743 /*
6734 6744 * validate that we opened a regular file. This handles a misbehaving
6735 6745 * server that returns an incorrect FH.
6736 6746 */
6737 6747 if ((error == 0) && *vpp && (*vpp)->v_type != VREG) {
6738 6748 error = EISDIR;
6739 6749 VN_RELE(*vpp);
6740 6750 }
6741 6751
6742 6752 /*
6743 6753 * If this is not an exclusive create, then the CREATE
6744 6754 * request will be made with the GUARDED mode set. This
6745 6755 * means that the server will return EEXIST if the file
6746 6756 * exists. The file could exist because of a retransmitted
6747 6757 * request. In this case, we recover by starting over and
6748 6758 * checking to see whether the file exists. This second
6749 6759 * time through it should and a CREATE request will not be
6750 6760 * sent.
6751 6761 *
6752 6762 * This handles the problem of a dangling CREATE request
6753 6763 * which contains attributes which indicate that the file
6754 6764 * should be truncated. This retransmitted request could
6755 6765 * possibly truncate valid data in the file if not caught
6756 6766 * by the duplicate request mechanism on the server or if
6757 6767 * not caught by other means. The scenario is:
6758 6768 *
6759 6769 * Client transmits CREATE request with size = 0
6760 6770 * Client times out, retransmits request.
6761 6771 * Response to the first request arrives from the server
6762 6772 * and the client proceeds on.
6763 6773 * Client writes data to the file.
6764 6774 * The server now processes retransmitted CREATE request
6765 6775 * and truncates file.
6766 6776 *
6767 6777 * The use of the GUARDED CREATE request prevents this from
6768 6778 * happening because the retransmitted CREATE would fail
6769 6779 * with EEXIST and would not truncate the file.
6770 6780 */
6771 6781 if (error == EEXIST && exclusive == NONEXCL) {
6772 6782 #ifdef DEBUG
6773 6783 nfs4_create_misses++;
6774 6784 #endif
6775 6785 goto top;
6776 6786 }
6777 6787 nfs_rw_exit(&drp->r_rwlock);
6778 6788 if (truncating && !error && *vpp) {
6779 6789 vnode_t *tvp;
6780 6790 rnode4_t *trp;
6781 6791 /*
6782 6792 * existing file got truncated, notify.
6783 6793 */
6784 6794 tvp = *vpp;
6785 6795 trp = VTOR4(tvp);
6786 6796 if (IS_SHADOW(tvp, trp))
6787 6797 tvp = RTOV4(trp);
6788 6798 vnevent_create(tvp, ct);
6789 6799 }
6790 6800 return (error);
6791 6801 }
6792 6802
6793 6803 /*
6794 6804 * Create compound (for mkdir, mknod, symlink):
6795 6805 * { Putfh <dfh>; Create; Getfh; Getattr }
6796 6806 * It's okay if setattr failed to set gid - this is not considered
6797 6807 * an error, but purge attrs in that case.
6798 6808 */
6799 6809 static int
6800 6810 call_nfs4_create_req(vnode_t *dvp, char *nm, void *data, struct vattr *va,
6801 6811 vnode_t **vpp, cred_t *cr, nfs_ftype4 type)
6802 6812 {
6803 6813 int need_end_op = FALSE;
6804 6814 COMPOUND4args_clnt args;
6805 6815 COMPOUND4res_clnt res, *resp = NULL;
6806 6816 nfs_argop4 *argop;
6807 6817 nfs_resop4 *resop;
6808 6818 int doqueue;
6809 6819 mntinfo4_t *mi;
6810 6820 rnode4_t *drp = VTOR4(dvp);
6811 6821 change_info4 *cinfo;
6812 6822 GETFH4res *gf_res;
6813 6823 struct vattr vattr;
6814 6824 vnode_t *vp;
6815 6825 fattr4 *crattr;
6816 6826 bool_t needrecov = FALSE;
6817 6827 nfs4_recov_state_t recov_state;
6818 6828 nfs4_sharedfh_t *sfhp = NULL;
6819 6829 hrtime_t t;
6820 6830 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
6821 6831 int numops, argoplist_size, setgid_flag, idx_create, idx_fattr;
6822 6832 dirattr_info_t dinfo, *dinfop;
6823 6833 servinfo4_t *svp;
6824 6834 bitmap4 supp_attrs;
6825 6835
6826 6836 ASSERT(type == NF4DIR || type == NF4LNK || type == NF4BLK ||
6827 6837 type == NF4CHR || type == NF4SOCK || type == NF4FIFO);
6828 6838
6829 6839 mi = VTOMI4(dvp);
6830 6840
6831 6841 /*
6832 6842 * Make sure we properly deal with setting the right gid
6833 6843 * on a new directory to reflect the parent's setgid bit
6834 6844 */
6835 6845 setgid_flag = 0;
6836 6846 if (type == NF4DIR) {
6837 6847 struct vattr dva;
6838 6848
6839 6849 va->va_mode &= ~VSGID;
6840 6850 dva.va_mask = AT_MODE | AT_GID;
6841 6851 if (VOP_GETATTR(dvp, &dva, 0, cr, NULL) == 0) {
6842 6852
6843 6853 /*
6844 6854 * If the parent's directory has the setgid bit set
6845 6855 * _and_ the client was able to get a valid mapping
6846 6856 * for the parent dir's owner_group, we want to
6847 6857 * append NVERIFY(owner_group == dva.va_gid) and
6848 6858 * SETTATTR to the CREATE compound.
6849 6859 */
6850 6860 if (mi->mi_flags & MI4_GRPID || dva.va_mode & VSGID) {
6851 6861 setgid_flag = 1;
6852 6862 va->va_mode |= VSGID;
6853 6863 if (dva.va_gid != GID_NOBODY) {
6854 6864 va->va_mask |= AT_GID;
6855 6865 va->va_gid = dva.va_gid;
6856 6866 }
6857 6867 }
6858 6868 }
6859 6869 }
6860 6870
6861 6871 /*
6862 6872 * Create ops:
6863 6873 * 0:putfh(dir) 1:savefh(dir) 2:create 3:getfh(new) 4:getattr(new)
6864 6874 * 5:restorefh(dir) 6:getattr(dir)
6865 6875 *
6866 6876 * if (setgid)
6867 6877 * 0:putfh(dir) 1:create 2:getfh(new) 3:getattr(new)
6868 6878 * 4:savefh(new) 5:putfh(dir) 6:getattr(dir) 7:restorefh(new)
6869 6879 * 8:nverify 9:setattr
6870 6880 */
6871 6881 if (setgid_flag) {
6872 6882 numops = 10;
6873 6883 idx_create = 1;
6874 6884 idx_fattr = 3;
6875 6885 } else {
6876 6886 numops = 7;
6877 6887 idx_create = 2;
6878 6888 idx_fattr = 4;
6879 6889 }
6880 6890
6881 6891 ASSERT(nfs_zone() == mi->mi_zone);
6882 6892 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp))) {
6883 6893 return (EINTR);
6884 6894 }
6885 6895 recov_state.rs_flags = 0;
6886 6896 recov_state.rs_num_retry_despite_err = 0;
6887 6897
6888 6898 argoplist_size = numops * sizeof (nfs_argop4);
6889 6899 argop = kmem_alloc(argoplist_size, KM_SLEEP);
6890 6900
6891 6901 recov_retry:
6892 6902 if (type == NF4LNK)
6893 6903 args.ctag = TAG_SYMLINK;
6894 6904 else if (type == NF4DIR)
6895 6905 args.ctag = TAG_MKDIR;
6896 6906 else
6897 6907 args.ctag = TAG_MKNOD;
6898 6908
6899 6909 args.array_len = numops;
6900 6910 args.array = argop;
6901 6911
6902 6912 if (e.error = nfs4_start_op(mi, dvp, NULL, &recov_state)) {
6903 6913 nfs_rw_exit(&drp->r_rwlock);
6904 6914 kmem_free(argop, argoplist_size);
6905 6915 return (e.error);
6906 6916 }
6907 6917 need_end_op = TRUE;
6908 6918
6909 6919
6910 6920 /* 0: putfh directory */
6911 6921 argop[0].argop = OP_CPUTFH;
6912 6922 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
6913 6923
6914 6924 /* 1/2: Create object */
6915 6925 argop[idx_create].argop = OP_CCREATE;
6916 6926 argop[idx_create].nfs_argop4_u.opccreate.cname = nm;
6917 6927 argop[idx_create].nfs_argop4_u.opccreate.type = type;
6918 6928 if (type == NF4LNK) {
6919 6929 /*
6920 6930 * symlink, treat name as data
6921 6931 */
6922 6932 ASSERT(data != NULL);
6923 6933 argop[idx_create].nfs_argop4_u.opccreate.ftype4_u.clinkdata =
6924 6934 (char *)data;
6925 6935 }
6926 6936 if (type == NF4BLK || type == NF4CHR) {
6927 6937 ASSERT(data != NULL);
6928 6938 argop[idx_create].nfs_argop4_u.opccreate.ftype4_u.devdata =
6929 6939 *((specdata4 *)data);
6930 6940 }
6931 6941
6932 6942 crattr = &argop[idx_create].nfs_argop4_u.opccreate.createattrs;
6933 6943
6934 6944 svp = drp->r_server;
6935 6945 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
6936 6946 supp_attrs = svp->sv_supp_attrs;
6937 6947 nfs_rw_exit(&svp->sv_lock);
6938 6948
6939 6949 if (vattr_to_fattr4(va, NULL, crattr, 0, OP_CREATE, supp_attrs)) {
6940 6950 nfs_rw_exit(&drp->r_rwlock);
6941 6951 nfs4_end_op(mi, dvp, NULL, &recov_state, needrecov);
6942 6952 e.error = EINVAL;
6943 6953 kmem_free(argop, argoplist_size);
6944 6954 return (e.error);
6945 6955 }
6946 6956
6947 6957 /* 2/3: getfh fh of created object */
6948 6958 ASSERT(idx_create + 1 == idx_fattr - 1);
6949 6959 argop[idx_create + 1].argop = OP_GETFH;
6950 6960
6951 6961 /* 3/4: getattr of new object */
6952 6962 argop[idx_fattr].argop = OP_GETATTR;
6953 6963 argop[idx_fattr].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
6954 6964 argop[idx_fattr].nfs_argop4_u.opgetattr.mi = mi;
6955 6965
6956 6966 if (setgid_flag) {
6957 6967 vattr_t _v;
6958 6968
6959 6969 argop[4].argop = OP_SAVEFH;
6960 6970
6961 6971 argop[5].argop = OP_CPUTFH;
6962 6972 argop[5].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
6963 6973
6964 6974 argop[6].argop = OP_GETATTR;
6965 6975 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
6966 6976 argop[6].nfs_argop4_u.opgetattr.mi = mi;
6967 6977
6968 6978 argop[7].argop = OP_RESTOREFH;
6969 6979
6970 6980 /*
6971 6981 * nverify
6972 6982 *
6973 6983 * XXX - Revisit the last argument to nfs4_end_op()
6974 6984 * once 5020486 is fixed.
6975 6985 */
6976 6986 _v.va_mask = AT_GID;
6977 6987 _v.va_gid = va->va_gid;
6978 6988 if (e.error = nfs4args_verify(&argop[8], &_v, OP_NVERIFY,
6979 6989 supp_attrs)) {
6980 6990 nfs4_end_op(mi, dvp, *vpp, &recov_state, TRUE);
6981 6991 nfs_rw_exit(&drp->r_rwlock);
6982 6992 nfs4_fattr4_free(crattr);
6983 6993 kmem_free(argop, argoplist_size);
6984 6994 return (e.error);
6985 6995 }
6986 6996
6987 6997 /*
6988 6998 * setattr
6989 6999 *
6990 7000 * We _know_ we're not messing with AT_SIZE or AT_XTIME,
6991 7001 * so no need for stateid or flags. Also we specify NULL
6992 7002 * rp since we're only interested in setting owner_group
6993 7003 * attributes.
6994 7004 */
6995 7005 nfs4args_setattr(&argop[9], &_v, NULL, 0, NULL, cr, supp_attrs,
6996 7006 &e.error, 0);
6997 7007
6998 7008 if (e.error) {
6999 7009 nfs4_end_op(mi, dvp, *vpp, &recov_state, TRUE);
7000 7010 nfs_rw_exit(&drp->r_rwlock);
7001 7011 nfs4_fattr4_free(crattr);
7002 7012 nfs4args_verify_free(&argop[8]);
7003 7013 kmem_free(argop, argoplist_size);
7004 7014 return (e.error);
7005 7015 }
7006 7016 } else {
7007 7017 argop[1].argop = OP_SAVEFH;
7008 7018
7009 7019 argop[5].argop = OP_RESTOREFH;
7010 7020
7011 7021 argop[6].argop = OP_GETATTR;
7012 7022 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
7013 7023 argop[6].nfs_argop4_u.opgetattr.mi = mi;
7014 7024 }
7015 7025
7016 7026 dnlc_remove(dvp, nm);
7017 7027
7018 7028 doqueue = 1;
7019 7029 t = gethrtime();
7020 7030 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
7021 7031
7022 7032 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
7023 7033 if (e.error) {
7024 7034 PURGE_ATTRCACHE4(dvp);
7025 7035 if (!needrecov)
7026 7036 goto out;
7027 7037 }
7028 7038
7029 7039 if (needrecov) {
7030 7040 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL,
7031 7041 OP_CREATE, NULL, NULL, NULL) == FALSE) {
7032 7042 nfs4_end_op(mi, dvp, NULL, &recov_state,
7033 7043 needrecov);
7034 7044 need_end_op = FALSE;
7035 7045 nfs4_fattr4_free(crattr);
7036 7046 if (setgid_flag) {
7037 7047 nfs4args_verify_free(&argop[8]);
7038 7048 nfs4args_setattr_free(&argop[9]);
7039 7049 }
7040 7050 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
7041 7051 goto recov_retry;
7042 7052 }
7043 7053 }
7044 7054
7045 7055 resp = &res;
7046 7056
7047 7057 if (res.status != NFS4_OK && res.array_len <= idx_fattr + 1) {
7048 7058
7049 7059 if (res.status == NFS4ERR_BADOWNER)
7050 7060 nfs4_log_badowner(mi, OP_CREATE);
7051 7061
7052 7062 e.error = geterrno4(res.status);
7053 7063
7054 7064 /*
7055 7065 * This check is left over from when create was implemented
7056 7066 * using a setattr op (instead of createattrs). If the
7057 7067 * putfh/create/getfh failed, the error was returned. If
7058 7068 * setattr/getattr failed, we keep going.
7059 7069 *
7060 7070 * It might be better to get rid of the GETFH also, and just
7061 7071 * do PUTFH/CREATE/GETATTR since the FH attr is mandatory.
7062 7072 * Then if any of the operations failed, we could return the
7063 7073 * error now, and remove much of the error code below.
7064 7074 */
7065 7075 if (res.array_len <= idx_fattr) {
7066 7076 /*
7067 7077 * Either Putfh, Create or Getfh failed.
7068 7078 */
7069 7079 PURGE_ATTRCACHE4(dvp);
7070 7080 /*
7071 7081 * nfs4_purge_stale_fh() may generate otw calls through
7072 7082 * nfs4_invalidate_pages. Hence the need to call
7073 7083 * nfs4_end_op() here to avoid nfs4_start_op() deadlock.
7074 7084 */
7075 7085 nfs4_end_op(mi, dvp, NULL, &recov_state,
7076 7086 needrecov);
7077 7087 need_end_op = FALSE;
7078 7088 nfs4_purge_stale_fh(e.error, dvp, cr);
7079 7089 goto out;
7080 7090 }
7081 7091 }
7082 7092
7083 7093 resop = &res.array[idx_create]; /* create res */
7084 7094 cinfo = &resop->nfs_resop4_u.opcreate.cinfo;
7085 7095
7086 7096 resop = &res.array[idx_create + 1]; /* getfh res */
7087 7097 gf_res = &resop->nfs_resop4_u.opgetfh;
7088 7098
7089 7099 sfhp = sfh4_get(&gf_res->object, mi);
7090 7100 if (e.error) {
7091 7101 *vpp = vp = makenfs4node(sfhp, NULL, dvp->v_vfsp, t, cr, dvp,
7092 7102 fn_get(VTOSV(dvp)->sv_name, nm, sfhp));
7093 7103 if (vp->v_type == VNON) {
7094 7104 vattr.va_mask = AT_TYPE;
7095 7105 /*
7096 7106 * Need to call nfs4_end_op before nfs4getattr to avoid
7097 7107 * potential nfs4_start_op deadlock. See RFE 4777612.
7098 7108 */
7099 7109 nfs4_end_op(mi, dvp, NULL, &recov_state,
7100 7110 needrecov);
7101 7111 need_end_op = FALSE;
7102 7112 e.error = nfs4getattr(vp, &vattr, cr);
7103 7113 if (e.error) {
7104 7114 VN_RELE(vp);
7105 7115 *vpp = NULL;
7106 7116 goto out;
7107 7117 }
7108 7118 vp->v_type = vattr.va_type;
7109 7119 }
7110 7120 e.error = 0;
7111 7121 } else {
7112 7122 *vpp = vp = makenfs4node(sfhp,
7113 7123 &res.array[idx_fattr].nfs_resop4_u.opgetattr.ga_res,
7114 7124 dvp->v_vfsp, t, cr,
7115 7125 dvp, fn_get(VTOSV(dvp)->sv_name, nm, sfhp));
7116 7126 }
7117 7127
7118 7128 /*
7119 7129 * If compound succeeded, then update dir attrs
7120 7130 */
7121 7131 if (res.status == NFS4_OK) {
7122 7132 dinfo.di_garp = &res.array[6].nfs_resop4_u.opgetattr.ga_res;
7123 7133 dinfo.di_cred = cr;
7124 7134 dinfo.di_time_call = t;
7125 7135 dinfop = &dinfo;
7126 7136 } else
7127 7137 dinfop = NULL;
7128 7138
7129 7139 /* Update directory cache attribute, readdir and dnlc caches */
7130 7140 nfs4_update_dircaches(cinfo, dvp, vp, nm, dinfop);
7131 7141
7132 7142 out:
7133 7143 if (sfhp != NULL)
7134 7144 sfh4_rele(&sfhp);
7135 7145 nfs_rw_exit(&drp->r_rwlock);
7136 7146 nfs4_fattr4_free(crattr);
7137 7147 if (setgid_flag) {
7138 7148 nfs4args_verify_free(&argop[8]);
7139 7149 nfs4args_setattr_free(&argop[9]);
7140 7150 }
7141 7151 if (resp)
7142 7152 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
7143 7153 if (need_end_op)
7144 7154 nfs4_end_op(mi, dvp, NULL, &recov_state, needrecov);
7145 7155
7146 7156 kmem_free(argop, argoplist_size);
7147 7157 return (e.error);
7148 7158 }
7149 7159
7150 7160 /* ARGSUSED */
7151 7161 static int
7152 7162 nfs4mknod(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive,
7153 7163 int mode, vnode_t **vpp, cred_t *cr)
7154 7164 {
7155 7165 int error;
7156 7166 vnode_t *vp;
7157 7167 nfs_ftype4 type;
7158 7168 specdata4 spec, *specp = NULL;
7159 7169
7160 7170 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
7161 7171
7162 7172 switch (va->va_type) {
7163 7173 case VCHR:
7164 7174 case VBLK:
7165 7175 type = (va->va_type == VCHR) ? NF4CHR : NF4BLK;
7166 7176 spec.specdata1 = getmajor(va->va_rdev);
7167 7177 spec.specdata2 = getminor(va->va_rdev);
7168 7178 specp = &spec;
7169 7179 break;
7170 7180
7171 7181 case VFIFO:
7172 7182 type = NF4FIFO;
7173 7183 break;
7174 7184 case VSOCK:
7175 7185 type = NF4SOCK;
7176 7186 break;
7177 7187
7178 7188 default:
7179 7189 return (EINVAL);
7180 7190 }
7181 7191
7182 7192 error = call_nfs4_create_req(dvp, nm, specp, va, &vp, cr, type);
7183 7193 if (error) {
7184 7194 return (error);
7185 7195 }
7186 7196
7187 7197 /*
7188 7198 * This might not be needed any more; special case to deal
7189 7199 * with problematic v2/v3 servers. Since create was unable
7190 7200 * to set group correctly, not sure what hope setattr has.
7191 7201 */
7192 7202 if (va->va_gid != VTOR4(vp)->r_attr.va_gid) {
7193 7203 va->va_mask = AT_GID;
7194 7204 (void) nfs4setattr(vp, va, 0, cr, NULL);
7195 7205 }
7196 7206
7197 7207 /*
7198 7208 * If vnode is a device create special vnode
7199 7209 */
7200 7210 if (ISVDEV(vp->v_type)) {
7201 7211 *vpp = specvp(vp, vp->v_rdev, vp->v_type, cr);
7202 7212 VN_RELE(vp);
7203 7213 } else {
7204 7214 *vpp = vp;
7205 7215 }
7206 7216 return (error);
7207 7217 }
7208 7218
7209 7219 /*
7210 7220 * Remove requires that the current fh be the target directory.
7211 7221 * After the operation, the current fh is unchanged.
7212 7222 * The compound op structure is:
7213 7223 * PUTFH(targetdir), REMOVE
7214 7224 *
7215 7225 * Weirdness: if the vnode to be removed is open
7216 7226 * we rename it instead of removing it and nfs_inactive
7217 7227 * will remove the new name.
7218 7228 */
7219 7229 /* ARGSUSED */
7220 7230 static int
7221 7231 nfs4_remove(vnode_t *dvp, char *nm, cred_t *cr, caller_context_t *ct, int flags)
7222 7232 {
7223 7233 COMPOUND4args_clnt args;
7224 7234 COMPOUND4res_clnt res, *resp = NULL;
7225 7235 REMOVE4res *rm_res;
7226 7236 nfs_argop4 argop[3];
7227 7237 nfs_resop4 *resop;
7228 7238 vnode_t *vp;
7229 7239 char *tmpname;
7230 7240 int doqueue;
7231 7241 mntinfo4_t *mi;
7232 7242 rnode4_t *rp;
7233 7243 rnode4_t *drp;
7234 7244 int needrecov = 0;
7235 7245 nfs4_recov_state_t recov_state;
7236 7246 int isopen;
7237 7247 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
7238 7248 dirattr_info_t dinfo;
7239 7249
7240 7250 if (nfs_zone() != VTOMI4(dvp)->mi_zone)
7241 7251 return (EPERM);
7242 7252 drp = VTOR4(dvp);
7243 7253 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp)))
7244 7254 return (EINTR);
7245 7255
7246 7256 e.error = nfs4lookup(dvp, nm, &vp, cr, 0);
7247 7257 if (e.error) {
7248 7258 nfs_rw_exit(&drp->r_rwlock);
7249 7259 return (e.error);
7250 7260 }
7251 7261
7252 7262 if (vp->v_type == VDIR) {
7253 7263 VN_RELE(vp);
7254 7264 nfs_rw_exit(&drp->r_rwlock);
7255 7265 return (EISDIR);
7256 7266 }
7257 7267
7258 7268 /*
7259 7269 * First just remove the entry from the name cache, as it
7260 7270 * is most likely the only entry for this vp.
7261 7271 */
7262 7272 dnlc_remove(dvp, nm);
7263 7273
7264 7274 rp = VTOR4(vp);
7265 7275
7266 7276 /*
7267 7277 * For regular file types, check to see if the file is open by looking
7268 7278 * at the open streams.
7269 7279 * For all other types, check the reference count on the vnode. Since
7270 7280 * they are not opened OTW they never have an open stream.
7271 7281 *
7272 7282 * If the file is open, rename it to .nfsXXXX.
7273 7283 */
7274 7284 if (vp->v_type != VREG) {
7275 7285 /*
7276 7286 * If the file has a v_count > 1 then there may be more than one
7277 7287 * entry in the name cache due multiple links or an open file,
7278 7288 * but we don't have the real reference count so flush all
7279 7289 * possible entries.
7280 7290 */
7281 7291 if (vp->v_count > 1)
7282 7292 dnlc_purge_vp(vp);
7283 7293
7284 7294 /*
7285 7295 * Now we have the real reference count.
7286 7296 */
7287 7297 isopen = vp->v_count > 1;
7288 7298 } else {
7289 7299 mutex_enter(&rp->r_os_lock);
7290 7300 isopen = list_head(&rp->r_open_streams) != NULL;
7291 7301 mutex_exit(&rp->r_os_lock);
7292 7302 }
7293 7303
7294 7304 mutex_enter(&rp->r_statelock);
7295 7305 if (isopen &&
7296 7306 (rp->r_unldvp == NULL || strcmp(nm, rp->r_unlname) == 0)) {
7297 7307 mutex_exit(&rp->r_statelock);
7298 7308 tmpname = newname();
7299 7309 e.error = nfs4rename(dvp, nm, dvp, tmpname, cr, ct);
7300 7310 if (e.error)
7301 7311 kmem_free(tmpname, MAXNAMELEN);
7302 7312 else {
7303 7313 mutex_enter(&rp->r_statelock);
7304 7314 if (rp->r_unldvp == NULL) {
7305 7315 VN_HOLD(dvp);
7306 7316 rp->r_unldvp = dvp;
7307 7317 if (rp->r_unlcred != NULL)
7308 7318 crfree(rp->r_unlcred);
7309 7319 crhold(cr);
7310 7320 rp->r_unlcred = cr;
7311 7321 rp->r_unlname = tmpname;
7312 7322 } else {
7313 7323 kmem_free(rp->r_unlname, MAXNAMELEN);
7314 7324 rp->r_unlname = tmpname;
7315 7325 }
7316 7326 mutex_exit(&rp->r_statelock);
7317 7327 }
7318 7328 VN_RELE(vp);
7319 7329 nfs_rw_exit(&drp->r_rwlock);
7320 7330 return (e.error);
7321 7331 }
7322 7332 /*
7323 7333 * Actually remove the file/dir
7324 7334 */
7325 7335 mutex_exit(&rp->r_statelock);
7326 7336
7327 7337 /*
7328 7338 * We need to flush any dirty pages which happen to
7329 7339 * be hanging around before removing the file.
7330 7340 * This shouldn't happen very often since in NFSv4
7331 7341 * we should be close to open consistent.
7332 7342 */
7333 7343 if (nfs4_has_pages(vp) &&
7334 7344 ((rp->r_flags & R4DIRTY) || rp->r_count > 0)) {
7335 7345 e.error = nfs4_putpage(vp, (u_offset_t)0, 0, 0, cr, ct);
7336 7346 if (e.error && (e.error == ENOSPC || e.error == EDQUOT)) {
7337 7347 mutex_enter(&rp->r_statelock);
7338 7348 if (!rp->r_error)
7339 7349 rp->r_error = e.error;
7340 7350 mutex_exit(&rp->r_statelock);
7341 7351 }
7342 7352 }
7343 7353
7344 7354 mi = VTOMI4(dvp);
7345 7355
7346 7356 (void) nfs4delegreturn(rp, NFS4_DR_REOPEN);
7347 7357 recov_state.rs_flags = 0;
7348 7358 recov_state.rs_num_retry_despite_err = 0;
7349 7359
7350 7360 recov_retry:
7351 7361 /*
7352 7362 * Remove ops: putfh dir; remove
7353 7363 */
7354 7364 args.ctag = TAG_REMOVE;
7355 7365 args.array_len = 3;
7356 7366 args.array = argop;
7357 7367
7358 7368 e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state);
7359 7369 if (e.error) {
7360 7370 nfs_rw_exit(&drp->r_rwlock);
7361 7371 VN_RELE(vp);
7362 7372 return (e.error);
7363 7373 }
7364 7374
7365 7375 /* putfh directory */
7366 7376 argop[0].argop = OP_CPUTFH;
7367 7377 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
7368 7378
7369 7379 /* remove */
7370 7380 argop[1].argop = OP_CREMOVE;
7371 7381 argop[1].nfs_argop4_u.opcremove.ctarget = nm;
7372 7382
7373 7383 /* getattr dir */
7374 7384 argop[2].argop = OP_GETATTR;
7375 7385 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
7376 7386 argop[2].nfs_argop4_u.opgetattr.mi = mi;
7377 7387
7378 7388 doqueue = 1;
7379 7389 dinfo.di_time_call = gethrtime();
7380 7390 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
7381 7391
7382 7392 PURGE_ATTRCACHE4(vp);
7383 7393
7384 7394 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
7385 7395 if (e.error)
7386 7396 PURGE_ATTRCACHE4(dvp);
7387 7397
7388 7398 if (needrecov) {
7389 7399 if (nfs4_start_recovery(&e, VTOMI4(dvp), dvp,
7390 7400 NULL, NULL, NULL, OP_REMOVE, NULL, NULL, NULL) == FALSE) {
7391 7401 if (!e.error)
7392 7402 (void) xdr_free(xdr_COMPOUND4res_clnt,
7393 7403 (caddr_t)&res);
7394 7404 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state,
7395 7405 needrecov);
7396 7406 goto recov_retry;
7397 7407 }
7398 7408 }
7399 7409
7400 7410 /*
7401 7411 * Matching nfs4_end_op() for start_op() above.
7402 7412 * There is a path in the code below which calls
7403 7413 * nfs4_purge_stale_fh(), which may generate otw calls through
7404 7414 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op()
7405 7415 * here to avoid nfs4_start_op() deadlock.
7406 7416 */
7407 7417 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov);
7408 7418
7409 7419 if (!e.error) {
7410 7420 resp = &res;
7411 7421
7412 7422 if (res.status) {
7413 7423 e.error = geterrno4(res.status);
7414 7424 PURGE_ATTRCACHE4(dvp);
7415 7425 nfs4_purge_stale_fh(e.error, dvp, cr);
7416 7426 } else {
7417 7427 resop = &res.array[1]; /* remove res */
7418 7428 rm_res = &resop->nfs_resop4_u.opremove;
7419 7429
7420 7430 dinfo.di_garp =
7421 7431 &res.array[2].nfs_resop4_u.opgetattr.ga_res;
7422 7432 dinfo.di_cred = cr;
7423 7433
7424 7434 /* Update directory attr, readdir and dnlc caches */
7425 7435 nfs4_update_dircaches(&rm_res->cinfo, dvp, NULL, NULL,
7426 7436 &dinfo);
7427 7437 }
7428 7438 }
7429 7439 nfs_rw_exit(&drp->r_rwlock);
7430 7440 if (resp)
7431 7441 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
7432 7442
7433 7443 if (e.error == 0) {
7434 7444 vnode_t *tvp;
7435 7445 rnode4_t *trp;
7436 7446 trp = VTOR4(vp);
7437 7447 tvp = vp;
7438 7448 if (IS_SHADOW(vp, trp))
7439 7449 tvp = RTOV4(trp);
7440 7450 vnevent_remove(tvp, dvp, nm, ct);
7441 7451 }
7442 7452 VN_RELE(vp);
7443 7453 return (e.error);
7444 7454 }
7445 7455
7446 7456 /*
7447 7457 * Link requires that the current fh be the target directory and the
7448 7458 * saved fh be the source fh. After the operation, the current fh is unchanged.
7449 7459 * Thus the compound op structure is:
7450 7460 * PUTFH(file), SAVEFH, PUTFH(targetdir), LINK, RESTOREFH,
7451 7461 * GETATTR(file)
7452 7462 */
7453 7463 /* ARGSUSED */
7454 7464 static int
7455 7465 nfs4_link(vnode_t *tdvp, vnode_t *svp, char *tnm, cred_t *cr,
7456 7466 caller_context_t *ct, int flags)
7457 7467 {
7458 7468 COMPOUND4args_clnt args;
7459 7469 COMPOUND4res_clnt res, *resp = NULL;
7460 7470 LINK4res *ln_res;
7461 7471 int argoplist_size = 7 * sizeof (nfs_argop4);
7462 7472 nfs_argop4 *argop;
7463 7473 nfs_resop4 *resop;
7464 7474 vnode_t *realvp, *nvp;
7465 7475 int doqueue;
7466 7476 mntinfo4_t *mi;
7467 7477 rnode4_t *tdrp;
7468 7478 bool_t needrecov = FALSE;
7469 7479 nfs4_recov_state_t recov_state;
7470 7480 hrtime_t t;
7471 7481 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
7472 7482 dirattr_info_t dinfo;
7473 7483
7474 7484 ASSERT(*tnm != '\0');
7475 7485 ASSERT(tdvp->v_type == VDIR);
7476 7486 ASSERT(nfs4_consistent_type(tdvp));
7477 7487 ASSERT(nfs4_consistent_type(svp));
7478 7488
7479 7489 if (nfs_zone() != VTOMI4(tdvp)->mi_zone)
7480 7490 return (EPERM);
7481 7491 if (VOP_REALVP(svp, &realvp, ct) == 0) {
7482 7492 svp = realvp;
7483 7493 ASSERT(nfs4_consistent_type(svp));
7484 7494 }
7485 7495
7486 7496 tdrp = VTOR4(tdvp);
7487 7497 mi = VTOMI4(svp);
7488 7498
7489 7499 if (!(mi->mi_flags & MI4_LINK)) {
7490 7500 return (EOPNOTSUPP);
7491 7501 }
7492 7502 recov_state.rs_flags = 0;
7493 7503 recov_state.rs_num_retry_despite_err = 0;
7494 7504
7495 7505 if (nfs_rw_enter_sig(&tdrp->r_rwlock, RW_WRITER, INTR4(tdvp)))
7496 7506 return (EINTR);
7497 7507
7498 7508 recov_retry:
7499 7509 argop = kmem_alloc(argoplist_size, KM_SLEEP);
7500 7510
7501 7511 args.ctag = TAG_LINK;
7502 7512
7503 7513 /*
7504 7514 * Link ops: putfh fl; savefh; putfh tdir; link; getattr(dir);
7505 7515 * restorefh; getattr(fl)
7506 7516 */
7507 7517 args.array_len = 7;
7508 7518 args.array = argop;
7509 7519
7510 7520 e.error = nfs4_start_op(VTOMI4(svp), svp, tdvp, &recov_state);
7511 7521 if (e.error) {
7512 7522 kmem_free(argop, argoplist_size);
7513 7523 nfs_rw_exit(&tdrp->r_rwlock);
7514 7524 return (e.error);
7515 7525 }
7516 7526
7517 7527 /* 0. putfh file */
7518 7528 argop[0].argop = OP_CPUTFH;
7519 7529 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(svp)->r_fh;
7520 7530
7521 7531 /* 1. save current fh to free up the space for the dir */
7522 7532 argop[1].argop = OP_SAVEFH;
7523 7533
7524 7534 /* 2. putfh targetdir */
7525 7535 argop[2].argop = OP_CPUTFH;
7526 7536 argop[2].nfs_argop4_u.opcputfh.sfh = tdrp->r_fh;
7527 7537
7528 7538 /* 3. link: current_fh is targetdir, saved_fh is source */
7529 7539 argop[3].argop = OP_CLINK;
7530 7540 argop[3].nfs_argop4_u.opclink.cnewname = tnm;
7531 7541
7532 7542 /* 4. Get attributes of dir */
7533 7543 argop[4].argop = OP_GETATTR;
7534 7544 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
7535 7545 argop[4].nfs_argop4_u.opgetattr.mi = mi;
7536 7546
7537 7547 /* 5. If link was successful, restore current vp to file */
7538 7548 argop[5].argop = OP_RESTOREFH;
7539 7549
7540 7550 /* 6. Get attributes of linked object */
7541 7551 argop[6].argop = OP_GETATTR;
7542 7552 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
7543 7553 argop[6].nfs_argop4_u.opgetattr.mi = mi;
7544 7554
7545 7555 dnlc_remove(tdvp, tnm);
7546 7556
7547 7557 doqueue = 1;
7548 7558 t = gethrtime();
7549 7559
7550 7560 rfs4call(VTOMI4(svp), &args, &res, cr, &doqueue, 0, &e);
7551 7561
7552 7562 needrecov = nfs4_needs_recovery(&e, FALSE, svp->v_vfsp);
7553 7563 if (e.error != 0 && !needrecov) {
7554 7564 PURGE_ATTRCACHE4(tdvp);
7555 7565 PURGE_ATTRCACHE4(svp);
7556 7566 nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state, needrecov);
7557 7567 goto out;
7558 7568 }
7559 7569
7560 7570 if (needrecov) {
7561 7571 bool_t abort;
7562 7572
7563 7573 abort = nfs4_start_recovery(&e, VTOMI4(svp), svp, tdvp,
7564 7574 NULL, NULL, OP_LINK, NULL, NULL, NULL);
7565 7575 if (abort == FALSE) {
7566 7576 nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state,
7567 7577 needrecov);
7568 7578 kmem_free(argop, argoplist_size);
7569 7579 if (!e.error)
7570 7580 (void) xdr_free(xdr_COMPOUND4res_clnt,
7571 7581 (caddr_t)&res);
7572 7582 goto recov_retry;
7573 7583 } else {
7574 7584 if (e.error != 0) {
7575 7585 PURGE_ATTRCACHE4(tdvp);
7576 7586 PURGE_ATTRCACHE4(svp);
7577 7587 nfs4_end_op(VTOMI4(svp), svp, tdvp,
7578 7588 &recov_state, needrecov);
7579 7589 goto out;
7580 7590 }
7581 7591 /* fall through for res.status case */
7582 7592 }
7583 7593 }
7584 7594
7585 7595 nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state, needrecov);
7586 7596
7587 7597 resp = &res;
7588 7598 if (res.status) {
7589 7599 /* If link succeeded, then don't return error */
7590 7600 e.error = geterrno4(res.status);
7591 7601 if (res.array_len <= 4) {
7592 7602 /*
7593 7603 * Either Putfh, Savefh, Putfh dir, or Link failed
7594 7604 */
7595 7605 PURGE_ATTRCACHE4(svp);
7596 7606 PURGE_ATTRCACHE4(tdvp);
7597 7607 if (e.error == EOPNOTSUPP) {
7598 7608 mutex_enter(&mi->mi_lock);
7599 7609 mi->mi_flags &= ~MI4_LINK;
7600 7610 mutex_exit(&mi->mi_lock);
7601 7611 }
7602 7612 /* Remap EISDIR to EPERM for non-root user for SVVS */
7603 7613 /* XXX-LP */
7604 7614 if (e.error == EISDIR && crgetuid(cr) != 0)
7605 7615 e.error = EPERM;
7606 7616 goto out;
7607 7617 }
7608 7618 }
7609 7619
7610 7620 /* either no error or one of the postop getattr failed */
7611 7621
7612 7622 /*
7613 7623 * XXX - if LINK succeeded, but no attrs were returned for link
7614 7624 * file, purge its cache.
7615 7625 *
7616 7626 * XXX Perform a simplified version of wcc checking. Instead of
7617 7627 * have another getattr to get pre-op, just purge cache if
7618 7628 * any of the ops prior to and including the getattr failed.
7619 7629 * If the getattr succeeded then update the attrcache accordingly.
7620 7630 */
7621 7631
7622 7632 /*
7623 7633 * update cache with link file postattrs.
7624 7634 * Note: at this point resop points to link res.
7625 7635 */
7626 7636 resop = &res.array[3]; /* link res */
7627 7637 ln_res = &resop->nfs_resop4_u.oplink;
7628 7638 if (res.status == NFS4_OK)
7629 7639 e.error = nfs4_update_attrcache(res.status,
7630 7640 &res.array[6].nfs_resop4_u.opgetattr.ga_res,
7631 7641 t, svp, cr);
7632 7642
7633 7643 /*
7634 7644 * Call makenfs4node to create the new shadow vp for tnm.
7635 7645 * We pass NULL attrs because we just cached attrs for
7636 7646 * the src object. All we're trying to accomplish is to
7637 7647 * to create the new shadow vnode.
7638 7648 */
7639 7649 nvp = makenfs4node(VTOR4(svp)->r_fh, NULL, tdvp->v_vfsp, t, cr,
7640 7650 tdvp, fn_get(VTOSV(tdvp)->sv_name, tnm, VTOR4(svp)->r_fh));
7641 7651
7642 7652 /* Update target cache attribute, readdir and dnlc caches */
7643 7653 dinfo.di_garp = &res.array[4].nfs_resop4_u.opgetattr.ga_res;
7644 7654 dinfo.di_time_call = t;
7645 7655 dinfo.di_cred = cr;
7646 7656
7647 7657 nfs4_update_dircaches(&ln_res->cinfo, tdvp, nvp, tnm, &dinfo);
7648 7658 ASSERT(nfs4_consistent_type(tdvp));
7649 7659 ASSERT(nfs4_consistent_type(svp));
7650 7660 ASSERT(nfs4_consistent_type(nvp));
7651 7661 VN_RELE(nvp);
7652 7662
7653 7663 if (!e.error) {
7654 7664 vnode_t *tvp;
7655 7665 rnode4_t *trp;
7656 7666 /*
7657 7667 * Notify the source file of this link operation.
7658 7668 */
7659 7669 trp = VTOR4(svp);
7660 7670 tvp = svp;
7661 7671 if (IS_SHADOW(svp, trp))
7662 7672 tvp = RTOV4(trp);
7663 7673 vnevent_link(tvp, ct);
7664 7674 }
7665 7675 out:
7666 7676 kmem_free(argop, argoplist_size);
7667 7677 if (resp)
7668 7678 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
7669 7679
7670 7680 nfs_rw_exit(&tdrp->r_rwlock);
7671 7681
7672 7682 return (e.error);
7673 7683 }
7674 7684
7675 7685 /* ARGSUSED */
7676 7686 static int
7677 7687 nfs4_rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr,
7678 7688 caller_context_t *ct, int flags)
7679 7689 {
7680 7690 vnode_t *realvp;
7681 7691
7682 7692 if (nfs_zone() != VTOMI4(odvp)->mi_zone)
7683 7693 return (EPERM);
7684 7694 if (VOP_REALVP(ndvp, &realvp, ct) == 0)
7685 7695 ndvp = realvp;
7686 7696
7687 7697 return (nfs4rename(odvp, onm, ndvp, nnm, cr, ct));
7688 7698 }
7689 7699
7690 7700 /*
7691 7701 * nfs4rename does the real work of renaming in NFS Version 4.
7692 7702 *
7693 7703 * A file handle is considered volatile for renaming purposes if either
7694 7704 * of the volatile bits are turned on. However, the compound may differ
7695 7705 * based on the likelihood of the filehandle to change during rename.
7696 7706 */
7697 7707 static int
7698 7708 nfs4rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr,
7699 7709 caller_context_t *ct)
7700 7710 {
7701 7711 int error;
7702 7712 mntinfo4_t *mi;
7703 7713 vnode_t *nvp = NULL;
7704 7714 vnode_t *ovp = NULL;
7705 7715 char *tmpname = NULL;
7706 7716 rnode4_t *rp;
7707 7717 rnode4_t *odrp;
7708 7718 rnode4_t *ndrp;
7709 7719 int did_link = 0;
7710 7720 int do_link = 1;
7711 7721 nfsstat4 stat = NFS4_OK;
7712 7722
7713 7723 ASSERT(nfs_zone() == VTOMI4(odvp)->mi_zone);
7714 7724 ASSERT(nfs4_consistent_type(odvp));
7715 7725 ASSERT(nfs4_consistent_type(ndvp));
7716 7726
7717 7727 if (onm[0] == '.' && (onm[1] == '\0' ||
7718 7728 (onm[1] == '.' && onm[2] == '\0')))
7719 7729 return (EINVAL);
7720 7730
7721 7731 if (nnm[0] == '.' && (nnm[1] == '\0' ||
7722 7732 (nnm[1] == '.' && nnm[2] == '\0')))
7723 7733 return (EINVAL);
7724 7734
7725 7735 odrp = VTOR4(odvp);
7726 7736 ndrp = VTOR4(ndvp);
7727 7737 if ((intptr_t)odrp < (intptr_t)ndrp) {
7728 7738 if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR4(odvp)))
7729 7739 return (EINTR);
7730 7740 if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR4(ndvp))) {
7731 7741 nfs_rw_exit(&odrp->r_rwlock);
7732 7742 return (EINTR);
7733 7743 }
7734 7744 } else {
7735 7745 if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR4(ndvp)))
7736 7746 return (EINTR);
7737 7747 if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR4(odvp))) {
7738 7748 nfs_rw_exit(&ndrp->r_rwlock);
7739 7749 return (EINTR);
7740 7750 }
7741 7751 }
7742 7752
7743 7753 /*
7744 7754 * Lookup the target file. If it exists, it needs to be
7745 7755 * checked to see whether it is a mount point and whether
7746 7756 * it is active (open).
7747 7757 */
7748 7758 error = nfs4lookup(ndvp, nnm, &nvp, cr, 0);
7749 7759 if (!error) {
7750 7760 int isactive;
7751 7761
7752 7762 ASSERT(nfs4_consistent_type(nvp));
7753 7763 /*
7754 7764 * If this file has been mounted on, then just
7755 7765 * return busy because renaming to it would remove
7756 7766 * the mounted file system from the name space.
7757 7767 */
7758 7768 if (vn_ismntpt(nvp)) {
7759 7769 VN_RELE(nvp);
7760 7770 nfs_rw_exit(&odrp->r_rwlock);
7761 7771 nfs_rw_exit(&ndrp->r_rwlock);
7762 7772 return (EBUSY);
7763 7773 }
7764 7774
7765 7775 /*
7766 7776 * First just remove the entry from the name cache, as it
7767 7777 * is most likely the only entry for this vp.
7768 7778 */
7769 7779 dnlc_remove(ndvp, nnm);
7770 7780
7771 7781 rp = VTOR4(nvp);
7772 7782
7773 7783 if (nvp->v_type != VREG) {
7774 7784 /*
7775 7785 * Purge the name cache of all references to this vnode
7776 7786 * so that we can check the reference count to infer
7777 7787 * whether it is active or not.
7778 7788 */
7779 7789 if (nvp->v_count > 1)
7780 7790 dnlc_purge_vp(nvp);
7781 7791
7782 7792 isactive = nvp->v_count > 1;
7783 7793 } else {
7784 7794 mutex_enter(&rp->r_os_lock);
7785 7795 isactive = list_head(&rp->r_open_streams) != NULL;
7786 7796 mutex_exit(&rp->r_os_lock);
7787 7797 }
7788 7798
7789 7799 /*
7790 7800 * If the vnode is active and is not a directory,
7791 7801 * arrange to rename it to a
7792 7802 * temporary file so that it will continue to be
7793 7803 * accessible. This implements the "unlink-open-file"
7794 7804 * semantics for the target of a rename operation.
7795 7805 * Before doing this though, make sure that the
7796 7806 * source and target files are not already the same.
7797 7807 */
7798 7808 if (isactive && nvp->v_type != VDIR) {
7799 7809 /*
7800 7810 * Lookup the source name.
7801 7811 */
7802 7812 error = nfs4lookup(odvp, onm, &ovp, cr, 0);
7803 7813
7804 7814 /*
7805 7815 * The source name *should* already exist.
7806 7816 */
7807 7817 if (error) {
7808 7818 VN_RELE(nvp);
7809 7819 nfs_rw_exit(&odrp->r_rwlock);
7810 7820 nfs_rw_exit(&ndrp->r_rwlock);
7811 7821 return (error);
7812 7822 }
7813 7823
7814 7824 ASSERT(nfs4_consistent_type(ovp));
7815 7825
7816 7826 /*
7817 7827 * Compare the two vnodes. If they are the same,
7818 7828 * just release all held vnodes and return success.
7819 7829 */
7820 7830 if (VN_CMP(ovp, nvp)) {
7821 7831 VN_RELE(ovp);
7822 7832 VN_RELE(nvp);
7823 7833 nfs_rw_exit(&odrp->r_rwlock);
7824 7834 nfs_rw_exit(&ndrp->r_rwlock);
7825 7835 return (0);
7826 7836 }
7827 7837
7828 7838 /*
7829 7839 * Can't mix and match directories and non-
7830 7840 * directories in rename operations. We already
7831 7841 * know that the target is not a directory. If
7832 7842 * the source is a directory, return an error.
7833 7843 */
7834 7844 if (ovp->v_type == VDIR) {
7835 7845 VN_RELE(ovp);
7836 7846 VN_RELE(nvp);
7837 7847 nfs_rw_exit(&odrp->r_rwlock);
7838 7848 nfs_rw_exit(&ndrp->r_rwlock);
7839 7849 return (ENOTDIR);
7840 7850 }
7841 7851 link_call:
7842 7852 /*
7843 7853 * The target file exists, is not the same as
7844 7854 * the source file, and is active. We first
7845 7855 * try to Link it to a temporary filename to
7846 7856 * avoid having the server removing the file
7847 7857 * completely (which could cause data loss to
7848 7858 * the user's POV in the event the Rename fails
7849 7859 * -- see bug 1165874).
7850 7860 */
7851 7861 /*
7852 7862 * The do_link and did_link booleans are
7853 7863 * introduced in the event we get NFS4ERR_FILE_OPEN
7854 7864 * returned for the Rename. Some servers can
7855 7865 * not Rename over an Open file, so they return
7856 7866 * this error. The client needs to Remove the
7857 7867 * newly created Link and do two Renames, just
7858 7868 * as if the server didn't support LINK.
7859 7869 */
7860 7870 tmpname = newname();
7861 7871 error = 0;
7862 7872
7863 7873 if (do_link) {
7864 7874 error = nfs4_link(ndvp, nvp, tmpname, cr,
7865 7875 NULL, 0);
7866 7876 }
7867 7877 if (error == EOPNOTSUPP || !do_link) {
7868 7878 error = nfs4_rename(ndvp, nnm, ndvp, tmpname,
7869 7879 cr, NULL, 0);
7870 7880 did_link = 0;
7871 7881 } else {
7872 7882 did_link = 1;
7873 7883 }
7874 7884 if (error) {
7875 7885 kmem_free(tmpname, MAXNAMELEN);
7876 7886 VN_RELE(ovp);
7877 7887 VN_RELE(nvp);
7878 7888 nfs_rw_exit(&odrp->r_rwlock);
7879 7889 nfs_rw_exit(&ndrp->r_rwlock);
7880 7890 return (error);
7881 7891 }
7882 7892
7883 7893 mutex_enter(&rp->r_statelock);
7884 7894 if (rp->r_unldvp == NULL) {
7885 7895 VN_HOLD(ndvp);
7886 7896 rp->r_unldvp = ndvp;
7887 7897 if (rp->r_unlcred != NULL)
7888 7898 crfree(rp->r_unlcred);
7889 7899 crhold(cr);
7890 7900 rp->r_unlcred = cr;
7891 7901 rp->r_unlname = tmpname;
7892 7902 } else {
7893 7903 if (rp->r_unlname)
7894 7904 kmem_free(rp->r_unlname, MAXNAMELEN);
7895 7905 rp->r_unlname = tmpname;
7896 7906 }
7897 7907 mutex_exit(&rp->r_statelock);
7898 7908 }
7899 7909
7900 7910 (void) nfs4delegreturn(VTOR4(nvp), NFS4_DR_PUSH|NFS4_DR_REOPEN);
7901 7911
7902 7912 ASSERT(nfs4_consistent_type(nvp));
7903 7913 }
7904 7914
7905 7915 if (ovp == NULL) {
7906 7916 /*
7907 7917 * When renaming directories to be a subdirectory of a
7908 7918 * different parent, the dnlc entry for ".." will no
7909 7919 * longer be valid, so it must be removed.
7910 7920 *
7911 7921 * We do a lookup here to determine whether we are renaming
7912 7922 * a directory and we need to check if we are renaming
7913 7923 * an unlinked file. This might have already been done
7914 7924 * in previous code, so we check ovp == NULL to avoid
7915 7925 * doing it twice.
7916 7926 */
7917 7927 error = nfs4lookup(odvp, onm, &ovp, cr, 0);
7918 7928 /*
7919 7929 * The source name *should* already exist.
7920 7930 */
7921 7931 if (error) {
7922 7932 nfs_rw_exit(&odrp->r_rwlock);
7923 7933 nfs_rw_exit(&ndrp->r_rwlock);
7924 7934 if (nvp) {
7925 7935 VN_RELE(nvp);
7926 7936 }
7927 7937 return (error);
7928 7938 }
7929 7939 ASSERT(ovp != NULL);
7930 7940 ASSERT(nfs4_consistent_type(ovp));
7931 7941 }
7932 7942
7933 7943 /*
7934 7944 * Is the object being renamed a dir, and if so, is
7935 7945 * it being renamed to a child of itself? The underlying
7936 7946 * fs should ultimately return EINVAL for this case;
7937 7947 * however, buggy beta non-Solaris NFSv4 servers at
7938 7948 * interop testing events have allowed this behavior,
7939 7949 * and it caused our client to panic due to a recursive
7940 7950 * mutex_enter in fn_move.
7941 7951 *
7942 7952 * The tedious locking in fn_move could be changed to
7943 7953 * deal with this case, and the client could avoid the
7944 7954 * panic; however, the client would just confuse itself
7945 7955 * later and misbehave. A better way to handle the broken
7946 7956 * server is to detect this condition and return EINVAL
7947 7957 * without ever sending the the bogus rename to the server.
7948 7958 * We know the rename is invalid -- just fail it now.
7949 7959 */
7950 7960 if (ovp->v_type == VDIR && VN_CMP(ndvp, ovp)) {
7951 7961 VN_RELE(ovp);
7952 7962 nfs_rw_exit(&odrp->r_rwlock);
7953 7963 nfs_rw_exit(&ndrp->r_rwlock);
7954 7964 if (nvp) {
7955 7965 VN_RELE(nvp);
7956 7966 }
7957 7967 return (EINVAL);
7958 7968 }
7959 7969
7960 7970 (void) nfs4delegreturn(VTOR4(ovp), NFS4_DR_PUSH|NFS4_DR_REOPEN);
7961 7971
7962 7972 /*
7963 7973 * If FH4_VOL_RENAME or FH4_VOLATILE_ANY bits are set, it is
7964 7974 * possible for the filehandle to change due to the rename.
7965 7975 * If neither of these bits is set, but FH4_VOL_MIGRATION is set,
7966 7976 * the fh will not change because of the rename, but we still need
7967 7977 * to update its rnode entry with the new name for
7968 7978 * an eventual fh change due to migration. The FH4_NOEXPIRE_ON_OPEN
7969 7979 * has no effect on these for now, but for future improvements,
7970 7980 * we might want to use it too to simplify handling of files
7971 7981 * that are open with that flag on. (XXX)
7972 7982 */
7973 7983 mi = VTOMI4(odvp);
7974 7984 if (NFS4_VOLATILE_FH(mi))
7975 7985 error = nfs4rename_volatile_fh(odvp, onm, ovp, ndvp, nnm, cr,
7976 7986 &stat);
7977 7987 else
7978 7988 error = nfs4rename_persistent_fh(odvp, onm, ovp, ndvp, nnm, cr,
7979 7989 &stat);
7980 7990
7981 7991 ASSERT(nfs4_consistent_type(odvp));
7982 7992 ASSERT(nfs4_consistent_type(ndvp));
7983 7993 ASSERT(nfs4_consistent_type(ovp));
7984 7994
7985 7995 if (stat == NFS4ERR_FILE_OPEN && did_link) {
7986 7996 do_link = 0;
7987 7997 /*
7988 7998 * Before the 'link_call' code, we did a nfs4_lookup
7989 7999 * that puts a VN_HOLD on nvp. After the nfs4_link
7990 8000 * call we call VN_RELE to match that hold. We need
7991 8001 * to place an additional VN_HOLD here since we will
7992 8002 * be hitting that VN_RELE again.
7993 8003 */
7994 8004 VN_HOLD(nvp);
7995 8005
7996 8006 (void) nfs4_remove(ndvp, tmpname, cr, NULL, 0);
7997 8007
7998 8008 /* Undo the unlinked file naming stuff we just did */
7999 8009 mutex_enter(&rp->r_statelock);
8000 8010 if (rp->r_unldvp) {
8001 8011 VN_RELE(ndvp);
8002 8012 rp->r_unldvp = NULL;
8003 8013 if (rp->r_unlcred != NULL)
8004 8014 crfree(rp->r_unlcred);
8005 8015 rp->r_unlcred = NULL;
8006 8016 /* rp->r_unlanme points to tmpname */
8007 8017 if (rp->r_unlname)
8008 8018 kmem_free(rp->r_unlname, MAXNAMELEN);
8009 8019 rp->r_unlname = NULL;
8010 8020 }
8011 8021 mutex_exit(&rp->r_statelock);
8012 8022
8013 8023 if (nvp) {
8014 8024 VN_RELE(nvp);
8015 8025 }
8016 8026 goto link_call;
8017 8027 }
8018 8028
8019 8029 if (error) {
8020 8030 VN_RELE(ovp);
8021 8031 nfs_rw_exit(&odrp->r_rwlock);
8022 8032 nfs_rw_exit(&ndrp->r_rwlock);
8023 8033 if (nvp) {
8024 8034 VN_RELE(nvp);
8025 8035 }
8026 8036 return (error);
8027 8037 }
8028 8038
8029 8039 /*
8030 8040 * when renaming directories to be a subdirectory of a
8031 8041 * different parent, the dnlc entry for ".." will no
8032 8042 * longer be valid, so it must be removed
8033 8043 */
8034 8044 rp = VTOR4(ovp);
8035 8045 if (ndvp != odvp) {
8036 8046 if (ovp->v_type == VDIR) {
8037 8047 dnlc_remove(ovp, "..");
8038 8048 if (rp->r_dir != NULL)
8039 8049 nfs4_purge_rddir_cache(ovp);
8040 8050 }
8041 8051 }
8042 8052
8043 8053 /*
8044 8054 * If we are renaming the unlinked file, update the
8045 8055 * r_unldvp and r_unlname as needed.
8046 8056 */
8047 8057 mutex_enter(&rp->r_statelock);
8048 8058 if (rp->r_unldvp != NULL) {
8049 8059 if (strcmp(rp->r_unlname, onm) == 0) {
8050 8060 (void) strncpy(rp->r_unlname, nnm, MAXNAMELEN);
8051 8061 rp->r_unlname[MAXNAMELEN - 1] = '\0';
8052 8062 if (ndvp != rp->r_unldvp) {
8053 8063 VN_RELE(rp->r_unldvp);
8054 8064 rp->r_unldvp = ndvp;
8055 8065 VN_HOLD(ndvp);
8056 8066 }
8057 8067 }
8058 8068 }
8059 8069 mutex_exit(&rp->r_statelock);
8060 8070
8061 8071 /*
8062 8072 * Notify the rename vnevents to source vnode, and to the target
8063 8073 * vnode if it already existed.
8064 8074 */
8065 8075 if (error == 0) {
8066 8076 vnode_t *tvp;
8067 8077 rnode4_t *trp;
8068 8078 /*
8069 8079 * Notify the vnode. Each links is represented by
8070 8080 * a different vnode, in nfsv4.
8071 8081 */
8072 8082 if (nvp) {
8073 8083 trp = VTOR4(nvp);
8074 8084 tvp = nvp;
8075 8085 if (IS_SHADOW(nvp, trp))
8076 8086 tvp = RTOV4(trp);
8077 8087 vnevent_rename_dest(tvp, ndvp, nnm, ct);
8078 8088 }
8079 8089
8080 8090 /*
8081 8091 * if the source and destination directory are not the
8082 8092 * same notify the destination directory.
8083 8093 */
8084 8094 if (VTOR4(odvp) != VTOR4(ndvp)) {
8085 8095 trp = VTOR4(ndvp);
8086 8096 tvp = ndvp;
8087 8097 if (IS_SHADOW(ndvp, trp))
8088 8098 tvp = RTOV4(trp);
8089 8099 vnevent_rename_dest_dir(tvp, ct);
8090 8100 }
8091 8101
8092 8102 trp = VTOR4(ovp);
8093 8103 tvp = ovp;
8094 8104 if (IS_SHADOW(ovp, trp))
8095 8105 tvp = RTOV4(trp);
8096 8106 vnevent_rename_src(tvp, odvp, onm, ct);
8097 8107 }
8098 8108
8099 8109 if (nvp) {
8100 8110 VN_RELE(nvp);
8101 8111 }
8102 8112 VN_RELE(ovp);
8103 8113
8104 8114 nfs_rw_exit(&odrp->r_rwlock);
8105 8115 nfs_rw_exit(&ndrp->r_rwlock);
8106 8116
8107 8117 return (error);
8108 8118 }
8109 8119
8110 8120 /*
8111 8121 * When the parent directory has changed, sv_dfh must be updated
8112 8122 */
8113 8123 static void
8114 8124 update_parentdir_sfh(vnode_t *vp, vnode_t *ndvp)
8115 8125 {
8116 8126 svnode_t *sv = VTOSV(vp);
8117 8127 nfs4_sharedfh_t *old_dfh = sv->sv_dfh;
8118 8128 nfs4_sharedfh_t *new_dfh = VTOR4(ndvp)->r_fh;
8119 8129
8120 8130 sfh4_hold(new_dfh);
8121 8131 sv->sv_dfh = new_dfh;
8122 8132 sfh4_rele(&old_dfh);
8123 8133 }
8124 8134
8125 8135 /*
8126 8136 * nfs4rename_persistent does the otw portion of renaming in NFS Version 4,
8127 8137 * when it is known that the filehandle is persistent through rename.
8128 8138 *
8129 8139 * Rename requires that the current fh be the target directory and the
8130 8140 * saved fh be the source directory. After the operation, the current fh
8131 8141 * is unchanged.
8132 8142 * The compound op structure for persistent fh rename is:
8133 8143 * PUTFH(sourcdir), SAVEFH, PUTFH(targetdir), RENAME
8134 8144 * Rather than bother with the directory postop args, we'll simply
8135 8145 * update that a change occurred in the cache, so no post-op getattrs.
8136 8146 */
8137 8147 static int
8138 8148 nfs4rename_persistent_fh(vnode_t *odvp, char *onm, vnode_t *renvp,
8139 8149 vnode_t *ndvp, char *nnm, cred_t *cr, nfsstat4 *statp)
8140 8150 {
8141 8151 COMPOUND4args_clnt args;
8142 8152 COMPOUND4res_clnt res, *resp = NULL;
8143 8153 nfs_argop4 *argop;
8144 8154 nfs_resop4 *resop;
8145 8155 int doqueue, argoplist_size;
8146 8156 mntinfo4_t *mi;
8147 8157 rnode4_t *odrp = VTOR4(odvp);
8148 8158 rnode4_t *ndrp = VTOR4(ndvp);
8149 8159 RENAME4res *rn_res;
8150 8160 bool_t needrecov;
8151 8161 nfs4_recov_state_t recov_state;
8152 8162 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
8153 8163 dirattr_info_t dinfo, *dinfop;
8154 8164
8155 8165 ASSERT(nfs_zone() == VTOMI4(odvp)->mi_zone);
8156 8166
8157 8167 recov_state.rs_flags = 0;
8158 8168 recov_state.rs_num_retry_despite_err = 0;
8159 8169
8160 8170 /*
8161 8171 * Rename ops: putfh sdir; savefh; putfh tdir; rename; getattr tdir
8162 8172 *
8163 8173 * If source/target are different dirs, then append putfh(src); getattr
8164 8174 */
8165 8175 args.array_len = (odvp == ndvp) ? 5 : 7;
8166 8176 argoplist_size = args.array_len * sizeof (nfs_argop4);
8167 8177 args.array = argop = kmem_alloc(argoplist_size, KM_SLEEP);
8168 8178
8169 8179 recov_retry:
8170 8180 *statp = NFS4_OK;
8171 8181
8172 8182 /* No need to Lookup the file, persistent fh */
8173 8183 args.ctag = TAG_RENAME;
8174 8184
8175 8185 mi = VTOMI4(odvp);
8176 8186 e.error = nfs4_start_op(mi, odvp, ndvp, &recov_state);
8177 8187 if (e.error) {
8178 8188 kmem_free(argop, argoplist_size);
8179 8189 return (e.error);
8180 8190 }
8181 8191
8182 8192 /* 0: putfh source directory */
8183 8193 argop[0].argop = OP_CPUTFH;
8184 8194 argop[0].nfs_argop4_u.opcputfh.sfh = odrp->r_fh;
8185 8195
8186 8196 /* 1: Save source fh to free up current for target */
8187 8197 argop[1].argop = OP_SAVEFH;
8188 8198
8189 8199 /* 2: putfh targetdir */
8190 8200 argop[2].argop = OP_CPUTFH;
8191 8201 argop[2].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh;
8192 8202
8193 8203 /* 3: current_fh is targetdir, saved_fh is sourcedir */
8194 8204 argop[3].argop = OP_CRENAME;
8195 8205 argop[3].nfs_argop4_u.opcrename.coldname = onm;
8196 8206 argop[3].nfs_argop4_u.opcrename.cnewname = nnm;
8197 8207
8198 8208 /* 4: getattr (targetdir) */
8199 8209 argop[4].argop = OP_GETATTR;
8200 8210 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
8201 8211 argop[4].nfs_argop4_u.opgetattr.mi = mi;
8202 8212
8203 8213 if (ndvp != odvp) {
8204 8214
8205 8215 /* 5: putfh (sourcedir) */
8206 8216 argop[5].argop = OP_CPUTFH;
8207 8217 argop[5].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh;
8208 8218
8209 8219 /* 6: getattr (sourcedir) */
8210 8220 argop[6].argop = OP_GETATTR;
8211 8221 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
8212 8222 argop[6].nfs_argop4_u.opgetattr.mi = mi;
8213 8223 }
8214 8224
8215 8225 dnlc_remove(odvp, onm);
8216 8226 dnlc_remove(ndvp, nnm);
8217 8227
8218 8228 doqueue = 1;
8219 8229 dinfo.di_time_call = gethrtime();
8220 8230 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
8221 8231
8222 8232 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
8223 8233 if (e.error) {
8224 8234 PURGE_ATTRCACHE4(odvp);
8225 8235 PURGE_ATTRCACHE4(ndvp);
8226 8236 } else {
8227 8237 *statp = res.status;
8228 8238 }
8229 8239
8230 8240 if (needrecov) {
8231 8241 if (nfs4_start_recovery(&e, mi, odvp, ndvp, NULL, NULL,
8232 8242 OP_RENAME, NULL, NULL, NULL) == FALSE) {
8233 8243 nfs4_end_op(mi, odvp, ndvp, &recov_state, needrecov);
8234 8244 if (!e.error)
8235 8245 (void) xdr_free(xdr_COMPOUND4res_clnt,
8236 8246 (caddr_t)&res);
8237 8247 goto recov_retry;
8238 8248 }
8239 8249 }
8240 8250
8241 8251 if (!e.error) {
8242 8252 resp = &res;
8243 8253 /*
8244 8254 * as long as OP_RENAME
8245 8255 */
8246 8256 if (res.status != NFS4_OK && res.array_len <= 4) {
8247 8257 e.error = geterrno4(res.status);
8248 8258 PURGE_ATTRCACHE4(odvp);
8249 8259 PURGE_ATTRCACHE4(ndvp);
8250 8260 /*
8251 8261 * System V defines rename to return EEXIST, not
8252 8262 * ENOTEMPTY if the target directory is not empty.
8253 8263 * Over the wire, the error is NFSERR_ENOTEMPTY
8254 8264 * which geterrno4 maps to ENOTEMPTY.
8255 8265 */
8256 8266 if (e.error == ENOTEMPTY)
8257 8267 e.error = EEXIST;
8258 8268 } else {
8259 8269
8260 8270 resop = &res.array[3]; /* rename res */
8261 8271 rn_res = &resop->nfs_resop4_u.oprename;
8262 8272
8263 8273 if (res.status == NFS4_OK) {
8264 8274 /*
8265 8275 * Update target attribute, readdir and dnlc
8266 8276 * caches.
8267 8277 */
8268 8278 dinfo.di_garp =
8269 8279 &res.array[4].nfs_resop4_u.opgetattr.ga_res;
8270 8280 dinfo.di_cred = cr;
8271 8281 dinfop = &dinfo;
8272 8282 } else
8273 8283 dinfop = NULL;
8274 8284
8275 8285 nfs4_update_dircaches(&rn_res->target_cinfo,
8276 8286 ndvp, NULL, NULL, dinfop);
8277 8287
8278 8288 /*
8279 8289 * Update source attribute, readdir and dnlc caches
8280 8290 *
8281 8291 */
8282 8292 if (ndvp != odvp) {
8283 8293 update_parentdir_sfh(renvp, ndvp);
8284 8294
8285 8295 if (dinfop)
8286 8296 dinfo.di_garp =
8287 8297 &(res.array[6].nfs_resop4_u.
8288 8298 opgetattr.ga_res);
8289 8299
8290 8300 nfs4_update_dircaches(&rn_res->source_cinfo,
8291 8301 odvp, NULL, NULL, dinfop);
8292 8302 }
8293 8303
8294 8304 fn_move(VTOSV(renvp)->sv_name, VTOSV(ndvp)->sv_name,
8295 8305 nnm);
8296 8306 }
8297 8307 }
8298 8308
8299 8309 if (resp)
8300 8310 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
8301 8311 nfs4_end_op(mi, odvp, ndvp, &recov_state, needrecov);
8302 8312 kmem_free(argop, argoplist_size);
8303 8313
8304 8314 return (e.error);
8305 8315 }
8306 8316
8307 8317 /*
8308 8318 * nfs4rename_volatile_fh does the otw part of renaming in NFS Version 4, when
8309 8319 * it is possible for the filehandle to change due to the rename.
8310 8320 *
8311 8321 * The compound req in this case includes a post-rename lookup and getattr
8312 8322 * to ensure that we have the correct fh and attributes for the object.
8313 8323 *
8314 8324 * Rename requires that the current fh be the target directory and the
8315 8325 * saved fh be the source directory. After the operation, the current fh
8316 8326 * is unchanged.
8317 8327 *
8318 8328 * We need the new filehandle (hence a LOOKUP and GETFH) so that we can
8319 8329 * update the filehandle for the renamed object. We also get the old
8320 8330 * filehandle for historical reasons; this should be taken out sometime.
8321 8331 * This results in a rather cumbersome compound...
8322 8332 *
8323 8333 * PUTFH(sourcdir), SAVEFH, LOOKUP(src), GETFH(old),
8324 8334 * PUTFH(targetdir), RENAME, LOOKUP(trgt), GETFH(new), GETATTR
8325 8335 *
8326 8336 */
8327 8337 static int
8328 8338 nfs4rename_volatile_fh(vnode_t *odvp, char *onm, vnode_t *ovp,
8329 8339 vnode_t *ndvp, char *nnm, cred_t *cr, nfsstat4 *statp)
8330 8340 {
8331 8341 COMPOUND4args_clnt args;
8332 8342 COMPOUND4res_clnt res, *resp = NULL;
8333 8343 int argoplist_size;
8334 8344 nfs_argop4 *argop;
8335 8345 nfs_resop4 *resop;
8336 8346 int doqueue;
8337 8347 mntinfo4_t *mi;
8338 8348 rnode4_t *odrp = VTOR4(odvp); /* old directory */
8339 8349 rnode4_t *ndrp = VTOR4(ndvp); /* new directory */
8340 8350 rnode4_t *orp = VTOR4(ovp); /* object being renamed */
8341 8351 RENAME4res *rn_res;
8342 8352 GETFH4res *ngf_res;
8343 8353 bool_t needrecov;
8344 8354 nfs4_recov_state_t recov_state;
8345 8355 hrtime_t t;
8346 8356 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
8347 8357 dirattr_info_t dinfo, *dinfop = &dinfo;
8348 8358
8349 8359 ASSERT(nfs_zone() == VTOMI4(odvp)->mi_zone);
8350 8360
8351 8361 recov_state.rs_flags = 0;
8352 8362 recov_state.rs_num_retry_despite_err = 0;
8353 8363
8354 8364 recov_retry:
8355 8365 *statp = NFS4_OK;
8356 8366
8357 8367 /*
8358 8368 * There is a window between the RPC and updating the path and
8359 8369 * filehandle stored in the rnode. Lock out the FHEXPIRED recovery
8360 8370 * code, so that it doesn't try to use the old path during that
8361 8371 * window.
8362 8372 */
8363 8373 mutex_enter(&orp->r_statelock);
8364 8374 while (orp->r_flags & R4RECEXPFH) {
8365 8375 klwp_t *lwp = ttolwp(curthread);
8366 8376
8367 8377 if (lwp != NULL)
8368 8378 lwp->lwp_nostop++;
8369 8379 if (cv_wait_sig(&orp->r_cv, &orp->r_statelock) == 0) {
8370 8380 mutex_exit(&orp->r_statelock);
8371 8381 if (lwp != NULL)
8372 8382 lwp->lwp_nostop--;
8373 8383 return (EINTR);
8374 8384 }
8375 8385 if (lwp != NULL)
8376 8386 lwp->lwp_nostop--;
8377 8387 }
8378 8388 orp->r_flags |= R4RECEXPFH;
8379 8389 mutex_exit(&orp->r_statelock);
8380 8390
8381 8391 mi = VTOMI4(odvp);
8382 8392
8383 8393 args.ctag = TAG_RENAME_VFH;
8384 8394 args.array_len = (odvp == ndvp) ? 10 : 12;
8385 8395 argoplist_size = args.array_len * sizeof (nfs_argop4);
8386 8396 argop = kmem_alloc(argoplist_size, KM_SLEEP);
8387 8397
8388 8398 /*
8389 8399 * Rename ops:
8390 8400 * PUTFH(sourcdir), SAVEFH, LOOKUP(src), GETFH(old),
8391 8401 * PUTFH(targetdir), RENAME, GETATTR(targetdir)
8392 8402 * LOOKUP(trgt), GETFH(new), GETATTR,
8393 8403 *
8394 8404 * if (odvp != ndvp)
8395 8405 * add putfh(sourcedir), getattr(sourcedir) }
8396 8406 */
8397 8407 args.array = argop;
8398 8408
8399 8409 e.error = nfs4_start_fop(mi, odvp, ndvp, OH_VFH_RENAME,
8400 8410 &recov_state, NULL);
8401 8411 if (e.error) {
8402 8412 kmem_free(argop, argoplist_size);
8403 8413 mutex_enter(&orp->r_statelock);
8404 8414 orp->r_flags &= ~R4RECEXPFH;
8405 8415 cv_broadcast(&orp->r_cv);
8406 8416 mutex_exit(&orp->r_statelock);
8407 8417 return (e.error);
8408 8418 }
8409 8419
8410 8420 /* 0: putfh source directory */
8411 8421 argop[0].argop = OP_CPUTFH;
8412 8422 argop[0].nfs_argop4_u.opcputfh.sfh = odrp->r_fh;
8413 8423
8414 8424 /* 1: Save source fh to free up current for target */
8415 8425 argop[1].argop = OP_SAVEFH;
8416 8426
8417 8427 /* 2: Lookup pre-rename fh of renamed object */
8418 8428 argop[2].argop = OP_CLOOKUP;
8419 8429 argop[2].nfs_argop4_u.opclookup.cname = onm;
8420 8430
8421 8431 /* 3: getfh fh of renamed object (before rename) */
8422 8432 argop[3].argop = OP_GETFH;
8423 8433
8424 8434 /* 4: putfh targetdir */
8425 8435 argop[4].argop = OP_CPUTFH;
8426 8436 argop[4].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh;
8427 8437
8428 8438 /* 5: current_fh is targetdir, saved_fh is sourcedir */
8429 8439 argop[5].argop = OP_CRENAME;
8430 8440 argop[5].nfs_argop4_u.opcrename.coldname = onm;
8431 8441 argop[5].nfs_argop4_u.opcrename.cnewname = nnm;
8432 8442
8433 8443 /* 6: getattr of target dir (post op attrs) */
8434 8444 argop[6].argop = OP_GETATTR;
8435 8445 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
8436 8446 argop[6].nfs_argop4_u.opgetattr.mi = mi;
8437 8447
8438 8448 /* 7: Lookup post-rename fh of renamed object */
8439 8449 argop[7].argop = OP_CLOOKUP;
8440 8450 argop[7].nfs_argop4_u.opclookup.cname = nnm;
8441 8451
8442 8452 /* 8: getfh fh of renamed object (after rename) */
8443 8453 argop[8].argop = OP_GETFH;
8444 8454
8445 8455 /* 9: getattr of renamed object */
8446 8456 argop[9].argop = OP_GETATTR;
8447 8457 argop[9].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
8448 8458 argop[9].nfs_argop4_u.opgetattr.mi = mi;
8449 8459
8450 8460 /*
8451 8461 * If source/target dirs are different, then get new post-op
8452 8462 * attrs for source dir also.
8453 8463 */
8454 8464 if (ndvp != odvp) {
8455 8465 /* 10: putfh (sourcedir) */
8456 8466 argop[10].argop = OP_CPUTFH;
8457 8467 argop[10].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh;
8458 8468
8459 8469 /* 11: getattr (sourcedir) */
8460 8470 argop[11].argop = OP_GETATTR;
8461 8471 argop[11].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
8462 8472 argop[11].nfs_argop4_u.opgetattr.mi = mi;
8463 8473 }
8464 8474
8465 8475 dnlc_remove(odvp, onm);
8466 8476 dnlc_remove(ndvp, nnm);
8467 8477
8468 8478 doqueue = 1;
8469 8479 t = gethrtime();
8470 8480 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
8471 8481
8472 8482 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
8473 8483 if (e.error) {
8474 8484 PURGE_ATTRCACHE4(odvp);
8475 8485 PURGE_ATTRCACHE4(ndvp);
8476 8486 if (!needrecov) {
8477 8487 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME,
8478 8488 &recov_state, needrecov);
8479 8489 goto out;
8480 8490 }
8481 8491 } else {
8482 8492 *statp = res.status;
8483 8493 }
8484 8494
8485 8495 if (needrecov) {
8486 8496 bool_t abort;
8487 8497
8488 8498 abort = nfs4_start_recovery(&e, mi, odvp, ndvp, NULL, NULL,
8489 8499 OP_RENAME, NULL, NULL, NULL);
8490 8500 if (abort == FALSE) {
8491 8501 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME,
8492 8502 &recov_state, needrecov);
8493 8503 kmem_free(argop, argoplist_size);
8494 8504 if (!e.error)
8495 8505 (void) xdr_free(xdr_COMPOUND4res_clnt,
8496 8506 (caddr_t)&res);
8497 8507 mutex_enter(&orp->r_statelock);
8498 8508 orp->r_flags &= ~R4RECEXPFH;
8499 8509 cv_broadcast(&orp->r_cv);
8500 8510 mutex_exit(&orp->r_statelock);
8501 8511 goto recov_retry;
8502 8512 } else {
8503 8513 if (e.error != 0) {
8504 8514 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME,
8505 8515 &recov_state, needrecov);
8506 8516 goto out;
8507 8517 }
8508 8518 /* fall through for res.status case */
8509 8519 }
8510 8520 }
8511 8521
8512 8522 resp = &res;
8513 8523 /*
8514 8524 * If OP_RENAME (or any prev op) failed, then return an error.
8515 8525 * OP_RENAME is index 5, so if array len <= 6 we return an error.
8516 8526 */
8517 8527 if ((res.status != NFS4_OK) && (res.array_len <= 6)) {
8518 8528 /*
8519 8529 * Error in an op other than last Getattr
8520 8530 */
8521 8531 e.error = geterrno4(res.status);
8522 8532 PURGE_ATTRCACHE4(odvp);
8523 8533 PURGE_ATTRCACHE4(ndvp);
8524 8534 /*
8525 8535 * System V defines rename to return EEXIST, not
8526 8536 * ENOTEMPTY if the target directory is not empty.
8527 8537 * Over the wire, the error is NFSERR_ENOTEMPTY
8528 8538 * which geterrno4 maps to ENOTEMPTY.
8529 8539 */
8530 8540 if (e.error == ENOTEMPTY)
8531 8541 e.error = EEXIST;
8532 8542 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, &recov_state,
8533 8543 needrecov);
8534 8544 goto out;
8535 8545 }
8536 8546
8537 8547 /* rename results */
8538 8548 rn_res = &res.array[5].nfs_resop4_u.oprename;
8539 8549
8540 8550 if (res.status == NFS4_OK) {
8541 8551 /* Update target attribute, readdir and dnlc caches */
8542 8552 dinfo.di_garp =
8543 8553 &res.array[6].nfs_resop4_u.opgetattr.ga_res;
8544 8554 dinfo.di_cred = cr;
8545 8555 dinfo.di_time_call = t;
8546 8556 } else
8547 8557 dinfop = NULL;
8548 8558
8549 8559 /* Update source cache attribute, readdir and dnlc caches */
8550 8560 nfs4_update_dircaches(&rn_res->target_cinfo, ndvp, NULL, NULL, dinfop);
8551 8561
8552 8562 /* Update source cache attribute, readdir and dnlc caches */
8553 8563 if (ndvp != odvp) {
8554 8564 update_parentdir_sfh(ovp, ndvp);
8555 8565
8556 8566 /*
8557 8567 * If dinfop is non-NULL, then compound succeded, so
8558 8568 * set di_garp to attrs for source dir. dinfop is only
8559 8569 * set to NULL when compound fails.
8560 8570 */
8561 8571 if (dinfop)
8562 8572 dinfo.di_garp =
8563 8573 &res.array[11].nfs_resop4_u.opgetattr.ga_res;
8564 8574 nfs4_update_dircaches(&rn_res->source_cinfo, odvp, NULL, NULL,
8565 8575 dinfop);
8566 8576 }
8567 8577
8568 8578 /*
8569 8579 * Update the rnode with the new component name and args,
8570 8580 * and if the file handle changed, also update it with the new fh.
8571 8581 * This is only necessary if the target object has an rnode
8572 8582 * entry and there is no need to create one for it.
8573 8583 */
8574 8584 resop = &res.array[8]; /* getfh new res */
8575 8585 ngf_res = &resop->nfs_resop4_u.opgetfh;
8576 8586
8577 8587 /*
8578 8588 * Update the path and filehandle for the renamed object.
8579 8589 */
8580 8590 nfs4rename_update(ovp, ndvp, &ngf_res->object, nnm);
8581 8591
8582 8592 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, &recov_state, needrecov);
8583 8593
8584 8594 if (res.status == NFS4_OK) {
8585 8595 resop++; /* getattr res */
8586 8596 e.error = nfs4_update_attrcache(res.status,
8587 8597 &resop->nfs_resop4_u.opgetattr.ga_res,
8588 8598 t, ovp, cr);
8589 8599 }
8590 8600
8591 8601 out:
8592 8602 kmem_free(argop, argoplist_size);
8593 8603 if (resp)
8594 8604 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
8595 8605 mutex_enter(&orp->r_statelock);
8596 8606 orp->r_flags &= ~R4RECEXPFH;
8597 8607 cv_broadcast(&orp->r_cv);
8598 8608 mutex_exit(&orp->r_statelock);
8599 8609
8600 8610 return (e.error);
8601 8611 }
8602 8612
8603 8613 /* ARGSUSED */
8604 8614 static int
8605 8615 nfs4_mkdir(vnode_t *dvp, char *nm, struct vattr *va, vnode_t **vpp, cred_t *cr,
8606 8616 caller_context_t *ct, int flags, vsecattr_t *vsecp)
8607 8617 {
8608 8618 int error;
8609 8619 vnode_t *vp;
8610 8620
8611 8621 if (nfs_zone() != VTOMI4(dvp)->mi_zone)
8612 8622 return (EPERM);
8613 8623 /*
8614 8624 * As ".." has special meaning and rather than send a mkdir
8615 8625 * over the wire to just let the server freak out, we just
8616 8626 * short circuit it here and return EEXIST
8617 8627 */
8618 8628 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0')
8619 8629 return (EEXIST);
8620 8630
8621 8631 /*
8622 8632 * Decision to get the right gid and setgid bit of the
8623 8633 * new directory is now made in call_nfs4_create_req.
8624 8634 */
8625 8635 va->va_mask |= AT_MODE;
8626 8636 error = call_nfs4_create_req(dvp, nm, NULL, va, &vp, cr, NF4DIR);
8627 8637 if (error)
8628 8638 return (error);
8629 8639
8630 8640 *vpp = vp;
8631 8641 return (0);
8632 8642 }
8633 8643
8634 8644
8635 8645 /*
8636 8646 * rmdir is using the same remove v4 op as does remove.
8637 8647 * Remove requires that the current fh be the target directory.
8638 8648 * After the operation, the current fh is unchanged.
8639 8649 * The compound op structure is:
8640 8650 * PUTFH(targetdir), REMOVE
8641 8651 */
8642 8652 /*ARGSUSED4*/
8643 8653 static int
8644 8654 nfs4_rmdir(vnode_t *dvp, char *nm, vnode_t *cdir, cred_t *cr,
8645 8655 caller_context_t *ct, int flags)
8646 8656 {
8647 8657 int need_end_op = FALSE;
8648 8658 COMPOUND4args_clnt args;
8649 8659 COMPOUND4res_clnt res, *resp = NULL;
8650 8660 REMOVE4res *rm_res;
8651 8661 nfs_argop4 argop[3];
8652 8662 nfs_resop4 *resop;
8653 8663 vnode_t *vp;
8654 8664 int doqueue;
8655 8665 mntinfo4_t *mi;
8656 8666 rnode4_t *drp;
8657 8667 bool_t needrecov = FALSE;
8658 8668 nfs4_recov_state_t recov_state;
8659 8669 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
8660 8670 dirattr_info_t dinfo, *dinfop;
8661 8671
8662 8672 if (nfs_zone() != VTOMI4(dvp)->mi_zone)
8663 8673 return (EPERM);
8664 8674 /*
8665 8675 * As ".." has special meaning and rather than send a rmdir
8666 8676 * over the wire to just let the server freak out, we just
8667 8677 * short circuit it here and return EEXIST
8668 8678 */
8669 8679 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0')
8670 8680 return (EEXIST);
8671 8681
8672 8682 drp = VTOR4(dvp);
8673 8683 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp)))
8674 8684 return (EINTR);
8675 8685
8676 8686 /*
8677 8687 * Attempt to prevent a rmdir(".") from succeeding.
8678 8688 */
8679 8689 e.error = nfs4lookup(dvp, nm, &vp, cr, 0);
8680 8690 if (e.error) {
8681 8691 nfs_rw_exit(&drp->r_rwlock);
8682 8692 return (e.error);
8683 8693 }
8684 8694 if (vp == cdir) {
8685 8695 VN_RELE(vp);
8686 8696 nfs_rw_exit(&drp->r_rwlock);
8687 8697 return (EINVAL);
8688 8698 }
8689 8699
8690 8700 /*
8691 8701 * Since nfsv4 remove op works on both files and directories,
8692 8702 * check that the removed object is indeed a directory.
8693 8703 */
8694 8704 if (vp->v_type != VDIR) {
8695 8705 VN_RELE(vp);
8696 8706 nfs_rw_exit(&drp->r_rwlock);
8697 8707 return (ENOTDIR);
8698 8708 }
8699 8709
8700 8710 /*
8701 8711 * First just remove the entry from the name cache, as it
8702 8712 * is most likely an entry for this vp.
8703 8713 */
8704 8714 dnlc_remove(dvp, nm);
8705 8715
8706 8716 /*
8707 8717 * If there vnode reference count is greater than one, then
8708 8718 * there may be additional references in the DNLC which will
8709 8719 * need to be purged. First, trying removing the entry for
8710 8720 * the parent directory and see if that removes the additional
8711 8721 * reference(s). If that doesn't do it, then use dnlc_purge_vp
8712 8722 * to completely remove any references to the directory which
8713 8723 * might still exist in the DNLC.
8714 8724 */
8715 8725 if (vp->v_count > 1) {
8716 8726 dnlc_remove(vp, "..");
8717 8727 if (vp->v_count > 1)
8718 8728 dnlc_purge_vp(vp);
8719 8729 }
8720 8730
8721 8731 mi = VTOMI4(dvp);
8722 8732 recov_state.rs_flags = 0;
8723 8733 recov_state.rs_num_retry_despite_err = 0;
8724 8734
8725 8735 recov_retry:
8726 8736 args.ctag = TAG_RMDIR;
8727 8737
8728 8738 /*
8729 8739 * Rmdir ops: putfh dir; remove
8730 8740 */
8731 8741 args.array_len = 3;
8732 8742 args.array = argop;
8733 8743
8734 8744 e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state);
8735 8745 if (e.error) {
8736 8746 nfs_rw_exit(&drp->r_rwlock);
8737 8747 return (e.error);
8738 8748 }
8739 8749 need_end_op = TRUE;
8740 8750
8741 8751 /* putfh directory */
8742 8752 argop[0].argop = OP_CPUTFH;
8743 8753 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
8744 8754
8745 8755 /* remove */
8746 8756 argop[1].argop = OP_CREMOVE;
8747 8757 argop[1].nfs_argop4_u.opcremove.ctarget = nm;
8748 8758
8749 8759 /* getattr (postop attrs for dir that contained removed dir) */
8750 8760 argop[2].argop = OP_GETATTR;
8751 8761 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
8752 8762 argop[2].nfs_argop4_u.opgetattr.mi = mi;
8753 8763
8754 8764 dinfo.di_time_call = gethrtime();
8755 8765 doqueue = 1;
8756 8766 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
8757 8767
8758 8768 PURGE_ATTRCACHE4(vp);
8759 8769
8760 8770 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
8761 8771 if (e.error) {
8762 8772 PURGE_ATTRCACHE4(dvp);
8763 8773 }
8764 8774
8765 8775 if (needrecov) {
8766 8776 if (nfs4_start_recovery(&e, VTOMI4(dvp), dvp, NULL, NULL,
8767 8777 NULL, OP_REMOVE, NULL, NULL, NULL) == FALSE) {
8768 8778 if (!e.error)
8769 8779 (void) xdr_free(xdr_COMPOUND4res_clnt,
8770 8780 (caddr_t)&res);
8771 8781
8772 8782 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state,
8773 8783 needrecov);
8774 8784 need_end_op = FALSE;
8775 8785 goto recov_retry;
8776 8786 }
8777 8787 }
8778 8788
8779 8789 if (!e.error) {
8780 8790 resp = &res;
8781 8791
8782 8792 /*
8783 8793 * Only return error if first 2 ops (OP_REMOVE or earlier)
8784 8794 * failed.
8785 8795 */
8786 8796 if (res.status != NFS4_OK && res.array_len <= 2) {
8787 8797 e.error = geterrno4(res.status);
8788 8798 PURGE_ATTRCACHE4(dvp);
8789 8799 nfs4_end_op(VTOMI4(dvp), dvp, NULL,
8790 8800 &recov_state, needrecov);
8791 8801 need_end_op = FALSE;
8792 8802 nfs4_purge_stale_fh(e.error, dvp, cr);
8793 8803 /*
8794 8804 * System V defines rmdir to return EEXIST, not
8795 8805 * ENOTEMPTY if the directory is not empty. Over
8796 8806 * the wire, the error is NFSERR_ENOTEMPTY which
8797 8807 * geterrno4 maps to ENOTEMPTY.
8798 8808 */
8799 8809 if (e.error == ENOTEMPTY)
8800 8810 e.error = EEXIST;
8801 8811 } else {
8802 8812 resop = &res.array[1]; /* remove res */
8803 8813 rm_res = &resop->nfs_resop4_u.opremove;
8804 8814
8805 8815 if (res.status == NFS4_OK) {
8806 8816 resop = &res.array[2]; /* dir attrs */
8807 8817 dinfo.di_garp =
8808 8818 &resop->nfs_resop4_u.opgetattr.ga_res;
8809 8819 dinfo.di_cred = cr;
8810 8820 dinfop = &dinfo;
8811 8821 } else
8812 8822 dinfop = NULL;
8813 8823
8814 8824 /* Update dir attribute, readdir and dnlc caches */
8815 8825 nfs4_update_dircaches(&rm_res->cinfo, dvp, NULL, NULL,
8816 8826 dinfop);
8817 8827
8818 8828 /* destroy rddir cache for dir that was removed */
8819 8829 if (VTOR4(vp)->r_dir != NULL)
8820 8830 nfs4_purge_rddir_cache(vp);
8821 8831 }
8822 8832 }
8823 8833
8824 8834 if (need_end_op)
8825 8835 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov);
8826 8836
8827 8837 nfs_rw_exit(&drp->r_rwlock);
8828 8838
8829 8839 if (resp)
8830 8840 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
8831 8841
8832 8842 if (e.error == 0) {
8833 8843 vnode_t *tvp;
8834 8844 rnode4_t *trp;
8835 8845 trp = VTOR4(vp);
8836 8846 tvp = vp;
8837 8847 if (IS_SHADOW(vp, trp))
8838 8848 tvp = RTOV4(trp);
8839 8849 vnevent_rmdir(tvp, dvp, nm, ct);
8840 8850 }
8841 8851
8842 8852 VN_RELE(vp);
8843 8853
8844 8854 return (e.error);
8845 8855 }
8846 8856
8847 8857 /* ARGSUSED */
8848 8858 static int
8849 8859 nfs4_symlink(vnode_t *dvp, char *lnm, struct vattr *tva, char *tnm, cred_t *cr,
8850 8860 caller_context_t *ct, int flags)
8851 8861 {
8852 8862 int error;
8853 8863 vnode_t *vp;
8854 8864 rnode4_t *rp;
8855 8865 char *contents;
8856 8866 mntinfo4_t *mi = VTOMI4(dvp);
8857 8867
8858 8868 if (nfs_zone() != mi->mi_zone)
8859 8869 return (EPERM);
8860 8870 if (!(mi->mi_flags & MI4_SYMLINK))
8861 8871 return (EOPNOTSUPP);
8862 8872
8863 8873 error = call_nfs4_create_req(dvp, lnm, tnm, tva, &vp, cr, NF4LNK);
8864 8874 if (error)
8865 8875 return (error);
8866 8876
8867 8877 ASSERT(nfs4_consistent_type(vp));
8868 8878 rp = VTOR4(vp);
8869 8879 if (nfs4_do_symlink_cache && rp->r_symlink.contents == NULL) {
8870 8880
8871 8881 contents = kmem_alloc(MAXPATHLEN, KM_SLEEP);
8872 8882
8873 8883 if (contents != NULL) {
8874 8884 mutex_enter(&rp->r_statelock);
8875 8885 if (rp->r_symlink.contents == NULL) {
8876 8886 rp->r_symlink.len = strlen(tnm);
8877 8887 bcopy(tnm, contents, rp->r_symlink.len);
8878 8888 rp->r_symlink.contents = contents;
8879 8889 rp->r_symlink.size = MAXPATHLEN;
8880 8890 mutex_exit(&rp->r_statelock);
8881 8891 } else {
8882 8892 mutex_exit(&rp->r_statelock);
8883 8893 kmem_free((void *)contents, MAXPATHLEN);
8884 8894 }
8885 8895 }
8886 8896 }
8887 8897 VN_RELE(vp);
8888 8898
8889 8899 return (error);
8890 8900 }
8891 8901
8892 8902
8893 8903 /*
8894 8904 * Read directory entries.
8895 8905 * There are some weird things to look out for here. The uio_loffset
8896 8906 * field is either 0 or it is the offset returned from a previous
8897 8907 * readdir. It is an opaque value used by the server to find the
8898 8908 * correct directory block to read. The count field is the number
8899 8909 * of blocks to read on the server. This is advisory only, the server
8900 8910 * may return only one block's worth of entries. Entries may be compressed
8901 8911 * on the server.
8902 8912 */
8903 8913 /* ARGSUSED */
8904 8914 static int
8905 8915 nfs4_readdir(vnode_t *vp, struct uio *uiop, cred_t *cr, int *eofp,
8906 8916 caller_context_t *ct, int flags)
8907 8917 {
8908 8918 int error;
8909 8919 uint_t count;
8910 8920 rnode4_t *rp;
8911 8921 rddir4_cache *rdc;
8912 8922 rddir4_cache *rrdc;
8913 8923
8914 8924 if (nfs_zone() != VTOMI4(vp)->mi_zone)
8915 8925 return (EIO);
8916 8926 rp = VTOR4(vp);
8917 8927
8918 8928 ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER));
8919 8929
8920 8930 /*
8921 8931 * Make sure that the directory cache is valid.
8922 8932 */
8923 8933 if (rp->r_dir != NULL) {
8924 8934 if (nfs_disable_rddir_cache != 0) {
8925 8935 /*
8926 8936 * Setting nfs_disable_rddir_cache in /etc/system
8927 8937 * allows interoperability with servers that do not
8928 8938 * properly update the attributes of directories.
8929 8939 * Any cached information gets purged before an
8930 8940 * access is made to it.
8931 8941 */
8932 8942 nfs4_purge_rddir_cache(vp);
8933 8943 }
8934 8944
8935 8945 error = nfs4_validate_caches(vp, cr);
8936 8946 if (error)
8937 8947 return (error);
8938 8948 }
8939 8949
8940 8950 count = MIN(uiop->uio_iov->iov_len, MAXBSIZE);
8941 8951
8942 8952 /*
8943 8953 * Short circuit last readdir which always returns 0 bytes.
8944 8954 * This can be done after the directory has been read through
8945 8955 * completely at least once. This will set r_direof which
8946 8956 * can be used to find the value of the last cookie.
8947 8957 */
8948 8958 mutex_enter(&rp->r_statelock);
8949 8959 if (rp->r_direof != NULL &&
8950 8960 uiop->uio_loffset == rp->r_direof->nfs4_ncookie) {
8951 8961 mutex_exit(&rp->r_statelock);
8952 8962 #ifdef DEBUG
8953 8963 nfs4_readdir_cache_shorts++;
8954 8964 #endif
8955 8965 if (eofp)
8956 8966 *eofp = 1;
8957 8967 return (0);
8958 8968 }
8959 8969
8960 8970 /*
8961 8971 * Look for a cache entry. Cache entries are identified
8962 8972 * by the NFS cookie value and the byte count requested.
8963 8973 */
8964 8974 rdc = rddir4_cache_lookup(rp, uiop->uio_loffset, count);
8965 8975
8966 8976 /*
8967 8977 * If rdc is NULL then the lookup resulted in an unrecoverable error.
8968 8978 */
8969 8979 if (rdc == NULL) {
8970 8980 mutex_exit(&rp->r_statelock);
8971 8981 return (EINTR);
8972 8982 }
8973 8983
8974 8984 /*
8975 8985 * Check to see if we need to fill this entry in.
8976 8986 */
8977 8987 if (rdc->flags & RDDIRREQ) {
8978 8988 rdc->flags &= ~RDDIRREQ;
8979 8989 rdc->flags |= RDDIR;
8980 8990 mutex_exit(&rp->r_statelock);
8981 8991
8982 8992 /*
8983 8993 * Do the readdir.
8984 8994 */
8985 8995 nfs4readdir(vp, rdc, cr);
8986 8996
8987 8997 /*
8988 8998 * Reacquire the lock, so that we can continue
8989 8999 */
8990 9000 mutex_enter(&rp->r_statelock);
8991 9001 /*
8992 9002 * The entry is now complete
8993 9003 */
8994 9004 rdc->flags &= ~RDDIR;
8995 9005 }
8996 9006
8997 9007 ASSERT(!(rdc->flags & RDDIR));
8998 9008
8999 9009 /*
9000 9010 * If an error occurred while attempting
9001 9011 * to fill the cache entry, mark the entry invalid and
9002 9012 * just return the error.
9003 9013 */
9004 9014 if (rdc->error) {
9005 9015 error = rdc->error;
9006 9016 rdc->flags |= RDDIRREQ;
9007 9017 rddir4_cache_rele(rp, rdc);
9008 9018 mutex_exit(&rp->r_statelock);
9009 9019 return (error);
9010 9020 }
9011 9021
9012 9022 /*
9013 9023 * The cache entry is complete and good,
9014 9024 * copyout the dirent structs to the calling
9015 9025 * thread.
9016 9026 */
9017 9027 error = uiomove(rdc->entries, rdc->actlen, UIO_READ, uiop);
9018 9028
9019 9029 /*
9020 9030 * If no error occurred during the copyout,
9021 9031 * update the offset in the uio struct to
9022 9032 * contain the value of the next NFS 4 cookie
9023 9033 * and set the eof value appropriately.
9024 9034 */
9025 9035 if (!error) {
9026 9036 uiop->uio_loffset = rdc->nfs4_ncookie;
9027 9037 if (eofp)
9028 9038 *eofp = rdc->eof;
9029 9039 }
9030 9040
9031 9041 /*
9032 9042 * Decide whether to do readahead. Don't if we
9033 9043 * have already read to the end of directory.
9034 9044 */
9035 9045 if (rdc->eof) {
9036 9046 /*
9037 9047 * Make the entry the direof only if it is cached
9038 9048 */
9039 9049 if (rdc->flags & RDDIRCACHED)
9040 9050 rp->r_direof = rdc;
9041 9051 rddir4_cache_rele(rp, rdc);
9042 9052 mutex_exit(&rp->r_statelock);
9043 9053 return (error);
9044 9054 }
9045 9055
9046 9056 /* Determine if a readdir readahead should be done */
9047 9057 if (!(rp->r_flags & R4LOOKUP)) {
9048 9058 rddir4_cache_rele(rp, rdc);
9049 9059 mutex_exit(&rp->r_statelock);
9050 9060 return (error);
9051 9061 }
9052 9062
9053 9063 /*
9054 9064 * Now look for a readahead entry.
9055 9065 *
9056 9066 * Check to see whether we found an entry for the readahead.
9057 9067 * If so, we don't need to do anything further, so free the new
9058 9068 * entry if one was allocated. Otherwise, allocate a new entry, add
9059 9069 * it to the cache, and then initiate an asynchronous readdir
9060 9070 * operation to fill it.
9061 9071 */
9062 9072 rrdc = rddir4_cache_lookup(rp, rdc->nfs4_ncookie, count);
9063 9073
9064 9074 /*
9065 9075 * A readdir cache entry could not be obtained for the readahead. In
9066 9076 * this case we skip the readahead and return.
9067 9077 */
9068 9078 if (rrdc == NULL) {
9069 9079 rddir4_cache_rele(rp, rdc);
9070 9080 mutex_exit(&rp->r_statelock);
9071 9081 return (error);
9072 9082 }
9073 9083
9074 9084 /*
9075 9085 * Check to see if we need to fill this entry in.
9076 9086 */
9077 9087 if (rrdc->flags & RDDIRREQ) {
9078 9088 rrdc->flags &= ~RDDIRREQ;
9079 9089 rrdc->flags |= RDDIR;
9080 9090 rddir4_cache_rele(rp, rdc);
9081 9091 mutex_exit(&rp->r_statelock);
9082 9092 #ifdef DEBUG
9083 9093 nfs4_readdir_readahead++;
9084 9094 #endif
9085 9095 /*
9086 9096 * Do the readdir.
9087 9097 */
9088 9098 nfs4_async_readdir(vp, rrdc, cr, do_nfs4readdir);
9089 9099 return (error);
9090 9100 }
9091 9101
9092 9102 rddir4_cache_rele(rp, rrdc);
9093 9103 rddir4_cache_rele(rp, rdc);
9094 9104 mutex_exit(&rp->r_statelock);
9095 9105 return (error);
9096 9106 }
9097 9107
9098 9108 static int
9099 9109 do_nfs4readdir(vnode_t *vp, rddir4_cache *rdc, cred_t *cr)
9100 9110 {
9101 9111 int error;
9102 9112 rnode4_t *rp;
9103 9113
9104 9114 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
9105 9115
9106 9116 rp = VTOR4(vp);
9107 9117
9108 9118 /*
9109 9119 * Obtain the readdir results for the caller.
9110 9120 */
9111 9121 nfs4readdir(vp, rdc, cr);
9112 9122
9113 9123 mutex_enter(&rp->r_statelock);
9114 9124 /*
9115 9125 * The entry is now complete
9116 9126 */
9117 9127 rdc->flags &= ~RDDIR;
9118 9128
9119 9129 error = rdc->error;
9120 9130 if (error)
9121 9131 rdc->flags |= RDDIRREQ;
9122 9132 rddir4_cache_rele(rp, rdc);
9123 9133 mutex_exit(&rp->r_statelock);
9124 9134
9125 9135 return (error);
9126 9136 }
9127 9137
9128 9138 /*
9129 9139 * Read directory entries.
9130 9140 * There are some weird things to look out for here. The uio_loffset
9131 9141 * field is either 0 or it is the offset returned from a previous
9132 9142 * readdir. It is an opaque value used by the server to find the
9133 9143 * correct directory block to read. The count field is the number
9134 9144 * of blocks to read on the server. This is advisory only, the server
9135 9145 * may return only one block's worth of entries. Entries may be compressed
9136 9146 * on the server.
9137 9147 *
9138 9148 * Generates the following compound request:
9139 9149 * 1. If readdir offset is zero and no dnlc entry for parent exists,
9140 9150 * must include a Lookupp as well. In this case, send:
9141 9151 * { Putfh <fh>; Readdir; Lookupp; Getfh; Getattr }
9142 9152 * 2. Otherwise just do: { Putfh <fh>; Readdir }
9143 9153 *
9144 9154 * Get complete attributes and filehandles for entries if this is the
9145 9155 * first read of the directory. Otherwise, just get fileid's.
9146 9156 */
9147 9157 static void
9148 9158 nfs4readdir(vnode_t *vp, rddir4_cache *rdc, cred_t *cr)
9149 9159 {
9150 9160 COMPOUND4args_clnt args;
9151 9161 COMPOUND4res_clnt res;
9152 9162 READDIR4args *rargs;
9153 9163 READDIR4res_clnt *rd_res;
9154 9164 bitmap4 rd_bitsval;
9155 9165 nfs_argop4 argop[5];
9156 9166 nfs_resop4 *resop;
9157 9167 rnode4_t *rp = VTOR4(vp);
9158 9168 mntinfo4_t *mi = VTOMI4(vp);
9159 9169 int doqueue;
9160 9170 u_longlong_t nodeid, pnodeid; /* id's of dir and its parents */
9161 9171 vnode_t *dvp;
9162 9172 nfs_cookie4 cookie = (nfs_cookie4)rdc->nfs4_cookie;
9163 9173 int num_ops, res_opcnt;
9164 9174 bool_t needrecov = FALSE;
9165 9175 nfs4_recov_state_t recov_state;
9166 9176 hrtime_t t;
9167 9177 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
9168 9178
9169 9179 ASSERT(nfs_zone() == mi->mi_zone);
9170 9180 ASSERT(rdc->flags & RDDIR);
9171 9181 ASSERT(rdc->entries == NULL);
9172 9182
9173 9183 /*
9174 9184 * If rp were a stub, it should have triggered and caused
9175 9185 * a mount for us to get this far.
9176 9186 */
9177 9187 ASSERT(!RP_ISSTUB(rp));
9178 9188
9179 9189 num_ops = 2;
9180 9190 if (cookie == (nfs_cookie4)0 || cookie == (nfs_cookie4)1) {
9181 9191 /*
9182 9192 * Since nfsv4 readdir may not return entries for "." and "..",
9183 9193 * the client must recreate them:
9184 9194 * To find the correct nodeid, do the following:
9185 9195 * For current node, get nodeid from dnlc.
9186 9196 * - if current node is rootvp, set pnodeid to nodeid.
9187 9197 * - else if parent is in the dnlc, get its nodeid from there.
9188 9198 * - else add LOOKUPP+GETATTR to compound.
9189 9199 */
9190 9200 nodeid = rp->r_attr.va_nodeid;
9191 9201 if (vp->v_flag & VROOT) {
9192 9202 pnodeid = nodeid; /* root of mount point */
9193 9203 } else {
9194 9204 dvp = dnlc_lookup(vp, "..");
9195 9205 if (dvp != NULL && dvp != DNLC_NO_VNODE) {
9196 9206 /* parent in dnlc cache - no need for otw */
9197 9207 pnodeid = VTOR4(dvp)->r_attr.va_nodeid;
9198 9208 } else {
9199 9209 /*
9200 9210 * parent not in dnlc cache,
9201 9211 * do lookupp to get its id
9202 9212 */
9203 9213 num_ops = 5;
9204 9214 pnodeid = 0; /* set later by getattr parent */
9205 9215 }
9206 9216 if (dvp)
9207 9217 VN_RELE(dvp);
9208 9218 }
9209 9219 }
9210 9220 recov_state.rs_flags = 0;
9211 9221 recov_state.rs_num_retry_despite_err = 0;
9212 9222
9213 9223 /* Save the original mount point security flavor */
9214 9224 (void) save_mnt_secinfo(mi->mi_curr_serv);
9215 9225
9216 9226 recov_retry:
9217 9227 args.ctag = TAG_READDIR;
9218 9228
9219 9229 args.array = argop;
9220 9230 args.array_len = num_ops;
9221 9231
9222 9232 if (e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_READDIR,
9223 9233 &recov_state, NULL)) {
9224 9234 /*
9225 9235 * If readdir a node that is a stub for a crossed mount point,
9226 9236 * keep the original secinfo flavor for the current file
9227 9237 * system, not the crossed one.
9228 9238 */
9229 9239 (void) check_mnt_secinfo(mi->mi_curr_serv, vp);
9230 9240 rdc->error = e.error;
9231 9241 return;
9232 9242 }
9233 9243
9234 9244 /*
9235 9245 * Determine which attrs to request for dirents. This code
9236 9246 * must be protected by nfs4_start/end_fop because of r_server
9237 9247 * (which will change during failover recovery).
9238 9248 *
9239 9249 */
9240 9250 if (rp->r_flags & (R4LOOKUP | R4READDIRWATTR)) {
9241 9251 /*
9242 9252 * Get all vattr attrs plus filehandle and rdattr_error
9243 9253 */
9244 9254 rd_bitsval = NFS4_VATTR_MASK |
9245 9255 FATTR4_RDATTR_ERROR_MASK |
9246 9256 FATTR4_FILEHANDLE_MASK;
9247 9257
9248 9258 if (rp->r_flags & R4READDIRWATTR) {
9249 9259 mutex_enter(&rp->r_statelock);
9250 9260 rp->r_flags &= ~R4READDIRWATTR;
9251 9261 mutex_exit(&rp->r_statelock);
9252 9262 }
9253 9263 } else {
9254 9264 servinfo4_t *svp = rp->r_server;
9255 9265
9256 9266 /*
9257 9267 * Already read directory. Use readdir with
9258 9268 * no attrs (except for mounted_on_fileid) for updates.
9259 9269 */
9260 9270 rd_bitsval = FATTR4_RDATTR_ERROR_MASK;
9261 9271
9262 9272 /*
9263 9273 * request mounted on fileid if supported, else request
9264 9274 * fileid. maybe we should verify that fileid is supported
9265 9275 * and request something else if not.
9266 9276 */
9267 9277 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
9268 9278 if (svp->sv_supp_attrs & FATTR4_MOUNTED_ON_FILEID_MASK)
9269 9279 rd_bitsval |= FATTR4_MOUNTED_ON_FILEID_MASK;
9270 9280 nfs_rw_exit(&svp->sv_lock);
9271 9281 }
9272 9282
9273 9283 /* putfh directory fh */
9274 9284 argop[0].argop = OP_CPUTFH;
9275 9285 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
9276 9286
9277 9287 argop[1].argop = OP_READDIR;
9278 9288 rargs = &argop[1].nfs_argop4_u.opreaddir;
9279 9289 /*
9280 9290 * 1 and 2 are reserved for client "." and ".." entry offset.
9281 9291 * cookie 0 should be used over-the-wire to start reading at
9282 9292 * the beginning of the directory excluding "." and "..".
9283 9293 */
9284 9294 if (rdc->nfs4_cookie == 0 ||
9285 9295 rdc->nfs4_cookie == 1 ||
9286 9296 rdc->nfs4_cookie == 2) {
9287 9297 rargs->cookie = (nfs_cookie4)0;
9288 9298 rargs->cookieverf = 0;
9289 9299 } else {
9290 9300 rargs->cookie = (nfs_cookie4)rdc->nfs4_cookie;
9291 9301 mutex_enter(&rp->r_statelock);
9292 9302 rargs->cookieverf = rp->r_cookieverf4;
9293 9303 mutex_exit(&rp->r_statelock);
9294 9304 }
9295 9305 rargs->dircount = MIN(rdc->buflen, mi->mi_tsize);
9296 9306 rargs->maxcount = mi->mi_tsize;
9297 9307 rargs->attr_request = rd_bitsval;
9298 9308 rargs->rdc = rdc;
9299 9309 rargs->dvp = vp;
9300 9310 rargs->mi = mi;
9301 9311 rargs->cr = cr;
9302 9312
9303 9313
9304 9314 /*
9305 9315 * If count < than the minimum required, we return no entries
9306 9316 * and fail with EINVAL
9307 9317 */
9308 9318 if (rargs->dircount < (DIRENT64_RECLEN(1) + DIRENT64_RECLEN(2))) {
9309 9319 rdc->error = EINVAL;
9310 9320 goto out;
9311 9321 }
9312 9322
9313 9323 if (args.array_len == 5) {
9314 9324 /*
9315 9325 * Add lookupp and getattr for parent nodeid.
9316 9326 */
9317 9327 argop[2].argop = OP_LOOKUPP;
9318 9328
9319 9329 argop[3].argop = OP_GETFH;
9320 9330
9321 9331 /* getattr parent */
9322 9332 argop[4].argop = OP_GETATTR;
9323 9333 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
9324 9334 argop[4].nfs_argop4_u.opgetattr.mi = mi;
9325 9335 }
9326 9336
9327 9337 doqueue = 1;
9328 9338
9329 9339 if (mi->mi_io_kstats) {
9330 9340 mutex_enter(&mi->mi_lock);
9331 9341 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
9332 9342 mutex_exit(&mi->mi_lock);
9333 9343 }
9334 9344
9335 9345 /* capture the time of this call */
9336 9346 rargs->t = t = gethrtime();
9337 9347
9338 9348 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
9339 9349
9340 9350 if (mi->mi_io_kstats) {
9341 9351 mutex_enter(&mi->mi_lock);
9342 9352 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
9343 9353 mutex_exit(&mi->mi_lock);
9344 9354 }
9345 9355
9346 9356 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
9347 9357
9348 9358 /*
9349 9359 * If RPC error occurred and it isn't an error that
9350 9360 * triggers recovery, then go ahead and fail now.
9351 9361 */
9352 9362 if (e.error != 0 && !needrecov) {
9353 9363 rdc->error = e.error;
9354 9364 goto out;
9355 9365 }
9356 9366
9357 9367 if (needrecov) {
9358 9368 bool_t abort;
9359 9369
9360 9370 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
9361 9371 "nfs4readdir: initiating recovery.\n"));
9362 9372
9363 9373 abort = nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL,
9364 9374 NULL, OP_READDIR, NULL, NULL, NULL);
9365 9375 if (abort == FALSE) {
9366 9376 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_READDIR,
9367 9377 &recov_state, needrecov);
9368 9378 if (!e.error)
9369 9379 (void) xdr_free(xdr_COMPOUND4res_clnt,
9370 9380 (caddr_t)&res);
9371 9381 if (rdc->entries != NULL) {
9372 9382 kmem_free(rdc->entries, rdc->entlen);
9373 9383 rdc->entries = NULL;
9374 9384 }
9375 9385 goto recov_retry;
9376 9386 }
9377 9387
9378 9388 if (e.error != 0) {
9379 9389 rdc->error = e.error;
9380 9390 goto out;
9381 9391 }
9382 9392
9383 9393 /* fall through for res.status case */
9384 9394 }
9385 9395
9386 9396 res_opcnt = res.array_len;
9387 9397
9388 9398 /*
9389 9399 * If compound failed first 2 ops (PUTFH+READDIR), then return
9390 9400 * failure here. Subsequent ops are for filling out dot-dot
9391 9401 * dirent, and if they fail, we still want to give the caller
9392 9402 * the dirents returned by (the successful) READDIR op, so we need
9393 9403 * to silently ignore failure for subsequent ops (LOOKUPP+GETATTR).
9394 9404 *
9395 9405 * One example where PUTFH+READDIR ops would succeed but
9396 9406 * LOOKUPP+GETATTR would fail would be a dir that has r perm
9397 9407 * but lacks x. In this case, a POSIX server's VOP_READDIR
9398 9408 * would succeed; however, VOP_LOOKUP(..) would fail since no
9399 9409 * x perm. We need to come up with a non-vendor-specific way
9400 9410 * for a POSIX server to return d_ino from dotdot's dirent if
9401 9411 * client only requests mounted_on_fileid, and just say the
9402 9412 * LOOKUPP succeeded and fill out the GETATTR. However, if
9403 9413 * client requested any mandatory attrs, server would be required
9404 9414 * to fail the GETATTR op because it can't call VOP_LOOKUP+VOP_GETATTR
9405 9415 * for dotdot.
9406 9416 */
9407 9417
9408 9418 if (res.status) {
9409 9419 if (res_opcnt <= 2) {
9410 9420 e.error = geterrno4(res.status);
9411 9421 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_READDIR,
9412 9422 &recov_state, needrecov);
9413 9423 nfs4_purge_stale_fh(e.error, vp, cr);
9414 9424 rdc->error = e.error;
9415 9425 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
9416 9426 if (rdc->entries != NULL) {
9417 9427 kmem_free(rdc->entries, rdc->entlen);
9418 9428 rdc->entries = NULL;
9419 9429 }
9420 9430 /*
9421 9431 * If readdir a node that is a stub for a
9422 9432 * crossed mount point, keep the original
9423 9433 * secinfo flavor for the current file system,
9424 9434 * not the crossed one.
9425 9435 */
9426 9436 (void) check_mnt_secinfo(mi->mi_curr_serv, vp);
9427 9437 return;
9428 9438 }
9429 9439 }
9430 9440
9431 9441 resop = &res.array[1]; /* readdir res */
9432 9442 rd_res = &resop->nfs_resop4_u.opreaddirclnt;
9433 9443
9434 9444 mutex_enter(&rp->r_statelock);
9435 9445 rp->r_cookieverf4 = rd_res->cookieverf;
9436 9446 mutex_exit(&rp->r_statelock);
9437 9447
9438 9448 /*
9439 9449 * For "." and ".." entries
9440 9450 * e.g.
9441 9451 * seek(cookie=0) -> "." entry with d_off = 1
9442 9452 * seek(cookie=1) -> ".." entry with d_off = 2
9443 9453 */
9444 9454 if (cookie == (nfs_cookie4) 0) {
9445 9455 if (rd_res->dotp)
9446 9456 rd_res->dotp->d_ino = nodeid;
9447 9457 if (rd_res->dotdotp)
9448 9458 rd_res->dotdotp->d_ino = pnodeid;
9449 9459 }
9450 9460 if (cookie == (nfs_cookie4) 1) {
9451 9461 if (rd_res->dotdotp)
9452 9462 rd_res->dotdotp->d_ino = pnodeid;
9453 9463 }
9454 9464
9455 9465
9456 9466 /* LOOKUPP+GETATTR attemped */
9457 9467 if (args.array_len == 5 && rd_res->dotdotp) {
9458 9468 if (res.status == NFS4_OK && res_opcnt == 5) {
9459 9469 nfs_fh4 *fhp;
9460 9470 nfs4_sharedfh_t *sfhp;
9461 9471 vnode_t *pvp;
9462 9472 nfs4_ga_res_t *garp;
9463 9473
9464 9474 resop++; /* lookupp */
9465 9475 resop++; /* getfh */
9466 9476 fhp = &resop->nfs_resop4_u.opgetfh.object;
9467 9477
9468 9478 resop++; /* getattr of parent */
9469 9479
9470 9480 /*
9471 9481 * First, take care of finishing the
9472 9482 * readdir results.
9473 9483 */
9474 9484 garp = &resop->nfs_resop4_u.opgetattr.ga_res;
9475 9485 /*
9476 9486 * The d_ino of .. must be the inode number
9477 9487 * of the mounted filesystem.
9478 9488 */
9479 9489 if (garp->n4g_va.va_mask & AT_NODEID)
9480 9490 rd_res->dotdotp->d_ino =
9481 9491 garp->n4g_va.va_nodeid;
9482 9492
9483 9493
9484 9494 /*
9485 9495 * Next, create the ".." dnlc entry
9486 9496 */
9487 9497 sfhp = sfh4_get(fhp, mi);
9488 9498 if (!nfs4_make_dotdot(sfhp, t, vp, cr, &pvp, 0)) {
9489 9499 dnlc_update(vp, "..", pvp);
9490 9500 VN_RELE(pvp);
9491 9501 }
9492 9502 sfh4_rele(&sfhp);
9493 9503 }
9494 9504 }
9495 9505
9496 9506 if (mi->mi_io_kstats) {
9497 9507 mutex_enter(&mi->mi_lock);
9498 9508 KSTAT_IO_PTR(mi->mi_io_kstats)->reads++;
9499 9509 KSTAT_IO_PTR(mi->mi_io_kstats)->nread += rdc->actlen;
9500 9510 mutex_exit(&mi->mi_lock);
9501 9511 }
9502 9512
9503 9513 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
9504 9514
9505 9515 out:
9506 9516 /*
9507 9517 * If readdir a node that is a stub for a crossed mount point,
9508 9518 * keep the original secinfo flavor for the current file system,
9509 9519 * not the crossed one.
9510 9520 */
9511 9521 (void) check_mnt_secinfo(mi->mi_curr_serv, vp);
9512 9522
9513 9523 nfs4_end_fop(mi, vp, NULL, OH_READDIR, &recov_state, needrecov);
9514 9524 }
9515 9525
9516 9526
9517 9527 static int
9518 9528 nfs4_bio(struct buf *bp, stable_how4 *stab_comm, cred_t *cr, bool_t readahead)
9519 9529 {
9520 9530 rnode4_t *rp = VTOR4(bp->b_vp);
9521 9531 int count;
9522 9532 int error;
9523 9533 cred_t *cred_otw = NULL;
9524 9534 offset_t offset;
9525 9535 nfs4_open_stream_t *osp = NULL;
9526 9536 bool_t first_time = TRUE; /* first time getting otw cred */
9527 9537 bool_t last_time = FALSE; /* last time getting otw cred */
9528 9538
9529 9539 ASSERT(nfs_zone() == VTOMI4(bp->b_vp)->mi_zone);
9530 9540
9531 9541 DTRACE_IO1(start, struct buf *, bp);
9532 9542 offset = ldbtob(bp->b_lblkno);
9533 9543
9534 9544 if (bp->b_flags & B_READ) {
9535 9545 read_again:
9536 9546 /*
9537 9547 * Releases the osp, if it is provided.
9538 9548 * Puts a hold on the cred_otw and the new osp (if found).
9539 9549 */
9540 9550 cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp,
9541 9551 &first_time, &last_time);
9542 9552 error = bp->b_error = nfs4read(bp->b_vp, bp->b_un.b_addr,
9543 9553 offset, bp->b_bcount, &bp->b_resid, cred_otw,
9544 9554 readahead, NULL);
9545 9555 crfree(cred_otw);
9546 9556 if (!error) {
9547 9557 if (bp->b_resid) {
9548 9558 /*
9549 9559 * Didn't get it all because we hit EOF,
9550 9560 * zero all the memory beyond the EOF.
9551 9561 */
9552 9562 /* bzero(rdaddr + */
9553 9563 bzero(bp->b_un.b_addr +
9554 9564 bp->b_bcount - bp->b_resid, bp->b_resid);
9555 9565 }
9556 9566 mutex_enter(&rp->r_statelock);
9557 9567 if (bp->b_resid == bp->b_bcount &&
9558 9568 offset >= rp->r_size) {
9559 9569 /*
9560 9570 * We didn't read anything at all as we are
9561 9571 * past EOF. Return an error indicator back
9562 9572 * but don't destroy the pages (yet).
9563 9573 */
9564 9574 error = NFS_EOF;
9565 9575 }
9566 9576 mutex_exit(&rp->r_statelock);
9567 9577 } else if (error == EACCES && last_time == FALSE) {
9568 9578 goto read_again;
9569 9579 }
9570 9580 } else {
9571 9581 if (!(rp->r_flags & R4STALE)) {
9572 9582 write_again:
9573 9583 /*
9574 9584 * Releases the osp, if it is provided.
9575 9585 * Puts a hold on the cred_otw and the new
9576 9586 * osp (if found).
9577 9587 */
9578 9588 cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp,
9579 9589 &first_time, &last_time);
9580 9590 mutex_enter(&rp->r_statelock);
9581 9591 count = MIN(bp->b_bcount, rp->r_size - offset);
9582 9592 mutex_exit(&rp->r_statelock);
9583 9593 if (count < 0)
9584 9594 cmn_err(CE_PANIC, "nfs4_bio: write count < 0");
9585 9595 #ifdef DEBUG
9586 9596 if (count == 0) {
9587 9597 zoneid_t zoneid = getzoneid();
9588 9598
9589 9599 zcmn_err(zoneid, CE_WARN,
9590 9600 "nfs4_bio: zero length write at %lld",
9591 9601 offset);
9592 9602 zcmn_err(zoneid, CE_CONT, "flags=0x%x, "
9593 9603 "b_bcount=%ld, file size=%lld",
9594 9604 rp->r_flags, (long)bp->b_bcount,
9595 9605 rp->r_size);
9596 9606 sfh4_printfhandle(VTOR4(bp->b_vp)->r_fh);
9597 9607 if (nfs4_bio_do_stop)
9598 9608 debug_enter("nfs4_bio");
9599 9609 }
9600 9610 #endif
9601 9611 error = nfs4write(bp->b_vp, bp->b_un.b_addr, offset,
9602 9612 count, cred_otw, stab_comm);
9603 9613 if (error == EACCES && last_time == FALSE) {
9604 9614 crfree(cred_otw);
9605 9615 goto write_again;
9606 9616 }
9607 9617 bp->b_error = error;
9608 9618 if (error && error != EINTR &&
9609 9619 !(bp->b_vp->v_vfsp->vfs_flag & VFS_UNMOUNTED)) {
9610 9620 /*
9611 9621 * Don't print EDQUOT errors on the console.
9612 9622 * Don't print asynchronous EACCES errors.
9613 9623 * Don't print EFBIG errors.
9614 9624 * Print all other write errors.
9615 9625 */
9616 9626 if (error != EDQUOT && error != EFBIG &&
9617 9627 (error != EACCES ||
9618 9628 !(bp->b_flags & B_ASYNC)))
9619 9629 nfs4_write_error(bp->b_vp,
9620 9630 error, cred_otw);
9621 9631 /*
9622 9632 * Update r_error and r_flags as appropriate.
9623 9633 * If the error was ESTALE, then mark the
9624 9634 * rnode as not being writeable and save
9625 9635 * the error status. Otherwise, save any
9626 9636 * errors which occur from asynchronous
9627 9637 * page invalidations. Any errors occurring
9628 9638 * from other operations should be saved
9629 9639 * by the caller.
9630 9640 */
9631 9641 mutex_enter(&rp->r_statelock);
9632 9642 if (error == ESTALE) {
9633 9643 rp->r_flags |= R4STALE;
9634 9644 if (!rp->r_error)
9635 9645 rp->r_error = error;
9636 9646 } else if (!rp->r_error &&
9637 9647 (bp->b_flags &
9638 9648 (B_INVAL|B_FORCE|B_ASYNC)) ==
9639 9649 (B_INVAL|B_FORCE|B_ASYNC)) {
9640 9650 rp->r_error = error;
9641 9651 }
9642 9652 mutex_exit(&rp->r_statelock);
9643 9653 }
9644 9654 crfree(cred_otw);
9645 9655 } else {
9646 9656 error = rp->r_error;
9647 9657 /*
9648 9658 * A close may have cleared r_error, if so,
9649 9659 * propagate ESTALE error return properly
9650 9660 */
9651 9661 if (error == 0)
9652 9662 error = ESTALE;
9653 9663 }
9654 9664 }
9655 9665
9656 9666 if (error != 0 && error != NFS_EOF)
9657 9667 bp->b_flags |= B_ERROR;
9658 9668
9659 9669 if (osp)
9660 9670 open_stream_rele(osp, rp);
9661 9671
9662 9672 DTRACE_IO1(done, struct buf *, bp);
9663 9673
9664 9674 return (error);
9665 9675 }
9666 9676
9667 9677 /* ARGSUSED */
9668 9678 int
9669 9679 nfs4_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
9670 9680 {
9671 9681 return (EREMOTE);
9672 9682 }
9673 9683
9674 9684 /* ARGSUSED2 */
9675 9685 int
9676 9686 nfs4_rwlock(vnode_t *vp, int write_lock, caller_context_t *ctp)
9677 9687 {
9678 9688 rnode4_t *rp = VTOR4(vp);
9679 9689
9680 9690 if (!write_lock) {
9681 9691 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE);
9682 9692 return (V_WRITELOCK_FALSE);
9683 9693 }
9684 9694
9685 9695 if ((rp->r_flags & R4DIRECTIO) ||
9686 9696 (VTOMI4(vp)->mi_flags & MI4_DIRECTIO)) {
9687 9697 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE);
9688 9698 if (rp->r_mapcnt == 0 && !nfs4_has_pages(vp))
9689 9699 return (V_WRITELOCK_FALSE);
9690 9700 nfs_rw_exit(&rp->r_rwlock);
9691 9701 }
9692 9702
9693 9703 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, FALSE);
9694 9704 return (V_WRITELOCK_TRUE);
9695 9705 }
9696 9706
9697 9707 /* ARGSUSED */
9698 9708 void
9699 9709 nfs4_rwunlock(vnode_t *vp, int write_lock, caller_context_t *ctp)
9700 9710 {
9701 9711 rnode4_t *rp = VTOR4(vp);
9702 9712
9703 9713 nfs_rw_exit(&rp->r_rwlock);
9704 9714 }
9705 9715
9706 9716 /* ARGSUSED */
9707 9717 static int
9708 9718 nfs4_seek(vnode_t *vp, offset_t ooff, offset_t *noffp, caller_context_t *ct)
9709 9719 {
9710 9720 if (nfs_zone() != VTOMI4(vp)->mi_zone)
9711 9721 return (EIO);
9712 9722
9713 9723 /*
9714 9724 * Because we stuff the readdir cookie into the offset field
9715 9725 * someone may attempt to do an lseek with the cookie which
9716 9726 * we want to succeed.
9717 9727 */
9718 9728 if (vp->v_type == VDIR)
9719 9729 return (0);
9720 9730 if (*noffp < 0)
9721 9731 return (EINVAL);
9722 9732 return (0);
9723 9733 }
9724 9734
9725 9735
9726 9736 /*
9727 9737 * Return all the pages from [off..off+len) in file
9728 9738 */
9729 9739 /* ARGSUSED */
9730 9740 static int
9731 9741 nfs4_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
9732 9742 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
9733 9743 enum seg_rw rw, cred_t *cr, caller_context_t *ct)
9734 9744 {
9735 9745 rnode4_t *rp;
9736 9746 int error;
9737 9747 mntinfo4_t *mi;
9738 9748
9739 9749 if (nfs_zone() != VTOMI4(vp)->mi_zone)
9740 9750 return (EIO);
9741 9751 rp = VTOR4(vp);
9742 9752 if (IS_SHADOW(vp, rp))
9743 9753 vp = RTOV4(rp);
9744 9754
9745 9755 if (vp->v_flag & VNOMAP)
9746 9756 return (ENOSYS);
9747 9757
9748 9758 if (protp != NULL)
9749 9759 *protp = PROT_ALL;
9750 9760
9751 9761 /*
9752 9762 * Now validate that the caches are up to date.
9753 9763 */
9754 9764 if (error = nfs4_validate_caches(vp, cr))
9755 9765 return (error);
9756 9766
9757 9767 mi = VTOMI4(vp);
9758 9768 retry:
9759 9769 mutex_enter(&rp->r_statelock);
9760 9770
9761 9771 /*
9762 9772 * Don't create dirty pages faster than they
9763 9773 * can be cleaned so that the system doesn't
9764 9774 * get imbalanced. If the async queue is
9765 9775 * maxed out, then wait for it to drain before
9766 9776 * creating more dirty pages. Also, wait for
9767 9777 * any threads doing pagewalks in the vop_getattr
9768 9778 * entry points so that they don't block for
9769 9779 * long periods.
9770 9780 */
9771 9781 if (rw == S_CREATE) {
9772 9782 while ((mi->mi_max_threads != 0 &&
9773 9783 rp->r_awcount > 2 * mi->mi_max_threads) ||
9774 9784 rp->r_gcount > 0)
9775 9785 cv_wait(&rp->r_cv, &rp->r_statelock);
9776 9786 }
9777 9787
9778 9788 /*
9779 9789 * If we are getting called as a side effect of an nfs_write()
9780 9790 * operation the local file size might not be extended yet.
9781 9791 * In this case we want to be able to return pages of zeroes.
9782 9792 */
9783 9793 if (off + len > rp->r_size + PAGEOFFSET && seg != segkmap) {
9784 9794 NFS4_DEBUG(nfs4_pageio_debug,
9785 9795 (CE_NOTE, "getpage beyond EOF: off=%lld, "
9786 9796 "len=%llu, size=%llu, attrsize =%llu", off,
9787 9797 (u_longlong_t)len, rp->r_size, rp->r_attr.va_size));
9788 9798 mutex_exit(&rp->r_statelock);
9789 9799 return (EFAULT); /* beyond EOF */
9790 9800 }
9791 9801
9792 9802 mutex_exit(&rp->r_statelock);
9793 9803
9794 9804 error = pvn_getpages(nfs4_getapage, vp, off, len, protp,
9795 9805 pl, plsz, seg, addr, rw, cr);
9796 9806 NFS4_DEBUG(nfs4_pageio_debug && error,
9797 9807 (CE_NOTE, "getpages error %d; off=%lld, len=%lld",
9798 9808 error, off, (u_longlong_t)len));
9799 9809
9800 9810 switch (error) {
9801 9811 case NFS_EOF:
9802 9812 nfs4_purge_caches(vp, NFS4_NOPURGE_DNLC, cr, FALSE);
9803 9813 goto retry;
9804 9814 case ESTALE:
9805 9815 nfs4_purge_stale_fh(error, vp, cr);
9806 9816 }
9807 9817
9808 9818 return (error);
9809 9819 }
9810 9820
9811 9821 /*
9812 9822 * Called from pvn_getpages to get a particular page.
9813 9823 */
9814 9824 /* ARGSUSED */
9815 9825 static int
9816 9826 nfs4_getapage(vnode_t *vp, u_offset_t off, size_t len, uint_t *protp,
9817 9827 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
9818 9828 enum seg_rw rw, cred_t *cr)
9819 9829 {
9820 9830 rnode4_t *rp;
9821 9831 uint_t bsize;
9822 9832 struct buf *bp;
9823 9833 page_t *pp;
9824 9834 u_offset_t lbn;
9825 9835 u_offset_t io_off;
9826 9836 u_offset_t blkoff;
9827 9837 u_offset_t rablkoff;
9828 9838 size_t io_len;
9829 9839 uint_t blksize;
9830 9840 int error;
9831 9841 int readahead;
9832 9842 int readahead_issued = 0;
9833 9843 int ra_window; /* readahead window */
9834 9844 page_t *pagefound;
9835 9845 page_t *savepp;
9836 9846
9837 9847 if (nfs_zone() != VTOMI4(vp)->mi_zone)
9838 9848 return (EIO);
9839 9849
9840 9850 rp = VTOR4(vp);
9841 9851 ASSERT(!IS_SHADOW(vp, rp));
9842 9852 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE);
9843 9853
9844 9854 reread:
9845 9855 bp = NULL;
9846 9856 pp = NULL;
9847 9857 pagefound = NULL;
9848 9858
9849 9859 if (pl != NULL)
9850 9860 pl[0] = NULL;
9851 9861
9852 9862 error = 0;
9853 9863 lbn = off / bsize;
9854 9864 blkoff = lbn * bsize;
9855 9865
9856 9866 /*
9857 9867 * Queueing up the readahead before doing the synchronous read
9858 9868 * results in a significant increase in read throughput because
9859 9869 * of the increased parallelism between the async threads and
9860 9870 * the process context.
9861 9871 */
9862 9872 if ((off & ((vp->v_vfsp->vfs_bsize) - 1)) == 0 &&
9863 9873 rw != S_CREATE &&
9864 9874 !(vp->v_flag & VNOCACHE)) {
9865 9875 mutex_enter(&rp->r_statelock);
9866 9876
9867 9877 /*
9868 9878 * Calculate the number of readaheads to do.
9869 9879 * a) No readaheads at offset = 0.
9870 9880 * b) Do maximum(nfs4_nra) readaheads when the readahead
9871 9881 * window is closed.
9872 9882 * c) Do readaheads between 1 to (nfs4_nra - 1) depending
9873 9883 * upon how far the readahead window is open or close.
9874 9884 * d) No readaheads if rp->r_nextr is not within the scope
9875 9885 * of the readahead window (random i/o).
9876 9886 */
9877 9887
9878 9888 if (off == 0)
9879 9889 readahead = 0;
9880 9890 else if (blkoff == rp->r_nextr)
9881 9891 readahead = nfs4_nra;
9882 9892 else if (rp->r_nextr > blkoff &&
9883 9893 ((ra_window = (rp->r_nextr - blkoff) / bsize)
9884 9894 <= (nfs4_nra - 1)))
9885 9895 readahead = nfs4_nra - ra_window;
9886 9896 else
9887 9897 readahead = 0;
9888 9898
9889 9899 rablkoff = rp->r_nextr;
9890 9900 while (readahead > 0 && rablkoff + bsize < rp->r_size) {
9891 9901 mutex_exit(&rp->r_statelock);
9892 9902 if (nfs4_async_readahead(vp, rablkoff + bsize,
9893 9903 addr + (rablkoff + bsize - off),
9894 9904 seg, cr, nfs4_readahead) < 0) {
9895 9905 mutex_enter(&rp->r_statelock);
9896 9906 break;
9897 9907 }
9898 9908 readahead--;
9899 9909 rablkoff += bsize;
9900 9910 /*
9901 9911 * Indicate that we did a readahead so
9902 9912 * readahead offset is not updated
9903 9913 * by the synchronous read below.
9904 9914 */
9905 9915 readahead_issued = 1;
9906 9916 mutex_enter(&rp->r_statelock);
9907 9917 /*
9908 9918 * set readahead offset to
9909 9919 * offset of last async readahead
9910 9920 * request.
9911 9921 */
9912 9922 rp->r_nextr = rablkoff;
9913 9923 }
9914 9924 mutex_exit(&rp->r_statelock);
9915 9925 }
9916 9926
9917 9927 again:
9918 9928 if ((pagefound = page_exists(vp, off)) == NULL) {
9919 9929 if (pl == NULL) {
9920 9930 (void) nfs4_async_readahead(vp, blkoff, addr, seg, cr,
9921 9931 nfs4_readahead);
9922 9932 } else if (rw == S_CREATE) {
9923 9933 /*
9924 9934 * Block for this page is not allocated, or the offset
9925 9935 * is beyond the current allocation size, or we're
9926 9936 * allocating a swap slot and the page was not found,
9927 9937 * so allocate it and return a zero page.
9928 9938 */
9929 9939 if ((pp = page_create_va(vp, off,
9930 9940 PAGESIZE, PG_WAIT, seg, addr)) == NULL)
9931 9941 cmn_err(CE_PANIC, "nfs4_getapage: page_create");
9932 9942 io_len = PAGESIZE;
9933 9943 mutex_enter(&rp->r_statelock);
9934 9944 rp->r_nextr = off + PAGESIZE;
9935 9945 mutex_exit(&rp->r_statelock);
9936 9946 } else {
9937 9947 /*
9938 9948 * Need to go to server to get a block
9939 9949 */
9940 9950 mutex_enter(&rp->r_statelock);
9941 9951 if (blkoff < rp->r_size &&
9942 9952 blkoff + bsize > rp->r_size) {
9943 9953 /*
9944 9954 * If less than a block left in
9945 9955 * file read less than a block.
9946 9956 */
9947 9957 if (rp->r_size <= off) {
9948 9958 /*
9949 9959 * Trying to access beyond EOF,
9950 9960 * set up to get at least one page.
9951 9961 */
9952 9962 blksize = off + PAGESIZE - blkoff;
9953 9963 } else
9954 9964 blksize = rp->r_size - blkoff;
9955 9965 } else if ((off == 0) ||
9956 9966 (off != rp->r_nextr && !readahead_issued)) {
9957 9967 blksize = PAGESIZE;
9958 9968 blkoff = off; /* block = page here */
9959 9969 } else
9960 9970 blksize = bsize;
9961 9971 mutex_exit(&rp->r_statelock);
9962 9972
9963 9973 pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
9964 9974 &io_len, blkoff, blksize, 0);
9965 9975
9966 9976 /*
9967 9977 * Some other thread has entered the page,
9968 9978 * so just use it.
9969 9979 */
9970 9980 if (pp == NULL)
9971 9981 goto again;
9972 9982
9973 9983 /*
9974 9984 * Now round the request size up to page boundaries.
9975 9985 * This ensures that the entire page will be
9976 9986 * initialized to zeroes if EOF is encountered.
9977 9987 */
9978 9988 io_len = ptob(btopr(io_len));
9979 9989
9980 9990 bp = pageio_setup(pp, io_len, vp, B_READ);
9981 9991 ASSERT(bp != NULL);
9982 9992
9983 9993 /*
9984 9994 * pageio_setup should have set b_addr to 0. This
9985 9995 * is correct since we want to do I/O on a page
9986 9996 * boundary. bp_mapin will use this addr to calculate
9987 9997 * an offset, and then set b_addr to the kernel virtual
9988 9998 * address it allocated for us.
9989 9999 */
9990 10000 ASSERT(bp->b_un.b_addr == 0);
9991 10001
9992 10002 bp->b_edev = 0;
9993 10003 bp->b_dev = 0;
9994 10004 bp->b_lblkno = lbtodb(io_off);
9995 10005 bp->b_file = vp;
9996 10006 bp->b_offset = (offset_t)off;
9997 10007 bp_mapin(bp);
9998 10008
9999 10009 /*
10000 10010 * If doing a write beyond what we believe is EOF,
10001 10011 * don't bother trying to read the pages from the
10002 10012 * server, we'll just zero the pages here. We
10003 10013 * don't check that the rw flag is S_WRITE here
10004 10014 * because some implementations may attempt a
10005 10015 * read access to the buffer before copying data.
10006 10016 */
10007 10017 mutex_enter(&rp->r_statelock);
10008 10018 if (io_off >= rp->r_size && seg == segkmap) {
10009 10019 mutex_exit(&rp->r_statelock);
10010 10020 bzero(bp->b_un.b_addr, io_len);
10011 10021 } else {
10012 10022 mutex_exit(&rp->r_statelock);
10013 10023 error = nfs4_bio(bp, NULL, cr, FALSE);
10014 10024 }
10015 10025
10016 10026 /*
10017 10027 * Unmap the buffer before freeing it.
10018 10028 */
10019 10029 bp_mapout(bp);
10020 10030 pageio_done(bp);
10021 10031
10022 10032 savepp = pp;
10023 10033 do {
10024 10034 pp->p_fsdata = C_NOCOMMIT;
10025 10035 } while ((pp = pp->p_next) != savepp);
10026 10036
10027 10037 if (error == NFS_EOF) {
10028 10038 /*
10029 10039 * If doing a write system call just return
10030 10040 * zeroed pages, else user tried to get pages
10031 10041 * beyond EOF, return error. We don't check
10032 10042 * that the rw flag is S_WRITE here because
10033 10043 * some implementations may attempt a read
10034 10044 * access to the buffer before copying data.
10035 10045 */
10036 10046 if (seg == segkmap)
10037 10047 error = 0;
10038 10048 else
10039 10049 error = EFAULT;
10040 10050 }
10041 10051
10042 10052 if (!readahead_issued && !error) {
10043 10053 mutex_enter(&rp->r_statelock);
10044 10054 rp->r_nextr = io_off + io_len;
10045 10055 mutex_exit(&rp->r_statelock);
10046 10056 }
10047 10057 }
10048 10058 }
10049 10059
10050 10060 out:
10051 10061 if (pl == NULL)
10052 10062 return (error);
10053 10063
10054 10064 if (error) {
10055 10065 if (pp != NULL)
10056 10066 pvn_read_done(pp, B_ERROR);
10057 10067 return (error);
10058 10068 }
10059 10069
10060 10070 if (pagefound) {
10061 10071 se_t se = (rw == S_CREATE ? SE_EXCL : SE_SHARED);
10062 10072
10063 10073 /*
10064 10074 * Page exists in the cache, acquire the appropriate lock.
10065 10075 * If this fails, start all over again.
10066 10076 */
10067 10077 if ((pp = page_lookup(vp, off, se)) == NULL) {
10068 10078 #ifdef DEBUG
10069 10079 nfs4_lostpage++;
10070 10080 #endif
10071 10081 goto reread;
10072 10082 }
10073 10083 pl[0] = pp;
10074 10084 pl[1] = NULL;
10075 10085 return (0);
10076 10086 }
10077 10087
10078 10088 if (pp != NULL)
10079 10089 pvn_plist_init(pp, pl, plsz, off, io_len, rw);
10080 10090
10081 10091 return (error);
10082 10092 }
10083 10093
10084 10094 static void
10085 10095 nfs4_readahead(vnode_t *vp, u_offset_t blkoff, caddr_t addr, struct seg *seg,
10086 10096 cred_t *cr)
10087 10097 {
10088 10098 int error;
10089 10099 page_t *pp;
10090 10100 u_offset_t io_off;
10091 10101 size_t io_len;
10092 10102 struct buf *bp;
10093 10103 uint_t bsize, blksize;
10094 10104 rnode4_t *rp = VTOR4(vp);
10095 10105 page_t *savepp;
10096 10106
10097 10107 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
10098 10108
10099 10109 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE);
10100 10110
10101 10111 mutex_enter(&rp->r_statelock);
10102 10112 if (blkoff < rp->r_size && blkoff + bsize > rp->r_size) {
10103 10113 /*
10104 10114 * If less than a block left in file read less
10105 10115 * than a block.
10106 10116 */
10107 10117 blksize = rp->r_size - blkoff;
10108 10118 } else
10109 10119 blksize = bsize;
10110 10120 mutex_exit(&rp->r_statelock);
10111 10121
10112 10122 pp = pvn_read_kluster(vp, blkoff, segkmap, addr,
10113 10123 &io_off, &io_len, blkoff, blksize, 1);
10114 10124 /*
10115 10125 * The isra flag passed to the kluster function is 1, we may have
10116 10126 * gotten a return value of NULL for a variety of reasons (# of free
10117 10127 * pages < minfree, someone entered the page on the vnode etc). In all
10118 10128 * cases, we want to punt on the readahead.
10119 10129 */
10120 10130 if (pp == NULL)
10121 10131 return;
10122 10132
10123 10133 /*
10124 10134 * Now round the request size up to page boundaries.
10125 10135 * This ensures that the entire page will be
10126 10136 * initialized to zeroes if EOF is encountered.
10127 10137 */
10128 10138 io_len = ptob(btopr(io_len));
10129 10139
10130 10140 bp = pageio_setup(pp, io_len, vp, B_READ);
10131 10141 ASSERT(bp != NULL);
10132 10142
10133 10143 /*
10134 10144 * pageio_setup should have set b_addr to 0. This is correct since
10135 10145 * we want to do I/O on a page boundary. bp_mapin() will use this addr
10136 10146 * to calculate an offset, and then set b_addr to the kernel virtual
10137 10147 * address it allocated for us.
10138 10148 */
10139 10149 ASSERT(bp->b_un.b_addr == 0);
10140 10150
10141 10151 bp->b_edev = 0;
10142 10152 bp->b_dev = 0;
10143 10153 bp->b_lblkno = lbtodb(io_off);
10144 10154 bp->b_file = vp;
10145 10155 bp->b_offset = (offset_t)blkoff;
10146 10156 bp_mapin(bp);
10147 10157
10148 10158 /*
10149 10159 * If doing a write beyond what we believe is EOF, don't bother trying
10150 10160 * to read the pages from the server, we'll just zero the pages here.
10151 10161 * We don't check that the rw flag is S_WRITE here because some
10152 10162 * implementations may attempt a read access to the buffer before
10153 10163 * copying data.
10154 10164 */
10155 10165 mutex_enter(&rp->r_statelock);
10156 10166 if (io_off >= rp->r_size && seg == segkmap) {
10157 10167 mutex_exit(&rp->r_statelock);
10158 10168 bzero(bp->b_un.b_addr, io_len);
10159 10169 error = 0;
10160 10170 } else {
10161 10171 mutex_exit(&rp->r_statelock);
10162 10172 error = nfs4_bio(bp, NULL, cr, TRUE);
10163 10173 if (error == NFS_EOF)
10164 10174 error = 0;
10165 10175 }
10166 10176
10167 10177 /*
10168 10178 * Unmap the buffer before freeing it.
10169 10179 */
10170 10180 bp_mapout(bp);
10171 10181 pageio_done(bp);
10172 10182
10173 10183 savepp = pp;
10174 10184 do {
10175 10185 pp->p_fsdata = C_NOCOMMIT;
10176 10186 } while ((pp = pp->p_next) != savepp);
10177 10187
10178 10188 pvn_read_done(pp, error ? B_READ | B_ERROR : B_READ);
10179 10189
10180 10190 /*
10181 10191 * In case of error set readahead offset
10182 10192 * to the lowest offset.
10183 10193 * pvn_read_done() calls VN_DISPOSE to destroy the pages
10184 10194 */
10185 10195 if (error && rp->r_nextr > io_off) {
10186 10196 mutex_enter(&rp->r_statelock);
10187 10197 if (rp->r_nextr > io_off)
10188 10198 rp->r_nextr = io_off;
10189 10199 mutex_exit(&rp->r_statelock);
10190 10200 }
10191 10201 }
10192 10202
10193 10203 /*
10194 10204 * Flags are composed of {B_INVAL, B_FREE, B_DONTNEED, B_FORCE}
10195 10205 * If len == 0, do from off to EOF.
10196 10206 *
10197 10207 * The normal cases should be len == 0 && off == 0 (entire vp list) or
10198 10208 * len == MAXBSIZE (from segmap_release actions), and len == PAGESIZE
10199 10209 * (from pageout).
10200 10210 */
10201 10211 /* ARGSUSED */
10202 10212 static int
10203 10213 nfs4_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr,
10204 10214 caller_context_t *ct)
10205 10215 {
10206 10216 int error;
10207 10217 rnode4_t *rp;
10208 10218
10209 10219 ASSERT(cr != NULL);
10210 10220
10211 10221 if (!(flags & B_ASYNC) && nfs_zone() != VTOMI4(vp)->mi_zone)
10212 10222 return (EIO);
10213 10223
10214 10224 rp = VTOR4(vp);
10215 10225 if (IS_SHADOW(vp, rp))
10216 10226 vp = RTOV4(rp);
10217 10227
10218 10228 /*
10219 10229 * XXX - Why should this check be made here?
10220 10230 */
10221 10231 if (vp->v_flag & VNOMAP)
10222 10232 return (ENOSYS);
10223 10233
10224 10234 if (len == 0 && !(flags & B_INVAL) &&
10225 10235 (vp->v_vfsp->vfs_flag & VFS_RDONLY))
10226 10236 return (0);
10227 10237
10228 10238 mutex_enter(&rp->r_statelock);
10229 10239 rp->r_count++;
10230 10240 mutex_exit(&rp->r_statelock);
10231 10241 error = nfs4_putpages(vp, off, len, flags, cr);
10232 10242 mutex_enter(&rp->r_statelock);
10233 10243 rp->r_count--;
10234 10244 cv_broadcast(&rp->r_cv);
10235 10245 mutex_exit(&rp->r_statelock);
10236 10246
10237 10247 return (error);
10238 10248 }
10239 10249
10240 10250 /*
10241 10251 * Write out a single page, possibly klustering adjacent dirty pages.
10242 10252 */
10243 10253 int
10244 10254 nfs4_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, size_t *lenp,
10245 10255 int flags, cred_t *cr)
10246 10256 {
10247 10257 u_offset_t io_off;
10248 10258 u_offset_t lbn_off;
10249 10259 u_offset_t lbn;
10250 10260 size_t io_len;
10251 10261 uint_t bsize;
10252 10262 int error;
10253 10263 rnode4_t *rp;
10254 10264
10255 10265 ASSERT(!(vp->v_vfsp->vfs_flag & VFS_RDONLY));
10256 10266 ASSERT(pp != NULL);
10257 10267 ASSERT(cr != NULL);
10258 10268 ASSERT((flags & B_ASYNC) || nfs_zone() == VTOMI4(vp)->mi_zone);
10259 10269
10260 10270 rp = VTOR4(vp);
10261 10271 ASSERT(rp->r_count > 0);
10262 10272 ASSERT(!IS_SHADOW(vp, rp));
10263 10273
10264 10274 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE);
10265 10275 lbn = pp->p_offset / bsize;
10266 10276 lbn_off = lbn * bsize;
10267 10277
10268 10278 /*
10269 10279 * Find a kluster that fits in one block, or in
10270 10280 * one page if pages are bigger than blocks. If
10271 10281 * there is less file space allocated than a whole
10272 10282 * page, we'll shorten the i/o request below.
10273 10283 */
10274 10284 pp = pvn_write_kluster(vp, pp, &io_off, &io_len, lbn_off,
10275 10285 roundup(bsize, PAGESIZE), flags);
10276 10286
10277 10287 /*
10278 10288 * pvn_write_kluster shouldn't have returned a page with offset
10279 10289 * behind the original page we were given. Verify that.
10280 10290 */
10281 10291 ASSERT((pp->p_offset / bsize) >= lbn);
10282 10292
10283 10293 /*
10284 10294 * Now pp will have the list of kept dirty pages marked for
10285 10295 * write back. It will also handle invalidation and freeing
10286 10296 * of pages that are not dirty. Check for page length rounding
10287 10297 * problems.
10288 10298 */
10289 10299 if (io_off + io_len > lbn_off + bsize) {
10290 10300 ASSERT((io_off + io_len) - (lbn_off + bsize) < PAGESIZE);
10291 10301 io_len = lbn_off + bsize - io_off;
10292 10302 }
10293 10303 /*
10294 10304 * The R4MODINPROGRESS flag makes sure that nfs4_bio() sees a
10295 10305 * consistent value of r_size. R4MODINPROGRESS is set in writerp4().
10296 10306 * When R4MODINPROGRESS is set it indicates that a uiomove() is in
10297 10307 * progress and the r_size has not been made consistent with the
10298 10308 * new size of the file. When the uiomove() completes the r_size is
10299 10309 * updated and the R4MODINPROGRESS flag is cleared.
10300 10310 *
10301 10311 * The R4MODINPROGRESS flag makes sure that nfs4_bio() sees a
10302 10312 * consistent value of r_size. Without this handshaking, it is
10303 10313 * possible that nfs4_bio() picks up the old value of r_size
10304 10314 * before the uiomove() in writerp4() completes. This will result
10305 10315 * in the write through nfs4_bio() being dropped.
10306 10316 *
10307 10317 * More precisely, there is a window between the time the uiomove()
10308 10318 * completes and the time the r_size is updated. If a VOP_PUTPAGE()
10309 10319 * operation intervenes in this window, the page will be picked up,
10310 10320 * because it is dirty (it will be unlocked, unless it was
10311 10321 * pagecreate'd). When the page is picked up as dirty, the dirty
10312 10322 * bit is reset (pvn_getdirty()). In nfs4write(), r_size is
10313 10323 * checked. This will still be the old size. Therefore the page will
10314 10324 * not be written out. When segmap_release() calls VOP_PUTPAGE(),
10315 10325 * the page will be found to be clean and the write will be dropped.
10316 10326 */
10317 10327 if (rp->r_flags & R4MODINPROGRESS) {
10318 10328 mutex_enter(&rp->r_statelock);
10319 10329 if ((rp->r_flags & R4MODINPROGRESS) &&
10320 10330 rp->r_modaddr + MAXBSIZE > io_off &&
10321 10331 rp->r_modaddr < io_off + io_len) {
10322 10332 page_t *plist;
10323 10333 /*
10324 10334 * A write is in progress for this region of the file.
10325 10335 * If we did not detect R4MODINPROGRESS here then this
10326 10336 * path through nfs_putapage() would eventually go to
10327 10337 * nfs4_bio() and may not write out all of the data
10328 10338 * in the pages. We end up losing data. So we decide
10329 10339 * to set the modified bit on each page in the page
10330 10340 * list and mark the rnode with R4DIRTY. This write
10331 10341 * will be restarted at some later time.
10332 10342 */
10333 10343 plist = pp;
10334 10344 while (plist != NULL) {
10335 10345 pp = plist;
10336 10346 page_sub(&plist, pp);
10337 10347 hat_setmod(pp);
10338 10348 page_io_unlock(pp);
10339 10349 page_unlock(pp);
10340 10350 }
10341 10351 rp->r_flags |= R4DIRTY;
10342 10352 mutex_exit(&rp->r_statelock);
10343 10353 if (offp)
10344 10354 *offp = io_off;
10345 10355 if (lenp)
10346 10356 *lenp = io_len;
10347 10357 return (0);
10348 10358 }
10349 10359 mutex_exit(&rp->r_statelock);
10350 10360 }
10351 10361
10352 10362 if (flags & B_ASYNC) {
10353 10363 error = nfs4_async_putapage(vp, pp, io_off, io_len, flags, cr,
10354 10364 nfs4_sync_putapage);
10355 10365 } else
10356 10366 error = nfs4_sync_putapage(vp, pp, io_off, io_len, flags, cr);
10357 10367
10358 10368 if (offp)
10359 10369 *offp = io_off;
10360 10370 if (lenp)
10361 10371 *lenp = io_len;
10362 10372 return (error);
10363 10373 }
10364 10374
10365 10375 static int
10366 10376 nfs4_sync_putapage(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len,
10367 10377 int flags, cred_t *cr)
10368 10378 {
10369 10379 int error;
10370 10380 rnode4_t *rp;
10371 10381
10372 10382 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
10373 10383
10374 10384 flags |= B_WRITE;
10375 10385
10376 10386 error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr);
10377 10387
10378 10388 rp = VTOR4(vp);
10379 10389
10380 10390 if ((error == ENOSPC || error == EDQUOT || error == EFBIG ||
10381 10391 error == EACCES) &&
10382 10392 (flags & (B_INVAL|B_FORCE)) != (B_INVAL|B_FORCE)) {
10383 10393 if (!(rp->r_flags & R4OUTOFSPACE)) {
10384 10394 mutex_enter(&rp->r_statelock);
10385 10395 rp->r_flags |= R4OUTOFSPACE;
10386 10396 mutex_exit(&rp->r_statelock);
10387 10397 }
10388 10398 flags |= B_ERROR;
10389 10399 pvn_write_done(pp, flags);
10390 10400 /*
10391 10401 * If this was not an async thread, then try again to
10392 10402 * write out the pages, but this time, also destroy
10393 10403 * them whether or not the write is successful. This
10394 10404 * will prevent memory from filling up with these
10395 10405 * pages and destroying them is the only alternative
10396 10406 * if they can't be written out.
10397 10407 *
10398 10408 * Don't do this if this is an async thread because
10399 10409 * when the pages are unlocked in pvn_write_done,
10400 10410 * some other thread could have come along, locked
10401 10411 * them, and queued for an async thread. It would be
10402 10412 * possible for all of the async threads to be tied
10403 10413 * up waiting to lock the pages again and they would
10404 10414 * all already be locked and waiting for an async
10405 10415 * thread to handle them. Deadlock.
10406 10416 */
10407 10417 if (!(flags & B_ASYNC)) {
10408 10418 error = nfs4_putpage(vp, io_off, io_len,
10409 10419 B_INVAL | B_FORCE, cr, NULL);
10410 10420 }
10411 10421 } else {
10412 10422 if (error)
10413 10423 flags |= B_ERROR;
10414 10424 else if (rp->r_flags & R4OUTOFSPACE) {
10415 10425 mutex_enter(&rp->r_statelock);
10416 10426 rp->r_flags &= ~R4OUTOFSPACE;
10417 10427 mutex_exit(&rp->r_statelock);
10418 10428 }
10419 10429 pvn_write_done(pp, flags);
10420 10430 if (freemem < desfree)
10421 10431 (void) nfs4_commit_vp(vp, (u_offset_t)0, 0, cr,
10422 10432 NFS4_WRITE_NOWAIT);
10423 10433 }
10424 10434
10425 10435 return (error);
10426 10436 }
10427 10437
10428 10438 #ifdef DEBUG
10429 10439 int nfs4_force_open_before_mmap = 0;
10430 10440 #endif
10431 10441
10432 10442 /* ARGSUSED */
10433 10443 static int
10434 10444 nfs4_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
10435 10445 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
10436 10446 caller_context_t *ct)
10437 10447 {
10438 10448 struct segvn_crargs vn_a;
10439 10449 int error = 0;
10440 10450 rnode4_t *rp = VTOR4(vp);
10441 10451 mntinfo4_t *mi = VTOMI4(vp);
10442 10452
10443 10453 if (nfs_zone() != VTOMI4(vp)->mi_zone)
10444 10454 return (EIO);
10445 10455
10446 10456 if (vp->v_flag & VNOMAP)
10447 10457 return (ENOSYS);
10448 10458
10449 10459 if (off < 0 || (off + len) < 0)
10450 10460 return (ENXIO);
10451 10461
10452 10462 if (vp->v_type != VREG)
10453 10463 return (ENODEV);
10454 10464
10455 10465 /*
10456 10466 * If the file is delegated to the client don't do anything.
10457 10467 * If the file is not delegated, then validate the data cache.
10458 10468 */
10459 10469 mutex_enter(&rp->r_statev4_lock);
10460 10470 if (rp->r_deleg_type == OPEN_DELEGATE_NONE) {
10461 10471 mutex_exit(&rp->r_statev4_lock);
10462 10472 error = nfs4_validate_caches(vp, cr);
10463 10473 if (error)
10464 10474 return (error);
10465 10475 } else {
10466 10476 mutex_exit(&rp->r_statev4_lock);
10467 10477 }
10468 10478
10469 10479 /*
10470 10480 * Check to see if the vnode is currently marked as not cachable.
10471 10481 * This means portions of the file are locked (through VOP_FRLOCK).
10472 10482 * In this case the map request must be refused. We use
10473 10483 * rp->r_lkserlock to avoid a race with concurrent lock requests.
10474 10484 *
10475 10485 * Atomically increment r_inmap after acquiring r_rwlock. The
10476 10486 * idea here is to acquire r_rwlock to block read/write and
10477 10487 * not to protect r_inmap. r_inmap will inform nfs4_read/write()
10478 10488 * that we are in nfs4_map(). Now, r_rwlock is acquired in order
10479 10489 * and we can prevent the deadlock that would have occurred
10480 10490 * when nfs4_addmap() would have acquired it out of order.
10481 10491 *
10482 10492 * Since we are not protecting r_inmap by any lock, we do not
10483 10493 * hold any lock when we decrement it. We atomically decrement
10484 10494 * r_inmap after we release r_lkserlock.
10485 10495 */
10486 10496
10487 10497 if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, INTR4(vp)))
10488 10498 return (EINTR);
10489 10499 atomic_inc_uint(&rp->r_inmap);
10490 10500 nfs_rw_exit(&rp->r_rwlock);
10491 10501
10492 10502 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR4(vp))) {
10493 10503 atomic_dec_uint(&rp->r_inmap);
10494 10504 return (EINTR);
10495 10505 }
10496 10506
10497 10507
10498 10508 if (vp->v_flag & VNOCACHE) {
10499 10509 error = EAGAIN;
10500 10510 goto done;
10501 10511 }
10502 10512
10503 10513 /*
10504 10514 * Don't allow concurrent locks and mapping if mandatory locking is
10505 10515 * enabled.
10506 10516 */
10507 10517 if (flk_has_remote_locks(vp)) {
10508 10518 struct vattr va;
10509 10519 va.va_mask = AT_MODE;
10510 10520 error = nfs4getattr(vp, &va, cr);
10511 10521 if (error != 0)
10512 10522 goto done;
10513 10523 if (MANDLOCK(vp, va.va_mode)) {
10514 10524 error = EAGAIN;
10515 10525 goto done;
10516 10526 }
10517 10527 }
10518 10528
10519 10529 /*
10520 10530 * It is possible that the rnode has a lost lock request that we
10521 10531 * are still trying to recover, and that the request conflicts with
10522 10532 * this map request.
10523 10533 *
10524 10534 * An alternative approach would be for nfs4_safemap() to consider
10525 10535 * queued lock requests when deciding whether to set or clear
10526 10536 * VNOCACHE. This would require the frlock code path to call
10527 10537 * nfs4_safemap() after enqueing a lost request.
10528 10538 */
10529 10539 if (nfs4_map_lost_lock_conflict(vp)) {
10530 10540 error = EAGAIN;
10531 10541 goto done;
10532 10542 }
10533 10543
10534 10544 as_rangelock(as);
10535 10545 error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
10536 10546 if (error != 0) {
10537 10547 as_rangeunlock(as);
10538 10548 goto done;
10539 10549 }
10540 10550
10541 10551 if (vp->v_type == VREG) {
10542 10552 /*
10543 10553 * We need to retrieve the open stream
10544 10554 */
10545 10555 nfs4_open_stream_t *osp = NULL;
10546 10556 nfs4_open_owner_t *oop = NULL;
10547 10557
10548 10558 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi);
10549 10559 if (oop != NULL) {
10550 10560 /* returns with 'os_sync_lock' held */
10551 10561 osp = find_open_stream(oop, rp);
10552 10562 open_owner_rele(oop);
10553 10563 }
10554 10564 if (osp == NULL) {
10555 10565 #ifdef DEBUG
10556 10566 if (nfs4_force_open_before_mmap) {
10557 10567 error = EIO;
10558 10568 goto done;
10559 10569 }
10560 10570 #endif
10561 10571 /* returns with 'os_sync_lock' held */
10562 10572 error = open_and_get_osp(vp, cr, &osp);
10563 10573 if (osp == NULL) {
10564 10574 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE,
10565 10575 "nfs4_map: we tried to OPEN the file "
10566 10576 "but again no osp, so fail with EIO"));
10567 10577 goto done;
10568 10578 }
10569 10579 }
10570 10580
10571 10581 if (osp->os_failed_reopen) {
10572 10582 mutex_exit(&osp->os_sync_lock);
10573 10583 open_stream_rele(osp, rp);
10574 10584 NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE,
10575 10585 "nfs4_map: os_failed_reopen set on "
10576 10586 "osp %p, cr %p, rp %s", (void *)osp,
10577 10587 (void *)cr, rnode4info(rp)));
10578 10588 error = EIO;
10579 10589 goto done;
10580 10590 }
10581 10591 mutex_exit(&osp->os_sync_lock);
10582 10592 open_stream_rele(osp, rp);
10583 10593 }
10584 10594
10585 10595 vn_a.vp = vp;
10586 10596 vn_a.offset = off;
10587 10597 vn_a.type = (flags & MAP_TYPE);
10588 10598 vn_a.prot = (uchar_t)prot;
10589 10599 vn_a.maxprot = (uchar_t)maxprot;
10590 10600 vn_a.flags = (flags & ~MAP_TYPE);
10591 10601 vn_a.cred = cr;
10592 10602 vn_a.amp = NULL;
10593 10603 vn_a.szc = 0;
10594 10604 vn_a.lgrp_mem_policy_flags = 0;
10595 10605
10596 10606 error = as_map(as, *addrp, len, segvn_create, &vn_a);
10597 10607 as_rangeunlock(as);
10598 10608
10599 10609 done:
10600 10610 nfs_rw_exit(&rp->r_lkserlock);
10601 10611 atomic_dec_uint(&rp->r_inmap);
10602 10612 return (error);
10603 10613 }
10604 10614
10605 10615 /*
10606 10616 * We're most likely dealing with a kernel module that likes to READ
10607 10617 * and mmap without OPENing the file (ie: lookup/read/mmap), so lets
10608 10618 * officially OPEN the file to create the necessary client state
10609 10619 * for bookkeeping of os_mmap_read/write counts.
10610 10620 *
10611 10621 * Since VOP_MAP only passes in a pointer to the vnode rather than
10612 10622 * a double pointer, we can't handle the case where nfs4open_otw()
10613 10623 * returns a different vnode than the one passed into VOP_MAP (since
10614 10624 * VOP_DELMAP will not see the vnode nfs4open_otw used). In this case,
10615 10625 * we return NULL and let nfs4_map() fail. Note: the only case where
10616 10626 * this should happen is if the file got removed and replaced with the
10617 10627 * same name on the server (in addition to the fact that we're trying
10618 10628 * to VOP_MAP withouth VOP_OPENing the file in the first place).
10619 10629 */
10620 10630 static int
10621 10631 open_and_get_osp(vnode_t *map_vp, cred_t *cr, nfs4_open_stream_t **ospp)
10622 10632 {
10623 10633 rnode4_t *rp, *drp;
10624 10634 vnode_t *dvp, *open_vp;
10625 10635 char file_name[MAXNAMELEN];
10626 10636 int just_created;
10627 10637 nfs4_open_stream_t *osp;
10628 10638 nfs4_open_owner_t *oop;
10629 10639 int error;
10630 10640
10631 10641 *ospp = NULL;
10632 10642 open_vp = map_vp;
10633 10643
10634 10644 rp = VTOR4(open_vp);
10635 10645 if ((error = vtodv(open_vp, &dvp, cr, TRUE)) != 0)
10636 10646 return (error);
10637 10647 drp = VTOR4(dvp);
10638 10648
10639 10649 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp))) {
10640 10650 VN_RELE(dvp);
10641 10651 return (EINTR);
10642 10652 }
10643 10653
10644 10654 if ((error = vtoname(open_vp, file_name, MAXNAMELEN)) != 0) {
10645 10655 nfs_rw_exit(&drp->r_rwlock);
10646 10656 VN_RELE(dvp);
10647 10657 return (error);
10648 10658 }
10649 10659
10650 10660 mutex_enter(&rp->r_statev4_lock);
10651 10661 if (rp->created_v4) {
10652 10662 rp->created_v4 = 0;
10653 10663 mutex_exit(&rp->r_statev4_lock);
10654 10664
10655 10665 dnlc_update(dvp, file_name, open_vp);
10656 10666 /* This is needed so we don't bump the open ref count */
10657 10667 just_created = 1;
10658 10668 } else {
10659 10669 mutex_exit(&rp->r_statev4_lock);
10660 10670 just_created = 0;
10661 10671 }
10662 10672
10663 10673 VN_HOLD(map_vp);
10664 10674
10665 10675 error = nfs4open_otw(dvp, file_name, NULL, &open_vp, cr, 0, FREAD, 0,
10666 10676 just_created);
10667 10677 if (error) {
10668 10678 nfs_rw_exit(&drp->r_rwlock);
10669 10679 VN_RELE(dvp);
10670 10680 VN_RELE(map_vp);
10671 10681 return (error);
10672 10682 }
10673 10683
10674 10684 nfs_rw_exit(&drp->r_rwlock);
10675 10685 VN_RELE(dvp);
10676 10686
10677 10687 /*
10678 10688 * If nfs4open_otw() returned a different vnode then "undo"
10679 10689 * the open and return failure to the caller.
10680 10690 */
10681 10691 if (!VN_CMP(open_vp, map_vp)) {
10682 10692 nfs4_error_t e;
10683 10693
10684 10694 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "open_and_get_osp: "
10685 10695 "open returned a different vnode"));
10686 10696 /*
10687 10697 * If there's an error, ignore it,
10688 10698 * and let VOP_INACTIVE handle it.
10689 10699 */
10690 10700 (void) nfs4close_one(open_vp, NULL, cr, FREAD, NULL, &e,
10691 10701 CLOSE_NORM, 0, 0, 0);
10692 10702 VN_RELE(map_vp);
10693 10703 return (EIO);
10694 10704 }
10695 10705
10696 10706 VN_RELE(map_vp);
10697 10707
10698 10708 oop = find_open_owner(cr, NFS4_PERM_CREATED, VTOMI4(open_vp));
10699 10709 if (!oop) {
10700 10710 nfs4_error_t e;
10701 10711
10702 10712 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "open_and_get_osp: "
10703 10713 "no open owner"));
10704 10714 /*
10705 10715 * If there's an error, ignore it,
10706 10716 * and let VOP_INACTIVE handle it.
10707 10717 */
10708 10718 (void) nfs4close_one(open_vp, NULL, cr, FREAD, NULL, &e,
10709 10719 CLOSE_NORM, 0, 0, 0);
10710 10720 return (EIO);
10711 10721 }
10712 10722 osp = find_open_stream(oop, rp);
10713 10723 open_owner_rele(oop);
10714 10724 *ospp = osp;
10715 10725 return (0);
10716 10726 }
10717 10727
10718 10728 /*
10719 10729 * Please be aware that when this function is called, the address space write
10720 10730 * a_lock is held. Do not put over the wire calls in this function.
10721 10731 */
10722 10732 /* ARGSUSED */
10723 10733 static int
10724 10734 nfs4_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
10725 10735 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
10726 10736 caller_context_t *ct)
10727 10737 {
10728 10738 rnode4_t *rp;
10729 10739 int error = 0;
10730 10740 mntinfo4_t *mi;
10731 10741
10732 10742 mi = VTOMI4(vp);
10733 10743 rp = VTOR4(vp);
10734 10744
10735 10745 if (nfs_zone() != mi->mi_zone)
10736 10746 return (EIO);
10737 10747 if (vp->v_flag & VNOMAP)
10738 10748 return (ENOSYS);
10739 10749
10740 10750 /*
10741 10751 * Don't need to update the open stream first, since this
10742 10752 * mmap can't add any additional share access that isn't
10743 10753 * already contained in the open stream (for the case where we
10744 10754 * open/mmap/only update rp->r_mapcnt/server reboots/reopen doesn't
10745 10755 * take into account os_mmap_read[write] counts).
10746 10756 */
10747 10757 atomic_add_long((ulong_t *)&rp->r_mapcnt, btopr(len));
10748 10758
10749 10759 if (vp->v_type == VREG) {
10750 10760 /*
10751 10761 * We need to retrieve the open stream and update the counts.
10752 10762 * If there is no open stream here, something is wrong.
10753 10763 */
10754 10764 nfs4_open_stream_t *osp = NULL;
10755 10765 nfs4_open_owner_t *oop = NULL;
10756 10766
10757 10767 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi);
10758 10768 if (oop != NULL) {
10759 10769 /* returns with 'os_sync_lock' held */
10760 10770 osp = find_open_stream(oop, rp);
10761 10771 open_owner_rele(oop);
10762 10772 }
10763 10773 if (osp == NULL) {
10764 10774 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE,
10765 10775 "nfs4_addmap: we should have an osp"
10766 10776 "but we don't, so fail with EIO"));
10767 10777 error = EIO;
10768 10778 goto out;
10769 10779 }
10770 10780
10771 10781 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "nfs4_addmap: osp %p,"
10772 10782 " pages %ld, prot 0x%x", (void *)osp, btopr(len), prot));
10773 10783
10774 10784 /*
10775 10785 * Update the map count in the open stream.
10776 10786 * This is necessary in the case where we
10777 10787 * open/mmap/close/, then the server reboots, and we
10778 10788 * attempt to reopen. If the mmap doesn't add share
10779 10789 * access then we send an invalid reopen with
10780 10790 * access = NONE.
10781 10791 *
10782 10792 * We need to specifically check each PROT_* so a mmap
10783 10793 * call of (PROT_WRITE | PROT_EXEC) will ensure us both
10784 10794 * read and write access. A simple comparison of prot
10785 10795 * to ~PROT_WRITE to determine read access is insufficient
10786 10796 * since prot can be |= with PROT_USER, etc.
10787 10797 */
10788 10798
10789 10799 /*
10790 10800 * Unless we're MAP_SHARED, no sense in adding os_mmap_write
10791 10801 */
10792 10802 if ((flags & MAP_SHARED) && (maxprot & PROT_WRITE))
10793 10803 osp->os_mmap_write += btopr(len);
10794 10804 if (maxprot & PROT_READ)
10795 10805 osp->os_mmap_read += btopr(len);
10796 10806 if (maxprot & PROT_EXEC)
10797 10807 osp->os_mmap_read += btopr(len);
10798 10808 /*
10799 10809 * Ensure that os_mmap_read gets incremented, even if
10800 10810 * maxprot were to look like PROT_NONE.
10801 10811 */
10802 10812 if (!(maxprot & PROT_READ) && !(maxprot & PROT_WRITE) &&
10803 10813 !(maxprot & PROT_EXEC))
10804 10814 osp->os_mmap_read += btopr(len);
10805 10815 osp->os_mapcnt += btopr(len);
10806 10816 mutex_exit(&osp->os_sync_lock);
10807 10817 open_stream_rele(osp, rp);
10808 10818 }
10809 10819
10810 10820 out:
10811 10821 /*
10812 10822 * If we got an error, then undo our
10813 10823 * incrementing of 'r_mapcnt'.
10814 10824 */
10815 10825
10816 10826 if (error) {
10817 10827 atomic_add_long((ulong_t *)&rp->r_mapcnt, -btopr(len));
10818 10828 ASSERT(rp->r_mapcnt >= 0);
10819 10829 }
10820 10830 return (error);
10821 10831 }
10822 10832
10823 10833 /* ARGSUSED */
10824 10834 static int
10825 10835 nfs4_cmp(vnode_t *vp1, vnode_t *vp2, caller_context_t *ct)
10826 10836 {
10827 10837
10828 10838 return (VTOR4(vp1) == VTOR4(vp2));
10829 10839 }
10830 10840
10831 10841 /* ARGSUSED */
10832 10842 static int
10833 10843 nfs4_frlock(vnode_t *vp, int cmd, struct flock64 *bfp, int flag,
10834 10844 offset_t offset, struct flk_callback *flk_cbp, cred_t *cr,
10835 10845 caller_context_t *ct)
10836 10846 {
10837 10847 int rc;
10838 10848 u_offset_t start, end;
10839 10849 rnode4_t *rp;
10840 10850 int error = 0, intr = INTR4(vp);
10841 10851 nfs4_error_t e;
10842 10852
10843 10853 if (nfs_zone() != VTOMI4(vp)->mi_zone)
10844 10854 return (EIO);
10845 10855
10846 10856 /* check for valid cmd parameter */
10847 10857 if (cmd != F_GETLK && cmd != F_SETLK && cmd != F_SETLKW)
10848 10858 return (EINVAL);
10849 10859
10850 10860 /* Verify l_type. */
10851 10861 switch (bfp->l_type) {
10852 10862 case F_RDLCK:
10853 10863 if (cmd != F_GETLK && !(flag & FREAD))
10854 10864 return (EBADF);
10855 10865 break;
10856 10866 case F_WRLCK:
10857 10867 if (cmd != F_GETLK && !(flag & FWRITE))
10858 10868 return (EBADF);
10859 10869 break;
10860 10870 case F_UNLCK:
10861 10871 intr = 0;
10862 10872 break;
10863 10873
10864 10874 default:
10865 10875 return (EINVAL);
10866 10876 }
10867 10877
10868 10878 /* check the validity of the lock range */
10869 10879 if (rc = flk_convert_lock_data(vp, bfp, &start, &end, offset))
10870 10880 return (rc);
10871 10881 if (rc = flk_check_lock_data(start, end, MAXEND))
10872 10882 return (rc);
10873 10883
10874 10884 /*
10875 10885 * If the filesystem is mounted using local locking, pass the
10876 10886 * request off to the local locking code.
10877 10887 */
10878 10888 if (VTOMI4(vp)->mi_flags & MI4_LLOCK || vp->v_type != VREG) {
10879 10889 if (cmd == F_SETLK || cmd == F_SETLKW) {
10880 10890 /*
10881 10891 * For complete safety, we should be holding
10882 10892 * r_lkserlock. However, we can't call
10883 10893 * nfs4_safelock and then fs_frlock while
10884 10894 * holding r_lkserlock, so just invoke
10885 10895 * nfs4_safelock and expect that this will
10886 10896 * catch enough of the cases.
10887 10897 */
10888 10898 if (!nfs4_safelock(vp, bfp, cr))
10889 10899 return (EAGAIN);
10890 10900 }
10891 10901 return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct));
10892 10902 }
10893 10903
10894 10904 rp = VTOR4(vp);
10895 10905
10896 10906 /*
10897 10907 * Check whether the given lock request can proceed, given the
10898 10908 * current file mappings.
10899 10909 */
10900 10910 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_WRITER, intr))
10901 10911 return (EINTR);
10902 10912 if (cmd == F_SETLK || cmd == F_SETLKW) {
10903 10913 if (!nfs4_safelock(vp, bfp, cr)) {
10904 10914 rc = EAGAIN;
10905 10915 goto done;
10906 10916 }
10907 10917 }
10908 10918
10909 10919 /*
10910 10920 * Flush the cache after waiting for async I/O to finish. For new
10911 10921 * locks, this is so that the process gets the latest bits from the
10912 10922 * server. For unlocks, this is so that other clients see the
10913 10923 * latest bits once the file has been unlocked. If currently dirty
10914 10924 * pages can't be flushed, then don't allow a lock to be set. But
10915 10925 * allow unlocks to succeed, to avoid having orphan locks on the
10916 10926 * server.
10917 10927 */
10918 10928 if (cmd != F_GETLK) {
10919 10929 mutex_enter(&rp->r_statelock);
10920 10930 while (rp->r_count > 0) {
10921 10931 if (intr) {
10922 10932 klwp_t *lwp = ttolwp(curthread);
10923 10933
10924 10934 if (lwp != NULL)
10925 10935 lwp->lwp_nostop++;
10926 10936 if (cv_wait_sig(&rp->r_cv,
10927 10937 &rp->r_statelock) == 0) {
10928 10938 if (lwp != NULL)
10929 10939 lwp->lwp_nostop--;
10930 10940 rc = EINTR;
10931 10941 break;
10932 10942 }
10933 10943 if (lwp != NULL)
10934 10944 lwp->lwp_nostop--;
10935 10945 } else
10936 10946 cv_wait(&rp->r_cv, &rp->r_statelock);
10937 10947 }
10938 10948 mutex_exit(&rp->r_statelock);
10939 10949 if (rc != 0)
10940 10950 goto done;
10941 10951 error = nfs4_putpage(vp, (offset_t)0, 0, B_INVAL, cr, ct);
10942 10952 if (error) {
10943 10953 if (error == ENOSPC || error == EDQUOT) {
10944 10954 mutex_enter(&rp->r_statelock);
10945 10955 if (!rp->r_error)
10946 10956 rp->r_error = error;
10947 10957 mutex_exit(&rp->r_statelock);
10948 10958 }
10949 10959 if (bfp->l_type != F_UNLCK) {
10950 10960 rc = ENOLCK;
10951 10961 goto done;
10952 10962 }
10953 10963 }
10954 10964 }
10955 10965
10956 10966 /*
10957 10967 * Call the lock manager to do the real work of contacting
10958 10968 * the server and obtaining the lock.
10959 10969 */
10960 10970 nfs4frlock(NFS4_LCK_CTYPE_NORM, vp, cmd, bfp, flag, offset,
10961 10971 cr, &e, NULL, NULL);
10962 10972 rc = e.error;
10963 10973
10964 10974 if (rc == 0)
10965 10975 nfs4_lockcompletion(vp, cmd);
10966 10976
10967 10977 done:
10968 10978 nfs_rw_exit(&rp->r_lkserlock);
10969 10979
10970 10980 return (rc);
10971 10981 }
10972 10982
10973 10983 /*
10974 10984 * Free storage space associated with the specified vnode. The portion
10975 10985 * to be freed is specified by bfp->l_start and bfp->l_len (already
10976 10986 * normalized to a "whence" of 0).
10977 10987 *
10978 10988 * This is an experimental facility whose continued existence is not
10979 10989 * guaranteed. Currently, we only support the special case
10980 10990 * of l_len == 0, meaning free to end of file.
10981 10991 */
10982 10992 /* ARGSUSED */
10983 10993 static int
10984 10994 nfs4_space(vnode_t *vp, int cmd, struct flock64 *bfp, int flag,
10985 10995 offset_t offset, cred_t *cr, caller_context_t *ct)
10986 10996 {
10987 10997 int error;
10988 10998
10989 10999 if (nfs_zone() != VTOMI4(vp)->mi_zone)
10990 11000 return (EIO);
10991 11001 ASSERT(vp->v_type == VREG);
10992 11002 if (cmd != F_FREESP)
10993 11003 return (EINVAL);
10994 11004
10995 11005 error = convoff(vp, bfp, 0, offset);
10996 11006 if (!error) {
10997 11007 ASSERT(bfp->l_start >= 0);
10998 11008 if (bfp->l_len == 0) {
10999 11009 struct vattr va;
11000 11010
11001 11011 va.va_mask = AT_SIZE;
11002 11012 va.va_size = bfp->l_start;
11003 11013 error = nfs4setattr(vp, &va, 0, cr, NULL);
11004 11014
11005 11015 if (error == 0 && bfp->l_start == 0)
11006 11016 vnevent_truncate(vp, ct);
11007 11017 } else
11008 11018 error = EINVAL;
11009 11019 }
11010 11020
11011 11021 return (error);
11012 11022 }
11013 11023
11014 11024 /* ARGSUSED */
11015 11025 int
11016 11026 nfs4_realvp(vnode_t *vp, vnode_t **vpp, caller_context_t *ct)
11017 11027 {
11018 11028 rnode4_t *rp;
11019 11029 rp = VTOR4(vp);
11020 11030
11021 11031 if (vp->v_type == VREG && IS_SHADOW(vp, rp)) {
11022 11032 vp = RTOV4(rp);
11023 11033 }
11024 11034 *vpp = vp;
11025 11035 return (0);
11026 11036 }
11027 11037
11028 11038 /*
11029 11039 * Setup and add an address space callback to do the work of the delmap call.
11030 11040 * The callback will (and must be) deleted in the actual callback function.
11031 11041 *
11032 11042 * This is done in order to take care of the problem that we have with holding
11033 11043 * the address space's a_lock for a long period of time (e.g. if the NFS server
11034 11044 * is down). Callbacks will be executed in the address space code while the
11035 11045 * a_lock is not held. Holding the address space's a_lock causes things such
11036 11046 * as ps and fork to hang because they are trying to acquire this lock as well.
11037 11047 */
11038 11048 /* ARGSUSED */
11039 11049 static int
11040 11050 nfs4_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
11041 11051 size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr,
11042 11052 caller_context_t *ct)
11043 11053 {
11044 11054 int caller_found;
11045 11055 int error;
11046 11056 rnode4_t *rp;
11047 11057 nfs4_delmap_args_t *dmapp;
11048 11058 nfs4_delmapcall_t *delmap_call;
11049 11059
11050 11060 if (vp->v_flag & VNOMAP)
11051 11061 return (ENOSYS);
11052 11062
11053 11063 /*
11054 11064 * A process may not change zones if it has NFS pages mmap'ed
11055 11065 * in, so we can't legitimately get here from the wrong zone.
11056 11066 */
11057 11067 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
11058 11068
11059 11069 rp = VTOR4(vp);
11060 11070
11061 11071 /*
11062 11072 * The way that the address space of this process deletes its mapping
11063 11073 * of this file is via the following call chains:
11064 11074 * - as_free()->SEGOP_UNMAP()/segvn_unmap()->VOP_DELMAP()/nfs4_delmap()
11065 11075 * - as_unmap()->SEGOP_UNMAP()/segvn_unmap()->VOP_DELMAP()/nfs4_delmap()
11066 11076 *
11067 11077 * With the use of address space callbacks we are allowed to drop the
11068 11078 * address space lock, a_lock, while executing the NFS operations that
11069 11079 * need to go over the wire. Returning EAGAIN to the caller of this
11070 11080 * function is what drives the execution of the callback that we add
11071 11081 * below. The callback will be executed by the address space code
11072 11082 * after dropping the a_lock. When the callback is finished, since
11073 11083 * we dropped the a_lock, it must be re-acquired and segvn_unmap()
11074 11084 * is called again on the same segment to finish the rest of the work
11075 11085 * that needs to happen during unmapping.
11076 11086 *
11077 11087 * This action of calling back into the segment driver causes
11078 11088 * nfs4_delmap() to get called again, but since the callback was
11079 11089 * already executed at this point, it already did the work and there
11080 11090 * is nothing left for us to do.
11081 11091 *
11082 11092 * To Summarize:
11083 11093 * - The first time nfs4_delmap is called by the current thread is when
11084 11094 * we add the caller associated with this delmap to the delmap caller
11085 11095 * list, add the callback, and return EAGAIN.
11086 11096 * - The second time in this call chain when nfs4_delmap is called we
11087 11097 * will find this caller in the delmap caller list and realize there
11088 11098 * is no more work to do thus removing this caller from the list and
11089 11099 * returning the error that was set in the callback execution.
11090 11100 */
11091 11101 caller_found = nfs4_find_and_delete_delmapcall(rp, &error);
11092 11102 if (caller_found) {
11093 11103 /*
11094 11104 * 'error' is from the actual delmap operations. To avoid
11095 11105 * hangs, we need to handle the return of EAGAIN differently
11096 11106 * since this is what drives the callback execution.
11097 11107 * In this case, we don't want to return EAGAIN and do the
11098 11108 * callback execution because there are none to execute.
11099 11109 */
11100 11110 if (error == EAGAIN)
11101 11111 return (0);
11102 11112 else
11103 11113 return (error);
11104 11114 }
11105 11115
11106 11116 /* current caller was not in the list */
11107 11117 delmap_call = nfs4_init_delmapcall();
11108 11118
11109 11119 mutex_enter(&rp->r_statelock);
11110 11120 list_insert_tail(&rp->r_indelmap, delmap_call);
11111 11121 mutex_exit(&rp->r_statelock);
11112 11122
11113 11123 dmapp = kmem_alloc(sizeof (nfs4_delmap_args_t), KM_SLEEP);
11114 11124
11115 11125 dmapp->vp = vp;
11116 11126 dmapp->off = off;
11117 11127 dmapp->addr = addr;
11118 11128 dmapp->len = len;
11119 11129 dmapp->prot = prot;
11120 11130 dmapp->maxprot = maxprot;
11121 11131 dmapp->flags = flags;
11122 11132 dmapp->cr = cr;
11123 11133 dmapp->caller = delmap_call;
11124 11134
11125 11135 error = as_add_callback(as, nfs4_delmap_callback, dmapp,
11126 11136 AS_UNMAP_EVENT, addr, len, KM_SLEEP);
11127 11137
11128 11138 return (error ? error : EAGAIN);
11129 11139 }
11130 11140
11131 11141 static nfs4_delmapcall_t *
11132 11142 nfs4_init_delmapcall()
11133 11143 {
11134 11144 nfs4_delmapcall_t *delmap_call;
11135 11145
11136 11146 delmap_call = kmem_alloc(sizeof (nfs4_delmapcall_t), KM_SLEEP);
11137 11147 delmap_call->call_id = curthread;
11138 11148 delmap_call->error = 0;
11139 11149
11140 11150 return (delmap_call);
11141 11151 }
11142 11152
11143 11153 static void
11144 11154 nfs4_free_delmapcall(nfs4_delmapcall_t *delmap_call)
11145 11155 {
11146 11156 kmem_free(delmap_call, sizeof (nfs4_delmapcall_t));
11147 11157 }
11148 11158
11149 11159 /*
11150 11160 * Searches for the current delmap caller (based on curthread) in the list of
11151 11161 * callers. If it is found, we remove it and free the delmap caller.
11152 11162 * Returns:
11153 11163 * 0 if the caller wasn't found
11154 11164 * 1 if the caller was found, removed and freed. *errp will be set
11155 11165 * to what the result of the delmap was.
11156 11166 */
11157 11167 static int
11158 11168 nfs4_find_and_delete_delmapcall(rnode4_t *rp, int *errp)
11159 11169 {
11160 11170 nfs4_delmapcall_t *delmap_call;
11161 11171
11162 11172 /*
11163 11173 * If the list doesn't exist yet, we create it and return
11164 11174 * that the caller wasn't found. No list = no callers.
11165 11175 */
11166 11176 mutex_enter(&rp->r_statelock);
11167 11177 if (!(rp->r_flags & R4DELMAPLIST)) {
11168 11178 /* The list does not exist */
11169 11179 list_create(&rp->r_indelmap, sizeof (nfs4_delmapcall_t),
11170 11180 offsetof(nfs4_delmapcall_t, call_node));
11171 11181 rp->r_flags |= R4DELMAPLIST;
11172 11182 mutex_exit(&rp->r_statelock);
11173 11183 return (0);
11174 11184 } else {
11175 11185 /* The list exists so search it */
11176 11186 for (delmap_call = list_head(&rp->r_indelmap);
11177 11187 delmap_call != NULL;
11178 11188 delmap_call = list_next(&rp->r_indelmap, delmap_call)) {
11179 11189 if (delmap_call->call_id == curthread) {
11180 11190 /* current caller is in the list */
11181 11191 *errp = delmap_call->error;
11182 11192 list_remove(&rp->r_indelmap, delmap_call);
11183 11193 mutex_exit(&rp->r_statelock);
11184 11194 nfs4_free_delmapcall(delmap_call);
11185 11195 return (1);
11186 11196 }
11187 11197 }
11188 11198 }
11189 11199 mutex_exit(&rp->r_statelock);
11190 11200 return (0);
11191 11201 }
11192 11202
11193 11203 /*
11194 11204 * Remove some pages from an mmap'd vnode. Just update the
11195 11205 * count of pages. If doing close-to-open, then flush and
11196 11206 * commit all of the pages associated with this file.
11197 11207 * Otherwise, start an asynchronous page flush to write out
11198 11208 * any dirty pages. This will also associate a credential
11199 11209 * with the rnode which can be used to write the pages.
11200 11210 */
11201 11211 /* ARGSUSED */
11202 11212 static void
11203 11213 nfs4_delmap_callback(struct as *as, void *arg, uint_t event)
11204 11214 {
11205 11215 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
11206 11216 rnode4_t *rp;
11207 11217 mntinfo4_t *mi;
11208 11218 nfs4_delmap_args_t *dmapp = (nfs4_delmap_args_t *)arg;
11209 11219
11210 11220 rp = VTOR4(dmapp->vp);
11211 11221 mi = VTOMI4(dmapp->vp);
11212 11222
11213 11223 atomic_add_long((ulong_t *)&rp->r_mapcnt, -btopr(dmapp->len));
11214 11224 ASSERT(rp->r_mapcnt >= 0);
11215 11225
11216 11226 /*
11217 11227 * Initiate a page flush and potential commit if there are
11218 11228 * pages, the file system was not mounted readonly, the segment
11219 11229 * was mapped shared, and the pages themselves were writeable.
11220 11230 */
11221 11231 if (nfs4_has_pages(dmapp->vp) &&
11222 11232 !(dmapp->vp->v_vfsp->vfs_flag & VFS_RDONLY) &&
11223 11233 dmapp->flags == MAP_SHARED && (dmapp->maxprot & PROT_WRITE)) {
11224 11234 mutex_enter(&rp->r_statelock);
11225 11235 rp->r_flags |= R4DIRTY;
11226 11236 mutex_exit(&rp->r_statelock);
11227 11237 e.error = nfs4_putpage_commit(dmapp->vp, dmapp->off,
11228 11238 dmapp->len, dmapp->cr);
11229 11239 if (!e.error) {
11230 11240 mutex_enter(&rp->r_statelock);
11231 11241 e.error = rp->r_error;
11232 11242 rp->r_error = 0;
11233 11243 mutex_exit(&rp->r_statelock);
11234 11244 }
11235 11245 } else
11236 11246 e.error = 0;
11237 11247
11238 11248 if ((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO))
11239 11249 (void) nfs4_putpage(dmapp->vp, dmapp->off, dmapp->len,
11240 11250 B_INVAL, dmapp->cr, NULL);
11241 11251
11242 11252 if (e.error) {
11243 11253 e.stat = puterrno4(e.error);
11244 11254 nfs4_queue_fact(RF_DELMAP_CB_ERR, mi, e.stat, 0,
11245 11255 OP_COMMIT, FALSE, NULL, 0, dmapp->vp);
11246 11256 dmapp->caller->error = e.error;
11247 11257 }
11248 11258
11249 11259 /* Check to see if we need to close the file */
11250 11260
11251 11261 if (dmapp->vp->v_type == VREG) {
11252 11262 nfs4close_one(dmapp->vp, NULL, dmapp->cr, 0, NULL, &e,
11253 11263 CLOSE_DELMAP, dmapp->len, dmapp->maxprot, dmapp->flags);
11254 11264
11255 11265 if (e.error != 0 || e.stat != NFS4_OK) {
11256 11266 /*
11257 11267 * Since it is possible that e.error == 0 and
11258 11268 * e.stat != NFS4_OK (and vice versa),
11259 11269 * we do the proper checking in order to get both
11260 11270 * e.error and e.stat reporting the correct info.
11261 11271 */
11262 11272 if (e.stat == NFS4_OK)
11263 11273 e.stat = puterrno4(e.error);
11264 11274 if (e.error == 0)
11265 11275 e.error = geterrno4(e.stat);
11266 11276
11267 11277 nfs4_queue_fact(RF_DELMAP_CB_ERR, mi, e.stat, 0,
11268 11278 OP_CLOSE, FALSE, NULL, 0, dmapp->vp);
11269 11279 dmapp->caller->error = e.error;
11270 11280 }
11271 11281 }
11272 11282
11273 11283 (void) as_delete_callback(as, arg);
11274 11284 kmem_free(dmapp, sizeof (nfs4_delmap_args_t));
11275 11285 }
11276 11286
11277 11287
11278 11288 static uint_t
11279 11289 fattr4_maxfilesize_to_bits(uint64_t ll)
11280 11290 {
11281 11291 uint_t l = 1;
11282 11292
11283 11293 if (ll == 0) {
11284 11294 return (0);
11285 11295 }
11286 11296
11287 11297 if (ll & 0xffffffff00000000) {
11288 11298 l += 32; ll >>= 32;
11289 11299 }
11290 11300 if (ll & 0xffff0000) {
11291 11301 l += 16; ll >>= 16;
11292 11302 }
11293 11303 if (ll & 0xff00) {
11294 11304 l += 8; ll >>= 8;
11295 11305 }
11296 11306 if (ll & 0xf0) {
11297 11307 l += 4; ll >>= 4;
11298 11308 }
11299 11309 if (ll & 0xc) {
11300 11310 l += 2; ll >>= 2;
11301 11311 }
11302 11312 if (ll & 0x2) {
11303 11313 l += 1;
11304 11314 }
11305 11315 return (l);
11306 11316 }
11307 11317
11308 11318 static int
11309 11319 nfs4_have_xattrs(vnode_t *vp, ulong_t *valp, cred_t *cr)
11310 11320 {
11311 11321 vnode_t *avp = NULL;
11312 11322 int error;
11313 11323
11314 11324 if ((error = nfs4lookup_xattr(vp, "", &avp,
11315 11325 LOOKUP_XATTR, cr)) == 0)
11316 11326 error = do_xattr_exists_check(avp, valp, cr);
11317 11327 if (avp)
11318 11328 VN_RELE(avp);
11319 11329
11320 11330 return (error);
11321 11331 }
11322 11332
11323 11333 /* ARGSUSED */
11324 11334 int
11325 11335 nfs4_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
11326 11336 caller_context_t *ct)
11327 11337 {
11328 11338 int error;
11329 11339 hrtime_t t;
11330 11340 rnode4_t *rp;
11331 11341 nfs4_ga_res_t gar;
11332 11342 nfs4_ga_ext_res_t ger;
11333 11343
11334 11344 gar.n4g_ext_res = &ger;
11335 11345
11336 11346 if (nfs_zone() != VTOMI4(vp)->mi_zone)
11337 11347 return (EIO);
11338 11348 if (cmd == _PC_PATH_MAX || cmd == _PC_SYMLINK_MAX) {
11339 11349 *valp = MAXPATHLEN;
11340 11350 return (0);
11341 11351 }
11342 11352 if (cmd == _PC_ACL_ENABLED) {
11343 11353 *valp = _ACL_ACE_ENABLED;
11344 11354 return (0);
11345 11355 }
11346 11356
11347 11357 rp = VTOR4(vp);
11348 11358 if (cmd == _PC_XATTR_EXISTS) {
11349 11359 /*
11350 11360 * The existence of the xattr directory is not sufficient
11351 11361 * for determining whether generic user attributes exists.
11352 11362 * The attribute directory could only be a transient directory
11353 11363 * used for Solaris sysattr support. Do a small readdir
11354 11364 * to verify if the only entries are sysattrs or not.
11355 11365 *
11356 11366 * pc4_xattr_valid can be only be trusted when r_xattr_dir
11357 11367 * is NULL. Once the xadir vp exists, we can create xattrs,
11358 11368 * and we don't have any way to update the "base" object's
11359 11369 * pc4_xattr_exists from the xattr or xadir. Maybe FEM
11360 11370 * could help out.
11361 11371 */
11362 11372 if (ATTRCACHE4_VALID(vp) && rp->r_pathconf.pc4_xattr_valid &&
11363 11373 rp->r_xattr_dir == NULL) {
11364 11374 return (nfs4_have_xattrs(vp, valp, cr));
11365 11375 }
11366 11376 } else { /* OLD CODE */
11367 11377 if (ATTRCACHE4_VALID(vp)) {
11368 11378 mutex_enter(&rp->r_statelock);
11369 11379 if (rp->r_pathconf.pc4_cache_valid) {
11370 11380 error = 0;
11371 11381 switch (cmd) {
11372 11382 case _PC_FILESIZEBITS:
11373 11383 *valp =
11374 11384 rp->r_pathconf.pc4_filesizebits;
11375 11385 break;
11376 11386 case _PC_LINK_MAX:
11377 11387 *valp =
11378 11388 rp->r_pathconf.pc4_link_max;
11379 11389 break;
11380 11390 case _PC_NAME_MAX:
11381 11391 *valp =
11382 11392 rp->r_pathconf.pc4_name_max;
11383 11393 break;
11384 11394 case _PC_CHOWN_RESTRICTED:
11385 11395 *valp =
11386 11396 rp->r_pathconf.pc4_chown_restricted;
11387 11397 break;
11388 11398 case _PC_NO_TRUNC:
11389 11399 *valp =
11390 11400 rp->r_pathconf.pc4_no_trunc;
11391 11401 break;
11392 11402 default:
11393 11403 error = EINVAL;
11394 11404 break;
11395 11405 }
11396 11406 mutex_exit(&rp->r_statelock);
11397 11407 #ifdef DEBUG
11398 11408 nfs4_pathconf_cache_hits++;
11399 11409 #endif
11400 11410 return (error);
11401 11411 }
11402 11412 mutex_exit(&rp->r_statelock);
11403 11413 }
11404 11414 }
11405 11415 #ifdef DEBUG
11406 11416 nfs4_pathconf_cache_misses++;
11407 11417 #endif
11408 11418
11409 11419 t = gethrtime();
11410 11420
11411 11421 error = nfs4_attr_otw(vp, TAG_PATHCONF, &gar, NFS4_PATHCONF_MASK, cr);
11412 11422
11413 11423 if (error) {
11414 11424 mutex_enter(&rp->r_statelock);
11415 11425 rp->r_pathconf.pc4_cache_valid = FALSE;
11416 11426 rp->r_pathconf.pc4_xattr_valid = FALSE;
11417 11427 mutex_exit(&rp->r_statelock);
11418 11428 return (error);
11419 11429 }
11420 11430
11421 11431 /* interpret the max filesize */
11422 11432 gar.n4g_ext_res->n4g_pc4.pc4_filesizebits =
11423 11433 fattr4_maxfilesize_to_bits(gar.n4g_ext_res->n4g_maxfilesize);
11424 11434
11425 11435 /* Store the attributes we just received */
11426 11436 nfs4_attr_cache(vp, &gar, t, cr, TRUE, NULL);
11427 11437
11428 11438 switch (cmd) {
11429 11439 case _PC_FILESIZEBITS:
11430 11440 *valp = gar.n4g_ext_res->n4g_pc4.pc4_filesizebits;
11431 11441 break;
11432 11442 case _PC_LINK_MAX:
11433 11443 *valp = gar.n4g_ext_res->n4g_pc4.pc4_link_max;
11434 11444 break;
11435 11445 case _PC_NAME_MAX:
11436 11446 *valp = gar.n4g_ext_res->n4g_pc4.pc4_name_max;
11437 11447 break;
11438 11448 case _PC_CHOWN_RESTRICTED:
11439 11449 *valp = gar.n4g_ext_res->n4g_pc4.pc4_chown_restricted;
11440 11450 break;
11441 11451 case _PC_NO_TRUNC:
11442 11452 *valp = gar.n4g_ext_res->n4g_pc4.pc4_no_trunc;
11443 11453 break;
11444 11454 case _PC_XATTR_EXISTS:
11445 11455 if (gar.n4g_ext_res->n4g_pc4.pc4_xattr_exists) {
11446 11456 if (error = nfs4_have_xattrs(vp, valp, cr))
11447 11457 return (error);
11448 11458 }
11449 11459 break;
11450 11460 default:
11451 11461 return (EINVAL);
11452 11462 }
11453 11463
11454 11464 return (0);
11455 11465 }
11456 11466
11457 11467 /*
11458 11468 * Called by async thread to do synchronous pageio. Do the i/o, wait
11459 11469 * for it to complete, and cleanup the page list when done.
11460 11470 */
11461 11471 static int
11462 11472 nfs4_sync_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len,
11463 11473 int flags, cred_t *cr)
11464 11474 {
11465 11475 int error;
11466 11476
11467 11477 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
11468 11478
11469 11479 error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr);
11470 11480 if (flags & B_READ)
11471 11481 pvn_read_done(pp, (error ? B_ERROR : 0) | flags);
11472 11482 else
11473 11483 pvn_write_done(pp, (error ? B_ERROR : 0) | flags);
11474 11484 return (error);
11475 11485 }
11476 11486
11477 11487 /* ARGSUSED */
11478 11488 static int
11479 11489 nfs4_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len,
11480 11490 int flags, cred_t *cr, caller_context_t *ct)
11481 11491 {
11482 11492 int error;
11483 11493 rnode4_t *rp;
11484 11494
11485 11495 if (!(flags & B_ASYNC) && nfs_zone() != VTOMI4(vp)->mi_zone)
11486 11496 return (EIO);
11487 11497
11488 11498 if (pp == NULL)
11489 11499 return (EINVAL);
11490 11500
11491 11501 rp = VTOR4(vp);
11492 11502 mutex_enter(&rp->r_statelock);
11493 11503 rp->r_count++;
11494 11504 mutex_exit(&rp->r_statelock);
11495 11505
11496 11506 if (flags & B_ASYNC) {
11497 11507 error = nfs4_async_pageio(vp, pp, io_off, io_len, flags, cr,
11498 11508 nfs4_sync_pageio);
11499 11509 } else
11500 11510 error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr);
11501 11511 mutex_enter(&rp->r_statelock);
11502 11512 rp->r_count--;
11503 11513 cv_broadcast(&rp->r_cv);
11504 11514 mutex_exit(&rp->r_statelock);
11505 11515 return (error);
11506 11516 }
11507 11517
11508 11518 /* ARGSUSED */
11509 11519 static void
11510 11520 nfs4_dispose(vnode_t *vp, page_t *pp, int fl, int dn, cred_t *cr,
11511 11521 caller_context_t *ct)
11512 11522 {
11513 11523 int error;
11514 11524 rnode4_t *rp;
11515 11525 page_t *plist;
11516 11526 page_t *pptr;
11517 11527 offset3 offset;
11518 11528 count3 len;
11519 11529 k_sigset_t smask;
11520 11530
11521 11531 /*
11522 11532 * We should get called with fl equal to either B_FREE or
11523 11533 * B_INVAL. Any other value is illegal.
11524 11534 *
11525 11535 * The page that we are either supposed to free or destroy
11526 11536 * should be exclusive locked and its io lock should not
11527 11537 * be held.
11528 11538 */
11529 11539 ASSERT(fl == B_FREE || fl == B_INVAL);
11530 11540 ASSERT((PAGE_EXCL(pp) && !page_iolock_assert(pp)) || panicstr);
11531 11541
11532 11542 rp = VTOR4(vp);
11533 11543
11534 11544 /*
11535 11545 * If the page doesn't need to be committed or we shouldn't
11536 11546 * even bother attempting to commit it, then just make sure
11537 11547 * that the p_fsdata byte is clear and then either free or
11538 11548 * destroy the page as appropriate.
11539 11549 */
11540 11550 if (pp->p_fsdata == C_NOCOMMIT || (rp->r_flags & R4STALE)) {
11541 11551 pp->p_fsdata = C_NOCOMMIT;
11542 11552 if (fl == B_FREE)
11543 11553 page_free(pp, dn);
11544 11554 else
11545 11555 page_destroy(pp, dn);
11546 11556 return;
11547 11557 }
11548 11558
11549 11559 /*
11550 11560 * If there is a page invalidation operation going on, then
11551 11561 * if this is one of the pages being destroyed, then just
11552 11562 * clear the p_fsdata byte and then either free or destroy
11553 11563 * the page as appropriate.
11554 11564 */
11555 11565 mutex_enter(&rp->r_statelock);
11556 11566 if ((rp->r_flags & R4TRUNCATE) && pp->p_offset >= rp->r_truncaddr) {
11557 11567 mutex_exit(&rp->r_statelock);
11558 11568 pp->p_fsdata = C_NOCOMMIT;
11559 11569 if (fl == B_FREE)
11560 11570 page_free(pp, dn);
11561 11571 else
11562 11572 page_destroy(pp, dn);
11563 11573 return;
11564 11574 }
11565 11575
11566 11576 /*
11567 11577 * If we are freeing this page and someone else is already
11568 11578 * waiting to do a commit, then just unlock the page and
11569 11579 * return. That other thread will take care of commiting
11570 11580 * this page. The page can be freed sometime after the
11571 11581 * commit has finished. Otherwise, if the page is marked
11572 11582 * as delay commit, then we may be getting called from
11573 11583 * pvn_write_done, one page at a time. This could result
11574 11584 * in one commit per page, so we end up doing lots of small
11575 11585 * commits instead of fewer larger commits. This is bad,
11576 11586 * we want do as few commits as possible.
11577 11587 */
11578 11588 if (fl == B_FREE) {
11579 11589 if (rp->r_flags & R4COMMITWAIT) {
11580 11590 page_unlock(pp);
11581 11591 mutex_exit(&rp->r_statelock);
11582 11592 return;
11583 11593 }
11584 11594 if (pp->p_fsdata == C_DELAYCOMMIT) {
11585 11595 pp->p_fsdata = C_COMMIT;
11586 11596 page_unlock(pp);
11587 11597 mutex_exit(&rp->r_statelock);
11588 11598 return;
11589 11599 }
11590 11600 }
11591 11601
11592 11602 /*
11593 11603 * Check to see if there is a signal which would prevent an
11594 11604 * attempt to commit the pages from being successful. If so,
11595 11605 * then don't bother with all of the work to gather pages and
11596 11606 * generate the unsuccessful RPC. Just return from here and
11597 11607 * let the page be committed at some later time.
11598 11608 */
11599 11609 sigintr(&smask, VTOMI4(vp)->mi_flags & MI4_INT);
11600 11610 if (ttolwp(curthread) != NULL && ISSIG(curthread, JUSTLOOKING)) {
11601 11611 sigunintr(&smask);
11602 11612 page_unlock(pp);
11603 11613 mutex_exit(&rp->r_statelock);
11604 11614 return;
11605 11615 }
11606 11616 sigunintr(&smask);
11607 11617
11608 11618 /*
11609 11619 * We are starting to need to commit pages, so let's try
11610 11620 * to commit as many as possible at once to reduce the
11611 11621 * overhead.
11612 11622 *
11613 11623 * Set the `commit inprogress' state bit. We must
11614 11624 * first wait until any current one finishes. Then
11615 11625 * we initialize the c_pages list with this page.
11616 11626 */
11617 11627 while (rp->r_flags & R4COMMIT) {
11618 11628 rp->r_flags |= R4COMMITWAIT;
11619 11629 cv_wait(&rp->r_commit.c_cv, &rp->r_statelock);
11620 11630 rp->r_flags &= ~R4COMMITWAIT;
11621 11631 }
11622 11632 rp->r_flags |= R4COMMIT;
11623 11633 mutex_exit(&rp->r_statelock);
11624 11634 ASSERT(rp->r_commit.c_pages == NULL);
11625 11635 rp->r_commit.c_pages = pp;
11626 11636 rp->r_commit.c_commbase = (offset3)pp->p_offset;
11627 11637 rp->r_commit.c_commlen = PAGESIZE;
11628 11638
11629 11639 /*
11630 11640 * Gather together all other pages which can be committed.
11631 11641 * They will all be chained off r_commit.c_pages.
11632 11642 */
11633 11643 nfs4_get_commit(vp);
11634 11644
11635 11645 /*
11636 11646 * Clear the `commit inprogress' status and disconnect
11637 11647 * the list of pages to be committed from the rnode.
11638 11648 * At this same time, we also save the starting offset
11639 11649 * and length of data to be committed on the server.
11640 11650 */
11641 11651 plist = rp->r_commit.c_pages;
11642 11652 rp->r_commit.c_pages = NULL;
11643 11653 offset = rp->r_commit.c_commbase;
11644 11654 len = rp->r_commit.c_commlen;
11645 11655 mutex_enter(&rp->r_statelock);
11646 11656 rp->r_flags &= ~R4COMMIT;
11647 11657 cv_broadcast(&rp->r_commit.c_cv);
11648 11658 mutex_exit(&rp->r_statelock);
11649 11659
11650 11660 if (curproc == proc_pageout || curproc == proc_fsflush ||
11651 11661 nfs_zone() != VTOMI4(vp)->mi_zone) {
11652 11662 nfs4_async_commit(vp, plist, offset, len,
11653 11663 cr, do_nfs4_async_commit);
11654 11664 return;
11655 11665 }
11656 11666
11657 11667 /*
11658 11668 * Actually generate the COMMIT op over the wire operation.
11659 11669 */
11660 11670 error = nfs4_commit(vp, (offset4)offset, (count4)len, cr);
11661 11671
11662 11672 /*
11663 11673 * If we got an error during the commit, just unlock all
11664 11674 * of the pages. The pages will get retransmitted to the
11665 11675 * server during a putpage operation.
11666 11676 */
11667 11677 if (error) {
11668 11678 while (plist != NULL) {
11669 11679 pptr = plist;
11670 11680 page_sub(&plist, pptr);
11671 11681 page_unlock(pptr);
11672 11682 }
11673 11683 return;
11674 11684 }
11675 11685
11676 11686 /*
11677 11687 * We've tried as hard as we can to commit the data to stable
11678 11688 * storage on the server. We just unlock the rest of the pages
11679 11689 * and clear the commit required state. They will be put
11680 11690 * onto the tail of the cachelist if they are nolonger
11681 11691 * mapped.
11682 11692 */
11683 11693 while (plist != pp) {
11684 11694 pptr = plist;
11685 11695 page_sub(&plist, pptr);
11686 11696 pptr->p_fsdata = C_NOCOMMIT;
11687 11697 page_unlock(pptr);
11688 11698 }
11689 11699
11690 11700 /*
11691 11701 * It is possible that nfs4_commit didn't return error but
11692 11702 * some other thread has modified the page we are going
11693 11703 * to free/destroy.
11694 11704 * In this case we need to rewrite the page. Do an explicit check
11695 11705 * before attempting to free/destroy the page. If modified, needs to
11696 11706 * be rewritten so unlock the page and return.
11697 11707 */
11698 11708 if (hat_ismod(pp)) {
11699 11709 pp->p_fsdata = C_NOCOMMIT;
11700 11710 page_unlock(pp);
11701 11711 return;
11702 11712 }
11703 11713
11704 11714 /*
11705 11715 * Now, as appropriate, either free or destroy the page
11706 11716 * that we were called with.
11707 11717 */
11708 11718 pp->p_fsdata = C_NOCOMMIT;
11709 11719 if (fl == B_FREE)
11710 11720 page_free(pp, dn);
11711 11721 else
11712 11722 page_destroy(pp, dn);
11713 11723 }
11714 11724
11715 11725 /*
11716 11726 * Commit requires that the current fh be the file written to.
11717 11727 * The compound op structure is:
11718 11728 * PUTFH(file), COMMIT
11719 11729 */
11720 11730 static int
11721 11731 nfs4_commit(vnode_t *vp, offset4 offset, count4 count, cred_t *cr)
11722 11732 {
11723 11733 COMPOUND4args_clnt args;
11724 11734 COMPOUND4res_clnt res;
11725 11735 COMMIT4res *cm_res;
11726 11736 nfs_argop4 argop[2];
11727 11737 nfs_resop4 *resop;
11728 11738 int doqueue;
11729 11739 mntinfo4_t *mi;
11730 11740 rnode4_t *rp;
11731 11741 cred_t *cred_otw = NULL;
11732 11742 bool_t needrecov = FALSE;
11733 11743 nfs4_recov_state_t recov_state;
11734 11744 nfs4_open_stream_t *osp = NULL;
11735 11745 bool_t first_time = TRUE; /* first time getting OTW cred */
11736 11746 bool_t last_time = FALSE; /* last time getting OTW cred */
11737 11747 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
11738 11748
11739 11749 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
11740 11750
11741 11751 rp = VTOR4(vp);
11742 11752
11743 11753 mi = VTOMI4(vp);
11744 11754 recov_state.rs_flags = 0;
11745 11755 recov_state.rs_num_retry_despite_err = 0;
11746 11756 get_commit_cred:
11747 11757 /*
11748 11758 * Releases the osp, if a valid open stream is provided.
11749 11759 * Puts a hold on the cred_otw and the new osp (if found).
11750 11760 */
11751 11761 cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp,
11752 11762 &first_time, &last_time);
11753 11763 args.ctag = TAG_COMMIT;
11754 11764 recov_retry:
11755 11765 /*
11756 11766 * Commit ops: putfh file; commit
11757 11767 */
11758 11768 args.array_len = 2;
11759 11769 args.array = argop;
11760 11770
11761 11771 e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_COMMIT,
11762 11772 &recov_state, NULL);
11763 11773 if (e.error) {
11764 11774 crfree(cred_otw);
11765 11775 if (osp != NULL)
11766 11776 open_stream_rele(osp, rp);
11767 11777 return (e.error);
11768 11778 }
11769 11779
11770 11780 /* putfh directory */
11771 11781 argop[0].argop = OP_CPUTFH;
11772 11782 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
11773 11783
11774 11784 /* commit */
11775 11785 argop[1].argop = OP_COMMIT;
11776 11786 argop[1].nfs_argop4_u.opcommit.offset = offset;
11777 11787 argop[1].nfs_argop4_u.opcommit.count = count;
11778 11788
11779 11789 doqueue = 1;
11780 11790 rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, &e);
11781 11791
11782 11792 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
11783 11793 if (!needrecov && e.error) {
11784 11794 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, &recov_state,
11785 11795 needrecov);
11786 11796 crfree(cred_otw);
11787 11797 if (e.error == EACCES && last_time == FALSE)
11788 11798 goto get_commit_cred;
11789 11799 if (osp != NULL)
11790 11800 open_stream_rele(osp, rp);
11791 11801 return (e.error);
11792 11802 }
11793 11803
11794 11804 if (needrecov) {
11795 11805 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL,
11796 11806 NULL, OP_COMMIT, NULL, NULL, NULL) == FALSE) {
11797 11807 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT,
11798 11808 &recov_state, needrecov);
11799 11809 if (!e.error)
11800 11810 (void) xdr_free(xdr_COMPOUND4res_clnt,
11801 11811 (caddr_t)&res);
11802 11812 goto recov_retry;
11803 11813 }
11804 11814 if (e.error) {
11805 11815 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT,
11806 11816 &recov_state, needrecov);
11807 11817 crfree(cred_otw);
11808 11818 if (osp != NULL)
11809 11819 open_stream_rele(osp, rp);
11810 11820 return (e.error);
11811 11821 }
11812 11822 /* fall through for res.status case */
11813 11823 }
11814 11824
11815 11825 if (res.status) {
11816 11826 e.error = geterrno4(res.status);
11817 11827 if (e.error == EACCES && last_time == FALSE) {
11818 11828 crfree(cred_otw);
11819 11829 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT,
11820 11830 &recov_state, needrecov);
11821 11831 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
11822 11832 goto get_commit_cred;
11823 11833 }
11824 11834 /*
11825 11835 * Can't do a nfs4_purge_stale_fh here because this
11826 11836 * can cause a deadlock. nfs4_commit can
11827 11837 * be called from nfs4_dispose which can be called
11828 11838 * indirectly via pvn_vplist_dirty. nfs4_purge_stale_fh
11829 11839 * can call back to pvn_vplist_dirty.
11830 11840 */
11831 11841 if (e.error == ESTALE) {
11832 11842 mutex_enter(&rp->r_statelock);
11833 11843 rp->r_flags |= R4STALE;
11834 11844 if (!rp->r_error)
11835 11845 rp->r_error = e.error;
11836 11846 mutex_exit(&rp->r_statelock);
11837 11847 PURGE_ATTRCACHE4(vp);
11838 11848 } else {
11839 11849 mutex_enter(&rp->r_statelock);
11840 11850 if (!rp->r_error)
11841 11851 rp->r_error = e.error;
11842 11852 mutex_exit(&rp->r_statelock);
11843 11853 }
11844 11854 } else {
11845 11855 ASSERT(rp->r_flags & R4HAVEVERF);
11846 11856 resop = &res.array[1]; /* commit res */
11847 11857 cm_res = &resop->nfs_resop4_u.opcommit;
11848 11858 mutex_enter(&rp->r_statelock);
11849 11859 if (cm_res->writeverf == rp->r_writeverf) {
11850 11860 mutex_exit(&rp->r_statelock);
11851 11861 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
11852 11862 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT,
11853 11863 &recov_state, needrecov);
11854 11864 crfree(cred_otw);
11855 11865 if (osp != NULL)
11856 11866 open_stream_rele(osp, rp);
11857 11867 return (0);
11858 11868 }
11859 11869 nfs4_set_mod(vp);
11860 11870 rp->r_writeverf = cm_res->writeverf;
11861 11871 mutex_exit(&rp->r_statelock);
11862 11872 e.error = NFS_VERF_MISMATCH;
11863 11873 }
11864 11874
11865 11875 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
11866 11876 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, &recov_state, needrecov);
11867 11877 crfree(cred_otw);
11868 11878 if (osp != NULL)
11869 11879 open_stream_rele(osp, rp);
11870 11880
11871 11881 return (e.error);
11872 11882 }
11873 11883
11874 11884 static void
11875 11885 nfs4_set_mod(vnode_t *vp)
11876 11886 {
11877 11887 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
11878 11888
11879 11889 /* make sure we're looking at the master vnode, not a shadow */
11880 11890 pvn_vplist_setdirty(RTOV4(VTOR4(vp)), nfs_setmod_check);
11881 11891 }
11882 11892
11883 11893 /*
11884 11894 * This function is used to gather a page list of the pages which
11885 11895 * can be committed on the server.
11886 11896 *
11887 11897 * The calling thread must have set R4COMMIT. This bit is used to
11888 11898 * serialize access to the commit structure in the rnode. As long
11889 11899 * as the thread has set R4COMMIT, then it can manipulate the commit
11890 11900 * structure without requiring any other locks.
11891 11901 *
11892 11902 * When this function is called from nfs4_dispose() the page passed
11893 11903 * into nfs4_dispose() will be SE_EXCL locked, and so this function
11894 11904 * will skip it. This is not a problem since we initially add the
11895 11905 * page to the r_commit page list.
11896 11906 *
11897 11907 */
11898 11908 static void
11899 11909 nfs4_get_commit(vnode_t *vp)
11900 11910 {
11901 11911 rnode4_t *rp;
11902 11912 page_t *pp;
11903 11913 kmutex_t *vphm;
11904 11914
11905 11915 rp = VTOR4(vp);
11906 11916
11907 11917 ASSERT(rp->r_flags & R4COMMIT);
11908 11918
11909 11919 /* make sure we're looking at the master vnode, not a shadow */
11910 11920
11911 11921 if (IS_SHADOW(vp, rp))
11912 11922 vp = RTOV4(rp);
11913 11923
11914 11924 vphm = page_vnode_mutex(vp);
11915 11925 mutex_enter(vphm);
11916 11926
11917 11927 /*
11918 11928 * If there are no pages associated with this vnode, then
11919 11929 * just return.
11920 11930 */
11921 11931 if ((pp = vp->v_pages) == NULL) {
11922 11932 mutex_exit(vphm);
11923 11933 return;
11924 11934 }
11925 11935
11926 11936 /*
11927 11937 * Step through all of the pages associated with this vnode
11928 11938 * looking for pages which need to be committed.
11929 11939 */
11930 11940 do {
11931 11941 /* Skip marker pages. */
11932 11942 if (pp->p_hash == PVN_VPLIST_HASH_TAG)
11933 11943 continue;
11934 11944
11935 11945 /*
11936 11946 * First short-cut everything (without the page_lock)
11937 11947 * and see if this page does not need to be committed
11938 11948 * or is modified if so then we'll just skip it.
11939 11949 */
11940 11950 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp))
11941 11951 continue;
11942 11952
11943 11953 /*
11944 11954 * Attempt to lock the page. If we can't, then
11945 11955 * someone else is messing with it or we have been
11946 11956 * called from nfs4_dispose and this is the page that
11947 11957 * nfs4_dispose was called with.. anyway just skip it.
11948 11958 */
11949 11959 if (!page_trylock(pp, SE_EXCL))
11950 11960 continue;
11951 11961
11952 11962 /*
11953 11963 * Lets check again now that we have the page lock.
11954 11964 */
11955 11965 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) {
11956 11966 page_unlock(pp);
11957 11967 continue;
11958 11968 }
11959 11969
11960 11970 /* this had better not be a free page */
11961 11971 ASSERT(PP_ISFREE(pp) == 0);
11962 11972
11963 11973 /*
11964 11974 * The page needs to be committed and we locked it.
11965 11975 * Update the base and length parameters and add it
11966 11976 * to r_pages.
11967 11977 */
11968 11978 if (rp->r_commit.c_pages == NULL) {
11969 11979 rp->r_commit.c_commbase = (offset3)pp->p_offset;
11970 11980 rp->r_commit.c_commlen = PAGESIZE;
11971 11981 } else if (pp->p_offset < rp->r_commit.c_commbase) {
11972 11982 rp->r_commit.c_commlen = rp->r_commit.c_commbase -
11973 11983 (offset3)pp->p_offset + rp->r_commit.c_commlen;
11974 11984 rp->r_commit.c_commbase = (offset3)pp->p_offset;
11975 11985 } else if ((rp->r_commit.c_commbase + rp->r_commit.c_commlen)
11976 11986 <= pp->p_offset) {
11977 11987 rp->r_commit.c_commlen = (offset3)pp->p_offset -
11978 11988 rp->r_commit.c_commbase + PAGESIZE;
11979 11989 }
11980 11990 page_add(&rp->r_commit.c_pages, pp);
11981 11991 } while ((pp = pp->p_vpnext) != vp->v_pages);
11982 11992
11983 11993 mutex_exit(vphm);
11984 11994 }
11985 11995
11986 11996 /*
11987 11997 * This routine is used to gather together a page list of the pages
11988 11998 * which are to be committed on the server. This routine must not
11989 11999 * be called if the calling thread holds any locked pages.
11990 12000 *
11991 12001 * The calling thread must have set R4COMMIT. This bit is used to
11992 12002 * serialize access to the commit structure in the rnode. As long
11993 12003 * as the thread has set R4COMMIT, then it can manipulate the commit
11994 12004 * structure without requiring any other locks.
11995 12005 */
11996 12006 static void
11997 12007 nfs4_get_commit_range(vnode_t *vp, u_offset_t soff, size_t len)
11998 12008 {
11999 12009
12000 12010 rnode4_t *rp;
12001 12011 page_t *pp;
12002 12012 u_offset_t end;
12003 12013 u_offset_t off;
12004 12014 ASSERT(len != 0);
12005 12015 rp = VTOR4(vp);
12006 12016 ASSERT(rp->r_flags & R4COMMIT);
12007 12017
12008 12018 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12009 12019
12010 12020 /* make sure we're looking at the master vnode, not a shadow */
12011 12021
12012 12022 if (IS_SHADOW(vp, rp))
12013 12023 vp = RTOV4(rp);
12014 12024
12015 12025 /*
12016 12026 * If there are no pages associated with this vnode, then
12017 12027 * just return.
12018 12028 */
12019 12029 if ((pp = vp->v_pages) == NULL)
12020 12030 return;
12021 12031 /*
12022 12032 * Calculate the ending offset.
12023 12033 */
12024 12034 end = soff + len;
12025 12035 for (off = soff; off < end; off += PAGESIZE) {
12026 12036 /*
12027 12037 * Lookup each page by vp, offset.
12028 12038 */
12029 12039 if ((pp = page_lookup_nowait(vp, off, SE_EXCL)) == NULL)
12030 12040 continue;
12031 12041 /*
12032 12042 * If this page does not need to be committed or is
12033 12043 * modified, then just skip it.
12034 12044 */
12035 12045 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) {
12036 12046 page_unlock(pp);
12037 12047 continue;
12038 12048 }
12039 12049
12040 12050 ASSERT(PP_ISFREE(pp) == 0);
12041 12051 /*
12042 12052 * The page needs to be committed and we locked it.
12043 12053 * Update the base and length parameters and add it
12044 12054 * to r_pages.
12045 12055 */
12046 12056 if (rp->r_commit.c_pages == NULL) {
12047 12057 rp->r_commit.c_commbase = (offset3)pp->p_offset;
12048 12058 rp->r_commit.c_commlen = PAGESIZE;
12049 12059 } else {
12050 12060 rp->r_commit.c_commlen = (offset3)pp->p_offset -
12051 12061 rp->r_commit.c_commbase + PAGESIZE;
12052 12062 }
12053 12063 page_add(&rp->r_commit.c_pages, pp);
12054 12064 }
12055 12065 }
12056 12066
12057 12067 /*
12058 12068 * Called from nfs4_close(), nfs4_fsync() and nfs4_delmap().
12059 12069 * Flushes and commits data to the server.
12060 12070 */
12061 12071 static int
12062 12072 nfs4_putpage_commit(vnode_t *vp, offset_t poff, size_t plen, cred_t *cr)
12063 12073 {
12064 12074 int error;
12065 12075 verifier4 write_verf;
12066 12076 rnode4_t *rp = VTOR4(vp);
12067 12077
12068 12078 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12069 12079
12070 12080 /*
12071 12081 * Flush the data portion of the file and then commit any
12072 12082 * portions which need to be committed. This may need to
12073 12083 * be done twice if the server has changed state since
12074 12084 * data was last written. The data will need to be
12075 12085 * rewritten to the server and then a new commit done.
12076 12086 *
12077 12087 * In fact, this may need to be done several times if the
12078 12088 * server is having problems and crashing while we are
12079 12089 * attempting to do this.
12080 12090 */
12081 12091
12082 12092 top:
12083 12093 /*
12084 12094 * Do a flush based on the poff and plen arguments. This
12085 12095 * will synchronously write out any modified pages in the
12086 12096 * range specified by (poff, plen). This starts all of the
12087 12097 * i/o operations which will be waited for in the next
12088 12098 * call to nfs4_putpage
12089 12099 */
12090 12100
12091 12101 mutex_enter(&rp->r_statelock);
12092 12102 write_verf = rp->r_writeverf;
12093 12103 mutex_exit(&rp->r_statelock);
12094 12104
12095 12105 error = nfs4_putpage(vp, poff, plen, B_ASYNC, cr, NULL);
12096 12106 if (error == EAGAIN)
12097 12107 error = 0;
12098 12108
12099 12109 /*
12100 12110 * Do a flush based on the poff and plen arguments. This
12101 12111 * will synchronously write out any modified pages in the
12102 12112 * range specified by (poff, plen) and wait until all of
12103 12113 * the asynchronous i/o's in that range are done as well.
12104 12114 */
12105 12115 if (!error)
12106 12116 error = nfs4_putpage(vp, poff, plen, 0, cr, NULL);
12107 12117
12108 12118 if (error)
12109 12119 return (error);
12110 12120
12111 12121 mutex_enter(&rp->r_statelock);
12112 12122 if (rp->r_writeverf != write_verf) {
12113 12123 mutex_exit(&rp->r_statelock);
12114 12124 goto top;
12115 12125 }
12116 12126 mutex_exit(&rp->r_statelock);
12117 12127
12118 12128 /*
12119 12129 * Now commit any pages which might need to be committed.
12120 12130 * If the error, NFS_VERF_MISMATCH, is returned, then
12121 12131 * start over with the flush operation.
12122 12132 */
12123 12133 error = nfs4_commit_vp(vp, poff, plen, cr, NFS4_WRITE_WAIT);
12124 12134
12125 12135 if (error == NFS_VERF_MISMATCH)
12126 12136 goto top;
12127 12137
12128 12138 return (error);
12129 12139 }
12130 12140
12131 12141 /*
12132 12142 * nfs4_commit_vp() will wait for other pending commits and
12133 12143 * will either commit the whole file or a range, plen dictates
12134 12144 * if we commit whole file. a value of zero indicates the whole
12135 12145 * file. Called from nfs4_putpage_commit() or nfs4_sync_putapage()
12136 12146 */
12137 12147 static int
12138 12148 nfs4_commit_vp(vnode_t *vp, u_offset_t poff, size_t plen,
12139 12149 cred_t *cr, int wait_on_writes)
12140 12150 {
12141 12151 rnode4_t *rp;
12142 12152 page_t *plist;
12143 12153 offset3 offset;
12144 12154 count3 len;
12145 12155
12146 12156 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12147 12157
12148 12158 rp = VTOR4(vp);
12149 12159
12150 12160 /*
12151 12161 * before we gather commitable pages make
12152 12162 * sure there are no outstanding async writes
12153 12163 */
12154 12164 if (rp->r_count && wait_on_writes == NFS4_WRITE_WAIT) {
12155 12165 mutex_enter(&rp->r_statelock);
12156 12166 while (rp->r_count > 0) {
12157 12167 cv_wait(&rp->r_cv, &rp->r_statelock);
12158 12168 }
12159 12169 mutex_exit(&rp->r_statelock);
12160 12170 }
12161 12171
12162 12172 /*
12163 12173 * Set the `commit inprogress' state bit. We must
12164 12174 * first wait until any current one finishes.
12165 12175 */
12166 12176 mutex_enter(&rp->r_statelock);
12167 12177 while (rp->r_flags & R4COMMIT) {
12168 12178 rp->r_flags |= R4COMMITWAIT;
12169 12179 cv_wait(&rp->r_commit.c_cv, &rp->r_statelock);
12170 12180 rp->r_flags &= ~R4COMMITWAIT;
12171 12181 }
12172 12182 rp->r_flags |= R4COMMIT;
12173 12183 mutex_exit(&rp->r_statelock);
12174 12184
12175 12185 /*
12176 12186 * Gather all of the pages which need to be
12177 12187 * committed.
12178 12188 */
12179 12189 if (plen == 0)
12180 12190 nfs4_get_commit(vp);
12181 12191 else
12182 12192 nfs4_get_commit_range(vp, poff, plen);
12183 12193
12184 12194 /*
12185 12195 * Clear the `commit inprogress' bit and disconnect the
12186 12196 * page list which was gathered by nfs4_get_commit.
12187 12197 */
12188 12198 plist = rp->r_commit.c_pages;
12189 12199 rp->r_commit.c_pages = NULL;
12190 12200 offset = rp->r_commit.c_commbase;
12191 12201 len = rp->r_commit.c_commlen;
12192 12202 mutex_enter(&rp->r_statelock);
12193 12203 rp->r_flags &= ~R4COMMIT;
12194 12204 cv_broadcast(&rp->r_commit.c_cv);
12195 12205 mutex_exit(&rp->r_statelock);
12196 12206
12197 12207 /*
12198 12208 * If any pages need to be committed, commit them and
12199 12209 * then unlock them so that they can be freed some
12200 12210 * time later.
12201 12211 */
12202 12212 if (plist == NULL)
12203 12213 return (0);
12204 12214
12205 12215 /*
12206 12216 * No error occurred during the flush portion
12207 12217 * of this operation, so now attempt to commit
12208 12218 * the data to stable storage on the server.
12209 12219 *
12210 12220 * This will unlock all of the pages on the list.
12211 12221 */
12212 12222 return (nfs4_sync_commit(vp, plist, offset, len, cr));
12213 12223 }
12214 12224
12215 12225 static int
12216 12226 nfs4_sync_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count,
12217 12227 cred_t *cr)
12218 12228 {
12219 12229 int error;
12220 12230 page_t *pp;
12221 12231
12222 12232 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12223 12233
12224 12234 error = nfs4_commit(vp, (offset4)offset, (count3)count, cr);
12225 12235
12226 12236 /*
12227 12237 * If we got an error, then just unlock all of the pages
12228 12238 * on the list.
12229 12239 */
12230 12240 if (error) {
12231 12241 while (plist != NULL) {
12232 12242 pp = plist;
12233 12243 page_sub(&plist, pp);
12234 12244 page_unlock(pp);
12235 12245 }
12236 12246 return (error);
12237 12247 }
12238 12248 /*
12239 12249 * We've tried as hard as we can to commit the data to stable
12240 12250 * storage on the server. We just unlock the pages and clear
12241 12251 * the commit required state. They will get freed later.
12242 12252 */
12243 12253 while (plist != NULL) {
12244 12254 pp = plist;
12245 12255 page_sub(&plist, pp);
12246 12256 pp->p_fsdata = C_NOCOMMIT;
12247 12257 page_unlock(pp);
12248 12258 }
12249 12259
12250 12260 return (error);
12251 12261 }
12252 12262
12253 12263 static void
12254 12264 do_nfs4_async_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count,
12255 12265 cred_t *cr)
12256 12266 {
12257 12267
12258 12268 (void) nfs4_sync_commit(vp, plist, offset, count, cr);
12259 12269 }
12260 12270
12261 12271 /*ARGSUSED*/
12262 12272 static int
12263 12273 nfs4_setsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr,
12264 12274 caller_context_t *ct)
12265 12275 {
12266 12276 int error = 0;
12267 12277 mntinfo4_t *mi;
12268 12278 vattr_t va;
12269 12279 vsecattr_t nfsace4_vsap;
12270 12280
12271 12281 mi = VTOMI4(vp);
12272 12282 if (nfs_zone() != mi->mi_zone)
12273 12283 return (EIO);
12274 12284 if (mi->mi_flags & MI4_ACL) {
12275 12285 /* if we have a delegation, return it */
12276 12286 if (VTOR4(vp)->r_deleg_type != OPEN_DELEGATE_NONE)
12277 12287 (void) nfs4delegreturn(VTOR4(vp),
12278 12288 NFS4_DR_REOPEN|NFS4_DR_PUSH);
12279 12289
12280 12290 error = nfs4_is_acl_mask_valid(vsecattr->vsa_mask,
12281 12291 NFS4_ACL_SET);
12282 12292 if (error) /* EINVAL */
12283 12293 return (error);
12284 12294
12285 12295 if (vsecattr->vsa_mask & (VSA_ACL | VSA_DFACL)) {
12286 12296 /*
12287 12297 * These are aclent_t type entries.
12288 12298 */
12289 12299 error = vs_aent_to_ace4(vsecattr, &nfsace4_vsap,
12290 12300 vp->v_type == VDIR, FALSE);
12291 12301 if (error)
12292 12302 return (error);
12293 12303 } else {
12294 12304 /*
12295 12305 * These are ace_t type entries.
12296 12306 */
12297 12307 error = vs_acet_to_ace4(vsecattr, &nfsace4_vsap,
12298 12308 FALSE);
12299 12309 if (error)
12300 12310 return (error);
12301 12311 }
12302 12312 bzero(&va, sizeof (va));
12303 12313 error = nfs4setattr(vp, &va, flag, cr, &nfsace4_vsap);
12304 12314 vs_ace4_destroy(&nfsace4_vsap);
12305 12315 return (error);
12306 12316 }
12307 12317 return (ENOSYS);
12308 12318 }
12309 12319
12310 12320 /* ARGSUSED */
12311 12321 int
12312 12322 nfs4_getsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr,
12313 12323 caller_context_t *ct)
12314 12324 {
12315 12325 int error;
12316 12326 mntinfo4_t *mi;
12317 12327 nfs4_ga_res_t gar;
12318 12328 rnode4_t *rp = VTOR4(vp);
12319 12329
12320 12330 mi = VTOMI4(vp);
12321 12331 if (nfs_zone() != mi->mi_zone)
12322 12332 return (EIO);
12323 12333
12324 12334 bzero(&gar, sizeof (gar));
12325 12335 gar.n4g_vsa.vsa_mask = vsecattr->vsa_mask;
12326 12336
12327 12337 /*
12328 12338 * vsecattr->vsa_mask holds the original acl request mask.
12329 12339 * This is needed when determining what to return.
12330 12340 * (See: nfs4_create_getsecattr_return())
12331 12341 */
12332 12342 error = nfs4_is_acl_mask_valid(vsecattr->vsa_mask, NFS4_ACL_GET);
12333 12343 if (error) /* EINVAL */
12334 12344 return (error);
12335 12345
12336 12346 /*
12337 12347 * If this is a referral stub, don't try to go OTW for an ACL
12338 12348 */
12339 12349 if (RP_ISSTUB_REFERRAL(VTOR4(vp)))
12340 12350 return (fs_fab_acl(vp, vsecattr, flag, cr, ct));
12341 12351
12342 12352 if (mi->mi_flags & MI4_ACL) {
12343 12353 /*
12344 12354 * Check if the data is cached and the cache is valid. If it
12345 12355 * is we don't go over the wire.
12346 12356 */
12347 12357 if (rp->r_secattr != NULL && ATTRCACHE4_VALID(vp)) {
12348 12358 mutex_enter(&rp->r_statelock);
12349 12359 if (rp->r_secattr != NULL) {
12350 12360 error = nfs4_create_getsecattr_return(
12351 12361 rp->r_secattr, vsecattr, rp->r_attr.va_uid,
12352 12362 rp->r_attr.va_gid,
12353 12363 vp->v_type == VDIR);
12354 12364 if (!error) { /* error == 0 - Success! */
12355 12365 mutex_exit(&rp->r_statelock);
12356 12366 return (error);
12357 12367 }
12358 12368 }
12359 12369 mutex_exit(&rp->r_statelock);
12360 12370 }
12361 12371
12362 12372 /*
12363 12373 * The getattr otw call will always get both the acl, in
12364 12374 * the form of a list of nfsace4's, and the number of acl
12365 12375 * entries; independent of the value of gar.n4g_vsa.vsa_mask.
12366 12376 */
12367 12377 gar.n4g_va.va_mask = AT_ALL;
12368 12378 error = nfs4_getattr_otw(vp, &gar, cr, 1);
12369 12379 if (error) {
12370 12380 vs_ace4_destroy(&gar.n4g_vsa);
12371 12381 if (error == ENOTSUP || error == EOPNOTSUPP)
12372 12382 error = fs_fab_acl(vp, vsecattr, flag, cr, ct);
12373 12383 return (error);
12374 12384 }
12375 12385
12376 12386 if (!(gar.n4g_resbmap & FATTR4_ACL_MASK)) {
12377 12387 /*
12378 12388 * No error was returned, but according to the response
12379 12389 * bitmap, neither was an acl.
12380 12390 */
12381 12391 vs_ace4_destroy(&gar.n4g_vsa);
12382 12392 error = fs_fab_acl(vp, vsecattr, flag, cr, ct);
12383 12393 return (error);
12384 12394 }
12385 12395
12386 12396 /*
12387 12397 * Update the cache with the ACL.
12388 12398 */
12389 12399 nfs4_acl_fill_cache(rp, &gar.n4g_vsa);
12390 12400
12391 12401 error = nfs4_create_getsecattr_return(&gar.n4g_vsa,
12392 12402 vsecattr, gar.n4g_va.va_uid, gar.n4g_va.va_gid,
12393 12403 vp->v_type == VDIR);
12394 12404 vs_ace4_destroy(&gar.n4g_vsa);
12395 12405 if ((error) && (vsecattr->vsa_mask &
12396 12406 (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT)) &&
12397 12407 (error != EACCES)) {
12398 12408 error = fs_fab_acl(vp, vsecattr, flag, cr, ct);
12399 12409 }
12400 12410 return (error);
12401 12411 }
12402 12412 error = fs_fab_acl(vp, vsecattr, flag, cr, ct);
12403 12413 return (error);
12404 12414 }
12405 12415
12406 12416 /*
12407 12417 * The function returns:
12408 12418 * - 0 (zero) if the passed in "acl_mask" is a valid request.
12409 12419 * - EINVAL if the passed in "acl_mask" is an invalid request.
12410 12420 *
12411 12421 * In the case of getting an acl (op == NFS4_ACL_GET) the mask is invalid if:
12412 12422 * - We have a mixture of ACE and ACL requests (e.g. VSA_ACL | VSA_ACE)
12413 12423 *
12414 12424 * In the case of setting an acl (op == NFS4_ACL_SET) the mask is invalid if:
12415 12425 * - We have a mixture of ACE and ACL requests (e.g. VSA_ACL | VSA_ACE)
12416 12426 * - We have a count field set without the corresponding acl field set. (e.g. -
12417 12427 * VSA_ACECNT is set, but VSA_ACE is not)
12418 12428 */
12419 12429 static int
12420 12430 nfs4_is_acl_mask_valid(uint_t acl_mask, nfs4_acl_op_t op)
12421 12431 {
12422 12432 /* Shortcut the masks that are always valid. */
12423 12433 if (acl_mask == (VSA_ACE | VSA_ACECNT))
12424 12434 return (0);
12425 12435 if (acl_mask == (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT))
12426 12436 return (0);
12427 12437
12428 12438 if (acl_mask & (VSA_ACE | VSA_ACECNT)) {
12429 12439 /*
12430 12440 * We can't have any VSA_ACL type stuff in the mask now.
12431 12441 */
12432 12442 if (acl_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL |
12433 12443 VSA_DFACLCNT))
12434 12444 return (EINVAL);
12435 12445
12436 12446 if (op == NFS4_ACL_SET) {
12437 12447 if ((acl_mask & VSA_ACECNT) && !(acl_mask & VSA_ACE))
12438 12448 return (EINVAL);
12439 12449 }
12440 12450 }
12441 12451
12442 12452 if (acl_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT)) {
12443 12453 /*
12444 12454 * We can't have any VSA_ACE type stuff in the mask now.
12445 12455 */
12446 12456 if (acl_mask & (VSA_ACE | VSA_ACECNT))
12447 12457 return (EINVAL);
12448 12458
12449 12459 if (op == NFS4_ACL_SET) {
12450 12460 if ((acl_mask & VSA_ACLCNT) && !(acl_mask & VSA_ACL))
12451 12461 return (EINVAL);
12452 12462
12453 12463 if ((acl_mask & VSA_DFACLCNT) &&
12454 12464 !(acl_mask & VSA_DFACL))
12455 12465 return (EINVAL);
12456 12466 }
12457 12467 }
12458 12468 return (0);
12459 12469 }
12460 12470
12461 12471 /*
12462 12472 * The theory behind creating the correct getsecattr return is simply this:
12463 12473 * "Don't return anything that the caller is not expecting to have to free."
12464 12474 */
12465 12475 static int
12466 12476 nfs4_create_getsecattr_return(vsecattr_t *filled_vsap, vsecattr_t *vsap,
12467 12477 uid_t uid, gid_t gid, int isdir)
12468 12478 {
12469 12479 int error = 0;
12470 12480 /* Save the mask since the translators modify it. */
12471 12481 uint_t orig_mask = vsap->vsa_mask;
12472 12482
12473 12483 if (orig_mask & (VSA_ACE | VSA_ACECNT)) {
12474 12484 error = vs_ace4_to_acet(filled_vsap, vsap, uid, gid, FALSE);
12475 12485
12476 12486 if (error)
12477 12487 return (error);
12478 12488
12479 12489 /*
12480 12490 * If the caller only asked for the ace count (VSA_ACECNT)
12481 12491 * don't give them the full acl (VSA_ACE), free it.
12482 12492 */
12483 12493 if (!orig_mask & VSA_ACE) {
12484 12494 if (vsap->vsa_aclentp != NULL) {
12485 12495 kmem_free(vsap->vsa_aclentp,
12486 12496 vsap->vsa_aclcnt * sizeof (ace_t));
12487 12497 vsap->vsa_aclentp = NULL;
12488 12498 }
12489 12499 }
12490 12500 vsap->vsa_mask = orig_mask;
12491 12501
12492 12502 } else if (orig_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL |
12493 12503 VSA_DFACLCNT)) {
12494 12504 error = vs_ace4_to_aent(filled_vsap, vsap, uid, gid,
12495 12505 isdir, FALSE);
12496 12506
12497 12507 if (error)
12498 12508 return (error);
12499 12509
12500 12510 /*
12501 12511 * If the caller only asked for the acl count (VSA_ACLCNT)
12502 12512 * and/or the default acl count (VSA_DFACLCNT) don't give them
12503 12513 * the acl (VSA_ACL) or default acl (VSA_DFACL), free it.
12504 12514 */
12505 12515 if (!orig_mask & VSA_ACL) {
12506 12516 if (vsap->vsa_aclentp != NULL) {
12507 12517 kmem_free(vsap->vsa_aclentp,
12508 12518 vsap->vsa_aclcnt * sizeof (aclent_t));
12509 12519 vsap->vsa_aclentp = NULL;
12510 12520 }
12511 12521 }
12512 12522
12513 12523 if (!orig_mask & VSA_DFACL) {
12514 12524 if (vsap->vsa_dfaclentp != NULL) {
12515 12525 kmem_free(vsap->vsa_dfaclentp,
12516 12526 vsap->vsa_dfaclcnt * sizeof (aclent_t));
12517 12527 vsap->vsa_dfaclentp = NULL;
12518 12528 }
12519 12529 }
12520 12530 vsap->vsa_mask = orig_mask;
12521 12531 }
12522 12532 return (0);
12523 12533 }
12524 12534
12525 12535 /* ARGSUSED */
12526 12536 int
12527 12537 nfs4_shrlock(vnode_t *vp, int cmd, struct shrlock *shr, int flag, cred_t *cr,
12528 12538 caller_context_t *ct)
12529 12539 {
12530 12540 int error;
12531 12541
12532 12542 if (nfs_zone() != VTOMI4(vp)->mi_zone)
12533 12543 return (EIO);
12534 12544 /*
12535 12545 * check for valid cmd parameter
12536 12546 */
12537 12547 if (cmd != F_SHARE && cmd != F_UNSHARE && cmd != F_HASREMOTELOCKS)
12538 12548 return (EINVAL);
12539 12549
12540 12550 /*
12541 12551 * Check access permissions
12542 12552 */
12543 12553 if ((cmd & F_SHARE) &&
12544 12554 (((shr->s_access & F_RDACC) && (flag & FREAD) == 0) ||
12545 12555 (shr->s_access == F_WRACC && (flag & FWRITE) == 0)))
12546 12556 return (EBADF);
12547 12557
12548 12558 /*
12549 12559 * If the filesystem is mounted using local locking, pass the
12550 12560 * request off to the local share code.
12551 12561 */
12552 12562 if (VTOMI4(vp)->mi_flags & MI4_LLOCK)
12553 12563 return (fs_shrlock(vp, cmd, shr, flag, cr, ct));
12554 12564
12555 12565 switch (cmd) {
12556 12566 case F_SHARE:
12557 12567 case F_UNSHARE:
12558 12568 /*
12559 12569 * This will be properly implemented later,
12560 12570 * see RFE: 4823948 .
12561 12571 */
12562 12572 error = EAGAIN;
12563 12573 break;
12564 12574
12565 12575 case F_HASREMOTELOCKS:
12566 12576 /*
12567 12577 * NFS client can't store remote locks itself
12568 12578 */
12569 12579 shr->s_access = 0;
12570 12580 error = 0;
12571 12581 break;
12572 12582
12573 12583 default:
12574 12584 error = EINVAL;
12575 12585 break;
12576 12586 }
12577 12587
12578 12588 return (error);
12579 12589 }
12580 12590
12581 12591 /*
12582 12592 * Common code called by directory ops to update the attrcache
12583 12593 */
12584 12594 static int
12585 12595 nfs4_update_attrcache(nfsstat4 status, nfs4_ga_res_t *garp,
12586 12596 hrtime_t t, vnode_t *vp, cred_t *cr)
12587 12597 {
12588 12598 int error = 0;
12589 12599
12590 12600 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12591 12601
12592 12602 if (status != NFS4_OK) {
12593 12603 /* getattr not done or failed */
12594 12604 PURGE_ATTRCACHE4(vp);
12595 12605 return (error);
12596 12606 }
12597 12607
12598 12608 if (garp) {
12599 12609 nfs4_attr_cache(vp, garp, t, cr, FALSE, NULL);
12600 12610 } else {
12601 12611 PURGE_ATTRCACHE4(vp);
12602 12612 }
12603 12613 return (error);
12604 12614 }
12605 12615
12606 12616 /*
12607 12617 * Update directory caches for directory modification ops (link, rename, etc.)
12608 12618 * When dinfo is NULL, manage dircaches in the old way.
12609 12619 */
12610 12620 static void
12611 12621 nfs4_update_dircaches(change_info4 *cinfo, vnode_t *dvp, vnode_t *vp, char *nm,
12612 12622 dirattr_info_t *dinfo)
12613 12623 {
12614 12624 rnode4_t *drp = VTOR4(dvp);
12615 12625
12616 12626 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
12617 12627
12618 12628 /* Purge rddir cache for dir since it changed */
12619 12629 if (drp->r_dir != NULL)
12620 12630 nfs4_purge_rddir_cache(dvp);
12621 12631
12622 12632 /*
12623 12633 * If caller provided dinfo, then use it to manage dir caches.
12624 12634 */
12625 12635 if (dinfo != NULL) {
12626 12636 if (vp != NULL) {
12627 12637 mutex_enter(&VTOR4(vp)->r_statev4_lock);
12628 12638 if (!VTOR4(vp)->created_v4) {
12629 12639 mutex_exit(&VTOR4(vp)->r_statev4_lock);
12630 12640 dnlc_update(dvp, nm, vp);
12631 12641 } else {
12632 12642 /*
12633 12643 * XXX don't update if the created_v4 flag is
12634 12644 * set
12635 12645 */
12636 12646 mutex_exit(&VTOR4(vp)->r_statev4_lock);
12637 12647 NFS4_DEBUG(nfs4_client_state_debug,
12638 12648 (CE_NOTE, "nfs4_update_dircaches: "
12639 12649 "don't update dnlc: created_v4 flag"));
12640 12650 }
12641 12651 }
12642 12652
12643 12653 nfs4_attr_cache(dvp, dinfo->di_garp, dinfo->di_time_call,
12644 12654 dinfo->di_cred, FALSE, cinfo);
12645 12655
12646 12656 return;
12647 12657 }
12648 12658
12649 12659 /*
12650 12660 * Caller didn't provide dinfo, then check change_info4 to update DNLC.
12651 12661 * Since caller modified dir but didn't receive post-dirmod-op dir
12652 12662 * attrs, the dir's attrs must be purged.
12653 12663 *
12654 12664 * XXX this check and dnlc update/purge should really be atomic,
12655 12665 * XXX but can't use rnode statelock because it'll deadlock in
12656 12666 * XXX dnlc_purge_vp, however, the risk is minimal even if a race
12657 12667 * XXX does occur.
12658 12668 *
12659 12669 * XXX We also may want to check that atomic is true in the
12660 12670 * XXX change_info struct. If it is not, the change_info may
12661 12671 * XXX reflect changes by more than one clients which means that
12662 12672 * XXX our cache may not be valid.
12663 12673 */
12664 12674 PURGE_ATTRCACHE4(dvp);
12665 12675 if (drp->r_change == cinfo->before) {
12666 12676 /* no changes took place in the directory prior to our link */
12667 12677 if (vp != NULL) {
12668 12678 mutex_enter(&VTOR4(vp)->r_statev4_lock);
12669 12679 if (!VTOR4(vp)->created_v4) {
12670 12680 mutex_exit(&VTOR4(vp)->r_statev4_lock);
12671 12681 dnlc_update(dvp, nm, vp);
12672 12682 } else {
12673 12683 /*
12674 12684 * XXX dont' update if the created_v4 flag
12675 12685 * is set
12676 12686 */
12677 12687 mutex_exit(&VTOR4(vp)->r_statev4_lock);
12678 12688 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE,
12679 12689 "nfs4_update_dircaches: don't"
12680 12690 " update dnlc: created_v4 flag"));
12681 12691 }
12682 12692 }
12683 12693 } else {
12684 12694 /* Another client modified directory - purge its dnlc cache */
12685 12695 dnlc_purge_vp(dvp);
12686 12696 }
12687 12697 }
12688 12698
12689 12699 /*
12690 12700 * The OPEN_CONFIRM operation confirms the sequence number used in OPENing a
12691 12701 * file.
12692 12702 *
12693 12703 * The 'reopening_file' boolean should be set to TRUE if we are reopening this
12694 12704 * file (ie: client recovery) and otherwise set to FALSE.
12695 12705 *
12696 12706 * 'nfs4_start/end_op' should have been called by the proper (ie: not recovery
12697 12707 * initiated) calling functions.
12698 12708 *
12699 12709 * 'resend' is set to TRUE if this is a OPEN_CONFIRM issued as a result
12700 12710 * of resending a 'lost' open request.
12701 12711 *
12702 12712 * 'num_bseqid_retryp' makes sure we don't loop forever on a broken
12703 12713 * server that hands out BAD_SEQID on open confirm.
12704 12714 *
12705 12715 * Errors are returned via the nfs4_error_t parameter.
12706 12716 */
12707 12717 void
12708 12718 nfs4open_confirm(vnode_t *vp, seqid4 *seqid, stateid4 *stateid, cred_t *cr,
12709 12719 bool_t reopening_file, bool_t *retry_open, nfs4_open_owner_t *oop,
12710 12720 bool_t resend, nfs4_error_t *ep, int *num_bseqid_retryp)
12711 12721 {
12712 12722 COMPOUND4args_clnt args;
12713 12723 COMPOUND4res_clnt res;
12714 12724 nfs_argop4 argop[2];
12715 12725 nfs_resop4 *resop;
12716 12726 int doqueue = 1;
12717 12727 mntinfo4_t *mi;
12718 12728 OPEN_CONFIRM4args *open_confirm_args;
12719 12729 int needrecov;
12720 12730
12721 12731 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12722 12732 #if DEBUG
12723 12733 mutex_enter(&oop->oo_lock);
12724 12734 ASSERT(oop->oo_seqid_inuse);
12725 12735 mutex_exit(&oop->oo_lock);
12726 12736 #endif
12727 12737
12728 12738 recov_retry_confirm:
12729 12739 nfs4_error_zinit(ep);
12730 12740 *retry_open = FALSE;
12731 12741
12732 12742 if (resend)
12733 12743 args.ctag = TAG_OPEN_CONFIRM_LOST;
12734 12744 else
12735 12745 args.ctag = TAG_OPEN_CONFIRM;
12736 12746
12737 12747 args.array_len = 2;
12738 12748 args.array = argop;
12739 12749
12740 12750 /* putfh target fh */
12741 12751 argop[0].argop = OP_CPUTFH;
12742 12752 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh;
12743 12753
12744 12754 argop[1].argop = OP_OPEN_CONFIRM;
12745 12755 open_confirm_args = &argop[1].nfs_argop4_u.opopen_confirm;
12746 12756
12747 12757 (*seqid) += 1;
12748 12758 open_confirm_args->seqid = *seqid;
12749 12759 open_confirm_args->open_stateid = *stateid;
12750 12760
12751 12761 mi = VTOMI4(vp);
12752 12762
12753 12763 rfs4call(mi, &args, &res, cr, &doqueue, 0, ep);
12754 12764
12755 12765 if (!ep->error && nfs4_need_to_bump_seqid(&res)) {
12756 12766 nfs4_set_open_seqid((*seqid), oop, args.ctag);
12757 12767 }
12758 12768
12759 12769 needrecov = nfs4_needs_recovery(ep, FALSE, mi->mi_vfsp);
12760 12770 if (!needrecov && ep->error)
12761 12771 return;
12762 12772
12763 12773 if (needrecov) {
12764 12774 bool_t abort = FALSE;
12765 12775
12766 12776 if (reopening_file == FALSE) {
12767 12777 nfs4_bseqid_entry_t *bsep = NULL;
12768 12778
12769 12779 if (!ep->error && res.status == NFS4ERR_BAD_SEQID)
12770 12780 bsep = nfs4_create_bseqid_entry(oop, NULL,
12771 12781 vp, 0, args.ctag,
12772 12782 open_confirm_args->seqid);
12773 12783
12774 12784 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL,
12775 12785 NULL, NULL, OP_OPEN_CONFIRM, bsep, NULL, NULL);
12776 12786 if (bsep) {
12777 12787 kmem_free(bsep, sizeof (*bsep));
12778 12788 if (num_bseqid_retryp &&
12779 12789 --(*num_bseqid_retryp) == 0)
12780 12790 abort = TRUE;
12781 12791 }
12782 12792 }
12783 12793 if ((ep->error == ETIMEDOUT ||
12784 12794 res.status == NFS4ERR_RESOURCE) &&
12785 12795 abort == FALSE && resend == FALSE) {
12786 12796 if (!ep->error)
12787 12797 (void) xdr_free(xdr_COMPOUND4res_clnt,
12788 12798 (caddr_t)&res);
12789 12799
12790 12800 delay(SEC_TO_TICK(confirm_retry_sec));
12791 12801 goto recov_retry_confirm;
12792 12802 }
12793 12803 /* State may have changed so retry the entire OPEN op */
12794 12804 if (abort == FALSE)
12795 12805 *retry_open = TRUE;
12796 12806 else
12797 12807 *retry_open = FALSE;
12798 12808 if (!ep->error)
12799 12809 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
12800 12810 return;
12801 12811 }
12802 12812
12803 12813 if (res.status) {
12804 12814 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
12805 12815 return;
12806 12816 }
12807 12817
12808 12818 resop = &res.array[1]; /* open confirm res */
12809 12819 bcopy(&resop->nfs_resop4_u.opopen_confirm.open_stateid,
12810 12820 stateid, sizeof (*stateid));
12811 12821
12812 12822 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
12813 12823 }
12814 12824
12815 12825 /*
12816 12826 * Return the credentials associated with a client state object. The
12817 12827 * caller is responsible for freeing the credentials.
12818 12828 */
12819 12829
12820 12830 static cred_t *
12821 12831 state_to_cred(nfs4_open_stream_t *osp)
12822 12832 {
12823 12833 cred_t *cr;
12824 12834
12825 12835 /*
12826 12836 * It's ok to not lock the open stream and open owner to get
12827 12837 * the oo_cred since this is only written once (upon creation)
12828 12838 * and will not change.
12829 12839 */
12830 12840 cr = osp->os_open_owner->oo_cred;
12831 12841 crhold(cr);
12832 12842
12833 12843 return (cr);
12834 12844 }
12835 12845
12836 12846 /*
12837 12847 * nfs4_find_sysid
12838 12848 *
12839 12849 * Find the sysid for the knetconfig associated with the given mi.
12840 12850 */
12841 12851 static struct lm_sysid *
12842 12852 nfs4_find_sysid(mntinfo4_t *mi)
12843 12853 {
12844 12854 ASSERT(nfs_zone() == mi->mi_zone);
12845 12855
12846 12856 /*
12847 12857 * Switch from RDMA knconf to original mount knconf
12848 12858 */
12849 12859 return (lm_get_sysid(ORIG_KNCONF(mi), &mi->mi_curr_serv->sv_addr,
12850 12860 mi->mi_curr_serv->sv_hostname, NULL));
12851 12861 }
12852 12862
12853 12863 #ifdef DEBUG
12854 12864 /*
12855 12865 * Return a string version of the call type for easy reading.
12856 12866 */
12857 12867 static char *
12858 12868 nfs4frlock_get_call_type(nfs4_lock_call_type_t ctype)
12859 12869 {
12860 12870 switch (ctype) {
12861 12871 case NFS4_LCK_CTYPE_NORM:
12862 12872 return ("NORMAL");
12863 12873 case NFS4_LCK_CTYPE_RECLAIM:
12864 12874 return ("RECLAIM");
12865 12875 case NFS4_LCK_CTYPE_RESEND:
12866 12876 return ("RESEND");
12867 12877 case NFS4_LCK_CTYPE_REINSTATE:
12868 12878 return ("REINSTATE");
12869 12879 default:
12870 12880 cmn_err(CE_PANIC, "nfs4frlock_get_call_type: got illegal "
12871 12881 "type %d", ctype);
12872 12882 return ("");
12873 12883 }
12874 12884 }
12875 12885 #endif
12876 12886
12877 12887 /*
12878 12888 * Map the frlock cmd and lock type to the NFSv4 over-the-wire lock type
12879 12889 * Unlock requests don't have an over-the-wire locktype, so we just return
12880 12890 * something non-threatening.
12881 12891 */
12882 12892
12883 12893 static nfs_lock_type4
12884 12894 flk_to_locktype(int cmd, int l_type)
12885 12895 {
12886 12896 ASSERT(l_type == F_RDLCK || l_type == F_WRLCK || l_type == F_UNLCK);
12887 12897
12888 12898 switch (l_type) {
12889 12899 case F_UNLCK:
12890 12900 return (READ_LT);
12891 12901 case F_RDLCK:
12892 12902 if (cmd == F_SETLK)
12893 12903 return (READ_LT);
12894 12904 else
12895 12905 return (READW_LT);
12896 12906 case F_WRLCK:
12897 12907 if (cmd == F_SETLK)
12898 12908 return (WRITE_LT);
12899 12909 else
12900 12910 return (WRITEW_LT);
12901 12911 }
12902 12912 panic("flk_to_locktype");
12903 12913 /*NOTREACHED*/
12904 12914 }
12905 12915
12906 12916 /*
12907 12917 * Do some preliminary checks for nfs4frlock.
12908 12918 */
12909 12919 static int
12910 12920 nfs4frlock_validate_args(int cmd, flock64_t *flk, int flag, vnode_t *vp,
12911 12921 u_offset_t offset)
12912 12922 {
12913 12923 int error = 0;
12914 12924
12915 12925 /*
12916 12926 * If we are setting a lock, check that the file is opened
12917 12927 * with the correct mode.
12918 12928 */
12919 12929 if (cmd == F_SETLK || cmd == F_SETLKW) {
12920 12930 if ((flk->l_type == F_RDLCK && (flag & FREAD) == 0) ||
12921 12931 (flk->l_type == F_WRLCK && (flag & FWRITE) == 0)) {
12922 12932 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
12923 12933 "nfs4frlock_validate_args: file was opened with "
12924 12934 "incorrect mode"));
12925 12935 return (EBADF);
12926 12936 }
12927 12937 }
12928 12938
12929 12939 /* Convert the offset. It may need to be restored before returning. */
12930 12940 if (error = convoff(vp, flk, 0, offset)) {
12931 12941 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
12932 12942 "nfs4frlock_validate_args: convoff => error= %d\n",
12933 12943 error));
12934 12944 return (error);
12935 12945 }
12936 12946
12937 12947 return (error);
12938 12948 }
12939 12949
12940 12950 /*
12941 12951 * Set the flock64's lm_sysid for nfs4frlock.
12942 12952 */
12943 12953 static int
12944 12954 nfs4frlock_get_sysid(struct lm_sysid **lspp, vnode_t *vp, flock64_t *flk)
12945 12955 {
12946 12956 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12947 12957
12948 12958 /* Find the lm_sysid */
12949 12959 *lspp = nfs4_find_sysid(VTOMI4(vp));
12950 12960
12951 12961 if (*lspp == NULL) {
12952 12962 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
12953 12963 "nfs4frlock_get_sysid: no sysid, return ENOLCK"));
12954 12964 return (ENOLCK);
12955 12965 }
12956 12966
12957 12967 flk->l_sysid = lm_sysidt(*lspp);
12958 12968
12959 12969 return (0);
12960 12970 }
12961 12971
12962 12972 /*
12963 12973 * Do the remaining preliminary setup for nfs4frlock.
12964 12974 */
12965 12975 static void
12966 12976 nfs4frlock_pre_setup(clock_t *tick_delayp, nfs4_recov_state_t *recov_statep,
12967 12977 flock64_t *flk, short *whencep, vnode_t *vp, cred_t *search_cr,
12968 12978 cred_t **cred_otw)
12969 12979 {
12970 12980 /*
12971 12981 * set tick_delay to the base delay time.
12972 12982 * (NFS4_BASE_WAIT_TIME is in secs)
12973 12983 */
12974 12984
12975 12985 *tick_delayp = drv_usectohz(NFS4_BASE_WAIT_TIME * 1000 * 1000);
12976 12986
12977 12987 /*
12978 12988 * If lock is relative to EOF, we need the newest length of the
12979 12989 * file. Therefore invalidate the ATTR_CACHE.
12980 12990 */
12981 12991
12982 12992 *whencep = flk->l_whence;
12983 12993
12984 12994 if (*whencep == 2) /* SEEK_END */
12985 12995 PURGE_ATTRCACHE4(vp);
12986 12996
12987 12997 recov_statep->rs_flags = 0;
12988 12998 recov_statep->rs_num_retry_despite_err = 0;
12989 12999 *cred_otw = nfs4_get_otw_cred(search_cr, VTOMI4(vp), NULL);
12990 13000 }
12991 13001
12992 13002 /*
12993 13003 * Initialize and allocate the data structures necessary for
12994 13004 * the nfs4frlock call.
12995 13005 * Allocates argsp's op array, frees up the saved_rqstpp if there is one.
12996 13006 */
12997 13007 static void
12998 13008 nfs4frlock_call_init(COMPOUND4args_clnt *argsp, COMPOUND4args_clnt **argspp,
12999 13009 nfs_argop4 **argopp, nfs4_op_hint_t *op_hintp, flock64_t *flk, int cmd,
13000 13010 bool_t *retry, bool_t *did_start_fop, COMPOUND4res_clnt **respp,
13001 13011 bool_t *skip_get_err, nfs4_lost_rqst_t *lost_rqstp)
13002 13012 {
13003 13013 int argoplist_size;
13004 13014 int num_ops = 2;
13005 13015
13006 13016 *retry = FALSE;
13007 13017 *did_start_fop = FALSE;
13008 13018 *skip_get_err = FALSE;
13009 13019 lost_rqstp->lr_op = 0;
13010 13020 argoplist_size = num_ops * sizeof (nfs_argop4);
13011 13021 /* fill array with zero */
13012 13022 *argopp = kmem_zalloc(argoplist_size, KM_SLEEP);
13013 13023
13014 13024 *argspp = argsp;
13015 13025 *respp = NULL;
13016 13026
13017 13027 argsp->array_len = num_ops;
13018 13028 argsp->array = *argopp;
13019 13029
13020 13030 /* initialize in case of error; will get real value down below */
13021 13031 argsp->ctag = TAG_NONE;
13022 13032
13023 13033 if ((cmd == F_SETLK || cmd == F_SETLKW) && flk->l_type == F_UNLCK)
13024 13034 *op_hintp = OH_LOCKU;
13025 13035 else
13026 13036 *op_hintp = OH_OTHER;
13027 13037 }
13028 13038
13029 13039 /*
13030 13040 * Call the nfs4_start_fop() for nfs4frlock, if necessary. Assign
13031 13041 * the proper nfs4_server_t for this instance of nfs4frlock.
13032 13042 * Returns 0 (success) or an errno value.
13033 13043 */
13034 13044 static int
13035 13045 nfs4frlock_start_call(nfs4_lock_call_type_t ctype, vnode_t *vp,
13036 13046 nfs4_op_hint_t op_hint, nfs4_recov_state_t *recov_statep,
13037 13047 bool_t *did_start_fop, bool_t *startrecovp)
13038 13048 {
13039 13049 int error = 0;
13040 13050 rnode4_t *rp;
13041 13051
13042 13052 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13043 13053
13044 13054 if (ctype == NFS4_LCK_CTYPE_NORM) {
13045 13055 error = nfs4_start_fop(VTOMI4(vp), vp, NULL, op_hint,
13046 13056 recov_statep, startrecovp);
13047 13057 if (error)
13048 13058 return (error);
13049 13059 *did_start_fop = TRUE;
13050 13060 } else {
13051 13061 *did_start_fop = FALSE;
13052 13062 *startrecovp = FALSE;
13053 13063 }
13054 13064
13055 13065 if (!error) {
13056 13066 rp = VTOR4(vp);
13057 13067
13058 13068 /* If the file failed recovery, just quit. */
13059 13069 mutex_enter(&rp->r_statelock);
13060 13070 if (rp->r_flags & R4RECOVERR) {
13061 13071 error = EIO;
13062 13072 }
13063 13073 mutex_exit(&rp->r_statelock);
13064 13074 }
13065 13075
13066 13076 return (error);
13067 13077 }
13068 13078
13069 13079 /*
13070 13080 * Setup the LOCK4/LOCKU4 arguments for resending a lost lock request. A
13071 13081 * resend nfs4frlock call is initiated by the recovery framework.
13072 13082 * Acquires the lop and oop seqid synchronization.
13073 13083 */
13074 13084 static void
13075 13085 nfs4frlock_setup_resend_lock_args(nfs4_lost_rqst_t *resend_rqstp,
13076 13086 COMPOUND4args_clnt *argsp, nfs_argop4 *argop, nfs4_lock_owner_t **lopp,
13077 13087 nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp,
13078 13088 LOCK4args **lock_argsp, LOCKU4args **locku_argsp)
13079 13089 {
13080 13090 mntinfo4_t *mi = VTOMI4(resend_rqstp->lr_vp);
13081 13091 int error;
13082 13092
13083 13093 NFS4_DEBUG((nfs4_lost_rqst_debug || nfs4_client_lock_debug),
13084 13094 (CE_NOTE,
13085 13095 "nfs4frlock_setup_resend_lock_args: have lost lock to resend"));
13086 13096 ASSERT(resend_rqstp != NULL);
13087 13097 ASSERT(resend_rqstp->lr_op == OP_LOCK ||
13088 13098 resend_rqstp->lr_op == OP_LOCKU);
13089 13099
13090 13100 *oopp = resend_rqstp->lr_oop;
13091 13101 if (resend_rqstp->lr_oop) {
13092 13102 open_owner_hold(resend_rqstp->lr_oop);
13093 13103 error = nfs4_start_open_seqid_sync(resend_rqstp->lr_oop, mi);
13094 13104 ASSERT(error == 0); /* recov thread always succeeds */
13095 13105 }
13096 13106
13097 13107 /* Must resend this lost lock/locku request. */
13098 13108 ASSERT(resend_rqstp->lr_lop != NULL);
13099 13109 *lopp = resend_rqstp->lr_lop;
13100 13110 lock_owner_hold(resend_rqstp->lr_lop);
13101 13111 error = nfs4_start_lock_seqid_sync(resend_rqstp->lr_lop, mi);
13102 13112 ASSERT(error == 0); /* recov thread always succeeds */
13103 13113
13104 13114 *ospp = resend_rqstp->lr_osp;
13105 13115 if (*ospp)
13106 13116 open_stream_hold(resend_rqstp->lr_osp);
13107 13117
13108 13118 if (resend_rqstp->lr_op == OP_LOCK) {
13109 13119 LOCK4args *lock_args;
13110 13120
13111 13121 argop->argop = OP_LOCK;
13112 13122 *lock_argsp = lock_args = &argop->nfs_argop4_u.oplock;
13113 13123 lock_args->locktype = resend_rqstp->lr_locktype;
13114 13124 lock_args->reclaim =
13115 13125 (resend_rqstp->lr_ctype == NFS4_LCK_CTYPE_RECLAIM);
13116 13126 lock_args->offset = resend_rqstp->lr_flk->l_start;
13117 13127 lock_args->length = resend_rqstp->lr_flk->l_len;
13118 13128 if (lock_args->length == 0)
13119 13129 lock_args->length = ~lock_args->length;
13120 13130 nfs4_setup_lock_args(*lopp, *oopp, *ospp,
13121 13131 mi2clientid(mi), &lock_args->locker);
13122 13132
13123 13133 switch (resend_rqstp->lr_ctype) {
13124 13134 case NFS4_LCK_CTYPE_RESEND:
13125 13135 argsp->ctag = TAG_LOCK_RESEND;
13126 13136 break;
13127 13137 case NFS4_LCK_CTYPE_REINSTATE:
13128 13138 argsp->ctag = TAG_LOCK_REINSTATE;
13129 13139 break;
13130 13140 case NFS4_LCK_CTYPE_RECLAIM:
13131 13141 argsp->ctag = TAG_LOCK_RECLAIM;
13132 13142 break;
13133 13143 default:
13134 13144 argsp->ctag = TAG_LOCK_UNKNOWN;
13135 13145 break;
13136 13146 }
13137 13147 } else {
13138 13148 LOCKU4args *locku_args;
13139 13149 nfs4_lock_owner_t *lop = resend_rqstp->lr_lop;
13140 13150
13141 13151 argop->argop = OP_LOCKU;
13142 13152 *locku_argsp = locku_args = &argop->nfs_argop4_u.oplocku;
13143 13153 locku_args->locktype = READ_LT;
13144 13154 locku_args->seqid = lop->lock_seqid + 1;
13145 13155 mutex_enter(&lop->lo_lock);
13146 13156 locku_args->lock_stateid = lop->lock_stateid;
13147 13157 mutex_exit(&lop->lo_lock);
13148 13158 locku_args->offset = resend_rqstp->lr_flk->l_start;
13149 13159 locku_args->length = resend_rqstp->lr_flk->l_len;
13150 13160 if (locku_args->length == 0)
13151 13161 locku_args->length = ~locku_args->length;
13152 13162
13153 13163 switch (resend_rqstp->lr_ctype) {
13154 13164 case NFS4_LCK_CTYPE_RESEND:
13155 13165 argsp->ctag = TAG_LOCKU_RESEND;
13156 13166 break;
13157 13167 case NFS4_LCK_CTYPE_REINSTATE:
13158 13168 argsp->ctag = TAG_LOCKU_REINSTATE;
13159 13169 break;
13160 13170 default:
13161 13171 argsp->ctag = TAG_LOCK_UNKNOWN;
13162 13172 break;
13163 13173 }
13164 13174 }
13165 13175 }
13166 13176
13167 13177 /*
13168 13178 * Setup the LOCKT4 arguments.
13169 13179 */
13170 13180 static void
13171 13181 nfs4frlock_setup_lockt_args(nfs4_lock_call_type_t ctype, nfs_argop4 *argop,
13172 13182 LOCKT4args **lockt_argsp, COMPOUND4args_clnt *argsp, flock64_t *flk,
13173 13183 rnode4_t *rp)
13174 13184 {
13175 13185 LOCKT4args *lockt_args;
13176 13186
13177 13187 ASSERT(nfs_zone() == VTOMI4(RTOV4(rp))->mi_zone);
13178 13188 ASSERT(ctype == NFS4_LCK_CTYPE_NORM);
13179 13189 argop->argop = OP_LOCKT;
13180 13190 argsp->ctag = TAG_LOCKT;
13181 13191 lockt_args = &argop->nfs_argop4_u.oplockt;
13182 13192
13183 13193 /*
13184 13194 * The locktype will be READ_LT unless it's
13185 13195 * a write lock. We do this because the Solaris
13186 13196 * system call allows the combination of
13187 13197 * F_UNLCK and F_GETLK* and so in that case the
13188 13198 * unlock is mapped to a read.
13189 13199 */
13190 13200 if (flk->l_type == F_WRLCK)
13191 13201 lockt_args->locktype = WRITE_LT;
13192 13202 else
13193 13203 lockt_args->locktype = READ_LT;
13194 13204
13195 13205 lockt_args->owner.clientid = mi2clientid(VTOMI4(RTOV4(rp)));
13196 13206 /* set the lock owner4 args */
13197 13207 nfs4_setlockowner_args(&lockt_args->owner, rp,
13198 13208 ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pidp->pid_id :
13199 13209 flk->l_pid);
13200 13210 lockt_args->offset = flk->l_start;
13201 13211 lockt_args->length = flk->l_len;
13202 13212 if (flk->l_len == 0)
13203 13213 lockt_args->length = ~lockt_args->length;
13204 13214
13205 13215 *lockt_argsp = lockt_args;
13206 13216 }
13207 13217
13208 13218 /*
13209 13219 * If the client is holding a delegation, and the open stream to be used
13210 13220 * with this lock request is a delegation open stream, then re-open the stream.
13211 13221 * Sets the nfs4_error_t to all zeros unless the open stream has already
13212 13222 * failed a reopen or we couldn't find the open stream. NFS4ERR_DELAY
13213 13223 * means the caller should retry (like a recovery retry).
13214 13224 */
13215 13225 static void
13216 13226 nfs4frlock_check_deleg(vnode_t *vp, nfs4_error_t *ep, cred_t *cr, int lt)
13217 13227 {
13218 13228 open_delegation_type4 dt;
13219 13229 bool_t reopen_needed, force;
13220 13230 nfs4_open_stream_t *osp;
13221 13231 open_claim_type4 oclaim;
13222 13232 rnode4_t *rp = VTOR4(vp);
13223 13233 mntinfo4_t *mi = VTOMI4(vp);
13224 13234
13225 13235 ASSERT(nfs_zone() == mi->mi_zone);
13226 13236
13227 13237 nfs4_error_zinit(ep);
13228 13238
13229 13239 mutex_enter(&rp->r_statev4_lock);
13230 13240 dt = rp->r_deleg_type;
13231 13241 mutex_exit(&rp->r_statev4_lock);
13232 13242
13233 13243 if (dt != OPEN_DELEGATE_NONE) {
13234 13244 nfs4_open_owner_t *oop;
13235 13245
13236 13246 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi);
13237 13247 if (!oop) {
13238 13248 ep->stat = NFS4ERR_IO;
13239 13249 return;
13240 13250 }
13241 13251 /* returns with 'os_sync_lock' held */
13242 13252 osp = find_open_stream(oop, rp);
13243 13253 if (!osp) {
13244 13254 open_owner_rele(oop);
13245 13255 ep->stat = NFS4ERR_IO;
13246 13256 return;
13247 13257 }
13248 13258
13249 13259 if (osp->os_failed_reopen) {
13250 13260 NFS4_DEBUG((nfs4_open_stream_debug ||
13251 13261 nfs4_client_lock_debug), (CE_NOTE,
13252 13262 "nfs4frlock_check_deleg: os_failed_reopen set "
13253 13263 "for osp %p, cr %p, rp %s", (void *)osp,
13254 13264 (void *)cr, rnode4info(rp)));
13255 13265 mutex_exit(&osp->os_sync_lock);
13256 13266 open_stream_rele(osp, rp);
13257 13267 open_owner_rele(oop);
13258 13268 ep->stat = NFS4ERR_IO;
13259 13269 return;
13260 13270 }
13261 13271
13262 13272 /*
13263 13273 * Determine whether a reopen is needed. If this
13264 13274 * is a delegation open stream, then send the open
13265 13275 * to the server to give visibility to the open owner.
13266 13276 * Even if it isn't a delegation open stream, we need
13267 13277 * to check if the previous open CLAIM_DELEGATE_CUR
13268 13278 * was sufficient.
13269 13279 */
13270 13280
13271 13281 reopen_needed = osp->os_delegation ||
13272 13282 ((lt == F_RDLCK &&
13273 13283 !(osp->os_dc_openacc & OPEN4_SHARE_ACCESS_READ)) ||
13274 13284 (lt == F_WRLCK &&
13275 13285 !(osp->os_dc_openacc & OPEN4_SHARE_ACCESS_WRITE)));
13276 13286
13277 13287 mutex_exit(&osp->os_sync_lock);
13278 13288 open_owner_rele(oop);
13279 13289
13280 13290 if (reopen_needed) {
13281 13291 /*
13282 13292 * Always use CLAIM_PREVIOUS after server reboot.
13283 13293 * The server will reject CLAIM_DELEGATE_CUR if
13284 13294 * it is used during the grace period.
13285 13295 */
13286 13296 mutex_enter(&mi->mi_lock);
13287 13297 if (mi->mi_recovflags & MI4R_SRV_REBOOT) {
13288 13298 oclaim = CLAIM_PREVIOUS;
13289 13299 force = TRUE;
13290 13300 } else {
13291 13301 oclaim = CLAIM_DELEGATE_CUR;
13292 13302 force = FALSE;
13293 13303 }
13294 13304 mutex_exit(&mi->mi_lock);
13295 13305
13296 13306 nfs4_reopen(vp, osp, ep, oclaim, force, FALSE);
13297 13307 if (ep->error == EAGAIN) {
13298 13308 nfs4_error_zinit(ep);
13299 13309 ep->stat = NFS4ERR_DELAY;
13300 13310 }
13301 13311 }
13302 13312 open_stream_rele(osp, rp);
13303 13313 osp = NULL;
13304 13314 }
13305 13315 }
13306 13316
13307 13317 /*
13308 13318 * Setup the LOCKU4 arguments.
13309 13319 * Returns errors via the nfs4_error_t.
13310 13320 * NFS4_OK no problems. *go_otwp is TRUE if call should go
13311 13321 * over-the-wire. The caller must release the
13312 13322 * reference on *lopp.
13313 13323 * NFS4ERR_DELAY caller should retry (like recovery retry)
13314 13324 * (other) unrecoverable error.
13315 13325 */
13316 13326 static void
13317 13327 nfs4frlock_setup_locku_args(nfs4_lock_call_type_t ctype, nfs_argop4 *argop,
13318 13328 LOCKU4args **locku_argsp, flock64_t *flk,
13319 13329 nfs4_lock_owner_t **lopp, nfs4_error_t *ep, COMPOUND4args_clnt *argsp,
13320 13330 vnode_t *vp, int flag, u_offset_t offset, cred_t *cr,
13321 13331 bool_t *skip_get_err, bool_t *go_otwp)
13322 13332 {
13323 13333 nfs4_lock_owner_t *lop = NULL;
13324 13334 LOCKU4args *locku_args;
13325 13335 pid_t pid;
13326 13336 bool_t is_spec = FALSE;
13327 13337 rnode4_t *rp = VTOR4(vp);
13328 13338
13329 13339 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13330 13340 ASSERT(ctype == NFS4_LCK_CTYPE_NORM);
13331 13341
13332 13342 nfs4frlock_check_deleg(vp, ep, cr, F_UNLCK);
13333 13343 if (ep->error || ep->stat)
13334 13344 return;
13335 13345
13336 13346 argop->argop = OP_LOCKU;
13337 13347 if (ctype == NFS4_LCK_CTYPE_REINSTATE)
13338 13348 argsp->ctag = TAG_LOCKU_REINSTATE;
13339 13349 else
13340 13350 argsp->ctag = TAG_LOCKU;
13341 13351 locku_args = &argop->nfs_argop4_u.oplocku;
13342 13352 *locku_argsp = locku_args;
13343 13353
13344 13354 /*
13345 13355 * XXX what should locku_args->locktype be?
13346 13356 * setting to ALWAYS be READ_LT so at least
13347 13357 * it is a valid locktype.
13348 13358 */
13349 13359
13350 13360 locku_args->locktype = READ_LT;
13351 13361
13352 13362 pid = ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pidp->pid_id :
13353 13363 flk->l_pid;
13354 13364
13355 13365 /*
13356 13366 * Get the lock owner stateid. If no lock owner
13357 13367 * exists, return success.
13358 13368 */
13359 13369 lop = find_lock_owner(rp, pid, LOWN_ANY);
13360 13370 *lopp = lop;
13361 13371 if (lop && CLNT_ISSPECIAL(&lop->lock_stateid))
13362 13372 is_spec = TRUE;
13363 13373 if (!lop || is_spec) {
13364 13374 /*
13365 13375 * No lock owner so no locks to unlock.
13366 13376 * Return success. If there was a failed
13367 13377 * reclaim earlier, the lock might still be
13368 13378 * registered with the local locking code,
13369 13379 * so notify it of the unlock.
13370 13380 *
13371 13381 * If the lockowner is using a special stateid,
13372 13382 * then the original lock request (that created
13373 13383 * this lockowner) was never successful, so we
13374 13384 * have no lock to undo OTW.
13375 13385 */
13376 13386 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
13377 13387 "nfs4frlock_setup_locku_args: LOCKU: no lock owner "
13378 13388 "(%ld) so return success", (long)pid));
13379 13389
13380 13390 if (ctype == NFS4_LCK_CTYPE_NORM)
13381 13391 flk->l_pid = curproc->p_pid;
13382 13392 nfs4_register_lock_locally(vp, flk, flag, offset);
13383 13393 /*
13384 13394 * Release our hold and NULL out so final_cleanup
13385 13395 * doesn't try to end a lock seqid sync we
13386 13396 * never started.
13387 13397 */
13388 13398 if (is_spec) {
13389 13399 lock_owner_rele(lop);
13390 13400 *lopp = NULL;
13391 13401 }
13392 13402 *skip_get_err = TRUE;
13393 13403 *go_otwp = FALSE;
13394 13404 return;
13395 13405 }
13396 13406
13397 13407 ep->error = nfs4_start_lock_seqid_sync(lop, VTOMI4(vp));
13398 13408 if (ep->error == EAGAIN) {
13399 13409 lock_owner_rele(lop);
13400 13410 *lopp = NULL;
13401 13411 return;
13402 13412 }
13403 13413
13404 13414 mutex_enter(&lop->lo_lock);
13405 13415 locku_args->lock_stateid = lop->lock_stateid;
13406 13416 mutex_exit(&lop->lo_lock);
13407 13417 locku_args->seqid = lop->lock_seqid + 1;
13408 13418
13409 13419 /* leave the ref count on lop, rele after RPC call */
13410 13420
13411 13421 locku_args->offset = flk->l_start;
13412 13422 locku_args->length = flk->l_len;
13413 13423 if (flk->l_len == 0)
13414 13424 locku_args->length = ~locku_args->length;
13415 13425
13416 13426 *go_otwp = TRUE;
13417 13427 }
13418 13428
13419 13429 /*
13420 13430 * Setup the LOCK4 arguments.
13421 13431 *
13422 13432 * Returns errors via the nfs4_error_t.
13423 13433 * NFS4_OK no problems
13424 13434 * NFS4ERR_DELAY caller should retry (like recovery retry)
13425 13435 * (other) unrecoverable error
13426 13436 */
13427 13437 static void
13428 13438 nfs4frlock_setup_lock_args(nfs4_lock_call_type_t ctype, LOCK4args **lock_argsp,
13429 13439 nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp,
13430 13440 nfs4_lock_owner_t **lopp, nfs_argop4 *argop, COMPOUND4args_clnt *argsp,
13431 13441 flock64_t *flk, int cmd, vnode_t *vp, cred_t *cr, nfs4_error_t *ep)
13432 13442 {
13433 13443 LOCK4args *lock_args;
13434 13444 nfs4_open_owner_t *oop = NULL;
13435 13445 nfs4_open_stream_t *osp = NULL;
13436 13446 nfs4_lock_owner_t *lop = NULL;
13437 13447 pid_t pid;
13438 13448 rnode4_t *rp = VTOR4(vp);
13439 13449
13440 13450 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13441 13451
13442 13452 nfs4frlock_check_deleg(vp, ep, cr, flk->l_type);
13443 13453 if (ep->error || ep->stat != NFS4_OK)
13444 13454 return;
13445 13455
13446 13456 argop->argop = OP_LOCK;
13447 13457 if (ctype == NFS4_LCK_CTYPE_NORM)
13448 13458 argsp->ctag = TAG_LOCK;
13449 13459 else if (ctype == NFS4_LCK_CTYPE_RECLAIM)
13450 13460 argsp->ctag = TAG_RELOCK;
13451 13461 else
13452 13462 argsp->ctag = TAG_LOCK_REINSTATE;
13453 13463 lock_args = &argop->nfs_argop4_u.oplock;
13454 13464 lock_args->locktype = flk_to_locktype(cmd, flk->l_type);
13455 13465 lock_args->reclaim = ctype == NFS4_LCK_CTYPE_RECLAIM ? 1 : 0;
13456 13466 /*
13457 13467 * Get the lock owner. If no lock owner exists,
13458 13468 * create a 'temporary' one and grab the open seqid
13459 13469 * synchronization (which puts a hold on the open
13460 13470 * owner and open stream).
13461 13471 * This also grabs the lock seqid synchronization.
13462 13472 */
13463 13473 pid = ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pid : flk->l_pid;
13464 13474 ep->stat =
13465 13475 nfs4_find_or_create_lock_owner(pid, rp, cr, &oop, &osp, &lop);
13466 13476
13467 13477 if (ep->stat != NFS4_OK)
13468 13478 goto out;
13469 13479
13470 13480 nfs4_setup_lock_args(lop, oop, osp, mi2clientid(VTOMI4(vp)),
13471 13481 &lock_args->locker);
13472 13482
13473 13483 lock_args->offset = flk->l_start;
13474 13484 lock_args->length = flk->l_len;
13475 13485 if (flk->l_len == 0)
13476 13486 lock_args->length = ~lock_args->length;
13477 13487 *lock_argsp = lock_args;
13478 13488 out:
13479 13489 *oopp = oop;
13480 13490 *ospp = osp;
13481 13491 *lopp = lop;
13482 13492 }
13483 13493
13484 13494 /*
13485 13495 * After we get the reply from the server, record the proper information
13486 13496 * for possible resend lock requests.
13487 13497 *
13488 13498 * Allocates memory for the saved_rqstp if we have a lost lock to save.
13489 13499 */
13490 13500 static void
13491 13501 nfs4frlock_save_lost_rqst(nfs4_lock_call_type_t ctype, int error,
13492 13502 nfs_lock_type4 locktype, nfs4_open_owner_t *oop,
13493 13503 nfs4_open_stream_t *osp, nfs4_lock_owner_t *lop, flock64_t *flk,
13494 13504 nfs4_lost_rqst_t *lost_rqstp, cred_t *cr, vnode_t *vp)
13495 13505 {
13496 13506 bool_t unlock = (flk->l_type == F_UNLCK);
13497 13507
13498 13508 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13499 13509 ASSERT(ctype == NFS4_LCK_CTYPE_NORM ||
13500 13510 ctype == NFS4_LCK_CTYPE_REINSTATE);
13501 13511
13502 13512 if (error != 0 && !unlock) {
13503 13513 NFS4_DEBUG((nfs4_lost_rqst_debug ||
13504 13514 nfs4_client_lock_debug), (CE_NOTE,
13505 13515 "nfs4frlock_save_lost_rqst: set lo_pending_rqsts to 1 "
13506 13516 " for lop %p", (void *)lop));
13507 13517 ASSERT(lop != NULL);
13508 13518 mutex_enter(&lop->lo_lock);
13509 13519 lop->lo_pending_rqsts = 1;
13510 13520 mutex_exit(&lop->lo_lock);
13511 13521 }
13512 13522
13513 13523 lost_rqstp->lr_putfirst = FALSE;
13514 13524 lost_rqstp->lr_op = 0;
13515 13525
13516 13526 /*
13517 13527 * For lock/locku requests, we treat EINTR as ETIMEDOUT for
13518 13528 * recovery purposes so that the lock request that was sent
13519 13529 * can be saved and re-issued later. Ditto for EIO from a forced
13520 13530 * unmount. This is done to have the client's local locking state
13521 13531 * match the v4 server's state; that is, the request was
13522 13532 * potentially received and accepted by the server but the client
13523 13533 * thinks it was not.
13524 13534 */
13525 13535 if (error == ETIMEDOUT || error == EINTR ||
13526 13536 NFS4_FRC_UNMT_ERR(error, vp->v_vfsp)) {
13527 13537 NFS4_DEBUG((nfs4_lost_rqst_debug ||
13528 13538 nfs4_client_lock_debug), (CE_NOTE,
13529 13539 "nfs4frlock_save_lost_rqst: got a lost %s lock for "
13530 13540 "lop %p oop %p osp %p", unlock ? "LOCKU" : "LOCK",
13531 13541 (void *)lop, (void *)oop, (void *)osp));
13532 13542 if (unlock)
13533 13543 lost_rqstp->lr_op = OP_LOCKU;
13534 13544 else {
13535 13545 lost_rqstp->lr_op = OP_LOCK;
13536 13546 lost_rqstp->lr_locktype = locktype;
13537 13547 }
13538 13548 /*
13539 13549 * Objects are held and rele'd via the recovery code.
13540 13550 * See nfs4_save_lost_rqst.
13541 13551 */
13542 13552 lost_rqstp->lr_vp = vp;
13543 13553 lost_rqstp->lr_dvp = NULL;
13544 13554 lost_rqstp->lr_oop = oop;
13545 13555 lost_rqstp->lr_osp = osp;
13546 13556 lost_rqstp->lr_lop = lop;
13547 13557 lost_rqstp->lr_cr = cr;
13548 13558 switch (ctype) {
13549 13559 case NFS4_LCK_CTYPE_NORM:
13550 13560 flk->l_pid = ttoproc(curthread)->p_pid;
13551 13561 lost_rqstp->lr_ctype = NFS4_LCK_CTYPE_RESEND;
13552 13562 break;
13553 13563 case NFS4_LCK_CTYPE_REINSTATE:
13554 13564 lost_rqstp->lr_putfirst = TRUE;
13555 13565 lost_rqstp->lr_ctype = ctype;
13556 13566 break;
13557 13567 default:
13558 13568 break;
13559 13569 }
13560 13570 lost_rqstp->lr_flk = flk;
13561 13571 }
13562 13572 }
13563 13573
13564 13574 /*
13565 13575 * Update lop's seqid. Also update the seqid stored in a resend request,
13566 13576 * if any. (Some recovery errors increment the seqid, and we may have to
13567 13577 * send the resend request again.)
13568 13578 */
13569 13579
13570 13580 static void
13571 13581 nfs4frlock_bump_seqid(LOCK4args *lock_args, LOCKU4args *locku_args,
13572 13582 nfs4_open_owner_t *oop, nfs4_lock_owner_t *lop, nfs4_tag_type_t tag_type)
13573 13583 {
13574 13584 if (lock_args) {
13575 13585 if (lock_args->locker.new_lock_owner == TRUE)
13576 13586 nfs4_get_and_set_next_open_seqid(oop, tag_type);
13577 13587 else {
13578 13588 ASSERT(lop->lo_flags & NFS4_LOCK_SEQID_INUSE);
13579 13589 nfs4_set_lock_seqid(lop->lock_seqid + 1, lop);
13580 13590 }
13581 13591 } else if (locku_args) {
13582 13592 ASSERT(lop->lo_flags & NFS4_LOCK_SEQID_INUSE);
13583 13593 nfs4_set_lock_seqid(lop->lock_seqid +1, lop);
13584 13594 }
13585 13595 }
13586 13596
13587 13597 /*
13588 13598 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the
13589 13599 * COMPOUND4 args/res for calls that need to retry.
13590 13600 * Switches the *cred_otwp to base_cr.
13591 13601 */
13592 13602 static void
13593 13603 nfs4frlock_check_access(vnode_t *vp, nfs4_op_hint_t op_hint,
13594 13604 nfs4_recov_state_t *recov_statep, int needrecov, bool_t *did_start_fop,
13595 13605 COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp, int error,
13596 13606 nfs4_lock_owner_t **lopp, nfs4_open_owner_t **oopp,
13597 13607 nfs4_open_stream_t **ospp, cred_t *base_cr, cred_t **cred_otwp)
13598 13608 {
13599 13609 nfs4_open_owner_t *oop = *oopp;
13600 13610 nfs4_open_stream_t *osp = *ospp;
13601 13611 nfs4_lock_owner_t *lop = *lopp;
13602 13612 nfs_argop4 *argop = (*argspp)->array;
13603 13613
13604 13614 if (*did_start_fop) {
13605 13615 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, recov_statep,
13606 13616 needrecov);
13607 13617 *did_start_fop = FALSE;
13608 13618 }
13609 13619 ASSERT((*argspp)->array_len == 2);
13610 13620 if (argop[1].argop == OP_LOCK)
13611 13621 nfs4args_lock_free(&argop[1]);
13612 13622 else if (argop[1].argop == OP_LOCKT)
13613 13623 nfs4args_lockt_free(&argop[1]);
13614 13624 kmem_free(argop, 2 * sizeof (nfs_argop4));
13615 13625 if (!error)
13616 13626 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)*respp);
13617 13627 *argspp = NULL;
13618 13628 *respp = NULL;
13619 13629
13620 13630 if (lop) {
13621 13631 nfs4_end_lock_seqid_sync(lop);
13622 13632 lock_owner_rele(lop);
13623 13633 *lopp = NULL;
13624 13634 }
13625 13635
13626 13636 /* need to free up the reference on osp for lock args */
13627 13637 if (osp != NULL) {
13628 13638 open_stream_rele(osp, VTOR4(vp));
13629 13639 *ospp = NULL;
13630 13640 }
13631 13641
13632 13642 /* need to free up the reference on oop for lock args */
13633 13643 if (oop != NULL) {
13634 13644 nfs4_end_open_seqid_sync(oop);
13635 13645 open_owner_rele(oop);
13636 13646 *oopp = NULL;
13637 13647 }
13638 13648
13639 13649 crfree(*cred_otwp);
13640 13650 *cred_otwp = base_cr;
13641 13651 crhold(*cred_otwp);
13642 13652 }
13643 13653
13644 13654 /*
13645 13655 * Function to process the client's recovery for nfs4frlock.
13646 13656 * Returns TRUE if we should retry the lock request; FALSE otherwise.
13647 13657 *
13648 13658 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the
13649 13659 * COMPOUND4 args/res for calls that need to retry.
13650 13660 *
13651 13661 * Note: the rp's r_lkserlock is *not* dropped during this path.
13652 13662 */
13653 13663 static bool_t
13654 13664 nfs4frlock_recovery(int needrecov, nfs4_error_t *ep,
13655 13665 COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp,
13656 13666 LOCK4args *lock_args, LOCKU4args *locku_args,
13657 13667 nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp,
13658 13668 nfs4_lock_owner_t **lopp, rnode4_t *rp, vnode_t *vp,
13659 13669 nfs4_recov_state_t *recov_statep, nfs4_op_hint_t op_hint,
13660 13670 bool_t *did_start_fop, nfs4_lost_rqst_t *lost_rqstp, flock64_t *flk)
13661 13671 {
13662 13672 nfs4_open_owner_t *oop = *oopp;
13663 13673 nfs4_open_stream_t *osp = *ospp;
13664 13674 nfs4_lock_owner_t *lop = *lopp;
13665 13675
13666 13676 bool_t abort, retry;
13667 13677
13668 13678 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13669 13679 ASSERT((*argspp) != NULL);
13670 13680 ASSERT((*respp) != NULL);
13671 13681 if (lock_args || locku_args)
13672 13682 ASSERT(lop != NULL);
13673 13683
13674 13684 NFS4_DEBUG((nfs4_client_lock_debug || nfs4_client_recov_debug),
13675 13685 (CE_NOTE, "nfs4frlock_recovery: initiating recovery\n"));
13676 13686
13677 13687 retry = TRUE;
13678 13688 abort = FALSE;
13679 13689 if (needrecov) {
13680 13690 nfs4_bseqid_entry_t *bsep = NULL;
13681 13691 nfs_opnum4 op;
13682 13692
13683 13693 op = lock_args ? OP_LOCK : locku_args ? OP_LOCKU : OP_LOCKT;
13684 13694
13685 13695 if (!ep->error && ep->stat == NFS4ERR_BAD_SEQID) {
13686 13696 seqid4 seqid;
13687 13697
13688 13698 if (lock_args) {
13689 13699 if (lock_args->locker.new_lock_owner == TRUE)
13690 13700 seqid = lock_args->locker.locker4_u.
13691 13701 open_owner.open_seqid;
13692 13702 else
13693 13703 seqid = lock_args->locker.locker4_u.
13694 13704 lock_owner.lock_seqid;
13695 13705 } else if (locku_args) {
13696 13706 seqid = locku_args->seqid;
13697 13707 } else {
13698 13708 seqid = 0;
13699 13709 }
13700 13710
13701 13711 bsep = nfs4_create_bseqid_entry(oop, lop, vp,
13702 13712 flk->l_pid, (*argspp)->ctag, seqid);
13703 13713 }
13704 13714
13705 13715 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL, NULL,
13706 13716 (lost_rqstp && (lost_rqstp->lr_op == OP_LOCK ||
13707 13717 lost_rqstp->lr_op == OP_LOCKU)) ? lost_rqstp :
13708 13718 NULL, op, bsep, NULL, NULL);
13709 13719
13710 13720 if (bsep)
13711 13721 kmem_free(bsep, sizeof (*bsep));
13712 13722 }
13713 13723
13714 13724 /*
13715 13725 * Return that we do not want to retry the request for 3 cases:
13716 13726 * 1. If we received EINTR or are bailing out because of a forced
13717 13727 * unmount, we came into this code path just for the sake of
13718 13728 * initiating recovery, we now need to return the error.
13719 13729 * 2. If we have aborted recovery.
13720 13730 * 3. We received NFS4ERR_BAD_SEQID.
13721 13731 */
13722 13732 if (ep->error == EINTR || NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp) ||
13723 13733 abort == TRUE || (ep->error == 0 && ep->stat == NFS4ERR_BAD_SEQID))
13724 13734 retry = FALSE;
13725 13735
13726 13736 if (*did_start_fop == TRUE) {
13727 13737 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, recov_statep,
13728 13738 needrecov);
13729 13739 *did_start_fop = FALSE;
13730 13740 }
13731 13741
13732 13742 if (retry == TRUE) {
13733 13743 nfs_argop4 *argop;
13734 13744
13735 13745 argop = (*argspp)->array;
13736 13746 ASSERT((*argspp)->array_len == 2);
13737 13747
13738 13748 if (argop[1].argop == OP_LOCK)
13739 13749 nfs4args_lock_free(&argop[1]);
13740 13750 else if (argop[1].argop == OP_LOCKT)
13741 13751 nfs4args_lockt_free(&argop[1]);
13742 13752 kmem_free(argop, 2 * sizeof (nfs_argop4));
13743 13753 if (!ep->error)
13744 13754 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)*respp);
13745 13755 *respp = NULL;
13746 13756 *argspp = NULL;
13747 13757 }
13748 13758
13749 13759 if (lop != NULL) {
13750 13760 nfs4_end_lock_seqid_sync(lop);
13751 13761 lock_owner_rele(lop);
13752 13762 }
13753 13763
13754 13764 *lopp = NULL;
13755 13765
13756 13766 /* need to free up the reference on osp for lock args */
13757 13767 if (osp != NULL) {
13758 13768 open_stream_rele(osp, rp);
13759 13769 *ospp = NULL;
13760 13770 }
13761 13771
13762 13772 /* need to free up the reference on oop for lock args */
13763 13773 if (oop != NULL) {
13764 13774 nfs4_end_open_seqid_sync(oop);
13765 13775 open_owner_rele(oop);
13766 13776 *oopp = NULL;
13767 13777 }
13768 13778
13769 13779 return (retry);
13770 13780 }
13771 13781
13772 13782 /*
13773 13783 * Handles the successful reply from the server for nfs4frlock.
13774 13784 */
13775 13785 static void
13776 13786 nfs4frlock_results_ok(nfs4_lock_call_type_t ctype, int cmd, flock64_t *flk,
13777 13787 vnode_t *vp, int flag, u_offset_t offset,
13778 13788 nfs4_lost_rqst_t *resend_rqstp)
13779 13789 {
13780 13790 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13781 13791 if ((cmd == F_SETLK || cmd == F_SETLKW) &&
13782 13792 (flk->l_type == F_RDLCK || flk->l_type == F_WRLCK)) {
13783 13793 if (ctype == NFS4_LCK_CTYPE_NORM) {
13784 13794 flk->l_pid = ttoproc(curthread)->p_pid;
13785 13795 /*
13786 13796 * We do not register lost locks locally in
13787 13797 * the 'resend' case since the user/application
13788 13798 * doesn't think we have the lock.
13789 13799 */
13790 13800 ASSERT(!resend_rqstp);
13791 13801 nfs4_register_lock_locally(vp, flk, flag, offset);
13792 13802 }
13793 13803 }
13794 13804 }
13795 13805
13796 13806 /*
13797 13807 * Handle the DENIED reply from the server for nfs4frlock.
13798 13808 * Returns TRUE if we should retry the request; FALSE otherwise.
13799 13809 *
13800 13810 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the
13801 13811 * COMPOUND4 args/res for calls that need to retry. Can also
13802 13812 * drop and regrab the r_lkserlock.
13803 13813 */
13804 13814 static bool_t
13805 13815 nfs4frlock_results_denied(nfs4_lock_call_type_t ctype, LOCK4args *lock_args,
13806 13816 LOCKT4args *lockt_args, nfs4_open_owner_t **oopp,
13807 13817 nfs4_open_stream_t **ospp, nfs4_lock_owner_t **lopp, int cmd,
13808 13818 vnode_t *vp, flock64_t *flk, nfs4_op_hint_t op_hint,
13809 13819 nfs4_recov_state_t *recov_statep, int needrecov,
13810 13820 COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp,
13811 13821 clock_t *tick_delayp, short *whencep, int *errorp,
13812 13822 nfs_resop4 *resop, cred_t *cr, bool_t *did_start_fop,
13813 13823 bool_t *skip_get_err)
13814 13824 {
13815 13825 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13816 13826
13817 13827 if (lock_args) {
13818 13828 nfs4_open_owner_t *oop = *oopp;
13819 13829 nfs4_open_stream_t *osp = *ospp;
13820 13830 nfs4_lock_owner_t *lop = *lopp;
13821 13831 int intr;
13822 13832
13823 13833 /*
13824 13834 * Blocking lock needs to sleep and retry from the request.
13825 13835 *
13826 13836 * Do not block and wait for 'resend' or 'reinstate'
13827 13837 * lock requests, just return the error.
13828 13838 *
13829 13839 * Note: reclaim requests have cmd == F_SETLK, not F_SETLKW.
13830 13840 */
13831 13841 if (cmd == F_SETLKW) {
13832 13842 rnode4_t *rp = VTOR4(vp);
13833 13843 nfs_argop4 *argop = (*argspp)->array;
13834 13844
13835 13845 ASSERT(ctype == NFS4_LCK_CTYPE_NORM);
13836 13846
13837 13847 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint,
13838 13848 recov_statep, needrecov);
13839 13849 *did_start_fop = FALSE;
13840 13850 ASSERT((*argspp)->array_len == 2);
13841 13851 if (argop[1].argop == OP_LOCK)
13842 13852 nfs4args_lock_free(&argop[1]);
13843 13853 else if (argop[1].argop == OP_LOCKT)
13844 13854 nfs4args_lockt_free(&argop[1]);
13845 13855 kmem_free(argop, 2 * sizeof (nfs_argop4));
13846 13856 if (*respp)
13847 13857 (void) xdr_free(xdr_COMPOUND4res_clnt,
13848 13858 (caddr_t)*respp);
13849 13859 *argspp = NULL;
13850 13860 *respp = NULL;
13851 13861 nfs4_end_lock_seqid_sync(lop);
13852 13862 lock_owner_rele(lop);
13853 13863 *lopp = NULL;
13854 13864 if (osp != NULL) {
13855 13865 open_stream_rele(osp, rp);
13856 13866 *ospp = NULL;
13857 13867 }
13858 13868 if (oop != NULL) {
13859 13869 nfs4_end_open_seqid_sync(oop);
13860 13870 open_owner_rele(oop);
13861 13871 *oopp = NULL;
13862 13872 }
13863 13873
13864 13874 nfs_rw_exit(&rp->r_lkserlock);
13865 13875
13866 13876 intr = nfs4_block_and_wait(tick_delayp, rp);
13867 13877
13868 13878 if (intr) {
13869 13879 (void) nfs_rw_enter_sig(&rp->r_lkserlock,
13870 13880 RW_WRITER, FALSE);
13871 13881 *errorp = EINTR;
13872 13882 return (FALSE);
13873 13883 }
13874 13884
13875 13885 (void) nfs_rw_enter_sig(&rp->r_lkserlock,
13876 13886 RW_WRITER, FALSE);
13877 13887
13878 13888 /*
13879 13889 * Make sure we are still safe to lock with
13880 13890 * regards to mmapping.
13881 13891 */
13882 13892 if (!nfs4_safelock(vp, flk, cr)) {
13883 13893 *errorp = EAGAIN;
13884 13894 return (FALSE);
13885 13895 }
13886 13896
13887 13897 return (TRUE);
13888 13898 }
13889 13899 if (ctype == NFS4_LCK_CTYPE_NORM)
13890 13900 *errorp = EAGAIN;
13891 13901 *skip_get_err = TRUE;
13892 13902 flk->l_whence = 0;
13893 13903 *whencep = 0;
13894 13904 return (FALSE);
13895 13905 } else if (lockt_args) {
13896 13906 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
13897 13907 "nfs4frlock_results_denied: OP_LOCKT DENIED"));
13898 13908
13899 13909 denied_to_flk(&resop->nfs_resop4_u.oplockt.denied,
13900 13910 flk, lockt_args);
13901 13911
13902 13912 /* according to NLM code */
13903 13913 *errorp = 0;
13904 13914 *whencep = 0;
13905 13915 *skip_get_err = TRUE;
13906 13916 return (FALSE);
13907 13917 }
13908 13918 return (FALSE);
13909 13919 }
13910 13920
13911 13921 /*
13912 13922 * Handles all NFS4 errors besides NFS4_OK and NFS4ERR_DENIED for nfs4frlock.
13913 13923 */
13914 13924 static void
13915 13925 nfs4frlock_results_default(COMPOUND4res_clnt *resp, int *errorp)
13916 13926 {
13917 13927 switch (resp->status) {
13918 13928 case NFS4ERR_ACCESS:
13919 13929 case NFS4ERR_ADMIN_REVOKED:
13920 13930 case NFS4ERR_BADHANDLE:
13921 13931 case NFS4ERR_BAD_RANGE:
13922 13932 case NFS4ERR_BAD_SEQID:
13923 13933 case NFS4ERR_BAD_STATEID:
13924 13934 case NFS4ERR_BADXDR:
13925 13935 case NFS4ERR_DEADLOCK:
13926 13936 case NFS4ERR_DELAY:
13927 13937 case NFS4ERR_EXPIRED:
13928 13938 case NFS4ERR_FHEXPIRED:
13929 13939 case NFS4ERR_GRACE:
13930 13940 case NFS4ERR_INVAL:
13931 13941 case NFS4ERR_ISDIR:
13932 13942 case NFS4ERR_LEASE_MOVED:
13933 13943 case NFS4ERR_LOCK_NOTSUPP:
13934 13944 case NFS4ERR_LOCK_RANGE:
13935 13945 case NFS4ERR_MOVED:
13936 13946 case NFS4ERR_NOFILEHANDLE:
13937 13947 case NFS4ERR_NO_GRACE:
13938 13948 case NFS4ERR_OLD_STATEID:
13939 13949 case NFS4ERR_OPENMODE:
13940 13950 case NFS4ERR_RECLAIM_BAD:
13941 13951 case NFS4ERR_RECLAIM_CONFLICT:
13942 13952 case NFS4ERR_RESOURCE:
13943 13953 case NFS4ERR_SERVERFAULT:
13944 13954 case NFS4ERR_STALE:
13945 13955 case NFS4ERR_STALE_CLIENTID:
13946 13956 case NFS4ERR_STALE_STATEID:
13947 13957 return;
13948 13958 default:
13949 13959 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
13950 13960 "nfs4frlock_results_default: got unrecognizable "
13951 13961 "res.status %d", resp->status));
13952 13962 *errorp = NFS4ERR_INVAL;
13953 13963 }
13954 13964 }
13955 13965
13956 13966 /*
13957 13967 * The lock request was successful, so update the client's state.
13958 13968 */
13959 13969 static void
13960 13970 nfs4frlock_update_state(LOCK4args *lock_args, LOCKU4args *locku_args,
13961 13971 LOCKT4args *lockt_args, nfs_resop4 *resop, nfs4_lock_owner_t *lop,
13962 13972 vnode_t *vp, flock64_t *flk, cred_t *cr,
13963 13973 nfs4_lost_rqst_t *resend_rqstp)
13964 13974 {
13965 13975 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13966 13976
13967 13977 if (lock_args) {
13968 13978 LOCK4res *lock_res;
13969 13979
13970 13980 lock_res = &resop->nfs_resop4_u.oplock;
13971 13981 /* update the stateid with server's response */
13972 13982
13973 13983 if (lock_args->locker.new_lock_owner == TRUE) {
13974 13984 mutex_enter(&lop->lo_lock);
13975 13985 lop->lo_just_created = NFS4_PERM_CREATED;
13976 13986 mutex_exit(&lop->lo_lock);
13977 13987 }
13978 13988
13979 13989 nfs4_set_lock_stateid(lop, lock_res->LOCK4res_u.lock_stateid);
13980 13990
13981 13991 /*
13982 13992 * If the lock was the result of a resending a lost
13983 13993 * request, we've synched up the stateid and seqid
13984 13994 * with the server, but now the server might be out of sync
13985 13995 * with what the application thinks it has for locks.
13986 13996 * Clean that up here. It's unclear whether we should do
13987 13997 * this even if the filesystem has been forcibly unmounted.
13988 13998 * For most servers, it's probably wasted effort, but
13989 13999 * RFC3530 lets servers require that unlocks exactly match
13990 14000 * the locks that are held.
13991 14001 */
13992 14002 if (resend_rqstp != NULL &&
13993 14003 resend_rqstp->lr_ctype != NFS4_LCK_CTYPE_REINSTATE) {
13994 14004 nfs4_reinstitute_local_lock_state(vp, flk, cr, lop);
13995 14005 } else {
13996 14006 flk->l_whence = 0;
13997 14007 }
13998 14008 } else if (locku_args) {
13999 14009 LOCKU4res *locku_res;
14000 14010
14001 14011 locku_res = &resop->nfs_resop4_u.oplocku;
14002 14012
14003 14013 /* Update the stateid with the server's response */
14004 14014 nfs4_set_lock_stateid(lop, locku_res->lock_stateid);
14005 14015 } else if (lockt_args) {
14006 14016 /* Switch the lock type to express success, see fcntl */
14007 14017 flk->l_type = F_UNLCK;
14008 14018 flk->l_whence = 0;
14009 14019 }
14010 14020 }
14011 14021
14012 14022 /*
14013 14023 * Do final cleanup before exiting nfs4frlock.
14014 14024 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the
14015 14025 * COMPOUND4 args/res for calls that haven't already.
14016 14026 */
14017 14027 static void
14018 14028 nfs4frlock_final_cleanup(nfs4_lock_call_type_t ctype, COMPOUND4args_clnt *argsp,
14019 14029 COMPOUND4res_clnt *resp, vnode_t *vp, nfs4_op_hint_t op_hint,
14020 14030 nfs4_recov_state_t *recov_statep, int needrecov, nfs4_open_owner_t *oop,
14021 14031 nfs4_open_stream_t *osp, nfs4_lock_owner_t *lop, flock64_t *flk,
14022 14032 short whence, u_offset_t offset, struct lm_sysid *ls,
14023 14033 int *errorp, LOCK4args *lock_args, LOCKU4args *locku_args,
14024 14034 bool_t did_start_fop, bool_t skip_get_err,
14025 14035 cred_t *cred_otw, cred_t *cred)
14026 14036 {
14027 14037 mntinfo4_t *mi = VTOMI4(vp);
14028 14038 rnode4_t *rp = VTOR4(vp);
14029 14039 int error = *errorp;
14030 14040 nfs_argop4 *argop;
14031 14041 int do_flush_pages = 0;
14032 14042
14033 14043 ASSERT(nfs_zone() == mi->mi_zone);
14034 14044 /*
14035 14045 * The client recovery code wants the raw status information,
14036 14046 * so don't map the NFS status code to an errno value for
14037 14047 * non-normal call types.
14038 14048 */
14039 14049 if (ctype == NFS4_LCK_CTYPE_NORM) {
14040 14050 if (*errorp == 0 && resp != NULL && skip_get_err == FALSE)
14041 14051 *errorp = geterrno4(resp->status);
14042 14052 if (did_start_fop == TRUE)
14043 14053 nfs4_end_fop(mi, vp, NULL, op_hint, recov_statep,
14044 14054 needrecov);
14045 14055
14046 14056 /*
14047 14057 * We've established a new lock on the server, so invalidate
14048 14058 * the pages associated with the vnode to get the most up to
14049 14059 * date pages from the server after acquiring the lock. We
14050 14060 * want to be sure that the read operation gets the newest data.
14051 14061 * N.B.
14052 14062 * We used to do this in nfs4frlock_results_ok but that doesn't
14053 14063 * work since VOP_PUTPAGE can call nfs4_commit which calls
14054 14064 * nfs4_start_fop. We flush the pages below after calling
14055 14065 * nfs4_end_fop above
14056 14066 * The flush of the page cache must be done after
14057 14067 * nfs4_end_open_seqid_sync() to avoid a 4-way hang.
14058 14068 */
14059 14069 if (!error && resp && resp->status == NFS4_OK)
14060 14070 do_flush_pages = 1;
14061 14071 }
14062 14072 if (argsp) {
14063 14073 ASSERT(argsp->array_len == 2);
14064 14074 argop = argsp->array;
14065 14075 if (argop[1].argop == OP_LOCK)
14066 14076 nfs4args_lock_free(&argop[1]);
14067 14077 else if (argop[1].argop == OP_LOCKT)
14068 14078 nfs4args_lockt_free(&argop[1]);
14069 14079 kmem_free(argop, 2 * sizeof (nfs_argop4));
14070 14080 if (resp)
14071 14081 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
14072 14082 }
14073 14083
14074 14084 /* free the reference on the lock owner */
14075 14085 if (lop != NULL) {
14076 14086 nfs4_end_lock_seqid_sync(lop);
14077 14087 lock_owner_rele(lop);
14078 14088 }
14079 14089
14080 14090 /* need to free up the reference on osp for lock args */
14081 14091 if (osp != NULL)
14082 14092 open_stream_rele(osp, rp);
14083 14093
14084 14094 /* need to free up the reference on oop for lock args */
14085 14095 if (oop != NULL) {
14086 14096 nfs4_end_open_seqid_sync(oop);
14087 14097 open_owner_rele(oop);
14088 14098 }
14089 14099
14090 14100 if (do_flush_pages)
14091 14101 nfs4_flush_pages(vp, cred);
14092 14102
14093 14103 (void) convoff(vp, flk, whence, offset);
14094 14104
14095 14105 lm_rel_sysid(ls);
14096 14106
14097 14107 /*
14098 14108 * Record debug information in the event we get EINVAL.
14099 14109 */
14100 14110 mutex_enter(&mi->mi_lock);
14101 14111 if (*errorp == EINVAL && (lock_args || locku_args) &&
14102 14112 (!(mi->mi_flags & MI4_POSIX_LOCK))) {
14103 14113 if (!(mi->mi_flags & MI4_LOCK_DEBUG)) {
14104 14114 zcmn_err(getzoneid(), CE_NOTE,
14105 14115 "%s operation failed with "
14106 14116 "EINVAL probably since the server, %s,"
14107 14117 " doesn't support POSIX style locking",
14108 14118 lock_args ? "LOCK" : "LOCKU",
14109 14119 mi->mi_curr_serv->sv_hostname);
14110 14120 mi->mi_flags |= MI4_LOCK_DEBUG;
14111 14121 }
14112 14122 }
14113 14123 mutex_exit(&mi->mi_lock);
14114 14124
14115 14125 if (cred_otw)
14116 14126 crfree(cred_otw);
14117 14127 }
14118 14128
14119 14129 /*
14120 14130 * This calls the server and the local locking code.
14121 14131 *
14122 14132 * Client locks are registerred locally by oring the sysid with
14123 14133 * LM_SYSID_CLIENT. The server registers locks locally using just the sysid.
14124 14134 * We need to distinguish between the two to avoid collision in case one
14125 14135 * machine is used as both client and server.
14126 14136 *
14127 14137 * Blocking lock requests will continually retry to acquire the lock
14128 14138 * forever.
14129 14139 *
14130 14140 * The ctype is defined as follows:
14131 14141 * NFS4_LCK_CTYPE_NORM: normal lock request.
14132 14142 *
14133 14143 * NFS4_LCK_CTYPE_RECLAIM: bypass the usual calls for synchronizing with client
14134 14144 * recovery, get the pid from flk instead of curproc, and don't reregister
14135 14145 * the lock locally.
14136 14146 *
14137 14147 * NFS4_LCK_CTYPE_RESEND: same as NFS4_LCK_CTYPE_RECLAIM, with the addition
14138 14148 * that we will use the information passed in via resend_rqstp to setup the
14139 14149 * lock/locku request. This resend is the exact same request as the 'lost
14140 14150 * lock', and is initiated by the recovery framework. A successful resend
14141 14151 * request can initiate one or more reinstate requests.
14142 14152 *
14143 14153 * NFS4_LCK_CTYPE_REINSTATE: same as NFS4_LCK_CTYPE_RESEND, except that it
14144 14154 * does not trigger additional reinstate requests. This lock call type is
14145 14155 * set for setting the v4 server's locking state back to match what the
14146 14156 * client's local locking state is in the event of a received 'lost lock'.
14147 14157 *
14148 14158 * Errors are returned via the nfs4_error_t parameter.
14149 14159 */
14150 14160 void
14151 14161 nfs4frlock(nfs4_lock_call_type_t ctype, vnode_t *vp, int cmd, flock64_t *flk,
14152 14162 int flag, u_offset_t offset, cred_t *cr, nfs4_error_t *ep,
14153 14163 nfs4_lost_rqst_t *resend_rqstp, int *did_reclaimp)
14154 14164 {
14155 14165 COMPOUND4args_clnt args, *argsp = NULL;
14156 14166 COMPOUND4res_clnt res, *resp = NULL;
14157 14167 nfs_argop4 *argop;
14158 14168 nfs_resop4 *resop;
14159 14169 rnode4_t *rp;
14160 14170 int doqueue = 1;
14161 14171 clock_t tick_delay; /* delay in clock ticks */
14162 14172 struct lm_sysid *ls;
14163 14173 LOCK4args *lock_args = NULL;
14164 14174 LOCKU4args *locku_args = NULL;
14165 14175 LOCKT4args *lockt_args = NULL;
14166 14176 nfs4_open_owner_t *oop = NULL;
14167 14177 nfs4_open_stream_t *osp = NULL;
14168 14178 nfs4_lock_owner_t *lop = NULL;
14169 14179 bool_t needrecov = FALSE;
14170 14180 nfs4_recov_state_t recov_state;
14171 14181 short whence;
14172 14182 nfs4_op_hint_t op_hint;
14173 14183 nfs4_lost_rqst_t lost_rqst;
14174 14184 bool_t retry = FALSE;
14175 14185 bool_t did_start_fop = FALSE;
14176 14186 bool_t skip_get_err = FALSE;
14177 14187 cred_t *cred_otw = NULL;
14178 14188 bool_t recovonly; /* just queue request */
14179 14189 int frc_no_reclaim = 0;
14180 14190 #ifdef DEBUG
14181 14191 char *name;
14182 14192 #endif
14183 14193
14184 14194 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
14185 14195
14186 14196 #ifdef DEBUG
14187 14197 name = fn_name(VTOSV(vp)->sv_name);
14188 14198 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4frlock: "
14189 14199 "%s: cmd %d, type %d, offset %llu, start %"PRIx64", "
14190 14200 "length %"PRIu64", pid %d, sysid %d, call type %s, "
14191 14201 "resend request %s", name, cmd, flk->l_type, offset, flk->l_start,
14192 14202 flk->l_len, ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pid :
14193 14203 flk->l_pid, flk->l_sysid, nfs4frlock_get_call_type(ctype),
14194 14204 resend_rqstp ? "TRUE" : "FALSE"));
14195 14205 kmem_free(name, MAXNAMELEN);
14196 14206 #endif
14197 14207
14198 14208 nfs4_error_zinit(ep);
14199 14209 ep->error = nfs4frlock_validate_args(cmd, flk, flag, vp, offset);
14200 14210 if (ep->error)
14201 14211 return;
14202 14212 ep->error = nfs4frlock_get_sysid(&ls, vp, flk);
14203 14213 if (ep->error)
14204 14214 return;
14205 14215 nfs4frlock_pre_setup(&tick_delay, &recov_state, flk, &whence,
14206 14216 vp, cr, &cred_otw);
14207 14217
14208 14218 recov_retry:
14209 14219 nfs4frlock_call_init(&args, &argsp, &argop, &op_hint, flk, cmd,
14210 14220 &retry, &did_start_fop, &resp, &skip_get_err, &lost_rqst);
14211 14221 rp = VTOR4(vp);
14212 14222
14213 14223 ep->error = nfs4frlock_start_call(ctype, vp, op_hint, &recov_state,
14214 14224 &did_start_fop, &recovonly);
14215 14225
14216 14226 if (ep->error)
14217 14227 goto out;
14218 14228
14219 14229 if (recovonly) {
14220 14230 /*
14221 14231 * Leave the request for the recovery system to deal with.
14222 14232 */
14223 14233 ASSERT(ctype == NFS4_LCK_CTYPE_NORM);
14224 14234 ASSERT(cmd != F_GETLK);
14225 14235 ASSERT(flk->l_type == F_UNLCK);
14226 14236
14227 14237 nfs4_error_init(ep, EINTR);
14228 14238 needrecov = TRUE;
14229 14239 lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY);
14230 14240 if (lop != NULL) {
14231 14241 nfs4frlock_save_lost_rqst(ctype, ep->error, READ_LT,
14232 14242 NULL, NULL, lop, flk, &lost_rqst, cr, vp);
14233 14243 (void) nfs4_start_recovery(ep,
14234 14244 VTOMI4(vp), vp, NULL, NULL,
14235 14245 (lost_rqst.lr_op == OP_LOCK ||
14236 14246 lost_rqst.lr_op == OP_LOCKU) ?
14237 14247 &lost_rqst : NULL, OP_LOCKU, NULL, NULL, NULL);
14238 14248 lock_owner_rele(lop);
14239 14249 lop = NULL;
14240 14250 }
14241 14251 flk->l_pid = curproc->p_pid;
14242 14252 nfs4_register_lock_locally(vp, flk, flag, offset);
14243 14253 goto out;
14244 14254 }
14245 14255
14246 14256 /* putfh directory fh */
14247 14257 argop[0].argop = OP_CPUTFH;
14248 14258 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
14249 14259
14250 14260 /*
14251 14261 * Set up the over-the-wire arguments and get references to the
14252 14262 * open owner, etc.
14253 14263 */
14254 14264
14255 14265 if (ctype == NFS4_LCK_CTYPE_RESEND ||
14256 14266 ctype == NFS4_LCK_CTYPE_REINSTATE) {
14257 14267 nfs4frlock_setup_resend_lock_args(resend_rqstp, argsp,
14258 14268 &argop[1], &lop, &oop, &osp, &lock_args, &locku_args);
14259 14269 } else {
14260 14270 bool_t go_otw = TRUE;
14261 14271
14262 14272 ASSERT(resend_rqstp == NULL);
14263 14273
14264 14274 switch (cmd) {
14265 14275 case F_GETLK:
14266 14276 case F_O_GETLK:
14267 14277 nfs4frlock_setup_lockt_args(ctype, &argop[1],
14268 14278 &lockt_args, argsp, flk, rp);
14269 14279 break;
14270 14280 case F_SETLKW:
14271 14281 case F_SETLK:
14272 14282 if (flk->l_type == F_UNLCK)
14273 14283 nfs4frlock_setup_locku_args(ctype,
14274 14284 &argop[1], &locku_args, flk,
14275 14285 &lop, ep, argsp,
14276 14286 vp, flag, offset, cr,
14277 14287 &skip_get_err, &go_otw);
14278 14288 else
14279 14289 nfs4frlock_setup_lock_args(ctype,
14280 14290 &lock_args, &oop, &osp, &lop, &argop[1],
14281 14291 argsp, flk, cmd, vp, cr, ep);
14282 14292
14283 14293 if (ep->error)
14284 14294 goto out;
14285 14295
14286 14296 switch (ep->stat) {
14287 14297 case NFS4_OK:
14288 14298 break;
14289 14299 case NFS4ERR_DELAY:
14290 14300 /* recov thread never gets this error */
14291 14301 ASSERT(resend_rqstp == NULL);
14292 14302 ASSERT(did_start_fop);
14293 14303
14294 14304 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint,
14295 14305 &recov_state, TRUE);
14296 14306 did_start_fop = FALSE;
14297 14307 if (argop[1].argop == OP_LOCK)
14298 14308 nfs4args_lock_free(&argop[1]);
14299 14309 else if (argop[1].argop == OP_LOCKT)
14300 14310 nfs4args_lockt_free(&argop[1]);
14301 14311 kmem_free(argop, 2 * sizeof (nfs_argop4));
14302 14312 argsp = NULL;
14303 14313 goto recov_retry;
14304 14314 default:
14305 14315 ep->error = EIO;
14306 14316 goto out;
14307 14317 }
14308 14318 break;
14309 14319 default:
14310 14320 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14311 14321 "nfs4_frlock: invalid cmd %d", cmd));
14312 14322 ep->error = EINVAL;
14313 14323 goto out;
14314 14324 }
14315 14325
14316 14326 if (!go_otw)
14317 14327 goto out;
14318 14328 }
14319 14329
14320 14330 /* XXX should we use the local reclock as a cache ? */
14321 14331 /*
14322 14332 * Unregister the lock with the local locking code before
14323 14333 * contacting the server. This avoids a potential race where
14324 14334 * another process gets notified that it has been granted a lock
14325 14335 * before we can unregister ourselves locally.
14326 14336 */
14327 14337 if ((cmd == F_SETLK || cmd == F_SETLKW) && flk->l_type == F_UNLCK) {
14328 14338 if (ctype == NFS4_LCK_CTYPE_NORM)
14329 14339 flk->l_pid = ttoproc(curthread)->p_pid;
14330 14340 nfs4_register_lock_locally(vp, flk, flag, offset);
14331 14341 }
14332 14342
14333 14343 /*
14334 14344 * Send the server the lock request. Continually loop with a delay
14335 14345 * if get error NFS4ERR_DENIED (for blocking locks) or NFS4ERR_GRACE.
14336 14346 */
14337 14347 resp = &res;
14338 14348
14339 14349 NFS4_DEBUG((nfs4_client_call_debug || nfs4_client_lock_debug),
14340 14350 (CE_NOTE,
14341 14351 "nfs4frlock: %s call, rp %s", needrecov ? "recov" : "first",
14342 14352 rnode4info(rp)));
14343 14353
14344 14354 if (lock_args && frc_no_reclaim) {
14345 14355 ASSERT(ctype == NFS4_LCK_CTYPE_RECLAIM);
14346 14356 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14347 14357 "nfs4frlock: frc_no_reclaim: clearing reclaim"));
14348 14358 lock_args->reclaim = FALSE;
14349 14359 if (did_reclaimp)
14350 14360 *did_reclaimp = 0;
14351 14361 }
14352 14362
14353 14363 /*
14354 14364 * Do the OTW call.
14355 14365 */
14356 14366 rfs4call(VTOMI4(vp), argsp, resp, cred_otw, &doqueue, 0, ep);
14357 14367
14358 14368 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14359 14369 "nfs4frlock: error %d, status %d", ep->error, resp->status));
14360 14370
14361 14371 needrecov = nfs4_needs_recovery(ep, TRUE, vp->v_vfsp);
14362 14372 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14363 14373 "nfs4frlock: needrecov %d", needrecov));
14364 14374
14365 14375 if (ep->error == 0 && nfs4_need_to_bump_seqid(resp))
14366 14376 nfs4frlock_bump_seqid(lock_args, locku_args, oop, lop,
14367 14377 args.ctag);
14368 14378
14369 14379 /*
14370 14380 * Check if one of these mutually exclusive error cases has
14371 14381 * happened:
14372 14382 * need to swap credentials due to access error
14373 14383 * recovery is needed
14374 14384 * different error (only known case is missing Kerberos ticket)
14375 14385 */
14376 14386
14377 14387 if ((ep->error == EACCES ||
14378 14388 (ep->error == 0 && resp->status == NFS4ERR_ACCESS)) &&
14379 14389 cred_otw != cr) {
14380 14390 nfs4frlock_check_access(vp, op_hint, &recov_state, needrecov,
14381 14391 &did_start_fop, &argsp, &resp, ep->error, &lop, &oop, &osp,
14382 14392 cr, &cred_otw);
14383 14393 goto recov_retry;
14384 14394 }
14385 14395
14386 14396 if (needrecov) {
14387 14397 /*
14388 14398 * LOCKT requests don't need to recover from lost
14389 14399 * requests since they don't create/modify state.
14390 14400 */
14391 14401 if ((ep->error == EINTR ||
14392 14402 NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp)) &&
14393 14403 lockt_args)
14394 14404 goto out;
14395 14405 /*
14396 14406 * Do not attempt recovery for requests initiated by
14397 14407 * the recovery framework. Let the framework redrive them.
14398 14408 */
14399 14409 if (ctype != NFS4_LCK_CTYPE_NORM)
14400 14410 goto out;
14401 14411 else {
14402 14412 ASSERT(resend_rqstp == NULL);
14403 14413 }
14404 14414
14405 14415 nfs4frlock_save_lost_rqst(ctype, ep->error,
14406 14416 flk_to_locktype(cmd, flk->l_type),
14407 14417 oop, osp, lop, flk, &lost_rqst, cred_otw, vp);
14408 14418
14409 14419 retry = nfs4frlock_recovery(needrecov, ep, &argsp,
14410 14420 &resp, lock_args, locku_args, &oop, &osp, &lop,
14411 14421 rp, vp, &recov_state, op_hint, &did_start_fop,
14412 14422 cmd != F_GETLK ? &lost_rqst : NULL, flk);
14413 14423
14414 14424 if (retry) {
14415 14425 ASSERT(oop == NULL);
14416 14426 ASSERT(osp == NULL);
14417 14427 ASSERT(lop == NULL);
14418 14428 goto recov_retry;
14419 14429 }
14420 14430 goto out;
14421 14431 }
14422 14432
14423 14433 /*
14424 14434 * Bail out if have reached this point with ep->error set. Can
14425 14435 * happen if (ep->error == EACCES && !needrecov && cred_otw == cr).
14426 14436 * This happens if Kerberos ticket has expired or has been
14427 14437 * destroyed.
14428 14438 */
14429 14439 if (ep->error != 0)
14430 14440 goto out;
14431 14441
14432 14442 /*
14433 14443 * Process the reply.
14434 14444 */
14435 14445 switch (resp->status) {
14436 14446 case NFS4_OK:
14437 14447 resop = &resp->array[1];
14438 14448 nfs4frlock_results_ok(ctype, cmd, flk, vp, flag, offset,
14439 14449 resend_rqstp);
14440 14450 /*
14441 14451 * Have a successful lock operation, now update state.
14442 14452 */
14443 14453 nfs4frlock_update_state(lock_args, locku_args, lockt_args,
14444 14454 resop, lop, vp, flk, cr, resend_rqstp);
14445 14455 break;
14446 14456
14447 14457 case NFS4ERR_DENIED:
14448 14458 resop = &resp->array[1];
14449 14459 retry = nfs4frlock_results_denied(ctype, lock_args, lockt_args,
14450 14460 &oop, &osp, &lop, cmd, vp, flk, op_hint,
14451 14461 &recov_state, needrecov, &argsp, &resp,
14452 14462 &tick_delay, &whence, &ep->error, resop, cr,
14453 14463 &did_start_fop, &skip_get_err);
14454 14464
14455 14465 if (retry) {
14456 14466 ASSERT(oop == NULL);
14457 14467 ASSERT(osp == NULL);
14458 14468 ASSERT(lop == NULL);
14459 14469 goto recov_retry;
14460 14470 }
14461 14471 break;
14462 14472 /*
14463 14473 * If the server won't let us reclaim, fall-back to trying to lock
14464 14474 * the file from scratch. Code elsewhere will check the changeinfo
14465 14475 * to ensure the file hasn't been changed.
14466 14476 */
14467 14477 case NFS4ERR_NO_GRACE:
14468 14478 if (lock_args && lock_args->reclaim == TRUE) {
14469 14479 ASSERT(ctype == NFS4_LCK_CTYPE_RECLAIM);
14470 14480 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14471 14481 "nfs4frlock: reclaim: NFS4ERR_NO_GRACE"));
14472 14482 frc_no_reclaim = 1;
14473 14483 /* clean up before retrying */
14474 14484 needrecov = 0;
14475 14485 (void) nfs4frlock_recovery(needrecov, ep, &argsp, &resp,
14476 14486 lock_args, locku_args, &oop, &osp, &lop, rp, vp,
14477 14487 &recov_state, op_hint, &did_start_fop, NULL, flk);
14478 14488 goto recov_retry;
14479 14489 }
14480 14490 /* FALLTHROUGH */
14481 14491
14482 14492 default:
14483 14493 nfs4frlock_results_default(resp, &ep->error);
14484 14494 break;
14485 14495 }
14486 14496 out:
14487 14497 /*
14488 14498 * Process and cleanup from error. Make interrupted unlock
14489 14499 * requests look successful, since they will be handled by the
14490 14500 * client recovery code.
14491 14501 */
14492 14502 nfs4frlock_final_cleanup(ctype, argsp, resp, vp, op_hint, &recov_state,
14493 14503 needrecov, oop, osp, lop, flk, whence, offset, ls, &ep->error,
14494 14504 lock_args, locku_args, did_start_fop,
14495 14505 skip_get_err, cred_otw, cr);
14496 14506
14497 14507 if (ep->error == EINTR && flk->l_type == F_UNLCK &&
14498 14508 (cmd == F_SETLK || cmd == F_SETLKW))
14499 14509 ep->error = 0;
14500 14510 }
14501 14511
14502 14512 /*
14503 14513 * nfs4_safelock:
14504 14514 *
14505 14515 * Return non-zero if the given lock request can be handled without
14506 14516 * violating the constraints on concurrent mapping and locking.
14507 14517 */
14508 14518
14509 14519 static int
14510 14520 nfs4_safelock(vnode_t *vp, const struct flock64 *bfp, cred_t *cr)
14511 14521 {
14512 14522 rnode4_t *rp = VTOR4(vp);
14513 14523 struct vattr va;
14514 14524 int error;
14515 14525
14516 14526 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
14517 14527 ASSERT(rp->r_mapcnt >= 0);
14518 14528 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock %s: "
14519 14529 "(%"PRIx64", %"PRIx64"); mapcnt = %ld", bfp->l_type == F_WRLCK ?
14520 14530 "write" : bfp->l_type == F_RDLCK ? "read" : "unlock",
14521 14531 bfp->l_start, bfp->l_len, rp->r_mapcnt));
14522 14532
14523 14533 if (rp->r_mapcnt == 0)
14524 14534 return (1); /* always safe if not mapped */
14525 14535
14526 14536 /*
14527 14537 * If the file is already mapped and there are locks, then they
14528 14538 * should be all safe locks. So adding or removing a lock is safe
14529 14539 * as long as the new request is safe (i.e., whole-file, meaning
14530 14540 * length and starting offset are both zero).
14531 14541 */
14532 14542
14533 14543 if (bfp->l_start != 0 || bfp->l_len != 0) {
14534 14544 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: "
14535 14545 "cannot lock a memory mapped file unless locking the "
14536 14546 "entire file: start %"PRIx64", len %"PRIx64,
14537 14547 bfp->l_start, bfp->l_len));
14538 14548 return (0);
14539 14549 }
14540 14550
14541 14551 /* mandatory locking and mapping don't mix */
14542 14552 va.va_mask = AT_MODE;
14543 14553 error = VOP_GETATTR(vp, &va, 0, cr, NULL);
14544 14554 if (error != 0) {
14545 14555 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: "
14546 14556 "getattr error %d", error));
14547 14557 return (0); /* treat errors conservatively */
14548 14558 }
14549 14559 if (MANDLOCK(vp, va.va_mode)) {
14550 14560 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: "
14551 14561 "cannot mandatory lock and mmap a file"));
14552 14562 return (0);
14553 14563 }
14554 14564
14555 14565 return (1);
14556 14566 }
14557 14567
14558 14568
14559 14569 /*
14560 14570 * Register the lock locally within Solaris.
14561 14571 * As the client, we "or" the sysid with LM_SYSID_CLIENT when
14562 14572 * recording locks locally.
14563 14573 *
14564 14574 * This should handle conflicts/cooperation with NFS v2/v3 since all locks
14565 14575 * are registered locally.
14566 14576 */
14567 14577 void
14568 14578 nfs4_register_lock_locally(vnode_t *vp, struct flock64 *flk, int flag,
14569 14579 u_offset_t offset)
14570 14580 {
14571 14581 int oldsysid;
14572 14582 int error;
14573 14583 #ifdef DEBUG
14574 14584 char *name;
14575 14585 #endif
14576 14586
14577 14587 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
14578 14588
14579 14589 #ifdef DEBUG
14580 14590 name = fn_name(VTOSV(vp)->sv_name);
14581 14591 NFS4_DEBUG(nfs4_client_lock_debug,
14582 14592 (CE_NOTE, "nfs4_register_lock_locally: %s: type %d, "
14583 14593 "start %"PRIx64", length %"PRIx64", pid %ld, sysid %d",
14584 14594 name, flk->l_type, flk->l_start, flk->l_len, (long)flk->l_pid,
14585 14595 flk->l_sysid));
14586 14596 kmem_free(name, MAXNAMELEN);
14587 14597 #endif
14588 14598
14589 14599 /* register the lock with local locking */
14590 14600 oldsysid = flk->l_sysid;
14591 14601 flk->l_sysid |= LM_SYSID_CLIENT;
14592 14602 error = reclock(vp, flk, SETFLCK, flag, offset, NULL);
14593 14603 #ifdef DEBUG
14594 14604 if (error != 0) {
14595 14605 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14596 14606 "nfs4_register_lock_locally: could not register with"
14597 14607 " local locking"));
14598 14608 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT,
14599 14609 "error %d, vp 0x%p, pid %d, sysid 0x%x",
14600 14610 error, (void *)vp, flk->l_pid, flk->l_sysid));
14601 14611 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT,
14602 14612 "type %d off 0x%" PRIx64 " len 0x%" PRIx64,
14603 14613 flk->l_type, flk->l_start, flk->l_len));
14604 14614 (void) reclock(vp, flk, 0, flag, offset, NULL);
14605 14615 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT,
14606 14616 "blocked by pid %d sysid 0x%x type %d "
14607 14617 "off 0x%" PRIx64 " len 0x%" PRIx64,
14608 14618 flk->l_pid, flk->l_sysid, flk->l_type, flk->l_start,
14609 14619 flk->l_len));
14610 14620 }
14611 14621 #endif
14612 14622 flk->l_sysid = oldsysid;
14613 14623 }
14614 14624
14615 14625 /*
14616 14626 * nfs4_lockrelease:
14617 14627 *
14618 14628 * Release any locks on the given vnode that are held by the current
14619 14629 * process. Also removes the lock owner (if one exists) from the rnode's
14620 14630 * list.
14621 14631 */
14622 14632 static int
14623 14633 nfs4_lockrelease(vnode_t *vp, int flag, offset_t offset, cred_t *cr)
14624 14634 {
14625 14635 flock64_t ld;
14626 14636 int ret, error;
14627 14637 rnode4_t *rp;
14628 14638 nfs4_lock_owner_t *lop;
14629 14639 nfs4_recov_state_t recov_state;
14630 14640 mntinfo4_t *mi;
14631 14641 bool_t possible_orphan = FALSE;
14632 14642 bool_t recovonly;
14633 14643
14634 14644 ASSERT((uintptr_t)vp > KERNELBASE);
14635 14645 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
14636 14646
14637 14647 rp = VTOR4(vp);
14638 14648 mi = VTOMI4(vp);
14639 14649
14640 14650 /*
14641 14651 * If we have not locked anything then we can
14642 14652 * just return since we have no work to do.
14643 14653 */
14644 14654 if (rp->r_lo_head.lo_next_rnode == &rp->r_lo_head) {
14645 14655 return (0);
14646 14656 }
14647 14657
14648 14658 /*
14649 14659 * We need to comprehend that another thread may
14650 14660 * kick off recovery and the lock_owner we have stashed
14651 14661 * in lop might be invalid so we should NOT cache it
14652 14662 * locally!
14653 14663 */
14654 14664 recov_state.rs_flags = 0;
14655 14665 recov_state.rs_num_retry_despite_err = 0;
14656 14666 error = nfs4_start_fop(mi, vp, NULL, OH_LOCKU, &recov_state,
14657 14667 &recovonly);
14658 14668 if (error) {
14659 14669 mutex_enter(&rp->r_statelock);
14660 14670 rp->r_flags |= R4LODANGLERS;
14661 14671 mutex_exit(&rp->r_statelock);
14662 14672 return (error);
14663 14673 }
14664 14674
14665 14675 lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY);
14666 14676
14667 14677 /*
14668 14678 * Check if the lock owner might have a lock (request was sent but
14669 14679 * no response was received). Also check if there are any remote
14670 14680 * locks on the file. (In theory we shouldn't have to make this
14671 14681 * second check if there's no lock owner, but for now we'll be
14672 14682 * conservative and do it anyway.) If either condition is true,
14673 14683 * send an unlock for the entire file to the server.
14674 14684 *
14675 14685 * Note that no explicit synchronization is needed here. At worst,
14676 14686 * flk_has_remote_locks() will return a false positive, in which case
14677 14687 * the unlock call wastes time but doesn't harm correctness.
14678 14688 */
14679 14689
14680 14690 if (lop) {
14681 14691 mutex_enter(&lop->lo_lock);
14682 14692 possible_orphan = lop->lo_pending_rqsts;
14683 14693 mutex_exit(&lop->lo_lock);
14684 14694 lock_owner_rele(lop);
14685 14695 }
14686 14696
14687 14697 nfs4_end_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 0);
14688 14698
14689 14699 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14690 14700 "nfs4_lockrelease: possible orphan %d, remote locks %d, for "
14691 14701 "lop %p.", possible_orphan, flk_has_remote_locks(vp),
14692 14702 (void *)lop));
14693 14703
14694 14704 if (possible_orphan || flk_has_remote_locks(vp)) {
14695 14705 ld.l_type = F_UNLCK; /* set to unlock entire file */
14696 14706 ld.l_whence = 0; /* unlock from start of file */
14697 14707 ld.l_start = 0;
14698 14708 ld.l_len = 0; /* do entire file */
14699 14709
14700 14710 ret = VOP_FRLOCK(vp, F_SETLK, &ld, flag, offset, NULL,
14701 14711 cr, NULL);
14702 14712
14703 14713 if (ret != 0) {
14704 14714 /*
14705 14715 * If VOP_FRLOCK fails, make sure we unregister
14706 14716 * local locks before we continue.
14707 14717 */
14708 14718 ld.l_pid = ttoproc(curthread)->p_pid;
14709 14719 nfs4_register_lock_locally(vp, &ld, flag, offset);
14710 14720 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14711 14721 "nfs4_lockrelease: lock release error on vp"
14712 14722 " %p: error %d.\n", (void *)vp, ret));
14713 14723 }
14714 14724 }
14715 14725
14716 14726 recov_state.rs_flags = 0;
14717 14727 recov_state.rs_num_retry_despite_err = 0;
14718 14728 error = nfs4_start_fop(mi, vp, NULL, OH_LOCKU, &recov_state,
14719 14729 &recovonly);
14720 14730 if (error) {
14721 14731 mutex_enter(&rp->r_statelock);
14722 14732 rp->r_flags |= R4LODANGLERS;
14723 14733 mutex_exit(&rp->r_statelock);
14724 14734 return (error);
14725 14735 }
14726 14736
14727 14737 /*
14728 14738 * So, here we're going to need to retrieve the lock-owner
14729 14739 * again (in case recovery has done a switch-a-roo) and
14730 14740 * remove it because we can.
14731 14741 */
14732 14742 lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY);
14733 14743
14734 14744 if (lop) {
14735 14745 nfs4_rnode_remove_lock_owner(rp, lop);
14736 14746 lock_owner_rele(lop);
14737 14747 }
14738 14748
14739 14749 nfs4_end_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 0);
14740 14750 return (0);
14741 14751 }
14742 14752
14743 14753 /*
14744 14754 * Wait for 'tick_delay' clock ticks.
14745 14755 * Implement exponential backoff until hit the lease_time of this nfs4_server.
14746 14756 * NOTE: lock_lease_time is in seconds.
14747 14757 *
14748 14758 * XXX For future improvements, should implement a waiting queue scheme.
14749 14759 */
14750 14760 static int
14751 14761 nfs4_block_and_wait(clock_t *tick_delay, rnode4_t *rp)
14752 14762 {
14753 14763 long milliseconds_delay;
14754 14764 time_t lock_lease_time;
14755 14765
14756 14766 /* wait tick_delay clock ticks or siginteruptus */
14757 14767 if (delay_sig(*tick_delay)) {
14758 14768 return (EINTR);
14759 14769 }
14760 14770 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_block_and_wait: "
14761 14771 "reissue the lock request: blocked for %ld clock ticks: %ld "
14762 14772 "milliseconds", *tick_delay, drv_hztousec(*tick_delay) / 1000));
14763 14773
14764 14774 /* get the lease time */
14765 14775 lock_lease_time = r2lease_time(rp);
14766 14776
14767 14777 /* drv_hztousec converts ticks to microseconds */
14768 14778 milliseconds_delay = drv_hztousec(*tick_delay) / 1000;
14769 14779 if (milliseconds_delay < lock_lease_time * 1000) {
14770 14780 *tick_delay = 2 * *tick_delay;
14771 14781 if (drv_hztousec(*tick_delay) > lock_lease_time * 1000 * 1000)
14772 14782 *tick_delay = drv_usectohz(lock_lease_time*1000*1000);
14773 14783 }
14774 14784 return (0);
14775 14785 }
14776 14786
14777 14787
14778 14788 void
14779 14789 nfs4_vnops_init(void)
14780 14790 {
14781 14791 }
14782 14792
14783 14793 void
14784 14794 nfs4_vnops_fini(void)
14785 14795 {
14786 14796 }
14787 14797
14788 14798 /*
14789 14799 * Return a reference to the directory (parent) vnode for a given vnode,
14790 14800 * using the saved pathname information and the directory file handle. The
14791 14801 * caller is responsible for disposing of the reference.
14792 14802 * Returns zero or an errno value.
14793 14803 *
14794 14804 * Caller should set need_start_op to FALSE if it is the recovery
14795 14805 * thread, or if a start_fop has already been done. Otherwise, TRUE.
14796 14806 */
14797 14807 int
14798 14808 vtodv(vnode_t *vp, vnode_t **dvpp, cred_t *cr, bool_t need_start_op)
14799 14809 {
14800 14810 svnode_t *svnp;
14801 14811 vnode_t *dvp = NULL;
14802 14812 servinfo4_t *svp;
14803 14813 nfs4_fname_t *mfname;
14804 14814 int error;
14805 14815
14806 14816 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
14807 14817
14808 14818 if (vp->v_flag & VROOT) {
14809 14819 nfs4_sharedfh_t *sfh;
14810 14820 nfs_fh4 fh;
14811 14821 mntinfo4_t *mi;
14812 14822
14813 14823 ASSERT(vp->v_type == VREG);
14814 14824
14815 14825 mi = VTOMI4(vp);
14816 14826 svp = mi->mi_curr_serv;
14817 14827 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
14818 14828 fh.nfs_fh4_len = svp->sv_pfhandle.fh_len;
14819 14829 fh.nfs_fh4_val = svp->sv_pfhandle.fh_buf;
14820 14830 sfh = sfh4_get(&fh, VTOMI4(vp));
14821 14831 nfs_rw_exit(&svp->sv_lock);
14822 14832 mfname = mi->mi_fname;
14823 14833 fn_hold(mfname);
14824 14834 dvp = makenfs4node_by_fh(sfh, NULL, &mfname, NULL, mi, cr, 0);
14825 14835 sfh4_rele(&sfh);
14826 14836
14827 14837 if (dvp->v_type == VNON)
14828 14838 dvp->v_type = VDIR;
14829 14839 *dvpp = dvp;
14830 14840 return (0);
14831 14841 }
14832 14842
14833 14843 svnp = VTOSV(vp);
14834 14844
14835 14845 if (svnp == NULL) {
14836 14846 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: "
14837 14847 "shadow node is NULL"));
14838 14848 return (EINVAL);
14839 14849 }
14840 14850
14841 14851 if (svnp->sv_name == NULL || svnp->sv_dfh == NULL) {
14842 14852 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: "
14843 14853 "shadow node name or dfh val == NULL"));
14844 14854 return (EINVAL);
14845 14855 }
14846 14856
14847 14857 error = nfs4_make_dotdot(svnp->sv_dfh, 0, vp, cr, &dvp,
14848 14858 (int)need_start_op);
14849 14859 if (error != 0) {
14850 14860 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: "
14851 14861 "nfs4_make_dotdot returned %d", error));
14852 14862 return (error);
14853 14863 }
14854 14864 if (!dvp) {
14855 14865 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: "
14856 14866 "nfs4_make_dotdot returned a NULL dvp"));
14857 14867 return (EIO);
14858 14868 }
14859 14869 if (dvp->v_type == VNON)
14860 14870 dvp->v_type = VDIR;
14861 14871 ASSERT(dvp->v_type == VDIR);
14862 14872 if (VTOR4(vp)->r_flags & R4ISXATTR) {
14863 14873 mutex_enter(&dvp->v_lock);
14864 14874 dvp->v_flag |= V_XATTRDIR;
14865 14875 mutex_exit(&dvp->v_lock);
14866 14876 }
14867 14877 *dvpp = dvp;
14868 14878 return (0);
14869 14879 }
14870 14880
14871 14881 /*
14872 14882 * Copy the (final) component name of vp to fnamep. maxlen is the maximum
14873 14883 * length that fnamep can accept, including the trailing null.
14874 14884 * Returns 0 if okay, returns an errno value if there was a problem.
14875 14885 */
14876 14886
14877 14887 int
14878 14888 vtoname(vnode_t *vp, char *fnamep, ssize_t maxlen)
14879 14889 {
14880 14890 char *fn;
14881 14891 int err = 0;
14882 14892 servinfo4_t *svp;
14883 14893 svnode_t *shvp;
14884 14894
14885 14895 /*
14886 14896 * If the file being opened has VROOT set, then this is
14887 14897 * a "file" mount. sv_name will not be interesting, so
14888 14898 * go back to the servinfo4 to get the original mount
14889 14899 * path and strip off all but the final edge. Otherwise
14890 14900 * just return the name from the shadow vnode.
14891 14901 */
14892 14902
14893 14903 if (vp->v_flag & VROOT) {
14894 14904
14895 14905 svp = VTOMI4(vp)->mi_curr_serv;
14896 14906 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
14897 14907
14898 14908 fn = strrchr(svp->sv_path, '/');
14899 14909 if (fn == NULL)
14900 14910 err = EINVAL;
14901 14911 else
14902 14912 fn++;
14903 14913 } else {
14904 14914 shvp = VTOSV(vp);
14905 14915 fn = fn_name(shvp->sv_name);
14906 14916 }
14907 14917
14908 14918 if (err == 0)
14909 14919 if (strlen(fn) < maxlen)
14910 14920 (void) strcpy(fnamep, fn);
14911 14921 else
14912 14922 err = ENAMETOOLONG;
14913 14923
14914 14924 if (vp->v_flag & VROOT)
14915 14925 nfs_rw_exit(&svp->sv_lock);
14916 14926 else
14917 14927 kmem_free(fn, MAXNAMELEN);
14918 14928
14919 14929 return (err);
14920 14930 }
14921 14931
14922 14932 /*
14923 14933 * Bookkeeping for a close that doesn't need to go over the wire.
14924 14934 * *have_lockp is set to 0 if 'os_sync_lock' is released; otherwise
14925 14935 * it is left at 1.
14926 14936 */
14927 14937 void
14928 14938 nfs4close_notw(vnode_t *vp, nfs4_open_stream_t *osp, int *have_lockp)
14929 14939 {
14930 14940 rnode4_t *rp;
14931 14941 mntinfo4_t *mi;
14932 14942
14933 14943 mi = VTOMI4(vp);
14934 14944 rp = VTOR4(vp);
14935 14945
14936 14946 NFS4_DEBUG(nfs4close_notw_debug, (CE_NOTE, "nfs4close_notw: "
14937 14947 "rp=%p osp=%p", (void *)rp, (void *)osp));
14938 14948 ASSERT(nfs_zone() == mi->mi_zone);
14939 14949 ASSERT(mutex_owned(&osp->os_sync_lock));
14940 14950 ASSERT(*have_lockp);
14941 14951
14942 14952 if (!osp->os_valid ||
14943 14953 osp->os_open_ref_count > 0 || osp->os_mapcnt > 0) {
14944 14954 return;
14945 14955 }
14946 14956
14947 14957 /*
14948 14958 * This removes the reference obtained at OPEN; ie,
14949 14959 * when the open stream structure was created.
14950 14960 *
14951 14961 * We don't have to worry about calling 'open_stream_rele'
14952 14962 * since we our currently holding a reference to this
14953 14963 * open stream which means the count can not go to 0 with
14954 14964 * this decrement.
14955 14965 */
14956 14966 ASSERT(osp->os_ref_count >= 2);
14957 14967 osp->os_ref_count--;
14958 14968 osp->os_valid = 0;
14959 14969 mutex_exit(&osp->os_sync_lock);
14960 14970 *have_lockp = 0;
14961 14971
14962 14972 nfs4_dec_state_ref_count(mi);
14963 14973 }
14964 14974
14965 14975 /*
14966 14976 * Close all remaining open streams on the rnode. These open streams
14967 14977 * could be here because:
14968 14978 * - The close attempted at either close or delmap failed
14969 14979 * - Some kernel entity did VOP_OPEN but never did VOP_CLOSE
14970 14980 * - Someone did mknod on a regular file but never opened it
14971 14981 */
14972 14982 int
14973 14983 nfs4close_all(vnode_t *vp, cred_t *cr)
14974 14984 {
14975 14985 nfs4_open_stream_t *osp;
14976 14986 int error;
14977 14987 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
14978 14988 rnode4_t *rp;
14979 14989
14980 14990 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
14981 14991
14982 14992 error = 0;
14983 14993 rp = VTOR4(vp);
14984 14994
14985 14995 /*
14986 14996 * At this point, all we know is that the last time
14987 14997 * someone called vn_rele, the count was 1. Since then,
14988 14998 * the vnode could have been re-activated. We want to
14989 14999 * loop through the open streams and close each one, but
14990 15000 * we have to be careful since once we release the rnode
14991 15001 * hash bucket lock, someone else is free to come in and
14992 15002 * re-activate the rnode and add new open streams. The
14993 15003 * strategy is take the rnode hash bucket lock, verify that
14994 15004 * the count is still 1, grab the open stream off the
14995 15005 * head of the list and mark it invalid, then release the
14996 15006 * rnode hash bucket lock and proceed with that open stream.
14997 15007 * This is ok because nfs4close_one() will acquire the proper
14998 15008 * open/create to close/destroy synchronization for open
14999 15009 * streams, and will ensure that if someone has reopened
15000 15010 * the open stream after we've dropped the hash bucket lock
15001 15011 * then we'll just simply return without destroying the
15002 15012 * open stream.
15003 15013 * Repeat until the list is empty.
15004 15014 */
15005 15015
15006 15016 for (;;) {
15007 15017
15008 15018 /* make sure vnode hasn't been reactivated */
15009 15019 rw_enter(&rp->r_hashq->r_lock, RW_READER);
15010 15020 mutex_enter(&vp->v_lock);
15011 15021 if (vp->v_count > 1) {
15012 15022 mutex_exit(&vp->v_lock);
15013 15023 rw_exit(&rp->r_hashq->r_lock);
15014 15024 break;
15015 15025 }
15016 15026 /*
15017 15027 * Grabbing r_os_lock before releasing v_lock prevents
15018 15028 * a window where the rnode/open stream could get
15019 15029 * reactivated (and os_force_close set to 0) before we
15020 15030 * had a chance to set os_force_close to 1.
15021 15031 */
15022 15032 mutex_enter(&rp->r_os_lock);
15023 15033 mutex_exit(&vp->v_lock);
15024 15034
15025 15035 osp = list_head(&rp->r_open_streams);
15026 15036 if (!osp) {
15027 15037 /* nothing left to CLOSE OTW, so return */
15028 15038 mutex_exit(&rp->r_os_lock);
15029 15039 rw_exit(&rp->r_hashq->r_lock);
15030 15040 break;
15031 15041 }
15032 15042
15033 15043 mutex_enter(&rp->r_statev4_lock);
15034 15044 /* the file can't still be mem mapped */
15035 15045 ASSERT(rp->r_mapcnt == 0);
15036 15046 if (rp->created_v4)
15037 15047 rp->created_v4 = 0;
15038 15048 mutex_exit(&rp->r_statev4_lock);
15039 15049
15040 15050 /*
15041 15051 * Grab a ref on this open stream; nfs4close_one
15042 15052 * will mark it as invalid
15043 15053 */
15044 15054 mutex_enter(&osp->os_sync_lock);
15045 15055 osp->os_ref_count++;
15046 15056 osp->os_force_close = 1;
15047 15057 mutex_exit(&osp->os_sync_lock);
15048 15058 mutex_exit(&rp->r_os_lock);
15049 15059 rw_exit(&rp->r_hashq->r_lock);
15050 15060
15051 15061 nfs4close_one(vp, osp, cr, 0, NULL, &e, CLOSE_FORCE, 0, 0, 0);
15052 15062
15053 15063 /* Update error if it isn't already non-zero */
15054 15064 if (error == 0) {
15055 15065 if (e.error)
15056 15066 error = e.error;
15057 15067 else if (e.stat)
15058 15068 error = geterrno4(e.stat);
15059 15069 }
15060 15070
15061 15071 #ifdef DEBUG
15062 15072 nfs4close_all_cnt++;
15063 15073 #endif
15064 15074 /* Release the ref on osp acquired above. */
15065 15075 open_stream_rele(osp, rp);
15066 15076
15067 15077 /* Proceed to the next open stream, if any */
15068 15078 }
15069 15079 return (error);
15070 15080 }
15071 15081
15072 15082 /*
15073 15083 * nfs4close_one - close one open stream for a file if needed.
15074 15084 *
15075 15085 * "close_type" indicates which close path this is:
15076 15086 * CLOSE_NORM: close initiated via VOP_CLOSE.
15077 15087 * CLOSE_DELMAP: close initiated via VOP_DELMAP.
15078 15088 * CLOSE_FORCE: close initiated via VOP_INACTIVE. This path forces
15079 15089 * the close and release of client state for this open stream
15080 15090 * (unless someone else has the open stream open).
15081 15091 * CLOSE_RESEND: indicates the request is a replay of an earlier request
15082 15092 * (e.g., due to abort because of a signal).
15083 15093 * CLOSE_AFTER_RESEND: close initiated to "undo" a successful resent OPEN.
15084 15094 *
15085 15095 * CLOSE_RESEND and CLOSE_AFTER_RESEND will not attempt to retry after client
15086 15096 * recovery. Instead, the caller is expected to deal with retries.
15087 15097 *
15088 15098 * The caller can either pass in the osp ('provided_osp') or not.
15089 15099 *
15090 15100 * 'access_bits' represents the access we are closing/downgrading.
15091 15101 *
15092 15102 * 'len', 'prot', and 'mmap_flags' are used for CLOSE_DELMAP. 'len' is the
15093 15103 * number of bytes we are unmapping, 'maxprot' is the mmap protection, and
15094 15104 * 'mmap_flags' tells us the type of sharing (MAP_PRIVATE or MAP_SHARED).
15095 15105 *
15096 15106 * Errors are returned via the nfs4_error_t.
15097 15107 */
15098 15108 void
15099 15109 nfs4close_one(vnode_t *vp, nfs4_open_stream_t *provided_osp, cred_t *cr,
15100 15110 int access_bits, nfs4_lost_rqst_t *lrp, nfs4_error_t *ep,
15101 15111 nfs4_close_type_t close_type, size_t len, uint_t maxprot,
15102 15112 uint_t mmap_flags)
15103 15113 {
15104 15114 nfs4_open_owner_t *oop;
15105 15115 nfs4_open_stream_t *osp = NULL;
15106 15116 int retry = 0;
15107 15117 int num_retries = NFS4_NUM_RECOV_RETRIES;
15108 15118 rnode4_t *rp;
15109 15119 mntinfo4_t *mi;
15110 15120 nfs4_recov_state_t recov_state;
15111 15121 cred_t *cred_otw = NULL;
15112 15122 bool_t recovonly = FALSE;
15113 15123 int isrecov;
15114 15124 int force_close;
15115 15125 int close_failed = 0;
15116 15126 int did_dec_count = 0;
15117 15127 int did_start_op = 0;
15118 15128 int did_force_recovlock = 0;
15119 15129 int did_start_seqid_sync = 0;
15120 15130 int have_sync_lock = 0;
15121 15131
15122 15132 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
15123 15133
15124 15134 NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE, "closing vp %p osp %p, "
15125 15135 "lrp %p, close type %d len %ld prot %x mmap flags %x bits %x",
15126 15136 (void *)vp, (void *)provided_osp, (void *)lrp, close_type,
15127 15137 len, maxprot, mmap_flags, access_bits));
15128 15138
15129 15139 nfs4_error_zinit(ep);
15130 15140 rp = VTOR4(vp);
15131 15141 mi = VTOMI4(vp);
15132 15142 isrecov = (close_type == CLOSE_RESEND ||
15133 15143 close_type == CLOSE_AFTER_RESEND);
15134 15144
15135 15145 /*
15136 15146 * First get the open owner.
15137 15147 */
15138 15148 if (!provided_osp) {
15139 15149 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi);
15140 15150 } else {
15141 15151 oop = provided_osp->os_open_owner;
15142 15152 ASSERT(oop != NULL);
15143 15153 open_owner_hold(oop);
15144 15154 }
15145 15155
15146 15156 if (!oop) {
15147 15157 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
15148 15158 "nfs4close_one: no oop, rp %p, mi %p, cr %p, osp %p, "
15149 15159 "close type %d", (void *)rp, (void *)mi, (void *)cr,
15150 15160 (void *)provided_osp, close_type));
15151 15161 ep->error = EIO;
15152 15162 goto out;
15153 15163 }
15154 15164
15155 15165 cred_otw = nfs4_get_otw_cred(cr, mi, oop);
15156 15166 recov_retry:
15157 15167 osp = NULL;
15158 15168 close_failed = 0;
15159 15169 force_close = (close_type == CLOSE_FORCE);
15160 15170 retry = 0;
15161 15171 did_start_op = 0;
15162 15172 did_force_recovlock = 0;
15163 15173 did_start_seqid_sync = 0;
15164 15174 have_sync_lock = 0;
15165 15175 recovonly = FALSE;
15166 15176 recov_state.rs_flags = 0;
15167 15177 recov_state.rs_num_retry_despite_err = 0;
15168 15178
15169 15179 /*
15170 15180 * Second synchronize with recovery.
15171 15181 */
15172 15182 if (!isrecov) {
15173 15183 ep->error = nfs4_start_fop(mi, vp, NULL, OH_CLOSE,
15174 15184 &recov_state, &recovonly);
15175 15185 if (!ep->error) {
15176 15186 did_start_op = 1;
15177 15187 } else {
15178 15188 close_failed = 1;
15179 15189 /*
15180 15190 * If we couldn't get start_fop, but have to
15181 15191 * cleanup state, then at least acquire the
15182 15192 * mi_recovlock so we can synchronize with
15183 15193 * recovery.
15184 15194 */
15185 15195 if (close_type == CLOSE_FORCE) {
15186 15196 (void) nfs_rw_enter_sig(&mi->mi_recovlock,
15187 15197 RW_READER, FALSE);
15188 15198 did_force_recovlock = 1;
15189 15199 } else
15190 15200 goto out;
15191 15201 }
15192 15202 }
15193 15203
15194 15204 /*
15195 15205 * We cannot attempt to get the open seqid sync if nfs4_start_fop
15196 15206 * set 'recovonly' to TRUE since most likely this is due to
15197 15207 * reovery being active (MI4_RECOV_ACTIV). If recovery is active,
15198 15208 * nfs4_start_open_seqid_sync() will fail with EAGAIN asking us
15199 15209 * to retry, causing us to loop until recovery finishes. Plus we
15200 15210 * don't need protection over the open seqid since we're not going
15201 15211 * OTW, hence don't need to use the seqid.
15202 15212 */
15203 15213 if (recovonly == FALSE) {
15204 15214 /* need to grab the open owner sync before 'os_sync_lock' */
15205 15215 ep->error = nfs4_start_open_seqid_sync(oop, mi);
15206 15216 if (ep->error == EAGAIN) {
15207 15217 ASSERT(!isrecov);
15208 15218 if (did_start_op)
15209 15219 nfs4_end_fop(mi, vp, NULL, OH_CLOSE,
15210 15220 &recov_state, TRUE);
15211 15221 if (did_force_recovlock)
15212 15222 nfs_rw_exit(&mi->mi_recovlock);
15213 15223 goto recov_retry;
15214 15224 }
15215 15225 did_start_seqid_sync = 1;
15216 15226 }
15217 15227
15218 15228 /*
15219 15229 * Third get an open stream and acquire 'os_sync_lock' to
15220 15230 * sychronize the opening/creating of an open stream with the
15221 15231 * closing/destroying of an open stream.
15222 15232 */
15223 15233 if (!provided_osp) {
15224 15234 /* returns with 'os_sync_lock' held */
15225 15235 osp = find_open_stream(oop, rp);
15226 15236 if (!osp) {
15227 15237 ep->error = EIO;
15228 15238 goto out;
15229 15239 }
15230 15240 } else {
15231 15241 osp = provided_osp;
15232 15242 open_stream_hold(osp);
15233 15243 mutex_enter(&osp->os_sync_lock);
15234 15244 }
15235 15245 have_sync_lock = 1;
15236 15246
15237 15247 ASSERT(oop == osp->os_open_owner);
15238 15248
15239 15249 /*
15240 15250 * Fourth, do any special pre-OTW CLOSE processing
15241 15251 * based on the specific close type.
15242 15252 */
15243 15253 if ((close_type == CLOSE_NORM || close_type == CLOSE_AFTER_RESEND) &&
15244 15254 !did_dec_count) {
15245 15255 ASSERT(osp->os_open_ref_count > 0);
15246 15256 osp->os_open_ref_count--;
15247 15257 did_dec_count = 1;
15248 15258 if (osp->os_open_ref_count == 0)
15249 15259 osp->os_final_close = 1;
15250 15260 }
15251 15261
15252 15262 if (close_type == CLOSE_FORCE) {
15253 15263 /* see if somebody reopened the open stream. */
15254 15264 if (!osp->os_force_close) {
15255 15265 NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE,
15256 15266 "nfs4close_one: skip CLOSE_FORCE as osp %p "
15257 15267 "was reopened, vp %p", (void *)osp, (void *)vp));
15258 15268 ep->error = 0;
15259 15269 ep->stat = NFS4_OK;
15260 15270 goto out;
15261 15271 }
15262 15272
15263 15273 if (!osp->os_final_close && !did_dec_count) {
15264 15274 osp->os_open_ref_count--;
15265 15275 did_dec_count = 1;
15266 15276 }
15267 15277
15268 15278 /*
15269 15279 * We can't depend on os_open_ref_count being 0 due to the
15270 15280 * way executables are opened (VN_RELE to match a VOP_OPEN).
15271 15281 */
15272 15282 #ifdef NOTYET
15273 15283 ASSERT(osp->os_open_ref_count == 0);
15274 15284 #endif
15275 15285 if (osp->os_open_ref_count != 0) {
15276 15286 NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE,
15277 15287 "nfs4close_one: should panic here on an "
15278 15288 "ASSERT(osp->os_open_ref_count == 0). Ignoring "
15279 15289 "since this is probably the exec problem."));
15280 15290
15281 15291 osp->os_open_ref_count = 0;
15282 15292 }
15283 15293
15284 15294 /*
15285 15295 * There is the possibility that nfs4close_one()
15286 15296 * for close_type == CLOSE_DELMAP couldn't find the
15287 15297 * open stream, thus couldn't decrement its os_mapcnt;
15288 15298 * therefore we can't use this ASSERT yet.
15289 15299 */
15290 15300 #ifdef NOTYET
15291 15301 ASSERT(osp->os_mapcnt == 0);
15292 15302 #endif
15293 15303 osp->os_mapcnt = 0;
15294 15304 }
15295 15305
15296 15306 if (close_type == CLOSE_DELMAP && !did_dec_count) {
15297 15307 ASSERT(osp->os_mapcnt >= btopr(len));
15298 15308
15299 15309 if ((mmap_flags & MAP_SHARED) && (maxprot & PROT_WRITE))
15300 15310 osp->os_mmap_write -= btopr(len);
15301 15311 if (maxprot & PROT_READ)
15302 15312 osp->os_mmap_read -= btopr(len);
15303 15313 if (maxprot & PROT_EXEC)
15304 15314 osp->os_mmap_read -= btopr(len);
15305 15315 /* mirror the PROT_NONE check in nfs4_addmap() */
15306 15316 if (!(maxprot & PROT_READ) && !(maxprot & PROT_WRITE) &&
15307 15317 !(maxprot & PROT_EXEC))
15308 15318 osp->os_mmap_read -= btopr(len);
15309 15319 osp->os_mapcnt -= btopr(len);
15310 15320 did_dec_count = 1;
15311 15321 }
15312 15322
15313 15323 if (recovonly) {
15314 15324 nfs4_lost_rqst_t lost_rqst;
15315 15325
15316 15326 /* request should not already be in recovery queue */
15317 15327 ASSERT(lrp == NULL);
15318 15328 nfs4_error_init(ep, EINTR);
15319 15329 nfs4close_save_lost_rqst(ep->error, &lost_rqst, oop,
15320 15330 osp, cred_otw, vp);
15321 15331 mutex_exit(&osp->os_sync_lock);
15322 15332 have_sync_lock = 0;
15323 15333 (void) nfs4_start_recovery(ep, mi, vp, NULL, NULL,
15324 15334 lost_rqst.lr_op == OP_CLOSE ?
15325 15335 &lost_rqst : NULL, OP_CLOSE, NULL, NULL, NULL);
15326 15336 close_failed = 1;
15327 15337 force_close = 0;
15328 15338 goto close_cleanup;
15329 15339 }
15330 15340
15331 15341 /*
15332 15342 * If a previous OTW call got NFS4ERR_BAD_SEQID, then
15333 15343 * we stopped operating on the open owner's <old oo_name, old seqid>
15334 15344 * space, which means we stopped operating on the open stream
15335 15345 * too. So don't go OTW (as the seqid is likely bad, and the
15336 15346 * stateid could be stale, potentially triggering a false
15337 15347 * setclientid), and just clean up the client's internal state.
15338 15348 */
15339 15349 if (osp->os_orig_oo_name != oop->oo_name) {
15340 15350 NFS4_DEBUG(nfs4close_one_debug || nfs4_client_recov_debug,
15341 15351 (CE_NOTE, "nfs4close_one: skip OTW close for osp %p "
15342 15352 "oop %p due to bad seqid (orig oo_name %" PRIx64 " current "
15343 15353 "oo_name %" PRIx64")",
15344 15354 (void *)osp, (void *)oop, osp->os_orig_oo_name,
15345 15355 oop->oo_name));
15346 15356 close_failed = 1;
15347 15357 }
15348 15358
15349 15359 /* If the file failed recovery, just quit. */
15350 15360 mutex_enter(&rp->r_statelock);
15351 15361 if (rp->r_flags & R4RECOVERR) {
15352 15362 close_failed = 1;
15353 15363 }
15354 15364 mutex_exit(&rp->r_statelock);
15355 15365
15356 15366 /*
15357 15367 * If the force close path failed to obtain start_fop
15358 15368 * then skip the OTW close and just remove the state.
15359 15369 */
15360 15370 if (close_failed)
15361 15371 goto close_cleanup;
15362 15372
15363 15373 /*
15364 15374 * Fifth, check to see if there are still mapped pages or other
15365 15375 * opens using this open stream. If there are then we can't
15366 15376 * close yet but we can see if an OPEN_DOWNGRADE is necessary.
15367 15377 */
15368 15378 if (osp->os_open_ref_count > 0 || osp->os_mapcnt > 0) {
15369 15379 nfs4_lost_rqst_t new_lost_rqst;
15370 15380 bool_t needrecov = FALSE;
15371 15381 cred_t *odg_cred_otw = NULL;
15372 15382 seqid4 open_dg_seqid = 0;
15373 15383
15374 15384 if (osp->os_delegation) {
15375 15385 /*
15376 15386 * If this open stream was never OPENed OTW then we
15377 15387 * surely can't DOWNGRADE it (especially since the
15378 15388 * osp->open_stateid is really a delegation stateid
15379 15389 * when os_delegation is 1).
15380 15390 */
15381 15391 if (access_bits & FREAD)
15382 15392 osp->os_share_acc_read--;
15383 15393 if (access_bits & FWRITE)
15384 15394 osp->os_share_acc_write--;
15385 15395 osp->os_share_deny_none--;
15386 15396 nfs4_error_zinit(ep);
15387 15397 goto out;
15388 15398 }
15389 15399 nfs4_open_downgrade(access_bits, 0, oop, osp, vp, cr,
15390 15400 lrp, ep, &odg_cred_otw, &open_dg_seqid);
15391 15401 needrecov = nfs4_needs_recovery(ep, TRUE, mi->mi_vfsp);
15392 15402 if (needrecov && !isrecov) {
15393 15403 bool_t abort;
15394 15404 nfs4_bseqid_entry_t *bsep = NULL;
15395 15405
15396 15406 if (!ep->error && ep->stat == NFS4ERR_BAD_SEQID)
15397 15407 bsep = nfs4_create_bseqid_entry(oop, NULL,
15398 15408 vp, 0,
15399 15409 lrp ? TAG_OPEN_DG_LOST : TAG_OPEN_DG,
15400 15410 open_dg_seqid);
15401 15411
15402 15412 nfs4open_dg_save_lost_rqst(ep->error, &new_lost_rqst,
15403 15413 oop, osp, odg_cred_otw, vp, access_bits, 0);
15404 15414 mutex_exit(&osp->os_sync_lock);
15405 15415 have_sync_lock = 0;
15406 15416 abort = nfs4_start_recovery(ep, mi, vp, NULL, NULL,
15407 15417 new_lost_rqst.lr_op == OP_OPEN_DOWNGRADE ?
15408 15418 &new_lost_rqst : NULL, OP_OPEN_DOWNGRADE,
15409 15419 bsep, NULL, NULL);
15410 15420 if (odg_cred_otw)
15411 15421 crfree(odg_cred_otw);
15412 15422 if (bsep)
15413 15423 kmem_free(bsep, sizeof (*bsep));
15414 15424
15415 15425 if (abort == TRUE)
15416 15426 goto out;
15417 15427
15418 15428 if (did_start_seqid_sync) {
15419 15429 nfs4_end_open_seqid_sync(oop);
15420 15430 did_start_seqid_sync = 0;
15421 15431 }
15422 15432 open_stream_rele(osp, rp);
15423 15433
15424 15434 if (did_start_op)
15425 15435 nfs4_end_fop(mi, vp, NULL, OH_CLOSE,
15426 15436 &recov_state, FALSE);
15427 15437 if (did_force_recovlock)
15428 15438 nfs_rw_exit(&mi->mi_recovlock);
15429 15439
15430 15440 goto recov_retry;
15431 15441 } else {
15432 15442 if (odg_cred_otw)
15433 15443 crfree(odg_cred_otw);
15434 15444 }
15435 15445 goto out;
15436 15446 }
15437 15447
15438 15448 /*
15439 15449 * If this open stream was created as the results of an open
15440 15450 * while holding a delegation, then just release it; no need
15441 15451 * to do an OTW close. Otherwise do a "normal" OTW close.
15442 15452 */
15443 15453 if (osp->os_delegation) {
15444 15454 nfs4close_notw(vp, osp, &have_sync_lock);
15445 15455 nfs4_error_zinit(ep);
15446 15456 goto out;
15447 15457 }
15448 15458
15449 15459 /*
15450 15460 * If this stream is not valid, we're done.
15451 15461 */
15452 15462 if (!osp->os_valid) {
15453 15463 nfs4_error_zinit(ep);
15454 15464 goto out;
15455 15465 }
15456 15466
15457 15467 /*
15458 15468 * Last open or mmap ref has vanished, need to do an OTW close.
15459 15469 * First check to see if a close is still necessary.
15460 15470 */
15461 15471 if (osp->os_failed_reopen) {
15462 15472 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
15463 15473 "don't close OTW osp %p since reopen failed.",
15464 15474 (void *)osp));
15465 15475 /*
15466 15476 * Reopen of the open stream failed, hence the
15467 15477 * stateid of the open stream is invalid/stale, and
15468 15478 * sending this OTW would incorrectly cause another
15469 15479 * round of recovery. In this case, we need to set
15470 15480 * the 'os_valid' bit to 0 so another thread doesn't
15471 15481 * come in and re-open this open stream before
15472 15482 * this "closing" thread cleans up state (decrementing
15473 15483 * the nfs4_server_t's state_ref_count and decrementing
15474 15484 * the os_ref_count).
15475 15485 */
15476 15486 osp->os_valid = 0;
15477 15487 /*
15478 15488 * This removes the reference obtained at OPEN; ie,
15479 15489 * when the open stream structure was created.
15480 15490 *
15481 15491 * We don't have to worry about calling 'open_stream_rele'
15482 15492 * since we our currently holding a reference to this
15483 15493 * open stream which means the count can not go to 0 with
15484 15494 * this decrement.
15485 15495 */
15486 15496 ASSERT(osp->os_ref_count >= 2);
15487 15497 osp->os_ref_count--;
15488 15498 nfs4_error_zinit(ep);
15489 15499 close_failed = 0;
15490 15500 goto close_cleanup;
15491 15501 }
15492 15502
15493 15503 ASSERT(osp->os_ref_count > 1);
15494 15504
15495 15505 /*
15496 15506 * Sixth, try the CLOSE OTW.
15497 15507 */
15498 15508 nfs4close_otw(rp, cred_otw, oop, osp, &retry, &did_start_seqid_sync,
15499 15509 close_type, ep, &have_sync_lock);
15500 15510
15501 15511 if (ep->error == EINTR || NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp)) {
15502 15512 /*
15503 15513 * Let the recovery thread be responsible for
15504 15514 * removing the state for CLOSE.
15505 15515 */
15506 15516 close_failed = 1;
15507 15517 force_close = 0;
15508 15518 retry = 0;
15509 15519 }
15510 15520
15511 15521 /* See if we need to retry with a different cred */
15512 15522 if ((ep->error == EACCES ||
15513 15523 (ep->error == 0 && ep->stat == NFS4ERR_ACCESS)) &&
15514 15524 cred_otw != cr) {
15515 15525 crfree(cred_otw);
15516 15526 cred_otw = cr;
15517 15527 crhold(cred_otw);
15518 15528 retry = 1;
15519 15529 }
15520 15530
15521 15531 if (ep->error || ep->stat)
15522 15532 close_failed = 1;
15523 15533
15524 15534 if (retry && !isrecov && num_retries-- > 0) {
15525 15535 if (have_sync_lock) {
15526 15536 mutex_exit(&osp->os_sync_lock);
15527 15537 have_sync_lock = 0;
15528 15538 }
15529 15539 if (did_start_seqid_sync) {
15530 15540 nfs4_end_open_seqid_sync(oop);
15531 15541 did_start_seqid_sync = 0;
15532 15542 }
15533 15543 open_stream_rele(osp, rp);
15534 15544
15535 15545 if (did_start_op)
15536 15546 nfs4_end_fop(mi, vp, NULL, OH_CLOSE,
15537 15547 &recov_state, FALSE);
15538 15548 if (did_force_recovlock)
15539 15549 nfs_rw_exit(&mi->mi_recovlock);
15540 15550 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
15541 15551 "nfs4close_one: need to retry the close "
15542 15552 "operation"));
15543 15553 goto recov_retry;
15544 15554 }
15545 15555 close_cleanup:
15546 15556 /*
15547 15557 * Seventh and lastly, process our results.
15548 15558 */
15549 15559 if (close_failed && force_close) {
15550 15560 /*
15551 15561 * It's ok to drop and regrab the 'os_sync_lock' since
15552 15562 * nfs4close_notw() will recheck to make sure the
15553 15563 * "close"/removal of state should happen.
15554 15564 */
15555 15565 if (!have_sync_lock) {
15556 15566 mutex_enter(&osp->os_sync_lock);
15557 15567 have_sync_lock = 1;
15558 15568 }
15559 15569 /*
15560 15570 * This is last call, remove the ref on the open
15561 15571 * stream created by open and clean everything up.
15562 15572 */
15563 15573 osp->os_pending_close = 0;
15564 15574 nfs4close_notw(vp, osp, &have_sync_lock);
15565 15575 nfs4_error_zinit(ep);
15566 15576 }
15567 15577
15568 15578 if (!close_failed) {
15569 15579 if (have_sync_lock) {
15570 15580 osp->os_pending_close = 0;
15571 15581 mutex_exit(&osp->os_sync_lock);
15572 15582 have_sync_lock = 0;
15573 15583 } else {
15574 15584 mutex_enter(&osp->os_sync_lock);
15575 15585 osp->os_pending_close = 0;
15576 15586 mutex_exit(&osp->os_sync_lock);
15577 15587 }
15578 15588 if (did_start_op && recov_state.rs_sp != NULL) {
15579 15589 mutex_enter(&recov_state.rs_sp->s_lock);
15580 15590 nfs4_dec_state_ref_count_nolock(recov_state.rs_sp, mi);
15581 15591 mutex_exit(&recov_state.rs_sp->s_lock);
15582 15592 } else {
15583 15593 nfs4_dec_state_ref_count(mi);
15584 15594 }
15585 15595 nfs4_error_zinit(ep);
15586 15596 }
15587 15597
15588 15598 out:
15589 15599 if (have_sync_lock)
15590 15600 mutex_exit(&osp->os_sync_lock);
15591 15601 if (did_start_op)
15592 15602 nfs4_end_fop(mi, vp, NULL, OH_CLOSE, &recov_state,
15593 15603 recovonly ? TRUE : FALSE);
15594 15604 if (did_force_recovlock)
15595 15605 nfs_rw_exit(&mi->mi_recovlock);
15596 15606 if (cred_otw)
15597 15607 crfree(cred_otw);
15598 15608 if (osp)
15599 15609 open_stream_rele(osp, rp);
15600 15610 if (oop) {
15601 15611 if (did_start_seqid_sync)
15602 15612 nfs4_end_open_seqid_sync(oop);
15603 15613 open_owner_rele(oop);
15604 15614 }
15605 15615 }
15606 15616
15607 15617 /*
15608 15618 * Convert information returned by the server in the LOCK4denied
15609 15619 * structure to the form required by fcntl.
15610 15620 */
15611 15621 static void
15612 15622 denied_to_flk(LOCK4denied *lockt_denied, flock64_t *flk, LOCKT4args *lockt_args)
15613 15623 {
15614 15624 nfs4_lo_name_t *lo;
15615 15625
15616 15626 #ifdef DEBUG
15617 15627 if (denied_to_flk_debug) {
15618 15628 lockt_denied_debug = lockt_denied;
15619 15629 debug_enter("lockt_denied");
15620 15630 }
15621 15631 #endif
15622 15632
15623 15633 flk->l_type = lockt_denied->locktype == READ_LT ? F_RDLCK : F_WRLCK;
15624 15634 flk->l_whence = 0; /* aka SEEK_SET */
15625 15635 flk->l_start = lockt_denied->offset;
15626 15636 flk->l_len = lockt_denied->length;
15627 15637
15628 15638 /*
15629 15639 * If the blocking clientid matches our client id, then we can
15630 15640 * interpret the lockowner (since we built it). If not, then
15631 15641 * fabricate a sysid and pid. Note that the l_sysid field
15632 15642 * in *flk already has the local sysid.
15633 15643 */
15634 15644
15635 15645 if (lockt_denied->owner.clientid == lockt_args->owner.clientid) {
15636 15646
15637 15647 if (lockt_denied->owner.owner_len == sizeof (*lo)) {
15638 15648 lo = (nfs4_lo_name_t *)
15639 15649 lockt_denied->owner.owner_val;
15640 15650
15641 15651 flk->l_pid = lo->ln_pid;
15642 15652 } else {
15643 15653 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
15644 15654 "denied_to_flk: bad lock owner length\n"));
15645 15655
15646 15656 flk->l_pid = lo_to_pid(&lockt_denied->owner);
15647 15657 }
15648 15658 } else {
15649 15659 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
15650 15660 "denied_to_flk: foreign clientid\n"));
15651 15661
15652 15662 /*
15653 15663 * Construct a new sysid which should be different from
15654 15664 * sysids of other systems.
15655 15665 */
15656 15666
15657 15667 flk->l_sysid++;
15658 15668 flk->l_pid = lo_to_pid(&lockt_denied->owner);
15659 15669 }
15660 15670 }
15661 15671
15662 15672 static pid_t
15663 15673 lo_to_pid(lock_owner4 *lop)
15664 15674 {
15665 15675 pid_t pid = 0;
15666 15676 uchar_t *cp;
15667 15677 int i;
15668 15678
15669 15679 cp = (uchar_t *)&lop->clientid;
15670 15680
15671 15681 for (i = 0; i < sizeof (lop->clientid); i++)
15672 15682 pid += (pid_t)*cp++;
15673 15683
15674 15684 cp = (uchar_t *)lop->owner_val;
15675 15685
15676 15686 for (i = 0; i < lop->owner_len; i++)
15677 15687 pid += (pid_t)*cp++;
15678 15688
15679 15689 return (pid);
15680 15690 }
15681 15691
15682 15692 /*
15683 15693 * Given a lock pointer, returns the length of that lock.
15684 15694 * "end" is the last locked offset the "l_len" covers from
15685 15695 * the start of the lock.
15686 15696 */
15687 15697 static off64_t
15688 15698 lock_to_end(flock64_t *lock)
15689 15699 {
15690 15700 off64_t lock_end;
15691 15701
15692 15702 if (lock->l_len == 0)
15693 15703 lock_end = (off64_t)MAXEND;
15694 15704 else
15695 15705 lock_end = lock->l_start + lock->l_len - 1;
15696 15706
15697 15707 return (lock_end);
15698 15708 }
15699 15709
15700 15710 /*
15701 15711 * Given the end of a lock, it will return you the length "l_len" for that lock.
15702 15712 */
15703 15713 static off64_t
15704 15714 end_to_len(off64_t start, off64_t end)
15705 15715 {
15706 15716 off64_t lock_len;
15707 15717
15708 15718 ASSERT(end >= start);
15709 15719 if (end == MAXEND)
15710 15720 lock_len = 0;
15711 15721 else
15712 15722 lock_len = end - start + 1;
15713 15723
15714 15724 return (lock_len);
15715 15725 }
15716 15726
15717 15727 /*
15718 15728 * On given end for a lock it determines if it is the last locked offset
15719 15729 * or not, if so keeps it as is, else adds one to return the length for
15720 15730 * valid start.
15721 15731 */
15722 15732 static off64_t
15723 15733 start_check(off64_t x)
15724 15734 {
15725 15735 if (x == MAXEND)
15726 15736 return (x);
15727 15737 else
15728 15738 return (x + 1);
15729 15739 }
15730 15740
15731 15741 /*
15732 15742 * See if these two locks overlap, and if so return 1;
15733 15743 * otherwise, return 0.
15734 15744 */
15735 15745 static int
15736 15746 locks_intersect(flock64_t *llfp, flock64_t *curfp)
15737 15747 {
15738 15748 off64_t llfp_end, curfp_end;
15739 15749
15740 15750 llfp_end = lock_to_end(llfp);
15741 15751 curfp_end = lock_to_end(curfp);
15742 15752
15743 15753 if (((llfp_end >= curfp->l_start) &&
15744 15754 (llfp->l_start <= curfp->l_start)) ||
15745 15755 ((curfp->l_start <= llfp->l_start) && (curfp_end >= llfp->l_start)))
15746 15756 return (1);
15747 15757 return (0);
15748 15758 }
15749 15759
15750 15760 /*
15751 15761 * Determine what the intersecting lock region is, and add that to the
15752 15762 * 'nl_llpp' locklist in increasing order (by l_start).
15753 15763 */
15754 15764 static void
15755 15765 nfs4_add_lock_range(flock64_t *lost_flp, flock64_t *local_flp,
15756 15766 locklist_t **nl_llpp, vnode_t *vp)
15757 15767 {
15758 15768 locklist_t *intersect_llp, *tmp_fllp, *cur_fllp;
15759 15769 off64_t lost_flp_end, local_flp_end, len, start;
15760 15770
15761 15771 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range:"));
15762 15772
15763 15773 if (!locks_intersect(lost_flp, local_flp))
15764 15774 return;
15765 15775
15766 15776 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range: "
15767 15777 "locks intersect"));
15768 15778
15769 15779 lost_flp_end = lock_to_end(lost_flp);
15770 15780 local_flp_end = lock_to_end(local_flp);
15771 15781
15772 15782 /* Find the starting point of the intersecting region */
15773 15783 if (local_flp->l_start > lost_flp->l_start)
15774 15784 start = local_flp->l_start;
15775 15785 else
15776 15786 start = lost_flp->l_start;
15777 15787
15778 15788 /* Find the lenght of the intersecting region */
15779 15789 if (lost_flp_end < local_flp_end)
15780 15790 len = end_to_len(start, lost_flp_end);
15781 15791 else
15782 15792 len = end_to_len(start, local_flp_end);
15783 15793
15784 15794 /*
15785 15795 * Prepare the flock structure for the intersection found and insert
15786 15796 * it into the new list in increasing l_start order. This list contains
15787 15797 * intersections of locks registered by the client with the local host
15788 15798 * and the lost lock.
15789 15799 * The lock type of this lock is the same as that of the local_flp.
15790 15800 */
15791 15801 intersect_llp = (locklist_t *)kmem_alloc(sizeof (locklist_t), KM_SLEEP);
15792 15802 intersect_llp->ll_flock.l_start = start;
15793 15803 intersect_llp->ll_flock.l_len = len;
15794 15804 intersect_llp->ll_flock.l_type = local_flp->l_type;
15795 15805 intersect_llp->ll_flock.l_pid = local_flp->l_pid;
15796 15806 intersect_llp->ll_flock.l_sysid = local_flp->l_sysid;
15797 15807 intersect_llp->ll_flock.l_whence = 0; /* aka SEEK_SET */
15798 15808 intersect_llp->ll_vp = vp;
15799 15809
15800 15810 tmp_fllp = *nl_llpp;
15801 15811 cur_fllp = NULL;
15802 15812 while (tmp_fllp != NULL && tmp_fllp->ll_flock.l_start <
15803 15813 intersect_llp->ll_flock.l_start) {
15804 15814 cur_fllp = tmp_fllp;
15805 15815 tmp_fllp = tmp_fllp->ll_next;
15806 15816 }
15807 15817 if (cur_fllp == NULL) {
15808 15818 /* first on the list */
15809 15819 intersect_llp->ll_next = *nl_llpp;
15810 15820 *nl_llpp = intersect_llp;
15811 15821 } else {
15812 15822 intersect_llp->ll_next = cur_fllp->ll_next;
15813 15823 cur_fllp->ll_next = intersect_llp;
15814 15824 }
15815 15825
15816 15826 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range: "
15817 15827 "created lock region: start %"PRIx64" end %"PRIx64" : %s\n",
15818 15828 intersect_llp->ll_flock.l_start,
15819 15829 intersect_llp->ll_flock.l_start + intersect_llp->ll_flock.l_len,
15820 15830 intersect_llp->ll_flock.l_type == F_RDLCK ? "READ" : "WRITE"));
15821 15831 }
15822 15832
15823 15833 /*
15824 15834 * Our local locking current state is potentially different than
15825 15835 * what the NFSv4 server thinks we have due to a lost lock that was
15826 15836 * resent and then received. We need to reset our "NFSv4" locking
15827 15837 * state to match the current local locking state for this pid since
15828 15838 * that is what the user/application sees as what the world is.
15829 15839 *
15830 15840 * We cannot afford to drop the open/lock seqid sync since then we can
15831 15841 * get confused about what the current local locking state "is" versus
15832 15842 * "was".
15833 15843 *
15834 15844 * If we are unable to fix up the locks, we send SIGLOST to the affected
15835 15845 * process. This is not done if the filesystem has been forcibly
15836 15846 * unmounted, in case the process has already exited and a new process
15837 15847 * exists with the same pid.
15838 15848 */
15839 15849 static void
15840 15850 nfs4_reinstitute_local_lock_state(vnode_t *vp, flock64_t *lost_flp, cred_t *cr,
15841 15851 nfs4_lock_owner_t *lop)
15842 15852 {
15843 15853 locklist_t *locks, *llp, *ri_llp, *tmp_llp;
15844 15854 mntinfo4_t *mi = VTOMI4(vp);
15845 15855 const int cmd = F_SETLK;
15846 15856 off64_t cur_start, llp_ll_flock_end, lost_flp_end;
15847 15857 flock64_t ul_fl;
15848 15858
15849 15859 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
15850 15860 "nfs4_reinstitute_local_lock_state"));
15851 15861
15852 15862 /*
15853 15863 * Find active locks for this vp from the local locking code.
15854 15864 * Scan through this list and find out the locks that intersect with
15855 15865 * the lost lock. Once we find the lock that intersects, add the
15856 15866 * intersection area as a new lock to a new list "ri_llp". The lock
15857 15867 * type of the intersection region lock added to ri_llp is the same
15858 15868 * as that found in the active lock list, "list". The intersecting
15859 15869 * region locks are added to ri_llp in increasing l_start order.
15860 15870 */
15861 15871 ASSERT(nfs_zone() == mi->mi_zone);
15862 15872
15863 15873 locks = flk_active_locks_for_vp(vp);
15864 15874 ri_llp = NULL;
15865 15875
15866 15876 for (llp = locks; llp != NULL; llp = llp->ll_next) {
15867 15877 ASSERT(llp->ll_vp == vp);
15868 15878 /*
15869 15879 * Pick locks that belong to this pid/lockowner
15870 15880 */
15871 15881 if (llp->ll_flock.l_pid != lost_flp->l_pid)
15872 15882 continue;
15873 15883
15874 15884 nfs4_add_lock_range(lost_flp, &llp->ll_flock, &ri_llp, vp);
15875 15885 }
15876 15886
15877 15887 /*
15878 15888 * Now we have the list of intersections with the lost lock. These are
15879 15889 * the locks that were/are active before the server replied to the
15880 15890 * last/lost lock. Issue these locks to the server here. Playing these
15881 15891 * locks to the server will re-establish aur current local locking state
15882 15892 * with the v4 server.
15883 15893 * If we get an error, send SIGLOST to the application for that lock.
15884 15894 */
15885 15895
15886 15896 for (llp = ri_llp; llp != NULL; llp = llp->ll_next) {
15887 15897 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
15888 15898 "nfs4_reinstitute_local_lock_state: need to issue "
15889 15899 "flock: [%"PRIx64" - %"PRIx64"] : %s",
15890 15900 llp->ll_flock.l_start,
15891 15901 llp->ll_flock.l_start + llp->ll_flock.l_len,
15892 15902 llp->ll_flock.l_type == F_RDLCK ? "READ" :
15893 15903 llp->ll_flock.l_type == F_WRLCK ? "WRITE" : "INVALID"));
15894 15904 /*
15895 15905 * No need to relock what we already have
15896 15906 */
15897 15907 if (llp->ll_flock.l_type == lost_flp->l_type)
15898 15908 continue;
15899 15909
15900 15910 push_reinstate(vp, cmd, &llp->ll_flock, cr, lop);
15901 15911 }
15902 15912
15903 15913 /*
15904 15914 * Now keeping the start of the lost lock as our reference parse the
15905 15915 * newly created ri_llp locklist to find the ranges that we have locked
15906 15916 * with the v4 server but not in the current local locking. We need
15907 15917 * to unlock these ranges.
15908 15918 * These ranges can also be reffered to as those ranges, where the lost
15909 15919 * lock does not overlap with the locks in the ri_llp but are locked
15910 15920 * since the server replied to the lost lock.
15911 15921 */
15912 15922 cur_start = lost_flp->l_start;
15913 15923 lost_flp_end = lock_to_end(lost_flp);
15914 15924
15915 15925 ul_fl.l_type = F_UNLCK;
15916 15926 ul_fl.l_whence = 0; /* aka SEEK_SET */
15917 15927 ul_fl.l_sysid = lost_flp->l_sysid;
15918 15928 ul_fl.l_pid = lost_flp->l_pid;
15919 15929
15920 15930 for (llp = ri_llp; llp != NULL; llp = llp->ll_next) {
15921 15931 llp_ll_flock_end = lock_to_end(&llp->ll_flock);
15922 15932
15923 15933 if (llp->ll_flock.l_start <= cur_start) {
15924 15934 cur_start = start_check(llp_ll_flock_end);
15925 15935 continue;
15926 15936 }
15927 15937 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
15928 15938 "nfs4_reinstitute_local_lock_state: "
15929 15939 "UNLOCK [%"PRIx64" - %"PRIx64"]",
15930 15940 cur_start, llp->ll_flock.l_start));
15931 15941
15932 15942 ul_fl.l_start = cur_start;
15933 15943 ul_fl.l_len = end_to_len(cur_start,
15934 15944 (llp->ll_flock.l_start - 1));
15935 15945
15936 15946 push_reinstate(vp, cmd, &ul_fl, cr, lop);
15937 15947 cur_start = start_check(llp_ll_flock_end);
15938 15948 }
15939 15949
15940 15950 /*
15941 15951 * In the case where the lost lock ends after all intersecting locks,
15942 15952 * unlock the last part of the lost lock range.
15943 15953 */
15944 15954 if (cur_start != start_check(lost_flp_end)) {
15945 15955 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
15946 15956 "nfs4_reinstitute_local_lock_state: UNLOCK end of the "
15947 15957 "lost lock region [%"PRIx64" - %"PRIx64"]",
15948 15958 cur_start, lost_flp->l_start + lost_flp->l_len));
15949 15959
15950 15960 ul_fl.l_start = cur_start;
15951 15961 /*
15952 15962 * Is it an to-EOF lock? if so unlock till the end
15953 15963 */
15954 15964 if (lost_flp->l_len == 0)
15955 15965 ul_fl.l_len = 0;
15956 15966 else
15957 15967 ul_fl.l_len = start_check(lost_flp_end) - cur_start;
15958 15968
15959 15969 push_reinstate(vp, cmd, &ul_fl, cr, lop);
15960 15970 }
15961 15971
15962 15972 if (locks != NULL)
15963 15973 flk_free_locklist(locks);
15964 15974
15965 15975 /* Free up our newly created locklist */
15966 15976 for (llp = ri_llp; llp != NULL; ) {
15967 15977 tmp_llp = llp->ll_next;
15968 15978 kmem_free(llp, sizeof (locklist_t));
15969 15979 llp = tmp_llp;
15970 15980 }
15971 15981
15972 15982 /*
15973 15983 * Now return back to the original calling nfs4frlock()
15974 15984 * and let us naturally drop our seqid syncs.
15975 15985 */
15976 15986 }
15977 15987
15978 15988 /*
15979 15989 * Create a lost state record for the given lock reinstantiation request
15980 15990 * and push it onto the lost state queue.
15981 15991 */
15982 15992 static void
15983 15993 push_reinstate(vnode_t *vp, int cmd, flock64_t *flk, cred_t *cr,
15984 15994 nfs4_lock_owner_t *lop)
15985 15995 {
15986 15996 nfs4_lost_rqst_t req;
15987 15997 nfs_lock_type4 locktype;
15988 15998 nfs4_error_t e = { EINTR, NFS4_OK, RPC_SUCCESS };
15989 15999
15990 16000 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
15991 16001
15992 16002 locktype = flk_to_locktype(cmd, flk->l_type);
15993 16003 nfs4frlock_save_lost_rqst(NFS4_LCK_CTYPE_REINSTATE, EINTR, locktype,
15994 16004 NULL, NULL, lop, flk, &req, cr, vp);
15995 16005 (void) nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL,
15996 16006 (req.lr_op == OP_LOCK || req.lr_op == OP_LOCKU) ?
15997 16007 &req : NULL, flk->l_type == F_UNLCK ? OP_LOCKU : OP_LOCK,
15998 16008 NULL, NULL, NULL);
15999 16009 }
↓ open down ↓ |
13408 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX