Print this page
patch lower-case-segops
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 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
23 23 * Use is subject to license terms.
24 24 */
25 25 /*
26 26 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
27 27 */
28 28
29 29 /*
30 30 * Copyright 1983,1984,1985,1986,1987,1988,1989 AT&T.
31 31 * All Rights Reserved
32 32 */
33 33
34 34 /*
35 35 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
36 36 */
37 37
38 38 #include <sys/param.h>
39 39 #include <sys/types.h>
40 40 #include <sys/systm.h>
41 41 #include <sys/cred.h>
42 42 #include <sys/time.h>
43 43 #include <sys/vnode.h>
44 44 #include <sys/vfs.h>
45 45 #include <sys/vfs_opreg.h>
46 46 #include <sys/file.h>
47 47 #include <sys/filio.h>
48 48 #include <sys/uio.h>
49 49 #include <sys/buf.h>
50 50 #include <sys/mman.h>
51 51 #include <sys/pathname.h>
52 52 #include <sys/dirent.h>
53 53 #include <sys/debug.h>
54 54 #include <sys/vmsystm.h>
55 55 #include <sys/fcntl.h>
56 56 #include <sys/flock.h>
57 57 #include <sys/swap.h>
58 58 #include <sys/errno.h>
59 59 #include <sys/strsubr.h>
60 60 #include <sys/sysmacros.h>
61 61 #include <sys/kmem.h>
62 62 #include <sys/cmn_err.h>
63 63 #include <sys/pathconf.h>
64 64 #include <sys/utsname.h>
65 65 #include <sys/dnlc.h>
66 66 #include <sys/acl.h>
67 67 #include <sys/systeminfo.h>
68 68 #include <sys/policy.h>
69 69 #include <sys/sdt.h>
70 70 #include <sys/list.h>
71 71 #include <sys/stat.h>
72 72 #include <sys/zone.h>
73 73
74 74 #include <rpc/types.h>
75 75 #include <rpc/auth.h>
76 76 #include <rpc/clnt.h>
77 77
78 78 #include <nfs/nfs.h>
79 79 #include <nfs/nfs_clnt.h>
80 80 #include <nfs/nfs_acl.h>
81 81 #include <nfs/lm.h>
82 82 #include <nfs/nfs4.h>
83 83 #include <nfs/nfs4_kprot.h>
84 84 #include <nfs/rnode4.h>
85 85 #include <nfs/nfs4_clnt.h>
86 86
87 87 #include <vm/hat.h>
88 88 #include <vm/as.h>
89 89 #include <vm/page.h>
90 90 #include <vm/pvn.h>
91 91 #include <vm/seg.h>
92 92 #include <vm/seg_map.h>
93 93 #include <vm/seg_kpm.h>
94 94 #include <vm/seg_vn.h>
95 95
96 96 #include <fs/fs_subr.h>
97 97
98 98 #include <sys/ddi.h>
99 99 #include <sys/int_fmtio.h>
100 100 #include <sys/fs/autofs.h>
101 101
102 102 typedef struct {
103 103 nfs4_ga_res_t *di_garp;
104 104 cred_t *di_cred;
105 105 hrtime_t di_time_call;
106 106 } dirattr_info_t;
107 107
108 108 typedef enum nfs4_acl_op {
109 109 NFS4_ACL_GET,
110 110 NFS4_ACL_SET
111 111 } nfs4_acl_op_t;
112 112
113 113 static struct lm_sysid *nfs4_find_sysid(mntinfo4_t *mi);
114 114
115 115 static void nfs4_update_dircaches(change_info4 *, vnode_t *, vnode_t *,
116 116 char *, dirattr_info_t *);
117 117
118 118 static void nfs4close_otw(rnode4_t *, cred_t *, nfs4_open_owner_t *,
119 119 nfs4_open_stream_t *, int *, int *, nfs4_close_type_t,
120 120 nfs4_error_t *, int *);
121 121 static int nfs4_rdwrlbn(vnode_t *, page_t *, u_offset_t, size_t, int,
122 122 cred_t *);
123 123 static int nfs4write(vnode_t *, caddr_t, u_offset_t, int, cred_t *,
124 124 stable_how4 *);
125 125 static int nfs4read(vnode_t *, caddr_t, offset_t, int, size_t *,
126 126 cred_t *, bool_t, struct uio *);
127 127 static int nfs4setattr(vnode_t *, struct vattr *, int, cred_t *,
128 128 vsecattr_t *);
129 129 static int nfs4openattr(vnode_t *, vnode_t **, int, cred_t *);
130 130 static int nfs4lookup(vnode_t *, char *, vnode_t **, cred_t *, int);
131 131 static int nfs4lookup_xattr(vnode_t *, char *, vnode_t **, int, cred_t *);
132 132 static int nfs4lookupvalidate_otw(vnode_t *, char *, vnode_t **, cred_t *);
133 133 static int nfs4lookupnew_otw(vnode_t *, char *, vnode_t **, cred_t *);
134 134 static int nfs4mknod(vnode_t *, char *, struct vattr *, enum vcexcl,
135 135 int, vnode_t **, cred_t *);
136 136 static int nfs4open_otw(vnode_t *, char *, struct vattr *, vnode_t **,
137 137 cred_t *, int, int, enum createmode4, int);
138 138 static int nfs4rename(vnode_t *, char *, vnode_t *, char *, cred_t *,
139 139 caller_context_t *);
140 140 static int nfs4rename_persistent_fh(vnode_t *, char *, vnode_t *,
141 141 vnode_t *, char *, cred_t *, nfsstat4 *);
142 142 static int nfs4rename_volatile_fh(vnode_t *, char *, vnode_t *,
143 143 vnode_t *, char *, cred_t *, nfsstat4 *);
144 144 static int do_nfs4readdir(vnode_t *, rddir4_cache *, cred_t *);
145 145 static void nfs4readdir(vnode_t *, rddir4_cache *, cred_t *);
146 146 static int nfs4_bio(struct buf *, stable_how4 *, cred_t *, bool_t);
147 147 static int nfs4_getapage(vnode_t *, u_offset_t, size_t, uint_t *,
148 148 page_t *[], size_t, struct seg *, caddr_t,
149 149 enum seg_rw, cred_t *);
150 150 static void nfs4_readahead(vnode_t *, u_offset_t, caddr_t, struct seg *,
151 151 cred_t *);
152 152 static int nfs4_sync_putapage(vnode_t *, page_t *, u_offset_t, size_t,
153 153 int, cred_t *);
154 154 static int nfs4_sync_pageio(vnode_t *, page_t *, u_offset_t, size_t,
155 155 int, cred_t *);
156 156 static int nfs4_commit(vnode_t *, offset4, count4, cred_t *);
157 157 static void nfs4_set_mod(vnode_t *);
158 158 static void nfs4_get_commit(vnode_t *);
159 159 static void nfs4_get_commit_range(vnode_t *, u_offset_t, size_t);
160 160 static int nfs4_putpage_commit(vnode_t *, offset_t, size_t, cred_t *);
161 161 static int nfs4_commit_vp(vnode_t *, u_offset_t, size_t, cred_t *, int);
162 162 static int nfs4_sync_commit(vnode_t *, page_t *, offset3, count3,
163 163 cred_t *);
164 164 static void do_nfs4_async_commit(vnode_t *, page_t *, offset3, count3,
165 165 cred_t *);
166 166 static int nfs4_update_attrcache(nfsstat4, nfs4_ga_res_t *,
167 167 hrtime_t, vnode_t *, cred_t *);
168 168 static int nfs4_open_non_reg_file(vnode_t **, int, cred_t *);
169 169 static int nfs4_safelock(vnode_t *, const struct flock64 *, cred_t *);
170 170 static void nfs4_register_lock_locally(vnode_t *, struct flock64 *, int,
171 171 u_offset_t);
172 172 static int nfs4_lockrelease(vnode_t *, int, offset_t, cred_t *);
173 173 static int nfs4_block_and_wait(clock_t *, rnode4_t *);
174 174 static cred_t *state_to_cred(nfs4_open_stream_t *);
175 175 static void denied_to_flk(LOCK4denied *, flock64_t *, LOCKT4args *);
176 176 static pid_t lo_to_pid(lock_owner4 *);
177 177 static void nfs4_reinstitute_local_lock_state(vnode_t *, flock64_t *,
178 178 cred_t *, nfs4_lock_owner_t *);
179 179 static void push_reinstate(vnode_t *, int, flock64_t *, cred_t *,
180 180 nfs4_lock_owner_t *);
181 181 static int open_and_get_osp(vnode_t *, cred_t *, nfs4_open_stream_t **);
182 182 static void nfs4_delmap_callback(struct as *, void *, uint_t);
183 183 static void nfs4_free_delmapcall(nfs4_delmapcall_t *);
184 184 static nfs4_delmapcall_t *nfs4_init_delmapcall();
185 185 static int nfs4_find_and_delete_delmapcall(rnode4_t *, int *);
186 186 static int nfs4_is_acl_mask_valid(uint_t, nfs4_acl_op_t);
187 187 static int nfs4_create_getsecattr_return(vsecattr_t *, vsecattr_t *,
188 188 uid_t, gid_t, int);
189 189
190 190 /*
191 191 * Routines that implement the setting of v4 args for the misc. ops
192 192 */
193 193 static void nfs4args_lock_free(nfs_argop4 *);
194 194 static void nfs4args_lockt_free(nfs_argop4 *);
195 195 static void nfs4args_setattr(nfs_argop4 *, vattr_t *, vsecattr_t *,
196 196 int, rnode4_t *, cred_t *, bitmap4, int *,
197 197 nfs4_stateid_types_t *);
198 198 static void nfs4args_setattr_free(nfs_argop4 *);
199 199 static int nfs4args_verify(nfs_argop4 *, vattr_t *, enum nfs_opnum4,
200 200 bitmap4);
201 201 static void nfs4args_verify_free(nfs_argop4 *);
202 202 static void nfs4args_write(nfs_argop4 *, stable_how4, rnode4_t *, cred_t *,
203 203 WRITE4args **, nfs4_stateid_types_t *);
204 204
205 205 /*
206 206 * These are the vnode ops functions that implement the vnode interface to
207 207 * the networked file system. See more comments below at nfs4_vnodeops.
208 208 */
209 209 static int nfs4_open(vnode_t **, int, cred_t *, caller_context_t *);
210 210 static int nfs4_close(vnode_t *, int, int, offset_t, cred_t *,
211 211 caller_context_t *);
212 212 static int nfs4_read(vnode_t *, struct uio *, int, cred_t *,
213 213 caller_context_t *);
214 214 static int nfs4_write(vnode_t *, struct uio *, int, cred_t *,
215 215 caller_context_t *);
216 216 static int nfs4_ioctl(vnode_t *, int, intptr_t, int, cred_t *, int *,
217 217 caller_context_t *);
218 218 static int nfs4_setattr(vnode_t *, struct vattr *, int, cred_t *,
219 219 caller_context_t *);
220 220 static int nfs4_access(vnode_t *, int, int, cred_t *, caller_context_t *);
221 221 static int nfs4_readlink(vnode_t *, struct uio *, cred_t *,
222 222 caller_context_t *);
223 223 static int nfs4_fsync(vnode_t *, int, cred_t *, caller_context_t *);
224 224 static int nfs4_create(vnode_t *, char *, struct vattr *, enum vcexcl,
225 225 int, vnode_t **, cred_t *, int, caller_context_t *,
226 226 vsecattr_t *);
227 227 static int nfs4_remove(vnode_t *, char *, cred_t *, caller_context_t *,
228 228 int);
229 229 static int nfs4_link(vnode_t *, vnode_t *, char *, cred_t *,
230 230 caller_context_t *, int);
231 231 static int nfs4_rename(vnode_t *, char *, vnode_t *, char *, cred_t *,
232 232 caller_context_t *, int);
233 233 static int nfs4_mkdir(vnode_t *, char *, struct vattr *, vnode_t **,
234 234 cred_t *, caller_context_t *, int, vsecattr_t *);
235 235 static int nfs4_rmdir(vnode_t *, char *, vnode_t *, cred_t *,
236 236 caller_context_t *, int);
237 237 static int nfs4_symlink(vnode_t *, char *, struct vattr *, char *,
238 238 cred_t *, caller_context_t *, int);
239 239 static int nfs4_readdir(vnode_t *, struct uio *, cred_t *, int *,
240 240 caller_context_t *, int);
241 241 static int nfs4_seek(vnode_t *, offset_t, offset_t *, caller_context_t *);
242 242 static int nfs4_getpage(vnode_t *, offset_t, size_t, uint_t *,
243 243 page_t *[], size_t, struct seg *, caddr_t,
244 244 enum seg_rw, cred_t *, caller_context_t *);
245 245 static int nfs4_putpage(vnode_t *, offset_t, size_t, int, cred_t *,
246 246 caller_context_t *);
247 247 static int nfs4_map(vnode_t *, offset_t, struct as *, caddr_t *, size_t,
248 248 uchar_t, uchar_t, uint_t, cred_t *, caller_context_t *);
249 249 static int nfs4_addmap(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_cmp(vnode_t *, vnode_t *, caller_context_t *);
252 252 static int nfs4_frlock(vnode_t *, int, struct flock64 *, int, offset_t,
253 253 struct flk_callback *, cred_t *, caller_context_t *);
254 254 static int nfs4_space(vnode_t *, int, struct flock64 *, int, offset_t,
255 255 cred_t *, caller_context_t *);
256 256 static int nfs4_delmap(vnode_t *, offset_t, struct as *, caddr_t, size_t,
257 257 uint_t, uint_t, uint_t, cred_t *, caller_context_t *);
258 258 static int nfs4_pageio(vnode_t *, page_t *, u_offset_t, size_t, int,
259 259 cred_t *, caller_context_t *);
260 260 static void nfs4_dispose(vnode_t *, page_t *, int, int, cred_t *,
261 261 caller_context_t *);
262 262 static int nfs4_setsecattr(vnode_t *, vsecattr_t *, int, cred_t *,
263 263 caller_context_t *);
264 264 /*
265 265 * These vnode ops are required to be called from outside this source file,
266 266 * e.g. by ephemeral mount stub vnode ops, and so may not be declared
267 267 * as static.
268 268 */
269 269 int nfs4_getattr(vnode_t *, struct vattr *, int, cred_t *,
270 270 caller_context_t *);
271 271 void nfs4_inactive(vnode_t *, cred_t *, caller_context_t *);
272 272 int nfs4_lookup(vnode_t *, char *, vnode_t **,
273 273 struct pathname *, int, vnode_t *, cred_t *,
274 274 caller_context_t *, int *, pathname_t *);
275 275 int nfs4_fid(vnode_t *, fid_t *, caller_context_t *);
276 276 int nfs4_rwlock(vnode_t *, int, caller_context_t *);
277 277 void nfs4_rwunlock(vnode_t *, int, caller_context_t *);
278 278 int nfs4_realvp(vnode_t *, vnode_t **, caller_context_t *);
279 279 int nfs4_pathconf(vnode_t *, int, ulong_t *, cred_t *,
280 280 caller_context_t *);
281 281 int nfs4_getsecattr(vnode_t *, vsecattr_t *, int, cred_t *,
282 282 caller_context_t *);
283 283 int nfs4_shrlock(vnode_t *, int, struct shrlock *, int, cred_t *,
284 284 caller_context_t *);
285 285
286 286 /*
287 287 * Used for nfs4_commit_vp() to indicate if we should
288 288 * wait on pending writes.
289 289 */
290 290 #define NFS4_WRITE_NOWAIT 0
291 291 #define NFS4_WRITE_WAIT 1
292 292
293 293 #define NFS4_BASE_WAIT_TIME 1 /* 1 second */
294 294
295 295 /*
296 296 * Error flags used to pass information about certain special errors
297 297 * which need to be handled specially.
298 298 */
299 299 #define NFS_EOF -98
300 300 #define NFS_VERF_MISMATCH -97
301 301
302 302 /*
303 303 * Flags used to differentiate between which operation drove the
304 304 * potential CLOSE OTW. (see nfs4_close_otw_if_necessary)
305 305 */
306 306 #define NFS4_CLOSE_OP 0x1
307 307 #define NFS4_DELMAP_OP 0x2
308 308 #define NFS4_INACTIVE_OP 0x3
309 309
310 310 #define ISVDEV(t) ((t == VBLK) || (t == VCHR) || (t == VFIFO))
311 311
312 312 /* ALIGN64 aligns the given buffer and adjust buffer size to 64 bit */
313 313 #define ALIGN64(x, ptr, sz) \
314 314 x = ((uintptr_t)(ptr)) & (sizeof (uint64_t) - 1); \
315 315 if (x) { \
316 316 x = sizeof (uint64_t) - (x); \
317 317 sz -= (x); \
318 318 ptr += (x); \
319 319 }
320 320
321 321 #ifdef DEBUG
322 322 int nfs4_client_attr_debug = 0;
323 323 int nfs4_client_state_debug = 0;
324 324 int nfs4_client_shadow_debug = 0;
325 325 int nfs4_client_lock_debug = 0;
326 326 int nfs4_seqid_sync = 0;
327 327 int nfs4_client_map_debug = 0;
328 328 static int nfs4_pageio_debug = 0;
329 329 int nfs4_client_inactive_debug = 0;
330 330 int nfs4_client_recov_debug = 0;
331 331 int nfs4_client_failover_debug = 0;
332 332 int nfs4_client_call_debug = 0;
333 333 int nfs4_client_lookup_debug = 0;
334 334 int nfs4_client_zone_debug = 0;
335 335 int nfs4_lost_rqst_debug = 0;
336 336 int nfs4_rdattrerr_debug = 0;
337 337 int nfs4_open_stream_debug = 0;
338 338
339 339 int nfs4read_error_inject;
340 340
341 341 static int nfs4_create_misses = 0;
342 342
343 343 static int nfs4_readdir_cache_shorts = 0;
344 344 static int nfs4_readdir_readahead = 0;
345 345
346 346 static int nfs4_bio_do_stop = 0;
347 347
348 348 static int nfs4_lostpage = 0; /* number of times we lost original page */
349 349
350 350 int nfs4_mmap_debug = 0;
351 351
352 352 static int nfs4_pathconf_cache_hits = 0;
353 353 static int nfs4_pathconf_cache_misses = 0;
354 354
355 355 int nfs4close_all_cnt;
356 356 int nfs4close_one_debug = 0;
357 357 int nfs4close_notw_debug = 0;
358 358
359 359 int denied_to_flk_debug = 0;
360 360 void *lockt_denied_debug;
361 361
362 362 #endif
363 363
364 364 /*
365 365 * How long to wait before trying again if OPEN_CONFIRM gets ETIMEDOUT
366 366 * or NFS4ERR_RESOURCE.
367 367 */
368 368 static int confirm_retry_sec = 30;
369 369
370 370 static int nfs4_lookup_neg_cache = 1;
371 371
372 372 /*
373 373 * number of pages to read ahead
374 374 * optimized for 100 base-T.
375 375 */
376 376 static int nfs4_nra = 4;
377 377
378 378 static int nfs4_do_symlink_cache = 1;
379 379
380 380 static int nfs4_pathconf_disable_cache = 0;
381 381
382 382 /*
383 383 * These are the vnode ops routines which implement the vnode interface to
384 384 * the networked file system. These routines just take their parameters,
385 385 * make them look networkish by putting the right info into interface structs,
386 386 * and then calling the appropriate remote routine(s) to do the work.
387 387 *
388 388 * Note on directory name lookup cacheing: If we detect a stale fhandle,
389 389 * we purge the directory cache relative to that vnode. This way, the
390 390 * user won't get burned by the cache repeatedly. See <nfs/rnode4.h> for
391 391 * more details on rnode locking.
392 392 */
393 393
394 394 struct vnodeops *nfs4_vnodeops;
395 395
396 396 const fs_operation_def_t nfs4_vnodeops_template[] = {
397 397 VOPNAME_OPEN, { .vop_open = nfs4_open },
398 398 VOPNAME_CLOSE, { .vop_close = nfs4_close },
399 399 VOPNAME_READ, { .vop_read = nfs4_read },
400 400 VOPNAME_WRITE, { .vop_write = nfs4_write },
401 401 VOPNAME_IOCTL, { .vop_ioctl = nfs4_ioctl },
402 402 VOPNAME_GETATTR, { .vop_getattr = nfs4_getattr },
403 403 VOPNAME_SETATTR, { .vop_setattr = nfs4_setattr },
404 404 VOPNAME_ACCESS, { .vop_access = nfs4_access },
405 405 VOPNAME_LOOKUP, { .vop_lookup = nfs4_lookup },
406 406 VOPNAME_CREATE, { .vop_create = nfs4_create },
407 407 VOPNAME_REMOVE, { .vop_remove = nfs4_remove },
408 408 VOPNAME_LINK, { .vop_link = nfs4_link },
409 409 VOPNAME_RENAME, { .vop_rename = nfs4_rename },
410 410 VOPNAME_MKDIR, { .vop_mkdir = nfs4_mkdir },
411 411 VOPNAME_RMDIR, { .vop_rmdir = nfs4_rmdir },
412 412 VOPNAME_READDIR, { .vop_readdir = nfs4_readdir },
413 413 VOPNAME_SYMLINK, { .vop_symlink = nfs4_symlink },
414 414 VOPNAME_READLINK, { .vop_readlink = nfs4_readlink },
415 415 VOPNAME_FSYNC, { .vop_fsync = nfs4_fsync },
416 416 VOPNAME_INACTIVE, { .vop_inactive = nfs4_inactive },
417 417 VOPNAME_FID, { .vop_fid = nfs4_fid },
418 418 VOPNAME_RWLOCK, { .vop_rwlock = nfs4_rwlock },
419 419 VOPNAME_RWUNLOCK, { .vop_rwunlock = nfs4_rwunlock },
420 420 VOPNAME_SEEK, { .vop_seek = nfs4_seek },
421 421 VOPNAME_FRLOCK, { .vop_frlock = nfs4_frlock },
422 422 VOPNAME_SPACE, { .vop_space = nfs4_space },
423 423 VOPNAME_REALVP, { .vop_realvp = nfs4_realvp },
424 424 VOPNAME_GETPAGE, { .vop_getpage = nfs4_getpage },
425 425 VOPNAME_PUTPAGE, { .vop_putpage = nfs4_putpage },
426 426 VOPNAME_MAP, { .vop_map = nfs4_map },
427 427 VOPNAME_ADDMAP, { .vop_addmap = nfs4_addmap },
428 428 VOPNAME_DELMAP, { .vop_delmap = nfs4_delmap },
429 429 /* no separate nfs4_dump */
430 430 VOPNAME_DUMP, { .vop_dump = nfs_dump },
431 431 VOPNAME_PATHCONF, { .vop_pathconf = nfs4_pathconf },
432 432 VOPNAME_PAGEIO, { .vop_pageio = nfs4_pageio },
433 433 VOPNAME_DISPOSE, { .vop_dispose = nfs4_dispose },
434 434 VOPNAME_SETSECATTR, { .vop_setsecattr = nfs4_setsecattr },
435 435 VOPNAME_GETSECATTR, { .vop_getsecattr = nfs4_getsecattr },
436 436 VOPNAME_SHRLOCK, { .vop_shrlock = nfs4_shrlock },
437 437 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
438 438 NULL, NULL
439 439 };
440 440
441 441 /*
442 442 * The following are subroutines and definitions to set args or get res
443 443 * for the different nfsv4 ops
444 444 */
445 445
446 446 void
447 447 nfs4args_lookup_free(nfs_argop4 *argop, int arglen)
448 448 {
449 449 int i;
450 450
451 451 for (i = 0; i < arglen; i++) {
452 452 if (argop[i].argop == OP_LOOKUP) {
453 453 kmem_free(
454 454 argop[i].nfs_argop4_u.oplookup.
455 455 objname.utf8string_val,
456 456 argop[i].nfs_argop4_u.oplookup.
457 457 objname.utf8string_len);
458 458 }
459 459 }
460 460 }
461 461
462 462 static void
463 463 nfs4args_lock_free(nfs_argop4 *argop)
464 464 {
465 465 locker4 *locker = &argop->nfs_argop4_u.oplock.locker;
466 466
467 467 if (locker->new_lock_owner == TRUE) {
468 468 open_to_lock_owner4 *open_owner;
469 469
470 470 open_owner = &locker->locker4_u.open_owner;
471 471 if (open_owner->lock_owner.owner_val != NULL) {
472 472 kmem_free(open_owner->lock_owner.owner_val,
473 473 open_owner->lock_owner.owner_len);
474 474 }
475 475 }
476 476 }
477 477
478 478 static void
479 479 nfs4args_lockt_free(nfs_argop4 *argop)
480 480 {
481 481 lock_owner4 *lowner = &argop->nfs_argop4_u.oplockt.owner;
482 482
483 483 if (lowner->owner_val != NULL) {
484 484 kmem_free(lowner->owner_val, lowner->owner_len);
485 485 }
486 486 }
487 487
488 488 static void
489 489 nfs4args_setattr(nfs_argop4 *argop, vattr_t *vap, vsecattr_t *vsap, int flags,
490 490 rnode4_t *rp, cred_t *cr, bitmap4 supp, int *error,
491 491 nfs4_stateid_types_t *sid_types)
492 492 {
493 493 fattr4 *attr = &argop->nfs_argop4_u.opsetattr.obj_attributes;
494 494 mntinfo4_t *mi;
495 495
496 496 argop->argop = OP_SETATTR;
497 497 /*
498 498 * The stateid is set to 0 if client is not modifying the size
499 499 * and otherwise to whatever nfs4_get_stateid() returns.
500 500 *
501 501 * XXX Note: nfs4_get_stateid() returns 0 if no lockowner and/or no
502 502 * state struct could be found for the process/file pair. We may
503 503 * want to change this in the future (by OPENing the file). See
504 504 * bug # 4474852.
505 505 */
506 506 if (vap->va_mask & AT_SIZE) {
507 507
508 508 ASSERT(rp != NULL);
509 509 mi = VTOMI4(RTOV4(rp));
510 510
511 511 argop->nfs_argop4_u.opsetattr.stateid =
512 512 nfs4_get_stateid(cr, rp, curproc->p_pidp->pid_id, mi,
513 513 OP_SETATTR, sid_types, FALSE);
514 514 } else {
515 515 bzero(&argop->nfs_argop4_u.opsetattr.stateid,
516 516 sizeof (stateid4));
517 517 }
518 518
519 519 *error = vattr_to_fattr4(vap, vsap, attr, flags, OP_SETATTR, supp);
520 520 if (*error)
521 521 bzero(attr, sizeof (*attr));
522 522 }
523 523
524 524 static void
525 525 nfs4args_setattr_free(nfs_argop4 *argop)
526 526 {
527 527 nfs4_fattr4_free(&argop->nfs_argop4_u.opsetattr.obj_attributes);
528 528 }
529 529
530 530 static int
531 531 nfs4args_verify(nfs_argop4 *argop, vattr_t *vap, enum nfs_opnum4 op,
532 532 bitmap4 supp)
533 533 {
534 534 fattr4 *attr;
535 535 int error = 0;
536 536
537 537 argop->argop = op;
538 538 switch (op) {
539 539 case OP_VERIFY:
540 540 attr = &argop->nfs_argop4_u.opverify.obj_attributes;
541 541 break;
542 542 case OP_NVERIFY:
543 543 attr = &argop->nfs_argop4_u.opnverify.obj_attributes;
544 544 break;
545 545 default:
546 546 return (EINVAL);
547 547 }
548 548 if (!error)
549 549 error = vattr_to_fattr4(vap, NULL, attr, 0, op, supp);
550 550 if (error)
551 551 bzero(attr, sizeof (*attr));
552 552 return (error);
553 553 }
554 554
555 555 static void
556 556 nfs4args_verify_free(nfs_argop4 *argop)
557 557 {
558 558 switch (argop->argop) {
559 559 case OP_VERIFY:
560 560 nfs4_fattr4_free(&argop->nfs_argop4_u.opverify.obj_attributes);
561 561 break;
562 562 case OP_NVERIFY:
563 563 nfs4_fattr4_free(&argop->nfs_argop4_u.opnverify.obj_attributes);
564 564 break;
565 565 default:
566 566 break;
567 567 }
568 568 }
569 569
570 570 static void
571 571 nfs4args_write(nfs_argop4 *argop, stable_how4 stable, rnode4_t *rp, cred_t *cr,
572 572 WRITE4args **wargs_pp, nfs4_stateid_types_t *sid_tp)
573 573 {
574 574 WRITE4args *wargs = &argop->nfs_argop4_u.opwrite;
575 575 mntinfo4_t *mi = VTOMI4(RTOV4(rp));
576 576
577 577 argop->argop = OP_WRITE;
578 578 wargs->stable = stable;
579 579 wargs->stateid = nfs4_get_w_stateid(cr, rp, curproc->p_pidp->pid_id,
580 580 mi, OP_WRITE, sid_tp);
581 581 wargs->mblk = NULL;
582 582 *wargs_pp = wargs;
583 583 }
584 584
585 585 void
586 586 nfs4args_copen_free(OPEN4cargs *open_args)
587 587 {
588 588 if (open_args->owner.owner_val) {
589 589 kmem_free(open_args->owner.owner_val,
590 590 open_args->owner.owner_len);
591 591 }
592 592 if ((open_args->opentype == OPEN4_CREATE) &&
593 593 (open_args->mode != EXCLUSIVE4)) {
594 594 nfs4_fattr4_free(&open_args->createhow4_u.createattrs);
595 595 }
596 596 }
597 597
598 598 /*
599 599 * XXX: This is referenced in modstubs.s
600 600 */
601 601 struct vnodeops *
602 602 nfs4_getvnodeops(void)
603 603 {
604 604 return (nfs4_vnodeops);
605 605 }
606 606
607 607 /*
608 608 * The OPEN operation opens a regular file.
609 609 */
610 610 /*ARGSUSED3*/
611 611 static int
612 612 nfs4_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
613 613 {
614 614 vnode_t *dvp = NULL;
615 615 rnode4_t *rp, *drp;
616 616 int error;
617 617 int just_been_created;
618 618 char fn[MAXNAMELEN];
619 619
620 620 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4_open: "));
621 621 if (nfs_zone() != VTOMI4(*vpp)->mi_zone)
622 622 return (EIO);
623 623 rp = VTOR4(*vpp);
624 624
625 625 /*
626 626 * Check to see if opening something besides a regular file;
627 627 * if so skip the OTW call
628 628 */
629 629 if ((*vpp)->v_type != VREG) {
630 630 error = nfs4_open_non_reg_file(vpp, flag, cr);
631 631 return (error);
632 632 }
633 633
634 634 /*
635 635 * XXX - would like a check right here to know if the file is
636 636 * executable or not, so as to skip OTW
637 637 */
638 638
639 639 if ((error = vtodv(*vpp, &dvp, cr, TRUE)) != 0)
640 640 return (error);
641 641
642 642 drp = VTOR4(dvp);
643 643 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp)))
644 644 return (EINTR);
645 645
646 646 if ((error = vtoname(*vpp, fn, MAXNAMELEN)) != 0) {
647 647 nfs_rw_exit(&drp->r_rwlock);
648 648 return (error);
649 649 }
650 650
651 651 /*
652 652 * See if this file has just been CREATEd.
653 653 * If so, clear the flag and update the dnlc, which was previously
654 654 * skipped in nfs4_create.
655 655 * XXX need better serilization on this.
656 656 * XXX move this into the nf4open_otw call, after we have
657 657 * XXX acquired the open owner seqid sync.
658 658 */
659 659 mutex_enter(&rp->r_statev4_lock);
660 660 if (rp->created_v4) {
661 661 rp->created_v4 = 0;
662 662 mutex_exit(&rp->r_statev4_lock);
663 663
664 664 dnlc_update(dvp, fn, *vpp);
665 665 /* This is needed so we don't bump the open ref count */
666 666 just_been_created = 1;
667 667 } else {
668 668 mutex_exit(&rp->r_statev4_lock);
669 669 just_been_created = 0;
670 670 }
671 671
672 672 /*
673 673 * If caller specified O_TRUNC/FTRUNC, then be sure to set
674 674 * FWRITE (to drive successful setattr(size=0) after open)
675 675 */
676 676 if (flag & FTRUNC)
677 677 flag |= FWRITE;
678 678
679 679 error = nfs4open_otw(dvp, fn, NULL, vpp, cr, 0, flag, 0,
680 680 just_been_created);
681 681
682 682 if (!error && !((*vpp)->v_flag & VROOT))
683 683 dnlc_update(dvp, fn, *vpp);
684 684
685 685 nfs_rw_exit(&drp->r_rwlock);
686 686
687 687 /* release the hold from vtodv */
688 688 VN_RELE(dvp);
689 689
690 690 /* exchange the shadow for the master vnode, if needed */
691 691
692 692 if (error == 0 && IS_SHADOW(*vpp, rp))
693 693 sv_exchange(vpp);
694 694
695 695 return (error);
696 696 }
697 697
698 698 /*
699 699 * See if there's a "lost open" request to be saved and recovered.
700 700 */
701 701 static void
702 702 nfs4open_save_lost_rqst(int error, nfs4_lost_rqst_t *lost_rqstp,
703 703 nfs4_open_owner_t *oop, cred_t *cr, vnode_t *vp,
704 704 vnode_t *dvp, OPEN4cargs *open_args)
705 705 {
706 706 vfs_t *vfsp;
707 707 char *srccfp;
708 708
709 709 vfsp = (dvp ? dvp->v_vfsp : vp->v_vfsp);
710 710
711 711 if (error != ETIMEDOUT && error != EINTR &&
712 712 !NFS4_FRC_UNMT_ERR(error, vfsp)) {
713 713 lost_rqstp->lr_op = 0;
714 714 return;
715 715 }
716 716
717 717 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
718 718 "nfs4open_save_lost_rqst: error %d", error));
719 719
720 720 lost_rqstp->lr_op = OP_OPEN;
721 721
722 722 /*
723 723 * The vp (if it is not NULL) and dvp are held and rele'd via
724 724 * the recovery code. See nfs4_save_lost_rqst.
725 725 */
726 726 lost_rqstp->lr_vp = vp;
727 727 lost_rqstp->lr_dvp = dvp;
728 728 lost_rqstp->lr_oop = oop;
729 729 lost_rqstp->lr_osp = NULL;
730 730 lost_rqstp->lr_lop = NULL;
731 731 lost_rqstp->lr_cr = cr;
732 732 lost_rqstp->lr_flk = NULL;
733 733 lost_rqstp->lr_oacc = open_args->share_access;
734 734 lost_rqstp->lr_odeny = open_args->share_deny;
735 735 lost_rqstp->lr_oclaim = open_args->claim;
736 736 if (open_args->claim == CLAIM_DELEGATE_CUR) {
737 737 lost_rqstp->lr_ostateid =
738 738 open_args->open_claim4_u.delegate_cur_info.delegate_stateid;
739 739 srccfp = open_args->open_claim4_u.delegate_cur_info.cfile;
740 740 } else {
741 741 srccfp = open_args->open_claim4_u.cfile;
742 742 }
743 743 lost_rqstp->lr_ofile.utf8string_len = 0;
744 744 lost_rqstp->lr_ofile.utf8string_val = NULL;
745 745 (void) str_to_utf8(srccfp, &lost_rqstp->lr_ofile);
746 746 lost_rqstp->lr_putfirst = FALSE;
747 747 }
748 748
749 749 struct nfs4_excl_time {
750 750 uint32 seconds;
751 751 uint32 nseconds;
752 752 };
753 753
754 754 /*
755 755 * The OPEN operation creates and/or opens a regular file
756 756 *
757 757 * ARGSUSED
758 758 */
759 759 static int
760 760 nfs4open_otw(vnode_t *dvp, char *file_name, struct vattr *in_va,
761 761 vnode_t **vpp, cred_t *cr, int create_flag, int open_flag,
762 762 enum createmode4 createmode, int file_just_been_created)
763 763 {
764 764 rnode4_t *rp;
765 765 rnode4_t *drp = VTOR4(dvp);
766 766 vnode_t *vp = NULL;
767 767 vnode_t *vpi = *vpp;
768 768 bool_t needrecov = FALSE;
769 769
770 770 int doqueue = 1;
771 771
772 772 COMPOUND4args_clnt args;
773 773 COMPOUND4res_clnt res;
774 774 nfs_argop4 *argop;
775 775 nfs_resop4 *resop;
776 776 int argoplist_size;
777 777 int idx_open, idx_fattr;
778 778
779 779 GETFH4res *gf_res = NULL;
780 780 OPEN4res *op_res = NULL;
781 781 nfs4_ga_res_t *garp;
782 782 fattr4 *attr = NULL;
783 783 struct nfs4_excl_time verf;
784 784 bool_t did_excl_setup = FALSE;
785 785 int created_osp;
786 786
787 787 OPEN4cargs *open_args;
788 788 nfs4_open_owner_t *oop = NULL;
789 789 nfs4_open_stream_t *osp = NULL;
790 790 seqid4 seqid = 0;
791 791 bool_t retry_open = FALSE;
792 792 nfs4_recov_state_t recov_state;
793 793 nfs4_lost_rqst_t lost_rqst;
794 794 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
795 795 hrtime_t t;
796 796 int acc = 0;
797 797 cred_t *cred_otw = NULL; /* cred used to do the RPC call */
798 798 cred_t *ncr = NULL;
799 799
800 800 nfs4_sharedfh_t *otw_sfh;
801 801 nfs4_sharedfh_t *orig_sfh;
802 802 int fh_differs = 0;
803 803 int numops, setgid_flag;
804 804 int num_bseqid_retry = NFS4_NUM_RETRY_BAD_SEQID + 1;
805 805
806 806 /*
807 807 * Make sure we properly deal with setting the right gid on
808 808 * a newly created file to reflect the parent's setgid bit
809 809 */
810 810 setgid_flag = 0;
811 811 if (create_flag && in_va) {
812 812
813 813 /*
814 814 * If there is grpid mount flag used or
815 815 * the parent's directory has the setgid bit set
816 816 * _and_ the client was able to get a valid mapping
817 817 * for the parent dir's owner_group, we want to
818 818 * append NVERIFY(owner_group == dva.va_gid) and
819 819 * SETATTR to the CREATE compound.
820 820 */
821 821 mutex_enter(&drp->r_statelock);
822 822 if ((VTOMI4(dvp)->mi_flags & MI4_GRPID ||
823 823 drp->r_attr.va_mode & VSGID) &&
824 824 drp->r_attr.va_gid != GID_NOBODY) {
825 825 in_va->va_mask |= AT_GID;
826 826 in_va->va_gid = drp->r_attr.va_gid;
827 827 setgid_flag = 1;
828 828 }
829 829 mutex_exit(&drp->r_statelock);
830 830 }
831 831
832 832 /*
833 833 * Normal/non-create compound:
834 834 * PUTFH(dfh) + OPEN(create) + GETFH + GETATTR(new)
835 835 *
836 836 * Open(create) compound no setgid:
837 837 * PUTFH(dfh) + SAVEFH + OPEN(create) + GETFH + GETATTR(new) +
838 838 * RESTOREFH + GETATTR
839 839 *
840 840 * Open(create) setgid:
841 841 * PUTFH(dfh) + OPEN(create) + GETFH + GETATTR(new) +
842 842 * SAVEFH + PUTFH(dfh) + GETATTR(dvp) + RESTOREFH +
843 843 * NVERIFY(grp) + SETATTR
844 844 */
845 845 if (setgid_flag) {
846 846 numops = 10;
847 847 idx_open = 1;
848 848 idx_fattr = 3;
849 849 } else if (create_flag) {
850 850 numops = 7;
851 851 idx_open = 2;
852 852 idx_fattr = 4;
853 853 } else {
854 854 numops = 4;
855 855 idx_open = 1;
856 856 idx_fattr = 3;
857 857 }
858 858
859 859 args.array_len = numops;
860 860 argoplist_size = numops * sizeof (nfs_argop4);
861 861 argop = kmem_alloc(argoplist_size, KM_SLEEP);
862 862
863 863 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4open_otw: "
864 864 "open %s open flag 0x%x cred %p", file_name, open_flag,
865 865 (void *)cr));
866 866
867 867 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
868 868 if (create_flag) {
869 869 /*
870 870 * We are to create a file. Initialize the passed in vnode
871 871 * pointer.
872 872 */
873 873 vpi = NULL;
874 874 } else {
875 875 /*
876 876 * Check to see if the client owns a read delegation and is
877 877 * trying to open for write. If so, then return the delegation
878 878 * to avoid the server doing a cb_recall and returning DELAY.
879 879 * NB - we don't use the statev4_lock here because we'd have
880 880 * to drop the lock anyway and the result would be stale.
881 881 */
882 882 if ((open_flag & FWRITE) &&
883 883 VTOR4(vpi)->r_deleg_type == OPEN_DELEGATE_READ)
884 884 (void) nfs4delegreturn(VTOR4(vpi), NFS4_DR_REOPEN);
885 885
886 886 /*
887 887 * If the file has a delegation, then do an access check up
888 888 * front. This avoids having to an access check later after
889 889 * we've already done start_op, which could deadlock.
890 890 */
891 891 if (VTOR4(vpi)->r_deleg_type != OPEN_DELEGATE_NONE) {
892 892 if (open_flag & FREAD &&
893 893 nfs4_access(vpi, VREAD, 0, cr, NULL) == 0)
894 894 acc |= VREAD;
895 895 if (open_flag & FWRITE &&
896 896 nfs4_access(vpi, VWRITE, 0, cr, NULL) == 0)
897 897 acc |= VWRITE;
898 898 }
899 899 }
900 900
901 901 drp = VTOR4(dvp);
902 902
903 903 recov_state.rs_flags = 0;
904 904 recov_state.rs_num_retry_despite_err = 0;
905 905 cred_otw = cr;
906 906
907 907 recov_retry:
908 908 fh_differs = 0;
909 909 nfs4_error_zinit(&e);
910 910
911 911 e.error = nfs4_start_op(VTOMI4(dvp), dvp, vpi, &recov_state);
912 912 if (e.error) {
913 913 if (ncr != NULL)
914 914 crfree(ncr);
915 915 kmem_free(argop, argoplist_size);
916 916 return (e.error);
917 917 }
918 918
919 919 args.ctag = TAG_OPEN;
920 920 args.array_len = numops;
921 921 args.array = argop;
922 922
923 923 /* putfh directory fh */
924 924 argop[0].argop = OP_CPUTFH;
925 925 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
926 926
927 927 /* OPEN: either op 1 or op 2 depending upon create/setgid flags */
928 928 argop[idx_open].argop = OP_COPEN;
929 929 open_args = &argop[idx_open].nfs_argop4_u.opcopen;
930 930 open_args->claim = CLAIM_NULL;
931 931
932 932 /* name of file */
933 933 open_args->open_claim4_u.cfile = file_name;
934 934 open_args->owner.owner_len = 0;
935 935 open_args->owner.owner_val = NULL;
936 936
937 937 if (create_flag) {
938 938 /* CREATE a file */
939 939 open_args->opentype = OPEN4_CREATE;
940 940 open_args->mode = createmode;
941 941 if (createmode == EXCLUSIVE4) {
942 942 if (did_excl_setup == FALSE) {
943 943 verf.seconds = zone_get_hostid(NULL);
944 944 if (verf.seconds != 0)
945 945 verf.nseconds = newnum();
946 946 else {
947 947 timestruc_t now;
948 948
949 949 gethrestime(&now);
950 950 verf.seconds = now.tv_sec;
951 951 verf.nseconds = now.tv_nsec;
952 952 }
953 953 /*
954 954 * Since the server will use this value for the
955 955 * mtime, make sure that it can't overflow. Zero
956 956 * out the MSB. The actual value does not matter
957 957 * here, only its uniqeness.
958 958 */
959 959 verf.seconds &= INT32_MAX;
960 960 did_excl_setup = TRUE;
961 961 }
962 962
963 963 /* Now copy over verifier to OPEN4args. */
964 964 open_args->createhow4_u.createverf = *(uint64_t *)&verf;
965 965 } else {
966 966 int v_error;
967 967 bitmap4 supp_attrs;
968 968 servinfo4_t *svp;
969 969
970 970 attr = &open_args->createhow4_u.createattrs;
971 971
972 972 svp = drp->r_server;
973 973 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
974 974 supp_attrs = svp->sv_supp_attrs;
975 975 nfs_rw_exit(&svp->sv_lock);
976 976
977 977 /* GUARDED4 or UNCHECKED4 */
978 978 v_error = vattr_to_fattr4(in_va, NULL, attr, 0, OP_OPEN,
979 979 supp_attrs);
980 980 if (v_error) {
981 981 bzero(attr, sizeof (*attr));
982 982 nfs4args_copen_free(open_args);
983 983 nfs4_end_op(VTOMI4(dvp), dvp, vpi,
984 984 &recov_state, FALSE);
985 985 if (ncr != NULL)
986 986 crfree(ncr);
987 987 kmem_free(argop, argoplist_size);
988 988 return (v_error);
989 989 }
990 990 }
991 991 } else {
992 992 /* NO CREATE */
993 993 open_args->opentype = OPEN4_NOCREATE;
994 994 }
995 995
996 996 if (recov_state.rs_sp != NULL) {
997 997 mutex_enter(&recov_state.rs_sp->s_lock);
998 998 open_args->owner.clientid = recov_state.rs_sp->clientid;
999 999 mutex_exit(&recov_state.rs_sp->s_lock);
1000 1000 } else {
1001 1001 /* XXX should we just fail here? */
1002 1002 open_args->owner.clientid = 0;
1003 1003 }
1004 1004
1005 1005 /*
1006 1006 * This increments oop's ref count or creates a temporary 'just_created'
1007 1007 * open owner that will become valid when this OPEN/OPEN_CONFIRM call
1008 1008 * completes.
1009 1009 */
1010 1010 mutex_enter(&VTOMI4(dvp)->mi_lock);
1011 1011
1012 1012 /* See if a permanent or just created open owner exists */
1013 1013 oop = find_open_owner_nolock(cr, NFS4_JUST_CREATED, VTOMI4(dvp));
1014 1014 if (!oop) {
1015 1015 /*
1016 1016 * This open owner does not exist so create a temporary
1017 1017 * just created one.
1018 1018 */
1019 1019 oop = create_open_owner(cr, VTOMI4(dvp));
1020 1020 ASSERT(oop != NULL);
1021 1021 }
1022 1022 mutex_exit(&VTOMI4(dvp)->mi_lock);
1023 1023
1024 1024 /* this length never changes, do alloc before seqid sync */
1025 1025 open_args->owner.owner_len = sizeof (oop->oo_name);
1026 1026 open_args->owner.owner_val =
1027 1027 kmem_alloc(open_args->owner.owner_len, KM_SLEEP);
1028 1028
1029 1029 e.error = nfs4_start_open_seqid_sync(oop, VTOMI4(dvp));
1030 1030 if (e.error == EAGAIN) {
1031 1031 open_owner_rele(oop);
1032 1032 nfs4args_copen_free(open_args);
1033 1033 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, TRUE);
1034 1034 if (ncr != NULL) {
1035 1035 crfree(ncr);
1036 1036 ncr = NULL;
1037 1037 }
1038 1038 goto recov_retry;
1039 1039 }
1040 1040
1041 1041 /* Check to see if we need to do the OTW call */
1042 1042 if (!create_flag) {
1043 1043 if (!nfs4_is_otw_open_necessary(oop, open_flag, vpi,
1044 1044 file_just_been_created, &e.error, acc, &recov_state)) {
1045 1045
1046 1046 /*
1047 1047 * The OTW open is not necessary. Either
1048 1048 * the open can succeed without it (eg.
1049 1049 * delegation, error == 0) or the open
1050 1050 * must fail due to an access failure
1051 1051 * (error != 0). In either case, tidy
1052 1052 * up and return.
1053 1053 */
1054 1054
1055 1055 nfs4_end_open_seqid_sync(oop);
1056 1056 open_owner_rele(oop);
1057 1057 nfs4args_copen_free(open_args);
1058 1058 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, FALSE);
1059 1059 if (ncr != NULL)
1060 1060 crfree(ncr);
1061 1061 kmem_free(argop, argoplist_size);
1062 1062 return (e.error);
1063 1063 }
1064 1064 }
1065 1065
1066 1066 bcopy(&oop->oo_name, open_args->owner.owner_val,
1067 1067 open_args->owner.owner_len);
1068 1068
1069 1069 seqid = nfs4_get_open_seqid(oop) + 1;
1070 1070 open_args->seqid = seqid;
1071 1071 open_args->share_access = 0;
1072 1072 if (open_flag & FREAD)
1073 1073 open_args->share_access |= OPEN4_SHARE_ACCESS_READ;
1074 1074 if (open_flag & FWRITE)
1075 1075 open_args->share_access |= OPEN4_SHARE_ACCESS_WRITE;
1076 1076 open_args->share_deny = OPEN4_SHARE_DENY_NONE;
1077 1077
1078 1078
1079 1079
1080 1080 /*
1081 1081 * getfh w/sanity check for idx_open/idx_fattr
1082 1082 */
1083 1083 ASSERT((idx_open + 1) == (idx_fattr - 1));
1084 1084 argop[idx_open + 1].argop = OP_GETFH;
1085 1085
1086 1086 /* getattr */
1087 1087 argop[idx_fattr].argop = OP_GETATTR;
1088 1088 argop[idx_fattr].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
1089 1089 argop[idx_fattr].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
1090 1090
1091 1091 if (setgid_flag) {
1092 1092 vattr_t _v;
1093 1093 servinfo4_t *svp;
1094 1094 bitmap4 supp_attrs;
1095 1095
1096 1096 svp = drp->r_server;
1097 1097 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
1098 1098 supp_attrs = svp->sv_supp_attrs;
1099 1099 nfs_rw_exit(&svp->sv_lock);
1100 1100
1101 1101 /*
1102 1102 * For setgid case, we need to:
1103 1103 * 4:savefh(new) 5:putfh(dir) 6:getattr(dir) 7:restorefh(new)
1104 1104 */
1105 1105 argop[4].argop = OP_SAVEFH;
1106 1106
1107 1107 argop[5].argop = OP_CPUTFH;
1108 1108 argop[5].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
1109 1109
1110 1110 argop[6].argop = OP_GETATTR;
1111 1111 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
1112 1112 argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
1113 1113
1114 1114 argop[7].argop = OP_RESTOREFH;
1115 1115
1116 1116 /*
1117 1117 * nverify
1118 1118 */
1119 1119 _v.va_mask = AT_GID;
1120 1120 _v.va_gid = in_va->va_gid;
1121 1121 if (!(e.error = nfs4args_verify(&argop[8], &_v, OP_NVERIFY,
1122 1122 supp_attrs))) {
1123 1123
1124 1124 /*
1125 1125 * setattr
1126 1126 *
1127 1127 * We _know_ we're not messing with AT_SIZE or
1128 1128 * AT_XTIME, so no need for stateid or flags.
1129 1129 * Also we specify NULL rp since we're only
1130 1130 * interested in setting owner_group attributes.
1131 1131 */
1132 1132 nfs4args_setattr(&argop[9], &_v, NULL, 0, NULL, cr,
1133 1133 supp_attrs, &e.error, 0);
1134 1134 if (e.error)
1135 1135 nfs4args_verify_free(&argop[8]);
1136 1136 }
1137 1137
1138 1138 if (e.error) {
1139 1139 /*
1140 1140 * XXX - Revisit the last argument to nfs4_end_op()
1141 1141 * once 5020486 is fixed.
1142 1142 */
1143 1143 nfs4_end_open_seqid_sync(oop);
1144 1144 open_owner_rele(oop);
1145 1145 nfs4args_copen_free(open_args);
1146 1146 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, TRUE);
1147 1147 if (ncr != NULL)
1148 1148 crfree(ncr);
1149 1149 kmem_free(argop, argoplist_size);
1150 1150 return (e.error);
1151 1151 }
1152 1152 } else if (create_flag) {
1153 1153 argop[1].argop = OP_SAVEFH;
1154 1154
1155 1155 argop[5].argop = OP_RESTOREFH;
1156 1156
1157 1157 argop[6].argop = OP_GETATTR;
1158 1158 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
1159 1159 argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
1160 1160 }
1161 1161
1162 1162 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE,
1163 1163 "nfs4open_otw: %s call, nm %s, rp %s",
1164 1164 needrecov ? "recov" : "first", file_name,
1165 1165 rnode4info(VTOR4(dvp))));
1166 1166
1167 1167 t = gethrtime();
1168 1168
1169 1169 rfs4call(VTOMI4(dvp), &args, &res, cred_otw, &doqueue, 0, &e);
1170 1170
1171 1171 if (!e.error && nfs4_need_to_bump_seqid(&res))
1172 1172 nfs4_set_open_seqid(seqid, oop, args.ctag);
1173 1173
1174 1174 needrecov = nfs4_needs_recovery(&e, TRUE, dvp->v_vfsp);
1175 1175
1176 1176 if (e.error || needrecov) {
1177 1177 bool_t abort = FALSE;
1178 1178
1179 1179 if (needrecov) {
1180 1180 nfs4_bseqid_entry_t *bsep = NULL;
1181 1181
1182 1182 nfs4open_save_lost_rqst(e.error, &lost_rqst, oop,
1183 1183 cred_otw, vpi, dvp, open_args);
1184 1184
1185 1185 if (!e.error && res.status == NFS4ERR_BAD_SEQID) {
1186 1186 bsep = nfs4_create_bseqid_entry(oop, NULL,
1187 1187 vpi, 0, args.ctag, open_args->seqid);
1188 1188 num_bseqid_retry--;
1189 1189 }
1190 1190
1191 1191 abort = nfs4_start_recovery(&e, VTOMI4(dvp), dvp, vpi,
1192 1192 NULL, lost_rqst.lr_op == OP_OPEN ?
1193 1193 &lost_rqst : NULL, OP_OPEN, bsep, NULL, NULL);
1194 1194
1195 1195 if (bsep)
1196 1196 kmem_free(bsep, sizeof (*bsep));
1197 1197 /* give up if we keep getting BAD_SEQID */
1198 1198 if (num_bseqid_retry == 0)
1199 1199 abort = TRUE;
1200 1200 if (abort == TRUE && e.error == 0)
1201 1201 e.error = geterrno4(res.status);
1202 1202 }
1203 1203 nfs4_end_open_seqid_sync(oop);
1204 1204 open_owner_rele(oop);
1205 1205 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov);
1206 1206 nfs4args_copen_free(open_args);
1207 1207 if (setgid_flag) {
1208 1208 nfs4args_verify_free(&argop[8]);
1209 1209 nfs4args_setattr_free(&argop[9]);
1210 1210 }
1211 1211 if (!e.error)
1212 1212 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1213 1213 if (ncr != NULL) {
1214 1214 crfree(ncr);
1215 1215 ncr = NULL;
1216 1216 }
1217 1217 if (!needrecov || abort == TRUE || e.error == EINTR ||
1218 1218 NFS4_FRC_UNMT_ERR(e.error, dvp->v_vfsp)) {
1219 1219 kmem_free(argop, argoplist_size);
1220 1220 return (e.error);
1221 1221 }
1222 1222 goto recov_retry;
1223 1223 }
1224 1224
1225 1225 /*
1226 1226 * Will check and update lease after checking the rflag for
1227 1227 * OPEN_CONFIRM in the successful OPEN call.
1228 1228 */
1229 1229 if (res.status != NFS4_OK && res.array_len <= idx_fattr + 1) {
1230 1230
1231 1231 /*
1232 1232 * XXX what if we're crossing mount points from server1:/drp
1233 1233 * to server2:/drp/rp.
1234 1234 */
1235 1235
1236 1236 /* Signal our end of use of the open seqid */
1237 1237 nfs4_end_open_seqid_sync(oop);
1238 1238
1239 1239 /*
1240 1240 * This will destroy the open owner if it was just created,
1241 1241 * and no one else has put a reference on it.
1242 1242 */
1243 1243 open_owner_rele(oop);
1244 1244 if (create_flag && (createmode != EXCLUSIVE4) &&
1245 1245 res.status == NFS4ERR_BADOWNER)
1246 1246 nfs4_log_badowner(VTOMI4(dvp), OP_OPEN);
1247 1247
1248 1248 e.error = geterrno4(res.status);
1249 1249 nfs4args_copen_free(open_args);
1250 1250 if (setgid_flag) {
1251 1251 nfs4args_verify_free(&argop[8]);
1252 1252 nfs4args_setattr_free(&argop[9]);
1253 1253 }
1254 1254 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1255 1255 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov);
1256 1256 /*
1257 1257 * If the reply is NFS4ERR_ACCESS, it may be because
1258 1258 * we are root (no root net access). If the real uid
1259 1259 * is not root, then retry with the real uid instead.
1260 1260 */
1261 1261 if (ncr != NULL) {
1262 1262 crfree(ncr);
1263 1263 ncr = NULL;
1264 1264 }
1265 1265 if (res.status == NFS4ERR_ACCESS &&
1266 1266 (ncr = crnetadjust(cred_otw)) != NULL) {
1267 1267 cred_otw = ncr;
1268 1268 goto recov_retry;
1269 1269 }
1270 1270 kmem_free(argop, argoplist_size);
1271 1271 return (e.error);
1272 1272 }
1273 1273
1274 1274 resop = &res.array[idx_open]; /* open res */
1275 1275 op_res = &resop->nfs_resop4_u.opopen;
1276 1276
1277 1277 #ifdef DEBUG
1278 1278 /*
1279 1279 * verify attrset bitmap
1280 1280 */
1281 1281 if (create_flag &&
1282 1282 (createmode == UNCHECKED4 || createmode == GUARDED4)) {
1283 1283 /* make sure attrset returned is what we asked for */
1284 1284 /* XXX Ignore this 'error' for now */
1285 1285 if (attr->attrmask != op_res->attrset)
1286 1286 /* EMPTY */;
1287 1287 }
1288 1288 #endif
1289 1289
1290 1290 if (op_res->rflags & OPEN4_RESULT_LOCKTYPE_POSIX) {
1291 1291 mutex_enter(&VTOMI4(dvp)->mi_lock);
1292 1292 VTOMI4(dvp)->mi_flags |= MI4_POSIX_LOCK;
1293 1293 mutex_exit(&VTOMI4(dvp)->mi_lock);
1294 1294 }
1295 1295
1296 1296 resop = &res.array[idx_open + 1]; /* getfh res */
1297 1297 gf_res = &resop->nfs_resop4_u.opgetfh;
1298 1298
1299 1299 otw_sfh = sfh4_get(&gf_res->object, VTOMI4(dvp));
1300 1300
1301 1301 /*
1302 1302 * The open stateid has been updated on the server but not
1303 1303 * on the client yet. There is a path: makenfs4node->nfs4_attr_cache->
1304 1304 * flush_pages->VOP_PUTPAGE->...->nfs4write where we will issue an OTW
1305 1305 * WRITE call. That, however, will use the old stateid, so go ahead
1306 1306 * and upate the open stateid now, before any call to makenfs4node.
1307 1307 */
1308 1308 if (vpi) {
1309 1309 nfs4_open_stream_t *tmp_osp;
1310 1310 rnode4_t *tmp_rp = VTOR4(vpi);
1311 1311
1312 1312 tmp_osp = find_open_stream(oop, tmp_rp);
1313 1313 if (tmp_osp) {
1314 1314 tmp_osp->open_stateid = op_res->stateid;
1315 1315 mutex_exit(&tmp_osp->os_sync_lock);
1316 1316 open_stream_rele(tmp_osp, tmp_rp);
1317 1317 }
1318 1318
1319 1319 /*
1320 1320 * We must determine if the file handle given by the otw open
1321 1321 * is the same as the file handle which was passed in with
1322 1322 * *vpp. This case can be reached if the file we are trying
1323 1323 * to open has been removed and another file has been created
1324 1324 * having the same file name. The passed in vnode is released
1325 1325 * later.
1326 1326 */
1327 1327 orig_sfh = VTOR4(vpi)->r_fh;
1328 1328 fh_differs = nfs4cmpfh(&orig_sfh->sfh_fh, &otw_sfh->sfh_fh);
1329 1329 }
1330 1330
1331 1331 garp = &res.array[idx_fattr].nfs_resop4_u.opgetattr.ga_res;
1332 1332
1333 1333 if (create_flag || fh_differs) {
1334 1334 int rnode_err = 0;
1335 1335
1336 1336 vp = makenfs4node(otw_sfh, garp, dvp->v_vfsp, t, cr,
1337 1337 dvp, fn_get(VTOSV(dvp)->sv_name, file_name, otw_sfh));
1338 1338
1339 1339 if (e.error)
1340 1340 PURGE_ATTRCACHE4(vp);
1341 1341 /*
1342 1342 * For the newly created vp case, make sure the rnode
1343 1343 * isn't bad before using it.
1344 1344 */
1345 1345 mutex_enter(&(VTOR4(vp))->r_statelock);
1346 1346 if (VTOR4(vp)->r_flags & R4RECOVERR)
1347 1347 rnode_err = EIO;
1348 1348 mutex_exit(&(VTOR4(vp))->r_statelock);
1349 1349
1350 1350 if (rnode_err) {
1351 1351 nfs4_end_open_seqid_sync(oop);
1352 1352 nfs4args_copen_free(open_args);
1353 1353 if (setgid_flag) {
1354 1354 nfs4args_verify_free(&argop[8]);
1355 1355 nfs4args_setattr_free(&argop[9]);
1356 1356 }
1357 1357 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1358 1358 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state,
1359 1359 needrecov);
1360 1360 open_owner_rele(oop);
1361 1361 VN_RELE(vp);
1362 1362 if (ncr != NULL)
1363 1363 crfree(ncr);
1364 1364 sfh4_rele(&otw_sfh);
1365 1365 kmem_free(argop, argoplist_size);
1366 1366 return (EIO);
1367 1367 }
1368 1368 } else {
1369 1369 vp = vpi;
1370 1370 }
1371 1371 sfh4_rele(&otw_sfh);
1372 1372
1373 1373 /*
1374 1374 * It seems odd to get a full set of attrs and then not update
1375 1375 * the object's attrcache in the non-create case. Create case uses
1376 1376 * the attrs since makenfs4node checks to see if the attrs need to
1377 1377 * be updated (and then updates them). The non-create case should
1378 1378 * update attrs also.
1379 1379 */
1380 1380 if (! create_flag && ! fh_differs && !e.error) {
1381 1381 nfs4_attr_cache(vp, garp, t, cr, TRUE, NULL);
1382 1382 }
1383 1383
1384 1384 nfs4_error_zinit(&e);
1385 1385 if (op_res->rflags & OPEN4_RESULT_CONFIRM) {
1386 1386 /* This does not do recovery for vp explicitly. */
1387 1387 nfs4open_confirm(vp, &seqid, &op_res->stateid, cred_otw, FALSE,
1388 1388 &retry_open, oop, FALSE, &e, &num_bseqid_retry);
1389 1389
1390 1390 if (e.error || e.stat) {
1391 1391 nfs4_end_open_seqid_sync(oop);
1392 1392 nfs4args_copen_free(open_args);
1393 1393 if (setgid_flag) {
1394 1394 nfs4args_verify_free(&argop[8]);
1395 1395 nfs4args_setattr_free(&argop[9]);
1396 1396 }
1397 1397 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1398 1398 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state,
1399 1399 needrecov);
1400 1400 open_owner_rele(oop);
1401 1401 if (create_flag || fh_differs) {
1402 1402 /* rele the makenfs4node */
1403 1403 VN_RELE(vp);
1404 1404 }
1405 1405 if (ncr != NULL) {
1406 1406 crfree(ncr);
1407 1407 ncr = NULL;
1408 1408 }
1409 1409 if (retry_open == TRUE) {
1410 1410 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
1411 1411 "nfs4open_otw: retry the open since OPEN "
1412 1412 "CONFIRM failed with error %d stat %d",
1413 1413 e.error, e.stat));
1414 1414 if (create_flag && createmode == GUARDED4) {
1415 1415 NFS4_DEBUG(nfs4_client_recov_debug,
1416 1416 (CE_NOTE, "nfs4open_otw: switch "
1417 1417 "createmode from GUARDED4 to "
1418 1418 "UNCHECKED4"));
1419 1419 createmode = UNCHECKED4;
1420 1420 }
1421 1421 goto recov_retry;
1422 1422 }
1423 1423 if (!e.error) {
1424 1424 if (create_flag && (createmode != EXCLUSIVE4) &&
1425 1425 e.stat == NFS4ERR_BADOWNER)
1426 1426 nfs4_log_badowner(VTOMI4(dvp), OP_OPEN);
1427 1427
1428 1428 e.error = geterrno4(e.stat);
1429 1429 }
1430 1430 kmem_free(argop, argoplist_size);
1431 1431 return (e.error);
1432 1432 }
1433 1433 }
1434 1434
1435 1435 rp = VTOR4(vp);
1436 1436
1437 1437 mutex_enter(&rp->r_statev4_lock);
1438 1438 if (create_flag)
1439 1439 rp->created_v4 = 1;
1440 1440 mutex_exit(&rp->r_statev4_lock);
1441 1441
1442 1442 mutex_enter(&oop->oo_lock);
1443 1443 /* Doesn't matter if 'oo_just_created' already was set as this */
1444 1444 oop->oo_just_created = NFS4_PERM_CREATED;
1445 1445 if (oop->oo_cred_otw)
1446 1446 crfree(oop->oo_cred_otw);
1447 1447 oop->oo_cred_otw = cred_otw;
1448 1448 crhold(oop->oo_cred_otw);
1449 1449 mutex_exit(&oop->oo_lock);
1450 1450
1451 1451 /* returns with 'os_sync_lock' held */
1452 1452 osp = find_or_create_open_stream(oop, rp, &created_osp);
1453 1453 if (!osp) {
1454 1454 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE,
1455 1455 "nfs4open_otw: failed to create an open stream"));
1456 1456 NFS4_DEBUG(nfs4_seqid_sync, (CE_NOTE, "nfs4open_otw: "
1457 1457 "signal our end of use of the open seqid"));
1458 1458
1459 1459 nfs4_end_open_seqid_sync(oop);
1460 1460 open_owner_rele(oop);
1461 1461 nfs4args_copen_free(open_args);
1462 1462 if (setgid_flag) {
1463 1463 nfs4args_verify_free(&argop[8]);
1464 1464 nfs4args_setattr_free(&argop[9]);
1465 1465 }
1466 1466 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1467 1467 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov);
1468 1468 if (create_flag || fh_differs)
1469 1469 VN_RELE(vp);
1470 1470 if (ncr != NULL)
1471 1471 crfree(ncr);
1472 1472
1473 1473 kmem_free(argop, argoplist_size);
1474 1474 return (EINVAL);
1475 1475
1476 1476 }
1477 1477
1478 1478 osp->open_stateid = op_res->stateid;
1479 1479
1480 1480 if (open_flag & FREAD)
1481 1481 osp->os_share_acc_read++;
1482 1482 if (open_flag & FWRITE)
1483 1483 osp->os_share_acc_write++;
1484 1484 osp->os_share_deny_none++;
1485 1485
1486 1486 /*
1487 1487 * Need to reset this bitfield for the possible case where we were
1488 1488 * going to OTW CLOSE the file, got a non-recoverable error, and before
1489 1489 * we could retry the CLOSE, OPENed the file again.
1490 1490 */
1491 1491 ASSERT(osp->os_open_owner->oo_seqid_inuse);
1492 1492 osp->os_final_close = 0;
1493 1493 osp->os_force_close = 0;
1494 1494 #ifdef DEBUG
1495 1495 if (osp->os_failed_reopen)
1496 1496 NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE, "nfs4open_otw:"
1497 1497 " clearing os_failed_reopen for osp %p, cr %p, rp %s",
1498 1498 (void *)osp, (void *)cr, rnode4info(rp)));
1499 1499 #endif
1500 1500 osp->os_failed_reopen = 0;
1501 1501
1502 1502 mutex_exit(&osp->os_sync_lock);
1503 1503
1504 1504 nfs4_end_open_seqid_sync(oop);
1505 1505
1506 1506 if (created_osp && recov_state.rs_sp != NULL) {
1507 1507 mutex_enter(&recov_state.rs_sp->s_lock);
1508 1508 nfs4_inc_state_ref_count_nolock(recov_state.rs_sp, VTOMI4(dvp));
1509 1509 mutex_exit(&recov_state.rs_sp->s_lock);
1510 1510 }
1511 1511
1512 1512 /* get rid of our reference to find oop */
1513 1513 open_owner_rele(oop);
1514 1514
1515 1515 open_stream_rele(osp, rp);
1516 1516
1517 1517 /* accept delegation, if any */
1518 1518 nfs4_delegation_accept(rp, CLAIM_NULL, op_res, garp, cred_otw);
1519 1519
1520 1520 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov);
1521 1521
1522 1522 if (createmode == EXCLUSIVE4 &&
1523 1523 (in_va->va_mask & ~(AT_GID | AT_SIZE))) {
1524 1524 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4open_otw:"
1525 1525 " EXCLUSIVE4: sending a SETATTR"));
1526 1526 /*
1527 1527 * If doing an exclusive create, then generate
1528 1528 * a SETATTR to set the initial attributes.
1529 1529 * Try to set the mtime and the atime to the
1530 1530 * server's current time. It is somewhat
1531 1531 * expected that these fields will be used to
1532 1532 * store the exclusive create cookie. If not,
1533 1533 * server implementors will need to know that
1534 1534 * a SETATTR will follow an exclusive create
1535 1535 * and the cookie should be destroyed if
1536 1536 * appropriate.
1537 1537 *
1538 1538 * The AT_GID and AT_SIZE bits are turned off
1539 1539 * so that the SETATTR request will not attempt
1540 1540 * to process these. The gid will be set
1541 1541 * separately if appropriate. The size is turned
1542 1542 * off because it is assumed that a new file will
1543 1543 * be created empty and if the file wasn't empty,
1544 1544 * then the exclusive create will have failed
1545 1545 * because the file must have existed already.
1546 1546 * Therefore, no truncate operation is needed.
1547 1547 */
1548 1548 in_va->va_mask &= ~(AT_GID | AT_SIZE);
1549 1549 in_va->va_mask |= (AT_MTIME | AT_ATIME);
1550 1550
1551 1551 e.error = nfs4setattr(vp, in_va, 0, cr, NULL);
1552 1552 if (e.error) {
1553 1553 /*
1554 1554 * Couldn't correct the attributes of
1555 1555 * the newly created file and the
1556 1556 * attributes are wrong. Remove the
1557 1557 * file and return an error to the
1558 1558 * application.
1559 1559 */
1560 1560 /* XXX will this take care of client state ? */
1561 1561 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE,
1562 1562 "nfs4open_otw: EXCLUSIVE4: error %d on SETATTR:"
1563 1563 " remove file", e.error));
1564 1564 VN_RELE(vp);
1565 1565 (void) nfs4_remove(dvp, file_name, cr, NULL, 0);
1566 1566 /*
1567 1567 * Since we've reled the vnode and removed
1568 1568 * the file we now need to return the error.
1569 1569 * At this point we don't want to update the
1570 1570 * dircaches, call nfs4_waitfor_purge_complete
1571 1571 * or set vpp to vp so we need to skip these
1572 1572 * as well.
1573 1573 */
1574 1574 goto skip_update_dircaches;
1575 1575 }
1576 1576 }
1577 1577
1578 1578 /*
1579 1579 * If we created or found the correct vnode, due to create_flag or
1580 1580 * fh_differs being set, then update directory cache attribute, readdir
1581 1581 * and dnlc caches.
1582 1582 */
1583 1583 if (create_flag || fh_differs) {
1584 1584 dirattr_info_t dinfo, *dinfop;
1585 1585
1586 1586 /*
1587 1587 * Make sure getattr succeeded before using results.
1588 1588 * note: op 7 is getattr(dir) for both flavors of
1589 1589 * open(create).
1590 1590 */
1591 1591 if (create_flag && res.status == NFS4_OK) {
1592 1592 dinfo.di_time_call = t;
1593 1593 dinfo.di_cred = cr;
1594 1594 dinfo.di_garp =
1595 1595 &res.array[6].nfs_resop4_u.opgetattr.ga_res;
1596 1596 dinfop = &dinfo;
1597 1597 } else {
1598 1598 dinfop = NULL;
1599 1599 }
1600 1600
1601 1601 nfs4_update_dircaches(&op_res->cinfo, dvp, vp, file_name,
1602 1602 dinfop);
1603 1603 }
1604 1604
1605 1605 /*
1606 1606 * If the page cache for this file was flushed from actions
1607 1607 * above, it was done asynchronously and if that is true,
1608 1608 * there is a need to wait here for it to complete. This must
1609 1609 * be done outside of start_fop/end_fop.
1610 1610 */
1611 1611 (void) nfs4_waitfor_purge_complete(vp);
1612 1612
1613 1613 /*
1614 1614 * It is implicit that we are in the open case (create_flag == 0) since
1615 1615 * fh_differs can only be set to a non-zero value in the open case.
1616 1616 */
1617 1617 if (fh_differs != 0 && vpi != NULL)
1618 1618 VN_RELE(vpi);
1619 1619
1620 1620 /*
1621 1621 * Be sure to set *vpp to the correct value before returning.
1622 1622 */
1623 1623 *vpp = vp;
1624 1624
1625 1625 skip_update_dircaches:
1626 1626
1627 1627 nfs4args_copen_free(open_args);
1628 1628 if (setgid_flag) {
1629 1629 nfs4args_verify_free(&argop[8]);
1630 1630 nfs4args_setattr_free(&argop[9]);
1631 1631 }
1632 1632 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1633 1633
1634 1634 if (ncr)
1635 1635 crfree(ncr);
1636 1636 kmem_free(argop, argoplist_size);
1637 1637 return (e.error);
1638 1638 }
1639 1639
1640 1640 /*
1641 1641 * Reopen an open instance. cf. nfs4open_otw().
1642 1642 *
1643 1643 * Errors are returned by the nfs4_error_t parameter.
1644 1644 * - ep->error contains an errno value or zero.
1645 1645 * - if it is zero, ep->stat is set to an NFS status code, if any.
1646 1646 * If the file could not be reopened, but the caller should continue, the
1647 1647 * file is marked dead and no error values are returned. If the caller
1648 1648 * should stop recovering open files and start over, either the ep->error
1649 1649 * value or ep->stat will indicate an error (either something that requires
1650 1650 * recovery or EAGAIN). Note that some recovery (e.g., expired volatile
1651 1651 * filehandles) may be handled silently by this routine.
1652 1652 * - if it is EINTR, ETIMEDOUT, or NFS4_FRC_UNMT_ERR, recovery for lost state
1653 1653 * will be started, so the caller should not do it.
1654 1654 *
1655 1655 * Gotos:
1656 1656 * - kill_file : reopen failed in such a fashion to constitute marking the
1657 1657 * file dead and setting the open stream's 'os_failed_reopen' as 1. This
1658 1658 * is for cases where recovery is not possible.
1659 1659 * - failed_reopen : same as above, except that the file has already been
1660 1660 * marked dead, so no need to do it again.
1661 1661 * - bailout : reopen failed but we are able to recover and retry the reopen -
1662 1662 * either within this function immediately or via the calling function.
1663 1663 */
1664 1664
1665 1665 void
1666 1666 nfs4_reopen(vnode_t *vp, nfs4_open_stream_t *osp, nfs4_error_t *ep,
1667 1667 open_claim_type4 claim, bool_t frc_use_claim_previous,
1668 1668 bool_t is_recov)
1669 1669 {
1670 1670 COMPOUND4args_clnt args;
1671 1671 COMPOUND4res_clnt res;
1672 1672 nfs_argop4 argop[4];
1673 1673 nfs_resop4 *resop;
1674 1674 OPEN4res *op_res = NULL;
1675 1675 OPEN4cargs *open_args;
1676 1676 GETFH4res *gf_res;
1677 1677 rnode4_t *rp = VTOR4(vp);
1678 1678 int doqueue = 1;
1679 1679 cred_t *cr = NULL, *cred_otw = NULL;
1680 1680 nfs4_open_owner_t *oop = NULL;
1681 1681 seqid4 seqid;
1682 1682 nfs4_ga_res_t *garp;
1683 1683 char fn[MAXNAMELEN];
1684 1684 nfs4_recov_state_t recov = {NULL, 0};
1685 1685 nfs4_lost_rqst_t lost_rqst;
1686 1686 mntinfo4_t *mi = VTOMI4(vp);
1687 1687 bool_t abort;
1688 1688 char *failed_msg = "";
1689 1689 int fh_different;
1690 1690 hrtime_t t;
1691 1691 nfs4_bseqid_entry_t *bsep = NULL;
1692 1692
1693 1693 ASSERT(nfs4_consistent_type(vp));
1694 1694 ASSERT(nfs_zone() == mi->mi_zone);
1695 1695
1696 1696 nfs4_error_zinit(ep);
1697 1697
1698 1698 /* this is the cred used to find the open owner */
1699 1699 cr = state_to_cred(osp);
1700 1700 if (cr == NULL) {
1701 1701 failed_msg = "Couldn't reopen: no cred";
1702 1702 goto kill_file;
1703 1703 }
1704 1704 /* use this cred for OTW operations */
1705 1705 cred_otw = nfs4_get_otw_cred(cr, mi, osp->os_open_owner);
1706 1706
1707 1707 top:
1708 1708 nfs4_error_zinit(ep);
1709 1709
1710 1710 if (mi->mi_vfsp->vfs_flag & VFS_UNMOUNTED) {
1711 1711 /* File system has been unmounted, quit */
1712 1712 ep->error = EIO;
1713 1713 failed_msg = "Couldn't reopen: file system has been unmounted";
1714 1714 goto kill_file;
1715 1715 }
1716 1716
1717 1717 oop = osp->os_open_owner;
1718 1718
1719 1719 ASSERT(oop != NULL);
1720 1720 if (oop == NULL) { /* be defensive in non-DEBUG */
1721 1721 failed_msg = "can't reopen: no open owner";
1722 1722 goto kill_file;
1723 1723 }
1724 1724 open_owner_hold(oop);
1725 1725
1726 1726 ep->error = nfs4_start_open_seqid_sync(oop, mi);
1727 1727 if (ep->error) {
1728 1728 open_owner_rele(oop);
1729 1729 oop = NULL;
1730 1730 goto bailout;
1731 1731 }
1732 1732
1733 1733 /*
1734 1734 * If the rnode has a delegation and the delegation has been
1735 1735 * recovered and the server didn't request a recall and the caller
1736 1736 * didn't specifically ask for CLAIM_PREVIOUS (nfs4frlock during
1737 1737 * recovery) and the rnode hasn't been marked dead, then install
1738 1738 * the delegation stateid in the open stream. Otherwise, proceed
1739 1739 * with a CLAIM_PREVIOUS or CLAIM_NULL OPEN.
1740 1740 */
1741 1741 mutex_enter(&rp->r_statev4_lock);
1742 1742 if (rp->r_deleg_type != OPEN_DELEGATE_NONE &&
1743 1743 !rp->r_deleg_return_pending &&
1744 1744 (rp->r_deleg_needs_recovery == OPEN_DELEGATE_NONE) &&
1745 1745 !rp->r_deleg_needs_recall &&
1746 1746 claim != CLAIM_DELEGATE_CUR && !frc_use_claim_previous &&
1747 1747 !(rp->r_flags & R4RECOVERR)) {
1748 1748 mutex_enter(&osp->os_sync_lock);
1749 1749 osp->os_delegation = 1;
1750 1750 osp->open_stateid = rp->r_deleg_stateid;
1751 1751 mutex_exit(&osp->os_sync_lock);
1752 1752 mutex_exit(&rp->r_statev4_lock);
1753 1753 goto bailout;
1754 1754 }
1755 1755 mutex_exit(&rp->r_statev4_lock);
1756 1756
1757 1757 /*
1758 1758 * If the file failed recovery, just quit. This failure need not
1759 1759 * affect other reopens, so don't return an error.
1760 1760 */
1761 1761 mutex_enter(&rp->r_statelock);
1762 1762 if (rp->r_flags & R4RECOVERR) {
1763 1763 mutex_exit(&rp->r_statelock);
1764 1764 ep->error = 0;
1765 1765 goto failed_reopen;
1766 1766 }
1767 1767 mutex_exit(&rp->r_statelock);
1768 1768
1769 1769 /*
1770 1770 * argop is empty here
1771 1771 *
1772 1772 * PUTFH, OPEN, GETATTR
1773 1773 */
1774 1774 args.ctag = TAG_REOPEN;
1775 1775 args.array_len = 4;
1776 1776 args.array = argop;
1777 1777
1778 1778 NFS4_DEBUG(nfs4_client_failover_debug, (CE_NOTE,
1779 1779 "nfs4_reopen: file is type %d, id %s",
1780 1780 vp->v_type, rnode4info(VTOR4(vp))));
1781 1781
1782 1782 argop[0].argop = OP_CPUTFH;
1783 1783
1784 1784 if (claim != CLAIM_PREVIOUS) {
1785 1785 /*
1786 1786 * if this is a file mount then
1787 1787 * use the mntinfo parentfh
1788 1788 */
1789 1789 argop[0].nfs_argop4_u.opcputfh.sfh =
1790 1790 (vp->v_flag & VROOT) ? mi->mi_srvparentfh :
1791 1791 VTOSV(vp)->sv_dfh;
1792 1792 } else {
1793 1793 /* putfh fh to reopen */
1794 1794 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
1795 1795 }
1796 1796
1797 1797 argop[1].argop = OP_COPEN;
1798 1798 open_args = &argop[1].nfs_argop4_u.opcopen;
1799 1799 open_args->claim = claim;
1800 1800
1801 1801 if (claim == CLAIM_NULL) {
1802 1802
1803 1803 if ((ep->error = vtoname(vp, fn, MAXNAMELEN)) != 0) {
1804 1804 nfs_cmn_err(ep->error, CE_WARN, "nfs4_reopen: vtoname "
1805 1805 "failed for vp 0x%p for CLAIM_NULL with %m",
1806 1806 (void *)vp);
1807 1807 failed_msg = "Couldn't reopen: vtoname failed for "
1808 1808 "CLAIM_NULL";
1809 1809 /* nothing allocated yet */
1810 1810 goto kill_file;
1811 1811 }
1812 1812
1813 1813 open_args->open_claim4_u.cfile = fn;
1814 1814 } else if (claim == CLAIM_PREVIOUS) {
1815 1815
1816 1816 /*
1817 1817 * We have two cases to deal with here:
1818 1818 * 1) We're being called to reopen files in order to satisfy
1819 1819 * a lock operation request which requires us to explicitly
1820 1820 * reopen files which were opened under a delegation. If
1821 1821 * we're in recovery, we *must* use CLAIM_PREVIOUS. In
1822 1822 * that case, frc_use_claim_previous is TRUE and we must
1823 1823 * use the rnode's current delegation type (r_deleg_type).
1824 1824 * 2) We're reopening files during some form of recovery.
1825 1825 * In this case, frc_use_claim_previous is FALSE and we
1826 1826 * use the delegation type appropriate for recovery
1827 1827 * (r_deleg_needs_recovery).
1828 1828 */
1829 1829 mutex_enter(&rp->r_statev4_lock);
1830 1830 open_args->open_claim4_u.delegate_type =
1831 1831 frc_use_claim_previous ?
1832 1832 rp->r_deleg_type :
1833 1833 rp->r_deleg_needs_recovery;
1834 1834 mutex_exit(&rp->r_statev4_lock);
1835 1835
1836 1836 } else if (claim == CLAIM_DELEGATE_CUR) {
1837 1837
1838 1838 if ((ep->error = vtoname(vp, fn, MAXNAMELEN)) != 0) {
1839 1839 nfs_cmn_err(ep->error, CE_WARN, "nfs4_reopen: vtoname "
1840 1840 "failed for vp 0x%p for CLAIM_DELEGATE_CUR "
1841 1841 "with %m", (void *)vp);
1842 1842 failed_msg = "Couldn't reopen: vtoname failed for "
1843 1843 "CLAIM_DELEGATE_CUR";
1844 1844 /* nothing allocated yet */
1845 1845 goto kill_file;
1846 1846 }
1847 1847
1848 1848 mutex_enter(&rp->r_statev4_lock);
1849 1849 open_args->open_claim4_u.delegate_cur_info.delegate_stateid =
1850 1850 rp->r_deleg_stateid;
1851 1851 mutex_exit(&rp->r_statev4_lock);
1852 1852
1853 1853 open_args->open_claim4_u.delegate_cur_info.cfile = fn;
1854 1854 }
1855 1855 open_args->opentype = OPEN4_NOCREATE;
1856 1856 open_args->owner.clientid = mi2clientid(mi);
1857 1857 open_args->owner.owner_len = sizeof (oop->oo_name);
1858 1858 open_args->owner.owner_val =
1859 1859 kmem_alloc(open_args->owner.owner_len, KM_SLEEP);
1860 1860 bcopy(&oop->oo_name, open_args->owner.owner_val,
1861 1861 open_args->owner.owner_len);
1862 1862 open_args->share_access = 0;
1863 1863 open_args->share_deny = 0;
1864 1864
1865 1865 mutex_enter(&osp->os_sync_lock);
1866 1866 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, "nfs4_reopen: osp %p rp "
1867 1867 "%p: read acc %"PRIu64" write acc %"PRIu64": open ref count %d: "
1868 1868 "mmap read %"PRIu64" mmap write %"PRIu64" claim %d ",
1869 1869 (void *)osp, (void *)rp, osp->os_share_acc_read,
1870 1870 osp->os_share_acc_write, osp->os_open_ref_count,
1871 1871 osp->os_mmap_read, osp->os_mmap_write, claim));
1872 1872
1873 1873 if (osp->os_share_acc_read || osp->os_mmap_read)
1874 1874 open_args->share_access |= OPEN4_SHARE_ACCESS_READ;
1875 1875 if (osp->os_share_acc_write || osp->os_mmap_write)
1876 1876 open_args->share_access |= OPEN4_SHARE_ACCESS_WRITE;
1877 1877 if (osp->os_share_deny_read)
1878 1878 open_args->share_deny |= OPEN4_SHARE_DENY_READ;
1879 1879 if (osp->os_share_deny_write)
1880 1880 open_args->share_deny |= OPEN4_SHARE_DENY_WRITE;
1881 1881 mutex_exit(&osp->os_sync_lock);
1882 1882
1883 1883 seqid = nfs4_get_open_seqid(oop) + 1;
1884 1884 open_args->seqid = seqid;
1885 1885
1886 1886 /* Construct the getfh part of the compound */
1887 1887 argop[2].argop = OP_GETFH;
1888 1888
1889 1889 /* Construct the getattr part of the compound */
1890 1890 argop[3].argop = OP_GETATTR;
1891 1891 argop[3].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
1892 1892 argop[3].nfs_argop4_u.opgetattr.mi = mi;
1893 1893
1894 1894 t = gethrtime();
1895 1895
1896 1896 rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, ep);
1897 1897
1898 1898 if (ep->error) {
1899 1899 if (!is_recov && !frc_use_claim_previous &&
1900 1900 (ep->error == EINTR || ep->error == ETIMEDOUT ||
1901 1901 NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp))) {
1902 1902 nfs4open_save_lost_rqst(ep->error, &lost_rqst, oop,
1903 1903 cred_otw, vp, NULL, open_args);
1904 1904 abort = nfs4_start_recovery(ep,
1905 1905 VTOMI4(vp), vp, NULL, NULL,
1906 1906 lost_rqst.lr_op == OP_OPEN ?
1907 1907 &lost_rqst : NULL, OP_OPEN, NULL, NULL, NULL);
1908 1908 nfs4args_copen_free(open_args);
1909 1909 goto bailout;
1910 1910 }
1911 1911
1912 1912 nfs4args_copen_free(open_args);
1913 1913
1914 1914 if (ep->error == EACCES && cred_otw != cr) {
1915 1915 crfree(cred_otw);
1916 1916 cred_otw = cr;
1917 1917 crhold(cred_otw);
1918 1918 nfs4_end_open_seqid_sync(oop);
1919 1919 open_owner_rele(oop);
1920 1920 oop = NULL;
1921 1921 goto top;
1922 1922 }
1923 1923 if (ep->error == ETIMEDOUT)
1924 1924 goto bailout;
1925 1925 failed_msg = "Couldn't reopen: rpc error";
1926 1926 goto kill_file;
1927 1927 }
1928 1928
1929 1929 if (nfs4_need_to_bump_seqid(&res))
1930 1930 nfs4_set_open_seqid(seqid, oop, args.ctag);
1931 1931
1932 1932 switch (res.status) {
1933 1933 case NFS4_OK:
1934 1934 if (recov.rs_flags & NFS4_RS_DELAY_MSG) {
1935 1935 mutex_enter(&rp->r_statelock);
1936 1936 rp->r_delay_interval = 0;
1937 1937 mutex_exit(&rp->r_statelock);
1938 1938 }
1939 1939 break;
1940 1940 case NFS4ERR_BAD_SEQID:
1941 1941 bsep = nfs4_create_bseqid_entry(oop, NULL, vp, 0,
1942 1942 args.ctag, open_args->seqid);
1943 1943
1944 1944 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL,
1945 1945 NULL, lost_rqst.lr_op == OP_OPEN ? &lost_rqst :
1946 1946 NULL, OP_OPEN, bsep, NULL, NULL);
1947 1947
1948 1948 nfs4args_copen_free(open_args);
1949 1949 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1950 1950 nfs4_end_open_seqid_sync(oop);
1951 1951 open_owner_rele(oop);
1952 1952 oop = NULL;
1953 1953 kmem_free(bsep, sizeof (*bsep));
1954 1954
1955 1955 goto kill_file;
1956 1956 case NFS4ERR_NO_GRACE:
1957 1957 nfs4args_copen_free(open_args);
1958 1958 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1959 1959 nfs4_end_open_seqid_sync(oop);
1960 1960 open_owner_rele(oop);
1961 1961 oop = NULL;
1962 1962 if (claim == CLAIM_PREVIOUS) {
1963 1963 /*
1964 1964 * Retry as a plain open. We don't need to worry about
1965 1965 * checking the changeinfo: it is acceptable for a
1966 1966 * client to re-open a file and continue processing
1967 1967 * (in the absence of locks).
1968 1968 */
1969 1969 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
1970 1970 "nfs4_reopen: CLAIM_PREVIOUS: NFS4ERR_NO_GRACE; "
1971 1971 "will retry as CLAIM_NULL"));
1972 1972 claim = CLAIM_NULL;
1973 1973 nfs4_mi_kstat_inc_no_grace(mi);
1974 1974 goto top;
1975 1975 }
1976 1976 failed_msg =
1977 1977 "Couldn't reopen: tried reclaim outside grace period. ";
1978 1978 goto kill_file;
1979 1979 case NFS4ERR_GRACE:
1980 1980 nfs4_set_grace_wait(mi);
1981 1981 nfs4args_copen_free(open_args);
1982 1982 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1983 1983 nfs4_end_open_seqid_sync(oop);
1984 1984 open_owner_rele(oop);
1985 1985 oop = NULL;
1986 1986 ep->error = nfs4_wait_for_grace(mi, &recov);
1987 1987 if (ep->error != 0)
1988 1988 goto bailout;
1989 1989 goto top;
1990 1990 case NFS4ERR_DELAY:
1991 1991 nfs4_set_delay_wait(vp);
1992 1992 nfs4args_copen_free(open_args);
1993 1993 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1994 1994 nfs4_end_open_seqid_sync(oop);
1995 1995 open_owner_rele(oop);
1996 1996 oop = NULL;
1997 1997 ep->error = nfs4_wait_for_delay(vp, &recov);
1998 1998 nfs4_mi_kstat_inc_delay(mi);
1999 1999 if (ep->error != 0)
2000 2000 goto bailout;
2001 2001 goto top;
2002 2002 case NFS4ERR_FHEXPIRED:
2003 2003 /* recover filehandle and retry */
2004 2004 abort = nfs4_start_recovery(ep,
2005 2005 mi, vp, NULL, NULL, NULL, OP_OPEN, NULL, NULL, NULL);
2006 2006 nfs4args_copen_free(open_args);
2007 2007 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2008 2008 nfs4_end_open_seqid_sync(oop);
2009 2009 open_owner_rele(oop);
2010 2010 oop = NULL;
2011 2011 if (abort == FALSE)
2012 2012 goto top;
2013 2013 failed_msg = "Couldn't reopen: recovery aborted";
2014 2014 goto kill_file;
2015 2015 case NFS4ERR_RESOURCE:
2016 2016 case NFS4ERR_STALE_CLIENTID:
2017 2017 case NFS4ERR_WRONGSEC:
2018 2018 case NFS4ERR_EXPIRED:
2019 2019 /*
2020 2020 * Do not mark the file dead and let the calling
2021 2021 * function initiate recovery.
2022 2022 */
2023 2023 nfs4args_copen_free(open_args);
2024 2024 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2025 2025 nfs4_end_open_seqid_sync(oop);
2026 2026 open_owner_rele(oop);
2027 2027 oop = NULL;
2028 2028 goto bailout;
2029 2029 case NFS4ERR_ACCESS:
2030 2030 if (cred_otw != cr) {
2031 2031 crfree(cred_otw);
2032 2032 cred_otw = cr;
2033 2033 crhold(cred_otw);
2034 2034 nfs4args_copen_free(open_args);
2035 2035 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2036 2036 nfs4_end_open_seqid_sync(oop);
2037 2037 open_owner_rele(oop);
2038 2038 oop = NULL;
2039 2039 goto top;
2040 2040 }
2041 2041 /* fall through */
2042 2042 default:
2043 2043 NFS4_DEBUG(nfs4_client_failover_debug, (CE_NOTE,
2044 2044 "nfs4_reopen: r_server 0x%p, mi_curr_serv 0x%p, rnode %s",
2045 2045 (void*)VTOR4(vp)->r_server, (void*)mi->mi_curr_serv,
2046 2046 rnode4info(VTOR4(vp))));
2047 2047 failed_msg = "Couldn't reopen: NFSv4 error";
2048 2048 nfs4args_copen_free(open_args);
2049 2049 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2050 2050 goto kill_file;
2051 2051 }
2052 2052
2053 2053 resop = &res.array[1]; /* open res */
2054 2054 op_res = &resop->nfs_resop4_u.opopen;
2055 2055
2056 2056 garp = &res.array[3].nfs_resop4_u.opgetattr.ga_res;
2057 2057
2058 2058 /*
2059 2059 * Check if the path we reopened really is the same
2060 2060 * file. We could end up in a situation where the file
2061 2061 * was removed and a new file created with the same name.
2062 2062 */
2063 2063 resop = &res.array[2];
2064 2064 gf_res = &resop->nfs_resop4_u.opgetfh;
2065 2065 (void) nfs_rw_enter_sig(&mi->mi_fh_lock, RW_READER, 0);
2066 2066 fh_different = (nfs4cmpfh(&rp->r_fh->sfh_fh, &gf_res->object) != 0);
2067 2067 if (fh_different) {
2068 2068 if (mi->mi_fh_expire_type == FH4_PERSISTENT ||
2069 2069 mi->mi_fh_expire_type & FH4_NOEXPIRE_WITH_OPEN) {
2070 2070 /* Oops, we don't have the same file */
2071 2071 if (mi->mi_fh_expire_type == FH4_PERSISTENT)
2072 2072 failed_msg = "Couldn't reopen: Persistent "
2073 2073 "file handle changed";
2074 2074 else
2075 2075 failed_msg = "Couldn't reopen: Volatile "
2076 2076 "(no expire on open) file handle changed";
2077 2077
2078 2078 nfs4args_copen_free(open_args);
2079 2079 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2080 2080 nfs_rw_exit(&mi->mi_fh_lock);
2081 2081 goto kill_file;
2082 2082
2083 2083 } else {
2084 2084 /*
2085 2085 * We have volatile file handles that don't compare.
2086 2086 * If the fids are the same then we assume that the
2087 2087 * file handle expired but the rnode still refers to
2088 2088 * the same file object.
2089 2089 *
2090 2090 * First check that we have fids or not.
2091 2091 * If we don't we have a dumb server so we will
2092 2092 * just assume every thing is ok for now.
2093 2093 */
2094 2094 if (!ep->error && garp->n4g_va.va_mask & AT_NODEID &&
2095 2095 rp->r_attr.va_mask & AT_NODEID &&
2096 2096 rp->r_attr.va_nodeid != garp->n4g_va.va_nodeid) {
2097 2097 /*
2098 2098 * We have fids, but they don't
2099 2099 * compare. So kill the file.
2100 2100 */
2101 2101 failed_msg =
2102 2102 "Couldn't reopen: file handle changed"
2103 2103 " due to mismatched fids";
2104 2104 nfs4args_copen_free(open_args);
2105 2105 (void) xdr_free(xdr_COMPOUND4res_clnt,
2106 2106 (caddr_t)&res);
2107 2107 nfs_rw_exit(&mi->mi_fh_lock);
2108 2108 goto kill_file;
2109 2109 } else {
2110 2110 /*
2111 2111 * We have volatile file handles that refers
2112 2112 * to the same file (at least they have the
2113 2113 * same fid) or we don't have fids so we
2114 2114 * can't tell. :(. We'll be a kind and accepting
2115 2115 * client so we'll update the rnode's file
2116 2116 * handle with the otw handle.
2117 2117 *
2118 2118 * We need to drop mi->mi_fh_lock since
2119 2119 * sh4_update acquires it. Since there is
2120 2120 * only one recovery thread there is no
2121 2121 * race.
2122 2122 */
2123 2123 nfs_rw_exit(&mi->mi_fh_lock);
2124 2124 sfh4_update(rp->r_fh, &gf_res->object);
2125 2125 }
2126 2126 }
2127 2127 } else {
2128 2128 nfs_rw_exit(&mi->mi_fh_lock);
2129 2129 }
2130 2130
2131 2131 ASSERT(nfs4_consistent_type(vp));
2132 2132
2133 2133 /*
2134 2134 * If the server wanted an OPEN_CONFIRM but that fails, just start
2135 2135 * over. Presumably if there is a persistent error it will show up
2136 2136 * when we resend the OPEN.
2137 2137 */
2138 2138 if (op_res->rflags & OPEN4_RESULT_CONFIRM) {
2139 2139 bool_t retry_open = FALSE;
2140 2140
2141 2141 nfs4open_confirm(vp, &seqid, &op_res->stateid,
2142 2142 cred_otw, is_recov, &retry_open,
2143 2143 oop, FALSE, ep, NULL);
2144 2144 if (ep->error || ep->stat) {
2145 2145 nfs4args_copen_free(open_args);
2146 2146 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2147 2147 nfs4_end_open_seqid_sync(oop);
2148 2148 open_owner_rele(oop);
2149 2149 oop = NULL;
2150 2150 goto top;
2151 2151 }
2152 2152 }
2153 2153
2154 2154 mutex_enter(&osp->os_sync_lock);
2155 2155 osp->open_stateid = op_res->stateid;
2156 2156 osp->os_delegation = 0;
2157 2157 /*
2158 2158 * Need to reset this bitfield for the possible case where we were
2159 2159 * going to OTW CLOSE the file, got a non-recoverable error, and before
2160 2160 * we could retry the CLOSE, OPENed the file again.
2161 2161 */
2162 2162 ASSERT(osp->os_open_owner->oo_seqid_inuse);
2163 2163 osp->os_final_close = 0;
2164 2164 osp->os_force_close = 0;
2165 2165 if (claim == CLAIM_DELEGATE_CUR || claim == CLAIM_PREVIOUS)
2166 2166 osp->os_dc_openacc = open_args->share_access;
2167 2167 mutex_exit(&osp->os_sync_lock);
2168 2168
2169 2169 nfs4_end_open_seqid_sync(oop);
2170 2170
2171 2171 /* accept delegation, if any */
2172 2172 nfs4_delegation_accept(rp, claim, op_res, garp, cred_otw);
2173 2173
2174 2174 nfs4args_copen_free(open_args);
2175 2175
2176 2176 nfs4_attr_cache(vp, garp, t, cr, TRUE, NULL);
2177 2177
2178 2178 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2179 2179
2180 2180 ASSERT(nfs4_consistent_type(vp));
2181 2181
2182 2182 open_owner_rele(oop);
2183 2183 crfree(cr);
2184 2184 crfree(cred_otw);
2185 2185 return;
2186 2186
2187 2187 kill_file:
2188 2188 nfs4_fail_recov(vp, failed_msg, ep->error, ep->stat);
2189 2189 failed_reopen:
2190 2190 NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE,
2191 2191 "nfs4_reopen: setting os_failed_reopen for osp %p, cr %p, rp %s",
2192 2192 (void *)osp, (void *)cr, rnode4info(rp)));
2193 2193 mutex_enter(&osp->os_sync_lock);
2194 2194 osp->os_failed_reopen = 1;
2195 2195 mutex_exit(&osp->os_sync_lock);
2196 2196 bailout:
2197 2197 if (oop != NULL) {
2198 2198 nfs4_end_open_seqid_sync(oop);
2199 2199 open_owner_rele(oop);
2200 2200 }
2201 2201 if (cr != NULL)
2202 2202 crfree(cr);
2203 2203 if (cred_otw != NULL)
2204 2204 crfree(cred_otw);
2205 2205 }
2206 2206
2207 2207 /* for . and .. OPENs */
2208 2208 /* ARGSUSED */
2209 2209 static int
2210 2210 nfs4_open_non_reg_file(vnode_t **vpp, int flag, cred_t *cr)
2211 2211 {
2212 2212 rnode4_t *rp;
2213 2213 nfs4_ga_res_t gar;
2214 2214
2215 2215 ASSERT(nfs_zone() == VTOMI4(*vpp)->mi_zone);
2216 2216
2217 2217 /*
2218 2218 * If close-to-open consistency checking is turned off or
2219 2219 * if there is no cached data, we can avoid
2220 2220 * the over the wire getattr. Otherwise, force a
2221 2221 * call to the server to get fresh attributes and to
2222 2222 * check caches. This is required for close-to-open
2223 2223 * consistency.
2224 2224 */
2225 2225 rp = VTOR4(*vpp);
2226 2226 if (VTOMI4(*vpp)->mi_flags & MI4_NOCTO ||
2227 2227 (rp->r_dir == NULL && !nfs4_has_pages(*vpp)))
2228 2228 return (0);
2229 2229
2230 2230 gar.n4g_va.va_mask = AT_ALL;
2231 2231 return (nfs4_getattr_otw(*vpp, &gar, cr, 0));
2232 2232 }
2233 2233
2234 2234 /*
2235 2235 * CLOSE a file
2236 2236 */
2237 2237 /* ARGSUSED */
2238 2238 static int
2239 2239 nfs4_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr,
2240 2240 caller_context_t *ct)
2241 2241 {
2242 2242 rnode4_t *rp;
2243 2243 int error = 0;
2244 2244 int r_error = 0;
2245 2245 int n4error = 0;
2246 2246 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
2247 2247
2248 2248 /*
2249 2249 * Remove client state for this (lockowner, file) pair.
2250 2250 * Issue otw v4 call to have the server do the same.
2251 2251 */
2252 2252
2253 2253 rp = VTOR4(vp);
2254 2254
2255 2255 /*
2256 2256 * zone_enter(2) prevents processes from changing zones with NFS files
2257 2257 * open; if we happen to get here from the wrong zone we can't do
2258 2258 * anything over the wire.
2259 2259 */
2260 2260 if (VTOMI4(vp)->mi_zone != nfs_zone()) {
2261 2261 /*
2262 2262 * We could attempt to clean up locks, except we're sure
2263 2263 * that the current process didn't acquire any locks on
2264 2264 * the file: any attempt to lock a file belong to another zone
2265 2265 * will fail, and one can't lock an NFS file and then change
2266 2266 * zones, as that fails too.
2267 2267 *
2268 2268 * Returning an error here is the sane thing to do. A
2269 2269 * subsequent call to VN_RELE() which translates to a
2270 2270 * nfs4_inactive() will clean up state: if the zone of the
2271 2271 * vnode's origin is still alive and kicking, the inactive
2272 2272 * thread will handle the request (from the correct zone), and
2273 2273 * everything (minus the OTW close call) should be OK. If the
2274 2274 * zone is going away nfs4_async_inactive() will throw away
2275 2275 * delegations, open streams and cached pages inline.
2276 2276 */
2277 2277 return (EIO);
2278 2278 }
2279 2279
2280 2280 /*
2281 2281 * If we are using local locking for this filesystem, then
2282 2282 * release all of the SYSV style record locks. Otherwise,
2283 2283 * we are doing network locking and we need to release all
2284 2284 * of the network locks. All of the locks held by this
2285 2285 * process on this file are released no matter what the
2286 2286 * incoming reference count is.
2287 2287 */
2288 2288 if (VTOMI4(vp)->mi_flags & MI4_LLOCK) {
2289 2289 cleanlocks(vp, ttoproc(curthread)->p_pid, 0);
2290 2290 cleanshares(vp, ttoproc(curthread)->p_pid);
2291 2291 } else
2292 2292 e.error = nfs4_lockrelease(vp, flag, offset, cr);
2293 2293
2294 2294 if (e.error) {
2295 2295 struct lm_sysid *lmsid;
2296 2296 lmsid = nfs4_find_sysid(VTOMI4(vp));
2297 2297 if (lmsid == NULL) {
2298 2298 DTRACE_PROBE2(unknown__sysid, int, e.error,
2299 2299 vnode_t *, vp);
2300 2300 } else {
2301 2301 cleanlocks(vp, ttoproc(curthread)->p_pid,
2302 2302 (lm_sysidt(lmsid) | LM_SYSID_CLIENT));
2303 2303 }
2304 2304 return (e.error);
2305 2305 }
2306 2306
2307 2307 if (count > 1)
2308 2308 return (0);
2309 2309
2310 2310 /*
2311 2311 * If the file has been `unlinked', then purge the
2312 2312 * DNLC so that this vnode will get reycled quicker
2313 2313 * and the .nfs* file on the server will get removed.
2314 2314 */
2315 2315 if (rp->r_unldvp != NULL)
2316 2316 dnlc_purge_vp(vp);
2317 2317
2318 2318 /*
2319 2319 * If the file was open for write and there are pages,
2320 2320 * do a synchronous flush and commit of all of the
2321 2321 * dirty and uncommitted pages.
2322 2322 */
2323 2323 ASSERT(!e.error);
2324 2324 if ((flag & FWRITE) && nfs4_has_pages(vp))
2325 2325 error = nfs4_putpage_commit(vp, 0, 0, cr);
2326 2326
2327 2327 mutex_enter(&rp->r_statelock);
2328 2328 r_error = rp->r_error;
2329 2329 rp->r_error = 0;
2330 2330 mutex_exit(&rp->r_statelock);
2331 2331
2332 2332 /*
2333 2333 * If this file type is one for which no explicit 'open' was
2334 2334 * done, then bail now (ie. no need for protocol 'close'). If
2335 2335 * there was an error w/the vm subsystem, return _that_ error,
2336 2336 * otherwise, return any errors that may've been reported via
2337 2337 * the rnode.
2338 2338 */
2339 2339 if (vp->v_type != VREG)
2340 2340 return (error ? error : r_error);
2341 2341
2342 2342 /*
2343 2343 * The sync putpage commit may have failed above, but since
2344 2344 * we're working w/a regular file, we need to do the protocol
2345 2345 * 'close' (nfs4close_one will figure out if an otw close is
2346 2346 * needed or not). Report any errors _after_ doing the protocol
2347 2347 * 'close'.
2348 2348 */
2349 2349 nfs4close_one(vp, NULL, cr, flag, NULL, &e, CLOSE_NORM, 0, 0, 0);
2350 2350 n4error = e.error ? e.error : geterrno4(e.stat);
2351 2351
2352 2352 /*
2353 2353 * Error reporting prio (Hi -> Lo)
2354 2354 *
2355 2355 * i) nfs4_putpage_commit (error)
2356 2356 * ii) rnode's (r_error)
2357 2357 * iii) nfs4close_one (n4error)
2358 2358 */
2359 2359 return (error ? error : (r_error ? r_error : n4error));
2360 2360 }
2361 2361
2362 2362 /*
2363 2363 * Initialize *lost_rqstp.
2364 2364 */
2365 2365
2366 2366 static void
2367 2367 nfs4close_save_lost_rqst(int error, nfs4_lost_rqst_t *lost_rqstp,
2368 2368 nfs4_open_owner_t *oop, nfs4_open_stream_t *osp, cred_t *cr,
2369 2369 vnode_t *vp)
2370 2370 {
2371 2371 if (error != ETIMEDOUT && error != EINTR &&
2372 2372 !NFS4_FRC_UNMT_ERR(error, vp->v_vfsp)) {
2373 2373 lost_rqstp->lr_op = 0;
2374 2374 return;
2375 2375 }
2376 2376
2377 2377 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
2378 2378 "nfs4close_save_lost_rqst: error %d", error));
2379 2379
2380 2380 lost_rqstp->lr_op = OP_CLOSE;
2381 2381 /*
2382 2382 * The vp is held and rele'd via the recovery code.
2383 2383 * See nfs4_save_lost_rqst.
2384 2384 */
2385 2385 lost_rqstp->lr_vp = vp;
2386 2386 lost_rqstp->lr_dvp = NULL;
2387 2387 lost_rqstp->lr_oop = oop;
2388 2388 lost_rqstp->lr_osp = osp;
2389 2389 ASSERT(osp != NULL);
2390 2390 ASSERT(mutex_owned(&osp->os_sync_lock));
2391 2391 osp->os_pending_close = 1;
2392 2392 lost_rqstp->lr_lop = NULL;
2393 2393 lost_rqstp->lr_cr = cr;
2394 2394 lost_rqstp->lr_flk = NULL;
2395 2395 lost_rqstp->lr_putfirst = FALSE;
2396 2396 }
2397 2397
2398 2398 /*
2399 2399 * Assumes you already have the open seqid sync grabbed as well as the
2400 2400 * 'os_sync_lock'. Note: this will release the open seqid sync and
2401 2401 * 'os_sync_lock' if client recovery starts. Calling functions have to
2402 2402 * be prepared to handle this.
2403 2403 *
2404 2404 * 'recov' is returned as 1 if the CLOSE operation detected client recovery
2405 2405 * was needed and was started, and that the calling function should retry
2406 2406 * this function; otherwise it is returned as 0.
2407 2407 *
2408 2408 * Errors are returned via the nfs4_error_t parameter.
2409 2409 */
2410 2410 static void
2411 2411 nfs4close_otw(rnode4_t *rp, cred_t *cred_otw, nfs4_open_owner_t *oop,
2412 2412 nfs4_open_stream_t *osp, int *recov, int *did_start_seqid_syncp,
2413 2413 nfs4_close_type_t close_type, nfs4_error_t *ep, int *have_sync_lockp)
2414 2414 {
2415 2415 COMPOUND4args_clnt args;
2416 2416 COMPOUND4res_clnt res;
2417 2417 CLOSE4args *close_args;
2418 2418 nfs_resop4 *resop;
2419 2419 nfs_argop4 argop[3];
2420 2420 int doqueue = 1;
2421 2421 mntinfo4_t *mi;
2422 2422 seqid4 seqid;
2423 2423 vnode_t *vp;
2424 2424 bool_t needrecov = FALSE;
2425 2425 nfs4_lost_rqst_t lost_rqst;
2426 2426 hrtime_t t;
2427 2427
2428 2428 ASSERT(nfs_zone() == VTOMI4(RTOV4(rp))->mi_zone);
2429 2429
2430 2430 ASSERT(MUTEX_HELD(&osp->os_sync_lock));
2431 2431
2432 2432 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4close_otw"));
2433 2433
2434 2434 /* Only set this to 1 if recovery is started */
2435 2435 *recov = 0;
2436 2436
2437 2437 /* do the OTW call to close the file */
2438 2438
2439 2439 if (close_type == CLOSE_RESEND)
2440 2440 args.ctag = TAG_CLOSE_LOST;
2441 2441 else if (close_type == CLOSE_AFTER_RESEND)
2442 2442 args.ctag = TAG_CLOSE_UNDO;
2443 2443 else
2444 2444 args.ctag = TAG_CLOSE;
2445 2445
2446 2446 args.array_len = 3;
2447 2447 args.array = argop;
2448 2448
2449 2449 vp = RTOV4(rp);
2450 2450
2451 2451 mi = VTOMI4(vp);
2452 2452
2453 2453 /* putfh target fh */
2454 2454 argop[0].argop = OP_CPUTFH;
2455 2455 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
2456 2456
2457 2457 argop[1].argop = OP_GETATTR;
2458 2458 argop[1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
2459 2459 argop[1].nfs_argop4_u.opgetattr.mi = mi;
2460 2460
2461 2461 argop[2].argop = OP_CLOSE;
2462 2462 close_args = &argop[2].nfs_argop4_u.opclose;
2463 2463
2464 2464 seqid = nfs4_get_open_seqid(oop) + 1;
2465 2465
2466 2466 close_args->seqid = seqid;
2467 2467 close_args->open_stateid = osp->open_stateid;
2468 2468
2469 2469 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE,
2470 2470 "nfs4close_otw: %s call, rp %s", needrecov ? "recov" : "first",
2471 2471 rnode4info(rp)));
2472 2472
2473 2473 t = gethrtime();
2474 2474
2475 2475 rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, ep);
2476 2476
2477 2477 if (!ep->error && nfs4_need_to_bump_seqid(&res)) {
2478 2478 nfs4_set_open_seqid(seqid, oop, args.ctag);
2479 2479 }
2480 2480
2481 2481 needrecov = nfs4_needs_recovery(ep, TRUE, mi->mi_vfsp);
2482 2482 if (ep->error && !needrecov) {
2483 2483 /*
2484 2484 * if there was an error and no recovery is to be done
2485 2485 * then then set up the file to flush its cache if
2486 2486 * needed for the next caller.
2487 2487 */
2488 2488 mutex_enter(&rp->r_statelock);
2489 2489 PURGE_ATTRCACHE4_LOCKED(rp);
2490 2490 rp->r_flags &= ~R4WRITEMODIFIED;
2491 2491 mutex_exit(&rp->r_statelock);
2492 2492 return;
2493 2493 }
2494 2494
2495 2495 if (needrecov) {
2496 2496 bool_t abort;
2497 2497 nfs4_bseqid_entry_t *bsep = NULL;
2498 2498
2499 2499 if (close_type != CLOSE_RESEND)
2500 2500 nfs4close_save_lost_rqst(ep->error, &lost_rqst, oop,
2501 2501 osp, cred_otw, vp);
2502 2502
2503 2503 if (!ep->error && res.status == NFS4ERR_BAD_SEQID)
2504 2504 bsep = nfs4_create_bseqid_entry(oop, NULL, vp,
2505 2505 0, args.ctag, close_args->seqid);
2506 2506
2507 2507 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
2508 2508 "nfs4close_otw: initiating recovery. error %d "
2509 2509 "res.status %d", ep->error, res.status));
2510 2510
2511 2511 /*
2512 2512 * Drop the 'os_sync_lock' here so we don't hit
2513 2513 * a potential recursive mutex_enter via an
2514 2514 * 'open_stream_hold()'.
2515 2515 */
2516 2516 mutex_exit(&osp->os_sync_lock);
2517 2517 *have_sync_lockp = 0;
2518 2518 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL, NULL,
2519 2519 (close_type != CLOSE_RESEND &&
2520 2520 lost_rqst.lr_op == OP_CLOSE) ? &lost_rqst : NULL,
2521 2521 OP_CLOSE, bsep, NULL, NULL);
2522 2522
2523 2523 /* drop open seq sync, and let the calling function regrab it */
2524 2524 nfs4_end_open_seqid_sync(oop);
2525 2525 *did_start_seqid_syncp = 0;
2526 2526
2527 2527 if (bsep)
2528 2528 kmem_free(bsep, sizeof (*bsep));
2529 2529 /*
2530 2530 * For signals, the caller wants to quit, so don't say to
2531 2531 * retry. For forced unmount, if it's a user thread, it
2532 2532 * wants to quit. If it's a recovery thread, the retry
2533 2533 * will happen higher-up on the call stack. Either way,
2534 2534 * don't say to retry.
2535 2535 */
2536 2536 if (abort == FALSE && ep->error != EINTR &&
2537 2537 !NFS4_FRC_UNMT_ERR(ep->error, mi->mi_vfsp) &&
2538 2538 close_type != CLOSE_RESEND &&
2539 2539 close_type != CLOSE_AFTER_RESEND)
2540 2540 *recov = 1;
2541 2541 else
2542 2542 *recov = 0;
2543 2543
2544 2544 if (!ep->error)
2545 2545 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2546 2546 return;
2547 2547 }
2548 2548
2549 2549 if (res.status) {
2550 2550 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2551 2551 return;
2552 2552 }
2553 2553
2554 2554 mutex_enter(&rp->r_statev4_lock);
2555 2555 rp->created_v4 = 0;
2556 2556 mutex_exit(&rp->r_statev4_lock);
2557 2557
2558 2558 resop = &res.array[2];
2559 2559 osp->open_stateid = resop->nfs_resop4_u.opclose.open_stateid;
2560 2560 osp->os_valid = 0;
2561 2561
2562 2562 /*
2563 2563 * This removes the reference obtained at OPEN; ie, when the
2564 2564 * open stream structure was created.
2565 2565 *
2566 2566 * We don't have to worry about calling 'open_stream_rele'
2567 2567 * since we our currently holding a reference to the open
2568 2568 * stream which means the count cannot go to 0 with this
2569 2569 * decrement.
2570 2570 */
2571 2571 ASSERT(osp->os_ref_count >= 2);
2572 2572 osp->os_ref_count--;
2573 2573
2574 2574 if (!ep->error)
2575 2575 nfs4_attr_cache(vp,
2576 2576 &res.array[1].nfs_resop4_u.opgetattr.ga_res,
2577 2577 t, cred_otw, TRUE, NULL);
2578 2578
2579 2579 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4close_otw:"
2580 2580 " returning %d", ep->error));
2581 2581
2582 2582 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2583 2583 }
2584 2584
2585 2585 /* ARGSUSED */
2586 2586 static int
2587 2587 nfs4_read(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr,
2588 2588 caller_context_t *ct)
2589 2589 {
2590 2590 rnode4_t *rp;
2591 2591 u_offset_t off;
2592 2592 offset_t diff;
2593 2593 uint_t on;
2594 2594 uint_t n;
2595 2595 caddr_t base;
2596 2596 uint_t flags;
2597 2597 int error;
2598 2598 mntinfo4_t *mi;
2599 2599
2600 2600 rp = VTOR4(vp);
2601 2601
2602 2602 ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER));
2603 2603
2604 2604 if (IS_SHADOW(vp, rp))
2605 2605 vp = RTOV4(rp);
2606 2606
2607 2607 if (vp->v_type != VREG)
2608 2608 return (EISDIR);
2609 2609
2610 2610 mi = VTOMI4(vp);
2611 2611
2612 2612 if (nfs_zone() != mi->mi_zone)
2613 2613 return (EIO);
2614 2614
2615 2615 if (uiop->uio_resid == 0)
2616 2616 return (0);
2617 2617
2618 2618 if (uiop->uio_loffset < 0 || uiop->uio_loffset + uiop->uio_resid < 0)
2619 2619 return (EINVAL);
2620 2620
2621 2621 mutex_enter(&rp->r_statelock);
2622 2622 if (rp->r_flags & R4RECOVERRP)
2623 2623 error = (rp->r_error ? rp->r_error : EIO);
2624 2624 else
2625 2625 error = 0;
2626 2626 mutex_exit(&rp->r_statelock);
2627 2627 if (error)
2628 2628 return (error);
2629 2629
2630 2630 /*
2631 2631 * Bypass VM if caching has been disabled (e.g., locking) or if
2632 2632 * using client-side direct I/O and the file is not mmap'd and
2633 2633 * there are no cached pages.
2634 2634 */
2635 2635 if ((vp->v_flag & VNOCACHE) ||
2636 2636 (((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO)) &&
2637 2637 rp->r_mapcnt == 0 && rp->r_inmap == 0 && !nfs4_has_pages(vp))) {
2638 2638 size_t resid = 0;
2639 2639
2640 2640 return (nfs4read(vp, NULL, uiop->uio_loffset,
2641 2641 uiop->uio_resid, &resid, cr, FALSE, uiop));
2642 2642 }
2643 2643
2644 2644 error = 0;
2645 2645
2646 2646 do {
2647 2647 off = uiop->uio_loffset & MAXBMASK; /* mapping offset */
2648 2648 on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */
2649 2649 n = MIN(MAXBSIZE - on, uiop->uio_resid);
2650 2650
2651 2651 if (error = nfs4_validate_caches(vp, cr))
2652 2652 break;
2653 2653
2654 2654 mutex_enter(&rp->r_statelock);
2655 2655 while (rp->r_flags & R4INCACHEPURGE) {
2656 2656 if (!cv_wait_sig(&rp->r_cv, &rp->r_statelock)) {
2657 2657 mutex_exit(&rp->r_statelock);
2658 2658 return (EINTR);
2659 2659 }
2660 2660 }
2661 2661 diff = rp->r_size - uiop->uio_loffset;
2662 2662 mutex_exit(&rp->r_statelock);
2663 2663 if (diff <= 0)
2664 2664 break;
2665 2665 if (diff < n)
2666 2666 n = (uint_t)diff;
2667 2667
2668 2668 if (vpm_enable) {
2669 2669 /*
2670 2670 * Copy data.
2671 2671 */
2672 2672 error = vpm_data_copy(vp, off + on, n, uiop,
2673 2673 1, NULL, 0, S_READ);
2674 2674 } else {
2675 2675 base = segmap_getmapflt(segkmap, vp, off + on, n, 1,
2676 2676 S_READ);
2677 2677
2678 2678 error = uiomove(base + on, n, UIO_READ, uiop);
2679 2679 }
2680 2680
2681 2681 if (!error) {
2682 2682 /*
2683 2683 * If read a whole block or read to eof,
2684 2684 * won't need this buffer again soon.
2685 2685 */
2686 2686 mutex_enter(&rp->r_statelock);
2687 2687 if (n + on == MAXBSIZE ||
2688 2688 uiop->uio_loffset == rp->r_size)
2689 2689 flags = SM_DONTNEED;
2690 2690 else
2691 2691 flags = 0;
2692 2692 mutex_exit(&rp->r_statelock);
2693 2693 if (vpm_enable) {
2694 2694 error = vpm_sync_pages(vp, off, n, flags);
2695 2695 } else {
2696 2696 error = segmap_release(segkmap, base, flags);
2697 2697 }
2698 2698 } else {
2699 2699 if (vpm_enable) {
2700 2700 (void) vpm_sync_pages(vp, off, n, 0);
2701 2701 } else {
2702 2702 (void) segmap_release(segkmap, base, 0);
2703 2703 }
2704 2704 }
2705 2705 } while (!error && uiop->uio_resid > 0);
2706 2706
2707 2707 return (error);
2708 2708 }
2709 2709
2710 2710 /* ARGSUSED */
2711 2711 static int
2712 2712 nfs4_write(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr,
2713 2713 caller_context_t *ct)
2714 2714 {
2715 2715 rlim64_t limit = uiop->uio_llimit;
2716 2716 rnode4_t *rp;
2717 2717 u_offset_t off;
2718 2718 caddr_t base;
2719 2719 uint_t flags;
2720 2720 int remainder;
2721 2721 size_t n;
2722 2722 int on;
2723 2723 int error;
2724 2724 int resid;
2725 2725 u_offset_t offset;
2726 2726 mntinfo4_t *mi;
2727 2727 uint_t bsize;
2728 2728
2729 2729 rp = VTOR4(vp);
2730 2730
2731 2731 if (IS_SHADOW(vp, rp))
2732 2732 vp = RTOV4(rp);
2733 2733
2734 2734 if (vp->v_type != VREG)
2735 2735 return (EISDIR);
2736 2736
2737 2737 mi = VTOMI4(vp);
2738 2738
2739 2739 if (nfs_zone() != mi->mi_zone)
2740 2740 return (EIO);
2741 2741
2742 2742 if (uiop->uio_resid == 0)
2743 2743 return (0);
2744 2744
2745 2745 mutex_enter(&rp->r_statelock);
2746 2746 if (rp->r_flags & R4RECOVERRP)
2747 2747 error = (rp->r_error ? rp->r_error : EIO);
2748 2748 else
2749 2749 error = 0;
2750 2750 mutex_exit(&rp->r_statelock);
2751 2751 if (error)
2752 2752 return (error);
2753 2753
2754 2754 if (ioflag & FAPPEND) {
2755 2755 struct vattr va;
2756 2756
2757 2757 /*
2758 2758 * Must serialize if appending.
2759 2759 */
2760 2760 if (nfs_rw_lock_held(&rp->r_rwlock, RW_READER)) {
2761 2761 nfs_rw_exit(&rp->r_rwlock);
2762 2762 if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER,
2763 2763 INTR4(vp)))
2764 2764 return (EINTR);
2765 2765 }
2766 2766
2767 2767 va.va_mask = AT_SIZE;
2768 2768 error = nfs4getattr(vp, &va, cr);
2769 2769 if (error)
2770 2770 return (error);
2771 2771 uiop->uio_loffset = va.va_size;
2772 2772 }
2773 2773
2774 2774 offset = uiop->uio_loffset + uiop->uio_resid;
2775 2775
2776 2776 if (uiop->uio_loffset < (offset_t)0 || offset < 0)
2777 2777 return (EINVAL);
2778 2778
2779 2779 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
2780 2780 limit = MAXOFFSET_T;
2781 2781
2782 2782 /*
2783 2783 * Check to make sure that the process will not exceed
2784 2784 * its limit on file size. It is okay to write up to
2785 2785 * the limit, but not beyond. Thus, the write which
2786 2786 * reaches the limit will be short and the next write
2787 2787 * will return an error.
2788 2788 */
2789 2789 remainder = 0;
2790 2790 if (offset > uiop->uio_llimit) {
2791 2791 remainder = offset - uiop->uio_llimit;
2792 2792 uiop->uio_resid = uiop->uio_llimit - uiop->uio_loffset;
2793 2793 if (uiop->uio_resid <= 0) {
2794 2794 proc_t *p = ttoproc(curthread);
2795 2795
2796 2796 uiop->uio_resid += remainder;
2797 2797 mutex_enter(&p->p_lock);
2798 2798 (void) rctl_action(rctlproc_legacy[RLIMIT_FSIZE],
2799 2799 p->p_rctls, p, RCA_UNSAFE_SIGINFO);
2800 2800 mutex_exit(&p->p_lock);
2801 2801 return (EFBIG);
2802 2802 }
2803 2803 }
2804 2804
2805 2805 /* update the change attribute, if we have a write delegation */
2806 2806
2807 2807 mutex_enter(&rp->r_statev4_lock);
2808 2808 if (rp->r_deleg_type == OPEN_DELEGATE_WRITE)
2809 2809 rp->r_deleg_change++;
2810 2810
2811 2811 mutex_exit(&rp->r_statev4_lock);
2812 2812
2813 2813 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR4(vp)))
2814 2814 return (EINTR);
2815 2815
2816 2816 /*
2817 2817 * Bypass VM if caching has been disabled (e.g., locking) or if
2818 2818 * using client-side direct I/O and the file is not mmap'd and
2819 2819 * there are no cached pages.
2820 2820 */
2821 2821 if ((vp->v_flag & VNOCACHE) ||
2822 2822 (((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO)) &&
2823 2823 rp->r_mapcnt == 0 && rp->r_inmap == 0 && !nfs4_has_pages(vp))) {
2824 2824 size_t bufsize;
2825 2825 int count;
2826 2826 u_offset_t org_offset;
2827 2827 stable_how4 stab_comm;
2828 2828 nfs4_fwrite:
2829 2829 if (rp->r_flags & R4STALE) {
2830 2830 resid = uiop->uio_resid;
2831 2831 offset = uiop->uio_loffset;
2832 2832 error = rp->r_error;
2833 2833 /*
2834 2834 * A close may have cleared r_error, if so,
2835 2835 * propagate ESTALE error return properly
2836 2836 */
2837 2837 if (error == 0)
2838 2838 error = ESTALE;
2839 2839 goto bottom;
2840 2840 }
2841 2841
2842 2842 bufsize = MIN(uiop->uio_resid, mi->mi_stsize);
2843 2843 base = kmem_alloc(bufsize, KM_SLEEP);
2844 2844 do {
2845 2845 if (ioflag & FDSYNC)
2846 2846 stab_comm = DATA_SYNC4;
2847 2847 else
2848 2848 stab_comm = FILE_SYNC4;
2849 2849 resid = uiop->uio_resid;
2850 2850 offset = uiop->uio_loffset;
2851 2851 count = MIN(uiop->uio_resid, bufsize);
2852 2852 org_offset = uiop->uio_loffset;
2853 2853 error = uiomove(base, count, UIO_WRITE, uiop);
2854 2854 if (!error) {
2855 2855 error = nfs4write(vp, base, org_offset,
2856 2856 count, cr, &stab_comm);
2857 2857 if (!error) {
2858 2858 mutex_enter(&rp->r_statelock);
2859 2859 if (rp->r_size < uiop->uio_loffset)
2860 2860 rp->r_size = uiop->uio_loffset;
2861 2861 mutex_exit(&rp->r_statelock);
2862 2862 }
2863 2863 }
2864 2864 } while (!error && uiop->uio_resid > 0);
2865 2865 kmem_free(base, bufsize);
2866 2866 goto bottom;
2867 2867 }
2868 2868
2869 2869 bsize = vp->v_vfsp->vfs_bsize;
2870 2870
2871 2871 do {
2872 2872 off = uiop->uio_loffset & MAXBMASK; /* mapping offset */
2873 2873 on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */
2874 2874 n = MIN(MAXBSIZE - on, uiop->uio_resid);
2875 2875
2876 2876 resid = uiop->uio_resid;
2877 2877 offset = uiop->uio_loffset;
2878 2878
2879 2879 if (rp->r_flags & R4STALE) {
2880 2880 error = rp->r_error;
2881 2881 /*
2882 2882 * A close may have cleared r_error, if so,
2883 2883 * propagate ESTALE error return properly
2884 2884 */
2885 2885 if (error == 0)
2886 2886 error = ESTALE;
2887 2887 break;
2888 2888 }
2889 2889
2890 2890 /*
2891 2891 * Don't create dirty pages faster than they
2892 2892 * can be cleaned so that the system doesn't
2893 2893 * get imbalanced. If the async queue is
2894 2894 * maxed out, then wait for it to drain before
2895 2895 * creating more dirty pages. Also, wait for
2896 2896 * any threads doing pagewalks in the vop_getattr
2897 2897 * entry points so that they don't block for
2898 2898 * long periods.
2899 2899 */
2900 2900 mutex_enter(&rp->r_statelock);
2901 2901 while ((mi->mi_max_threads != 0 &&
2902 2902 rp->r_awcount > 2 * mi->mi_max_threads) ||
2903 2903 rp->r_gcount > 0) {
2904 2904 if (INTR4(vp)) {
2905 2905 klwp_t *lwp = ttolwp(curthread);
2906 2906
2907 2907 if (lwp != NULL)
2908 2908 lwp->lwp_nostop++;
2909 2909 if (!cv_wait_sig(&rp->r_cv, &rp->r_statelock)) {
2910 2910 mutex_exit(&rp->r_statelock);
2911 2911 if (lwp != NULL)
2912 2912 lwp->lwp_nostop--;
2913 2913 error = EINTR;
2914 2914 goto bottom;
2915 2915 }
2916 2916 if (lwp != NULL)
2917 2917 lwp->lwp_nostop--;
2918 2918 } else
2919 2919 cv_wait(&rp->r_cv, &rp->r_statelock);
2920 2920 }
2921 2921 mutex_exit(&rp->r_statelock);
2922 2922
2923 2923 /*
2924 2924 * Touch the page and fault it in if it is not in core
2925 2925 * before segmap_getmapflt or vpm_data_copy can lock it.
2926 2926 * This is to avoid the deadlock if the buffer is mapped
2927 2927 * to the same file through mmap which we want to write.
2928 2928 */
2929 2929 uio_prefaultpages((long)n, uiop);
2930 2930
2931 2931 if (vpm_enable) {
2932 2932 /*
2933 2933 * It will use kpm mappings, so no need to
2934 2934 * pass an address.
2935 2935 */
2936 2936 error = writerp4(rp, NULL, n, uiop, 0);
2937 2937 } else {
2938 2938 if (segmap_kpm) {
2939 2939 int pon = uiop->uio_loffset & PAGEOFFSET;
2940 2940 size_t pn = MIN(PAGESIZE - pon,
2941 2941 uiop->uio_resid);
2942 2942 int pagecreate;
2943 2943
2944 2944 mutex_enter(&rp->r_statelock);
2945 2945 pagecreate = (pon == 0) && (pn == PAGESIZE ||
2946 2946 uiop->uio_loffset + pn >= rp->r_size);
2947 2947 mutex_exit(&rp->r_statelock);
2948 2948
2949 2949 base = segmap_getmapflt(segkmap, vp, off + on,
2950 2950 pn, !pagecreate, S_WRITE);
2951 2951
2952 2952 error = writerp4(rp, base + pon, n, uiop,
2953 2953 pagecreate);
2954 2954
2955 2955 } else {
2956 2956 base = segmap_getmapflt(segkmap, vp, off + on,
2957 2957 n, 0, S_READ);
2958 2958 error = writerp4(rp, base + on, n, uiop, 0);
2959 2959 }
2960 2960 }
2961 2961
2962 2962 if (!error) {
2963 2963 if (mi->mi_flags & MI4_NOAC)
2964 2964 flags = SM_WRITE;
2965 2965 else if ((uiop->uio_loffset % bsize) == 0 ||
2966 2966 IS_SWAPVP(vp)) {
2967 2967 /*
2968 2968 * Have written a whole block.
2969 2969 * Start an asynchronous write
2970 2970 * and mark the buffer to
2971 2971 * indicate that it won't be
2972 2972 * needed again soon.
2973 2973 */
2974 2974 flags = SM_WRITE | SM_ASYNC | SM_DONTNEED;
2975 2975 } else
2976 2976 flags = 0;
2977 2977 if ((ioflag & (FSYNC|FDSYNC)) ||
2978 2978 (rp->r_flags & R4OUTOFSPACE)) {
2979 2979 flags &= ~SM_ASYNC;
2980 2980 flags |= SM_WRITE;
2981 2981 }
2982 2982 if (vpm_enable) {
2983 2983 error = vpm_sync_pages(vp, off, n, flags);
2984 2984 } else {
2985 2985 error = segmap_release(segkmap, base, flags);
2986 2986 }
2987 2987 } else {
2988 2988 if (vpm_enable) {
2989 2989 (void) vpm_sync_pages(vp, off, n, 0);
2990 2990 } else {
2991 2991 (void) segmap_release(segkmap, base, 0);
2992 2992 }
2993 2993 /*
2994 2994 * In the event that we got an access error while
2995 2995 * faulting in a page for a write-only file just
2996 2996 * force a write.
2997 2997 */
2998 2998 if (error == EACCES)
2999 2999 goto nfs4_fwrite;
3000 3000 }
3001 3001 } while (!error && uiop->uio_resid > 0);
3002 3002
3003 3003 bottom:
3004 3004 if (error) {
3005 3005 uiop->uio_resid = resid + remainder;
3006 3006 uiop->uio_loffset = offset;
3007 3007 } else {
3008 3008 uiop->uio_resid += remainder;
3009 3009
3010 3010 mutex_enter(&rp->r_statev4_lock);
3011 3011 if (rp->r_deleg_type == OPEN_DELEGATE_WRITE) {
3012 3012 gethrestime(&rp->r_attr.va_mtime);
3013 3013 rp->r_attr.va_ctime = rp->r_attr.va_mtime;
3014 3014 }
3015 3015 mutex_exit(&rp->r_statev4_lock);
3016 3016 }
3017 3017
3018 3018 nfs_rw_exit(&rp->r_lkserlock);
3019 3019
3020 3020 return (error);
3021 3021 }
3022 3022
3023 3023 /*
3024 3024 * Flags are composed of {B_ASYNC, B_INVAL, B_FREE, B_DONTNEED}
3025 3025 */
3026 3026 static int
3027 3027 nfs4_rdwrlbn(vnode_t *vp, page_t *pp, u_offset_t off, size_t len,
3028 3028 int flags, cred_t *cr)
3029 3029 {
3030 3030 struct buf *bp;
3031 3031 int error;
3032 3032 page_t *savepp;
3033 3033 uchar_t fsdata;
3034 3034 stable_how4 stab_comm;
3035 3035
3036 3036 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
3037 3037 bp = pageio_setup(pp, len, vp, flags);
3038 3038 ASSERT(bp != NULL);
3039 3039
3040 3040 /*
3041 3041 * pageio_setup should have set b_addr to 0. This
3042 3042 * is correct since we want to do I/O on a page
3043 3043 * boundary. bp_mapin will use this addr to calculate
3044 3044 * an offset, and then set b_addr to the kernel virtual
3045 3045 * address it allocated for us.
3046 3046 */
3047 3047 ASSERT(bp->b_un.b_addr == 0);
3048 3048
3049 3049 bp->b_edev = 0;
3050 3050 bp->b_dev = 0;
3051 3051 bp->b_lblkno = lbtodb(off);
3052 3052 bp->b_file = vp;
3053 3053 bp->b_offset = (offset_t)off;
3054 3054 bp_mapin(bp);
3055 3055
3056 3056 if ((flags & (B_WRITE|B_ASYNC)) == (B_WRITE|B_ASYNC) &&
3057 3057 freemem > desfree)
3058 3058 stab_comm = UNSTABLE4;
3059 3059 else
3060 3060 stab_comm = FILE_SYNC4;
3061 3061
3062 3062 error = nfs4_bio(bp, &stab_comm, cr, FALSE);
3063 3063
3064 3064 bp_mapout(bp);
3065 3065 pageio_done(bp);
3066 3066
3067 3067 if (stab_comm == UNSTABLE4)
3068 3068 fsdata = C_DELAYCOMMIT;
3069 3069 else
3070 3070 fsdata = C_NOCOMMIT;
3071 3071
3072 3072 savepp = pp;
3073 3073 do {
3074 3074 pp->p_fsdata = fsdata;
3075 3075 } while ((pp = pp->p_next) != savepp);
3076 3076
3077 3077 return (error);
3078 3078 }
3079 3079
3080 3080 /*
3081 3081 */
3082 3082 static int
3083 3083 nfs4rdwr_check_osid(vnode_t *vp, nfs4_error_t *ep, cred_t *cr)
3084 3084 {
3085 3085 nfs4_open_owner_t *oop;
3086 3086 nfs4_open_stream_t *osp;
3087 3087 rnode4_t *rp = VTOR4(vp);
3088 3088 mntinfo4_t *mi = VTOMI4(vp);
3089 3089 int reopen_needed;
3090 3090
3091 3091 ASSERT(nfs_zone() == mi->mi_zone);
3092 3092
3093 3093
3094 3094 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi);
3095 3095 if (!oop)
3096 3096 return (EIO);
3097 3097
3098 3098 /* returns with 'os_sync_lock' held */
3099 3099 osp = find_open_stream(oop, rp);
3100 3100 if (!osp) {
3101 3101 open_owner_rele(oop);
3102 3102 return (EIO);
3103 3103 }
3104 3104
3105 3105 if (osp->os_failed_reopen) {
3106 3106 mutex_exit(&osp->os_sync_lock);
3107 3107 open_stream_rele(osp, rp);
3108 3108 open_owner_rele(oop);
3109 3109 return (EIO);
3110 3110 }
3111 3111
3112 3112 /*
3113 3113 * Determine whether a reopen is needed. If this
3114 3114 * is a delegation open stream, then the os_delegation bit
3115 3115 * should be set.
3116 3116 */
3117 3117
3118 3118 reopen_needed = osp->os_delegation;
3119 3119
3120 3120 mutex_exit(&osp->os_sync_lock);
3121 3121 open_owner_rele(oop);
3122 3122
3123 3123 if (reopen_needed) {
3124 3124 nfs4_error_zinit(ep);
3125 3125 nfs4_reopen(vp, osp, ep, CLAIM_NULL, FALSE, FALSE);
3126 3126 mutex_enter(&osp->os_sync_lock);
3127 3127 if (ep->error || ep->stat || osp->os_failed_reopen) {
3128 3128 mutex_exit(&osp->os_sync_lock);
3129 3129 open_stream_rele(osp, rp);
3130 3130 return (EIO);
3131 3131 }
3132 3132 mutex_exit(&osp->os_sync_lock);
3133 3133 }
3134 3134 open_stream_rele(osp, rp);
3135 3135
3136 3136 return (0);
3137 3137 }
3138 3138
3139 3139 /*
3140 3140 * Write to file. Writes to remote server in largest size
3141 3141 * chunks that the server can handle. Write is synchronous.
3142 3142 */
3143 3143 static int
3144 3144 nfs4write(vnode_t *vp, caddr_t base, u_offset_t offset, int count, cred_t *cr,
3145 3145 stable_how4 *stab_comm)
3146 3146 {
3147 3147 mntinfo4_t *mi;
3148 3148 COMPOUND4args_clnt args;
3149 3149 COMPOUND4res_clnt res;
3150 3150 WRITE4args *wargs;
3151 3151 WRITE4res *wres;
3152 3152 nfs_argop4 argop[2];
3153 3153 nfs_resop4 *resop;
3154 3154 int tsize;
3155 3155 stable_how4 stable;
3156 3156 rnode4_t *rp;
3157 3157 int doqueue = 1;
3158 3158 bool_t needrecov;
3159 3159 nfs4_recov_state_t recov_state;
3160 3160 nfs4_stateid_types_t sid_types;
3161 3161 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
3162 3162 int recov;
3163 3163
3164 3164 rp = VTOR4(vp);
3165 3165 mi = VTOMI4(vp);
3166 3166
3167 3167 ASSERT(nfs_zone() == mi->mi_zone);
3168 3168
3169 3169 stable = *stab_comm;
3170 3170 *stab_comm = FILE_SYNC4;
3171 3171
3172 3172 needrecov = FALSE;
3173 3173 recov_state.rs_flags = 0;
3174 3174 recov_state.rs_num_retry_despite_err = 0;
3175 3175 nfs4_init_stateid_types(&sid_types);
3176 3176
3177 3177 /* Is curthread the recovery thread? */
3178 3178 mutex_enter(&mi->mi_lock);
3179 3179 recov = (mi->mi_recovthread == curthread);
3180 3180 mutex_exit(&mi->mi_lock);
3181 3181
3182 3182 recov_retry:
3183 3183 args.ctag = TAG_WRITE;
3184 3184 args.array_len = 2;
3185 3185 args.array = argop;
3186 3186
3187 3187 if (!recov) {
3188 3188 e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_WRITE,
3189 3189 &recov_state, NULL);
3190 3190 if (e.error)
3191 3191 return (e.error);
3192 3192 }
3193 3193
3194 3194 /* 0. putfh target fh */
3195 3195 argop[0].argop = OP_CPUTFH;
3196 3196 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
3197 3197
3198 3198 /* 1. write */
3199 3199 nfs4args_write(&argop[1], stable, rp, cr, &wargs, &sid_types);
3200 3200
3201 3201 do {
3202 3202
3203 3203 wargs->offset = (offset4)offset;
3204 3204 wargs->data_val = base;
3205 3205
3206 3206 if (mi->mi_io_kstats) {
3207 3207 mutex_enter(&mi->mi_lock);
3208 3208 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
3209 3209 mutex_exit(&mi->mi_lock);
3210 3210 }
3211 3211
3212 3212 if ((vp->v_flag & VNOCACHE) ||
3213 3213 (rp->r_flags & R4DIRECTIO) ||
3214 3214 (mi->mi_flags & MI4_DIRECTIO))
3215 3215 tsize = MIN(mi->mi_stsize, count);
3216 3216 else
3217 3217 tsize = MIN(mi->mi_curwrite, count);
3218 3218 wargs->data_len = (uint_t)tsize;
3219 3219 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
3220 3220
3221 3221 if (mi->mi_io_kstats) {
3222 3222 mutex_enter(&mi->mi_lock);
3223 3223 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
3224 3224 mutex_exit(&mi->mi_lock);
3225 3225 }
3226 3226
3227 3227 if (!recov) {
3228 3228 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
3229 3229 if (e.error && !needrecov) {
3230 3230 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE,
3231 3231 &recov_state, needrecov);
3232 3232 return (e.error);
3233 3233 }
3234 3234 } else {
3235 3235 if (e.error)
3236 3236 return (e.error);
3237 3237 }
3238 3238
3239 3239 /*
3240 3240 * Do handling of OLD_STATEID outside
3241 3241 * of the normal recovery framework.
3242 3242 *
3243 3243 * If write receives a BAD stateid error while using a
3244 3244 * delegation stateid, retry using the open stateid (if it
3245 3245 * exists). If it doesn't have an open stateid, reopen the
3246 3246 * file first, then retry.
3247 3247 */
3248 3248 if (!e.error && res.status == NFS4ERR_OLD_STATEID &&
3249 3249 sid_types.cur_sid_type != SPEC_SID) {
3250 3250 nfs4_save_stateid(&wargs->stateid, &sid_types);
3251 3251 if (!recov)
3252 3252 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE,
3253 3253 &recov_state, needrecov);
3254 3254 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3255 3255 goto recov_retry;
3256 3256 } else if (e.error == 0 && res.status == NFS4ERR_BAD_STATEID &&
3257 3257 sid_types.cur_sid_type == DEL_SID) {
3258 3258 nfs4_save_stateid(&wargs->stateid, &sid_types);
3259 3259 mutex_enter(&rp->r_statev4_lock);
3260 3260 rp->r_deleg_return_pending = TRUE;
3261 3261 mutex_exit(&rp->r_statev4_lock);
3262 3262 if (nfs4rdwr_check_osid(vp, &e, cr)) {
3263 3263 if (!recov)
3264 3264 nfs4_end_fop(mi, vp, NULL, OH_WRITE,
3265 3265 &recov_state, needrecov);
3266 3266 (void) xdr_free(xdr_COMPOUND4res_clnt,
3267 3267 (caddr_t)&res);
3268 3268 return (EIO);
3269 3269 }
3270 3270 if (!recov)
3271 3271 nfs4_end_fop(mi, vp, NULL, OH_WRITE,
3272 3272 &recov_state, needrecov);
3273 3273 /* hold needed for nfs4delegreturn_thread */
3274 3274 VN_HOLD(vp);
3275 3275 nfs4delegreturn_async(rp, (NFS4_DR_PUSH|NFS4_DR_REOPEN|
3276 3276 NFS4_DR_DISCARD), FALSE);
3277 3277 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3278 3278 goto recov_retry;
3279 3279 }
3280 3280
3281 3281 if (needrecov) {
3282 3282 bool_t abort;
3283 3283
3284 3284 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
3285 3285 "nfs4write: client got error %d, res.status %d"
3286 3286 ", so start recovery", e.error, res.status));
3287 3287
3288 3288 abort = nfs4_start_recovery(&e,
3289 3289 VTOMI4(vp), vp, NULL, &wargs->stateid,
3290 3290 NULL, OP_WRITE, NULL, NULL, NULL);
3291 3291 if (!e.error) {
3292 3292 e.error = geterrno4(res.status);
3293 3293 (void) xdr_free(xdr_COMPOUND4res_clnt,
3294 3294 (caddr_t)&res);
3295 3295 }
3296 3296 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE,
3297 3297 &recov_state, needrecov);
3298 3298 if (abort == FALSE)
3299 3299 goto recov_retry;
3300 3300 return (e.error);
3301 3301 }
3302 3302
3303 3303 if (res.status) {
3304 3304 e.error = geterrno4(res.status);
3305 3305 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3306 3306 if (!recov)
3307 3307 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE,
3308 3308 &recov_state, needrecov);
3309 3309 return (e.error);
3310 3310 }
3311 3311
3312 3312 resop = &res.array[1]; /* write res */
3313 3313 wres = &resop->nfs_resop4_u.opwrite;
3314 3314
3315 3315 if ((int)wres->count > tsize) {
3316 3316 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3317 3317
3318 3318 zcmn_err(getzoneid(), CE_WARN,
3319 3319 "nfs4write: server wrote %u, requested was %u",
3320 3320 (int)wres->count, tsize);
3321 3321 if (!recov)
3322 3322 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE,
3323 3323 &recov_state, needrecov);
3324 3324 return (EIO);
3325 3325 }
3326 3326 if (wres->committed == UNSTABLE4) {
3327 3327 *stab_comm = UNSTABLE4;
3328 3328 if (wargs->stable == DATA_SYNC4 ||
3329 3329 wargs->stable == FILE_SYNC4) {
3330 3330 (void) xdr_free(xdr_COMPOUND4res_clnt,
3331 3331 (caddr_t)&res);
3332 3332 zcmn_err(getzoneid(), CE_WARN,
3333 3333 "nfs4write: server %s did not commit "
3334 3334 "to stable storage",
3335 3335 rp->r_server->sv_hostname);
3336 3336 if (!recov)
3337 3337 nfs4_end_fop(VTOMI4(vp), vp, NULL,
3338 3338 OH_WRITE, &recov_state, needrecov);
3339 3339 return (EIO);
3340 3340 }
3341 3341 }
3342 3342
3343 3343 tsize = (int)wres->count;
3344 3344 count -= tsize;
3345 3345 base += tsize;
3346 3346 offset += tsize;
3347 3347 if (mi->mi_io_kstats) {
3348 3348 mutex_enter(&mi->mi_lock);
3349 3349 KSTAT_IO_PTR(mi->mi_io_kstats)->writes++;
3350 3350 KSTAT_IO_PTR(mi->mi_io_kstats)->nwritten +=
3351 3351 tsize;
3352 3352 mutex_exit(&mi->mi_lock);
3353 3353 }
3354 3354 lwp_stat_update(LWP_STAT_OUBLK, 1);
3355 3355 mutex_enter(&rp->r_statelock);
3356 3356 if (rp->r_flags & R4HAVEVERF) {
3357 3357 if (rp->r_writeverf != wres->writeverf) {
3358 3358 nfs4_set_mod(vp);
3359 3359 rp->r_writeverf = wres->writeverf;
3360 3360 }
3361 3361 } else {
3362 3362 rp->r_writeverf = wres->writeverf;
3363 3363 rp->r_flags |= R4HAVEVERF;
3364 3364 }
3365 3365 PURGE_ATTRCACHE4_LOCKED(rp);
3366 3366 rp->r_flags |= R4WRITEMODIFIED;
3367 3367 gethrestime(&rp->r_attr.va_mtime);
3368 3368 rp->r_attr.va_ctime = rp->r_attr.va_mtime;
3369 3369 mutex_exit(&rp->r_statelock);
3370 3370 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3371 3371 } while (count);
3372 3372
3373 3373 if (!recov)
3374 3374 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, &recov_state,
3375 3375 needrecov);
3376 3376
3377 3377 return (e.error);
3378 3378 }
3379 3379
3380 3380 /*
3381 3381 * Read from a file. Reads data in largest chunks our interface can handle.
3382 3382 */
3383 3383 static int
3384 3384 nfs4read(vnode_t *vp, caddr_t base, offset_t offset, int count,
3385 3385 size_t *residp, cred_t *cr, bool_t async, struct uio *uiop)
3386 3386 {
3387 3387 mntinfo4_t *mi;
3388 3388 COMPOUND4args_clnt args;
3389 3389 COMPOUND4res_clnt res;
3390 3390 READ4args *rargs;
3391 3391 nfs_argop4 argop[2];
3392 3392 int tsize;
3393 3393 int doqueue;
3394 3394 rnode4_t *rp;
3395 3395 int data_len;
3396 3396 bool_t is_eof;
3397 3397 bool_t needrecov = FALSE;
3398 3398 nfs4_recov_state_t recov_state;
3399 3399 nfs4_stateid_types_t sid_types;
3400 3400 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
3401 3401
3402 3402 rp = VTOR4(vp);
3403 3403 mi = VTOMI4(vp);
3404 3404 doqueue = 1;
3405 3405
3406 3406 ASSERT(nfs_zone() == mi->mi_zone);
3407 3407
3408 3408 args.ctag = async ? TAG_READAHEAD : TAG_READ;
3409 3409
3410 3410 args.array_len = 2;
3411 3411 args.array = argop;
3412 3412
3413 3413 nfs4_init_stateid_types(&sid_types);
3414 3414
3415 3415 recov_state.rs_flags = 0;
3416 3416 recov_state.rs_num_retry_despite_err = 0;
3417 3417
3418 3418 recov_retry:
3419 3419 e.error = nfs4_start_fop(mi, vp, NULL, OH_READ,
3420 3420 &recov_state, NULL);
3421 3421 if (e.error)
3422 3422 return (e.error);
3423 3423
3424 3424 /* putfh target fh */
3425 3425 argop[0].argop = OP_CPUTFH;
3426 3426 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
3427 3427
3428 3428 /* read */
3429 3429 argop[1].argop = OP_READ;
3430 3430 rargs = &argop[1].nfs_argop4_u.opread;
3431 3431 rargs->stateid = nfs4_get_stateid(cr, rp, curproc->p_pidp->pid_id, mi,
3432 3432 OP_READ, &sid_types, async);
3433 3433
3434 3434 do {
3435 3435 if (mi->mi_io_kstats) {
3436 3436 mutex_enter(&mi->mi_lock);
3437 3437 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
3438 3438 mutex_exit(&mi->mi_lock);
3439 3439 }
3440 3440
3441 3441 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE,
3442 3442 "nfs4read: %s call, rp %s",
3443 3443 needrecov ? "recov" : "first",
3444 3444 rnode4info(rp)));
3445 3445
3446 3446 if ((vp->v_flag & VNOCACHE) ||
3447 3447 (rp->r_flags & R4DIRECTIO) ||
3448 3448 (mi->mi_flags & MI4_DIRECTIO))
3449 3449 tsize = MIN(mi->mi_tsize, count);
3450 3450 else
3451 3451 tsize = MIN(mi->mi_curread, count);
3452 3452
3453 3453 rargs->offset = (offset4)offset;
3454 3454 rargs->count = (count4)tsize;
3455 3455 rargs->res_data_val_alt = NULL;
3456 3456 rargs->res_mblk = NULL;
3457 3457 rargs->res_uiop = NULL;
3458 3458 rargs->res_maxsize = 0;
3459 3459 rargs->wlist = NULL;
3460 3460
3461 3461 if (uiop)
3462 3462 rargs->res_uiop = uiop;
3463 3463 else
3464 3464 rargs->res_data_val_alt = base;
3465 3465 rargs->res_maxsize = tsize;
3466 3466
3467 3467 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
3468 3468 #ifdef DEBUG
3469 3469 if (nfs4read_error_inject) {
3470 3470 res.status = nfs4read_error_inject;
3471 3471 nfs4read_error_inject = 0;
3472 3472 }
3473 3473 #endif
3474 3474
3475 3475 if (mi->mi_io_kstats) {
3476 3476 mutex_enter(&mi->mi_lock);
3477 3477 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
3478 3478 mutex_exit(&mi->mi_lock);
3479 3479 }
3480 3480
3481 3481 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
3482 3482 if (e.error != 0 && !needrecov) {
3483 3483 nfs4_end_fop(mi, vp, NULL, OH_READ,
3484 3484 &recov_state, needrecov);
3485 3485 return (e.error);
3486 3486 }
3487 3487
3488 3488 /*
3489 3489 * Do proper retry for OLD and BAD stateid errors outside
3490 3490 * of the normal recovery framework. There are two differences
3491 3491 * between async and sync reads. The first is that we allow
3492 3492 * retry on BAD_STATEID for async reads, but not sync reads.
3493 3493 * The second is that we mark the file dead for a failed
3494 3494 * attempt with a special stateid for sync reads, but just
3495 3495 * return EIO for async reads.
3496 3496 *
3497 3497 * If a sync read receives a BAD stateid error while using a
3498 3498 * delegation stateid, retry using the open stateid (if it
3499 3499 * exists). If it doesn't have an open stateid, reopen the
3500 3500 * file first, then retry.
3501 3501 */
3502 3502 if (e.error == 0 && (res.status == NFS4ERR_OLD_STATEID ||
3503 3503 res.status == NFS4ERR_BAD_STATEID) && async) {
3504 3504 nfs4_end_fop(mi, vp, NULL, OH_READ,
3505 3505 &recov_state, needrecov);
3506 3506 if (sid_types.cur_sid_type == SPEC_SID) {
3507 3507 (void) xdr_free(xdr_COMPOUND4res_clnt,
3508 3508 (caddr_t)&res);
3509 3509 return (EIO);
3510 3510 }
3511 3511 nfs4_save_stateid(&rargs->stateid, &sid_types);
3512 3512 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3513 3513 goto recov_retry;
3514 3514 } else if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID &&
3515 3515 !async && sid_types.cur_sid_type != SPEC_SID) {
3516 3516 nfs4_save_stateid(&rargs->stateid, &sid_types);
3517 3517 nfs4_end_fop(mi, vp, NULL, OH_READ,
3518 3518 &recov_state, needrecov);
3519 3519 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3520 3520 goto recov_retry;
3521 3521 } else if (e.error == 0 && res.status == NFS4ERR_BAD_STATEID &&
3522 3522 sid_types.cur_sid_type == DEL_SID) {
3523 3523 nfs4_save_stateid(&rargs->stateid, &sid_types);
3524 3524 mutex_enter(&rp->r_statev4_lock);
3525 3525 rp->r_deleg_return_pending = TRUE;
3526 3526 mutex_exit(&rp->r_statev4_lock);
3527 3527 if (nfs4rdwr_check_osid(vp, &e, cr)) {
3528 3528 nfs4_end_fop(mi, vp, NULL, OH_READ,
3529 3529 &recov_state, needrecov);
3530 3530 (void) xdr_free(xdr_COMPOUND4res_clnt,
3531 3531 (caddr_t)&res);
3532 3532 return (EIO);
3533 3533 }
3534 3534 nfs4_end_fop(mi, vp, NULL, OH_READ,
3535 3535 &recov_state, needrecov);
3536 3536 /* hold needed for nfs4delegreturn_thread */
3537 3537 VN_HOLD(vp);
3538 3538 nfs4delegreturn_async(rp, (NFS4_DR_PUSH|NFS4_DR_REOPEN|
3539 3539 NFS4_DR_DISCARD), FALSE);
3540 3540 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3541 3541 goto recov_retry;
3542 3542 }
3543 3543 if (needrecov) {
3544 3544 bool_t abort;
3545 3545
3546 3546 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
3547 3547 "nfs4read: initiating recovery\n"));
3548 3548 abort = nfs4_start_recovery(&e,
3549 3549 mi, vp, NULL, &rargs->stateid,
3550 3550 NULL, OP_READ, NULL, NULL, NULL);
3551 3551 nfs4_end_fop(mi, vp, NULL, OH_READ,
3552 3552 &recov_state, needrecov);
3553 3553 /*
3554 3554 * Do not retry if we got OLD_STATEID using a special
3555 3555 * stateid. This avoids looping with a broken server.
3556 3556 */
3557 3557 if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID &&
3558 3558 sid_types.cur_sid_type == SPEC_SID)
3559 3559 abort = TRUE;
3560 3560
3561 3561 if (abort == FALSE) {
3562 3562 /*
3563 3563 * Need to retry all possible stateids in
3564 3564 * case the recovery error wasn't stateid
3565 3565 * related or the stateids have become
3566 3566 * stale (server reboot).
3567 3567 */
3568 3568 nfs4_init_stateid_types(&sid_types);
3569 3569 (void) xdr_free(xdr_COMPOUND4res_clnt,
3570 3570 (caddr_t)&res);
3571 3571 goto recov_retry;
3572 3572 }
3573 3573
3574 3574 if (!e.error) {
3575 3575 e.error = geterrno4(res.status);
3576 3576 (void) xdr_free(xdr_COMPOUND4res_clnt,
3577 3577 (caddr_t)&res);
3578 3578 }
3579 3579 return (e.error);
3580 3580 }
3581 3581
3582 3582 if (res.status) {
3583 3583 e.error = geterrno4(res.status);
3584 3584 nfs4_end_fop(mi, vp, NULL, OH_READ,
3585 3585 &recov_state, needrecov);
3586 3586 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3587 3587 return (e.error);
3588 3588 }
3589 3589
3590 3590 data_len = res.array[1].nfs_resop4_u.opread.data_len;
3591 3591 count -= data_len;
3592 3592 if (base)
3593 3593 base += data_len;
3594 3594 offset += data_len;
3595 3595 if (mi->mi_io_kstats) {
3596 3596 mutex_enter(&mi->mi_lock);
3597 3597 KSTAT_IO_PTR(mi->mi_io_kstats)->reads++;
3598 3598 KSTAT_IO_PTR(mi->mi_io_kstats)->nread += data_len;
3599 3599 mutex_exit(&mi->mi_lock);
3600 3600 }
3601 3601 lwp_stat_update(LWP_STAT_INBLK, 1);
3602 3602 is_eof = res.array[1].nfs_resop4_u.opread.eof;
3603 3603 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3604 3604
3605 3605 } while (count && !is_eof);
3606 3606
3607 3607 *residp = count;
3608 3608
3609 3609 nfs4_end_fop(mi, vp, NULL, OH_READ, &recov_state, needrecov);
3610 3610
3611 3611 return (e.error);
3612 3612 }
3613 3613
3614 3614 /* ARGSUSED */
3615 3615 static int
3616 3616 nfs4_ioctl(vnode_t *vp, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp,
3617 3617 caller_context_t *ct)
3618 3618 {
3619 3619 if (nfs_zone() != VTOMI4(vp)->mi_zone)
3620 3620 return (EIO);
3621 3621 switch (cmd) {
3622 3622 case _FIODIRECTIO:
3623 3623 return (nfs4_directio(vp, (int)arg, cr));
3624 3624 default:
3625 3625 return (ENOTTY);
3626 3626 }
3627 3627 }
3628 3628
3629 3629 /* ARGSUSED */
3630 3630 int
3631 3631 nfs4_getattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr,
3632 3632 caller_context_t *ct)
3633 3633 {
3634 3634 int error;
3635 3635 rnode4_t *rp = VTOR4(vp);
3636 3636
3637 3637 if (nfs_zone() != VTOMI4(vp)->mi_zone)
3638 3638 return (EIO);
3639 3639 /*
3640 3640 * If it has been specified that the return value will
3641 3641 * just be used as a hint, and we are only being asked
3642 3642 * for size, fsid or rdevid, then return the client's
3643 3643 * notion of these values without checking to make sure
3644 3644 * that the attribute cache is up to date.
3645 3645 * The whole point is to avoid an over the wire GETATTR
3646 3646 * call.
3647 3647 */
3648 3648 if (flags & ATTR_HINT) {
3649 3649 if (!(vap->va_mask & ~(AT_SIZE | AT_FSID | AT_RDEV))) {
3650 3650 mutex_enter(&rp->r_statelock);
3651 3651 if (vap->va_mask & AT_SIZE)
3652 3652 vap->va_size = rp->r_size;
3653 3653 if (vap->va_mask & AT_FSID)
3654 3654 vap->va_fsid = rp->r_attr.va_fsid;
3655 3655 if (vap->va_mask & AT_RDEV)
3656 3656 vap->va_rdev = rp->r_attr.va_rdev;
3657 3657 mutex_exit(&rp->r_statelock);
3658 3658 return (0);
3659 3659 }
3660 3660 }
3661 3661
3662 3662 /*
3663 3663 * Only need to flush pages if asking for the mtime
3664 3664 * and if there any dirty pages or any outstanding
3665 3665 * asynchronous (write) requests for this file.
3666 3666 */
3667 3667 if (vap->va_mask & AT_MTIME) {
3668 3668 rp = VTOR4(vp);
3669 3669 if (nfs4_has_pages(vp)) {
3670 3670 mutex_enter(&rp->r_statev4_lock);
3671 3671 if (rp->r_deleg_type != OPEN_DELEGATE_WRITE) {
3672 3672 mutex_exit(&rp->r_statev4_lock);
3673 3673 if (rp->r_flags & R4DIRTY ||
3674 3674 rp->r_awcount > 0) {
3675 3675 mutex_enter(&rp->r_statelock);
3676 3676 rp->r_gcount++;
3677 3677 mutex_exit(&rp->r_statelock);
3678 3678 error =
3679 3679 nfs4_putpage(vp, (u_offset_t)0,
3680 3680 0, 0, cr, NULL);
3681 3681 mutex_enter(&rp->r_statelock);
3682 3682 if (error && (error == ENOSPC ||
3683 3683 error == EDQUOT)) {
3684 3684 if (!rp->r_error)
3685 3685 rp->r_error = error;
3686 3686 }
3687 3687 if (--rp->r_gcount == 0)
3688 3688 cv_broadcast(&rp->r_cv);
3689 3689 mutex_exit(&rp->r_statelock);
3690 3690 }
3691 3691 } else {
3692 3692 mutex_exit(&rp->r_statev4_lock);
3693 3693 }
3694 3694 }
3695 3695 }
3696 3696 return (nfs4getattr(vp, vap, cr));
3697 3697 }
3698 3698
3699 3699 int
3700 3700 nfs4_compare_modes(mode_t from_server, mode_t on_client)
3701 3701 {
3702 3702 /*
3703 3703 * If these are the only two bits cleared
3704 3704 * on the server then return 0 (OK) else
3705 3705 * return 1 (BAD).
3706 3706 */
3707 3707 on_client &= ~(S_ISUID|S_ISGID);
3708 3708 if (on_client == from_server)
3709 3709 return (0);
3710 3710 else
3711 3711 return (1);
3712 3712 }
3713 3713
3714 3714 /*ARGSUSED4*/
3715 3715 static int
3716 3716 nfs4_setattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr,
3717 3717 caller_context_t *ct)
3718 3718 {
3719 3719 int error;
3720 3720
3721 3721 if (vap->va_mask & AT_NOSET)
3722 3722 return (EINVAL);
3723 3723
3724 3724 if (nfs_zone() != VTOMI4(vp)->mi_zone)
3725 3725 return (EIO);
3726 3726
3727 3727 /*
3728 3728 * Don't call secpolicy_vnode_setattr, the client cannot
3729 3729 * use its cached attributes to make security decisions
3730 3730 * as the server may be faking mode bits or mapping uid/gid.
3731 3731 * Always just let the server to the checking.
3732 3732 * If we provide the ability to remove basic priviledges
3733 3733 * to setattr (e.g. basic without chmod) then we will
3734 3734 * need to add a check here before calling the server.
3735 3735 */
3736 3736 error = nfs4setattr(vp, vap, flags, cr, NULL);
3737 3737
3738 3738 if (error == 0 && (vap->va_mask & AT_SIZE) && vap->va_size == 0)
3739 3739 vnevent_truncate(vp, ct);
3740 3740
3741 3741 return (error);
3742 3742 }
3743 3743
3744 3744 /*
3745 3745 * To replace the "guarded" version 3 setattr, we use two types of compound
3746 3746 * setattr requests:
3747 3747 * 1. The "normal" setattr, used when the size of the file isn't being
3748 3748 * changed - { Putfh <fh>; Setattr; Getattr }/
3749 3749 * 2. If the size is changed, precede Setattr with: Getattr; Verify
3750 3750 * with only ctime as the argument. If the server ctime differs from
3751 3751 * what is cached on the client, the verify will fail, but we would
3752 3752 * already have the ctime from the preceding getattr, so just set it
3753 3753 * and retry. Thus the compound here is - { Putfh <fh>; Getattr; Verify;
3754 3754 * Setattr; Getattr }.
3755 3755 *
3756 3756 * The vsecattr_t * input parameter will be non-NULL if ACLs are being set in
3757 3757 * this setattr and NULL if they are not.
3758 3758 */
3759 3759 static int
3760 3760 nfs4setattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr,
3761 3761 vsecattr_t *vsap)
3762 3762 {
3763 3763 COMPOUND4args_clnt args;
3764 3764 COMPOUND4res_clnt res, *resp = NULL;
3765 3765 nfs4_ga_res_t *garp = NULL;
3766 3766 int numops = 3; /* { Putfh; Setattr; Getattr } */
3767 3767 nfs_argop4 argop[5];
3768 3768 int verify_argop = -1;
3769 3769 int setattr_argop = 1;
3770 3770 nfs_resop4 *resop;
3771 3771 vattr_t va;
3772 3772 rnode4_t *rp;
3773 3773 int doqueue = 1;
3774 3774 uint_t mask = vap->va_mask;
3775 3775 mode_t omode;
3776 3776 vsecattr_t *vsp;
3777 3777 timestruc_t ctime;
3778 3778 bool_t needrecov = FALSE;
3779 3779 nfs4_recov_state_t recov_state;
3780 3780 nfs4_stateid_types_t sid_types;
3781 3781 stateid4 stateid;
3782 3782 hrtime_t t;
3783 3783 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
3784 3784 servinfo4_t *svp;
3785 3785 bitmap4 supp_attrs;
3786 3786
3787 3787 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
3788 3788 rp = VTOR4(vp);
3789 3789 nfs4_init_stateid_types(&sid_types);
3790 3790
3791 3791 /*
3792 3792 * Only need to flush pages if there are any pages and
3793 3793 * if the file is marked as dirty in some fashion. The
3794 3794 * file must be flushed so that we can accurately
3795 3795 * determine the size of the file and the cached data
3796 3796 * after the SETATTR returns. A file is considered to
3797 3797 * be dirty if it is either marked with R4DIRTY, has
3798 3798 * outstanding i/o's active, or is mmap'd. In this
3799 3799 * last case, we can't tell whether there are dirty
3800 3800 * pages, so we flush just to be sure.
3801 3801 */
3802 3802 if (nfs4_has_pages(vp) &&
3803 3803 ((rp->r_flags & R4DIRTY) ||
3804 3804 rp->r_count > 0 ||
3805 3805 rp->r_mapcnt > 0)) {
3806 3806 ASSERT(vp->v_type != VCHR);
3807 3807 e.error = nfs4_putpage(vp, (offset_t)0, 0, 0, cr, NULL);
3808 3808 if (e.error && (e.error == ENOSPC || e.error == EDQUOT)) {
3809 3809 mutex_enter(&rp->r_statelock);
3810 3810 if (!rp->r_error)
3811 3811 rp->r_error = e.error;
3812 3812 mutex_exit(&rp->r_statelock);
3813 3813 }
3814 3814 }
3815 3815
3816 3816 if (mask & AT_SIZE) {
3817 3817 /*
3818 3818 * Verification setattr compound for non-deleg AT_SIZE:
3819 3819 * { Putfh; Getattr; Verify; Setattr; Getattr }
3820 3820 * Set ctime local here (outside the do_again label)
3821 3821 * so that subsequent retries (after failed VERIFY)
3822 3822 * will use ctime from GETATTR results (from failed
3823 3823 * verify compound) as VERIFY arg.
3824 3824 * If file has delegation, then VERIFY(time_metadata)
3825 3825 * is of little added value, so don't bother.
3826 3826 */
3827 3827 mutex_enter(&rp->r_statev4_lock);
3828 3828 if (rp->r_deleg_type == OPEN_DELEGATE_NONE ||
3829 3829 rp->r_deleg_return_pending) {
3830 3830 numops = 5;
3831 3831 ctime = rp->r_attr.va_ctime;
3832 3832 }
3833 3833 mutex_exit(&rp->r_statev4_lock);
3834 3834 }
3835 3835
3836 3836 recov_state.rs_flags = 0;
3837 3837 recov_state.rs_num_retry_despite_err = 0;
3838 3838
3839 3839 args.ctag = TAG_SETATTR;
3840 3840 do_again:
3841 3841 recov_retry:
3842 3842 setattr_argop = numops - 2;
3843 3843
3844 3844 args.array = argop;
3845 3845 args.array_len = numops;
3846 3846
3847 3847 e.error = nfs4_start_op(VTOMI4(vp), vp, NULL, &recov_state);
3848 3848 if (e.error)
3849 3849 return (e.error);
3850 3850
3851 3851
3852 3852 /* putfh target fh */
3853 3853 argop[0].argop = OP_CPUTFH;
3854 3854 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
3855 3855
3856 3856 if (numops == 5) {
3857 3857 /*
3858 3858 * We only care about the ctime, but need to get mtime
3859 3859 * and size for proper cache update.
3860 3860 */
3861 3861 /* getattr */
3862 3862 argop[1].argop = OP_GETATTR;
3863 3863 argop[1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
3864 3864 argop[1].nfs_argop4_u.opgetattr.mi = VTOMI4(vp);
3865 3865
3866 3866 /* verify - set later in loop */
3867 3867 verify_argop = 2;
3868 3868 }
3869 3869
3870 3870 /* setattr */
3871 3871 svp = rp->r_server;
3872 3872 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
3873 3873 supp_attrs = svp->sv_supp_attrs;
3874 3874 nfs_rw_exit(&svp->sv_lock);
3875 3875
3876 3876 nfs4args_setattr(&argop[setattr_argop], vap, vsap, flags, rp, cr,
3877 3877 supp_attrs, &e.error, &sid_types);
3878 3878 stateid = argop[setattr_argop].nfs_argop4_u.opsetattr.stateid;
3879 3879 if (e.error) {
3880 3880 /* req time field(s) overflow - return immediately */
3881 3881 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov);
3882 3882 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u.
3883 3883 opsetattr.obj_attributes);
3884 3884 return (e.error);
3885 3885 }
3886 3886 omode = rp->r_attr.va_mode;
3887 3887
3888 3888 /* getattr */
3889 3889 argop[numops-1].argop = OP_GETATTR;
3890 3890 argop[numops-1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
3891 3891 /*
3892 3892 * If we are setting the ACL (indicated only by vsap != NULL), request
3893 3893 * the ACL in this getattr. The ACL returned from this getattr will be
3894 3894 * used in updating the ACL cache.
3895 3895 */
3896 3896 if (vsap != NULL)
3897 3897 argop[numops-1].nfs_argop4_u.opgetattr.attr_request |=
3898 3898 FATTR4_ACL_MASK;
3899 3899 argop[numops-1].nfs_argop4_u.opgetattr.mi = VTOMI4(vp);
3900 3900
3901 3901 /*
3902 3902 * setattr iterates if the object size is set and the cached ctime
3903 3903 * does not match the file ctime. In that case, verify the ctime first.
3904 3904 */
3905 3905
3906 3906 do {
3907 3907 if (verify_argop != -1) {
3908 3908 /*
3909 3909 * Verify that the ctime match before doing setattr.
3910 3910 */
3911 3911 va.va_mask = AT_CTIME;
3912 3912 va.va_ctime = ctime;
3913 3913 svp = rp->r_server;
3914 3914 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
3915 3915 supp_attrs = svp->sv_supp_attrs;
3916 3916 nfs_rw_exit(&svp->sv_lock);
3917 3917 e.error = nfs4args_verify(&argop[verify_argop], &va,
3918 3918 OP_VERIFY, supp_attrs);
3919 3919 if (e.error) {
3920 3920 /* req time field(s) overflow - return */
3921 3921 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state,
3922 3922 needrecov);
3923 3923 break;
3924 3924 }
3925 3925 }
3926 3926
3927 3927 doqueue = 1;
3928 3928
3929 3929 t = gethrtime();
3930 3930
3931 3931 rfs4call(VTOMI4(vp), &args, &res, cr, &doqueue, 0, &e);
3932 3932
3933 3933 /*
3934 3934 * Purge the access cache and ACL cache if changing either the
3935 3935 * owner of the file, the group owner, or the mode. These may
3936 3936 * change the access permissions of the file, so purge old
3937 3937 * information and start over again.
3938 3938 */
3939 3939 if (mask & (AT_UID | AT_GID | AT_MODE)) {
3940 3940 (void) nfs4_access_purge_rp(rp);
3941 3941 if (rp->r_secattr != NULL) {
3942 3942 mutex_enter(&rp->r_statelock);
3943 3943 vsp = rp->r_secattr;
3944 3944 rp->r_secattr = NULL;
3945 3945 mutex_exit(&rp->r_statelock);
3946 3946 if (vsp != NULL)
3947 3947 nfs4_acl_free_cache(vsp);
3948 3948 }
3949 3949 }
3950 3950
3951 3951 /*
3952 3952 * If res.array_len == numops, then everything succeeded,
3953 3953 * except for possibly the final getattr. If only the
3954 3954 * last getattr failed, give up, and don't try recovery.
3955 3955 */
3956 3956 if (res.array_len == numops) {
3957 3957 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state,
3958 3958 needrecov);
3959 3959 if (! e.error)
3960 3960 resp = &res;
3961 3961 break;
3962 3962 }
3963 3963
3964 3964 /*
3965 3965 * if either rpc call failed or completely succeeded - done
3966 3966 */
3967 3967 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp);
3968 3968 if (e.error) {
3969 3969 PURGE_ATTRCACHE4(vp);
3970 3970 if (!needrecov) {
3971 3971 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state,
3972 3972 needrecov);
3973 3973 break;
3974 3974 }
3975 3975 }
3976 3976
3977 3977 /*
3978 3978 * Do proper retry for OLD_STATEID outside of the normal
3979 3979 * recovery framework.
3980 3980 */
3981 3981 if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID &&
3982 3982 sid_types.cur_sid_type != SPEC_SID &&
3983 3983 sid_types.cur_sid_type != NO_SID) {
3984 3984 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state,
3985 3985 needrecov);
3986 3986 nfs4_save_stateid(&stateid, &sid_types);
3987 3987 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u.
3988 3988 opsetattr.obj_attributes);
3989 3989 if (verify_argop != -1) {
3990 3990 nfs4args_verify_free(&argop[verify_argop]);
3991 3991 verify_argop = -1;
3992 3992 }
3993 3993 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3994 3994 goto recov_retry;
3995 3995 }
3996 3996
3997 3997 if (needrecov) {
3998 3998 bool_t abort;
3999 3999
4000 4000 abort = nfs4_start_recovery(&e,
4001 4001 VTOMI4(vp), vp, NULL, NULL, NULL,
4002 4002 OP_SETATTR, NULL, NULL, NULL);
4003 4003 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state,
4004 4004 needrecov);
4005 4005 /*
4006 4006 * Do not retry if we failed with OLD_STATEID using
4007 4007 * a special stateid. This is done to avoid looping
4008 4008 * with a broken server.
4009 4009 */
4010 4010 if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID &&
4011 4011 (sid_types.cur_sid_type == SPEC_SID ||
4012 4012 sid_types.cur_sid_type == NO_SID))
4013 4013 abort = TRUE;
4014 4014 if (!e.error) {
4015 4015 if (res.status == NFS4ERR_BADOWNER)
4016 4016 nfs4_log_badowner(VTOMI4(vp),
4017 4017 OP_SETATTR);
4018 4018
4019 4019 e.error = geterrno4(res.status);
4020 4020 (void) xdr_free(xdr_COMPOUND4res_clnt,
4021 4021 (caddr_t)&res);
4022 4022 }
4023 4023 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u.
4024 4024 opsetattr.obj_attributes);
4025 4025 if (verify_argop != -1) {
4026 4026 nfs4args_verify_free(&argop[verify_argop]);
4027 4027 verify_argop = -1;
4028 4028 }
4029 4029 if (abort == FALSE) {
4030 4030 /*
4031 4031 * Need to retry all possible stateids in
4032 4032 * case the recovery error wasn't stateid
4033 4033 * related or the stateids have become
4034 4034 * stale (server reboot).
4035 4035 */
4036 4036 nfs4_init_stateid_types(&sid_types);
4037 4037 goto recov_retry;
4038 4038 }
4039 4039 return (e.error);
4040 4040 }
4041 4041
4042 4042 /*
4043 4043 * Need to call nfs4_end_op before nfs4getattr to
4044 4044 * avoid potential nfs4_start_op deadlock. See RFE
4045 4045 * 4777612. Calls to nfs4_invalidate_pages() and
4046 4046 * nfs4_purge_stale_fh() might also generate over the
4047 4047 * wire calls which my cause nfs4_start_op() deadlock.
4048 4048 */
4049 4049 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov);
4050 4050
4051 4051 /*
4052 4052 * Check to update lease.
4053 4053 */
4054 4054 resp = &res;
4055 4055 if (res.status == NFS4_OK) {
4056 4056 break;
4057 4057 }
4058 4058
4059 4059 /*
4060 4060 * Check if verify failed to see if try again
4061 4061 */
4062 4062 if ((verify_argop == -1) || (res.array_len != 3)) {
4063 4063 /*
4064 4064 * can't continue...
4065 4065 */
4066 4066 if (res.status == NFS4ERR_BADOWNER)
4067 4067 nfs4_log_badowner(VTOMI4(vp), OP_SETATTR);
4068 4068
4069 4069 e.error = geterrno4(res.status);
4070 4070 } else {
4071 4071 /*
4072 4072 * When the verify request fails, the client ctime is
4073 4073 * not in sync with the server. This is the same as
4074 4074 * the version 3 "not synchronized" error, and we
4075 4075 * handle it in a similar manner (XXX do we need to???).
4076 4076 * Use the ctime returned in the first getattr for
4077 4077 * the input to the next verify.
4078 4078 * If we couldn't get the attributes, then we give up
4079 4079 * because we can't complete the operation as required.
4080 4080 */
4081 4081 garp = &res.array[1].nfs_resop4_u.opgetattr.ga_res;
4082 4082 }
4083 4083 if (e.error) {
4084 4084 PURGE_ATTRCACHE4(vp);
4085 4085 nfs4_purge_stale_fh(e.error, vp, cr);
4086 4086 } else {
4087 4087 /*
4088 4088 * retry with a new verify value
4089 4089 */
4090 4090 ctime = garp->n4g_va.va_ctime;
4091 4091 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4092 4092 resp = NULL;
4093 4093 }
4094 4094 if (!e.error) {
4095 4095 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u.
4096 4096 opsetattr.obj_attributes);
4097 4097 if (verify_argop != -1) {
4098 4098 nfs4args_verify_free(&argop[verify_argop]);
4099 4099 verify_argop = -1;
4100 4100 }
4101 4101 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4102 4102 goto do_again;
4103 4103 }
4104 4104 } while (!e.error);
4105 4105
4106 4106 if (e.error) {
4107 4107 /*
4108 4108 * If we are here, rfs4call has an irrecoverable error - return
4109 4109 */
4110 4110 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u.
4111 4111 opsetattr.obj_attributes);
4112 4112 if (verify_argop != -1) {
4113 4113 nfs4args_verify_free(&argop[verify_argop]);
4114 4114 verify_argop = -1;
4115 4115 }
4116 4116 if (resp)
4117 4117 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
4118 4118 return (e.error);
4119 4119 }
4120 4120
4121 4121
4122 4122
4123 4123 /*
4124 4124 * If changing the size of the file, invalidate
4125 4125 * any local cached data which is no longer part
4126 4126 * of the file. We also possibly invalidate the
4127 4127 * last page in the file. We could use
4128 4128 * pvn_vpzero(), but this would mark the page as
4129 4129 * modified and require it to be written back to
4130 4130 * the server for no particularly good reason.
4131 4131 * This way, if we access it, then we bring it
4132 4132 * back in. A read should be cheaper than a
4133 4133 * write.
4134 4134 */
4135 4135 if (mask & AT_SIZE) {
4136 4136 nfs4_invalidate_pages(vp, (vap->va_size & PAGEMASK), cr);
4137 4137 }
4138 4138
4139 4139 /* either no error or one of the postop getattr failed */
4140 4140
4141 4141 /*
4142 4142 * XXX Perform a simplified version of wcc checking. Instead of
4143 4143 * have another getattr to get pre-op, just purge cache if
4144 4144 * any of the ops prior to and including the getattr failed.
4145 4145 * If the getattr succeeded then update the attrcache accordingly.
4146 4146 */
4147 4147
4148 4148 garp = NULL;
4149 4149 if (res.status == NFS4_OK) {
4150 4150 /*
4151 4151 * Last getattr
4152 4152 */
4153 4153 resop = &res.array[numops - 1];
4154 4154 garp = &resop->nfs_resop4_u.opgetattr.ga_res;
4155 4155 }
4156 4156 /*
4157 4157 * In certain cases, nfs4_update_attrcache() will purge the attrcache,
4158 4158 * rather than filling it. See the function itself for details.
4159 4159 */
4160 4160 e.error = nfs4_update_attrcache(res.status, garp, t, vp, cr);
4161 4161 if (garp != NULL) {
4162 4162 if (garp->n4g_resbmap & FATTR4_ACL_MASK) {
4163 4163 nfs4_acl_fill_cache(rp, &garp->n4g_vsa);
4164 4164 vs_ace4_destroy(&garp->n4g_vsa);
4165 4165 } else {
4166 4166 if (vsap != NULL) {
4167 4167 /*
4168 4168 * The ACL was supposed to be set and to be
4169 4169 * returned in the last getattr of this
4170 4170 * compound, but for some reason the getattr
4171 4171 * result doesn't contain the ACL. In this
4172 4172 * case, purge the ACL cache.
4173 4173 */
4174 4174 if (rp->r_secattr != NULL) {
4175 4175 mutex_enter(&rp->r_statelock);
4176 4176 vsp = rp->r_secattr;
4177 4177 rp->r_secattr = NULL;
4178 4178 mutex_exit(&rp->r_statelock);
4179 4179 if (vsp != NULL)
4180 4180 nfs4_acl_free_cache(vsp);
4181 4181 }
4182 4182 }
4183 4183 }
4184 4184 }
4185 4185
4186 4186 if (res.status == NFS4_OK && (mask & AT_SIZE)) {
4187 4187 /*
4188 4188 * Set the size, rather than relying on getting it updated
4189 4189 * via a GETATTR. With delegations the client tries to
4190 4190 * suppress GETATTR calls.
4191 4191 */
4192 4192 mutex_enter(&rp->r_statelock);
4193 4193 rp->r_size = vap->va_size;
4194 4194 mutex_exit(&rp->r_statelock);
4195 4195 }
4196 4196
4197 4197 /*
4198 4198 * Can free up request args and res
4199 4199 */
4200 4200 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u.
4201 4201 opsetattr.obj_attributes);
4202 4202 if (verify_argop != -1) {
4203 4203 nfs4args_verify_free(&argop[verify_argop]);
4204 4204 verify_argop = -1;
4205 4205 }
4206 4206 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4207 4207
4208 4208 /*
4209 4209 * Some servers will change the mode to clear the setuid
4210 4210 * and setgid bits when changing the uid or gid. The
4211 4211 * client needs to compensate appropriately.
4212 4212 */
4213 4213 if (mask & (AT_UID | AT_GID)) {
4214 4214 int terror, do_setattr;
4215 4215
4216 4216 do_setattr = 0;
4217 4217 va.va_mask = AT_MODE;
4218 4218 terror = nfs4getattr(vp, &va, cr);
4219 4219 if (!terror &&
4220 4220 (((mask & AT_MODE) && va.va_mode != vap->va_mode) ||
4221 4221 (!(mask & AT_MODE) && va.va_mode != omode))) {
4222 4222 va.va_mask = AT_MODE;
4223 4223 if (mask & AT_MODE) {
4224 4224 /*
4225 4225 * We asked the mode to be changed and what
4226 4226 * we just got from the server in getattr is
4227 4227 * not what we wanted it to be, so set it now.
4228 4228 */
4229 4229 va.va_mode = vap->va_mode;
4230 4230 do_setattr = 1;
4231 4231 } else {
4232 4232 /*
4233 4233 * We did not ask the mode to be changed,
4234 4234 * Check to see that the server just cleared
4235 4235 * I_SUID and I_GUID from it. If not then
4236 4236 * set mode to omode with UID/GID cleared.
4237 4237 */
4238 4238 if (nfs4_compare_modes(va.va_mode, omode)) {
4239 4239 omode &= ~(S_ISUID|S_ISGID);
4240 4240 va.va_mode = omode;
4241 4241 do_setattr = 1;
4242 4242 }
4243 4243 }
4244 4244
4245 4245 if (do_setattr)
4246 4246 (void) nfs4setattr(vp, &va, 0, cr, NULL);
4247 4247 }
4248 4248 }
4249 4249
4250 4250 return (e.error);
4251 4251 }
4252 4252
4253 4253 /* ARGSUSED */
4254 4254 static int
4255 4255 nfs4_access(vnode_t *vp, int mode, int flags, cred_t *cr, caller_context_t *ct)
4256 4256 {
4257 4257 COMPOUND4args_clnt args;
4258 4258 COMPOUND4res_clnt res;
4259 4259 int doqueue;
4260 4260 uint32_t acc, resacc, argacc;
4261 4261 rnode4_t *rp;
4262 4262 cred_t *cred, *ncr, *ncrfree = NULL;
4263 4263 nfs4_access_type_t cacc;
4264 4264 int num_ops;
4265 4265 nfs_argop4 argop[3];
4266 4266 nfs_resop4 *resop;
4267 4267 bool_t needrecov = FALSE, do_getattr;
4268 4268 nfs4_recov_state_t recov_state;
4269 4269 int rpc_error;
4270 4270 hrtime_t t;
4271 4271 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
4272 4272 mntinfo4_t *mi = VTOMI4(vp);
4273 4273
4274 4274 if (nfs_zone() != mi->mi_zone)
4275 4275 return (EIO);
4276 4276
4277 4277 acc = 0;
4278 4278 if (mode & VREAD)
4279 4279 acc |= ACCESS4_READ;
4280 4280 if (mode & VWRITE) {
4281 4281 if ((vp->v_vfsp->vfs_flag & VFS_RDONLY) && !ISVDEV(vp->v_type))
4282 4282 return (EROFS);
4283 4283 if (vp->v_type == VDIR)
4284 4284 acc |= ACCESS4_DELETE;
4285 4285 acc |= ACCESS4_MODIFY | ACCESS4_EXTEND;
4286 4286 }
4287 4287 if (mode & VEXEC) {
4288 4288 if (vp->v_type == VDIR)
4289 4289 acc |= ACCESS4_LOOKUP;
4290 4290 else
4291 4291 acc |= ACCESS4_EXECUTE;
4292 4292 }
4293 4293
4294 4294 if (VTOR4(vp)->r_acache != NULL) {
4295 4295 e.error = nfs4_validate_caches(vp, cr);
4296 4296 if (e.error)
4297 4297 return (e.error);
4298 4298 }
4299 4299
4300 4300 rp = VTOR4(vp);
4301 4301 if (vp->v_type == VDIR)
4302 4302 argacc = ACCESS4_READ | ACCESS4_DELETE | ACCESS4_MODIFY |
4303 4303 ACCESS4_EXTEND | ACCESS4_LOOKUP;
4304 4304 else
4305 4305 argacc = ACCESS4_READ | ACCESS4_MODIFY | ACCESS4_EXTEND |
4306 4306 ACCESS4_EXECUTE;
4307 4307 recov_state.rs_flags = 0;
4308 4308 recov_state.rs_num_retry_despite_err = 0;
4309 4309
4310 4310 cred = cr;
4311 4311 /*
4312 4312 * ncr and ncrfree both initially
4313 4313 * point to the memory area returned
4314 4314 * by crnetadjust();
4315 4315 * ncrfree not NULL when exiting means
4316 4316 * that we need to release it
4317 4317 */
4318 4318 ncr = crnetadjust(cred);
4319 4319 ncrfree = ncr;
4320 4320
4321 4321 tryagain:
4322 4322 cacc = nfs4_access_check(rp, acc, cred);
4323 4323 if (cacc == NFS4_ACCESS_ALLOWED) {
4324 4324 if (ncrfree != NULL)
4325 4325 crfree(ncrfree);
4326 4326 return (0);
4327 4327 }
4328 4328 if (cacc == NFS4_ACCESS_DENIED) {
4329 4329 /*
4330 4330 * If the cred can be adjusted, try again
4331 4331 * with the new cred.
4332 4332 */
4333 4333 if (ncr != NULL) {
4334 4334 cred = ncr;
4335 4335 ncr = NULL;
4336 4336 goto tryagain;
4337 4337 }
4338 4338 if (ncrfree != NULL)
4339 4339 crfree(ncrfree);
4340 4340 return (EACCES);
4341 4341 }
4342 4342
4343 4343 recov_retry:
4344 4344 /*
4345 4345 * Don't take with r_statev4_lock here. r_deleg_type could
4346 4346 * change as soon as lock is released. Since it is an int,
4347 4347 * there is no atomicity issue.
4348 4348 */
4349 4349 do_getattr = (rp->r_deleg_type == OPEN_DELEGATE_NONE);
4350 4350 num_ops = do_getattr ? 3 : 2;
4351 4351
4352 4352 args.ctag = TAG_ACCESS;
4353 4353
4354 4354 args.array_len = num_ops;
4355 4355 args.array = argop;
4356 4356
4357 4357 if (e.error = nfs4_start_fop(mi, vp, NULL, OH_ACCESS,
4358 4358 &recov_state, NULL)) {
4359 4359 if (ncrfree != NULL)
4360 4360 crfree(ncrfree);
4361 4361 return (e.error);
4362 4362 }
4363 4363
4364 4364 /* putfh target fh */
4365 4365 argop[0].argop = OP_CPUTFH;
4366 4366 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh;
4367 4367
4368 4368 /* access */
4369 4369 argop[1].argop = OP_ACCESS;
4370 4370 argop[1].nfs_argop4_u.opaccess.access = argacc;
4371 4371
4372 4372 /* getattr */
4373 4373 if (do_getattr) {
4374 4374 argop[2].argop = OP_GETATTR;
4375 4375 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
4376 4376 argop[2].nfs_argop4_u.opgetattr.mi = mi;
4377 4377 }
4378 4378
4379 4379 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE,
4380 4380 "nfs4_access: %s call, rp %s", needrecov ? "recov" : "first",
4381 4381 rnode4info(VTOR4(vp))));
4382 4382
4383 4383 doqueue = 1;
4384 4384 t = gethrtime();
4385 4385 rfs4call(VTOMI4(vp), &args, &res, cred, &doqueue, 0, &e);
4386 4386 rpc_error = e.error;
4387 4387
4388 4388 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp);
4389 4389 if (needrecov) {
4390 4390 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
4391 4391 "nfs4_access: initiating recovery\n"));
4392 4392
4393 4393 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL,
4394 4394 NULL, OP_ACCESS, NULL, NULL, NULL) == FALSE) {
4395 4395 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_ACCESS,
4396 4396 &recov_state, needrecov);
4397 4397 if (!e.error)
4398 4398 (void) xdr_free(xdr_COMPOUND4res_clnt,
4399 4399 (caddr_t)&res);
4400 4400 goto recov_retry;
4401 4401 }
4402 4402 }
4403 4403 nfs4_end_fop(mi, vp, NULL, OH_ACCESS, &recov_state, needrecov);
4404 4404
4405 4405 if (e.error)
4406 4406 goto out;
4407 4407
4408 4408 if (res.status) {
4409 4409 e.error = geterrno4(res.status);
4410 4410 /*
4411 4411 * This might generate over the wire calls throught
4412 4412 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op()
4413 4413 * here to avoid a deadlock.
4414 4414 */
4415 4415 nfs4_purge_stale_fh(e.error, vp, cr);
4416 4416 goto out;
4417 4417 }
4418 4418 resop = &res.array[1]; /* access res */
4419 4419
4420 4420 resacc = resop->nfs_resop4_u.opaccess.access;
4421 4421
4422 4422 if (do_getattr) {
4423 4423 resop++; /* getattr res */
4424 4424 nfs4_attr_cache(vp, &resop->nfs_resop4_u.opgetattr.ga_res,
4425 4425 t, cr, FALSE, NULL);
4426 4426 }
4427 4427
4428 4428 if (!e.error) {
4429 4429 nfs4_access_cache(rp, argacc, resacc, cred);
4430 4430 /*
4431 4431 * we just cached results with cred; if cred is the
4432 4432 * adjusted credentials from crnetadjust, we do not want
4433 4433 * to release them before exiting: hence setting ncrfree
4434 4434 * to NULL
4435 4435 */
4436 4436 if (cred != cr)
4437 4437 ncrfree = NULL;
4438 4438 /* XXX check the supported bits too? */
4439 4439 if ((acc & resacc) != acc) {
4440 4440 /*
4441 4441 * The following code implements the semantic
4442 4442 * that a setuid root program has *at least* the
4443 4443 * permissions of the user that is running the
4444 4444 * program. See rfs3call() for more portions
4445 4445 * of the implementation of this functionality.
4446 4446 */
4447 4447 /* XXX-LP */
4448 4448 if (ncr != NULL) {
4449 4449 (void) xdr_free(xdr_COMPOUND4res_clnt,
4450 4450 (caddr_t)&res);
4451 4451 cred = ncr;
4452 4452 ncr = NULL;
4453 4453 goto tryagain;
4454 4454 }
4455 4455 e.error = EACCES;
4456 4456 }
4457 4457 }
4458 4458
4459 4459 out:
4460 4460 if (!rpc_error)
4461 4461 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4462 4462
4463 4463 if (ncrfree != NULL)
4464 4464 crfree(ncrfree);
4465 4465
4466 4466 return (e.error);
4467 4467 }
4468 4468
4469 4469 /* ARGSUSED */
4470 4470 static int
4471 4471 nfs4_readlink(vnode_t *vp, struct uio *uiop, cred_t *cr, caller_context_t *ct)
4472 4472 {
4473 4473 COMPOUND4args_clnt args;
4474 4474 COMPOUND4res_clnt res;
4475 4475 int doqueue;
4476 4476 rnode4_t *rp;
4477 4477 nfs_argop4 argop[3];
4478 4478 nfs_resop4 *resop;
4479 4479 READLINK4res *lr_res;
4480 4480 nfs4_ga_res_t *garp;
4481 4481 uint_t len;
4482 4482 char *linkdata;
4483 4483 bool_t needrecov = FALSE;
4484 4484 nfs4_recov_state_t recov_state;
4485 4485 hrtime_t t;
4486 4486 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
4487 4487
4488 4488 if (nfs_zone() != VTOMI4(vp)->mi_zone)
4489 4489 return (EIO);
4490 4490 /*
4491 4491 * Can't readlink anything other than a symbolic link.
4492 4492 */
4493 4493 if (vp->v_type != VLNK)
4494 4494 return (EINVAL);
4495 4495
4496 4496 rp = VTOR4(vp);
4497 4497 if (nfs4_do_symlink_cache && rp->r_symlink.contents != NULL) {
4498 4498 e.error = nfs4_validate_caches(vp, cr);
4499 4499 if (e.error)
4500 4500 return (e.error);
4501 4501 mutex_enter(&rp->r_statelock);
4502 4502 if (rp->r_symlink.contents != NULL) {
4503 4503 e.error = uiomove(rp->r_symlink.contents,
4504 4504 rp->r_symlink.len, UIO_READ, uiop);
4505 4505 mutex_exit(&rp->r_statelock);
4506 4506 return (e.error);
4507 4507 }
4508 4508 mutex_exit(&rp->r_statelock);
4509 4509 }
4510 4510 recov_state.rs_flags = 0;
4511 4511 recov_state.rs_num_retry_despite_err = 0;
4512 4512
4513 4513 recov_retry:
4514 4514 args.array_len = 3;
4515 4515 args.array = argop;
4516 4516 args.ctag = TAG_READLINK;
4517 4517
4518 4518 e.error = nfs4_start_op(VTOMI4(vp), vp, NULL, &recov_state);
4519 4519 if (e.error) {
4520 4520 return (e.error);
4521 4521 }
4522 4522
4523 4523 /* 0. putfh symlink fh */
4524 4524 argop[0].argop = OP_CPUTFH;
4525 4525 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh;
4526 4526
4527 4527 /* 1. readlink */
4528 4528 argop[1].argop = OP_READLINK;
4529 4529
4530 4530 /* 2. getattr */
4531 4531 argop[2].argop = OP_GETATTR;
4532 4532 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
4533 4533 argop[2].nfs_argop4_u.opgetattr.mi = VTOMI4(vp);
4534 4534
4535 4535 doqueue = 1;
4536 4536
4537 4537 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE,
4538 4538 "nfs4_readlink: %s call, rp %s", needrecov ? "recov" : "first",
4539 4539 rnode4info(VTOR4(vp))));
4540 4540
4541 4541 t = gethrtime();
4542 4542
4543 4543 rfs4call(VTOMI4(vp), &args, &res, cr, &doqueue, 0, &e);
4544 4544
4545 4545 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp);
4546 4546 if (needrecov) {
4547 4547 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
4548 4548 "nfs4_readlink: initiating recovery\n"));
4549 4549
4550 4550 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL,
4551 4551 NULL, OP_READLINK, NULL, NULL, NULL) == FALSE) {
4552 4552 if (!e.error)
4553 4553 (void) xdr_free(xdr_COMPOUND4res_clnt,
4554 4554 (caddr_t)&res);
4555 4555
4556 4556 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state,
4557 4557 needrecov);
4558 4558 goto recov_retry;
4559 4559 }
4560 4560 }
4561 4561
4562 4562 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov);
4563 4563
4564 4564 if (e.error)
4565 4565 return (e.error);
4566 4566
4567 4567 /*
4568 4568 * There is an path in the code below which calls
4569 4569 * nfs4_purge_stale_fh(), which may generate otw calls through
4570 4570 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op()
4571 4571 * here to avoid nfs4_start_op() deadlock.
4572 4572 */
4573 4573
4574 4574 if (res.status && (res.array_len < args.array_len)) {
4575 4575 /*
4576 4576 * either Putfh or Link failed
4577 4577 */
4578 4578 e.error = geterrno4(res.status);
4579 4579 nfs4_purge_stale_fh(e.error, vp, cr);
4580 4580 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4581 4581 return (e.error);
4582 4582 }
4583 4583
4584 4584 resop = &res.array[1]; /* readlink res */
4585 4585 lr_res = &resop->nfs_resop4_u.opreadlink;
4586 4586
4587 4587 /*
4588 4588 * treat symlink names as data
4589 4589 */
4590 4590 linkdata = utf8_to_str(&lr_res->link, &len, NULL);
4591 4591 if (linkdata != NULL) {
4592 4592 int uio_len = len - 1;
4593 4593 /* len includes null byte, which we won't uiomove */
4594 4594 e.error = uiomove(linkdata, uio_len, UIO_READ, uiop);
4595 4595 if (nfs4_do_symlink_cache && rp->r_symlink.contents == NULL) {
4596 4596 mutex_enter(&rp->r_statelock);
4597 4597 if (rp->r_symlink.contents == NULL) {
4598 4598 rp->r_symlink.contents = linkdata;
4599 4599 rp->r_symlink.len = uio_len;
4600 4600 rp->r_symlink.size = len;
4601 4601 mutex_exit(&rp->r_statelock);
4602 4602 } else {
4603 4603 mutex_exit(&rp->r_statelock);
4604 4604 kmem_free(linkdata, len);
4605 4605 }
4606 4606 } else {
4607 4607 kmem_free(linkdata, len);
4608 4608 }
4609 4609 }
4610 4610 if (res.status == NFS4_OK) {
4611 4611 resop++; /* getattr res */
4612 4612 garp = &resop->nfs_resop4_u.opgetattr.ga_res;
4613 4613 }
4614 4614 e.error = nfs4_update_attrcache(res.status, garp, t, vp, cr);
4615 4615
4616 4616 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4617 4617
4618 4618 /*
4619 4619 * The over the wire error for attempting to readlink something
4620 4620 * other than a symbolic link is ENXIO. However, we need to
4621 4621 * return EINVAL instead of ENXIO, so we map it here.
4622 4622 */
4623 4623 return (e.error == ENXIO ? EINVAL : e.error);
4624 4624 }
4625 4625
4626 4626 /*
4627 4627 * Flush local dirty pages to stable storage on the server.
4628 4628 *
4629 4629 * If FNODSYNC is specified, then there is nothing to do because
4630 4630 * metadata changes are not cached on the client before being
4631 4631 * sent to the server.
4632 4632 */
4633 4633 /* ARGSUSED */
4634 4634 static int
4635 4635 nfs4_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
4636 4636 {
4637 4637 int error;
4638 4638
4639 4639 if ((syncflag & FNODSYNC) || IS_SWAPVP(vp))
4640 4640 return (0);
4641 4641 if (nfs_zone() != VTOMI4(vp)->mi_zone)
4642 4642 return (EIO);
4643 4643 error = nfs4_putpage_commit(vp, (offset_t)0, 0, cr);
4644 4644 if (!error)
4645 4645 error = VTOR4(vp)->r_error;
4646 4646 return (error);
4647 4647 }
4648 4648
4649 4649 /*
4650 4650 * Weirdness: if the file was removed or the target of a rename
4651 4651 * operation while it was open, it got renamed instead. Here we
4652 4652 * remove the renamed file.
4653 4653 */
4654 4654 /* ARGSUSED */
4655 4655 void
4656 4656 nfs4_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
4657 4657 {
4658 4658 rnode4_t *rp;
4659 4659
4660 4660 ASSERT(vp != DNLC_NO_VNODE);
4661 4661
4662 4662 rp = VTOR4(vp);
4663 4663
4664 4664 if (IS_SHADOW(vp, rp)) {
4665 4665 sv_inactive(vp);
4666 4666 return;
4667 4667 }
4668 4668
4669 4669 /*
4670 4670 * If this is coming from the wrong zone, we let someone in the right
4671 4671 * zone take care of it asynchronously. We can get here due to
4672 4672 * VN_RELE() being called from pageout() or fsflush(). This call may
4673 4673 * potentially turn into an expensive no-op if, for instance, v_count
4674 4674 * gets incremented in the meantime, but it's still correct.
4675 4675 */
4676 4676 if (nfs_zone() != VTOMI4(vp)->mi_zone) {
4677 4677 nfs4_async_inactive(vp, cr);
4678 4678 return;
4679 4679 }
4680 4680
4681 4681 /*
4682 4682 * Some of the cleanup steps might require over-the-wire
4683 4683 * operations. Since VOP_INACTIVE can get called as a result of
4684 4684 * other over-the-wire operations (e.g., an attribute cache update
4685 4685 * can lead to a DNLC purge), doing those steps now would lead to a
4686 4686 * nested call to the recovery framework, which can deadlock. So
4687 4687 * do any over-the-wire cleanups asynchronously, in a separate
4688 4688 * thread.
4689 4689 */
4690 4690
4691 4691 mutex_enter(&rp->r_os_lock);
4692 4692 mutex_enter(&rp->r_statelock);
4693 4693 mutex_enter(&rp->r_statev4_lock);
4694 4694
4695 4695 if (vp->v_type == VREG && list_head(&rp->r_open_streams) != NULL) {
4696 4696 mutex_exit(&rp->r_statev4_lock);
4697 4697 mutex_exit(&rp->r_statelock);
4698 4698 mutex_exit(&rp->r_os_lock);
4699 4699 nfs4_async_inactive(vp, cr);
4700 4700 return;
4701 4701 }
4702 4702
4703 4703 if (rp->r_deleg_type == OPEN_DELEGATE_READ ||
4704 4704 rp->r_deleg_type == OPEN_DELEGATE_WRITE) {
4705 4705 mutex_exit(&rp->r_statev4_lock);
4706 4706 mutex_exit(&rp->r_statelock);
4707 4707 mutex_exit(&rp->r_os_lock);
4708 4708 nfs4_async_inactive(vp, cr);
4709 4709 return;
4710 4710 }
4711 4711
4712 4712 if (rp->r_unldvp != NULL) {
4713 4713 mutex_exit(&rp->r_statev4_lock);
4714 4714 mutex_exit(&rp->r_statelock);
4715 4715 mutex_exit(&rp->r_os_lock);
4716 4716 nfs4_async_inactive(vp, cr);
4717 4717 return;
4718 4718 }
4719 4719 mutex_exit(&rp->r_statev4_lock);
4720 4720 mutex_exit(&rp->r_statelock);
4721 4721 mutex_exit(&rp->r_os_lock);
4722 4722
4723 4723 rp4_addfree(rp, cr);
4724 4724 }
4725 4725
4726 4726 /*
4727 4727 * nfs4_inactive_otw - nfs4_inactive, plus over-the-wire calls to free up
4728 4728 * various bits of state. The caller must not refer to vp after this call.
4729 4729 */
4730 4730
4731 4731 void
4732 4732 nfs4_inactive_otw(vnode_t *vp, cred_t *cr)
4733 4733 {
4734 4734 rnode4_t *rp = VTOR4(vp);
4735 4735 nfs4_recov_state_t recov_state;
4736 4736 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
4737 4737 vnode_t *unldvp;
4738 4738 char *unlname;
4739 4739 cred_t *unlcred;
4740 4740 COMPOUND4args_clnt args;
4741 4741 COMPOUND4res_clnt res, *resp;
4742 4742 nfs_argop4 argop[2];
4743 4743 int doqueue;
4744 4744 #ifdef DEBUG
4745 4745 char *name;
4746 4746 #endif
4747 4747
4748 4748 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
4749 4749 ASSERT(!IS_SHADOW(vp, rp));
4750 4750
4751 4751 #ifdef DEBUG
4752 4752 name = fn_name(VTOSV(vp)->sv_name);
4753 4753 NFS4_DEBUG(nfs4_client_inactive_debug, (CE_NOTE, "nfs4_inactive_otw: "
4754 4754 "release vnode %s", name));
4755 4755 kmem_free(name, MAXNAMELEN);
4756 4756 #endif
4757 4757
4758 4758 if (vp->v_type == VREG) {
4759 4759 bool_t recov_failed = FALSE;
4760 4760
4761 4761 e.error = nfs4close_all(vp, cr);
4762 4762 if (e.error) {
4763 4763 /* Check to see if recovery failed */
4764 4764 mutex_enter(&(VTOMI4(vp)->mi_lock));
4765 4765 if (VTOMI4(vp)->mi_flags & MI4_RECOV_FAIL)
4766 4766 recov_failed = TRUE;
4767 4767 mutex_exit(&(VTOMI4(vp)->mi_lock));
4768 4768 if (!recov_failed) {
4769 4769 mutex_enter(&rp->r_statelock);
4770 4770 if (rp->r_flags & R4RECOVERR)
4771 4771 recov_failed = TRUE;
4772 4772 mutex_exit(&rp->r_statelock);
4773 4773 }
4774 4774 if (recov_failed) {
4775 4775 NFS4_DEBUG(nfs4_client_recov_debug,
4776 4776 (CE_NOTE, "nfs4_inactive_otw: "
4777 4777 "close failed (recovery failure)"));
4778 4778 }
4779 4779 }
4780 4780 }
4781 4781
4782 4782 redo:
4783 4783 if (rp->r_unldvp == NULL) {
4784 4784 rp4_addfree(rp, cr);
4785 4785 return;
4786 4786 }
4787 4787
4788 4788 /*
4789 4789 * Save the vnode pointer for the directory where the
4790 4790 * unlinked-open file got renamed, then set it to NULL
4791 4791 * to prevent another thread from getting here before
4792 4792 * we're done with the remove. While we have the
4793 4793 * statelock, make local copies of the pertinent rnode
4794 4794 * fields. If we weren't to do this in an atomic way, the
4795 4795 * the unl* fields could become inconsistent with respect
4796 4796 * to each other due to a race condition between this
4797 4797 * code and nfs_remove(). See bug report 1034328.
4798 4798 */
4799 4799 mutex_enter(&rp->r_statelock);
4800 4800 if (rp->r_unldvp == NULL) {
4801 4801 mutex_exit(&rp->r_statelock);
4802 4802 rp4_addfree(rp, cr);
4803 4803 return;
4804 4804 }
4805 4805
4806 4806 unldvp = rp->r_unldvp;
4807 4807 rp->r_unldvp = NULL;
4808 4808 unlname = rp->r_unlname;
4809 4809 rp->r_unlname = NULL;
4810 4810 unlcred = rp->r_unlcred;
4811 4811 rp->r_unlcred = NULL;
4812 4812 mutex_exit(&rp->r_statelock);
4813 4813
4814 4814 /*
4815 4815 * If there are any dirty pages left, then flush
4816 4816 * them. This is unfortunate because they just
4817 4817 * may get thrown away during the remove operation,
4818 4818 * but we have to do this for correctness.
4819 4819 */
4820 4820 if (nfs4_has_pages(vp) &&
4821 4821 ((rp->r_flags & R4DIRTY) || rp->r_count > 0)) {
4822 4822 ASSERT(vp->v_type != VCHR);
4823 4823 e.error = nfs4_putpage(vp, (u_offset_t)0, 0, 0, cr, NULL);
4824 4824 if (e.error) {
4825 4825 mutex_enter(&rp->r_statelock);
4826 4826 if (!rp->r_error)
4827 4827 rp->r_error = e.error;
4828 4828 mutex_exit(&rp->r_statelock);
4829 4829 }
4830 4830 }
4831 4831
4832 4832 recov_state.rs_flags = 0;
4833 4833 recov_state.rs_num_retry_despite_err = 0;
4834 4834 recov_retry_remove:
4835 4835 /*
4836 4836 * Do the remove operation on the renamed file
4837 4837 */
4838 4838 args.ctag = TAG_INACTIVE;
4839 4839
4840 4840 /*
4841 4841 * Remove ops: putfh dir; remove
4842 4842 */
4843 4843 args.array_len = 2;
4844 4844 args.array = argop;
4845 4845
4846 4846 e.error = nfs4_start_op(VTOMI4(unldvp), unldvp, NULL, &recov_state);
4847 4847 if (e.error) {
4848 4848 kmem_free(unlname, MAXNAMELEN);
4849 4849 crfree(unlcred);
4850 4850 VN_RELE(unldvp);
4851 4851 /*
4852 4852 * Try again; this time around r_unldvp will be NULL, so we'll
4853 4853 * just call rp4_addfree() and return.
4854 4854 */
4855 4855 goto redo;
4856 4856 }
4857 4857
4858 4858 /* putfh directory */
4859 4859 argop[0].argop = OP_CPUTFH;
4860 4860 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(unldvp)->r_fh;
4861 4861
4862 4862 /* remove */
4863 4863 argop[1].argop = OP_CREMOVE;
4864 4864 argop[1].nfs_argop4_u.opcremove.ctarget = unlname;
4865 4865
4866 4866 doqueue = 1;
4867 4867 resp = &res;
4868 4868
4869 4869 #if 0 /* notyet */
4870 4870 /*
4871 4871 * Can't do this yet. We may be being called from
4872 4872 * dnlc_purge_XXX while that routine is holding a
4873 4873 * mutex lock to the nc_rele list. The calls to
4874 4874 * nfs3_cache_wcc_data may result in calls to
4875 4875 * dnlc_purge_XXX. This will result in a deadlock.
4876 4876 */
4877 4877 rfs4call(VTOMI4(unldvp), &args, &res, unlcred, &doqueue, 0, &e);
4878 4878 if (e.error) {
4879 4879 PURGE_ATTRCACHE4(unldvp);
4880 4880 resp = NULL;
4881 4881 } else if (res.status) {
4882 4882 e.error = geterrno4(res.status);
4883 4883 PURGE_ATTRCACHE4(unldvp);
4884 4884 /*
4885 4885 * This code is inactive right now
4886 4886 * but if made active there should
4887 4887 * be a nfs4_end_op() call before
4888 4888 * nfs4_purge_stale_fh to avoid start_op()
4889 4889 * deadlock. See BugId: 4948726
4890 4890 */
4891 4891 nfs4_purge_stale_fh(error, unldvp, cr);
4892 4892 } else {
4893 4893 nfs_resop4 *resop;
4894 4894 REMOVE4res *rm_res;
4895 4895
4896 4896 resop = &res.array[1];
4897 4897 rm_res = &resop->nfs_resop4_u.opremove;
4898 4898 /*
4899 4899 * Update directory cache attribute,
4900 4900 * readdir and dnlc caches.
4901 4901 */
4902 4902 nfs4_update_dircaches(&rm_res->cinfo, unldvp, NULL, NULL, NULL);
4903 4903 }
4904 4904 #else
4905 4905 rfs4call(VTOMI4(unldvp), &args, &res, unlcred, &doqueue, 0, &e);
4906 4906
4907 4907 PURGE_ATTRCACHE4(unldvp);
4908 4908 #endif
4909 4909
4910 4910 if (nfs4_needs_recovery(&e, FALSE, unldvp->v_vfsp)) {
4911 4911 if (nfs4_start_recovery(&e, VTOMI4(unldvp), unldvp, NULL,
4912 4912 NULL, NULL, OP_REMOVE, NULL, NULL, NULL) == FALSE) {
4913 4913 if (!e.error)
4914 4914 (void) xdr_free(xdr_COMPOUND4res_clnt,
4915 4915 (caddr_t)&res);
4916 4916 nfs4_end_op(VTOMI4(unldvp), unldvp, NULL,
4917 4917 &recov_state, TRUE);
4918 4918 goto recov_retry_remove;
4919 4919 }
4920 4920 }
4921 4921 nfs4_end_op(VTOMI4(unldvp), unldvp, NULL, &recov_state, FALSE);
4922 4922
4923 4923 /*
4924 4924 * Release stuff held for the remove
4925 4925 */
4926 4926 VN_RELE(unldvp);
4927 4927 if (!e.error && resp)
4928 4928 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
4929 4929
4930 4930 kmem_free(unlname, MAXNAMELEN);
4931 4931 crfree(unlcred);
4932 4932 goto redo;
4933 4933 }
4934 4934
4935 4935 /*
4936 4936 * Remote file system operations having to do with directory manipulation.
4937 4937 */
4938 4938 /* ARGSUSED3 */
4939 4939 int
4940 4940 nfs4_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
4941 4941 int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
4942 4942 int *direntflags, pathname_t *realpnp)
4943 4943 {
4944 4944 int error;
4945 4945 vnode_t *vp, *avp = NULL;
4946 4946 rnode4_t *drp;
4947 4947
4948 4948 *vpp = NULL;
4949 4949 if (nfs_zone() != VTOMI4(dvp)->mi_zone)
4950 4950 return (EPERM);
4951 4951 /*
4952 4952 * if LOOKUP_XATTR, must replace dvp (object) with
4953 4953 * object's attrdir before continuing with lookup
4954 4954 */
4955 4955 if (flags & LOOKUP_XATTR) {
4956 4956 error = nfs4lookup_xattr(dvp, nm, &avp, flags, cr);
4957 4957 if (error)
4958 4958 return (error);
4959 4959
4960 4960 dvp = avp;
4961 4961
4962 4962 /*
4963 4963 * If lookup is for "", just return dvp now. The attrdir
4964 4964 * has already been activated (from nfs4lookup_xattr), and
4965 4965 * the caller will RELE the original dvp -- not
4966 4966 * the attrdir. So, set vpp and return.
4967 4967 * Currently, when the LOOKUP_XATTR flag is
4968 4968 * passed to VOP_LOOKUP, the name is always empty, and
4969 4969 * shortcircuiting here avoids 3 unneeded lock/unlock
4970 4970 * pairs.
4971 4971 *
4972 4972 * If a non-empty name was provided, then it is the
4973 4973 * attribute name, and it will be looked up below.
4974 4974 */
4975 4975 if (*nm == '\0') {
4976 4976 *vpp = dvp;
4977 4977 return (0);
4978 4978 }
4979 4979
4980 4980 /*
4981 4981 * The vfs layer never sends a name when asking for the
4982 4982 * attrdir, so we should never get here (unless of course
4983 4983 * name is passed at some time in future -- at which time
4984 4984 * we'll blow up here).
4985 4985 */
4986 4986 ASSERT(0);
4987 4987 }
4988 4988
4989 4989 drp = VTOR4(dvp);
4990 4990 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp)))
4991 4991 return (EINTR);
4992 4992
4993 4993 error = nfs4lookup(dvp, nm, vpp, cr, 0);
4994 4994 nfs_rw_exit(&drp->r_rwlock);
4995 4995
4996 4996 /*
4997 4997 * If vnode is a device, create special vnode.
4998 4998 */
4999 4999 if (!error && ISVDEV((*vpp)->v_type)) {
5000 5000 vp = *vpp;
5001 5001 *vpp = specvp(vp, vp->v_rdev, vp->v_type, cr);
5002 5002 VN_RELE(vp);
5003 5003 }
5004 5004
5005 5005 return (error);
5006 5006 }
5007 5007
5008 5008 /* ARGSUSED */
5009 5009 static int
5010 5010 nfs4lookup_xattr(vnode_t *dvp, char *nm, vnode_t **vpp, int flags, cred_t *cr)
5011 5011 {
5012 5012 int error;
5013 5013 rnode4_t *drp;
5014 5014 int cflag = ((flags & CREATE_XATTR_DIR) != 0);
5015 5015 mntinfo4_t *mi;
5016 5016
5017 5017 mi = VTOMI4(dvp);
5018 5018 if (!(mi->mi_vfsp->vfs_flag & VFS_XATTR) &&
5019 5019 !vfs_has_feature(mi->mi_vfsp, VFSFT_SYSATTR_VIEWS))
5020 5020 return (EINVAL);
5021 5021
5022 5022 drp = VTOR4(dvp);
5023 5023 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp)))
5024 5024 return (EINTR);
5025 5025
5026 5026 mutex_enter(&drp->r_statelock);
5027 5027 /*
5028 5028 * If the server doesn't support xattrs just return EINVAL
5029 5029 */
5030 5030 if (drp->r_xattr_dir == NFS4_XATTR_DIR_NOTSUPP) {
5031 5031 mutex_exit(&drp->r_statelock);
5032 5032 nfs_rw_exit(&drp->r_rwlock);
5033 5033 return (EINVAL);
5034 5034 }
5035 5035
5036 5036 /*
5037 5037 * If there is a cached xattr directory entry,
5038 5038 * use it as long as the attributes are valid. If the
5039 5039 * attributes are not valid, take the simple approach and
5040 5040 * free the cached value and re-fetch a new value.
5041 5041 *
5042 5042 * We don't negative entry cache for now, if we did we
5043 5043 * would need to check if the file has changed on every
5044 5044 * lookup. But xattrs don't exist very often and failing
5045 5045 * an openattr is not much more expensive than and NVERIFY or GETATTR
5046 5046 * so do an openattr over the wire for now.
5047 5047 */
5048 5048 if (drp->r_xattr_dir != NULL) {
5049 5049 if (ATTRCACHE4_VALID(dvp)) {
5050 5050 VN_HOLD(drp->r_xattr_dir);
5051 5051 *vpp = drp->r_xattr_dir;
5052 5052 mutex_exit(&drp->r_statelock);
5053 5053 nfs_rw_exit(&drp->r_rwlock);
5054 5054 return (0);
5055 5055 }
5056 5056 VN_RELE(drp->r_xattr_dir);
5057 5057 drp->r_xattr_dir = NULL;
5058 5058 }
5059 5059 mutex_exit(&drp->r_statelock);
5060 5060
5061 5061 error = nfs4openattr(dvp, vpp, cflag, cr);
5062 5062
5063 5063 nfs_rw_exit(&drp->r_rwlock);
5064 5064
5065 5065 return (error);
5066 5066 }
5067 5067
5068 5068 static int
5069 5069 nfs4lookup(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr, int skipdnlc)
5070 5070 {
5071 5071 int error;
5072 5072 rnode4_t *drp;
5073 5073
5074 5074 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
5075 5075
5076 5076 /*
5077 5077 * If lookup is for "", just return dvp. Don't need
5078 5078 * to send it over the wire, look it up in the dnlc,
5079 5079 * or perform any access checks.
5080 5080 */
5081 5081 if (*nm == '\0') {
5082 5082 VN_HOLD(dvp);
5083 5083 *vpp = dvp;
5084 5084 return (0);
5085 5085 }
5086 5086
5087 5087 /*
5088 5088 * Can't do lookups in non-directories.
5089 5089 */
5090 5090 if (dvp->v_type != VDIR)
5091 5091 return (ENOTDIR);
5092 5092
5093 5093 /*
5094 5094 * If lookup is for ".", just return dvp. Don't need
5095 5095 * to send it over the wire or look it up in the dnlc,
5096 5096 * just need to check access.
5097 5097 */
5098 5098 if (nm[0] == '.' && nm[1] == '\0') {
5099 5099 error = nfs4_access(dvp, VEXEC, 0, cr, NULL);
5100 5100 if (error)
5101 5101 return (error);
5102 5102 VN_HOLD(dvp);
5103 5103 *vpp = dvp;
5104 5104 return (0);
5105 5105 }
5106 5106
5107 5107 drp = VTOR4(dvp);
5108 5108 if (!(drp->r_flags & R4LOOKUP)) {
5109 5109 mutex_enter(&drp->r_statelock);
5110 5110 drp->r_flags |= R4LOOKUP;
5111 5111 mutex_exit(&drp->r_statelock);
5112 5112 }
5113 5113
5114 5114 *vpp = NULL;
5115 5115 /*
5116 5116 * Lookup this name in the DNLC. If there is no entry
5117 5117 * lookup over the wire.
5118 5118 */
5119 5119 if (!skipdnlc)
5120 5120 *vpp = dnlc_lookup(dvp, nm);
5121 5121 if (*vpp == NULL) {
5122 5122 /*
5123 5123 * We need to go over the wire to lookup the name.
5124 5124 */
5125 5125 return (nfs4lookupnew_otw(dvp, nm, vpp, cr));
5126 5126 }
5127 5127
5128 5128 /*
5129 5129 * We hit on the dnlc
5130 5130 */
5131 5131 if (*vpp != DNLC_NO_VNODE ||
5132 5132 (dvp->v_vfsp->vfs_flag & VFS_RDONLY)) {
5133 5133 /*
5134 5134 * But our attrs may not be valid.
5135 5135 */
5136 5136 if (ATTRCACHE4_VALID(dvp)) {
5137 5137 error = nfs4_waitfor_purge_complete(dvp);
5138 5138 if (error) {
5139 5139 VN_RELE(*vpp);
5140 5140 *vpp = NULL;
5141 5141 return (error);
5142 5142 }
5143 5143
5144 5144 /*
5145 5145 * If after the purge completes, check to make sure
5146 5146 * our attrs are still valid.
5147 5147 */
5148 5148 if (ATTRCACHE4_VALID(dvp)) {
5149 5149 /*
5150 5150 * If we waited for a purge we may have
5151 5151 * lost our vnode so look it up again.
5152 5152 */
5153 5153 VN_RELE(*vpp);
5154 5154 *vpp = dnlc_lookup(dvp, nm);
5155 5155 if (*vpp == NULL)
5156 5156 return (nfs4lookupnew_otw(dvp,
5157 5157 nm, vpp, cr));
5158 5158
5159 5159 /*
5160 5160 * The access cache should almost always hit
5161 5161 */
5162 5162 error = nfs4_access(dvp, VEXEC, 0, cr, NULL);
5163 5163
5164 5164 if (error) {
5165 5165 VN_RELE(*vpp);
5166 5166 *vpp = NULL;
5167 5167 return (error);
5168 5168 }
5169 5169 if (*vpp == DNLC_NO_VNODE) {
5170 5170 VN_RELE(*vpp);
5171 5171 *vpp = NULL;
5172 5172 return (ENOENT);
5173 5173 }
5174 5174 return (0);
5175 5175 }
5176 5176 }
5177 5177 }
5178 5178
5179 5179 ASSERT(*vpp != NULL);
5180 5180
5181 5181 /*
5182 5182 * We may have gotten here we have one of the following cases:
5183 5183 * 1) vpp != DNLC_NO_VNODE, our attrs have timed out so we
5184 5184 * need to validate them.
5185 5185 * 2) vpp == DNLC_NO_VNODE, a negative entry that we always
5186 5186 * must validate.
5187 5187 *
5188 5188 * Go to the server and check if the directory has changed, if
5189 5189 * it hasn't we are done and can use the dnlc entry.
5190 5190 */
5191 5191 return (nfs4lookupvalidate_otw(dvp, nm, vpp, cr));
5192 5192 }
5193 5193
5194 5194 /*
5195 5195 * Go to the server and check if the directory has changed, if
5196 5196 * it hasn't we are done and can use the dnlc entry. If it
5197 5197 * has changed we get a new copy of its attributes and check
5198 5198 * the access for VEXEC, then relookup the filename and
5199 5199 * get its filehandle and attributes.
5200 5200 *
5201 5201 * PUTFH dfh NVERIFY GETATTR ACCESS LOOKUP GETFH GETATTR
5202 5202 * if the NVERIFY failed we must
5203 5203 * purge the caches
5204 5204 * cache new attributes (will set r_time_attr_inval)
5205 5205 * cache new access
5206 5206 * recheck VEXEC access
5207 5207 * add name to dnlc, possibly negative
5208 5208 * if LOOKUP succeeded
5209 5209 * cache new attributes
5210 5210 * else
5211 5211 * set a new r_time_attr_inval for dvp
5212 5212 * check to make sure we have access
5213 5213 *
5214 5214 * The vpp returned is the vnode passed in if the directory is valid,
5215 5215 * a new vnode if successful lookup, or NULL on error.
5216 5216 */
5217 5217 static int
5218 5218 nfs4lookupvalidate_otw(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr)
5219 5219 {
5220 5220 COMPOUND4args_clnt args;
5221 5221 COMPOUND4res_clnt res;
5222 5222 fattr4 *ver_fattr;
5223 5223 fattr4_change dchange;
5224 5224 int32_t *ptr;
5225 5225 int argoplist_size = 7 * sizeof (nfs_argop4);
5226 5226 nfs_argop4 *argop;
5227 5227 int doqueue;
5228 5228 mntinfo4_t *mi;
5229 5229 nfs4_recov_state_t recov_state;
5230 5230 hrtime_t t;
5231 5231 int isdotdot;
5232 5232 vnode_t *nvp;
5233 5233 nfs_fh4 *fhp;
5234 5234 nfs4_sharedfh_t *sfhp;
5235 5235 nfs4_access_type_t cacc;
5236 5236 rnode4_t *nrp;
5237 5237 rnode4_t *drp = VTOR4(dvp);
5238 5238 nfs4_ga_res_t *garp = NULL;
5239 5239 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
5240 5240
5241 5241 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
5242 5242 ASSERT(nm != NULL);
5243 5243 ASSERT(nm[0] != '\0');
5244 5244 ASSERT(dvp->v_type == VDIR);
5245 5245 ASSERT(nm[0] != '.' || nm[1] != '\0');
5246 5246 ASSERT(*vpp != NULL);
5247 5247
5248 5248 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') {
5249 5249 isdotdot = 1;
5250 5250 args.ctag = TAG_LOOKUP_VPARENT;
5251 5251 } else {
5252 5252 /*
5253 5253 * If dvp were a stub, it should have triggered and caused
5254 5254 * a mount for us to get this far.
5255 5255 */
5256 5256 ASSERT(!RP_ISSTUB(VTOR4(dvp)));
5257 5257
5258 5258 isdotdot = 0;
5259 5259 args.ctag = TAG_LOOKUP_VALID;
5260 5260 }
5261 5261
5262 5262 mi = VTOMI4(dvp);
5263 5263 recov_state.rs_flags = 0;
5264 5264 recov_state.rs_num_retry_despite_err = 0;
5265 5265
5266 5266 nvp = NULL;
5267 5267
5268 5268 /* Save the original mount point security information */
5269 5269 (void) save_mnt_secinfo(mi->mi_curr_serv);
5270 5270
5271 5271 recov_retry:
5272 5272 e.error = nfs4_start_fop(mi, dvp, NULL, OH_LOOKUP,
5273 5273 &recov_state, NULL);
5274 5274 if (e.error) {
5275 5275 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5276 5276 VN_RELE(*vpp);
5277 5277 *vpp = NULL;
5278 5278 return (e.error);
5279 5279 }
5280 5280
5281 5281 argop = kmem_alloc(argoplist_size, KM_SLEEP);
5282 5282
5283 5283 /* PUTFH dfh NVERIFY GETATTR ACCESS LOOKUP GETFH GETATTR */
5284 5284 args.array_len = 7;
5285 5285 args.array = argop;
5286 5286
5287 5287 /* 0. putfh file */
5288 5288 argop[0].argop = OP_CPUTFH;
5289 5289 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(dvp)->r_fh;
5290 5290
5291 5291 /* 1. nverify the change info */
5292 5292 argop[1].argop = OP_NVERIFY;
5293 5293 ver_fattr = &argop[1].nfs_argop4_u.opnverify.obj_attributes;
5294 5294 ver_fattr->attrmask = FATTR4_CHANGE_MASK;
5295 5295 ver_fattr->attrlist4 = (char *)&dchange;
5296 5296 ptr = (int32_t *)&dchange;
5297 5297 IXDR_PUT_HYPER(ptr, VTOR4(dvp)->r_change);
5298 5298 ver_fattr->attrlist4_len = sizeof (fattr4_change);
5299 5299
5300 5300 /* 2. getattr directory */
5301 5301 argop[2].argop = OP_GETATTR;
5302 5302 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
5303 5303 argop[2].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
5304 5304
5305 5305 /* 3. access directory */
5306 5306 argop[3].argop = OP_ACCESS;
5307 5307 argop[3].nfs_argop4_u.opaccess.access = ACCESS4_READ | ACCESS4_DELETE |
5308 5308 ACCESS4_MODIFY | ACCESS4_EXTEND | ACCESS4_LOOKUP;
5309 5309
5310 5310 /* 4. lookup name */
5311 5311 if (isdotdot) {
5312 5312 argop[4].argop = OP_LOOKUPP;
5313 5313 } else {
5314 5314 argop[4].argop = OP_CLOOKUP;
5315 5315 argop[4].nfs_argop4_u.opclookup.cname = nm;
5316 5316 }
5317 5317
5318 5318 /* 5. resulting file handle */
5319 5319 argop[5].argop = OP_GETFH;
5320 5320
5321 5321 /* 6. resulting file attributes */
5322 5322 argop[6].argop = OP_GETATTR;
5323 5323 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
5324 5324 argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
5325 5325
5326 5326 doqueue = 1;
5327 5327 t = gethrtime();
5328 5328
5329 5329 rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e);
5330 5330
5331 5331 if (!isdotdot && res.status == NFS4ERR_MOVED) {
5332 5332 e.error = nfs4_setup_referral(dvp, nm, vpp, cr);
5333 5333 if (e.error != 0 && *vpp != NULL)
5334 5334 VN_RELE(*vpp);
5335 5335 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5336 5336 &recov_state, FALSE);
5337 5337 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5338 5338 kmem_free(argop, argoplist_size);
5339 5339 return (e.error);
5340 5340 }
5341 5341
5342 5342 if (nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp)) {
5343 5343 /*
5344 5344 * For WRONGSEC of a non-dotdot case, send secinfo directly
5345 5345 * from this thread, do not go thru the recovery thread since
5346 5346 * we need the nm information.
5347 5347 *
5348 5348 * Not doing dotdot case because there is no specification
5349 5349 * for (PUTFH, SECINFO "..") yet.
5350 5350 */
5351 5351 if (!isdotdot && res.status == NFS4ERR_WRONGSEC) {
5352 5352 if ((e.error = nfs4_secinfo_vnode_otw(dvp, nm, cr)))
5353 5353 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5354 5354 &recov_state, FALSE);
5355 5355 else
5356 5356 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5357 5357 &recov_state, TRUE);
5358 5358 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5359 5359 kmem_free(argop, argoplist_size);
5360 5360 if (!e.error)
5361 5361 goto recov_retry;
5362 5362 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5363 5363 VN_RELE(*vpp);
5364 5364 *vpp = NULL;
5365 5365 return (e.error);
5366 5366 }
5367 5367
5368 5368 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL,
5369 5369 OP_LOOKUP, NULL, NULL, NULL) == FALSE) {
5370 5370 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5371 5371 &recov_state, TRUE);
5372 5372
5373 5373 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5374 5374 kmem_free(argop, argoplist_size);
5375 5375 goto recov_retry;
5376 5376 }
5377 5377 }
5378 5378
5379 5379 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, &recov_state, FALSE);
5380 5380
5381 5381 if (e.error || res.array_len == 0) {
5382 5382 /*
5383 5383 * If e.error isn't set, then reply has no ops (or we couldn't
5384 5384 * be here). The only legal way to reply without an op array
5385 5385 * is via NFS4ERR_MINOR_VERS_MISMATCH. An ops array should
5386 5386 * be in the reply for all other status values.
5387 5387 *
5388 5388 * For valid replies without an ops array, return ENOTSUP
5389 5389 * (geterrno4 xlation of VERS_MISMATCH). For illegal replies,
5390 5390 * return EIO -- don't trust status.
5391 5391 */
5392 5392 if (e.error == 0)
5393 5393 e.error = (res.status == NFS4ERR_MINOR_VERS_MISMATCH) ?
5394 5394 ENOTSUP : EIO;
5395 5395 VN_RELE(*vpp);
5396 5396 *vpp = NULL;
5397 5397 kmem_free(argop, argoplist_size);
5398 5398 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5399 5399 return (e.error);
5400 5400 }
5401 5401
5402 5402 if (res.status != NFS4ERR_SAME) {
5403 5403 e.error = geterrno4(res.status);
5404 5404
5405 5405 /*
5406 5406 * The NVERIFY "failed" so the directory has changed
5407 5407 * First make sure PUTFH succeeded and NVERIFY "failed"
5408 5408 * cleanly.
5409 5409 */
5410 5410 if ((res.array[0].nfs_resop4_u.opputfh.status != NFS4_OK) ||
5411 5411 (res.array[1].nfs_resop4_u.opnverify.status != NFS4_OK)) {
5412 5412 nfs4_purge_stale_fh(e.error, dvp, cr);
5413 5413 VN_RELE(*vpp);
5414 5414 *vpp = NULL;
5415 5415 goto exit;
5416 5416 }
5417 5417
5418 5418 /*
5419 5419 * We know the NVERIFY "failed" so we must:
5420 5420 * purge the caches (access and indirectly dnlc if needed)
5421 5421 */
5422 5422 nfs4_purge_caches(dvp, NFS4_NOPURGE_DNLC, cr, TRUE);
5423 5423
5424 5424 if (res.array[2].nfs_resop4_u.opgetattr.status != NFS4_OK) {
5425 5425 nfs4_purge_stale_fh(e.error, dvp, cr);
5426 5426 VN_RELE(*vpp);
5427 5427 *vpp = NULL;
5428 5428 goto exit;
5429 5429 }
5430 5430
5431 5431 /*
5432 5432 * Install new cached attributes for the directory
5433 5433 */
5434 5434 nfs4_attr_cache(dvp,
5435 5435 &res.array[2].nfs_resop4_u.opgetattr.ga_res,
5436 5436 t, cr, FALSE, NULL);
5437 5437
5438 5438 if (res.array[3].nfs_resop4_u.opaccess.status != NFS4_OK) {
5439 5439 nfs4_purge_stale_fh(e.error, dvp, cr);
5440 5440 VN_RELE(*vpp);
5441 5441 *vpp = NULL;
5442 5442 e.error = geterrno4(res.status);
5443 5443 goto exit;
5444 5444 }
5445 5445
5446 5446 /*
5447 5447 * Now we know the directory is valid,
5448 5448 * cache new directory access
5449 5449 */
5450 5450 nfs4_access_cache(drp,
5451 5451 args.array[3].nfs_argop4_u.opaccess.access,
5452 5452 res.array[3].nfs_resop4_u.opaccess.access, cr);
5453 5453
5454 5454 /*
5455 5455 * recheck VEXEC access
5456 5456 */
5457 5457 cacc = nfs4_access_check(drp, ACCESS4_LOOKUP, cr);
5458 5458 if (cacc != NFS4_ACCESS_ALLOWED) {
5459 5459 /*
5460 5460 * Directory permissions might have been revoked
5461 5461 */
5462 5462 if (cacc == NFS4_ACCESS_DENIED) {
5463 5463 e.error = EACCES;
5464 5464 VN_RELE(*vpp);
5465 5465 *vpp = NULL;
5466 5466 goto exit;
5467 5467 }
5468 5468
5469 5469 /*
5470 5470 * Somehow we must not have asked for enough
5471 5471 * so try a singleton ACCESS, should never happen.
5472 5472 */
5473 5473 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL);
5474 5474 if (e.error) {
5475 5475 VN_RELE(*vpp);
5476 5476 *vpp = NULL;
5477 5477 goto exit;
5478 5478 }
5479 5479 }
5480 5480
5481 5481 e.error = geterrno4(res.status);
5482 5482 if (res.array[4].nfs_resop4_u.oplookup.status != NFS4_OK) {
5483 5483 /*
5484 5484 * The lookup failed, probably no entry
5485 5485 */
5486 5486 if (e.error == ENOENT && nfs4_lookup_neg_cache) {
5487 5487 dnlc_update(dvp, nm, DNLC_NO_VNODE);
5488 5488 } else {
5489 5489 /*
5490 5490 * Might be some other error, so remove
5491 5491 * the dnlc entry to make sure we start all
5492 5492 * over again, next time.
5493 5493 */
5494 5494 dnlc_remove(dvp, nm);
5495 5495 }
5496 5496 VN_RELE(*vpp);
5497 5497 *vpp = NULL;
5498 5498 goto exit;
5499 5499 }
5500 5500
5501 5501 if (res.array[5].nfs_resop4_u.opgetfh.status != NFS4_OK) {
5502 5502 /*
5503 5503 * The file exists but we can't get its fh for
5504 5504 * some unknown reason. Remove it from the dnlc
5505 5505 * and error out to be safe.
5506 5506 */
5507 5507 dnlc_remove(dvp, nm);
5508 5508 VN_RELE(*vpp);
5509 5509 *vpp = NULL;
5510 5510 goto exit;
5511 5511 }
5512 5512 fhp = &res.array[5].nfs_resop4_u.opgetfh.object;
5513 5513 if (fhp->nfs_fh4_len == 0) {
5514 5514 /*
5515 5515 * The file exists but a bogus fh
5516 5516 * some unknown reason. Remove it from the dnlc
5517 5517 * and error out to be safe.
5518 5518 */
5519 5519 e.error = ENOENT;
5520 5520 dnlc_remove(dvp, nm);
5521 5521 VN_RELE(*vpp);
5522 5522 *vpp = NULL;
5523 5523 goto exit;
5524 5524 }
5525 5525 sfhp = sfh4_get(fhp, mi);
5526 5526
5527 5527 if (res.array[6].nfs_resop4_u.opgetattr.status == NFS4_OK)
5528 5528 garp = &res.array[6].nfs_resop4_u.opgetattr.ga_res;
5529 5529
5530 5530 /*
5531 5531 * Make the new rnode
5532 5532 */
5533 5533 if (isdotdot) {
5534 5534 e.error = nfs4_make_dotdot(sfhp, t, dvp, cr, &nvp, 1);
5535 5535 if (e.error) {
5536 5536 sfh4_rele(&sfhp);
5537 5537 VN_RELE(*vpp);
5538 5538 *vpp = NULL;
5539 5539 goto exit;
5540 5540 }
5541 5541 /*
5542 5542 * XXX if nfs4_make_dotdot uses an existing rnode
5543 5543 * XXX it doesn't update the attributes.
5544 5544 * XXX for now just save them again to save an OTW
5545 5545 */
5546 5546 nfs4_attr_cache(nvp, garp, t, cr, FALSE, NULL);
5547 5547 } else {
5548 5548 nvp = makenfs4node(sfhp, garp, dvp->v_vfsp, t, cr,
5549 5549 dvp, fn_get(VTOSV(dvp)->sv_name, nm, sfhp));
5550 5550 /*
5551 5551 * If v_type == VNON, then garp was NULL because
5552 5552 * the last op in the compound failed and makenfs4node
5553 5553 * could not find the vnode for sfhp. It created
5554 5554 * a new vnode, so we have nothing to purge here.
5555 5555 */
5556 5556 if (nvp->v_type == VNON) {
5557 5557 vattr_t vattr;
5558 5558
5559 5559 vattr.va_mask = AT_TYPE;
5560 5560 /*
5561 5561 * N.B. We've already called nfs4_end_fop above.
5562 5562 */
5563 5563 e.error = nfs4getattr(nvp, &vattr, cr);
5564 5564 if (e.error) {
5565 5565 sfh4_rele(&sfhp);
5566 5566 VN_RELE(*vpp);
5567 5567 *vpp = NULL;
5568 5568 VN_RELE(nvp);
5569 5569 goto exit;
5570 5570 }
5571 5571 nvp->v_type = vattr.va_type;
5572 5572 }
5573 5573 }
5574 5574 sfh4_rele(&sfhp);
5575 5575
5576 5576 nrp = VTOR4(nvp);
5577 5577 mutex_enter(&nrp->r_statev4_lock);
5578 5578 if (!nrp->created_v4) {
5579 5579 mutex_exit(&nrp->r_statev4_lock);
5580 5580 dnlc_update(dvp, nm, nvp);
5581 5581 } else
5582 5582 mutex_exit(&nrp->r_statev4_lock);
5583 5583
5584 5584 VN_RELE(*vpp);
5585 5585 *vpp = nvp;
5586 5586 } else {
5587 5587 hrtime_t now;
5588 5588 hrtime_t delta = 0;
5589 5589
5590 5590 e.error = 0;
5591 5591
5592 5592 /*
5593 5593 * Because the NVERIFY "succeeded" we know that the
5594 5594 * directory attributes are still valid
5595 5595 * so update r_time_attr_inval
5596 5596 */
5597 5597 now = gethrtime();
5598 5598 mutex_enter(&drp->r_statelock);
5599 5599 if (!(mi->mi_flags & MI4_NOAC) && !(dvp->v_flag & VNOCACHE)) {
5600 5600 delta = now - drp->r_time_attr_saved;
5601 5601 if (delta < mi->mi_acdirmin)
5602 5602 delta = mi->mi_acdirmin;
5603 5603 else if (delta > mi->mi_acdirmax)
5604 5604 delta = mi->mi_acdirmax;
5605 5605 }
5606 5606 drp->r_time_attr_inval = now + delta;
5607 5607 mutex_exit(&drp->r_statelock);
5608 5608 dnlc_update(dvp, nm, *vpp);
5609 5609
5610 5610 /*
5611 5611 * Even though we have a valid directory attr cache
5612 5612 * and dnlc entry, we may not have access.
5613 5613 * This should almost always hit the cache.
5614 5614 */
5615 5615 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL);
5616 5616 if (e.error) {
5617 5617 VN_RELE(*vpp);
5618 5618 *vpp = NULL;
5619 5619 }
5620 5620
5621 5621 if (*vpp == DNLC_NO_VNODE) {
5622 5622 VN_RELE(*vpp);
5623 5623 *vpp = NULL;
5624 5624 e.error = ENOENT;
5625 5625 }
5626 5626 }
5627 5627
5628 5628 exit:
5629 5629 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5630 5630 kmem_free(argop, argoplist_size);
5631 5631 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5632 5632 return (e.error);
5633 5633 }
5634 5634
5635 5635 /*
5636 5636 * We need to go over the wire to lookup the name, but
5637 5637 * while we are there verify the directory has not
5638 5638 * changed but if it has, get new attributes and check access
5639 5639 *
5640 5640 * PUTFH dfh SAVEFH LOOKUP nm GETFH GETATTR RESTOREFH
5641 5641 * NVERIFY GETATTR ACCESS
5642 5642 *
5643 5643 * With the results:
5644 5644 * if the NVERIFY failed we must purge the caches, add new attributes,
5645 5645 * and cache new access.
5646 5646 * set a new r_time_attr_inval
5647 5647 * add name to dnlc, possibly negative
5648 5648 * if LOOKUP succeeded
5649 5649 * cache new attributes
5650 5650 */
5651 5651 static int
5652 5652 nfs4lookupnew_otw(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr)
5653 5653 {
5654 5654 COMPOUND4args_clnt args;
5655 5655 COMPOUND4res_clnt res;
5656 5656 fattr4 *ver_fattr;
5657 5657 fattr4_change dchange;
5658 5658 int32_t *ptr;
5659 5659 nfs4_ga_res_t *garp = NULL;
5660 5660 int argoplist_size = 9 * sizeof (nfs_argop4);
5661 5661 nfs_argop4 *argop;
5662 5662 int doqueue;
5663 5663 mntinfo4_t *mi;
5664 5664 nfs4_recov_state_t recov_state;
5665 5665 hrtime_t t;
5666 5666 int isdotdot;
5667 5667 vnode_t *nvp;
5668 5668 nfs_fh4 *fhp;
5669 5669 nfs4_sharedfh_t *sfhp;
5670 5670 nfs4_access_type_t cacc;
5671 5671 rnode4_t *nrp;
5672 5672 rnode4_t *drp = VTOR4(dvp);
5673 5673 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
5674 5674
5675 5675 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
5676 5676 ASSERT(nm != NULL);
5677 5677 ASSERT(nm[0] != '\0');
5678 5678 ASSERT(dvp->v_type == VDIR);
5679 5679 ASSERT(nm[0] != '.' || nm[1] != '\0');
5680 5680 ASSERT(*vpp == NULL);
5681 5681
5682 5682 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') {
5683 5683 isdotdot = 1;
5684 5684 args.ctag = TAG_LOOKUP_PARENT;
5685 5685 } else {
5686 5686 /*
5687 5687 * If dvp were a stub, it should have triggered and caused
5688 5688 * a mount for us to get this far.
5689 5689 */
5690 5690 ASSERT(!RP_ISSTUB(VTOR4(dvp)));
5691 5691
5692 5692 isdotdot = 0;
5693 5693 args.ctag = TAG_LOOKUP;
5694 5694 }
5695 5695
5696 5696 mi = VTOMI4(dvp);
5697 5697 recov_state.rs_flags = 0;
5698 5698 recov_state.rs_num_retry_despite_err = 0;
5699 5699
5700 5700 nvp = NULL;
5701 5701
5702 5702 /* Save the original mount point security information */
5703 5703 (void) save_mnt_secinfo(mi->mi_curr_serv);
5704 5704
5705 5705 recov_retry:
5706 5706 e.error = nfs4_start_fop(mi, dvp, NULL, OH_LOOKUP,
5707 5707 &recov_state, NULL);
5708 5708 if (e.error) {
5709 5709 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5710 5710 return (e.error);
5711 5711 }
5712 5712
5713 5713 argop = kmem_alloc(argoplist_size, KM_SLEEP);
5714 5714
5715 5715 /* PUTFH SAVEFH LOOKUP GETFH GETATTR RESTOREFH NVERIFY GETATTR ACCESS */
5716 5716 args.array_len = 9;
5717 5717 args.array = argop;
5718 5718
5719 5719 /* 0. putfh file */
5720 5720 argop[0].argop = OP_CPUTFH;
5721 5721 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(dvp)->r_fh;
5722 5722
5723 5723 /* 1. savefh for the nverify */
5724 5724 argop[1].argop = OP_SAVEFH;
5725 5725
5726 5726 /* 2. lookup name */
5727 5727 if (isdotdot) {
5728 5728 argop[2].argop = OP_LOOKUPP;
5729 5729 } else {
5730 5730 argop[2].argop = OP_CLOOKUP;
5731 5731 argop[2].nfs_argop4_u.opclookup.cname = nm;
5732 5732 }
5733 5733
5734 5734 /* 3. resulting file handle */
5735 5735 argop[3].argop = OP_GETFH;
5736 5736
5737 5737 /* 4. resulting file attributes */
5738 5738 argop[4].argop = OP_GETATTR;
5739 5739 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
5740 5740 argop[4].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
5741 5741
5742 5742 /* 5. restorefh back the directory for the nverify */
5743 5743 argop[5].argop = OP_RESTOREFH;
5744 5744
5745 5745 /* 6. nverify the change info */
5746 5746 argop[6].argop = OP_NVERIFY;
5747 5747 ver_fattr = &argop[6].nfs_argop4_u.opnverify.obj_attributes;
5748 5748 ver_fattr->attrmask = FATTR4_CHANGE_MASK;
5749 5749 ver_fattr->attrlist4 = (char *)&dchange;
5750 5750 ptr = (int32_t *)&dchange;
5751 5751 IXDR_PUT_HYPER(ptr, VTOR4(dvp)->r_change);
5752 5752 ver_fattr->attrlist4_len = sizeof (fattr4_change);
5753 5753
5754 5754 /* 7. getattr directory */
5755 5755 argop[7].argop = OP_GETATTR;
5756 5756 argop[7].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
5757 5757 argop[7].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
5758 5758
5759 5759 /* 8. access directory */
5760 5760 argop[8].argop = OP_ACCESS;
5761 5761 argop[8].nfs_argop4_u.opaccess.access = ACCESS4_READ | ACCESS4_DELETE |
5762 5762 ACCESS4_MODIFY | ACCESS4_EXTEND | ACCESS4_LOOKUP;
5763 5763
5764 5764 doqueue = 1;
5765 5765 t = gethrtime();
5766 5766
5767 5767 rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e);
5768 5768
5769 5769 if (!isdotdot && res.status == NFS4ERR_MOVED) {
5770 5770 e.error = nfs4_setup_referral(dvp, nm, vpp, cr);
5771 5771 if (e.error != 0 && *vpp != NULL)
5772 5772 VN_RELE(*vpp);
5773 5773 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5774 5774 &recov_state, FALSE);
5775 5775 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5776 5776 kmem_free(argop, argoplist_size);
5777 5777 return (e.error);
5778 5778 }
5779 5779
5780 5780 if (nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp)) {
5781 5781 /*
5782 5782 * For WRONGSEC of a non-dotdot case, send secinfo directly
5783 5783 * from this thread, do not go thru the recovery thread since
5784 5784 * we need the nm information.
5785 5785 *
5786 5786 * Not doing dotdot case because there is no specification
5787 5787 * for (PUTFH, SECINFO "..") yet.
5788 5788 */
5789 5789 if (!isdotdot && res.status == NFS4ERR_WRONGSEC) {
5790 5790 if ((e.error = nfs4_secinfo_vnode_otw(dvp, nm, cr)))
5791 5791 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5792 5792 &recov_state, FALSE);
5793 5793 else
5794 5794 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5795 5795 &recov_state, TRUE);
5796 5796 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5797 5797 kmem_free(argop, argoplist_size);
5798 5798 if (!e.error)
5799 5799 goto recov_retry;
5800 5800 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5801 5801 return (e.error);
5802 5802 }
5803 5803
5804 5804 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL,
5805 5805 OP_LOOKUP, NULL, NULL, NULL) == FALSE) {
5806 5806 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5807 5807 &recov_state, TRUE);
5808 5808
5809 5809 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5810 5810 kmem_free(argop, argoplist_size);
5811 5811 goto recov_retry;
5812 5812 }
5813 5813 }
5814 5814
5815 5815 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, &recov_state, FALSE);
5816 5816
5817 5817 if (e.error || res.array_len == 0) {
5818 5818 /*
5819 5819 * If e.error isn't set, then reply has no ops (or we couldn't
5820 5820 * be here). The only legal way to reply without an op array
5821 5821 * is via NFS4ERR_MINOR_VERS_MISMATCH. An ops array should
5822 5822 * be in the reply for all other status values.
5823 5823 *
5824 5824 * For valid replies without an ops array, return ENOTSUP
5825 5825 * (geterrno4 xlation of VERS_MISMATCH). For illegal replies,
5826 5826 * return EIO -- don't trust status.
5827 5827 */
5828 5828 if (e.error == 0)
5829 5829 e.error = (res.status == NFS4ERR_MINOR_VERS_MISMATCH) ?
5830 5830 ENOTSUP : EIO;
5831 5831
5832 5832 kmem_free(argop, argoplist_size);
5833 5833 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5834 5834 return (e.error);
5835 5835 }
5836 5836
5837 5837 e.error = geterrno4(res.status);
5838 5838
5839 5839 /*
5840 5840 * The PUTFH and SAVEFH may have failed.
5841 5841 */
5842 5842 if ((res.array[0].nfs_resop4_u.opputfh.status != NFS4_OK) ||
5843 5843 (res.array[1].nfs_resop4_u.opsavefh.status != NFS4_OK)) {
5844 5844 nfs4_purge_stale_fh(e.error, dvp, cr);
5845 5845 goto exit;
5846 5846 }
5847 5847
5848 5848 /*
5849 5849 * Check if the file exists, if it does delay entering
5850 5850 * into the dnlc until after we update the directory
5851 5851 * attributes so we don't cause it to get purged immediately.
5852 5852 */
5853 5853 if (res.array[2].nfs_resop4_u.oplookup.status != NFS4_OK) {
5854 5854 /*
5855 5855 * The lookup failed, probably no entry
5856 5856 */
5857 5857 if (e.error == ENOENT && nfs4_lookup_neg_cache)
5858 5858 dnlc_update(dvp, nm, DNLC_NO_VNODE);
5859 5859 goto exit;
5860 5860 }
5861 5861
5862 5862 if (res.array[3].nfs_resop4_u.opgetfh.status != NFS4_OK) {
5863 5863 /*
5864 5864 * The file exists but we can't get its fh for
5865 5865 * some unknown reason. Error out to be safe.
5866 5866 */
5867 5867 goto exit;
5868 5868 }
5869 5869
5870 5870 fhp = &res.array[3].nfs_resop4_u.opgetfh.object;
5871 5871 if (fhp->nfs_fh4_len == 0) {
5872 5872 /*
5873 5873 * The file exists but a bogus fh
5874 5874 * some unknown reason. Error out to be safe.
5875 5875 */
5876 5876 e.error = EIO;
5877 5877 goto exit;
5878 5878 }
5879 5879 sfhp = sfh4_get(fhp, mi);
5880 5880
5881 5881 if (res.array[4].nfs_resop4_u.opgetattr.status != NFS4_OK) {
5882 5882 sfh4_rele(&sfhp);
5883 5883 goto exit;
5884 5884 }
5885 5885 garp = &res.array[4].nfs_resop4_u.opgetattr.ga_res;
5886 5886
5887 5887 /*
5888 5888 * The RESTOREFH may have failed
5889 5889 */
5890 5890 if (res.array[5].nfs_resop4_u.oprestorefh.status != NFS4_OK) {
5891 5891 sfh4_rele(&sfhp);
5892 5892 e.error = EIO;
5893 5893 goto exit;
5894 5894 }
5895 5895
5896 5896 if (res.array[6].nfs_resop4_u.opnverify.status != NFS4ERR_SAME) {
5897 5897 /*
5898 5898 * First make sure the NVERIFY failed as we expected,
5899 5899 * if it didn't then be conservative and error out
5900 5900 * as we can't trust the directory.
5901 5901 */
5902 5902 if (res.array[6].nfs_resop4_u.opnverify.status != NFS4_OK) {
5903 5903 sfh4_rele(&sfhp);
5904 5904 e.error = EIO;
5905 5905 goto exit;
5906 5906 }
5907 5907
5908 5908 /*
5909 5909 * We know the NVERIFY "failed" so the directory has changed,
5910 5910 * so we must:
5911 5911 * purge the caches (access and indirectly dnlc if needed)
5912 5912 */
5913 5913 nfs4_purge_caches(dvp, NFS4_NOPURGE_DNLC, cr, TRUE);
5914 5914
5915 5915 if (res.array[7].nfs_resop4_u.opgetattr.status != NFS4_OK) {
5916 5916 sfh4_rele(&sfhp);
5917 5917 goto exit;
5918 5918 }
5919 5919 nfs4_attr_cache(dvp,
5920 5920 &res.array[7].nfs_resop4_u.opgetattr.ga_res,
5921 5921 t, cr, FALSE, NULL);
5922 5922
5923 5923 if (res.array[8].nfs_resop4_u.opaccess.status != NFS4_OK) {
5924 5924 nfs4_purge_stale_fh(e.error, dvp, cr);
5925 5925 sfh4_rele(&sfhp);
5926 5926 e.error = geterrno4(res.status);
5927 5927 goto exit;
5928 5928 }
5929 5929
5930 5930 /*
5931 5931 * Now we know the directory is valid,
5932 5932 * cache new directory access
5933 5933 */
5934 5934 nfs4_access_cache(drp,
5935 5935 args.array[8].nfs_argop4_u.opaccess.access,
5936 5936 res.array[8].nfs_resop4_u.opaccess.access, cr);
5937 5937
5938 5938 /*
5939 5939 * recheck VEXEC access
5940 5940 */
5941 5941 cacc = nfs4_access_check(drp, ACCESS4_LOOKUP, cr);
5942 5942 if (cacc != NFS4_ACCESS_ALLOWED) {
5943 5943 /*
5944 5944 * Directory permissions might have been revoked
5945 5945 */
5946 5946 if (cacc == NFS4_ACCESS_DENIED) {
5947 5947 sfh4_rele(&sfhp);
5948 5948 e.error = EACCES;
5949 5949 goto exit;
5950 5950 }
5951 5951
5952 5952 /*
5953 5953 * Somehow we must not have asked for enough
5954 5954 * so try a singleton ACCESS should never happen
5955 5955 */
5956 5956 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL);
5957 5957 if (e.error) {
5958 5958 sfh4_rele(&sfhp);
5959 5959 goto exit;
5960 5960 }
5961 5961 }
5962 5962
5963 5963 e.error = geterrno4(res.status);
5964 5964 } else {
5965 5965 hrtime_t now;
5966 5966 hrtime_t delta = 0;
5967 5967
5968 5968 e.error = 0;
5969 5969
5970 5970 /*
5971 5971 * Because the NVERIFY "succeeded" we know that the
5972 5972 * directory attributes are still valid
5973 5973 * so update r_time_attr_inval
5974 5974 */
5975 5975 now = gethrtime();
5976 5976 mutex_enter(&drp->r_statelock);
5977 5977 if (!(mi->mi_flags & MI4_NOAC) && !(dvp->v_flag & VNOCACHE)) {
5978 5978 delta = now - drp->r_time_attr_saved;
5979 5979 if (delta < mi->mi_acdirmin)
5980 5980 delta = mi->mi_acdirmin;
5981 5981 else if (delta > mi->mi_acdirmax)
5982 5982 delta = mi->mi_acdirmax;
5983 5983 }
5984 5984 drp->r_time_attr_inval = now + delta;
5985 5985 mutex_exit(&drp->r_statelock);
5986 5986
5987 5987 /*
5988 5988 * Even though we have a valid directory attr cache,
5989 5989 * we may not have access.
5990 5990 * This should almost always hit the cache.
5991 5991 */
5992 5992 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL);
5993 5993 if (e.error) {
5994 5994 sfh4_rele(&sfhp);
5995 5995 goto exit;
5996 5996 }
5997 5997 }
5998 5998
5999 5999 /*
6000 6000 * Now we have successfully completed the lookup, if the
6001 6001 * directory has changed we now have the valid attributes.
6002 6002 * We also know we have directory access.
6003 6003 * Create the new rnode and insert it in the dnlc.
6004 6004 */
6005 6005 if (isdotdot) {
6006 6006 e.error = nfs4_make_dotdot(sfhp, t, dvp, cr, &nvp, 1);
6007 6007 if (e.error) {
6008 6008 sfh4_rele(&sfhp);
6009 6009 goto exit;
6010 6010 }
6011 6011 /*
6012 6012 * XXX if nfs4_make_dotdot uses an existing rnode
6013 6013 * XXX it doesn't update the attributes.
6014 6014 * XXX for now just save them again to save an OTW
6015 6015 */
6016 6016 nfs4_attr_cache(nvp, garp, t, cr, FALSE, NULL);
6017 6017 } else {
6018 6018 nvp = makenfs4node(sfhp, garp, dvp->v_vfsp, t, cr,
6019 6019 dvp, fn_get(VTOSV(dvp)->sv_name, nm, sfhp));
6020 6020 }
6021 6021 sfh4_rele(&sfhp);
6022 6022
6023 6023 nrp = VTOR4(nvp);
6024 6024 mutex_enter(&nrp->r_statev4_lock);
6025 6025 if (!nrp->created_v4) {
6026 6026 mutex_exit(&nrp->r_statev4_lock);
6027 6027 dnlc_update(dvp, nm, nvp);
6028 6028 } else
6029 6029 mutex_exit(&nrp->r_statev4_lock);
6030 6030
6031 6031 *vpp = nvp;
6032 6032
6033 6033 exit:
6034 6034 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
6035 6035 kmem_free(argop, argoplist_size);
6036 6036 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
6037 6037 return (e.error);
6038 6038 }
6039 6039
6040 6040 #ifdef DEBUG
6041 6041 void
6042 6042 nfs4lookup_dump_compound(char *where, nfs_argop4 *argbase, int argcnt)
6043 6043 {
6044 6044 uint_t i, len;
6045 6045 zoneid_t zoneid = getzoneid();
6046 6046 char *s;
6047 6047
6048 6048 zcmn_err(zoneid, CE_NOTE, "%s: dumping cmpd", where);
6049 6049 for (i = 0; i < argcnt; i++) {
6050 6050 nfs_argop4 *op = &argbase[i];
6051 6051 switch (op->argop) {
6052 6052 case OP_CPUTFH:
6053 6053 case OP_PUTFH:
6054 6054 zcmn_err(zoneid, CE_NOTE, "\t op %d, putfh", i);
6055 6055 break;
6056 6056 case OP_PUTROOTFH:
6057 6057 zcmn_err(zoneid, CE_NOTE, "\t op %d, putrootfh", i);
6058 6058 break;
6059 6059 case OP_CLOOKUP:
6060 6060 s = op->nfs_argop4_u.opclookup.cname;
6061 6061 zcmn_err(zoneid, CE_NOTE, "\t op %d, lookup %s", i, s);
6062 6062 break;
6063 6063 case OP_LOOKUP:
6064 6064 s = utf8_to_str(&op->nfs_argop4_u.oplookup.objname,
6065 6065 &len, NULL);
6066 6066 zcmn_err(zoneid, CE_NOTE, "\t op %d, lookup %s", i, s);
6067 6067 kmem_free(s, len);
6068 6068 break;
6069 6069 case OP_LOOKUPP:
6070 6070 zcmn_err(zoneid, CE_NOTE, "\t op %d, lookupp ..", i);
6071 6071 break;
6072 6072 case OP_GETFH:
6073 6073 zcmn_err(zoneid, CE_NOTE, "\t op %d, getfh", i);
6074 6074 break;
6075 6075 case OP_GETATTR:
6076 6076 zcmn_err(zoneid, CE_NOTE, "\t op %d, getattr", i);
6077 6077 break;
6078 6078 case OP_OPENATTR:
6079 6079 zcmn_err(zoneid, CE_NOTE, "\t op %d, openattr", i);
6080 6080 break;
6081 6081 default:
6082 6082 zcmn_err(zoneid, CE_NOTE, "\t op %d, opcode %d", i,
6083 6083 op->argop);
6084 6084 break;
6085 6085 }
6086 6086 }
6087 6087 }
6088 6088 #endif
6089 6089
6090 6090 /*
6091 6091 * nfs4lookup_setup - constructs a multi-lookup compound request.
6092 6092 *
6093 6093 * Given the path "nm1/nm2/.../nmn", the following compound requests
6094 6094 * may be created:
6095 6095 *
6096 6096 * Note: Getfh is not be needed because filehandle attr is mandatory, but it
6097 6097 * is faster, for now.
6098 6098 *
6099 6099 * l4_getattrs indicates the type of compound requested.
6100 6100 *
6101 6101 * LKP4_NO_ATTRIBUTE - no attributes (used by secinfo):
6102 6102 *
6103 6103 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Lookup {nmn} }
6104 6104 *
6105 6105 * total number of ops is n + 1.
6106 6106 *
6107 6107 * LKP4_LAST_NAMED_ATTR - multi-component path for a named
6108 6108 * attribute: create lookups plus one OPENATTR/GETFH/GETATTR
6109 6109 * before the last component, and only get attributes
6110 6110 * for the last component. Note that the second-to-last
6111 6111 * pathname component is XATTR_RPATH, which does NOT go
6112 6112 * over-the-wire as a lookup.
6113 6113 *
6114 6114 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Lookup {nmn-2};
6115 6115 * Openattr; Getfh; Getattr; Lookup {nmn}; Getfh; Getattr }
6116 6116 *
6117 6117 * and total number of ops is n + 5.
6118 6118 *
6119 6119 * LKP4_LAST_ATTRDIR - multi-component path for the hidden named
6120 6120 * attribute directory: create lookups plus an OPENATTR
6121 6121 * replacing the last lookup. Note that the last pathname
6122 6122 * component is XATTR_RPATH, which does NOT go over-the-wire
6123 6123 * as a lookup.
6124 6124 *
6125 6125 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Getfh; Getattr;
6126 6126 * Openattr; Getfh; Getattr }
6127 6127 *
6128 6128 * and total number of ops is n + 5.
6129 6129 *
6130 6130 * LKP4_ALL_ATTRIBUTES - create lookups and get attributes for intermediate
6131 6131 * nodes too.
6132 6132 *
6133 6133 * compound { Put*fh; Lookup {nm1}; Getfh; Getattr;
6134 6134 * Lookup {nm2}; ... Lookup {nmn}; Getfh; Getattr }
6135 6135 *
6136 6136 * and total number of ops is 3*n + 1.
6137 6137 *
6138 6138 * All cases: returns the index in the arg array of the final LOOKUP op, or
6139 6139 * -1 if no LOOKUPs were used.
6140 6140 */
6141 6141 int
6142 6142 nfs4lookup_setup(char *nm, lookup4_param_t *lookupargp, int needgetfh)
6143 6143 {
6144 6144 enum lkp4_attr_setup l4_getattrs = lookupargp->l4_getattrs;
6145 6145 nfs_argop4 *argbase, *argop;
6146 6146 int arglen, argcnt;
6147 6147 int n = 1; /* number of components */
6148 6148 int nga = 1; /* number of Getattr's in request */
6149 6149 char c = '\0', *s, *p;
6150 6150 int lookup_idx = -1;
6151 6151 int argoplist_size;
6152 6152
6153 6153 /* set lookuparg response result to 0 */
6154 6154 lookupargp->resp->status = NFS4_OK;
6155 6155
6156 6156 /* skip leading "/" or "." e.g. ".//./" if there is */
6157 6157 for (; ; nm++) {
6158 6158 if (*nm != '/' && *nm != '.')
6159 6159 break;
6160 6160
6161 6161 /* ".." is counted as 1 component */
6162 6162 if (*nm == '.' && *(nm + 1) != '/')
6163 6163 break;
6164 6164 }
6165 6165
6166 6166 /*
6167 6167 * Find n = number of components - nm must be null terminated
6168 6168 * Skip "." components.
6169 6169 */
6170 6170 if (*nm != '\0')
6171 6171 for (n = 1, s = nm; *s != '\0'; s++) {
6172 6172 if ((*s == '/') && (*(s + 1) != '/') &&
6173 6173 (*(s + 1) != '\0') &&
6174 6174 !(*(s + 1) == '.' && (*(s + 2) == '/' ||
6175 6175 *(s + 2) == '\0')))
6176 6176 n++;
6177 6177 }
6178 6178 else
6179 6179 n = 0;
6180 6180
6181 6181 /*
6182 6182 * nga is number of components that need Getfh+Getattr
6183 6183 */
6184 6184 switch (l4_getattrs) {
6185 6185 case LKP4_NO_ATTRIBUTES:
6186 6186 nga = 0;
6187 6187 break;
6188 6188 case LKP4_ALL_ATTRIBUTES:
6189 6189 nga = n;
6190 6190 /*
6191 6191 * Always have at least 1 getfh, getattr pair
6192 6192 */
6193 6193 if (nga == 0)
6194 6194 nga++;
6195 6195 break;
6196 6196 case LKP4_LAST_ATTRDIR:
6197 6197 case LKP4_LAST_NAMED_ATTR:
6198 6198 nga = n+1;
6199 6199 break;
6200 6200 }
6201 6201
6202 6202 /*
6203 6203 * If change to use the filehandle attr instead of getfh
6204 6204 * the following line can be deleted.
6205 6205 */
6206 6206 nga *= 2;
6207 6207
6208 6208 /*
6209 6209 * calculate number of ops in request as
6210 6210 * header + trailer + lookups + getattrs
6211 6211 */
6212 6212 arglen = lookupargp->header_len + lookupargp->trailer_len + n + nga;
6213 6213
6214 6214 argoplist_size = arglen * sizeof (nfs_argop4);
6215 6215 argop = argbase = kmem_alloc(argoplist_size, KM_SLEEP);
6216 6216 lookupargp->argsp->array = argop;
6217 6217
6218 6218 argcnt = lookupargp->header_len;
6219 6219 argop += argcnt;
6220 6220
6221 6221 /*
6222 6222 * loop and create a lookup op and possibly getattr/getfh for
6223 6223 * each component. Skip "." components.
6224 6224 */
6225 6225 for (s = nm; *s != '\0'; s = p) {
6226 6226 /*
6227 6227 * Set up a pathname struct for each component if needed
6228 6228 */
6229 6229 while (*s == '/')
6230 6230 s++;
6231 6231 if (*s == '\0')
6232 6232 break;
6233 6233
6234 6234 for (p = s; (*p != '/') && (*p != '\0'); p++)
6235 6235 ;
6236 6236 c = *p;
6237 6237 *p = '\0';
6238 6238
6239 6239 if (s[0] == '.' && s[1] == '\0') {
6240 6240 *p = c;
6241 6241 continue;
6242 6242 }
6243 6243 if (l4_getattrs == LKP4_LAST_ATTRDIR &&
6244 6244 strcmp(s, XATTR_RPATH) == 0) {
6245 6245 /* getfh XXX may not be needed in future */
6246 6246 argop->argop = OP_GETFH;
6247 6247 argop++;
6248 6248 argcnt++;
6249 6249
6250 6250 /* getattr */
6251 6251 argop->argop = OP_GETATTR;
6252 6252 argop->nfs_argop4_u.opgetattr.attr_request =
6253 6253 lookupargp->ga_bits;
6254 6254 argop->nfs_argop4_u.opgetattr.mi =
6255 6255 lookupargp->mi;
6256 6256 argop++;
6257 6257 argcnt++;
6258 6258
6259 6259 /* openattr */
6260 6260 argop->argop = OP_OPENATTR;
6261 6261 } else if (l4_getattrs == LKP4_LAST_NAMED_ATTR &&
6262 6262 strcmp(s, XATTR_RPATH) == 0) {
6263 6263 /* openattr */
6264 6264 argop->argop = OP_OPENATTR;
6265 6265 argop++;
6266 6266 argcnt++;
6267 6267
6268 6268 /* getfh XXX may not be needed in future */
6269 6269 argop->argop = OP_GETFH;
6270 6270 argop++;
6271 6271 argcnt++;
6272 6272
6273 6273 /* getattr */
6274 6274 argop->argop = OP_GETATTR;
6275 6275 argop->nfs_argop4_u.opgetattr.attr_request =
6276 6276 lookupargp->ga_bits;
6277 6277 argop->nfs_argop4_u.opgetattr.mi =
6278 6278 lookupargp->mi;
6279 6279 argop++;
6280 6280 argcnt++;
6281 6281 *p = c;
6282 6282 continue;
6283 6283 } else if (s[0] == '.' && s[1] == '.' && s[2] == '\0') {
6284 6284 /* lookupp */
6285 6285 argop->argop = OP_LOOKUPP;
6286 6286 } else {
6287 6287 /* lookup */
6288 6288 argop->argop = OP_LOOKUP;
6289 6289 (void) str_to_utf8(s,
6290 6290 &argop->nfs_argop4_u.oplookup.objname);
6291 6291 }
6292 6292 lookup_idx = argcnt;
6293 6293 argop++;
6294 6294 argcnt++;
6295 6295
6296 6296 *p = c;
6297 6297
6298 6298 if (l4_getattrs == LKP4_ALL_ATTRIBUTES) {
6299 6299 /* getfh XXX may not be needed in future */
6300 6300 argop->argop = OP_GETFH;
6301 6301 argop++;
6302 6302 argcnt++;
6303 6303
6304 6304 /* getattr */
6305 6305 argop->argop = OP_GETATTR;
6306 6306 argop->nfs_argop4_u.opgetattr.attr_request =
6307 6307 lookupargp->ga_bits;
6308 6308 argop->nfs_argop4_u.opgetattr.mi =
6309 6309 lookupargp->mi;
6310 6310 argop++;
6311 6311 argcnt++;
6312 6312 }
6313 6313 }
6314 6314
6315 6315 if ((l4_getattrs != LKP4_NO_ATTRIBUTES) &&
6316 6316 ((l4_getattrs != LKP4_ALL_ATTRIBUTES) || (lookup_idx < 0))) {
6317 6317 if (needgetfh) {
6318 6318 /* stick in a post-lookup getfh */
6319 6319 argop->argop = OP_GETFH;
6320 6320 argcnt++;
6321 6321 argop++;
6322 6322 }
6323 6323 /* post-lookup getattr */
6324 6324 argop->argop = OP_GETATTR;
6325 6325 argop->nfs_argop4_u.opgetattr.attr_request =
6326 6326 lookupargp->ga_bits;
6327 6327 argop->nfs_argop4_u.opgetattr.mi = lookupargp->mi;
6328 6328 argcnt++;
6329 6329 }
6330 6330 argcnt += lookupargp->trailer_len; /* actual op count */
6331 6331 lookupargp->argsp->array_len = argcnt;
6332 6332 lookupargp->arglen = arglen;
6333 6333
6334 6334 #ifdef DEBUG
6335 6335 if (nfs4_client_lookup_debug)
6336 6336 nfs4lookup_dump_compound("nfs4lookup_setup", argbase, argcnt);
6337 6337 #endif
6338 6338
6339 6339 return (lookup_idx);
6340 6340 }
6341 6341
6342 6342 static int
6343 6343 nfs4openattr(vnode_t *dvp, vnode_t **avp, int cflag, cred_t *cr)
6344 6344 {
6345 6345 COMPOUND4args_clnt args;
6346 6346 COMPOUND4res_clnt res;
6347 6347 GETFH4res *gf_res = NULL;
6348 6348 nfs_argop4 argop[4];
6349 6349 nfs_resop4 *resop = NULL;
6350 6350 nfs4_sharedfh_t *sfhp;
6351 6351 hrtime_t t;
6352 6352 nfs4_error_t e;
6353 6353
6354 6354 rnode4_t *drp;
6355 6355 int doqueue = 1;
6356 6356 vnode_t *vp;
6357 6357 int needrecov = 0;
6358 6358 nfs4_recov_state_t recov_state;
6359 6359
6360 6360 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
6361 6361
6362 6362 *avp = NULL;
6363 6363 recov_state.rs_flags = 0;
6364 6364 recov_state.rs_num_retry_despite_err = 0;
6365 6365
6366 6366 recov_retry:
6367 6367 /* COMPOUND: putfh, openattr, getfh, getattr */
6368 6368 args.array_len = 4;
6369 6369 args.array = argop;
6370 6370 args.ctag = TAG_OPENATTR;
6371 6371
6372 6372 e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state);
6373 6373 if (e.error)
6374 6374 return (e.error);
6375 6375
6376 6376 drp = VTOR4(dvp);
6377 6377
6378 6378 /* putfh */
6379 6379 argop[0].argop = OP_CPUTFH;
6380 6380 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
6381 6381
6382 6382 /* openattr */
6383 6383 argop[1].argop = OP_OPENATTR;
6384 6384 argop[1].nfs_argop4_u.opopenattr.createdir = (cflag ? TRUE : FALSE);
6385 6385
6386 6386 /* getfh */
6387 6387 argop[2].argop = OP_GETFH;
6388 6388
6389 6389 /* getattr */
6390 6390 argop[3].argop = OP_GETATTR;
6391 6391 argop[3].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
6392 6392 argop[3].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
6393 6393
6394 6394 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE,
6395 6395 "nfs4openattr: %s call, drp %s", needrecov ? "recov" : "first",
6396 6396 rnode4info(drp)));
6397 6397
6398 6398 t = gethrtime();
6399 6399
6400 6400 rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e);
6401 6401
6402 6402 needrecov = nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp);
6403 6403 if (needrecov) {
6404 6404 bool_t abort;
6405 6405
6406 6406 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
6407 6407 "nfs4openattr: initiating recovery\n"));
6408 6408
6409 6409 abort = nfs4_start_recovery(&e,
6410 6410 VTOMI4(dvp), dvp, NULL, NULL, NULL,
6411 6411 OP_OPENATTR, NULL, NULL, NULL);
6412 6412 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov);
6413 6413 if (!e.error) {
6414 6414 e.error = geterrno4(res.status);
6415 6415 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
6416 6416 }
6417 6417 if (abort == FALSE)
6418 6418 goto recov_retry;
6419 6419 return (e.error);
6420 6420 }
6421 6421
6422 6422 if (e.error) {
6423 6423 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov);
6424 6424 return (e.error);
6425 6425 }
6426 6426
6427 6427 if (res.status) {
6428 6428 /*
6429 6429 * If OTW errro is NOTSUPP, then it should be
6430 6430 * translated to EINVAL. All Solaris file system
6431 6431 * implementations return EINVAL to the syscall layer
6432 6432 * when the attrdir cannot be created due to an
6433 6433 * implementation restriction or noxattr mount option.
6434 6434 */
6435 6435 if (res.status == NFS4ERR_NOTSUPP) {
6436 6436 mutex_enter(&drp->r_statelock);
6437 6437 if (drp->r_xattr_dir)
6438 6438 VN_RELE(drp->r_xattr_dir);
6439 6439 VN_HOLD(NFS4_XATTR_DIR_NOTSUPP);
6440 6440 drp->r_xattr_dir = NFS4_XATTR_DIR_NOTSUPP;
6441 6441 mutex_exit(&drp->r_statelock);
6442 6442
6443 6443 e.error = EINVAL;
6444 6444 } else {
6445 6445 e.error = geterrno4(res.status);
6446 6446 }
6447 6447
6448 6448 if (e.error) {
6449 6449 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
6450 6450 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state,
6451 6451 needrecov);
6452 6452 return (e.error);
6453 6453 }
6454 6454 }
6455 6455
6456 6456 resop = &res.array[0]; /* putfh res */
6457 6457 ASSERT(resop->nfs_resop4_u.opgetfh.status == NFS4_OK);
6458 6458
6459 6459 resop = &res.array[1]; /* openattr res */
6460 6460 ASSERT(resop->nfs_resop4_u.opopenattr.status == NFS4_OK);
6461 6461
6462 6462 resop = &res.array[2]; /* getfh res */
6463 6463 gf_res = &resop->nfs_resop4_u.opgetfh;
6464 6464 if (gf_res->object.nfs_fh4_len == 0) {
6465 6465 *avp = NULL;
6466 6466 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
6467 6467 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov);
6468 6468 return (ENOENT);
6469 6469 }
6470 6470
6471 6471 sfhp = sfh4_get(&gf_res->object, VTOMI4(dvp));
6472 6472 vp = makenfs4node(sfhp, &res.array[3].nfs_resop4_u.opgetattr.ga_res,
6473 6473 dvp->v_vfsp, t, cr, dvp,
6474 6474 fn_get(VTOSV(dvp)->sv_name, XATTR_RPATH, sfhp));
6475 6475 sfh4_rele(&sfhp);
6476 6476
6477 6477 if (e.error)
6478 6478 PURGE_ATTRCACHE4(vp);
6479 6479
6480 6480 mutex_enter(&vp->v_lock);
6481 6481 vp->v_flag |= V_XATTRDIR;
6482 6482 mutex_exit(&vp->v_lock);
6483 6483
6484 6484 *avp = vp;
6485 6485
6486 6486 mutex_enter(&drp->r_statelock);
6487 6487 if (drp->r_xattr_dir)
6488 6488 VN_RELE(drp->r_xattr_dir);
6489 6489 VN_HOLD(vp);
6490 6490 drp->r_xattr_dir = vp;
6491 6491
6492 6492 /*
6493 6493 * Invalidate pathconf4 cache because r_xattr_dir is no longer
6494 6494 * NULL. xattrs could be created at any time, and we have no
6495 6495 * way to update pc4_xattr_exists in the base object if/when
6496 6496 * it happens.
6497 6497 */
6498 6498 drp->r_pathconf.pc4_xattr_valid = 0;
6499 6499
6500 6500 mutex_exit(&drp->r_statelock);
6501 6501
6502 6502 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov);
6503 6503
6504 6504 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
6505 6505
6506 6506 return (0);
6507 6507 }
6508 6508
6509 6509 /* ARGSUSED */
6510 6510 static int
6511 6511 nfs4_create(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive,
6512 6512 int mode, vnode_t **vpp, cred_t *cr, int flags, caller_context_t *ct,
6513 6513 vsecattr_t *vsecp)
6514 6514 {
6515 6515 int error;
6516 6516 vnode_t *vp = NULL;
6517 6517 rnode4_t *rp;
6518 6518 struct vattr vattr;
6519 6519 rnode4_t *drp;
6520 6520 vnode_t *tempvp;
6521 6521 enum createmode4 createmode;
6522 6522 bool_t must_trunc = FALSE;
6523 6523 int truncating = 0;
6524 6524
6525 6525 if (nfs_zone() != VTOMI4(dvp)->mi_zone)
6526 6526 return (EPERM);
6527 6527 if (exclusive == EXCL && (dvp->v_flag & V_XATTRDIR)) {
6528 6528 return (EINVAL);
6529 6529 }
6530 6530
6531 6531 /* . and .. have special meaning in the protocol, reject them. */
6532 6532
6533 6533 if (nm[0] == '.' && (nm[1] == '\0' || (nm[1] == '.' && nm[2] == '\0')))
6534 6534 return (EISDIR);
6535 6535
6536 6536 drp = VTOR4(dvp);
6537 6537
6538 6538 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp)))
6539 6539 return (EINTR);
6540 6540
6541 6541 top:
6542 6542 /*
6543 6543 * We make a copy of the attributes because the caller does not
6544 6544 * expect us to change what va points to.
6545 6545 */
6546 6546 vattr = *va;
6547 6547
6548 6548 /*
6549 6549 * If the pathname is "", then dvp is the root vnode of
6550 6550 * a remote file mounted over a local directory.
6551 6551 * All that needs to be done is access
6552 6552 * checking and truncation. Note that we avoid doing
6553 6553 * open w/ create because the parent directory might
6554 6554 * be in pseudo-fs and the open would fail.
6555 6555 */
6556 6556 if (*nm == '\0') {
6557 6557 error = 0;
6558 6558 VN_HOLD(dvp);
6559 6559 vp = dvp;
6560 6560 must_trunc = TRUE;
6561 6561 } else {
6562 6562 /*
6563 6563 * We need to go over the wire, just to be sure whether the
6564 6564 * file exists or not. Using the DNLC can be dangerous in
6565 6565 * this case when making a decision regarding existence.
6566 6566 */
6567 6567 error = nfs4lookup(dvp, nm, &vp, cr, 1);
6568 6568 }
6569 6569
6570 6570 if (exclusive)
6571 6571 createmode = EXCLUSIVE4;
6572 6572 else
6573 6573 createmode = GUARDED4;
6574 6574
6575 6575 /*
6576 6576 * error would be set if the file does not exist on the
6577 6577 * server, so lets go create it.
6578 6578 */
6579 6579 if (error) {
6580 6580 goto create_otw;
6581 6581 }
6582 6582
6583 6583 /*
6584 6584 * File does exist on the server
6585 6585 */
6586 6586 if (exclusive == EXCL)
6587 6587 error = EEXIST;
6588 6588 else if (vp->v_type == VDIR && (mode & VWRITE))
6589 6589 error = EISDIR;
6590 6590 else {
6591 6591 /*
6592 6592 * If vnode is a device, create special vnode.
6593 6593 */
6594 6594 if (ISVDEV(vp->v_type)) {
6595 6595 tempvp = vp;
6596 6596 vp = specvp(vp, vp->v_rdev, vp->v_type, cr);
6597 6597 VN_RELE(tempvp);
6598 6598 }
6599 6599 if (!(error = VOP_ACCESS(vp, mode, 0, cr, ct))) {
6600 6600 if ((vattr.va_mask & AT_SIZE) &&
6601 6601 vp->v_type == VREG) {
6602 6602 rp = VTOR4(vp);
6603 6603 /*
6604 6604 * Check here for large file handled
6605 6605 * by LF-unaware process (as
6606 6606 * ufs_create() does)
6607 6607 */
6608 6608 if (!(flags & FOFFMAX)) {
6609 6609 mutex_enter(&rp->r_statelock);
6610 6610 if (rp->r_size > MAXOFF32_T)
6611 6611 error = EOVERFLOW;
6612 6612 mutex_exit(&rp->r_statelock);
6613 6613 }
6614 6614
6615 6615 /* if error is set then we need to return */
6616 6616 if (error) {
6617 6617 nfs_rw_exit(&drp->r_rwlock);
6618 6618 VN_RELE(vp);
6619 6619 return (error);
6620 6620 }
6621 6621
6622 6622 if (must_trunc) {
6623 6623 vattr.va_mask = AT_SIZE;
6624 6624 error = nfs4setattr(vp, &vattr, 0, cr,
6625 6625 NULL);
6626 6626 } else {
6627 6627 /*
6628 6628 * we know we have a regular file that already
6629 6629 * exists and we may end up truncating the file
6630 6630 * as a result of the open_otw, so flush out
6631 6631 * any dirty pages for this file first.
6632 6632 */
6633 6633 if (nfs4_has_pages(vp) &&
6634 6634 ((rp->r_flags & R4DIRTY) ||
6635 6635 rp->r_count > 0 ||
6636 6636 rp->r_mapcnt > 0)) {
6637 6637 error = nfs4_putpage(vp,
6638 6638 (offset_t)0, 0, 0, cr, ct);
6639 6639 if (error && (error == ENOSPC ||
6640 6640 error == EDQUOT)) {
6641 6641 mutex_enter(
6642 6642 &rp->r_statelock);
6643 6643 if (!rp->r_error)
6644 6644 rp->r_error =
6645 6645 error;
6646 6646 mutex_exit(
6647 6647 &rp->r_statelock);
6648 6648 }
6649 6649 }
6650 6650 vattr.va_mask = (AT_SIZE |
6651 6651 AT_TYPE | AT_MODE);
6652 6652 vattr.va_type = VREG;
6653 6653 createmode = UNCHECKED4;
6654 6654 truncating = 1;
6655 6655 goto create_otw;
6656 6656 }
6657 6657 }
6658 6658 }
6659 6659 }
6660 6660 nfs_rw_exit(&drp->r_rwlock);
6661 6661 if (error) {
6662 6662 VN_RELE(vp);
6663 6663 } else {
6664 6664 vnode_t *tvp;
6665 6665 rnode4_t *trp;
6666 6666 tvp = vp;
6667 6667 if (vp->v_type == VREG) {
6668 6668 trp = VTOR4(vp);
6669 6669 if (IS_SHADOW(vp, trp))
6670 6670 tvp = RTOV4(trp);
6671 6671 }
6672 6672
6673 6673 if (must_trunc) {
6674 6674 /*
6675 6675 * existing file got truncated, notify.
6676 6676 */
6677 6677 vnevent_create(tvp, ct);
6678 6678 }
6679 6679
6680 6680 *vpp = vp;
6681 6681 }
6682 6682 return (error);
6683 6683
6684 6684 create_otw:
6685 6685 dnlc_remove(dvp, nm);
6686 6686
6687 6687 ASSERT(vattr.va_mask & AT_TYPE);
6688 6688
6689 6689 /*
6690 6690 * If not a regular file let nfs4mknod() handle it.
6691 6691 */
6692 6692 if (vattr.va_type != VREG) {
6693 6693 error = nfs4mknod(dvp, nm, &vattr, exclusive, mode, vpp, cr);
6694 6694 nfs_rw_exit(&drp->r_rwlock);
6695 6695 return (error);
6696 6696 }
6697 6697
6698 6698 /*
6699 6699 * It _is_ a regular file.
6700 6700 */
6701 6701 ASSERT(vattr.va_mask & AT_MODE);
6702 6702 if (MANDMODE(vattr.va_mode)) {
6703 6703 nfs_rw_exit(&drp->r_rwlock);
6704 6704 return (EACCES);
6705 6705 }
6706 6706
6707 6707 /*
6708 6708 * If this happens to be a mknod of a regular file, then flags will
6709 6709 * have neither FREAD or FWRITE. However, we must set at least one
6710 6710 * for the call to nfs4open_otw. If it's open(O_CREAT) driving
6711 6711 * nfs4_create, then either FREAD, FWRITE, or FRDWR has already been
6712 6712 * set (based on openmode specified by app).
6713 6713 */
6714 6714 if ((flags & (FREAD|FWRITE)) == 0)
6715 6715 flags |= (FREAD|FWRITE);
6716 6716
6717 6717 error = nfs4open_otw(dvp, nm, &vattr, vpp, cr, 1, flags, createmode, 0);
6718 6718
6719 6719 if (vp != NULL) {
6720 6720 /* if create was successful, throw away the file's pages */
6721 6721 if (!error && (vattr.va_mask & AT_SIZE))
6722 6722 nfs4_invalidate_pages(vp, (vattr.va_size & PAGEMASK),
6723 6723 cr);
6724 6724 /* release the lookup hold */
6725 6725 VN_RELE(vp);
6726 6726 vp = NULL;
6727 6727 }
6728 6728
6729 6729 /*
6730 6730 * validate that we opened a regular file. This handles a misbehaving
6731 6731 * server that returns an incorrect FH.
6732 6732 */
6733 6733 if ((error == 0) && *vpp && (*vpp)->v_type != VREG) {
6734 6734 error = EISDIR;
6735 6735 VN_RELE(*vpp);
6736 6736 }
6737 6737
6738 6738 /*
6739 6739 * If this is not an exclusive create, then the CREATE
6740 6740 * request will be made with the GUARDED mode set. This
6741 6741 * means that the server will return EEXIST if the file
6742 6742 * exists. The file could exist because of a retransmitted
6743 6743 * request. In this case, we recover by starting over and
6744 6744 * checking to see whether the file exists. This second
6745 6745 * time through it should and a CREATE request will not be
6746 6746 * sent.
6747 6747 *
6748 6748 * This handles the problem of a dangling CREATE request
6749 6749 * which contains attributes which indicate that the file
6750 6750 * should be truncated. This retransmitted request could
6751 6751 * possibly truncate valid data in the file if not caught
6752 6752 * by the duplicate request mechanism on the server or if
6753 6753 * not caught by other means. The scenario is:
6754 6754 *
6755 6755 * Client transmits CREATE request with size = 0
6756 6756 * Client times out, retransmits request.
6757 6757 * Response to the first request arrives from the server
6758 6758 * and the client proceeds on.
6759 6759 * Client writes data to the file.
6760 6760 * The server now processes retransmitted CREATE request
6761 6761 * and truncates file.
6762 6762 *
6763 6763 * The use of the GUARDED CREATE request prevents this from
6764 6764 * happening because the retransmitted CREATE would fail
6765 6765 * with EEXIST and would not truncate the file.
6766 6766 */
6767 6767 if (error == EEXIST && exclusive == NONEXCL) {
6768 6768 #ifdef DEBUG
6769 6769 nfs4_create_misses++;
6770 6770 #endif
6771 6771 goto top;
6772 6772 }
6773 6773 nfs_rw_exit(&drp->r_rwlock);
6774 6774 if (truncating && !error && *vpp) {
6775 6775 vnode_t *tvp;
6776 6776 rnode4_t *trp;
6777 6777 /*
6778 6778 * existing file got truncated, notify.
6779 6779 */
6780 6780 tvp = *vpp;
6781 6781 trp = VTOR4(tvp);
6782 6782 if (IS_SHADOW(tvp, trp))
6783 6783 tvp = RTOV4(trp);
6784 6784 vnevent_create(tvp, ct);
6785 6785 }
6786 6786 return (error);
6787 6787 }
6788 6788
6789 6789 /*
6790 6790 * Create compound (for mkdir, mknod, symlink):
6791 6791 * { Putfh <dfh>; Create; Getfh; Getattr }
6792 6792 * It's okay if setattr failed to set gid - this is not considered
6793 6793 * an error, but purge attrs in that case.
6794 6794 */
6795 6795 static int
6796 6796 call_nfs4_create_req(vnode_t *dvp, char *nm, void *data, struct vattr *va,
6797 6797 vnode_t **vpp, cred_t *cr, nfs_ftype4 type)
6798 6798 {
6799 6799 int need_end_op = FALSE;
6800 6800 COMPOUND4args_clnt args;
6801 6801 COMPOUND4res_clnt res, *resp = NULL;
6802 6802 nfs_argop4 *argop;
6803 6803 nfs_resop4 *resop;
6804 6804 int doqueue;
6805 6805 mntinfo4_t *mi;
6806 6806 rnode4_t *drp = VTOR4(dvp);
6807 6807 change_info4 *cinfo;
6808 6808 GETFH4res *gf_res;
6809 6809 struct vattr vattr;
6810 6810 vnode_t *vp;
6811 6811 fattr4 *crattr;
6812 6812 bool_t needrecov = FALSE;
6813 6813 nfs4_recov_state_t recov_state;
6814 6814 nfs4_sharedfh_t *sfhp = NULL;
6815 6815 hrtime_t t;
6816 6816 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
6817 6817 int numops, argoplist_size, setgid_flag, idx_create, idx_fattr;
6818 6818 dirattr_info_t dinfo, *dinfop;
6819 6819 servinfo4_t *svp;
6820 6820 bitmap4 supp_attrs;
6821 6821
6822 6822 ASSERT(type == NF4DIR || type == NF4LNK || type == NF4BLK ||
6823 6823 type == NF4CHR || type == NF4SOCK || type == NF4FIFO);
6824 6824
6825 6825 mi = VTOMI4(dvp);
6826 6826
6827 6827 /*
6828 6828 * Make sure we properly deal with setting the right gid
6829 6829 * on a new directory to reflect the parent's setgid bit
6830 6830 */
6831 6831 setgid_flag = 0;
6832 6832 if (type == NF4DIR) {
6833 6833 struct vattr dva;
6834 6834
6835 6835 va->va_mode &= ~VSGID;
6836 6836 dva.va_mask = AT_MODE | AT_GID;
6837 6837 if (VOP_GETATTR(dvp, &dva, 0, cr, NULL) == 0) {
6838 6838
6839 6839 /*
6840 6840 * If the parent's directory has the setgid bit set
6841 6841 * _and_ the client was able to get a valid mapping
6842 6842 * for the parent dir's owner_group, we want to
6843 6843 * append NVERIFY(owner_group == dva.va_gid) and
6844 6844 * SETTATTR to the CREATE compound.
6845 6845 */
6846 6846 if (mi->mi_flags & MI4_GRPID || dva.va_mode & VSGID) {
6847 6847 setgid_flag = 1;
6848 6848 va->va_mode |= VSGID;
6849 6849 if (dva.va_gid != GID_NOBODY) {
6850 6850 va->va_mask |= AT_GID;
6851 6851 va->va_gid = dva.va_gid;
6852 6852 }
6853 6853 }
6854 6854 }
6855 6855 }
6856 6856
6857 6857 /*
6858 6858 * Create ops:
6859 6859 * 0:putfh(dir) 1:savefh(dir) 2:create 3:getfh(new) 4:getattr(new)
6860 6860 * 5:restorefh(dir) 6:getattr(dir)
6861 6861 *
6862 6862 * if (setgid)
6863 6863 * 0:putfh(dir) 1:create 2:getfh(new) 3:getattr(new)
6864 6864 * 4:savefh(new) 5:putfh(dir) 6:getattr(dir) 7:restorefh(new)
6865 6865 * 8:nverify 9:setattr
6866 6866 */
6867 6867 if (setgid_flag) {
6868 6868 numops = 10;
6869 6869 idx_create = 1;
6870 6870 idx_fattr = 3;
6871 6871 } else {
6872 6872 numops = 7;
6873 6873 idx_create = 2;
6874 6874 idx_fattr = 4;
6875 6875 }
6876 6876
6877 6877 ASSERT(nfs_zone() == mi->mi_zone);
6878 6878 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp))) {
6879 6879 return (EINTR);
6880 6880 }
6881 6881 recov_state.rs_flags = 0;
6882 6882 recov_state.rs_num_retry_despite_err = 0;
6883 6883
6884 6884 argoplist_size = numops * sizeof (nfs_argop4);
6885 6885 argop = kmem_alloc(argoplist_size, KM_SLEEP);
6886 6886
6887 6887 recov_retry:
6888 6888 if (type == NF4LNK)
6889 6889 args.ctag = TAG_SYMLINK;
6890 6890 else if (type == NF4DIR)
6891 6891 args.ctag = TAG_MKDIR;
6892 6892 else
6893 6893 args.ctag = TAG_MKNOD;
6894 6894
6895 6895 args.array_len = numops;
6896 6896 args.array = argop;
6897 6897
6898 6898 if (e.error = nfs4_start_op(mi, dvp, NULL, &recov_state)) {
6899 6899 nfs_rw_exit(&drp->r_rwlock);
6900 6900 kmem_free(argop, argoplist_size);
6901 6901 return (e.error);
6902 6902 }
6903 6903 need_end_op = TRUE;
6904 6904
6905 6905
6906 6906 /* 0: putfh directory */
6907 6907 argop[0].argop = OP_CPUTFH;
6908 6908 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
6909 6909
6910 6910 /* 1/2: Create object */
6911 6911 argop[idx_create].argop = OP_CCREATE;
6912 6912 argop[idx_create].nfs_argop4_u.opccreate.cname = nm;
6913 6913 argop[idx_create].nfs_argop4_u.opccreate.type = type;
6914 6914 if (type == NF4LNK) {
6915 6915 /*
6916 6916 * symlink, treat name as data
6917 6917 */
6918 6918 ASSERT(data != NULL);
6919 6919 argop[idx_create].nfs_argop4_u.opccreate.ftype4_u.clinkdata =
6920 6920 (char *)data;
6921 6921 }
6922 6922 if (type == NF4BLK || type == NF4CHR) {
6923 6923 ASSERT(data != NULL);
6924 6924 argop[idx_create].nfs_argop4_u.opccreate.ftype4_u.devdata =
6925 6925 *((specdata4 *)data);
6926 6926 }
6927 6927
6928 6928 crattr = &argop[idx_create].nfs_argop4_u.opccreate.createattrs;
6929 6929
6930 6930 svp = drp->r_server;
6931 6931 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
6932 6932 supp_attrs = svp->sv_supp_attrs;
6933 6933 nfs_rw_exit(&svp->sv_lock);
6934 6934
6935 6935 if (vattr_to_fattr4(va, NULL, crattr, 0, OP_CREATE, supp_attrs)) {
6936 6936 nfs_rw_exit(&drp->r_rwlock);
6937 6937 nfs4_end_op(mi, dvp, NULL, &recov_state, needrecov);
6938 6938 e.error = EINVAL;
6939 6939 kmem_free(argop, argoplist_size);
6940 6940 return (e.error);
6941 6941 }
6942 6942
6943 6943 /* 2/3: getfh fh of created object */
6944 6944 ASSERT(idx_create + 1 == idx_fattr - 1);
6945 6945 argop[idx_create + 1].argop = OP_GETFH;
6946 6946
6947 6947 /* 3/4: getattr of new object */
6948 6948 argop[idx_fattr].argop = OP_GETATTR;
6949 6949 argop[idx_fattr].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
6950 6950 argop[idx_fattr].nfs_argop4_u.opgetattr.mi = mi;
6951 6951
6952 6952 if (setgid_flag) {
6953 6953 vattr_t _v;
6954 6954
6955 6955 argop[4].argop = OP_SAVEFH;
6956 6956
6957 6957 argop[5].argop = OP_CPUTFH;
6958 6958 argop[5].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
6959 6959
6960 6960 argop[6].argop = OP_GETATTR;
6961 6961 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
6962 6962 argop[6].nfs_argop4_u.opgetattr.mi = mi;
6963 6963
6964 6964 argop[7].argop = OP_RESTOREFH;
6965 6965
6966 6966 /*
6967 6967 * nverify
6968 6968 *
6969 6969 * XXX - Revisit the last argument to nfs4_end_op()
6970 6970 * once 5020486 is fixed.
6971 6971 */
6972 6972 _v.va_mask = AT_GID;
6973 6973 _v.va_gid = va->va_gid;
6974 6974 if (e.error = nfs4args_verify(&argop[8], &_v, OP_NVERIFY,
6975 6975 supp_attrs)) {
6976 6976 nfs4_end_op(mi, dvp, *vpp, &recov_state, TRUE);
6977 6977 nfs_rw_exit(&drp->r_rwlock);
6978 6978 nfs4_fattr4_free(crattr);
6979 6979 kmem_free(argop, argoplist_size);
6980 6980 return (e.error);
6981 6981 }
6982 6982
6983 6983 /*
6984 6984 * setattr
6985 6985 *
6986 6986 * We _know_ we're not messing with AT_SIZE or AT_XTIME,
6987 6987 * so no need for stateid or flags. Also we specify NULL
6988 6988 * rp since we're only interested in setting owner_group
6989 6989 * attributes.
6990 6990 */
6991 6991 nfs4args_setattr(&argop[9], &_v, NULL, 0, NULL, cr, supp_attrs,
6992 6992 &e.error, 0);
6993 6993
6994 6994 if (e.error) {
6995 6995 nfs4_end_op(mi, dvp, *vpp, &recov_state, TRUE);
6996 6996 nfs_rw_exit(&drp->r_rwlock);
6997 6997 nfs4_fattr4_free(crattr);
6998 6998 nfs4args_verify_free(&argop[8]);
6999 6999 kmem_free(argop, argoplist_size);
7000 7000 return (e.error);
7001 7001 }
7002 7002 } else {
7003 7003 argop[1].argop = OP_SAVEFH;
7004 7004
7005 7005 argop[5].argop = OP_RESTOREFH;
7006 7006
7007 7007 argop[6].argop = OP_GETATTR;
7008 7008 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
7009 7009 argop[6].nfs_argop4_u.opgetattr.mi = mi;
7010 7010 }
7011 7011
7012 7012 dnlc_remove(dvp, nm);
7013 7013
7014 7014 doqueue = 1;
7015 7015 t = gethrtime();
7016 7016 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
7017 7017
7018 7018 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
7019 7019 if (e.error) {
7020 7020 PURGE_ATTRCACHE4(dvp);
7021 7021 if (!needrecov)
7022 7022 goto out;
7023 7023 }
7024 7024
7025 7025 if (needrecov) {
7026 7026 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL,
7027 7027 OP_CREATE, NULL, NULL, NULL) == FALSE) {
7028 7028 nfs4_end_op(mi, dvp, NULL, &recov_state,
7029 7029 needrecov);
7030 7030 need_end_op = FALSE;
7031 7031 nfs4_fattr4_free(crattr);
7032 7032 if (setgid_flag) {
7033 7033 nfs4args_verify_free(&argop[8]);
7034 7034 nfs4args_setattr_free(&argop[9]);
7035 7035 }
7036 7036 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
7037 7037 goto recov_retry;
7038 7038 }
7039 7039 }
7040 7040
7041 7041 resp = &res;
7042 7042
7043 7043 if (res.status != NFS4_OK && res.array_len <= idx_fattr + 1) {
7044 7044
7045 7045 if (res.status == NFS4ERR_BADOWNER)
7046 7046 nfs4_log_badowner(mi, OP_CREATE);
7047 7047
7048 7048 e.error = geterrno4(res.status);
7049 7049
7050 7050 /*
7051 7051 * This check is left over from when create was implemented
7052 7052 * using a setattr op (instead of createattrs). If the
7053 7053 * putfh/create/getfh failed, the error was returned. If
7054 7054 * setattr/getattr failed, we keep going.
7055 7055 *
7056 7056 * It might be better to get rid of the GETFH also, and just
7057 7057 * do PUTFH/CREATE/GETATTR since the FH attr is mandatory.
7058 7058 * Then if any of the operations failed, we could return the
7059 7059 * error now, and remove much of the error code below.
7060 7060 */
7061 7061 if (res.array_len <= idx_fattr) {
7062 7062 /*
7063 7063 * Either Putfh, Create or Getfh failed.
7064 7064 */
7065 7065 PURGE_ATTRCACHE4(dvp);
7066 7066 /*
7067 7067 * nfs4_purge_stale_fh() may generate otw calls through
7068 7068 * nfs4_invalidate_pages. Hence the need to call
7069 7069 * nfs4_end_op() here to avoid nfs4_start_op() deadlock.
7070 7070 */
7071 7071 nfs4_end_op(mi, dvp, NULL, &recov_state,
7072 7072 needrecov);
7073 7073 need_end_op = FALSE;
7074 7074 nfs4_purge_stale_fh(e.error, dvp, cr);
7075 7075 goto out;
7076 7076 }
7077 7077 }
7078 7078
7079 7079 resop = &res.array[idx_create]; /* create res */
7080 7080 cinfo = &resop->nfs_resop4_u.opcreate.cinfo;
7081 7081
7082 7082 resop = &res.array[idx_create + 1]; /* getfh res */
7083 7083 gf_res = &resop->nfs_resop4_u.opgetfh;
7084 7084
7085 7085 sfhp = sfh4_get(&gf_res->object, mi);
7086 7086 if (e.error) {
7087 7087 *vpp = vp = makenfs4node(sfhp, NULL, dvp->v_vfsp, t, cr, dvp,
7088 7088 fn_get(VTOSV(dvp)->sv_name, nm, sfhp));
7089 7089 if (vp->v_type == VNON) {
7090 7090 vattr.va_mask = AT_TYPE;
7091 7091 /*
7092 7092 * Need to call nfs4_end_op before nfs4getattr to avoid
7093 7093 * potential nfs4_start_op deadlock. See RFE 4777612.
7094 7094 */
7095 7095 nfs4_end_op(mi, dvp, NULL, &recov_state,
7096 7096 needrecov);
7097 7097 need_end_op = FALSE;
7098 7098 e.error = nfs4getattr(vp, &vattr, cr);
7099 7099 if (e.error) {
7100 7100 VN_RELE(vp);
7101 7101 *vpp = NULL;
7102 7102 goto out;
7103 7103 }
7104 7104 vp->v_type = vattr.va_type;
7105 7105 }
7106 7106 e.error = 0;
7107 7107 } else {
7108 7108 *vpp = vp = makenfs4node(sfhp,
7109 7109 &res.array[idx_fattr].nfs_resop4_u.opgetattr.ga_res,
7110 7110 dvp->v_vfsp, t, cr,
7111 7111 dvp, fn_get(VTOSV(dvp)->sv_name, nm, sfhp));
7112 7112 }
7113 7113
7114 7114 /*
7115 7115 * If compound succeeded, then update dir attrs
7116 7116 */
7117 7117 if (res.status == NFS4_OK) {
7118 7118 dinfo.di_garp = &res.array[6].nfs_resop4_u.opgetattr.ga_res;
7119 7119 dinfo.di_cred = cr;
7120 7120 dinfo.di_time_call = t;
7121 7121 dinfop = &dinfo;
7122 7122 } else
7123 7123 dinfop = NULL;
7124 7124
7125 7125 /* Update directory cache attribute, readdir and dnlc caches */
7126 7126 nfs4_update_dircaches(cinfo, dvp, vp, nm, dinfop);
7127 7127
7128 7128 out:
7129 7129 if (sfhp != NULL)
7130 7130 sfh4_rele(&sfhp);
7131 7131 nfs_rw_exit(&drp->r_rwlock);
7132 7132 nfs4_fattr4_free(crattr);
7133 7133 if (setgid_flag) {
7134 7134 nfs4args_verify_free(&argop[8]);
7135 7135 nfs4args_setattr_free(&argop[9]);
7136 7136 }
7137 7137 if (resp)
7138 7138 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
7139 7139 if (need_end_op)
7140 7140 nfs4_end_op(mi, dvp, NULL, &recov_state, needrecov);
7141 7141
7142 7142 kmem_free(argop, argoplist_size);
7143 7143 return (e.error);
7144 7144 }
7145 7145
7146 7146 /* ARGSUSED */
7147 7147 static int
7148 7148 nfs4mknod(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive,
7149 7149 int mode, vnode_t **vpp, cred_t *cr)
7150 7150 {
7151 7151 int error;
7152 7152 vnode_t *vp;
7153 7153 nfs_ftype4 type;
7154 7154 specdata4 spec, *specp = NULL;
7155 7155
7156 7156 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
7157 7157
7158 7158 switch (va->va_type) {
7159 7159 case VCHR:
7160 7160 case VBLK:
7161 7161 type = (va->va_type == VCHR) ? NF4CHR : NF4BLK;
7162 7162 spec.specdata1 = getmajor(va->va_rdev);
7163 7163 spec.specdata2 = getminor(va->va_rdev);
7164 7164 specp = &spec;
7165 7165 break;
7166 7166
7167 7167 case VFIFO:
7168 7168 type = NF4FIFO;
7169 7169 break;
7170 7170 case VSOCK:
7171 7171 type = NF4SOCK;
7172 7172 break;
7173 7173
7174 7174 default:
7175 7175 return (EINVAL);
7176 7176 }
7177 7177
7178 7178 error = call_nfs4_create_req(dvp, nm, specp, va, &vp, cr, type);
7179 7179 if (error) {
7180 7180 return (error);
7181 7181 }
7182 7182
7183 7183 /*
7184 7184 * This might not be needed any more; special case to deal
7185 7185 * with problematic v2/v3 servers. Since create was unable
7186 7186 * to set group correctly, not sure what hope setattr has.
7187 7187 */
7188 7188 if (va->va_gid != VTOR4(vp)->r_attr.va_gid) {
7189 7189 va->va_mask = AT_GID;
7190 7190 (void) nfs4setattr(vp, va, 0, cr, NULL);
7191 7191 }
7192 7192
7193 7193 /*
7194 7194 * If vnode is a device create special vnode
7195 7195 */
7196 7196 if (ISVDEV(vp->v_type)) {
7197 7197 *vpp = specvp(vp, vp->v_rdev, vp->v_type, cr);
7198 7198 VN_RELE(vp);
7199 7199 } else {
7200 7200 *vpp = vp;
7201 7201 }
7202 7202 return (error);
7203 7203 }
7204 7204
7205 7205 /*
7206 7206 * Remove requires that the current fh be the target directory.
7207 7207 * After the operation, the current fh is unchanged.
7208 7208 * The compound op structure is:
7209 7209 * PUTFH(targetdir), REMOVE
7210 7210 *
7211 7211 * Weirdness: if the vnode to be removed is open
7212 7212 * we rename it instead of removing it and nfs_inactive
7213 7213 * will remove the new name.
7214 7214 */
7215 7215 /* ARGSUSED */
7216 7216 static int
7217 7217 nfs4_remove(vnode_t *dvp, char *nm, cred_t *cr, caller_context_t *ct, int flags)
7218 7218 {
7219 7219 COMPOUND4args_clnt args;
7220 7220 COMPOUND4res_clnt res, *resp = NULL;
7221 7221 REMOVE4res *rm_res;
7222 7222 nfs_argop4 argop[3];
7223 7223 nfs_resop4 *resop;
7224 7224 vnode_t *vp;
7225 7225 char *tmpname;
7226 7226 int doqueue;
7227 7227 mntinfo4_t *mi;
7228 7228 rnode4_t *rp;
7229 7229 rnode4_t *drp;
7230 7230 int needrecov = 0;
7231 7231 nfs4_recov_state_t recov_state;
7232 7232 int isopen;
7233 7233 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
7234 7234 dirattr_info_t dinfo;
7235 7235
7236 7236 if (nfs_zone() != VTOMI4(dvp)->mi_zone)
7237 7237 return (EPERM);
7238 7238 drp = VTOR4(dvp);
7239 7239 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp)))
7240 7240 return (EINTR);
7241 7241
7242 7242 e.error = nfs4lookup(dvp, nm, &vp, cr, 0);
7243 7243 if (e.error) {
7244 7244 nfs_rw_exit(&drp->r_rwlock);
7245 7245 return (e.error);
7246 7246 }
7247 7247
7248 7248 if (vp->v_type == VDIR) {
7249 7249 VN_RELE(vp);
7250 7250 nfs_rw_exit(&drp->r_rwlock);
7251 7251 return (EISDIR);
7252 7252 }
7253 7253
7254 7254 /*
7255 7255 * First just remove the entry from the name cache, as it
7256 7256 * is most likely the only entry for this vp.
7257 7257 */
7258 7258 dnlc_remove(dvp, nm);
7259 7259
7260 7260 rp = VTOR4(vp);
7261 7261
7262 7262 /*
7263 7263 * For regular file types, check to see if the file is open by looking
7264 7264 * at the open streams.
7265 7265 * For all other types, check the reference count on the vnode. Since
7266 7266 * they are not opened OTW they never have an open stream.
7267 7267 *
7268 7268 * If the file is open, rename it to .nfsXXXX.
7269 7269 */
7270 7270 if (vp->v_type != VREG) {
7271 7271 /*
7272 7272 * If the file has a v_count > 1 then there may be more than one
7273 7273 * entry in the name cache due multiple links or an open file,
7274 7274 * but we don't have the real reference count so flush all
7275 7275 * possible entries.
7276 7276 */
7277 7277 if (vp->v_count > 1)
7278 7278 dnlc_purge_vp(vp);
7279 7279
7280 7280 /*
7281 7281 * Now we have the real reference count.
7282 7282 */
7283 7283 isopen = vp->v_count > 1;
7284 7284 } else {
7285 7285 mutex_enter(&rp->r_os_lock);
7286 7286 isopen = list_head(&rp->r_open_streams) != NULL;
7287 7287 mutex_exit(&rp->r_os_lock);
7288 7288 }
7289 7289
7290 7290 mutex_enter(&rp->r_statelock);
7291 7291 if (isopen &&
7292 7292 (rp->r_unldvp == NULL || strcmp(nm, rp->r_unlname) == 0)) {
7293 7293 mutex_exit(&rp->r_statelock);
7294 7294 tmpname = newname();
7295 7295 e.error = nfs4rename(dvp, nm, dvp, tmpname, cr, ct);
7296 7296 if (e.error)
7297 7297 kmem_free(tmpname, MAXNAMELEN);
7298 7298 else {
7299 7299 mutex_enter(&rp->r_statelock);
7300 7300 if (rp->r_unldvp == NULL) {
7301 7301 VN_HOLD(dvp);
7302 7302 rp->r_unldvp = dvp;
7303 7303 if (rp->r_unlcred != NULL)
7304 7304 crfree(rp->r_unlcred);
7305 7305 crhold(cr);
7306 7306 rp->r_unlcred = cr;
7307 7307 rp->r_unlname = tmpname;
7308 7308 } else {
7309 7309 kmem_free(rp->r_unlname, MAXNAMELEN);
7310 7310 rp->r_unlname = tmpname;
7311 7311 }
7312 7312 mutex_exit(&rp->r_statelock);
7313 7313 }
7314 7314 VN_RELE(vp);
7315 7315 nfs_rw_exit(&drp->r_rwlock);
7316 7316 return (e.error);
7317 7317 }
7318 7318 /*
7319 7319 * Actually remove the file/dir
7320 7320 */
7321 7321 mutex_exit(&rp->r_statelock);
7322 7322
7323 7323 /*
7324 7324 * We need to flush any dirty pages which happen to
7325 7325 * be hanging around before removing the file.
7326 7326 * This shouldn't happen very often since in NFSv4
7327 7327 * we should be close to open consistent.
7328 7328 */
7329 7329 if (nfs4_has_pages(vp) &&
7330 7330 ((rp->r_flags & R4DIRTY) || rp->r_count > 0)) {
7331 7331 e.error = nfs4_putpage(vp, (u_offset_t)0, 0, 0, cr, ct);
7332 7332 if (e.error && (e.error == ENOSPC || e.error == EDQUOT)) {
7333 7333 mutex_enter(&rp->r_statelock);
7334 7334 if (!rp->r_error)
7335 7335 rp->r_error = e.error;
7336 7336 mutex_exit(&rp->r_statelock);
7337 7337 }
7338 7338 }
7339 7339
7340 7340 mi = VTOMI4(dvp);
7341 7341
7342 7342 (void) nfs4delegreturn(rp, NFS4_DR_REOPEN);
7343 7343 recov_state.rs_flags = 0;
7344 7344 recov_state.rs_num_retry_despite_err = 0;
7345 7345
7346 7346 recov_retry:
7347 7347 /*
7348 7348 * Remove ops: putfh dir; remove
7349 7349 */
7350 7350 args.ctag = TAG_REMOVE;
7351 7351 args.array_len = 3;
7352 7352 args.array = argop;
7353 7353
7354 7354 e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state);
7355 7355 if (e.error) {
7356 7356 nfs_rw_exit(&drp->r_rwlock);
7357 7357 VN_RELE(vp);
7358 7358 return (e.error);
7359 7359 }
7360 7360
7361 7361 /* putfh directory */
7362 7362 argop[0].argop = OP_CPUTFH;
7363 7363 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
7364 7364
7365 7365 /* remove */
7366 7366 argop[1].argop = OP_CREMOVE;
7367 7367 argop[1].nfs_argop4_u.opcremove.ctarget = nm;
7368 7368
7369 7369 /* getattr dir */
7370 7370 argop[2].argop = OP_GETATTR;
7371 7371 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
7372 7372 argop[2].nfs_argop4_u.opgetattr.mi = mi;
7373 7373
7374 7374 doqueue = 1;
7375 7375 dinfo.di_time_call = gethrtime();
7376 7376 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
7377 7377
7378 7378 PURGE_ATTRCACHE4(vp);
7379 7379
7380 7380 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
7381 7381 if (e.error)
7382 7382 PURGE_ATTRCACHE4(dvp);
7383 7383
7384 7384 if (needrecov) {
7385 7385 if (nfs4_start_recovery(&e, VTOMI4(dvp), dvp,
7386 7386 NULL, NULL, NULL, OP_REMOVE, NULL, NULL, NULL) == FALSE) {
7387 7387 if (!e.error)
7388 7388 (void) xdr_free(xdr_COMPOUND4res_clnt,
7389 7389 (caddr_t)&res);
7390 7390 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state,
7391 7391 needrecov);
7392 7392 goto recov_retry;
7393 7393 }
7394 7394 }
7395 7395
7396 7396 /*
7397 7397 * Matching nfs4_end_op() for start_op() above.
7398 7398 * There is a path in the code below which calls
7399 7399 * nfs4_purge_stale_fh(), which may generate otw calls through
7400 7400 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op()
7401 7401 * here to avoid nfs4_start_op() deadlock.
7402 7402 */
7403 7403 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov);
7404 7404
7405 7405 if (!e.error) {
7406 7406 resp = &res;
7407 7407
7408 7408 if (res.status) {
7409 7409 e.error = geterrno4(res.status);
7410 7410 PURGE_ATTRCACHE4(dvp);
7411 7411 nfs4_purge_stale_fh(e.error, dvp, cr);
7412 7412 } else {
7413 7413 resop = &res.array[1]; /* remove res */
7414 7414 rm_res = &resop->nfs_resop4_u.opremove;
7415 7415
7416 7416 dinfo.di_garp =
7417 7417 &res.array[2].nfs_resop4_u.opgetattr.ga_res;
7418 7418 dinfo.di_cred = cr;
7419 7419
7420 7420 /* Update directory attr, readdir and dnlc caches */
7421 7421 nfs4_update_dircaches(&rm_res->cinfo, dvp, NULL, NULL,
7422 7422 &dinfo);
7423 7423 }
7424 7424 }
7425 7425 nfs_rw_exit(&drp->r_rwlock);
7426 7426 if (resp)
7427 7427 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
7428 7428
7429 7429 if (e.error == 0) {
7430 7430 vnode_t *tvp;
7431 7431 rnode4_t *trp;
7432 7432 trp = VTOR4(vp);
7433 7433 tvp = vp;
7434 7434 if (IS_SHADOW(vp, trp))
7435 7435 tvp = RTOV4(trp);
7436 7436 vnevent_remove(tvp, dvp, nm, ct);
7437 7437 }
7438 7438 VN_RELE(vp);
7439 7439 return (e.error);
7440 7440 }
7441 7441
7442 7442 /*
7443 7443 * Link requires that the current fh be the target directory and the
7444 7444 * saved fh be the source fh. After the operation, the current fh is unchanged.
7445 7445 * Thus the compound op structure is:
7446 7446 * PUTFH(file), SAVEFH, PUTFH(targetdir), LINK, RESTOREFH,
7447 7447 * GETATTR(file)
7448 7448 */
7449 7449 /* ARGSUSED */
7450 7450 static int
7451 7451 nfs4_link(vnode_t *tdvp, vnode_t *svp, char *tnm, cred_t *cr,
7452 7452 caller_context_t *ct, int flags)
7453 7453 {
7454 7454 COMPOUND4args_clnt args;
7455 7455 COMPOUND4res_clnt res, *resp = NULL;
7456 7456 LINK4res *ln_res;
7457 7457 int argoplist_size = 7 * sizeof (nfs_argop4);
7458 7458 nfs_argop4 *argop;
7459 7459 nfs_resop4 *resop;
7460 7460 vnode_t *realvp, *nvp;
7461 7461 int doqueue;
7462 7462 mntinfo4_t *mi;
7463 7463 rnode4_t *tdrp;
7464 7464 bool_t needrecov = FALSE;
7465 7465 nfs4_recov_state_t recov_state;
7466 7466 hrtime_t t;
7467 7467 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
7468 7468 dirattr_info_t dinfo;
7469 7469
7470 7470 ASSERT(*tnm != '\0');
7471 7471 ASSERT(tdvp->v_type == VDIR);
7472 7472 ASSERT(nfs4_consistent_type(tdvp));
7473 7473 ASSERT(nfs4_consistent_type(svp));
7474 7474
7475 7475 if (nfs_zone() != VTOMI4(tdvp)->mi_zone)
7476 7476 return (EPERM);
7477 7477 if (VOP_REALVP(svp, &realvp, ct) == 0) {
7478 7478 svp = realvp;
7479 7479 ASSERT(nfs4_consistent_type(svp));
7480 7480 }
7481 7481
7482 7482 tdrp = VTOR4(tdvp);
7483 7483 mi = VTOMI4(svp);
7484 7484
7485 7485 if (!(mi->mi_flags & MI4_LINK)) {
7486 7486 return (EOPNOTSUPP);
7487 7487 }
7488 7488 recov_state.rs_flags = 0;
7489 7489 recov_state.rs_num_retry_despite_err = 0;
7490 7490
7491 7491 if (nfs_rw_enter_sig(&tdrp->r_rwlock, RW_WRITER, INTR4(tdvp)))
7492 7492 return (EINTR);
7493 7493
7494 7494 recov_retry:
7495 7495 argop = kmem_alloc(argoplist_size, KM_SLEEP);
7496 7496
7497 7497 args.ctag = TAG_LINK;
7498 7498
7499 7499 /*
7500 7500 * Link ops: putfh fl; savefh; putfh tdir; link; getattr(dir);
7501 7501 * restorefh; getattr(fl)
7502 7502 */
7503 7503 args.array_len = 7;
7504 7504 args.array = argop;
7505 7505
7506 7506 e.error = nfs4_start_op(VTOMI4(svp), svp, tdvp, &recov_state);
7507 7507 if (e.error) {
7508 7508 kmem_free(argop, argoplist_size);
7509 7509 nfs_rw_exit(&tdrp->r_rwlock);
7510 7510 return (e.error);
7511 7511 }
7512 7512
7513 7513 /* 0. putfh file */
7514 7514 argop[0].argop = OP_CPUTFH;
7515 7515 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(svp)->r_fh;
7516 7516
7517 7517 /* 1. save current fh to free up the space for the dir */
7518 7518 argop[1].argop = OP_SAVEFH;
7519 7519
7520 7520 /* 2. putfh targetdir */
7521 7521 argop[2].argop = OP_CPUTFH;
7522 7522 argop[2].nfs_argop4_u.opcputfh.sfh = tdrp->r_fh;
7523 7523
7524 7524 /* 3. link: current_fh is targetdir, saved_fh is source */
7525 7525 argop[3].argop = OP_CLINK;
7526 7526 argop[3].nfs_argop4_u.opclink.cnewname = tnm;
7527 7527
7528 7528 /* 4. Get attributes of dir */
7529 7529 argop[4].argop = OP_GETATTR;
7530 7530 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
7531 7531 argop[4].nfs_argop4_u.opgetattr.mi = mi;
7532 7532
7533 7533 /* 5. If link was successful, restore current vp to file */
7534 7534 argop[5].argop = OP_RESTOREFH;
7535 7535
7536 7536 /* 6. Get attributes of linked object */
7537 7537 argop[6].argop = OP_GETATTR;
7538 7538 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
7539 7539 argop[6].nfs_argop4_u.opgetattr.mi = mi;
7540 7540
7541 7541 dnlc_remove(tdvp, tnm);
7542 7542
7543 7543 doqueue = 1;
7544 7544 t = gethrtime();
7545 7545
7546 7546 rfs4call(VTOMI4(svp), &args, &res, cr, &doqueue, 0, &e);
7547 7547
7548 7548 needrecov = nfs4_needs_recovery(&e, FALSE, svp->v_vfsp);
7549 7549 if (e.error != 0 && !needrecov) {
7550 7550 PURGE_ATTRCACHE4(tdvp);
7551 7551 PURGE_ATTRCACHE4(svp);
7552 7552 nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state, needrecov);
7553 7553 goto out;
7554 7554 }
7555 7555
7556 7556 if (needrecov) {
7557 7557 bool_t abort;
7558 7558
7559 7559 abort = nfs4_start_recovery(&e, VTOMI4(svp), svp, tdvp,
7560 7560 NULL, NULL, OP_LINK, NULL, NULL, NULL);
7561 7561 if (abort == FALSE) {
7562 7562 nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state,
7563 7563 needrecov);
7564 7564 kmem_free(argop, argoplist_size);
7565 7565 if (!e.error)
7566 7566 (void) xdr_free(xdr_COMPOUND4res_clnt,
7567 7567 (caddr_t)&res);
7568 7568 goto recov_retry;
7569 7569 } else {
7570 7570 if (e.error != 0) {
7571 7571 PURGE_ATTRCACHE4(tdvp);
7572 7572 PURGE_ATTRCACHE4(svp);
7573 7573 nfs4_end_op(VTOMI4(svp), svp, tdvp,
7574 7574 &recov_state, needrecov);
7575 7575 goto out;
7576 7576 }
7577 7577 /* fall through for res.status case */
7578 7578 }
7579 7579 }
7580 7580
7581 7581 nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state, needrecov);
7582 7582
7583 7583 resp = &res;
7584 7584 if (res.status) {
7585 7585 /* If link succeeded, then don't return error */
7586 7586 e.error = geterrno4(res.status);
7587 7587 if (res.array_len <= 4) {
7588 7588 /*
7589 7589 * Either Putfh, Savefh, Putfh dir, or Link failed
7590 7590 */
7591 7591 PURGE_ATTRCACHE4(svp);
7592 7592 PURGE_ATTRCACHE4(tdvp);
7593 7593 if (e.error == EOPNOTSUPP) {
7594 7594 mutex_enter(&mi->mi_lock);
7595 7595 mi->mi_flags &= ~MI4_LINK;
7596 7596 mutex_exit(&mi->mi_lock);
7597 7597 }
7598 7598 /* Remap EISDIR to EPERM for non-root user for SVVS */
7599 7599 /* XXX-LP */
7600 7600 if (e.error == EISDIR && crgetuid(cr) != 0)
7601 7601 e.error = EPERM;
7602 7602 goto out;
7603 7603 }
7604 7604 }
7605 7605
7606 7606 /* either no error or one of the postop getattr failed */
7607 7607
7608 7608 /*
7609 7609 * XXX - if LINK succeeded, but no attrs were returned for link
7610 7610 * file, purge its cache.
7611 7611 *
7612 7612 * XXX Perform a simplified version of wcc checking. Instead of
7613 7613 * have another getattr to get pre-op, just purge cache if
7614 7614 * any of the ops prior to and including the getattr failed.
7615 7615 * If the getattr succeeded then update the attrcache accordingly.
7616 7616 */
7617 7617
7618 7618 /*
7619 7619 * update cache with link file postattrs.
7620 7620 * Note: at this point resop points to link res.
7621 7621 */
7622 7622 resop = &res.array[3]; /* link res */
7623 7623 ln_res = &resop->nfs_resop4_u.oplink;
7624 7624 if (res.status == NFS4_OK)
7625 7625 e.error = nfs4_update_attrcache(res.status,
7626 7626 &res.array[6].nfs_resop4_u.opgetattr.ga_res,
7627 7627 t, svp, cr);
7628 7628
7629 7629 /*
7630 7630 * Call makenfs4node to create the new shadow vp for tnm.
7631 7631 * We pass NULL attrs because we just cached attrs for
7632 7632 * the src object. All we're trying to accomplish is to
7633 7633 * to create the new shadow vnode.
7634 7634 */
7635 7635 nvp = makenfs4node(VTOR4(svp)->r_fh, NULL, tdvp->v_vfsp, t, cr,
7636 7636 tdvp, fn_get(VTOSV(tdvp)->sv_name, tnm, VTOR4(svp)->r_fh));
7637 7637
7638 7638 /* Update target cache attribute, readdir and dnlc caches */
7639 7639 dinfo.di_garp = &res.array[4].nfs_resop4_u.opgetattr.ga_res;
7640 7640 dinfo.di_time_call = t;
7641 7641 dinfo.di_cred = cr;
7642 7642
7643 7643 nfs4_update_dircaches(&ln_res->cinfo, tdvp, nvp, tnm, &dinfo);
7644 7644 ASSERT(nfs4_consistent_type(tdvp));
7645 7645 ASSERT(nfs4_consistent_type(svp));
7646 7646 ASSERT(nfs4_consistent_type(nvp));
7647 7647 VN_RELE(nvp);
7648 7648
7649 7649 if (!e.error) {
7650 7650 vnode_t *tvp;
7651 7651 rnode4_t *trp;
7652 7652 /*
7653 7653 * Notify the source file of this link operation.
7654 7654 */
7655 7655 trp = VTOR4(svp);
7656 7656 tvp = svp;
7657 7657 if (IS_SHADOW(svp, trp))
7658 7658 tvp = RTOV4(trp);
7659 7659 vnevent_link(tvp, ct);
7660 7660 }
7661 7661 out:
7662 7662 kmem_free(argop, argoplist_size);
7663 7663 if (resp)
7664 7664 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
7665 7665
7666 7666 nfs_rw_exit(&tdrp->r_rwlock);
7667 7667
7668 7668 return (e.error);
7669 7669 }
7670 7670
7671 7671 /* ARGSUSED */
7672 7672 static int
7673 7673 nfs4_rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr,
7674 7674 caller_context_t *ct, int flags)
7675 7675 {
7676 7676 vnode_t *realvp;
7677 7677
7678 7678 if (nfs_zone() != VTOMI4(odvp)->mi_zone)
7679 7679 return (EPERM);
7680 7680 if (VOP_REALVP(ndvp, &realvp, ct) == 0)
7681 7681 ndvp = realvp;
7682 7682
7683 7683 return (nfs4rename(odvp, onm, ndvp, nnm, cr, ct));
7684 7684 }
7685 7685
7686 7686 /*
7687 7687 * nfs4rename does the real work of renaming in NFS Version 4.
7688 7688 *
7689 7689 * A file handle is considered volatile for renaming purposes if either
7690 7690 * of the volatile bits are turned on. However, the compound may differ
7691 7691 * based on the likelihood of the filehandle to change during rename.
7692 7692 */
7693 7693 static int
7694 7694 nfs4rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr,
7695 7695 caller_context_t *ct)
7696 7696 {
7697 7697 int error;
7698 7698 mntinfo4_t *mi;
7699 7699 vnode_t *nvp = NULL;
7700 7700 vnode_t *ovp = NULL;
7701 7701 char *tmpname = NULL;
7702 7702 rnode4_t *rp;
7703 7703 rnode4_t *odrp;
7704 7704 rnode4_t *ndrp;
7705 7705 int did_link = 0;
7706 7706 int do_link = 1;
7707 7707 nfsstat4 stat = NFS4_OK;
7708 7708
7709 7709 ASSERT(nfs_zone() == VTOMI4(odvp)->mi_zone);
7710 7710 ASSERT(nfs4_consistent_type(odvp));
7711 7711 ASSERT(nfs4_consistent_type(ndvp));
7712 7712
7713 7713 if (onm[0] == '.' && (onm[1] == '\0' ||
7714 7714 (onm[1] == '.' && onm[2] == '\0')))
7715 7715 return (EINVAL);
7716 7716
7717 7717 if (nnm[0] == '.' && (nnm[1] == '\0' ||
7718 7718 (nnm[1] == '.' && nnm[2] == '\0')))
7719 7719 return (EINVAL);
7720 7720
7721 7721 odrp = VTOR4(odvp);
7722 7722 ndrp = VTOR4(ndvp);
7723 7723 if ((intptr_t)odrp < (intptr_t)ndrp) {
7724 7724 if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR4(odvp)))
7725 7725 return (EINTR);
7726 7726 if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR4(ndvp))) {
7727 7727 nfs_rw_exit(&odrp->r_rwlock);
7728 7728 return (EINTR);
7729 7729 }
7730 7730 } else {
7731 7731 if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR4(ndvp)))
7732 7732 return (EINTR);
7733 7733 if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR4(odvp))) {
7734 7734 nfs_rw_exit(&ndrp->r_rwlock);
7735 7735 return (EINTR);
7736 7736 }
7737 7737 }
7738 7738
7739 7739 /*
7740 7740 * Lookup the target file. If it exists, it needs to be
7741 7741 * checked to see whether it is a mount point and whether
7742 7742 * it is active (open).
7743 7743 */
7744 7744 error = nfs4lookup(ndvp, nnm, &nvp, cr, 0);
7745 7745 if (!error) {
7746 7746 int isactive;
7747 7747
7748 7748 ASSERT(nfs4_consistent_type(nvp));
7749 7749 /*
7750 7750 * If this file has been mounted on, then just
7751 7751 * return busy because renaming to it would remove
7752 7752 * the mounted file system from the name space.
7753 7753 */
7754 7754 if (vn_ismntpt(nvp)) {
7755 7755 VN_RELE(nvp);
7756 7756 nfs_rw_exit(&odrp->r_rwlock);
7757 7757 nfs_rw_exit(&ndrp->r_rwlock);
7758 7758 return (EBUSY);
7759 7759 }
7760 7760
7761 7761 /*
7762 7762 * First just remove the entry from the name cache, as it
7763 7763 * is most likely the only entry for this vp.
7764 7764 */
7765 7765 dnlc_remove(ndvp, nnm);
7766 7766
7767 7767 rp = VTOR4(nvp);
7768 7768
7769 7769 if (nvp->v_type != VREG) {
7770 7770 /*
7771 7771 * Purge the name cache of all references to this vnode
7772 7772 * so that we can check the reference count to infer
7773 7773 * whether it is active or not.
7774 7774 */
7775 7775 if (nvp->v_count > 1)
7776 7776 dnlc_purge_vp(nvp);
7777 7777
7778 7778 isactive = nvp->v_count > 1;
7779 7779 } else {
7780 7780 mutex_enter(&rp->r_os_lock);
7781 7781 isactive = list_head(&rp->r_open_streams) != NULL;
7782 7782 mutex_exit(&rp->r_os_lock);
7783 7783 }
7784 7784
7785 7785 /*
7786 7786 * If the vnode is active and is not a directory,
7787 7787 * arrange to rename it to a
7788 7788 * temporary file so that it will continue to be
7789 7789 * accessible. This implements the "unlink-open-file"
7790 7790 * semantics for the target of a rename operation.
7791 7791 * Before doing this though, make sure that the
7792 7792 * source and target files are not already the same.
7793 7793 */
7794 7794 if (isactive && nvp->v_type != VDIR) {
7795 7795 /*
7796 7796 * Lookup the source name.
7797 7797 */
7798 7798 error = nfs4lookup(odvp, onm, &ovp, cr, 0);
7799 7799
7800 7800 /*
7801 7801 * The source name *should* already exist.
7802 7802 */
7803 7803 if (error) {
7804 7804 VN_RELE(nvp);
7805 7805 nfs_rw_exit(&odrp->r_rwlock);
7806 7806 nfs_rw_exit(&ndrp->r_rwlock);
7807 7807 return (error);
7808 7808 }
7809 7809
7810 7810 ASSERT(nfs4_consistent_type(ovp));
7811 7811
7812 7812 /*
7813 7813 * Compare the two vnodes. If they are the same,
7814 7814 * just release all held vnodes and return success.
7815 7815 */
7816 7816 if (VN_CMP(ovp, nvp)) {
7817 7817 VN_RELE(ovp);
7818 7818 VN_RELE(nvp);
7819 7819 nfs_rw_exit(&odrp->r_rwlock);
7820 7820 nfs_rw_exit(&ndrp->r_rwlock);
7821 7821 return (0);
7822 7822 }
7823 7823
7824 7824 /*
7825 7825 * Can't mix and match directories and non-
7826 7826 * directories in rename operations. We already
7827 7827 * know that the target is not a directory. If
7828 7828 * the source is a directory, return an error.
7829 7829 */
7830 7830 if (ovp->v_type == VDIR) {
7831 7831 VN_RELE(ovp);
7832 7832 VN_RELE(nvp);
7833 7833 nfs_rw_exit(&odrp->r_rwlock);
7834 7834 nfs_rw_exit(&ndrp->r_rwlock);
7835 7835 return (ENOTDIR);
7836 7836 }
7837 7837 link_call:
7838 7838 /*
7839 7839 * The target file exists, is not the same as
7840 7840 * the source file, and is active. We first
7841 7841 * try to Link it to a temporary filename to
7842 7842 * avoid having the server removing the file
7843 7843 * completely (which could cause data loss to
7844 7844 * the user's POV in the event the Rename fails
7845 7845 * -- see bug 1165874).
7846 7846 */
7847 7847 /*
7848 7848 * The do_link and did_link booleans are
7849 7849 * introduced in the event we get NFS4ERR_FILE_OPEN
7850 7850 * returned for the Rename. Some servers can
7851 7851 * not Rename over an Open file, so they return
7852 7852 * this error. The client needs to Remove the
7853 7853 * newly created Link and do two Renames, just
7854 7854 * as if the server didn't support LINK.
7855 7855 */
7856 7856 tmpname = newname();
7857 7857 error = 0;
7858 7858
7859 7859 if (do_link) {
7860 7860 error = nfs4_link(ndvp, nvp, tmpname, cr,
7861 7861 NULL, 0);
7862 7862 }
7863 7863 if (error == EOPNOTSUPP || !do_link) {
7864 7864 error = nfs4_rename(ndvp, nnm, ndvp, tmpname,
7865 7865 cr, NULL, 0);
7866 7866 did_link = 0;
7867 7867 } else {
7868 7868 did_link = 1;
7869 7869 }
7870 7870 if (error) {
7871 7871 kmem_free(tmpname, MAXNAMELEN);
7872 7872 VN_RELE(ovp);
7873 7873 VN_RELE(nvp);
7874 7874 nfs_rw_exit(&odrp->r_rwlock);
7875 7875 nfs_rw_exit(&ndrp->r_rwlock);
7876 7876 return (error);
7877 7877 }
7878 7878
7879 7879 mutex_enter(&rp->r_statelock);
7880 7880 if (rp->r_unldvp == NULL) {
7881 7881 VN_HOLD(ndvp);
7882 7882 rp->r_unldvp = ndvp;
7883 7883 if (rp->r_unlcred != NULL)
7884 7884 crfree(rp->r_unlcred);
7885 7885 crhold(cr);
7886 7886 rp->r_unlcred = cr;
7887 7887 rp->r_unlname = tmpname;
7888 7888 } else {
7889 7889 if (rp->r_unlname)
7890 7890 kmem_free(rp->r_unlname, MAXNAMELEN);
7891 7891 rp->r_unlname = tmpname;
7892 7892 }
7893 7893 mutex_exit(&rp->r_statelock);
7894 7894 }
7895 7895
7896 7896 (void) nfs4delegreturn(VTOR4(nvp), NFS4_DR_PUSH|NFS4_DR_REOPEN);
7897 7897
7898 7898 ASSERT(nfs4_consistent_type(nvp));
7899 7899 }
7900 7900
7901 7901 if (ovp == NULL) {
7902 7902 /*
7903 7903 * When renaming directories to be a subdirectory of a
7904 7904 * different parent, the dnlc entry for ".." will no
7905 7905 * longer be valid, so it must be removed.
7906 7906 *
7907 7907 * We do a lookup here to determine whether we are renaming
7908 7908 * a directory and we need to check if we are renaming
7909 7909 * an unlinked file. This might have already been done
7910 7910 * in previous code, so we check ovp == NULL to avoid
7911 7911 * doing it twice.
7912 7912 */
7913 7913 error = nfs4lookup(odvp, onm, &ovp, cr, 0);
7914 7914 /*
7915 7915 * The source name *should* already exist.
7916 7916 */
7917 7917 if (error) {
7918 7918 nfs_rw_exit(&odrp->r_rwlock);
7919 7919 nfs_rw_exit(&ndrp->r_rwlock);
7920 7920 if (nvp) {
7921 7921 VN_RELE(nvp);
7922 7922 }
7923 7923 return (error);
7924 7924 }
7925 7925 ASSERT(ovp != NULL);
7926 7926 ASSERT(nfs4_consistent_type(ovp));
7927 7927 }
7928 7928
7929 7929 /*
7930 7930 * Is the object being renamed a dir, and if so, is
7931 7931 * it being renamed to a child of itself? The underlying
7932 7932 * fs should ultimately return EINVAL for this case;
7933 7933 * however, buggy beta non-Solaris NFSv4 servers at
7934 7934 * interop testing events have allowed this behavior,
7935 7935 * and it caused our client to panic due to a recursive
7936 7936 * mutex_enter in fn_move.
7937 7937 *
7938 7938 * The tedious locking in fn_move could be changed to
7939 7939 * deal with this case, and the client could avoid the
7940 7940 * panic; however, the client would just confuse itself
7941 7941 * later and misbehave. A better way to handle the broken
7942 7942 * server is to detect this condition and return EINVAL
7943 7943 * without ever sending the the bogus rename to the server.
7944 7944 * We know the rename is invalid -- just fail it now.
7945 7945 */
7946 7946 if (ovp->v_type == VDIR && VN_CMP(ndvp, ovp)) {
7947 7947 VN_RELE(ovp);
7948 7948 nfs_rw_exit(&odrp->r_rwlock);
7949 7949 nfs_rw_exit(&ndrp->r_rwlock);
7950 7950 if (nvp) {
7951 7951 VN_RELE(nvp);
7952 7952 }
7953 7953 return (EINVAL);
7954 7954 }
7955 7955
7956 7956 (void) nfs4delegreturn(VTOR4(ovp), NFS4_DR_PUSH|NFS4_DR_REOPEN);
7957 7957
7958 7958 /*
7959 7959 * If FH4_VOL_RENAME or FH4_VOLATILE_ANY bits are set, it is
7960 7960 * possible for the filehandle to change due to the rename.
7961 7961 * If neither of these bits is set, but FH4_VOL_MIGRATION is set,
7962 7962 * the fh will not change because of the rename, but we still need
7963 7963 * to update its rnode entry with the new name for
7964 7964 * an eventual fh change due to migration. The FH4_NOEXPIRE_ON_OPEN
7965 7965 * has no effect on these for now, but for future improvements,
7966 7966 * we might want to use it too to simplify handling of files
7967 7967 * that are open with that flag on. (XXX)
7968 7968 */
7969 7969 mi = VTOMI4(odvp);
7970 7970 if (NFS4_VOLATILE_FH(mi))
7971 7971 error = nfs4rename_volatile_fh(odvp, onm, ovp, ndvp, nnm, cr,
7972 7972 &stat);
7973 7973 else
7974 7974 error = nfs4rename_persistent_fh(odvp, onm, ovp, ndvp, nnm, cr,
7975 7975 &stat);
7976 7976
7977 7977 ASSERT(nfs4_consistent_type(odvp));
7978 7978 ASSERT(nfs4_consistent_type(ndvp));
7979 7979 ASSERT(nfs4_consistent_type(ovp));
7980 7980
7981 7981 if (stat == NFS4ERR_FILE_OPEN && did_link) {
7982 7982 do_link = 0;
7983 7983 /*
7984 7984 * Before the 'link_call' code, we did a nfs4_lookup
7985 7985 * that puts a VN_HOLD on nvp. After the nfs4_link
7986 7986 * call we call VN_RELE to match that hold. We need
7987 7987 * to place an additional VN_HOLD here since we will
7988 7988 * be hitting that VN_RELE again.
7989 7989 */
7990 7990 VN_HOLD(nvp);
7991 7991
7992 7992 (void) nfs4_remove(ndvp, tmpname, cr, NULL, 0);
7993 7993
7994 7994 /* Undo the unlinked file naming stuff we just did */
7995 7995 mutex_enter(&rp->r_statelock);
7996 7996 if (rp->r_unldvp) {
7997 7997 VN_RELE(ndvp);
7998 7998 rp->r_unldvp = NULL;
7999 7999 if (rp->r_unlcred != NULL)
8000 8000 crfree(rp->r_unlcred);
8001 8001 rp->r_unlcred = NULL;
8002 8002 /* rp->r_unlanme points to tmpname */
8003 8003 if (rp->r_unlname)
8004 8004 kmem_free(rp->r_unlname, MAXNAMELEN);
8005 8005 rp->r_unlname = NULL;
8006 8006 }
8007 8007 mutex_exit(&rp->r_statelock);
8008 8008
8009 8009 if (nvp) {
8010 8010 VN_RELE(nvp);
8011 8011 }
8012 8012 goto link_call;
8013 8013 }
8014 8014
8015 8015 if (error) {
8016 8016 VN_RELE(ovp);
8017 8017 nfs_rw_exit(&odrp->r_rwlock);
8018 8018 nfs_rw_exit(&ndrp->r_rwlock);
8019 8019 if (nvp) {
8020 8020 VN_RELE(nvp);
8021 8021 }
8022 8022 return (error);
8023 8023 }
8024 8024
8025 8025 /*
8026 8026 * when renaming directories to be a subdirectory of a
8027 8027 * different parent, the dnlc entry for ".." will no
8028 8028 * longer be valid, so it must be removed
8029 8029 */
8030 8030 rp = VTOR4(ovp);
8031 8031 if (ndvp != odvp) {
8032 8032 if (ovp->v_type == VDIR) {
8033 8033 dnlc_remove(ovp, "..");
8034 8034 if (rp->r_dir != NULL)
8035 8035 nfs4_purge_rddir_cache(ovp);
8036 8036 }
8037 8037 }
8038 8038
8039 8039 /*
8040 8040 * If we are renaming the unlinked file, update the
8041 8041 * r_unldvp and r_unlname as needed.
8042 8042 */
8043 8043 mutex_enter(&rp->r_statelock);
8044 8044 if (rp->r_unldvp != NULL) {
8045 8045 if (strcmp(rp->r_unlname, onm) == 0) {
8046 8046 (void) strncpy(rp->r_unlname, nnm, MAXNAMELEN);
8047 8047 rp->r_unlname[MAXNAMELEN - 1] = '\0';
8048 8048 if (ndvp != rp->r_unldvp) {
8049 8049 VN_RELE(rp->r_unldvp);
8050 8050 rp->r_unldvp = ndvp;
8051 8051 VN_HOLD(ndvp);
8052 8052 }
8053 8053 }
8054 8054 }
8055 8055 mutex_exit(&rp->r_statelock);
8056 8056
8057 8057 /*
8058 8058 * Notify the rename vnevents to source vnode, and to the target
8059 8059 * vnode if it already existed.
8060 8060 */
8061 8061 if (error == 0) {
8062 8062 vnode_t *tvp;
8063 8063 rnode4_t *trp;
8064 8064 /*
8065 8065 * Notify the vnode. Each links is represented by
8066 8066 * a different vnode, in nfsv4.
8067 8067 */
8068 8068 if (nvp) {
8069 8069 trp = VTOR4(nvp);
8070 8070 tvp = nvp;
8071 8071 if (IS_SHADOW(nvp, trp))
8072 8072 tvp = RTOV4(trp);
8073 8073 vnevent_rename_dest(tvp, ndvp, nnm, ct);
8074 8074 }
8075 8075
8076 8076 /*
8077 8077 * if the source and destination directory are not the
8078 8078 * same notify the destination directory.
8079 8079 */
8080 8080 if (VTOR4(odvp) != VTOR4(ndvp)) {
8081 8081 trp = VTOR4(ndvp);
8082 8082 tvp = ndvp;
8083 8083 if (IS_SHADOW(ndvp, trp))
8084 8084 tvp = RTOV4(trp);
8085 8085 vnevent_rename_dest_dir(tvp, ct);
8086 8086 }
8087 8087
8088 8088 trp = VTOR4(ovp);
8089 8089 tvp = ovp;
8090 8090 if (IS_SHADOW(ovp, trp))
8091 8091 tvp = RTOV4(trp);
8092 8092 vnevent_rename_src(tvp, odvp, onm, ct);
8093 8093 }
8094 8094
8095 8095 if (nvp) {
8096 8096 VN_RELE(nvp);
8097 8097 }
8098 8098 VN_RELE(ovp);
8099 8099
8100 8100 nfs_rw_exit(&odrp->r_rwlock);
8101 8101 nfs_rw_exit(&ndrp->r_rwlock);
8102 8102
8103 8103 return (error);
8104 8104 }
8105 8105
8106 8106 /*
8107 8107 * When the parent directory has changed, sv_dfh must be updated
8108 8108 */
8109 8109 static void
8110 8110 update_parentdir_sfh(vnode_t *vp, vnode_t *ndvp)
8111 8111 {
8112 8112 svnode_t *sv = VTOSV(vp);
8113 8113 nfs4_sharedfh_t *old_dfh = sv->sv_dfh;
8114 8114 nfs4_sharedfh_t *new_dfh = VTOR4(ndvp)->r_fh;
8115 8115
8116 8116 sfh4_hold(new_dfh);
8117 8117 sv->sv_dfh = new_dfh;
8118 8118 sfh4_rele(&old_dfh);
8119 8119 }
8120 8120
8121 8121 /*
8122 8122 * nfs4rename_persistent does the otw portion of renaming in NFS Version 4,
8123 8123 * when it is known that the filehandle is persistent through rename.
8124 8124 *
8125 8125 * Rename requires that the current fh be the target directory and the
8126 8126 * saved fh be the source directory. After the operation, the current fh
8127 8127 * is unchanged.
8128 8128 * The compound op structure for persistent fh rename is:
8129 8129 * PUTFH(sourcdir), SAVEFH, PUTFH(targetdir), RENAME
8130 8130 * Rather than bother with the directory postop args, we'll simply
8131 8131 * update that a change occurred in the cache, so no post-op getattrs.
8132 8132 */
8133 8133 static int
8134 8134 nfs4rename_persistent_fh(vnode_t *odvp, char *onm, vnode_t *renvp,
8135 8135 vnode_t *ndvp, char *nnm, cred_t *cr, nfsstat4 *statp)
8136 8136 {
8137 8137 COMPOUND4args_clnt args;
8138 8138 COMPOUND4res_clnt res, *resp = NULL;
8139 8139 nfs_argop4 *argop;
8140 8140 nfs_resop4 *resop;
8141 8141 int doqueue, argoplist_size;
8142 8142 mntinfo4_t *mi;
8143 8143 rnode4_t *odrp = VTOR4(odvp);
8144 8144 rnode4_t *ndrp = VTOR4(ndvp);
8145 8145 RENAME4res *rn_res;
8146 8146 bool_t needrecov;
8147 8147 nfs4_recov_state_t recov_state;
8148 8148 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
8149 8149 dirattr_info_t dinfo, *dinfop;
8150 8150
8151 8151 ASSERT(nfs_zone() == VTOMI4(odvp)->mi_zone);
8152 8152
8153 8153 recov_state.rs_flags = 0;
8154 8154 recov_state.rs_num_retry_despite_err = 0;
8155 8155
8156 8156 /*
8157 8157 * Rename ops: putfh sdir; savefh; putfh tdir; rename; getattr tdir
8158 8158 *
8159 8159 * If source/target are different dirs, then append putfh(src); getattr
8160 8160 */
8161 8161 args.array_len = (odvp == ndvp) ? 5 : 7;
8162 8162 argoplist_size = args.array_len * sizeof (nfs_argop4);
8163 8163 args.array = argop = kmem_alloc(argoplist_size, KM_SLEEP);
8164 8164
8165 8165 recov_retry:
8166 8166 *statp = NFS4_OK;
8167 8167
8168 8168 /* No need to Lookup the file, persistent fh */
8169 8169 args.ctag = TAG_RENAME;
8170 8170
8171 8171 mi = VTOMI4(odvp);
8172 8172 e.error = nfs4_start_op(mi, odvp, ndvp, &recov_state);
8173 8173 if (e.error) {
8174 8174 kmem_free(argop, argoplist_size);
8175 8175 return (e.error);
8176 8176 }
8177 8177
8178 8178 /* 0: putfh source directory */
8179 8179 argop[0].argop = OP_CPUTFH;
8180 8180 argop[0].nfs_argop4_u.opcputfh.sfh = odrp->r_fh;
8181 8181
8182 8182 /* 1: Save source fh to free up current for target */
8183 8183 argop[1].argop = OP_SAVEFH;
8184 8184
8185 8185 /* 2: putfh targetdir */
8186 8186 argop[2].argop = OP_CPUTFH;
8187 8187 argop[2].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh;
8188 8188
8189 8189 /* 3: current_fh is targetdir, saved_fh is sourcedir */
8190 8190 argop[3].argop = OP_CRENAME;
8191 8191 argop[3].nfs_argop4_u.opcrename.coldname = onm;
8192 8192 argop[3].nfs_argop4_u.opcrename.cnewname = nnm;
8193 8193
8194 8194 /* 4: getattr (targetdir) */
8195 8195 argop[4].argop = OP_GETATTR;
8196 8196 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
8197 8197 argop[4].nfs_argop4_u.opgetattr.mi = mi;
8198 8198
8199 8199 if (ndvp != odvp) {
8200 8200
8201 8201 /* 5: putfh (sourcedir) */
8202 8202 argop[5].argop = OP_CPUTFH;
8203 8203 argop[5].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh;
8204 8204
8205 8205 /* 6: getattr (sourcedir) */
8206 8206 argop[6].argop = OP_GETATTR;
8207 8207 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
8208 8208 argop[6].nfs_argop4_u.opgetattr.mi = mi;
8209 8209 }
8210 8210
8211 8211 dnlc_remove(odvp, onm);
8212 8212 dnlc_remove(ndvp, nnm);
8213 8213
8214 8214 doqueue = 1;
8215 8215 dinfo.di_time_call = gethrtime();
8216 8216 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
8217 8217
8218 8218 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
8219 8219 if (e.error) {
8220 8220 PURGE_ATTRCACHE4(odvp);
8221 8221 PURGE_ATTRCACHE4(ndvp);
8222 8222 } else {
8223 8223 *statp = res.status;
8224 8224 }
8225 8225
8226 8226 if (needrecov) {
8227 8227 if (nfs4_start_recovery(&e, mi, odvp, ndvp, NULL, NULL,
8228 8228 OP_RENAME, NULL, NULL, NULL) == FALSE) {
8229 8229 nfs4_end_op(mi, odvp, ndvp, &recov_state, needrecov);
8230 8230 if (!e.error)
8231 8231 (void) xdr_free(xdr_COMPOUND4res_clnt,
8232 8232 (caddr_t)&res);
8233 8233 goto recov_retry;
8234 8234 }
8235 8235 }
8236 8236
8237 8237 if (!e.error) {
8238 8238 resp = &res;
8239 8239 /*
8240 8240 * as long as OP_RENAME
8241 8241 */
8242 8242 if (res.status != NFS4_OK && res.array_len <= 4) {
8243 8243 e.error = geterrno4(res.status);
8244 8244 PURGE_ATTRCACHE4(odvp);
8245 8245 PURGE_ATTRCACHE4(ndvp);
8246 8246 /*
8247 8247 * System V defines rename to return EEXIST, not
8248 8248 * ENOTEMPTY if the target directory is not empty.
8249 8249 * Over the wire, the error is NFSERR_ENOTEMPTY
8250 8250 * which geterrno4 maps to ENOTEMPTY.
8251 8251 */
8252 8252 if (e.error == ENOTEMPTY)
8253 8253 e.error = EEXIST;
8254 8254 } else {
8255 8255
8256 8256 resop = &res.array[3]; /* rename res */
8257 8257 rn_res = &resop->nfs_resop4_u.oprename;
8258 8258
8259 8259 if (res.status == NFS4_OK) {
8260 8260 /*
8261 8261 * Update target attribute, readdir and dnlc
8262 8262 * caches.
8263 8263 */
8264 8264 dinfo.di_garp =
8265 8265 &res.array[4].nfs_resop4_u.opgetattr.ga_res;
8266 8266 dinfo.di_cred = cr;
8267 8267 dinfop = &dinfo;
8268 8268 } else
8269 8269 dinfop = NULL;
8270 8270
8271 8271 nfs4_update_dircaches(&rn_res->target_cinfo,
8272 8272 ndvp, NULL, NULL, dinfop);
8273 8273
8274 8274 /*
8275 8275 * Update source attribute, readdir and dnlc caches
8276 8276 *
8277 8277 */
8278 8278 if (ndvp != odvp) {
8279 8279 update_parentdir_sfh(renvp, ndvp);
8280 8280
8281 8281 if (dinfop)
8282 8282 dinfo.di_garp =
8283 8283 &(res.array[6].nfs_resop4_u.
8284 8284 opgetattr.ga_res);
8285 8285
8286 8286 nfs4_update_dircaches(&rn_res->source_cinfo,
8287 8287 odvp, NULL, NULL, dinfop);
8288 8288 }
8289 8289
8290 8290 fn_move(VTOSV(renvp)->sv_name, VTOSV(ndvp)->sv_name,
8291 8291 nnm);
8292 8292 }
8293 8293 }
8294 8294
8295 8295 if (resp)
8296 8296 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
8297 8297 nfs4_end_op(mi, odvp, ndvp, &recov_state, needrecov);
8298 8298 kmem_free(argop, argoplist_size);
8299 8299
8300 8300 return (e.error);
8301 8301 }
8302 8302
8303 8303 /*
8304 8304 * nfs4rename_volatile_fh does the otw part of renaming in NFS Version 4, when
8305 8305 * it is possible for the filehandle to change due to the rename.
8306 8306 *
8307 8307 * The compound req in this case includes a post-rename lookup and getattr
8308 8308 * to ensure that we have the correct fh and attributes for the object.
8309 8309 *
8310 8310 * Rename requires that the current fh be the target directory and the
8311 8311 * saved fh be the source directory. After the operation, the current fh
8312 8312 * is unchanged.
8313 8313 *
8314 8314 * We need the new filehandle (hence a LOOKUP and GETFH) so that we can
8315 8315 * update the filehandle for the renamed object. We also get the old
8316 8316 * filehandle for historical reasons; this should be taken out sometime.
8317 8317 * This results in a rather cumbersome compound...
8318 8318 *
8319 8319 * PUTFH(sourcdir), SAVEFH, LOOKUP(src), GETFH(old),
8320 8320 * PUTFH(targetdir), RENAME, LOOKUP(trgt), GETFH(new), GETATTR
8321 8321 *
8322 8322 */
8323 8323 static int
8324 8324 nfs4rename_volatile_fh(vnode_t *odvp, char *onm, vnode_t *ovp,
8325 8325 vnode_t *ndvp, char *nnm, cred_t *cr, nfsstat4 *statp)
8326 8326 {
8327 8327 COMPOUND4args_clnt args;
8328 8328 COMPOUND4res_clnt res, *resp = NULL;
8329 8329 int argoplist_size;
8330 8330 nfs_argop4 *argop;
8331 8331 nfs_resop4 *resop;
8332 8332 int doqueue;
8333 8333 mntinfo4_t *mi;
8334 8334 rnode4_t *odrp = VTOR4(odvp); /* old directory */
8335 8335 rnode4_t *ndrp = VTOR4(ndvp); /* new directory */
8336 8336 rnode4_t *orp = VTOR4(ovp); /* object being renamed */
8337 8337 RENAME4res *rn_res;
8338 8338 GETFH4res *ngf_res;
8339 8339 bool_t needrecov;
8340 8340 nfs4_recov_state_t recov_state;
8341 8341 hrtime_t t;
8342 8342 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
8343 8343 dirattr_info_t dinfo, *dinfop = &dinfo;
8344 8344
8345 8345 ASSERT(nfs_zone() == VTOMI4(odvp)->mi_zone);
8346 8346
8347 8347 recov_state.rs_flags = 0;
8348 8348 recov_state.rs_num_retry_despite_err = 0;
8349 8349
8350 8350 recov_retry:
8351 8351 *statp = NFS4_OK;
8352 8352
8353 8353 /*
8354 8354 * There is a window between the RPC and updating the path and
8355 8355 * filehandle stored in the rnode. Lock out the FHEXPIRED recovery
8356 8356 * code, so that it doesn't try to use the old path during that
8357 8357 * window.
8358 8358 */
8359 8359 mutex_enter(&orp->r_statelock);
8360 8360 while (orp->r_flags & R4RECEXPFH) {
8361 8361 klwp_t *lwp = ttolwp(curthread);
8362 8362
8363 8363 if (lwp != NULL)
8364 8364 lwp->lwp_nostop++;
8365 8365 if (cv_wait_sig(&orp->r_cv, &orp->r_statelock) == 0) {
8366 8366 mutex_exit(&orp->r_statelock);
8367 8367 if (lwp != NULL)
8368 8368 lwp->lwp_nostop--;
8369 8369 return (EINTR);
8370 8370 }
8371 8371 if (lwp != NULL)
8372 8372 lwp->lwp_nostop--;
8373 8373 }
8374 8374 orp->r_flags |= R4RECEXPFH;
8375 8375 mutex_exit(&orp->r_statelock);
8376 8376
8377 8377 mi = VTOMI4(odvp);
8378 8378
8379 8379 args.ctag = TAG_RENAME_VFH;
8380 8380 args.array_len = (odvp == ndvp) ? 10 : 12;
8381 8381 argoplist_size = args.array_len * sizeof (nfs_argop4);
8382 8382 argop = kmem_alloc(argoplist_size, KM_SLEEP);
8383 8383
8384 8384 /*
8385 8385 * Rename ops:
8386 8386 * PUTFH(sourcdir), SAVEFH, LOOKUP(src), GETFH(old),
8387 8387 * PUTFH(targetdir), RENAME, GETATTR(targetdir)
8388 8388 * LOOKUP(trgt), GETFH(new), GETATTR,
8389 8389 *
8390 8390 * if (odvp != ndvp)
8391 8391 * add putfh(sourcedir), getattr(sourcedir) }
8392 8392 */
8393 8393 args.array = argop;
8394 8394
8395 8395 e.error = nfs4_start_fop(mi, odvp, ndvp, OH_VFH_RENAME,
8396 8396 &recov_state, NULL);
8397 8397 if (e.error) {
8398 8398 kmem_free(argop, argoplist_size);
8399 8399 mutex_enter(&orp->r_statelock);
8400 8400 orp->r_flags &= ~R4RECEXPFH;
8401 8401 cv_broadcast(&orp->r_cv);
8402 8402 mutex_exit(&orp->r_statelock);
8403 8403 return (e.error);
8404 8404 }
8405 8405
8406 8406 /* 0: putfh source directory */
8407 8407 argop[0].argop = OP_CPUTFH;
8408 8408 argop[0].nfs_argop4_u.opcputfh.sfh = odrp->r_fh;
8409 8409
8410 8410 /* 1: Save source fh to free up current for target */
8411 8411 argop[1].argop = OP_SAVEFH;
8412 8412
8413 8413 /* 2: Lookup pre-rename fh of renamed object */
8414 8414 argop[2].argop = OP_CLOOKUP;
8415 8415 argop[2].nfs_argop4_u.opclookup.cname = onm;
8416 8416
8417 8417 /* 3: getfh fh of renamed object (before rename) */
8418 8418 argop[3].argop = OP_GETFH;
8419 8419
8420 8420 /* 4: putfh targetdir */
8421 8421 argop[4].argop = OP_CPUTFH;
8422 8422 argop[4].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh;
8423 8423
8424 8424 /* 5: current_fh is targetdir, saved_fh is sourcedir */
8425 8425 argop[5].argop = OP_CRENAME;
8426 8426 argop[5].nfs_argop4_u.opcrename.coldname = onm;
8427 8427 argop[5].nfs_argop4_u.opcrename.cnewname = nnm;
8428 8428
8429 8429 /* 6: getattr of target dir (post op attrs) */
8430 8430 argop[6].argop = OP_GETATTR;
8431 8431 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
8432 8432 argop[6].nfs_argop4_u.opgetattr.mi = mi;
8433 8433
8434 8434 /* 7: Lookup post-rename fh of renamed object */
8435 8435 argop[7].argop = OP_CLOOKUP;
8436 8436 argop[7].nfs_argop4_u.opclookup.cname = nnm;
8437 8437
8438 8438 /* 8: getfh fh of renamed object (after rename) */
8439 8439 argop[8].argop = OP_GETFH;
8440 8440
8441 8441 /* 9: getattr of renamed object */
8442 8442 argop[9].argop = OP_GETATTR;
8443 8443 argop[9].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
8444 8444 argop[9].nfs_argop4_u.opgetattr.mi = mi;
8445 8445
8446 8446 /*
8447 8447 * If source/target dirs are different, then get new post-op
8448 8448 * attrs for source dir also.
8449 8449 */
8450 8450 if (ndvp != odvp) {
8451 8451 /* 10: putfh (sourcedir) */
8452 8452 argop[10].argop = OP_CPUTFH;
8453 8453 argop[10].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh;
8454 8454
8455 8455 /* 11: getattr (sourcedir) */
8456 8456 argop[11].argop = OP_GETATTR;
8457 8457 argop[11].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
8458 8458 argop[11].nfs_argop4_u.opgetattr.mi = mi;
8459 8459 }
8460 8460
8461 8461 dnlc_remove(odvp, onm);
8462 8462 dnlc_remove(ndvp, nnm);
8463 8463
8464 8464 doqueue = 1;
8465 8465 t = gethrtime();
8466 8466 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
8467 8467
8468 8468 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
8469 8469 if (e.error) {
8470 8470 PURGE_ATTRCACHE4(odvp);
8471 8471 PURGE_ATTRCACHE4(ndvp);
8472 8472 if (!needrecov) {
8473 8473 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME,
8474 8474 &recov_state, needrecov);
8475 8475 goto out;
8476 8476 }
8477 8477 } else {
8478 8478 *statp = res.status;
8479 8479 }
8480 8480
8481 8481 if (needrecov) {
8482 8482 bool_t abort;
8483 8483
8484 8484 abort = nfs4_start_recovery(&e, mi, odvp, ndvp, NULL, NULL,
8485 8485 OP_RENAME, NULL, NULL, NULL);
8486 8486 if (abort == FALSE) {
8487 8487 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME,
8488 8488 &recov_state, needrecov);
8489 8489 kmem_free(argop, argoplist_size);
8490 8490 if (!e.error)
8491 8491 (void) xdr_free(xdr_COMPOUND4res_clnt,
8492 8492 (caddr_t)&res);
8493 8493 mutex_enter(&orp->r_statelock);
8494 8494 orp->r_flags &= ~R4RECEXPFH;
8495 8495 cv_broadcast(&orp->r_cv);
8496 8496 mutex_exit(&orp->r_statelock);
8497 8497 goto recov_retry;
8498 8498 } else {
8499 8499 if (e.error != 0) {
8500 8500 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME,
8501 8501 &recov_state, needrecov);
8502 8502 goto out;
8503 8503 }
8504 8504 /* fall through for res.status case */
8505 8505 }
8506 8506 }
8507 8507
8508 8508 resp = &res;
8509 8509 /*
8510 8510 * If OP_RENAME (or any prev op) failed, then return an error.
8511 8511 * OP_RENAME is index 5, so if array len <= 6 we return an error.
8512 8512 */
8513 8513 if ((res.status != NFS4_OK) && (res.array_len <= 6)) {
8514 8514 /*
8515 8515 * Error in an op other than last Getattr
8516 8516 */
8517 8517 e.error = geterrno4(res.status);
8518 8518 PURGE_ATTRCACHE4(odvp);
8519 8519 PURGE_ATTRCACHE4(ndvp);
8520 8520 /*
8521 8521 * System V defines rename to return EEXIST, not
8522 8522 * ENOTEMPTY if the target directory is not empty.
8523 8523 * Over the wire, the error is NFSERR_ENOTEMPTY
8524 8524 * which geterrno4 maps to ENOTEMPTY.
8525 8525 */
8526 8526 if (e.error == ENOTEMPTY)
8527 8527 e.error = EEXIST;
8528 8528 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, &recov_state,
8529 8529 needrecov);
8530 8530 goto out;
8531 8531 }
8532 8532
8533 8533 /* rename results */
8534 8534 rn_res = &res.array[5].nfs_resop4_u.oprename;
8535 8535
8536 8536 if (res.status == NFS4_OK) {
8537 8537 /* Update target attribute, readdir and dnlc caches */
8538 8538 dinfo.di_garp =
8539 8539 &res.array[6].nfs_resop4_u.opgetattr.ga_res;
8540 8540 dinfo.di_cred = cr;
8541 8541 dinfo.di_time_call = t;
8542 8542 } else
8543 8543 dinfop = NULL;
8544 8544
8545 8545 /* Update source cache attribute, readdir and dnlc caches */
8546 8546 nfs4_update_dircaches(&rn_res->target_cinfo, ndvp, NULL, NULL, dinfop);
8547 8547
8548 8548 /* Update source cache attribute, readdir and dnlc caches */
8549 8549 if (ndvp != odvp) {
8550 8550 update_parentdir_sfh(ovp, ndvp);
8551 8551
8552 8552 /*
8553 8553 * If dinfop is non-NULL, then compound succeded, so
8554 8554 * set di_garp to attrs for source dir. dinfop is only
8555 8555 * set to NULL when compound fails.
8556 8556 */
8557 8557 if (dinfop)
8558 8558 dinfo.di_garp =
8559 8559 &res.array[11].nfs_resop4_u.opgetattr.ga_res;
8560 8560 nfs4_update_dircaches(&rn_res->source_cinfo, odvp, NULL, NULL,
8561 8561 dinfop);
8562 8562 }
8563 8563
8564 8564 /*
8565 8565 * Update the rnode with the new component name and args,
8566 8566 * and if the file handle changed, also update it with the new fh.
8567 8567 * This is only necessary if the target object has an rnode
8568 8568 * entry and there is no need to create one for it.
8569 8569 */
8570 8570 resop = &res.array[8]; /* getfh new res */
8571 8571 ngf_res = &resop->nfs_resop4_u.opgetfh;
8572 8572
8573 8573 /*
8574 8574 * Update the path and filehandle for the renamed object.
8575 8575 */
8576 8576 nfs4rename_update(ovp, ndvp, &ngf_res->object, nnm);
8577 8577
8578 8578 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, &recov_state, needrecov);
8579 8579
8580 8580 if (res.status == NFS4_OK) {
8581 8581 resop++; /* getattr res */
8582 8582 e.error = nfs4_update_attrcache(res.status,
8583 8583 &resop->nfs_resop4_u.opgetattr.ga_res,
8584 8584 t, ovp, cr);
8585 8585 }
8586 8586
8587 8587 out:
8588 8588 kmem_free(argop, argoplist_size);
8589 8589 if (resp)
8590 8590 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
8591 8591 mutex_enter(&orp->r_statelock);
8592 8592 orp->r_flags &= ~R4RECEXPFH;
8593 8593 cv_broadcast(&orp->r_cv);
8594 8594 mutex_exit(&orp->r_statelock);
8595 8595
8596 8596 return (e.error);
8597 8597 }
8598 8598
8599 8599 /* ARGSUSED */
8600 8600 static int
8601 8601 nfs4_mkdir(vnode_t *dvp, char *nm, struct vattr *va, vnode_t **vpp, cred_t *cr,
8602 8602 caller_context_t *ct, int flags, vsecattr_t *vsecp)
8603 8603 {
8604 8604 int error;
8605 8605 vnode_t *vp;
8606 8606
8607 8607 if (nfs_zone() != VTOMI4(dvp)->mi_zone)
8608 8608 return (EPERM);
8609 8609 /*
8610 8610 * As ".." has special meaning and rather than send a mkdir
8611 8611 * over the wire to just let the server freak out, we just
8612 8612 * short circuit it here and return EEXIST
8613 8613 */
8614 8614 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0')
8615 8615 return (EEXIST);
8616 8616
8617 8617 /*
8618 8618 * Decision to get the right gid and setgid bit of the
8619 8619 * new directory is now made in call_nfs4_create_req.
8620 8620 */
8621 8621 va->va_mask |= AT_MODE;
8622 8622 error = call_nfs4_create_req(dvp, nm, NULL, va, &vp, cr, NF4DIR);
8623 8623 if (error)
8624 8624 return (error);
8625 8625
8626 8626 *vpp = vp;
8627 8627 return (0);
8628 8628 }
8629 8629
8630 8630
8631 8631 /*
8632 8632 * rmdir is using the same remove v4 op as does remove.
8633 8633 * Remove requires that the current fh be the target directory.
8634 8634 * After the operation, the current fh is unchanged.
8635 8635 * The compound op structure is:
8636 8636 * PUTFH(targetdir), REMOVE
8637 8637 */
8638 8638 /*ARGSUSED4*/
8639 8639 static int
8640 8640 nfs4_rmdir(vnode_t *dvp, char *nm, vnode_t *cdir, cred_t *cr,
8641 8641 caller_context_t *ct, int flags)
8642 8642 {
8643 8643 int need_end_op = FALSE;
8644 8644 COMPOUND4args_clnt args;
8645 8645 COMPOUND4res_clnt res, *resp = NULL;
8646 8646 REMOVE4res *rm_res;
8647 8647 nfs_argop4 argop[3];
8648 8648 nfs_resop4 *resop;
8649 8649 vnode_t *vp;
8650 8650 int doqueue;
8651 8651 mntinfo4_t *mi;
8652 8652 rnode4_t *drp;
8653 8653 bool_t needrecov = FALSE;
8654 8654 nfs4_recov_state_t recov_state;
8655 8655 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
8656 8656 dirattr_info_t dinfo, *dinfop;
8657 8657
8658 8658 if (nfs_zone() != VTOMI4(dvp)->mi_zone)
8659 8659 return (EPERM);
8660 8660 /*
8661 8661 * As ".." has special meaning and rather than send a rmdir
8662 8662 * over the wire to just let the server freak out, we just
8663 8663 * short circuit it here and return EEXIST
8664 8664 */
8665 8665 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0')
8666 8666 return (EEXIST);
8667 8667
8668 8668 drp = VTOR4(dvp);
8669 8669 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp)))
8670 8670 return (EINTR);
8671 8671
8672 8672 /*
8673 8673 * Attempt to prevent a rmdir(".") from succeeding.
8674 8674 */
8675 8675 e.error = nfs4lookup(dvp, nm, &vp, cr, 0);
8676 8676 if (e.error) {
8677 8677 nfs_rw_exit(&drp->r_rwlock);
8678 8678 return (e.error);
8679 8679 }
8680 8680 if (vp == cdir) {
8681 8681 VN_RELE(vp);
8682 8682 nfs_rw_exit(&drp->r_rwlock);
8683 8683 return (EINVAL);
8684 8684 }
8685 8685
8686 8686 /*
8687 8687 * Since nfsv4 remove op works on both files and directories,
8688 8688 * check that the removed object is indeed a directory.
8689 8689 */
8690 8690 if (vp->v_type != VDIR) {
8691 8691 VN_RELE(vp);
8692 8692 nfs_rw_exit(&drp->r_rwlock);
8693 8693 return (ENOTDIR);
8694 8694 }
8695 8695
8696 8696 /*
8697 8697 * First just remove the entry from the name cache, as it
8698 8698 * is most likely an entry for this vp.
8699 8699 */
8700 8700 dnlc_remove(dvp, nm);
8701 8701
8702 8702 /*
8703 8703 * If there vnode reference count is greater than one, then
8704 8704 * there may be additional references in the DNLC which will
8705 8705 * need to be purged. First, trying removing the entry for
8706 8706 * the parent directory and see if that removes the additional
8707 8707 * reference(s). If that doesn't do it, then use dnlc_purge_vp
8708 8708 * to completely remove any references to the directory which
8709 8709 * might still exist in the DNLC.
8710 8710 */
8711 8711 if (vp->v_count > 1) {
8712 8712 dnlc_remove(vp, "..");
8713 8713 if (vp->v_count > 1)
8714 8714 dnlc_purge_vp(vp);
8715 8715 }
8716 8716
8717 8717 mi = VTOMI4(dvp);
8718 8718 recov_state.rs_flags = 0;
8719 8719 recov_state.rs_num_retry_despite_err = 0;
8720 8720
8721 8721 recov_retry:
8722 8722 args.ctag = TAG_RMDIR;
8723 8723
8724 8724 /*
8725 8725 * Rmdir ops: putfh dir; remove
8726 8726 */
8727 8727 args.array_len = 3;
8728 8728 args.array = argop;
8729 8729
8730 8730 e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state);
8731 8731 if (e.error) {
8732 8732 nfs_rw_exit(&drp->r_rwlock);
8733 8733 return (e.error);
8734 8734 }
8735 8735 need_end_op = TRUE;
8736 8736
8737 8737 /* putfh directory */
8738 8738 argop[0].argop = OP_CPUTFH;
8739 8739 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
8740 8740
8741 8741 /* remove */
8742 8742 argop[1].argop = OP_CREMOVE;
8743 8743 argop[1].nfs_argop4_u.opcremove.ctarget = nm;
8744 8744
8745 8745 /* getattr (postop attrs for dir that contained removed dir) */
8746 8746 argop[2].argop = OP_GETATTR;
8747 8747 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
8748 8748 argop[2].nfs_argop4_u.opgetattr.mi = mi;
8749 8749
8750 8750 dinfo.di_time_call = gethrtime();
8751 8751 doqueue = 1;
8752 8752 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
8753 8753
8754 8754 PURGE_ATTRCACHE4(vp);
8755 8755
8756 8756 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
8757 8757 if (e.error) {
8758 8758 PURGE_ATTRCACHE4(dvp);
8759 8759 }
8760 8760
8761 8761 if (needrecov) {
8762 8762 if (nfs4_start_recovery(&e, VTOMI4(dvp), dvp, NULL, NULL,
8763 8763 NULL, OP_REMOVE, NULL, NULL, NULL) == FALSE) {
8764 8764 if (!e.error)
8765 8765 (void) xdr_free(xdr_COMPOUND4res_clnt,
8766 8766 (caddr_t)&res);
8767 8767
8768 8768 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state,
8769 8769 needrecov);
8770 8770 need_end_op = FALSE;
8771 8771 goto recov_retry;
8772 8772 }
8773 8773 }
8774 8774
8775 8775 if (!e.error) {
8776 8776 resp = &res;
8777 8777
8778 8778 /*
8779 8779 * Only return error if first 2 ops (OP_REMOVE or earlier)
8780 8780 * failed.
8781 8781 */
8782 8782 if (res.status != NFS4_OK && res.array_len <= 2) {
8783 8783 e.error = geterrno4(res.status);
8784 8784 PURGE_ATTRCACHE4(dvp);
8785 8785 nfs4_end_op(VTOMI4(dvp), dvp, NULL,
8786 8786 &recov_state, needrecov);
8787 8787 need_end_op = FALSE;
8788 8788 nfs4_purge_stale_fh(e.error, dvp, cr);
8789 8789 /*
8790 8790 * System V defines rmdir to return EEXIST, not
8791 8791 * ENOTEMPTY if the directory is not empty. Over
8792 8792 * the wire, the error is NFSERR_ENOTEMPTY which
8793 8793 * geterrno4 maps to ENOTEMPTY.
8794 8794 */
8795 8795 if (e.error == ENOTEMPTY)
8796 8796 e.error = EEXIST;
8797 8797 } else {
8798 8798 resop = &res.array[1]; /* remove res */
8799 8799 rm_res = &resop->nfs_resop4_u.opremove;
8800 8800
8801 8801 if (res.status == NFS4_OK) {
8802 8802 resop = &res.array[2]; /* dir attrs */
8803 8803 dinfo.di_garp =
8804 8804 &resop->nfs_resop4_u.opgetattr.ga_res;
8805 8805 dinfo.di_cred = cr;
8806 8806 dinfop = &dinfo;
8807 8807 } else
8808 8808 dinfop = NULL;
8809 8809
8810 8810 /* Update dir attribute, readdir and dnlc caches */
8811 8811 nfs4_update_dircaches(&rm_res->cinfo, dvp, NULL, NULL,
8812 8812 dinfop);
8813 8813
8814 8814 /* destroy rddir cache for dir that was removed */
8815 8815 if (VTOR4(vp)->r_dir != NULL)
8816 8816 nfs4_purge_rddir_cache(vp);
8817 8817 }
8818 8818 }
8819 8819
8820 8820 if (need_end_op)
8821 8821 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov);
8822 8822
8823 8823 nfs_rw_exit(&drp->r_rwlock);
8824 8824
8825 8825 if (resp)
8826 8826 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
8827 8827
8828 8828 if (e.error == 0) {
8829 8829 vnode_t *tvp;
8830 8830 rnode4_t *trp;
8831 8831 trp = VTOR4(vp);
8832 8832 tvp = vp;
8833 8833 if (IS_SHADOW(vp, trp))
8834 8834 tvp = RTOV4(trp);
8835 8835 vnevent_rmdir(tvp, dvp, nm, ct);
8836 8836 }
8837 8837
8838 8838 VN_RELE(vp);
8839 8839
8840 8840 return (e.error);
8841 8841 }
8842 8842
8843 8843 /* ARGSUSED */
8844 8844 static int
8845 8845 nfs4_symlink(vnode_t *dvp, char *lnm, struct vattr *tva, char *tnm, cred_t *cr,
8846 8846 caller_context_t *ct, int flags)
8847 8847 {
8848 8848 int error;
8849 8849 vnode_t *vp;
8850 8850 rnode4_t *rp;
8851 8851 char *contents;
8852 8852 mntinfo4_t *mi = VTOMI4(dvp);
8853 8853
8854 8854 if (nfs_zone() != mi->mi_zone)
8855 8855 return (EPERM);
8856 8856 if (!(mi->mi_flags & MI4_SYMLINK))
8857 8857 return (EOPNOTSUPP);
8858 8858
8859 8859 error = call_nfs4_create_req(dvp, lnm, tnm, tva, &vp, cr, NF4LNK);
8860 8860 if (error)
8861 8861 return (error);
8862 8862
8863 8863 ASSERT(nfs4_consistent_type(vp));
8864 8864 rp = VTOR4(vp);
8865 8865 if (nfs4_do_symlink_cache && rp->r_symlink.contents == NULL) {
8866 8866
8867 8867 contents = kmem_alloc(MAXPATHLEN, KM_SLEEP);
8868 8868
8869 8869 if (contents != NULL) {
8870 8870 mutex_enter(&rp->r_statelock);
8871 8871 if (rp->r_symlink.contents == NULL) {
8872 8872 rp->r_symlink.len = strlen(tnm);
8873 8873 bcopy(tnm, contents, rp->r_symlink.len);
8874 8874 rp->r_symlink.contents = contents;
8875 8875 rp->r_symlink.size = MAXPATHLEN;
8876 8876 mutex_exit(&rp->r_statelock);
8877 8877 } else {
8878 8878 mutex_exit(&rp->r_statelock);
8879 8879 kmem_free((void *)contents, MAXPATHLEN);
8880 8880 }
8881 8881 }
8882 8882 }
8883 8883 VN_RELE(vp);
8884 8884
8885 8885 return (error);
8886 8886 }
8887 8887
8888 8888
8889 8889 /*
8890 8890 * Read directory entries.
8891 8891 * There are some weird things to look out for here. The uio_loffset
8892 8892 * field is either 0 or it is the offset returned from a previous
8893 8893 * readdir. It is an opaque value used by the server to find the
8894 8894 * correct directory block to read. The count field is the number
8895 8895 * of blocks to read on the server. This is advisory only, the server
8896 8896 * may return only one block's worth of entries. Entries may be compressed
8897 8897 * on the server.
8898 8898 */
8899 8899 /* ARGSUSED */
8900 8900 static int
8901 8901 nfs4_readdir(vnode_t *vp, struct uio *uiop, cred_t *cr, int *eofp,
8902 8902 caller_context_t *ct, int flags)
8903 8903 {
8904 8904 int error;
8905 8905 uint_t count;
8906 8906 rnode4_t *rp;
8907 8907 rddir4_cache *rdc;
8908 8908 rddir4_cache *rrdc;
8909 8909
8910 8910 if (nfs_zone() != VTOMI4(vp)->mi_zone)
8911 8911 return (EIO);
8912 8912 rp = VTOR4(vp);
8913 8913
8914 8914 ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER));
8915 8915
8916 8916 /*
8917 8917 * Make sure that the directory cache is valid.
8918 8918 */
8919 8919 if (rp->r_dir != NULL) {
8920 8920 if (nfs_disable_rddir_cache != 0) {
8921 8921 /*
8922 8922 * Setting nfs_disable_rddir_cache in /etc/system
8923 8923 * allows interoperability with servers that do not
8924 8924 * properly update the attributes of directories.
8925 8925 * Any cached information gets purged before an
8926 8926 * access is made to it.
8927 8927 */
8928 8928 nfs4_purge_rddir_cache(vp);
8929 8929 }
8930 8930
8931 8931 error = nfs4_validate_caches(vp, cr);
8932 8932 if (error)
8933 8933 return (error);
8934 8934 }
8935 8935
8936 8936 count = MIN(uiop->uio_iov->iov_len, MAXBSIZE);
8937 8937
8938 8938 /*
8939 8939 * Short circuit last readdir which always returns 0 bytes.
8940 8940 * This can be done after the directory has been read through
8941 8941 * completely at least once. This will set r_direof which
8942 8942 * can be used to find the value of the last cookie.
8943 8943 */
8944 8944 mutex_enter(&rp->r_statelock);
8945 8945 if (rp->r_direof != NULL &&
8946 8946 uiop->uio_loffset == rp->r_direof->nfs4_ncookie) {
8947 8947 mutex_exit(&rp->r_statelock);
8948 8948 #ifdef DEBUG
8949 8949 nfs4_readdir_cache_shorts++;
8950 8950 #endif
8951 8951 if (eofp)
8952 8952 *eofp = 1;
8953 8953 return (0);
8954 8954 }
8955 8955
8956 8956 /*
8957 8957 * Look for a cache entry. Cache entries are identified
8958 8958 * by the NFS cookie value and the byte count requested.
8959 8959 */
8960 8960 rdc = rddir4_cache_lookup(rp, uiop->uio_loffset, count);
8961 8961
8962 8962 /*
8963 8963 * If rdc is NULL then the lookup resulted in an unrecoverable error.
8964 8964 */
8965 8965 if (rdc == NULL) {
8966 8966 mutex_exit(&rp->r_statelock);
8967 8967 return (EINTR);
8968 8968 }
8969 8969
8970 8970 /*
8971 8971 * Check to see if we need to fill this entry in.
8972 8972 */
8973 8973 if (rdc->flags & RDDIRREQ) {
8974 8974 rdc->flags &= ~RDDIRREQ;
8975 8975 rdc->flags |= RDDIR;
8976 8976 mutex_exit(&rp->r_statelock);
8977 8977
8978 8978 /*
8979 8979 * Do the readdir.
8980 8980 */
8981 8981 nfs4readdir(vp, rdc, cr);
8982 8982
8983 8983 /*
8984 8984 * Reacquire the lock, so that we can continue
8985 8985 */
8986 8986 mutex_enter(&rp->r_statelock);
8987 8987 /*
8988 8988 * The entry is now complete
8989 8989 */
8990 8990 rdc->flags &= ~RDDIR;
8991 8991 }
8992 8992
8993 8993 ASSERT(!(rdc->flags & RDDIR));
8994 8994
8995 8995 /*
8996 8996 * If an error occurred while attempting
8997 8997 * to fill the cache entry, mark the entry invalid and
8998 8998 * just return the error.
8999 8999 */
9000 9000 if (rdc->error) {
9001 9001 error = rdc->error;
9002 9002 rdc->flags |= RDDIRREQ;
9003 9003 rddir4_cache_rele(rp, rdc);
9004 9004 mutex_exit(&rp->r_statelock);
9005 9005 return (error);
9006 9006 }
9007 9007
9008 9008 /*
9009 9009 * The cache entry is complete and good,
9010 9010 * copyout the dirent structs to the calling
9011 9011 * thread.
9012 9012 */
9013 9013 error = uiomove(rdc->entries, rdc->actlen, UIO_READ, uiop);
9014 9014
9015 9015 /*
9016 9016 * If no error occurred during the copyout,
9017 9017 * update the offset in the uio struct to
9018 9018 * contain the value of the next NFS 4 cookie
9019 9019 * and set the eof value appropriately.
9020 9020 */
9021 9021 if (!error) {
9022 9022 uiop->uio_loffset = rdc->nfs4_ncookie;
9023 9023 if (eofp)
9024 9024 *eofp = rdc->eof;
9025 9025 }
9026 9026
9027 9027 /*
9028 9028 * Decide whether to do readahead. Don't if we
9029 9029 * have already read to the end of directory.
9030 9030 */
9031 9031 if (rdc->eof) {
9032 9032 /*
9033 9033 * Make the entry the direof only if it is cached
9034 9034 */
9035 9035 if (rdc->flags & RDDIRCACHED)
9036 9036 rp->r_direof = rdc;
9037 9037 rddir4_cache_rele(rp, rdc);
9038 9038 mutex_exit(&rp->r_statelock);
9039 9039 return (error);
9040 9040 }
9041 9041
9042 9042 /* Determine if a readdir readahead should be done */
9043 9043 if (!(rp->r_flags & R4LOOKUP)) {
9044 9044 rddir4_cache_rele(rp, rdc);
9045 9045 mutex_exit(&rp->r_statelock);
9046 9046 return (error);
9047 9047 }
9048 9048
9049 9049 /*
9050 9050 * Now look for a readahead entry.
9051 9051 *
9052 9052 * Check to see whether we found an entry for the readahead.
9053 9053 * If so, we don't need to do anything further, so free the new
9054 9054 * entry if one was allocated. Otherwise, allocate a new entry, add
9055 9055 * it to the cache, and then initiate an asynchronous readdir
9056 9056 * operation to fill it.
9057 9057 */
9058 9058 rrdc = rddir4_cache_lookup(rp, rdc->nfs4_ncookie, count);
9059 9059
9060 9060 /*
9061 9061 * A readdir cache entry could not be obtained for the readahead. In
9062 9062 * this case we skip the readahead and return.
9063 9063 */
9064 9064 if (rrdc == NULL) {
9065 9065 rddir4_cache_rele(rp, rdc);
9066 9066 mutex_exit(&rp->r_statelock);
9067 9067 return (error);
9068 9068 }
9069 9069
9070 9070 /*
9071 9071 * Check to see if we need to fill this entry in.
9072 9072 */
9073 9073 if (rrdc->flags & RDDIRREQ) {
9074 9074 rrdc->flags &= ~RDDIRREQ;
9075 9075 rrdc->flags |= RDDIR;
9076 9076 rddir4_cache_rele(rp, rdc);
9077 9077 mutex_exit(&rp->r_statelock);
9078 9078 #ifdef DEBUG
9079 9079 nfs4_readdir_readahead++;
9080 9080 #endif
9081 9081 /*
9082 9082 * Do the readdir.
9083 9083 */
9084 9084 nfs4_async_readdir(vp, rrdc, cr, do_nfs4readdir);
9085 9085 return (error);
9086 9086 }
9087 9087
9088 9088 rddir4_cache_rele(rp, rrdc);
9089 9089 rddir4_cache_rele(rp, rdc);
9090 9090 mutex_exit(&rp->r_statelock);
9091 9091 return (error);
9092 9092 }
9093 9093
9094 9094 static int
9095 9095 do_nfs4readdir(vnode_t *vp, rddir4_cache *rdc, cred_t *cr)
9096 9096 {
9097 9097 int error;
9098 9098 rnode4_t *rp;
9099 9099
9100 9100 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
9101 9101
9102 9102 rp = VTOR4(vp);
9103 9103
9104 9104 /*
9105 9105 * Obtain the readdir results for the caller.
9106 9106 */
9107 9107 nfs4readdir(vp, rdc, cr);
9108 9108
9109 9109 mutex_enter(&rp->r_statelock);
9110 9110 /*
9111 9111 * The entry is now complete
9112 9112 */
9113 9113 rdc->flags &= ~RDDIR;
9114 9114
9115 9115 error = rdc->error;
9116 9116 if (error)
9117 9117 rdc->flags |= RDDIRREQ;
9118 9118 rddir4_cache_rele(rp, rdc);
9119 9119 mutex_exit(&rp->r_statelock);
9120 9120
9121 9121 return (error);
9122 9122 }
9123 9123
9124 9124 /*
9125 9125 * Read directory entries.
9126 9126 * There are some weird things to look out for here. The uio_loffset
9127 9127 * field is either 0 or it is the offset returned from a previous
9128 9128 * readdir. It is an opaque value used by the server to find the
9129 9129 * correct directory block to read. The count field is the number
9130 9130 * of blocks to read on the server. This is advisory only, the server
9131 9131 * may return only one block's worth of entries. Entries may be compressed
9132 9132 * on the server.
9133 9133 *
9134 9134 * Generates the following compound request:
9135 9135 * 1. If readdir offset is zero and no dnlc entry for parent exists,
9136 9136 * must include a Lookupp as well. In this case, send:
9137 9137 * { Putfh <fh>; Readdir; Lookupp; Getfh; Getattr }
9138 9138 * 2. Otherwise just do: { Putfh <fh>; Readdir }
9139 9139 *
9140 9140 * Get complete attributes and filehandles for entries if this is the
9141 9141 * first read of the directory. Otherwise, just get fileid's.
9142 9142 */
9143 9143 static void
9144 9144 nfs4readdir(vnode_t *vp, rddir4_cache *rdc, cred_t *cr)
9145 9145 {
9146 9146 COMPOUND4args_clnt args;
9147 9147 COMPOUND4res_clnt res;
9148 9148 READDIR4args *rargs;
9149 9149 READDIR4res_clnt *rd_res;
9150 9150 bitmap4 rd_bitsval;
9151 9151 nfs_argop4 argop[5];
9152 9152 nfs_resop4 *resop;
9153 9153 rnode4_t *rp = VTOR4(vp);
9154 9154 mntinfo4_t *mi = VTOMI4(vp);
9155 9155 int doqueue;
9156 9156 u_longlong_t nodeid, pnodeid; /* id's of dir and its parents */
9157 9157 vnode_t *dvp;
9158 9158 nfs_cookie4 cookie = (nfs_cookie4)rdc->nfs4_cookie;
9159 9159 int num_ops, res_opcnt;
9160 9160 bool_t needrecov = FALSE;
9161 9161 nfs4_recov_state_t recov_state;
9162 9162 hrtime_t t;
9163 9163 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
9164 9164
9165 9165 ASSERT(nfs_zone() == mi->mi_zone);
9166 9166 ASSERT(rdc->flags & RDDIR);
9167 9167 ASSERT(rdc->entries == NULL);
9168 9168
9169 9169 /*
9170 9170 * If rp were a stub, it should have triggered and caused
9171 9171 * a mount for us to get this far.
9172 9172 */
9173 9173 ASSERT(!RP_ISSTUB(rp));
9174 9174
9175 9175 num_ops = 2;
9176 9176 if (cookie == (nfs_cookie4)0 || cookie == (nfs_cookie4)1) {
9177 9177 /*
9178 9178 * Since nfsv4 readdir may not return entries for "." and "..",
9179 9179 * the client must recreate them:
9180 9180 * To find the correct nodeid, do the following:
9181 9181 * For current node, get nodeid from dnlc.
9182 9182 * - if current node is rootvp, set pnodeid to nodeid.
9183 9183 * - else if parent is in the dnlc, get its nodeid from there.
9184 9184 * - else add LOOKUPP+GETATTR to compound.
9185 9185 */
9186 9186 nodeid = rp->r_attr.va_nodeid;
9187 9187 if (vp->v_flag & VROOT) {
9188 9188 pnodeid = nodeid; /* root of mount point */
9189 9189 } else {
9190 9190 dvp = dnlc_lookup(vp, "..");
9191 9191 if (dvp != NULL && dvp != DNLC_NO_VNODE) {
9192 9192 /* parent in dnlc cache - no need for otw */
9193 9193 pnodeid = VTOR4(dvp)->r_attr.va_nodeid;
9194 9194 } else {
9195 9195 /*
9196 9196 * parent not in dnlc cache,
9197 9197 * do lookupp to get its id
9198 9198 */
9199 9199 num_ops = 5;
9200 9200 pnodeid = 0; /* set later by getattr parent */
9201 9201 }
9202 9202 if (dvp)
9203 9203 VN_RELE(dvp);
9204 9204 }
9205 9205 }
9206 9206 recov_state.rs_flags = 0;
9207 9207 recov_state.rs_num_retry_despite_err = 0;
9208 9208
9209 9209 /* Save the original mount point security flavor */
9210 9210 (void) save_mnt_secinfo(mi->mi_curr_serv);
9211 9211
9212 9212 recov_retry:
9213 9213 args.ctag = TAG_READDIR;
9214 9214
9215 9215 args.array = argop;
9216 9216 args.array_len = num_ops;
9217 9217
9218 9218 if (e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_READDIR,
9219 9219 &recov_state, NULL)) {
9220 9220 /*
9221 9221 * If readdir a node that is a stub for a crossed mount point,
9222 9222 * keep the original secinfo flavor for the current file
9223 9223 * system, not the crossed one.
9224 9224 */
9225 9225 (void) check_mnt_secinfo(mi->mi_curr_serv, vp);
9226 9226 rdc->error = e.error;
9227 9227 return;
9228 9228 }
9229 9229
9230 9230 /*
9231 9231 * Determine which attrs to request for dirents. This code
9232 9232 * must be protected by nfs4_start/end_fop because of r_server
9233 9233 * (which will change during failover recovery).
9234 9234 *
9235 9235 */
9236 9236 if (rp->r_flags & (R4LOOKUP | R4READDIRWATTR)) {
9237 9237 /*
9238 9238 * Get all vattr attrs plus filehandle and rdattr_error
9239 9239 */
9240 9240 rd_bitsval = NFS4_VATTR_MASK |
9241 9241 FATTR4_RDATTR_ERROR_MASK |
9242 9242 FATTR4_FILEHANDLE_MASK;
9243 9243
9244 9244 if (rp->r_flags & R4READDIRWATTR) {
9245 9245 mutex_enter(&rp->r_statelock);
9246 9246 rp->r_flags &= ~R4READDIRWATTR;
9247 9247 mutex_exit(&rp->r_statelock);
9248 9248 }
9249 9249 } else {
9250 9250 servinfo4_t *svp = rp->r_server;
9251 9251
9252 9252 /*
9253 9253 * Already read directory. Use readdir with
9254 9254 * no attrs (except for mounted_on_fileid) for updates.
9255 9255 */
9256 9256 rd_bitsval = FATTR4_RDATTR_ERROR_MASK;
9257 9257
9258 9258 /*
9259 9259 * request mounted on fileid if supported, else request
9260 9260 * fileid. maybe we should verify that fileid is supported
9261 9261 * and request something else if not.
9262 9262 */
9263 9263 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
9264 9264 if (svp->sv_supp_attrs & FATTR4_MOUNTED_ON_FILEID_MASK)
9265 9265 rd_bitsval |= FATTR4_MOUNTED_ON_FILEID_MASK;
9266 9266 nfs_rw_exit(&svp->sv_lock);
9267 9267 }
9268 9268
9269 9269 /* putfh directory fh */
9270 9270 argop[0].argop = OP_CPUTFH;
9271 9271 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
9272 9272
9273 9273 argop[1].argop = OP_READDIR;
9274 9274 rargs = &argop[1].nfs_argop4_u.opreaddir;
9275 9275 /*
9276 9276 * 1 and 2 are reserved for client "." and ".." entry offset.
9277 9277 * cookie 0 should be used over-the-wire to start reading at
9278 9278 * the beginning of the directory excluding "." and "..".
9279 9279 */
9280 9280 if (rdc->nfs4_cookie == 0 ||
9281 9281 rdc->nfs4_cookie == 1 ||
9282 9282 rdc->nfs4_cookie == 2) {
9283 9283 rargs->cookie = (nfs_cookie4)0;
9284 9284 rargs->cookieverf = 0;
9285 9285 } else {
9286 9286 rargs->cookie = (nfs_cookie4)rdc->nfs4_cookie;
9287 9287 mutex_enter(&rp->r_statelock);
9288 9288 rargs->cookieverf = rp->r_cookieverf4;
9289 9289 mutex_exit(&rp->r_statelock);
9290 9290 }
9291 9291 rargs->dircount = MIN(rdc->buflen, mi->mi_tsize);
9292 9292 rargs->maxcount = mi->mi_tsize;
9293 9293 rargs->attr_request = rd_bitsval;
9294 9294 rargs->rdc = rdc;
9295 9295 rargs->dvp = vp;
9296 9296 rargs->mi = mi;
9297 9297 rargs->cr = cr;
9298 9298
9299 9299
9300 9300 /*
9301 9301 * If count < than the minimum required, we return no entries
9302 9302 * and fail with EINVAL
9303 9303 */
9304 9304 if (rargs->dircount < (DIRENT64_RECLEN(1) + DIRENT64_RECLEN(2))) {
9305 9305 rdc->error = EINVAL;
9306 9306 goto out;
9307 9307 }
9308 9308
9309 9309 if (args.array_len == 5) {
9310 9310 /*
9311 9311 * Add lookupp and getattr for parent nodeid.
9312 9312 */
9313 9313 argop[2].argop = OP_LOOKUPP;
9314 9314
9315 9315 argop[3].argop = OP_GETFH;
9316 9316
9317 9317 /* getattr parent */
9318 9318 argop[4].argop = OP_GETATTR;
9319 9319 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
9320 9320 argop[4].nfs_argop4_u.opgetattr.mi = mi;
9321 9321 }
9322 9322
9323 9323 doqueue = 1;
9324 9324
9325 9325 if (mi->mi_io_kstats) {
9326 9326 mutex_enter(&mi->mi_lock);
9327 9327 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
9328 9328 mutex_exit(&mi->mi_lock);
9329 9329 }
9330 9330
9331 9331 /* capture the time of this call */
9332 9332 rargs->t = t = gethrtime();
9333 9333
9334 9334 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
9335 9335
9336 9336 if (mi->mi_io_kstats) {
9337 9337 mutex_enter(&mi->mi_lock);
9338 9338 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
9339 9339 mutex_exit(&mi->mi_lock);
9340 9340 }
9341 9341
9342 9342 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
9343 9343
9344 9344 /*
9345 9345 * If RPC error occurred and it isn't an error that
9346 9346 * triggers recovery, then go ahead and fail now.
9347 9347 */
9348 9348 if (e.error != 0 && !needrecov) {
9349 9349 rdc->error = e.error;
9350 9350 goto out;
9351 9351 }
9352 9352
9353 9353 if (needrecov) {
9354 9354 bool_t abort;
9355 9355
9356 9356 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
9357 9357 "nfs4readdir: initiating recovery.\n"));
9358 9358
9359 9359 abort = nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL,
9360 9360 NULL, OP_READDIR, NULL, NULL, NULL);
9361 9361 if (abort == FALSE) {
9362 9362 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_READDIR,
9363 9363 &recov_state, needrecov);
9364 9364 if (!e.error)
9365 9365 (void) xdr_free(xdr_COMPOUND4res_clnt,
9366 9366 (caddr_t)&res);
9367 9367 if (rdc->entries != NULL) {
9368 9368 kmem_free(rdc->entries, rdc->entlen);
9369 9369 rdc->entries = NULL;
9370 9370 }
9371 9371 goto recov_retry;
9372 9372 }
9373 9373
9374 9374 if (e.error != 0) {
9375 9375 rdc->error = e.error;
9376 9376 goto out;
9377 9377 }
9378 9378
9379 9379 /* fall through for res.status case */
9380 9380 }
9381 9381
9382 9382 res_opcnt = res.array_len;
9383 9383
9384 9384 /*
9385 9385 * If compound failed first 2 ops (PUTFH+READDIR), then return
9386 9386 * failure here. Subsequent ops are for filling out dot-dot
9387 9387 * dirent, and if they fail, we still want to give the caller
9388 9388 * the dirents returned by (the successful) READDIR op, so we need
9389 9389 * to silently ignore failure for subsequent ops (LOOKUPP+GETATTR).
9390 9390 *
9391 9391 * One example where PUTFH+READDIR ops would succeed but
9392 9392 * LOOKUPP+GETATTR would fail would be a dir that has r perm
9393 9393 * but lacks x. In this case, a POSIX server's VOP_READDIR
9394 9394 * would succeed; however, VOP_LOOKUP(..) would fail since no
9395 9395 * x perm. We need to come up with a non-vendor-specific way
9396 9396 * for a POSIX server to return d_ino from dotdot's dirent if
9397 9397 * client only requests mounted_on_fileid, and just say the
9398 9398 * LOOKUPP succeeded and fill out the GETATTR. However, if
9399 9399 * client requested any mandatory attrs, server would be required
9400 9400 * to fail the GETATTR op because it can't call VOP_LOOKUP+VOP_GETATTR
9401 9401 * for dotdot.
9402 9402 */
9403 9403
9404 9404 if (res.status) {
9405 9405 if (res_opcnt <= 2) {
9406 9406 e.error = geterrno4(res.status);
9407 9407 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_READDIR,
9408 9408 &recov_state, needrecov);
9409 9409 nfs4_purge_stale_fh(e.error, vp, cr);
9410 9410 rdc->error = e.error;
9411 9411 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
9412 9412 if (rdc->entries != NULL) {
9413 9413 kmem_free(rdc->entries, rdc->entlen);
9414 9414 rdc->entries = NULL;
9415 9415 }
9416 9416 /*
9417 9417 * If readdir a node that is a stub for a
9418 9418 * crossed mount point, keep the original
9419 9419 * secinfo flavor for the current file system,
9420 9420 * not the crossed one.
9421 9421 */
9422 9422 (void) check_mnt_secinfo(mi->mi_curr_serv, vp);
9423 9423 return;
9424 9424 }
9425 9425 }
9426 9426
9427 9427 resop = &res.array[1]; /* readdir res */
9428 9428 rd_res = &resop->nfs_resop4_u.opreaddirclnt;
9429 9429
9430 9430 mutex_enter(&rp->r_statelock);
9431 9431 rp->r_cookieverf4 = rd_res->cookieverf;
9432 9432 mutex_exit(&rp->r_statelock);
9433 9433
9434 9434 /*
9435 9435 * For "." and ".." entries
9436 9436 * e.g.
9437 9437 * seek(cookie=0) -> "." entry with d_off = 1
9438 9438 * seek(cookie=1) -> ".." entry with d_off = 2
9439 9439 */
9440 9440 if (cookie == (nfs_cookie4) 0) {
9441 9441 if (rd_res->dotp)
9442 9442 rd_res->dotp->d_ino = nodeid;
9443 9443 if (rd_res->dotdotp)
9444 9444 rd_res->dotdotp->d_ino = pnodeid;
9445 9445 }
9446 9446 if (cookie == (nfs_cookie4) 1) {
9447 9447 if (rd_res->dotdotp)
9448 9448 rd_res->dotdotp->d_ino = pnodeid;
9449 9449 }
9450 9450
9451 9451
9452 9452 /* LOOKUPP+GETATTR attemped */
9453 9453 if (args.array_len == 5 && rd_res->dotdotp) {
9454 9454 if (res.status == NFS4_OK && res_opcnt == 5) {
9455 9455 nfs_fh4 *fhp;
9456 9456 nfs4_sharedfh_t *sfhp;
9457 9457 vnode_t *pvp;
9458 9458 nfs4_ga_res_t *garp;
9459 9459
9460 9460 resop++; /* lookupp */
9461 9461 resop++; /* getfh */
9462 9462 fhp = &resop->nfs_resop4_u.opgetfh.object;
9463 9463
9464 9464 resop++; /* getattr of parent */
9465 9465
9466 9466 /*
9467 9467 * First, take care of finishing the
9468 9468 * readdir results.
9469 9469 */
9470 9470 garp = &resop->nfs_resop4_u.opgetattr.ga_res;
9471 9471 /*
9472 9472 * The d_ino of .. must be the inode number
9473 9473 * of the mounted filesystem.
9474 9474 */
9475 9475 if (garp->n4g_va.va_mask & AT_NODEID)
9476 9476 rd_res->dotdotp->d_ino =
9477 9477 garp->n4g_va.va_nodeid;
9478 9478
9479 9479
9480 9480 /*
9481 9481 * Next, create the ".." dnlc entry
9482 9482 */
9483 9483 sfhp = sfh4_get(fhp, mi);
9484 9484 if (!nfs4_make_dotdot(sfhp, t, vp, cr, &pvp, 0)) {
9485 9485 dnlc_update(vp, "..", pvp);
9486 9486 VN_RELE(pvp);
9487 9487 }
9488 9488 sfh4_rele(&sfhp);
9489 9489 }
9490 9490 }
9491 9491
9492 9492 if (mi->mi_io_kstats) {
9493 9493 mutex_enter(&mi->mi_lock);
9494 9494 KSTAT_IO_PTR(mi->mi_io_kstats)->reads++;
9495 9495 KSTAT_IO_PTR(mi->mi_io_kstats)->nread += rdc->actlen;
9496 9496 mutex_exit(&mi->mi_lock);
9497 9497 }
9498 9498
9499 9499 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
9500 9500
9501 9501 out:
9502 9502 /*
9503 9503 * If readdir a node that is a stub for a crossed mount point,
9504 9504 * keep the original secinfo flavor for the current file system,
9505 9505 * not the crossed one.
9506 9506 */
9507 9507 (void) check_mnt_secinfo(mi->mi_curr_serv, vp);
9508 9508
9509 9509 nfs4_end_fop(mi, vp, NULL, OH_READDIR, &recov_state, needrecov);
9510 9510 }
9511 9511
9512 9512
9513 9513 static int
9514 9514 nfs4_bio(struct buf *bp, stable_how4 *stab_comm, cred_t *cr, bool_t readahead)
9515 9515 {
9516 9516 rnode4_t *rp = VTOR4(bp->b_vp);
9517 9517 int count;
9518 9518 int error;
9519 9519 cred_t *cred_otw = NULL;
9520 9520 offset_t offset;
9521 9521 nfs4_open_stream_t *osp = NULL;
9522 9522 bool_t first_time = TRUE; /* first time getting otw cred */
9523 9523 bool_t last_time = FALSE; /* last time getting otw cred */
9524 9524
9525 9525 ASSERT(nfs_zone() == VTOMI4(bp->b_vp)->mi_zone);
9526 9526
9527 9527 DTRACE_IO1(start, struct buf *, bp);
9528 9528 offset = ldbtob(bp->b_lblkno);
9529 9529
9530 9530 if (bp->b_flags & B_READ) {
9531 9531 read_again:
9532 9532 /*
9533 9533 * Releases the osp, if it is provided.
9534 9534 * Puts a hold on the cred_otw and the new osp (if found).
9535 9535 */
9536 9536 cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp,
9537 9537 &first_time, &last_time);
9538 9538 error = bp->b_error = nfs4read(bp->b_vp, bp->b_un.b_addr,
9539 9539 offset, bp->b_bcount, &bp->b_resid, cred_otw,
9540 9540 readahead, NULL);
9541 9541 crfree(cred_otw);
9542 9542 if (!error) {
9543 9543 if (bp->b_resid) {
9544 9544 /*
9545 9545 * Didn't get it all because we hit EOF,
9546 9546 * zero all the memory beyond the EOF.
9547 9547 */
9548 9548 /* bzero(rdaddr + */
9549 9549 bzero(bp->b_un.b_addr +
9550 9550 bp->b_bcount - bp->b_resid, bp->b_resid);
9551 9551 }
9552 9552 mutex_enter(&rp->r_statelock);
9553 9553 if (bp->b_resid == bp->b_bcount &&
9554 9554 offset >= rp->r_size) {
9555 9555 /*
9556 9556 * We didn't read anything at all as we are
9557 9557 * past EOF. Return an error indicator back
9558 9558 * but don't destroy the pages (yet).
9559 9559 */
9560 9560 error = NFS_EOF;
9561 9561 }
9562 9562 mutex_exit(&rp->r_statelock);
9563 9563 } else if (error == EACCES && last_time == FALSE) {
9564 9564 goto read_again;
9565 9565 }
9566 9566 } else {
9567 9567 if (!(rp->r_flags & R4STALE)) {
9568 9568 write_again:
9569 9569 /*
9570 9570 * Releases the osp, if it is provided.
9571 9571 * Puts a hold on the cred_otw and the new
9572 9572 * osp (if found).
9573 9573 */
9574 9574 cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp,
9575 9575 &first_time, &last_time);
9576 9576 mutex_enter(&rp->r_statelock);
9577 9577 count = MIN(bp->b_bcount, rp->r_size - offset);
9578 9578 mutex_exit(&rp->r_statelock);
9579 9579 if (count < 0)
9580 9580 cmn_err(CE_PANIC, "nfs4_bio: write count < 0");
9581 9581 #ifdef DEBUG
9582 9582 if (count == 0) {
9583 9583 zoneid_t zoneid = getzoneid();
9584 9584
9585 9585 zcmn_err(zoneid, CE_WARN,
9586 9586 "nfs4_bio: zero length write at %lld",
9587 9587 offset);
9588 9588 zcmn_err(zoneid, CE_CONT, "flags=0x%x, "
9589 9589 "b_bcount=%ld, file size=%lld",
9590 9590 rp->r_flags, (long)bp->b_bcount,
9591 9591 rp->r_size);
9592 9592 sfh4_printfhandle(VTOR4(bp->b_vp)->r_fh);
9593 9593 if (nfs4_bio_do_stop)
9594 9594 debug_enter("nfs4_bio");
9595 9595 }
9596 9596 #endif
9597 9597 error = nfs4write(bp->b_vp, bp->b_un.b_addr, offset,
9598 9598 count, cred_otw, stab_comm);
9599 9599 if (error == EACCES && last_time == FALSE) {
9600 9600 crfree(cred_otw);
9601 9601 goto write_again;
9602 9602 }
9603 9603 bp->b_error = error;
9604 9604 if (error && error != EINTR &&
9605 9605 !(bp->b_vp->v_vfsp->vfs_flag & VFS_UNMOUNTED)) {
9606 9606 /*
9607 9607 * Don't print EDQUOT errors on the console.
9608 9608 * Don't print asynchronous EACCES errors.
9609 9609 * Don't print EFBIG errors.
9610 9610 * Print all other write errors.
9611 9611 */
9612 9612 if (error != EDQUOT && error != EFBIG &&
9613 9613 (error != EACCES ||
9614 9614 !(bp->b_flags & B_ASYNC)))
9615 9615 nfs4_write_error(bp->b_vp,
9616 9616 error, cred_otw);
9617 9617 /*
9618 9618 * Update r_error and r_flags as appropriate.
9619 9619 * If the error was ESTALE, then mark the
9620 9620 * rnode as not being writeable and save
9621 9621 * the error status. Otherwise, save any
9622 9622 * errors which occur from asynchronous
9623 9623 * page invalidations. Any errors occurring
9624 9624 * from other operations should be saved
9625 9625 * by the caller.
9626 9626 */
9627 9627 mutex_enter(&rp->r_statelock);
9628 9628 if (error == ESTALE) {
9629 9629 rp->r_flags |= R4STALE;
9630 9630 if (!rp->r_error)
9631 9631 rp->r_error = error;
9632 9632 } else if (!rp->r_error &&
9633 9633 (bp->b_flags &
9634 9634 (B_INVAL|B_FORCE|B_ASYNC)) ==
9635 9635 (B_INVAL|B_FORCE|B_ASYNC)) {
9636 9636 rp->r_error = error;
9637 9637 }
9638 9638 mutex_exit(&rp->r_statelock);
9639 9639 }
9640 9640 crfree(cred_otw);
9641 9641 } else {
9642 9642 error = rp->r_error;
9643 9643 /*
9644 9644 * A close may have cleared r_error, if so,
9645 9645 * propagate ESTALE error return properly
9646 9646 */
9647 9647 if (error == 0)
9648 9648 error = ESTALE;
9649 9649 }
9650 9650 }
9651 9651
9652 9652 if (error != 0 && error != NFS_EOF)
9653 9653 bp->b_flags |= B_ERROR;
9654 9654
9655 9655 if (osp)
9656 9656 open_stream_rele(osp, rp);
9657 9657
9658 9658 DTRACE_IO1(done, struct buf *, bp);
9659 9659
9660 9660 return (error);
9661 9661 }
9662 9662
9663 9663 /* ARGSUSED */
9664 9664 int
9665 9665 nfs4_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
9666 9666 {
9667 9667 return (EREMOTE);
9668 9668 }
9669 9669
9670 9670 /* ARGSUSED2 */
9671 9671 int
9672 9672 nfs4_rwlock(vnode_t *vp, int write_lock, caller_context_t *ctp)
9673 9673 {
9674 9674 rnode4_t *rp = VTOR4(vp);
9675 9675
9676 9676 if (!write_lock) {
9677 9677 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE);
9678 9678 return (V_WRITELOCK_FALSE);
9679 9679 }
9680 9680
9681 9681 if ((rp->r_flags & R4DIRECTIO) ||
9682 9682 (VTOMI4(vp)->mi_flags & MI4_DIRECTIO)) {
9683 9683 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE);
9684 9684 if (rp->r_mapcnt == 0 && !nfs4_has_pages(vp))
9685 9685 return (V_WRITELOCK_FALSE);
9686 9686 nfs_rw_exit(&rp->r_rwlock);
9687 9687 }
9688 9688
9689 9689 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, FALSE);
9690 9690 return (V_WRITELOCK_TRUE);
9691 9691 }
9692 9692
9693 9693 /* ARGSUSED */
9694 9694 void
9695 9695 nfs4_rwunlock(vnode_t *vp, int write_lock, caller_context_t *ctp)
9696 9696 {
9697 9697 rnode4_t *rp = VTOR4(vp);
9698 9698
9699 9699 nfs_rw_exit(&rp->r_rwlock);
9700 9700 }
9701 9701
9702 9702 /* ARGSUSED */
9703 9703 static int
9704 9704 nfs4_seek(vnode_t *vp, offset_t ooff, offset_t *noffp, caller_context_t *ct)
9705 9705 {
9706 9706 if (nfs_zone() != VTOMI4(vp)->mi_zone)
9707 9707 return (EIO);
9708 9708
9709 9709 /*
9710 9710 * Because we stuff the readdir cookie into the offset field
9711 9711 * someone may attempt to do an lseek with the cookie which
9712 9712 * we want to succeed.
9713 9713 */
9714 9714 if (vp->v_type == VDIR)
9715 9715 return (0);
9716 9716 if (*noffp < 0)
9717 9717 return (EINVAL);
9718 9718 return (0);
9719 9719 }
9720 9720
9721 9721
9722 9722 /*
9723 9723 * Return all the pages from [off..off+len) in file
9724 9724 */
9725 9725 /* ARGSUSED */
9726 9726 static int
9727 9727 nfs4_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
9728 9728 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
9729 9729 enum seg_rw rw, cred_t *cr, caller_context_t *ct)
9730 9730 {
9731 9731 rnode4_t *rp;
9732 9732 int error;
9733 9733 mntinfo4_t *mi;
9734 9734
9735 9735 if (nfs_zone() != VTOMI4(vp)->mi_zone)
9736 9736 return (EIO);
9737 9737 rp = VTOR4(vp);
9738 9738 if (IS_SHADOW(vp, rp))
9739 9739 vp = RTOV4(rp);
9740 9740
9741 9741 if (vp->v_flag & VNOMAP)
9742 9742 return (ENOSYS);
9743 9743
9744 9744 if (protp != NULL)
9745 9745 *protp = PROT_ALL;
9746 9746
9747 9747 /*
9748 9748 * Now validate that the caches are up to date.
9749 9749 */
9750 9750 if (error = nfs4_validate_caches(vp, cr))
9751 9751 return (error);
9752 9752
9753 9753 mi = VTOMI4(vp);
9754 9754 retry:
9755 9755 mutex_enter(&rp->r_statelock);
9756 9756
9757 9757 /*
9758 9758 * Don't create dirty pages faster than they
9759 9759 * can be cleaned so that the system doesn't
9760 9760 * get imbalanced. If the async queue is
9761 9761 * maxed out, then wait for it to drain before
9762 9762 * creating more dirty pages. Also, wait for
9763 9763 * any threads doing pagewalks in the vop_getattr
9764 9764 * entry points so that they don't block for
9765 9765 * long periods.
9766 9766 */
9767 9767 if (rw == S_CREATE) {
9768 9768 while ((mi->mi_max_threads != 0 &&
9769 9769 rp->r_awcount > 2 * mi->mi_max_threads) ||
9770 9770 rp->r_gcount > 0)
9771 9771 cv_wait(&rp->r_cv, &rp->r_statelock);
9772 9772 }
9773 9773
9774 9774 /*
9775 9775 * If we are getting called as a side effect of an nfs_write()
9776 9776 * operation the local file size might not be extended yet.
9777 9777 * In this case we want to be able to return pages of zeroes.
9778 9778 */
9779 9779 if (off + len > rp->r_size + PAGEOFFSET && seg != segkmap) {
9780 9780 NFS4_DEBUG(nfs4_pageio_debug,
9781 9781 (CE_NOTE, "getpage beyond EOF: off=%lld, "
9782 9782 "len=%llu, size=%llu, attrsize =%llu", off,
9783 9783 (u_longlong_t)len, rp->r_size, rp->r_attr.va_size));
9784 9784 mutex_exit(&rp->r_statelock);
9785 9785 return (EFAULT); /* beyond EOF */
9786 9786 }
9787 9787
9788 9788 mutex_exit(&rp->r_statelock);
9789 9789
9790 9790 error = pvn_getpages(nfs4_getapage, vp, off, len, protp,
9791 9791 pl, plsz, seg, addr, rw, cr);
9792 9792 NFS4_DEBUG(nfs4_pageio_debug && error,
9793 9793 (CE_NOTE, "getpages error %d; off=%lld, len=%lld",
9794 9794 error, off, (u_longlong_t)len));
9795 9795
9796 9796 switch (error) {
9797 9797 case NFS_EOF:
9798 9798 nfs4_purge_caches(vp, NFS4_NOPURGE_DNLC, cr, FALSE);
9799 9799 goto retry;
9800 9800 case ESTALE:
9801 9801 nfs4_purge_stale_fh(error, vp, cr);
9802 9802 }
9803 9803
9804 9804 return (error);
9805 9805 }
9806 9806
9807 9807 /*
9808 9808 * Called from pvn_getpages to get a particular page.
9809 9809 */
9810 9810 /* ARGSUSED */
9811 9811 static int
9812 9812 nfs4_getapage(vnode_t *vp, u_offset_t off, size_t len, uint_t *protp,
9813 9813 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
9814 9814 enum seg_rw rw, cred_t *cr)
9815 9815 {
9816 9816 rnode4_t *rp;
9817 9817 uint_t bsize;
9818 9818 struct buf *bp;
9819 9819 page_t *pp;
9820 9820 u_offset_t lbn;
9821 9821 u_offset_t io_off;
9822 9822 u_offset_t blkoff;
9823 9823 u_offset_t rablkoff;
9824 9824 size_t io_len;
9825 9825 uint_t blksize;
9826 9826 int error;
9827 9827 int readahead;
9828 9828 int readahead_issued = 0;
9829 9829 int ra_window; /* readahead window */
9830 9830 page_t *pagefound;
9831 9831 page_t *savepp;
9832 9832
9833 9833 if (nfs_zone() != VTOMI4(vp)->mi_zone)
9834 9834 return (EIO);
9835 9835
9836 9836 rp = VTOR4(vp);
9837 9837 ASSERT(!IS_SHADOW(vp, rp));
9838 9838 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE);
9839 9839
9840 9840 reread:
9841 9841 bp = NULL;
9842 9842 pp = NULL;
9843 9843 pagefound = NULL;
9844 9844
9845 9845 if (pl != NULL)
9846 9846 pl[0] = NULL;
9847 9847
9848 9848 error = 0;
9849 9849 lbn = off / bsize;
9850 9850 blkoff = lbn * bsize;
9851 9851
9852 9852 /*
9853 9853 * Queueing up the readahead before doing the synchronous read
9854 9854 * results in a significant increase in read throughput because
9855 9855 * of the increased parallelism between the async threads and
9856 9856 * the process context.
9857 9857 */
9858 9858 if ((off & ((vp->v_vfsp->vfs_bsize) - 1)) == 0 &&
9859 9859 rw != S_CREATE &&
9860 9860 !(vp->v_flag & VNOCACHE)) {
9861 9861 mutex_enter(&rp->r_statelock);
9862 9862
9863 9863 /*
9864 9864 * Calculate the number of readaheads to do.
9865 9865 * a) No readaheads at offset = 0.
9866 9866 * b) Do maximum(nfs4_nra) readaheads when the readahead
9867 9867 * window is closed.
9868 9868 * c) Do readaheads between 1 to (nfs4_nra - 1) depending
9869 9869 * upon how far the readahead window is open or close.
9870 9870 * d) No readaheads if rp->r_nextr is not within the scope
9871 9871 * of the readahead window (random i/o).
9872 9872 */
9873 9873
9874 9874 if (off == 0)
9875 9875 readahead = 0;
9876 9876 else if (blkoff == rp->r_nextr)
9877 9877 readahead = nfs4_nra;
9878 9878 else if (rp->r_nextr > blkoff &&
9879 9879 ((ra_window = (rp->r_nextr - blkoff) / bsize)
9880 9880 <= (nfs4_nra - 1)))
9881 9881 readahead = nfs4_nra - ra_window;
9882 9882 else
9883 9883 readahead = 0;
9884 9884
9885 9885 rablkoff = rp->r_nextr;
9886 9886 while (readahead > 0 && rablkoff + bsize < rp->r_size) {
9887 9887 mutex_exit(&rp->r_statelock);
9888 9888 if (nfs4_async_readahead(vp, rablkoff + bsize,
9889 9889 addr + (rablkoff + bsize - off),
9890 9890 seg, cr, nfs4_readahead) < 0) {
9891 9891 mutex_enter(&rp->r_statelock);
9892 9892 break;
9893 9893 }
9894 9894 readahead--;
9895 9895 rablkoff += bsize;
9896 9896 /*
9897 9897 * Indicate that we did a readahead so
9898 9898 * readahead offset is not updated
9899 9899 * by the synchronous read below.
9900 9900 */
9901 9901 readahead_issued = 1;
9902 9902 mutex_enter(&rp->r_statelock);
9903 9903 /*
9904 9904 * set readahead offset to
9905 9905 * offset of last async readahead
9906 9906 * request.
9907 9907 */
9908 9908 rp->r_nextr = rablkoff;
9909 9909 }
9910 9910 mutex_exit(&rp->r_statelock);
9911 9911 }
9912 9912
9913 9913 again:
9914 9914 if ((pagefound = page_exists(vp, off)) == NULL) {
9915 9915 if (pl == NULL) {
9916 9916 (void) nfs4_async_readahead(vp, blkoff, addr, seg, cr,
9917 9917 nfs4_readahead);
9918 9918 } else if (rw == S_CREATE) {
9919 9919 /*
9920 9920 * Block for this page is not allocated, or the offset
9921 9921 * is beyond the current allocation size, or we're
9922 9922 * allocating a swap slot and the page was not found,
9923 9923 * so allocate it and return a zero page.
9924 9924 */
9925 9925 if ((pp = page_create_va(vp, off,
9926 9926 PAGESIZE, PG_WAIT, seg, addr)) == NULL)
9927 9927 cmn_err(CE_PANIC, "nfs4_getapage: page_create");
9928 9928 io_len = PAGESIZE;
9929 9929 mutex_enter(&rp->r_statelock);
9930 9930 rp->r_nextr = off + PAGESIZE;
9931 9931 mutex_exit(&rp->r_statelock);
9932 9932 } else {
9933 9933 /*
9934 9934 * Need to go to server to get a block
9935 9935 */
9936 9936 mutex_enter(&rp->r_statelock);
9937 9937 if (blkoff < rp->r_size &&
9938 9938 blkoff + bsize > rp->r_size) {
9939 9939 /*
9940 9940 * If less than a block left in
9941 9941 * file read less than a block.
9942 9942 */
9943 9943 if (rp->r_size <= off) {
9944 9944 /*
9945 9945 * Trying to access beyond EOF,
9946 9946 * set up to get at least one page.
9947 9947 */
9948 9948 blksize = off + PAGESIZE - blkoff;
9949 9949 } else
9950 9950 blksize = rp->r_size - blkoff;
9951 9951 } else if ((off == 0) ||
9952 9952 (off != rp->r_nextr && !readahead_issued)) {
9953 9953 blksize = PAGESIZE;
9954 9954 blkoff = off; /* block = page here */
9955 9955 } else
9956 9956 blksize = bsize;
9957 9957 mutex_exit(&rp->r_statelock);
9958 9958
9959 9959 pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
9960 9960 &io_len, blkoff, blksize, 0);
9961 9961
9962 9962 /*
9963 9963 * Some other thread has entered the page,
9964 9964 * so just use it.
9965 9965 */
9966 9966 if (pp == NULL)
9967 9967 goto again;
9968 9968
9969 9969 /*
9970 9970 * Now round the request size up to page boundaries.
9971 9971 * This ensures that the entire page will be
9972 9972 * initialized to zeroes if EOF is encountered.
9973 9973 */
9974 9974 io_len = ptob(btopr(io_len));
9975 9975
9976 9976 bp = pageio_setup(pp, io_len, vp, B_READ);
9977 9977 ASSERT(bp != NULL);
9978 9978
9979 9979 /*
9980 9980 * pageio_setup should have set b_addr to 0. This
9981 9981 * is correct since we want to do I/O on a page
9982 9982 * boundary. bp_mapin will use this addr to calculate
9983 9983 * an offset, and then set b_addr to the kernel virtual
9984 9984 * address it allocated for us.
9985 9985 */
9986 9986 ASSERT(bp->b_un.b_addr == 0);
9987 9987
9988 9988 bp->b_edev = 0;
9989 9989 bp->b_dev = 0;
9990 9990 bp->b_lblkno = lbtodb(io_off);
9991 9991 bp->b_file = vp;
9992 9992 bp->b_offset = (offset_t)off;
9993 9993 bp_mapin(bp);
9994 9994
9995 9995 /*
9996 9996 * If doing a write beyond what we believe is EOF,
9997 9997 * don't bother trying to read the pages from the
9998 9998 * server, we'll just zero the pages here. We
9999 9999 * don't check that the rw flag is S_WRITE here
10000 10000 * because some implementations may attempt a
10001 10001 * read access to the buffer before copying data.
10002 10002 */
10003 10003 mutex_enter(&rp->r_statelock);
10004 10004 if (io_off >= rp->r_size && seg == segkmap) {
10005 10005 mutex_exit(&rp->r_statelock);
10006 10006 bzero(bp->b_un.b_addr, io_len);
10007 10007 } else {
10008 10008 mutex_exit(&rp->r_statelock);
10009 10009 error = nfs4_bio(bp, NULL, cr, FALSE);
10010 10010 }
10011 10011
10012 10012 /*
10013 10013 * Unmap the buffer before freeing it.
10014 10014 */
10015 10015 bp_mapout(bp);
10016 10016 pageio_done(bp);
10017 10017
10018 10018 savepp = pp;
10019 10019 do {
10020 10020 pp->p_fsdata = C_NOCOMMIT;
10021 10021 } while ((pp = pp->p_next) != savepp);
10022 10022
10023 10023 if (error == NFS_EOF) {
10024 10024 /*
10025 10025 * If doing a write system call just return
10026 10026 * zeroed pages, else user tried to get pages
10027 10027 * beyond EOF, return error. We don't check
10028 10028 * that the rw flag is S_WRITE here because
10029 10029 * some implementations may attempt a read
10030 10030 * access to the buffer before copying data.
10031 10031 */
10032 10032 if (seg == segkmap)
10033 10033 error = 0;
10034 10034 else
10035 10035 error = EFAULT;
10036 10036 }
10037 10037
10038 10038 if (!readahead_issued && !error) {
10039 10039 mutex_enter(&rp->r_statelock);
10040 10040 rp->r_nextr = io_off + io_len;
10041 10041 mutex_exit(&rp->r_statelock);
10042 10042 }
10043 10043 }
10044 10044 }
10045 10045
10046 10046 out:
10047 10047 if (pl == NULL)
10048 10048 return (error);
10049 10049
10050 10050 if (error) {
10051 10051 if (pp != NULL)
10052 10052 pvn_read_done(pp, B_ERROR);
10053 10053 return (error);
10054 10054 }
10055 10055
10056 10056 if (pagefound) {
10057 10057 se_t se = (rw == S_CREATE ? SE_EXCL : SE_SHARED);
10058 10058
10059 10059 /*
10060 10060 * Page exists in the cache, acquire the appropriate lock.
10061 10061 * If this fails, start all over again.
10062 10062 */
10063 10063 if ((pp = page_lookup(vp, off, se)) == NULL) {
10064 10064 #ifdef DEBUG
10065 10065 nfs4_lostpage++;
10066 10066 #endif
10067 10067 goto reread;
10068 10068 }
10069 10069 pl[0] = pp;
10070 10070 pl[1] = NULL;
10071 10071 return (0);
10072 10072 }
10073 10073
10074 10074 if (pp != NULL)
10075 10075 pvn_plist_init(pp, pl, plsz, off, io_len, rw);
10076 10076
10077 10077 return (error);
10078 10078 }
10079 10079
10080 10080 static void
10081 10081 nfs4_readahead(vnode_t *vp, u_offset_t blkoff, caddr_t addr, struct seg *seg,
10082 10082 cred_t *cr)
10083 10083 {
10084 10084 int error;
10085 10085 page_t *pp;
10086 10086 u_offset_t io_off;
10087 10087 size_t io_len;
10088 10088 struct buf *bp;
10089 10089 uint_t bsize, blksize;
10090 10090 rnode4_t *rp = VTOR4(vp);
10091 10091 page_t *savepp;
10092 10092
10093 10093 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
10094 10094
10095 10095 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE);
10096 10096
10097 10097 mutex_enter(&rp->r_statelock);
10098 10098 if (blkoff < rp->r_size && blkoff + bsize > rp->r_size) {
10099 10099 /*
10100 10100 * If less than a block left in file read less
10101 10101 * than a block.
10102 10102 */
10103 10103 blksize = rp->r_size - blkoff;
10104 10104 } else
10105 10105 blksize = bsize;
10106 10106 mutex_exit(&rp->r_statelock);
10107 10107
10108 10108 pp = pvn_read_kluster(vp, blkoff, segkmap, addr,
10109 10109 &io_off, &io_len, blkoff, blksize, 1);
10110 10110 /*
10111 10111 * The isra flag passed to the kluster function is 1, we may have
10112 10112 * gotten a return value of NULL for a variety of reasons (# of free
10113 10113 * pages < minfree, someone entered the page on the vnode etc). In all
10114 10114 * cases, we want to punt on the readahead.
10115 10115 */
10116 10116 if (pp == NULL)
10117 10117 return;
10118 10118
10119 10119 /*
10120 10120 * Now round the request size up to page boundaries.
10121 10121 * This ensures that the entire page will be
10122 10122 * initialized to zeroes if EOF is encountered.
10123 10123 */
10124 10124 io_len = ptob(btopr(io_len));
10125 10125
10126 10126 bp = pageio_setup(pp, io_len, vp, B_READ);
10127 10127 ASSERT(bp != NULL);
10128 10128
10129 10129 /*
10130 10130 * pageio_setup should have set b_addr to 0. This is correct since
10131 10131 * we want to do I/O on a page boundary. bp_mapin() will use this addr
10132 10132 * to calculate an offset, and then set b_addr to the kernel virtual
10133 10133 * address it allocated for us.
10134 10134 */
10135 10135 ASSERT(bp->b_un.b_addr == 0);
10136 10136
10137 10137 bp->b_edev = 0;
10138 10138 bp->b_dev = 0;
10139 10139 bp->b_lblkno = lbtodb(io_off);
10140 10140 bp->b_file = vp;
10141 10141 bp->b_offset = (offset_t)blkoff;
10142 10142 bp_mapin(bp);
10143 10143
10144 10144 /*
10145 10145 * If doing a write beyond what we believe is EOF, don't bother trying
10146 10146 * to read the pages from the server, we'll just zero the pages here.
10147 10147 * We don't check that the rw flag is S_WRITE here because some
10148 10148 * implementations may attempt a read access to the buffer before
10149 10149 * copying data.
10150 10150 */
10151 10151 mutex_enter(&rp->r_statelock);
10152 10152 if (io_off >= rp->r_size && seg == segkmap) {
10153 10153 mutex_exit(&rp->r_statelock);
10154 10154 bzero(bp->b_un.b_addr, io_len);
10155 10155 error = 0;
10156 10156 } else {
10157 10157 mutex_exit(&rp->r_statelock);
10158 10158 error = nfs4_bio(bp, NULL, cr, TRUE);
10159 10159 if (error == NFS_EOF)
10160 10160 error = 0;
10161 10161 }
10162 10162
10163 10163 /*
10164 10164 * Unmap the buffer before freeing it.
10165 10165 */
10166 10166 bp_mapout(bp);
10167 10167 pageio_done(bp);
10168 10168
10169 10169 savepp = pp;
10170 10170 do {
10171 10171 pp->p_fsdata = C_NOCOMMIT;
10172 10172 } while ((pp = pp->p_next) != savepp);
10173 10173
10174 10174 pvn_read_done(pp, error ? B_READ | B_ERROR : B_READ);
10175 10175
10176 10176 /*
10177 10177 * In case of error set readahead offset
10178 10178 * to the lowest offset.
10179 10179 * pvn_read_done() calls VN_DISPOSE to destroy the pages
10180 10180 */
10181 10181 if (error && rp->r_nextr > io_off) {
10182 10182 mutex_enter(&rp->r_statelock);
10183 10183 if (rp->r_nextr > io_off)
10184 10184 rp->r_nextr = io_off;
10185 10185 mutex_exit(&rp->r_statelock);
10186 10186 }
10187 10187 }
10188 10188
10189 10189 /*
10190 10190 * Flags are composed of {B_INVAL, B_FREE, B_DONTNEED, B_FORCE}
10191 10191 * If len == 0, do from off to EOF.
10192 10192 *
10193 10193 * The normal cases should be len == 0 && off == 0 (entire vp list) or
10194 10194 * len == MAXBSIZE (from segmap_release actions), and len == PAGESIZE
10195 10195 * (from pageout).
10196 10196 */
10197 10197 /* ARGSUSED */
10198 10198 static int
10199 10199 nfs4_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr,
10200 10200 caller_context_t *ct)
10201 10201 {
10202 10202 int error;
10203 10203 rnode4_t *rp;
10204 10204
10205 10205 ASSERT(cr != NULL);
10206 10206
10207 10207 if (!(flags & B_ASYNC) && nfs_zone() != VTOMI4(vp)->mi_zone)
10208 10208 return (EIO);
10209 10209
10210 10210 rp = VTOR4(vp);
10211 10211 if (IS_SHADOW(vp, rp))
10212 10212 vp = RTOV4(rp);
10213 10213
10214 10214 /*
10215 10215 * XXX - Why should this check be made here?
10216 10216 */
10217 10217 if (vp->v_flag & VNOMAP)
10218 10218 return (ENOSYS);
10219 10219
10220 10220 if (len == 0 && !(flags & B_INVAL) &&
10221 10221 (vp->v_vfsp->vfs_flag & VFS_RDONLY))
10222 10222 return (0);
10223 10223
10224 10224 mutex_enter(&rp->r_statelock);
10225 10225 rp->r_count++;
10226 10226 mutex_exit(&rp->r_statelock);
10227 10227 error = nfs4_putpages(vp, off, len, flags, cr);
10228 10228 mutex_enter(&rp->r_statelock);
10229 10229 rp->r_count--;
10230 10230 cv_broadcast(&rp->r_cv);
10231 10231 mutex_exit(&rp->r_statelock);
10232 10232
10233 10233 return (error);
10234 10234 }
10235 10235
10236 10236 /*
10237 10237 * Write out a single page, possibly klustering adjacent dirty pages.
10238 10238 */
10239 10239 int
10240 10240 nfs4_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, size_t *lenp,
10241 10241 int flags, cred_t *cr)
10242 10242 {
10243 10243 u_offset_t io_off;
10244 10244 u_offset_t lbn_off;
10245 10245 u_offset_t lbn;
10246 10246 size_t io_len;
10247 10247 uint_t bsize;
10248 10248 int error;
10249 10249 rnode4_t *rp;
10250 10250
10251 10251 ASSERT(!(vp->v_vfsp->vfs_flag & VFS_RDONLY));
10252 10252 ASSERT(pp != NULL);
10253 10253 ASSERT(cr != NULL);
10254 10254 ASSERT((flags & B_ASYNC) || nfs_zone() == VTOMI4(vp)->mi_zone);
10255 10255
10256 10256 rp = VTOR4(vp);
10257 10257 ASSERT(rp->r_count > 0);
10258 10258 ASSERT(!IS_SHADOW(vp, rp));
10259 10259
10260 10260 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE);
10261 10261 lbn = pp->p_offset / bsize;
10262 10262 lbn_off = lbn * bsize;
10263 10263
10264 10264 /*
10265 10265 * Find a kluster that fits in one block, or in
10266 10266 * one page if pages are bigger than blocks. If
10267 10267 * there is less file space allocated than a whole
10268 10268 * page, we'll shorten the i/o request below.
10269 10269 */
10270 10270 pp = pvn_write_kluster(vp, pp, &io_off, &io_len, lbn_off,
10271 10271 roundup(bsize, PAGESIZE), flags);
10272 10272
10273 10273 /*
10274 10274 * pvn_write_kluster shouldn't have returned a page with offset
10275 10275 * behind the original page we were given. Verify that.
10276 10276 */
10277 10277 ASSERT((pp->p_offset / bsize) >= lbn);
10278 10278
10279 10279 /*
10280 10280 * Now pp will have the list of kept dirty pages marked for
10281 10281 * write back. It will also handle invalidation and freeing
10282 10282 * of pages that are not dirty. Check for page length rounding
10283 10283 * problems.
10284 10284 */
10285 10285 if (io_off + io_len > lbn_off + bsize) {
10286 10286 ASSERT((io_off + io_len) - (lbn_off + bsize) < PAGESIZE);
10287 10287 io_len = lbn_off + bsize - io_off;
10288 10288 }
10289 10289 /*
10290 10290 * The R4MODINPROGRESS flag makes sure that nfs4_bio() sees a
10291 10291 * consistent value of r_size. R4MODINPROGRESS is set in writerp4().
10292 10292 * When R4MODINPROGRESS is set it indicates that a uiomove() is in
10293 10293 * progress and the r_size has not been made consistent with the
10294 10294 * new size of the file. When the uiomove() completes the r_size is
10295 10295 * updated and the R4MODINPROGRESS flag is cleared.
10296 10296 *
10297 10297 * The R4MODINPROGRESS flag makes sure that nfs4_bio() sees a
10298 10298 * consistent value of r_size. Without this handshaking, it is
10299 10299 * possible that nfs4_bio() picks up the old value of r_size
10300 10300 * before the uiomove() in writerp4() completes. This will result
10301 10301 * in the write through nfs4_bio() being dropped.
10302 10302 *
10303 10303 * More precisely, there is a window between the time the uiomove()
10304 10304 * completes and the time the r_size is updated. If a VOP_PUTPAGE()
10305 10305 * operation intervenes in this window, the page will be picked up,
10306 10306 * because it is dirty (it will be unlocked, unless it was
10307 10307 * pagecreate'd). When the page is picked up as dirty, the dirty
10308 10308 * bit is reset (pvn_getdirty()). In nfs4write(), r_size is
10309 10309 * checked. This will still be the old size. Therefore the page will
10310 10310 * not be written out. When segmap_release() calls VOP_PUTPAGE(),
10311 10311 * the page will be found to be clean and the write will be dropped.
10312 10312 */
10313 10313 if (rp->r_flags & R4MODINPROGRESS) {
10314 10314 mutex_enter(&rp->r_statelock);
10315 10315 if ((rp->r_flags & R4MODINPROGRESS) &&
10316 10316 rp->r_modaddr + MAXBSIZE > io_off &&
10317 10317 rp->r_modaddr < io_off + io_len) {
10318 10318 page_t *plist;
10319 10319 /*
10320 10320 * A write is in progress for this region of the file.
10321 10321 * If we did not detect R4MODINPROGRESS here then this
10322 10322 * path through nfs_putapage() would eventually go to
10323 10323 * nfs4_bio() and may not write out all of the data
10324 10324 * in the pages. We end up losing data. So we decide
10325 10325 * to set the modified bit on each page in the page
10326 10326 * list and mark the rnode with R4DIRTY. This write
10327 10327 * will be restarted at some later time.
10328 10328 */
10329 10329 plist = pp;
10330 10330 while (plist != NULL) {
10331 10331 pp = plist;
10332 10332 page_sub(&plist, pp);
10333 10333 hat_setmod(pp);
10334 10334 page_io_unlock(pp);
10335 10335 page_unlock(pp);
10336 10336 }
10337 10337 rp->r_flags |= R4DIRTY;
10338 10338 mutex_exit(&rp->r_statelock);
10339 10339 if (offp)
10340 10340 *offp = io_off;
10341 10341 if (lenp)
10342 10342 *lenp = io_len;
10343 10343 return (0);
10344 10344 }
10345 10345 mutex_exit(&rp->r_statelock);
10346 10346 }
10347 10347
10348 10348 if (flags & B_ASYNC) {
10349 10349 error = nfs4_async_putapage(vp, pp, io_off, io_len, flags, cr,
10350 10350 nfs4_sync_putapage);
10351 10351 } else
10352 10352 error = nfs4_sync_putapage(vp, pp, io_off, io_len, flags, cr);
10353 10353
10354 10354 if (offp)
10355 10355 *offp = io_off;
10356 10356 if (lenp)
10357 10357 *lenp = io_len;
10358 10358 return (error);
10359 10359 }
10360 10360
10361 10361 static int
10362 10362 nfs4_sync_putapage(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len,
10363 10363 int flags, cred_t *cr)
10364 10364 {
10365 10365 int error;
10366 10366 rnode4_t *rp;
10367 10367
10368 10368 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
10369 10369
10370 10370 flags |= B_WRITE;
10371 10371
10372 10372 error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr);
10373 10373
10374 10374 rp = VTOR4(vp);
10375 10375
10376 10376 if ((error == ENOSPC || error == EDQUOT || error == EFBIG ||
10377 10377 error == EACCES) &&
10378 10378 (flags & (B_INVAL|B_FORCE)) != (B_INVAL|B_FORCE)) {
10379 10379 if (!(rp->r_flags & R4OUTOFSPACE)) {
10380 10380 mutex_enter(&rp->r_statelock);
10381 10381 rp->r_flags |= R4OUTOFSPACE;
10382 10382 mutex_exit(&rp->r_statelock);
10383 10383 }
10384 10384 flags |= B_ERROR;
10385 10385 pvn_write_done(pp, flags);
10386 10386 /*
10387 10387 * If this was not an async thread, then try again to
10388 10388 * write out the pages, but this time, also destroy
10389 10389 * them whether or not the write is successful. This
10390 10390 * will prevent memory from filling up with these
10391 10391 * pages and destroying them is the only alternative
10392 10392 * if they can't be written out.
10393 10393 *
10394 10394 * Don't do this if this is an async thread because
10395 10395 * when the pages are unlocked in pvn_write_done,
10396 10396 * some other thread could have come along, locked
10397 10397 * them, and queued for an async thread. It would be
10398 10398 * possible for all of the async threads to be tied
10399 10399 * up waiting to lock the pages again and they would
10400 10400 * all already be locked and waiting for an async
10401 10401 * thread to handle them. Deadlock.
10402 10402 */
10403 10403 if (!(flags & B_ASYNC)) {
10404 10404 error = nfs4_putpage(vp, io_off, io_len,
10405 10405 B_INVAL | B_FORCE, cr, NULL);
10406 10406 }
10407 10407 } else {
10408 10408 if (error)
10409 10409 flags |= B_ERROR;
10410 10410 else if (rp->r_flags & R4OUTOFSPACE) {
10411 10411 mutex_enter(&rp->r_statelock);
10412 10412 rp->r_flags &= ~R4OUTOFSPACE;
10413 10413 mutex_exit(&rp->r_statelock);
10414 10414 }
10415 10415 pvn_write_done(pp, flags);
10416 10416 if (freemem < desfree)
10417 10417 (void) nfs4_commit_vp(vp, (u_offset_t)0, 0, cr,
10418 10418 NFS4_WRITE_NOWAIT);
10419 10419 }
10420 10420
10421 10421 return (error);
10422 10422 }
10423 10423
10424 10424 #ifdef DEBUG
10425 10425 int nfs4_force_open_before_mmap = 0;
10426 10426 #endif
10427 10427
10428 10428 /* ARGSUSED */
10429 10429 static int
10430 10430 nfs4_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
10431 10431 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
10432 10432 caller_context_t *ct)
10433 10433 {
10434 10434 struct segvn_crargs vn_a;
10435 10435 int error = 0;
10436 10436 rnode4_t *rp = VTOR4(vp);
10437 10437 mntinfo4_t *mi = VTOMI4(vp);
10438 10438
10439 10439 if (nfs_zone() != VTOMI4(vp)->mi_zone)
10440 10440 return (EIO);
10441 10441
10442 10442 if (vp->v_flag & VNOMAP)
10443 10443 return (ENOSYS);
10444 10444
10445 10445 if (off < 0 || (off + len) < 0)
10446 10446 return (ENXIO);
10447 10447
10448 10448 if (vp->v_type != VREG)
10449 10449 return (ENODEV);
10450 10450
10451 10451 /*
10452 10452 * If the file is delegated to the client don't do anything.
10453 10453 * If the file is not delegated, then validate the data cache.
10454 10454 */
10455 10455 mutex_enter(&rp->r_statev4_lock);
10456 10456 if (rp->r_deleg_type == OPEN_DELEGATE_NONE) {
10457 10457 mutex_exit(&rp->r_statev4_lock);
10458 10458 error = nfs4_validate_caches(vp, cr);
10459 10459 if (error)
10460 10460 return (error);
10461 10461 } else {
10462 10462 mutex_exit(&rp->r_statev4_lock);
10463 10463 }
10464 10464
10465 10465 /*
10466 10466 * Check to see if the vnode is currently marked as not cachable.
10467 10467 * This means portions of the file are locked (through VOP_FRLOCK).
10468 10468 * In this case the map request must be refused. We use
10469 10469 * rp->r_lkserlock to avoid a race with concurrent lock requests.
10470 10470 *
10471 10471 * Atomically increment r_inmap after acquiring r_rwlock. The
10472 10472 * idea here is to acquire r_rwlock to block read/write and
10473 10473 * not to protect r_inmap. r_inmap will inform nfs4_read/write()
10474 10474 * that we are in nfs4_map(). Now, r_rwlock is acquired in order
10475 10475 * and we can prevent the deadlock that would have occurred
10476 10476 * when nfs4_addmap() would have acquired it out of order.
10477 10477 *
10478 10478 * Since we are not protecting r_inmap by any lock, we do not
10479 10479 * hold any lock when we decrement it. We atomically decrement
10480 10480 * r_inmap after we release r_lkserlock.
10481 10481 */
10482 10482
10483 10483 if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, INTR4(vp)))
10484 10484 return (EINTR);
10485 10485 atomic_inc_uint(&rp->r_inmap);
10486 10486 nfs_rw_exit(&rp->r_rwlock);
10487 10487
10488 10488 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR4(vp))) {
10489 10489 atomic_dec_uint(&rp->r_inmap);
10490 10490 return (EINTR);
10491 10491 }
10492 10492
10493 10493
10494 10494 if (vp->v_flag & VNOCACHE) {
10495 10495 error = EAGAIN;
10496 10496 goto done;
10497 10497 }
10498 10498
10499 10499 /*
10500 10500 * Don't allow concurrent locks and mapping if mandatory locking is
10501 10501 * enabled.
10502 10502 */
10503 10503 if (flk_has_remote_locks(vp)) {
10504 10504 struct vattr va;
10505 10505 va.va_mask = AT_MODE;
10506 10506 error = nfs4getattr(vp, &va, cr);
10507 10507 if (error != 0)
10508 10508 goto done;
10509 10509 if (MANDLOCK(vp, va.va_mode)) {
10510 10510 error = EAGAIN;
10511 10511 goto done;
10512 10512 }
10513 10513 }
10514 10514
10515 10515 /*
10516 10516 * It is possible that the rnode has a lost lock request that we
10517 10517 * are still trying to recover, and that the request conflicts with
10518 10518 * this map request.
10519 10519 *
10520 10520 * An alternative approach would be for nfs4_safemap() to consider
10521 10521 * queued lock requests when deciding whether to set or clear
10522 10522 * VNOCACHE. This would require the frlock code path to call
10523 10523 * nfs4_safemap() after enqueing a lost request.
10524 10524 */
10525 10525 if (nfs4_map_lost_lock_conflict(vp)) {
10526 10526 error = EAGAIN;
10527 10527 goto done;
10528 10528 }
10529 10529
10530 10530 as_rangelock(as);
10531 10531 error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
10532 10532 if (error != 0) {
10533 10533 as_rangeunlock(as);
10534 10534 goto done;
10535 10535 }
10536 10536
10537 10537 if (vp->v_type == VREG) {
10538 10538 /*
10539 10539 * We need to retrieve the open stream
10540 10540 */
10541 10541 nfs4_open_stream_t *osp = NULL;
10542 10542 nfs4_open_owner_t *oop = NULL;
10543 10543
10544 10544 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi);
10545 10545 if (oop != NULL) {
10546 10546 /* returns with 'os_sync_lock' held */
10547 10547 osp = find_open_stream(oop, rp);
10548 10548 open_owner_rele(oop);
10549 10549 }
10550 10550 if (osp == NULL) {
10551 10551 #ifdef DEBUG
10552 10552 if (nfs4_force_open_before_mmap) {
10553 10553 error = EIO;
10554 10554 goto done;
10555 10555 }
10556 10556 #endif
10557 10557 /* returns with 'os_sync_lock' held */
10558 10558 error = open_and_get_osp(vp, cr, &osp);
10559 10559 if (osp == NULL) {
10560 10560 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE,
10561 10561 "nfs4_map: we tried to OPEN the file "
10562 10562 "but again no osp, so fail with EIO"));
10563 10563 goto done;
10564 10564 }
10565 10565 }
10566 10566
10567 10567 if (osp->os_failed_reopen) {
10568 10568 mutex_exit(&osp->os_sync_lock);
10569 10569 open_stream_rele(osp, rp);
10570 10570 NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE,
10571 10571 "nfs4_map: os_failed_reopen set on "
10572 10572 "osp %p, cr %p, rp %s", (void *)osp,
10573 10573 (void *)cr, rnode4info(rp)));
10574 10574 error = EIO;
10575 10575 goto done;
10576 10576 }
10577 10577 mutex_exit(&osp->os_sync_lock);
10578 10578 open_stream_rele(osp, rp);
10579 10579 }
10580 10580
10581 10581 vn_a.vp = vp;
10582 10582 vn_a.offset = off;
10583 10583 vn_a.type = (flags & MAP_TYPE);
10584 10584 vn_a.prot = (uchar_t)prot;
10585 10585 vn_a.maxprot = (uchar_t)maxprot;
10586 10586 vn_a.flags = (flags & ~MAP_TYPE);
10587 10587 vn_a.cred = cr;
10588 10588 vn_a.amp = NULL;
10589 10589 vn_a.szc = 0;
10590 10590 vn_a.lgrp_mem_policy_flags = 0;
10591 10591
10592 10592 error = as_map(as, *addrp, len, segvn_create, &vn_a);
10593 10593 as_rangeunlock(as);
10594 10594
10595 10595 done:
10596 10596 nfs_rw_exit(&rp->r_lkserlock);
10597 10597 atomic_dec_uint(&rp->r_inmap);
10598 10598 return (error);
10599 10599 }
10600 10600
10601 10601 /*
10602 10602 * We're most likely dealing with a kernel module that likes to READ
10603 10603 * and mmap without OPENing the file (ie: lookup/read/mmap), so lets
10604 10604 * officially OPEN the file to create the necessary client state
10605 10605 * for bookkeeping of os_mmap_read/write counts.
10606 10606 *
10607 10607 * Since VOP_MAP only passes in a pointer to the vnode rather than
10608 10608 * a double pointer, we can't handle the case where nfs4open_otw()
10609 10609 * returns a different vnode than the one passed into VOP_MAP (since
10610 10610 * VOP_DELMAP will not see the vnode nfs4open_otw used). In this case,
10611 10611 * we return NULL and let nfs4_map() fail. Note: the only case where
10612 10612 * this should happen is if the file got removed and replaced with the
10613 10613 * same name on the server (in addition to the fact that we're trying
10614 10614 * to VOP_MAP withouth VOP_OPENing the file in the first place).
10615 10615 */
10616 10616 static int
10617 10617 open_and_get_osp(vnode_t *map_vp, cred_t *cr, nfs4_open_stream_t **ospp)
10618 10618 {
10619 10619 rnode4_t *rp, *drp;
10620 10620 vnode_t *dvp, *open_vp;
10621 10621 char file_name[MAXNAMELEN];
10622 10622 int just_created;
10623 10623 nfs4_open_stream_t *osp;
10624 10624 nfs4_open_owner_t *oop;
10625 10625 int error;
10626 10626
10627 10627 *ospp = NULL;
10628 10628 open_vp = map_vp;
10629 10629
10630 10630 rp = VTOR4(open_vp);
10631 10631 if ((error = vtodv(open_vp, &dvp, cr, TRUE)) != 0)
10632 10632 return (error);
10633 10633 drp = VTOR4(dvp);
10634 10634
10635 10635 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp))) {
10636 10636 VN_RELE(dvp);
10637 10637 return (EINTR);
10638 10638 }
10639 10639
10640 10640 if ((error = vtoname(open_vp, file_name, MAXNAMELEN)) != 0) {
10641 10641 nfs_rw_exit(&drp->r_rwlock);
10642 10642 VN_RELE(dvp);
10643 10643 return (error);
10644 10644 }
10645 10645
10646 10646 mutex_enter(&rp->r_statev4_lock);
10647 10647 if (rp->created_v4) {
10648 10648 rp->created_v4 = 0;
10649 10649 mutex_exit(&rp->r_statev4_lock);
10650 10650
10651 10651 dnlc_update(dvp, file_name, open_vp);
10652 10652 /* This is needed so we don't bump the open ref count */
10653 10653 just_created = 1;
10654 10654 } else {
10655 10655 mutex_exit(&rp->r_statev4_lock);
10656 10656 just_created = 0;
10657 10657 }
10658 10658
10659 10659 VN_HOLD(map_vp);
10660 10660
10661 10661 error = nfs4open_otw(dvp, file_name, NULL, &open_vp, cr, 0, FREAD, 0,
10662 10662 just_created);
10663 10663 if (error) {
10664 10664 nfs_rw_exit(&drp->r_rwlock);
10665 10665 VN_RELE(dvp);
10666 10666 VN_RELE(map_vp);
10667 10667 return (error);
10668 10668 }
10669 10669
10670 10670 nfs_rw_exit(&drp->r_rwlock);
10671 10671 VN_RELE(dvp);
10672 10672
10673 10673 /*
10674 10674 * If nfs4open_otw() returned a different vnode then "undo"
10675 10675 * the open and return failure to the caller.
10676 10676 */
10677 10677 if (!VN_CMP(open_vp, map_vp)) {
10678 10678 nfs4_error_t e;
10679 10679
10680 10680 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "open_and_get_osp: "
10681 10681 "open returned a different vnode"));
10682 10682 /*
10683 10683 * If there's an error, ignore it,
10684 10684 * and let VOP_INACTIVE handle it.
10685 10685 */
10686 10686 (void) nfs4close_one(open_vp, NULL, cr, FREAD, NULL, &e,
10687 10687 CLOSE_NORM, 0, 0, 0);
10688 10688 VN_RELE(map_vp);
10689 10689 return (EIO);
10690 10690 }
10691 10691
10692 10692 VN_RELE(map_vp);
10693 10693
10694 10694 oop = find_open_owner(cr, NFS4_PERM_CREATED, VTOMI4(open_vp));
10695 10695 if (!oop) {
10696 10696 nfs4_error_t e;
10697 10697
10698 10698 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "open_and_get_osp: "
10699 10699 "no open owner"));
10700 10700 /*
10701 10701 * If there's an error, ignore it,
10702 10702 * and let VOP_INACTIVE handle it.
10703 10703 */
10704 10704 (void) nfs4close_one(open_vp, NULL, cr, FREAD, NULL, &e,
10705 10705 CLOSE_NORM, 0, 0, 0);
10706 10706 return (EIO);
10707 10707 }
10708 10708 osp = find_open_stream(oop, rp);
10709 10709 open_owner_rele(oop);
10710 10710 *ospp = osp;
10711 10711 return (0);
10712 10712 }
10713 10713
10714 10714 /*
10715 10715 * Please be aware that when this function is called, the address space write
10716 10716 * a_lock is held. Do not put over the wire calls in this function.
10717 10717 */
10718 10718 /* ARGSUSED */
10719 10719 static int
10720 10720 nfs4_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
10721 10721 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
10722 10722 caller_context_t *ct)
10723 10723 {
10724 10724 rnode4_t *rp;
10725 10725 int error = 0;
10726 10726 mntinfo4_t *mi;
10727 10727
10728 10728 mi = VTOMI4(vp);
10729 10729 rp = VTOR4(vp);
10730 10730
10731 10731 if (nfs_zone() != mi->mi_zone)
10732 10732 return (EIO);
10733 10733 if (vp->v_flag & VNOMAP)
10734 10734 return (ENOSYS);
10735 10735
10736 10736 /*
10737 10737 * Don't need to update the open stream first, since this
10738 10738 * mmap can't add any additional share access that isn't
10739 10739 * already contained in the open stream (for the case where we
10740 10740 * open/mmap/only update rp->r_mapcnt/server reboots/reopen doesn't
10741 10741 * take into account os_mmap_read[write] counts).
10742 10742 */
10743 10743 atomic_add_long((ulong_t *)&rp->r_mapcnt, btopr(len));
10744 10744
10745 10745 if (vp->v_type == VREG) {
10746 10746 /*
10747 10747 * We need to retrieve the open stream and update the counts.
10748 10748 * If there is no open stream here, something is wrong.
10749 10749 */
10750 10750 nfs4_open_stream_t *osp = NULL;
10751 10751 nfs4_open_owner_t *oop = NULL;
10752 10752
10753 10753 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi);
10754 10754 if (oop != NULL) {
10755 10755 /* returns with 'os_sync_lock' held */
10756 10756 osp = find_open_stream(oop, rp);
10757 10757 open_owner_rele(oop);
10758 10758 }
10759 10759 if (osp == NULL) {
10760 10760 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE,
10761 10761 "nfs4_addmap: we should have an osp"
10762 10762 "but we don't, so fail with EIO"));
10763 10763 error = EIO;
10764 10764 goto out;
10765 10765 }
10766 10766
10767 10767 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "nfs4_addmap: osp %p,"
10768 10768 " pages %ld, prot 0x%x", (void *)osp, btopr(len), prot));
10769 10769
10770 10770 /*
10771 10771 * Update the map count in the open stream.
10772 10772 * This is necessary in the case where we
10773 10773 * open/mmap/close/, then the server reboots, and we
10774 10774 * attempt to reopen. If the mmap doesn't add share
10775 10775 * access then we send an invalid reopen with
10776 10776 * access = NONE.
10777 10777 *
10778 10778 * We need to specifically check each PROT_* so a mmap
10779 10779 * call of (PROT_WRITE | PROT_EXEC) will ensure us both
10780 10780 * read and write access. A simple comparison of prot
10781 10781 * to ~PROT_WRITE to determine read access is insufficient
10782 10782 * since prot can be |= with PROT_USER, etc.
10783 10783 */
10784 10784
10785 10785 /*
10786 10786 * Unless we're MAP_SHARED, no sense in adding os_mmap_write
10787 10787 */
10788 10788 if ((flags & MAP_SHARED) && (maxprot & PROT_WRITE))
10789 10789 osp->os_mmap_write += btopr(len);
10790 10790 if (maxprot & PROT_READ)
10791 10791 osp->os_mmap_read += btopr(len);
10792 10792 if (maxprot & PROT_EXEC)
10793 10793 osp->os_mmap_read += btopr(len);
10794 10794 /*
10795 10795 * Ensure that os_mmap_read gets incremented, even if
10796 10796 * maxprot were to look like PROT_NONE.
10797 10797 */
10798 10798 if (!(maxprot & PROT_READ) && !(maxprot & PROT_WRITE) &&
10799 10799 !(maxprot & PROT_EXEC))
10800 10800 osp->os_mmap_read += btopr(len);
10801 10801 osp->os_mapcnt += btopr(len);
10802 10802 mutex_exit(&osp->os_sync_lock);
10803 10803 open_stream_rele(osp, rp);
10804 10804 }
10805 10805
10806 10806 out:
10807 10807 /*
10808 10808 * If we got an error, then undo our
10809 10809 * incrementing of 'r_mapcnt'.
10810 10810 */
10811 10811
10812 10812 if (error) {
10813 10813 atomic_add_long((ulong_t *)&rp->r_mapcnt, -btopr(len));
10814 10814 ASSERT(rp->r_mapcnt >= 0);
10815 10815 }
10816 10816 return (error);
10817 10817 }
10818 10818
10819 10819 /* ARGSUSED */
10820 10820 static int
10821 10821 nfs4_cmp(vnode_t *vp1, vnode_t *vp2, caller_context_t *ct)
10822 10822 {
10823 10823
10824 10824 return (VTOR4(vp1) == VTOR4(vp2));
10825 10825 }
10826 10826
10827 10827 /* ARGSUSED */
10828 10828 static int
10829 10829 nfs4_frlock(vnode_t *vp, int cmd, struct flock64 *bfp, int flag,
10830 10830 offset_t offset, struct flk_callback *flk_cbp, cred_t *cr,
10831 10831 caller_context_t *ct)
10832 10832 {
10833 10833 int rc;
10834 10834 u_offset_t start, end;
10835 10835 rnode4_t *rp;
10836 10836 int error = 0, intr = INTR4(vp);
10837 10837 nfs4_error_t e;
10838 10838
10839 10839 if (nfs_zone() != VTOMI4(vp)->mi_zone)
10840 10840 return (EIO);
10841 10841
10842 10842 /* check for valid cmd parameter */
10843 10843 if (cmd != F_GETLK && cmd != F_SETLK && cmd != F_SETLKW)
10844 10844 return (EINVAL);
10845 10845
10846 10846 /* Verify l_type. */
10847 10847 switch (bfp->l_type) {
10848 10848 case F_RDLCK:
10849 10849 if (cmd != F_GETLK && !(flag & FREAD))
10850 10850 return (EBADF);
10851 10851 break;
10852 10852 case F_WRLCK:
10853 10853 if (cmd != F_GETLK && !(flag & FWRITE))
10854 10854 return (EBADF);
10855 10855 break;
10856 10856 case F_UNLCK:
10857 10857 intr = 0;
10858 10858 break;
10859 10859
10860 10860 default:
10861 10861 return (EINVAL);
10862 10862 }
10863 10863
10864 10864 /* check the validity of the lock range */
10865 10865 if (rc = flk_convert_lock_data(vp, bfp, &start, &end, offset))
10866 10866 return (rc);
10867 10867 if (rc = flk_check_lock_data(start, end, MAXEND))
10868 10868 return (rc);
10869 10869
10870 10870 /*
10871 10871 * If the filesystem is mounted using local locking, pass the
10872 10872 * request off to the local locking code.
10873 10873 */
10874 10874 if (VTOMI4(vp)->mi_flags & MI4_LLOCK || vp->v_type != VREG) {
10875 10875 if (cmd == F_SETLK || cmd == F_SETLKW) {
10876 10876 /*
10877 10877 * For complete safety, we should be holding
10878 10878 * r_lkserlock. However, we can't call
10879 10879 * nfs4_safelock and then fs_frlock while
10880 10880 * holding r_lkserlock, so just invoke
10881 10881 * nfs4_safelock and expect that this will
10882 10882 * catch enough of the cases.
10883 10883 */
10884 10884 if (!nfs4_safelock(vp, bfp, cr))
10885 10885 return (EAGAIN);
10886 10886 }
10887 10887 return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct));
10888 10888 }
10889 10889
10890 10890 rp = VTOR4(vp);
10891 10891
10892 10892 /*
10893 10893 * Check whether the given lock request can proceed, given the
10894 10894 * current file mappings.
10895 10895 */
10896 10896 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_WRITER, intr))
10897 10897 return (EINTR);
10898 10898 if (cmd == F_SETLK || cmd == F_SETLKW) {
10899 10899 if (!nfs4_safelock(vp, bfp, cr)) {
10900 10900 rc = EAGAIN;
10901 10901 goto done;
10902 10902 }
10903 10903 }
10904 10904
10905 10905 /*
10906 10906 * Flush the cache after waiting for async I/O to finish. For new
10907 10907 * locks, this is so that the process gets the latest bits from the
10908 10908 * server. For unlocks, this is so that other clients see the
10909 10909 * latest bits once the file has been unlocked. If currently dirty
10910 10910 * pages can't be flushed, then don't allow a lock to be set. But
10911 10911 * allow unlocks to succeed, to avoid having orphan locks on the
10912 10912 * server.
10913 10913 */
10914 10914 if (cmd != F_GETLK) {
10915 10915 mutex_enter(&rp->r_statelock);
10916 10916 while (rp->r_count > 0) {
10917 10917 if (intr) {
10918 10918 klwp_t *lwp = ttolwp(curthread);
10919 10919
10920 10920 if (lwp != NULL)
10921 10921 lwp->lwp_nostop++;
10922 10922 if (cv_wait_sig(&rp->r_cv,
10923 10923 &rp->r_statelock) == 0) {
10924 10924 if (lwp != NULL)
10925 10925 lwp->lwp_nostop--;
10926 10926 rc = EINTR;
10927 10927 break;
10928 10928 }
10929 10929 if (lwp != NULL)
10930 10930 lwp->lwp_nostop--;
10931 10931 } else
10932 10932 cv_wait(&rp->r_cv, &rp->r_statelock);
10933 10933 }
10934 10934 mutex_exit(&rp->r_statelock);
10935 10935 if (rc != 0)
10936 10936 goto done;
10937 10937 error = nfs4_putpage(vp, (offset_t)0, 0, B_INVAL, cr, ct);
10938 10938 if (error) {
10939 10939 if (error == ENOSPC || error == EDQUOT) {
10940 10940 mutex_enter(&rp->r_statelock);
10941 10941 if (!rp->r_error)
10942 10942 rp->r_error = error;
10943 10943 mutex_exit(&rp->r_statelock);
10944 10944 }
10945 10945 if (bfp->l_type != F_UNLCK) {
10946 10946 rc = ENOLCK;
10947 10947 goto done;
10948 10948 }
10949 10949 }
10950 10950 }
10951 10951
10952 10952 /*
10953 10953 * Call the lock manager to do the real work of contacting
10954 10954 * the server and obtaining the lock.
10955 10955 */
10956 10956 nfs4frlock(NFS4_LCK_CTYPE_NORM, vp, cmd, bfp, flag, offset,
10957 10957 cr, &e, NULL, NULL);
10958 10958 rc = e.error;
10959 10959
10960 10960 if (rc == 0)
10961 10961 nfs4_lockcompletion(vp, cmd);
10962 10962
10963 10963 done:
10964 10964 nfs_rw_exit(&rp->r_lkserlock);
10965 10965
10966 10966 return (rc);
10967 10967 }
10968 10968
10969 10969 /*
10970 10970 * Free storage space associated with the specified vnode. The portion
10971 10971 * to be freed is specified by bfp->l_start and bfp->l_len (already
10972 10972 * normalized to a "whence" of 0).
10973 10973 *
10974 10974 * This is an experimental facility whose continued existence is not
10975 10975 * guaranteed. Currently, we only support the special case
10976 10976 * of l_len == 0, meaning free to end of file.
10977 10977 */
10978 10978 /* ARGSUSED */
10979 10979 static int
10980 10980 nfs4_space(vnode_t *vp, int cmd, struct flock64 *bfp, int flag,
10981 10981 offset_t offset, cred_t *cr, caller_context_t *ct)
10982 10982 {
10983 10983 int error;
10984 10984
10985 10985 if (nfs_zone() != VTOMI4(vp)->mi_zone)
10986 10986 return (EIO);
10987 10987 ASSERT(vp->v_type == VREG);
10988 10988 if (cmd != F_FREESP)
10989 10989 return (EINVAL);
10990 10990
10991 10991 error = convoff(vp, bfp, 0, offset);
10992 10992 if (!error) {
10993 10993 ASSERT(bfp->l_start >= 0);
10994 10994 if (bfp->l_len == 0) {
10995 10995 struct vattr va;
10996 10996
10997 10997 va.va_mask = AT_SIZE;
10998 10998 va.va_size = bfp->l_start;
10999 10999 error = nfs4setattr(vp, &va, 0, cr, NULL);
11000 11000
11001 11001 if (error == 0 && bfp->l_start == 0)
11002 11002 vnevent_truncate(vp, ct);
11003 11003 } else
11004 11004 error = EINVAL;
11005 11005 }
11006 11006
11007 11007 return (error);
11008 11008 }
11009 11009
11010 11010 /* ARGSUSED */
11011 11011 int
11012 11012 nfs4_realvp(vnode_t *vp, vnode_t **vpp, caller_context_t *ct)
11013 11013 {
11014 11014 rnode4_t *rp;
11015 11015 rp = VTOR4(vp);
11016 11016
11017 11017 if (vp->v_type == VREG && IS_SHADOW(vp, rp)) {
11018 11018 vp = RTOV4(rp);
11019 11019 }
11020 11020 *vpp = vp;
11021 11021 return (0);
11022 11022 }
11023 11023
11024 11024 /*
11025 11025 * Setup and add an address space callback to do the work of the delmap call.
11026 11026 * The callback will (and must be) deleted in the actual callback function.
11027 11027 *
11028 11028 * This is done in order to take care of the problem that we have with holding
11029 11029 * the address space's a_lock for a long period of time (e.g. if the NFS server
11030 11030 * is down). Callbacks will be executed in the address space code while the
11031 11031 * a_lock is not held. Holding the address space's a_lock causes things such
11032 11032 * as ps and fork to hang because they are trying to acquire this lock as well.
11033 11033 */
11034 11034 /* ARGSUSED */
11035 11035 static int
11036 11036 nfs4_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
11037 11037 size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr,
11038 11038 caller_context_t *ct)
11039 11039 {
11040 11040 int caller_found;
11041 11041 int error;
11042 11042 rnode4_t *rp;
11043 11043 nfs4_delmap_args_t *dmapp;
11044 11044 nfs4_delmapcall_t *delmap_call;
11045 11045
11046 11046 if (vp->v_flag & VNOMAP)
11047 11047 return (ENOSYS);
11048 11048
11049 11049 /*
↓ open down ↓ |
11049 lines elided |
↑ open up ↑ |
11050 11050 * A process may not change zones if it has NFS pages mmap'ed
11051 11051 * in, so we can't legitimately get here from the wrong zone.
11052 11052 */
11053 11053 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
11054 11054
11055 11055 rp = VTOR4(vp);
11056 11056
11057 11057 /*
11058 11058 * The way that the address space of this process deletes its mapping
11059 11059 * of this file is via the following call chains:
11060 - * - as_free()->SEGOP_UNMAP()/segvn_unmap()->VOP_DELMAP()/nfs4_delmap()
11061 - * - as_unmap()->SEGOP_UNMAP()/segvn_unmap()->VOP_DELMAP()/nfs4_delmap()
11060 + * - as_free()->segop_unmap()/segvn_unmap()->VOP_DELMAP()/nfs4_delmap()
11061 + * - as_unmap()->segop_unmap()/segvn_unmap()->VOP_DELMAP()/nfs4_delmap()
11062 11062 *
11063 11063 * With the use of address space callbacks we are allowed to drop the
11064 11064 * address space lock, a_lock, while executing the NFS operations that
11065 11065 * need to go over the wire. Returning EAGAIN to the caller of this
11066 11066 * function is what drives the execution of the callback that we add
11067 11067 * below. The callback will be executed by the address space code
11068 11068 * after dropping the a_lock. When the callback is finished, since
11069 11069 * we dropped the a_lock, it must be re-acquired and segvn_unmap()
11070 11070 * is called again on the same segment to finish the rest of the work
11071 11071 * that needs to happen during unmapping.
11072 11072 *
11073 11073 * This action of calling back into the segment driver causes
11074 11074 * nfs4_delmap() to get called again, but since the callback was
11075 11075 * already executed at this point, it already did the work and there
11076 11076 * is nothing left for us to do.
11077 11077 *
11078 11078 * To Summarize:
11079 11079 * - The first time nfs4_delmap is called by the current thread is when
11080 11080 * we add the caller associated with this delmap to the delmap caller
11081 11081 * list, add the callback, and return EAGAIN.
11082 11082 * - The second time in this call chain when nfs4_delmap is called we
11083 11083 * will find this caller in the delmap caller list and realize there
11084 11084 * is no more work to do thus removing this caller from the list and
11085 11085 * returning the error that was set in the callback execution.
11086 11086 */
11087 11087 caller_found = nfs4_find_and_delete_delmapcall(rp, &error);
11088 11088 if (caller_found) {
11089 11089 /*
11090 11090 * 'error' is from the actual delmap operations. To avoid
11091 11091 * hangs, we need to handle the return of EAGAIN differently
11092 11092 * since this is what drives the callback execution.
11093 11093 * In this case, we don't want to return EAGAIN and do the
11094 11094 * callback execution because there are none to execute.
11095 11095 */
11096 11096 if (error == EAGAIN)
11097 11097 return (0);
11098 11098 else
11099 11099 return (error);
11100 11100 }
11101 11101
11102 11102 /* current caller was not in the list */
11103 11103 delmap_call = nfs4_init_delmapcall();
11104 11104
11105 11105 mutex_enter(&rp->r_statelock);
11106 11106 list_insert_tail(&rp->r_indelmap, delmap_call);
11107 11107 mutex_exit(&rp->r_statelock);
11108 11108
11109 11109 dmapp = kmem_alloc(sizeof (nfs4_delmap_args_t), KM_SLEEP);
11110 11110
11111 11111 dmapp->vp = vp;
11112 11112 dmapp->off = off;
11113 11113 dmapp->addr = addr;
11114 11114 dmapp->len = len;
11115 11115 dmapp->prot = prot;
11116 11116 dmapp->maxprot = maxprot;
11117 11117 dmapp->flags = flags;
11118 11118 dmapp->cr = cr;
11119 11119 dmapp->caller = delmap_call;
11120 11120
11121 11121 error = as_add_callback(as, nfs4_delmap_callback, dmapp,
11122 11122 AS_UNMAP_EVENT, addr, len, KM_SLEEP);
11123 11123
11124 11124 return (error ? error : EAGAIN);
11125 11125 }
11126 11126
11127 11127 static nfs4_delmapcall_t *
11128 11128 nfs4_init_delmapcall()
11129 11129 {
11130 11130 nfs4_delmapcall_t *delmap_call;
11131 11131
11132 11132 delmap_call = kmem_alloc(sizeof (nfs4_delmapcall_t), KM_SLEEP);
11133 11133 delmap_call->call_id = curthread;
11134 11134 delmap_call->error = 0;
11135 11135
11136 11136 return (delmap_call);
11137 11137 }
11138 11138
11139 11139 static void
11140 11140 nfs4_free_delmapcall(nfs4_delmapcall_t *delmap_call)
11141 11141 {
11142 11142 kmem_free(delmap_call, sizeof (nfs4_delmapcall_t));
11143 11143 }
11144 11144
11145 11145 /*
11146 11146 * Searches for the current delmap caller (based on curthread) in the list of
11147 11147 * callers. If it is found, we remove it and free the delmap caller.
11148 11148 * Returns:
11149 11149 * 0 if the caller wasn't found
11150 11150 * 1 if the caller was found, removed and freed. *errp will be set
11151 11151 * to what the result of the delmap was.
11152 11152 */
11153 11153 static int
11154 11154 nfs4_find_and_delete_delmapcall(rnode4_t *rp, int *errp)
11155 11155 {
11156 11156 nfs4_delmapcall_t *delmap_call;
11157 11157
11158 11158 /*
11159 11159 * If the list doesn't exist yet, we create it and return
11160 11160 * that the caller wasn't found. No list = no callers.
11161 11161 */
11162 11162 mutex_enter(&rp->r_statelock);
11163 11163 if (!(rp->r_flags & R4DELMAPLIST)) {
11164 11164 /* The list does not exist */
11165 11165 list_create(&rp->r_indelmap, sizeof (nfs4_delmapcall_t),
11166 11166 offsetof(nfs4_delmapcall_t, call_node));
11167 11167 rp->r_flags |= R4DELMAPLIST;
11168 11168 mutex_exit(&rp->r_statelock);
11169 11169 return (0);
11170 11170 } else {
11171 11171 /* The list exists so search it */
11172 11172 for (delmap_call = list_head(&rp->r_indelmap);
11173 11173 delmap_call != NULL;
11174 11174 delmap_call = list_next(&rp->r_indelmap, delmap_call)) {
11175 11175 if (delmap_call->call_id == curthread) {
11176 11176 /* current caller is in the list */
11177 11177 *errp = delmap_call->error;
11178 11178 list_remove(&rp->r_indelmap, delmap_call);
11179 11179 mutex_exit(&rp->r_statelock);
11180 11180 nfs4_free_delmapcall(delmap_call);
11181 11181 return (1);
11182 11182 }
11183 11183 }
11184 11184 }
11185 11185 mutex_exit(&rp->r_statelock);
11186 11186 return (0);
11187 11187 }
11188 11188
11189 11189 /*
11190 11190 * Remove some pages from an mmap'd vnode. Just update the
11191 11191 * count of pages. If doing close-to-open, then flush and
11192 11192 * commit all of the pages associated with this file.
11193 11193 * Otherwise, start an asynchronous page flush to write out
11194 11194 * any dirty pages. This will also associate a credential
11195 11195 * with the rnode which can be used to write the pages.
11196 11196 */
11197 11197 /* ARGSUSED */
11198 11198 static void
11199 11199 nfs4_delmap_callback(struct as *as, void *arg, uint_t event)
11200 11200 {
11201 11201 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
11202 11202 rnode4_t *rp;
11203 11203 mntinfo4_t *mi;
11204 11204 nfs4_delmap_args_t *dmapp = (nfs4_delmap_args_t *)arg;
11205 11205
11206 11206 rp = VTOR4(dmapp->vp);
11207 11207 mi = VTOMI4(dmapp->vp);
11208 11208
11209 11209 atomic_add_long((ulong_t *)&rp->r_mapcnt, -btopr(dmapp->len));
11210 11210 ASSERT(rp->r_mapcnt >= 0);
11211 11211
11212 11212 /*
11213 11213 * Initiate a page flush and potential commit if there are
11214 11214 * pages, the file system was not mounted readonly, the segment
11215 11215 * was mapped shared, and the pages themselves were writeable.
11216 11216 */
11217 11217 if (nfs4_has_pages(dmapp->vp) &&
11218 11218 !(dmapp->vp->v_vfsp->vfs_flag & VFS_RDONLY) &&
11219 11219 dmapp->flags == MAP_SHARED && (dmapp->maxprot & PROT_WRITE)) {
11220 11220 mutex_enter(&rp->r_statelock);
11221 11221 rp->r_flags |= R4DIRTY;
11222 11222 mutex_exit(&rp->r_statelock);
11223 11223 e.error = nfs4_putpage_commit(dmapp->vp, dmapp->off,
11224 11224 dmapp->len, dmapp->cr);
11225 11225 if (!e.error) {
11226 11226 mutex_enter(&rp->r_statelock);
11227 11227 e.error = rp->r_error;
11228 11228 rp->r_error = 0;
11229 11229 mutex_exit(&rp->r_statelock);
11230 11230 }
11231 11231 } else
11232 11232 e.error = 0;
11233 11233
11234 11234 if ((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO))
11235 11235 (void) nfs4_putpage(dmapp->vp, dmapp->off, dmapp->len,
11236 11236 B_INVAL, dmapp->cr, NULL);
11237 11237
11238 11238 if (e.error) {
11239 11239 e.stat = puterrno4(e.error);
11240 11240 nfs4_queue_fact(RF_DELMAP_CB_ERR, mi, e.stat, 0,
11241 11241 OP_COMMIT, FALSE, NULL, 0, dmapp->vp);
11242 11242 dmapp->caller->error = e.error;
11243 11243 }
11244 11244
11245 11245 /* Check to see if we need to close the file */
11246 11246
11247 11247 if (dmapp->vp->v_type == VREG) {
11248 11248 nfs4close_one(dmapp->vp, NULL, dmapp->cr, 0, NULL, &e,
11249 11249 CLOSE_DELMAP, dmapp->len, dmapp->maxprot, dmapp->flags);
11250 11250
11251 11251 if (e.error != 0 || e.stat != NFS4_OK) {
11252 11252 /*
11253 11253 * Since it is possible that e.error == 0 and
11254 11254 * e.stat != NFS4_OK (and vice versa),
11255 11255 * we do the proper checking in order to get both
11256 11256 * e.error and e.stat reporting the correct info.
11257 11257 */
11258 11258 if (e.stat == NFS4_OK)
11259 11259 e.stat = puterrno4(e.error);
11260 11260 if (e.error == 0)
11261 11261 e.error = geterrno4(e.stat);
11262 11262
11263 11263 nfs4_queue_fact(RF_DELMAP_CB_ERR, mi, e.stat, 0,
11264 11264 OP_CLOSE, FALSE, NULL, 0, dmapp->vp);
11265 11265 dmapp->caller->error = e.error;
11266 11266 }
11267 11267 }
11268 11268
11269 11269 (void) as_delete_callback(as, arg);
11270 11270 kmem_free(dmapp, sizeof (nfs4_delmap_args_t));
11271 11271 }
11272 11272
11273 11273
11274 11274 static uint_t
11275 11275 fattr4_maxfilesize_to_bits(uint64_t ll)
11276 11276 {
11277 11277 uint_t l = 1;
11278 11278
11279 11279 if (ll == 0) {
11280 11280 return (0);
11281 11281 }
11282 11282
11283 11283 if (ll & 0xffffffff00000000) {
11284 11284 l += 32; ll >>= 32;
11285 11285 }
11286 11286 if (ll & 0xffff0000) {
11287 11287 l += 16; ll >>= 16;
11288 11288 }
11289 11289 if (ll & 0xff00) {
11290 11290 l += 8; ll >>= 8;
11291 11291 }
11292 11292 if (ll & 0xf0) {
11293 11293 l += 4; ll >>= 4;
11294 11294 }
11295 11295 if (ll & 0xc) {
11296 11296 l += 2; ll >>= 2;
11297 11297 }
11298 11298 if (ll & 0x2) {
11299 11299 l += 1;
11300 11300 }
11301 11301 return (l);
11302 11302 }
11303 11303
11304 11304 static int
11305 11305 nfs4_have_xattrs(vnode_t *vp, ulong_t *valp, cred_t *cr)
11306 11306 {
11307 11307 vnode_t *avp = NULL;
11308 11308 int error;
11309 11309
11310 11310 if ((error = nfs4lookup_xattr(vp, "", &avp,
11311 11311 LOOKUP_XATTR, cr)) == 0)
11312 11312 error = do_xattr_exists_check(avp, valp, cr);
11313 11313 if (avp)
11314 11314 VN_RELE(avp);
11315 11315
11316 11316 return (error);
11317 11317 }
11318 11318
11319 11319 /* ARGSUSED */
11320 11320 int
11321 11321 nfs4_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
11322 11322 caller_context_t *ct)
11323 11323 {
11324 11324 int error;
11325 11325 hrtime_t t;
11326 11326 rnode4_t *rp;
11327 11327 nfs4_ga_res_t gar;
11328 11328 nfs4_ga_ext_res_t ger;
11329 11329
11330 11330 gar.n4g_ext_res = &ger;
11331 11331
11332 11332 if (nfs_zone() != VTOMI4(vp)->mi_zone)
11333 11333 return (EIO);
11334 11334 if (cmd == _PC_PATH_MAX || cmd == _PC_SYMLINK_MAX) {
11335 11335 *valp = MAXPATHLEN;
11336 11336 return (0);
11337 11337 }
11338 11338 if (cmd == _PC_ACL_ENABLED) {
11339 11339 *valp = _ACL_ACE_ENABLED;
11340 11340 return (0);
11341 11341 }
11342 11342
11343 11343 rp = VTOR4(vp);
11344 11344 if (cmd == _PC_XATTR_EXISTS) {
11345 11345 /*
11346 11346 * The existence of the xattr directory is not sufficient
11347 11347 * for determining whether generic user attributes exists.
11348 11348 * The attribute directory could only be a transient directory
11349 11349 * used for Solaris sysattr support. Do a small readdir
11350 11350 * to verify if the only entries are sysattrs or not.
11351 11351 *
11352 11352 * pc4_xattr_valid can be only be trusted when r_xattr_dir
11353 11353 * is NULL. Once the xadir vp exists, we can create xattrs,
11354 11354 * and we don't have any way to update the "base" object's
11355 11355 * pc4_xattr_exists from the xattr or xadir. Maybe FEM
11356 11356 * could help out.
11357 11357 */
11358 11358 if (ATTRCACHE4_VALID(vp) && rp->r_pathconf.pc4_xattr_valid &&
11359 11359 rp->r_xattr_dir == NULL) {
11360 11360 return (nfs4_have_xattrs(vp, valp, cr));
11361 11361 }
11362 11362 } else { /* OLD CODE */
11363 11363 if (ATTRCACHE4_VALID(vp)) {
11364 11364 mutex_enter(&rp->r_statelock);
11365 11365 if (rp->r_pathconf.pc4_cache_valid) {
11366 11366 error = 0;
11367 11367 switch (cmd) {
11368 11368 case _PC_FILESIZEBITS:
11369 11369 *valp =
11370 11370 rp->r_pathconf.pc4_filesizebits;
11371 11371 break;
11372 11372 case _PC_LINK_MAX:
11373 11373 *valp =
11374 11374 rp->r_pathconf.pc4_link_max;
11375 11375 break;
11376 11376 case _PC_NAME_MAX:
11377 11377 *valp =
11378 11378 rp->r_pathconf.pc4_name_max;
11379 11379 break;
11380 11380 case _PC_CHOWN_RESTRICTED:
11381 11381 *valp =
11382 11382 rp->r_pathconf.pc4_chown_restricted;
11383 11383 break;
11384 11384 case _PC_NO_TRUNC:
11385 11385 *valp =
11386 11386 rp->r_pathconf.pc4_no_trunc;
11387 11387 break;
11388 11388 default:
11389 11389 error = EINVAL;
11390 11390 break;
11391 11391 }
11392 11392 mutex_exit(&rp->r_statelock);
11393 11393 #ifdef DEBUG
11394 11394 nfs4_pathconf_cache_hits++;
11395 11395 #endif
11396 11396 return (error);
11397 11397 }
11398 11398 mutex_exit(&rp->r_statelock);
11399 11399 }
11400 11400 }
11401 11401 #ifdef DEBUG
11402 11402 nfs4_pathconf_cache_misses++;
11403 11403 #endif
11404 11404
11405 11405 t = gethrtime();
11406 11406
11407 11407 error = nfs4_attr_otw(vp, TAG_PATHCONF, &gar, NFS4_PATHCONF_MASK, cr);
11408 11408
11409 11409 if (error) {
11410 11410 mutex_enter(&rp->r_statelock);
11411 11411 rp->r_pathconf.pc4_cache_valid = FALSE;
11412 11412 rp->r_pathconf.pc4_xattr_valid = FALSE;
11413 11413 mutex_exit(&rp->r_statelock);
11414 11414 return (error);
11415 11415 }
11416 11416
11417 11417 /* interpret the max filesize */
11418 11418 gar.n4g_ext_res->n4g_pc4.pc4_filesizebits =
11419 11419 fattr4_maxfilesize_to_bits(gar.n4g_ext_res->n4g_maxfilesize);
11420 11420
11421 11421 /* Store the attributes we just received */
11422 11422 nfs4_attr_cache(vp, &gar, t, cr, TRUE, NULL);
11423 11423
11424 11424 switch (cmd) {
11425 11425 case _PC_FILESIZEBITS:
11426 11426 *valp = gar.n4g_ext_res->n4g_pc4.pc4_filesizebits;
11427 11427 break;
11428 11428 case _PC_LINK_MAX:
11429 11429 *valp = gar.n4g_ext_res->n4g_pc4.pc4_link_max;
11430 11430 break;
11431 11431 case _PC_NAME_MAX:
11432 11432 *valp = gar.n4g_ext_res->n4g_pc4.pc4_name_max;
11433 11433 break;
11434 11434 case _PC_CHOWN_RESTRICTED:
11435 11435 *valp = gar.n4g_ext_res->n4g_pc4.pc4_chown_restricted;
11436 11436 break;
11437 11437 case _PC_NO_TRUNC:
11438 11438 *valp = gar.n4g_ext_res->n4g_pc4.pc4_no_trunc;
11439 11439 break;
11440 11440 case _PC_XATTR_EXISTS:
11441 11441 if (gar.n4g_ext_res->n4g_pc4.pc4_xattr_exists) {
11442 11442 if (error = nfs4_have_xattrs(vp, valp, cr))
11443 11443 return (error);
11444 11444 }
11445 11445 break;
11446 11446 default:
11447 11447 return (EINVAL);
11448 11448 }
11449 11449
11450 11450 return (0);
11451 11451 }
11452 11452
11453 11453 /*
11454 11454 * Called by async thread to do synchronous pageio. Do the i/o, wait
11455 11455 * for it to complete, and cleanup the page list when done.
11456 11456 */
11457 11457 static int
11458 11458 nfs4_sync_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len,
11459 11459 int flags, cred_t *cr)
11460 11460 {
11461 11461 int error;
11462 11462
11463 11463 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
11464 11464
11465 11465 error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr);
11466 11466 if (flags & B_READ)
11467 11467 pvn_read_done(pp, (error ? B_ERROR : 0) | flags);
11468 11468 else
11469 11469 pvn_write_done(pp, (error ? B_ERROR : 0) | flags);
11470 11470 return (error);
11471 11471 }
11472 11472
11473 11473 /* ARGSUSED */
11474 11474 static int
11475 11475 nfs4_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len,
11476 11476 int flags, cred_t *cr, caller_context_t *ct)
11477 11477 {
11478 11478 int error;
11479 11479 rnode4_t *rp;
11480 11480
11481 11481 if (!(flags & B_ASYNC) && nfs_zone() != VTOMI4(vp)->mi_zone)
11482 11482 return (EIO);
11483 11483
11484 11484 if (pp == NULL)
11485 11485 return (EINVAL);
11486 11486
11487 11487 rp = VTOR4(vp);
11488 11488 mutex_enter(&rp->r_statelock);
11489 11489 rp->r_count++;
11490 11490 mutex_exit(&rp->r_statelock);
11491 11491
11492 11492 if (flags & B_ASYNC) {
11493 11493 error = nfs4_async_pageio(vp, pp, io_off, io_len, flags, cr,
11494 11494 nfs4_sync_pageio);
11495 11495 } else
11496 11496 error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr);
11497 11497 mutex_enter(&rp->r_statelock);
11498 11498 rp->r_count--;
11499 11499 cv_broadcast(&rp->r_cv);
11500 11500 mutex_exit(&rp->r_statelock);
11501 11501 return (error);
11502 11502 }
11503 11503
11504 11504 /* ARGSUSED */
11505 11505 static void
11506 11506 nfs4_dispose(vnode_t *vp, page_t *pp, int fl, int dn, cred_t *cr,
11507 11507 caller_context_t *ct)
11508 11508 {
11509 11509 int error;
11510 11510 rnode4_t *rp;
11511 11511 page_t *plist;
11512 11512 page_t *pptr;
11513 11513 offset3 offset;
11514 11514 count3 len;
11515 11515 k_sigset_t smask;
11516 11516
11517 11517 /*
11518 11518 * We should get called with fl equal to either B_FREE or
11519 11519 * B_INVAL. Any other value is illegal.
11520 11520 *
11521 11521 * The page that we are either supposed to free or destroy
11522 11522 * should be exclusive locked and its io lock should not
11523 11523 * be held.
11524 11524 */
11525 11525 ASSERT(fl == B_FREE || fl == B_INVAL);
11526 11526 ASSERT((PAGE_EXCL(pp) && !page_iolock_assert(pp)) || panicstr);
11527 11527
11528 11528 rp = VTOR4(vp);
11529 11529
11530 11530 /*
11531 11531 * If the page doesn't need to be committed or we shouldn't
11532 11532 * even bother attempting to commit it, then just make sure
11533 11533 * that the p_fsdata byte is clear and then either free or
11534 11534 * destroy the page as appropriate.
11535 11535 */
11536 11536 if (pp->p_fsdata == C_NOCOMMIT || (rp->r_flags & R4STALE)) {
11537 11537 pp->p_fsdata = C_NOCOMMIT;
11538 11538 if (fl == B_FREE)
11539 11539 page_free(pp, dn);
11540 11540 else
11541 11541 page_destroy(pp, dn);
11542 11542 return;
11543 11543 }
11544 11544
11545 11545 /*
11546 11546 * If there is a page invalidation operation going on, then
11547 11547 * if this is one of the pages being destroyed, then just
11548 11548 * clear the p_fsdata byte and then either free or destroy
11549 11549 * the page as appropriate.
11550 11550 */
11551 11551 mutex_enter(&rp->r_statelock);
11552 11552 if ((rp->r_flags & R4TRUNCATE) && pp->p_offset >= rp->r_truncaddr) {
11553 11553 mutex_exit(&rp->r_statelock);
11554 11554 pp->p_fsdata = C_NOCOMMIT;
11555 11555 if (fl == B_FREE)
11556 11556 page_free(pp, dn);
11557 11557 else
11558 11558 page_destroy(pp, dn);
11559 11559 return;
11560 11560 }
11561 11561
11562 11562 /*
11563 11563 * If we are freeing this page and someone else is already
11564 11564 * waiting to do a commit, then just unlock the page and
11565 11565 * return. That other thread will take care of commiting
11566 11566 * this page. The page can be freed sometime after the
11567 11567 * commit has finished. Otherwise, if the page is marked
11568 11568 * as delay commit, then we may be getting called from
11569 11569 * pvn_write_done, one page at a time. This could result
11570 11570 * in one commit per page, so we end up doing lots of small
11571 11571 * commits instead of fewer larger commits. This is bad,
11572 11572 * we want do as few commits as possible.
11573 11573 */
11574 11574 if (fl == B_FREE) {
11575 11575 if (rp->r_flags & R4COMMITWAIT) {
11576 11576 page_unlock(pp);
11577 11577 mutex_exit(&rp->r_statelock);
11578 11578 return;
11579 11579 }
11580 11580 if (pp->p_fsdata == C_DELAYCOMMIT) {
11581 11581 pp->p_fsdata = C_COMMIT;
11582 11582 page_unlock(pp);
11583 11583 mutex_exit(&rp->r_statelock);
11584 11584 return;
11585 11585 }
11586 11586 }
11587 11587
11588 11588 /*
11589 11589 * Check to see if there is a signal which would prevent an
11590 11590 * attempt to commit the pages from being successful. If so,
11591 11591 * then don't bother with all of the work to gather pages and
11592 11592 * generate the unsuccessful RPC. Just return from here and
11593 11593 * let the page be committed at some later time.
11594 11594 */
11595 11595 sigintr(&smask, VTOMI4(vp)->mi_flags & MI4_INT);
11596 11596 if (ttolwp(curthread) != NULL && ISSIG(curthread, JUSTLOOKING)) {
11597 11597 sigunintr(&smask);
11598 11598 page_unlock(pp);
11599 11599 mutex_exit(&rp->r_statelock);
11600 11600 return;
11601 11601 }
11602 11602 sigunintr(&smask);
11603 11603
11604 11604 /*
11605 11605 * We are starting to need to commit pages, so let's try
11606 11606 * to commit as many as possible at once to reduce the
11607 11607 * overhead.
11608 11608 *
11609 11609 * Set the `commit inprogress' state bit. We must
11610 11610 * first wait until any current one finishes. Then
11611 11611 * we initialize the c_pages list with this page.
11612 11612 */
11613 11613 while (rp->r_flags & R4COMMIT) {
11614 11614 rp->r_flags |= R4COMMITWAIT;
11615 11615 cv_wait(&rp->r_commit.c_cv, &rp->r_statelock);
11616 11616 rp->r_flags &= ~R4COMMITWAIT;
11617 11617 }
11618 11618 rp->r_flags |= R4COMMIT;
11619 11619 mutex_exit(&rp->r_statelock);
11620 11620 ASSERT(rp->r_commit.c_pages == NULL);
11621 11621 rp->r_commit.c_pages = pp;
11622 11622 rp->r_commit.c_commbase = (offset3)pp->p_offset;
11623 11623 rp->r_commit.c_commlen = PAGESIZE;
11624 11624
11625 11625 /*
11626 11626 * Gather together all other pages which can be committed.
11627 11627 * They will all be chained off r_commit.c_pages.
11628 11628 */
11629 11629 nfs4_get_commit(vp);
11630 11630
11631 11631 /*
11632 11632 * Clear the `commit inprogress' status and disconnect
11633 11633 * the list of pages to be committed from the rnode.
11634 11634 * At this same time, we also save the starting offset
11635 11635 * and length of data to be committed on the server.
11636 11636 */
11637 11637 plist = rp->r_commit.c_pages;
11638 11638 rp->r_commit.c_pages = NULL;
11639 11639 offset = rp->r_commit.c_commbase;
11640 11640 len = rp->r_commit.c_commlen;
11641 11641 mutex_enter(&rp->r_statelock);
11642 11642 rp->r_flags &= ~R4COMMIT;
11643 11643 cv_broadcast(&rp->r_commit.c_cv);
11644 11644 mutex_exit(&rp->r_statelock);
11645 11645
11646 11646 if (curproc == proc_pageout || curproc == proc_fsflush ||
11647 11647 nfs_zone() != VTOMI4(vp)->mi_zone) {
11648 11648 nfs4_async_commit(vp, plist, offset, len,
11649 11649 cr, do_nfs4_async_commit);
11650 11650 return;
11651 11651 }
11652 11652
11653 11653 /*
11654 11654 * Actually generate the COMMIT op over the wire operation.
11655 11655 */
11656 11656 error = nfs4_commit(vp, (offset4)offset, (count4)len, cr);
11657 11657
11658 11658 /*
11659 11659 * If we got an error during the commit, just unlock all
11660 11660 * of the pages. The pages will get retransmitted to the
11661 11661 * server during a putpage operation.
11662 11662 */
11663 11663 if (error) {
11664 11664 while (plist != NULL) {
11665 11665 pptr = plist;
11666 11666 page_sub(&plist, pptr);
11667 11667 page_unlock(pptr);
11668 11668 }
11669 11669 return;
11670 11670 }
11671 11671
11672 11672 /*
11673 11673 * We've tried as hard as we can to commit the data to stable
11674 11674 * storage on the server. We just unlock the rest of the pages
11675 11675 * and clear the commit required state. They will be put
11676 11676 * onto the tail of the cachelist if they are nolonger
11677 11677 * mapped.
11678 11678 */
11679 11679 while (plist != pp) {
11680 11680 pptr = plist;
11681 11681 page_sub(&plist, pptr);
11682 11682 pptr->p_fsdata = C_NOCOMMIT;
11683 11683 page_unlock(pptr);
11684 11684 }
11685 11685
11686 11686 /*
11687 11687 * It is possible that nfs4_commit didn't return error but
11688 11688 * some other thread has modified the page we are going
11689 11689 * to free/destroy.
11690 11690 * In this case we need to rewrite the page. Do an explicit check
11691 11691 * before attempting to free/destroy the page. If modified, needs to
11692 11692 * be rewritten so unlock the page and return.
11693 11693 */
11694 11694 if (hat_ismod(pp)) {
11695 11695 pp->p_fsdata = C_NOCOMMIT;
11696 11696 page_unlock(pp);
11697 11697 return;
11698 11698 }
11699 11699
11700 11700 /*
11701 11701 * Now, as appropriate, either free or destroy the page
11702 11702 * that we were called with.
11703 11703 */
11704 11704 pp->p_fsdata = C_NOCOMMIT;
11705 11705 if (fl == B_FREE)
11706 11706 page_free(pp, dn);
11707 11707 else
11708 11708 page_destroy(pp, dn);
11709 11709 }
11710 11710
11711 11711 /*
11712 11712 * Commit requires that the current fh be the file written to.
11713 11713 * The compound op structure is:
11714 11714 * PUTFH(file), COMMIT
11715 11715 */
11716 11716 static int
11717 11717 nfs4_commit(vnode_t *vp, offset4 offset, count4 count, cred_t *cr)
11718 11718 {
11719 11719 COMPOUND4args_clnt args;
11720 11720 COMPOUND4res_clnt res;
11721 11721 COMMIT4res *cm_res;
11722 11722 nfs_argop4 argop[2];
11723 11723 nfs_resop4 *resop;
11724 11724 int doqueue;
11725 11725 mntinfo4_t *mi;
11726 11726 rnode4_t *rp;
11727 11727 cred_t *cred_otw = NULL;
11728 11728 bool_t needrecov = FALSE;
11729 11729 nfs4_recov_state_t recov_state;
11730 11730 nfs4_open_stream_t *osp = NULL;
11731 11731 bool_t first_time = TRUE; /* first time getting OTW cred */
11732 11732 bool_t last_time = FALSE; /* last time getting OTW cred */
11733 11733 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
11734 11734
11735 11735 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
11736 11736
11737 11737 rp = VTOR4(vp);
11738 11738
11739 11739 mi = VTOMI4(vp);
11740 11740 recov_state.rs_flags = 0;
11741 11741 recov_state.rs_num_retry_despite_err = 0;
11742 11742 get_commit_cred:
11743 11743 /*
11744 11744 * Releases the osp, if a valid open stream is provided.
11745 11745 * Puts a hold on the cred_otw and the new osp (if found).
11746 11746 */
11747 11747 cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp,
11748 11748 &first_time, &last_time);
11749 11749 args.ctag = TAG_COMMIT;
11750 11750 recov_retry:
11751 11751 /*
11752 11752 * Commit ops: putfh file; commit
11753 11753 */
11754 11754 args.array_len = 2;
11755 11755 args.array = argop;
11756 11756
11757 11757 e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_COMMIT,
11758 11758 &recov_state, NULL);
11759 11759 if (e.error) {
11760 11760 crfree(cred_otw);
11761 11761 if (osp != NULL)
11762 11762 open_stream_rele(osp, rp);
11763 11763 return (e.error);
11764 11764 }
11765 11765
11766 11766 /* putfh directory */
11767 11767 argop[0].argop = OP_CPUTFH;
11768 11768 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
11769 11769
11770 11770 /* commit */
11771 11771 argop[1].argop = OP_COMMIT;
11772 11772 argop[1].nfs_argop4_u.opcommit.offset = offset;
11773 11773 argop[1].nfs_argop4_u.opcommit.count = count;
11774 11774
11775 11775 doqueue = 1;
11776 11776 rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, &e);
11777 11777
11778 11778 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
11779 11779 if (!needrecov && e.error) {
11780 11780 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, &recov_state,
11781 11781 needrecov);
11782 11782 crfree(cred_otw);
11783 11783 if (e.error == EACCES && last_time == FALSE)
11784 11784 goto get_commit_cred;
11785 11785 if (osp != NULL)
11786 11786 open_stream_rele(osp, rp);
11787 11787 return (e.error);
11788 11788 }
11789 11789
11790 11790 if (needrecov) {
11791 11791 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL,
11792 11792 NULL, OP_COMMIT, NULL, NULL, NULL) == FALSE) {
11793 11793 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT,
11794 11794 &recov_state, needrecov);
11795 11795 if (!e.error)
11796 11796 (void) xdr_free(xdr_COMPOUND4res_clnt,
11797 11797 (caddr_t)&res);
11798 11798 goto recov_retry;
11799 11799 }
11800 11800 if (e.error) {
11801 11801 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT,
11802 11802 &recov_state, needrecov);
11803 11803 crfree(cred_otw);
11804 11804 if (osp != NULL)
11805 11805 open_stream_rele(osp, rp);
11806 11806 return (e.error);
11807 11807 }
11808 11808 /* fall through for res.status case */
11809 11809 }
11810 11810
11811 11811 if (res.status) {
11812 11812 e.error = geterrno4(res.status);
11813 11813 if (e.error == EACCES && last_time == FALSE) {
11814 11814 crfree(cred_otw);
11815 11815 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT,
11816 11816 &recov_state, needrecov);
11817 11817 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
11818 11818 goto get_commit_cred;
11819 11819 }
11820 11820 /*
11821 11821 * Can't do a nfs4_purge_stale_fh here because this
11822 11822 * can cause a deadlock. nfs4_commit can
11823 11823 * be called from nfs4_dispose which can be called
11824 11824 * indirectly via pvn_vplist_dirty. nfs4_purge_stale_fh
11825 11825 * can call back to pvn_vplist_dirty.
11826 11826 */
11827 11827 if (e.error == ESTALE) {
11828 11828 mutex_enter(&rp->r_statelock);
11829 11829 rp->r_flags |= R4STALE;
11830 11830 if (!rp->r_error)
11831 11831 rp->r_error = e.error;
11832 11832 mutex_exit(&rp->r_statelock);
11833 11833 PURGE_ATTRCACHE4(vp);
11834 11834 } else {
11835 11835 mutex_enter(&rp->r_statelock);
11836 11836 if (!rp->r_error)
11837 11837 rp->r_error = e.error;
11838 11838 mutex_exit(&rp->r_statelock);
11839 11839 }
11840 11840 } else {
11841 11841 ASSERT(rp->r_flags & R4HAVEVERF);
11842 11842 resop = &res.array[1]; /* commit res */
11843 11843 cm_res = &resop->nfs_resop4_u.opcommit;
11844 11844 mutex_enter(&rp->r_statelock);
11845 11845 if (cm_res->writeverf == rp->r_writeverf) {
11846 11846 mutex_exit(&rp->r_statelock);
11847 11847 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
11848 11848 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT,
11849 11849 &recov_state, needrecov);
11850 11850 crfree(cred_otw);
11851 11851 if (osp != NULL)
11852 11852 open_stream_rele(osp, rp);
11853 11853 return (0);
11854 11854 }
11855 11855 nfs4_set_mod(vp);
11856 11856 rp->r_writeverf = cm_res->writeverf;
11857 11857 mutex_exit(&rp->r_statelock);
11858 11858 e.error = NFS_VERF_MISMATCH;
11859 11859 }
11860 11860
11861 11861 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
11862 11862 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, &recov_state, needrecov);
11863 11863 crfree(cred_otw);
11864 11864 if (osp != NULL)
11865 11865 open_stream_rele(osp, rp);
11866 11866
11867 11867 return (e.error);
11868 11868 }
11869 11869
11870 11870 static void
11871 11871 nfs4_set_mod(vnode_t *vp)
11872 11872 {
11873 11873 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
11874 11874
11875 11875 /* make sure we're looking at the master vnode, not a shadow */
11876 11876 pvn_vplist_setdirty(RTOV4(VTOR4(vp)), nfs_setmod_check);
11877 11877 }
11878 11878
11879 11879 /*
11880 11880 * This function is used to gather a page list of the pages which
11881 11881 * can be committed on the server.
11882 11882 *
11883 11883 * The calling thread must have set R4COMMIT. This bit is used to
11884 11884 * serialize access to the commit structure in the rnode. As long
11885 11885 * as the thread has set R4COMMIT, then it can manipulate the commit
11886 11886 * structure without requiring any other locks.
11887 11887 *
11888 11888 * When this function is called from nfs4_dispose() the page passed
11889 11889 * into nfs4_dispose() will be SE_EXCL locked, and so this function
11890 11890 * will skip it. This is not a problem since we initially add the
11891 11891 * page to the r_commit page list.
11892 11892 *
11893 11893 */
11894 11894 static void
11895 11895 nfs4_get_commit(vnode_t *vp)
11896 11896 {
11897 11897 rnode4_t *rp;
11898 11898 page_t *pp;
11899 11899 kmutex_t *vphm;
11900 11900
11901 11901 rp = VTOR4(vp);
11902 11902
11903 11903 ASSERT(rp->r_flags & R4COMMIT);
11904 11904
11905 11905 /* make sure we're looking at the master vnode, not a shadow */
11906 11906
11907 11907 if (IS_SHADOW(vp, rp))
11908 11908 vp = RTOV4(rp);
11909 11909
11910 11910 vphm = page_vnode_mutex(vp);
11911 11911 mutex_enter(vphm);
11912 11912
11913 11913 /*
11914 11914 * If there are no pages associated with this vnode, then
11915 11915 * just return.
11916 11916 */
11917 11917 if ((pp = vp->v_pages) == NULL) {
11918 11918 mutex_exit(vphm);
11919 11919 return;
11920 11920 }
11921 11921
11922 11922 /*
11923 11923 * Step through all of the pages associated with this vnode
11924 11924 * looking for pages which need to be committed.
11925 11925 */
11926 11926 do {
11927 11927 /* Skip marker pages. */
11928 11928 if (pp->p_hash == PVN_VPLIST_HASH_TAG)
11929 11929 continue;
11930 11930
11931 11931 /*
11932 11932 * First short-cut everything (without the page_lock)
11933 11933 * and see if this page does not need to be committed
11934 11934 * or is modified if so then we'll just skip it.
11935 11935 */
11936 11936 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp))
11937 11937 continue;
11938 11938
11939 11939 /*
11940 11940 * Attempt to lock the page. If we can't, then
11941 11941 * someone else is messing with it or we have been
11942 11942 * called from nfs4_dispose and this is the page that
11943 11943 * nfs4_dispose was called with.. anyway just skip it.
11944 11944 */
11945 11945 if (!page_trylock(pp, SE_EXCL))
11946 11946 continue;
11947 11947
11948 11948 /*
11949 11949 * Lets check again now that we have the page lock.
11950 11950 */
11951 11951 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) {
11952 11952 page_unlock(pp);
11953 11953 continue;
11954 11954 }
11955 11955
11956 11956 /* this had better not be a free page */
11957 11957 ASSERT(PP_ISFREE(pp) == 0);
11958 11958
11959 11959 /*
11960 11960 * The page needs to be committed and we locked it.
11961 11961 * Update the base and length parameters and add it
11962 11962 * to r_pages.
11963 11963 */
11964 11964 if (rp->r_commit.c_pages == NULL) {
11965 11965 rp->r_commit.c_commbase = (offset3)pp->p_offset;
11966 11966 rp->r_commit.c_commlen = PAGESIZE;
11967 11967 } else if (pp->p_offset < rp->r_commit.c_commbase) {
11968 11968 rp->r_commit.c_commlen = rp->r_commit.c_commbase -
11969 11969 (offset3)pp->p_offset + rp->r_commit.c_commlen;
11970 11970 rp->r_commit.c_commbase = (offset3)pp->p_offset;
11971 11971 } else if ((rp->r_commit.c_commbase + rp->r_commit.c_commlen)
11972 11972 <= pp->p_offset) {
11973 11973 rp->r_commit.c_commlen = (offset3)pp->p_offset -
11974 11974 rp->r_commit.c_commbase + PAGESIZE;
11975 11975 }
11976 11976 page_add(&rp->r_commit.c_pages, pp);
11977 11977 } while ((pp = pp->p_vpnext) != vp->v_pages);
11978 11978
11979 11979 mutex_exit(vphm);
11980 11980 }
11981 11981
11982 11982 /*
11983 11983 * This routine is used to gather together a page list of the pages
11984 11984 * which are to be committed on the server. This routine must not
11985 11985 * be called if the calling thread holds any locked pages.
11986 11986 *
11987 11987 * The calling thread must have set R4COMMIT. This bit is used to
11988 11988 * serialize access to the commit structure in the rnode. As long
11989 11989 * as the thread has set R4COMMIT, then it can manipulate the commit
11990 11990 * structure without requiring any other locks.
11991 11991 */
11992 11992 static void
11993 11993 nfs4_get_commit_range(vnode_t *vp, u_offset_t soff, size_t len)
11994 11994 {
11995 11995
11996 11996 rnode4_t *rp;
11997 11997 page_t *pp;
11998 11998 u_offset_t end;
11999 11999 u_offset_t off;
12000 12000 ASSERT(len != 0);
12001 12001 rp = VTOR4(vp);
12002 12002 ASSERT(rp->r_flags & R4COMMIT);
12003 12003
12004 12004 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12005 12005
12006 12006 /* make sure we're looking at the master vnode, not a shadow */
12007 12007
12008 12008 if (IS_SHADOW(vp, rp))
12009 12009 vp = RTOV4(rp);
12010 12010
12011 12011 /*
12012 12012 * If there are no pages associated with this vnode, then
12013 12013 * just return.
12014 12014 */
12015 12015 if ((pp = vp->v_pages) == NULL)
12016 12016 return;
12017 12017 /*
12018 12018 * Calculate the ending offset.
12019 12019 */
12020 12020 end = soff + len;
12021 12021 for (off = soff; off < end; off += PAGESIZE) {
12022 12022 /*
12023 12023 * Lookup each page by vp, offset.
12024 12024 */
12025 12025 if ((pp = page_lookup_nowait(vp, off, SE_EXCL)) == NULL)
12026 12026 continue;
12027 12027 /*
12028 12028 * If this page does not need to be committed or is
12029 12029 * modified, then just skip it.
12030 12030 */
12031 12031 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) {
12032 12032 page_unlock(pp);
12033 12033 continue;
12034 12034 }
12035 12035
12036 12036 ASSERT(PP_ISFREE(pp) == 0);
12037 12037 /*
12038 12038 * The page needs to be committed and we locked it.
12039 12039 * Update the base and length parameters and add it
12040 12040 * to r_pages.
12041 12041 */
12042 12042 if (rp->r_commit.c_pages == NULL) {
12043 12043 rp->r_commit.c_commbase = (offset3)pp->p_offset;
12044 12044 rp->r_commit.c_commlen = PAGESIZE;
12045 12045 } else {
12046 12046 rp->r_commit.c_commlen = (offset3)pp->p_offset -
12047 12047 rp->r_commit.c_commbase + PAGESIZE;
12048 12048 }
12049 12049 page_add(&rp->r_commit.c_pages, pp);
12050 12050 }
12051 12051 }
12052 12052
12053 12053 /*
12054 12054 * Called from nfs4_close(), nfs4_fsync() and nfs4_delmap().
12055 12055 * Flushes and commits data to the server.
12056 12056 */
12057 12057 static int
12058 12058 nfs4_putpage_commit(vnode_t *vp, offset_t poff, size_t plen, cred_t *cr)
12059 12059 {
12060 12060 int error;
12061 12061 verifier4 write_verf;
12062 12062 rnode4_t *rp = VTOR4(vp);
12063 12063
12064 12064 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12065 12065
12066 12066 /*
12067 12067 * Flush the data portion of the file and then commit any
12068 12068 * portions which need to be committed. This may need to
12069 12069 * be done twice if the server has changed state since
12070 12070 * data was last written. The data will need to be
12071 12071 * rewritten to the server and then a new commit done.
12072 12072 *
12073 12073 * In fact, this may need to be done several times if the
12074 12074 * server is having problems and crashing while we are
12075 12075 * attempting to do this.
12076 12076 */
12077 12077
12078 12078 top:
12079 12079 /*
12080 12080 * Do a flush based on the poff and plen arguments. This
12081 12081 * will synchronously write out any modified pages in the
12082 12082 * range specified by (poff, plen). This starts all of the
12083 12083 * i/o operations which will be waited for in the next
12084 12084 * call to nfs4_putpage
12085 12085 */
12086 12086
12087 12087 mutex_enter(&rp->r_statelock);
12088 12088 write_verf = rp->r_writeverf;
12089 12089 mutex_exit(&rp->r_statelock);
12090 12090
12091 12091 error = nfs4_putpage(vp, poff, plen, B_ASYNC, cr, NULL);
12092 12092 if (error == EAGAIN)
12093 12093 error = 0;
12094 12094
12095 12095 /*
12096 12096 * Do a flush based on the poff and plen arguments. This
12097 12097 * will synchronously write out any modified pages in the
12098 12098 * range specified by (poff, plen) and wait until all of
12099 12099 * the asynchronous i/o's in that range are done as well.
12100 12100 */
12101 12101 if (!error)
12102 12102 error = nfs4_putpage(vp, poff, plen, 0, cr, NULL);
12103 12103
12104 12104 if (error)
12105 12105 return (error);
12106 12106
12107 12107 mutex_enter(&rp->r_statelock);
12108 12108 if (rp->r_writeverf != write_verf) {
12109 12109 mutex_exit(&rp->r_statelock);
12110 12110 goto top;
12111 12111 }
12112 12112 mutex_exit(&rp->r_statelock);
12113 12113
12114 12114 /*
12115 12115 * Now commit any pages which might need to be committed.
12116 12116 * If the error, NFS_VERF_MISMATCH, is returned, then
12117 12117 * start over with the flush operation.
12118 12118 */
12119 12119 error = nfs4_commit_vp(vp, poff, plen, cr, NFS4_WRITE_WAIT);
12120 12120
12121 12121 if (error == NFS_VERF_MISMATCH)
12122 12122 goto top;
12123 12123
12124 12124 return (error);
12125 12125 }
12126 12126
12127 12127 /*
12128 12128 * nfs4_commit_vp() will wait for other pending commits and
12129 12129 * will either commit the whole file or a range, plen dictates
12130 12130 * if we commit whole file. a value of zero indicates the whole
12131 12131 * file. Called from nfs4_putpage_commit() or nfs4_sync_putapage()
12132 12132 */
12133 12133 static int
12134 12134 nfs4_commit_vp(vnode_t *vp, u_offset_t poff, size_t plen,
12135 12135 cred_t *cr, int wait_on_writes)
12136 12136 {
12137 12137 rnode4_t *rp;
12138 12138 page_t *plist;
12139 12139 offset3 offset;
12140 12140 count3 len;
12141 12141
12142 12142 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12143 12143
12144 12144 rp = VTOR4(vp);
12145 12145
12146 12146 /*
12147 12147 * before we gather commitable pages make
12148 12148 * sure there are no outstanding async writes
12149 12149 */
12150 12150 if (rp->r_count && wait_on_writes == NFS4_WRITE_WAIT) {
12151 12151 mutex_enter(&rp->r_statelock);
12152 12152 while (rp->r_count > 0) {
12153 12153 cv_wait(&rp->r_cv, &rp->r_statelock);
12154 12154 }
12155 12155 mutex_exit(&rp->r_statelock);
12156 12156 }
12157 12157
12158 12158 /*
12159 12159 * Set the `commit inprogress' state bit. We must
12160 12160 * first wait until any current one finishes.
12161 12161 */
12162 12162 mutex_enter(&rp->r_statelock);
12163 12163 while (rp->r_flags & R4COMMIT) {
12164 12164 rp->r_flags |= R4COMMITWAIT;
12165 12165 cv_wait(&rp->r_commit.c_cv, &rp->r_statelock);
12166 12166 rp->r_flags &= ~R4COMMITWAIT;
12167 12167 }
12168 12168 rp->r_flags |= R4COMMIT;
12169 12169 mutex_exit(&rp->r_statelock);
12170 12170
12171 12171 /*
12172 12172 * Gather all of the pages which need to be
12173 12173 * committed.
12174 12174 */
12175 12175 if (plen == 0)
12176 12176 nfs4_get_commit(vp);
12177 12177 else
12178 12178 nfs4_get_commit_range(vp, poff, plen);
12179 12179
12180 12180 /*
12181 12181 * Clear the `commit inprogress' bit and disconnect the
12182 12182 * page list which was gathered by nfs4_get_commit.
12183 12183 */
12184 12184 plist = rp->r_commit.c_pages;
12185 12185 rp->r_commit.c_pages = NULL;
12186 12186 offset = rp->r_commit.c_commbase;
12187 12187 len = rp->r_commit.c_commlen;
12188 12188 mutex_enter(&rp->r_statelock);
12189 12189 rp->r_flags &= ~R4COMMIT;
12190 12190 cv_broadcast(&rp->r_commit.c_cv);
12191 12191 mutex_exit(&rp->r_statelock);
12192 12192
12193 12193 /*
12194 12194 * If any pages need to be committed, commit them and
12195 12195 * then unlock them so that they can be freed some
12196 12196 * time later.
12197 12197 */
12198 12198 if (plist == NULL)
12199 12199 return (0);
12200 12200
12201 12201 /*
12202 12202 * No error occurred during the flush portion
12203 12203 * of this operation, so now attempt to commit
12204 12204 * the data to stable storage on the server.
12205 12205 *
12206 12206 * This will unlock all of the pages on the list.
12207 12207 */
12208 12208 return (nfs4_sync_commit(vp, plist, offset, len, cr));
12209 12209 }
12210 12210
12211 12211 static int
12212 12212 nfs4_sync_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count,
12213 12213 cred_t *cr)
12214 12214 {
12215 12215 int error;
12216 12216 page_t *pp;
12217 12217
12218 12218 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12219 12219
12220 12220 error = nfs4_commit(vp, (offset4)offset, (count3)count, cr);
12221 12221
12222 12222 /*
12223 12223 * If we got an error, then just unlock all of the pages
12224 12224 * on the list.
12225 12225 */
12226 12226 if (error) {
12227 12227 while (plist != NULL) {
12228 12228 pp = plist;
12229 12229 page_sub(&plist, pp);
12230 12230 page_unlock(pp);
12231 12231 }
12232 12232 return (error);
12233 12233 }
12234 12234 /*
12235 12235 * We've tried as hard as we can to commit the data to stable
12236 12236 * storage on the server. We just unlock the pages and clear
12237 12237 * the commit required state. They will get freed later.
12238 12238 */
12239 12239 while (plist != NULL) {
12240 12240 pp = plist;
12241 12241 page_sub(&plist, pp);
12242 12242 pp->p_fsdata = C_NOCOMMIT;
12243 12243 page_unlock(pp);
12244 12244 }
12245 12245
12246 12246 return (error);
12247 12247 }
12248 12248
12249 12249 static void
12250 12250 do_nfs4_async_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count,
12251 12251 cred_t *cr)
12252 12252 {
12253 12253
12254 12254 (void) nfs4_sync_commit(vp, plist, offset, count, cr);
12255 12255 }
12256 12256
12257 12257 /*ARGSUSED*/
12258 12258 static int
12259 12259 nfs4_setsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr,
12260 12260 caller_context_t *ct)
12261 12261 {
12262 12262 int error = 0;
12263 12263 mntinfo4_t *mi;
12264 12264 vattr_t va;
12265 12265 vsecattr_t nfsace4_vsap;
12266 12266
12267 12267 mi = VTOMI4(vp);
12268 12268 if (nfs_zone() != mi->mi_zone)
12269 12269 return (EIO);
12270 12270 if (mi->mi_flags & MI4_ACL) {
12271 12271 /* if we have a delegation, return it */
12272 12272 if (VTOR4(vp)->r_deleg_type != OPEN_DELEGATE_NONE)
12273 12273 (void) nfs4delegreturn(VTOR4(vp),
12274 12274 NFS4_DR_REOPEN|NFS4_DR_PUSH);
12275 12275
12276 12276 error = nfs4_is_acl_mask_valid(vsecattr->vsa_mask,
12277 12277 NFS4_ACL_SET);
12278 12278 if (error) /* EINVAL */
12279 12279 return (error);
12280 12280
12281 12281 if (vsecattr->vsa_mask & (VSA_ACL | VSA_DFACL)) {
12282 12282 /*
12283 12283 * These are aclent_t type entries.
12284 12284 */
12285 12285 error = vs_aent_to_ace4(vsecattr, &nfsace4_vsap,
12286 12286 vp->v_type == VDIR, FALSE);
12287 12287 if (error)
12288 12288 return (error);
12289 12289 } else {
12290 12290 /*
12291 12291 * These are ace_t type entries.
12292 12292 */
12293 12293 error = vs_acet_to_ace4(vsecattr, &nfsace4_vsap,
12294 12294 FALSE);
12295 12295 if (error)
12296 12296 return (error);
12297 12297 }
12298 12298 bzero(&va, sizeof (va));
12299 12299 error = nfs4setattr(vp, &va, flag, cr, &nfsace4_vsap);
12300 12300 vs_ace4_destroy(&nfsace4_vsap);
12301 12301 return (error);
12302 12302 }
12303 12303 return (ENOSYS);
12304 12304 }
12305 12305
12306 12306 /* ARGSUSED */
12307 12307 int
12308 12308 nfs4_getsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr,
12309 12309 caller_context_t *ct)
12310 12310 {
12311 12311 int error;
12312 12312 mntinfo4_t *mi;
12313 12313 nfs4_ga_res_t gar;
12314 12314 rnode4_t *rp = VTOR4(vp);
12315 12315
12316 12316 mi = VTOMI4(vp);
12317 12317 if (nfs_zone() != mi->mi_zone)
12318 12318 return (EIO);
12319 12319
12320 12320 bzero(&gar, sizeof (gar));
12321 12321 gar.n4g_vsa.vsa_mask = vsecattr->vsa_mask;
12322 12322
12323 12323 /*
12324 12324 * vsecattr->vsa_mask holds the original acl request mask.
12325 12325 * This is needed when determining what to return.
12326 12326 * (See: nfs4_create_getsecattr_return())
12327 12327 */
12328 12328 error = nfs4_is_acl_mask_valid(vsecattr->vsa_mask, NFS4_ACL_GET);
12329 12329 if (error) /* EINVAL */
12330 12330 return (error);
12331 12331
12332 12332 /*
12333 12333 * If this is a referral stub, don't try to go OTW for an ACL
12334 12334 */
12335 12335 if (RP_ISSTUB_REFERRAL(VTOR4(vp)))
12336 12336 return (fs_fab_acl(vp, vsecattr, flag, cr, ct));
12337 12337
12338 12338 if (mi->mi_flags & MI4_ACL) {
12339 12339 /*
12340 12340 * Check if the data is cached and the cache is valid. If it
12341 12341 * is we don't go over the wire.
12342 12342 */
12343 12343 if (rp->r_secattr != NULL && ATTRCACHE4_VALID(vp)) {
12344 12344 mutex_enter(&rp->r_statelock);
12345 12345 if (rp->r_secattr != NULL) {
12346 12346 error = nfs4_create_getsecattr_return(
12347 12347 rp->r_secattr, vsecattr, rp->r_attr.va_uid,
12348 12348 rp->r_attr.va_gid,
12349 12349 vp->v_type == VDIR);
12350 12350 if (!error) { /* error == 0 - Success! */
12351 12351 mutex_exit(&rp->r_statelock);
12352 12352 return (error);
12353 12353 }
12354 12354 }
12355 12355 mutex_exit(&rp->r_statelock);
12356 12356 }
12357 12357
12358 12358 /*
12359 12359 * The getattr otw call will always get both the acl, in
12360 12360 * the form of a list of nfsace4's, and the number of acl
12361 12361 * entries; independent of the value of gar.n4g_vsa.vsa_mask.
12362 12362 */
12363 12363 gar.n4g_va.va_mask = AT_ALL;
12364 12364 error = nfs4_getattr_otw(vp, &gar, cr, 1);
12365 12365 if (error) {
12366 12366 vs_ace4_destroy(&gar.n4g_vsa);
12367 12367 if (error == ENOTSUP || error == EOPNOTSUPP)
12368 12368 error = fs_fab_acl(vp, vsecattr, flag, cr, ct);
12369 12369 return (error);
12370 12370 }
12371 12371
12372 12372 if (!(gar.n4g_resbmap & FATTR4_ACL_MASK)) {
12373 12373 /*
12374 12374 * No error was returned, but according to the response
12375 12375 * bitmap, neither was an acl.
12376 12376 */
12377 12377 vs_ace4_destroy(&gar.n4g_vsa);
12378 12378 error = fs_fab_acl(vp, vsecattr, flag, cr, ct);
12379 12379 return (error);
12380 12380 }
12381 12381
12382 12382 /*
12383 12383 * Update the cache with the ACL.
12384 12384 */
12385 12385 nfs4_acl_fill_cache(rp, &gar.n4g_vsa);
12386 12386
12387 12387 error = nfs4_create_getsecattr_return(&gar.n4g_vsa,
12388 12388 vsecattr, gar.n4g_va.va_uid, gar.n4g_va.va_gid,
12389 12389 vp->v_type == VDIR);
12390 12390 vs_ace4_destroy(&gar.n4g_vsa);
12391 12391 if ((error) && (vsecattr->vsa_mask &
12392 12392 (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT)) &&
12393 12393 (error != EACCES)) {
12394 12394 error = fs_fab_acl(vp, vsecattr, flag, cr, ct);
12395 12395 }
12396 12396 return (error);
12397 12397 }
12398 12398 error = fs_fab_acl(vp, vsecattr, flag, cr, ct);
12399 12399 return (error);
12400 12400 }
12401 12401
12402 12402 /*
12403 12403 * The function returns:
12404 12404 * - 0 (zero) if the passed in "acl_mask" is a valid request.
12405 12405 * - EINVAL if the passed in "acl_mask" is an invalid request.
12406 12406 *
12407 12407 * In the case of getting an acl (op == NFS4_ACL_GET) the mask is invalid if:
12408 12408 * - We have a mixture of ACE and ACL requests (e.g. VSA_ACL | VSA_ACE)
12409 12409 *
12410 12410 * In the case of setting an acl (op == NFS4_ACL_SET) the mask is invalid if:
12411 12411 * - We have a mixture of ACE and ACL requests (e.g. VSA_ACL | VSA_ACE)
12412 12412 * - We have a count field set without the corresponding acl field set. (e.g. -
12413 12413 * VSA_ACECNT is set, but VSA_ACE is not)
12414 12414 */
12415 12415 static int
12416 12416 nfs4_is_acl_mask_valid(uint_t acl_mask, nfs4_acl_op_t op)
12417 12417 {
12418 12418 /* Shortcut the masks that are always valid. */
12419 12419 if (acl_mask == (VSA_ACE | VSA_ACECNT))
12420 12420 return (0);
12421 12421 if (acl_mask == (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT))
12422 12422 return (0);
12423 12423
12424 12424 if (acl_mask & (VSA_ACE | VSA_ACECNT)) {
12425 12425 /*
12426 12426 * We can't have any VSA_ACL type stuff in the mask now.
12427 12427 */
12428 12428 if (acl_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL |
12429 12429 VSA_DFACLCNT))
12430 12430 return (EINVAL);
12431 12431
12432 12432 if (op == NFS4_ACL_SET) {
12433 12433 if ((acl_mask & VSA_ACECNT) && !(acl_mask & VSA_ACE))
12434 12434 return (EINVAL);
12435 12435 }
12436 12436 }
12437 12437
12438 12438 if (acl_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT)) {
12439 12439 /*
12440 12440 * We can't have any VSA_ACE type stuff in the mask now.
12441 12441 */
12442 12442 if (acl_mask & (VSA_ACE | VSA_ACECNT))
12443 12443 return (EINVAL);
12444 12444
12445 12445 if (op == NFS4_ACL_SET) {
12446 12446 if ((acl_mask & VSA_ACLCNT) && !(acl_mask & VSA_ACL))
12447 12447 return (EINVAL);
12448 12448
12449 12449 if ((acl_mask & VSA_DFACLCNT) &&
12450 12450 !(acl_mask & VSA_DFACL))
12451 12451 return (EINVAL);
12452 12452 }
12453 12453 }
12454 12454 return (0);
12455 12455 }
12456 12456
12457 12457 /*
12458 12458 * The theory behind creating the correct getsecattr return is simply this:
12459 12459 * "Don't return anything that the caller is not expecting to have to free."
12460 12460 */
12461 12461 static int
12462 12462 nfs4_create_getsecattr_return(vsecattr_t *filled_vsap, vsecattr_t *vsap,
12463 12463 uid_t uid, gid_t gid, int isdir)
12464 12464 {
12465 12465 int error = 0;
12466 12466 /* Save the mask since the translators modify it. */
12467 12467 uint_t orig_mask = vsap->vsa_mask;
12468 12468
12469 12469 if (orig_mask & (VSA_ACE | VSA_ACECNT)) {
12470 12470 error = vs_ace4_to_acet(filled_vsap, vsap, uid, gid, FALSE);
12471 12471
12472 12472 if (error)
12473 12473 return (error);
12474 12474
12475 12475 /*
12476 12476 * If the caller only asked for the ace count (VSA_ACECNT)
12477 12477 * don't give them the full acl (VSA_ACE), free it.
12478 12478 */
12479 12479 if (!orig_mask & VSA_ACE) {
12480 12480 if (vsap->vsa_aclentp != NULL) {
12481 12481 kmem_free(vsap->vsa_aclentp,
12482 12482 vsap->vsa_aclcnt * sizeof (ace_t));
12483 12483 vsap->vsa_aclentp = NULL;
12484 12484 }
12485 12485 }
12486 12486 vsap->vsa_mask = orig_mask;
12487 12487
12488 12488 } else if (orig_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL |
12489 12489 VSA_DFACLCNT)) {
12490 12490 error = vs_ace4_to_aent(filled_vsap, vsap, uid, gid,
12491 12491 isdir, FALSE);
12492 12492
12493 12493 if (error)
12494 12494 return (error);
12495 12495
12496 12496 /*
12497 12497 * If the caller only asked for the acl count (VSA_ACLCNT)
12498 12498 * and/or the default acl count (VSA_DFACLCNT) don't give them
12499 12499 * the acl (VSA_ACL) or default acl (VSA_DFACL), free it.
12500 12500 */
12501 12501 if (!orig_mask & VSA_ACL) {
12502 12502 if (vsap->vsa_aclentp != NULL) {
12503 12503 kmem_free(vsap->vsa_aclentp,
12504 12504 vsap->vsa_aclcnt * sizeof (aclent_t));
12505 12505 vsap->vsa_aclentp = NULL;
12506 12506 }
12507 12507 }
12508 12508
12509 12509 if (!orig_mask & VSA_DFACL) {
12510 12510 if (vsap->vsa_dfaclentp != NULL) {
12511 12511 kmem_free(vsap->vsa_dfaclentp,
12512 12512 vsap->vsa_dfaclcnt * sizeof (aclent_t));
12513 12513 vsap->vsa_dfaclentp = NULL;
12514 12514 }
12515 12515 }
12516 12516 vsap->vsa_mask = orig_mask;
12517 12517 }
12518 12518 return (0);
12519 12519 }
12520 12520
12521 12521 /* ARGSUSED */
12522 12522 int
12523 12523 nfs4_shrlock(vnode_t *vp, int cmd, struct shrlock *shr, int flag, cred_t *cr,
12524 12524 caller_context_t *ct)
12525 12525 {
12526 12526 int error;
12527 12527
12528 12528 if (nfs_zone() != VTOMI4(vp)->mi_zone)
12529 12529 return (EIO);
12530 12530 /*
12531 12531 * check for valid cmd parameter
12532 12532 */
12533 12533 if (cmd != F_SHARE && cmd != F_UNSHARE && cmd != F_HASREMOTELOCKS)
12534 12534 return (EINVAL);
12535 12535
12536 12536 /*
12537 12537 * Check access permissions
12538 12538 */
12539 12539 if ((cmd & F_SHARE) &&
12540 12540 (((shr->s_access & F_RDACC) && (flag & FREAD) == 0) ||
12541 12541 (shr->s_access == F_WRACC && (flag & FWRITE) == 0)))
12542 12542 return (EBADF);
12543 12543
12544 12544 /*
12545 12545 * If the filesystem is mounted using local locking, pass the
12546 12546 * request off to the local share code.
12547 12547 */
12548 12548 if (VTOMI4(vp)->mi_flags & MI4_LLOCK)
12549 12549 return (fs_shrlock(vp, cmd, shr, flag, cr, ct));
12550 12550
12551 12551 switch (cmd) {
12552 12552 case F_SHARE:
12553 12553 case F_UNSHARE:
12554 12554 /*
12555 12555 * This will be properly implemented later,
12556 12556 * see RFE: 4823948 .
12557 12557 */
12558 12558 error = EAGAIN;
12559 12559 break;
12560 12560
12561 12561 case F_HASREMOTELOCKS:
12562 12562 /*
12563 12563 * NFS client can't store remote locks itself
12564 12564 */
12565 12565 shr->s_access = 0;
12566 12566 error = 0;
12567 12567 break;
12568 12568
12569 12569 default:
12570 12570 error = EINVAL;
12571 12571 break;
12572 12572 }
12573 12573
12574 12574 return (error);
12575 12575 }
12576 12576
12577 12577 /*
12578 12578 * Common code called by directory ops to update the attrcache
12579 12579 */
12580 12580 static int
12581 12581 nfs4_update_attrcache(nfsstat4 status, nfs4_ga_res_t *garp,
12582 12582 hrtime_t t, vnode_t *vp, cred_t *cr)
12583 12583 {
12584 12584 int error = 0;
12585 12585
12586 12586 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12587 12587
12588 12588 if (status != NFS4_OK) {
12589 12589 /* getattr not done or failed */
12590 12590 PURGE_ATTRCACHE4(vp);
12591 12591 return (error);
12592 12592 }
12593 12593
12594 12594 if (garp) {
12595 12595 nfs4_attr_cache(vp, garp, t, cr, FALSE, NULL);
12596 12596 } else {
12597 12597 PURGE_ATTRCACHE4(vp);
12598 12598 }
12599 12599 return (error);
12600 12600 }
12601 12601
12602 12602 /*
12603 12603 * Update directory caches for directory modification ops (link, rename, etc.)
12604 12604 * When dinfo is NULL, manage dircaches in the old way.
12605 12605 */
12606 12606 static void
12607 12607 nfs4_update_dircaches(change_info4 *cinfo, vnode_t *dvp, vnode_t *vp, char *nm,
12608 12608 dirattr_info_t *dinfo)
12609 12609 {
12610 12610 rnode4_t *drp = VTOR4(dvp);
12611 12611
12612 12612 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
12613 12613
12614 12614 /* Purge rddir cache for dir since it changed */
12615 12615 if (drp->r_dir != NULL)
12616 12616 nfs4_purge_rddir_cache(dvp);
12617 12617
12618 12618 /*
12619 12619 * If caller provided dinfo, then use it to manage dir caches.
12620 12620 */
12621 12621 if (dinfo != NULL) {
12622 12622 if (vp != NULL) {
12623 12623 mutex_enter(&VTOR4(vp)->r_statev4_lock);
12624 12624 if (!VTOR4(vp)->created_v4) {
12625 12625 mutex_exit(&VTOR4(vp)->r_statev4_lock);
12626 12626 dnlc_update(dvp, nm, vp);
12627 12627 } else {
12628 12628 /*
12629 12629 * XXX don't update if the created_v4 flag is
12630 12630 * set
12631 12631 */
12632 12632 mutex_exit(&VTOR4(vp)->r_statev4_lock);
12633 12633 NFS4_DEBUG(nfs4_client_state_debug,
12634 12634 (CE_NOTE, "nfs4_update_dircaches: "
12635 12635 "don't update dnlc: created_v4 flag"));
12636 12636 }
12637 12637 }
12638 12638
12639 12639 nfs4_attr_cache(dvp, dinfo->di_garp, dinfo->di_time_call,
12640 12640 dinfo->di_cred, FALSE, cinfo);
12641 12641
12642 12642 return;
12643 12643 }
12644 12644
12645 12645 /*
12646 12646 * Caller didn't provide dinfo, then check change_info4 to update DNLC.
12647 12647 * Since caller modified dir but didn't receive post-dirmod-op dir
12648 12648 * attrs, the dir's attrs must be purged.
12649 12649 *
12650 12650 * XXX this check and dnlc update/purge should really be atomic,
12651 12651 * XXX but can't use rnode statelock because it'll deadlock in
12652 12652 * XXX dnlc_purge_vp, however, the risk is minimal even if a race
12653 12653 * XXX does occur.
12654 12654 *
12655 12655 * XXX We also may want to check that atomic is true in the
12656 12656 * XXX change_info struct. If it is not, the change_info may
12657 12657 * XXX reflect changes by more than one clients which means that
12658 12658 * XXX our cache may not be valid.
12659 12659 */
12660 12660 PURGE_ATTRCACHE4(dvp);
12661 12661 if (drp->r_change == cinfo->before) {
12662 12662 /* no changes took place in the directory prior to our link */
12663 12663 if (vp != NULL) {
12664 12664 mutex_enter(&VTOR4(vp)->r_statev4_lock);
12665 12665 if (!VTOR4(vp)->created_v4) {
12666 12666 mutex_exit(&VTOR4(vp)->r_statev4_lock);
12667 12667 dnlc_update(dvp, nm, vp);
12668 12668 } else {
12669 12669 /*
12670 12670 * XXX dont' update if the created_v4 flag
12671 12671 * is set
12672 12672 */
12673 12673 mutex_exit(&VTOR4(vp)->r_statev4_lock);
12674 12674 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE,
12675 12675 "nfs4_update_dircaches: don't"
12676 12676 " update dnlc: created_v4 flag"));
12677 12677 }
12678 12678 }
12679 12679 } else {
12680 12680 /* Another client modified directory - purge its dnlc cache */
12681 12681 dnlc_purge_vp(dvp);
12682 12682 }
12683 12683 }
12684 12684
12685 12685 /*
12686 12686 * The OPEN_CONFIRM operation confirms the sequence number used in OPENing a
12687 12687 * file.
12688 12688 *
12689 12689 * The 'reopening_file' boolean should be set to TRUE if we are reopening this
12690 12690 * file (ie: client recovery) and otherwise set to FALSE.
12691 12691 *
12692 12692 * 'nfs4_start/end_op' should have been called by the proper (ie: not recovery
12693 12693 * initiated) calling functions.
12694 12694 *
12695 12695 * 'resend' is set to TRUE if this is a OPEN_CONFIRM issued as a result
12696 12696 * of resending a 'lost' open request.
12697 12697 *
12698 12698 * 'num_bseqid_retryp' makes sure we don't loop forever on a broken
12699 12699 * server that hands out BAD_SEQID on open confirm.
12700 12700 *
12701 12701 * Errors are returned via the nfs4_error_t parameter.
12702 12702 */
12703 12703 void
12704 12704 nfs4open_confirm(vnode_t *vp, seqid4 *seqid, stateid4 *stateid, cred_t *cr,
12705 12705 bool_t reopening_file, bool_t *retry_open, nfs4_open_owner_t *oop,
12706 12706 bool_t resend, nfs4_error_t *ep, int *num_bseqid_retryp)
12707 12707 {
12708 12708 COMPOUND4args_clnt args;
12709 12709 COMPOUND4res_clnt res;
12710 12710 nfs_argop4 argop[2];
12711 12711 nfs_resop4 *resop;
12712 12712 int doqueue = 1;
12713 12713 mntinfo4_t *mi;
12714 12714 OPEN_CONFIRM4args *open_confirm_args;
12715 12715 int needrecov;
12716 12716
12717 12717 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12718 12718 #if DEBUG
12719 12719 mutex_enter(&oop->oo_lock);
12720 12720 ASSERT(oop->oo_seqid_inuse);
12721 12721 mutex_exit(&oop->oo_lock);
12722 12722 #endif
12723 12723
12724 12724 recov_retry_confirm:
12725 12725 nfs4_error_zinit(ep);
12726 12726 *retry_open = FALSE;
12727 12727
12728 12728 if (resend)
12729 12729 args.ctag = TAG_OPEN_CONFIRM_LOST;
12730 12730 else
12731 12731 args.ctag = TAG_OPEN_CONFIRM;
12732 12732
12733 12733 args.array_len = 2;
12734 12734 args.array = argop;
12735 12735
12736 12736 /* putfh target fh */
12737 12737 argop[0].argop = OP_CPUTFH;
12738 12738 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh;
12739 12739
12740 12740 argop[1].argop = OP_OPEN_CONFIRM;
12741 12741 open_confirm_args = &argop[1].nfs_argop4_u.opopen_confirm;
12742 12742
12743 12743 (*seqid) += 1;
12744 12744 open_confirm_args->seqid = *seqid;
12745 12745 open_confirm_args->open_stateid = *stateid;
12746 12746
12747 12747 mi = VTOMI4(vp);
12748 12748
12749 12749 rfs4call(mi, &args, &res, cr, &doqueue, 0, ep);
12750 12750
12751 12751 if (!ep->error && nfs4_need_to_bump_seqid(&res)) {
12752 12752 nfs4_set_open_seqid((*seqid), oop, args.ctag);
12753 12753 }
12754 12754
12755 12755 needrecov = nfs4_needs_recovery(ep, FALSE, mi->mi_vfsp);
12756 12756 if (!needrecov && ep->error)
12757 12757 return;
12758 12758
12759 12759 if (needrecov) {
12760 12760 bool_t abort = FALSE;
12761 12761
12762 12762 if (reopening_file == FALSE) {
12763 12763 nfs4_bseqid_entry_t *bsep = NULL;
12764 12764
12765 12765 if (!ep->error && res.status == NFS4ERR_BAD_SEQID)
12766 12766 bsep = nfs4_create_bseqid_entry(oop, NULL,
12767 12767 vp, 0, args.ctag,
12768 12768 open_confirm_args->seqid);
12769 12769
12770 12770 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL,
12771 12771 NULL, NULL, OP_OPEN_CONFIRM, bsep, NULL, NULL);
12772 12772 if (bsep) {
12773 12773 kmem_free(bsep, sizeof (*bsep));
12774 12774 if (num_bseqid_retryp &&
12775 12775 --(*num_bseqid_retryp) == 0)
12776 12776 abort = TRUE;
12777 12777 }
12778 12778 }
12779 12779 if ((ep->error == ETIMEDOUT ||
12780 12780 res.status == NFS4ERR_RESOURCE) &&
12781 12781 abort == FALSE && resend == FALSE) {
12782 12782 if (!ep->error)
12783 12783 (void) xdr_free(xdr_COMPOUND4res_clnt,
12784 12784 (caddr_t)&res);
12785 12785
12786 12786 delay(SEC_TO_TICK(confirm_retry_sec));
12787 12787 goto recov_retry_confirm;
12788 12788 }
12789 12789 /* State may have changed so retry the entire OPEN op */
12790 12790 if (abort == FALSE)
12791 12791 *retry_open = TRUE;
12792 12792 else
12793 12793 *retry_open = FALSE;
12794 12794 if (!ep->error)
12795 12795 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
12796 12796 return;
12797 12797 }
12798 12798
12799 12799 if (res.status) {
12800 12800 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
12801 12801 return;
12802 12802 }
12803 12803
12804 12804 resop = &res.array[1]; /* open confirm res */
12805 12805 bcopy(&resop->nfs_resop4_u.opopen_confirm.open_stateid,
12806 12806 stateid, sizeof (*stateid));
12807 12807
12808 12808 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
12809 12809 }
12810 12810
12811 12811 /*
12812 12812 * Return the credentials associated with a client state object. The
12813 12813 * caller is responsible for freeing the credentials.
12814 12814 */
12815 12815
12816 12816 static cred_t *
12817 12817 state_to_cred(nfs4_open_stream_t *osp)
12818 12818 {
12819 12819 cred_t *cr;
12820 12820
12821 12821 /*
12822 12822 * It's ok to not lock the open stream and open owner to get
12823 12823 * the oo_cred since this is only written once (upon creation)
12824 12824 * and will not change.
12825 12825 */
12826 12826 cr = osp->os_open_owner->oo_cred;
12827 12827 crhold(cr);
12828 12828
12829 12829 return (cr);
12830 12830 }
12831 12831
12832 12832 /*
12833 12833 * nfs4_find_sysid
12834 12834 *
12835 12835 * Find the sysid for the knetconfig associated with the given mi.
12836 12836 */
12837 12837 static struct lm_sysid *
12838 12838 nfs4_find_sysid(mntinfo4_t *mi)
12839 12839 {
12840 12840 ASSERT(nfs_zone() == mi->mi_zone);
12841 12841
12842 12842 /*
12843 12843 * Switch from RDMA knconf to original mount knconf
12844 12844 */
12845 12845 return (lm_get_sysid(ORIG_KNCONF(mi), &mi->mi_curr_serv->sv_addr,
12846 12846 mi->mi_curr_serv->sv_hostname, NULL));
12847 12847 }
12848 12848
12849 12849 #ifdef DEBUG
12850 12850 /*
12851 12851 * Return a string version of the call type for easy reading.
12852 12852 */
12853 12853 static char *
12854 12854 nfs4frlock_get_call_type(nfs4_lock_call_type_t ctype)
12855 12855 {
12856 12856 switch (ctype) {
12857 12857 case NFS4_LCK_CTYPE_NORM:
12858 12858 return ("NORMAL");
12859 12859 case NFS4_LCK_CTYPE_RECLAIM:
12860 12860 return ("RECLAIM");
12861 12861 case NFS4_LCK_CTYPE_RESEND:
12862 12862 return ("RESEND");
12863 12863 case NFS4_LCK_CTYPE_REINSTATE:
12864 12864 return ("REINSTATE");
12865 12865 default:
12866 12866 cmn_err(CE_PANIC, "nfs4frlock_get_call_type: got illegal "
12867 12867 "type %d", ctype);
12868 12868 return ("");
12869 12869 }
12870 12870 }
12871 12871 #endif
12872 12872
12873 12873 /*
12874 12874 * Map the frlock cmd and lock type to the NFSv4 over-the-wire lock type
12875 12875 * Unlock requests don't have an over-the-wire locktype, so we just return
12876 12876 * something non-threatening.
12877 12877 */
12878 12878
12879 12879 static nfs_lock_type4
12880 12880 flk_to_locktype(int cmd, int l_type)
12881 12881 {
12882 12882 ASSERT(l_type == F_RDLCK || l_type == F_WRLCK || l_type == F_UNLCK);
12883 12883
12884 12884 switch (l_type) {
12885 12885 case F_UNLCK:
12886 12886 return (READ_LT);
12887 12887 case F_RDLCK:
12888 12888 if (cmd == F_SETLK)
12889 12889 return (READ_LT);
12890 12890 else
12891 12891 return (READW_LT);
12892 12892 case F_WRLCK:
12893 12893 if (cmd == F_SETLK)
12894 12894 return (WRITE_LT);
12895 12895 else
12896 12896 return (WRITEW_LT);
12897 12897 }
12898 12898 panic("flk_to_locktype");
12899 12899 /*NOTREACHED*/
12900 12900 }
12901 12901
12902 12902 /*
12903 12903 * Do some preliminary checks for nfs4frlock.
12904 12904 */
12905 12905 static int
12906 12906 nfs4frlock_validate_args(int cmd, flock64_t *flk, int flag, vnode_t *vp,
12907 12907 u_offset_t offset)
12908 12908 {
12909 12909 int error = 0;
12910 12910
12911 12911 /*
12912 12912 * If we are setting a lock, check that the file is opened
12913 12913 * with the correct mode.
12914 12914 */
12915 12915 if (cmd == F_SETLK || cmd == F_SETLKW) {
12916 12916 if ((flk->l_type == F_RDLCK && (flag & FREAD) == 0) ||
12917 12917 (flk->l_type == F_WRLCK && (flag & FWRITE) == 0)) {
12918 12918 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
12919 12919 "nfs4frlock_validate_args: file was opened with "
12920 12920 "incorrect mode"));
12921 12921 return (EBADF);
12922 12922 }
12923 12923 }
12924 12924
12925 12925 /* Convert the offset. It may need to be restored before returning. */
12926 12926 if (error = convoff(vp, flk, 0, offset)) {
12927 12927 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
12928 12928 "nfs4frlock_validate_args: convoff => error= %d\n",
12929 12929 error));
12930 12930 return (error);
12931 12931 }
12932 12932
12933 12933 return (error);
12934 12934 }
12935 12935
12936 12936 /*
12937 12937 * Set the flock64's lm_sysid for nfs4frlock.
12938 12938 */
12939 12939 static int
12940 12940 nfs4frlock_get_sysid(struct lm_sysid **lspp, vnode_t *vp, flock64_t *flk)
12941 12941 {
12942 12942 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12943 12943
12944 12944 /* Find the lm_sysid */
12945 12945 *lspp = nfs4_find_sysid(VTOMI4(vp));
12946 12946
12947 12947 if (*lspp == NULL) {
12948 12948 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
12949 12949 "nfs4frlock_get_sysid: no sysid, return ENOLCK"));
12950 12950 return (ENOLCK);
12951 12951 }
12952 12952
12953 12953 flk->l_sysid = lm_sysidt(*lspp);
12954 12954
12955 12955 return (0);
12956 12956 }
12957 12957
12958 12958 /*
12959 12959 * Do the remaining preliminary setup for nfs4frlock.
12960 12960 */
12961 12961 static void
12962 12962 nfs4frlock_pre_setup(clock_t *tick_delayp, nfs4_recov_state_t *recov_statep,
12963 12963 flock64_t *flk, short *whencep, vnode_t *vp, cred_t *search_cr,
12964 12964 cred_t **cred_otw)
12965 12965 {
12966 12966 /*
12967 12967 * set tick_delay to the base delay time.
12968 12968 * (NFS4_BASE_WAIT_TIME is in secs)
12969 12969 */
12970 12970
12971 12971 *tick_delayp = drv_usectohz(NFS4_BASE_WAIT_TIME * 1000 * 1000);
12972 12972
12973 12973 /*
12974 12974 * If lock is relative to EOF, we need the newest length of the
12975 12975 * file. Therefore invalidate the ATTR_CACHE.
12976 12976 */
12977 12977
12978 12978 *whencep = flk->l_whence;
12979 12979
12980 12980 if (*whencep == 2) /* SEEK_END */
12981 12981 PURGE_ATTRCACHE4(vp);
12982 12982
12983 12983 recov_statep->rs_flags = 0;
12984 12984 recov_statep->rs_num_retry_despite_err = 0;
12985 12985 *cred_otw = nfs4_get_otw_cred(search_cr, VTOMI4(vp), NULL);
12986 12986 }
12987 12987
12988 12988 /*
12989 12989 * Initialize and allocate the data structures necessary for
12990 12990 * the nfs4frlock call.
12991 12991 * Allocates argsp's op array, frees up the saved_rqstpp if there is one.
12992 12992 */
12993 12993 static void
12994 12994 nfs4frlock_call_init(COMPOUND4args_clnt *argsp, COMPOUND4args_clnt **argspp,
12995 12995 nfs_argop4 **argopp, nfs4_op_hint_t *op_hintp, flock64_t *flk, int cmd,
12996 12996 bool_t *retry, bool_t *did_start_fop, COMPOUND4res_clnt **respp,
12997 12997 bool_t *skip_get_err, nfs4_lost_rqst_t *lost_rqstp)
12998 12998 {
12999 12999 int argoplist_size;
13000 13000 int num_ops = 2;
13001 13001
13002 13002 *retry = FALSE;
13003 13003 *did_start_fop = FALSE;
13004 13004 *skip_get_err = FALSE;
13005 13005 lost_rqstp->lr_op = 0;
13006 13006 argoplist_size = num_ops * sizeof (nfs_argop4);
13007 13007 /* fill array with zero */
13008 13008 *argopp = kmem_zalloc(argoplist_size, KM_SLEEP);
13009 13009
13010 13010 *argspp = argsp;
13011 13011 *respp = NULL;
13012 13012
13013 13013 argsp->array_len = num_ops;
13014 13014 argsp->array = *argopp;
13015 13015
13016 13016 /* initialize in case of error; will get real value down below */
13017 13017 argsp->ctag = TAG_NONE;
13018 13018
13019 13019 if ((cmd == F_SETLK || cmd == F_SETLKW) && flk->l_type == F_UNLCK)
13020 13020 *op_hintp = OH_LOCKU;
13021 13021 else
13022 13022 *op_hintp = OH_OTHER;
13023 13023 }
13024 13024
13025 13025 /*
13026 13026 * Call the nfs4_start_fop() for nfs4frlock, if necessary. Assign
13027 13027 * the proper nfs4_server_t for this instance of nfs4frlock.
13028 13028 * Returns 0 (success) or an errno value.
13029 13029 */
13030 13030 static int
13031 13031 nfs4frlock_start_call(nfs4_lock_call_type_t ctype, vnode_t *vp,
13032 13032 nfs4_op_hint_t op_hint, nfs4_recov_state_t *recov_statep,
13033 13033 bool_t *did_start_fop, bool_t *startrecovp)
13034 13034 {
13035 13035 int error = 0;
13036 13036 rnode4_t *rp;
13037 13037
13038 13038 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13039 13039
13040 13040 if (ctype == NFS4_LCK_CTYPE_NORM) {
13041 13041 error = nfs4_start_fop(VTOMI4(vp), vp, NULL, op_hint,
13042 13042 recov_statep, startrecovp);
13043 13043 if (error)
13044 13044 return (error);
13045 13045 *did_start_fop = TRUE;
13046 13046 } else {
13047 13047 *did_start_fop = FALSE;
13048 13048 *startrecovp = FALSE;
13049 13049 }
13050 13050
13051 13051 if (!error) {
13052 13052 rp = VTOR4(vp);
13053 13053
13054 13054 /* If the file failed recovery, just quit. */
13055 13055 mutex_enter(&rp->r_statelock);
13056 13056 if (rp->r_flags & R4RECOVERR) {
13057 13057 error = EIO;
13058 13058 }
13059 13059 mutex_exit(&rp->r_statelock);
13060 13060 }
13061 13061
13062 13062 return (error);
13063 13063 }
13064 13064
13065 13065 /*
13066 13066 * Setup the LOCK4/LOCKU4 arguments for resending a lost lock request. A
13067 13067 * resend nfs4frlock call is initiated by the recovery framework.
13068 13068 * Acquires the lop and oop seqid synchronization.
13069 13069 */
13070 13070 static void
13071 13071 nfs4frlock_setup_resend_lock_args(nfs4_lost_rqst_t *resend_rqstp,
13072 13072 COMPOUND4args_clnt *argsp, nfs_argop4 *argop, nfs4_lock_owner_t **lopp,
13073 13073 nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp,
13074 13074 LOCK4args **lock_argsp, LOCKU4args **locku_argsp)
13075 13075 {
13076 13076 mntinfo4_t *mi = VTOMI4(resend_rqstp->lr_vp);
13077 13077 int error;
13078 13078
13079 13079 NFS4_DEBUG((nfs4_lost_rqst_debug || nfs4_client_lock_debug),
13080 13080 (CE_NOTE,
13081 13081 "nfs4frlock_setup_resend_lock_args: have lost lock to resend"));
13082 13082 ASSERT(resend_rqstp != NULL);
13083 13083 ASSERT(resend_rqstp->lr_op == OP_LOCK ||
13084 13084 resend_rqstp->lr_op == OP_LOCKU);
13085 13085
13086 13086 *oopp = resend_rqstp->lr_oop;
13087 13087 if (resend_rqstp->lr_oop) {
13088 13088 open_owner_hold(resend_rqstp->lr_oop);
13089 13089 error = nfs4_start_open_seqid_sync(resend_rqstp->lr_oop, mi);
13090 13090 ASSERT(error == 0); /* recov thread always succeeds */
13091 13091 }
13092 13092
13093 13093 /* Must resend this lost lock/locku request. */
13094 13094 ASSERT(resend_rqstp->lr_lop != NULL);
13095 13095 *lopp = resend_rqstp->lr_lop;
13096 13096 lock_owner_hold(resend_rqstp->lr_lop);
13097 13097 error = nfs4_start_lock_seqid_sync(resend_rqstp->lr_lop, mi);
13098 13098 ASSERT(error == 0); /* recov thread always succeeds */
13099 13099
13100 13100 *ospp = resend_rqstp->lr_osp;
13101 13101 if (*ospp)
13102 13102 open_stream_hold(resend_rqstp->lr_osp);
13103 13103
13104 13104 if (resend_rqstp->lr_op == OP_LOCK) {
13105 13105 LOCK4args *lock_args;
13106 13106
13107 13107 argop->argop = OP_LOCK;
13108 13108 *lock_argsp = lock_args = &argop->nfs_argop4_u.oplock;
13109 13109 lock_args->locktype = resend_rqstp->lr_locktype;
13110 13110 lock_args->reclaim =
13111 13111 (resend_rqstp->lr_ctype == NFS4_LCK_CTYPE_RECLAIM);
13112 13112 lock_args->offset = resend_rqstp->lr_flk->l_start;
13113 13113 lock_args->length = resend_rqstp->lr_flk->l_len;
13114 13114 if (lock_args->length == 0)
13115 13115 lock_args->length = ~lock_args->length;
13116 13116 nfs4_setup_lock_args(*lopp, *oopp, *ospp,
13117 13117 mi2clientid(mi), &lock_args->locker);
13118 13118
13119 13119 switch (resend_rqstp->lr_ctype) {
13120 13120 case NFS4_LCK_CTYPE_RESEND:
13121 13121 argsp->ctag = TAG_LOCK_RESEND;
13122 13122 break;
13123 13123 case NFS4_LCK_CTYPE_REINSTATE:
13124 13124 argsp->ctag = TAG_LOCK_REINSTATE;
13125 13125 break;
13126 13126 case NFS4_LCK_CTYPE_RECLAIM:
13127 13127 argsp->ctag = TAG_LOCK_RECLAIM;
13128 13128 break;
13129 13129 default:
13130 13130 argsp->ctag = TAG_LOCK_UNKNOWN;
13131 13131 break;
13132 13132 }
13133 13133 } else {
13134 13134 LOCKU4args *locku_args;
13135 13135 nfs4_lock_owner_t *lop = resend_rqstp->lr_lop;
13136 13136
13137 13137 argop->argop = OP_LOCKU;
13138 13138 *locku_argsp = locku_args = &argop->nfs_argop4_u.oplocku;
13139 13139 locku_args->locktype = READ_LT;
13140 13140 locku_args->seqid = lop->lock_seqid + 1;
13141 13141 mutex_enter(&lop->lo_lock);
13142 13142 locku_args->lock_stateid = lop->lock_stateid;
13143 13143 mutex_exit(&lop->lo_lock);
13144 13144 locku_args->offset = resend_rqstp->lr_flk->l_start;
13145 13145 locku_args->length = resend_rqstp->lr_flk->l_len;
13146 13146 if (locku_args->length == 0)
13147 13147 locku_args->length = ~locku_args->length;
13148 13148
13149 13149 switch (resend_rqstp->lr_ctype) {
13150 13150 case NFS4_LCK_CTYPE_RESEND:
13151 13151 argsp->ctag = TAG_LOCKU_RESEND;
13152 13152 break;
13153 13153 case NFS4_LCK_CTYPE_REINSTATE:
13154 13154 argsp->ctag = TAG_LOCKU_REINSTATE;
13155 13155 break;
13156 13156 default:
13157 13157 argsp->ctag = TAG_LOCK_UNKNOWN;
13158 13158 break;
13159 13159 }
13160 13160 }
13161 13161 }
13162 13162
13163 13163 /*
13164 13164 * Setup the LOCKT4 arguments.
13165 13165 */
13166 13166 static void
13167 13167 nfs4frlock_setup_lockt_args(nfs4_lock_call_type_t ctype, nfs_argop4 *argop,
13168 13168 LOCKT4args **lockt_argsp, COMPOUND4args_clnt *argsp, flock64_t *flk,
13169 13169 rnode4_t *rp)
13170 13170 {
13171 13171 LOCKT4args *lockt_args;
13172 13172
13173 13173 ASSERT(nfs_zone() == VTOMI4(RTOV4(rp))->mi_zone);
13174 13174 ASSERT(ctype == NFS4_LCK_CTYPE_NORM);
13175 13175 argop->argop = OP_LOCKT;
13176 13176 argsp->ctag = TAG_LOCKT;
13177 13177 lockt_args = &argop->nfs_argop4_u.oplockt;
13178 13178
13179 13179 /*
13180 13180 * The locktype will be READ_LT unless it's
13181 13181 * a write lock. We do this because the Solaris
13182 13182 * system call allows the combination of
13183 13183 * F_UNLCK and F_GETLK* and so in that case the
13184 13184 * unlock is mapped to a read.
13185 13185 */
13186 13186 if (flk->l_type == F_WRLCK)
13187 13187 lockt_args->locktype = WRITE_LT;
13188 13188 else
13189 13189 lockt_args->locktype = READ_LT;
13190 13190
13191 13191 lockt_args->owner.clientid = mi2clientid(VTOMI4(RTOV4(rp)));
13192 13192 /* set the lock owner4 args */
13193 13193 nfs4_setlockowner_args(&lockt_args->owner, rp,
13194 13194 ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pidp->pid_id :
13195 13195 flk->l_pid);
13196 13196 lockt_args->offset = flk->l_start;
13197 13197 lockt_args->length = flk->l_len;
13198 13198 if (flk->l_len == 0)
13199 13199 lockt_args->length = ~lockt_args->length;
13200 13200
13201 13201 *lockt_argsp = lockt_args;
13202 13202 }
13203 13203
13204 13204 /*
13205 13205 * If the client is holding a delegation, and the open stream to be used
13206 13206 * with this lock request is a delegation open stream, then re-open the stream.
13207 13207 * Sets the nfs4_error_t to all zeros unless the open stream has already
13208 13208 * failed a reopen or we couldn't find the open stream. NFS4ERR_DELAY
13209 13209 * means the caller should retry (like a recovery retry).
13210 13210 */
13211 13211 static void
13212 13212 nfs4frlock_check_deleg(vnode_t *vp, nfs4_error_t *ep, cred_t *cr, int lt)
13213 13213 {
13214 13214 open_delegation_type4 dt;
13215 13215 bool_t reopen_needed, force;
13216 13216 nfs4_open_stream_t *osp;
13217 13217 open_claim_type4 oclaim;
13218 13218 rnode4_t *rp = VTOR4(vp);
13219 13219 mntinfo4_t *mi = VTOMI4(vp);
13220 13220
13221 13221 ASSERT(nfs_zone() == mi->mi_zone);
13222 13222
13223 13223 nfs4_error_zinit(ep);
13224 13224
13225 13225 mutex_enter(&rp->r_statev4_lock);
13226 13226 dt = rp->r_deleg_type;
13227 13227 mutex_exit(&rp->r_statev4_lock);
13228 13228
13229 13229 if (dt != OPEN_DELEGATE_NONE) {
13230 13230 nfs4_open_owner_t *oop;
13231 13231
13232 13232 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi);
13233 13233 if (!oop) {
13234 13234 ep->stat = NFS4ERR_IO;
13235 13235 return;
13236 13236 }
13237 13237 /* returns with 'os_sync_lock' held */
13238 13238 osp = find_open_stream(oop, rp);
13239 13239 if (!osp) {
13240 13240 open_owner_rele(oop);
13241 13241 ep->stat = NFS4ERR_IO;
13242 13242 return;
13243 13243 }
13244 13244
13245 13245 if (osp->os_failed_reopen) {
13246 13246 NFS4_DEBUG((nfs4_open_stream_debug ||
13247 13247 nfs4_client_lock_debug), (CE_NOTE,
13248 13248 "nfs4frlock_check_deleg: os_failed_reopen set "
13249 13249 "for osp %p, cr %p, rp %s", (void *)osp,
13250 13250 (void *)cr, rnode4info(rp)));
13251 13251 mutex_exit(&osp->os_sync_lock);
13252 13252 open_stream_rele(osp, rp);
13253 13253 open_owner_rele(oop);
13254 13254 ep->stat = NFS4ERR_IO;
13255 13255 return;
13256 13256 }
13257 13257
13258 13258 /*
13259 13259 * Determine whether a reopen is needed. If this
13260 13260 * is a delegation open stream, then send the open
13261 13261 * to the server to give visibility to the open owner.
13262 13262 * Even if it isn't a delegation open stream, we need
13263 13263 * to check if the previous open CLAIM_DELEGATE_CUR
13264 13264 * was sufficient.
13265 13265 */
13266 13266
13267 13267 reopen_needed = osp->os_delegation ||
13268 13268 ((lt == F_RDLCK &&
13269 13269 !(osp->os_dc_openacc & OPEN4_SHARE_ACCESS_READ)) ||
13270 13270 (lt == F_WRLCK &&
13271 13271 !(osp->os_dc_openacc & OPEN4_SHARE_ACCESS_WRITE)));
13272 13272
13273 13273 mutex_exit(&osp->os_sync_lock);
13274 13274 open_owner_rele(oop);
13275 13275
13276 13276 if (reopen_needed) {
13277 13277 /*
13278 13278 * Always use CLAIM_PREVIOUS after server reboot.
13279 13279 * The server will reject CLAIM_DELEGATE_CUR if
13280 13280 * it is used during the grace period.
13281 13281 */
13282 13282 mutex_enter(&mi->mi_lock);
13283 13283 if (mi->mi_recovflags & MI4R_SRV_REBOOT) {
13284 13284 oclaim = CLAIM_PREVIOUS;
13285 13285 force = TRUE;
13286 13286 } else {
13287 13287 oclaim = CLAIM_DELEGATE_CUR;
13288 13288 force = FALSE;
13289 13289 }
13290 13290 mutex_exit(&mi->mi_lock);
13291 13291
13292 13292 nfs4_reopen(vp, osp, ep, oclaim, force, FALSE);
13293 13293 if (ep->error == EAGAIN) {
13294 13294 nfs4_error_zinit(ep);
13295 13295 ep->stat = NFS4ERR_DELAY;
13296 13296 }
13297 13297 }
13298 13298 open_stream_rele(osp, rp);
13299 13299 osp = NULL;
13300 13300 }
13301 13301 }
13302 13302
13303 13303 /*
13304 13304 * Setup the LOCKU4 arguments.
13305 13305 * Returns errors via the nfs4_error_t.
13306 13306 * NFS4_OK no problems. *go_otwp is TRUE if call should go
13307 13307 * over-the-wire. The caller must release the
13308 13308 * reference on *lopp.
13309 13309 * NFS4ERR_DELAY caller should retry (like recovery retry)
13310 13310 * (other) unrecoverable error.
13311 13311 */
13312 13312 static void
13313 13313 nfs4frlock_setup_locku_args(nfs4_lock_call_type_t ctype, nfs_argop4 *argop,
13314 13314 LOCKU4args **locku_argsp, flock64_t *flk,
13315 13315 nfs4_lock_owner_t **lopp, nfs4_error_t *ep, COMPOUND4args_clnt *argsp,
13316 13316 vnode_t *vp, int flag, u_offset_t offset, cred_t *cr,
13317 13317 bool_t *skip_get_err, bool_t *go_otwp)
13318 13318 {
13319 13319 nfs4_lock_owner_t *lop = NULL;
13320 13320 LOCKU4args *locku_args;
13321 13321 pid_t pid;
13322 13322 bool_t is_spec = FALSE;
13323 13323 rnode4_t *rp = VTOR4(vp);
13324 13324
13325 13325 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13326 13326 ASSERT(ctype == NFS4_LCK_CTYPE_NORM);
13327 13327
13328 13328 nfs4frlock_check_deleg(vp, ep, cr, F_UNLCK);
13329 13329 if (ep->error || ep->stat)
13330 13330 return;
13331 13331
13332 13332 argop->argop = OP_LOCKU;
13333 13333 if (ctype == NFS4_LCK_CTYPE_REINSTATE)
13334 13334 argsp->ctag = TAG_LOCKU_REINSTATE;
13335 13335 else
13336 13336 argsp->ctag = TAG_LOCKU;
13337 13337 locku_args = &argop->nfs_argop4_u.oplocku;
13338 13338 *locku_argsp = locku_args;
13339 13339
13340 13340 /*
13341 13341 * XXX what should locku_args->locktype be?
13342 13342 * setting to ALWAYS be READ_LT so at least
13343 13343 * it is a valid locktype.
13344 13344 */
13345 13345
13346 13346 locku_args->locktype = READ_LT;
13347 13347
13348 13348 pid = ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pidp->pid_id :
13349 13349 flk->l_pid;
13350 13350
13351 13351 /*
13352 13352 * Get the lock owner stateid. If no lock owner
13353 13353 * exists, return success.
13354 13354 */
13355 13355 lop = find_lock_owner(rp, pid, LOWN_ANY);
13356 13356 *lopp = lop;
13357 13357 if (lop && CLNT_ISSPECIAL(&lop->lock_stateid))
13358 13358 is_spec = TRUE;
13359 13359 if (!lop || is_spec) {
13360 13360 /*
13361 13361 * No lock owner so no locks to unlock.
13362 13362 * Return success. If there was a failed
13363 13363 * reclaim earlier, the lock might still be
13364 13364 * registered with the local locking code,
13365 13365 * so notify it of the unlock.
13366 13366 *
13367 13367 * If the lockowner is using a special stateid,
13368 13368 * then the original lock request (that created
13369 13369 * this lockowner) was never successful, so we
13370 13370 * have no lock to undo OTW.
13371 13371 */
13372 13372 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
13373 13373 "nfs4frlock_setup_locku_args: LOCKU: no lock owner "
13374 13374 "(%ld) so return success", (long)pid));
13375 13375
13376 13376 if (ctype == NFS4_LCK_CTYPE_NORM)
13377 13377 flk->l_pid = curproc->p_pid;
13378 13378 nfs4_register_lock_locally(vp, flk, flag, offset);
13379 13379 /*
13380 13380 * Release our hold and NULL out so final_cleanup
13381 13381 * doesn't try to end a lock seqid sync we
13382 13382 * never started.
13383 13383 */
13384 13384 if (is_spec) {
13385 13385 lock_owner_rele(lop);
13386 13386 *lopp = NULL;
13387 13387 }
13388 13388 *skip_get_err = TRUE;
13389 13389 *go_otwp = FALSE;
13390 13390 return;
13391 13391 }
13392 13392
13393 13393 ep->error = nfs4_start_lock_seqid_sync(lop, VTOMI4(vp));
13394 13394 if (ep->error == EAGAIN) {
13395 13395 lock_owner_rele(lop);
13396 13396 *lopp = NULL;
13397 13397 return;
13398 13398 }
13399 13399
13400 13400 mutex_enter(&lop->lo_lock);
13401 13401 locku_args->lock_stateid = lop->lock_stateid;
13402 13402 mutex_exit(&lop->lo_lock);
13403 13403 locku_args->seqid = lop->lock_seqid + 1;
13404 13404
13405 13405 /* leave the ref count on lop, rele after RPC call */
13406 13406
13407 13407 locku_args->offset = flk->l_start;
13408 13408 locku_args->length = flk->l_len;
13409 13409 if (flk->l_len == 0)
13410 13410 locku_args->length = ~locku_args->length;
13411 13411
13412 13412 *go_otwp = TRUE;
13413 13413 }
13414 13414
13415 13415 /*
13416 13416 * Setup the LOCK4 arguments.
13417 13417 *
13418 13418 * Returns errors via the nfs4_error_t.
13419 13419 * NFS4_OK no problems
13420 13420 * NFS4ERR_DELAY caller should retry (like recovery retry)
13421 13421 * (other) unrecoverable error
13422 13422 */
13423 13423 static void
13424 13424 nfs4frlock_setup_lock_args(nfs4_lock_call_type_t ctype, LOCK4args **lock_argsp,
13425 13425 nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp,
13426 13426 nfs4_lock_owner_t **lopp, nfs_argop4 *argop, COMPOUND4args_clnt *argsp,
13427 13427 flock64_t *flk, int cmd, vnode_t *vp, cred_t *cr, nfs4_error_t *ep)
13428 13428 {
13429 13429 LOCK4args *lock_args;
13430 13430 nfs4_open_owner_t *oop = NULL;
13431 13431 nfs4_open_stream_t *osp = NULL;
13432 13432 nfs4_lock_owner_t *lop = NULL;
13433 13433 pid_t pid;
13434 13434 rnode4_t *rp = VTOR4(vp);
13435 13435
13436 13436 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13437 13437
13438 13438 nfs4frlock_check_deleg(vp, ep, cr, flk->l_type);
13439 13439 if (ep->error || ep->stat != NFS4_OK)
13440 13440 return;
13441 13441
13442 13442 argop->argop = OP_LOCK;
13443 13443 if (ctype == NFS4_LCK_CTYPE_NORM)
13444 13444 argsp->ctag = TAG_LOCK;
13445 13445 else if (ctype == NFS4_LCK_CTYPE_RECLAIM)
13446 13446 argsp->ctag = TAG_RELOCK;
13447 13447 else
13448 13448 argsp->ctag = TAG_LOCK_REINSTATE;
13449 13449 lock_args = &argop->nfs_argop4_u.oplock;
13450 13450 lock_args->locktype = flk_to_locktype(cmd, flk->l_type);
13451 13451 lock_args->reclaim = ctype == NFS4_LCK_CTYPE_RECLAIM ? 1 : 0;
13452 13452 /*
13453 13453 * Get the lock owner. If no lock owner exists,
13454 13454 * create a 'temporary' one and grab the open seqid
13455 13455 * synchronization (which puts a hold on the open
13456 13456 * owner and open stream).
13457 13457 * This also grabs the lock seqid synchronization.
13458 13458 */
13459 13459 pid = ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pid : flk->l_pid;
13460 13460 ep->stat =
13461 13461 nfs4_find_or_create_lock_owner(pid, rp, cr, &oop, &osp, &lop);
13462 13462
13463 13463 if (ep->stat != NFS4_OK)
13464 13464 goto out;
13465 13465
13466 13466 nfs4_setup_lock_args(lop, oop, osp, mi2clientid(VTOMI4(vp)),
13467 13467 &lock_args->locker);
13468 13468
13469 13469 lock_args->offset = flk->l_start;
13470 13470 lock_args->length = flk->l_len;
13471 13471 if (flk->l_len == 0)
13472 13472 lock_args->length = ~lock_args->length;
13473 13473 *lock_argsp = lock_args;
13474 13474 out:
13475 13475 *oopp = oop;
13476 13476 *ospp = osp;
13477 13477 *lopp = lop;
13478 13478 }
13479 13479
13480 13480 /*
13481 13481 * After we get the reply from the server, record the proper information
13482 13482 * for possible resend lock requests.
13483 13483 *
13484 13484 * Allocates memory for the saved_rqstp if we have a lost lock to save.
13485 13485 */
13486 13486 static void
13487 13487 nfs4frlock_save_lost_rqst(nfs4_lock_call_type_t ctype, int error,
13488 13488 nfs_lock_type4 locktype, nfs4_open_owner_t *oop,
13489 13489 nfs4_open_stream_t *osp, nfs4_lock_owner_t *lop, flock64_t *flk,
13490 13490 nfs4_lost_rqst_t *lost_rqstp, cred_t *cr, vnode_t *vp)
13491 13491 {
13492 13492 bool_t unlock = (flk->l_type == F_UNLCK);
13493 13493
13494 13494 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13495 13495 ASSERT(ctype == NFS4_LCK_CTYPE_NORM ||
13496 13496 ctype == NFS4_LCK_CTYPE_REINSTATE);
13497 13497
13498 13498 if (error != 0 && !unlock) {
13499 13499 NFS4_DEBUG((nfs4_lost_rqst_debug ||
13500 13500 nfs4_client_lock_debug), (CE_NOTE,
13501 13501 "nfs4frlock_save_lost_rqst: set lo_pending_rqsts to 1 "
13502 13502 " for lop %p", (void *)lop));
13503 13503 ASSERT(lop != NULL);
13504 13504 mutex_enter(&lop->lo_lock);
13505 13505 lop->lo_pending_rqsts = 1;
13506 13506 mutex_exit(&lop->lo_lock);
13507 13507 }
13508 13508
13509 13509 lost_rqstp->lr_putfirst = FALSE;
13510 13510 lost_rqstp->lr_op = 0;
13511 13511
13512 13512 /*
13513 13513 * For lock/locku requests, we treat EINTR as ETIMEDOUT for
13514 13514 * recovery purposes so that the lock request that was sent
13515 13515 * can be saved and re-issued later. Ditto for EIO from a forced
13516 13516 * unmount. This is done to have the client's local locking state
13517 13517 * match the v4 server's state; that is, the request was
13518 13518 * potentially received and accepted by the server but the client
13519 13519 * thinks it was not.
13520 13520 */
13521 13521 if (error == ETIMEDOUT || error == EINTR ||
13522 13522 NFS4_FRC_UNMT_ERR(error, vp->v_vfsp)) {
13523 13523 NFS4_DEBUG((nfs4_lost_rqst_debug ||
13524 13524 nfs4_client_lock_debug), (CE_NOTE,
13525 13525 "nfs4frlock_save_lost_rqst: got a lost %s lock for "
13526 13526 "lop %p oop %p osp %p", unlock ? "LOCKU" : "LOCK",
13527 13527 (void *)lop, (void *)oop, (void *)osp));
13528 13528 if (unlock)
13529 13529 lost_rqstp->lr_op = OP_LOCKU;
13530 13530 else {
13531 13531 lost_rqstp->lr_op = OP_LOCK;
13532 13532 lost_rqstp->lr_locktype = locktype;
13533 13533 }
13534 13534 /*
13535 13535 * Objects are held and rele'd via the recovery code.
13536 13536 * See nfs4_save_lost_rqst.
13537 13537 */
13538 13538 lost_rqstp->lr_vp = vp;
13539 13539 lost_rqstp->lr_dvp = NULL;
13540 13540 lost_rqstp->lr_oop = oop;
13541 13541 lost_rqstp->lr_osp = osp;
13542 13542 lost_rqstp->lr_lop = lop;
13543 13543 lost_rqstp->lr_cr = cr;
13544 13544 switch (ctype) {
13545 13545 case NFS4_LCK_CTYPE_NORM:
13546 13546 flk->l_pid = ttoproc(curthread)->p_pid;
13547 13547 lost_rqstp->lr_ctype = NFS4_LCK_CTYPE_RESEND;
13548 13548 break;
13549 13549 case NFS4_LCK_CTYPE_REINSTATE:
13550 13550 lost_rqstp->lr_putfirst = TRUE;
13551 13551 lost_rqstp->lr_ctype = ctype;
13552 13552 break;
13553 13553 default:
13554 13554 break;
13555 13555 }
13556 13556 lost_rqstp->lr_flk = flk;
13557 13557 }
13558 13558 }
13559 13559
13560 13560 /*
13561 13561 * Update lop's seqid. Also update the seqid stored in a resend request,
13562 13562 * if any. (Some recovery errors increment the seqid, and we may have to
13563 13563 * send the resend request again.)
13564 13564 */
13565 13565
13566 13566 static void
13567 13567 nfs4frlock_bump_seqid(LOCK4args *lock_args, LOCKU4args *locku_args,
13568 13568 nfs4_open_owner_t *oop, nfs4_lock_owner_t *lop, nfs4_tag_type_t tag_type)
13569 13569 {
13570 13570 if (lock_args) {
13571 13571 if (lock_args->locker.new_lock_owner == TRUE)
13572 13572 nfs4_get_and_set_next_open_seqid(oop, tag_type);
13573 13573 else {
13574 13574 ASSERT(lop->lo_flags & NFS4_LOCK_SEQID_INUSE);
13575 13575 nfs4_set_lock_seqid(lop->lock_seqid + 1, lop);
13576 13576 }
13577 13577 } else if (locku_args) {
13578 13578 ASSERT(lop->lo_flags & NFS4_LOCK_SEQID_INUSE);
13579 13579 nfs4_set_lock_seqid(lop->lock_seqid +1, lop);
13580 13580 }
13581 13581 }
13582 13582
13583 13583 /*
13584 13584 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the
13585 13585 * COMPOUND4 args/res for calls that need to retry.
13586 13586 * Switches the *cred_otwp to base_cr.
13587 13587 */
13588 13588 static void
13589 13589 nfs4frlock_check_access(vnode_t *vp, nfs4_op_hint_t op_hint,
13590 13590 nfs4_recov_state_t *recov_statep, int needrecov, bool_t *did_start_fop,
13591 13591 COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp, int error,
13592 13592 nfs4_lock_owner_t **lopp, nfs4_open_owner_t **oopp,
13593 13593 nfs4_open_stream_t **ospp, cred_t *base_cr, cred_t **cred_otwp)
13594 13594 {
13595 13595 nfs4_open_owner_t *oop = *oopp;
13596 13596 nfs4_open_stream_t *osp = *ospp;
13597 13597 nfs4_lock_owner_t *lop = *lopp;
13598 13598 nfs_argop4 *argop = (*argspp)->array;
13599 13599
13600 13600 if (*did_start_fop) {
13601 13601 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, recov_statep,
13602 13602 needrecov);
13603 13603 *did_start_fop = FALSE;
13604 13604 }
13605 13605 ASSERT((*argspp)->array_len == 2);
13606 13606 if (argop[1].argop == OP_LOCK)
13607 13607 nfs4args_lock_free(&argop[1]);
13608 13608 else if (argop[1].argop == OP_LOCKT)
13609 13609 nfs4args_lockt_free(&argop[1]);
13610 13610 kmem_free(argop, 2 * sizeof (nfs_argop4));
13611 13611 if (!error)
13612 13612 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)*respp);
13613 13613 *argspp = NULL;
13614 13614 *respp = NULL;
13615 13615
13616 13616 if (lop) {
13617 13617 nfs4_end_lock_seqid_sync(lop);
13618 13618 lock_owner_rele(lop);
13619 13619 *lopp = NULL;
13620 13620 }
13621 13621
13622 13622 /* need to free up the reference on osp for lock args */
13623 13623 if (osp != NULL) {
13624 13624 open_stream_rele(osp, VTOR4(vp));
13625 13625 *ospp = NULL;
13626 13626 }
13627 13627
13628 13628 /* need to free up the reference on oop for lock args */
13629 13629 if (oop != NULL) {
13630 13630 nfs4_end_open_seqid_sync(oop);
13631 13631 open_owner_rele(oop);
13632 13632 *oopp = NULL;
13633 13633 }
13634 13634
13635 13635 crfree(*cred_otwp);
13636 13636 *cred_otwp = base_cr;
13637 13637 crhold(*cred_otwp);
13638 13638 }
13639 13639
13640 13640 /*
13641 13641 * Function to process the client's recovery for nfs4frlock.
13642 13642 * Returns TRUE if we should retry the lock request; FALSE otherwise.
13643 13643 *
13644 13644 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the
13645 13645 * COMPOUND4 args/res for calls that need to retry.
13646 13646 *
13647 13647 * Note: the rp's r_lkserlock is *not* dropped during this path.
13648 13648 */
13649 13649 static bool_t
13650 13650 nfs4frlock_recovery(int needrecov, nfs4_error_t *ep,
13651 13651 COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp,
13652 13652 LOCK4args *lock_args, LOCKU4args *locku_args,
13653 13653 nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp,
13654 13654 nfs4_lock_owner_t **lopp, rnode4_t *rp, vnode_t *vp,
13655 13655 nfs4_recov_state_t *recov_statep, nfs4_op_hint_t op_hint,
13656 13656 bool_t *did_start_fop, nfs4_lost_rqst_t *lost_rqstp, flock64_t *flk)
13657 13657 {
13658 13658 nfs4_open_owner_t *oop = *oopp;
13659 13659 nfs4_open_stream_t *osp = *ospp;
13660 13660 nfs4_lock_owner_t *lop = *lopp;
13661 13661
13662 13662 bool_t abort, retry;
13663 13663
13664 13664 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13665 13665 ASSERT((*argspp) != NULL);
13666 13666 ASSERT((*respp) != NULL);
13667 13667 if (lock_args || locku_args)
13668 13668 ASSERT(lop != NULL);
13669 13669
13670 13670 NFS4_DEBUG((nfs4_client_lock_debug || nfs4_client_recov_debug),
13671 13671 (CE_NOTE, "nfs4frlock_recovery: initiating recovery\n"));
13672 13672
13673 13673 retry = TRUE;
13674 13674 abort = FALSE;
13675 13675 if (needrecov) {
13676 13676 nfs4_bseqid_entry_t *bsep = NULL;
13677 13677 nfs_opnum4 op;
13678 13678
13679 13679 op = lock_args ? OP_LOCK : locku_args ? OP_LOCKU : OP_LOCKT;
13680 13680
13681 13681 if (!ep->error && ep->stat == NFS4ERR_BAD_SEQID) {
13682 13682 seqid4 seqid;
13683 13683
13684 13684 if (lock_args) {
13685 13685 if (lock_args->locker.new_lock_owner == TRUE)
13686 13686 seqid = lock_args->locker.locker4_u.
13687 13687 open_owner.open_seqid;
13688 13688 else
13689 13689 seqid = lock_args->locker.locker4_u.
13690 13690 lock_owner.lock_seqid;
13691 13691 } else if (locku_args) {
13692 13692 seqid = locku_args->seqid;
13693 13693 } else {
13694 13694 seqid = 0;
13695 13695 }
13696 13696
13697 13697 bsep = nfs4_create_bseqid_entry(oop, lop, vp,
13698 13698 flk->l_pid, (*argspp)->ctag, seqid);
13699 13699 }
13700 13700
13701 13701 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL, NULL,
13702 13702 (lost_rqstp && (lost_rqstp->lr_op == OP_LOCK ||
13703 13703 lost_rqstp->lr_op == OP_LOCKU)) ? lost_rqstp :
13704 13704 NULL, op, bsep, NULL, NULL);
13705 13705
13706 13706 if (bsep)
13707 13707 kmem_free(bsep, sizeof (*bsep));
13708 13708 }
13709 13709
13710 13710 /*
13711 13711 * Return that we do not want to retry the request for 3 cases:
13712 13712 * 1. If we received EINTR or are bailing out because of a forced
13713 13713 * unmount, we came into this code path just for the sake of
13714 13714 * initiating recovery, we now need to return the error.
13715 13715 * 2. If we have aborted recovery.
13716 13716 * 3. We received NFS4ERR_BAD_SEQID.
13717 13717 */
13718 13718 if (ep->error == EINTR || NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp) ||
13719 13719 abort == TRUE || (ep->error == 0 && ep->stat == NFS4ERR_BAD_SEQID))
13720 13720 retry = FALSE;
13721 13721
13722 13722 if (*did_start_fop == TRUE) {
13723 13723 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, recov_statep,
13724 13724 needrecov);
13725 13725 *did_start_fop = FALSE;
13726 13726 }
13727 13727
13728 13728 if (retry == TRUE) {
13729 13729 nfs_argop4 *argop;
13730 13730
13731 13731 argop = (*argspp)->array;
13732 13732 ASSERT((*argspp)->array_len == 2);
13733 13733
13734 13734 if (argop[1].argop == OP_LOCK)
13735 13735 nfs4args_lock_free(&argop[1]);
13736 13736 else if (argop[1].argop == OP_LOCKT)
13737 13737 nfs4args_lockt_free(&argop[1]);
13738 13738 kmem_free(argop, 2 * sizeof (nfs_argop4));
13739 13739 if (!ep->error)
13740 13740 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)*respp);
13741 13741 *respp = NULL;
13742 13742 *argspp = NULL;
13743 13743 }
13744 13744
13745 13745 if (lop != NULL) {
13746 13746 nfs4_end_lock_seqid_sync(lop);
13747 13747 lock_owner_rele(lop);
13748 13748 }
13749 13749
13750 13750 *lopp = NULL;
13751 13751
13752 13752 /* need to free up the reference on osp for lock args */
13753 13753 if (osp != NULL) {
13754 13754 open_stream_rele(osp, rp);
13755 13755 *ospp = NULL;
13756 13756 }
13757 13757
13758 13758 /* need to free up the reference on oop for lock args */
13759 13759 if (oop != NULL) {
13760 13760 nfs4_end_open_seqid_sync(oop);
13761 13761 open_owner_rele(oop);
13762 13762 *oopp = NULL;
13763 13763 }
13764 13764
13765 13765 return (retry);
13766 13766 }
13767 13767
13768 13768 /*
13769 13769 * Handles the successful reply from the server for nfs4frlock.
13770 13770 */
13771 13771 static void
13772 13772 nfs4frlock_results_ok(nfs4_lock_call_type_t ctype, int cmd, flock64_t *flk,
13773 13773 vnode_t *vp, int flag, u_offset_t offset,
13774 13774 nfs4_lost_rqst_t *resend_rqstp)
13775 13775 {
13776 13776 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13777 13777 if ((cmd == F_SETLK || cmd == F_SETLKW) &&
13778 13778 (flk->l_type == F_RDLCK || flk->l_type == F_WRLCK)) {
13779 13779 if (ctype == NFS4_LCK_CTYPE_NORM) {
13780 13780 flk->l_pid = ttoproc(curthread)->p_pid;
13781 13781 /*
13782 13782 * We do not register lost locks locally in
13783 13783 * the 'resend' case since the user/application
13784 13784 * doesn't think we have the lock.
13785 13785 */
13786 13786 ASSERT(!resend_rqstp);
13787 13787 nfs4_register_lock_locally(vp, flk, flag, offset);
13788 13788 }
13789 13789 }
13790 13790 }
13791 13791
13792 13792 /*
13793 13793 * Handle the DENIED reply from the server for nfs4frlock.
13794 13794 * Returns TRUE if we should retry the request; FALSE otherwise.
13795 13795 *
13796 13796 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the
13797 13797 * COMPOUND4 args/res for calls that need to retry. Can also
13798 13798 * drop and regrab the r_lkserlock.
13799 13799 */
13800 13800 static bool_t
13801 13801 nfs4frlock_results_denied(nfs4_lock_call_type_t ctype, LOCK4args *lock_args,
13802 13802 LOCKT4args *lockt_args, nfs4_open_owner_t **oopp,
13803 13803 nfs4_open_stream_t **ospp, nfs4_lock_owner_t **lopp, int cmd,
13804 13804 vnode_t *vp, flock64_t *flk, nfs4_op_hint_t op_hint,
13805 13805 nfs4_recov_state_t *recov_statep, int needrecov,
13806 13806 COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp,
13807 13807 clock_t *tick_delayp, short *whencep, int *errorp,
13808 13808 nfs_resop4 *resop, cred_t *cr, bool_t *did_start_fop,
13809 13809 bool_t *skip_get_err)
13810 13810 {
13811 13811 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13812 13812
13813 13813 if (lock_args) {
13814 13814 nfs4_open_owner_t *oop = *oopp;
13815 13815 nfs4_open_stream_t *osp = *ospp;
13816 13816 nfs4_lock_owner_t *lop = *lopp;
13817 13817 int intr;
13818 13818
13819 13819 /*
13820 13820 * Blocking lock needs to sleep and retry from the request.
13821 13821 *
13822 13822 * Do not block and wait for 'resend' or 'reinstate'
13823 13823 * lock requests, just return the error.
13824 13824 *
13825 13825 * Note: reclaim requests have cmd == F_SETLK, not F_SETLKW.
13826 13826 */
13827 13827 if (cmd == F_SETLKW) {
13828 13828 rnode4_t *rp = VTOR4(vp);
13829 13829 nfs_argop4 *argop = (*argspp)->array;
13830 13830
13831 13831 ASSERT(ctype == NFS4_LCK_CTYPE_NORM);
13832 13832
13833 13833 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint,
13834 13834 recov_statep, needrecov);
13835 13835 *did_start_fop = FALSE;
13836 13836 ASSERT((*argspp)->array_len == 2);
13837 13837 if (argop[1].argop == OP_LOCK)
13838 13838 nfs4args_lock_free(&argop[1]);
13839 13839 else if (argop[1].argop == OP_LOCKT)
13840 13840 nfs4args_lockt_free(&argop[1]);
13841 13841 kmem_free(argop, 2 * sizeof (nfs_argop4));
13842 13842 if (*respp)
13843 13843 (void) xdr_free(xdr_COMPOUND4res_clnt,
13844 13844 (caddr_t)*respp);
13845 13845 *argspp = NULL;
13846 13846 *respp = NULL;
13847 13847 nfs4_end_lock_seqid_sync(lop);
13848 13848 lock_owner_rele(lop);
13849 13849 *lopp = NULL;
13850 13850 if (osp != NULL) {
13851 13851 open_stream_rele(osp, rp);
13852 13852 *ospp = NULL;
13853 13853 }
13854 13854 if (oop != NULL) {
13855 13855 nfs4_end_open_seqid_sync(oop);
13856 13856 open_owner_rele(oop);
13857 13857 *oopp = NULL;
13858 13858 }
13859 13859
13860 13860 nfs_rw_exit(&rp->r_lkserlock);
13861 13861
13862 13862 intr = nfs4_block_and_wait(tick_delayp, rp);
13863 13863
13864 13864 if (intr) {
13865 13865 (void) nfs_rw_enter_sig(&rp->r_lkserlock,
13866 13866 RW_WRITER, FALSE);
13867 13867 *errorp = EINTR;
13868 13868 return (FALSE);
13869 13869 }
13870 13870
13871 13871 (void) nfs_rw_enter_sig(&rp->r_lkserlock,
13872 13872 RW_WRITER, FALSE);
13873 13873
13874 13874 /*
13875 13875 * Make sure we are still safe to lock with
13876 13876 * regards to mmapping.
13877 13877 */
13878 13878 if (!nfs4_safelock(vp, flk, cr)) {
13879 13879 *errorp = EAGAIN;
13880 13880 return (FALSE);
13881 13881 }
13882 13882
13883 13883 return (TRUE);
13884 13884 }
13885 13885 if (ctype == NFS4_LCK_CTYPE_NORM)
13886 13886 *errorp = EAGAIN;
13887 13887 *skip_get_err = TRUE;
13888 13888 flk->l_whence = 0;
13889 13889 *whencep = 0;
13890 13890 return (FALSE);
13891 13891 } else if (lockt_args) {
13892 13892 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
13893 13893 "nfs4frlock_results_denied: OP_LOCKT DENIED"));
13894 13894
13895 13895 denied_to_flk(&resop->nfs_resop4_u.oplockt.denied,
13896 13896 flk, lockt_args);
13897 13897
13898 13898 /* according to NLM code */
13899 13899 *errorp = 0;
13900 13900 *whencep = 0;
13901 13901 *skip_get_err = TRUE;
13902 13902 return (FALSE);
13903 13903 }
13904 13904 return (FALSE);
13905 13905 }
13906 13906
13907 13907 /*
13908 13908 * Handles all NFS4 errors besides NFS4_OK and NFS4ERR_DENIED for nfs4frlock.
13909 13909 */
13910 13910 static void
13911 13911 nfs4frlock_results_default(COMPOUND4res_clnt *resp, int *errorp)
13912 13912 {
13913 13913 switch (resp->status) {
13914 13914 case NFS4ERR_ACCESS:
13915 13915 case NFS4ERR_ADMIN_REVOKED:
13916 13916 case NFS4ERR_BADHANDLE:
13917 13917 case NFS4ERR_BAD_RANGE:
13918 13918 case NFS4ERR_BAD_SEQID:
13919 13919 case NFS4ERR_BAD_STATEID:
13920 13920 case NFS4ERR_BADXDR:
13921 13921 case NFS4ERR_DEADLOCK:
13922 13922 case NFS4ERR_DELAY:
13923 13923 case NFS4ERR_EXPIRED:
13924 13924 case NFS4ERR_FHEXPIRED:
13925 13925 case NFS4ERR_GRACE:
13926 13926 case NFS4ERR_INVAL:
13927 13927 case NFS4ERR_ISDIR:
13928 13928 case NFS4ERR_LEASE_MOVED:
13929 13929 case NFS4ERR_LOCK_NOTSUPP:
13930 13930 case NFS4ERR_LOCK_RANGE:
13931 13931 case NFS4ERR_MOVED:
13932 13932 case NFS4ERR_NOFILEHANDLE:
13933 13933 case NFS4ERR_NO_GRACE:
13934 13934 case NFS4ERR_OLD_STATEID:
13935 13935 case NFS4ERR_OPENMODE:
13936 13936 case NFS4ERR_RECLAIM_BAD:
13937 13937 case NFS4ERR_RECLAIM_CONFLICT:
13938 13938 case NFS4ERR_RESOURCE:
13939 13939 case NFS4ERR_SERVERFAULT:
13940 13940 case NFS4ERR_STALE:
13941 13941 case NFS4ERR_STALE_CLIENTID:
13942 13942 case NFS4ERR_STALE_STATEID:
13943 13943 return;
13944 13944 default:
13945 13945 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
13946 13946 "nfs4frlock_results_default: got unrecognizable "
13947 13947 "res.status %d", resp->status));
13948 13948 *errorp = NFS4ERR_INVAL;
13949 13949 }
13950 13950 }
13951 13951
13952 13952 /*
13953 13953 * The lock request was successful, so update the client's state.
13954 13954 */
13955 13955 static void
13956 13956 nfs4frlock_update_state(LOCK4args *lock_args, LOCKU4args *locku_args,
13957 13957 LOCKT4args *lockt_args, nfs_resop4 *resop, nfs4_lock_owner_t *lop,
13958 13958 vnode_t *vp, flock64_t *flk, cred_t *cr,
13959 13959 nfs4_lost_rqst_t *resend_rqstp)
13960 13960 {
13961 13961 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13962 13962
13963 13963 if (lock_args) {
13964 13964 LOCK4res *lock_res;
13965 13965
13966 13966 lock_res = &resop->nfs_resop4_u.oplock;
13967 13967 /* update the stateid with server's response */
13968 13968
13969 13969 if (lock_args->locker.new_lock_owner == TRUE) {
13970 13970 mutex_enter(&lop->lo_lock);
13971 13971 lop->lo_just_created = NFS4_PERM_CREATED;
13972 13972 mutex_exit(&lop->lo_lock);
13973 13973 }
13974 13974
13975 13975 nfs4_set_lock_stateid(lop, lock_res->LOCK4res_u.lock_stateid);
13976 13976
13977 13977 /*
13978 13978 * If the lock was the result of a resending a lost
13979 13979 * request, we've synched up the stateid and seqid
13980 13980 * with the server, but now the server might be out of sync
13981 13981 * with what the application thinks it has for locks.
13982 13982 * Clean that up here. It's unclear whether we should do
13983 13983 * this even if the filesystem has been forcibly unmounted.
13984 13984 * For most servers, it's probably wasted effort, but
13985 13985 * RFC3530 lets servers require that unlocks exactly match
13986 13986 * the locks that are held.
13987 13987 */
13988 13988 if (resend_rqstp != NULL &&
13989 13989 resend_rqstp->lr_ctype != NFS4_LCK_CTYPE_REINSTATE) {
13990 13990 nfs4_reinstitute_local_lock_state(vp, flk, cr, lop);
13991 13991 } else {
13992 13992 flk->l_whence = 0;
13993 13993 }
13994 13994 } else if (locku_args) {
13995 13995 LOCKU4res *locku_res;
13996 13996
13997 13997 locku_res = &resop->nfs_resop4_u.oplocku;
13998 13998
13999 13999 /* Update the stateid with the server's response */
14000 14000 nfs4_set_lock_stateid(lop, locku_res->lock_stateid);
14001 14001 } else if (lockt_args) {
14002 14002 /* Switch the lock type to express success, see fcntl */
14003 14003 flk->l_type = F_UNLCK;
14004 14004 flk->l_whence = 0;
14005 14005 }
14006 14006 }
14007 14007
14008 14008 /*
14009 14009 * Do final cleanup before exiting nfs4frlock.
14010 14010 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the
14011 14011 * COMPOUND4 args/res for calls that haven't already.
14012 14012 */
14013 14013 static void
14014 14014 nfs4frlock_final_cleanup(nfs4_lock_call_type_t ctype, COMPOUND4args_clnt *argsp,
14015 14015 COMPOUND4res_clnt *resp, vnode_t *vp, nfs4_op_hint_t op_hint,
14016 14016 nfs4_recov_state_t *recov_statep, int needrecov, nfs4_open_owner_t *oop,
14017 14017 nfs4_open_stream_t *osp, nfs4_lock_owner_t *lop, flock64_t *flk,
14018 14018 short whence, u_offset_t offset, struct lm_sysid *ls,
14019 14019 int *errorp, LOCK4args *lock_args, LOCKU4args *locku_args,
14020 14020 bool_t did_start_fop, bool_t skip_get_err,
14021 14021 cred_t *cred_otw, cred_t *cred)
14022 14022 {
14023 14023 mntinfo4_t *mi = VTOMI4(vp);
14024 14024 rnode4_t *rp = VTOR4(vp);
14025 14025 int error = *errorp;
14026 14026 nfs_argop4 *argop;
14027 14027 int do_flush_pages = 0;
14028 14028
14029 14029 ASSERT(nfs_zone() == mi->mi_zone);
14030 14030 /*
14031 14031 * The client recovery code wants the raw status information,
14032 14032 * so don't map the NFS status code to an errno value for
14033 14033 * non-normal call types.
14034 14034 */
14035 14035 if (ctype == NFS4_LCK_CTYPE_NORM) {
14036 14036 if (*errorp == 0 && resp != NULL && skip_get_err == FALSE)
14037 14037 *errorp = geterrno4(resp->status);
14038 14038 if (did_start_fop == TRUE)
14039 14039 nfs4_end_fop(mi, vp, NULL, op_hint, recov_statep,
14040 14040 needrecov);
14041 14041
14042 14042 /*
14043 14043 * We've established a new lock on the server, so invalidate
14044 14044 * the pages associated with the vnode to get the most up to
14045 14045 * date pages from the server after acquiring the lock. We
14046 14046 * want to be sure that the read operation gets the newest data.
14047 14047 * N.B.
14048 14048 * We used to do this in nfs4frlock_results_ok but that doesn't
14049 14049 * work since VOP_PUTPAGE can call nfs4_commit which calls
14050 14050 * nfs4_start_fop. We flush the pages below after calling
14051 14051 * nfs4_end_fop above
14052 14052 * The flush of the page cache must be done after
14053 14053 * nfs4_end_open_seqid_sync() to avoid a 4-way hang.
14054 14054 */
14055 14055 if (!error && resp && resp->status == NFS4_OK)
14056 14056 do_flush_pages = 1;
14057 14057 }
14058 14058 if (argsp) {
14059 14059 ASSERT(argsp->array_len == 2);
14060 14060 argop = argsp->array;
14061 14061 if (argop[1].argop == OP_LOCK)
14062 14062 nfs4args_lock_free(&argop[1]);
14063 14063 else if (argop[1].argop == OP_LOCKT)
14064 14064 nfs4args_lockt_free(&argop[1]);
14065 14065 kmem_free(argop, 2 * sizeof (nfs_argop4));
14066 14066 if (resp)
14067 14067 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
14068 14068 }
14069 14069
14070 14070 /* free the reference on the lock owner */
14071 14071 if (lop != NULL) {
14072 14072 nfs4_end_lock_seqid_sync(lop);
14073 14073 lock_owner_rele(lop);
14074 14074 }
14075 14075
14076 14076 /* need to free up the reference on osp for lock args */
14077 14077 if (osp != NULL)
14078 14078 open_stream_rele(osp, rp);
14079 14079
14080 14080 /* need to free up the reference on oop for lock args */
14081 14081 if (oop != NULL) {
14082 14082 nfs4_end_open_seqid_sync(oop);
14083 14083 open_owner_rele(oop);
14084 14084 }
14085 14085
14086 14086 if (do_flush_pages)
14087 14087 nfs4_flush_pages(vp, cred);
14088 14088
14089 14089 (void) convoff(vp, flk, whence, offset);
14090 14090
14091 14091 lm_rel_sysid(ls);
14092 14092
14093 14093 /*
14094 14094 * Record debug information in the event we get EINVAL.
14095 14095 */
14096 14096 mutex_enter(&mi->mi_lock);
14097 14097 if (*errorp == EINVAL && (lock_args || locku_args) &&
14098 14098 (!(mi->mi_flags & MI4_POSIX_LOCK))) {
14099 14099 if (!(mi->mi_flags & MI4_LOCK_DEBUG)) {
14100 14100 zcmn_err(getzoneid(), CE_NOTE,
14101 14101 "%s operation failed with "
14102 14102 "EINVAL probably since the server, %s,"
14103 14103 " doesn't support POSIX style locking",
14104 14104 lock_args ? "LOCK" : "LOCKU",
14105 14105 mi->mi_curr_serv->sv_hostname);
14106 14106 mi->mi_flags |= MI4_LOCK_DEBUG;
14107 14107 }
14108 14108 }
14109 14109 mutex_exit(&mi->mi_lock);
14110 14110
14111 14111 if (cred_otw)
14112 14112 crfree(cred_otw);
14113 14113 }
14114 14114
14115 14115 /*
14116 14116 * This calls the server and the local locking code.
14117 14117 *
14118 14118 * Client locks are registerred locally by oring the sysid with
14119 14119 * LM_SYSID_CLIENT. The server registers locks locally using just the sysid.
14120 14120 * We need to distinguish between the two to avoid collision in case one
14121 14121 * machine is used as both client and server.
14122 14122 *
14123 14123 * Blocking lock requests will continually retry to acquire the lock
14124 14124 * forever.
14125 14125 *
14126 14126 * The ctype is defined as follows:
14127 14127 * NFS4_LCK_CTYPE_NORM: normal lock request.
14128 14128 *
14129 14129 * NFS4_LCK_CTYPE_RECLAIM: bypass the usual calls for synchronizing with client
14130 14130 * recovery, get the pid from flk instead of curproc, and don't reregister
14131 14131 * the lock locally.
14132 14132 *
14133 14133 * NFS4_LCK_CTYPE_RESEND: same as NFS4_LCK_CTYPE_RECLAIM, with the addition
14134 14134 * that we will use the information passed in via resend_rqstp to setup the
14135 14135 * lock/locku request. This resend is the exact same request as the 'lost
14136 14136 * lock', and is initiated by the recovery framework. A successful resend
14137 14137 * request can initiate one or more reinstate requests.
14138 14138 *
14139 14139 * NFS4_LCK_CTYPE_REINSTATE: same as NFS4_LCK_CTYPE_RESEND, except that it
14140 14140 * does not trigger additional reinstate requests. This lock call type is
14141 14141 * set for setting the v4 server's locking state back to match what the
14142 14142 * client's local locking state is in the event of a received 'lost lock'.
14143 14143 *
14144 14144 * Errors are returned via the nfs4_error_t parameter.
14145 14145 */
14146 14146 void
14147 14147 nfs4frlock(nfs4_lock_call_type_t ctype, vnode_t *vp, int cmd, flock64_t *flk,
14148 14148 int flag, u_offset_t offset, cred_t *cr, nfs4_error_t *ep,
14149 14149 nfs4_lost_rqst_t *resend_rqstp, int *did_reclaimp)
14150 14150 {
14151 14151 COMPOUND4args_clnt args, *argsp = NULL;
14152 14152 COMPOUND4res_clnt res, *resp = NULL;
14153 14153 nfs_argop4 *argop;
14154 14154 nfs_resop4 *resop;
14155 14155 rnode4_t *rp;
14156 14156 int doqueue = 1;
14157 14157 clock_t tick_delay; /* delay in clock ticks */
14158 14158 struct lm_sysid *ls;
14159 14159 LOCK4args *lock_args = NULL;
14160 14160 LOCKU4args *locku_args = NULL;
14161 14161 LOCKT4args *lockt_args = NULL;
14162 14162 nfs4_open_owner_t *oop = NULL;
14163 14163 nfs4_open_stream_t *osp = NULL;
14164 14164 nfs4_lock_owner_t *lop = NULL;
14165 14165 bool_t needrecov = FALSE;
14166 14166 nfs4_recov_state_t recov_state;
14167 14167 short whence;
14168 14168 nfs4_op_hint_t op_hint;
14169 14169 nfs4_lost_rqst_t lost_rqst;
14170 14170 bool_t retry = FALSE;
14171 14171 bool_t did_start_fop = FALSE;
14172 14172 bool_t skip_get_err = FALSE;
14173 14173 cred_t *cred_otw = NULL;
14174 14174 bool_t recovonly; /* just queue request */
14175 14175 int frc_no_reclaim = 0;
14176 14176 #ifdef DEBUG
14177 14177 char *name;
14178 14178 #endif
14179 14179
14180 14180 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
14181 14181
14182 14182 #ifdef DEBUG
14183 14183 name = fn_name(VTOSV(vp)->sv_name);
14184 14184 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4frlock: "
14185 14185 "%s: cmd %d, type %d, offset %llu, start %"PRIx64", "
14186 14186 "length %"PRIu64", pid %d, sysid %d, call type %s, "
14187 14187 "resend request %s", name, cmd, flk->l_type, offset, flk->l_start,
14188 14188 flk->l_len, ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pid :
14189 14189 flk->l_pid, flk->l_sysid, nfs4frlock_get_call_type(ctype),
14190 14190 resend_rqstp ? "TRUE" : "FALSE"));
14191 14191 kmem_free(name, MAXNAMELEN);
14192 14192 #endif
14193 14193
14194 14194 nfs4_error_zinit(ep);
14195 14195 ep->error = nfs4frlock_validate_args(cmd, flk, flag, vp, offset);
14196 14196 if (ep->error)
14197 14197 return;
14198 14198 ep->error = nfs4frlock_get_sysid(&ls, vp, flk);
14199 14199 if (ep->error)
14200 14200 return;
14201 14201 nfs4frlock_pre_setup(&tick_delay, &recov_state, flk, &whence,
14202 14202 vp, cr, &cred_otw);
14203 14203
14204 14204 recov_retry:
14205 14205 nfs4frlock_call_init(&args, &argsp, &argop, &op_hint, flk, cmd,
14206 14206 &retry, &did_start_fop, &resp, &skip_get_err, &lost_rqst);
14207 14207 rp = VTOR4(vp);
14208 14208
14209 14209 ep->error = nfs4frlock_start_call(ctype, vp, op_hint, &recov_state,
14210 14210 &did_start_fop, &recovonly);
14211 14211
14212 14212 if (ep->error)
14213 14213 goto out;
14214 14214
14215 14215 if (recovonly) {
14216 14216 /*
14217 14217 * Leave the request for the recovery system to deal with.
14218 14218 */
14219 14219 ASSERT(ctype == NFS4_LCK_CTYPE_NORM);
14220 14220 ASSERT(cmd != F_GETLK);
14221 14221 ASSERT(flk->l_type == F_UNLCK);
14222 14222
14223 14223 nfs4_error_init(ep, EINTR);
14224 14224 needrecov = TRUE;
14225 14225 lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY);
14226 14226 if (lop != NULL) {
14227 14227 nfs4frlock_save_lost_rqst(ctype, ep->error, READ_LT,
14228 14228 NULL, NULL, lop, flk, &lost_rqst, cr, vp);
14229 14229 (void) nfs4_start_recovery(ep,
14230 14230 VTOMI4(vp), vp, NULL, NULL,
14231 14231 (lost_rqst.lr_op == OP_LOCK ||
14232 14232 lost_rqst.lr_op == OP_LOCKU) ?
14233 14233 &lost_rqst : NULL, OP_LOCKU, NULL, NULL, NULL);
14234 14234 lock_owner_rele(lop);
14235 14235 lop = NULL;
14236 14236 }
14237 14237 flk->l_pid = curproc->p_pid;
14238 14238 nfs4_register_lock_locally(vp, flk, flag, offset);
14239 14239 goto out;
14240 14240 }
14241 14241
14242 14242 /* putfh directory fh */
14243 14243 argop[0].argop = OP_CPUTFH;
14244 14244 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
14245 14245
14246 14246 /*
14247 14247 * Set up the over-the-wire arguments and get references to the
14248 14248 * open owner, etc.
14249 14249 */
14250 14250
14251 14251 if (ctype == NFS4_LCK_CTYPE_RESEND ||
14252 14252 ctype == NFS4_LCK_CTYPE_REINSTATE) {
14253 14253 nfs4frlock_setup_resend_lock_args(resend_rqstp, argsp,
14254 14254 &argop[1], &lop, &oop, &osp, &lock_args, &locku_args);
14255 14255 } else {
14256 14256 bool_t go_otw = TRUE;
14257 14257
14258 14258 ASSERT(resend_rqstp == NULL);
14259 14259
14260 14260 switch (cmd) {
14261 14261 case F_GETLK:
14262 14262 case F_O_GETLK:
14263 14263 nfs4frlock_setup_lockt_args(ctype, &argop[1],
14264 14264 &lockt_args, argsp, flk, rp);
14265 14265 break;
14266 14266 case F_SETLKW:
14267 14267 case F_SETLK:
14268 14268 if (flk->l_type == F_UNLCK)
14269 14269 nfs4frlock_setup_locku_args(ctype,
14270 14270 &argop[1], &locku_args, flk,
14271 14271 &lop, ep, argsp,
14272 14272 vp, flag, offset, cr,
14273 14273 &skip_get_err, &go_otw);
14274 14274 else
14275 14275 nfs4frlock_setup_lock_args(ctype,
14276 14276 &lock_args, &oop, &osp, &lop, &argop[1],
14277 14277 argsp, flk, cmd, vp, cr, ep);
14278 14278
14279 14279 if (ep->error)
14280 14280 goto out;
14281 14281
14282 14282 switch (ep->stat) {
14283 14283 case NFS4_OK:
14284 14284 break;
14285 14285 case NFS4ERR_DELAY:
14286 14286 /* recov thread never gets this error */
14287 14287 ASSERT(resend_rqstp == NULL);
14288 14288 ASSERT(did_start_fop);
14289 14289
14290 14290 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint,
14291 14291 &recov_state, TRUE);
14292 14292 did_start_fop = FALSE;
14293 14293 if (argop[1].argop == OP_LOCK)
14294 14294 nfs4args_lock_free(&argop[1]);
14295 14295 else if (argop[1].argop == OP_LOCKT)
14296 14296 nfs4args_lockt_free(&argop[1]);
14297 14297 kmem_free(argop, 2 * sizeof (nfs_argop4));
14298 14298 argsp = NULL;
14299 14299 goto recov_retry;
14300 14300 default:
14301 14301 ep->error = EIO;
14302 14302 goto out;
14303 14303 }
14304 14304 break;
14305 14305 default:
14306 14306 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14307 14307 "nfs4_frlock: invalid cmd %d", cmd));
14308 14308 ep->error = EINVAL;
14309 14309 goto out;
14310 14310 }
14311 14311
14312 14312 if (!go_otw)
14313 14313 goto out;
14314 14314 }
14315 14315
14316 14316 /* XXX should we use the local reclock as a cache ? */
14317 14317 /*
14318 14318 * Unregister the lock with the local locking code before
14319 14319 * contacting the server. This avoids a potential race where
14320 14320 * another process gets notified that it has been granted a lock
14321 14321 * before we can unregister ourselves locally.
14322 14322 */
14323 14323 if ((cmd == F_SETLK || cmd == F_SETLKW) && flk->l_type == F_UNLCK) {
14324 14324 if (ctype == NFS4_LCK_CTYPE_NORM)
14325 14325 flk->l_pid = ttoproc(curthread)->p_pid;
14326 14326 nfs4_register_lock_locally(vp, flk, flag, offset);
14327 14327 }
14328 14328
14329 14329 /*
14330 14330 * Send the server the lock request. Continually loop with a delay
14331 14331 * if get error NFS4ERR_DENIED (for blocking locks) or NFS4ERR_GRACE.
14332 14332 */
14333 14333 resp = &res;
14334 14334
14335 14335 NFS4_DEBUG((nfs4_client_call_debug || nfs4_client_lock_debug),
14336 14336 (CE_NOTE,
14337 14337 "nfs4frlock: %s call, rp %s", needrecov ? "recov" : "first",
14338 14338 rnode4info(rp)));
14339 14339
14340 14340 if (lock_args && frc_no_reclaim) {
14341 14341 ASSERT(ctype == NFS4_LCK_CTYPE_RECLAIM);
14342 14342 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14343 14343 "nfs4frlock: frc_no_reclaim: clearing reclaim"));
14344 14344 lock_args->reclaim = FALSE;
14345 14345 if (did_reclaimp)
14346 14346 *did_reclaimp = 0;
14347 14347 }
14348 14348
14349 14349 /*
14350 14350 * Do the OTW call.
14351 14351 */
14352 14352 rfs4call(VTOMI4(vp), argsp, resp, cred_otw, &doqueue, 0, ep);
14353 14353
14354 14354 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14355 14355 "nfs4frlock: error %d, status %d", ep->error, resp->status));
14356 14356
14357 14357 needrecov = nfs4_needs_recovery(ep, TRUE, vp->v_vfsp);
14358 14358 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14359 14359 "nfs4frlock: needrecov %d", needrecov));
14360 14360
14361 14361 if (ep->error == 0 && nfs4_need_to_bump_seqid(resp))
14362 14362 nfs4frlock_bump_seqid(lock_args, locku_args, oop, lop,
14363 14363 args.ctag);
14364 14364
14365 14365 /*
14366 14366 * Check if one of these mutually exclusive error cases has
14367 14367 * happened:
14368 14368 * need to swap credentials due to access error
14369 14369 * recovery is needed
14370 14370 * different error (only known case is missing Kerberos ticket)
14371 14371 */
14372 14372
14373 14373 if ((ep->error == EACCES ||
14374 14374 (ep->error == 0 && resp->status == NFS4ERR_ACCESS)) &&
14375 14375 cred_otw != cr) {
14376 14376 nfs4frlock_check_access(vp, op_hint, &recov_state, needrecov,
14377 14377 &did_start_fop, &argsp, &resp, ep->error, &lop, &oop, &osp,
14378 14378 cr, &cred_otw);
14379 14379 goto recov_retry;
14380 14380 }
14381 14381
14382 14382 if (needrecov) {
14383 14383 /*
14384 14384 * LOCKT requests don't need to recover from lost
14385 14385 * requests since they don't create/modify state.
14386 14386 */
14387 14387 if ((ep->error == EINTR ||
14388 14388 NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp)) &&
14389 14389 lockt_args)
14390 14390 goto out;
14391 14391 /*
14392 14392 * Do not attempt recovery for requests initiated by
14393 14393 * the recovery framework. Let the framework redrive them.
14394 14394 */
14395 14395 if (ctype != NFS4_LCK_CTYPE_NORM)
14396 14396 goto out;
14397 14397 else {
14398 14398 ASSERT(resend_rqstp == NULL);
14399 14399 }
14400 14400
14401 14401 nfs4frlock_save_lost_rqst(ctype, ep->error,
14402 14402 flk_to_locktype(cmd, flk->l_type),
14403 14403 oop, osp, lop, flk, &lost_rqst, cred_otw, vp);
14404 14404
14405 14405 retry = nfs4frlock_recovery(needrecov, ep, &argsp,
14406 14406 &resp, lock_args, locku_args, &oop, &osp, &lop,
14407 14407 rp, vp, &recov_state, op_hint, &did_start_fop,
14408 14408 cmd != F_GETLK ? &lost_rqst : NULL, flk);
14409 14409
14410 14410 if (retry) {
14411 14411 ASSERT(oop == NULL);
14412 14412 ASSERT(osp == NULL);
14413 14413 ASSERT(lop == NULL);
14414 14414 goto recov_retry;
14415 14415 }
14416 14416 goto out;
14417 14417 }
14418 14418
14419 14419 /*
14420 14420 * Bail out if have reached this point with ep->error set. Can
14421 14421 * happen if (ep->error == EACCES && !needrecov && cred_otw == cr).
14422 14422 * This happens if Kerberos ticket has expired or has been
14423 14423 * destroyed.
14424 14424 */
14425 14425 if (ep->error != 0)
14426 14426 goto out;
14427 14427
14428 14428 /*
14429 14429 * Process the reply.
14430 14430 */
14431 14431 switch (resp->status) {
14432 14432 case NFS4_OK:
14433 14433 resop = &resp->array[1];
14434 14434 nfs4frlock_results_ok(ctype, cmd, flk, vp, flag, offset,
14435 14435 resend_rqstp);
14436 14436 /*
14437 14437 * Have a successful lock operation, now update state.
14438 14438 */
14439 14439 nfs4frlock_update_state(lock_args, locku_args, lockt_args,
14440 14440 resop, lop, vp, flk, cr, resend_rqstp);
14441 14441 break;
14442 14442
14443 14443 case NFS4ERR_DENIED:
14444 14444 resop = &resp->array[1];
14445 14445 retry = nfs4frlock_results_denied(ctype, lock_args, lockt_args,
14446 14446 &oop, &osp, &lop, cmd, vp, flk, op_hint,
14447 14447 &recov_state, needrecov, &argsp, &resp,
14448 14448 &tick_delay, &whence, &ep->error, resop, cr,
14449 14449 &did_start_fop, &skip_get_err);
14450 14450
14451 14451 if (retry) {
14452 14452 ASSERT(oop == NULL);
14453 14453 ASSERT(osp == NULL);
14454 14454 ASSERT(lop == NULL);
14455 14455 goto recov_retry;
14456 14456 }
14457 14457 break;
14458 14458 /*
14459 14459 * If the server won't let us reclaim, fall-back to trying to lock
14460 14460 * the file from scratch. Code elsewhere will check the changeinfo
14461 14461 * to ensure the file hasn't been changed.
14462 14462 */
14463 14463 case NFS4ERR_NO_GRACE:
14464 14464 if (lock_args && lock_args->reclaim == TRUE) {
14465 14465 ASSERT(ctype == NFS4_LCK_CTYPE_RECLAIM);
14466 14466 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14467 14467 "nfs4frlock: reclaim: NFS4ERR_NO_GRACE"));
14468 14468 frc_no_reclaim = 1;
14469 14469 /* clean up before retrying */
14470 14470 needrecov = 0;
14471 14471 (void) nfs4frlock_recovery(needrecov, ep, &argsp, &resp,
14472 14472 lock_args, locku_args, &oop, &osp, &lop, rp, vp,
14473 14473 &recov_state, op_hint, &did_start_fop, NULL, flk);
14474 14474 goto recov_retry;
14475 14475 }
14476 14476 /* FALLTHROUGH */
14477 14477
14478 14478 default:
14479 14479 nfs4frlock_results_default(resp, &ep->error);
14480 14480 break;
14481 14481 }
14482 14482 out:
14483 14483 /*
14484 14484 * Process and cleanup from error. Make interrupted unlock
14485 14485 * requests look successful, since they will be handled by the
14486 14486 * client recovery code.
14487 14487 */
14488 14488 nfs4frlock_final_cleanup(ctype, argsp, resp, vp, op_hint, &recov_state,
14489 14489 needrecov, oop, osp, lop, flk, whence, offset, ls, &ep->error,
14490 14490 lock_args, locku_args, did_start_fop,
14491 14491 skip_get_err, cred_otw, cr);
14492 14492
14493 14493 if (ep->error == EINTR && flk->l_type == F_UNLCK &&
14494 14494 (cmd == F_SETLK || cmd == F_SETLKW))
14495 14495 ep->error = 0;
14496 14496 }
14497 14497
14498 14498 /*
14499 14499 * nfs4_safelock:
14500 14500 *
14501 14501 * Return non-zero if the given lock request can be handled without
14502 14502 * violating the constraints on concurrent mapping and locking.
14503 14503 */
14504 14504
14505 14505 static int
14506 14506 nfs4_safelock(vnode_t *vp, const struct flock64 *bfp, cred_t *cr)
14507 14507 {
14508 14508 rnode4_t *rp = VTOR4(vp);
14509 14509 struct vattr va;
14510 14510 int error;
14511 14511
14512 14512 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
14513 14513 ASSERT(rp->r_mapcnt >= 0);
14514 14514 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock %s: "
14515 14515 "(%"PRIx64", %"PRIx64"); mapcnt = %ld", bfp->l_type == F_WRLCK ?
14516 14516 "write" : bfp->l_type == F_RDLCK ? "read" : "unlock",
14517 14517 bfp->l_start, bfp->l_len, rp->r_mapcnt));
14518 14518
14519 14519 if (rp->r_mapcnt == 0)
14520 14520 return (1); /* always safe if not mapped */
14521 14521
14522 14522 /*
14523 14523 * If the file is already mapped and there are locks, then they
14524 14524 * should be all safe locks. So adding or removing a lock is safe
14525 14525 * as long as the new request is safe (i.e., whole-file, meaning
14526 14526 * length and starting offset are both zero).
14527 14527 */
14528 14528
14529 14529 if (bfp->l_start != 0 || bfp->l_len != 0) {
14530 14530 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: "
14531 14531 "cannot lock a memory mapped file unless locking the "
14532 14532 "entire file: start %"PRIx64", len %"PRIx64,
14533 14533 bfp->l_start, bfp->l_len));
14534 14534 return (0);
14535 14535 }
14536 14536
14537 14537 /* mandatory locking and mapping don't mix */
14538 14538 va.va_mask = AT_MODE;
14539 14539 error = VOP_GETATTR(vp, &va, 0, cr, NULL);
14540 14540 if (error != 0) {
14541 14541 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: "
14542 14542 "getattr error %d", error));
14543 14543 return (0); /* treat errors conservatively */
14544 14544 }
14545 14545 if (MANDLOCK(vp, va.va_mode)) {
14546 14546 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: "
14547 14547 "cannot mandatory lock and mmap a file"));
14548 14548 return (0);
14549 14549 }
14550 14550
14551 14551 return (1);
14552 14552 }
14553 14553
14554 14554
14555 14555 /*
14556 14556 * Register the lock locally within Solaris.
14557 14557 * As the client, we "or" the sysid with LM_SYSID_CLIENT when
14558 14558 * recording locks locally.
14559 14559 *
14560 14560 * This should handle conflicts/cooperation with NFS v2/v3 since all locks
14561 14561 * are registered locally.
14562 14562 */
14563 14563 void
14564 14564 nfs4_register_lock_locally(vnode_t *vp, struct flock64 *flk, int flag,
14565 14565 u_offset_t offset)
14566 14566 {
14567 14567 int oldsysid;
14568 14568 int error;
14569 14569 #ifdef DEBUG
14570 14570 char *name;
14571 14571 #endif
14572 14572
14573 14573 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
14574 14574
14575 14575 #ifdef DEBUG
14576 14576 name = fn_name(VTOSV(vp)->sv_name);
14577 14577 NFS4_DEBUG(nfs4_client_lock_debug,
14578 14578 (CE_NOTE, "nfs4_register_lock_locally: %s: type %d, "
14579 14579 "start %"PRIx64", length %"PRIx64", pid %ld, sysid %d",
14580 14580 name, flk->l_type, flk->l_start, flk->l_len, (long)flk->l_pid,
14581 14581 flk->l_sysid));
14582 14582 kmem_free(name, MAXNAMELEN);
14583 14583 #endif
14584 14584
14585 14585 /* register the lock with local locking */
14586 14586 oldsysid = flk->l_sysid;
14587 14587 flk->l_sysid |= LM_SYSID_CLIENT;
14588 14588 error = reclock(vp, flk, SETFLCK, flag, offset, NULL);
14589 14589 #ifdef DEBUG
14590 14590 if (error != 0) {
14591 14591 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14592 14592 "nfs4_register_lock_locally: could not register with"
14593 14593 " local locking"));
14594 14594 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT,
14595 14595 "error %d, vp 0x%p, pid %d, sysid 0x%x",
14596 14596 error, (void *)vp, flk->l_pid, flk->l_sysid));
14597 14597 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT,
14598 14598 "type %d off 0x%" PRIx64 " len 0x%" PRIx64,
14599 14599 flk->l_type, flk->l_start, flk->l_len));
14600 14600 (void) reclock(vp, flk, 0, flag, offset, NULL);
14601 14601 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT,
14602 14602 "blocked by pid %d sysid 0x%x type %d "
14603 14603 "off 0x%" PRIx64 " len 0x%" PRIx64,
14604 14604 flk->l_pid, flk->l_sysid, flk->l_type, flk->l_start,
14605 14605 flk->l_len));
14606 14606 }
14607 14607 #endif
14608 14608 flk->l_sysid = oldsysid;
14609 14609 }
14610 14610
14611 14611 /*
14612 14612 * nfs4_lockrelease:
14613 14613 *
14614 14614 * Release any locks on the given vnode that are held by the current
14615 14615 * process. Also removes the lock owner (if one exists) from the rnode's
14616 14616 * list.
14617 14617 */
14618 14618 static int
14619 14619 nfs4_lockrelease(vnode_t *vp, int flag, offset_t offset, cred_t *cr)
14620 14620 {
14621 14621 flock64_t ld;
14622 14622 int ret, error;
14623 14623 rnode4_t *rp;
14624 14624 nfs4_lock_owner_t *lop;
14625 14625 nfs4_recov_state_t recov_state;
14626 14626 mntinfo4_t *mi;
14627 14627 bool_t possible_orphan = FALSE;
14628 14628 bool_t recovonly;
14629 14629
14630 14630 ASSERT((uintptr_t)vp > KERNELBASE);
14631 14631 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
14632 14632
14633 14633 rp = VTOR4(vp);
14634 14634 mi = VTOMI4(vp);
14635 14635
14636 14636 /*
14637 14637 * If we have not locked anything then we can
14638 14638 * just return since we have no work to do.
14639 14639 */
14640 14640 if (rp->r_lo_head.lo_next_rnode == &rp->r_lo_head) {
14641 14641 return (0);
14642 14642 }
14643 14643
14644 14644 /*
14645 14645 * We need to comprehend that another thread may
14646 14646 * kick off recovery and the lock_owner we have stashed
14647 14647 * in lop might be invalid so we should NOT cache it
14648 14648 * locally!
14649 14649 */
14650 14650 recov_state.rs_flags = 0;
14651 14651 recov_state.rs_num_retry_despite_err = 0;
14652 14652 error = nfs4_start_fop(mi, vp, NULL, OH_LOCKU, &recov_state,
14653 14653 &recovonly);
14654 14654 if (error) {
14655 14655 mutex_enter(&rp->r_statelock);
14656 14656 rp->r_flags |= R4LODANGLERS;
14657 14657 mutex_exit(&rp->r_statelock);
14658 14658 return (error);
14659 14659 }
14660 14660
14661 14661 lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY);
14662 14662
14663 14663 /*
14664 14664 * Check if the lock owner might have a lock (request was sent but
14665 14665 * no response was received). Also check if there are any remote
14666 14666 * locks on the file. (In theory we shouldn't have to make this
14667 14667 * second check if there's no lock owner, but for now we'll be
14668 14668 * conservative and do it anyway.) If either condition is true,
14669 14669 * send an unlock for the entire file to the server.
14670 14670 *
14671 14671 * Note that no explicit synchronization is needed here. At worst,
14672 14672 * flk_has_remote_locks() will return a false positive, in which case
14673 14673 * the unlock call wastes time but doesn't harm correctness.
14674 14674 */
14675 14675
14676 14676 if (lop) {
14677 14677 mutex_enter(&lop->lo_lock);
14678 14678 possible_orphan = lop->lo_pending_rqsts;
14679 14679 mutex_exit(&lop->lo_lock);
14680 14680 lock_owner_rele(lop);
14681 14681 }
14682 14682
14683 14683 nfs4_end_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 0);
14684 14684
14685 14685 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14686 14686 "nfs4_lockrelease: possible orphan %d, remote locks %d, for "
14687 14687 "lop %p.", possible_orphan, flk_has_remote_locks(vp),
14688 14688 (void *)lop));
14689 14689
14690 14690 if (possible_orphan || flk_has_remote_locks(vp)) {
14691 14691 ld.l_type = F_UNLCK; /* set to unlock entire file */
14692 14692 ld.l_whence = 0; /* unlock from start of file */
14693 14693 ld.l_start = 0;
14694 14694 ld.l_len = 0; /* do entire file */
14695 14695
14696 14696 ret = VOP_FRLOCK(vp, F_SETLK, &ld, flag, offset, NULL,
14697 14697 cr, NULL);
14698 14698
14699 14699 if (ret != 0) {
14700 14700 /*
14701 14701 * If VOP_FRLOCK fails, make sure we unregister
14702 14702 * local locks before we continue.
14703 14703 */
14704 14704 ld.l_pid = ttoproc(curthread)->p_pid;
14705 14705 nfs4_register_lock_locally(vp, &ld, flag, offset);
14706 14706 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14707 14707 "nfs4_lockrelease: lock release error on vp"
14708 14708 " %p: error %d.\n", (void *)vp, ret));
14709 14709 }
14710 14710 }
14711 14711
14712 14712 recov_state.rs_flags = 0;
14713 14713 recov_state.rs_num_retry_despite_err = 0;
14714 14714 error = nfs4_start_fop(mi, vp, NULL, OH_LOCKU, &recov_state,
14715 14715 &recovonly);
14716 14716 if (error) {
14717 14717 mutex_enter(&rp->r_statelock);
14718 14718 rp->r_flags |= R4LODANGLERS;
14719 14719 mutex_exit(&rp->r_statelock);
14720 14720 return (error);
14721 14721 }
14722 14722
14723 14723 /*
14724 14724 * So, here we're going to need to retrieve the lock-owner
14725 14725 * again (in case recovery has done a switch-a-roo) and
14726 14726 * remove it because we can.
14727 14727 */
14728 14728 lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY);
14729 14729
14730 14730 if (lop) {
14731 14731 nfs4_rnode_remove_lock_owner(rp, lop);
14732 14732 lock_owner_rele(lop);
14733 14733 }
14734 14734
14735 14735 nfs4_end_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 0);
14736 14736 return (0);
14737 14737 }
14738 14738
14739 14739 /*
14740 14740 * Wait for 'tick_delay' clock ticks.
14741 14741 * Implement exponential backoff until hit the lease_time of this nfs4_server.
14742 14742 * NOTE: lock_lease_time is in seconds.
14743 14743 *
14744 14744 * XXX For future improvements, should implement a waiting queue scheme.
14745 14745 */
14746 14746 static int
14747 14747 nfs4_block_and_wait(clock_t *tick_delay, rnode4_t *rp)
14748 14748 {
14749 14749 long milliseconds_delay;
14750 14750 time_t lock_lease_time;
14751 14751
14752 14752 /* wait tick_delay clock ticks or siginteruptus */
14753 14753 if (delay_sig(*tick_delay)) {
14754 14754 return (EINTR);
14755 14755 }
14756 14756 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_block_and_wait: "
14757 14757 "reissue the lock request: blocked for %ld clock ticks: %ld "
14758 14758 "milliseconds", *tick_delay, drv_hztousec(*tick_delay) / 1000));
14759 14759
14760 14760 /* get the lease time */
14761 14761 lock_lease_time = r2lease_time(rp);
14762 14762
14763 14763 /* drv_hztousec converts ticks to microseconds */
14764 14764 milliseconds_delay = drv_hztousec(*tick_delay) / 1000;
14765 14765 if (milliseconds_delay < lock_lease_time * 1000) {
14766 14766 *tick_delay = 2 * *tick_delay;
14767 14767 if (drv_hztousec(*tick_delay) > lock_lease_time * 1000 * 1000)
14768 14768 *tick_delay = drv_usectohz(lock_lease_time*1000*1000);
14769 14769 }
14770 14770 return (0);
14771 14771 }
14772 14772
14773 14773
14774 14774 void
14775 14775 nfs4_vnops_init(void)
14776 14776 {
14777 14777 }
14778 14778
14779 14779 void
14780 14780 nfs4_vnops_fini(void)
14781 14781 {
14782 14782 }
14783 14783
14784 14784 /*
14785 14785 * Return a reference to the directory (parent) vnode for a given vnode,
14786 14786 * using the saved pathname information and the directory file handle. The
14787 14787 * caller is responsible for disposing of the reference.
14788 14788 * Returns zero or an errno value.
14789 14789 *
14790 14790 * Caller should set need_start_op to FALSE if it is the recovery
14791 14791 * thread, or if a start_fop has already been done. Otherwise, TRUE.
14792 14792 */
14793 14793 int
14794 14794 vtodv(vnode_t *vp, vnode_t **dvpp, cred_t *cr, bool_t need_start_op)
14795 14795 {
14796 14796 svnode_t *svnp;
14797 14797 vnode_t *dvp = NULL;
14798 14798 servinfo4_t *svp;
14799 14799 nfs4_fname_t *mfname;
14800 14800 int error;
14801 14801
14802 14802 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
14803 14803
14804 14804 if (vp->v_flag & VROOT) {
14805 14805 nfs4_sharedfh_t *sfh;
14806 14806 nfs_fh4 fh;
14807 14807 mntinfo4_t *mi;
14808 14808
14809 14809 ASSERT(vp->v_type == VREG);
14810 14810
14811 14811 mi = VTOMI4(vp);
14812 14812 svp = mi->mi_curr_serv;
14813 14813 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
14814 14814 fh.nfs_fh4_len = svp->sv_pfhandle.fh_len;
14815 14815 fh.nfs_fh4_val = svp->sv_pfhandle.fh_buf;
14816 14816 sfh = sfh4_get(&fh, VTOMI4(vp));
14817 14817 nfs_rw_exit(&svp->sv_lock);
14818 14818 mfname = mi->mi_fname;
14819 14819 fn_hold(mfname);
14820 14820 dvp = makenfs4node_by_fh(sfh, NULL, &mfname, NULL, mi, cr, 0);
14821 14821 sfh4_rele(&sfh);
14822 14822
14823 14823 if (dvp->v_type == VNON)
14824 14824 dvp->v_type = VDIR;
14825 14825 *dvpp = dvp;
14826 14826 return (0);
14827 14827 }
14828 14828
14829 14829 svnp = VTOSV(vp);
14830 14830
14831 14831 if (svnp == NULL) {
14832 14832 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: "
14833 14833 "shadow node is NULL"));
14834 14834 return (EINVAL);
14835 14835 }
14836 14836
14837 14837 if (svnp->sv_name == NULL || svnp->sv_dfh == NULL) {
14838 14838 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: "
14839 14839 "shadow node name or dfh val == NULL"));
14840 14840 return (EINVAL);
14841 14841 }
14842 14842
14843 14843 error = nfs4_make_dotdot(svnp->sv_dfh, 0, vp, cr, &dvp,
14844 14844 (int)need_start_op);
14845 14845 if (error != 0) {
14846 14846 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: "
14847 14847 "nfs4_make_dotdot returned %d", error));
14848 14848 return (error);
14849 14849 }
14850 14850 if (!dvp) {
14851 14851 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: "
14852 14852 "nfs4_make_dotdot returned a NULL dvp"));
14853 14853 return (EIO);
14854 14854 }
14855 14855 if (dvp->v_type == VNON)
14856 14856 dvp->v_type = VDIR;
14857 14857 ASSERT(dvp->v_type == VDIR);
14858 14858 if (VTOR4(vp)->r_flags & R4ISXATTR) {
14859 14859 mutex_enter(&dvp->v_lock);
14860 14860 dvp->v_flag |= V_XATTRDIR;
14861 14861 mutex_exit(&dvp->v_lock);
14862 14862 }
14863 14863 *dvpp = dvp;
14864 14864 return (0);
14865 14865 }
14866 14866
14867 14867 /*
14868 14868 * Copy the (final) component name of vp to fnamep. maxlen is the maximum
14869 14869 * length that fnamep can accept, including the trailing null.
14870 14870 * Returns 0 if okay, returns an errno value if there was a problem.
14871 14871 */
14872 14872
14873 14873 int
14874 14874 vtoname(vnode_t *vp, char *fnamep, ssize_t maxlen)
14875 14875 {
14876 14876 char *fn;
14877 14877 int err = 0;
14878 14878 servinfo4_t *svp;
14879 14879 svnode_t *shvp;
14880 14880
14881 14881 /*
14882 14882 * If the file being opened has VROOT set, then this is
14883 14883 * a "file" mount. sv_name will not be interesting, so
14884 14884 * go back to the servinfo4 to get the original mount
14885 14885 * path and strip off all but the final edge. Otherwise
14886 14886 * just return the name from the shadow vnode.
14887 14887 */
14888 14888
14889 14889 if (vp->v_flag & VROOT) {
14890 14890
14891 14891 svp = VTOMI4(vp)->mi_curr_serv;
14892 14892 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
14893 14893
14894 14894 fn = strrchr(svp->sv_path, '/');
14895 14895 if (fn == NULL)
14896 14896 err = EINVAL;
14897 14897 else
14898 14898 fn++;
14899 14899 } else {
14900 14900 shvp = VTOSV(vp);
14901 14901 fn = fn_name(shvp->sv_name);
14902 14902 }
14903 14903
14904 14904 if (err == 0)
14905 14905 if (strlen(fn) < maxlen)
14906 14906 (void) strcpy(fnamep, fn);
14907 14907 else
14908 14908 err = ENAMETOOLONG;
14909 14909
14910 14910 if (vp->v_flag & VROOT)
14911 14911 nfs_rw_exit(&svp->sv_lock);
14912 14912 else
14913 14913 kmem_free(fn, MAXNAMELEN);
14914 14914
14915 14915 return (err);
14916 14916 }
14917 14917
14918 14918 /*
14919 14919 * Bookkeeping for a close that doesn't need to go over the wire.
14920 14920 * *have_lockp is set to 0 if 'os_sync_lock' is released; otherwise
14921 14921 * it is left at 1.
14922 14922 */
14923 14923 void
14924 14924 nfs4close_notw(vnode_t *vp, nfs4_open_stream_t *osp, int *have_lockp)
14925 14925 {
14926 14926 rnode4_t *rp;
14927 14927 mntinfo4_t *mi;
14928 14928
14929 14929 mi = VTOMI4(vp);
14930 14930 rp = VTOR4(vp);
14931 14931
14932 14932 NFS4_DEBUG(nfs4close_notw_debug, (CE_NOTE, "nfs4close_notw: "
14933 14933 "rp=%p osp=%p", (void *)rp, (void *)osp));
14934 14934 ASSERT(nfs_zone() == mi->mi_zone);
14935 14935 ASSERT(mutex_owned(&osp->os_sync_lock));
14936 14936 ASSERT(*have_lockp);
14937 14937
14938 14938 if (!osp->os_valid ||
14939 14939 osp->os_open_ref_count > 0 || osp->os_mapcnt > 0) {
14940 14940 return;
14941 14941 }
14942 14942
14943 14943 /*
14944 14944 * This removes the reference obtained at OPEN; ie,
14945 14945 * when the open stream structure was created.
14946 14946 *
14947 14947 * We don't have to worry about calling 'open_stream_rele'
14948 14948 * since we our currently holding a reference to this
14949 14949 * open stream which means the count can not go to 0 with
14950 14950 * this decrement.
14951 14951 */
14952 14952 ASSERT(osp->os_ref_count >= 2);
14953 14953 osp->os_ref_count--;
14954 14954 osp->os_valid = 0;
14955 14955 mutex_exit(&osp->os_sync_lock);
14956 14956 *have_lockp = 0;
14957 14957
14958 14958 nfs4_dec_state_ref_count(mi);
14959 14959 }
14960 14960
14961 14961 /*
14962 14962 * Close all remaining open streams on the rnode. These open streams
14963 14963 * could be here because:
14964 14964 * - The close attempted at either close or delmap failed
14965 14965 * - Some kernel entity did VOP_OPEN but never did VOP_CLOSE
14966 14966 * - Someone did mknod on a regular file but never opened it
14967 14967 */
14968 14968 int
14969 14969 nfs4close_all(vnode_t *vp, cred_t *cr)
14970 14970 {
14971 14971 nfs4_open_stream_t *osp;
14972 14972 int error;
14973 14973 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
14974 14974 rnode4_t *rp;
14975 14975
14976 14976 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
14977 14977
14978 14978 error = 0;
14979 14979 rp = VTOR4(vp);
14980 14980
14981 14981 /*
14982 14982 * At this point, all we know is that the last time
14983 14983 * someone called vn_rele, the count was 1. Since then,
14984 14984 * the vnode could have been re-activated. We want to
14985 14985 * loop through the open streams and close each one, but
14986 14986 * we have to be careful since once we release the rnode
14987 14987 * hash bucket lock, someone else is free to come in and
14988 14988 * re-activate the rnode and add new open streams. The
14989 14989 * strategy is take the rnode hash bucket lock, verify that
14990 14990 * the count is still 1, grab the open stream off the
14991 14991 * head of the list and mark it invalid, then release the
14992 14992 * rnode hash bucket lock and proceed with that open stream.
14993 14993 * This is ok because nfs4close_one() will acquire the proper
14994 14994 * open/create to close/destroy synchronization for open
14995 14995 * streams, and will ensure that if someone has reopened
14996 14996 * the open stream after we've dropped the hash bucket lock
14997 14997 * then we'll just simply return without destroying the
14998 14998 * open stream.
14999 14999 * Repeat until the list is empty.
15000 15000 */
15001 15001
15002 15002 for (;;) {
15003 15003
15004 15004 /* make sure vnode hasn't been reactivated */
15005 15005 rw_enter(&rp->r_hashq->r_lock, RW_READER);
15006 15006 mutex_enter(&vp->v_lock);
15007 15007 if (vp->v_count > 1) {
15008 15008 mutex_exit(&vp->v_lock);
15009 15009 rw_exit(&rp->r_hashq->r_lock);
15010 15010 break;
15011 15011 }
15012 15012 /*
15013 15013 * Grabbing r_os_lock before releasing v_lock prevents
15014 15014 * a window where the rnode/open stream could get
15015 15015 * reactivated (and os_force_close set to 0) before we
15016 15016 * had a chance to set os_force_close to 1.
15017 15017 */
15018 15018 mutex_enter(&rp->r_os_lock);
15019 15019 mutex_exit(&vp->v_lock);
15020 15020
15021 15021 osp = list_head(&rp->r_open_streams);
15022 15022 if (!osp) {
15023 15023 /* nothing left to CLOSE OTW, so return */
15024 15024 mutex_exit(&rp->r_os_lock);
15025 15025 rw_exit(&rp->r_hashq->r_lock);
15026 15026 break;
15027 15027 }
15028 15028
15029 15029 mutex_enter(&rp->r_statev4_lock);
15030 15030 /* the file can't still be mem mapped */
15031 15031 ASSERT(rp->r_mapcnt == 0);
15032 15032 if (rp->created_v4)
15033 15033 rp->created_v4 = 0;
15034 15034 mutex_exit(&rp->r_statev4_lock);
15035 15035
15036 15036 /*
15037 15037 * Grab a ref on this open stream; nfs4close_one
15038 15038 * will mark it as invalid
15039 15039 */
15040 15040 mutex_enter(&osp->os_sync_lock);
15041 15041 osp->os_ref_count++;
15042 15042 osp->os_force_close = 1;
15043 15043 mutex_exit(&osp->os_sync_lock);
15044 15044 mutex_exit(&rp->r_os_lock);
15045 15045 rw_exit(&rp->r_hashq->r_lock);
15046 15046
15047 15047 nfs4close_one(vp, osp, cr, 0, NULL, &e, CLOSE_FORCE, 0, 0, 0);
15048 15048
15049 15049 /* Update error if it isn't already non-zero */
15050 15050 if (error == 0) {
15051 15051 if (e.error)
15052 15052 error = e.error;
15053 15053 else if (e.stat)
15054 15054 error = geterrno4(e.stat);
15055 15055 }
15056 15056
15057 15057 #ifdef DEBUG
15058 15058 nfs4close_all_cnt++;
15059 15059 #endif
15060 15060 /* Release the ref on osp acquired above. */
15061 15061 open_stream_rele(osp, rp);
15062 15062
15063 15063 /* Proceed to the next open stream, if any */
15064 15064 }
15065 15065 return (error);
15066 15066 }
15067 15067
15068 15068 /*
15069 15069 * nfs4close_one - close one open stream for a file if needed.
15070 15070 *
15071 15071 * "close_type" indicates which close path this is:
15072 15072 * CLOSE_NORM: close initiated via VOP_CLOSE.
15073 15073 * CLOSE_DELMAP: close initiated via VOP_DELMAP.
15074 15074 * CLOSE_FORCE: close initiated via VOP_INACTIVE. This path forces
15075 15075 * the close and release of client state for this open stream
15076 15076 * (unless someone else has the open stream open).
15077 15077 * CLOSE_RESEND: indicates the request is a replay of an earlier request
15078 15078 * (e.g., due to abort because of a signal).
15079 15079 * CLOSE_AFTER_RESEND: close initiated to "undo" a successful resent OPEN.
15080 15080 *
15081 15081 * CLOSE_RESEND and CLOSE_AFTER_RESEND will not attempt to retry after client
15082 15082 * recovery. Instead, the caller is expected to deal with retries.
15083 15083 *
15084 15084 * The caller can either pass in the osp ('provided_osp') or not.
15085 15085 *
15086 15086 * 'access_bits' represents the access we are closing/downgrading.
15087 15087 *
15088 15088 * 'len', 'prot', and 'mmap_flags' are used for CLOSE_DELMAP. 'len' is the
15089 15089 * number of bytes we are unmapping, 'maxprot' is the mmap protection, and
15090 15090 * 'mmap_flags' tells us the type of sharing (MAP_PRIVATE or MAP_SHARED).
15091 15091 *
15092 15092 * Errors are returned via the nfs4_error_t.
15093 15093 */
15094 15094 void
15095 15095 nfs4close_one(vnode_t *vp, nfs4_open_stream_t *provided_osp, cred_t *cr,
15096 15096 int access_bits, nfs4_lost_rqst_t *lrp, nfs4_error_t *ep,
15097 15097 nfs4_close_type_t close_type, size_t len, uint_t maxprot,
15098 15098 uint_t mmap_flags)
15099 15099 {
15100 15100 nfs4_open_owner_t *oop;
15101 15101 nfs4_open_stream_t *osp = NULL;
15102 15102 int retry = 0;
15103 15103 int num_retries = NFS4_NUM_RECOV_RETRIES;
15104 15104 rnode4_t *rp;
15105 15105 mntinfo4_t *mi;
15106 15106 nfs4_recov_state_t recov_state;
15107 15107 cred_t *cred_otw = NULL;
15108 15108 bool_t recovonly = FALSE;
15109 15109 int isrecov;
15110 15110 int force_close;
15111 15111 int close_failed = 0;
15112 15112 int did_dec_count = 0;
15113 15113 int did_start_op = 0;
15114 15114 int did_force_recovlock = 0;
15115 15115 int did_start_seqid_sync = 0;
15116 15116 int have_sync_lock = 0;
15117 15117
15118 15118 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
15119 15119
15120 15120 NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE, "closing vp %p osp %p, "
15121 15121 "lrp %p, close type %d len %ld prot %x mmap flags %x bits %x",
15122 15122 (void *)vp, (void *)provided_osp, (void *)lrp, close_type,
15123 15123 len, maxprot, mmap_flags, access_bits));
15124 15124
15125 15125 nfs4_error_zinit(ep);
15126 15126 rp = VTOR4(vp);
15127 15127 mi = VTOMI4(vp);
15128 15128 isrecov = (close_type == CLOSE_RESEND ||
15129 15129 close_type == CLOSE_AFTER_RESEND);
15130 15130
15131 15131 /*
15132 15132 * First get the open owner.
15133 15133 */
15134 15134 if (!provided_osp) {
15135 15135 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi);
15136 15136 } else {
15137 15137 oop = provided_osp->os_open_owner;
15138 15138 ASSERT(oop != NULL);
15139 15139 open_owner_hold(oop);
15140 15140 }
15141 15141
15142 15142 if (!oop) {
15143 15143 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
15144 15144 "nfs4close_one: no oop, rp %p, mi %p, cr %p, osp %p, "
15145 15145 "close type %d", (void *)rp, (void *)mi, (void *)cr,
15146 15146 (void *)provided_osp, close_type));
15147 15147 ep->error = EIO;
15148 15148 goto out;
15149 15149 }
15150 15150
15151 15151 cred_otw = nfs4_get_otw_cred(cr, mi, oop);
15152 15152 recov_retry:
15153 15153 osp = NULL;
15154 15154 close_failed = 0;
15155 15155 force_close = (close_type == CLOSE_FORCE);
15156 15156 retry = 0;
15157 15157 did_start_op = 0;
15158 15158 did_force_recovlock = 0;
15159 15159 did_start_seqid_sync = 0;
15160 15160 have_sync_lock = 0;
15161 15161 recovonly = FALSE;
15162 15162 recov_state.rs_flags = 0;
15163 15163 recov_state.rs_num_retry_despite_err = 0;
15164 15164
15165 15165 /*
15166 15166 * Second synchronize with recovery.
15167 15167 */
15168 15168 if (!isrecov) {
15169 15169 ep->error = nfs4_start_fop(mi, vp, NULL, OH_CLOSE,
15170 15170 &recov_state, &recovonly);
15171 15171 if (!ep->error) {
15172 15172 did_start_op = 1;
15173 15173 } else {
15174 15174 close_failed = 1;
15175 15175 /*
15176 15176 * If we couldn't get start_fop, but have to
15177 15177 * cleanup state, then at least acquire the
15178 15178 * mi_recovlock so we can synchronize with
15179 15179 * recovery.
15180 15180 */
15181 15181 if (close_type == CLOSE_FORCE) {
15182 15182 (void) nfs_rw_enter_sig(&mi->mi_recovlock,
15183 15183 RW_READER, FALSE);
15184 15184 did_force_recovlock = 1;
15185 15185 } else
15186 15186 goto out;
15187 15187 }
15188 15188 }
15189 15189
15190 15190 /*
15191 15191 * We cannot attempt to get the open seqid sync if nfs4_start_fop
15192 15192 * set 'recovonly' to TRUE since most likely this is due to
15193 15193 * reovery being active (MI4_RECOV_ACTIV). If recovery is active,
15194 15194 * nfs4_start_open_seqid_sync() will fail with EAGAIN asking us
15195 15195 * to retry, causing us to loop until recovery finishes. Plus we
15196 15196 * don't need protection over the open seqid since we're not going
15197 15197 * OTW, hence don't need to use the seqid.
15198 15198 */
15199 15199 if (recovonly == FALSE) {
15200 15200 /* need to grab the open owner sync before 'os_sync_lock' */
15201 15201 ep->error = nfs4_start_open_seqid_sync(oop, mi);
15202 15202 if (ep->error == EAGAIN) {
15203 15203 ASSERT(!isrecov);
15204 15204 if (did_start_op)
15205 15205 nfs4_end_fop(mi, vp, NULL, OH_CLOSE,
15206 15206 &recov_state, TRUE);
15207 15207 if (did_force_recovlock)
15208 15208 nfs_rw_exit(&mi->mi_recovlock);
15209 15209 goto recov_retry;
15210 15210 }
15211 15211 did_start_seqid_sync = 1;
15212 15212 }
15213 15213
15214 15214 /*
15215 15215 * Third get an open stream and acquire 'os_sync_lock' to
15216 15216 * sychronize the opening/creating of an open stream with the
15217 15217 * closing/destroying of an open stream.
15218 15218 */
15219 15219 if (!provided_osp) {
15220 15220 /* returns with 'os_sync_lock' held */
15221 15221 osp = find_open_stream(oop, rp);
15222 15222 if (!osp) {
15223 15223 ep->error = EIO;
15224 15224 goto out;
15225 15225 }
15226 15226 } else {
15227 15227 osp = provided_osp;
15228 15228 open_stream_hold(osp);
15229 15229 mutex_enter(&osp->os_sync_lock);
15230 15230 }
15231 15231 have_sync_lock = 1;
15232 15232
15233 15233 ASSERT(oop == osp->os_open_owner);
15234 15234
15235 15235 /*
15236 15236 * Fourth, do any special pre-OTW CLOSE processing
15237 15237 * based on the specific close type.
15238 15238 */
15239 15239 if ((close_type == CLOSE_NORM || close_type == CLOSE_AFTER_RESEND) &&
15240 15240 !did_dec_count) {
15241 15241 ASSERT(osp->os_open_ref_count > 0);
15242 15242 osp->os_open_ref_count--;
15243 15243 did_dec_count = 1;
15244 15244 if (osp->os_open_ref_count == 0)
15245 15245 osp->os_final_close = 1;
15246 15246 }
15247 15247
15248 15248 if (close_type == CLOSE_FORCE) {
15249 15249 /* see if somebody reopened the open stream. */
15250 15250 if (!osp->os_force_close) {
15251 15251 NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE,
15252 15252 "nfs4close_one: skip CLOSE_FORCE as osp %p "
15253 15253 "was reopened, vp %p", (void *)osp, (void *)vp));
15254 15254 ep->error = 0;
15255 15255 ep->stat = NFS4_OK;
15256 15256 goto out;
15257 15257 }
15258 15258
15259 15259 if (!osp->os_final_close && !did_dec_count) {
15260 15260 osp->os_open_ref_count--;
15261 15261 did_dec_count = 1;
15262 15262 }
15263 15263
15264 15264 /*
15265 15265 * We can't depend on os_open_ref_count being 0 due to the
15266 15266 * way executables are opened (VN_RELE to match a VOP_OPEN).
15267 15267 */
15268 15268 #ifdef NOTYET
15269 15269 ASSERT(osp->os_open_ref_count == 0);
15270 15270 #endif
15271 15271 if (osp->os_open_ref_count != 0) {
15272 15272 NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE,
15273 15273 "nfs4close_one: should panic here on an "
15274 15274 "ASSERT(osp->os_open_ref_count == 0). Ignoring "
15275 15275 "since this is probably the exec problem."));
15276 15276
15277 15277 osp->os_open_ref_count = 0;
15278 15278 }
15279 15279
15280 15280 /*
15281 15281 * There is the possibility that nfs4close_one()
15282 15282 * for close_type == CLOSE_DELMAP couldn't find the
15283 15283 * open stream, thus couldn't decrement its os_mapcnt;
15284 15284 * therefore we can't use this ASSERT yet.
15285 15285 */
15286 15286 #ifdef NOTYET
15287 15287 ASSERT(osp->os_mapcnt == 0);
15288 15288 #endif
15289 15289 osp->os_mapcnt = 0;
15290 15290 }
15291 15291
15292 15292 if (close_type == CLOSE_DELMAP && !did_dec_count) {
15293 15293 ASSERT(osp->os_mapcnt >= btopr(len));
15294 15294
15295 15295 if ((mmap_flags & MAP_SHARED) && (maxprot & PROT_WRITE))
15296 15296 osp->os_mmap_write -= btopr(len);
15297 15297 if (maxprot & PROT_READ)
15298 15298 osp->os_mmap_read -= btopr(len);
15299 15299 if (maxprot & PROT_EXEC)
15300 15300 osp->os_mmap_read -= btopr(len);
15301 15301 /* mirror the PROT_NONE check in nfs4_addmap() */
15302 15302 if (!(maxprot & PROT_READ) && !(maxprot & PROT_WRITE) &&
15303 15303 !(maxprot & PROT_EXEC))
15304 15304 osp->os_mmap_read -= btopr(len);
15305 15305 osp->os_mapcnt -= btopr(len);
15306 15306 did_dec_count = 1;
15307 15307 }
15308 15308
15309 15309 if (recovonly) {
15310 15310 nfs4_lost_rqst_t lost_rqst;
15311 15311
15312 15312 /* request should not already be in recovery queue */
15313 15313 ASSERT(lrp == NULL);
15314 15314 nfs4_error_init(ep, EINTR);
15315 15315 nfs4close_save_lost_rqst(ep->error, &lost_rqst, oop,
15316 15316 osp, cred_otw, vp);
15317 15317 mutex_exit(&osp->os_sync_lock);
15318 15318 have_sync_lock = 0;
15319 15319 (void) nfs4_start_recovery(ep, mi, vp, NULL, NULL,
15320 15320 lost_rqst.lr_op == OP_CLOSE ?
15321 15321 &lost_rqst : NULL, OP_CLOSE, NULL, NULL, NULL);
15322 15322 close_failed = 1;
15323 15323 force_close = 0;
15324 15324 goto close_cleanup;
15325 15325 }
15326 15326
15327 15327 /*
15328 15328 * If a previous OTW call got NFS4ERR_BAD_SEQID, then
15329 15329 * we stopped operating on the open owner's <old oo_name, old seqid>
15330 15330 * space, which means we stopped operating on the open stream
15331 15331 * too. So don't go OTW (as the seqid is likely bad, and the
15332 15332 * stateid could be stale, potentially triggering a false
15333 15333 * setclientid), and just clean up the client's internal state.
15334 15334 */
15335 15335 if (osp->os_orig_oo_name != oop->oo_name) {
15336 15336 NFS4_DEBUG(nfs4close_one_debug || nfs4_client_recov_debug,
15337 15337 (CE_NOTE, "nfs4close_one: skip OTW close for osp %p "
15338 15338 "oop %p due to bad seqid (orig oo_name %" PRIx64 " current "
15339 15339 "oo_name %" PRIx64")",
15340 15340 (void *)osp, (void *)oop, osp->os_orig_oo_name,
15341 15341 oop->oo_name));
15342 15342 close_failed = 1;
15343 15343 }
15344 15344
15345 15345 /* If the file failed recovery, just quit. */
15346 15346 mutex_enter(&rp->r_statelock);
15347 15347 if (rp->r_flags & R4RECOVERR) {
15348 15348 close_failed = 1;
15349 15349 }
15350 15350 mutex_exit(&rp->r_statelock);
15351 15351
15352 15352 /*
15353 15353 * If the force close path failed to obtain start_fop
15354 15354 * then skip the OTW close and just remove the state.
15355 15355 */
15356 15356 if (close_failed)
15357 15357 goto close_cleanup;
15358 15358
15359 15359 /*
15360 15360 * Fifth, check to see if there are still mapped pages or other
15361 15361 * opens using this open stream. If there are then we can't
15362 15362 * close yet but we can see if an OPEN_DOWNGRADE is necessary.
15363 15363 */
15364 15364 if (osp->os_open_ref_count > 0 || osp->os_mapcnt > 0) {
15365 15365 nfs4_lost_rqst_t new_lost_rqst;
15366 15366 bool_t needrecov = FALSE;
15367 15367 cred_t *odg_cred_otw = NULL;
15368 15368 seqid4 open_dg_seqid = 0;
15369 15369
15370 15370 if (osp->os_delegation) {
15371 15371 /*
15372 15372 * If this open stream was never OPENed OTW then we
15373 15373 * surely can't DOWNGRADE it (especially since the
15374 15374 * osp->open_stateid is really a delegation stateid
15375 15375 * when os_delegation is 1).
15376 15376 */
15377 15377 if (access_bits & FREAD)
15378 15378 osp->os_share_acc_read--;
15379 15379 if (access_bits & FWRITE)
15380 15380 osp->os_share_acc_write--;
15381 15381 osp->os_share_deny_none--;
15382 15382 nfs4_error_zinit(ep);
15383 15383 goto out;
15384 15384 }
15385 15385 nfs4_open_downgrade(access_bits, 0, oop, osp, vp, cr,
15386 15386 lrp, ep, &odg_cred_otw, &open_dg_seqid);
15387 15387 needrecov = nfs4_needs_recovery(ep, TRUE, mi->mi_vfsp);
15388 15388 if (needrecov && !isrecov) {
15389 15389 bool_t abort;
15390 15390 nfs4_bseqid_entry_t *bsep = NULL;
15391 15391
15392 15392 if (!ep->error && ep->stat == NFS4ERR_BAD_SEQID)
15393 15393 bsep = nfs4_create_bseqid_entry(oop, NULL,
15394 15394 vp, 0,
15395 15395 lrp ? TAG_OPEN_DG_LOST : TAG_OPEN_DG,
15396 15396 open_dg_seqid);
15397 15397
15398 15398 nfs4open_dg_save_lost_rqst(ep->error, &new_lost_rqst,
15399 15399 oop, osp, odg_cred_otw, vp, access_bits, 0);
15400 15400 mutex_exit(&osp->os_sync_lock);
15401 15401 have_sync_lock = 0;
15402 15402 abort = nfs4_start_recovery(ep, mi, vp, NULL, NULL,
15403 15403 new_lost_rqst.lr_op == OP_OPEN_DOWNGRADE ?
15404 15404 &new_lost_rqst : NULL, OP_OPEN_DOWNGRADE,
15405 15405 bsep, NULL, NULL);
15406 15406 if (odg_cred_otw)
15407 15407 crfree(odg_cred_otw);
15408 15408 if (bsep)
15409 15409 kmem_free(bsep, sizeof (*bsep));
15410 15410
15411 15411 if (abort == TRUE)
15412 15412 goto out;
15413 15413
15414 15414 if (did_start_seqid_sync) {
15415 15415 nfs4_end_open_seqid_sync(oop);
15416 15416 did_start_seqid_sync = 0;
15417 15417 }
15418 15418 open_stream_rele(osp, rp);
15419 15419
15420 15420 if (did_start_op)
15421 15421 nfs4_end_fop(mi, vp, NULL, OH_CLOSE,
15422 15422 &recov_state, FALSE);
15423 15423 if (did_force_recovlock)
15424 15424 nfs_rw_exit(&mi->mi_recovlock);
15425 15425
15426 15426 goto recov_retry;
15427 15427 } else {
15428 15428 if (odg_cred_otw)
15429 15429 crfree(odg_cred_otw);
15430 15430 }
15431 15431 goto out;
15432 15432 }
15433 15433
15434 15434 /*
15435 15435 * If this open stream was created as the results of an open
15436 15436 * while holding a delegation, then just release it; no need
15437 15437 * to do an OTW close. Otherwise do a "normal" OTW close.
15438 15438 */
15439 15439 if (osp->os_delegation) {
15440 15440 nfs4close_notw(vp, osp, &have_sync_lock);
15441 15441 nfs4_error_zinit(ep);
15442 15442 goto out;
15443 15443 }
15444 15444
15445 15445 /*
15446 15446 * If this stream is not valid, we're done.
15447 15447 */
15448 15448 if (!osp->os_valid) {
15449 15449 nfs4_error_zinit(ep);
15450 15450 goto out;
15451 15451 }
15452 15452
15453 15453 /*
15454 15454 * Last open or mmap ref has vanished, need to do an OTW close.
15455 15455 * First check to see if a close is still necessary.
15456 15456 */
15457 15457 if (osp->os_failed_reopen) {
15458 15458 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
15459 15459 "don't close OTW osp %p since reopen failed.",
15460 15460 (void *)osp));
15461 15461 /*
15462 15462 * Reopen of the open stream failed, hence the
15463 15463 * stateid of the open stream is invalid/stale, and
15464 15464 * sending this OTW would incorrectly cause another
15465 15465 * round of recovery. In this case, we need to set
15466 15466 * the 'os_valid' bit to 0 so another thread doesn't
15467 15467 * come in and re-open this open stream before
15468 15468 * this "closing" thread cleans up state (decrementing
15469 15469 * the nfs4_server_t's state_ref_count and decrementing
15470 15470 * the os_ref_count).
15471 15471 */
15472 15472 osp->os_valid = 0;
15473 15473 /*
15474 15474 * This removes the reference obtained at OPEN; ie,
15475 15475 * when the open stream structure was created.
15476 15476 *
15477 15477 * We don't have to worry about calling 'open_stream_rele'
15478 15478 * since we our currently holding a reference to this
15479 15479 * open stream which means the count can not go to 0 with
15480 15480 * this decrement.
15481 15481 */
15482 15482 ASSERT(osp->os_ref_count >= 2);
15483 15483 osp->os_ref_count--;
15484 15484 nfs4_error_zinit(ep);
15485 15485 close_failed = 0;
15486 15486 goto close_cleanup;
15487 15487 }
15488 15488
15489 15489 ASSERT(osp->os_ref_count > 1);
15490 15490
15491 15491 /*
15492 15492 * Sixth, try the CLOSE OTW.
15493 15493 */
15494 15494 nfs4close_otw(rp, cred_otw, oop, osp, &retry, &did_start_seqid_sync,
15495 15495 close_type, ep, &have_sync_lock);
15496 15496
15497 15497 if (ep->error == EINTR || NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp)) {
15498 15498 /*
15499 15499 * Let the recovery thread be responsible for
15500 15500 * removing the state for CLOSE.
15501 15501 */
15502 15502 close_failed = 1;
15503 15503 force_close = 0;
15504 15504 retry = 0;
15505 15505 }
15506 15506
15507 15507 /* See if we need to retry with a different cred */
15508 15508 if ((ep->error == EACCES ||
15509 15509 (ep->error == 0 && ep->stat == NFS4ERR_ACCESS)) &&
15510 15510 cred_otw != cr) {
15511 15511 crfree(cred_otw);
15512 15512 cred_otw = cr;
15513 15513 crhold(cred_otw);
15514 15514 retry = 1;
15515 15515 }
15516 15516
15517 15517 if (ep->error || ep->stat)
15518 15518 close_failed = 1;
15519 15519
15520 15520 if (retry && !isrecov && num_retries-- > 0) {
15521 15521 if (have_sync_lock) {
15522 15522 mutex_exit(&osp->os_sync_lock);
15523 15523 have_sync_lock = 0;
15524 15524 }
15525 15525 if (did_start_seqid_sync) {
15526 15526 nfs4_end_open_seqid_sync(oop);
15527 15527 did_start_seqid_sync = 0;
15528 15528 }
15529 15529 open_stream_rele(osp, rp);
15530 15530
15531 15531 if (did_start_op)
15532 15532 nfs4_end_fop(mi, vp, NULL, OH_CLOSE,
15533 15533 &recov_state, FALSE);
15534 15534 if (did_force_recovlock)
15535 15535 nfs_rw_exit(&mi->mi_recovlock);
15536 15536 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
15537 15537 "nfs4close_one: need to retry the close "
15538 15538 "operation"));
15539 15539 goto recov_retry;
15540 15540 }
15541 15541 close_cleanup:
15542 15542 /*
15543 15543 * Seventh and lastly, process our results.
15544 15544 */
15545 15545 if (close_failed && force_close) {
15546 15546 /*
15547 15547 * It's ok to drop and regrab the 'os_sync_lock' since
15548 15548 * nfs4close_notw() will recheck to make sure the
15549 15549 * "close"/removal of state should happen.
15550 15550 */
15551 15551 if (!have_sync_lock) {
15552 15552 mutex_enter(&osp->os_sync_lock);
15553 15553 have_sync_lock = 1;
15554 15554 }
15555 15555 /*
15556 15556 * This is last call, remove the ref on the open
15557 15557 * stream created by open and clean everything up.
15558 15558 */
15559 15559 osp->os_pending_close = 0;
15560 15560 nfs4close_notw(vp, osp, &have_sync_lock);
15561 15561 nfs4_error_zinit(ep);
15562 15562 }
15563 15563
15564 15564 if (!close_failed) {
15565 15565 if (have_sync_lock) {
15566 15566 osp->os_pending_close = 0;
15567 15567 mutex_exit(&osp->os_sync_lock);
15568 15568 have_sync_lock = 0;
15569 15569 } else {
15570 15570 mutex_enter(&osp->os_sync_lock);
15571 15571 osp->os_pending_close = 0;
15572 15572 mutex_exit(&osp->os_sync_lock);
15573 15573 }
15574 15574 if (did_start_op && recov_state.rs_sp != NULL) {
15575 15575 mutex_enter(&recov_state.rs_sp->s_lock);
15576 15576 nfs4_dec_state_ref_count_nolock(recov_state.rs_sp, mi);
15577 15577 mutex_exit(&recov_state.rs_sp->s_lock);
15578 15578 } else {
15579 15579 nfs4_dec_state_ref_count(mi);
15580 15580 }
15581 15581 nfs4_error_zinit(ep);
15582 15582 }
15583 15583
15584 15584 out:
15585 15585 if (have_sync_lock)
15586 15586 mutex_exit(&osp->os_sync_lock);
15587 15587 if (did_start_op)
15588 15588 nfs4_end_fop(mi, vp, NULL, OH_CLOSE, &recov_state,
15589 15589 recovonly ? TRUE : FALSE);
15590 15590 if (did_force_recovlock)
15591 15591 nfs_rw_exit(&mi->mi_recovlock);
15592 15592 if (cred_otw)
15593 15593 crfree(cred_otw);
15594 15594 if (osp)
15595 15595 open_stream_rele(osp, rp);
15596 15596 if (oop) {
15597 15597 if (did_start_seqid_sync)
15598 15598 nfs4_end_open_seqid_sync(oop);
15599 15599 open_owner_rele(oop);
15600 15600 }
15601 15601 }
15602 15602
15603 15603 /*
15604 15604 * Convert information returned by the server in the LOCK4denied
15605 15605 * structure to the form required by fcntl.
15606 15606 */
15607 15607 static void
15608 15608 denied_to_flk(LOCK4denied *lockt_denied, flock64_t *flk, LOCKT4args *lockt_args)
15609 15609 {
15610 15610 nfs4_lo_name_t *lo;
15611 15611
15612 15612 #ifdef DEBUG
15613 15613 if (denied_to_flk_debug) {
15614 15614 lockt_denied_debug = lockt_denied;
15615 15615 debug_enter("lockt_denied");
15616 15616 }
15617 15617 #endif
15618 15618
15619 15619 flk->l_type = lockt_denied->locktype == READ_LT ? F_RDLCK : F_WRLCK;
15620 15620 flk->l_whence = 0; /* aka SEEK_SET */
15621 15621 flk->l_start = lockt_denied->offset;
15622 15622 flk->l_len = lockt_denied->length;
15623 15623
15624 15624 /*
15625 15625 * If the blocking clientid matches our client id, then we can
15626 15626 * interpret the lockowner (since we built it). If not, then
15627 15627 * fabricate a sysid and pid. Note that the l_sysid field
15628 15628 * in *flk already has the local sysid.
15629 15629 */
15630 15630
15631 15631 if (lockt_denied->owner.clientid == lockt_args->owner.clientid) {
15632 15632
15633 15633 if (lockt_denied->owner.owner_len == sizeof (*lo)) {
15634 15634 lo = (nfs4_lo_name_t *)
15635 15635 lockt_denied->owner.owner_val;
15636 15636
15637 15637 flk->l_pid = lo->ln_pid;
15638 15638 } else {
15639 15639 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
15640 15640 "denied_to_flk: bad lock owner length\n"));
15641 15641
15642 15642 flk->l_pid = lo_to_pid(&lockt_denied->owner);
15643 15643 }
15644 15644 } else {
15645 15645 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
15646 15646 "denied_to_flk: foreign clientid\n"));
15647 15647
15648 15648 /*
15649 15649 * Construct a new sysid which should be different from
15650 15650 * sysids of other systems.
15651 15651 */
15652 15652
15653 15653 flk->l_sysid++;
15654 15654 flk->l_pid = lo_to_pid(&lockt_denied->owner);
15655 15655 }
15656 15656 }
15657 15657
15658 15658 static pid_t
15659 15659 lo_to_pid(lock_owner4 *lop)
15660 15660 {
15661 15661 pid_t pid = 0;
15662 15662 uchar_t *cp;
15663 15663 int i;
15664 15664
15665 15665 cp = (uchar_t *)&lop->clientid;
15666 15666
15667 15667 for (i = 0; i < sizeof (lop->clientid); i++)
15668 15668 pid += (pid_t)*cp++;
15669 15669
15670 15670 cp = (uchar_t *)lop->owner_val;
15671 15671
15672 15672 for (i = 0; i < lop->owner_len; i++)
15673 15673 pid += (pid_t)*cp++;
15674 15674
15675 15675 return (pid);
15676 15676 }
15677 15677
15678 15678 /*
15679 15679 * Given a lock pointer, returns the length of that lock.
15680 15680 * "end" is the last locked offset the "l_len" covers from
15681 15681 * the start of the lock.
15682 15682 */
15683 15683 static off64_t
15684 15684 lock_to_end(flock64_t *lock)
15685 15685 {
15686 15686 off64_t lock_end;
15687 15687
15688 15688 if (lock->l_len == 0)
15689 15689 lock_end = (off64_t)MAXEND;
15690 15690 else
15691 15691 lock_end = lock->l_start + lock->l_len - 1;
15692 15692
15693 15693 return (lock_end);
15694 15694 }
15695 15695
15696 15696 /*
15697 15697 * Given the end of a lock, it will return you the length "l_len" for that lock.
15698 15698 */
15699 15699 static off64_t
15700 15700 end_to_len(off64_t start, off64_t end)
15701 15701 {
15702 15702 off64_t lock_len;
15703 15703
15704 15704 ASSERT(end >= start);
15705 15705 if (end == MAXEND)
15706 15706 lock_len = 0;
15707 15707 else
15708 15708 lock_len = end - start + 1;
15709 15709
15710 15710 return (lock_len);
15711 15711 }
15712 15712
15713 15713 /*
15714 15714 * On given end for a lock it determines if it is the last locked offset
15715 15715 * or not, if so keeps it as is, else adds one to return the length for
15716 15716 * valid start.
15717 15717 */
15718 15718 static off64_t
15719 15719 start_check(off64_t x)
15720 15720 {
15721 15721 if (x == MAXEND)
15722 15722 return (x);
15723 15723 else
15724 15724 return (x + 1);
15725 15725 }
15726 15726
15727 15727 /*
15728 15728 * See if these two locks overlap, and if so return 1;
15729 15729 * otherwise, return 0.
15730 15730 */
15731 15731 static int
15732 15732 locks_intersect(flock64_t *llfp, flock64_t *curfp)
15733 15733 {
15734 15734 off64_t llfp_end, curfp_end;
15735 15735
15736 15736 llfp_end = lock_to_end(llfp);
15737 15737 curfp_end = lock_to_end(curfp);
15738 15738
15739 15739 if (((llfp_end >= curfp->l_start) &&
15740 15740 (llfp->l_start <= curfp->l_start)) ||
15741 15741 ((curfp->l_start <= llfp->l_start) && (curfp_end >= llfp->l_start)))
15742 15742 return (1);
15743 15743 return (0);
15744 15744 }
15745 15745
15746 15746 /*
15747 15747 * Determine what the intersecting lock region is, and add that to the
15748 15748 * 'nl_llpp' locklist in increasing order (by l_start).
15749 15749 */
15750 15750 static void
15751 15751 nfs4_add_lock_range(flock64_t *lost_flp, flock64_t *local_flp,
15752 15752 locklist_t **nl_llpp, vnode_t *vp)
15753 15753 {
15754 15754 locklist_t *intersect_llp, *tmp_fllp, *cur_fllp;
15755 15755 off64_t lost_flp_end, local_flp_end, len, start;
15756 15756
15757 15757 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range:"));
15758 15758
15759 15759 if (!locks_intersect(lost_flp, local_flp))
15760 15760 return;
15761 15761
15762 15762 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range: "
15763 15763 "locks intersect"));
15764 15764
15765 15765 lost_flp_end = lock_to_end(lost_flp);
15766 15766 local_flp_end = lock_to_end(local_flp);
15767 15767
15768 15768 /* Find the starting point of the intersecting region */
15769 15769 if (local_flp->l_start > lost_flp->l_start)
15770 15770 start = local_flp->l_start;
15771 15771 else
15772 15772 start = lost_flp->l_start;
15773 15773
15774 15774 /* Find the lenght of the intersecting region */
15775 15775 if (lost_flp_end < local_flp_end)
15776 15776 len = end_to_len(start, lost_flp_end);
15777 15777 else
15778 15778 len = end_to_len(start, local_flp_end);
15779 15779
15780 15780 /*
15781 15781 * Prepare the flock structure for the intersection found and insert
15782 15782 * it into the new list in increasing l_start order. This list contains
15783 15783 * intersections of locks registered by the client with the local host
15784 15784 * and the lost lock.
15785 15785 * The lock type of this lock is the same as that of the local_flp.
15786 15786 */
15787 15787 intersect_llp = (locklist_t *)kmem_alloc(sizeof (locklist_t), KM_SLEEP);
15788 15788 intersect_llp->ll_flock.l_start = start;
15789 15789 intersect_llp->ll_flock.l_len = len;
15790 15790 intersect_llp->ll_flock.l_type = local_flp->l_type;
15791 15791 intersect_llp->ll_flock.l_pid = local_flp->l_pid;
15792 15792 intersect_llp->ll_flock.l_sysid = local_flp->l_sysid;
15793 15793 intersect_llp->ll_flock.l_whence = 0; /* aka SEEK_SET */
15794 15794 intersect_llp->ll_vp = vp;
15795 15795
15796 15796 tmp_fllp = *nl_llpp;
15797 15797 cur_fllp = NULL;
15798 15798 while (tmp_fllp != NULL && tmp_fllp->ll_flock.l_start <
15799 15799 intersect_llp->ll_flock.l_start) {
15800 15800 cur_fllp = tmp_fllp;
15801 15801 tmp_fllp = tmp_fllp->ll_next;
15802 15802 }
15803 15803 if (cur_fllp == NULL) {
15804 15804 /* first on the list */
15805 15805 intersect_llp->ll_next = *nl_llpp;
15806 15806 *nl_llpp = intersect_llp;
15807 15807 } else {
15808 15808 intersect_llp->ll_next = cur_fllp->ll_next;
15809 15809 cur_fllp->ll_next = intersect_llp;
15810 15810 }
15811 15811
15812 15812 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range: "
15813 15813 "created lock region: start %"PRIx64" end %"PRIx64" : %s\n",
15814 15814 intersect_llp->ll_flock.l_start,
15815 15815 intersect_llp->ll_flock.l_start + intersect_llp->ll_flock.l_len,
15816 15816 intersect_llp->ll_flock.l_type == F_RDLCK ? "READ" : "WRITE"));
15817 15817 }
15818 15818
15819 15819 /*
15820 15820 * Our local locking current state is potentially different than
15821 15821 * what the NFSv4 server thinks we have due to a lost lock that was
15822 15822 * resent and then received. We need to reset our "NFSv4" locking
15823 15823 * state to match the current local locking state for this pid since
15824 15824 * that is what the user/application sees as what the world is.
15825 15825 *
15826 15826 * We cannot afford to drop the open/lock seqid sync since then we can
15827 15827 * get confused about what the current local locking state "is" versus
15828 15828 * "was".
15829 15829 *
15830 15830 * If we are unable to fix up the locks, we send SIGLOST to the affected
15831 15831 * process. This is not done if the filesystem has been forcibly
15832 15832 * unmounted, in case the process has already exited and a new process
15833 15833 * exists with the same pid.
15834 15834 */
15835 15835 static void
15836 15836 nfs4_reinstitute_local_lock_state(vnode_t *vp, flock64_t *lost_flp, cred_t *cr,
15837 15837 nfs4_lock_owner_t *lop)
15838 15838 {
15839 15839 locklist_t *locks, *llp, *ri_llp, *tmp_llp;
15840 15840 mntinfo4_t *mi = VTOMI4(vp);
15841 15841 const int cmd = F_SETLK;
15842 15842 off64_t cur_start, llp_ll_flock_end, lost_flp_end;
15843 15843 flock64_t ul_fl;
15844 15844
15845 15845 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
15846 15846 "nfs4_reinstitute_local_lock_state"));
15847 15847
15848 15848 /*
15849 15849 * Find active locks for this vp from the local locking code.
15850 15850 * Scan through this list and find out the locks that intersect with
15851 15851 * the lost lock. Once we find the lock that intersects, add the
15852 15852 * intersection area as a new lock to a new list "ri_llp". The lock
15853 15853 * type of the intersection region lock added to ri_llp is the same
15854 15854 * as that found in the active lock list, "list". The intersecting
15855 15855 * region locks are added to ri_llp in increasing l_start order.
15856 15856 */
15857 15857 ASSERT(nfs_zone() == mi->mi_zone);
15858 15858
15859 15859 locks = flk_active_locks_for_vp(vp);
15860 15860 ri_llp = NULL;
15861 15861
15862 15862 for (llp = locks; llp != NULL; llp = llp->ll_next) {
15863 15863 ASSERT(llp->ll_vp == vp);
15864 15864 /*
15865 15865 * Pick locks that belong to this pid/lockowner
15866 15866 */
15867 15867 if (llp->ll_flock.l_pid != lost_flp->l_pid)
15868 15868 continue;
15869 15869
15870 15870 nfs4_add_lock_range(lost_flp, &llp->ll_flock, &ri_llp, vp);
15871 15871 }
15872 15872
15873 15873 /*
15874 15874 * Now we have the list of intersections with the lost lock. These are
15875 15875 * the locks that were/are active before the server replied to the
15876 15876 * last/lost lock. Issue these locks to the server here. Playing these
15877 15877 * locks to the server will re-establish aur current local locking state
15878 15878 * with the v4 server.
15879 15879 * If we get an error, send SIGLOST to the application for that lock.
15880 15880 */
15881 15881
15882 15882 for (llp = ri_llp; llp != NULL; llp = llp->ll_next) {
15883 15883 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
15884 15884 "nfs4_reinstitute_local_lock_state: need to issue "
15885 15885 "flock: [%"PRIx64" - %"PRIx64"] : %s",
15886 15886 llp->ll_flock.l_start,
15887 15887 llp->ll_flock.l_start + llp->ll_flock.l_len,
15888 15888 llp->ll_flock.l_type == F_RDLCK ? "READ" :
15889 15889 llp->ll_flock.l_type == F_WRLCK ? "WRITE" : "INVALID"));
15890 15890 /*
15891 15891 * No need to relock what we already have
15892 15892 */
15893 15893 if (llp->ll_flock.l_type == lost_flp->l_type)
15894 15894 continue;
15895 15895
15896 15896 push_reinstate(vp, cmd, &llp->ll_flock, cr, lop);
15897 15897 }
15898 15898
15899 15899 /*
15900 15900 * Now keeping the start of the lost lock as our reference parse the
15901 15901 * newly created ri_llp locklist to find the ranges that we have locked
15902 15902 * with the v4 server but not in the current local locking. We need
15903 15903 * to unlock these ranges.
15904 15904 * These ranges can also be reffered to as those ranges, where the lost
15905 15905 * lock does not overlap with the locks in the ri_llp but are locked
15906 15906 * since the server replied to the lost lock.
15907 15907 */
15908 15908 cur_start = lost_flp->l_start;
15909 15909 lost_flp_end = lock_to_end(lost_flp);
15910 15910
15911 15911 ul_fl.l_type = F_UNLCK;
15912 15912 ul_fl.l_whence = 0; /* aka SEEK_SET */
15913 15913 ul_fl.l_sysid = lost_flp->l_sysid;
15914 15914 ul_fl.l_pid = lost_flp->l_pid;
15915 15915
15916 15916 for (llp = ri_llp; llp != NULL; llp = llp->ll_next) {
15917 15917 llp_ll_flock_end = lock_to_end(&llp->ll_flock);
15918 15918
15919 15919 if (llp->ll_flock.l_start <= cur_start) {
15920 15920 cur_start = start_check(llp_ll_flock_end);
15921 15921 continue;
15922 15922 }
15923 15923 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
15924 15924 "nfs4_reinstitute_local_lock_state: "
15925 15925 "UNLOCK [%"PRIx64" - %"PRIx64"]",
15926 15926 cur_start, llp->ll_flock.l_start));
15927 15927
15928 15928 ul_fl.l_start = cur_start;
15929 15929 ul_fl.l_len = end_to_len(cur_start,
15930 15930 (llp->ll_flock.l_start - 1));
15931 15931
15932 15932 push_reinstate(vp, cmd, &ul_fl, cr, lop);
15933 15933 cur_start = start_check(llp_ll_flock_end);
15934 15934 }
15935 15935
15936 15936 /*
15937 15937 * In the case where the lost lock ends after all intersecting locks,
15938 15938 * unlock the last part of the lost lock range.
15939 15939 */
15940 15940 if (cur_start != start_check(lost_flp_end)) {
15941 15941 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
15942 15942 "nfs4_reinstitute_local_lock_state: UNLOCK end of the "
15943 15943 "lost lock region [%"PRIx64" - %"PRIx64"]",
15944 15944 cur_start, lost_flp->l_start + lost_flp->l_len));
15945 15945
15946 15946 ul_fl.l_start = cur_start;
15947 15947 /*
15948 15948 * Is it an to-EOF lock? if so unlock till the end
15949 15949 */
15950 15950 if (lost_flp->l_len == 0)
15951 15951 ul_fl.l_len = 0;
15952 15952 else
15953 15953 ul_fl.l_len = start_check(lost_flp_end) - cur_start;
15954 15954
15955 15955 push_reinstate(vp, cmd, &ul_fl, cr, lop);
15956 15956 }
15957 15957
15958 15958 if (locks != NULL)
15959 15959 flk_free_locklist(locks);
15960 15960
15961 15961 /* Free up our newly created locklist */
15962 15962 for (llp = ri_llp; llp != NULL; ) {
15963 15963 tmp_llp = llp->ll_next;
15964 15964 kmem_free(llp, sizeof (locklist_t));
15965 15965 llp = tmp_llp;
15966 15966 }
15967 15967
15968 15968 /*
15969 15969 * Now return back to the original calling nfs4frlock()
15970 15970 * and let us naturally drop our seqid syncs.
15971 15971 */
15972 15972 }
15973 15973
15974 15974 /*
15975 15975 * Create a lost state record for the given lock reinstantiation request
15976 15976 * and push it onto the lost state queue.
15977 15977 */
15978 15978 static void
15979 15979 push_reinstate(vnode_t *vp, int cmd, flock64_t *flk, cred_t *cr,
15980 15980 nfs4_lock_owner_t *lop)
15981 15981 {
15982 15982 nfs4_lost_rqst_t req;
15983 15983 nfs_lock_type4 locktype;
15984 15984 nfs4_error_t e = { EINTR, NFS4_OK, RPC_SUCCESS };
15985 15985
15986 15986 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
15987 15987
15988 15988 locktype = flk_to_locktype(cmd, flk->l_type);
15989 15989 nfs4frlock_save_lost_rqst(NFS4_LCK_CTYPE_REINSTATE, EINTR, locktype,
15990 15990 NULL, NULL, lop, flk, &req, cr, vp);
15991 15991 (void) nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL,
15992 15992 (req.lr_op == OP_LOCK || req.lr_op == OP_LOCKU) ?
15993 15993 &req : NULL, flk->l_type == F_UNLCK ? OP_LOCKU : OP_LOCK,
15994 15994 NULL, NULL, NULL);
15995 15995 }
↓ open down ↓ |
4924 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX