Print this page
re #13613 rb4516 Tunables needs volatile keyword
| Split |
Close |
| Expand all |
| Collapse all |
--- old/usr/src/uts/common/fs/nfs/nfs3_vnops.c
+++ new/usr/src/uts/common/fs/nfs/nfs3_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.
|
↓ open down ↓ |
14 lines elided |
↑ open up ↑ |
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 +/*
26 + * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
27 + */
25 28
26 29 /*
27 30 * Copyright (c) 1983,1984,1985,1986,1987,1988,1989 AT&T.
28 31 * All rights reserved.
29 32 */
30 33
31 34 /*
32 35 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
33 36 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
34 37 */
35 38
36 39 #include <sys/param.h>
37 40 #include <sys/types.h>
38 41 #include <sys/systm.h>
39 42 #include <sys/cred.h>
40 43 #include <sys/time.h>
41 44 #include <sys/vnode.h>
42 45 #include <sys/vfs.h>
43 46 #include <sys/vfs_opreg.h>
44 47 #include <sys/file.h>
45 48 #include <sys/filio.h>
46 49 #include <sys/uio.h>
47 50 #include <sys/buf.h>
48 51 #include <sys/mman.h>
49 52 #include <sys/pathname.h>
50 53 #include <sys/dirent.h>
51 54 #include <sys/debug.h>
52 55 #include <sys/vmsystm.h>
53 56 #include <sys/fcntl.h>
54 57 #include <sys/flock.h>
55 58 #include <sys/swap.h>
56 59 #include <sys/errno.h>
57 60 #include <sys/strsubr.h>
58 61 #include <sys/sysmacros.h>
59 62 #include <sys/kmem.h>
60 63 #include <sys/cmn_err.h>
61 64 #include <sys/pathconf.h>
62 65 #include <sys/utsname.h>
63 66 #include <sys/dnlc.h>
64 67 #include <sys/acl.h>
65 68 #include <sys/systeminfo.h>
66 69 #include <sys/atomic.h>
67 70 #include <sys/policy.h>
68 71 #include <sys/sdt.h>
69 72 #include <sys/zone.h>
70 73
71 74 #include <rpc/types.h>
72 75 #include <rpc/auth.h>
73 76 #include <rpc/clnt.h>
74 77 #include <rpc/rpc_rdma.h>
75 78
76 79 #include <nfs/nfs.h>
77 80 #include <nfs/nfs_clnt.h>
78 81 #include <nfs/rnode.h>
79 82 #include <nfs/nfs_acl.h>
80 83 #include <nfs/lm.h>
81 84
82 85 #include <vm/hat.h>
83 86 #include <vm/as.h>
84 87 #include <vm/page.h>
85 88 #include <vm/pvn.h>
86 89 #include <vm/seg.h>
87 90 #include <vm/seg_map.h>
88 91 #include <vm/seg_kpm.h>
89 92 #include <vm/seg_vn.h>
90 93
91 94 #include <fs/fs_subr.h>
92 95
93 96 #include <sys/ddi.h>
94 97
95 98 static int nfs3_rdwrlbn(vnode_t *, page_t *, u_offset_t, size_t, int,
96 99 cred_t *);
97 100 static int nfs3write(vnode_t *, caddr_t, u_offset_t, int, cred_t *,
98 101 stable_how *);
99 102 static int nfs3read(vnode_t *, caddr_t, offset_t, int, size_t *, cred_t *);
100 103 static int nfs3setattr(vnode_t *, struct vattr *, int, cred_t *);
101 104 static int nfs3_accessx(void *, int, cred_t *);
102 105 static int nfs3lookup_dnlc(vnode_t *, char *, vnode_t **, cred_t *);
103 106 static int nfs3lookup_otw(vnode_t *, char *, vnode_t **, cred_t *, int);
104 107 static int nfs3create(vnode_t *, char *, struct vattr *, enum vcexcl,
105 108 int, vnode_t **, cred_t *, int);
106 109 static int nfs3excl_create_settimes(vnode_t *, struct vattr *, cred_t *);
107 110 static int nfs3mknod(vnode_t *, char *, struct vattr *, enum vcexcl,
108 111 int, vnode_t **, cred_t *);
109 112 static int nfs3rename(vnode_t *, char *, vnode_t *, char *, cred_t *,
110 113 caller_context_t *);
111 114 static int do_nfs3readdir(vnode_t *, rddir_cache *, cred_t *);
112 115 static void nfs3readdir(vnode_t *, rddir_cache *, cred_t *);
113 116 static void nfs3readdirplus(vnode_t *, rddir_cache *, cred_t *);
114 117 static int nfs3_bio(struct buf *, stable_how *, cred_t *);
115 118 static int nfs3_getapage(vnode_t *, u_offset_t, size_t, uint_t *,
116 119 page_t *[], size_t, struct seg *, caddr_t,
117 120 enum seg_rw, cred_t *);
118 121 static void nfs3_readahead(vnode_t *, u_offset_t, caddr_t, struct seg *,
119 122 cred_t *);
120 123 static int nfs3_sync_putapage(vnode_t *, page_t *, u_offset_t, size_t,
121 124 int, cred_t *);
122 125 static int nfs3_sync_pageio(vnode_t *, page_t *, u_offset_t, size_t,
123 126 int, cred_t *);
124 127 static int nfs3_commit(vnode_t *, offset3, count3, cred_t *);
125 128 static void nfs3_set_mod(vnode_t *);
126 129 static void nfs3_get_commit(vnode_t *);
127 130 static void nfs3_get_commit_range(vnode_t *, u_offset_t, size_t);
128 131 static int nfs3_putpage_commit(vnode_t *, offset_t, size_t, cred_t *);
129 132 static int nfs3_commit_vp(vnode_t *, u_offset_t, size_t, cred_t *);
130 133 static int nfs3_sync_commit(vnode_t *, page_t *, offset3, count3,
131 134 cred_t *);
132 135 static void nfs3_async_commit(vnode_t *, page_t *, offset3, count3,
133 136 cred_t *);
134 137 static void nfs3_delmap_callback(struct as *, void *, uint_t);
135 138
136 139 /*
137 140 * Error flags used to pass information about certain special errors
138 141 * which need to be handled specially.
139 142 */
140 143 #define NFS_EOF -98
141 144 #define NFS_VERF_MISMATCH -97
142 145
143 146 /* ALIGN64 aligns the given buffer and adjust buffer size to 64 bit */
144 147 #define ALIGN64(x, ptr, sz) \
145 148 x = ((uintptr_t)(ptr)) & (sizeof (uint64_t) - 1); \
146 149 if (x) { \
147 150 x = sizeof (uint64_t) - (x); \
148 151 sz -= (x); \
149 152 ptr += (x); \
150 153 }
151 154
152 155 /*
153 156 * These are the vnode ops routines which implement the vnode interface to
154 157 * the networked file system. These routines just take their parameters,
155 158 * make them look networkish by putting the right info into interface structs,
156 159 * and then calling the appropriate remote routine(s) to do the work.
157 160 *
158 161 * Note on directory name lookup cacheing: If we detect a stale fhandle,
159 162 * we purge the directory cache relative to that vnode. This way, the
160 163 * user won't get burned by the cache repeatedly. See <nfs/rnode.h> for
161 164 * more details on rnode locking.
162 165 */
163 166
164 167 static int nfs3_open(vnode_t **, int, cred_t *, caller_context_t *);
165 168 static int nfs3_close(vnode_t *, int, int, offset_t, cred_t *,
166 169 caller_context_t *);
167 170 static int nfs3_read(vnode_t *, struct uio *, int, cred_t *,
168 171 caller_context_t *);
169 172 static int nfs3_write(vnode_t *, struct uio *, int, cred_t *,
170 173 caller_context_t *);
171 174 static int nfs3_ioctl(vnode_t *, int, intptr_t, int, cred_t *, int *,
172 175 caller_context_t *);
173 176 static int nfs3_getattr(vnode_t *, struct vattr *, int, cred_t *,
174 177 caller_context_t *);
175 178 static int nfs3_setattr(vnode_t *, struct vattr *, int, cred_t *,
176 179 caller_context_t *);
177 180 static int nfs3_access(vnode_t *, int, int, cred_t *, caller_context_t *);
178 181 static int nfs3_readlink(vnode_t *, struct uio *, cred_t *,
179 182 caller_context_t *);
180 183 static int nfs3_fsync(vnode_t *, int, cred_t *, caller_context_t *);
181 184 static void nfs3_inactive(vnode_t *, cred_t *, caller_context_t *);
182 185 static int nfs3_lookup(vnode_t *, char *, vnode_t **,
183 186 struct pathname *, int, vnode_t *, cred_t *,
184 187 caller_context_t *, int *, pathname_t *);
185 188 static int nfs3_create(vnode_t *, char *, struct vattr *, enum vcexcl,
186 189 int, vnode_t **, cred_t *, int, caller_context_t *,
187 190 vsecattr_t *);
188 191 static int nfs3_remove(vnode_t *, char *, cred_t *, caller_context_t *,
189 192 int);
190 193 static int nfs3_link(vnode_t *, vnode_t *, char *, cred_t *,
191 194 caller_context_t *, int);
192 195 static int nfs3_rename(vnode_t *, char *, vnode_t *, char *, cred_t *,
193 196 caller_context_t *, int);
194 197 static int nfs3_mkdir(vnode_t *, char *, struct vattr *, vnode_t **,
195 198 cred_t *, caller_context_t *, int, vsecattr_t *);
196 199 static int nfs3_rmdir(vnode_t *, char *, vnode_t *, cred_t *,
197 200 caller_context_t *, int);
198 201 static int nfs3_symlink(vnode_t *, char *, struct vattr *, char *,
199 202 cred_t *, caller_context_t *, int);
200 203 static int nfs3_readdir(vnode_t *, struct uio *, cred_t *, int *,
201 204 caller_context_t *, int);
202 205 static int nfs3_fid(vnode_t *, fid_t *, caller_context_t *);
203 206 static int nfs3_rwlock(vnode_t *, int, caller_context_t *);
204 207 static void nfs3_rwunlock(vnode_t *, int, caller_context_t *);
205 208 static int nfs3_seek(vnode_t *, offset_t, offset_t *, caller_context_t *);
206 209 static int nfs3_getpage(vnode_t *, offset_t, size_t, uint_t *,
207 210 page_t *[], size_t, struct seg *, caddr_t,
208 211 enum seg_rw, cred_t *, caller_context_t *);
209 212 static int nfs3_putpage(vnode_t *, offset_t, size_t, int, cred_t *,
210 213 caller_context_t *);
211 214 static int nfs3_map(vnode_t *, offset_t, struct as *, caddr_t *, size_t,
212 215 uchar_t, uchar_t, uint_t, cred_t *, caller_context_t *);
213 216 static int nfs3_addmap(vnode_t *, offset_t, struct as *, caddr_t, size_t,
214 217 uchar_t, uchar_t, uint_t, cred_t *, caller_context_t *);
215 218 static int nfs3_frlock(vnode_t *, int, struct flock64 *, int, offset_t,
216 219 struct flk_callback *, cred_t *, caller_context_t *);
217 220 static int nfs3_space(vnode_t *, int, struct flock64 *, int, offset_t,
218 221 cred_t *, caller_context_t *);
219 222 static int nfs3_realvp(vnode_t *, vnode_t **, caller_context_t *);
220 223 static int nfs3_delmap(vnode_t *, offset_t, struct as *, caddr_t, size_t,
221 224 uint_t, uint_t, uint_t, cred_t *, caller_context_t *);
222 225 static int nfs3_pathconf(vnode_t *, int, ulong_t *, cred_t *,
223 226 caller_context_t *);
224 227 static int nfs3_pageio(vnode_t *, page_t *, u_offset_t, size_t, int,
225 228 cred_t *, caller_context_t *);
226 229 static void nfs3_dispose(vnode_t *, page_t *, int, int, cred_t *,
227 230 caller_context_t *);
228 231 static int nfs3_setsecattr(vnode_t *, vsecattr_t *, int, cred_t *,
229 232 caller_context_t *);
230 233 static int nfs3_getsecattr(vnode_t *, vsecattr_t *, int, cred_t *,
231 234 caller_context_t *);
232 235 static int nfs3_shrlock(vnode_t *, int, struct shrlock *, int, cred_t *,
233 236 caller_context_t *);
234 237
235 238 struct vnodeops *nfs3_vnodeops;
236 239
237 240 const fs_operation_def_t nfs3_vnodeops_template[] = {
238 241 VOPNAME_OPEN, { .vop_open = nfs3_open },
239 242 VOPNAME_CLOSE, { .vop_close = nfs3_close },
240 243 VOPNAME_READ, { .vop_read = nfs3_read },
241 244 VOPNAME_WRITE, { .vop_write = nfs3_write },
242 245 VOPNAME_IOCTL, { .vop_ioctl = nfs3_ioctl },
243 246 VOPNAME_GETATTR, { .vop_getattr = nfs3_getattr },
244 247 VOPNAME_SETATTR, { .vop_setattr = nfs3_setattr },
245 248 VOPNAME_ACCESS, { .vop_access = nfs3_access },
246 249 VOPNAME_LOOKUP, { .vop_lookup = nfs3_lookup },
247 250 VOPNAME_CREATE, { .vop_create = nfs3_create },
248 251 VOPNAME_REMOVE, { .vop_remove = nfs3_remove },
249 252 VOPNAME_LINK, { .vop_link = nfs3_link },
250 253 VOPNAME_RENAME, { .vop_rename = nfs3_rename },
251 254 VOPNAME_MKDIR, { .vop_mkdir = nfs3_mkdir },
252 255 VOPNAME_RMDIR, { .vop_rmdir = nfs3_rmdir },
253 256 VOPNAME_READDIR, { .vop_readdir = nfs3_readdir },
254 257 VOPNAME_SYMLINK, { .vop_symlink = nfs3_symlink },
255 258 VOPNAME_READLINK, { .vop_readlink = nfs3_readlink },
256 259 VOPNAME_FSYNC, { .vop_fsync = nfs3_fsync },
257 260 VOPNAME_INACTIVE, { .vop_inactive = nfs3_inactive },
258 261 VOPNAME_FID, { .vop_fid = nfs3_fid },
259 262 VOPNAME_RWLOCK, { .vop_rwlock = nfs3_rwlock },
260 263 VOPNAME_RWUNLOCK, { .vop_rwunlock = nfs3_rwunlock },
261 264 VOPNAME_SEEK, { .vop_seek = nfs3_seek },
262 265 VOPNAME_FRLOCK, { .vop_frlock = nfs3_frlock },
263 266 VOPNAME_SPACE, { .vop_space = nfs3_space },
264 267 VOPNAME_REALVP, { .vop_realvp = nfs3_realvp },
265 268 VOPNAME_GETPAGE, { .vop_getpage = nfs3_getpage },
266 269 VOPNAME_PUTPAGE, { .vop_putpage = nfs3_putpage },
267 270 VOPNAME_MAP, { .vop_map = nfs3_map },
268 271 VOPNAME_ADDMAP, { .vop_addmap = nfs3_addmap },
269 272 VOPNAME_DELMAP, { .vop_delmap = nfs3_delmap },
270 273 /* no separate nfs3_dump */
271 274 VOPNAME_DUMP, { .vop_dump = nfs_dump },
272 275 VOPNAME_PATHCONF, { .vop_pathconf = nfs3_pathconf },
273 276 VOPNAME_PAGEIO, { .vop_pageio = nfs3_pageio },
274 277 VOPNAME_DISPOSE, { .vop_dispose = nfs3_dispose },
275 278 VOPNAME_SETSECATTR, { .vop_setsecattr = nfs3_setsecattr },
276 279 VOPNAME_GETSECATTR, { .vop_getsecattr = nfs3_getsecattr },
277 280 VOPNAME_SHRLOCK, { .vop_shrlock = nfs3_shrlock },
278 281 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
279 282 NULL, NULL
280 283 };
281 284
282 285 /*
283 286 * XXX: This is referenced in modstubs.s
284 287 */
285 288 struct vnodeops *
286 289 nfs3_getvnodeops(void)
287 290 {
288 291 return (nfs3_vnodeops);
289 292 }
290 293
291 294 /* ARGSUSED */
292 295 static int
293 296 nfs3_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
294 297 {
295 298 int error;
296 299 struct vattr va;
297 300 rnode_t *rp;
298 301 vnode_t *vp;
299 302
300 303 vp = *vpp;
301 304 if (nfs_zone() != VTOMI(vp)->mi_zone)
302 305 return (EIO);
303 306 rp = VTOR(vp);
304 307 mutex_enter(&rp->r_statelock);
305 308 if (rp->r_cred == NULL) {
306 309 crhold(cr);
307 310 rp->r_cred = cr;
308 311 }
309 312 mutex_exit(&rp->r_statelock);
310 313
311 314 /*
312 315 * If there is no cached data or if close-to-open
313 316 * consistency checking is turned off, we can avoid
314 317 * the over the wire getattr. Otherwise, if the
315 318 * file system is mounted readonly, then just verify
316 319 * the caches are up to date using the normal mechanism.
317 320 * Else, if the file is not mmap'd, then just mark
318 321 * the attributes as timed out. They will be refreshed
319 322 * and the caches validated prior to being used.
320 323 * Else, the file system is mounted writeable so
321 324 * force an over the wire GETATTR in order to ensure
322 325 * that all cached data is valid.
323 326 */
324 327 if (vp->v_count > 1 ||
325 328 ((vn_has_cached_data(vp) || HAVE_RDDIR_CACHE(rp)) &&
326 329 !(VTOMI(vp)->mi_flags & MI_NOCTO))) {
327 330 if (vn_is_readonly(vp))
328 331 error = nfs3_validate_caches(vp, cr);
329 332 else if (rp->r_mapcnt == 0 && vp->v_count == 1) {
330 333 PURGE_ATTRCACHE(vp);
331 334 error = 0;
332 335 } else {
333 336 va.va_mask = AT_ALL;
334 337 error = nfs3_getattr_otw(vp, &va, cr);
335 338 }
336 339 } else
337 340 error = 0;
338 341
339 342 return (error);
340 343 }
341 344
342 345 /* ARGSUSED */
343 346 static int
344 347 nfs3_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr,
345 348 caller_context_t *ct)
346 349 {
347 350 rnode_t *rp;
348 351 int error;
349 352 struct vattr va;
350 353
351 354 /*
352 355 * zone_enter(2) prevents processes from changing zones with NFS files
353 356 * open; if we happen to get here from the wrong zone we can't do
354 357 * anything over the wire.
355 358 */
356 359 if (VTOMI(vp)->mi_zone != nfs_zone()) {
357 360 /*
358 361 * We could attempt to clean up locks, except we're sure
359 362 * that the current process didn't acquire any locks on
360 363 * the file: any attempt to lock a file belong to another zone
361 364 * will fail, and one can't lock an NFS file and then change
362 365 * zones, as that fails too.
363 366 *
364 367 * Returning an error here is the sane thing to do. A
365 368 * subsequent call to VN_RELE() which translates to a
366 369 * nfs3_inactive() will clean up state: if the zone of the
367 370 * vnode's origin is still alive and kicking, an async worker
368 371 * thread will handle the request (from the correct zone), and
369 372 * everything (minus the commit and final nfs3_getattr_otw()
370 373 * call) should be OK. If the zone is going away
371 374 * nfs_async_inactive() will throw away cached pages inline.
372 375 */
373 376 return (EIO);
374 377 }
375 378
376 379 /*
377 380 * If we are using local locking for this filesystem, then
378 381 * release all of the SYSV style record locks. Otherwise,
379 382 * we are doing network locking and we need to release all
380 383 * of the network locks. All of the locks held by this
381 384 * process on this file are released no matter what the
382 385 * incoming reference count is.
383 386 */
384 387 if (VTOMI(vp)->mi_flags & MI_LLOCK) {
385 388 cleanlocks(vp, ttoproc(curthread)->p_pid, 0);
386 389 cleanshares(vp, ttoproc(curthread)->p_pid);
387 390 } else
388 391 nfs_lockrelease(vp, flag, offset, cr);
389 392
390 393 if (count > 1)
391 394 return (0);
392 395
393 396 /*
394 397 * If the file has been `unlinked', then purge the
395 398 * DNLC so that this vnode will get reycled quicker
396 399 * and the .nfs* file on the server will get removed.
397 400 */
398 401 rp = VTOR(vp);
399 402 if (rp->r_unldvp != NULL)
400 403 dnlc_purge_vp(vp);
401 404
402 405 /*
403 406 * If the file was open for write and there are pages,
404 407 * then if the file system was mounted using the "no-close-
405 408 * to-open" semantics, then start an asynchronous flush
406 409 * of the all of the pages in the file.
407 410 * else the file system was not mounted using the "no-close-
408 411 * to-open" semantics, then do a synchronous flush and
409 412 * commit of all of the dirty and uncommitted pages.
410 413 *
411 414 * The asynchronous flush of the pages in the "nocto" path
412 415 * mostly just associates a cred pointer with the rnode so
413 416 * writes which happen later will have a better chance of
414 417 * working. It also starts the data being written to the
415 418 * server, but without unnecessarily delaying the application.
416 419 */
417 420 if ((flag & FWRITE) && vn_has_cached_data(vp)) {
418 421 if (VTOMI(vp)->mi_flags & MI_NOCTO) {
419 422 error = nfs3_putpage(vp, (offset_t)0, 0, B_ASYNC,
420 423 cr, ct);
421 424 if (error == EAGAIN)
422 425 error = 0;
423 426 } else
424 427 error = nfs3_putpage_commit(vp, (offset_t)0, 0, cr);
425 428 if (!error) {
426 429 mutex_enter(&rp->r_statelock);
427 430 error = rp->r_error;
428 431 rp->r_error = 0;
429 432 mutex_exit(&rp->r_statelock);
430 433 }
431 434 } else {
432 435 mutex_enter(&rp->r_statelock);
433 436 error = rp->r_error;
434 437 rp->r_error = 0;
435 438 mutex_exit(&rp->r_statelock);
436 439 }
437 440
438 441 /*
439 442 * If RWRITEATTR is set, then issue an over the wire GETATTR to
440 443 * refresh the attribute cache with a set of attributes which
441 444 * weren't returned from a WRITE. This will enable the close-
442 445 * to-open processing to work.
443 446 */
444 447 if (rp->r_flags & RWRITEATTR)
445 448 (void) nfs3_getattr_otw(vp, &va, cr);
446 449
447 450 return (error);
448 451 }
449 452
450 453 /* ARGSUSED */
451 454 static int
452 455 nfs3_directio_read(vnode_t *vp, struct uio *uiop, cred_t *cr)
453 456 {
454 457 mntinfo_t *mi;
455 458 READ3args args;
456 459 READ3uiores res;
457 460 int tsize;
458 461 offset_t offset;
459 462 ssize_t count;
460 463 int error;
461 464 int douprintf;
462 465 failinfo_t fi;
463 466 char *sv_hostname;
464 467
465 468 mi = VTOMI(vp);
466 469 ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
467 470 sv_hostname = VTOR(vp)->r_server->sv_hostname;
468 471
469 472 douprintf = 1;
470 473 args.file = *VTOFH3(vp);
471 474 fi.vp = vp;
472 475 fi.fhp = (caddr_t)&args.file;
473 476 fi.copyproc = nfs3copyfh;
474 477 fi.lookupproc = nfs3lookup;
475 478 fi.xattrdirproc = acl_getxattrdir3;
476 479
477 480 res.uiop = uiop;
478 481
479 482 res.wlist = NULL;
480 483
481 484 offset = uiop->uio_loffset;
482 485 count = uiop->uio_resid;
483 486
484 487 do {
485 488 if (mi->mi_io_kstats) {
486 489 mutex_enter(&mi->mi_lock);
487 490 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
488 491 mutex_exit(&mi->mi_lock);
489 492 }
490 493
491 494 do {
492 495 tsize = MIN(mi->mi_tsize, count);
493 496 args.offset = (offset3)offset;
494 497 args.count = (count3)tsize;
495 498 res.size = (uint_t)tsize;
496 499 args.res_uiop = uiop;
497 500 args.res_data_val_alt = NULL;
498 501
499 502 error = rfs3call(mi, NFSPROC3_READ,
500 503 xdr_READ3args, (caddr_t)&args,
501 504 xdr_READ3uiores, (caddr_t)&res, cr,
502 505 &douprintf, &res.status, 0, &fi);
503 506 } while (error == ENFS_TRYAGAIN);
504 507
505 508 if (mi->mi_io_kstats) {
506 509 mutex_enter(&mi->mi_lock);
507 510 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
508 511 mutex_exit(&mi->mi_lock);
509 512 }
510 513
511 514 if (error)
512 515 return (error);
513 516
514 517 error = geterrno3(res.status);
515 518 if (error)
516 519 return (error);
517 520
518 521 if (res.count != res.size) {
519 522 zcmn_err(getzoneid(), CE_WARN,
520 523 "nfs3_directio_read: server %s returned incorrect amount",
521 524 sv_hostname);
522 525 return (EIO);
523 526 }
524 527 count -= res.count;
525 528 offset += res.count;
526 529 if (mi->mi_io_kstats) {
527 530 mutex_enter(&mi->mi_lock);
528 531 KSTAT_IO_PTR(mi->mi_io_kstats)->reads++;
529 532 KSTAT_IO_PTR(mi->mi_io_kstats)->nread += res.count;
530 533 mutex_exit(&mi->mi_lock);
531 534 }
532 535 lwp_stat_update(LWP_STAT_INBLK, 1);
533 536 } while (count && !res.eof);
534 537
535 538 return (0);
536 539 }
537 540
538 541 /* ARGSUSED */
539 542 static int
540 543 nfs3_read(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr,
541 544 caller_context_t *ct)
542 545 {
543 546 rnode_t *rp;
544 547 u_offset_t off;
545 548 offset_t diff;
546 549 int on;
547 550 size_t n;
548 551 caddr_t base;
549 552 uint_t flags;
550 553 int error = 0;
551 554 mntinfo_t *mi;
552 555
553 556 rp = VTOR(vp);
554 557 mi = VTOMI(vp);
555 558
556 559 ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER));
557 560
558 561 if (nfs_zone() != mi->mi_zone)
559 562 return (EIO);
560 563
561 564 if (vp->v_type != VREG)
562 565 return (EISDIR);
563 566
564 567 if (uiop->uio_resid == 0)
565 568 return (0);
566 569
567 570 if (uiop->uio_loffset < 0 || uiop->uio_loffset + uiop->uio_resid < 0)
568 571 return (EINVAL);
569 572
570 573 /*
571 574 * Bypass VM if caching has been disabled (e.g., locking) or if
572 575 * using client-side direct I/O and the file is not mmap'd and
573 576 * there are no cached pages.
574 577 */
575 578 if ((vp->v_flag & VNOCACHE) ||
576 579 (((rp->r_flags & RDIRECTIO) || (mi->mi_flags & MI_DIRECTIO)) &&
577 580 rp->r_mapcnt == 0 && rp->r_inmap == 0 &&
578 581 !vn_has_cached_data(vp))) {
579 582 return (nfs3_directio_read(vp, uiop, cr));
580 583 }
581 584
582 585 do {
583 586 off = uiop->uio_loffset & MAXBMASK; /* mapping offset */
584 587 on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */
585 588 n = MIN(MAXBSIZE - on, uiop->uio_resid);
586 589
587 590 error = nfs3_validate_caches(vp, cr);
588 591 if (error)
589 592 break;
590 593
591 594 mutex_enter(&rp->r_statelock);
592 595 while (rp->r_flags & RINCACHEPURGE) {
593 596 if (!cv_wait_sig(&rp->r_cv, &rp->r_statelock)) {
594 597 mutex_exit(&rp->r_statelock);
595 598 return (EINTR);
596 599 }
597 600 }
598 601 diff = rp->r_size - uiop->uio_loffset;
599 602 mutex_exit(&rp->r_statelock);
600 603 if (diff <= 0)
601 604 break;
602 605 if (diff < n)
603 606 n = (size_t)diff;
604 607
605 608 if (vpm_enable) {
606 609 /*
607 610 * Copy data.
608 611 */
609 612 error = vpm_data_copy(vp, off + on, n, uiop,
610 613 1, NULL, 0, S_READ);
611 614 } else {
612 615 base = segmap_getmapflt(segkmap, vp, off + on, n, 1,
613 616 S_READ);
614 617
615 618 error = uiomove(base + on, n, UIO_READ, uiop);
616 619 }
617 620
618 621 if (!error) {
619 622 /*
620 623 * If read a whole block or read to eof,
621 624 * won't need this buffer again soon.
622 625 */
623 626 mutex_enter(&rp->r_statelock);
624 627 if (n + on == MAXBSIZE ||
625 628 uiop->uio_loffset == rp->r_size)
626 629 flags = SM_DONTNEED;
627 630 else
628 631 flags = 0;
629 632 mutex_exit(&rp->r_statelock);
630 633 if (vpm_enable) {
631 634 error = vpm_sync_pages(vp, off, n, flags);
632 635 } else {
633 636 error = segmap_release(segkmap, base, flags);
634 637 }
635 638 } else {
636 639 if (vpm_enable) {
637 640 (void) vpm_sync_pages(vp, off, n, 0);
638 641 } else {
639 642 (void) segmap_release(segkmap, base, 0);
640 643 }
641 644 }
642 645 } while (!error && uiop->uio_resid > 0);
643 646
644 647 return (error);
645 648 }
646 649
647 650 /* ARGSUSED */
648 651 static int
649 652 nfs3_write(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr,
650 653 caller_context_t *ct)
651 654 {
652 655 rlim64_t limit = uiop->uio_llimit;
653 656 rnode_t *rp;
654 657 u_offset_t off;
655 658 caddr_t base;
656 659 uint_t flags;
657 660 int remainder;
658 661 size_t n;
659 662 int on;
660 663 int error;
661 664 int resid;
662 665 offset_t offset;
663 666 mntinfo_t *mi;
664 667 uint_t bsize;
665 668
666 669 rp = VTOR(vp);
667 670
668 671 if (vp->v_type != VREG)
669 672 return (EISDIR);
670 673
671 674 mi = VTOMI(vp);
672 675 if (nfs_zone() != mi->mi_zone)
673 676 return (EIO);
674 677 if (uiop->uio_resid == 0)
675 678 return (0);
676 679
677 680 if (ioflag & FAPPEND) {
678 681 struct vattr va;
679 682
680 683 /*
681 684 * Must serialize if appending.
682 685 */
683 686 if (nfs_rw_lock_held(&rp->r_rwlock, RW_READER)) {
684 687 nfs_rw_exit(&rp->r_rwlock);
685 688 if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER,
686 689 INTR(vp)))
687 690 return (EINTR);
688 691 }
689 692
690 693 va.va_mask = AT_SIZE;
691 694 error = nfs3getattr(vp, &va, cr);
692 695 if (error)
693 696 return (error);
694 697 uiop->uio_loffset = va.va_size;
695 698 }
696 699
697 700 offset = uiop->uio_loffset + uiop->uio_resid;
698 701
699 702 if (uiop->uio_loffset < 0 || offset < 0)
700 703 return (EINVAL);
701 704
702 705 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
703 706 limit = MAXOFFSET_T;
704 707
705 708 /*
706 709 * Check to make sure that the process will not exceed
707 710 * its limit on file size. It is okay to write up to
708 711 * the limit, but not beyond. Thus, the write which
709 712 * reaches the limit will be short and the next write
710 713 * will return an error.
711 714 */
712 715 remainder = 0;
713 716 if (offset > limit) {
714 717 remainder = offset - limit;
715 718 uiop->uio_resid = limit - uiop->uio_loffset;
716 719 if (uiop->uio_resid <= 0) {
717 720 proc_t *p = ttoproc(curthread);
718 721
719 722 uiop->uio_resid += remainder;
720 723 mutex_enter(&p->p_lock);
721 724 (void) rctl_action(rctlproc_legacy[RLIMIT_FSIZE],
722 725 p->p_rctls, p, RCA_UNSAFE_SIGINFO);
723 726 mutex_exit(&p->p_lock);
724 727 return (EFBIG);
725 728 }
726 729 }
727 730
728 731 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR(vp)))
729 732 return (EINTR);
730 733
731 734 /*
732 735 * Bypass VM if caching has been disabled (e.g., locking) or if
733 736 * using client-side direct I/O and the file is not mmap'd and
734 737 * there are no cached pages.
735 738 */
736 739 if ((vp->v_flag & VNOCACHE) ||
737 740 (((rp->r_flags & RDIRECTIO) || (mi->mi_flags & MI_DIRECTIO)) &&
738 741 rp->r_mapcnt == 0 && rp->r_inmap == 0 &&
739 742 !vn_has_cached_data(vp))) {
740 743 size_t bufsize;
741 744 int count;
742 745 u_offset_t org_offset;
743 746 stable_how stab_comm;
744 747
745 748 nfs3_fwrite:
746 749 if (rp->r_flags & RSTALE) {
747 750 resid = uiop->uio_resid;
748 751 offset = uiop->uio_loffset;
749 752 error = rp->r_error;
750 753 /*
751 754 * A close may have cleared r_error, if so,
752 755 * propagate ESTALE error return properly
753 756 */
754 757 if (error == 0)
755 758 error = ESTALE;
756 759 goto bottom;
757 760 }
758 761 bufsize = MIN(uiop->uio_resid, mi->mi_stsize);
759 762 base = kmem_alloc(bufsize, KM_SLEEP);
760 763 do {
761 764 if (ioflag & FDSYNC)
762 765 stab_comm = DATA_SYNC;
763 766 else
764 767 stab_comm = FILE_SYNC;
765 768 resid = uiop->uio_resid;
766 769 offset = uiop->uio_loffset;
767 770 count = MIN(uiop->uio_resid, bufsize);
768 771 org_offset = uiop->uio_loffset;
769 772 error = uiomove(base, count, UIO_WRITE, uiop);
770 773 if (!error) {
771 774 error = nfs3write(vp, base, org_offset,
772 775 count, cr, &stab_comm);
773 776 }
774 777 } while (!error && uiop->uio_resid > 0);
775 778 kmem_free(base, bufsize);
776 779 goto bottom;
777 780 }
778 781
779 782
780 783 bsize = vp->v_vfsp->vfs_bsize;
781 784
782 785 do {
783 786 off = uiop->uio_loffset & MAXBMASK; /* mapping offset */
784 787 on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */
785 788 n = MIN(MAXBSIZE - on, uiop->uio_resid);
786 789
787 790 resid = uiop->uio_resid;
788 791 offset = uiop->uio_loffset;
789 792
790 793 if (rp->r_flags & RSTALE) {
791 794 error = rp->r_error;
792 795 /*
793 796 * A close may have cleared r_error, if so,
794 797 * propagate ESTALE error return properly
795 798 */
796 799 if (error == 0)
797 800 error = ESTALE;
798 801 break;
799 802 }
800 803
801 804 /*
802 805 * Don't create dirty pages faster than they
803 806 * can be cleaned so that the system doesn't
804 807 * get imbalanced. If the async queue is
805 808 * maxed out, then wait for it to drain before
806 809 * creating more dirty pages. Also, wait for
807 810 * any threads doing pagewalks in the vop_getattr
808 811 * entry points so that they don't block for
809 812 * long periods.
810 813 */
811 814 mutex_enter(&rp->r_statelock);
812 815 while ((mi->mi_max_threads != 0 &&
813 816 rp->r_awcount > 2 * mi->mi_max_threads) ||
814 817 rp->r_gcount > 0) {
815 818 if (INTR(vp)) {
816 819 klwp_t *lwp = ttolwp(curthread);
817 820
818 821 if (lwp != NULL)
819 822 lwp->lwp_nostop++;
820 823 if (!cv_wait_sig(&rp->r_cv, &rp->r_statelock)) {
821 824 mutex_exit(&rp->r_statelock);
822 825 if (lwp != NULL)
823 826 lwp->lwp_nostop--;
824 827 error = EINTR;
825 828 goto bottom;
826 829 }
827 830 if (lwp != NULL)
828 831 lwp->lwp_nostop--;
829 832 } else
830 833 cv_wait(&rp->r_cv, &rp->r_statelock);
831 834 }
832 835 mutex_exit(&rp->r_statelock);
833 836
834 837 /*
835 838 * Touch the page and fault it in if it is not in core
836 839 * before segmap_getmapflt or vpm_data_copy can lock it.
837 840 * This is to avoid the deadlock if the buffer is mapped
838 841 * to the same file through mmap which we want to write.
839 842 */
840 843 uio_prefaultpages((long)n, uiop);
841 844
842 845 if (vpm_enable) {
843 846 /*
844 847 * It will use kpm mappings, so no need to
845 848 * pass an address.
846 849 */
847 850 error = writerp(rp, NULL, n, uiop, 0);
848 851 } else {
849 852 if (segmap_kpm) {
850 853 int pon = uiop->uio_loffset & PAGEOFFSET;
851 854 size_t pn = MIN(PAGESIZE - pon,
852 855 uiop->uio_resid);
853 856 int pagecreate;
854 857
855 858 mutex_enter(&rp->r_statelock);
856 859 pagecreate = (pon == 0) && (pn == PAGESIZE ||
857 860 uiop->uio_loffset + pn >= rp->r_size);
858 861 mutex_exit(&rp->r_statelock);
859 862
860 863 base = segmap_getmapflt(segkmap, vp, off + on,
861 864 pn, !pagecreate, S_WRITE);
862 865
863 866 error = writerp(rp, base + pon, n, uiop,
864 867 pagecreate);
865 868
866 869 } else {
867 870 base = segmap_getmapflt(segkmap, vp, off + on,
868 871 n, 0, S_READ);
869 872 error = writerp(rp, base + on, n, uiop, 0);
870 873 }
871 874 }
872 875
873 876 if (!error) {
874 877 if (mi->mi_flags & MI_NOAC)
875 878 flags = SM_WRITE;
876 879 else if ((uiop->uio_loffset % bsize) == 0 ||
877 880 IS_SWAPVP(vp)) {
878 881 /*
879 882 * Have written a whole block.
880 883 * Start an asynchronous write
881 884 * and mark the buffer to
882 885 * indicate that it won't be
883 886 * needed again soon.
884 887 */
885 888 flags = SM_WRITE | SM_ASYNC | SM_DONTNEED;
886 889 } else
887 890 flags = 0;
888 891 if ((ioflag & (FSYNC|FDSYNC)) ||
889 892 (rp->r_flags & ROUTOFSPACE)) {
890 893 flags &= ~SM_ASYNC;
891 894 flags |= SM_WRITE;
892 895 }
893 896 if (vpm_enable) {
894 897 error = vpm_sync_pages(vp, off, n, flags);
895 898 } else {
896 899 error = segmap_release(segkmap, base, flags);
897 900 }
898 901 } else {
899 902 if (vpm_enable) {
900 903 (void) vpm_sync_pages(vp, off, n, 0);
901 904 } else {
902 905 (void) segmap_release(segkmap, base, 0);
903 906 }
904 907 /*
905 908 * In the event that we got an access error while
906 909 * faulting in a page for a write-only file just
907 910 * force a write.
908 911 */
909 912 if (error == EACCES)
910 913 goto nfs3_fwrite;
911 914 }
912 915 } while (!error && uiop->uio_resid > 0);
913 916
914 917 bottom:
915 918 if (error) {
916 919 uiop->uio_resid = resid + remainder;
917 920 uiop->uio_loffset = offset;
918 921 } else
919 922 uiop->uio_resid += remainder;
920 923
921 924 nfs_rw_exit(&rp->r_lkserlock);
922 925
923 926 return (error);
924 927 }
925 928
926 929 /*
927 930 * Flags are composed of {B_ASYNC, B_INVAL, B_FREE, B_DONTNEED}
928 931 */
929 932 static int
930 933 nfs3_rdwrlbn(vnode_t *vp, page_t *pp, u_offset_t off, size_t len,
931 934 int flags, cred_t *cr)
932 935 {
933 936 struct buf *bp;
934 937 int error;
935 938 page_t *savepp;
936 939 uchar_t fsdata;
937 940 stable_how stab_comm;
938 941
939 942 ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
940 943 bp = pageio_setup(pp, len, vp, flags);
941 944 ASSERT(bp != NULL);
942 945
943 946 /*
944 947 * pageio_setup should have set b_addr to 0. This
945 948 * is correct since we want to do I/O on a page
946 949 * boundary. bp_mapin will use this addr to calculate
947 950 * an offset, and then set b_addr to the kernel virtual
948 951 * address it allocated for us.
949 952 */
950 953 ASSERT(bp->b_un.b_addr == 0);
951 954
952 955 bp->b_edev = 0;
953 956 bp->b_dev = 0;
954 957 bp->b_lblkno = lbtodb(off);
955 958 bp->b_file = vp;
956 959 bp->b_offset = (offset_t)off;
957 960 bp_mapin(bp);
958 961
959 962 /*
960 963 * Calculate the desired level of stability to write data
961 964 * on the server and then mark all of the pages to reflect
962 965 * this.
963 966 */
964 967 if ((flags & (B_WRITE|B_ASYNC)) == (B_WRITE|B_ASYNC) &&
965 968 freemem > desfree) {
966 969 stab_comm = UNSTABLE;
967 970 fsdata = C_DELAYCOMMIT;
968 971 } else {
969 972 stab_comm = FILE_SYNC;
970 973 fsdata = C_NOCOMMIT;
971 974 }
972 975
973 976 savepp = pp;
974 977 do {
975 978 pp->p_fsdata = fsdata;
976 979 } while ((pp = pp->p_next) != savepp);
977 980
978 981 error = nfs3_bio(bp, &stab_comm, cr);
979 982
980 983 bp_mapout(bp);
981 984 pageio_done(bp);
982 985
983 986 /*
984 987 * If the server wrote pages in a more stable fashion than
985 988 * was requested, then clear all of the marks in the pages
986 989 * indicating that COMMIT operations were required.
987 990 */
988 991 if (stab_comm != UNSTABLE && fsdata == C_DELAYCOMMIT) {
989 992 do {
990 993 pp->p_fsdata = C_NOCOMMIT;
991 994 } while ((pp = pp->p_next) != savepp);
992 995 }
993 996
994 997 return (error);
995 998 }
996 999
997 1000 /*
998 1001 * Write to file. Writes to remote server in largest size
999 1002 * chunks that the server can handle. Write is synchronous.
1000 1003 */
1001 1004 static int
1002 1005 nfs3write(vnode_t *vp, caddr_t base, u_offset_t offset, int count, cred_t *cr,
1003 1006 stable_how *stab_comm)
1004 1007 {
1005 1008 mntinfo_t *mi;
1006 1009 WRITE3args args;
1007 1010 WRITE3res res;
1008 1011 int error;
1009 1012 int tsize;
1010 1013 rnode_t *rp;
1011 1014 int douprintf;
1012 1015
1013 1016 rp = VTOR(vp);
1014 1017 mi = VTOMI(vp);
1015 1018
1016 1019 ASSERT(nfs_zone() == mi->mi_zone);
1017 1020
1018 1021 args.file = *VTOFH3(vp);
1019 1022 args.stable = *stab_comm;
1020 1023
1021 1024 *stab_comm = FILE_SYNC;
1022 1025
1023 1026 douprintf = 1;
1024 1027
1025 1028 do {
1026 1029 if ((vp->v_flag & VNOCACHE) ||
1027 1030 (rp->r_flags & RDIRECTIO) ||
1028 1031 (mi->mi_flags & MI_DIRECTIO))
1029 1032 tsize = MIN(mi->mi_stsize, count);
1030 1033 else
1031 1034 tsize = MIN(mi->mi_curwrite, count);
1032 1035 args.offset = (offset3)offset;
1033 1036 args.count = (count3)tsize;
1034 1037 args.data.data_len = (uint_t)tsize;
1035 1038 args.data.data_val = base;
1036 1039
1037 1040 if (mi->mi_io_kstats) {
1038 1041 mutex_enter(&mi->mi_lock);
1039 1042 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
1040 1043 mutex_exit(&mi->mi_lock);
1041 1044 }
1042 1045 args.mblk = NULL;
1043 1046 do {
1044 1047 error = rfs3call(mi, NFSPROC3_WRITE,
1045 1048 xdr_WRITE3args, (caddr_t)&args,
1046 1049 xdr_WRITE3res, (caddr_t)&res, cr,
1047 1050 &douprintf, &res.status, 0, NULL);
1048 1051 } while (error == ENFS_TRYAGAIN);
1049 1052 if (mi->mi_io_kstats) {
1050 1053 mutex_enter(&mi->mi_lock);
1051 1054 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
1052 1055 mutex_exit(&mi->mi_lock);
1053 1056 }
1054 1057
1055 1058 if (error)
1056 1059 return (error);
1057 1060 error = geterrno3(res.status);
1058 1061 if (!error) {
1059 1062 if (res.resok.count > args.count) {
1060 1063 zcmn_err(getzoneid(), CE_WARN,
1061 1064 "nfs3write: server %s wrote %u, "
1062 1065 "requested was %u",
1063 1066 rp->r_server->sv_hostname,
1064 1067 res.resok.count, args.count);
1065 1068 return (EIO);
1066 1069 }
1067 1070 if (res.resok.committed == UNSTABLE) {
1068 1071 *stab_comm = UNSTABLE;
1069 1072 if (args.stable == DATA_SYNC ||
1070 1073 args.stable == FILE_SYNC) {
1071 1074 zcmn_err(getzoneid(), CE_WARN,
1072 1075 "nfs3write: server %s did not commit to stable storage",
1073 1076 rp->r_server->sv_hostname);
1074 1077 return (EIO);
1075 1078 }
1076 1079 }
1077 1080 tsize = (int)res.resok.count;
1078 1081 count -= tsize;
1079 1082 base += tsize;
1080 1083 offset += tsize;
1081 1084 if (mi->mi_io_kstats) {
1082 1085 mutex_enter(&mi->mi_lock);
1083 1086 KSTAT_IO_PTR(mi->mi_io_kstats)->writes++;
1084 1087 KSTAT_IO_PTR(mi->mi_io_kstats)->nwritten +=
1085 1088 tsize;
1086 1089 mutex_exit(&mi->mi_lock);
1087 1090 }
1088 1091 lwp_stat_update(LWP_STAT_OUBLK, 1);
1089 1092 mutex_enter(&rp->r_statelock);
1090 1093 if (rp->r_flags & RHAVEVERF) {
1091 1094 if (rp->r_verf != res.resok.verf) {
1092 1095 nfs3_set_mod(vp);
1093 1096 rp->r_verf = res.resok.verf;
1094 1097 /*
1095 1098 * If the data was written UNSTABLE,
1096 1099 * then might as well stop because
1097 1100 * the whole block will have to get
1098 1101 * rewritten anyway.
1099 1102 */
1100 1103 if (*stab_comm == UNSTABLE) {
1101 1104 mutex_exit(&rp->r_statelock);
1102 1105 break;
1103 1106 }
1104 1107 }
1105 1108 } else {
1106 1109 rp->r_verf = res.resok.verf;
1107 1110 rp->r_flags |= RHAVEVERF;
1108 1111 }
1109 1112 /*
1110 1113 * Mark the attribute cache as timed out and
1111 1114 * set RWRITEATTR to indicate that the file
1112 1115 * was modified with a WRITE operation and
1113 1116 * that the attributes can not be trusted.
1114 1117 */
1115 1118 PURGE_ATTRCACHE_LOCKED(rp);
1116 1119 rp->r_flags |= RWRITEATTR;
1117 1120 mutex_exit(&rp->r_statelock);
1118 1121 }
1119 1122 } while (!error && count);
1120 1123
1121 1124 return (error);
1122 1125 }
1123 1126
1124 1127 /*
1125 1128 * Read from a file. Reads data in largest chunks our interface can handle.
1126 1129 */
1127 1130 static int
1128 1131 nfs3read(vnode_t *vp, caddr_t base, offset_t offset, int count,
1129 1132 size_t *residp, cred_t *cr)
1130 1133 {
1131 1134 mntinfo_t *mi;
1132 1135 READ3args args;
1133 1136 READ3vres res;
1134 1137 int tsize;
1135 1138 int error;
1136 1139 int douprintf;
1137 1140 failinfo_t fi;
1138 1141 rnode_t *rp;
1139 1142 struct vattr va;
1140 1143 hrtime_t t;
1141 1144
1142 1145 rp = VTOR(vp);
1143 1146 mi = VTOMI(vp);
1144 1147 ASSERT(nfs_zone() == mi->mi_zone);
1145 1148 douprintf = 1;
1146 1149
1147 1150 args.file = *VTOFH3(vp);
1148 1151 fi.vp = vp;
1149 1152 fi.fhp = (caddr_t)&args.file;
1150 1153 fi.copyproc = nfs3copyfh;
1151 1154 fi.lookupproc = nfs3lookup;
1152 1155 fi.xattrdirproc = acl_getxattrdir3;
1153 1156
1154 1157 res.pov.fres.vp = vp;
1155 1158 res.pov.fres.vap = &va;
1156 1159
1157 1160 res.wlist = NULL;
1158 1161 *residp = count;
1159 1162 do {
1160 1163 if (mi->mi_io_kstats) {
1161 1164 mutex_enter(&mi->mi_lock);
1162 1165 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
1163 1166 mutex_exit(&mi->mi_lock);
1164 1167 }
1165 1168
1166 1169 do {
1167 1170 if ((vp->v_flag & VNOCACHE) ||
1168 1171 (rp->r_flags & RDIRECTIO) ||
1169 1172 (mi->mi_flags & MI_DIRECTIO))
1170 1173 tsize = MIN(mi->mi_tsize, count);
1171 1174 else
1172 1175 tsize = MIN(mi->mi_curread, count);
1173 1176 res.data.data_val = base;
1174 1177 res.data.data_len = tsize;
1175 1178 args.offset = (offset3)offset;
1176 1179 args.count = (count3)tsize;
1177 1180 args.res_uiop = NULL;
1178 1181 args.res_data_val_alt = base;
1179 1182
1180 1183 t = gethrtime();
1181 1184 error = rfs3call(mi, NFSPROC3_READ,
1182 1185 xdr_READ3args, (caddr_t)&args,
1183 1186 xdr_READ3vres, (caddr_t)&res, cr,
1184 1187 &douprintf, &res.status, 0, &fi);
1185 1188 } while (error == ENFS_TRYAGAIN);
1186 1189
1187 1190 if (mi->mi_io_kstats) {
1188 1191 mutex_enter(&mi->mi_lock);
1189 1192 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
1190 1193 mutex_exit(&mi->mi_lock);
1191 1194 }
1192 1195
1193 1196 if (error)
1194 1197 return (error);
1195 1198
1196 1199 error = geterrno3(res.status);
1197 1200 if (error)
1198 1201 return (error);
1199 1202
1200 1203 if (res.count != res.data.data_len) {
1201 1204 zcmn_err(getzoneid(), CE_WARN,
1202 1205 "nfs3read: server %s returned incorrect amount",
1203 1206 rp->r_server->sv_hostname);
1204 1207 return (EIO);
1205 1208 }
1206 1209
1207 1210 count -= res.count;
1208 1211 *residp = count;
1209 1212 base += res.count;
1210 1213 offset += res.count;
1211 1214 if (mi->mi_io_kstats) {
1212 1215 mutex_enter(&mi->mi_lock);
1213 1216 KSTAT_IO_PTR(mi->mi_io_kstats)->reads++;
1214 1217 KSTAT_IO_PTR(mi->mi_io_kstats)->nread += res.count;
1215 1218 mutex_exit(&mi->mi_lock);
1216 1219 }
1217 1220 lwp_stat_update(LWP_STAT_INBLK, 1);
1218 1221 } while (count && !res.eof);
1219 1222
1220 1223 if (res.pov.attributes) {
1221 1224 mutex_enter(&rp->r_statelock);
1222 1225 if (!CACHE_VALID(rp, va.va_mtime, va.va_size)) {
1223 1226 mutex_exit(&rp->r_statelock);
1224 1227 PURGE_ATTRCACHE(vp);
1225 1228 } else {
1226 1229 if (rp->r_mtime <= t)
1227 1230 nfs_attrcache_va(vp, &va);
1228 1231 mutex_exit(&rp->r_statelock);
1229 1232 }
1230 1233 }
1231 1234
1232 1235 return (0);
1233 1236 }
1234 1237
1235 1238 /* ARGSUSED */
1236 1239 static int
1237 1240 nfs3_ioctl(vnode_t *vp, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp,
1238 1241 caller_context_t *ct)
1239 1242 {
1240 1243
1241 1244 if (nfs_zone() != VTOMI(vp)->mi_zone)
1242 1245 return (EIO);
1243 1246 switch (cmd) {
1244 1247 case _FIODIRECTIO:
1245 1248 return (nfs_directio(vp, (int)arg, cr));
1246 1249 default:
1247 1250 return (ENOTTY);
1248 1251 }
1249 1252 }
1250 1253
1251 1254 /* ARGSUSED */
1252 1255 static int
1253 1256 nfs3_getattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr,
1254 1257 caller_context_t *ct)
1255 1258 {
1256 1259 int error;
1257 1260 rnode_t *rp;
1258 1261
1259 1262 if (nfs_zone() != VTOMI(vp)->mi_zone)
1260 1263 return (EIO);
1261 1264 /*
1262 1265 * If it has been specified that the return value will
1263 1266 * just be used as a hint, and we are only being asked
1264 1267 * for size, fsid or rdevid, then return the client's
1265 1268 * notion of these values without checking to make sure
1266 1269 * that the attribute cache is up to date.
1267 1270 * The whole point is to avoid an over the wire GETATTR
1268 1271 * call.
1269 1272 */
1270 1273 rp = VTOR(vp);
1271 1274 if (flags & ATTR_HINT) {
1272 1275 if (vap->va_mask ==
1273 1276 (vap->va_mask & (AT_SIZE | AT_FSID | AT_RDEV))) {
1274 1277 mutex_enter(&rp->r_statelock);
1275 1278 if (vap->va_mask | AT_SIZE)
1276 1279 vap->va_size = rp->r_size;
1277 1280 if (vap->va_mask | AT_FSID)
1278 1281 vap->va_fsid = rp->r_attr.va_fsid;
1279 1282 if (vap->va_mask | AT_RDEV)
1280 1283 vap->va_rdev = rp->r_attr.va_rdev;
1281 1284 mutex_exit(&rp->r_statelock);
1282 1285 return (0);
1283 1286 }
1284 1287 }
1285 1288
1286 1289 /*
1287 1290 * Only need to flush pages if asking for the mtime
1288 1291 * and if there any dirty pages or any outstanding
1289 1292 * asynchronous (write) requests for this file.
1290 1293 */
1291 1294 if (vap->va_mask & AT_MTIME) {
1292 1295 if (vn_has_cached_data(vp) &&
1293 1296 ((rp->r_flags & RDIRTY) || rp->r_awcount > 0)) {
1294 1297 mutex_enter(&rp->r_statelock);
1295 1298 rp->r_gcount++;
1296 1299 mutex_exit(&rp->r_statelock);
1297 1300 error = nfs3_putpage(vp, (offset_t)0, 0, 0, cr, ct);
1298 1301 mutex_enter(&rp->r_statelock);
1299 1302 if (error && (error == ENOSPC || error == EDQUOT)) {
1300 1303 if (!rp->r_error)
1301 1304 rp->r_error = error;
1302 1305 }
1303 1306 if (--rp->r_gcount == 0)
1304 1307 cv_broadcast(&rp->r_cv);
1305 1308 mutex_exit(&rp->r_statelock);
1306 1309 }
1307 1310 }
1308 1311
1309 1312 return (nfs3getattr(vp, vap, cr));
1310 1313 }
1311 1314
1312 1315 /*ARGSUSED4*/
1313 1316 static int
1314 1317 nfs3_setattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr,
1315 1318 caller_context_t *ct)
1316 1319 {
1317 1320 int error;
1318 1321 struct vattr va;
1319 1322
1320 1323 if (vap->va_mask & AT_NOSET)
1321 1324 return (EINVAL);
1322 1325 if (nfs_zone() != VTOMI(vp)->mi_zone)
1323 1326 return (EIO);
1324 1327
1325 1328 va.va_mask = AT_UID | AT_MODE;
1326 1329 error = nfs3getattr(vp, &va, cr);
1327 1330 if (error)
1328 1331 return (error);
1329 1332
1330 1333 error = secpolicy_vnode_setattr(cr, vp, vap, &va, flags, nfs3_accessx,
1331 1334 vp);
1332 1335 if (error)
1333 1336 return (error);
1334 1337
1335 1338 error = nfs3setattr(vp, vap, flags, cr);
1336 1339
1337 1340 if (error == 0 && (vap->va_mask & AT_SIZE) && vap->va_size == 0)
1338 1341 vnevent_truncate(vp, ct);
1339 1342
1340 1343 return (error);
1341 1344 }
1342 1345
1343 1346 static int
1344 1347 nfs3setattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr)
1345 1348 {
1346 1349 int error;
1347 1350 uint_t mask;
1348 1351 SETATTR3args args;
1349 1352 SETATTR3res res;
1350 1353 int douprintf;
1351 1354 rnode_t *rp;
1352 1355 struct vattr va;
1353 1356 mode_t omode;
1354 1357 vsecattr_t *vsp;
1355 1358 hrtime_t t;
1356 1359
1357 1360 ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
1358 1361 mask = vap->va_mask;
1359 1362
1360 1363 rp = VTOR(vp);
1361 1364
1362 1365 /*
1363 1366 * Only need to flush pages if there are any pages and
1364 1367 * if the file is marked as dirty in some fashion. The
1365 1368 * file must be flushed so that we can accurately
1366 1369 * determine the size of the file and the cached data
1367 1370 * after the SETATTR returns. A file is considered to
1368 1371 * be dirty if it is either marked with RDIRTY, has
1369 1372 * outstanding i/o's active, or is mmap'd. In this
1370 1373 * last case, we can't tell whether there are dirty
1371 1374 * pages, so we flush just to be sure.
1372 1375 */
1373 1376 if (vn_has_cached_data(vp) &&
1374 1377 ((rp->r_flags & RDIRTY) ||
1375 1378 rp->r_count > 0 ||
1376 1379 rp->r_mapcnt > 0)) {
1377 1380 ASSERT(vp->v_type != VCHR);
1378 1381 error = nfs3_putpage(vp, (offset_t)0, 0, 0, cr, NULL);
1379 1382 if (error && (error == ENOSPC || error == EDQUOT)) {
1380 1383 mutex_enter(&rp->r_statelock);
1381 1384 if (!rp->r_error)
1382 1385 rp->r_error = error;
1383 1386 mutex_exit(&rp->r_statelock);
1384 1387 }
1385 1388 }
1386 1389
1387 1390 args.object = *RTOFH3(rp);
1388 1391 /*
1389 1392 * If the intent is for the server to set the times,
1390 1393 * there is no point in have the mask indicating set mtime or
1391 1394 * atime, because the vap values may be junk, and so result
1392 1395 * in an overflow error. Remove these flags from the vap mask
1393 1396 * before calling in this case, and restore them afterwards.
1394 1397 */
1395 1398 if ((mask & (AT_ATIME | AT_MTIME)) && !(flags & ATTR_UTIME)) {
1396 1399 /* Use server times, so don't set the args time fields */
1397 1400 vap->va_mask &= ~(AT_ATIME | AT_MTIME);
1398 1401 error = vattr_to_sattr3(vap, &args.new_attributes);
1399 1402 vap->va_mask |= (mask & (AT_ATIME | AT_MTIME));
1400 1403 if (mask & AT_ATIME) {
1401 1404 args.new_attributes.atime.set_it = SET_TO_SERVER_TIME;
1402 1405 }
1403 1406 if (mask & AT_MTIME) {
1404 1407 args.new_attributes.mtime.set_it = SET_TO_SERVER_TIME;
1405 1408 }
1406 1409 } else {
1407 1410 /* Either do not set times or use the client specified times */
1408 1411 error = vattr_to_sattr3(vap, &args.new_attributes);
1409 1412 }
1410 1413
1411 1414 if (error) {
1412 1415 /* req time field(s) overflow - return immediately */
1413 1416 return (error);
1414 1417 }
1415 1418
1416 1419 va.va_mask = AT_MODE | AT_CTIME;
1417 1420 error = nfs3getattr(vp, &va, cr);
1418 1421 if (error)
1419 1422 return (error);
1420 1423 omode = va.va_mode;
1421 1424
1422 1425 tryagain:
1423 1426 if (mask & AT_SIZE) {
1424 1427 args.guard.check = TRUE;
1425 1428 args.guard.obj_ctime.seconds = va.va_ctime.tv_sec;
1426 1429 args.guard.obj_ctime.nseconds = va.va_ctime.tv_nsec;
1427 1430 } else
1428 1431 args.guard.check = FALSE;
1429 1432
1430 1433 douprintf = 1;
1431 1434
1432 1435 t = gethrtime();
1433 1436
1434 1437 error = rfs3call(VTOMI(vp), NFSPROC3_SETATTR,
1435 1438 xdr_SETATTR3args, (caddr_t)&args,
1436 1439 xdr_SETATTR3res, (caddr_t)&res, cr,
1437 1440 &douprintf, &res.status, 0, NULL);
1438 1441
1439 1442 /*
1440 1443 * Purge the access cache and ACL cache if changing either the
1441 1444 * owner of the file, the group owner, or the mode. These may
1442 1445 * change the access permissions of the file, so purge old
1443 1446 * information and start over again.
1444 1447 */
1445 1448 if (mask & (AT_UID | AT_GID | AT_MODE)) {
1446 1449 (void) nfs_access_purge_rp(rp);
1447 1450 if (rp->r_secattr != NULL) {
1448 1451 mutex_enter(&rp->r_statelock);
1449 1452 vsp = rp->r_secattr;
1450 1453 rp->r_secattr = NULL;
1451 1454 mutex_exit(&rp->r_statelock);
1452 1455 if (vsp != NULL)
1453 1456 nfs_acl_free(vsp);
1454 1457 }
1455 1458 }
1456 1459
1457 1460 if (error) {
1458 1461 PURGE_ATTRCACHE(vp);
1459 1462 return (error);
1460 1463 }
1461 1464
1462 1465 error = geterrno3(res.status);
1463 1466 if (!error) {
1464 1467 /*
1465 1468 * If changing the size of the file, invalidate
1466 1469 * any local cached data which is no longer part
1467 1470 * of the file. We also possibly invalidate the
1468 1471 * last page in the file. We could use
1469 1472 * pvn_vpzero(), but this would mark the page as
1470 1473 * modified and require it to be written back to
1471 1474 * the server for no particularly good reason.
1472 1475 * This way, if we access it, then we bring it
1473 1476 * back in. A read should be cheaper than a
1474 1477 * write.
1475 1478 */
1476 1479 if (mask & AT_SIZE) {
1477 1480 nfs_invalidate_pages(vp,
1478 1481 (vap->va_size & PAGEMASK), cr);
1479 1482 }
1480 1483 nfs3_cache_wcc_data(vp, &res.resok.obj_wcc, t, cr);
1481 1484 /*
1482 1485 * Some servers will change the mode to clear the setuid
1483 1486 * and setgid bits when changing the uid or gid. The
1484 1487 * client needs to compensate appropriately.
1485 1488 */
1486 1489 if (mask & (AT_UID | AT_GID)) {
1487 1490 int terror;
1488 1491
1489 1492 va.va_mask = AT_MODE;
1490 1493 terror = nfs3getattr(vp, &va, cr);
1491 1494 if (!terror &&
1492 1495 (((mask & AT_MODE) && va.va_mode != vap->va_mode) ||
1493 1496 (!(mask & AT_MODE) && va.va_mode != omode))) {
1494 1497 va.va_mask = AT_MODE;
1495 1498 if (mask & AT_MODE)
1496 1499 va.va_mode = vap->va_mode;
1497 1500 else
1498 1501 va.va_mode = omode;
1499 1502 (void) nfs3setattr(vp, &va, 0, cr);
1500 1503 }
1501 1504 }
1502 1505 } else {
1503 1506 nfs3_cache_wcc_data(vp, &res.resfail.obj_wcc, t, cr);
1504 1507 /*
1505 1508 * If we got back a "not synchronized" error, then
1506 1509 * we need to retry with a new guard value. The
1507 1510 * guard value used is the change time. If the
1508 1511 * server returned post_op_attr, then we can just
1509 1512 * retry because we have the latest attributes.
1510 1513 * Otherwise, we issue a GETATTR to get the latest
1511 1514 * attributes and then retry. If we couldn't get
1512 1515 * the attributes this way either, then we give
1513 1516 * up because we can't complete the operation as
1514 1517 * required.
1515 1518 */
1516 1519 if (res.status == NFS3ERR_NOT_SYNC) {
1517 1520 va.va_mask = AT_CTIME;
1518 1521 if (nfs3getattr(vp, &va, cr) == 0)
1519 1522 goto tryagain;
1520 1523 }
1521 1524 PURGE_STALE_FH(error, vp, cr);
1522 1525 }
1523 1526
1524 1527 return (error);
1525 1528 }
1526 1529
1527 1530 static int
1528 1531 nfs3_accessx(void *vp, int mode, cred_t *cr)
1529 1532 {
1530 1533 ASSERT(nfs_zone() == VTOMI((vnode_t *)vp)->mi_zone);
1531 1534 return (nfs3_access(vp, mode, 0, cr, NULL));
1532 1535 }
1533 1536
1534 1537 /* ARGSUSED */
1535 1538 static int
1536 1539 nfs3_access(vnode_t *vp, int mode, int flags, cred_t *cr, caller_context_t *ct)
1537 1540 {
1538 1541 int error;
1539 1542 ACCESS3args args;
1540 1543 ACCESS3res res;
1541 1544 int douprintf;
1542 1545 uint32 acc;
1543 1546 rnode_t *rp;
1544 1547 cred_t *cred, *ncr, *ncrfree = NULL;
1545 1548 failinfo_t fi;
1546 1549 nfs_access_type_t cacc;
1547 1550 hrtime_t t;
1548 1551
1549 1552 acc = 0;
1550 1553 if (nfs_zone() != VTOMI(vp)->mi_zone)
1551 1554 return (EIO);
1552 1555 if (mode & VREAD)
1553 1556 acc |= ACCESS3_READ;
1554 1557 if (mode & VWRITE) {
1555 1558 if (vn_is_readonly(vp) && !IS_DEVVP(vp))
1556 1559 return (EROFS);
1557 1560 if (vp->v_type == VDIR)
1558 1561 acc |= ACCESS3_DELETE;
1559 1562 acc |= ACCESS3_MODIFY | ACCESS3_EXTEND;
1560 1563 }
1561 1564 if (mode & VEXEC) {
1562 1565 if (vp->v_type == VDIR)
1563 1566 acc |= ACCESS3_LOOKUP;
1564 1567 else
1565 1568 acc |= ACCESS3_EXECUTE;
1566 1569 }
1567 1570
1568 1571 rp = VTOR(vp);
1569 1572 args.object = *VTOFH3(vp);
1570 1573 if (vp->v_type == VDIR) {
1571 1574 args.access = ACCESS3_READ | ACCESS3_DELETE | ACCESS3_MODIFY |
1572 1575 ACCESS3_EXTEND | ACCESS3_LOOKUP;
1573 1576 } else {
1574 1577 args.access = ACCESS3_READ | ACCESS3_MODIFY | ACCESS3_EXTEND |
1575 1578 ACCESS3_EXECUTE;
1576 1579 }
1577 1580 fi.vp = vp;
1578 1581 fi.fhp = (caddr_t)&args.object;
1579 1582 fi.copyproc = nfs3copyfh;
1580 1583 fi.lookupproc = nfs3lookup;
1581 1584 fi.xattrdirproc = acl_getxattrdir3;
1582 1585
1583 1586 cred = cr;
1584 1587 /*
1585 1588 * ncr and ncrfree both initially
1586 1589 * point to the memory area returned
1587 1590 * by crnetadjust();
1588 1591 * ncrfree not NULL when exiting means
1589 1592 * that we need to release it
1590 1593 */
1591 1594 ncr = crnetadjust(cred);
1592 1595 ncrfree = ncr;
1593 1596 tryagain:
1594 1597 if (rp->r_acache != NULL) {
1595 1598 cacc = nfs_access_check(rp, acc, cred);
1596 1599 if (cacc == NFS_ACCESS_ALLOWED) {
1597 1600 if (ncrfree != NULL)
1598 1601 crfree(ncrfree);
1599 1602 return (0);
1600 1603 }
1601 1604 if (cacc == NFS_ACCESS_DENIED) {
1602 1605 /*
1603 1606 * If the cred can be adjusted, try again
1604 1607 * with the new cred.
1605 1608 */
1606 1609 if (ncr != NULL) {
1607 1610 cred = ncr;
1608 1611 ncr = NULL;
1609 1612 goto tryagain;
1610 1613 }
1611 1614 if (ncrfree != NULL)
1612 1615 crfree(ncrfree);
1613 1616 return (EACCES);
1614 1617 }
1615 1618 }
1616 1619
1617 1620 douprintf = 1;
1618 1621
1619 1622 t = gethrtime();
1620 1623
1621 1624 error = rfs3call(VTOMI(vp), NFSPROC3_ACCESS,
1622 1625 xdr_ACCESS3args, (caddr_t)&args,
1623 1626 xdr_ACCESS3res, (caddr_t)&res, cred,
1624 1627 &douprintf, &res.status, 0, &fi);
1625 1628
1626 1629 if (error) {
1627 1630 if (ncrfree != NULL)
1628 1631 crfree(ncrfree);
1629 1632 return (error);
1630 1633 }
1631 1634
1632 1635 error = geterrno3(res.status);
1633 1636 if (!error) {
1634 1637 nfs3_cache_post_op_attr(vp, &res.resok.obj_attributes, t, cr);
1635 1638 nfs_access_cache(rp, args.access, res.resok.access, cred);
1636 1639 /*
1637 1640 * we just cached results with cred; if cred is the
1638 1641 * adjusted credentials from crnetadjust, we do not want
1639 1642 * to release them before exiting: hence setting ncrfree
1640 1643 * to NULL
1641 1644 */
1642 1645 if (cred != cr)
1643 1646 ncrfree = NULL;
1644 1647 if ((acc & res.resok.access) != acc) {
1645 1648 /*
1646 1649 * If the cred can be adjusted, try again
1647 1650 * with the new cred.
1648 1651 */
1649 1652 if (ncr != NULL) {
1650 1653 cred = ncr;
1651 1654 ncr = NULL;
1652 1655 goto tryagain;
1653 1656 }
1654 1657 error = EACCES;
1655 1658 }
1656 1659 } else {
|
↓ open down ↓ |
1622 lines elided |
↑ open up ↑ |
1657 1660 nfs3_cache_post_op_attr(vp, &res.resfail.obj_attributes, t, cr);
1658 1661 PURGE_STALE_FH(error, vp, cr);
1659 1662 }
1660 1663
1661 1664 if (ncrfree != NULL)
1662 1665 crfree(ncrfree);
1663 1666
1664 1667 return (error);
1665 1668 }
1666 1669
1667 -static int nfs3_do_symlink_cache = 1;
1670 +volatile int nfs3_do_symlink_cache = 1;
1668 1671
1669 1672 /* ARGSUSED */
1670 1673 static int
1671 1674 nfs3_readlink(vnode_t *vp, struct uio *uiop, cred_t *cr, caller_context_t *ct)
1672 1675 {
1673 1676 int error;
1674 1677 READLINK3args args;
1675 1678 READLINK3res res;
1676 1679 nfspath3 resdata_backup;
1677 1680 rnode_t *rp;
1678 1681 int douprintf;
1679 1682 int len;
1680 1683 failinfo_t fi;
1681 1684 hrtime_t t;
1682 1685
1683 1686 /*
1684 1687 * Can't readlink anything other than a symbolic link.
1685 1688 */
1686 1689 if (vp->v_type != VLNK)
1687 1690 return (EINVAL);
1688 1691 if (nfs_zone() != VTOMI(vp)->mi_zone)
1689 1692 return (EIO);
1690 1693
1691 1694 rp = VTOR(vp);
1692 1695 if (nfs3_do_symlink_cache && rp->r_symlink.contents != NULL) {
1693 1696 error = nfs3_validate_caches(vp, cr);
1694 1697 if (error)
1695 1698 return (error);
1696 1699 mutex_enter(&rp->r_statelock);
1697 1700 if (rp->r_symlink.contents != NULL) {
1698 1701 error = uiomove(rp->r_symlink.contents,
1699 1702 rp->r_symlink.len, UIO_READ, uiop);
1700 1703 mutex_exit(&rp->r_statelock);
1701 1704 return (error);
1702 1705 }
1703 1706 mutex_exit(&rp->r_statelock);
1704 1707 }
1705 1708
1706 1709 args.symlink = *VTOFH3(vp);
1707 1710 fi.vp = vp;
1708 1711 fi.fhp = (caddr_t)&args.symlink;
1709 1712 fi.copyproc = nfs3copyfh;
1710 1713 fi.lookupproc = nfs3lookup;
1711 1714 fi.xattrdirproc = acl_getxattrdir3;
1712 1715
1713 1716 res.resok.data = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1714 1717
1715 1718 resdata_backup = res.resok.data;
1716 1719
1717 1720 douprintf = 1;
1718 1721
1719 1722 t = gethrtime();
1720 1723
1721 1724 error = rfs3call(VTOMI(vp), NFSPROC3_READLINK,
1722 1725 xdr_READLINK3args, (caddr_t)&args,
1723 1726 xdr_READLINK3res, (caddr_t)&res, cr,
1724 1727 &douprintf, &res.status, 0, &fi);
1725 1728
1726 1729 if (res.resok.data == nfs3nametoolong)
1727 1730 error = EINVAL;
1728 1731
1729 1732 if (error) {
1730 1733 kmem_free(resdata_backup, MAXPATHLEN);
1731 1734 return (error);
1732 1735 }
1733 1736
1734 1737 error = geterrno3(res.status);
1735 1738 if (!error) {
1736 1739 nfs3_cache_post_op_attr(vp, &res.resok.symlink_attributes, t,
1737 1740 cr);
1738 1741 len = strlen(res.resok.data);
1739 1742 error = uiomove(res.resok.data, len, UIO_READ, uiop);
1740 1743 if (nfs3_do_symlink_cache && rp->r_symlink.contents == NULL) {
1741 1744 mutex_enter(&rp->r_statelock);
1742 1745 if (rp->r_symlink.contents == NULL) {
1743 1746 rp->r_symlink.contents = res.resok.data;
1744 1747 rp->r_symlink.len = len;
1745 1748 rp->r_symlink.size = MAXPATHLEN;
1746 1749 mutex_exit(&rp->r_statelock);
1747 1750 } else {
1748 1751 mutex_exit(&rp->r_statelock);
1749 1752
1750 1753 kmem_free((void *)res.resok.data, MAXPATHLEN);
1751 1754 }
1752 1755 } else {
1753 1756 kmem_free((void *)res.resok.data, MAXPATHLEN);
1754 1757 }
1755 1758 } else {
1756 1759 nfs3_cache_post_op_attr(vp,
1757 1760 &res.resfail.symlink_attributes, t, cr);
1758 1761 PURGE_STALE_FH(error, vp, cr);
1759 1762
1760 1763 kmem_free((void *)res.resok.data, MAXPATHLEN);
1761 1764
1762 1765 }
1763 1766
1764 1767 /*
1765 1768 * The over the wire error for attempting to readlink something
1766 1769 * other than a symbolic link is ENXIO. However, we need to
1767 1770 * return EINVAL instead of ENXIO, so we map it here.
1768 1771 */
1769 1772 return (error == ENXIO ? EINVAL : error);
1770 1773 }
1771 1774
1772 1775 /*
1773 1776 * Flush local dirty pages to stable storage on the server.
1774 1777 *
1775 1778 * If FNODSYNC is specified, then there is nothing to do because
1776 1779 * metadata changes are not cached on the client before being
1777 1780 * sent to the server.
1778 1781 */
1779 1782 /* ARGSUSED */
1780 1783 static int
1781 1784 nfs3_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
1782 1785 {
1783 1786 int error;
1784 1787
1785 1788 if ((syncflag & FNODSYNC) || IS_SWAPVP(vp))
1786 1789 return (0);
1787 1790 if (nfs_zone() != VTOMI(vp)->mi_zone)
1788 1791 return (EIO);
1789 1792
1790 1793 error = nfs3_putpage_commit(vp, (offset_t)0, 0, cr);
1791 1794 if (!error)
1792 1795 error = VTOR(vp)->r_error;
1793 1796 return (error);
1794 1797 }
1795 1798
1796 1799 /*
1797 1800 * Weirdness: if the file was removed or the target of a rename
1798 1801 * operation while it was open, it got renamed instead. Here we
1799 1802 * remove the renamed file.
1800 1803 */
1801 1804 /* ARGSUSED */
1802 1805 static void
1803 1806 nfs3_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
1804 1807 {
1805 1808 rnode_t *rp;
1806 1809
1807 1810 ASSERT(vp != DNLC_NO_VNODE);
1808 1811
1809 1812 /*
1810 1813 * If this is coming from the wrong zone, we let someone in the right
1811 1814 * zone take care of it asynchronously. We can get here due to
1812 1815 * VN_RELE() being called from pageout() or fsflush(). This call may
1813 1816 * potentially turn into an expensive no-op if, for instance, v_count
1814 1817 * gets incremented in the meantime, but it's still correct.
1815 1818 */
1816 1819 if (nfs_zone() != VTOMI(vp)->mi_zone) {
1817 1820 nfs_async_inactive(vp, cr, nfs3_inactive);
1818 1821 return;
1819 1822 }
1820 1823
1821 1824 rp = VTOR(vp);
1822 1825 redo:
1823 1826 if (rp->r_unldvp != NULL) {
1824 1827 /*
1825 1828 * Save the vnode pointer for the directory where the
1826 1829 * unlinked-open file got renamed, then set it to NULL
1827 1830 * to prevent another thread from getting here before
1828 1831 * we're done with the remove. While we have the
1829 1832 * statelock, make local copies of the pertinent rnode
1830 1833 * fields. If we weren't to do this in an atomic way, the
1831 1834 * the unl* fields could become inconsistent with respect
1832 1835 * to each other due to a race condition between this
1833 1836 * code and nfs_remove(). See bug report 1034328.
1834 1837 */
1835 1838 mutex_enter(&rp->r_statelock);
1836 1839 if (rp->r_unldvp != NULL) {
1837 1840 vnode_t *unldvp;
1838 1841 char *unlname;
1839 1842 cred_t *unlcred;
1840 1843 REMOVE3args args;
1841 1844 REMOVE3res res;
1842 1845 int douprintf;
1843 1846 int error;
1844 1847 hrtime_t t;
1845 1848
1846 1849 unldvp = rp->r_unldvp;
1847 1850 rp->r_unldvp = NULL;
1848 1851 unlname = rp->r_unlname;
1849 1852 rp->r_unlname = NULL;
1850 1853 unlcred = rp->r_unlcred;
1851 1854 rp->r_unlcred = NULL;
1852 1855 mutex_exit(&rp->r_statelock);
1853 1856
1854 1857 /*
1855 1858 * If there are any dirty pages left, then flush
1856 1859 * them. This is unfortunate because they just
1857 1860 * may get thrown away during the remove operation,
1858 1861 * but we have to do this for correctness.
1859 1862 */
1860 1863 if (vn_has_cached_data(vp) &&
1861 1864 ((rp->r_flags & RDIRTY) || rp->r_count > 0)) {
1862 1865 ASSERT(vp->v_type != VCHR);
1863 1866 error = nfs3_putpage(vp, (offset_t)0, 0, 0,
1864 1867 cr, ct);
1865 1868 if (error) {
1866 1869 mutex_enter(&rp->r_statelock);
1867 1870 if (!rp->r_error)
1868 1871 rp->r_error = error;
1869 1872 mutex_exit(&rp->r_statelock);
1870 1873 }
1871 1874 }
1872 1875
1873 1876 /*
1874 1877 * Do the remove operation on the renamed file
1875 1878 */
1876 1879 setdiropargs3(&args.object, unlname, unldvp);
1877 1880
1878 1881 douprintf = 1;
1879 1882
1880 1883 t = gethrtime();
1881 1884
1882 1885 error = rfs3call(VTOMI(unldvp), NFSPROC3_REMOVE,
1883 1886 xdr_diropargs3, (caddr_t)&args,
1884 1887 xdr_REMOVE3res, (caddr_t)&res, unlcred,
1885 1888 &douprintf, &res.status, 0, NULL);
1886 1889
1887 1890 if (error) {
1888 1891 PURGE_ATTRCACHE(unldvp);
1889 1892 } else {
1890 1893 error = geterrno3(res.status);
1891 1894 if (!error) {
1892 1895 nfs3_cache_wcc_data(unldvp,
1893 1896 &res.resok.dir_wcc, t, cr);
1894 1897 if (HAVE_RDDIR_CACHE(VTOR(unldvp)))
1895 1898 nfs_purge_rddir_cache(unldvp);
1896 1899 } else {
1897 1900 nfs3_cache_wcc_data(unldvp,
1898 1901 &res.resfail.dir_wcc, t, cr);
1899 1902 PURGE_STALE_FH(error, unldvp, cr);
1900 1903 }
1901 1904 }
1902 1905
1903 1906 /*
1904 1907 * Release stuff held for the remove
1905 1908 */
1906 1909 VN_RELE(unldvp);
1907 1910 kmem_free(unlname, MAXNAMELEN);
1908 1911 crfree(unlcred);
1909 1912 goto redo;
1910 1913 }
1911 1914 mutex_exit(&rp->r_statelock);
1912 1915 }
1913 1916
1914 1917 rp_addfree(rp, cr);
1915 1918 }
1916 1919
1917 1920 /*
1918 1921 * Remote file system operations having to do with directory manipulation.
1919 1922 */
1920 1923
1921 1924 /* ARGSUSED */
1922 1925 static int
1923 1926 nfs3_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
1924 1927 int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
1925 1928 int *direntflags, pathname_t *realpnp)
1926 1929 {
1927 1930 int error;
1928 1931 vnode_t *vp;
1929 1932 vnode_t *avp = NULL;
1930 1933 rnode_t *drp;
1931 1934
1932 1935 if (nfs_zone() != VTOMI(dvp)->mi_zone)
1933 1936 return (EPERM);
1934 1937
1935 1938 drp = VTOR(dvp);
1936 1939
1937 1940 /*
1938 1941 * Are we looking up extended attributes? If so, "dvp" is
1939 1942 * the file or directory for which we want attributes, and
1940 1943 * we need a lookup of the hidden attribute directory
1941 1944 * before we lookup the rest of the path.
1942 1945 */
1943 1946 if (flags & LOOKUP_XATTR) {
1944 1947 bool_t cflag = ((flags & CREATE_XATTR_DIR) != 0);
1945 1948 mntinfo_t *mi;
1946 1949
1947 1950 mi = VTOMI(dvp);
1948 1951 if (!(mi->mi_flags & MI_EXTATTR))
1949 1952 return (EINVAL);
1950 1953
1951 1954 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR(dvp)))
1952 1955 return (EINTR);
1953 1956
1954 1957 (void) nfs3lookup_dnlc(dvp, XATTR_DIR_NAME, &avp, cr);
1955 1958 if (avp == NULL)
1956 1959 error = acl_getxattrdir3(dvp, &avp, cflag, cr, 0);
1957 1960 else
1958 1961 error = 0;
1959 1962
1960 1963 nfs_rw_exit(&drp->r_rwlock);
1961 1964
1962 1965 if (error) {
1963 1966 if (mi->mi_flags & MI_EXTATTR)
1964 1967 return (error);
1965 1968 return (EINVAL);
1966 1969 }
1967 1970 dvp = avp;
1968 1971 drp = VTOR(dvp);
1969 1972 }
1970 1973
1971 1974 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR(dvp))) {
1972 1975 error = EINTR;
1973 1976 goto out;
1974 1977 }
1975 1978
1976 1979 error = nfs3lookup(dvp, nm, vpp, pnp, flags, rdir, cr, 0);
1977 1980
1978 1981 nfs_rw_exit(&drp->r_rwlock);
1979 1982
1980 1983 /*
1981 1984 * If vnode is a device, create special vnode.
1982 1985 */
1983 1986 if (!error && IS_DEVVP(*vpp)) {
1984 1987 vp = *vpp;
1985 1988 *vpp = specvp(vp, vp->v_rdev, vp->v_type, cr);
|
↓ open down ↓ |
308 lines elided |
↑ open up ↑ |
1986 1989 VN_RELE(vp);
1987 1990 }
1988 1991
1989 1992 out:
1990 1993 if (avp != NULL)
1991 1994 VN_RELE(avp);
1992 1995
1993 1996 return (error);
1994 1997 }
1995 1998
1996 -static int nfs3_lookup_neg_cache = 1;
1999 +volatile int nfs3_lookup_neg_cache = 1;
1997 2000
1998 2001 #ifdef DEBUG
1999 2002 static int nfs3_lookup_dnlc_hits = 0;
2000 2003 static int nfs3_lookup_dnlc_misses = 0;
2001 2004 static int nfs3_lookup_dnlc_neg_hits = 0;
2002 2005 static int nfs3_lookup_dnlc_disappears = 0;
2003 2006 static int nfs3_lookup_dnlc_lookups = 0;
2004 2007 #endif
2005 2008
2006 2009 /* ARGSUSED */
2007 2010 int
2008 2011 nfs3lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
2009 2012 int flags, vnode_t *rdir, cred_t *cr, int rfscall_flags)
2010 2013 {
2011 2014 int error;
2012 2015 rnode_t *drp;
2013 2016
2014 2017 ASSERT(nfs_zone() == VTOMI(dvp)->mi_zone);
2015 2018 /*
2016 2019 * If lookup is for "", just return dvp. Don't need
2017 2020 * to send it over the wire, look it up in the dnlc,
2018 2021 * or perform any access checks.
2019 2022 */
2020 2023 if (*nm == '\0') {
2021 2024 VN_HOLD(dvp);
2022 2025 *vpp = dvp;
2023 2026 return (0);
2024 2027 }
2025 2028
2026 2029 /*
2027 2030 * Can't do lookups in non-directories.
2028 2031 */
2029 2032 if (dvp->v_type != VDIR)
2030 2033 return (ENOTDIR);
2031 2034
2032 2035 /*
2033 2036 * If we're called with RFSCALL_SOFT, it's important that
2034 2037 * the only rfscall is one we make directly; if we permit
2035 2038 * an access call because we're looking up "." or validating
2036 2039 * a dnlc hit, we'll deadlock because that rfscall will not
2037 2040 * have the RFSCALL_SOFT set.
2038 2041 */
2039 2042 if (rfscall_flags & RFSCALL_SOFT)
2040 2043 goto callit;
2041 2044
2042 2045 /*
2043 2046 * If lookup is for ".", just return dvp. Don't need
2044 2047 * to send it over the wire or look it up in the dnlc,
2045 2048 * just need to check access.
2046 2049 */
2047 2050 if (strcmp(nm, ".") == 0) {
2048 2051 error = nfs3_access(dvp, VEXEC, 0, cr, NULL);
2049 2052 if (error)
2050 2053 return (error);
2051 2054 VN_HOLD(dvp);
2052 2055 *vpp = dvp;
2053 2056 return (0);
2054 2057 }
2055 2058
2056 2059 drp = VTOR(dvp);
2057 2060 if (!(drp->r_flags & RLOOKUP)) {
2058 2061 mutex_enter(&drp->r_statelock);
2059 2062 drp->r_flags |= RLOOKUP;
2060 2063 mutex_exit(&drp->r_statelock);
2061 2064 }
2062 2065
2063 2066 /*
2064 2067 * Lookup this name in the DNLC. If there was a valid entry,
2065 2068 * then return the results of the lookup.
2066 2069 */
2067 2070 error = nfs3lookup_dnlc(dvp, nm, vpp, cr);
2068 2071 if (error || *vpp != NULL)
2069 2072 return (error);
2070 2073
2071 2074 callit:
2072 2075 error = nfs3lookup_otw(dvp, nm, vpp, cr, rfscall_flags);
2073 2076
2074 2077 return (error);
2075 2078 }
2076 2079
2077 2080 static int
2078 2081 nfs3lookup_dnlc(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr)
2079 2082 {
2080 2083 int error;
2081 2084 vnode_t *vp;
2082 2085
2083 2086 ASSERT(*nm != '\0');
2084 2087 ASSERT(nfs_zone() == VTOMI(dvp)->mi_zone);
2085 2088 /*
2086 2089 * Lookup this name in the DNLC. If successful, then validate
2087 2090 * the caches and then recheck the DNLC. The DNLC is rechecked
2088 2091 * just in case this entry got invalidated during the call
2089 2092 * to nfs3_validate_caches.
2090 2093 *
2091 2094 * An assumption is being made that it is safe to say that a
2092 2095 * file exists which may not on the server. Any operations to
2093 2096 * the server will fail with ESTALE.
2094 2097 */
2095 2098 #ifdef DEBUG
2096 2099 nfs3_lookup_dnlc_lookups++;
2097 2100 #endif
2098 2101 vp = dnlc_lookup(dvp, nm);
2099 2102 if (vp != NULL) {
2100 2103 VN_RELE(vp);
2101 2104 if (vp == DNLC_NO_VNODE && !vn_is_readonly(dvp)) {
2102 2105 PURGE_ATTRCACHE(dvp);
2103 2106 }
2104 2107 error = nfs3_validate_caches(dvp, cr);
2105 2108 if (error)
2106 2109 return (error);
2107 2110 vp = dnlc_lookup(dvp, nm);
2108 2111 if (vp != NULL) {
2109 2112 error = nfs3_access(dvp, VEXEC, 0, cr, NULL);
2110 2113 if (error) {
2111 2114 VN_RELE(vp);
2112 2115 return (error);
2113 2116 }
2114 2117 if (vp == DNLC_NO_VNODE) {
2115 2118 VN_RELE(vp);
2116 2119 #ifdef DEBUG
2117 2120 nfs3_lookup_dnlc_neg_hits++;
2118 2121 #endif
2119 2122 return (ENOENT);
2120 2123 }
2121 2124 *vpp = vp;
2122 2125 #ifdef DEBUG
2123 2126 nfs3_lookup_dnlc_hits++;
2124 2127 #endif
2125 2128 return (0);
2126 2129 }
2127 2130 #ifdef DEBUG
2128 2131 nfs3_lookup_dnlc_disappears++;
2129 2132 #endif
2130 2133 }
2131 2134 #ifdef DEBUG
2132 2135 else
2133 2136 nfs3_lookup_dnlc_misses++;
2134 2137 #endif
2135 2138
2136 2139 *vpp = NULL;
2137 2140
2138 2141 return (0);
2139 2142 }
2140 2143
2141 2144 static int
2142 2145 nfs3lookup_otw(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr,
2143 2146 int rfscall_flags)
2144 2147 {
2145 2148 int error;
2146 2149 LOOKUP3args args;
2147 2150 LOOKUP3vres res;
2148 2151 int douprintf;
2149 2152 struct vattr vattr;
2150 2153 struct vattr dvattr;
2151 2154 vnode_t *vp;
2152 2155 failinfo_t fi;
2153 2156 hrtime_t t;
2154 2157
2155 2158 ASSERT(*nm != '\0');
2156 2159 ASSERT(dvp->v_type == VDIR);
2157 2160 ASSERT(nfs_zone() == VTOMI(dvp)->mi_zone);
2158 2161
2159 2162 setdiropargs3(&args.what, nm, dvp);
2160 2163
2161 2164 fi.vp = dvp;
2162 2165 fi.fhp = (caddr_t)&args.what.dir;
2163 2166 fi.copyproc = nfs3copyfh;
2164 2167 fi.lookupproc = nfs3lookup;
2165 2168 fi.xattrdirproc = acl_getxattrdir3;
2166 2169 res.obj_attributes.fres.vp = dvp;
2167 2170 res.obj_attributes.fres.vap = &vattr;
2168 2171 res.dir_attributes.fres.vp = dvp;
2169 2172 res.dir_attributes.fres.vap = &dvattr;
2170 2173
2171 2174 douprintf = 1;
2172 2175
2173 2176 t = gethrtime();
2174 2177
2175 2178 error = rfs3call(VTOMI(dvp), NFSPROC3_LOOKUP,
2176 2179 xdr_diropargs3, (caddr_t)&args,
2177 2180 xdr_LOOKUP3vres, (caddr_t)&res, cr,
2178 2181 &douprintf, &res.status, rfscall_flags, &fi);
2179 2182
2180 2183 if (error)
2181 2184 return (error);
2182 2185
2183 2186 nfs3_cache_post_op_vattr(dvp, &res.dir_attributes, t, cr);
2184 2187
2185 2188 error = geterrno3(res.status);
2186 2189 if (error) {
2187 2190 PURGE_STALE_FH(error, dvp, cr);
2188 2191 if (error == ENOENT && nfs3_lookup_neg_cache)
2189 2192 dnlc_enter(dvp, nm, DNLC_NO_VNODE);
2190 2193 return (error);
2191 2194 }
2192 2195
2193 2196 if (res.obj_attributes.attributes) {
2194 2197 vp = makenfs3node_va(&res.object, res.obj_attributes.fres.vap,
2195 2198 dvp->v_vfsp, t, cr, VTOR(dvp)->r_path, nm);
2196 2199 } else {
2197 2200 vp = makenfs3node_va(&res.object, NULL,
2198 2201 dvp->v_vfsp, t, cr, VTOR(dvp)->r_path, nm);
2199 2202 if (vp->v_type == VNON) {
2200 2203 vattr.va_mask = AT_TYPE;
2201 2204 error = nfs3getattr(vp, &vattr, cr);
2202 2205 if (error) {
2203 2206 VN_RELE(vp);
2204 2207 return (error);
2205 2208 }
2206 2209 vp->v_type = vattr.va_type;
2207 2210 }
2208 2211 }
2209 2212
2210 2213 if (!(rfscall_flags & RFSCALL_SOFT))
2211 2214 dnlc_update(dvp, nm, vp);
2212 2215
2213 2216 *vpp = vp;
2214 2217
2215 2218 return (error);
2216 2219 }
2217 2220
2218 2221 #ifdef DEBUG
2219 2222 static int nfs3_create_misses = 0;
2220 2223 #endif
2221 2224
2222 2225 /* ARGSUSED */
2223 2226 static int
2224 2227 nfs3_create(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive,
2225 2228 int mode, vnode_t **vpp, cred_t *cr, int lfaware, caller_context_t *ct,
2226 2229 vsecattr_t *vsecp)
2227 2230 {
2228 2231 int error;
2229 2232 vnode_t *vp;
2230 2233 rnode_t *rp;
2231 2234 struct vattr vattr;
2232 2235 rnode_t *drp;
2233 2236 vnode_t *tempvp;
2234 2237
2235 2238 drp = VTOR(dvp);
2236 2239 if (nfs_zone() != VTOMI(dvp)->mi_zone)
2237 2240 return (EPERM);
2238 2241 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR(dvp)))
2239 2242 return (EINTR);
2240 2243
2241 2244 top:
2242 2245 /*
2243 2246 * We make a copy of the attributes because the caller does not
2244 2247 * expect us to change what va points to.
2245 2248 */
2246 2249 vattr = *va;
2247 2250
2248 2251 /*
2249 2252 * If the pathname is "", just use dvp. Don't need
2250 2253 * to send it over the wire, look it up in the dnlc,
2251 2254 * or perform any access checks.
2252 2255 */
2253 2256 if (*nm == '\0') {
2254 2257 error = 0;
2255 2258 VN_HOLD(dvp);
2256 2259 vp = dvp;
2257 2260 /*
2258 2261 * If the pathname is ".", just use dvp. Don't need
2259 2262 * to send it over the wire or look it up in the dnlc,
2260 2263 * just need to check access.
2261 2264 */
2262 2265 } else if (strcmp(nm, ".") == 0) {
2263 2266 error = nfs3_access(dvp, VEXEC, 0, cr, ct);
2264 2267 if (error) {
2265 2268 nfs_rw_exit(&drp->r_rwlock);
2266 2269 return (error);
2267 2270 }
2268 2271 VN_HOLD(dvp);
2269 2272 vp = dvp;
2270 2273 /*
2271 2274 * We need to go over the wire, just to be sure whether the
2272 2275 * file exists or not. Using the DNLC can be dangerous in
2273 2276 * this case when making a decision regarding existence.
2274 2277 */
2275 2278 } else {
2276 2279 error = nfs3lookup_otw(dvp, nm, &vp, cr, 0);
2277 2280 }
2278 2281 if (!error) {
2279 2282 if (exclusive == EXCL)
2280 2283 error = EEXIST;
2281 2284 else if (vp->v_type == VDIR && (mode & VWRITE))
2282 2285 error = EISDIR;
2283 2286 else {
2284 2287 /*
2285 2288 * If vnode is a device, create special vnode.
2286 2289 */
2287 2290 if (IS_DEVVP(vp)) {
2288 2291 tempvp = vp;
2289 2292 vp = specvp(vp, vp->v_rdev, vp->v_type, cr);
2290 2293 VN_RELE(tempvp);
2291 2294 }
2292 2295 if (!(error = VOP_ACCESS(vp, mode, 0, cr, ct))) {
2293 2296 if ((vattr.va_mask & AT_SIZE) &&
2294 2297 vp->v_type == VREG) {
2295 2298 rp = VTOR(vp);
2296 2299 /*
2297 2300 * Check here for large file handled
2298 2301 * by LF-unaware process (as
2299 2302 * ufs_create() does)
2300 2303 */
2301 2304 if (!(lfaware & FOFFMAX)) {
2302 2305 mutex_enter(&rp->r_statelock);
2303 2306 if (rp->r_size > MAXOFF32_T)
2304 2307 error = EOVERFLOW;
2305 2308 mutex_exit(&rp->r_statelock);
2306 2309 }
2307 2310 if (!error) {
2308 2311 vattr.va_mask = AT_SIZE;
2309 2312 error = nfs3setattr(vp,
2310 2313 &vattr, 0, cr);
2311 2314
2312 2315 /*
2313 2316 * Existing file was truncated;
2314 2317 * emit a create event.
2315 2318 */
2316 2319 vnevent_create(vp, ct);
2317 2320 }
2318 2321 }
2319 2322 }
2320 2323 }
2321 2324 nfs_rw_exit(&drp->r_rwlock);
2322 2325 if (error) {
2323 2326 VN_RELE(vp);
2324 2327 } else {
2325 2328 *vpp = vp;
2326 2329 }
2327 2330
2328 2331 return (error);
2329 2332 }
2330 2333
2331 2334 dnlc_remove(dvp, nm);
2332 2335
2333 2336 /*
2334 2337 * Decide what the group-id of the created file should be.
2335 2338 * Set it in attribute list as advisory...
2336 2339 */
2337 2340 error = setdirgid(dvp, &vattr.va_gid, cr);
2338 2341 if (error) {
2339 2342 nfs_rw_exit(&drp->r_rwlock);
2340 2343 return (error);
2341 2344 }
2342 2345 vattr.va_mask |= AT_GID;
2343 2346
2344 2347 ASSERT(vattr.va_mask & AT_TYPE);
2345 2348 if (vattr.va_type == VREG) {
2346 2349 ASSERT(vattr.va_mask & AT_MODE);
2347 2350 if (MANDMODE(vattr.va_mode)) {
2348 2351 nfs_rw_exit(&drp->r_rwlock);
2349 2352 return (EACCES);
2350 2353 }
2351 2354 error = nfs3create(dvp, nm, &vattr, exclusive, mode, vpp, cr,
2352 2355 lfaware);
2353 2356 /*
2354 2357 * If this is not an exclusive create, then the CREATE
2355 2358 * request will be made with the GUARDED mode set. This
2356 2359 * means that the server will return EEXIST if the file
2357 2360 * exists. The file could exist because of a retransmitted
2358 2361 * request. In this case, we recover by starting over and
2359 2362 * checking to see whether the file exists. This second
2360 2363 * time through it should and a CREATE request will not be
2361 2364 * sent.
2362 2365 *
2363 2366 * This handles the problem of a dangling CREATE request
2364 2367 * which contains attributes which indicate that the file
2365 2368 * should be truncated. This retransmitted request could
2366 2369 * possibly truncate valid data in the file if not caught
2367 2370 * by the duplicate request mechanism on the server or if
2368 2371 * not caught by other means. The scenario is:
2369 2372 *
2370 2373 * Client transmits CREATE request with size = 0
2371 2374 * Client times out, retransmits request.
2372 2375 * Response to the first request arrives from the server
2373 2376 * and the client proceeds on.
2374 2377 * Client writes data to the file.
2375 2378 * The server now processes retransmitted CREATE request
2376 2379 * and truncates file.
2377 2380 *
2378 2381 * The use of the GUARDED CREATE request prevents this from
2379 2382 * happening because the retransmitted CREATE would fail
2380 2383 * with EEXIST and would not truncate the file.
2381 2384 */
2382 2385 if (error == EEXIST && exclusive == NONEXCL) {
2383 2386 #ifdef DEBUG
2384 2387 nfs3_create_misses++;
2385 2388 #endif
2386 2389 goto top;
2387 2390 }
2388 2391 nfs_rw_exit(&drp->r_rwlock);
2389 2392 return (error);
2390 2393 }
2391 2394 error = nfs3mknod(dvp, nm, &vattr, exclusive, mode, vpp, cr);
2392 2395 nfs_rw_exit(&drp->r_rwlock);
2393 2396 return (error);
2394 2397 }
2395 2398
2396 2399 /* ARGSUSED */
2397 2400 static int
2398 2401 nfs3create(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive,
2399 2402 int mode, vnode_t **vpp, cred_t *cr, int lfaware)
2400 2403 {
2401 2404 int error;
2402 2405 CREATE3args args;
2403 2406 CREATE3res res;
2404 2407 int douprintf;
2405 2408 vnode_t *vp;
2406 2409 struct vattr vattr;
2407 2410 nfstime3 *verfp;
2408 2411 rnode_t *rp;
2409 2412 timestruc_t now;
2410 2413 hrtime_t t;
2411 2414
2412 2415 ASSERT(nfs_zone() == VTOMI(dvp)->mi_zone);
2413 2416 setdiropargs3(&args.where, nm, dvp);
2414 2417 if (exclusive == EXCL) {
2415 2418 args.how.mode = EXCLUSIVE;
2416 2419 /*
2417 2420 * Construct the create verifier. This verifier needs
2418 2421 * to be unique between different clients. It also needs
2419 2422 * to vary for each exclusive create request generated
2420 2423 * from the client to the server.
2421 2424 *
2422 2425 * The first attempt is made to use the hostid and a
2423 2426 * unique number on the client. If the hostid has not
2424 2427 * been set, the high resolution time that the exclusive
2425 2428 * create request is being made is used. This will work
2426 2429 * unless two different clients, both with the hostid
2427 2430 * not set, attempt an exclusive create request on the
2428 2431 * same file, at exactly the same clock time. The
2429 2432 * chances of this happening seem small enough to be
2430 2433 * reasonable.
2431 2434 */
2432 2435 verfp = (nfstime3 *)&args.how.createhow3_u.verf;
2433 2436 verfp->seconds = zone_get_hostid(NULL);
2434 2437 if (verfp->seconds != 0)
2435 2438 verfp->nseconds = newnum();
2436 2439 else {
2437 2440 gethrestime(&now);
2438 2441 verfp->seconds = now.tv_sec;
2439 2442 verfp->nseconds = now.tv_nsec;
2440 2443 }
2441 2444 /*
2442 2445 * Since the server will use this value for the mtime,
2443 2446 * make sure that it can't overflow. Zero out the MSB.
2444 2447 * The actual value does not matter here, only its uniqeness.
2445 2448 */
2446 2449 verfp->seconds %= INT32_MAX;
2447 2450 } else {
2448 2451 /*
2449 2452 * Issue the non-exclusive create in guarded mode. This
2450 2453 * may result in some false EEXIST responses for
2451 2454 * retransmitted requests, but these will be handled at
2452 2455 * a higher level. By using GUARDED, duplicate requests
2453 2456 * to do file truncation and possible access problems
2454 2457 * can be avoided.
2455 2458 */
2456 2459 args.how.mode = GUARDED;
2457 2460 error = vattr_to_sattr3(va,
2458 2461 &args.how.createhow3_u.obj_attributes);
2459 2462 if (error) {
2460 2463 /* req time field(s) overflow - return immediately */
2461 2464 return (error);
2462 2465 }
2463 2466 }
2464 2467
2465 2468 douprintf = 1;
2466 2469
2467 2470 t = gethrtime();
2468 2471
2469 2472 error = rfs3call(VTOMI(dvp), NFSPROC3_CREATE,
2470 2473 xdr_CREATE3args, (caddr_t)&args,
2471 2474 xdr_CREATE3res, (caddr_t)&res, cr,
2472 2475 &douprintf, &res.status, 0, NULL);
2473 2476
2474 2477 if (error) {
2475 2478 PURGE_ATTRCACHE(dvp);
2476 2479 return (error);
2477 2480 }
2478 2481
2479 2482 error = geterrno3(res.status);
2480 2483 if (!error) {
2481 2484 nfs3_cache_wcc_data(dvp, &res.resok.dir_wcc, t, cr);
2482 2485 if (HAVE_RDDIR_CACHE(VTOR(dvp)))
2483 2486 nfs_purge_rddir_cache(dvp);
2484 2487
2485 2488 /*
2486 2489 * On exclusive create the times need to be explicitly
2487 2490 * set to clear any potential verifier that may be stored
2488 2491 * in one of these fields (see comment below). This
2489 2492 * is done here to cover the case where no post op attrs
2490 2493 * were returned or a 'invalid' time was returned in
2491 2494 * the attributes.
2492 2495 */
2493 2496 if (exclusive == EXCL)
2494 2497 va->va_mask |= (AT_MTIME | AT_ATIME);
2495 2498
2496 2499 if (!res.resok.obj.handle_follows) {
2497 2500 error = nfs3lookup(dvp, nm, &vp, NULL, 0, NULL, cr, 0);
2498 2501 if (error)
2499 2502 return (error);
2500 2503 } else {
2501 2504 if (res.resok.obj_attributes.attributes) {
2502 2505 vp = makenfs3node(&res.resok.obj.handle,
2503 2506 &res.resok.obj_attributes.attr,
2504 2507 dvp->v_vfsp, t, cr, NULL, NULL);
2505 2508 } else {
2506 2509 vp = makenfs3node(&res.resok.obj.handle, NULL,
2507 2510 dvp->v_vfsp, t, cr, NULL, NULL);
2508 2511
2509 2512 /*
2510 2513 * On an exclusive create, it is possible
2511 2514 * that attributes were returned but those
2512 2515 * postop attributes failed to decode
2513 2516 * properly. If this is the case,
2514 2517 * then most likely the atime or mtime
2515 2518 * were invalid for our client; this
2516 2519 * is caused by the server storing the
2517 2520 * create verifier in one of the time
2518 2521 * fields(most likely mtime).
2519 2522 * So... we are going to setattr just the
2520 2523 * atime/mtime to clear things up.
2521 2524 */
2522 2525 if (exclusive == EXCL) {
2523 2526 if (error =
2524 2527 nfs3excl_create_settimes(vp,
2525 2528 va, cr)) {
2526 2529 /*
2527 2530 * Setting the times failed.
2528 2531 * Remove the file and return
2529 2532 * the error.
2530 2533 */
2531 2534 VN_RELE(vp);
2532 2535 (void) nfs3_remove(dvp,
2533 2536 nm, cr, NULL, 0);
2534 2537 return (error);
2535 2538 }
2536 2539 }
2537 2540
2538 2541 /*
2539 2542 * This handles the non-exclusive case
2540 2543 * and the exclusive case where no post op
2541 2544 * attrs were returned.
2542 2545 */
2543 2546 if (vp->v_type == VNON) {
2544 2547 vattr.va_mask = AT_TYPE;
2545 2548 error = nfs3getattr(vp, &vattr, cr);
2546 2549 if (error) {
2547 2550 VN_RELE(vp);
2548 2551 return (error);
2549 2552 }
2550 2553 vp->v_type = vattr.va_type;
2551 2554 }
2552 2555 }
2553 2556 dnlc_update(dvp, nm, vp);
2554 2557 }
2555 2558
2556 2559 rp = VTOR(vp);
2557 2560
2558 2561 /*
2559 2562 * Check here for large file handled by
2560 2563 * LF-unaware process (as ufs_create() does)
2561 2564 */
2562 2565 if ((va->va_mask & AT_SIZE) && vp->v_type == VREG &&
2563 2566 !(lfaware & FOFFMAX)) {
2564 2567 mutex_enter(&rp->r_statelock);
2565 2568 if (rp->r_size > MAXOFF32_T) {
2566 2569 mutex_exit(&rp->r_statelock);
2567 2570 VN_RELE(vp);
2568 2571 return (EOVERFLOW);
2569 2572 }
2570 2573 mutex_exit(&rp->r_statelock);
2571 2574 }
2572 2575
2573 2576 if (exclusive == EXCL &&
2574 2577 (va->va_mask & ~(AT_GID | AT_SIZE))) {
2575 2578 /*
2576 2579 * If doing an exclusive create, then generate
2577 2580 * a SETATTR to set the initial attributes.
2578 2581 * Try to set the mtime and the atime to the
2579 2582 * server's current time. It is somewhat
2580 2583 * expected that these fields will be used to
2581 2584 * store the exclusive create cookie. If not,
2582 2585 * server implementors will need to know that
2583 2586 * a SETATTR will follow an exclusive create
2584 2587 * and the cookie should be destroyed if
2585 2588 * appropriate. This work may have been done
2586 2589 * earlier in this function if post op attrs
2587 2590 * were not available.
2588 2591 *
2589 2592 * The AT_GID and AT_SIZE bits are turned off
2590 2593 * so that the SETATTR request will not attempt
2591 2594 * to process these. The gid will be set
2592 2595 * separately if appropriate. The size is turned
2593 2596 * off because it is assumed that a new file will
2594 2597 * be created empty and if the file wasn't empty,
2595 2598 * then the exclusive create will have failed
2596 2599 * because the file must have existed already.
2597 2600 * Therefore, no truncate operation is needed.
2598 2601 */
2599 2602 va->va_mask &= ~(AT_GID | AT_SIZE);
2600 2603 error = nfs3setattr(vp, va, 0, cr);
2601 2604 if (error) {
2602 2605 /*
2603 2606 * Couldn't correct the attributes of
2604 2607 * the newly created file and the
2605 2608 * attributes are wrong. Remove the
2606 2609 * file and return an error to the
2607 2610 * application.
2608 2611 */
2609 2612 VN_RELE(vp);
2610 2613 (void) nfs3_remove(dvp, nm, cr, NULL, 0);
2611 2614 return (error);
2612 2615 }
2613 2616 }
2614 2617
2615 2618 if (va->va_gid != rp->r_attr.va_gid) {
2616 2619 /*
2617 2620 * If the gid on the file isn't right, then
2618 2621 * generate a SETATTR to attempt to change
2619 2622 * it. This may or may not work, depending
2620 2623 * upon the server's semantics for allowing
2621 2624 * file ownership changes.
2622 2625 */
2623 2626 va->va_mask = AT_GID;
2624 2627 (void) nfs3setattr(vp, va, 0, cr);
2625 2628 }
2626 2629
2627 2630 /*
2628 2631 * If vnode is a device create special vnode
2629 2632 */
2630 2633 if (IS_DEVVP(vp)) {
2631 2634 *vpp = specvp(vp, vp->v_rdev, vp->v_type, cr);
2632 2635 VN_RELE(vp);
2633 2636 } else
2634 2637 *vpp = vp;
2635 2638 } else {
2636 2639 nfs3_cache_wcc_data(dvp, &res.resfail.dir_wcc, t, cr);
2637 2640 PURGE_STALE_FH(error, dvp, cr);
2638 2641 }
2639 2642
2640 2643 return (error);
2641 2644 }
2642 2645
2643 2646 /*
2644 2647 * Special setattr function to take care of rest of atime/mtime
2645 2648 * after successful exclusive create. This function exists to avoid
2646 2649 * handling attributes from the server; exclusive the atime/mtime fields
2647 2650 * may be 'invalid' in client's view and therefore can not be trusted.
2648 2651 */
2649 2652 static int
2650 2653 nfs3excl_create_settimes(vnode_t *vp, struct vattr *vap, cred_t *cr)
2651 2654 {
2652 2655 int error;
2653 2656 uint_t mask;
2654 2657 SETATTR3args args;
2655 2658 SETATTR3res res;
2656 2659 int douprintf;
2657 2660 rnode_t *rp;
2658 2661 hrtime_t t;
2659 2662
2660 2663 ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
2661 2664 /* save the caller's mask so that it can be reset later */
2662 2665 mask = vap->va_mask;
2663 2666
2664 2667 rp = VTOR(vp);
2665 2668
2666 2669 args.object = *RTOFH3(rp);
2667 2670 args.guard.check = FALSE;
2668 2671
2669 2672 /* Use the mask to initialize the arguments */
2670 2673 vap->va_mask = 0;
2671 2674 error = vattr_to_sattr3(vap, &args.new_attributes);
2672 2675
2673 2676 /* We want to set just atime/mtime on this request */
2674 2677 args.new_attributes.atime.set_it = SET_TO_SERVER_TIME;
2675 2678 args.new_attributes.mtime.set_it = SET_TO_SERVER_TIME;
2676 2679
2677 2680 douprintf = 1;
2678 2681
2679 2682 t = gethrtime();
2680 2683
2681 2684 error = rfs3call(VTOMI(vp), NFSPROC3_SETATTR,
2682 2685 xdr_SETATTR3args, (caddr_t)&args,
2683 2686 xdr_SETATTR3res, (caddr_t)&res, cr,
2684 2687 &douprintf, &res.status, 0, NULL);
2685 2688
2686 2689 if (error) {
2687 2690 vap->va_mask = mask;
2688 2691 return (error);
2689 2692 }
2690 2693
2691 2694 error = geterrno3(res.status);
2692 2695 if (!error) {
2693 2696 /*
2694 2697 * It is important to pick up the attributes.
2695 2698 * Since this is the exclusive create path, the
2696 2699 * attributes on the initial create were ignored
2697 2700 * and we need these to have the correct info.
2698 2701 */
2699 2702 nfs3_cache_wcc_data(vp, &res.resok.obj_wcc, t, cr);
2700 2703 /*
2701 2704 * No need to do the atime/mtime work again so clear
2702 2705 * the bits.
2703 2706 */
2704 2707 mask &= ~(AT_ATIME | AT_MTIME);
2705 2708 } else {
2706 2709 nfs3_cache_wcc_data(vp, &res.resfail.obj_wcc, t, cr);
2707 2710 }
2708 2711
2709 2712 vap->va_mask = mask;
2710 2713
2711 2714 return (error);
2712 2715 }
2713 2716
2714 2717 /* ARGSUSED */
2715 2718 static int
2716 2719 nfs3mknod(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive,
2717 2720 int mode, vnode_t **vpp, cred_t *cr)
2718 2721 {
2719 2722 int error;
2720 2723 MKNOD3args args;
2721 2724 MKNOD3res res;
2722 2725 int douprintf;
2723 2726 vnode_t *vp;
2724 2727 struct vattr vattr;
2725 2728 hrtime_t t;
2726 2729
2727 2730 ASSERT(nfs_zone() == VTOMI(dvp)->mi_zone);
2728 2731 switch (va->va_type) {
2729 2732 case VCHR:
2730 2733 case VBLK:
2731 2734 setdiropargs3(&args.where, nm, dvp);
2732 2735 args.what.type = (va->va_type == VCHR) ? NF3CHR : NF3BLK;
2733 2736 error = vattr_to_sattr3(va,
2734 2737 &args.what.mknoddata3_u.device.dev_attributes);
2735 2738 if (error) {
2736 2739 /* req time field(s) overflow - return immediately */
2737 2740 return (error);
2738 2741 }
2739 2742 args.what.mknoddata3_u.device.spec.specdata1 =
2740 2743 getmajor(va->va_rdev);
2741 2744 args.what.mknoddata3_u.device.spec.specdata2 =
2742 2745 getminor(va->va_rdev);
2743 2746 break;
2744 2747
2745 2748 case VFIFO:
2746 2749 case VSOCK:
2747 2750 setdiropargs3(&args.where, nm, dvp);
2748 2751 args.what.type = (va->va_type == VFIFO) ? NF3FIFO : NF3SOCK;
2749 2752 error = vattr_to_sattr3(va,
2750 2753 &args.what.mknoddata3_u.pipe_attributes);
2751 2754 if (error) {
2752 2755 /* req time field(s) overflow - return immediately */
2753 2756 return (error);
2754 2757 }
2755 2758 break;
2756 2759
2757 2760 default:
2758 2761 return (EINVAL);
2759 2762 }
2760 2763
2761 2764 douprintf = 1;
2762 2765
2763 2766 t = gethrtime();
2764 2767
2765 2768 error = rfs3call(VTOMI(dvp), NFSPROC3_MKNOD,
2766 2769 xdr_MKNOD3args, (caddr_t)&args,
2767 2770 xdr_MKNOD3res, (caddr_t)&res, cr,
2768 2771 &douprintf, &res.status, 0, NULL);
2769 2772
2770 2773 if (error) {
2771 2774 PURGE_ATTRCACHE(dvp);
2772 2775 return (error);
2773 2776 }
2774 2777
2775 2778 error = geterrno3(res.status);
2776 2779 if (!error) {
2777 2780 nfs3_cache_wcc_data(dvp, &res.resok.dir_wcc, t, cr);
2778 2781 if (HAVE_RDDIR_CACHE(VTOR(dvp)))
2779 2782 nfs_purge_rddir_cache(dvp);
2780 2783
2781 2784 if (!res.resok.obj.handle_follows) {
2782 2785 error = nfs3lookup(dvp, nm, &vp, NULL, 0, NULL, cr, 0);
2783 2786 if (error)
2784 2787 return (error);
2785 2788 } else {
2786 2789 if (res.resok.obj_attributes.attributes) {
2787 2790 vp = makenfs3node(&res.resok.obj.handle,
2788 2791 &res.resok.obj_attributes.attr,
2789 2792 dvp->v_vfsp, t, cr, NULL, NULL);
2790 2793 } else {
2791 2794 vp = makenfs3node(&res.resok.obj.handle, NULL,
2792 2795 dvp->v_vfsp, t, cr, NULL, NULL);
2793 2796 if (vp->v_type == VNON) {
2794 2797 vattr.va_mask = AT_TYPE;
2795 2798 error = nfs3getattr(vp, &vattr, cr);
2796 2799 if (error) {
2797 2800 VN_RELE(vp);
2798 2801 return (error);
2799 2802 }
2800 2803 vp->v_type = vattr.va_type;
2801 2804 }
2802 2805
2803 2806 }
2804 2807 dnlc_update(dvp, nm, vp);
2805 2808 }
2806 2809
2807 2810 if (va->va_gid != VTOR(vp)->r_attr.va_gid) {
2808 2811 va->va_mask = AT_GID;
2809 2812 (void) nfs3setattr(vp, va, 0, cr);
2810 2813 }
2811 2814
2812 2815 /*
2813 2816 * If vnode is a device create special vnode
2814 2817 */
2815 2818 if (IS_DEVVP(vp)) {
2816 2819 *vpp = specvp(vp, vp->v_rdev, vp->v_type, cr);
2817 2820 VN_RELE(vp);
2818 2821 } else
2819 2822 *vpp = vp;
2820 2823 } else {
2821 2824 nfs3_cache_wcc_data(dvp, &res.resfail.dir_wcc, t, cr);
2822 2825 PURGE_STALE_FH(error, dvp, cr);
2823 2826 }
2824 2827 return (error);
2825 2828 }
2826 2829
2827 2830 /*
2828 2831 * Weirdness: if the vnode to be removed is open
2829 2832 * we rename it instead of removing it and nfs_inactive
2830 2833 * will remove the new name.
2831 2834 */
2832 2835 /* ARGSUSED */
2833 2836 static int
2834 2837 nfs3_remove(vnode_t *dvp, char *nm, cred_t *cr, caller_context_t *ct, int flags)
2835 2838 {
2836 2839 int error;
2837 2840 REMOVE3args args;
2838 2841 REMOVE3res res;
2839 2842 vnode_t *vp;
2840 2843 char *tmpname;
2841 2844 int douprintf;
2842 2845 rnode_t *rp;
2843 2846 rnode_t *drp;
2844 2847 hrtime_t t;
2845 2848
2846 2849 if (nfs_zone() != VTOMI(dvp)->mi_zone)
2847 2850 return (EPERM);
2848 2851 drp = VTOR(dvp);
2849 2852 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR(dvp)))
2850 2853 return (EINTR);
2851 2854
2852 2855 error = nfs3lookup(dvp, nm, &vp, NULL, 0, NULL, cr, 0);
2853 2856 if (error) {
2854 2857 nfs_rw_exit(&drp->r_rwlock);
2855 2858 return (error);
2856 2859 }
2857 2860
2858 2861 if (vp->v_type == VDIR && secpolicy_fs_linkdir(cr, dvp->v_vfsp)) {
2859 2862 VN_RELE(vp);
2860 2863 nfs_rw_exit(&drp->r_rwlock);
2861 2864 return (EPERM);
2862 2865 }
2863 2866
2864 2867 /*
2865 2868 * First just remove the entry from the name cache, as it
2866 2869 * is most likely the only entry for this vp.
2867 2870 */
2868 2871 dnlc_remove(dvp, nm);
2869 2872
2870 2873 /*
2871 2874 * If the file has a v_count > 1 then there may be more than one
2872 2875 * entry in the name cache due multiple links or an open file,
2873 2876 * but we don't have the real reference count so flush all
2874 2877 * possible entries.
2875 2878 */
2876 2879 if (vp->v_count > 1)
2877 2880 dnlc_purge_vp(vp);
2878 2881
2879 2882 /*
2880 2883 * Now we have the real reference count on the vnode
2881 2884 */
2882 2885 rp = VTOR(vp);
2883 2886 mutex_enter(&rp->r_statelock);
2884 2887 if (vp->v_count > 1 &&
2885 2888 (rp->r_unldvp == NULL || strcmp(nm, rp->r_unlname) == 0)) {
2886 2889 mutex_exit(&rp->r_statelock);
2887 2890 tmpname = newname();
2888 2891 error = nfs3rename(dvp, nm, dvp, tmpname, cr, ct);
2889 2892 if (error)
2890 2893 kmem_free(tmpname, MAXNAMELEN);
2891 2894 else {
2892 2895 mutex_enter(&rp->r_statelock);
2893 2896 if (rp->r_unldvp == NULL) {
2894 2897 VN_HOLD(dvp);
2895 2898 rp->r_unldvp = dvp;
2896 2899 if (rp->r_unlcred != NULL)
2897 2900 crfree(rp->r_unlcred);
2898 2901 crhold(cr);
2899 2902 rp->r_unlcred = cr;
2900 2903 rp->r_unlname = tmpname;
2901 2904 } else {
2902 2905 kmem_free(rp->r_unlname, MAXNAMELEN);
2903 2906 rp->r_unlname = tmpname;
2904 2907 }
2905 2908 mutex_exit(&rp->r_statelock);
2906 2909 }
2907 2910 } else {
2908 2911 mutex_exit(&rp->r_statelock);
2909 2912 /*
2910 2913 * We need to flush any dirty pages which happen to
2911 2914 * be hanging around before removing the file. This
2912 2915 * shouldn't happen very often and mostly on file
2913 2916 * systems mounted "nocto".
2914 2917 */
2915 2918 if (vn_has_cached_data(vp) &&
2916 2919 ((rp->r_flags & RDIRTY) || rp->r_count > 0)) {
2917 2920 error = nfs3_putpage(vp, (offset_t)0, 0, 0, cr, ct);
2918 2921 if (error && (error == ENOSPC || error == EDQUOT)) {
2919 2922 mutex_enter(&rp->r_statelock);
2920 2923 if (!rp->r_error)
2921 2924 rp->r_error = error;
2922 2925 mutex_exit(&rp->r_statelock);
2923 2926 }
2924 2927 }
2925 2928
2926 2929 setdiropargs3(&args.object, nm, dvp);
2927 2930
2928 2931 douprintf = 1;
2929 2932
2930 2933 t = gethrtime();
2931 2934
2932 2935 error = rfs3call(VTOMI(dvp), NFSPROC3_REMOVE,
2933 2936 xdr_diropargs3, (caddr_t)&args,
2934 2937 xdr_REMOVE3res, (caddr_t)&res, cr,
2935 2938 &douprintf, &res.status, 0, NULL);
2936 2939
2937 2940 /*
2938 2941 * The xattr dir may be gone after last attr is removed,
2939 2942 * so flush it from dnlc.
2940 2943 */
2941 2944 if (dvp->v_flag & V_XATTRDIR)
2942 2945 dnlc_purge_vp(dvp);
2943 2946
2944 2947 PURGE_ATTRCACHE(vp);
2945 2948
2946 2949 if (error) {
2947 2950 PURGE_ATTRCACHE(dvp);
2948 2951 } else {
2949 2952 error = geterrno3(res.status);
2950 2953 if (!error) {
2951 2954 nfs3_cache_wcc_data(dvp, &res.resok.dir_wcc, t,
2952 2955 cr);
2953 2956 if (HAVE_RDDIR_CACHE(drp))
2954 2957 nfs_purge_rddir_cache(dvp);
2955 2958 } else {
2956 2959 nfs3_cache_wcc_data(dvp, &res.resfail.dir_wcc,
2957 2960 t, cr);
2958 2961 PURGE_STALE_FH(error, dvp, cr);
2959 2962 }
2960 2963 }
2961 2964 }
2962 2965
2963 2966 if (error == 0) {
2964 2967 vnevent_remove(vp, dvp, nm, ct);
2965 2968 }
2966 2969 VN_RELE(vp);
2967 2970
2968 2971 nfs_rw_exit(&drp->r_rwlock);
2969 2972
2970 2973 return (error);
2971 2974 }
2972 2975
2973 2976 /* ARGSUSED */
2974 2977 static int
2975 2978 nfs3_link(vnode_t *tdvp, vnode_t *svp, char *tnm, cred_t *cr,
2976 2979 caller_context_t *ct, int flags)
2977 2980 {
2978 2981 int error;
2979 2982 LINK3args args;
2980 2983 LINK3res res;
2981 2984 vnode_t *realvp;
2982 2985 int douprintf;
2983 2986 mntinfo_t *mi;
2984 2987 rnode_t *tdrp;
2985 2988 hrtime_t t;
2986 2989
2987 2990 if (nfs_zone() != VTOMI(tdvp)->mi_zone)
2988 2991 return (EPERM);
2989 2992 if (VOP_REALVP(svp, &realvp, ct) == 0)
2990 2993 svp = realvp;
2991 2994
2992 2995 mi = VTOMI(svp);
2993 2996
2994 2997 if (!(mi->mi_flags & MI_LINK))
2995 2998 return (EOPNOTSUPP);
2996 2999
2997 3000 args.file = *VTOFH3(svp);
2998 3001 setdiropargs3(&args.link, tnm, tdvp);
2999 3002
3000 3003 tdrp = VTOR(tdvp);
3001 3004 if (nfs_rw_enter_sig(&tdrp->r_rwlock, RW_WRITER, INTR(tdvp)))
3002 3005 return (EINTR);
3003 3006
3004 3007 dnlc_remove(tdvp, tnm);
3005 3008
3006 3009 douprintf = 1;
3007 3010
3008 3011 t = gethrtime();
3009 3012
3010 3013 error = rfs3call(mi, NFSPROC3_LINK,
3011 3014 xdr_LINK3args, (caddr_t)&args,
3012 3015 xdr_LINK3res, (caddr_t)&res, cr,
3013 3016 &douprintf, &res.status, 0, NULL);
3014 3017
3015 3018 if (error) {
3016 3019 PURGE_ATTRCACHE(tdvp);
3017 3020 PURGE_ATTRCACHE(svp);
3018 3021 nfs_rw_exit(&tdrp->r_rwlock);
3019 3022 return (error);
3020 3023 }
3021 3024
3022 3025 error = geterrno3(res.status);
3023 3026
3024 3027 if (!error) {
3025 3028 nfs3_cache_post_op_attr(svp, &res.resok.file_attributes, t, cr);
3026 3029 nfs3_cache_wcc_data(tdvp, &res.resok.linkdir_wcc, t, cr);
3027 3030 if (HAVE_RDDIR_CACHE(tdrp))
3028 3031 nfs_purge_rddir_cache(tdvp);
3029 3032 dnlc_update(tdvp, tnm, svp);
3030 3033 } else {
3031 3034 nfs3_cache_post_op_attr(svp, &res.resfail.file_attributes, t,
3032 3035 cr);
3033 3036 nfs3_cache_wcc_data(tdvp, &res.resfail.linkdir_wcc, t, cr);
3034 3037 if (error == EOPNOTSUPP) {
3035 3038 mutex_enter(&mi->mi_lock);
3036 3039 mi->mi_flags &= ~MI_LINK;
3037 3040 mutex_exit(&mi->mi_lock);
3038 3041 }
3039 3042 }
3040 3043
3041 3044 nfs_rw_exit(&tdrp->r_rwlock);
3042 3045
3043 3046 if (!error) {
3044 3047 /*
3045 3048 * Notify the source file of this link operation.
3046 3049 */
3047 3050 vnevent_link(svp, ct);
3048 3051 }
3049 3052 return (error);
3050 3053 }
3051 3054
3052 3055 /* ARGSUSED */
3053 3056 static int
3054 3057 nfs3_rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr,
3055 3058 caller_context_t *ct, int flags)
3056 3059 {
3057 3060 vnode_t *realvp;
3058 3061
3059 3062 if (nfs_zone() != VTOMI(odvp)->mi_zone)
3060 3063 return (EPERM);
3061 3064 if (VOP_REALVP(ndvp, &realvp, ct) == 0)
3062 3065 ndvp = realvp;
3063 3066
3064 3067 return (nfs3rename(odvp, onm, ndvp, nnm, cr, ct));
3065 3068 }
3066 3069
3067 3070 /*
3068 3071 * nfs3rename does the real work of renaming in NFS Version 3.
3069 3072 */
3070 3073 static int
3071 3074 nfs3rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr,
3072 3075 caller_context_t *ct)
3073 3076 {
3074 3077 int error;
3075 3078 RENAME3args args;
3076 3079 RENAME3res res;
3077 3080 int douprintf;
3078 3081 vnode_t *nvp = NULL;
3079 3082 vnode_t *ovp = NULL;
3080 3083 char *tmpname;
3081 3084 rnode_t *rp;
3082 3085 rnode_t *odrp;
3083 3086 rnode_t *ndrp;
3084 3087 hrtime_t t;
3085 3088
3086 3089 ASSERT(nfs_zone() == VTOMI(odvp)->mi_zone);
3087 3090
3088 3091 if (strcmp(onm, ".") == 0 || strcmp(onm, "..") == 0 ||
3089 3092 strcmp(nnm, ".") == 0 || strcmp(nnm, "..") == 0)
3090 3093 return (EINVAL);
3091 3094
3092 3095 odrp = VTOR(odvp);
3093 3096 ndrp = VTOR(ndvp);
3094 3097 if ((intptr_t)odrp < (intptr_t)ndrp) {
3095 3098 if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR(odvp)))
3096 3099 return (EINTR);
3097 3100 if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR(ndvp))) {
3098 3101 nfs_rw_exit(&odrp->r_rwlock);
3099 3102 return (EINTR);
3100 3103 }
3101 3104 } else {
3102 3105 if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR(ndvp)))
3103 3106 return (EINTR);
3104 3107 if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR(odvp))) {
3105 3108 nfs_rw_exit(&ndrp->r_rwlock);
3106 3109 return (EINTR);
3107 3110 }
3108 3111 }
3109 3112
3110 3113 /*
3111 3114 * Lookup the target file. If it exists, it needs to be
3112 3115 * checked to see whether it is a mount point and whether
3113 3116 * it is active (open).
3114 3117 */
3115 3118 error = nfs3lookup(ndvp, nnm, &nvp, NULL, 0, NULL, cr, 0);
3116 3119 if (!error) {
3117 3120 /*
3118 3121 * If this file has been mounted on, then just
3119 3122 * return busy because renaming to it would remove
3120 3123 * the mounted file system from the name space.
3121 3124 */
3122 3125 if (vn_mountedvfs(nvp) != NULL) {
3123 3126 VN_RELE(nvp);
3124 3127 nfs_rw_exit(&odrp->r_rwlock);
3125 3128 nfs_rw_exit(&ndrp->r_rwlock);
3126 3129 return (EBUSY);
3127 3130 }
3128 3131
3129 3132 /*
3130 3133 * Purge the name cache of all references to this vnode
3131 3134 * so that we can check the reference count to infer
3132 3135 * whether it is active or not.
3133 3136 */
3134 3137 /*
3135 3138 * First just remove the entry from the name cache, as it
3136 3139 * is most likely the only entry for this vp.
3137 3140 */
3138 3141 dnlc_remove(ndvp, nnm);
3139 3142 /*
3140 3143 * If the file has a v_count > 1 then there may be more
3141 3144 * than one entry in the name cache due multiple links
3142 3145 * or an open file, but we don't have the real reference
3143 3146 * count so flush all possible entries.
3144 3147 */
3145 3148 if (nvp->v_count > 1)
3146 3149 dnlc_purge_vp(nvp);
3147 3150
3148 3151 /*
3149 3152 * If the vnode is active and is not a directory,
3150 3153 * arrange to rename it to a
3151 3154 * temporary file so that it will continue to be
3152 3155 * accessible. This implements the "unlink-open-file"
3153 3156 * semantics for the target of a rename operation.
3154 3157 * Before doing this though, make sure that the
3155 3158 * source and target files are not already the same.
3156 3159 */
3157 3160 if (nvp->v_count > 1 && nvp->v_type != VDIR) {
3158 3161 /*
3159 3162 * Lookup the source name.
3160 3163 */
3161 3164 error = nfs3lookup(odvp, onm, &ovp, NULL, 0, NULL,
3162 3165 cr, 0);
3163 3166
3164 3167 /*
3165 3168 * The source name *should* already exist.
3166 3169 */
3167 3170 if (error) {
3168 3171 VN_RELE(nvp);
3169 3172 nfs_rw_exit(&odrp->r_rwlock);
3170 3173 nfs_rw_exit(&ndrp->r_rwlock);
3171 3174 return (error);
3172 3175 }
3173 3176
3174 3177 /*
3175 3178 * Compare the two vnodes. If they are the same,
3176 3179 * just release all held vnodes and return success.
3177 3180 */
3178 3181 if (ovp == nvp) {
3179 3182 VN_RELE(ovp);
3180 3183 VN_RELE(nvp);
3181 3184 nfs_rw_exit(&odrp->r_rwlock);
3182 3185 nfs_rw_exit(&ndrp->r_rwlock);
3183 3186 return (0);
3184 3187 }
3185 3188
3186 3189 /*
3187 3190 * Can't mix and match directories and non-
3188 3191 * directories in rename operations. We already
3189 3192 * know that the target is not a directory. If
3190 3193 * the source is a directory, return an error.
3191 3194 */
3192 3195 if (ovp->v_type == VDIR) {
3193 3196 VN_RELE(ovp);
3194 3197 VN_RELE(nvp);
3195 3198 nfs_rw_exit(&odrp->r_rwlock);
3196 3199 nfs_rw_exit(&ndrp->r_rwlock);
3197 3200 return (ENOTDIR);
3198 3201 }
3199 3202
3200 3203 /*
3201 3204 * The target file exists, is not the same as
3202 3205 * the source file, and is active. Link it
3203 3206 * to a temporary filename to avoid having
3204 3207 * the server removing the file completely.
3205 3208 */
3206 3209 tmpname = newname();
3207 3210 error = nfs3_link(ndvp, nvp, tmpname, cr, NULL, 0);
3208 3211 if (error == EOPNOTSUPP) {
3209 3212 error = nfs3_rename(ndvp, nnm, ndvp, tmpname,
3210 3213 cr, NULL, 0);
3211 3214 }
3212 3215 if (error) {
3213 3216 kmem_free(tmpname, MAXNAMELEN);
3214 3217 VN_RELE(ovp);
3215 3218 VN_RELE(nvp);
3216 3219 nfs_rw_exit(&odrp->r_rwlock);
3217 3220 nfs_rw_exit(&ndrp->r_rwlock);
3218 3221 return (error);
3219 3222 }
3220 3223 rp = VTOR(nvp);
3221 3224 mutex_enter(&rp->r_statelock);
3222 3225 if (rp->r_unldvp == NULL) {
3223 3226 VN_HOLD(ndvp);
3224 3227 rp->r_unldvp = ndvp;
3225 3228 if (rp->r_unlcred != NULL)
3226 3229 crfree(rp->r_unlcred);
3227 3230 crhold(cr);
3228 3231 rp->r_unlcred = cr;
3229 3232 rp->r_unlname = tmpname;
3230 3233 } else {
3231 3234 kmem_free(rp->r_unlname, MAXNAMELEN);
3232 3235 rp->r_unlname = tmpname;
3233 3236 }
3234 3237 mutex_exit(&rp->r_statelock);
3235 3238 }
3236 3239 }
3237 3240
3238 3241 if (ovp == NULL) {
3239 3242 /*
3240 3243 * When renaming directories to be a subdirectory of a
3241 3244 * different parent, the dnlc entry for ".." will no
3242 3245 * longer be valid, so it must be removed.
3243 3246 *
3244 3247 * We do a lookup here to determine whether we are renaming
3245 3248 * a directory and we need to check if we are renaming
3246 3249 * an unlinked file. This might have already been done
3247 3250 * in previous code, so we check ovp == NULL to avoid
3248 3251 * doing it twice.
3249 3252 */
3250 3253
3251 3254 error = nfs3lookup(odvp, onm, &ovp, NULL, 0, NULL, cr, 0);
3252 3255 /*
3253 3256 * The source name *should* already exist.
3254 3257 */
3255 3258 if (error) {
3256 3259 nfs_rw_exit(&odrp->r_rwlock);
3257 3260 nfs_rw_exit(&ndrp->r_rwlock);
3258 3261 if (nvp) {
3259 3262 VN_RELE(nvp);
3260 3263 }
3261 3264 return (error);
3262 3265 }
3263 3266 ASSERT(ovp != NULL);
3264 3267 }
3265 3268
3266 3269 dnlc_remove(odvp, onm);
3267 3270 dnlc_remove(ndvp, nnm);
3268 3271
3269 3272 setdiropargs3(&args.from, onm, odvp);
3270 3273 setdiropargs3(&args.to, nnm, ndvp);
3271 3274
3272 3275 douprintf = 1;
3273 3276
3274 3277 t = gethrtime();
3275 3278
3276 3279 error = rfs3call(VTOMI(odvp), NFSPROC3_RENAME,
3277 3280 xdr_RENAME3args, (caddr_t)&args,
3278 3281 xdr_RENAME3res, (caddr_t)&res, cr,
3279 3282 &douprintf, &res.status, 0, NULL);
3280 3283
3281 3284 if (error) {
3282 3285 PURGE_ATTRCACHE(odvp);
3283 3286 PURGE_ATTRCACHE(ndvp);
3284 3287 VN_RELE(ovp);
3285 3288 nfs_rw_exit(&odrp->r_rwlock);
3286 3289 nfs_rw_exit(&ndrp->r_rwlock);
3287 3290 if (nvp) {
3288 3291 VN_RELE(nvp);
3289 3292 }
3290 3293 return (error);
3291 3294 }
3292 3295
3293 3296 error = geterrno3(res.status);
3294 3297
3295 3298 if (!error) {
3296 3299 nfs3_cache_wcc_data(odvp, &res.resok.fromdir_wcc, t, cr);
3297 3300 if (HAVE_RDDIR_CACHE(odrp))
3298 3301 nfs_purge_rddir_cache(odvp);
3299 3302 if (ndvp != odvp) {
3300 3303 nfs3_cache_wcc_data(ndvp, &res.resok.todir_wcc, t, cr);
3301 3304 if (HAVE_RDDIR_CACHE(ndrp))
3302 3305 nfs_purge_rddir_cache(ndvp);
3303 3306 }
3304 3307 /*
3305 3308 * when renaming directories to be a subdirectory of a
3306 3309 * different parent, the dnlc entry for ".." will no
3307 3310 * longer be valid, so it must be removed
3308 3311 */
3309 3312 rp = VTOR(ovp);
3310 3313 if (ndvp != odvp) {
3311 3314 if (ovp->v_type == VDIR) {
3312 3315 dnlc_remove(ovp, "..");
3313 3316 if (HAVE_RDDIR_CACHE(rp))
3314 3317 nfs_purge_rddir_cache(ovp);
3315 3318 }
3316 3319 }
3317 3320
3318 3321 /*
3319 3322 * If we are renaming the unlinked file, update the
3320 3323 * r_unldvp and r_unlname as needed.
3321 3324 */
3322 3325 mutex_enter(&rp->r_statelock);
3323 3326 if (rp->r_unldvp != NULL) {
3324 3327 if (strcmp(rp->r_unlname, onm) == 0) {
3325 3328 (void) strncpy(rp->r_unlname, nnm, MAXNAMELEN);
3326 3329 rp->r_unlname[MAXNAMELEN - 1] = '\0';
3327 3330
3328 3331 if (ndvp != rp->r_unldvp) {
3329 3332 VN_RELE(rp->r_unldvp);
3330 3333 rp->r_unldvp = ndvp;
3331 3334 VN_HOLD(ndvp);
3332 3335 }
3333 3336 }
3334 3337 }
3335 3338 mutex_exit(&rp->r_statelock);
3336 3339 } else {
3337 3340 nfs3_cache_wcc_data(odvp, &res.resfail.fromdir_wcc, t, cr);
3338 3341 if (ndvp != odvp) {
3339 3342 nfs3_cache_wcc_data(ndvp, &res.resfail.todir_wcc, t,
3340 3343 cr);
3341 3344 }
3342 3345 /*
3343 3346 * System V defines rename to return EEXIST, not
3344 3347 * ENOTEMPTY if the target directory is not empty.
3345 3348 * Over the wire, the error is NFSERR_ENOTEMPTY
3346 3349 * which geterrno maps to ENOTEMPTY.
3347 3350 */
3348 3351 if (error == ENOTEMPTY)
3349 3352 error = EEXIST;
3350 3353 }
3351 3354
3352 3355 if (error == 0) {
3353 3356 if (nvp)
3354 3357 vnevent_rename_dest(nvp, ndvp, nnm, ct);
3355 3358
3356 3359 if (odvp != ndvp)
3357 3360 vnevent_rename_dest_dir(ndvp, ct);
3358 3361 ASSERT(ovp != NULL);
3359 3362 vnevent_rename_src(ovp, odvp, onm, ct);
3360 3363 }
3361 3364
3362 3365 if (nvp) {
3363 3366 VN_RELE(nvp);
3364 3367 }
3365 3368 VN_RELE(ovp);
3366 3369
3367 3370 nfs_rw_exit(&odrp->r_rwlock);
3368 3371 nfs_rw_exit(&ndrp->r_rwlock);
3369 3372
3370 3373 return (error);
3371 3374 }
3372 3375
3373 3376 /* ARGSUSED */
3374 3377 static int
3375 3378 nfs3_mkdir(vnode_t *dvp, char *nm, struct vattr *va, vnode_t **vpp, cred_t *cr,
3376 3379 caller_context_t *ct, int flags, vsecattr_t *vsecp)
3377 3380 {
3378 3381 int error;
3379 3382 MKDIR3args args;
3380 3383 MKDIR3res res;
3381 3384 int douprintf;
3382 3385 struct vattr vattr;
3383 3386 vnode_t *vp;
3384 3387 rnode_t *drp;
3385 3388 hrtime_t t;
3386 3389
3387 3390 if (nfs_zone() != VTOMI(dvp)->mi_zone)
3388 3391 return (EPERM);
3389 3392 setdiropargs3(&args.where, nm, dvp);
3390 3393
3391 3394 /*
3392 3395 * Decide what the group-id and set-gid bit of the created directory
3393 3396 * should be. May have to do a setattr to get the gid right.
3394 3397 */
3395 3398 error = setdirgid(dvp, &va->va_gid, cr);
3396 3399 if (error)
3397 3400 return (error);
3398 3401 error = setdirmode(dvp, &va->va_mode, cr);
3399 3402 if (error)
3400 3403 return (error);
3401 3404 va->va_mask |= AT_MODE|AT_GID;
3402 3405
3403 3406 error = vattr_to_sattr3(va, &args.attributes);
3404 3407 if (error) {
3405 3408 /* req time field(s) overflow - return immediately */
3406 3409 return (error);
3407 3410 }
3408 3411
3409 3412 drp = VTOR(dvp);
3410 3413 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR(dvp)))
3411 3414 return (EINTR);
3412 3415
3413 3416 dnlc_remove(dvp, nm);
3414 3417
3415 3418 douprintf = 1;
3416 3419
3417 3420 t = gethrtime();
3418 3421
3419 3422 error = rfs3call(VTOMI(dvp), NFSPROC3_MKDIR,
3420 3423 xdr_MKDIR3args, (caddr_t)&args,
3421 3424 xdr_MKDIR3res, (caddr_t)&res, cr,
3422 3425 &douprintf, &res.status, 0, NULL);
3423 3426
3424 3427 if (error) {
3425 3428 PURGE_ATTRCACHE(dvp);
3426 3429 nfs_rw_exit(&drp->r_rwlock);
3427 3430 return (error);
3428 3431 }
3429 3432
3430 3433 error = geterrno3(res.status);
3431 3434 if (!error) {
3432 3435 nfs3_cache_wcc_data(dvp, &res.resok.dir_wcc, t, cr);
3433 3436 if (HAVE_RDDIR_CACHE(drp))
3434 3437 nfs_purge_rddir_cache(dvp);
3435 3438
3436 3439 if (!res.resok.obj.handle_follows) {
3437 3440 error = nfs3lookup(dvp, nm, &vp, NULL, 0, NULL, cr, 0);
3438 3441 if (error) {
3439 3442 nfs_rw_exit(&drp->r_rwlock);
3440 3443 return (error);
3441 3444 }
3442 3445 } else {
3443 3446 if (res.resok.obj_attributes.attributes) {
3444 3447 vp = makenfs3node(&res.resok.obj.handle,
3445 3448 &res.resok.obj_attributes.attr,
3446 3449 dvp->v_vfsp, t, cr, NULL, NULL);
3447 3450 } else {
3448 3451 vp = makenfs3node(&res.resok.obj.handle, NULL,
3449 3452 dvp->v_vfsp, t, cr, NULL, NULL);
3450 3453 if (vp->v_type == VNON) {
3451 3454 vattr.va_mask = AT_TYPE;
3452 3455 error = nfs3getattr(vp, &vattr, cr);
3453 3456 if (error) {
3454 3457 VN_RELE(vp);
3455 3458 nfs_rw_exit(&drp->r_rwlock);
3456 3459 return (error);
3457 3460 }
3458 3461 vp->v_type = vattr.va_type;
3459 3462 }
3460 3463 }
3461 3464 dnlc_update(dvp, nm, vp);
3462 3465 }
3463 3466 if (va->va_gid != VTOR(vp)->r_attr.va_gid) {
3464 3467 va->va_mask = AT_GID;
3465 3468 (void) nfs3setattr(vp, va, 0, cr);
3466 3469 }
3467 3470 *vpp = vp;
3468 3471 } else {
3469 3472 nfs3_cache_wcc_data(dvp, &res.resfail.dir_wcc, t, cr);
3470 3473 PURGE_STALE_FH(error, dvp, cr);
3471 3474 }
3472 3475
3473 3476 nfs_rw_exit(&drp->r_rwlock);
3474 3477
3475 3478 return (error);
3476 3479 }
3477 3480
3478 3481 /* ARGSUSED */
3479 3482 static int
3480 3483 nfs3_rmdir(vnode_t *dvp, char *nm, vnode_t *cdir, cred_t *cr,
3481 3484 caller_context_t *ct, int flags)
3482 3485 {
3483 3486 int error;
3484 3487 RMDIR3args args;
3485 3488 RMDIR3res res;
3486 3489 vnode_t *vp;
3487 3490 int douprintf;
3488 3491 rnode_t *drp;
3489 3492 hrtime_t t;
3490 3493
3491 3494 if (nfs_zone() != VTOMI(dvp)->mi_zone)
3492 3495 return (EPERM);
3493 3496 drp = VTOR(dvp);
3494 3497 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR(dvp)))
3495 3498 return (EINTR);
3496 3499
3497 3500 /*
3498 3501 * Attempt to prevent a rmdir(".") from succeeding.
3499 3502 */
3500 3503 error = nfs3lookup(dvp, nm, &vp, NULL, 0, NULL, cr, 0);
3501 3504 if (error) {
3502 3505 nfs_rw_exit(&drp->r_rwlock);
3503 3506 return (error);
3504 3507 }
3505 3508
3506 3509 if (vp == cdir) {
3507 3510 VN_RELE(vp);
3508 3511 nfs_rw_exit(&drp->r_rwlock);
3509 3512 return (EINVAL);
3510 3513 }
3511 3514
3512 3515 setdiropargs3(&args.object, nm, dvp);
3513 3516
3514 3517 /*
3515 3518 * First just remove the entry from the name cache, as it
3516 3519 * is most likely an entry for this vp.
3517 3520 */
3518 3521 dnlc_remove(dvp, nm);
3519 3522
3520 3523 /*
3521 3524 * If there vnode reference count is greater than one, then
3522 3525 * there may be additional references in the DNLC which will
3523 3526 * need to be purged. First, trying removing the entry for
3524 3527 * the parent directory and see if that removes the additional
3525 3528 * reference(s). If that doesn't do it, then use dnlc_purge_vp
3526 3529 * to completely remove any references to the directory which
3527 3530 * might still exist in the DNLC.
3528 3531 */
3529 3532 if (vp->v_count > 1) {
3530 3533 dnlc_remove(vp, "..");
3531 3534 if (vp->v_count > 1)
3532 3535 dnlc_purge_vp(vp);
3533 3536 }
3534 3537
3535 3538 douprintf = 1;
3536 3539
3537 3540 t = gethrtime();
3538 3541
3539 3542 error = rfs3call(VTOMI(dvp), NFSPROC3_RMDIR,
3540 3543 xdr_diropargs3, (caddr_t)&args,
3541 3544 xdr_RMDIR3res, (caddr_t)&res, cr,
3542 3545 &douprintf, &res.status, 0, NULL);
3543 3546
3544 3547 PURGE_ATTRCACHE(vp);
3545 3548
3546 3549 if (error) {
3547 3550 PURGE_ATTRCACHE(dvp);
3548 3551 VN_RELE(vp);
3549 3552 nfs_rw_exit(&drp->r_rwlock);
3550 3553 return (error);
3551 3554 }
3552 3555
3553 3556 error = geterrno3(res.status);
3554 3557 if (!error) {
3555 3558 nfs3_cache_wcc_data(dvp, &res.resok.dir_wcc, t, cr);
3556 3559 if (HAVE_RDDIR_CACHE(drp))
3557 3560 nfs_purge_rddir_cache(dvp);
3558 3561 if (HAVE_RDDIR_CACHE(VTOR(vp)))
3559 3562 nfs_purge_rddir_cache(vp);
3560 3563 } else {
3561 3564 nfs3_cache_wcc_data(dvp, &res.resfail.dir_wcc, t, cr);
3562 3565 PURGE_STALE_FH(error, dvp, cr);
3563 3566 /*
3564 3567 * System V defines rmdir to return EEXIST, not
3565 3568 * ENOTEMPTY if the directory is not empty. Over
3566 3569 * the wire, the error is NFSERR_ENOTEMPTY which
3567 3570 * geterrno maps to ENOTEMPTY.
3568 3571 */
3569 3572 if (error == ENOTEMPTY)
3570 3573 error = EEXIST;
3571 3574 }
3572 3575
3573 3576 if (error == 0) {
3574 3577 vnevent_rmdir(vp, dvp, nm, ct);
3575 3578 }
3576 3579 VN_RELE(vp);
3577 3580
3578 3581 nfs_rw_exit(&drp->r_rwlock);
3579 3582
3580 3583 return (error);
3581 3584 }
3582 3585
3583 3586 /* ARGSUSED */
3584 3587 static int
3585 3588 nfs3_symlink(vnode_t *dvp, char *lnm, struct vattr *tva, char *tnm, cred_t *cr,
3586 3589 caller_context_t *ct, int flags)
3587 3590 {
3588 3591 int error;
3589 3592 SYMLINK3args args;
3590 3593 SYMLINK3res res;
3591 3594 int douprintf;
3592 3595 mntinfo_t *mi;
3593 3596 vnode_t *vp;
3594 3597 rnode_t *rp;
3595 3598 char *contents;
3596 3599 rnode_t *drp;
3597 3600 hrtime_t t;
3598 3601
3599 3602 mi = VTOMI(dvp);
3600 3603
3601 3604 if (nfs_zone() != mi->mi_zone)
3602 3605 return (EPERM);
3603 3606 if (!(mi->mi_flags & MI_SYMLINK))
3604 3607 return (EOPNOTSUPP);
3605 3608
3606 3609 setdiropargs3(&args.where, lnm, dvp);
3607 3610 error = vattr_to_sattr3(tva, &args.symlink.symlink_attributes);
3608 3611 if (error) {
3609 3612 /* req time field(s) overflow - return immediately */
3610 3613 return (error);
3611 3614 }
3612 3615 args.symlink.symlink_data = tnm;
3613 3616
3614 3617 drp = VTOR(dvp);
3615 3618 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR(dvp)))
3616 3619 return (EINTR);
3617 3620
3618 3621 dnlc_remove(dvp, lnm);
3619 3622
3620 3623 douprintf = 1;
3621 3624
3622 3625 t = gethrtime();
3623 3626
3624 3627 error = rfs3call(mi, NFSPROC3_SYMLINK,
3625 3628 xdr_SYMLINK3args, (caddr_t)&args,
3626 3629 xdr_SYMLINK3res, (caddr_t)&res, cr,
3627 3630 &douprintf, &res.status, 0, NULL);
3628 3631
3629 3632 if (error) {
3630 3633 PURGE_ATTRCACHE(dvp);
3631 3634 nfs_rw_exit(&drp->r_rwlock);
3632 3635 return (error);
3633 3636 }
3634 3637
3635 3638 error = geterrno3(res.status);
3636 3639 if (!error) {
3637 3640 nfs3_cache_wcc_data(dvp, &res.resok.dir_wcc, t, cr);
3638 3641 if (HAVE_RDDIR_CACHE(drp))
3639 3642 nfs_purge_rddir_cache(dvp);
3640 3643
3641 3644 if (res.resok.obj.handle_follows) {
3642 3645 if (res.resok.obj_attributes.attributes) {
3643 3646 vp = makenfs3node(&res.resok.obj.handle,
3644 3647 &res.resok.obj_attributes.attr,
3645 3648 dvp->v_vfsp, t, cr, NULL, NULL);
3646 3649 } else {
3647 3650 vp = makenfs3node(&res.resok.obj.handle, NULL,
3648 3651 dvp->v_vfsp, t, cr, NULL, NULL);
3649 3652 vp->v_type = VLNK;
3650 3653 vp->v_rdev = 0;
3651 3654 }
3652 3655 dnlc_update(dvp, lnm, vp);
3653 3656 rp = VTOR(vp);
3654 3657 if (nfs3_do_symlink_cache &&
3655 3658 rp->r_symlink.contents == NULL) {
3656 3659
3657 3660 contents = kmem_alloc(MAXPATHLEN,
3658 3661 KM_NOSLEEP);
3659 3662
3660 3663 if (contents != NULL) {
3661 3664 mutex_enter(&rp->r_statelock);
3662 3665 if (rp->r_symlink.contents == NULL) {
3663 3666 rp->r_symlink.len = strlen(tnm);
3664 3667 bcopy(tnm, contents,
3665 3668 rp->r_symlink.len);
3666 3669 rp->r_symlink.contents =
3667 3670 contents;
3668 3671 rp->r_symlink.size = MAXPATHLEN;
3669 3672 mutex_exit(&rp->r_statelock);
3670 3673 } else {
3671 3674 mutex_exit(&rp->r_statelock);
3672 3675 kmem_free((void *)contents,
3673 3676 MAXPATHLEN);
3674 3677 }
3675 3678 }
3676 3679 }
3677 3680 VN_RELE(vp);
3678 3681 }
3679 3682 } else {
3680 3683 nfs3_cache_wcc_data(dvp, &res.resfail.dir_wcc, t, cr);
3681 3684 PURGE_STALE_FH(error, dvp, cr);
3682 3685 if (error == EOPNOTSUPP) {
3683 3686 mutex_enter(&mi->mi_lock);
3684 3687 mi->mi_flags &= ~MI_SYMLINK;
3685 3688 mutex_exit(&mi->mi_lock);
3686 3689 }
3687 3690 }
3688 3691
3689 3692 nfs_rw_exit(&drp->r_rwlock);
3690 3693
3691 3694 return (error);
|
↓ open down ↓ |
1685 lines elided |
↑ open up ↑ |
3692 3695 }
3693 3696
3694 3697 #ifdef DEBUG
3695 3698 static int nfs3_readdir_cache_hits = 0;
3696 3699 static int nfs3_readdir_cache_shorts = 0;
3697 3700 static int nfs3_readdir_cache_waits = 0;
3698 3701 static int nfs3_readdir_cache_misses = 0;
3699 3702 static int nfs3_readdir_readahead = 0;
3700 3703 #endif
3701 3704
3702 -static int nfs3_shrinkreaddir = 0;
3705 +volatile int nfs3_shrinkreaddir = 0;
3703 3706
3704 3707 /*
3705 3708 * Read directory entries.
3706 3709 * There are some weird things to look out for here. The uio_loffset
3707 3710 * field is either 0 or it is the offset returned from a previous
3708 3711 * readdir. It is an opaque value used by the server to find the
3709 3712 * correct directory block to read. The count field is the number
3710 3713 * of blocks to read on the server. This is advisory only, the server
3711 3714 * may return only one block's worth of entries. Entries may be compressed
3712 3715 * on the server.
3713 3716 */
3714 3717 /* ARGSUSED */
3715 3718 static int
3716 3719 nfs3_readdir(vnode_t *vp, struct uio *uiop, cred_t *cr, int *eofp,
3717 3720 caller_context_t *ct, int flags)
3718 3721 {
3719 3722 int error;
3720 3723 size_t count;
3721 3724 rnode_t *rp;
3722 3725 rddir_cache *rdc;
3723 3726 rddir_cache *nrdc;
3724 3727 rddir_cache *rrdc;
3725 3728 #ifdef DEBUG
3726 3729 int missed;
3727 3730 #endif
3728 3731 int doreadahead;
3729 3732 rddir_cache srdc;
3730 3733 avl_index_t where;
3731 3734
3732 3735 if (nfs_zone() != VTOMI(vp)->mi_zone)
3733 3736 return (EIO);
3734 3737 rp = VTOR(vp);
3735 3738
3736 3739 ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER));
3737 3740
3738 3741 /*
3739 3742 * Make sure that the directory cache is valid.
3740 3743 */
3741 3744 if (HAVE_RDDIR_CACHE(rp)) {
3742 3745 if (nfs_disable_rddir_cache) {
3743 3746 /*
3744 3747 * Setting nfs_disable_rddir_cache in /etc/system
3745 3748 * allows interoperability with servers that do not
3746 3749 * properly update the attributes of directories.
3747 3750 * Any cached information gets purged before an
3748 3751 * access is made to it.
3749 3752 */
3750 3753 nfs_purge_rddir_cache(vp);
3751 3754 } else {
3752 3755 error = nfs3_validate_caches(vp, cr);
3753 3756 if (error)
3754 3757 return (error);
3755 3758 }
3756 3759 }
3757 3760
3758 3761 /*
3759 3762 * It is possible that some servers may not be able to correctly
3760 3763 * handle a large READDIR or READDIRPLUS request due to bugs in
3761 3764 * their implementation. In order to continue to interoperate
3762 3765 * with them, this workaround is provided to limit the maximum
3763 3766 * size of a READDIRPLUS request to 1024. In any case, the request
3764 3767 * size is limited to MAXBSIZE.
3765 3768 */
3766 3769 count = MIN(uiop->uio_iov->iov_len,
3767 3770 nfs3_shrinkreaddir ? 1024 : MAXBSIZE);
3768 3771
3769 3772 nrdc = NULL;
3770 3773 #ifdef DEBUG
3771 3774 missed = 0;
3772 3775 #endif
3773 3776 top:
3774 3777 /*
3775 3778 * Short circuit last readdir which always returns 0 bytes.
3776 3779 * This can be done after the directory has been read through
3777 3780 * completely at least once. This will set r_direof which
3778 3781 * can be used to find the value of the last cookie.
3779 3782 */
3780 3783 mutex_enter(&rp->r_statelock);
3781 3784 if (rp->r_direof != NULL &&
3782 3785 uiop->uio_loffset == rp->r_direof->nfs3_ncookie) {
3783 3786 mutex_exit(&rp->r_statelock);
3784 3787 #ifdef DEBUG
3785 3788 nfs3_readdir_cache_shorts++;
3786 3789 #endif
3787 3790 if (eofp)
3788 3791 *eofp = 1;
3789 3792 if (nrdc != NULL)
3790 3793 rddir_cache_rele(nrdc);
3791 3794 return (0);
3792 3795 }
3793 3796 /*
3794 3797 * Look for a cache entry. Cache entries are identified
3795 3798 * by the NFS cookie value and the byte count requested.
3796 3799 */
3797 3800 srdc.nfs3_cookie = uiop->uio_loffset;
3798 3801 srdc.buflen = count;
3799 3802 rdc = avl_find(&rp->r_dir, &srdc, &where);
3800 3803 if (rdc != NULL) {
3801 3804 rddir_cache_hold(rdc);
3802 3805 /*
3803 3806 * If the cache entry is in the process of being
3804 3807 * filled in, wait until this completes. The
3805 3808 * RDDIRWAIT bit is set to indicate that someone
3806 3809 * is waiting and then the thread currently
3807 3810 * filling the entry is done, it should do a
3808 3811 * cv_broadcast to wakeup all of the threads
3809 3812 * waiting for it to finish.
3810 3813 */
3811 3814 if (rdc->flags & RDDIR) {
3812 3815 nfs_rw_exit(&rp->r_rwlock);
3813 3816 rdc->flags |= RDDIRWAIT;
3814 3817 #ifdef DEBUG
3815 3818 nfs3_readdir_cache_waits++;
3816 3819 #endif
3817 3820 if (!cv_wait_sig(&rdc->cv, &rp->r_statelock)) {
3818 3821 /*
3819 3822 * We got interrupted, probably
3820 3823 * the user typed ^C or an alarm
3821 3824 * fired. We free the new entry
3822 3825 * if we allocated one.
3823 3826 */
3824 3827 mutex_exit(&rp->r_statelock);
3825 3828 (void) nfs_rw_enter_sig(&rp->r_rwlock,
3826 3829 RW_READER, FALSE);
3827 3830 rddir_cache_rele(rdc);
3828 3831 if (nrdc != NULL)
3829 3832 rddir_cache_rele(nrdc);
3830 3833 return (EINTR);
3831 3834 }
3832 3835 mutex_exit(&rp->r_statelock);
3833 3836 (void) nfs_rw_enter_sig(&rp->r_rwlock,
3834 3837 RW_READER, FALSE);
3835 3838 rddir_cache_rele(rdc);
3836 3839 goto top;
3837 3840 }
3838 3841 /*
3839 3842 * Check to see if a readdir is required to
3840 3843 * fill the entry. If so, mark this entry
3841 3844 * as being filled, remove our reference,
3842 3845 * and branch to the code to fill the entry.
3843 3846 */
3844 3847 if (rdc->flags & RDDIRREQ) {
3845 3848 rdc->flags &= ~RDDIRREQ;
3846 3849 rdc->flags |= RDDIR;
3847 3850 if (nrdc != NULL)
3848 3851 rddir_cache_rele(nrdc);
3849 3852 nrdc = rdc;
3850 3853 mutex_exit(&rp->r_statelock);
3851 3854 goto bottom;
3852 3855 }
3853 3856 #ifdef DEBUG
3854 3857 if (!missed)
3855 3858 nfs3_readdir_cache_hits++;
3856 3859 #endif
3857 3860 /*
3858 3861 * If an error occurred while attempting
3859 3862 * to fill the cache entry, just return it.
3860 3863 */
3861 3864 if (rdc->error) {
3862 3865 error = rdc->error;
3863 3866 mutex_exit(&rp->r_statelock);
3864 3867 rddir_cache_rele(rdc);
3865 3868 if (nrdc != NULL)
3866 3869 rddir_cache_rele(nrdc);
3867 3870 return (error);
3868 3871 }
3869 3872
3870 3873 /*
3871 3874 * The cache entry is complete and good,
3872 3875 * copyout the dirent structs to the calling
3873 3876 * thread.
3874 3877 */
3875 3878 error = uiomove(rdc->entries, rdc->entlen, UIO_READ, uiop);
3876 3879
3877 3880 /*
3878 3881 * If no error occurred during the copyout,
3879 3882 * update the offset in the uio struct to
3880 3883 * contain the value of the next cookie
3881 3884 * and set the eof value appropriately.
3882 3885 */
3883 3886 if (!error) {
3884 3887 uiop->uio_loffset = rdc->nfs3_ncookie;
3885 3888 if (eofp)
3886 3889 *eofp = rdc->eof;
3887 3890 }
3888 3891
3889 3892 /*
3890 3893 * Decide whether to do readahead.
3891 3894 *
3892 3895 * Don't if have already read to the end of
3893 3896 * directory. There is nothing more to read.
3894 3897 *
3895 3898 * Don't if the application is not doing
3896 3899 * lookups in the directory. The readahead
3897 3900 * is only effective if the application can
3898 3901 * be doing work while an async thread is
3899 3902 * handling the over the wire request.
3900 3903 */
3901 3904 if (rdc->eof) {
3902 3905 rp->r_direof = rdc;
3903 3906 doreadahead = FALSE;
3904 3907 } else if (!(rp->r_flags & RLOOKUP))
3905 3908 doreadahead = FALSE;
3906 3909 else
3907 3910 doreadahead = TRUE;
3908 3911
3909 3912 if (!doreadahead) {
3910 3913 mutex_exit(&rp->r_statelock);
3911 3914 rddir_cache_rele(rdc);
3912 3915 if (nrdc != NULL)
3913 3916 rddir_cache_rele(nrdc);
3914 3917 return (error);
3915 3918 }
3916 3919
3917 3920 /*
3918 3921 * Check to see whether we found an entry
3919 3922 * for the readahead. If so, we don't need
3920 3923 * to do anything further, so free the new
3921 3924 * entry if one was allocated. Otherwise,
3922 3925 * allocate a new entry, add it to the cache,
3923 3926 * and then initiate an asynchronous readdir
3924 3927 * operation to fill it.
3925 3928 */
3926 3929 srdc.nfs3_cookie = rdc->nfs3_ncookie;
3927 3930 srdc.buflen = count;
3928 3931 rrdc = avl_find(&rp->r_dir, &srdc, &where);
3929 3932 if (rrdc != NULL) {
3930 3933 if (nrdc != NULL)
3931 3934 rddir_cache_rele(nrdc);
3932 3935 } else {
3933 3936 if (nrdc != NULL)
3934 3937 rrdc = nrdc;
3935 3938 else {
3936 3939 rrdc = rddir_cache_alloc(KM_NOSLEEP);
3937 3940 }
3938 3941 if (rrdc != NULL) {
3939 3942 rrdc->nfs3_cookie = rdc->nfs3_ncookie;
3940 3943 rrdc->buflen = count;
3941 3944 avl_insert(&rp->r_dir, rrdc, where);
3942 3945 rddir_cache_hold(rrdc);
3943 3946 mutex_exit(&rp->r_statelock);
3944 3947 rddir_cache_rele(rdc);
3945 3948 #ifdef DEBUG
3946 3949 nfs3_readdir_readahead++;
3947 3950 #endif
3948 3951 nfs_async_readdir(vp, rrdc, cr, do_nfs3readdir);
3949 3952 return (error);
3950 3953 }
3951 3954 }
3952 3955
3953 3956 mutex_exit(&rp->r_statelock);
3954 3957 rddir_cache_rele(rdc);
3955 3958 return (error);
3956 3959 }
3957 3960
3958 3961 /*
3959 3962 * Didn't find an entry in the cache. Construct a new empty
3960 3963 * entry and link it into the cache. Other processes attempting
3961 3964 * to access this entry will need to wait until it is filled in.
3962 3965 *
3963 3966 * Since kmem_alloc may block, another pass through the cache
3964 3967 * will need to be taken to make sure that another process
3965 3968 * hasn't already added an entry to the cache for this request.
3966 3969 */
3967 3970 if (nrdc == NULL) {
3968 3971 mutex_exit(&rp->r_statelock);
3969 3972 nrdc = rddir_cache_alloc(KM_SLEEP);
3970 3973 nrdc->nfs3_cookie = uiop->uio_loffset;
3971 3974 nrdc->buflen = count;
3972 3975 goto top;
3973 3976 }
3974 3977
3975 3978 /*
3976 3979 * Add this entry to the cache.
3977 3980 */
3978 3981 avl_insert(&rp->r_dir, nrdc, where);
3979 3982 rddir_cache_hold(nrdc);
3980 3983 mutex_exit(&rp->r_statelock);
3981 3984
3982 3985 bottom:
3983 3986 #ifdef DEBUG
3984 3987 missed = 1;
3985 3988 nfs3_readdir_cache_misses++;
3986 3989 #endif
3987 3990 /*
3988 3991 * Do the readdir. This routine decides whether to use
3989 3992 * READDIR or READDIRPLUS.
3990 3993 */
3991 3994 error = do_nfs3readdir(vp, nrdc, cr);
3992 3995
3993 3996 /*
3994 3997 * If this operation failed, just return the error which occurred.
3995 3998 */
3996 3999 if (error != 0)
3997 4000 return (error);
3998 4001
3999 4002 /*
4000 4003 * Since the RPC operation will have taken sometime and blocked
4001 4004 * this process, another pass through the cache will need to be
4002 4005 * taken to find the correct cache entry. It is possible that
4003 4006 * the correct cache entry will not be there (although one was
4004 4007 * added) because the directory changed during the RPC operation
4005 4008 * and the readdir cache was flushed. In this case, just start
4006 4009 * over. It is hoped that this will not happen too often... :-)
4007 4010 */
4008 4011 nrdc = NULL;
4009 4012 goto top;
4010 4013 /* NOTREACHED */
4011 4014 }
4012 4015
4013 4016 static int
4014 4017 do_nfs3readdir(vnode_t *vp, rddir_cache *rdc, cred_t *cr)
4015 4018 {
4016 4019 int error;
4017 4020 rnode_t *rp;
4018 4021 mntinfo_t *mi;
4019 4022
4020 4023 rp = VTOR(vp);
4021 4024 mi = VTOMI(vp);
4022 4025 ASSERT(nfs_zone() == mi->mi_zone);
4023 4026 /*
4024 4027 * Issue the proper request.
4025 4028 *
4026 4029 * If the server does not support READDIRPLUS, then use READDIR.
4027 4030 *
4028 4031 * Otherwise --
4029 4032 * Issue a READDIRPLUS if reading to fill an empty cache or if
4030 4033 * an application has performed a lookup in the directory which
4031 4034 * required an over the wire lookup. The use of READDIRPLUS
4032 4035 * will help to (re)populate the DNLC.
4033 4036 */
4034 4037 if (!(mi->mi_flags & MI_READDIRONLY) &&
4035 4038 (rp->r_flags & (RLOOKUP | RREADDIRPLUS))) {
4036 4039 if (rp->r_flags & RREADDIRPLUS) {
4037 4040 mutex_enter(&rp->r_statelock);
4038 4041 rp->r_flags &= ~RREADDIRPLUS;
4039 4042 mutex_exit(&rp->r_statelock);
4040 4043 }
4041 4044 nfs3readdirplus(vp, rdc, cr);
4042 4045 if (rdc->error == EOPNOTSUPP)
4043 4046 nfs3readdir(vp, rdc, cr);
4044 4047 } else
4045 4048 nfs3readdir(vp, rdc, cr);
4046 4049
4047 4050 mutex_enter(&rp->r_statelock);
4048 4051 rdc->flags &= ~RDDIR;
4049 4052 if (rdc->flags & RDDIRWAIT) {
4050 4053 rdc->flags &= ~RDDIRWAIT;
4051 4054 cv_broadcast(&rdc->cv);
4052 4055 }
4053 4056 error = rdc->error;
4054 4057 if (error)
4055 4058 rdc->flags |= RDDIRREQ;
4056 4059 mutex_exit(&rp->r_statelock);
4057 4060
4058 4061 rddir_cache_rele(rdc);
4059 4062
4060 4063 return (error);
4061 4064 }
4062 4065
4063 4066 static void
4064 4067 nfs3readdir(vnode_t *vp, rddir_cache *rdc, cred_t *cr)
4065 4068 {
4066 4069 int error;
4067 4070 READDIR3args args;
4068 4071 READDIR3vres res;
4069 4072 vattr_t dva;
4070 4073 rnode_t *rp;
4071 4074 int douprintf;
4072 4075 failinfo_t fi, *fip = NULL;
4073 4076 mntinfo_t *mi;
4074 4077 hrtime_t t;
4075 4078
4076 4079 rp = VTOR(vp);
4077 4080 mi = VTOMI(vp);
4078 4081 ASSERT(nfs_zone() == mi->mi_zone);
4079 4082
4080 4083 args.dir = *RTOFH3(rp);
4081 4084 args.cookie = (cookie3)rdc->nfs3_cookie;
4082 4085 args.cookieverf = rp->r_cookieverf;
4083 4086 args.count = rdc->buflen;
4084 4087
4085 4088 /*
4086 4089 * NFS client failover support
4087 4090 * suppress failover unless we have a zero cookie
4088 4091 */
4089 4092 if (args.cookie == (cookie3) 0) {
4090 4093 fi.vp = vp;
4091 4094 fi.fhp = (caddr_t)&args.dir;
4092 4095 fi.copyproc = nfs3copyfh;
4093 4096 fi.lookupproc = nfs3lookup;
4094 4097 fi.xattrdirproc = acl_getxattrdir3;
4095 4098 fip = &fi;
4096 4099 }
4097 4100
4098 4101 #ifdef DEBUG
4099 4102 rdc->entries = rddir_cache_buf_alloc(rdc->buflen, KM_SLEEP);
4100 4103 #else
4101 4104 rdc->entries = kmem_alloc(rdc->buflen, KM_SLEEP);
4102 4105 #endif
4103 4106
4104 4107 res.entries = (dirent64_t *)rdc->entries;
4105 4108 res.entries_size = rdc->buflen;
4106 4109 res.dir_attributes.fres.vap = &dva;
4107 4110 res.dir_attributes.fres.vp = vp;
4108 4111 res.loff = rdc->nfs3_cookie;
4109 4112
4110 4113 douprintf = 1;
4111 4114
4112 4115 if (mi->mi_io_kstats) {
4113 4116 mutex_enter(&mi->mi_lock);
4114 4117 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
4115 4118 mutex_exit(&mi->mi_lock);
4116 4119 }
4117 4120
4118 4121 t = gethrtime();
4119 4122
4120 4123 error = rfs3call(VTOMI(vp), NFSPROC3_READDIR,
4121 4124 xdr_READDIR3args, (caddr_t)&args,
4122 4125 xdr_READDIR3vres, (caddr_t)&res, cr,
4123 4126 &douprintf, &res.status, 0, fip);
4124 4127
4125 4128 if (mi->mi_io_kstats) {
4126 4129 mutex_enter(&mi->mi_lock);
4127 4130 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
4128 4131 mutex_exit(&mi->mi_lock);
4129 4132 }
4130 4133
4131 4134 if (error)
4132 4135 goto err;
4133 4136
4134 4137 nfs3_cache_post_op_vattr(vp, &res.dir_attributes, t, cr);
4135 4138
4136 4139 error = geterrno3(res.status);
4137 4140 if (error) {
4138 4141 PURGE_STALE_FH(error, vp, cr);
4139 4142 goto err;
4140 4143 }
4141 4144
4142 4145 if (mi->mi_io_kstats) {
4143 4146 mutex_enter(&mi->mi_lock);
4144 4147 KSTAT_IO_PTR(mi->mi_io_kstats)->reads++;
4145 4148 KSTAT_IO_PTR(mi->mi_io_kstats)->nread += res.size;
4146 4149 mutex_exit(&mi->mi_lock);
4147 4150 }
4148 4151
4149 4152 rdc->nfs3_ncookie = res.loff;
4150 4153 rp->r_cookieverf = res.cookieverf;
4151 4154 rdc->eof = res.eof ? 1 : 0;
4152 4155 rdc->entlen = res.size;
4153 4156 ASSERT(rdc->entlen <= rdc->buflen);
4154 4157 rdc->error = 0;
4155 4158 return;
4156 4159
4157 4160 err:
4158 4161 kmem_free(rdc->entries, rdc->buflen);
4159 4162 rdc->entries = NULL;
4160 4163 rdc->error = error;
4161 4164 }
4162 4165
4163 4166 /*
4164 4167 * Read directory entries.
4165 4168 * There are some weird things to look out for here. The uio_loffset
4166 4169 * field is either 0 or it is the offset returned from a previous
4167 4170 * readdir. It is an opaque value used by the server to find the
4168 4171 * correct directory block to read. The count field is the number
4169 4172 * of blocks to read on the server. This is advisory only, the server
4170 4173 * may return only one block's worth of entries. Entries may be compressed
4171 4174 * on the server.
4172 4175 */
4173 4176 static void
4174 4177 nfs3readdirplus(vnode_t *vp, rddir_cache *rdc, cred_t *cr)
4175 4178 {
4176 4179 int error;
4177 4180 READDIRPLUS3args args;
4178 4181 READDIRPLUS3vres res;
4179 4182 vattr_t dva;
4180 4183 rnode_t *rp;
4181 4184 mntinfo_t *mi;
4182 4185 int douprintf;
4183 4186 failinfo_t fi, *fip = NULL;
4184 4187
4185 4188 rp = VTOR(vp);
4186 4189 mi = VTOMI(vp);
4187 4190 ASSERT(nfs_zone() == mi->mi_zone);
4188 4191
4189 4192 args.dir = *RTOFH3(rp);
4190 4193 args.cookie = (cookie3)rdc->nfs3_cookie;
4191 4194 args.cookieverf = rp->r_cookieverf;
4192 4195 args.dircount = rdc->buflen;
4193 4196 args.maxcount = mi->mi_tsize;
4194 4197
4195 4198 /*
4196 4199 * NFS client failover support
4197 4200 * suppress failover unless we have a zero cookie
4198 4201 */
4199 4202 if (args.cookie == (cookie3)0) {
4200 4203 fi.vp = vp;
4201 4204 fi.fhp = (caddr_t)&args.dir;
4202 4205 fi.copyproc = nfs3copyfh;
4203 4206 fi.lookupproc = nfs3lookup;
4204 4207 fi.xattrdirproc = acl_getxattrdir3;
4205 4208 fip = &fi;
4206 4209 }
4207 4210
4208 4211 #ifdef DEBUG
4209 4212 rdc->entries = rddir_cache_buf_alloc(rdc->buflen, KM_SLEEP);
4210 4213 #else
4211 4214 rdc->entries = kmem_alloc(rdc->buflen, KM_SLEEP);
4212 4215 #endif
4213 4216
4214 4217 res.entries = (dirent64_t *)rdc->entries;
4215 4218 res.entries_size = rdc->buflen;
4216 4219 res.dir_attributes.fres.vap = &dva;
4217 4220 res.dir_attributes.fres.vp = vp;
4218 4221 res.loff = rdc->nfs3_cookie;
4219 4222 res.credentials = cr;
4220 4223
4221 4224 douprintf = 1;
4222 4225
4223 4226 if (mi->mi_io_kstats) {
4224 4227 mutex_enter(&mi->mi_lock);
4225 4228 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
4226 4229 mutex_exit(&mi->mi_lock);
4227 4230 }
4228 4231
4229 4232 res.time = gethrtime();
4230 4233
4231 4234 error = rfs3call(mi, NFSPROC3_READDIRPLUS,
4232 4235 xdr_READDIRPLUS3args, (caddr_t)&args,
4233 4236 xdr_READDIRPLUS3vres, (caddr_t)&res, cr,
4234 4237 &douprintf, &res.status, 0, fip);
4235 4238
4236 4239 if (mi->mi_io_kstats) {
4237 4240 mutex_enter(&mi->mi_lock);
4238 4241 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
4239 4242 mutex_exit(&mi->mi_lock);
4240 4243 }
4241 4244
4242 4245 if (error) {
4243 4246 goto err;
4244 4247 }
4245 4248
4246 4249 nfs3_cache_post_op_vattr(vp, &res.dir_attributes, res.time, cr);
4247 4250
4248 4251 error = geterrno3(res.status);
4249 4252 if (error) {
4250 4253 PURGE_STALE_FH(error, vp, cr);
4251 4254 if (error == EOPNOTSUPP) {
4252 4255 mutex_enter(&mi->mi_lock);
4253 4256 mi->mi_flags |= MI_READDIRONLY;
4254 4257 mutex_exit(&mi->mi_lock);
4255 4258 }
4256 4259 goto err;
4257 4260 }
4258 4261
4259 4262 if (mi->mi_io_kstats) {
4260 4263 mutex_enter(&mi->mi_lock);
4261 4264 KSTAT_IO_PTR(mi->mi_io_kstats)->reads++;
4262 4265 KSTAT_IO_PTR(mi->mi_io_kstats)->nread += res.size;
4263 4266 mutex_exit(&mi->mi_lock);
4264 4267 }
4265 4268
4266 4269 rdc->nfs3_ncookie = res.loff;
4267 4270 rp->r_cookieverf = res.cookieverf;
4268 4271 rdc->eof = res.eof ? 1 : 0;
4269 4272 rdc->entlen = res.size;
4270 4273 ASSERT(rdc->entlen <= rdc->buflen);
4271 4274 rdc->error = 0;
4272 4275
4273 4276 return;
4274 4277
4275 4278 err:
4276 4279 kmem_free(rdc->entries, rdc->buflen);
4277 4280 rdc->entries = NULL;
4278 4281 rdc->error = error;
4279 4282 }
4280 4283
4281 4284 #ifdef DEBUG
4282 4285 static int nfs3_bio_do_stop = 0;
4283 4286 #endif
4284 4287
4285 4288 static int
4286 4289 nfs3_bio(struct buf *bp, stable_how *stab_comm, cred_t *cr)
4287 4290 {
4288 4291 rnode_t *rp = VTOR(bp->b_vp);
4289 4292 int count;
4290 4293 int error;
4291 4294 cred_t *cred;
4292 4295 offset_t offset;
4293 4296
4294 4297 ASSERT(nfs_zone() == VTOMI(bp->b_vp)->mi_zone);
4295 4298 offset = ldbtob(bp->b_lblkno);
4296 4299
4297 4300 DTRACE_IO1(start, struct buf *, bp);
4298 4301
4299 4302 if (bp->b_flags & B_READ) {
4300 4303 mutex_enter(&rp->r_statelock);
4301 4304 if (rp->r_cred != NULL) {
4302 4305 cred = rp->r_cred;
4303 4306 crhold(cred);
4304 4307 } else {
4305 4308 rp->r_cred = cr;
4306 4309 crhold(cr);
4307 4310 cred = cr;
4308 4311 crhold(cred);
4309 4312 }
4310 4313 mutex_exit(&rp->r_statelock);
4311 4314 read_again:
4312 4315 error = bp->b_error = nfs3read(bp->b_vp, bp->b_un.b_addr,
4313 4316 offset, bp->b_bcount, &bp->b_resid, cred);
4314 4317 crfree(cred);
4315 4318 if (!error) {
4316 4319 if (bp->b_resid) {
4317 4320 /*
4318 4321 * Didn't get it all because we hit EOF,
4319 4322 * zero all the memory beyond the EOF.
4320 4323 */
4321 4324 /* bzero(rdaddr + */
4322 4325 bzero(bp->b_un.b_addr +
4323 4326 bp->b_bcount - bp->b_resid, bp->b_resid);
4324 4327 }
4325 4328 mutex_enter(&rp->r_statelock);
4326 4329 if (bp->b_resid == bp->b_bcount &&
4327 4330 offset >= rp->r_size) {
4328 4331 /*
4329 4332 * We didn't read anything at all as we are
4330 4333 * past EOF. Return an error indicator back
4331 4334 * but don't destroy the pages (yet).
4332 4335 */
4333 4336 error = NFS_EOF;
4334 4337 }
4335 4338 mutex_exit(&rp->r_statelock);
4336 4339 } else if (error == EACCES) {
4337 4340 mutex_enter(&rp->r_statelock);
4338 4341 if (cred != cr) {
4339 4342 if (rp->r_cred != NULL)
4340 4343 crfree(rp->r_cred);
4341 4344 rp->r_cred = cr;
4342 4345 crhold(cr);
4343 4346 cred = cr;
4344 4347 crhold(cred);
4345 4348 mutex_exit(&rp->r_statelock);
4346 4349 goto read_again;
4347 4350 }
4348 4351 mutex_exit(&rp->r_statelock);
4349 4352 }
4350 4353 } else {
4351 4354 if (!(rp->r_flags & RSTALE)) {
4352 4355 mutex_enter(&rp->r_statelock);
4353 4356 if (rp->r_cred != NULL) {
4354 4357 cred = rp->r_cred;
4355 4358 crhold(cred);
4356 4359 } else {
4357 4360 rp->r_cred = cr;
4358 4361 crhold(cr);
4359 4362 cred = cr;
4360 4363 crhold(cred);
4361 4364 }
4362 4365 mutex_exit(&rp->r_statelock);
4363 4366 write_again:
4364 4367 mutex_enter(&rp->r_statelock);
4365 4368 count = MIN(bp->b_bcount, rp->r_size - offset);
4366 4369 mutex_exit(&rp->r_statelock);
4367 4370 if (count < 0)
4368 4371 cmn_err(CE_PANIC, "nfs3_bio: write count < 0");
4369 4372 #ifdef DEBUG
4370 4373 if (count == 0) {
4371 4374 zcmn_err(getzoneid(), CE_WARN,
4372 4375 "nfs3_bio: zero length write at %lld",
4373 4376 offset);
4374 4377 nfs_printfhandle(&rp->r_fh);
4375 4378 if (nfs3_bio_do_stop)
4376 4379 debug_enter("nfs3_bio");
4377 4380 }
4378 4381 #endif
4379 4382 error = nfs3write(bp->b_vp, bp->b_un.b_addr, offset,
4380 4383 count, cred, stab_comm);
4381 4384 if (error == EACCES) {
4382 4385 mutex_enter(&rp->r_statelock);
4383 4386 if (cred != cr) {
4384 4387 if (rp->r_cred != NULL)
4385 4388 crfree(rp->r_cred);
4386 4389 rp->r_cred = cr;
4387 4390 crhold(cr);
4388 4391 crfree(cred);
4389 4392 cred = cr;
4390 4393 crhold(cred);
4391 4394 mutex_exit(&rp->r_statelock);
4392 4395 goto write_again;
4393 4396 }
4394 4397 mutex_exit(&rp->r_statelock);
4395 4398 }
4396 4399 bp->b_error = error;
4397 4400 if (error && error != EINTR) {
4398 4401 /*
4399 4402 * Don't print EDQUOT errors on the console.
4400 4403 * Don't print asynchronous EACCES errors.
4401 4404 * Don't print EFBIG errors.
4402 4405 * Print all other write errors.
4403 4406 */
4404 4407 if (error != EDQUOT && error != EFBIG &&
4405 4408 (error != EACCES ||
4406 4409 !(bp->b_flags & B_ASYNC)))
4407 4410 nfs_write_error(bp->b_vp, error, cred);
4408 4411 /*
4409 4412 * Update r_error and r_flags as appropriate.
4410 4413 * If the error was ESTALE, then mark the
4411 4414 * rnode as not being writeable and save
4412 4415 * the error status. Otherwise, save any
4413 4416 * errors which occur from asynchronous
4414 4417 * page invalidations. Any errors occurring
4415 4418 * from other operations should be saved
4416 4419 * by the caller.
4417 4420 */
4418 4421 mutex_enter(&rp->r_statelock);
4419 4422 if (error == ESTALE) {
4420 4423 rp->r_flags |= RSTALE;
4421 4424 if (!rp->r_error)
4422 4425 rp->r_error = error;
4423 4426 } else if (!rp->r_error &&
4424 4427 (bp->b_flags &
4425 4428 (B_INVAL|B_FORCE|B_ASYNC)) ==
4426 4429 (B_INVAL|B_FORCE|B_ASYNC)) {
4427 4430 rp->r_error = error;
4428 4431 }
4429 4432 mutex_exit(&rp->r_statelock);
4430 4433 }
4431 4434 crfree(cred);
4432 4435 } else {
4433 4436 error = rp->r_error;
4434 4437 /*
4435 4438 * A close may have cleared r_error, if so,
4436 4439 * propagate ESTALE error return properly
4437 4440 */
4438 4441 if (error == 0)
4439 4442 error = ESTALE;
4440 4443 }
4441 4444 }
4442 4445
4443 4446 if (error != 0 && error != NFS_EOF)
4444 4447 bp->b_flags |= B_ERROR;
4445 4448
4446 4449 DTRACE_IO1(done, struct buf *, bp);
4447 4450
4448 4451 return (error);
4449 4452 }
4450 4453
4451 4454 /* ARGSUSED */
4452 4455 static int
4453 4456 nfs3_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
4454 4457 {
4455 4458 rnode_t *rp;
4456 4459
4457 4460 if (nfs_zone() != VTOMI(vp)->mi_zone)
4458 4461 return (EIO);
4459 4462 rp = VTOR(vp);
4460 4463
4461 4464 if (fidp->fid_len < (ushort_t)rp->r_fh.fh_len) {
4462 4465 fidp->fid_len = rp->r_fh.fh_len;
4463 4466 return (ENOSPC);
4464 4467 }
4465 4468 fidp->fid_len = rp->r_fh.fh_len;
4466 4469 bcopy(rp->r_fh.fh_buf, fidp->fid_data, fidp->fid_len);
4467 4470 return (0);
4468 4471 }
4469 4472
4470 4473 /* ARGSUSED2 */
4471 4474 static int
4472 4475 nfs3_rwlock(vnode_t *vp, int write_lock, caller_context_t *ctp)
4473 4476 {
4474 4477 rnode_t *rp = VTOR(vp);
4475 4478
4476 4479 if (!write_lock) {
4477 4480 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE);
4478 4481 return (V_WRITELOCK_FALSE);
4479 4482 }
4480 4483
4481 4484 if ((rp->r_flags & RDIRECTIO) || (VTOMI(vp)->mi_flags & MI_DIRECTIO)) {
4482 4485 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE);
4483 4486 if (rp->r_mapcnt == 0 && !vn_has_cached_data(vp))
4484 4487 return (V_WRITELOCK_FALSE);
4485 4488 nfs_rw_exit(&rp->r_rwlock);
4486 4489 }
4487 4490
4488 4491 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, FALSE);
4489 4492 return (V_WRITELOCK_TRUE);
4490 4493 }
4491 4494
4492 4495 /* ARGSUSED */
4493 4496 static void
4494 4497 nfs3_rwunlock(vnode_t *vp, int write_lock, caller_context_t *ctp)
4495 4498 {
4496 4499 rnode_t *rp = VTOR(vp);
4497 4500
4498 4501 nfs_rw_exit(&rp->r_rwlock);
4499 4502 }
4500 4503
4501 4504 /* ARGSUSED */
4502 4505 static int
4503 4506 nfs3_seek(vnode_t *vp, offset_t ooff, offset_t *noffp, caller_context_t *ct)
4504 4507 {
4505 4508
4506 4509 /*
4507 4510 * Because we stuff the readdir cookie into the offset field
4508 4511 * someone may attempt to do an lseek with the cookie which
4509 4512 * we want to succeed.
4510 4513 */
|
↓ open down ↓ |
798 lines elided |
↑ open up ↑ |
4511 4514 if (vp->v_type == VDIR)
4512 4515 return (0);
4513 4516 if (*noffp < 0)
4514 4517 return (EINVAL);
4515 4518 return (0);
4516 4519 }
4517 4520
4518 4521 /*
4519 4522 * number of nfs3_bsize blocks to read ahead.
4520 4523 */
4521 -static int nfs3_nra = 4;
4524 +volatile int nfs3_nra = 4;
4522 4525
4523 4526 #ifdef DEBUG
4524 4527 static int nfs3_lostpage = 0; /* number of times we lost original page */
4525 4528 #endif
4526 4529
4527 4530 /*
4528 4531 * Return all the pages from [off..off+len) in file
4529 4532 */
4530 4533 /* ARGSUSED */
4531 4534 static int
4532 4535 nfs3_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
4533 4536 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
4534 4537 enum seg_rw rw, cred_t *cr, caller_context_t *ct)
4535 4538 {
4536 4539 rnode_t *rp;
4537 4540 int error;
4538 4541 mntinfo_t *mi;
4539 4542
4540 4543 if (vp->v_flag & VNOMAP)
4541 4544 return (ENOSYS);
4542 4545
4543 4546 if (nfs_zone() != VTOMI(vp)->mi_zone)
4544 4547 return (EIO);
4545 4548 if (protp != NULL)
4546 4549 *protp = PROT_ALL;
4547 4550
4548 4551 /*
4549 4552 * Now valididate that the caches are up to date.
4550 4553 */
4551 4554 error = nfs3_validate_caches(vp, cr);
4552 4555 if (error)
4553 4556 return (error);
4554 4557
4555 4558 rp = VTOR(vp);
4556 4559 mi = VTOMI(vp);
4557 4560 retry:
4558 4561 mutex_enter(&rp->r_statelock);
4559 4562
4560 4563 /*
4561 4564 * Don't create dirty pages faster than they
4562 4565 * can be cleaned so that the system doesn't
4563 4566 * get imbalanced. If the async queue is
4564 4567 * maxed out, then wait for it to drain before
4565 4568 * creating more dirty pages. Also, wait for
4566 4569 * any threads doing pagewalks in the vop_getattr
4567 4570 * entry points so that they don't block for
4568 4571 * long periods.
4569 4572 */
4570 4573 if (rw == S_CREATE) {
4571 4574 while ((mi->mi_max_threads != 0 &&
4572 4575 rp->r_awcount > 2 * mi->mi_max_threads) ||
4573 4576 rp->r_gcount > 0)
4574 4577 cv_wait(&rp->r_cv, &rp->r_statelock);
4575 4578 }
4576 4579
4577 4580 /*
4578 4581 * If we are getting called as a side effect of an nfs_write()
4579 4582 * operation the local file size might not be extended yet.
4580 4583 * In this case we want to be able to return pages of zeroes.
4581 4584 */
4582 4585 if (off + len > rp->r_size + PAGEOFFSET && seg != segkmap) {
4583 4586 mutex_exit(&rp->r_statelock);
4584 4587 return (EFAULT); /* beyond EOF */
4585 4588 }
4586 4589
4587 4590 mutex_exit(&rp->r_statelock);
4588 4591
4589 4592 error = pvn_getpages(nfs3_getapage, vp, off, len, protp,
4590 4593 pl, plsz, seg, addr, rw, cr);
4591 4594
4592 4595 switch (error) {
4593 4596 case NFS_EOF:
4594 4597 nfs_purge_caches(vp, NFS_NOPURGE_DNLC, cr);
4595 4598 goto retry;
4596 4599 case ESTALE:
4597 4600 PURGE_STALE_FH(error, vp, cr);
4598 4601 }
4599 4602
4600 4603 return (error);
4601 4604 }
4602 4605
4603 4606 /*
4604 4607 * Called from pvn_getpages to get a particular page.
4605 4608 */
4606 4609 /* ARGSUSED */
4607 4610 static int
4608 4611 nfs3_getapage(vnode_t *vp, u_offset_t off, size_t len, uint_t *protp,
4609 4612 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
4610 4613 enum seg_rw rw, cred_t *cr)
4611 4614 {
4612 4615 rnode_t *rp;
4613 4616 uint_t bsize;
4614 4617 struct buf *bp;
4615 4618 page_t *pp;
4616 4619 u_offset_t lbn;
4617 4620 u_offset_t io_off;
4618 4621 u_offset_t blkoff;
4619 4622 u_offset_t rablkoff;
4620 4623 size_t io_len;
4621 4624 uint_t blksize;
4622 4625 int error;
4623 4626 int readahead;
4624 4627 int readahead_issued = 0;
4625 4628 int ra_window; /* readahead window */
4626 4629 page_t *pagefound;
4627 4630 page_t *savepp;
4628 4631
4629 4632 if (nfs_zone() != VTOMI(vp)->mi_zone)
4630 4633 return (EIO);
4631 4634 rp = VTOR(vp);
4632 4635 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE);
4633 4636
4634 4637 reread:
4635 4638 bp = NULL;
4636 4639 pp = NULL;
4637 4640 pagefound = NULL;
4638 4641
4639 4642 if (pl != NULL)
4640 4643 pl[0] = NULL;
4641 4644
4642 4645 error = 0;
4643 4646 lbn = off / bsize;
4644 4647 blkoff = lbn * bsize;
4645 4648
4646 4649 /*
4647 4650 * Queueing up the readahead before doing the synchronous read
4648 4651 * results in a significant increase in read throughput because
4649 4652 * of the increased parallelism between the async threads and
4650 4653 * the process context.
4651 4654 */
4652 4655 if ((off & ((vp->v_vfsp->vfs_bsize) - 1)) == 0 &&
4653 4656 rw != S_CREATE &&
4654 4657 !(vp->v_flag & VNOCACHE)) {
4655 4658 mutex_enter(&rp->r_statelock);
4656 4659
4657 4660 /*
4658 4661 * Calculate the number of readaheads to do.
4659 4662 * a) No readaheads at offset = 0.
4660 4663 * b) Do maximum(nfs3_nra) readaheads when the readahead
4661 4664 * window is closed.
4662 4665 * c) Do readaheads between 1 to (nfs3_nra - 1) depending
4663 4666 * upon how far the readahead window is open or close.
4664 4667 * d) No readaheads if rp->r_nextr is not within the scope
4665 4668 * of the readahead window (random i/o).
4666 4669 */
4667 4670
4668 4671 if (off == 0)
4669 4672 readahead = 0;
4670 4673 else if (blkoff == rp->r_nextr)
4671 4674 readahead = nfs3_nra;
4672 4675 else if (rp->r_nextr > blkoff &&
4673 4676 ((ra_window = (rp->r_nextr - blkoff) / bsize)
4674 4677 <= (nfs3_nra - 1)))
4675 4678 readahead = nfs3_nra - ra_window;
4676 4679 else
4677 4680 readahead = 0;
4678 4681
4679 4682 rablkoff = rp->r_nextr;
4680 4683 while (readahead > 0 && rablkoff + bsize < rp->r_size) {
4681 4684 mutex_exit(&rp->r_statelock);
4682 4685 if (nfs_async_readahead(vp, rablkoff + bsize,
4683 4686 addr + (rablkoff + bsize - off), seg, cr,
4684 4687 nfs3_readahead) < 0) {
4685 4688 mutex_enter(&rp->r_statelock);
4686 4689 break;
4687 4690 }
4688 4691 readahead--;
4689 4692 rablkoff += bsize;
4690 4693 /*
4691 4694 * Indicate that we did a readahead so
4692 4695 * readahead offset is not updated
4693 4696 * by the synchronous read below.
4694 4697 */
4695 4698 readahead_issued = 1;
4696 4699 mutex_enter(&rp->r_statelock);
4697 4700 /*
4698 4701 * set readahead offset to
4699 4702 * offset of last async readahead
4700 4703 * request.
4701 4704 */
4702 4705 rp->r_nextr = rablkoff;
4703 4706 }
4704 4707 mutex_exit(&rp->r_statelock);
4705 4708 }
4706 4709
4707 4710 again:
4708 4711 if ((pagefound = page_exists(vp, off)) == NULL) {
4709 4712 if (pl == NULL) {
4710 4713 (void) nfs_async_readahead(vp, blkoff, addr, seg, cr,
4711 4714 nfs3_readahead);
4712 4715 } else if (rw == S_CREATE) {
4713 4716 /*
4714 4717 * Block for this page is not allocated, or the offset
4715 4718 * is beyond the current allocation size, or we're
4716 4719 * allocating a swap slot and the page was not found,
4717 4720 * so allocate it and return a zero page.
4718 4721 */
4719 4722 if ((pp = page_create_va(vp, off,
4720 4723 PAGESIZE, PG_WAIT, seg, addr)) == NULL)
4721 4724 cmn_err(CE_PANIC, "nfs3_getapage: page_create");
4722 4725 io_len = PAGESIZE;
4723 4726 mutex_enter(&rp->r_statelock);
4724 4727 rp->r_nextr = off + PAGESIZE;
4725 4728 mutex_exit(&rp->r_statelock);
4726 4729 } else {
4727 4730 /*
4728 4731 * Need to go to server to get a BLOCK, exception to
4729 4732 * that being while reading at offset = 0 or doing
4730 4733 * random i/o, in that case read only a PAGE.
4731 4734 */
4732 4735 mutex_enter(&rp->r_statelock);
4733 4736 if (blkoff < rp->r_size &&
4734 4737 blkoff + bsize >= rp->r_size) {
4735 4738 /*
4736 4739 * If only a block or less is left in
4737 4740 * the file, read all that is remaining.
4738 4741 */
4739 4742 if (rp->r_size <= off) {
4740 4743 /*
4741 4744 * Trying to access beyond EOF,
4742 4745 * set up to get at least one page.
4743 4746 */
4744 4747 blksize = off + PAGESIZE - blkoff;
4745 4748 } else
4746 4749 blksize = rp->r_size - blkoff;
4747 4750 } else if ((off == 0) ||
4748 4751 (off != rp->r_nextr && !readahead_issued)) {
4749 4752 blksize = PAGESIZE;
4750 4753 blkoff = off; /* block = page here */
4751 4754 } else
4752 4755 blksize = bsize;
4753 4756 mutex_exit(&rp->r_statelock);
4754 4757
4755 4758 pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
4756 4759 &io_len, blkoff, blksize, 0);
4757 4760
4758 4761 /*
4759 4762 * Some other thread has entered the page,
4760 4763 * so just use it.
4761 4764 */
4762 4765 if (pp == NULL)
4763 4766 goto again;
4764 4767
4765 4768 /*
4766 4769 * Now round the request size up to page boundaries.
4767 4770 * This ensures that the entire page will be
4768 4771 * initialized to zeroes if EOF is encountered.
4769 4772 */
4770 4773 io_len = ptob(btopr(io_len));
4771 4774
4772 4775 bp = pageio_setup(pp, io_len, vp, B_READ);
4773 4776 ASSERT(bp != NULL);
4774 4777
4775 4778 /*
4776 4779 * pageio_setup should have set b_addr to 0. This
4777 4780 * is correct since we want to do I/O on a page
4778 4781 * boundary. bp_mapin will use this addr to calculate
4779 4782 * an offset, and then set b_addr to the kernel virtual
4780 4783 * address it allocated for us.
4781 4784 */
4782 4785 ASSERT(bp->b_un.b_addr == 0);
4783 4786
4784 4787 bp->b_edev = 0;
4785 4788 bp->b_dev = 0;
4786 4789 bp->b_lblkno = lbtodb(io_off);
4787 4790 bp->b_file = vp;
4788 4791 bp->b_offset = (offset_t)off;
4789 4792 bp_mapin(bp);
4790 4793
4791 4794 /*
4792 4795 * If doing a write beyond what we believe is EOF,
4793 4796 * don't bother trying to read the pages from the
4794 4797 * server, we'll just zero the pages here. We
4795 4798 * don't check that the rw flag is S_WRITE here
4796 4799 * because some implementations may attempt a
4797 4800 * read access to the buffer before copying data.
4798 4801 */
4799 4802 mutex_enter(&rp->r_statelock);
4800 4803 if (io_off >= rp->r_size && seg == segkmap) {
4801 4804 mutex_exit(&rp->r_statelock);
4802 4805 bzero(bp->b_un.b_addr, io_len);
4803 4806 } else {
4804 4807 mutex_exit(&rp->r_statelock);
4805 4808 error = nfs3_bio(bp, NULL, cr);
4806 4809 }
4807 4810
4808 4811 /*
4809 4812 * Unmap the buffer before freeing it.
4810 4813 */
4811 4814 bp_mapout(bp);
4812 4815 pageio_done(bp);
4813 4816
4814 4817 savepp = pp;
4815 4818 do {
4816 4819 pp->p_fsdata = C_NOCOMMIT;
4817 4820 } while ((pp = pp->p_next) != savepp);
4818 4821
4819 4822 if (error == NFS_EOF) {
4820 4823 /*
4821 4824 * If doing a write system call just return
4822 4825 * zeroed pages, else user tried to get pages
4823 4826 * beyond EOF, return error. We don't check
4824 4827 * that the rw flag is S_WRITE here because
4825 4828 * some implementations may attempt a read
4826 4829 * access to the buffer before copying data.
4827 4830 */
4828 4831 if (seg == segkmap)
4829 4832 error = 0;
4830 4833 else
4831 4834 error = EFAULT;
4832 4835 }
4833 4836
4834 4837 if (!readahead_issued && !error) {
4835 4838 mutex_enter(&rp->r_statelock);
4836 4839 rp->r_nextr = io_off + io_len;
4837 4840 mutex_exit(&rp->r_statelock);
4838 4841 }
4839 4842 }
4840 4843 }
4841 4844
4842 4845 out:
4843 4846 if (pl == NULL)
4844 4847 return (error);
4845 4848
4846 4849 if (error) {
4847 4850 if (pp != NULL)
4848 4851 pvn_read_done(pp, B_ERROR);
4849 4852 return (error);
4850 4853 }
4851 4854
4852 4855 if (pagefound) {
4853 4856 se_t se = (rw == S_CREATE ? SE_EXCL : SE_SHARED);
4854 4857
4855 4858 /*
4856 4859 * Page exists in the cache, acquire the appropriate lock.
4857 4860 * If this fails, start all over again.
4858 4861 */
4859 4862 if ((pp = page_lookup(vp, off, se)) == NULL) {
4860 4863 #ifdef DEBUG
4861 4864 nfs3_lostpage++;
4862 4865 #endif
4863 4866 goto reread;
4864 4867 }
4865 4868 pl[0] = pp;
4866 4869 pl[1] = NULL;
4867 4870 return (0);
4868 4871 }
4869 4872
4870 4873 if (pp != NULL)
4871 4874 pvn_plist_init(pp, pl, plsz, off, io_len, rw);
4872 4875
4873 4876 return (error);
4874 4877 }
4875 4878
4876 4879 static void
4877 4880 nfs3_readahead(vnode_t *vp, u_offset_t blkoff, caddr_t addr, struct seg *seg,
4878 4881 cred_t *cr)
4879 4882 {
4880 4883 int error;
4881 4884 page_t *pp;
4882 4885 u_offset_t io_off;
4883 4886 size_t io_len;
4884 4887 struct buf *bp;
4885 4888 uint_t bsize, blksize;
4886 4889 rnode_t *rp = VTOR(vp);
4887 4890 page_t *savepp;
4888 4891
4889 4892 ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
4890 4893 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE);
4891 4894
4892 4895 mutex_enter(&rp->r_statelock);
4893 4896 if (blkoff < rp->r_size && blkoff + bsize > rp->r_size) {
4894 4897 /*
4895 4898 * If less than a block left in file read less
4896 4899 * than a block.
4897 4900 */
4898 4901 blksize = rp->r_size - blkoff;
4899 4902 } else
4900 4903 blksize = bsize;
4901 4904 mutex_exit(&rp->r_statelock);
4902 4905
4903 4906 pp = pvn_read_kluster(vp, blkoff, segkmap, addr,
4904 4907 &io_off, &io_len, blkoff, blksize, 1);
4905 4908 /*
4906 4909 * The isra flag passed to the kluster function is 1, we may have
4907 4910 * gotten a return value of NULL for a variety of reasons (# of free
4908 4911 * pages < minfree, someone entered the page on the vnode etc). In all
4909 4912 * cases, we want to punt on the readahead.
4910 4913 */
4911 4914 if (pp == NULL)
4912 4915 return;
4913 4916
4914 4917 /*
4915 4918 * Now round the request size up to page boundaries.
4916 4919 * This ensures that the entire page will be
4917 4920 * initialized to zeroes if EOF is encountered.
4918 4921 */
4919 4922 io_len = ptob(btopr(io_len));
4920 4923
4921 4924 bp = pageio_setup(pp, io_len, vp, B_READ);
4922 4925 ASSERT(bp != NULL);
4923 4926
4924 4927 /*
4925 4928 * pageio_setup should have set b_addr to 0. This is correct since
4926 4929 * we want to do I/O on a page boundary. bp_mapin() will use this addr
4927 4930 * to calculate an offset, and then set b_addr to the kernel virtual
4928 4931 * address it allocated for us.
4929 4932 */
4930 4933 ASSERT(bp->b_un.b_addr == 0);
4931 4934
4932 4935 bp->b_edev = 0;
4933 4936 bp->b_dev = 0;
4934 4937 bp->b_lblkno = lbtodb(io_off);
4935 4938 bp->b_file = vp;
4936 4939 bp->b_offset = (offset_t)blkoff;
4937 4940 bp_mapin(bp);
4938 4941
4939 4942 /*
4940 4943 * If doing a write beyond what we believe is EOF, don't bother trying
4941 4944 * to read the pages from the server, we'll just zero the pages here.
4942 4945 * We don't check that the rw flag is S_WRITE here because some
4943 4946 * implementations may attempt a read access to the buffer before
4944 4947 * copying data.
4945 4948 */
4946 4949 mutex_enter(&rp->r_statelock);
4947 4950 if (io_off >= rp->r_size && seg == segkmap) {
4948 4951 mutex_exit(&rp->r_statelock);
4949 4952 bzero(bp->b_un.b_addr, io_len);
4950 4953 error = 0;
4951 4954 } else {
4952 4955 mutex_exit(&rp->r_statelock);
4953 4956 error = nfs3_bio(bp, NULL, cr);
4954 4957 if (error == NFS_EOF)
4955 4958 error = 0;
4956 4959 }
4957 4960
4958 4961 /*
4959 4962 * Unmap the buffer before freeing it.
4960 4963 */
4961 4964 bp_mapout(bp);
4962 4965 pageio_done(bp);
4963 4966
4964 4967 savepp = pp;
4965 4968 do {
4966 4969 pp->p_fsdata = C_NOCOMMIT;
4967 4970 } while ((pp = pp->p_next) != savepp);
4968 4971
4969 4972 pvn_read_done(pp, error ? B_READ | B_ERROR : B_READ);
4970 4973
4971 4974 /*
4972 4975 * In case of error set readahead offset
4973 4976 * to the lowest offset.
4974 4977 * pvn_read_done() calls VN_DISPOSE to destroy the pages
4975 4978 */
4976 4979 if (error && rp->r_nextr > io_off) {
4977 4980 mutex_enter(&rp->r_statelock);
4978 4981 if (rp->r_nextr > io_off)
4979 4982 rp->r_nextr = io_off;
4980 4983 mutex_exit(&rp->r_statelock);
4981 4984 }
4982 4985 }
4983 4986
4984 4987 /*
4985 4988 * Flags are composed of {B_INVAL, B_FREE, B_DONTNEED, B_FORCE}
4986 4989 * If len == 0, do from off to EOF.
4987 4990 *
4988 4991 * The normal cases should be len == 0 && off == 0 (entire vp list),
4989 4992 * len == MAXBSIZE (from segmap_release actions), and len == PAGESIZE
4990 4993 * (from pageout).
4991 4994 */
4992 4995 /* ARGSUSED */
4993 4996 static int
4994 4997 nfs3_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr,
4995 4998 caller_context_t *ct)
4996 4999 {
4997 5000 int error;
4998 5001 rnode_t *rp;
4999 5002
5000 5003 ASSERT(cr != NULL);
5001 5004
5002 5005 /*
5003 5006 * XXX - Why should this check be made here?
5004 5007 */
5005 5008 if (vp->v_flag & VNOMAP)
5006 5009 return (ENOSYS);
5007 5010 if (len == 0 && !(flags & B_INVAL) && vn_is_readonly(vp))
5008 5011 return (0);
5009 5012 if (!(flags & B_ASYNC) && nfs_zone() != VTOMI(vp)->mi_zone)
5010 5013 return (EIO);
5011 5014
5012 5015 rp = VTOR(vp);
5013 5016 mutex_enter(&rp->r_statelock);
5014 5017 rp->r_count++;
5015 5018 mutex_exit(&rp->r_statelock);
5016 5019 error = nfs_putpages(vp, off, len, flags, cr);
5017 5020 mutex_enter(&rp->r_statelock);
5018 5021 rp->r_count--;
5019 5022 cv_broadcast(&rp->r_cv);
5020 5023 mutex_exit(&rp->r_statelock);
5021 5024
5022 5025 return (error);
5023 5026 }
5024 5027
5025 5028 /*
5026 5029 * Write out a single page, possibly klustering adjacent dirty pages.
5027 5030 */
5028 5031 int
5029 5032 nfs3_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, size_t *lenp,
5030 5033 int flags, cred_t *cr)
5031 5034 {
5032 5035 u_offset_t io_off;
5033 5036 u_offset_t lbn_off;
5034 5037 u_offset_t lbn;
5035 5038 size_t io_len;
5036 5039 uint_t bsize;
5037 5040 int error;
5038 5041 rnode_t *rp;
5039 5042
5040 5043 ASSERT(!vn_is_readonly(vp));
5041 5044 ASSERT(pp != NULL);
5042 5045 ASSERT(cr != NULL);
5043 5046 ASSERT((flags & B_ASYNC) || nfs_zone() == VTOMI(vp)->mi_zone);
5044 5047
5045 5048 rp = VTOR(vp);
5046 5049 ASSERT(rp->r_count > 0);
5047 5050
5048 5051 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE);
5049 5052 lbn = pp->p_offset / bsize;
5050 5053 lbn_off = lbn * bsize;
5051 5054
5052 5055 /*
5053 5056 * Find a kluster that fits in one block, or in
5054 5057 * one page if pages are bigger than blocks. If
5055 5058 * there is less file space allocated than a whole
5056 5059 * page, we'll shorten the i/o request below.
5057 5060 */
5058 5061 pp = pvn_write_kluster(vp, pp, &io_off, &io_len, lbn_off,
5059 5062 roundup(bsize, PAGESIZE), flags);
5060 5063
5061 5064 /*
5062 5065 * pvn_write_kluster shouldn't have returned a page with offset
5063 5066 * behind the original page we were given. Verify that.
5064 5067 */
5065 5068 ASSERT((pp->p_offset / bsize) >= lbn);
5066 5069
5067 5070 /*
5068 5071 * Now pp will have the list of kept dirty pages marked for
5069 5072 * write back. It will also handle invalidation and freeing
5070 5073 * of pages that are not dirty. Check for page length rounding
5071 5074 * problems.
5072 5075 */
5073 5076 if (io_off + io_len > lbn_off + bsize) {
5074 5077 ASSERT((io_off + io_len) - (lbn_off + bsize) < PAGESIZE);
5075 5078 io_len = lbn_off + bsize - io_off;
5076 5079 }
5077 5080 /*
5078 5081 * The RMODINPROGRESS flag makes sure that nfs(3)_bio() sees a
5079 5082 * consistent value of r_size. RMODINPROGRESS is set in writerp().
5080 5083 * When RMODINPROGRESS is set it indicates that a uiomove() is in
5081 5084 * progress and the r_size has not been made consistent with the
5082 5085 * new size of the file. When the uiomove() completes the r_size is
5083 5086 * updated and the RMODINPROGRESS flag is cleared.
5084 5087 *
5085 5088 * The RMODINPROGRESS flag makes sure that nfs(3)_bio() sees a
5086 5089 * consistent value of r_size. Without this handshaking, it is
5087 5090 * possible that nfs(3)_bio() picks up the old value of r_size
5088 5091 * before the uiomove() in writerp() completes. This will result
5089 5092 * in the write through nfs(3)_bio() being dropped.
5090 5093 *
5091 5094 * More precisely, there is a window between the time the uiomove()
5092 5095 * completes and the time the r_size is updated. If a VOP_PUTPAGE()
5093 5096 * operation intervenes in this window, the page will be picked up,
5094 5097 * because it is dirty (it will be unlocked, unless it was
5095 5098 * pagecreate'd). When the page is picked up as dirty, the dirty
5096 5099 * bit is reset (pvn_getdirty()). In nfs(3)write(), r_size is
5097 5100 * checked. This will still be the old size. Therefore the page will
5098 5101 * not be written out. When segmap_release() calls VOP_PUTPAGE(),
5099 5102 * the page will be found to be clean and the write will be dropped.
5100 5103 */
5101 5104 if (rp->r_flags & RMODINPROGRESS) {
5102 5105 mutex_enter(&rp->r_statelock);
5103 5106 if ((rp->r_flags & RMODINPROGRESS) &&
5104 5107 rp->r_modaddr + MAXBSIZE > io_off &&
5105 5108 rp->r_modaddr < io_off + io_len) {
5106 5109 page_t *plist;
5107 5110 /*
5108 5111 * A write is in progress for this region of the file.
5109 5112 * If we did not detect RMODINPROGRESS here then this
5110 5113 * path through nfs_putapage() would eventually go to
5111 5114 * nfs(3)_bio() and may not write out all of the data
5112 5115 * in the pages. We end up losing data. So we decide
5113 5116 * to set the modified bit on each page in the page
5114 5117 * list and mark the rnode with RDIRTY. This write
5115 5118 * will be restarted at some later time.
5116 5119 */
5117 5120 plist = pp;
5118 5121 while (plist != NULL) {
5119 5122 pp = plist;
5120 5123 page_sub(&plist, pp);
5121 5124 hat_setmod(pp);
5122 5125 page_io_unlock(pp);
5123 5126 page_unlock(pp);
5124 5127 }
5125 5128 rp->r_flags |= RDIRTY;
5126 5129 mutex_exit(&rp->r_statelock);
5127 5130 if (offp)
5128 5131 *offp = io_off;
5129 5132 if (lenp)
5130 5133 *lenp = io_len;
5131 5134 return (0);
5132 5135 }
5133 5136 mutex_exit(&rp->r_statelock);
5134 5137 }
5135 5138
5136 5139 if (flags & B_ASYNC) {
5137 5140 error = nfs_async_putapage(vp, pp, io_off, io_len, flags, cr,
5138 5141 nfs3_sync_putapage);
5139 5142 } else
5140 5143 error = nfs3_sync_putapage(vp, pp, io_off, io_len, flags, cr);
5141 5144
5142 5145 if (offp)
5143 5146 *offp = io_off;
5144 5147 if (lenp)
5145 5148 *lenp = io_len;
5146 5149 return (error);
5147 5150 }
5148 5151
5149 5152 static int
5150 5153 nfs3_sync_putapage(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len,
5151 5154 int flags, cred_t *cr)
5152 5155 {
5153 5156 int error;
5154 5157 rnode_t *rp;
5155 5158
5156 5159 ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
5157 5160
5158 5161 flags |= B_WRITE;
5159 5162
5160 5163 error = nfs3_rdwrlbn(vp, pp, io_off, io_len, flags, cr);
5161 5164
5162 5165 rp = VTOR(vp);
5163 5166
5164 5167 if ((error == ENOSPC || error == EDQUOT || error == EFBIG ||
5165 5168 error == EACCES) &&
5166 5169 (flags & (B_INVAL|B_FORCE)) != (B_INVAL|B_FORCE)) {
5167 5170 if (!(rp->r_flags & ROUTOFSPACE)) {
5168 5171 mutex_enter(&rp->r_statelock);
5169 5172 rp->r_flags |= ROUTOFSPACE;
5170 5173 mutex_exit(&rp->r_statelock);
5171 5174 }
5172 5175 flags |= B_ERROR;
5173 5176 pvn_write_done(pp, flags);
5174 5177 /*
5175 5178 * If this was not an async thread, then try again to
5176 5179 * write out the pages, but this time, also destroy
5177 5180 * them whether or not the write is successful. This
5178 5181 * will prevent memory from filling up with these
5179 5182 * pages and destroying them is the only alternative
5180 5183 * if they can't be written out.
5181 5184 *
5182 5185 * Don't do this if this is an async thread because
5183 5186 * when the pages are unlocked in pvn_write_done,
5184 5187 * some other thread could have come along, locked
5185 5188 * them, and queued for an async thread. It would be
5186 5189 * possible for all of the async threads to be tied
5187 5190 * up waiting to lock the pages again and they would
5188 5191 * all already be locked and waiting for an async
5189 5192 * thread to handle them. Deadlock.
5190 5193 */
5191 5194 if (!(flags & B_ASYNC)) {
5192 5195 error = nfs3_putpage(vp, io_off, io_len,
5193 5196 B_INVAL | B_FORCE, cr, NULL);
5194 5197 }
5195 5198 } else {
5196 5199 if (error)
5197 5200 flags |= B_ERROR;
5198 5201 else if (rp->r_flags & ROUTOFSPACE) {
5199 5202 mutex_enter(&rp->r_statelock);
5200 5203 rp->r_flags &= ~ROUTOFSPACE;
5201 5204 mutex_exit(&rp->r_statelock);
5202 5205 }
5203 5206 pvn_write_done(pp, flags);
5204 5207 if (freemem < desfree)
5205 5208 (void) nfs3_commit_vp(vp, (u_offset_t)0, 0, cr);
5206 5209 }
5207 5210
5208 5211 return (error);
5209 5212 }
5210 5213
5211 5214 /* ARGSUSED */
5212 5215 static int
5213 5216 nfs3_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
5214 5217 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags,
5215 5218 cred_t *cr, caller_context_t *ct)
5216 5219 {
5217 5220 struct segvn_crargs vn_a;
5218 5221 int error;
5219 5222 rnode_t *rp;
5220 5223 struct vattr va;
5221 5224
5222 5225 if (nfs_zone() != VTOMI(vp)->mi_zone)
5223 5226 return (EIO);
5224 5227
5225 5228 if (vp->v_flag & VNOMAP)
5226 5229 return (ENOSYS);
5227 5230
5228 5231 if (off < 0 || off + len < 0)
5229 5232 return (ENXIO);
5230 5233
5231 5234 if (vp->v_type != VREG)
5232 5235 return (ENODEV);
5233 5236
5234 5237 /*
5235 5238 * If there is cached data and if close-to-open consistency
5236 5239 * checking is not turned off and if the file system is not
5237 5240 * mounted readonly, then force an over the wire getattr.
5238 5241 * Otherwise, just invoke nfs3getattr to get a copy of the
5239 5242 * attributes. The attribute cache will be used unless it
5240 5243 * is timed out and if it is, then an over the wire getattr
5241 5244 * will be issued.
5242 5245 */
5243 5246 va.va_mask = AT_ALL;
5244 5247 if (vn_has_cached_data(vp) &&
5245 5248 !(VTOMI(vp)->mi_flags & MI_NOCTO) && !vn_is_readonly(vp))
5246 5249 error = nfs3_getattr_otw(vp, &va, cr);
5247 5250 else
5248 5251 error = nfs3getattr(vp, &va, cr);
5249 5252 if (error)
5250 5253 return (error);
5251 5254
5252 5255 /*
5253 5256 * Check to see if the vnode is currently marked as not cachable.
5254 5257 * This means portions of the file are locked (through VOP_FRLOCK).
5255 5258 * In this case the map request must be refused. We use
5256 5259 * rp->r_lkserlock to avoid a race with concurrent lock requests.
5257 5260 */
5258 5261 rp = VTOR(vp);
5259 5262
5260 5263 /*
5261 5264 * Atomically increment r_inmap after acquiring r_rwlock. The
5262 5265 * idea here is to acquire r_rwlock to block read/write and
5263 5266 * not to protect r_inmap. r_inmap will inform nfs3_read/write()
5264 5267 * that we are in nfs3_map(). Now, r_rwlock is acquired in order
5265 5268 * and we can prevent the deadlock that would have occurred
5266 5269 * when nfs3_addmap() would have acquired it out of order.
5267 5270 *
5268 5271 * Since we are not protecting r_inmap by any lock, we do not
5269 5272 * hold any lock when we decrement it. We atomically decrement
5270 5273 * r_inmap after we release r_lkserlock.
5271 5274 */
5272 5275
5273 5276 if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, INTR(vp)))
5274 5277 return (EINTR);
5275 5278 atomic_inc_uint(&rp->r_inmap);
5276 5279 nfs_rw_exit(&rp->r_rwlock);
5277 5280
5278 5281 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR(vp))) {
5279 5282 atomic_dec_uint(&rp->r_inmap);
5280 5283 return (EINTR);
5281 5284 }
5282 5285
5283 5286 if (vp->v_flag & VNOCACHE) {
5284 5287 error = EAGAIN;
5285 5288 goto done;
5286 5289 }
5287 5290
5288 5291 /*
5289 5292 * Don't allow concurrent locks and mapping if mandatory locking is
5290 5293 * enabled.
5291 5294 */
5292 5295 if ((flk_has_remote_locks(vp) || lm_has_sleep(vp)) &&
5293 5296 MANDLOCK(vp, va.va_mode)) {
5294 5297 error = EAGAIN;
5295 5298 goto done;
5296 5299 }
5297 5300
5298 5301 as_rangelock(as);
5299 5302 error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
5300 5303 if (error != 0) {
5301 5304 as_rangeunlock(as);
5302 5305 goto done;
5303 5306 }
5304 5307
5305 5308 vn_a.vp = vp;
5306 5309 vn_a.offset = off;
5307 5310 vn_a.type = (flags & MAP_TYPE);
5308 5311 vn_a.prot = (uchar_t)prot;
5309 5312 vn_a.maxprot = (uchar_t)maxprot;
5310 5313 vn_a.flags = (flags & ~MAP_TYPE);
5311 5314 vn_a.cred = cr;
5312 5315 vn_a.amp = NULL;
5313 5316 vn_a.szc = 0;
5314 5317 vn_a.lgrp_mem_policy_flags = 0;
5315 5318
5316 5319 error = as_map(as, *addrp, len, segvn_create, &vn_a);
5317 5320 as_rangeunlock(as);
5318 5321
5319 5322 done:
5320 5323 nfs_rw_exit(&rp->r_lkserlock);
5321 5324 atomic_dec_uint(&rp->r_inmap);
5322 5325 return (error);
5323 5326 }
5324 5327
5325 5328 /* ARGSUSED */
5326 5329 static int
5327 5330 nfs3_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
5328 5331 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags,
5329 5332 cred_t *cr, caller_context_t *ct)
5330 5333 {
5331 5334 rnode_t *rp;
5332 5335
5333 5336 if (vp->v_flag & VNOMAP)
5334 5337 return (ENOSYS);
5335 5338 if (nfs_zone() != VTOMI(vp)->mi_zone)
5336 5339 return (EIO);
5337 5340
5338 5341 rp = VTOR(vp);
5339 5342 atomic_add_long((ulong_t *)&rp->r_mapcnt, btopr(len));
5340 5343
5341 5344 return (0);
5342 5345 }
5343 5346
5344 5347 /* ARGSUSED */
5345 5348 static int
5346 5349 nfs3_frlock(vnode_t *vp, int cmd, struct flock64 *bfp, int flag,
5347 5350 offset_t offset, struct flk_callback *flk_cbp, cred_t *cr,
5348 5351 caller_context_t *ct)
5349 5352 {
5350 5353 netobj lm_fh3;
5351 5354 int rc;
5352 5355 u_offset_t start, end;
5353 5356 rnode_t *rp;
5354 5357 int error = 0, intr = INTR(vp);
5355 5358
5356 5359 if (nfs_zone() != VTOMI(vp)->mi_zone)
5357 5360 return (EIO);
5358 5361 /* check for valid cmd parameter */
5359 5362 if (cmd != F_GETLK && cmd != F_SETLK && cmd != F_SETLKW)
5360 5363 return (EINVAL);
5361 5364
5362 5365 /* Verify l_type. */
5363 5366 switch (bfp->l_type) {
5364 5367 case F_RDLCK:
5365 5368 if (cmd != F_GETLK && !(flag & FREAD))
5366 5369 return (EBADF);
5367 5370 break;
5368 5371 case F_WRLCK:
5369 5372 if (cmd != F_GETLK && !(flag & FWRITE))
5370 5373 return (EBADF);
5371 5374 break;
5372 5375 case F_UNLCK:
5373 5376 intr = 0;
5374 5377 break;
5375 5378
5376 5379 default:
5377 5380 return (EINVAL);
5378 5381 }
5379 5382
5380 5383 /* check the validity of the lock range */
5381 5384 if (rc = flk_convert_lock_data(vp, bfp, &start, &end, offset))
5382 5385 return (rc);
5383 5386 if (rc = flk_check_lock_data(start, end, MAXEND))
5384 5387 return (rc);
5385 5388
5386 5389 /*
5387 5390 * If the filesystem is mounted using local locking, pass the
5388 5391 * request off to the local locking code.
5389 5392 */
5390 5393 if (VTOMI(vp)->mi_flags & MI_LLOCK) {
5391 5394 if (cmd == F_SETLK || cmd == F_SETLKW) {
5392 5395 /*
5393 5396 * For complete safety, we should be holding
5394 5397 * r_lkserlock. However, we can't call
5395 5398 * lm_safelock and then fs_frlock while
5396 5399 * holding r_lkserlock, so just invoke
5397 5400 * lm_safelock and expect that this will
5398 5401 * catch enough of the cases.
5399 5402 */
5400 5403 if (!lm_safelock(vp, bfp, cr))
5401 5404 return (EAGAIN);
5402 5405 }
5403 5406 return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct));
5404 5407 }
5405 5408
5406 5409 rp = VTOR(vp);
5407 5410
5408 5411 /*
5409 5412 * Check whether the given lock request can proceed, given the
5410 5413 * current file mappings.
5411 5414 */
5412 5415 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_WRITER, intr))
5413 5416 return (EINTR);
5414 5417 if (cmd == F_SETLK || cmd == F_SETLKW) {
5415 5418 if (!lm_safelock(vp, bfp, cr)) {
5416 5419 rc = EAGAIN;
5417 5420 goto done;
5418 5421 }
5419 5422 }
5420 5423
5421 5424 /*
5422 5425 * Flush the cache after waiting for async I/O to finish. For new
5423 5426 * locks, this is so that the process gets the latest bits from the
5424 5427 * server. For unlocks, this is so that other clients see the
5425 5428 * latest bits once the file has been unlocked. If currently dirty
5426 5429 * pages can't be flushed, then don't allow a lock to be set. But
5427 5430 * allow unlocks to succeed, to avoid having orphan locks on the
5428 5431 * server.
5429 5432 */
5430 5433 if (cmd != F_GETLK) {
5431 5434 mutex_enter(&rp->r_statelock);
5432 5435 while (rp->r_count > 0) {
5433 5436 if (intr) {
5434 5437 klwp_t *lwp = ttolwp(curthread);
5435 5438
5436 5439 if (lwp != NULL)
5437 5440 lwp->lwp_nostop++;
5438 5441 if (cv_wait_sig(&rp->r_cv,
5439 5442 &rp->r_statelock) == 0) {
5440 5443 if (lwp != NULL)
5441 5444 lwp->lwp_nostop--;
5442 5445 rc = EINTR;
5443 5446 break;
5444 5447 }
5445 5448 if (lwp != NULL)
5446 5449 lwp->lwp_nostop--;
5447 5450 } else
5448 5451 cv_wait(&rp->r_cv, &rp->r_statelock);
5449 5452 }
5450 5453 mutex_exit(&rp->r_statelock);
5451 5454 if (rc != 0)
5452 5455 goto done;
5453 5456 error = nfs3_putpage(vp, (offset_t)0, 0, B_INVAL, cr, ct);
5454 5457 if (error) {
5455 5458 if (error == ENOSPC || error == EDQUOT) {
5456 5459 mutex_enter(&rp->r_statelock);
5457 5460 if (!rp->r_error)
5458 5461 rp->r_error = error;
5459 5462 mutex_exit(&rp->r_statelock);
5460 5463 }
5461 5464 if (bfp->l_type != F_UNLCK) {
5462 5465 rc = ENOLCK;
5463 5466 goto done;
5464 5467 }
5465 5468 }
5466 5469 }
5467 5470
5468 5471 lm_fh3.n_len = VTOFH3(vp)->fh3_length;
5469 5472 lm_fh3.n_bytes = (char *)&(VTOFH3(vp)->fh3_u.data);
5470 5473
5471 5474 /*
5472 5475 * Call the lock manager to do the real work of contacting
5473 5476 * the server and obtaining the lock.
5474 5477 */
5475 5478 rc = lm4_frlock(vp, cmd, bfp, flag, offset, cr, &lm_fh3, flk_cbp);
5476 5479
5477 5480 if (rc == 0)
5478 5481 nfs_lockcompletion(vp, cmd);
5479 5482
5480 5483 done:
5481 5484 nfs_rw_exit(&rp->r_lkserlock);
5482 5485 return (rc);
5483 5486 }
5484 5487
5485 5488 /*
5486 5489 * Free storage space associated with the specified vnode. The portion
5487 5490 * to be freed is specified by bfp->l_start and bfp->l_len (already
5488 5491 * normalized to a "whence" of 0).
5489 5492 *
5490 5493 * This is an experimental facility whose continued existence is not
5491 5494 * guaranteed. Currently, we only support the special case
5492 5495 * of l_len == 0, meaning free to end of file.
5493 5496 */
5494 5497 /* ARGSUSED */
5495 5498 static int
5496 5499 nfs3_space(vnode_t *vp, int cmd, struct flock64 *bfp, int flag,
5497 5500 offset_t offset, cred_t *cr, caller_context_t *ct)
5498 5501 {
5499 5502 int error;
5500 5503
5501 5504 ASSERT(vp->v_type == VREG);
5502 5505 if (cmd != F_FREESP)
5503 5506 return (EINVAL);
5504 5507 if (nfs_zone() != VTOMI(vp)->mi_zone)
5505 5508 return (EIO);
5506 5509
5507 5510 error = convoff(vp, bfp, 0, offset);
5508 5511 if (!error) {
5509 5512 ASSERT(bfp->l_start >= 0);
5510 5513 if (bfp->l_len == 0) {
5511 5514 struct vattr va;
5512 5515
5513 5516 /*
5514 5517 * ftruncate should not change the ctime and
5515 5518 * mtime if we truncate the file to its
5516 5519 * previous size.
5517 5520 */
5518 5521 va.va_mask = AT_SIZE;
5519 5522 error = nfs3getattr(vp, &va, cr);
5520 5523 if (error || va.va_size == bfp->l_start)
5521 5524 return (error);
5522 5525 va.va_mask = AT_SIZE;
5523 5526 va.va_size = bfp->l_start;
5524 5527 error = nfs3setattr(vp, &va, 0, cr);
5525 5528
5526 5529 if (error == 0 && bfp->l_start == 0)
5527 5530 vnevent_truncate(vp, ct);
5528 5531 } else
5529 5532 error = EINVAL;
5530 5533 }
5531 5534
5532 5535 return (error);
5533 5536 }
5534 5537
5535 5538 /* ARGSUSED */
5536 5539 static int
5537 5540 nfs3_realvp(vnode_t *vp, vnode_t **vpp, caller_context_t *ct)
5538 5541 {
5539 5542
5540 5543 return (EINVAL);
5541 5544 }
5542 5545
5543 5546 /*
5544 5547 * Setup and add an address space callback to do the work of the delmap call.
5545 5548 * The callback will (and must be) deleted in the actual callback function.
5546 5549 *
5547 5550 * This is done in order to take care of the problem that we have with holding
5548 5551 * the address space's a_lock for a long period of time (e.g. if the NFS server
5549 5552 * is down). Callbacks will be executed in the address space code while the
5550 5553 * a_lock is not held. Holding the address space's a_lock causes things such
5551 5554 * as ps and fork to hang because they are trying to acquire this lock as well.
5552 5555 */
5553 5556 /* ARGSUSED */
5554 5557 static int
5555 5558 nfs3_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
5556 5559 size_t len, uint_t prot, uint_t maxprot, uint_t flags,
5557 5560 cred_t *cr, caller_context_t *ct)
5558 5561 {
5559 5562 int caller_found;
5560 5563 int error;
5561 5564 rnode_t *rp;
5562 5565 nfs_delmap_args_t *dmapp;
5563 5566 nfs_delmapcall_t *delmap_call;
5564 5567
5565 5568 if (vp->v_flag & VNOMAP)
5566 5569 return (ENOSYS);
5567 5570 /*
5568 5571 * A process may not change zones if it has NFS pages mmap'ed
5569 5572 * in, so we can't legitimately get here from the wrong zone.
5570 5573 */
5571 5574 ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
5572 5575
5573 5576 rp = VTOR(vp);
5574 5577
5575 5578 /*
5576 5579 * The way that the address space of this process deletes its mapping
5577 5580 * of this file is via the following call chains:
5578 5581 * - as_free()->SEGOP_UNMAP()/segvn_unmap()->VOP_DELMAP()/nfs3_delmap()
5579 5582 * - as_unmap()->SEGOP_UNMAP()/segvn_unmap()->VOP_DELMAP()/nfs3_delmap()
5580 5583 *
5581 5584 * With the use of address space callbacks we are allowed to drop the
5582 5585 * address space lock, a_lock, while executing the NFS operations that
5583 5586 * need to go over the wire. Returning EAGAIN to the caller of this
5584 5587 * function is what drives the execution of the callback that we add
5585 5588 * below. The callback will be executed by the address space code
5586 5589 * after dropping the a_lock. When the callback is finished, since
5587 5590 * we dropped the a_lock, it must be re-acquired and segvn_unmap()
5588 5591 * is called again on the same segment to finish the rest of the work
5589 5592 * that needs to happen during unmapping.
5590 5593 *
5591 5594 * This action of calling back into the segment driver causes
5592 5595 * nfs3_delmap() to get called again, but since the callback was
5593 5596 * already executed at this point, it already did the work and there
5594 5597 * is nothing left for us to do.
5595 5598 *
5596 5599 * To Summarize:
5597 5600 * - The first time nfs3_delmap is called by the current thread is when
5598 5601 * we add the caller associated with this delmap to the delmap caller
5599 5602 * list, add the callback, and return EAGAIN.
5600 5603 * - The second time in this call chain when nfs3_delmap is called we
5601 5604 * will find this caller in the delmap caller list and realize there
5602 5605 * is no more work to do thus removing this caller from the list and
5603 5606 * returning the error that was set in the callback execution.
5604 5607 */
5605 5608 caller_found = nfs_find_and_delete_delmapcall(rp, &error);
5606 5609 if (caller_found) {
5607 5610 /*
5608 5611 * 'error' is from the actual delmap operations. To avoid
5609 5612 * hangs, we need to handle the return of EAGAIN differently
5610 5613 * since this is what drives the callback execution.
5611 5614 * In this case, we don't want to return EAGAIN and do the
5612 5615 * callback execution because there are none to execute.
5613 5616 */
5614 5617 if (error == EAGAIN)
5615 5618 return (0);
5616 5619 else
5617 5620 return (error);
5618 5621 }
5619 5622
5620 5623 /* current caller was not in the list */
5621 5624 delmap_call = nfs_init_delmapcall();
5622 5625
5623 5626 mutex_enter(&rp->r_statelock);
5624 5627 list_insert_tail(&rp->r_indelmap, delmap_call);
5625 5628 mutex_exit(&rp->r_statelock);
5626 5629
5627 5630 dmapp = kmem_alloc(sizeof (nfs_delmap_args_t), KM_SLEEP);
5628 5631
5629 5632 dmapp->vp = vp;
5630 5633 dmapp->off = off;
5631 5634 dmapp->addr = addr;
5632 5635 dmapp->len = len;
5633 5636 dmapp->prot = prot;
5634 5637 dmapp->maxprot = maxprot;
5635 5638 dmapp->flags = flags;
5636 5639 dmapp->cr = cr;
5637 5640 dmapp->caller = delmap_call;
5638 5641
5639 5642 error = as_add_callback(as, nfs3_delmap_callback, dmapp,
5640 5643 AS_UNMAP_EVENT, addr, len, KM_SLEEP);
5641 5644
5642 5645 return (error ? error : EAGAIN);
5643 5646 }
5644 5647
5645 5648 /*
5646 5649 * Remove some pages from an mmap'd vnode. Just update the
5647 5650 * count of pages. If doing close-to-open, then flush and
5648 5651 * commit all of the pages associated with this file.
5649 5652 * Otherwise, start an asynchronous page flush to write out
5650 5653 * any dirty pages. This will also associate a credential
5651 5654 * with the rnode which can be used to write the pages.
5652 5655 */
5653 5656 /* ARGSUSED */
5654 5657 static void
5655 5658 nfs3_delmap_callback(struct as *as, void *arg, uint_t event)
5656 5659 {
5657 5660 int error;
5658 5661 rnode_t *rp;
5659 5662 mntinfo_t *mi;
5660 5663 nfs_delmap_args_t *dmapp = (nfs_delmap_args_t *)arg;
5661 5664
5662 5665 rp = VTOR(dmapp->vp);
5663 5666 mi = VTOMI(dmapp->vp);
5664 5667
5665 5668 atomic_add_long((ulong_t *)&rp->r_mapcnt, -btopr(dmapp->len));
5666 5669 ASSERT(rp->r_mapcnt >= 0);
5667 5670
5668 5671 /*
5669 5672 * Initiate a page flush and potential commit if there are
5670 5673 * pages, the file system was not mounted readonly, the segment
5671 5674 * was mapped shared, and the pages themselves were writeable.
5672 5675 */
5673 5676 if (vn_has_cached_data(dmapp->vp) && !vn_is_readonly(dmapp->vp) &&
5674 5677 dmapp->flags == MAP_SHARED && (dmapp->maxprot & PROT_WRITE)) {
5675 5678 mutex_enter(&rp->r_statelock);
5676 5679 rp->r_flags |= RDIRTY;
5677 5680 mutex_exit(&rp->r_statelock);
5678 5681 /*
5679 5682 * If this is a cross-zone access a sync putpage won't work, so
5680 5683 * the best we can do is try an async putpage. That seems
5681 5684 * better than something more draconian such as discarding the
5682 5685 * dirty pages.
5683 5686 */
5684 5687 if ((mi->mi_flags & MI_NOCTO) ||
5685 5688 nfs_zone() != mi->mi_zone)
5686 5689 error = nfs3_putpage(dmapp->vp, dmapp->off, dmapp->len,
5687 5690 B_ASYNC, dmapp->cr, NULL);
5688 5691 else
5689 5692 error = nfs3_putpage_commit(dmapp->vp, dmapp->off,
5690 5693 dmapp->len, dmapp->cr);
5691 5694 if (!error) {
5692 5695 mutex_enter(&rp->r_statelock);
5693 5696 error = rp->r_error;
5694 5697 rp->r_error = 0;
5695 5698 mutex_exit(&rp->r_statelock);
5696 5699 }
5697 5700 } else
5698 5701 error = 0;
|
↓ open down ↓ |
1167 lines elided |
↑ open up ↑ |
5699 5702
5700 5703 if ((rp->r_flags & RDIRECTIO) || (mi->mi_flags & MI_DIRECTIO))
5701 5704 (void) nfs3_putpage(dmapp->vp, dmapp->off, dmapp->len,
5702 5705 B_INVAL, dmapp->cr, NULL);
5703 5706
5704 5707 dmapp->caller->error = error;
5705 5708 (void) as_delete_callback(as, arg);
5706 5709 kmem_free(dmapp, sizeof (nfs_delmap_args_t));
5707 5710 }
5708 5711
5709 -static int nfs3_pathconf_disable_cache = 0;
5712 +volatile int nfs3_pathconf_disable_cache = 0;
5710 5713
5711 5714 #ifdef DEBUG
5712 5715 static int nfs3_pathconf_cache_hits = 0;
5713 5716 static int nfs3_pathconf_cache_misses = 0;
5714 5717 #endif
5715 5718
5716 5719 /* ARGSUSED */
5717 5720 static int
5718 5721 nfs3_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
5719 5722 caller_context_t *ct)
5720 5723 {
5721 5724 int error;
5722 5725 PATHCONF3args args;
5723 5726 PATHCONF3res res;
5724 5727 int douprintf;
5725 5728 failinfo_t fi;
5726 5729 rnode_t *rp;
5727 5730 hrtime_t t;
5728 5731
5729 5732 if (nfs_zone() != VTOMI(vp)->mi_zone)
5730 5733 return (EIO);
5731 5734 /*
5732 5735 * Large file spec - need to base answer on info stored
5733 5736 * on original FSINFO response.
5734 5737 */
5735 5738 if (cmd == _PC_FILESIZEBITS) {
5736 5739 unsigned long long ll;
5737 5740 long l = 1;
5738 5741
5739 5742 ll = VTOMI(vp)->mi_maxfilesize;
5740 5743
5741 5744 if (ll == 0) {
5742 5745 *valp = 0;
5743 5746 return (0);
5744 5747 }
5745 5748
5746 5749 if (ll & 0xffffffff00000000) {
5747 5750 l += 32; ll >>= 32;
5748 5751 }
5749 5752 if (ll & 0xffff0000) {
5750 5753 l += 16; ll >>= 16;
5751 5754 }
5752 5755 if (ll & 0xff00) {
5753 5756 l += 8; ll >>= 8;
5754 5757 }
5755 5758 if (ll & 0xf0) {
5756 5759 l += 4; ll >>= 4;
5757 5760 }
5758 5761 if (ll & 0xc) {
5759 5762 l += 2; ll >>= 2;
5760 5763 }
5761 5764 if (ll & 0x2)
5762 5765 l += 2;
5763 5766 else if (ll & 0x1)
5764 5767 l += 1;
5765 5768 *valp = l;
5766 5769 return (0);
5767 5770 }
5768 5771
5769 5772 if (cmd == _PC_ACL_ENABLED) {
5770 5773 *valp = _ACL_ACLENT_ENABLED;
5771 5774 return (0);
5772 5775 }
5773 5776
5774 5777 if (cmd == _PC_XATTR_EXISTS) {
5775 5778 error = 0;
5776 5779 *valp = 0;
5777 5780 if (vp->v_vfsp->vfs_flag & VFS_XATTR) {
5778 5781 vnode_t *avp;
5779 5782 rnode_t *rp;
5780 5783 int error = 0;
5781 5784 mntinfo_t *mi = VTOMI(vp);
5782 5785
5783 5786 if (!(mi->mi_flags & MI_EXTATTR))
5784 5787 return (0);
5785 5788
5786 5789 rp = VTOR(vp);
5787 5790 if (nfs_rw_enter_sig(&rp->r_rwlock, RW_READER,
5788 5791 INTR(vp)))
5789 5792 return (EINTR);
5790 5793
5791 5794 error = nfs3lookup_dnlc(vp, XATTR_DIR_NAME, &avp, cr);
5792 5795 if (error || avp == NULL)
5793 5796 error = acl_getxattrdir3(vp, &avp, 0, cr, 0);
5794 5797
5795 5798 nfs_rw_exit(&rp->r_rwlock);
5796 5799
5797 5800 if (error == 0 && avp != NULL) {
5798 5801 error = do_xattr_exists_check(avp, valp, cr);
5799 5802 VN_RELE(avp);
5800 5803 } else if (error == ENOENT) {
5801 5804 error = 0;
5802 5805 *valp = 0;
5803 5806 }
5804 5807 }
5805 5808 return (error);
5806 5809 }
5807 5810
5808 5811 rp = VTOR(vp);
5809 5812 if (rp->r_pathconf != NULL) {
5810 5813 mutex_enter(&rp->r_statelock);
5811 5814 if (rp->r_pathconf != NULL && nfs3_pathconf_disable_cache) {
5812 5815 kmem_free(rp->r_pathconf, sizeof (*rp->r_pathconf));
5813 5816 rp->r_pathconf = NULL;
5814 5817 }
5815 5818 if (rp->r_pathconf != NULL) {
5816 5819 error = 0;
5817 5820 switch (cmd) {
5818 5821 case _PC_LINK_MAX:
5819 5822 *valp = rp->r_pathconf->link_max;
5820 5823 break;
5821 5824 case _PC_NAME_MAX:
5822 5825 *valp = rp->r_pathconf->name_max;
5823 5826 break;
5824 5827 case _PC_PATH_MAX:
5825 5828 case _PC_SYMLINK_MAX:
5826 5829 *valp = MAXPATHLEN;
5827 5830 break;
5828 5831 case _PC_CHOWN_RESTRICTED:
5829 5832 *valp = rp->r_pathconf->chown_restricted;
5830 5833 break;
5831 5834 case _PC_NO_TRUNC:
5832 5835 *valp = rp->r_pathconf->no_trunc;
5833 5836 break;
5834 5837 default:
5835 5838 error = EINVAL;
5836 5839 break;
5837 5840 }
5838 5841 mutex_exit(&rp->r_statelock);
5839 5842 #ifdef DEBUG
5840 5843 nfs3_pathconf_cache_hits++;
5841 5844 #endif
5842 5845 return (error);
5843 5846 }
5844 5847 mutex_exit(&rp->r_statelock);
5845 5848 }
5846 5849 #ifdef DEBUG
5847 5850 nfs3_pathconf_cache_misses++;
5848 5851 #endif
5849 5852
5850 5853 args.object = *VTOFH3(vp);
5851 5854 fi.vp = vp;
5852 5855 fi.fhp = (caddr_t)&args.object;
5853 5856 fi.copyproc = nfs3copyfh;
5854 5857 fi.lookupproc = nfs3lookup;
5855 5858 fi.xattrdirproc = acl_getxattrdir3;
5856 5859
5857 5860 douprintf = 1;
5858 5861
5859 5862 t = gethrtime();
5860 5863
5861 5864 error = rfs3call(VTOMI(vp), NFSPROC3_PATHCONF,
5862 5865 xdr_nfs_fh3, (caddr_t)&args,
5863 5866 xdr_PATHCONF3res, (caddr_t)&res, cr,
5864 5867 &douprintf, &res.status, 0, &fi);
5865 5868
5866 5869 if (error)
5867 5870 return (error);
5868 5871
5869 5872 error = geterrno3(res.status);
5870 5873
5871 5874 if (!error) {
5872 5875 nfs3_cache_post_op_attr(vp, &res.resok.obj_attributes, t, cr);
5873 5876 if (!nfs3_pathconf_disable_cache) {
5874 5877 mutex_enter(&rp->r_statelock);
5875 5878 if (rp->r_pathconf == NULL) {
5876 5879 rp->r_pathconf = kmem_alloc(
5877 5880 sizeof (*rp->r_pathconf), KM_NOSLEEP);
5878 5881 if (rp->r_pathconf != NULL)
5879 5882 *rp->r_pathconf = res.resok.info;
5880 5883 }
5881 5884 mutex_exit(&rp->r_statelock);
5882 5885 }
5883 5886 switch (cmd) {
5884 5887 case _PC_LINK_MAX:
5885 5888 *valp = res.resok.info.link_max;
5886 5889 break;
5887 5890 case _PC_NAME_MAX:
5888 5891 *valp = res.resok.info.name_max;
5889 5892 break;
5890 5893 case _PC_PATH_MAX:
5891 5894 case _PC_SYMLINK_MAX:
5892 5895 *valp = MAXPATHLEN;
5893 5896 break;
5894 5897 case _PC_CHOWN_RESTRICTED:
5895 5898 *valp = res.resok.info.chown_restricted;
5896 5899 break;
5897 5900 case _PC_NO_TRUNC:
5898 5901 *valp = res.resok.info.no_trunc;
5899 5902 break;
5900 5903 default:
5901 5904 return (EINVAL);
5902 5905 }
5903 5906 } else {
5904 5907 nfs3_cache_post_op_attr(vp, &res.resfail.obj_attributes, t, cr);
5905 5908 PURGE_STALE_FH(error, vp, cr);
5906 5909 }
5907 5910
5908 5911 return (error);
5909 5912 }
5910 5913
5911 5914 /*
5912 5915 * Called by async thread to do synchronous pageio. Do the i/o, wait
5913 5916 * for it to complete, and cleanup the page list when done.
5914 5917 */
5915 5918 static int
5916 5919 nfs3_sync_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len,
5917 5920 int flags, cred_t *cr)
5918 5921 {
5919 5922 int error;
5920 5923
5921 5924 ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
5922 5925 error = nfs3_rdwrlbn(vp, pp, io_off, io_len, flags, cr);
5923 5926 if (flags & B_READ)
5924 5927 pvn_read_done(pp, (error ? B_ERROR : 0) | flags);
5925 5928 else
5926 5929 pvn_write_done(pp, (error ? B_ERROR : 0) | flags);
5927 5930 return (error);
5928 5931 }
5929 5932
5930 5933 /* ARGSUSED */
5931 5934 static int
5932 5935 nfs3_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len,
5933 5936 int flags, cred_t *cr, caller_context_t *ct)
5934 5937 {
5935 5938 int error;
5936 5939 rnode_t *rp;
5937 5940
5938 5941 if (pp == NULL)
5939 5942 return (EINVAL);
5940 5943 if (!(flags & B_ASYNC) && nfs_zone() != VTOMI(vp)->mi_zone)
5941 5944 return (EIO);
5942 5945
5943 5946 rp = VTOR(vp);
5944 5947 mutex_enter(&rp->r_statelock);
5945 5948 rp->r_count++;
5946 5949 mutex_exit(&rp->r_statelock);
5947 5950
5948 5951 if (flags & B_ASYNC) {
5949 5952 error = nfs_async_pageio(vp, pp, io_off, io_len, flags, cr,
5950 5953 nfs3_sync_pageio);
5951 5954 } else
5952 5955 error = nfs3_rdwrlbn(vp, pp, io_off, io_len, flags, cr);
5953 5956 mutex_enter(&rp->r_statelock);
5954 5957 rp->r_count--;
5955 5958 cv_broadcast(&rp->r_cv);
5956 5959 mutex_exit(&rp->r_statelock);
5957 5960 return (error);
5958 5961 }
5959 5962
5960 5963 /* ARGSUSED */
5961 5964 static void
5962 5965 nfs3_dispose(vnode_t *vp, page_t *pp, int fl, int dn, cred_t *cr,
5963 5966 caller_context_t *ct)
5964 5967 {
5965 5968 int error;
5966 5969 rnode_t *rp;
5967 5970 page_t *plist;
5968 5971 page_t *pptr;
5969 5972 offset3 offset;
5970 5973 count3 len;
5971 5974 k_sigset_t smask;
5972 5975
5973 5976 /*
5974 5977 * We should get called with fl equal to either B_FREE or
5975 5978 * B_INVAL. Any other value is illegal.
5976 5979 *
5977 5980 * The page that we are either supposed to free or destroy
5978 5981 * should be exclusive locked and its io lock should not
5979 5982 * be held.
5980 5983 */
5981 5984 ASSERT(fl == B_FREE || fl == B_INVAL);
5982 5985 ASSERT((PAGE_EXCL(pp) && !page_iolock_assert(pp)) || panicstr);
5983 5986 rp = VTOR(vp);
5984 5987
5985 5988 /*
5986 5989 * If the page doesn't need to be committed or we shouldn't
5987 5990 * even bother attempting to commit it, then just make sure
5988 5991 * that the p_fsdata byte is clear and then either free or
5989 5992 * destroy the page as appropriate.
5990 5993 */
5991 5994 if (pp->p_fsdata == C_NOCOMMIT || (rp->r_flags & RSTALE)) {
5992 5995 pp->p_fsdata = C_NOCOMMIT;
5993 5996 if (fl == B_FREE)
5994 5997 page_free(pp, dn);
5995 5998 else
5996 5999 page_destroy(pp, dn);
5997 6000 return;
5998 6001 }
5999 6002
6000 6003 /*
6001 6004 * If there is a page invalidation operation going on, then
6002 6005 * if this is one of the pages being destroyed, then just
6003 6006 * clear the p_fsdata byte and then either free or destroy
6004 6007 * the page as appropriate.
6005 6008 */
6006 6009 mutex_enter(&rp->r_statelock);
6007 6010 if ((rp->r_flags & RTRUNCATE) && pp->p_offset >= rp->r_truncaddr) {
6008 6011 mutex_exit(&rp->r_statelock);
6009 6012 pp->p_fsdata = C_NOCOMMIT;
6010 6013 if (fl == B_FREE)
6011 6014 page_free(pp, dn);
6012 6015 else
6013 6016 page_destroy(pp, dn);
6014 6017 return;
6015 6018 }
6016 6019
6017 6020 /*
6018 6021 * If we are freeing this page and someone else is already
6019 6022 * waiting to do a commit, then just unlock the page and
6020 6023 * return. That other thread will take care of commiting
6021 6024 * this page. The page can be freed sometime after the
6022 6025 * commit has finished. Otherwise, if the page is marked
6023 6026 * as delay commit, then we may be getting called from
6024 6027 * pvn_write_done, one page at a time. This could result
6025 6028 * in one commit per page, so we end up doing lots of small
6026 6029 * commits instead of fewer larger commits. This is bad,
6027 6030 * we want do as few commits as possible.
6028 6031 */
6029 6032 if (fl == B_FREE) {
6030 6033 if (rp->r_flags & RCOMMITWAIT) {
6031 6034 page_unlock(pp);
6032 6035 mutex_exit(&rp->r_statelock);
6033 6036 return;
6034 6037 }
6035 6038 if (pp->p_fsdata == C_DELAYCOMMIT) {
6036 6039 pp->p_fsdata = C_COMMIT;
6037 6040 page_unlock(pp);
6038 6041 mutex_exit(&rp->r_statelock);
6039 6042 return;
6040 6043 }
6041 6044 }
6042 6045
6043 6046 /*
6044 6047 * Check to see if there is a signal which would prevent an
6045 6048 * attempt to commit the pages from being successful. If so,
6046 6049 * then don't bother with all of the work to gather pages and
6047 6050 * generate the unsuccessful RPC. Just return from here and
6048 6051 * let the page be committed at some later time.
6049 6052 */
6050 6053 sigintr(&smask, VTOMI(vp)->mi_flags & MI_INT);
6051 6054 if (ttolwp(curthread) != NULL && ISSIG(curthread, JUSTLOOKING)) {
6052 6055 sigunintr(&smask);
6053 6056 page_unlock(pp);
6054 6057 mutex_exit(&rp->r_statelock);
6055 6058 return;
6056 6059 }
6057 6060 sigunintr(&smask);
6058 6061
6059 6062 /*
6060 6063 * We are starting to need to commit pages, so let's try
6061 6064 * to commit as many as possible at once to reduce the
6062 6065 * overhead.
6063 6066 *
6064 6067 * Set the `commit inprogress' state bit. We must
6065 6068 * first wait until any current one finishes. Then
6066 6069 * we initialize the c_pages list with this page.
6067 6070 */
6068 6071 while (rp->r_flags & RCOMMIT) {
6069 6072 rp->r_flags |= RCOMMITWAIT;
6070 6073 cv_wait(&rp->r_commit.c_cv, &rp->r_statelock);
6071 6074 rp->r_flags &= ~RCOMMITWAIT;
6072 6075 }
6073 6076 rp->r_flags |= RCOMMIT;
6074 6077 mutex_exit(&rp->r_statelock);
6075 6078 ASSERT(rp->r_commit.c_pages == NULL);
6076 6079 rp->r_commit.c_pages = pp;
6077 6080 rp->r_commit.c_commbase = (offset3)pp->p_offset;
6078 6081 rp->r_commit.c_commlen = PAGESIZE;
6079 6082
6080 6083 /*
6081 6084 * Gather together all other pages which can be committed.
6082 6085 * They will all be chained off r_commit.c_pages.
6083 6086 */
6084 6087 nfs3_get_commit(vp);
6085 6088
6086 6089 /*
6087 6090 * Clear the `commit inprogress' status and disconnect
6088 6091 * the list of pages to be committed from the rnode.
6089 6092 * At this same time, we also save the starting offset
6090 6093 * and length of data to be committed on the server.
6091 6094 */
6092 6095 plist = rp->r_commit.c_pages;
6093 6096 rp->r_commit.c_pages = NULL;
6094 6097 offset = rp->r_commit.c_commbase;
6095 6098 len = rp->r_commit.c_commlen;
6096 6099 mutex_enter(&rp->r_statelock);
6097 6100 rp->r_flags &= ~RCOMMIT;
6098 6101 cv_broadcast(&rp->r_commit.c_cv);
6099 6102 mutex_exit(&rp->r_statelock);
6100 6103
6101 6104 if (curproc == proc_pageout || curproc == proc_fsflush ||
6102 6105 nfs_zone() != VTOMI(vp)->mi_zone) {
6103 6106 nfs_async_commit(vp, plist, offset, len, cr, nfs3_async_commit);
6104 6107 return;
6105 6108 }
6106 6109
6107 6110 /*
6108 6111 * Actually generate the COMMIT3 over the wire operation.
6109 6112 */
6110 6113 error = nfs3_commit(vp, offset, len, cr);
6111 6114
6112 6115 /*
6113 6116 * If we got an error during the commit, just unlock all
6114 6117 * of the pages. The pages will get retransmitted to the
6115 6118 * server during a putpage operation.
6116 6119 */
6117 6120 if (error) {
6118 6121 while (plist != NULL) {
6119 6122 pptr = plist;
6120 6123 page_sub(&plist, pptr);
6121 6124 page_unlock(pptr);
6122 6125 }
6123 6126 return;
6124 6127 }
6125 6128
6126 6129 /*
6127 6130 * We've tried as hard as we can to commit the data to stable
6128 6131 * storage on the server. We release the rest of the pages
6129 6132 * and clear the commit required state. They will be put
6130 6133 * onto the tail of the cachelist if they are nolonger
6131 6134 * mapped.
6132 6135 */
6133 6136 while (plist != pp) {
6134 6137 pptr = plist;
6135 6138 page_sub(&plist, pptr);
6136 6139 pptr->p_fsdata = C_NOCOMMIT;
6137 6140 (void) page_release(pptr, 1);
6138 6141 }
6139 6142
6140 6143 /*
6141 6144 * It is possible that nfs3_commit didn't return error but
6142 6145 * some other thread has modified the page we are going
6143 6146 * to free/destroy.
6144 6147 * In this case we need to rewrite the page. Do an explicit check
6145 6148 * before attempting to free/destroy the page. If modified, needs to
6146 6149 * be rewritten so unlock the page and return.
6147 6150 */
6148 6151 if (hat_ismod(pp)) {
6149 6152 pp->p_fsdata = C_NOCOMMIT;
6150 6153 page_unlock(pp);
6151 6154 return;
6152 6155 }
6153 6156
6154 6157 /*
6155 6158 * Now, as appropriate, either free or destroy the page
6156 6159 * that we were called with.
6157 6160 */
6158 6161 pp->p_fsdata = C_NOCOMMIT;
6159 6162 if (fl == B_FREE)
6160 6163 page_free(pp, dn);
6161 6164 else
6162 6165 page_destroy(pp, dn);
6163 6166 }
6164 6167
6165 6168 static int
6166 6169 nfs3_commit(vnode_t *vp, offset3 offset, count3 count, cred_t *cr)
6167 6170 {
6168 6171 int error;
6169 6172 rnode_t *rp;
6170 6173 COMMIT3args args;
6171 6174 COMMIT3res res;
6172 6175 int douprintf;
6173 6176 cred_t *cred;
6174 6177
6175 6178 rp = VTOR(vp);
6176 6179 ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
6177 6180
6178 6181 mutex_enter(&rp->r_statelock);
6179 6182 if (rp->r_cred != NULL) {
6180 6183 cred = rp->r_cred;
6181 6184 crhold(cred);
6182 6185 } else {
6183 6186 rp->r_cred = cr;
6184 6187 crhold(cr);
6185 6188 cred = cr;
6186 6189 crhold(cred);
6187 6190 }
6188 6191 mutex_exit(&rp->r_statelock);
6189 6192
6190 6193 args.file = *VTOFH3(vp);
6191 6194 args.offset = offset;
6192 6195 args.count = count;
6193 6196
6194 6197 doitagain:
6195 6198 douprintf = 1;
6196 6199 error = rfs3call(VTOMI(vp), NFSPROC3_COMMIT,
6197 6200 xdr_COMMIT3args, (caddr_t)&args,
6198 6201 xdr_COMMIT3res, (caddr_t)&res, cred,
6199 6202 &douprintf, &res.status, 0, NULL);
6200 6203
6201 6204 crfree(cred);
6202 6205
6203 6206 if (error)
6204 6207 return (error);
6205 6208
6206 6209 error = geterrno3(res.status);
6207 6210 if (!error) {
6208 6211 ASSERT(rp->r_flags & RHAVEVERF);
6209 6212 mutex_enter(&rp->r_statelock);
6210 6213 if (rp->r_verf == res.resok.verf) {
6211 6214 mutex_exit(&rp->r_statelock);
6212 6215 return (0);
6213 6216 }
6214 6217 nfs3_set_mod(vp);
6215 6218 rp->r_verf = res.resok.verf;
6216 6219 mutex_exit(&rp->r_statelock);
6217 6220 error = NFS_VERF_MISMATCH;
6218 6221 } else {
6219 6222 if (error == EACCES) {
6220 6223 mutex_enter(&rp->r_statelock);
6221 6224 if (cred != cr) {
6222 6225 if (rp->r_cred != NULL)
6223 6226 crfree(rp->r_cred);
6224 6227 rp->r_cred = cr;
6225 6228 crhold(cr);
6226 6229 cred = cr;
6227 6230 crhold(cred);
6228 6231 mutex_exit(&rp->r_statelock);
6229 6232 goto doitagain;
6230 6233 }
6231 6234 mutex_exit(&rp->r_statelock);
6232 6235 }
6233 6236 /*
6234 6237 * Can't do a PURGE_STALE_FH here because this
6235 6238 * can cause a deadlock. nfs3_commit can
6236 6239 * be called from nfs3_dispose which can be called
6237 6240 * indirectly via pvn_vplist_dirty. PURGE_STALE_FH
6238 6241 * can call back to pvn_vplist_dirty.
6239 6242 */
6240 6243 if (error == ESTALE) {
6241 6244 mutex_enter(&rp->r_statelock);
6242 6245 rp->r_flags |= RSTALE;
6243 6246 if (!rp->r_error)
6244 6247 rp->r_error = error;
6245 6248 mutex_exit(&rp->r_statelock);
6246 6249 PURGE_ATTRCACHE(vp);
6247 6250 } else {
6248 6251 mutex_enter(&rp->r_statelock);
6249 6252 if (!rp->r_error)
6250 6253 rp->r_error = error;
6251 6254 mutex_exit(&rp->r_statelock);
6252 6255 }
6253 6256 }
6254 6257
6255 6258 return (error);
6256 6259 }
6257 6260
6258 6261 static void
6259 6262 nfs3_set_mod(vnode_t *vp)
6260 6263 {
6261 6264 ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
6262 6265
6263 6266 pvn_vplist_setdirty(vp, nfs_setmod_check);
6264 6267 }
6265 6268
6266 6269 /*
6267 6270 * This routine is used to gather together a page list of the pages
6268 6271 * which are to be committed on the server. This routine must not
6269 6272 * be called if the calling thread holds any locked pages.
6270 6273 *
6271 6274 * The calling thread must have set RCOMMIT. This bit is used to
6272 6275 * serialize access to the commit structure in the rnode. As long
6273 6276 * as the thread has set RCOMMIT, then it can manipulate the commit
6274 6277 * structure without requiring any other locks.
6275 6278 */
6276 6279 static void
6277 6280 nfs3_get_commit(vnode_t *vp)
6278 6281 {
6279 6282 rnode_t *rp;
6280 6283 page_t *pp;
6281 6284 kmutex_t *vphm;
6282 6285
6283 6286 rp = VTOR(vp);
6284 6287
6285 6288 ASSERT(rp->r_flags & RCOMMIT);
6286 6289
6287 6290 vphm = page_vnode_mutex(vp);
6288 6291 mutex_enter(vphm);
6289 6292
6290 6293 /*
6291 6294 * If there are no pages associated with this vnode, then
6292 6295 * just return.
6293 6296 */
6294 6297 if ((pp = vp->v_pages) == NULL) {
6295 6298 mutex_exit(vphm);
6296 6299 return;
6297 6300 }
6298 6301
6299 6302 /*
6300 6303 * Step through all of the pages associated with this vnode
6301 6304 * looking for pages which need to be committed.
6302 6305 */
6303 6306 do {
6304 6307 /* Skip marker pages. */
6305 6308 if (pp->p_hash == PVN_VPLIST_HASH_TAG)
6306 6309 continue;
6307 6310
6308 6311 /*
6309 6312 * If this page does not need to be committed or is
6310 6313 * modified, then just skip it.
6311 6314 */
6312 6315 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp))
6313 6316 continue;
6314 6317
6315 6318 /*
6316 6319 * Attempt to lock the page. If we can't, then
6317 6320 * someone else is messing with it and we will
6318 6321 * just skip it.
6319 6322 */
6320 6323 if (!page_trylock(pp, SE_EXCL))
6321 6324 continue;
6322 6325
6323 6326 /*
6324 6327 * If this page does not need to be committed or is
6325 6328 * modified, then just skip it. Recheck now that
6326 6329 * the page is locked.
6327 6330 */
6328 6331 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) {
6329 6332 page_unlock(pp);
6330 6333 continue;
6331 6334 }
6332 6335
6333 6336 if (PP_ISFREE(pp)) {
6334 6337 cmn_err(CE_PANIC, "nfs3_get_commit: %p is free",
6335 6338 (void *)pp);
6336 6339 }
6337 6340
6338 6341 /*
6339 6342 * The page needs to be committed and we locked it.
6340 6343 * Update the base and length parameters and add it
6341 6344 * to r_pages.
6342 6345 */
6343 6346 if (rp->r_commit.c_pages == NULL) {
6344 6347 rp->r_commit.c_commbase = (offset3)pp->p_offset;
6345 6348 rp->r_commit.c_commlen = PAGESIZE;
6346 6349 } else if (pp->p_offset < rp->r_commit.c_commbase) {
6347 6350 rp->r_commit.c_commlen = rp->r_commit.c_commbase -
6348 6351 (offset3)pp->p_offset + rp->r_commit.c_commlen;
6349 6352 rp->r_commit.c_commbase = (offset3)pp->p_offset;
6350 6353 } else if ((rp->r_commit.c_commbase + rp->r_commit.c_commlen)
6351 6354 <= pp->p_offset) {
6352 6355 rp->r_commit.c_commlen = (offset3)pp->p_offset -
6353 6356 rp->r_commit.c_commbase + PAGESIZE;
6354 6357 }
6355 6358 page_add(&rp->r_commit.c_pages, pp);
6356 6359 } while ((pp = pp->p_vpnext) != vp->v_pages);
6357 6360
6358 6361 mutex_exit(vphm);
6359 6362 }
6360 6363
6361 6364 /*
6362 6365 * This routine is used to gather together a page list of the pages
6363 6366 * which are to be committed on the server. This routine must not
6364 6367 * be called if the calling thread holds any locked pages.
6365 6368 *
6366 6369 * The calling thread must have set RCOMMIT. This bit is used to
6367 6370 * serialize access to the commit structure in the rnode. As long
6368 6371 * as the thread has set RCOMMIT, then it can manipulate the commit
6369 6372 * structure without requiring any other locks.
6370 6373 */
6371 6374 static void
6372 6375 nfs3_get_commit_range(vnode_t *vp, u_offset_t soff, size_t len)
6373 6376 {
6374 6377
6375 6378 rnode_t *rp;
6376 6379 page_t *pp;
6377 6380 u_offset_t end;
6378 6381 u_offset_t off;
6379 6382
6380 6383 ASSERT(len != 0);
6381 6384
6382 6385 rp = VTOR(vp);
6383 6386
6384 6387 ASSERT(rp->r_flags & RCOMMIT);
6385 6388 ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
6386 6389
6387 6390 /*
6388 6391 * If there are no pages associated with this vnode, then
6389 6392 * just return.
6390 6393 */
6391 6394 if ((pp = vp->v_pages) == NULL)
6392 6395 return;
6393 6396
6394 6397 /*
6395 6398 * Calculate the ending offset.
6396 6399 */
6397 6400 end = soff + len;
6398 6401
6399 6402 for (off = soff; off < end; off += PAGESIZE) {
6400 6403 /*
6401 6404 * Lookup each page by vp, offset.
6402 6405 */
6403 6406 if ((pp = page_lookup_nowait(vp, off, SE_EXCL)) == NULL)
6404 6407 continue;
6405 6408
6406 6409 /*
6407 6410 * If this page does not need to be committed or is
6408 6411 * modified, then just skip it.
6409 6412 */
6410 6413 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) {
6411 6414 page_unlock(pp);
6412 6415 continue;
6413 6416 }
6414 6417
6415 6418 ASSERT(PP_ISFREE(pp) == 0);
6416 6419
6417 6420 /*
6418 6421 * The page needs to be committed and we locked it.
6419 6422 * Update the base and length parameters and add it
6420 6423 * to r_pages.
6421 6424 */
6422 6425 if (rp->r_commit.c_pages == NULL) {
6423 6426 rp->r_commit.c_commbase = (offset3)pp->p_offset;
6424 6427 rp->r_commit.c_commlen = PAGESIZE;
6425 6428 } else {
6426 6429 rp->r_commit.c_commlen = (offset3)pp->p_offset -
6427 6430 rp->r_commit.c_commbase + PAGESIZE;
6428 6431 }
6429 6432 page_add(&rp->r_commit.c_pages, pp);
6430 6433 }
6431 6434 }
6432 6435
6433 6436 static int
6434 6437 nfs3_putpage_commit(vnode_t *vp, offset_t poff, size_t plen, cred_t *cr)
6435 6438 {
6436 6439 int error;
6437 6440 writeverf3 write_verf;
6438 6441 rnode_t *rp = VTOR(vp);
6439 6442
6440 6443 ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
6441 6444 /*
6442 6445 * Flush the data portion of the file and then commit any
6443 6446 * portions which need to be committed. This may need to
6444 6447 * be done twice if the server has changed state since
6445 6448 * data was last written. The data will need to be
6446 6449 * rewritten to the server and then a new commit done.
6447 6450 *
6448 6451 * In fact, this may need to be done several times if the
6449 6452 * server is having problems and crashing while we are
6450 6453 * attempting to do this.
6451 6454 */
6452 6455
6453 6456 top:
6454 6457 /*
6455 6458 * Do a flush based on the poff and plen arguments. This
6456 6459 * will asynchronously write out any modified pages in the
6457 6460 * range specified by (poff, plen). This starts all of the
6458 6461 * i/o operations which will be waited for in the next
6459 6462 * call to nfs3_putpage
6460 6463 */
6461 6464
6462 6465 mutex_enter(&rp->r_statelock);
6463 6466 write_verf = rp->r_verf;
6464 6467 mutex_exit(&rp->r_statelock);
6465 6468
6466 6469 error = nfs3_putpage(vp, poff, plen, B_ASYNC, cr, NULL);
6467 6470 if (error == EAGAIN)
6468 6471 error = 0;
6469 6472
6470 6473 /*
6471 6474 * Do a flush based on the poff and plen arguments. This
6472 6475 * will synchronously write out any modified pages in the
6473 6476 * range specified by (poff, plen) and wait until all of
6474 6477 * the asynchronous i/o's in that range are done as well.
6475 6478 */
6476 6479 if (!error)
6477 6480 error = nfs3_putpage(vp, poff, plen, 0, cr, NULL);
6478 6481
6479 6482 if (error)
6480 6483 return (error);
6481 6484
6482 6485 mutex_enter(&rp->r_statelock);
6483 6486 if (rp->r_verf != write_verf) {
6484 6487 mutex_exit(&rp->r_statelock);
6485 6488 goto top;
6486 6489 }
6487 6490 mutex_exit(&rp->r_statelock);
6488 6491
6489 6492 /*
6490 6493 * Now commit any pages which might need to be committed.
6491 6494 * If the error, NFS_VERF_MISMATCH, is returned, then
6492 6495 * start over with the flush operation.
6493 6496 */
6494 6497
6495 6498 error = nfs3_commit_vp(vp, poff, plen, cr);
6496 6499
6497 6500 if (error == NFS_VERF_MISMATCH)
6498 6501 goto top;
6499 6502
6500 6503 return (error);
6501 6504 }
6502 6505
6503 6506 static int
6504 6507 nfs3_commit_vp(vnode_t *vp, u_offset_t poff, size_t plen, cred_t *cr)
6505 6508 {
6506 6509 rnode_t *rp;
6507 6510 page_t *plist;
6508 6511 offset3 offset;
6509 6512 count3 len;
6510 6513
6511 6514
6512 6515 rp = VTOR(vp);
6513 6516
6514 6517 if (nfs_zone() != VTOMI(vp)->mi_zone)
6515 6518 return (EIO);
6516 6519 /*
6517 6520 * Set the `commit inprogress' state bit. We must
6518 6521 * first wait until any current one finishes.
6519 6522 */
6520 6523 mutex_enter(&rp->r_statelock);
6521 6524 while (rp->r_flags & RCOMMIT) {
6522 6525 rp->r_flags |= RCOMMITWAIT;
6523 6526 cv_wait(&rp->r_commit.c_cv, &rp->r_statelock);
6524 6527 rp->r_flags &= ~RCOMMITWAIT;
6525 6528 }
6526 6529 rp->r_flags |= RCOMMIT;
6527 6530 mutex_exit(&rp->r_statelock);
6528 6531
6529 6532 /*
6530 6533 * Gather together all of the pages which need to be
6531 6534 * committed.
6532 6535 */
6533 6536 if (plen == 0)
6534 6537 nfs3_get_commit(vp);
6535 6538 else
6536 6539 nfs3_get_commit_range(vp, poff, plen);
6537 6540
6538 6541 /*
6539 6542 * Clear the `commit inprogress' bit and disconnect the
6540 6543 * page list which was gathered together in nfs3_get_commit.
6541 6544 */
6542 6545 plist = rp->r_commit.c_pages;
6543 6546 rp->r_commit.c_pages = NULL;
6544 6547 offset = rp->r_commit.c_commbase;
6545 6548 len = rp->r_commit.c_commlen;
6546 6549 mutex_enter(&rp->r_statelock);
6547 6550 rp->r_flags &= ~RCOMMIT;
6548 6551 cv_broadcast(&rp->r_commit.c_cv);
6549 6552 mutex_exit(&rp->r_statelock);
6550 6553
6551 6554 /*
6552 6555 * If any pages need to be committed, commit them and
6553 6556 * then unlock them so that they can be freed some
6554 6557 * time later.
6555 6558 */
6556 6559 if (plist != NULL) {
6557 6560 /*
6558 6561 * No error occurred during the flush portion
6559 6562 * of this operation, so now attempt to commit
6560 6563 * the data to stable storage on the server.
6561 6564 *
6562 6565 * This will unlock all of the pages on the list.
6563 6566 */
6564 6567 return (nfs3_sync_commit(vp, plist, offset, len, cr));
6565 6568 }
6566 6569 return (0);
6567 6570 }
6568 6571
6569 6572 static int
6570 6573 nfs3_sync_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count,
6571 6574 cred_t *cr)
6572 6575 {
6573 6576 int error;
6574 6577 page_t *pp;
6575 6578
6576 6579 ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
6577 6580 error = nfs3_commit(vp, offset, count, cr);
6578 6581
6579 6582 /*
6580 6583 * If we got an error, then just unlock all of the pages
6581 6584 * on the list.
6582 6585 */
6583 6586 if (error) {
6584 6587 while (plist != NULL) {
6585 6588 pp = plist;
6586 6589 page_sub(&plist, pp);
6587 6590 page_unlock(pp);
6588 6591 }
6589 6592 return (error);
6590 6593 }
6591 6594 /*
6592 6595 * We've tried as hard as we can to commit the data to stable
6593 6596 * storage on the server. We just unlock the pages and clear
6594 6597 * the commit required state. They will get freed later.
6595 6598 */
6596 6599 while (plist != NULL) {
6597 6600 pp = plist;
6598 6601 page_sub(&plist, pp);
6599 6602 pp->p_fsdata = C_NOCOMMIT;
6600 6603 page_unlock(pp);
6601 6604 }
6602 6605
6603 6606 return (error);
6604 6607 }
6605 6608
6606 6609 static void
6607 6610 nfs3_async_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count,
6608 6611 cred_t *cr)
6609 6612 {
6610 6613 ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
6611 6614 (void) nfs3_sync_commit(vp, plist, offset, count, cr);
6612 6615 }
6613 6616
6614 6617 /* ARGSUSED */
6615 6618 static int
6616 6619 nfs3_setsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr,
6617 6620 caller_context_t *ct)
6618 6621 {
6619 6622 int error;
6620 6623 mntinfo_t *mi;
6621 6624
6622 6625 mi = VTOMI(vp);
6623 6626
6624 6627 if (nfs_zone() != mi->mi_zone)
6625 6628 return (EIO);
6626 6629
6627 6630 if (mi->mi_flags & MI_ACL) {
6628 6631 error = acl_setacl3(vp, vsecattr, flag, cr);
6629 6632 if (mi->mi_flags & MI_ACL)
6630 6633 return (error);
6631 6634 }
6632 6635
6633 6636 return (ENOSYS);
6634 6637 }
6635 6638
6636 6639 /* ARGSUSED */
6637 6640 static int
6638 6641 nfs3_getsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr,
6639 6642 caller_context_t *ct)
6640 6643 {
6641 6644 int error;
6642 6645 mntinfo_t *mi;
6643 6646
6644 6647 mi = VTOMI(vp);
6645 6648
6646 6649 if (nfs_zone() != mi->mi_zone)
6647 6650 return (EIO);
6648 6651
6649 6652 if (mi->mi_flags & MI_ACL) {
6650 6653 error = acl_getacl3(vp, vsecattr, flag, cr);
6651 6654 if (mi->mi_flags & MI_ACL)
6652 6655 return (error);
6653 6656 }
6654 6657
6655 6658 return (fs_fab_acl(vp, vsecattr, flag, cr, ct));
6656 6659 }
6657 6660
6658 6661 /* ARGSUSED */
6659 6662 static int
6660 6663 nfs3_shrlock(vnode_t *vp, int cmd, struct shrlock *shr, int flag, cred_t *cr,
6661 6664 caller_context_t *ct)
6662 6665 {
6663 6666 int error;
6664 6667 struct shrlock nshr;
6665 6668 struct nfs_owner nfs_owner;
6666 6669 netobj lm_fh3;
6667 6670
6668 6671 if (nfs_zone() != VTOMI(vp)->mi_zone)
6669 6672 return (EIO);
6670 6673
6671 6674 /*
6672 6675 * check for valid cmd parameter
6673 6676 */
6674 6677 if (cmd != F_SHARE && cmd != F_UNSHARE && cmd != F_HASREMOTELOCKS)
6675 6678 return (EINVAL);
6676 6679
6677 6680 /*
6678 6681 * Check access permissions
6679 6682 */
6680 6683 if (cmd == F_SHARE &&
6681 6684 (((shr->s_access & F_RDACC) && !(flag & FREAD)) ||
6682 6685 ((shr->s_access & F_WRACC) && !(flag & FWRITE))))
6683 6686 return (EBADF);
6684 6687
6685 6688 /*
6686 6689 * If the filesystem is mounted using local locking, pass the
6687 6690 * request off to the local share code.
6688 6691 */
6689 6692 if (VTOMI(vp)->mi_flags & MI_LLOCK)
6690 6693 return (fs_shrlock(vp, cmd, shr, flag, cr, ct));
6691 6694
6692 6695 switch (cmd) {
6693 6696 case F_SHARE:
6694 6697 case F_UNSHARE:
6695 6698 lm_fh3.n_len = VTOFH3(vp)->fh3_length;
6696 6699 lm_fh3.n_bytes = (char *)&(VTOFH3(vp)->fh3_u.data);
6697 6700
6698 6701 /*
6699 6702 * If passed an owner that is too large to fit in an
6700 6703 * nfs_owner it is likely a recursive call from the
6701 6704 * lock manager client and pass it straight through. If
6702 6705 * it is not a nfs_owner then simply return an error.
6703 6706 */
6704 6707 if (shr->s_own_len > sizeof (nfs_owner.lowner)) {
6705 6708 if (((struct nfs_owner *)shr->s_owner)->magic !=
6706 6709 NFS_OWNER_MAGIC)
6707 6710 return (EINVAL);
6708 6711
6709 6712 if (error = lm4_shrlock(vp, cmd, shr, flag, &lm_fh3)) {
6710 6713 error = set_errno(error);
6711 6714 }
6712 6715 return (error);
6713 6716 }
6714 6717 /*
6715 6718 * Remote share reservations owner is a combination of
6716 6719 * a magic number, hostname, and the local owner
6717 6720 */
6718 6721 bzero(&nfs_owner, sizeof (nfs_owner));
6719 6722 nfs_owner.magic = NFS_OWNER_MAGIC;
6720 6723 (void) strncpy(nfs_owner.hname, uts_nodename(),
6721 6724 sizeof (nfs_owner.hname));
6722 6725 bcopy(shr->s_owner, nfs_owner.lowner, shr->s_own_len);
6723 6726 nshr.s_access = shr->s_access;
6724 6727 nshr.s_deny = shr->s_deny;
6725 6728 nshr.s_sysid = 0;
6726 6729 nshr.s_pid = ttoproc(curthread)->p_pid;
6727 6730 nshr.s_own_len = sizeof (nfs_owner);
6728 6731 nshr.s_owner = (caddr_t)&nfs_owner;
6729 6732
6730 6733 if (error = lm4_shrlock(vp, cmd, &nshr, flag, &lm_fh3)) {
6731 6734 error = set_errno(error);
6732 6735 }
6733 6736
6734 6737 break;
6735 6738
6736 6739 case F_HASREMOTELOCKS:
6737 6740 /*
6738 6741 * NFS client can't store remote locks itself
6739 6742 */
6740 6743 shr->s_access = 0;
6741 6744 error = 0;
6742 6745 break;
6743 6746
6744 6747 default:
6745 6748 error = EINVAL;
6746 6749 break;
6747 6750 }
6748 6751
6749 6752 return (error);
6750 6753 }
|
↓ open down ↓ |
1031 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX