su.static: link with proto area libs (esp. libc)
[unleashed.git] / kernel / fs / nfs / nfs_vnops.c
blob81640bd6089145007fb0ee960be7860b67ecef40
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 1983,1984,1985,1986,1987,1988,1989 AT&T.
25 * All rights reserved.
29 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
30 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
33 #include <sys/param.h>
34 #include <sys/types.h>
35 #include <sys/systm.h>
36 #include <sys/cred.h>
37 #include <sys/time.h>
38 #include <sys/vnode.h>
39 #include <sys/vfs.h>
40 #include <sys/file.h>
41 #include <sys/filio.h>
42 #include <sys/uio.h>
43 #include <sys/buf.h>
44 #include <sys/mman.h>
45 #include <sys/pathname.h>
46 #include <sys/dirent.h>
47 #include <sys/debug.h>
48 #include <sys/vmsystm.h>
49 #include <sys/fcntl.h>
50 #include <sys/flock.h>
51 #include <sys/swap.h>
52 #include <sys/errno.h>
53 #include <sys/strsubr.h>
54 #include <sys/sysmacros.h>
55 #include <sys/kmem.h>
56 #include <sys/cmn_err.h>
57 #include <sys/pathconf.h>
58 #include <sys/utsname.h>
59 #include <sys/dnlc.h>
60 #include <sys/acl.h>
61 #include <sys/atomic.h>
62 #include <sys/policy.h>
63 #include <sys/sdt.h>
65 #include <rpc/types.h>
66 #include <rpc/auth.h>
67 #include <rpc/clnt.h>
69 #include <nfs/nfs.h>
70 #include <nfs/nfs_clnt.h>
71 #include <nfs/rnode.h>
72 #include <nfs/nfs_acl.h>
73 #include <nfs/lm.h>
75 #include <vm/hat.h>
76 #include <vm/as.h>
77 #include <vm/page.h>
78 #include <vm/pvn.h>
79 #include <vm/seg.h>
80 #include <vm/seg_map.h>
81 #include <vm/seg_kpm.h>
82 #include <vm/seg_vn.h>
84 #include <sys/fs_subr.h>
86 #include <sys/ddi.h>
88 static int nfs_rdwrlbn(vnode_t *, page_t *, uoff_t, size_t, int,
89 cred_t *);
90 static int nfswrite(vnode_t *, caddr_t, uint_t, int, cred_t *);
91 static int nfsread(vnode_t *, caddr_t, uint_t, int, size_t *, cred_t *);
92 static int nfssetattr(vnode_t *, struct vattr *, int, cred_t *);
93 static int nfslookup_dnlc(vnode_t *, char *, vnode_t **, cred_t *);
94 static int nfslookup_otw(vnode_t *, char *, vnode_t **, cred_t *, int);
95 static int nfsrename(vnode_t *, char *, vnode_t *, char *, cred_t *,
96 caller_context_t *);
97 static int nfsreaddir(vnode_t *, rddir_cache *, cred_t *);
98 static int nfs_bio(struct buf *, cred_t *);
99 static int nfs_getapage(vnode_t *, uoff_t, size_t, uint_t *,
100 page_t *[], size_t, struct seg *, caddr_t,
101 enum seg_rw, cred_t *);
102 static void nfs_readahead(vnode_t *, uoff_t, caddr_t, struct seg *,
103 cred_t *);
104 static int nfs_sync_putapage(vnode_t *, page_t *, uoff_t, size_t,
105 int, cred_t *);
106 static int nfs_sync_pageio(vnode_t *, page_t *, uoff_t, size_t,
107 int, cred_t *);
108 static void nfs_delmap_callback(struct as *, void *, uint_t);
111 * Error flags used to pass information about certain special errors
112 * which need to be handled specially.
114 #define NFS_EOF -98
117 * These are the vnode ops routines which implement the vnode interface to
118 * the networked file system. These routines just take their parameters,
119 * make them look networkish by putting the right info into interface structs,
120 * and then calling the appropriate remote routine(s) to do the work.
122 * Note on directory name lookup cacheing: If we detect a stale fhandle,
123 * we purge the directory cache relative to that vnode. This way, the
124 * user won't get burned by the cache repeatedly. See <nfs/rnode.h> for
125 * more details on rnode locking.
128 static int nfs_open(vnode_t **, int, cred_t *, caller_context_t *);
129 static int nfs_close(vnode_t *, int, int, offset_t, cred_t *,
130 caller_context_t *);
131 static int nfs_read(vnode_t *, struct uio *, int, cred_t *,
132 caller_context_t *);
133 static int nfs_write(vnode_t *, struct uio *, int, cred_t *,
134 caller_context_t *);
135 static int nfs_ioctl(vnode_t *, int, intptr_t, int, cred_t *, int *,
136 caller_context_t *);
137 static int nfs_getattr(vnode_t *, struct vattr *, int, cred_t *,
138 caller_context_t *);
139 static int nfs_setattr(vnode_t *, struct vattr *, int, cred_t *,
140 caller_context_t *);
141 static int nfs_access(vnode_t *, int, int, cred_t *, caller_context_t *);
142 static int nfs_accessx(void *, int, cred_t *);
143 static int nfs_readlink(vnode_t *, struct uio *, cred_t *,
144 caller_context_t *);
145 static int nfs_fsync(vnode_t *, int, cred_t *, caller_context_t *);
146 static void nfs_inactive(vnode_t *, cred_t *, caller_context_t *);
147 static int nfs_lookup(vnode_t *, char *, vnode_t **, struct pathname *,
148 int, vnode_t *, cred_t *, caller_context_t *,
149 int *, pathname_t *);
150 static int nfs_create(vnode_t *, char *, struct vattr *, enum vcexcl,
151 int, vnode_t **, cred_t *, int, caller_context_t *,
152 vsecattr_t *);
153 static int nfs_remove(vnode_t *, char *, cred_t *, caller_context_t *,
154 int);
155 static int nfs_link(vnode_t *, vnode_t *, char *, cred_t *,
156 caller_context_t *, int);
157 static int nfs_rename(vnode_t *, char *, vnode_t *, char *, cred_t *,
158 caller_context_t *, int);
159 static int nfs_mkdir(vnode_t *, char *, struct vattr *, vnode_t **,
160 cred_t *, caller_context_t *, int, vsecattr_t *);
161 static int nfs_rmdir(vnode_t *, char *, vnode_t *, cred_t *,
162 caller_context_t *, int);
163 static int nfs_symlink(vnode_t *, char *, struct vattr *, char *,
164 cred_t *, caller_context_t *, int);
165 static int nfs_readdir(vnode_t *, struct uio *, cred_t *, int *,
166 caller_context_t *, int);
167 static int nfs_fid(vnode_t *, fid_t *, caller_context_t *);
168 static int nfs_rwlock(vnode_t *, int, caller_context_t *);
169 static void nfs_rwunlock(vnode_t *, int, caller_context_t *);
170 static int nfs_seek(vnode_t *, offset_t, offset_t *, caller_context_t *);
171 static int nfs_getpage(vnode_t *, offset_t, size_t, uint_t *,
172 page_t *[], size_t, struct seg *, caddr_t,
173 enum seg_rw, cred_t *, caller_context_t *);
174 static int nfs_putpage(vnode_t *, offset_t, size_t, int, cred_t *,
175 caller_context_t *);
176 static int nfs_map(vnode_t *, offset_t, struct as *, caddr_t *, size_t,
177 uchar_t, uchar_t, uint_t, cred_t *, caller_context_t *);
178 static int nfs_addmap(vnode_t *, offset_t, struct as *, caddr_t, size_t,
179 uchar_t, uchar_t, uint_t, cred_t *, caller_context_t *);
180 static int nfs_frlock(vnode_t *, int, struct flock64 *, int, offset_t,
181 struct flk_callback *, cred_t *, caller_context_t *);
182 static int nfs_space(vnode_t *, int, struct flock64 *, int, offset_t,
183 cred_t *, caller_context_t *);
184 static int nfs_realvp(vnode_t *, vnode_t **, caller_context_t *);
185 static int nfs_delmap(vnode_t *, offset_t, struct as *, caddr_t, size_t,
186 uint_t, uint_t, uint_t, cred_t *, caller_context_t *);
187 static int nfs_pathconf(vnode_t *, int, ulong_t *, cred_t *,
188 caller_context_t *);
189 static int nfs_pageio(vnode_t *, page_t *, uoff_t, size_t, int,
190 cred_t *, caller_context_t *);
191 static int nfs_setsecattr(vnode_t *, vsecattr_t *, int, cred_t *,
192 caller_context_t *);
193 static int nfs_getsecattr(vnode_t *, vsecattr_t *, int, cred_t *,
194 caller_context_t *);
195 static int nfs_shrlock(vnode_t *, int, struct shrlock *, int, cred_t *,
196 caller_context_t *);
198 const struct vnodeops nfs_vnodeops = {
199 .vnop_name = "nfs2",
200 .vop_open = nfs_open,
201 .vop_close = nfs_close,
202 .vop_read = nfs_read,
203 .vop_write = nfs_write,
204 .vop_ioctl = nfs_ioctl,
205 .vop_getattr = nfs_getattr,
206 .vop_setattr = nfs_setattr,
207 .vop_access = nfs_access,
208 .vop_lookup = nfs_lookup,
209 .vop_create = nfs_create,
210 .vop_remove = nfs_remove,
211 .vop_link = nfs_link,
212 .vop_rename = nfs_rename,
213 .vop_mkdir = nfs_mkdir,
214 .vop_rmdir = nfs_rmdir,
215 .vop_readdir = nfs_readdir,
216 .vop_symlink = nfs_symlink,
217 .vop_readlink = nfs_readlink,
218 .vop_fsync = nfs_fsync,
219 .vop_inactive = nfs_inactive,
220 .vop_fid = nfs_fid,
221 .vop_rwlock = nfs_rwlock,
222 .vop_rwunlock = nfs_rwunlock,
223 .vop_seek = nfs_seek,
224 .vop_frlock = nfs_frlock,
225 .vop_space = nfs_space,
226 .vop_realvp = nfs_realvp,
227 .vop_getpage = nfs_getpage,
228 .vop_putpage = nfs_putpage,
229 .vop_map = nfs_map,
230 .vop_addmap = nfs_addmap,
231 .vop_delmap = nfs_delmap,
232 .vop_dump = nfs_dump,
233 .vop_pathconf = nfs_pathconf,
234 .vop_pageio = nfs_pageio,
235 .vop_setsecattr = nfs_setsecattr,
236 .vop_getsecattr = nfs_getsecattr,
237 .vop_shrlock = nfs_shrlock,
238 .vop_vnevent = fs_vnevent_support,
242 * XXX: This is referenced in modstubs.s
244 const struct vnodeops *
245 nfs_getvnodeops(void)
247 return (&nfs_vnodeops);
250 /* ARGSUSED */
251 static int
252 nfs_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
254 int error;
255 struct vattr va;
256 rnode_t *rp;
257 vnode_t *vp;
259 vp = *vpp;
260 rp = VTOR(vp);
261 if (nfs_zone() != VTOMI(vp)->mi_zone)
262 return (EIO);
263 mutex_enter(&rp->r_statelock);
264 if (rp->r_cred == NULL) {
265 crhold(cr);
266 rp->r_cred = cr;
268 mutex_exit(&rp->r_statelock);
271 * If there is no cached data or if close-to-open
272 * consistency checking is turned off, we can avoid
273 * the over the wire getattr. Otherwise, if the
274 * file system is mounted readonly, then just verify
275 * the caches are up to date using the normal mechanism.
276 * Else, if the file is not mmap'd, then just mark
277 * the attributes as timed out. They will be refreshed
278 * and the caches validated prior to being used.
279 * Else, the file system is mounted writeable so
280 * force an over the wire GETATTR in order to ensure
281 * that all cached data is valid.
283 if (vp->v_count > 1 ||
284 ((vn_has_cached_data(vp) || HAVE_RDDIR_CACHE(rp)) &&
285 !(VTOMI(vp)->mi_flags & MI_NOCTO))) {
286 if (vn_is_readonly(vp))
287 error = nfs_validate_caches(vp, cr);
288 else if (rp->r_mapcnt == 0 && vp->v_count == 1) {
289 PURGE_ATTRCACHE(vp);
290 error = 0;
291 } else {
292 va.va_mask = VATTR_ALL;
293 error = nfs_getattr_otw(vp, &va, cr);
295 } else
296 error = 0;
298 return (error);
301 /* ARGSUSED */
302 static int
303 nfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr,
304 caller_context_t *ct)
306 rnode_t *rp;
307 int error;
308 struct vattr va;
311 * zone_enter(2) prevents processes from changing zones with NFS files
312 * open; if we happen to get here from the wrong zone we can't do
313 * anything over the wire.
315 if (VTOMI(vp)->mi_zone != nfs_zone()) {
317 * We could attempt to clean up locks, except we're sure
318 * that the current process didn't acquire any locks on
319 * the file: any attempt to lock a file belong to another zone
320 * will fail, and one can't lock an NFS file and then change
321 * zones, as that fails too.
323 * Returning an error here is the sane thing to do. A
324 * subsequent call to VN_RELE() which translates to a
325 * nfs_inactive() will clean up state: if the zone of the
326 * vnode's origin is still alive and kicking, an async worker
327 * thread will handle the request (from the correct zone), and
328 * everything (minus the final nfs_getattr_otw() call) should
329 * be OK. If the zone is going away nfs_async_inactive() will
330 * throw away cached pages inline.
332 return (EIO);
336 * If we are using local locking for this filesystem, then
337 * release all of the SYSV style record locks. Otherwise,
338 * we are doing network locking and we need to release all
339 * of the network locks. All of the locks held by this
340 * process on this file are released no matter what the
341 * incoming reference count is.
343 if (VTOMI(vp)->mi_flags & MI_LLOCK) {
344 cleanlocks(vp, ttoproc(curthread)->p_pid, 0);
345 cleanshares(vp, ttoproc(curthread)->p_pid);
346 } else
347 nfs_lockrelease(vp, flag, offset, cr);
349 if (count > 1)
350 return (0);
353 * If the file has been `unlinked', then purge the
354 * DNLC so that this vnode will get reycled quicker
355 * and the .nfs* file on the server will get removed.
357 rp = VTOR(vp);
358 if (rp->r_unldvp != NULL)
359 dnlc_purge_vp(vp);
362 * If the file was open for write and there are pages,
363 * then if the file system was mounted using the "no-close-
364 * to-open" semantics, then start an asynchronous flush
365 * of the all of the pages in the file.
366 * else the file system was not mounted using the "no-close-
367 * to-open" semantics, then do a synchronous flush and
368 * commit of all of the dirty and uncommitted pages.
370 * The asynchronous flush of the pages in the "nocto" path
371 * mostly just associates a cred pointer with the rnode so
372 * writes which happen later will have a better chance of
373 * working. It also starts the data being written to the
374 * server, but without unnecessarily delaying the application.
376 if ((flag & FWRITE) && vn_has_cached_data(vp)) {
377 if ((VTOMI(vp)->mi_flags & MI_NOCTO)) {
378 error = nfs_putpage(vp, 0, 0, B_ASYNC,
379 cr, ct);
380 if (error == EAGAIN)
381 error = 0;
382 } else
383 error = nfs_putpage(vp, 0, 0, 0, cr, ct);
384 if (!error) {
385 mutex_enter(&rp->r_statelock);
386 error = rp->r_error;
387 rp->r_error = 0;
388 mutex_exit(&rp->r_statelock);
390 } else {
391 mutex_enter(&rp->r_statelock);
392 error = rp->r_error;
393 rp->r_error = 0;
394 mutex_exit(&rp->r_statelock);
398 * If RWRITEATTR is set, then issue an over the wire GETATTR to
399 * refresh the attribute cache with a set of attributes which
400 * weren't returned from a WRITE. This will enable the close-
401 * to-open processing to work.
403 if (rp->r_flags & RWRITEATTR)
404 (void) nfs_getattr_otw(vp, &va, cr);
406 return (error);
409 /* ARGSUSED */
410 static int
411 nfs_read(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr,
412 caller_context_t *ct)
414 rnode_t *rp;
415 uoff_t off;
416 offset_t diff;
417 int on;
418 size_t n;
419 caddr_t base;
420 uint_t flags;
421 int error;
422 mntinfo_t *mi;
424 rp = VTOR(vp);
425 mi = VTOMI(vp);
427 if (nfs_zone() != mi->mi_zone)
428 return (EIO);
430 ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER));
432 if (vp->v_type != VREG)
433 return (EISDIR);
435 if (uiop->uio_resid == 0)
436 return (0);
438 if (uiop->uio_loffset > MAXOFF32_T)
439 return (EFBIG);
441 if (uiop->uio_loffset < 0 ||
442 uiop->uio_loffset + uiop->uio_resid > MAXOFF32_T)
443 return (EINVAL);
446 * Bypass VM if caching has been disabled (e.g., locking) or if
447 * using client-side direct I/O and the file is not mmap'd and
448 * there are no cached pages.
450 if ((vp->v_flag & VNOCACHE) ||
451 (((rp->r_flags & RDIRECTIO) || (mi->mi_flags & MI_DIRECTIO)) &&
452 rp->r_mapcnt == 0 && rp->r_inmap == 0 &&
453 !vn_has_cached_data(vp))) {
454 size_t bufsize;
455 size_t resid = 0;
458 * Let's try to do read in as large a chunk as we can
459 * (Filesystem (NFS client) bsize if possible/needed).
460 * For V3, this is 32K and for V2, this is 8K.
462 bufsize = MIN(uiop->uio_resid, VTOMI(vp)->mi_curread);
463 base = kmem_alloc(bufsize, KM_SLEEP);
464 do {
465 n = MIN(uiop->uio_resid, bufsize);
466 error = nfsread(vp, base, uiop->uio_offset, n,
467 &resid, cr);
468 if (!error) {
469 n -= resid;
470 error = uiomove(base, n, UIO_READ, uiop);
472 } while (!error && uiop->uio_resid > 0 && n > 0);
473 kmem_free(base, bufsize);
474 return (error);
477 error = 0;
479 do {
480 off = uiop->uio_loffset & MAXBMASK; /* mapping offset */
481 on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */
482 n = MIN(MAXBSIZE - on, uiop->uio_resid);
484 error = nfs_validate_caches(vp, cr);
485 if (error)
486 break;
488 mutex_enter(&rp->r_statelock);
489 while (rp->r_flags & RINCACHEPURGE) {
490 if (!cv_wait_sig(&rp->r_cv, &rp->r_statelock)) {
491 mutex_exit(&rp->r_statelock);
492 return (EINTR);
495 diff = rp->r_size - uiop->uio_loffset;
496 mutex_exit(&rp->r_statelock);
497 if (diff <= 0)
498 break;
499 if (diff < n)
500 n = (size_t)diff;
502 if (vpm_enable) {
504 * Copy data.
506 error = vpm_data_copy(vp, off + on, n, uiop,
507 1, NULL, 0, S_READ);
508 } else {
509 base = segmap_getmapflt(segkmap, vp, off + on, n,
510 1, S_READ);
511 error = uiomove(base + on, n, UIO_READ, uiop);
514 if (!error) {
516 * If read a whole block or read to eof,
517 * won't need this buffer again soon.
519 mutex_enter(&rp->r_statelock);
520 if (n + on == MAXBSIZE ||
521 uiop->uio_loffset == rp->r_size)
522 flags = SM_DONTNEED;
523 else
524 flags = 0;
525 mutex_exit(&rp->r_statelock);
526 if (vpm_enable) {
527 error = vpm_sync_pages(vp, off, n, flags);
528 } else {
529 error = segmap_release(segkmap, base, flags);
531 } else {
532 if (vpm_enable) {
533 (void) vpm_sync_pages(vp, off, n, 0);
534 } else {
535 (void) segmap_release(segkmap, base, 0);
538 } while (!error && uiop->uio_resid > 0);
540 return (error);
543 /* ARGSUSED */
544 static int
545 nfs_write(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr,
546 caller_context_t *ct)
548 rnode_t *rp;
549 uoff_t off;
550 caddr_t base;
551 uint_t flags;
552 int remainder;
553 size_t n;
554 int on;
555 int error;
556 int resid;
557 offset_t offset;
558 rlim_t limit;
559 mntinfo_t *mi;
561 rp = VTOR(vp);
563 mi = VTOMI(vp);
564 if (nfs_zone() != mi->mi_zone)
565 return (EIO);
566 if (vp->v_type != VREG)
567 return (EISDIR);
569 if (uiop->uio_resid == 0)
570 return (0);
572 if (ioflag & FAPPEND) {
573 struct vattr va;
576 * Must serialize if appending.
578 if (nfs_rw_lock_held(&rp->r_rwlock, RW_READER)) {
579 nfs_rw_exit(&rp->r_rwlock);
580 if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER,
581 INTR(vp)))
582 return (EINTR);
585 va.va_mask = VATTR_SIZE;
586 error = nfsgetattr(vp, &va, cr);
587 if (error)
588 return (error);
589 uiop->uio_loffset = va.va_size;
592 if (uiop->uio_loffset > MAXOFF32_T)
593 return (EFBIG);
595 offset = uiop->uio_loffset + uiop->uio_resid;
597 if (uiop->uio_loffset < 0 || offset > MAXOFF32_T)
598 return (EINVAL);
600 if (uiop->uio_llimit > (rlim64_t)MAXOFF32_T) {
601 limit = MAXOFF32_T;
602 } else {
603 limit = (rlim_t)uiop->uio_llimit;
607 * Check to make sure that the process will not exceed
608 * its limit on file size. It is okay to write up to
609 * the limit, but not beyond. Thus, the write which
610 * reaches the limit will be short and the next write
611 * will return an error.
613 remainder = 0;
614 if (offset > limit) {
615 remainder = offset - limit;
616 uiop->uio_resid = limit - uiop->uio_offset;
617 if (uiop->uio_resid <= 0) {
618 proc_t *p = ttoproc(curthread);
620 uiop->uio_resid += remainder;
621 mutex_enter(&p->p_lock);
622 (void) rctl_action(rctlproc_legacy[RLIMIT_FSIZE],
623 p->p_rctls, p, RCA_UNSAFE_SIGINFO);
624 mutex_exit(&p->p_lock);
625 return (EFBIG);
629 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR(vp)))
630 return (EINTR);
633 * Bypass VM if caching has been disabled (e.g., locking) or if
634 * using client-side direct I/O and the file is not mmap'd and
635 * there are no cached pages.
637 if ((vp->v_flag & VNOCACHE) ||
638 (((rp->r_flags & RDIRECTIO) || (mi->mi_flags & MI_DIRECTIO)) &&
639 rp->r_mapcnt == 0 && rp->r_inmap == 0 &&
640 !vn_has_cached_data(vp))) {
641 size_t bufsize;
642 int count;
643 uint_t org_offset;
645 nfs_fwrite:
646 if (rp->r_flags & RSTALE) {
647 resid = uiop->uio_resid;
648 offset = uiop->uio_loffset;
649 error = rp->r_error;
651 * A close may have cleared r_error, if so,
652 * propagate ESTALE error return properly
654 if (error == 0)
655 error = ESTALE;
656 goto bottom;
658 bufsize = MIN(uiop->uio_resid, mi->mi_curwrite);
659 base = kmem_alloc(bufsize, KM_SLEEP);
660 do {
661 resid = uiop->uio_resid;
662 offset = uiop->uio_loffset;
663 count = MIN(uiop->uio_resid, bufsize);
664 org_offset = uiop->uio_offset;
665 error = uiomove(base, count, UIO_WRITE, uiop);
666 if (!error) {
667 error = nfswrite(vp, base, org_offset,
668 count, cr);
670 } while (!error && uiop->uio_resid > 0);
671 kmem_free(base, bufsize);
672 goto bottom;
675 do {
676 off = uiop->uio_loffset & MAXBMASK; /* mapping offset */
677 on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */
678 n = MIN(MAXBSIZE - on, uiop->uio_resid);
680 resid = uiop->uio_resid;
681 offset = uiop->uio_loffset;
683 if (rp->r_flags & RSTALE) {
684 error = rp->r_error;
686 * A close may have cleared r_error, if so,
687 * propagate ESTALE error return properly
689 if (error == 0)
690 error = ESTALE;
691 break;
695 * Don't create dirty pages faster than they
696 * can be cleaned so that the system doesn't
697 * get imbalanced. If the async queue is
698 * maxed out, then wait for it to drain before
699 * creating more dirty pages. Also, wait for
700 * any threads doing pagewalks in the vop_getattr
701 * entry points so that they don't block for
702 * long periods.
704 mutex_enter(&rp->r_statelock);
705 while ((mi->mi_max_threads != 0 &&
706 rp->r_awcount > 2 * mi->mi_max_threads) ||
707 rp->r_gcount > 0) {
708 if (INTR(vp)) {
709 klwp_t *lwp = ttolwp(curthread);
711 if (lwp != NULL)
712 lwp->lwp_nostop++;
713 if (!cv_wait_sig(&rp->r_cv, &rp->r_statelock)) {
714 mutex_exit(&rp->r_statelock);
715 if (lwp != NULL)
716 lwp->lwp_nostop--;
717 error = EINTR;
718 goto bottom;
720 if (lwp != NULL)
721 lwp->lwp_nostop--;
722 } else
723 cv_wait(&rp->r_cv, &rp->r_statelock);
725 mutex_exit(&rp->r_statelock);
728 * Touch the page and fault it in if it is not in core
729 * before segmap_getmapflt or vpm_data_copy can lock it.
730 * This is to avoid the deadlock if the buffer is mapped
731 * to the same file through mmap which we want to write.
733 uio_prefaultpages((long)n, uiop);
735 if (vpm_enable) {
737 * It will use kpm mappings, so no need to
738 * pass an address.
740 error = writerp(rp, NULL, n, uiop, 0);
741 } else {
742 if (segmap_kpm) {
743 int pon = uiop->uio_loffset & PAGEOFFSET;
744 size_t pn = MIN(PAGESIZE - pon,
745 uiop->uio_resid);
746 int pagecreate;
748 mutex_enter(&rp->r_statelock);
749 pagecreate = (pon == 0) && (pn == PAGESIZE ||
750 uiop->uio_loffset + pn >= rp->r_size);
751 mutex_exit(&rp->r_statelock);
753 base = segmap_getmapflt(segkmap, vp, off + on,
754 pn, !pagecreate, S_WRITE);
756 error = writerp(rp, base + pon, n, uiop,
757 pagecreate);
759 } else {
760 base = segmap_getmapflt(segkmap, vp, off + on,
761 n, 0, S_READ);
762 error = writerp(rp, base + on, n, uiop, 0);
766 if (!error) {
767 if (mi->mi_flags & MI_NOAC)
768 flags = SM_WRITE;
769 else if (n + on == MAXBSIZE || IS_SWAPVP(vp)) {
771 * Have written a whole block.
772 * Start an asynchronous write
773 * and mark the buffer to
774 * indicate that it won't be
775 * needed again soon.
777 flags = SM_WRITE | SM_ASYNC | SM_DONTNEED;
778 } else
779 flags = 0;
780 if ((ioflag & (FSYNC|FDSYNC)) ||
781 (rp->r_flags & ROUTOFSPACE)) {
782 flags &= ~SM_ASYNC;
783 flags |= SM_WRITE;
785 if (vpm_enable) {
786 error = vpm_sync_pages(vp, off, n, flags);
787 } else {
788 error = segmap_release(segkmap, base, flags);
790 } else {
791 if (vpm_enable) {
792 (void) vpm_sync_pages(vp, off, n, 0);
793 } else {
794 (void) segmap_release(segkmap, base, 0);
797 * In the event that we got an access error while
798 * faulting in a page for a write-only file just
799 * force a write.
801 if (error == EACCES)
802 goto nfs_fwrite;
804 } while (!error && uiop->uio_resid > 0);
806 bottom:
807 if (error) {
808 uiop->uio_resid = resid + remainder;
809 uiop->uio_loffset = offset;
810 } else
811 uiop->uio_resid += remainder;
813 nfs_rw_exit(&rp->r_lkserlock);
815 return (error);
819 * Flags are composed of {B_ASYNC, B_INVAL, B_FREE, B_DONTNEED}
821 static int
822 nfs_rdwrlbn(vnode_t *vp, page_t *pp, uoff_t off, size_t len,
823 int flags, cred_t *cr)
825 struct buf *bp;
826 int error;
828 ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
829 bp = pageio_setup(pp, len, vp, flags);
830 ASSERT(bp != NULL);
833 * pageio_setup should have set b_addr to 0. This
834 * is correct since we want to do I/O on a page
835 * boundary. bp_mapin will use this addr to calculate
836 * an offset, and then set b_addr to the kernel virtual
837 * address it allocated for us.
839 ASSERT(bp->b_un.b_addr == 0);
841 bp->b_edev = 0;
842 bp->b_dev = 0;
843 bp->b_lblkno = lbtodb(off);
844 bp->b_file = vp;
845 bp->b_offset = (offset_t)off;
846 bp_mapin(bp);
848 error = nfs_bio(bp, cr);
850 bp_mapout(bp);
851 pageio_done(bp);
853 return (error);
857 * Write to file. Writes to remote server in largest size
858 * chunks that the server can handle. Write is synchronous.
860 static int
861 nfswrite(vnode_t *vp, caddr_t base, uint_t offset, int count, cred_t *cr)
863 rnode_t *rp;
864 mntinfo_t *mi;
865 struct nfswriteargs wa;
866 struct nfsattrstat ns;
867 int error;
868 int tsize;
869 int douprintf;
871 douprintf = 1;
873 rp = VTOR(vp);
874 mi = VTOMI(vp);
876 ASSERT(nfs_zone() == mi->mi_zone);
878 wa.wa_args = &wa.wa_args_buf;
879 wa.wa_fhandle = *VTOFH(vp);
881 do {
882 tsize = MIN(mi->mi_curwrite, count);
883 wa.wa_data = base;
884 wa.wa_begoff = offset;
885 wa.wa_totcount = tsize;
886 wa.wa_count = tsize;
887 wa.wa_offset = offset;
889 if (mi->mi_io_kstats) {
890 mutex_enter(&mi->mi_lock);
891 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
892 mutex_exit(&mi->mi_lock);
894 wa.wa_mblk = NULL;
895 do {
896 error = rfs2call(mi, RFS_WRITE,
897 xdr_writeargs, (caddr_t)&wa,
898 xdr_attrstat, (caddr_t)&ns, cr,
899 &douprintf, &ns.ns_status, 0, NULL);
900 } while (error == ENFS_TRYAGAIN);
901 if (mi->mi_io_kstats) {
902 mutex_enter(&mi->mi_lock);
903 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
904 mutex_exit(&mi->mi_lock);
907 if (!error) {
908 error = geterrno(ns.ns_status);
910 * Can't check for stale fhandle and purge caches
911 * here because pages are held by nfs_getpage.
912 * Just mark the attribute cache as timed out
913 * and set RWRITEATTR to indicate that the file
914 * was modified with a WRITE operation.
916 if (!error) {
917 count -= tsize;
918 base += tsize;
919 offset += tsize;
920 if (mi->mi_io_kstats) {
921 mutex_enter(&mi->mi_lock);
922 KSTAT_IO_PTR(mi->mi_io_kstats)->
923 writes++;
924 KSTAT_IO_PTR(mi->mi_io_kstats)->
925 nwritten += tsize;
926 mutex_exit(&mi->mi_lock);
928 lwp_stat_update(LWP_STAT_OUBLK, 1);
929 mutex_enter(&rp->r_statelock);
930 PURGE_ATTRCACHE_LOCKED(rp);
931 rp->r_flags |= RWRITEATTR;
932 mutex_exit(&rp->r_statelock);
935 } while (!error && count);
937 return (error);
941 * Read from a file. Reads data in largest chunks our interface can handle.
943 static int
944 nfsread(vnode_t *vp, caddr_t base, uint_t offset,
945 int count, size_t *residp, cred_t *cr)
947 mntinfo_t *mi;
948 struct nfsreadargs ra;
949 struct nfsrdresult rr;
950 int tsize;
951 int error;
952 int douprintf;
953 failinfo_t fi;
954 rnode_t *rp;
955 struct vattr va;
956 hrtime_t t;
958 rp = VTOR(vp);
959 mi = VTOMI(vp);
961 ASSERT(nfs_zone() == mi->mi_zone);
963 douprintf = 1;
965 ra.ra_fhandle = *VTOFH(vp);
967 fi.vp = vp;
968 fi.fhp = (caddr_t)&ra.ra_fhandle;
969 fi.copyproc = nfscopyfh;
970 fi.lookupproc = nfslookup;
971 fi.xattrdirproc = acl_getxattrdir2;
973 do {
974 if (mi->mi_io_kstats) {
975 mutex_enter(&mi->mi_lock);
976 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
977 mutex_exit(&mi->mi_lock);
980 do {
981 tsize = MIN(mi->mi_curread, count);
982 rr.rr_data = base;
983 ra.ra_offset = offset;
984 ra.ra_totcount = tsize;
985 ra.ra_count = tsize;
986 ra.ra_data = base;
987 t = gethrtime();
988 error = rfs2call(mi, RFS_READ,
989 xdr_readargs, (caddr_t)&ra,
990 xdr_rdresult, (caddr_t)&rr, cr,
991 &douprintf, &rr.rr_status, 0, &fi);
992 } while (error == ENFS_TRYAGAIN);
994 if (mi->mi_io_kstats) {
995 mutex_enter(&mi->mi_lock);
996 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
997 mutex_exit(&mi->mi_lock);
1000 if (!error) {
1001 error = geterrno(rr.rr_status);
1002 if (!error) {
1003 count -= rr.rr_count;
1004 base += rr.rr_count;
1005 offset += rr.rr_count;
1006 if (mi->mi_io_kstats) {
1007 mutex_enter(&mi->mi_lock);
1008 KSTAT_IO_PTR(mi->mi_io_kstats)->reads++;
1009 KSTAT_IO_PTR(mi->mi_io_kstats)->nread +=
1010 rr.rr_count;
1011 mutex_exit(&mi->mi_lock);
1013 lwp_stat_update(LWP_STAT_INBLK, 1);
1016 } while (!error && count && rr.rr_count == tsize);
1018 *residp = count;
1020 if (!error) {
1022 * Since no error occurred, we have the current
1023 * attributes and we need to do a cache check and then
1024 * potentially update the cached attributes. We can't
1025 * use the normal attribute check and cache mechanisms
1026 * because they might cause a cache flush which would
1027 * deadlock. Instead, we just check the cache to see
1028 * if the attributes have changed. If it is, then we
1029 * just mark the attributes as out of date. The next
1030 * time that the attributes are checked, they will be
1031 * out of date, new attributes will be fetched, and
1032 * the page cache will be flushed. If the attributes
1033 * weren't changed, then we just update the cached
1034 * attributes with these attributes.
1037 * If NFS_ACL is supported on the server, then the
1038 * attributes returned by server may have minimal
1039 * permissions sometimes denying access to users having
1040 * proper access. To get the proper attributes, mark
1041 * the attributes as expired so that they will be
1042 * regotten via the NFS_ACL GETATTR2 procedure.
1044 error = nattr_to_vattr(vp, &rr.rr_attr, &va);
1045 mutex_enter(&rp->r_statelock);
1046 if (error || !CACHE_VALID(rp, va.va_mtime, va.va_size) ||
1047 (mi->mi_flags & MI_ACL)) {
1048 mutex_exit(&rp->r_statelock);
1049 PURGE_ATTRCACHE(vp);
1050 } else {
1051 if (rp->r_mtime <= t) {
1052 nfs_attrcache_va(vp, &va);
1054 mutex_exit(&rp->r_statelock);
1058 return (error);
1061 /* ARGSUSED */
1062 static int
1063 nfs_ioctl(vnode_t *vp, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp,
1064 caller_context_t *ct)
1067 if (nfs_zone() != VTOMI(vp)->mi_zone)
1068 return (EIO);
1069 switch (cmd) {
1070 case _FIODIRECTIO:
1071 return (nfs_directio(vp, (int)arg, cr));
1072 default:
1073 return (ENOTTY);
1077 /* ARGSUSED */
1078 static int
1079 nfs_getattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr,
1080 caller_context_t *ct)
1082 int error;
1083 rnode_t *rp;
1085 if (nfs_zone() != VTOMI(vp)->mi_zone)
1086 return (EIO);
1088 * If it has been specified that the return value will
1089 * just be used as a hint, and we are only being asked
1090 * for size, fsid or rdevid, then return the client's
1091 * notion of these values without checking to make sure
1092 * that the attribute cache is up to date.
1093 * The whole point is to avoid an over the wire GETATTR
1094 * call.
1096 rp = VTOR(vp);
1097 if ((flags & ATTR_HINT) &&
1098 ((vap->va_mask & ~(VATTR_SIZE | VATTR_FSID | VATTR_RDEV)) == 0)) {
1099 mutex_enter(&rp->r_statelock);
1100 if (vap->va_mask & VATTR_SIZE)
1101 vap->va_size = rp->r_size;
1102 if (vap->va_mask & VATTR_FSID)
1103 vap->va_fsid = rp->r_attr.va_fsid;
1104 if (vap->va_mask & VATTR_RDEV)
1105 vap->va_rdev = rp->r_attr.va_rdev;
1106 mutex_exit(&rp->r_statelock);
1107 return (0);
1111 * Only need to flush pages if asking for the mtime
1112 * and if there any dirty pages or any outstanding
1113 * asynchronous (write) requests for this file.
1115 if (vap->va_mask & VATTR_MTIME) {
1116 if (vn_has_cached_data(vp) &&
1117 ((rp->r_flags & RDIRTY) || rp->r_awcount > 0)) {
1118 mutex_enter(&rp->r_statelock);
1119 rp->r_gcount++;
1120 mutex_exit(&rp->r_statelock);
1121 error = nfs_putpage(vp, 0, 0, 0, cr, ct);
1122 mutex_enter(&rp->r_statelock);
1123 if (error && (error == ENOSPC || error == EDQUOT)) {
1124 if (!rp->r_error)
1125 rp->r_error = error;
1127 if (--rp->r_gcount == 0)
1128 cv_broadcast(&rp->r_cv);
1129 mutex_exit(&rp->r_statelock);
1133 return (nfsgetattr(vp, vap, cr));
1136 /*ARGSUSED4*/
1137 static int
1138 nfs_setattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr,
1139 caller_context_t *ct)
1141 int error;
1142 uint_t mask;
1143 struct vattr va;
1145 mask = vap->va_mask;
1147 if (mask & VATTR_NOSET)
1148 return (EINVAL);
1150 if ((mask & VATTR_SIZE) &&
1151 vap->va_type == VREG &&
1152 vap->va_size > MAXOFF32_T)
1153 return (EFBIG);
1155 if (nfs_zone() != VTOMI(vp)->mi_zone)
1156 return (EIO);
1158 va.va_mask = VATTR_UID | VATTR_MODE;
1160 error = nfsgetattr(vp, &va, cr);
1161 if (error)
1162 return (error);
1164 error = secpolicy_vnode_setattr(cr, vp, vap, &va, flags, nfs_accessx,
1165 vp);
1167 if (error)
1168 return (error);
1170 error = nfssetattr(vp, vap, flags, cr);
1172 if (error == 0 && (mask & VATTR_SIZE) && vap->va_size == 0)
1173 vnevent_truncate(vp, ct);
1175 return (error);
1178 static int
1179 nfssetattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr)
1181 int error;
1182 uint_t mask;
1183 struct nfssaargs args;
1184 struct nfsattrstat ns;
1185 int douprintf;
1186 rnode_t *rp;
1187 struct vattr va;
1188 mode_t omode;
1189 mntinfo_t *mi;
1190 vsecattr_t *vsp;
1191 hrtime_t t;
1193 mask = vap->va_mask;
1195 ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
1197 rp = VTOR(vp);
1200 * Only need to flush pages if there are any pages and
1201 * if the file is marked as dirty in some fashion. The
1202 * file must be flushed so that we can accurately
1203 * determine the size of the file and the cached data
1204 * after the SETATTR returns. A file is considered to
1205 * be dirty if it is either marked with RDIRTY, has
1206 * outstanding i/o's active, or is mmap'd. In this
1207 * last case, we can't tell whether there are dirty
1208 * pages, so we flush just to be sure.
1210 if (vn_has_cached_data(vp) &&
1211 ((rp->r_flags & RDIRTY) ||
1212 rp->r_count > 0 ||
1213 rp->r_mapcnt > 0)) {
1214 ASSERT(vp->v_type != VCHR);
1215 error = nfs_putpage(vp, 0, 0, 0, cr, NULL);
1216 if (error && (error == ENOSPC || error == EDQUOT)) {
1217 mutex_enter(&rp->r_statelock);
1218 if (!rp->r_error)
1219 rp->r_error = error;
1220 mutex_exit(&rp->r_statelock);
1225 * If the system call was utime(2) or utimes(2) and the
1226 * application did not specify the times, then set the
1227 * mtime nanosecond field to 1 billion. This will get
1228 * translated from 1 billion nanoseconds to 1 million
1229 * microseconds in the over the wire request. The
1230 * server will use 1 million in the microsecond field
1231 * to tell whether both the mtime and atime should be
1232 * set to the server's current time.
1234 * This is an overload of the protocol and should be
1235 * documented in the NFS Version 2 protocol specification.
1237 if ((mask & VATTR_MTIME) && !(flags & ATTR_UTIME)) {
1238 vap->va_mtime.tv_nsec = 1000000000;
1239 if (NFS_TIME_T_OK(vap->va_mtime.tv_sec) &&
1240 NFS_TIME_T_OK(vap->va_atime.tv_sec)) {
1241 error = vattr_to_sattr(vap, &args.saa_sa);
1242 } else {
1244 * Use server times. vap time values will not be used.
1245 * To ensure no time overflow, make sure vap has
1246 * valid values, but retain the original values.
1248 timestruc_t mtime = vap->va_mtime;
1249 timestruc_t atime = vap->va_atime;
1250 time_t now;
1252 now = gethrestime_sec();
1253 if (NFS_TIME_T_OK(now)) {
1254 /* Just in case server does not know of this */
1255 vap->va_mtime.tv_sec = now;
1256 vap->va_atime.tv_sec = now;
1257 } else {
1258 vap->va_mtime.tv_sec = 0;
1259 vap->va_atime.tv_sec = 0;
1261 error = vattr_to_sattr(vap, &args.saa_sa);
1262 /* set vap times back on */
1263 vap->va_mtime = mtime;
1264 vap->va_atime = atime;
1266 } else {
1267 /* Either do not set times or use the client specified times */
1268 error = vattr_to_sattr(vap, &args.saa_sa);
1270 if (error) {
1271 /* req time field(s) overflow - return immediately */
1272 return (error);
1274 args.saa_fh = *VTOFH(vp);
1276 va.va_mask = VATTR_MODE;
1277 error = nfsgetattr(vp, &va, cr);
1278 if (error)
1279 return (error);
1280 omode = va.va_mode;
1282 mi = VTOMI(vp);
1284 douprintf = 1;
1286 t = gethrtime();
1288 error = rfs2call(mi, RFS_SETATTR,
1289 xdr_saargs, (caddr_t)&args,
1290 xdr_attrstat, (caddr_t)&ns, cr,
1291 &douprintf, &ns.ns_status, 0, NULL);
1294 * Purge the access cache and ACL cache if changing either the
1295 * owner of the file, the group owner, or the mode. These may
1296 * change the access permissions of the file, so purge old
1297 * information and start over again.
1299 if ((mask & (VATTR_UID | VATTR_GID | VATTR_MODE)) && (mi->mi_flags & MI_ACL)) {
1300 (void) nfs_access_purge_rp(rp);
1301 if (rp->r_secattr != NULL) {
1302 mutex_enter(&rp->r_statelock);
1303 vsp = rp->r_secattr;
1304 rp->r_secattr = NULL;
1305 mutex_exit(&rp->r_statelock);
1306 if (vsp != NULL)
1307 nfs_acl_free(vsp);
1311 if (!error) {
1312 error = geterrno(ns.ns_status);
1313 if (!error) {
1315 * If changing the size of the file, invalidate
1316 * any local cached data which is no longer part
1317 * of the file. We also possibly invalidate the
1318 * last page in the file. We could use
1319 * pvn_vpzero(), but this would mark the page as
1320 * modified and require it to be written back to
1321 * the server for no particularly good reason.
1322 * This way, if we access it, then we bring it
1323 * back in. A read should be cheaper than a
1324 * write.
1326 if (mask & VATTR_SIZE) {
1327 nfs_invalidate_pages(vp,
1328 (vap->va_size & PAGEMASK), cr);
1330 (void) nfs_cache_fattr(vp, &ns.ns_attr, &va, t, cr);
1332 * If NFS_ACL is supported on the server, then the
1333 * attributes returned by server may have minimal
1334 * permissions sometimes denying access to users having
1335 * proper access. To get the proper attributes, mark
1336 * the attributes as expired so that they will be
1337 * regotten via the NFS_ACL GETATTR2 procedure.
1339 if (mi->mi_flags & MI_ACL) {
1340 PURGE_ATTRCACHE(vp);
1343 * This next check attempts to deal with NFS
1344 * servers which can not handle increasing
1345 * the size of the file via setattr. Most
1346 * of these servers do not return an error,
1347 * but do not change the size of the file.
1348 * Hence, this check and then attempt to set
1349 * the file size by writing 1 byte at the
1350 * offset of the end of the file that we need.
1352 if ((mask & VATTR_SIZE) &&
1353 ns.ns_attr.na_size < (uint32_t)vap->va_size) {
1354 char zb = '\0';
1356 error = nfswrite(vp, &zb,
1357 vap->va_size - sizeof (zb),
1358 sizeof (zb), cr);
1361 * Some servers will change the mode to clear the setuid
1362 * and setgid bits when changing the uid or gid. The
1363 * client needs to compensate appropriately.
1365 if (mask & (VATTR_UID | VATTR_GID)) {
1366 int terror;
1368 va.va_mask = VATTR_MODE;
1369 terror = nfsgetattr(vp, &va, cr);
1370 if (!terror &&
1371 (((mask & VATTR_MODE) &&
1372 va.va_mode != vap->va_mode) ||
1373 (!(mask & VATTR_MODE) &&
1374 va.va_mode != omode))) {
1375 va.va_mask = VATTR_MODE;
1376 if (mask & VATTR_MODE)
1377 va.va_mode = vap->va_mode;
1378 else
1379 va.va_mode = omode;
1380 (void) nfssetattr(vp, &va, 0, cr);
1383 } else {
1384 PURGE_ATTRCACHE(vp);
1385 PURGE_STALE_FH(error, vp, cr);
1387 } else {
1388 PURGE_ATTRCACHE(vp);
1391 return (error);
1394 static int
1395 nfs_accessx(void *vp, int mode, cred_t *cr)
1397 ASSERT(nfs_zone() == VTOMI((vnode_t *)vp)->mi_zone);
1398 return (nfs_access(vp, mode, 0, cr, NULL));
1401 /* ARGSUSED */
1402 static int
1403 nfs_access(vnode_t *vp, int mode, int flags, cred_t *cr, caller_context_t *ct)
1405 struct vattr va;
1406 int error;
1407 mntinfo_t *mi;
1408 int shift = 0;
1410 mi = VTOMI(vp);
1412 if (nfs_zone() != mi->mi_zone)
1413 return (EIO);
1414 if (mi->mi_flags & MI_ACL) {
1415 error = acl_access2(vp, mode, flags, cr);
1416 if (mi->mi_flags & MI_ACL)
1417 return (error);
1420 va.va_mask = VATTR_MODE | VATTR_UID | VATTR_GID;
1421 error = nfsgetattr(vp, &va, cr);
1422 if (error)
1423 return (error);
1426 * Disallow write attempts on read-only
1427 * file systems, unless the file is a
1428 * device node.
1430 if ((mode & VWRITE) && vn_is_readonly(vp) && !IS_DEVVP(vp))
1431 return (EROFS);
1434 * Disallow attempts to access mandatory lock files.
1436 if ((mode & (VWRITE | VREAD | VEXEC)) &&
1437 MANDLOCK(vp, va.va_mode))
1438 return (EACCES);
1441 * Access check is based on only
1442 * one of owner, group, public.
1443 * If not owner, then check group.
1444 * If not a member of the group,
1445 * then check public access.
1447 if (crgetuid(cr) != va.va_uid) {
1448 shift += 3;
1449 if (!groupmember(va.va_gid, cr))
1450 shift += 3;
1453 return (secpolicy_vnode_access2(cr, vp, va.va_uid,
1454 va.va_mode << shift, mode));
1457 static int nfs_do_symlink_cache = 1;
1459 /* ARGSUSED */
1460 static int
1461 nfs_readlink(vnode_t *vp, struct uio *uiop, cred_t *cr, caller_context_t *ct)
1463 int error;
1464 struct nfsrdlnres rl;
1465 rnode_t *rp;
1466 int douprintf;
1467 failinfo_t fi;
1470 * We want to be consistent with UFS semantics so we will return
1471 * EINVAL instead of ENXIO. This violates the XNFS spec and
1472 * the RFC 1094, which are wrong any way. BUGID 1138002.
1474 if (vp->v_type != VLNK)
1475 return (EINVAL);
1477 if (nfs_zone() != VTOMI(vp)->mi_zone)
1478 return (EIO);
1480 rp = VTOR(vp);
1481 if (nfs_do_symlink_cache && rp->r_symlink.contents != NULL) {
1482 error = nfs_validate_caches(vp, cr);
1483 if (error)
1484 return (error);
1485 mutex_enter(&rp->r_statelock);
1486 if (rp->r_symlink.contents != NULL) {
1487 error = uiomove(rp->r_symlink.contents,
1488 rp->r_symlink.len, UIO_READ, uiop);
1489 mutex_exit(&rp->r_statelock);
1490 return (error);
1492 mutex_exit(&rp->r_statelock);
1496 rl.rl_data = kmem_alloc(NFS_MAXPATHLEN, KM_SLEEP);
1498 fi.vp = vp;
1499 fi.fhp = NULL; /* no need to update, filehandle not copied */
1500 fi.copyproc = nfscopyfh;
1501 fi.lookupproc = nfslookup;
1502 fi.xattrdirproc = acl_getxattrdir2;
1504 douprintf = 1;
1506 error = rfs2call(VTOMI(vp), RFS_READLINK,
1507 xdr_readlink, (caddr_t)VTOFH(vp),
1508 xdr_rdlnres, (caddr_t)&rl, cr,
1509 &douprintf, &rl.rl_status, 0, &fi);
1511 if (error) {
1513 kmem_free((void *)rl.rl_data, NFS_MAXPATHLEN);
1514 return (error);
1517 error = geterrno(rl.rl_status);
1518 if (!error) {
1519 error = uiomove(rl.rl_data, (int)rl.rl_count, UIO_READ, uiop);
1520 if (nfs_do_symlink_cache && rp->r_symlink.contents == NULL) {
1521 mutex_enter(&rp->r_statelock);
1522 if (rp->r_symlink.contents == NULL) {
1523 rp->r_symlink.contents = rl.rl_data;
1524 rp->r_symlink.len = (int)rl.rl_count;
1525 rp->r_symlink.size = NFS_MAXPATHLEN;
1526 mutex_exit(&rp->r_statelock);
1527 } else {
1528 mutex_exit(&rp->r_statelock);
1530 kmem_free((void *)rl.rl_data,
1531 NFS_MAXPATHLEN);
1533 } else {
1535 kmem_free((void *)rl.rl_data, NFS_MAXPATHLEN);
1537 } else {
1538 PURGE_STALE_FH(error, vp, cr);
1540 kmem_free((void *)rl.rl_data, NFS_MAXPATHLEN);
1544 * Conform to UFS semantics (see comment above)
1546 return (error == ENXIO ? EINVAL : error);
1550 * Flush local dirty pages to stable storage on the server.
1552 * If FNODSYNC is specified, then there is nothing to do because
1553 * metadata changes are not cached on the client before being
1554 * sent to the server.
1556 /* ARGSUSED */
1557 static int
1558 nfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
1560 int error;
1562 if ((syncflag & FNODSYNC) || IS_SWAPVP(vp))
1563 return (0);
1565 if (nfs_zone() != VTOMI(vp)->mi_zone)
1566 return (EIO);
1568 error = nfs_putpage(vp, 0, 0, 0, cr, ct);
1569 if (!error)
1570 error = VTOR(vp)->r_error;
1571 return (error);
1576 * Weirdness: if the file was removed or the target of a rename
1577 * operation while it was open, it got renamed instead. Here we
1578 * remove the renamed file.
1580 /* ARGSUSED */
1581 static void
1582 nfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
1584 rnode_t *rp;
1586 ASSERT(vp != DNLC_NO_VNODE);
1589 * If this is coming from the wrong zone, we let someone in the right
1590 * zone take care of it asynchronously. We can get here due to
1591 * VN_RELE() being called from pageout() or fsflush(). This call may
1592 * potentially turn into an expensive no-op if, for instance, v_count
1593 * gets incremented in the meantime, but it's still correct.
1595 if (nfs_zone() != VTOMI(vp)->mi_zone) {
1596 nfs_async_inactive(vp, cr, nfs_inactive);
1597 return;
1600 rp = VTOR(vp);
1601 redo:
1602 if (rp->r_unldvp != NULL) {
1604 * Save the vnode pointer for the directory where the
1605 * unlinked-open file got renamed, then set it to NULL
1606 * to prevent another thread from getting here before
1607 * we're done with the remove. While we have the
1608 * statelock, make local copies of the pertinent rnode
1609 * fields. If we weren't to do this in an atomic way, the
1610 * the unl* fields could become inconsistent with respect
1611 * to each other due to a race condition between this
1612 * code and nfs_remove(). See bug report 1034328.
1614 mutex_enter(&rp->r_statelock);
1615 if (rp->r_unldvp != NULL) {
1616 vnode_t *unldvp;
1617 char *unlname;
1618 cred_t *unlcred;
1619 struct nfsdiropargs da;
1620 enum nfsstat status;
1621 int douprintf;
1622 int error;
1624 unldvp = rp->r_unldvp;
1625 rp->r_unldvp = NULL;
1626 unlname = rp->r_unlname;
1627 rp->r_unlname = NULL;
1628 unlcred = rp->r_unlcred;
1629 rp->r_unlcred = NULL;
1630 mutex_exit(&rp->r_statelock);
1633 * If there are any dirty pages left, then flush
1634 * them. This is unfortunate because they just
1635 * may get thrown away during the remove operation,
1636 * but we have to do this for correctness.
1638 if (vn_has_cached_data(vp) &&
1639 ((rp->r_flags & RDIRTY) || rp->r_count > 0)) {
1640 ASSERT(vp->v_type != VCHR);
1641 error = nfs_putpage(vp, 0, 0, 0,
1642 cr, ct);
1643 if (error) {
1644 mutex_enter(&rp->r_statelock);
1645 if (!rp->r_error)
1646 rp->r_error = error;
1647 mutex_exit(&rp->r_statelock);
1652 * Do the remove operation on the renamed file
1654 setdiropargs(&da, unlname, unldvp);
1656 douprintf = 1;
1658 (void) rfs2call(VTOMI(unldvp), RFS_REMOVE,
1659 xdr_diropargs, (caddr_t)&da,
1660 xdr_enum, (caddr_t)&status, unlcred,
1661 &douprintf, &status, 0, NULL);
1663 if (HAVE_RDDIR_CACHE(VTOR(unldvp)))
1664 nfs_purge_rddir_cache(unldvp);
1665 PURGE_ATTRCACHE(unldvp);
1668 * Release stuff held for the remove
1670 VN_RELE(unldvp);
1671 kmem_free(unlname, MAXNAMELEN);
1672 crfree(unlcred);
1673 goto redo;
1675 mutex_exit(&rp->r_statelock);
1678 rp_addfree(rp, cr);
1682 * Remote file system operations having to do with directory manipulation.
1685 /* ARGSUSED */
1686 static int
1687 nfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
1688 int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
1689 int *direntflags, pathname_t *realpnp)
1691 int error;
1692 vnode_t *vp;
1693 vnode_t *avp = NULL;
1694 rnode_t *drp;
1696 if (nfs_zone() != VTOMI(dvp)->mi_zone)
1697 return (EPERM);
1699 drp = VTOR(dvp);
1702 * Are we looking up extended attributes? If so, "dvp" is
1703 * the file or directory for which we want attributes, and
1704 * we need a lookup of the hidden attribute directory
1705 * before we lookup the rest of the path.
1707 if (flags & LOOKUP_XATTR) {
1708 bool_t cflag = ((flags & CREATE_XATTR_DIR) != 0);
1709 mntinfo_t *mi;
1711 mi = VTOMI(dvp);
1712 if (!(mi->mi_flags & MI_EXTATTR))
1713 return (EINVAL);
1715 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR(dvp)))
1716 return (EINTR);
1718 (void) nfslookup_dnlc(dvp, XATTR_DIR_NAME, &avp, cr);
1719 if (avp == NULL)
1720 error = acl_getxattrdir2(dvp, &avp, cflag, cr, 0);
1721 else
1722 error = 0;
1724 nfs_rw_exit(&drp->r_rwlock);
1726 if (error) {
1727 if (mi->mi_flags & MI_EXTATTR)
1728 return (error);
1729 return (EINVAL);
1731 dvp = avp;
1732 drp = VTOR(dvp);
1735 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR(dvp))) {
1736 error = EINTR;
1737 goto out;
1740 error = nfslookup(dvp, nm, vpp, pnp, flags, rdir, cr, 0);
1742 nfs_rw_exit(&drp->r_rwlock);
1745 * If vnode is a device, create special vnode.
1747 if (!error && IS_DEVVP(*vpp)) {
1748 vp = *vpp;
1749 *vpp = specvp(vp, vp->v_rdev, vp->v_type, cr);
1750 VN_RELE(vp);
1753 out:
1754 if (avp != NULL)
1755 VN_RELE(avp);
1757 return (error);
1760 static int nfs_lookup_neg_cache = 1;
1762 #ifdef DEBUG
1763 static int nfs_lookup_dnlc_hits = 0;
1764 static int nfs_lookup_dnlc_misses = 0;
1765 static int nfs_lookup_dnlc_neg_hits = 0;
1766 static int nfs_lookup_dnlc_disappears = 0;
1767 static int nfs_lookup_dnlc_lookups = 0;
1768 #endif
1770 /* ARGSUSED */
1772 nfslookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
1773 int flags, vnode_t *rdir, cred_t *cr, int rfscall_flags)
1775 int error;
1777 ASSERT(nfs_zone() == VTOMI(dvp)->mi_zone);
1780 * If lookup is for "", just return dvp. Don't need
1781 * to send it over the wire, look it up in the dnlc,
1782 * or perform any access checks.
1784 if (*nm == '\0') {
1785 VN_HOLD(dvp);
1786 *vpp = dvp;
1787 return (0);
1791 * Can't do lookups in non-directories.
1793 if (dvp->v_type != VDIR)
1794 return (ENOTDIR);
1797 * If we're called with RFSCALL_SOFT, it's important that
1798 * the only rfscall is one we make directly; if we permit
1799 * an access call because we're looking up "." or validating
1800 * a dnlc hit, we'll deadlock because that rfscall will not
1801 * have the RFSCALL_SOFT set.
1803 if (rfscall_flags & RFSCALL_SOFT)
1804 goto callit;
1807 * If lookup is for ".", just return dvp. Don't need
1808 * to send it over the wire or look it up in the dnlc,
1809 * just need to check access.
1811 if (strcmp(nm, ".") == 0) {
1812 error = nfs_access(dvp, VEXEC, 0, cr, NULL);
1813 if (error)
1814 return (error);
1815 VN_HOLD(dvp);
1816 *vpp = dvp;
1817 return (0);
1821 * Lookup this name in the DNLC. If there was a valid entry,
1822 * then return the results of the lookup.
1824 error = nfslookup_dnlc(dvp, nm, vpp, cr);
1825 if (error || *vpp != NULL)
1826 return (error);
1828 callit:
1829 error = nfslookup_otw(dvp, nm, vpp, cr, rfscall_flags);
1831 return (error);
1834 static int
1835 nfslookup_dnlc(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr)
1837 int error;
1838 vnode_t *vp;
1840 ASSERT(*nm != '\0');
1841 ASSERT(nfs_zone() == VTOMI(dvp)->mi_zone);
1844 * Lookup this name in the DNLC. If successful, then validate
1845 * the caches and then recheck the DNLC. The DNLC is rechecked
1846 * just in case this entry got invalidated during the call
1847 * to nfs_validate_caches.
1849 * An assumption is being made that it is safe to say that a
1850 * file exists which may not on the server. Any operations to
1851 * the server will fail with ESTALE.
1853 #ifdef DEBUG
1854 nfs_lookup_dnlc_lookups++;
1855 #endif
1856 vp = dnlc_lookup(dvp, nm);
1857 if (vp != NULL) {
1858 VN_RELE(vp);
1859 if (vp == DNLC_NO_VNODE && !vn_is_readonly(dvp)) {
1860 PURGE_ATTRCACHE(dvp);
1862 error = nfs_validate_caches(dvp, cr);
1863 if (error)
1864 return (error);
1865 vp = dnlc_lookup(dvp, nm);
1866 if (vp != NULL) {
1867 error = nfs_access(dvp, VEXEC, 0, cr, NULL);
1868 if (error) {
1869 VN_RELE(vp);
1870 return (error);
1872 if (vp == DNLC_NO_VNODE) {
1873 VN_RELE(vp);
1874 #ifdef DEBUG
1875 nfs_lookup_dnlc_neg_hits++;
1876 #endif
1877 return (ENOENT);
1879 *vpp = vp;
1880 #ifdef DEBUG
1881 nfs_lookup_dnlc_hits++;
1882 #endif
1883 return (0);
1885 #ifdef DEBUG
1886 nfs_lookup_dnlc_disappears++;
1887 #endif
1889 #ifdef DEBUG
1890 else
1891 nfs_lookup_dnlc_misses++;
1892 #endif
1894 *vpp = NULL;
1896 return (0);
1899 static int
1900 nfslookup_otw(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr,
1901 int rfscall_flags)
1903 int error;
1904 struct nfsdiropargs da;
1905 struct nfsdiropres dr;
1906 int douprintf;
1907 failinfo_t fi;
1908 hrtime_t t;
1910 ASSERT(*nm != '\0');
1911 ASSERT(dvp->v_type == VDIR);
1912 ASSERT(nfs_zone() == VTOMI(dvp)->mi_zone);
1914 setdiropargs(&da, nm, dvp);
1916 fi.vp = dvp;
1917 fi.fhp = NULL; /* no need to update, filehandle not copied */
1918 fi.copyproc = nfscopyfh;
1919 fi.lookupproc = nfslookup;
1920 fi.xattrdirproc = acl_getxattrdir2;
1922 douprintf = 1;
1924 t = gethrtime();
1926 error = rfs2call(VTOMI(dvp), RFS_LOOKUP,
1927 xdr_diropargs, (caddr_t)&da,
1928 xdr_diropres, (caddr_t)&dr, cr,
1929 &douprintf, &dr.dr_status, rfscall_flags, &fi);
1931 if (!error) {
1932 error = geterrno(dr.dr_status);
1933 if (!error) {
1934 *vpp = makenfsnode(&dr.dr_fhandle, &dr.dr_attr,
1935 dvp->v_vfsp, t, cr, VTOR(dvp)->r_path, nm);
1937 * If NFS_ACL is supported on the server, then the
1938 * attributes returned by server may have minimal
1939 * permissions sometimes denying access to users having
1940 * proper access. To get the proper attributes, mark
1941 * the attributes as expired so that they will be
1942 * regotten via the NFS_ACL GETATTR2 procedure.
1944 if (VTOMI(*vpp)->mi_flags & MI_ACL) {
1945 PURGE_ATTRCACHE(*vpp);
1947 if (!(rfscall_flags & RFSCALL_SOFT))
1948 dnlc_update(dvp, nm, *vpp);
1949 } else {
1950 PURGE_STALE_FH(error, dvp, cr);
1951 if (error == ENOENT && nfs_lookup_neg_cache)
1952 dnlc_enter(dvp, nm, DNLC_NO_VNODE);
1956 return (error);
1959 /* ARGSUSED */
1960 static int
1961 nfs_create(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive,
1962 int mode, vnode_t **vpp, cred_t *cr, int lfaware, caller_context_t *ct,
1963 vsecattr_t *vsecp)
1965 int error;
1966 struct nfscreatargs args;
1967 struct nfsdiropres dr;
1968 int douprintf;
1969 vnode_t *vp;
1970 rnode_t *rp;
1971 struct vattr vattr;
1972 rnode_t *drp;
1973 vnode_t *tempvp;
1974 hrtime_t t;
1976 drp = VTOR(dvp);
1978 if (nfs_zone() != VTOMI(dvp)->mi_zone)
1979 return (EPERM);
1980 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR(dvp)))
1981 return (EINTR);
1984 * We make a copy of the attributes because the caller does not
1985 * expect us to change what va points to.
1987 vattr = *va;
1990 * If the pathname is "", just use dvp. Don't need
1991 * to send it over the wire, look it up in the dnlc,
1992 * or perform any access checks.
1994 if (*nm == '\0') {
1995 error = 0;
1996 VN_HOLD(dvp);
1997 vp = dvp;
1999 * If the pathname is ".", just use dvp. Don't need
2000 * to send it over the wire or look it up in the dnlc,
2001 * just need to check access.
2003 } else if (strcmp(nm, ".") == 0) {
2004 error = nfs_access(dvp, VEXEC, 0, cr, ct);
2005 if (error) {
2006 nfs_rw_exit(&drp->r_rwlock);
2007 return (error);
2009 VN_HOLD(dvp);
2010 vp = dvp;
2012 * We need to go over the wire, just to be sure whether the
2013 * file exists or not. Using the DNLC can be dangerous in
2014 * this case when making a decision regarding existence.
2016 } else {
2017 error = nfslookup_otw(dvp, nm, &vp, cr, 0);
2019 if (!error) {
2020 if (exclusive == EXCL)
2021 error = EEXIST;
2022 else if (vp->v_type == VDIR && (mode & VWRITE))
2023 error = EISDIR;
2024 else {
2026 * If vnode is a device, create special vnode.
2028 if (IS_DEVVP(vp)) {
2029 tempvp = vp;
2030 vp = specvp(vp, vp->v_rdev, vp->v_type, cr);
2031 VN_RELE(tempvp);
2033 if (!(error = fop_access(vp, mode, 0, cr, ct))) {
2034 if ((vattr.va_mask & VATTR_SIZE) &&
2035 vp->v_type == VREG) {
2036 vattr.va_mask = VATTR_SIZE;
2037 error = nfssetattr(vp, &vattr, 0, cr);
2039 if (!error) {
2041 * Existing file was truncated;
2042 * emit a create event.
2044 vnevent_create(vp, ct);
2049 nfs_rw_exit(&drp->r_rwlock);
2050 if (error) {
2051 VN_RELE(vp);
2052 } else {
2053 *vpp = vp;
2055 return (error);
2058 ASSERT(vattr.va_mask & VATTR_TYPE);
2059 if (vattr.va_type == VREG) {
2060 ASSERT(vattr.va_mask & VATTR_MODE);
2061 if (MANDMODE(vattr.va_mode)) {
2062 nfs_rw_exit(&drp->r_rwlock);
2063 return (EACCES);
2067 dnlc_remove(dvp, nm);
2069 setdiropargs(&args.ca_da, nm, dvp);
2072 * Decide what the group-id of the created file should be.
2073 * Set it in attribute list as advisory...then do a setattr
2074 * if the server didn't get it right the first time.
2076 error = setdirgid(dvp, &vattr.va_gid, cr);
2077 if (error) {
2078 nfs_rw_exit(&drp->r_rwlock);
2079 return (error);
2081 vattr.va_mask |= VATTR_GID;
2084 * This is a completely gross hack to make mknod
2085 * work over the wire until we can wack the protocol
2087 #define IFCHR 0020000 /* character special */
2088 #define IFBLK 0060000 /* block special */
2089 #define IFSOCK 0140000 /* socket */
2092 * dev_t is uint_t in 5.x and short in 4.x. Both 4.x
2093 * supports 8 bit majors. 5.x supports 14 bit majors. 5.x supports 18
2094 * bits in the minor number where 4.x supports 8 bits. If the 5.x
2095 * minor/major numbers <= 8 bits long, compress the device
2096 * number before sending it. Otherwise, the 4.x server will not
2097 * create the device with the correct device number and nothing can be
2098 * done about this.
2100 if (vattr.va_type == VCHR || vattr.va_type == VBLK) {
2101 dev_t d = vattr.va_rdev;
2102 dev32_t dev32;
2104 if (vattr.va_type == VCHR)
2105 vattr.va_mode |= IFCHR;
2106 else
2107 vattr.va_mode |= IFBLK;
2109 (void) cmpldev(&dev32, d);
2110 if (dev32 & ~((SO4_MAXMAJ << L_BITSMINOR32) | SO4_MAXMIN))
2111 vattr.va_size = (uoff_t)dev32;
2112 else
2113 vattr.va_size = (uoff_t)nfsv2_cmpdev(d);
2115 vattr.va_mask |= VATTR_MODE|VATTR_SIZE;
2116 } else if (vattr.va_type == VFIFO) {
2117 vattr.va_mode |= IFCHR; /* xtra kludge for namedpipe */
2118 vattr.va_size = (uoff_t)NFS_FIFO_DEV; /* blech */
2119 vattr.va_mask |= VATTR_MODE|VATTR_SIZE;
2120 } else if (vattr.va_type == VSOCK) {
2121 vattr.va_mode |= IFSOCK;
2123 * To avoid triggering bugs in the servers set VATTR_SIZE
2124 * (all other RFS_CREATE calls set this).
2126 vattr.va_size = 0;
2127 vattr.va_mask |= VATTR_MODE|VATTR_SIZE;
2130 args.ca_sa = &args.ca_sa_buf;
2131 error = vattr_to_sattr(&vattr, args.ca_sa);
2132 if (error) {
2133 /* req time field(s) overflow - return immediately */
2134 nfs_rw_exit(&drp->r_rwlock);
2135 return (error);
2138 douprintf = 1;
2140 t = gethrtime();
2142 error = rfs2call(VTOMI(dvp), RFS_CREATE,
2143 xdr_creatargs, (caddr_t)&args,
2144 xdr_diropres, (caddr_t)&dr, cr,
2145 &douprintf, &dr.dr_status, 0, NULL);
2147 PURGE_ATTRCACHE(dvp); /* mod time changed */
2149 if (!error) {
2150 error = geterrno(dr.dr_status);
2151 if (!error) {
2152 if (HAVE_RDDIR_CACHE(drp))
2153 nfs_purge_rddir_cache(dvp);
2154 vp = makenfsnode(&dr.dr_fhandle, &dr.dr_attr,
2155 dvp->v_vfsp, t, cr, NULL, NULL);
2157 * If NFS_ACL is supported on the server, then the
2158 * attributes returned by server may have minimal
2159 * permissions sometimes denying access to users having
2160 * proper access. To get the proper attributes, mark
2161 * the attributes as expired so that they will be
2162 * regotten via the NFS_ACL GETATTR2 procedure.
2164 if (VTOMI(vp)->mi_flags & MI_ACL) {
2165 PURGE_ATTRCACHE(vp);
2167 dnlc_update(dvp, nm, vp);
2168 rp = VTOR(vp);
2169 if (vattr.va_size == 0) {
2170 mutex_enter(&rp->r_statelock);
2171 rp->r_size = 0;
2172 mutex_exit(&rp->r_statelock);
2173 if (vn_has_cached_data(vp)) {
2174 ASSERT(vp->v_type != VCHR);
2175 nfs_invalidate_pages(vp,
2176 0, cr);
2181 * Make sure the gid was set correctly.
2182 * If not, try to set it (but don't lose
2183 * any sleep over it).
2185 if (vattr.va_gid != rp->r_attr.va_gid) {
2186 vattr.va_mask = VATTR_GID;
2187 (void) nfssetattr(vp, &vattr, 0, cr);
2191 * If vnode is a device create special vnode
2193 if (IS_DEVVP(vp)) {
2194 *vpp = specvp(vp, vp->v_rdev, vp->v_type, cr);
2195 VN_RELE(vp);
2196 } else
2197 *vpp = vp;
2198 } else {
2199 PURGE_STALE_FH(error, dvp, cr);
2203 nfs_rw_exit(&drp->r_rwlock);
2205 return (error);
2209 * Weirdness: if the vnode to be removed is open
2210 * we rename it instead of removing it and nfs_inactive
2211 * will remove the new name.
2213 /* ARGSUSED */
2214 static int
2215 nfs_remove(vnode_t *dvp, char *nm, cred_t *cr, caller_context_t *ct, int flags)
2217 int error;
2218 struct nfsdiropargs da;
2219 enum nfsstat status;
2220 vnode_t *vp;
2221 char *tmpname;
2222 int douprintf;
2223 rnode_t *rp;
2224 rnode_t *drp;
2226 if (nfs_zone() != VTOMI(dvp)->mi_zone)
2227 return (EPERM);
2228 drp = VTOR(dvp);
2229 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR(dvp)))
2230 return (EINTR);
2232 error = nfslookup(dvp, nm, &vp, NULL, 0, NULL, cr, 0);
2233 if (error) {
2234 nfs_rw_exit(&drp->r_rwlock);
2235 return (error);
2238 if (vp->v_type == VDIR && secpolicy_fs_linkdir(cr, dvp->v_vfsp)) {
2239 VN_RELE(vp);
2240 nfs_rw_exit(&drp->r_rwlock);
2241 return (EPERM);
2245 * First just remove the entry from the name cache, as it
2246 * is most likely the only entry for this vp.
2248 dnlc_remove(dvp, nm);
2251 * If the file has a v_count > 1 then there may be more than one
2252 * entry in the name cache due multiple links or an open file,
2253 * but we don't have the real reference count so flush all
2254 * possible entries.
2256 if (vp->v_count > 1)
2257 dnlc_purge_vp(vp);
2260 * Now we have the real reference count on the vnode
2262 rp = VTOR(vp);
2263 mutex_enter(&rp->r_statelock);
2264 if (vp->v_count > 1 &&
2265 (rp->r_unldvp == NULL || strcmp(nm, rp->r_unlname) == 0)) {
2266 mutex_exit(&rp->r_statelock);
2267 tmpname = newname();
2268 error = nfsrename(dvp, nm, dvp, tmpname, cr, ct);
2269 if (error)
2270 kmem_free(tmpname, MAXNAMELEN);
2271 else {
2272 mutex_enter(&rp->r_statelock);
2273 if (rp->r_unldvp == NULL) {
2274 VN_HOLD(dvp);
2275 rp->r_unldvp = dvp;
2276 if (rp->r_unlcred != NULL)
2277 crfree(rp->r_unlcred);
2278 crhold(cr);
2279 rp->r_unlcred = cr;
2280 rp->r_unlname = tmpname;
2281 } else {
2282 kmem_free(rp->r_unlname, MAXNAMELEN);
2283 rp->r_unlname = tmpname;
2285 mutex_exit(&rp->r_statelock);
2287 } else {
2288 mutex_exit(&rp->r_statelock);
2290 * We need to flush any dirty pages which happen to
2291 * be hanging around before removing the file. This
2292 * shouldn't happen very often and mostly on file
2293 * systems mounted "nocto".
2295 if (vn_has_cached_data(vp) &&
2296 ((rp->r_flags & RDIRTY) || rp->r_count > 0)) {
2297 error = nfs_putpage(vp, 0, 0, 0, cr, ct);
2298 if (error && (error == ENOSPC || error == EDQUOT)) {
2299 mutex_enter(&rp->r_statelock);
2300 if (!rp->r_error)
2301 rp->r_error = error;
2302 mutex_exit(&rp->r_statelock);
2306 setdiropargs(&da, nm, dvp);
2308 douprintf = 1;
2310 error = rfs2call(VTOMI(dvp), RFS_REMOVE,
2311 xdr_diropargs, (caddr_t)&da,
2312 xdr_enum, (caddr_t)&status, cr,
2313 &douprintf, &status, 0, NULL);
2316 * The xattr dir may be gone after last attr is removed,
2317 * so flush it from dnlc.
2319 if (dvp->v_flag & V_XATTRDIR)
2320 dnlc_purge_vp(dvp);
2322 PURGE_ATTRCACHE(dvp); /* mod time changed */
2323 PURGE_ATTRCACHE(vp); /* link count changed */
2325 if (!error) {
2326 error = geterrno(status);
2327 if (!error) {
2328 if (HAVE_RDDIR_CACHE(drp))
2329 nfs_purge_rddir_cache(dvp);
2330 } else {
2331 PURGE_STALE_FH(error, dvp, cr);
2336 if (error == 0) {
2337 vnevent_remove(vp, dvp, nm, ct);
2339 VN_RELE(vp);
2341 nfs_rw_exit(&drp->r_rwlock);
2343 return (error);
2346 /* ARGSUSED */
2347 static int
2348 nfs_link(vnode_t *tdvp, vnode_t *svp, char *tnm, cred_t *cr,
2349 caller_context_t *ct, int flags)
2351 int error;
2352 struct nfslinkargs args;
2353 enum nfsstat status;
2354 vnode_t *realvp;
2355 int douprintf;
2356 rnode_t *tdrp;
2358 if (nfs_zone() != VTOMI(tdvp)->mi_zone)
2359 return (EPERM);
2360 if (fop_realvp(svp, &realvp, ct) == 0)
2361 svp = realvp;
2363 args.la_from = VTOFH(svp);
2364 setdiropargs(&args.la_to, tnm, tdvp);
2366 tdrp = VTOR(tdvp);
2367 if (nfs_rw_enter_sig(&tdrp->r_rwlock, RW_WRITER, INTR(tdvp)))
2368 return (EINTR);
2370 dnlc_remove(tdvp, tnm);
2372 douprintf = 1;
2374 error = rfs2call(VTOMI(svp), RFS_LINK,
2375 xdr_linkargs, (caddr_t)&args,
2376 xdr_enum, (caddr_t)&status, cr,
2377 &douprintf, &status, 0, NULL);
2379 PURGE_ATTRCACHE(tdvp); /* mod time changed */
2380 PURGE_ATTRCACHE(svp); /* link count changed */
2382 if (!error) {
2383 error = geterrno(status);
2384 if (!error) {
2385 if (HAVE_RDDIR_CACHE(tdrp))
2386 nfs_purge_rddir_cache(tdvp);
2390 nfs_rw_exit(&tdrp->r_rwlock);
2392 if (!error) {
2394 * Notify the source file of this link operation.
2396 vnevent_link(svp, ct);
2398 return (error);
2401 /* ARGSUSED */
2402 static int
2403 nfs_rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr,
2404 caller_context_t *ct, int flags)
2406 vnode_t *realvp;
2408 if (nfs_zone() != VTOMI(odvp)->mi_zone)
2409 return (EPERM);
2410 if (fop_realvp(ndvp, &realvp, ct) == 0)
2411 ndvp = realvp;
2413 return (nfsrename(odvp, onm, ndvp, nnm, cr, ct));
2417 * nfsrename does the real work of renaming in NFS Version 2.
2419 static int
2420 nfsrename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr,
2421 caller_context_t *ct)
2423 int error;
2424 enum nfsstat status;
2425 struct nfsrnmargs args;
2426 int douprintf;
2427 vnode_t *nvp = NULL;
2428 vnode_t *ovp = NULL;
2429 char *tmpname;
2430 rnode_t *rp;
2431 rnode_t *odrp;
2432 rnode_t *ndrp;
2434 ASSERT(nfs_zone() == VTOMI(odvp)->mi_zone);
2435 if (strcmp(onm, ".") == 0 || strcmp(onm, "..") == 0 ||
2436 strcmp(nnm, ".") == 0 || strcmp(nnm, "..") == 0)
2437 return (EINVAL);
2439 odrp = VTOR(odvp);
2440 ndrp = VTOR(ndvp);
2441 if ((intptr_t)odrp < (intptr_t)ndrp) {
2442 if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR(odvp)))
2443 return (EINTR);
2444 if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR(ndvp))) {
2445 nfs_rw_exit(&odrp->r_rwlock);
2446 return (EINTR);
2448 } else {
2449 if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR(ndvp)))
2450 return (EINTR);
2451 if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR(odvp))) {
2452 nfs_rw_exit(&ndrp->r_rwlock);
2453 return (EINTR);
2458 * Lookup the target file. If it exists, it needs to be
2459 * checked to see whether it is a mount point and whether
2460 * it is active (open).
2462 error = nfslookup(ndvp, nnm, &nvp, NULL, 0, NULL, cr, 0);
2463 if (!error) {
2465 * If this file has been mounted on, then just
2466 * return busy because renaming to it would remove
2467 * the mounted file system from the name space.
2469 if (vn_mountedvfs(nvp) != NULL) {
2470 VN_RELE(nvp);
2471 nfs_rw_exit(&odrp->r_rwlock);
2472 nfs_rw_exit(&ndrp->r_rwlock);
2473 return (EBUSY);
2477 * Purge the name cache of all references to this vnode
2478 * so that we can check the reference count to infer
2479 * whether it is active or not.
2482 * First just remove the entry from the name cache, as it
2483 * is most likely the only entry for this vp.
2485 dnlc_remove(ndvp, nnm);
2487 * If the file has a v_count > 1 then there may be more
2488 * than one entry in the name cache due multiple links
2489 * or an open file, but we don't have the real reference
2490 * count so flush all possible entries.
2492 if (nvp->v_count > 1)
2493 dnlc_purge_vp(nvp);
2496 * If the vnode is active and is not a directory,
2497 * arrange to rename it to a
2498 * temporary file so that it will continue to be
2499 * accessible. This implements the "unlink-open-file"
2500 * semantics for the target of a rename operation.
2501 * Before doing this though, make sure that the
2502 * source and target files are not already the same.
2504 if (nvp->v_count > 1 && nvp->v_type != VDIR) {
2506 * Lookup the source name.
2508 error = nfslookup(odvp, onm, &ovp, NULL, 0, NULL,
2509 cr, 0);
2512 * The source name *should* already exist.
2514 if (error) {
2515 VN_RELE(nvp);
2516 nfs_rw_exit(&odrp->r_rwlock);
2517 nfs_rw_exit(&ndrp->r_rwlock);
2518 return (error);
2522 * Compare the two vnodes. If they are the same,
2523 * just release all held vnodes and return success.
2525 if (ovp == nvp) {
2526 VN_RELE(ovp);
2527 VN_RELE(nvp);
2528 nfs_rw_exit(&odrp->r_rwlock);
2529 nfs_rw_exit(&ndrp->r_rwlock);
2530 return (0);
2534 * Can't mix and match directories and non-
2535 * directories in rename operations. We already
2536 * know that the target is not a directory. If
2537 * the source is a directory, return an error.
2539 if (ovp->v_type == VDIR) {
2540 VN_RELE(ovp);
2541 VN_RELE(nvp);
2542 nfs_rw_exit(&odrp->r_rwlock);
2543 nfs_rw_exit(&ndrp->r_rwlock);
2544 return (ENOTDIR);
2548 * The target file exists, is not the same as
2549 * the source file, and is active. Link it
2550 * to a temporary filename to avoid having
2551 * the server removing the file completely.
2553 tmpname = newname();
2554 error = nfs_link(ndvp, nvp, tmpname, cr, NULL, 0);
2555 if (error == EOPNOTSUPP) {
2556 error = nfs_rename(ndvp, nnm, ndvp, tmpname,
2557 cr, NULL, 0);
2559 if (error) {
2560 kmem_free(tmpname, MAXNAMELEN);
2561 VN_RELE(ovp);
2562 VN_RELE(nvp);
2563 nfs_rw_exit(&odrp->r_rwlock);
2564 nfs_rw_exit(&ndrp->r_rwlock);
2565 return (error);
2567 rp = VTOR(nvp);
2568 mutex_enter(&rp->r_statelock);
2569 if (rp->r_unldvp == NULL) {
2570 VN_HOLD(ndvp);
2571 rp->r_unldvp = ndvp;
2572 if (rp->r_unlcred != NULL)
2573 crfree(rp->r_unlcred);
2574 crhold(cr);
2575 rp->r_unlcred = cr;
2576 rp->r_unlname = tmpname;
2577 } else {
2578 kmem_free(rp->r_unlname, MAXNAMELEN);
2579 rp->r_unlname = tmpname;
2581 mutex_exit(&rp->r_statelock);
2585 if (ovp == NULL) {
2587 * When renaming directories to be a subdirectory of a
2588 * different parent, the dnlc entry for ".." will no
2589 * longer be valid, so it must be removed.
2591 * We do a lookup here to determine whether we are renaming
2592 * a directory and we need to check if we are renaming
2593 * an unlinked file. This might have already been done
2594 * in previous code, so we check ovp == NULL to avoid
2595 * doing it twice.
2598 error = nfslookup(odvp, onm, &ovp, NULL, 0, NULL, cr, 0);
2601 * The source name *should* already exist.
2603 if (error) {
2604 nfs_rw_exit(&odrp->r_rwlock);
2605 nfs_rw_exit(&ndrp->r_rwlock);
2606 if (nvp) {
2607 VN_RELE(nvp);
2609 return (error);
2611 ASSERT(ovp != NULL);
2614 dnlc_remove(odvp, onm);
2615 dnlc_remove(ndvp, nnm);
2617 setdiropargs(&args.rna_from, onm, odvp);
2618 setdiropargs(&args.rna_to, nnm, ndvp);
2620 douprintf = 1;
2622 error = rfs2call(VTOMI(odvp), RFS_RENAME,
2623 xdr_rnmargs, (caddr_t)&args,
2624 xdr_enum, (caddr_t)&status, cr,
2625 &douprintf, &status, 0, NULL);
2627 PURGE_ATTRCACHE(odvp); /* mod time changed */
2628 PURGE_ATTRCACHE(ndvp); /* mod time changed */
2630 if (!error) {
2631 error = geterrno(status);
2632 if (!error) {
2633 if (HAVE_RDDIR_CACHE(odrp))
2634 nfs_purge_rddir_cache(odvp);
2635 if (HAVE_RDDIR_CACHE(ndrp))
2636 nfs_purge_rddir_cache(ndvp);
2638 * when renaming directories to be a subdirectory of a
2639 * different parent, the dnlc entry for ".." will no
2640 * longer be valid, so it must be removed
2642 rp = VTOR(ovp);
2643 if (ndvp != odvp) {
2644 if (ovp->v_type == VDIR) {
2645 dnlc_remove(ovp, "..");
2646 if (HAVE_RDDIR_CACHE(rp))
2647 nfs_purge_rddir_cache(ovp);
2652 * If we are renaming the unlinked file, update the
2653 * r_unldvp and r_unlname as needed.
2655 mutex_enter(&rp->r_statelock);
2656 if (rp->r_unldvp != NULL) {
2657 if (strcmp(rp->r_unlname, onm) == 0) {
2658 (void) strncpy(rp->r_unlname,
2659 nnm, MAXNAMELEN);
2660 rp->r_unlname[MAXNAMELEN - 1] = '\0';
2662 if (ndvp != rp->r_unldvp) {
2663 VN_RELE(rp->r_unldvp);
2664 rp->r_unldvp = ndvp;
2665 VN_HOLD(ndvp);
2669 mutex_exit(&rp->r_statelock);
2670 } else {
2672 * System V defines rename to return EEXIST, not
2673 * ENOTEMPTY if the target directory is not empty.
2674 * Over the wire, the error is NFSERR_ENOTEMPTY
2675 * which geterrno maps to ENOTEMPTY.
2677 if (error == ENOTEMPTY)
2678 error = EEXIST;
2682 if (error == 0) {
2683 if (nvp)
2684 vnevent_rename_dest(nvp, ndvp, nnm, ct);
2686 if (odvp != ndvp)
2687 vnevent_rename_dest_dir(ndvp, ct);
2689 ASSERT(ovp != NULL);
2690 vnevent_rename_src(ovp, odvp, onm, ct);
2693 if (nvp) {
2694 VN_RELE(nvp);
2696 VN_RELE(ovp);
2698 nfs_rw_exit(&odrp->r_rwlock);
2699 nfs_rw_exit(&ndrp->r_rwlock);
2701 return (error);
2704 /* ARGSUSED */
2705 static int
2706 nfs_mkdir(vnode_t *dvp, char *nm, struct vattr *va, vnode_t **vpp, cred_t *cr,
2707 caller_context_t *ct, int flags, vsecattr_t *vsecp)
2709 int error;
2710 struct nfscreatargs args;
2711 struct nfsdiropres dr;
2712 int douprintf;
2713 rnode_t *drp;
2714 hrtime_t t;
2716 if (nfs_zone() != VTOMI(dvp)->mi_zone)
2717 return (EPERM);
2719 setdiropargs(&args.ca_da, nm, dvp);
2722 * Decide what the group-id and set-gid bit of the created directory
2723 * should be. May have to do a setattr to get the gid right.
2725 error = setdirgid(dvp, &va->va_gid, cr);
2726 if (error)
2727 return (error);
2728 error = setdirmode(dvp, &va->va_mode, cr);
2729 if (error)
2730 return (error);
2731 va->va_mask |= VATTR_MODE|VATTR_GID;
2733 args.ca_sa = &args.ca_sa_buf;
2734 error = vattr_to_sattr(va, args.ca_sa);
2735 if (error) {
2736 /* req time field(s) overflow - return immediately */
2737 return (error);
2740 drp = VTOR(dvp);
2741 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR(dvp)))
2742 return (EINTR);
2744 dnlc_remove(dvp, nm);
2746 douprintf = 1;
2748 t = gethrtime();
2750 error = rfs2call(VTOMI(dvp), RFS_MKDIR,
2751 xdr_creatargs, (caddr_t)&args,
2752 xdr_diropres, (caddr_t)&dr, cr,
2753 &douprintf, &dr.dr_status, 0, NULL);
2755 PURGE_ATTRCACHE(dvp); /* mod time changed */
2757 if (!error) {
2758 error = geterrno(dr.dr_status);
2759 if (!error) {
2760 if (HAVE_RDDIR_CACHE(drp))
2761 nfs_purge_rddir_cache(dvp);
2763 * The attributes returned by RFS_MKDIR can not
2764 * be depended upon, so mark the attribute cache
2765 * as purged. A subsequent GETATTR will get the
2766 * correct attributes from the server.
2768 *vpp = makenfsnode(&dr.dr_fhandle, &dr.dr_attr,
2769 dvp->v_vfsp, t, cr, NULL, NULL);
2770 PURGE_ATTRCACHE(*vpp);
2771 dnlc_update(dvp, nm, *vpp);
2774 * Make sure the gid was set correctly.
2775 * If not, try to set it (but don't lose
2776 * any sleep over it).
2778 if (va->va_gid != VTOR(*vpp)->r_attr.va_gid) {
2779 va->va_mask = VATTR_GID;
2780 (void) nfssetattr(*vpp, va, 0, cr);
2782 } else {
2783 PURGE_STALE_FH(error, dvp, cr);
2787 nfs_rw_exit(&drp->r_rwlock);
2789 return (error);
2792 /* ARGSUSED */
2793 static int
2794 nfs_rmdir(vnode_t *dvp, char *nm, vnode_t *cdir, cred_t *cr,
2795 caller_context_t *ct, int flags)
2797 int error;
2798 enum nfsstat status;
2799 struct nfsdiropargs da;
2800 vnode_t *vp;
2801 int douprintf;
2802 rnode_t *drp;
2804 if (nfs_zone() != VTOMI(dvp)->mi_zone)
2805 return (EPERM);
2806 drp = VTOR(dvp);
2807 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR(dvp)))
2808 return (EINTR);
2811 * Attempt to prevent a rmdir(".") from succeeding.
2813 error = nfslookup(dvp, nm, &vp, NULL, 0, NULL, cr, 0);
2814 if (error) {
2815 nfs_rw_exit(&drp->r_rwlock);
2816 return (error);
2819 if (vp == cdir) {
2820 VN_RELE(vp);
2821 nfs_rw_exit(&drp->r_rwlock);
2822 return (EINVAL);
2825 setdiropargs(&da, nm, dvp);
2828 * First just remove the entry from the name cache, as it
2829 * is most likely an entry for this vp.
2831 dnlc_remove(dvp, nm);
2834 * If there vnode reference count is greater than one, then
2835 * there may be additional references in the DNLC which will
2836 * need to be purged. First, trying removing the entry for
2837 * the parent directory and see if that removes the additional
2838 * reference(s). If that doesn't do it, then use dnlc_purge_vp
2839 * to completely remove any references to the directory which
2840 * might still exist in the DNLC.
2842 if (vp->v_count > 1) {
2843 dnlc_remove(vp, "..");
2844 if (vp->v_count > 1)
2845 dnlc_purge_vp(vp);
2848 douprintf = 1;
2850 error = rfs2call(VTOMI(dvp), RFS_RMDIR,
2851 xdr_diropargs, (caddr_t)&da,
2852 xdr_enum, (caddr_t)&status, cr,
2853 &douprintf, &status, 0, NULL);
2855 PURGE_ATTRCACHE(dvp); /* mod time changed */
2857 if (error) {
2858 VN_RELE(vp);
2859 nfs_rw_exit(&drp->r_rwlock);
2860 return (error);
2863 error = geterrno(status);
2864 if (!error) {
2865 if (HAVE_RDDIR_CACHE(drp))
2866 nfs_purge_rddir_cache(dvp);
2867 if (HAVE_RDDIR_CACHE(VTOR(vp)))
2868 nfs_purge_rddir_cache(vp);
2869 } else {
2870 PURGE_STALE_FH(error, dvp, cr);
2872 * System V defines rmdir to return EEXIST, not
2873 * ENOTEMPTY if the directory is not empty. Over
2874 * the wire, the error is NFSERR_ENOTEMPTY which
2875 * geterrno maps to ENOTEMPTY.
2877 if (error == ENOTEMPTY)
2878 error = EEXIST;
2881 if (error == 0) {
2882 vnevent_rmdir(vp, dvp, nm, ct);
2884 VN_RELE(vp);
2886 nfs_rw_exit(&drp->r_rwlock);
2888 return (error);
2891 /* ARGSUSED */
2892 static int
2893 nfs_symlink(vnode_t *dvp, char *lnm, struct vattr *tva, char *tnm, cred_t *cr,
2894 caller_context_t *ct, int flags)
2896 int error;
2897 struct nfsslargs args;
2898 enum nfsstat status;
2899 int douprintf;
2900 rnode_t *drp;
2902 if (nfs_zone() != VTOMI(dvp)->mi_zone)
2903 return (EPERM);
2904 setdiropargs(&args.sla_from, lnm, dvp);
2905 args.sla_sa = &args.sla_sa_buf;
2906 error = vattr_to_sattr(tva, args.sla_sa);
2907 if (error) {
2908 /* req time field(s) overflow - return immediately */
2909 return (error);
2911 args.sla_tnm = tnm;
2913 drp = VTOR(dvp);
2914 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR(dvp)))
2915 return (EINTR);
2917 dnlc_remove(dvp, lnm);
2919 douprintf = 1;
2921 error = rfs2call(VTOMI(dvp), RFS_SYMLINK,
2922 xdr_slargs, (caddr_t)&args,
2923 xdr_enum, (caddr_t)&status, cr,
2924 &douprintf, &status, 0, NULL);
2926 PURGE_ATTRCACHE(dvp); /* mod time changed */
2928 if (!error) {
2929 error = geterrno(status);
2930 if (!error) {
2931 if (HAVE_RDDIR_CACHE(drp))
2932 nfs_purge_rddir_cache(dvp);
2933 } else {
2934 PURGE_STALE_FH(error, dvp, cr);
2938 nfs_rw_exit(&drp->r_rwlock);
2940 return (error);
2943 #ifdef DEBUG
2944 static int nfs_readdir_cache_hits = 0;
2945 static int nfs_readdir_cache_shorts = 0;
2946 static int nfs_readdir_cache_waits = 0;
2947 static int nfs_readdir_cache_misses = 0;
2948 static int nfs_readdir_readahead = 0;
2949 #endif
2951 static int nfs_shrinkreaddir = 0;
2954 * Read directory entries.
2955 * There are some weird things to look out for here. The uio_offset
2956 * field is either 0 or it is the offset returned from a previous
2957 * readdir. It is an opaque value used by the server to find the
2958 * correct directory block to read. The count field is the number
2959 * of blocks to read on the server. This is advisory only, the server
2960 * may return only one block's worth of entries. Entries may be compressed
2961 * on the server.
2963 /* ARGSUSED */
2964 static int
2965 nfs_readdir(vnode_t *vp, struct uio *uiop, cred_t *cr, int *eofp,
2966 caller_context_t *ct, int flags)
2968 int error;
2969 size_t count;
2970 rnode_t *rp;
2971 rddir_cache *rdc;
2972 rddir_cache *nrdc;
2973 rddir_cache *rrdc;
2974 #ifdef DEBUG
2975 int missed;
2976 #endif
2977 rddir_cache srdc;
2978 avl_index_t where;
2980 rp = VTOR(vp);
2982 ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER));
2983 if (nfs_zone() != VTOMI(vp)->mi_zone)
2984 return (EIO);
2986 * Make sure that the directory cache is valid.
2988 if (HAVE_RDDIR_CACHE(rp)) {
2989 if (nfs_disable_rddir_cache) {
2991 * Setting nfs_disable_rddir_cache in /etc/system
2992 * allows interoperability with servers that do not
2993 * properly update the attributes of directories.
2994 * Any cached information gets purged before an
2995 * access is made to it.
2997 nfs_purge_rddir_cache(vp);
2998 } else {
2999 error = nfs_validate_caches(vp, cr);
3000 if (error)
3001 return (error);
3006 * UGLINESS: SunOS 3.2 servers apparently cannot always handle an
3007 * RFS_READDIR request with rda_count set to more than 0x400. So
3008 * we reduce the request size here purely for compatibility.
3010 * In general, this is no longer required. However, if a server
3011 * is discovered which can not handle requests larger than 1024,
3012 * nfs_shrinkreaddir can be set to 1 to enable this backwards
3013 * compatibility.
3015 * In any case, the request size is limited to NFS_MAXDATA bytes.
3017 count = MIN(uiop->uio_iov->iov_len,
3018 nfs_shrinkreaddir ? 0x400 : NFS_MAXDATA);
3020 nrdc = NULL;
3021 #ifdef DEBUG
3022 missed = 0;
3023 #endif
3024 top:
3026 * Short circuit last readdir which always returns 0 bytes.
3027 * This can be done after the directory has been read through
3028 * completely at least once. This will set r_direof which
3029 * can be used to find the value of the last cookie.
3031 mutex_enter(&rp->r_statelock);
3032 if (rp->r_direof != NULL &&
3033 uiop->uio_offset == rp->r_direof->nfs_ncookie) {
3034 mutex_exit(&rp->r_statelock);
3035 #ifdef DEBUG
3036 nfs_readdir_cache_shorts++;
3037 #endif
3038 if (eofp)
3039 *eofp = 1;
3040 if (nrdc != NULL)
3041 rddir_cache_rele(nrdc);
3042 return (0);
3045 * Look for a cache entry. Cache entries are identified
3046 * by the NFS cookie value and the byte count requested.
3048 srdc.nfs_cookie = uiop->uio_offset;
3049 srdc.buflen = count;
3050 rdc = avl_find(&rp->r_dir, &srdc, &where);
3051 if (rdc != NULL) {
3052 rddir_cache_hold(rdc);
3054 * If the cache entry is in the process of being
3055 * filled in, wait until this completes. The
3056 * RDDIRWAIT bit is set to indicate that someone
3057 * is waiting and then the thread currently
3058 * filling the entry is done, it should do a
3059 * cv_broadcast to wakeup all of the threads
3060 * waiting for it to finish.
3062 if (rdc->flags & RDDIR) {
3063 nfs_rw_exit(&rp->r_rwlock);
3064 rdc->flags |= RDDIRWAIT;
3065 #ifdef DEBUG
3066 nfs_readdir_cache_waits++;
3067 #endif
3068 if (!cv_wait_sig(&rdc->cv, &rp->r_statelock)) {
3070 * We got interrupted, probably
3071 * the user typed ^C or an alarm
3072 * fired. We free the new entry
3073 * if we allocated one.
3075 mutex_exit(&rp->r_statelock);
3076 (void) nfs_rw_enter_sig(&rp->r_rwlock,
3077 RW_READER, FALSE);
3078 rddir_cache_rele(rdc);
3079 if (nrdc != NULL)
3080 rddir_cache_rele(nrdc);
3081 return (EINTR);
3083 mutex_exit(&rp->r_statelock);
3084 (void) nfs_rw_enter_sig(&rp->r_rwlock,
3085 RW_READER, FALSE);
3086 rddir_cache_rele(rdc);
3087 goto top;
3090 * Check to see if a readdir is required to
3091 * fill the entry. If so, mark this entry
3092 * as being filled, remove our reference,
3093 * and branch to the code to fill the entry.
3095 if (rdc->flags & RDDIRREQ) {
3096 rdc->flags &= ~RDDIRREQ;
3097 rdc->flags |= RDDIR;
3098 if (nrdc != NULL)
3099 rddir_cache_rele(nrdc);
3100 nrdc = rdc;
3101 mutex_exit(&rp->r_statelock);
3102 goto bottom;
3104 #ifdef DEBUG
3105 if (!missed)
3106 nfs_readdir_cache_hits++;
3107 #endif
3109 * If an error occurred while attempting
3110 * to fill the cache entry, just return it.
3112 if (rdc->error) {
3113 error = rdc->error;
3114 mutex_exit(&rp->r_statelock);
3115 rddir_cache_rele(rdc);
3116 if (nrdc != NULL)
3117 rddir_cache_rele(nrdc);
3118 return (error);
3122 * The cache entry is complete and good,
3123 * copyout the dirent structs to the calling
3124 * thread.
3126 error = uiomove(rdc->entries, rdc->entlen, UIO_READ, uiop);
3129 * If no error occurred during the copyout,
3130 * update the offset in the uio struct to
3131 * contain the value of the next cookie
3132 * and set the eof value appropriately.
3134 if (!error) {
3135 uiop->uio_offset = rdc->nfs_ncookie;
3136 if (eofp)
3137 *eofp = rdc->eof;
3141 * Decide whether to do readahead. Don't if
3142 * have already read to the end of directory.
3144 if (rdc->eof) {
3145 rp->r_direof = rdc;
3146 mutex_exit(&rp->r_statelock);
3147 rddir_cache_rele(rdc);
3148 if (nrdc != NULL)
3149 rddir_cache_rele(nrdc);
3150 return (error);
3154 * Check to see whether we found an entry
3155 * for the readahead. If so, we don't need
3156 * to do anything further, so free the new
3157 * entry if one was allocated. Otherwise,
3158 * allocate a new entry, add it to the cache,
3159 * and then initiate an asynchronous readdir
3160 * operation to fill it.
3162 srdc.nfs_cookie = rdc->nfs_ncookie;
3163 srdc.buflen = count;
3164 rrdc = avl_find(&rp->r_dir, &srdc, &where);
3165 if (rrdc != NULL) {
3166 if (nrdc != NULL)
3167 rddir_cache_rele(nrdc);
3168 } else {
3169 if (nrdc != NULL)
3170 rrdc = nrdc;
3171 else {
3172 rrdc = rddir_cache_alloc(KM_NOSLEEP);
3174 if (rrdc != NULL) {
3175 rrdc->nfs_cookie = rdc->nfs_ncookie;
3176 rrdc->buflen = count;
3177 avl_insert(&rp->r_dir, rrdc, where);
3178 rddir_cache_hold(rrdc);
3179 mutex_exit(&rp->r_statelock);
3180 rddir_cache_rele(rdc);
3181 #ifdef DEBUG
3182 nfs_readdir_readahead++;
3183 #endif
3184 nfs_async_readdir(vp, rrdc, cr, nfsreaddir);
3185 return (error);
3189 mutex_exit(&rp->r_statelock);
3190 rddir_cache_rele(rdc);
3191 return (error);
3195 * Didn't find an entry in the cache. Construct a new empty
3196 * entry and link it into the cache. Other processes attempting
3197 * to access this entry will need to wait until it is filled in.
3199 * Since kmem_alloc may block, another pass through the cache
3200 * will need to be taken to make sure that another process
3201 * hasn't already added an entry to the cache for this request.
3203 if (nrdc == NULL) {
3204 mutex_exit(&rp->r_statelock);
3205 nrdc = rddir_cache_alloc(KM_SLEEP);
3206 nrdc->nfs_cookie = uiop->uio_offset;
3207 nrdc->buflen = count;
3208 goto top;
3212 * Add this entry to the cache.
3214 avl_insert(&rp->r_dir, nrdc, where);
3215 rddir_cache_hold(nrdc);
3216 mutex_exit(&rp->r_statelock);
3218 bottom:
3219 #ifdef DEBUG
3220 missed = 1;
3221 nfs_readdir_cache_misses++;
3222 #endif
3224 * Do the readdir.
3226 error = nfsreaddir(vp, nrdc, cr);
3229 * If this operation failed, just return the error which occurred.
3231 if (error != 0)
3232 return (error);
3235 * Since the RPC operation will have taken sometime and blocked
3236 * this process, another pass through the cache will need to be
3237 * taken to find the correct cache entry. It is possible that
3238 * the correct cache entry will not be there (although one was
3239 * added) because the directory changed during the RPC operation
3240 * and the readdir cache was flushed. In this case, just start
3241 * over. It is hoped that this will not happen too often... :-)
3243 nrdc = NULL;
3244 goto top;
3245 /* NOTREACHED */
3248 static int
3249 nfsreaddir(vnode_t *vp, rddir_cache *rdc, cred_t *cr)
3251 int error;
3252 struct nfsrddirargs rda;
3253 struct nfsrddirres rd;
3254 rnode_t *rp;
3255 mntinfo_t *mi;
3256 uint_t count;
3257 int douprintf;
3258 failinfo_t fi, *fip;
3260 ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
3261 count = rdc->buflen;
3263 rp = VTOR(vp);
3264 mi = VTOMI(vp);
3266 rda.rda_fh = *VTOFH(vp);
3267 rda.rda_offset = rdc->nfs_cookie;
3270 * NFS client failover support
3271 * suppress failover unless we have a zero cookie
3273 if (rdc->nfs_cookie == (off_t)0) {
3274 fi.vp = vp;
3275 fi.fhp = (caddr_t)&rda.rda_fh;
3276 fi.copyproc = nfscopyfh;
3277 fi.lookupproc = nfslookup;
3278 fi.xattrdirproc = acl_getxattrdir2;
3279 fip = &fi;
3280 } else {
3281 fip = NULL;
3284 rd.rd_entries = kmem_alloc(rdc->buflen, KM_SLEEP);
3285 rd.rd_size = count;
3286 rd.rd_offset = rda.rda_offset;
3288 douprintf = 1;
3290 if (mi->mi_io_kstats) {
3291 mutex_enter(&mi->mi_lock);
3292 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
3293 mutex_exit(&mi->mi_lock);
3296 do {
3297 rda.rda_count = MIN(count, mi->mi_curread);
3298 error = rfs2call(mi, RFS_READDIR,
3299 xdr_rddirargs, (caddr_t)&rda,
3300 xdr_getrddirres, (caddr_t)&rd, cr,
3301 &douprintf, &rd.rd_status, 0, fip);
3302 } while (error == ENFS_TRYAGAIN);
3304 if (mi->mi_io_kstats) {
3305 mutex_enter(&mi->mi_lock);
3306 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
3307 mutex_exit(&mi->mi_lock);
3311 * Since we are actually doing a READDIR RPC, we must have
3312 * exclusive access to the cache entry being filled. Thus,
3313 * it is safe to update all fields except for the flags
3314 * field. The r_statelock in the rnode must be held to
3315 * prevent two different threads from simultaneously
3316 * attempting to update the flags field. This can happen
3317 * if we are turning off RDDIR and the other thread is
3318 * trying to set RDDIRWAIT.
3320 ASSERT(rdc->flags & RDDIR);
3321 if (!error) {
3322 error = geterrno(rd.rd_status);
3323 if (!error) {
3324 rdc->nfs_ncookie = rd.rd_offset;
3325 rdc->eof = rd.rd_eof ? 1 : 0;
3326 rdc->entlen = rd.rd_size;
3327 ASSERT(rdc->entlen <= rdc->buflen);
3328 #ifdef DEBUG
3329 rdc->entries = rddir_cache_buf_alloc(rdc->buflen,
3330 KM_SLEEP);
3331 #else
3332 rdc->entries = kmem_alloc(rdc->buflen, KM_SLEEP);
3333 #endif
3334 bcopy(rd.rd_entries, rdc->entries, rdc->entlen);
3335 rdc->error = 0;
3336 if (mi->mi_io_kstats) {
3337 mutex_enter(&mi->mi_lock);
3338 KSTAT_IO_PTR(mi->mi_io_kstats)->reads++;
3339 KSTAT_IO_PTR(mi->mi_io_kstats)->nread +=
3340 rd.rd_size;
3341 mutex_exit(&mi->mi_lock);
3343 } else {
3344 PURGE_STALE_FH(error, vp, cr);
3347 if (error) {
3348 rdc->entries = NULL;
3349 rdc->error = error;
3351 kmem_free(rd.rd_entries, rdc->buflen);
3353 mutex_enter(&rp->r_statelock);
3354 rdc->flags &= ~RDDIR;
3355 if (rdc->flags & RDDIRWAIT) {
3356 rdc->flags &= ~RDDIRWAIT;
3357 cv_broadcast(&rdc->cv);
3359 if (error)
3360 rdc->flags |= RDDIRREQ;
3361 mutex_exit(&rp->r_statelock);
3363 rddir_cache_rele(rdc);
3365 return (error);
3368 #ifdef DEBUG
3369 static int nfs_bio_do_stop = 0;
3370 #endif
3372 static int
3373 nfs_bio(struct buf *bp, cred_t *cr)
3375 rnode_t *rp = VTOR(bp->b_vp);
3376 int count;
3377 int error;
3378 cred_t *cred;
3379 uint_t offset;
3381 DTRACE_IO1(start, struct buf *, bp);
3383 ASSERT(nfs_zone() == VTOMI(bp->b_vp)->mi_zone);
3384 offset = dbtob(bp->b_blkno);
3386 if (bp->b_flags & B_READ) {
3387 mutex_enter(&rp->r_statelock);
3388 if (rp->r_cred != NULL) {
3389 cred = rp->r_cred;
3390 crhold(cred);
3391 } else {
3392 rp->r_cred = cr;
3393 crhold(cr);
3394 cred = cr;
3395 crhold(cred);
3397 mutex_exit(&rp->r_statelock);
3398 read_again:
3399 error = bp->b_error = nfsread(bp->b_vp, bp->b_un.b_addr,
3400 offset, bp->b_bcount, &bp->b_resid, cred);
3402 crfree(cred);
3403 if (!error) {
3404 if (bp->b_resid) {
3406 * Didn't get it all because we hit EOF,
3407 * zero all the memory beyond the EOF.
3409 /* bzero(rdaddr + */
3410 bzero(bp->b_un.b_addr +
3411 bp->b_bcount - bp->b_resid, bp->b_resid);
3413 mutex_enter(&rp->r_statelock);
3414 if (bp->b_resid == bp->b_bcount &&
3415 offset >= rp->r_size) {
3417 * We didn't read anything at all as we are
3418 * past EOF. Return an error indicator back
3419 * but don't destroy the pages (yet).
3421 error = NFS_EOF;
3423 mutex_exit(&rp->r_statelock);
3424 } else if (error == EACCES) {
3425 mutex_enter(&rp->r_statelock);
3426 if (cred != cr) {
3427 if (rp->r_cred != NULL)
3428 crfree(rp->r_cred);
3429 rp->r_cred = cr;
3430 crhold(cr);
3431 cred = cr;
3432 crhold(cred);
3433 mutex_exit(&rp->r_statelock);
3434 goto read_again;
3436 mutex_exit(&rp->r_statelock);
3438 } else {
3439 if (!(rp->r_flags & RSTALE)) {
3440 mutex_enter(&rp->r_statelock);
3441 if (rp->r_cred != NULL) {
3442 cred = rp->r_cred;
3443 crhold(cred);
3444 } else {
3445 rp->r_cred = cr;
3446 crhold(cr);
3447 cred = cr;
3448 crhold(cred);
3450 mutex_exit(&rp->r_statelock);
3451 write_again:
3452 mutex_enter(&rp->r_statelock);
3453 count = MIN(bp->b_bcount, rp->r_size - offset);
3454 mutex_exit(&rp->r_statelock);
3455 if (count < 0)
3456 cmn_err(CE_PANIC, "nfs_bio: write count < 0");
3457 #ifdef DEBUG
3458 if (count == 0) {
3459 zcmn_err(getzoneid(), CE_WARN,
3460 "nfs_bio: zero length write at %d",
3461 offset);
3462 nfs_printfhandle(&rp->r_fh);
3463 if (nfs_bio_do_stop)
3464 debug_enter("nfs_bio");
3466 #endif
3467 error = nfswrite(bp->b_vp, bp->b_un.b_addr, offset,
3468 count, cred);
3469 if (error == EACCES) {
3470 mutex_enter(&rp->r_statelock);
3471 if (cred != cr) {
3472 if (rp->r_cred != NULL)
3473 crfree(rp->r_cred);
3474 rp->r_cred = cr;
3475 crhold(cr);
3476 crfree(cred);
3477 cred = cr;
3478 crhold(cred);
3479 mutex_exit(&rp->r_statelock);
3480 goto write_again;
3482 mutex_exit(&rp->r_statelock);
3484 bp->b_error = error;
3485 if (error && error != EINTR) {
3487 * Don't print EDQUOT errors on the console.
3488 * Don't print asynchronous EACCES errors.
3489 * Don't print EFBIG errors.
3490 * Print all other write errors.
3492 if (error != EDQUOT && error != EFBIG &&
3493 (error != EACCES ||
3494 !(bp->b_flags & B_ASYNC)))
3495 nfs_write_error(bp->b_vp, error, cred);
3497 * Update r_error and r_flags as appropriate.
3498 * If the error was ESTALE, then mark the
3499 * rnode as not being writeable and save
3500 * the error status. Otherwise, save any
3501 * errors which occur from asynchronous
3502 * page invalidations. Any errors occurring
3503 * from other operations should be saved
3504 * by the caller.
3506 mutex_enter(&rp->r_statelock);
3507 if (error == ESTALE) {
3508 rp->r_flags |= RSTALE;
3509 if (!rp->r_error)
3510 rp->r_error = error;
3511 } else if (!rp->r_error &&
3512 (bp->b_flags &
3513 (B_INVAL|B_FORCE|B_ASYNC)) ==
3514 (B_INVAL|B_FORCE|B_ASYNC)) {
3515 rp->r_error = error;
3517 mutex_exit(&rp->r_statelock);
3519 crfree(cred);
3520 } else {
3521 error = rp->r_error;
3523 * A close may have cleared r_error, if so,
3524 * propagate ESTALE error return properly
3526 if (error == 0)
3527 error = ESTALE;
3531 if (error != 0 && error != NFS_EOF)
3532 bp->b_flags |= B_ERROR;
3534 DTRACE_IO1(done, struct buf *, bp);
3536 return (error);
3539 /* ARGSUSED */
3540 static int
3541 nfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
3543 struct nfs_fid *fp;
3544 rnode_t *rp;
3546 rp = VTOR(vp);
3548 if (fidp->fid_len < (sizeof (struct nfs_fid) - sizeof (short))) {
3549 fidp->fid_len = sizeof (struct nfs_fid) - sizeof (short);
3550 return (ENOSPC);
3552 fp = (struct nfs_fid *)fidp;
3553 fp->nf_pad = 0;
3554 fp->nf_len = sizeof (struct nfs_fid) - sizeof (short);
3555 bcopy(rp->r_fh.fh_buf, fp->nf_data, NFS_FHSIZE);
3556 return (0);
3559 /* ARGSUSED2 */
3560 static int
3561 nfs_rwlock(vnode_t *vp, int write_lock, caller_context_t *ctp)
3563 rnode_t *rp = VTOR(vp);
3565 if (!write_lock) {
3566 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE);
3567 return (V_WRITELOCK_FALSE);
3570 if ((rp->r_flags & RDIRECTIO) || (VTOMI(vp)->mi_flags & MI_DIRECTIO)) {
3571 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE);
3572 if (rp->r_mapcnt == 0 && !vn_has_cached_data(vp))
3573 return (V_WRITELOCK_FALSE);
3574 nfs_rw_exit(&rp->r_rwlock);
3577 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, FALSE);
3578 return (V_WRITELOCK_TRUE);
3581 /* ARGSUSED */
3582 static void
3583 nfs_rwunlock(vnode_t *vp, int write_lock, caller_context_t *ctp)
3585 rnode_t *rp = VTOR(vp);
3587 nfs_rw_exit(&rp->r_rwlock);
3590 /* ARGSUSED */
3591 static int
3592 nfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp, caller_context_t *ct)
3596 * Because we stuff the readdir cookie into the offset field
3597 * someone may attempt to do an lseek with the cookie which
3598 * we want to succeed.
3600 if (vp->v_type == VDIR)
3601 return (0);
3602 if (*noffp < 0 || *noffp > MAXOFF32_T)
3603 return (EINVAL);
3604 return (0);
3608 * number of NFS_MAXDATA blocks to read ahead
3609 * optimized for 100 base-T.
3611 static int nfs_nra = 4;
3613 #ifdef DEBUG
3614 static int nfs_lostpage = 0; /* number of times we lost original page */
3615 #endif
3618 * Return all the pages from [off..off+len) in file
3620 /* ARGSUSED */
3621 static int
3622 nfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
3623 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
3624 enum seg_rw rw, cred_t *cr, caller_context_t *ct)
3626 rnode_t *rp;
3627 int error;
3628 mntinfo_t *mi;
3630 if (vp->v_flag & VNOMAP)
3631 return (ENOSYS);
3633 ASSERT(off <= MAXOFF32_T);
3634 if (nfs_zone() != VTOMI(vp)->mi_zone)
3635 return (EIO);
3636 if (protp != NULL)
3637 *protp = PROT_ALL;
3640 * Now valididate that the caches are up to date.
3642 error = nfs_validate_caches(vp, cr);
3643 if (error)
3644 return (error);
3646 rp = VTOR(vp);
3647 mi = VTOMI(vp);
3648 retry:
3649 mutex_enter(&rp->r_statelock);
3652 * Don't create dirty pages faster than they
3653 * can be cleaned so that the system doesn't
3654 * get imbalanced. If the async queue is
3655 * maxed out, then wait for it to drain before
3656 * creating more dirty pages. Also, wait for
3657 * any threads doing pagewalks in the vop_getattr
3658 * entry points so that they don't block for
3659 * long periods.
3661 if (rw == S_CREATE) {
3662 while ((mi->mi_max_threads != 0 &&
3663 rp->r_awcount > 2 * mi->mi_max_threads) ||
3664 rp->r_gcount > 0)
3665 cv_wait(&rp->r_cv, &rp->r_statelock);
3669 * If we are getting called as a side effect of an nfs_write()
3670 * operation the local file size might not be extended yet.
3671 * In this case we want to be able to return pages of zeroes.
3673 if (off + len > rp->r_size + PAGEOFFSET && seg != segkmap) {
3674 mutex_exit(&rp->r_statelock);
3675 return (EFAULT); /* beyond EOF */
3678 mutex_exit(&rp->r_statelock);
3680 error = pvn_getpages(nfs_getapage, vp, off, len, protp, pl, plsz,
3681 seg, addr, rw, cr);
3683 switch (error) {
3684 case NFS_EOF:
3685 nfs_purge_caches(vp, NFS_NOPURGE_DNLC, cr);
3686 goto retry;
3687 case ESTALE:
3688 PURGE_STALE_FH(error, vp, cr);
3691 return (error);
3695 * Called from pvn_getpages to get a particular page.
3697 /* ARGSUSED */
3698 static int
3699 nfs_getapage(vnode_t *vp, uoff_t off, size_t len, uint_t *protp,
3700 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
3701 enum seg_rw rw, cred_t *cr)
3703 rnode_t *rp;
3704 uint_t bsize;
3705 struct buf *bp;
3706 page_t *pp;
3707 uoff_t lbn;
3708 uoff_t io_off;
3709 uoff_t blkoff;
3710 uoff_t rablkoff;
3711 size_t io_len;
3712 uint_t blksize;
3713 int error;
3714 int readahead;
3715 int readahead_issued = 0;
3716 int ra_window; /* readahead window */
3717 page_t *pagefound;
3719 if (nfs_zone() != VTOMI(vp)->mi_zone)
3720 return (EIO);
3721 rp = VTOR(vp);
3722 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE);
3724 reread:
3725 bp = NULL;
3726 pp = NULL;
3727 pagefound = NULL;
3729 if (pl != NULL)
3730 pl[0] = NULL;
3732 error = 0;
3733 lbn = off / bsize;
3734 blkoff = lbn * bsize;
3737 * Queueing up the readahead before doing the synchronous read
3738 * results in a significant increase in read throughput because
3739 * of the increased parallelism between the async threads and
3740 * the process context.
3742 if ((off & ((vp->v_vfsp->vfs_bsize) - 1)) == 0 &&
3743 rw != S_CREATE &&
3744 !(vp->v_flag & VNOCACHE)) {
3745 mutex_enter(&rp->r_statelock);
3748 * Calculate the number of readaheads to do.
3749 * a) No readaheads at offset = 0.
3750 * b) Do maximum(nfs_nra) readaheads when the readahead
3751 * window is closed.
3752 * c) Do readaheads between 1 to (nfs_nra - 1) depending
3753 * upon how far the readahead window is open or close.
3754 * d) No readaheads if rp->r_nextr is not within the scope
3755 * of the readahead window (random i/o).
3758 if (off == 0)
3759 readahead = 0;
3760 else if (blkoff == rp->r_nextr)
3761 readahead = nfs_nra;
3762 else if (rp->r_nextr > blkoff &&
3763 ((ra_window = (rp->r_nextr - blkoff) / bsize)
3764 <= (nfs_nra - 1)))
3765 readahead = nfs_nra - ra_window;
3766 else
3767 readahead = 0;
3769 rablkoff = rp->r_nextr;
3770 while (readahead > 0 && rablkoff + bsize < rp->r_size) {
3771 mutex_exit(&rp->r_statelock);
3772 if (nfs_async_readahead(vp, rablkoff + bsize,
3773 addr + (rablkoff + bsize - off), seg, cr,
3774 nfs_readahead) < 0) {
3775 mutex_enter(&rp->r_statelock);
3776 break;
3778 readahead--;
3779 rablkoff += bsize;
3781 * Indicate that we did a readahead so
3782 * readahead offset is not updated
3783 * by the synchronous read below.
3785 readahead_issued = 1;
3786 mutex_enter(&rp->r_statelock);
3788 * set readahead offset to
3789 * offset of last async readahead
3790 * request.
3792 rp->r_nextr = rablkoff;
3794 mutex_exit(&rp->r_statelock);
3797 again:
3798 if ((pagefound = page_exists(&vp->v_object, off)) == NULL) {
3799 if (pl == NULL) {
3800 (void) nfs_async_readahead(vp, blkoff, addr, seg, cr,
3801 nfs_readahead);
3802 } else if (rw == S_CREATE) {
3804 * Block for this page is not allocated, or the offset
3805 * is beyond the current allocation size, or we're
3806 * allocating a swap slot and the page was not found,
3807 * so allocate it and return a zero page.
3809 if ((pp = page_create_va(&vp->v_object, off,
3810 PAGESIZE, PG_WAIT, seg, addr)) == NULL)
3811 cmn_err(CE_PANIC, "nfs_getapage: page_create");
3812 io_len = PAGESIZE;
3813 mutex_enter(&rp->r_statelock);
3814 rp->r_nextr = off + PAGESIZE;
3815 mutex_exit(&rp->r_statelock);
3816 } else {
3818 * Need to go to server to get a BLOCK, exception to
3819 * that being while reading at offset = 0 or doing
3820 * random i/o, in that case read only a PAGE.
3822 mutex_enter(&rp->r_statelock);
3823 if (blkoff < rp->r_size &&
3824 blkoff + bsize >= rp->r_size) {
3826 * If only a block or less is left in
3827 * the file, read all that is remaining.
3829 if (rp->r_size <= off) {
3831 * Trying to access beyond EOF,
3832 * set up to get at least one page.
3834 blksize = off + PAGESIZE - blkoff;
3835 } else
3836 blksize = rp->r_size - blkoff;
3837 } else if ((off == 0) ||
3838 (off != rp->r_nextr && !readahead_issued)) {
3839 blksize = PAGESIZE;
3840 blkoff = off; /* block = page here */
3841 } else
3842 blksize = bsize;
3843 mutex_exit(&rp->r_statelock);
3845 pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
3846 &io_len, blkoff, blksize, 0);
3849 * Some other thread has entered the page,
3850 * so just use it.
3852 if (pp == NULL)
3853 goto again;
3856 * Now round the request size up to page boundaries.
3857 * This ensures that the entire page will be
3858 * initialized to zeroes if EOF is encountered.
3860 io_len = ptob(btopr(io_len));
3862 bp = pageio_setup(pp, io_len, vp, B_READ);
3863 ASSERT(bp != NULL);
3866 * pageio_setup should have set b_addr to 0. This
3867 * is correct since we want to do I/O on a page
3868 * boundary. bp_mapin will use this addr to calculate
3869 * an offset, and then set b_addr to the kernel virtual
3870 * address it allocated for us.
3872 ASSERT(bp->b_un.b_addr == 0);
3874 bp->b_edev = 0;
3875 bp->b_dev = 0;
3876 bp->b_lblkno = lbtodb(io_off);
3877 bp->b_file = vp;
3878 bp->b_offset = (offset_t)off;
3879 bp_mapin(bp);
3882 * If doing a write beyond what we believe is EOF,
3883 * don't bother trying to read the pages from the
3884 * server, we'll just zero the pages here. We
3885 * don't check that the rw flag is S_WRITE here
3886 * because some implementations may attempt a
3887 * read access to the buffer before copying data.
3889 mutex_enter(&rp->r_statelock);
3890 if (io_off >= rp->r_size && seg == segkmap) {
3891 mutex_exit(&rp->r_statelock);
3892 bzero(bp->b_un.b_addr, io_len);
3893 } else {
3894 mutex_exit(&rp->r_statelock);
3895 error = nfs_bio(bp, cr);
3899 * Unmap the buffer before freeing it.
3901 bp_mapout(bp);
3902 pageio_done(bp);
3904 if (error == NFS_EOF) {
3906 * If doing a write system call just return
3907 * zeroed pages, else user tried to get pages
3908 * beyond EOF, return error. We don't check
3909 * that the rw flag is S_WRITE here because
3910 * some implementations may attempt a read
3911 * access to the buffer before copying data.
3913 if (seg == segkmap)
3914 error = 0;
3915 else
3916 error = EFAULT;
3919 if (!readahead_issued && !error) {
3920 mutex_enter(&rp->r_statelock);
3921 rp->r_nextr = io_off + io_len;
3922 mutex_exit(&rp->r_statelock);
3927 out:
3928 if (pl == NULL)
3929 return (error);
3931 if (error) {
3932 if (pp != NULL)
3933 pvn_read_done(pp, B_ERROR);
3934 return (error);
3937 if (pagefound) {
3938 se_t se = (rw == S_CREATE ? SE_EXCL : SE_SHARED);
3941 * Page exists in the cache, acquire the appropriate lock.
3942 * If this fails, start all over again.
3944 if ((pp = page_lookup(&vp->v_object, off, se)) == NULL) {
3945 #ifdef DEBUG
3946 nfs_lostpage++;
3947 #endif
3948 goto reread;
3950 pl[0] = pp;
3951 pl[1] = NULL;
3952 return (0);
3955 if (pp != NULL)
3956 pvn_plist_init(pp, pl, plsz, off, io_len, rw);
3958 return (error);
3961 static void
3962 nfs_readahead(vnode_t *vp, uoff_t blkoff, caddr_t addr, struct seg *seg,
3963 cred_t *cr)
3965 int error;
3966 page_t *pp;
3967 uoff_t io_off;
3968 size_t io_len;
3969 struct buf *bp;
3970 uint_t bsize, blksize;
3971 rnode_t *rp = VTOR(vp);
3973 ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
3975 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE);
3977 mutex_enter(&rp->r_statelock);
3978 if (blkoff < rp->r_size && blkoff + bsize > rp->r_size) {
3980 * If less than a block left in file read less
3981 * than a block.
3983 blksize = rp->r_size - blkoff;
3984 } else
3985 blksize = bsize;
3986 mutex_exit(&rp->r_statelock);
3988 pp = pvn_read_kluster(vp, blkoff, segkmap, addr,
3989 &io_off, &io_len, blkoff, blksize, 1);
3991 * The isra flag passed to the kluster function is 1, we may have
3992 * gotten a return value of NULL for a variety of reasons (# of free
3993 * pages < minfree, someone entered the page on the vnode etc). In all
3994 * cases, we want to punt on the readahead.
3996 if (pp == NULL)
3997 return;
4000 * Now round the request size up to page boundaries.
4001 * This ensures that the entire page will be
4002 * initialized to zeroes if EOF is encountered.
4004 io_len = ptob(btopr(io_len));
4006 bp = pageio_setup(pp, io_len, vp, B_READ);
4007 ASSERT(bp != NULL);
4010 * pageio_setup should have set b_addr to 0. This is correct since
4011 * we want to do I/O on a page boundary. bp_mapin() will use this addr
4012 * to calculate an offset, and then set b_addr to the kernel virtual
4013 * address it allocated for us.
4015 ASSERT(bp->b_un.b_addr == 0);
4017 bp->b_edev = 0;
4018 bp->b_dev = 0;
4019 bp->b_lblkno = lbtodb(io_off);
4020 bp->b_file = vp;
4021 bp->b_offset = (offset_t)blkoff;
4022 bp_mapin(bp);
4025 * If doing a write beyond what we believe is EOF, don't bother trying
4026 * to read the pages from the server, we'll just zero the pages here.
4027 * We don't check that the rw flag is S_WRITE here because some
4028 * implementations may attempt a read access to the buffer before
4029 * copying data.
4031 mutex_enter(&rp->r_statelock);
4032 if (io_off >= rp->r_size && seg == segkmap) {
4033 mutex_exit(&rp->r_statelock);
4034 bzero(bp->b_un.b_addr, io_len);
4035 error = 0;
4036 } else {
4037 mutex_exit(&rp->r_statelock);
4038 error = nfs_bio(bp, cr);
4039 if (error == NFS_EOF)
4040 error = 0;
4044 * Unmap the buffer before freeing it.
4046 bp_mapout(bp);
4047 pageio_done(bp);
4049 pvn_read_done(pp, error ? B_READ | B_ERROR : B_READ);
4052 * In case of error set readahead offset
4053 * to the lowest offset.
4054 * pvn_read_done() calls VN_DISPOSE to destroy the pages
4056 if (error && rp->r_nextr > io_off) {
4057 mutex_enter(&rp->r_statelock);
4058 if (rp->r_nextr > io_off)
4059 rp->r_nextr = io_off;
4060 mutex_exit(&rp->r_statelock);
4065 * Flags are composed of {B_INVAL, B_FREE, B_DONTNEED, B_FORCE}
4066 * If len == 0, do from off to EOF.
4068 * The normal cases should be len == 0 && off == 0 (entire vp list),
4069 * len == MAXBSIZE (from segmap_release actions), and len == PAGESIZE
4070 * (from pageout).
4072 /* ARGSUSED */
4073 static int
4074 nfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr,
4075 caller_context_t *ct)
4077 int error;
4078 rnode_t *rp;
4080 ASSERT(cr != NULL);
4083 * XXX - Why should this check be made here?
4085 if (vp->v_flag & VNOMAP)
4086 return (ENOSYS);
4088 if (len == 0 && !(flags & B_INVAL) && vn_is_readonly(vp))
4089 return (0);
4091 if (!(flags & B_ASYNC) && nfs_zone() != VTOMI(vp)->mi_zone)
4092 return (EIO);
4093 ASSERT(off <= MAXOFF32_T);
4095 rp = VTOR(vp);
4096 mutex_enter(&rp->r_statelock);
4097 rp->r_count++;
4098 mutex_exit(&rp->r_statelock);
4099 error = nfs_putpages(vp, off, len, flags, cr);
4100 mutex_enter(&rp->r_statelock);
4101 rp->r_count--;
4102 cv_broadcast(&rp->r_cv);
4103 mutex_exit(&rp->r_statelock);
4105 return (error);
4109 * Write out a single page, possibly klustering adjacent dirty pages.
4112 nfs_putapage(vnode_t *vp, page_t *pp, uoff_t *offp, size_t *lenp,
4113 int flags, cred_t *cr)
4115 uoff_t io_off;
4116 uoff_t lbn_off;
4117 uoff_t lbn;
4118 size_t io_len;
4119 uint_t bsize;
4120 int error;
4121 rnode_t *rp;
4123 ASSERT(!vn_is_readonly(vp));
4124 ASSERT(pp != NULL);
4125 ASSERT(cr != NULL);
4126 ASSERT((flags & B_ASYNC) || nfs_zone() == VTOMI(vp)->mi_zone);
4128 rp = VTOR(vp);
4129 ASSERT(rp->r_count > 0);
4131 ASSERT(pp->p_offset <= MAXOFF32_T);
4133 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE);
4134 lbn = pp->p_offset / bsize;
4135 lbn_off = lbn * bsize;
4138 * Find a kluster that fits in one block, or in
4139 * one page if pages are bigger than blocks. If
4140 * there is less file space allocated than a whole
4141 * page, we'll shorten the i/o request below.
4143 pp = pvn_write_kluster(vp, pp, &io_off, &io_len, lbn_off,
4144 roundup(bsize, PAGESIZE), flags);
4147 * pvn_write_kluster shouldn't have returned a page with offset
4148 * behind the original page we were given. Verify that.
4150 ASSERT((pp->p_offset / bsize) >= lbn);
4153 * Now pp will have the list of kept dirty pages marked for
4154 * write back. It will also handle invalidation and freeing
4155 * of pages that are not dirty. Check for page length rounding
4156 * problems.
4158 if (io_off + io_len > lbn_off + bsize) {
4159 ASSERT((io_off + io_len) - (lbn_off + bsize) < PAGESIZE);
4160 io_len = lbn_off + bsize - io_off;
4163 * The RMODINPROGRESS flag makes sure that nfs(3)_bio() sees a
4164 * consistent value of r_size. RMODINPROGRESS is set in writerp().
4165 * When RMODINPROGRESS is set it indicates that a uiomove() is in
4166 * progress and the r_size has not been made consistent with the
4167 * new size of the file. When the uiomove() completes the r_size is
4168 * updated and the RMODINPROGRESS flag is cleared.
4170 * The RMODINPROGRESS flag makes sure that nfs(3)_bio() sees a
4171 * consistent value of r_size. Without this handshaking, it is
4172 * possible that nfs(3)_bio() picks up the old value of r_size
4173 * before the uiomove() in writerp() completes. This will result
4174 * in the write through nfs(3)_bio() being dropped.
4176 * More precisely, there is a window between the time the uiomove()
4177 * completes and the time the r_size is updated. If a fop_putpage()
4178 * operation intervenes in this window, the page will be picked up,
4179 * because it is dirty (it will be unlocked, unless it was
4180 * pagecreate'd). When the page is picked up as dirty, the dirty
4181 * bit is reset (pvn_getdirty()). In nfs(3)write(), r_size is
4182 * checked. This will still be the old size. Therefore the page will
4183 * not be written out. When segmap_release() calls fop_putpage(),
4184 * the page will be found to be clean and the write will be dropped.
4186 if (rp->r_flags & RMODINPROGRESS) {
4187 mutex_enter(&rp->r_statelock);
4188 if ((rp->r_flags & RMODINPROGRESS) &&
4189 rp->r_modaddr + MAXBSIZE > io_off &&
4190 rp->r_modaddr < io_off + io_len) {
4191 page_t *plist;
4193 * A write is in progress for this region of the file.
4194 * If we did not detect RMODINPROGRESS here then this
4195 * path through nfs_putapage() would eventually go to
4196 * nfs(3)_bio() and may not write out all of the data
4197 * in the pages. We end up losing data. So we decide
4198 * to set the modified bit on each page in the page
4199 * list and mark the rnode with RDIRTY. This write
4200 * will be restarted at some later time.
4202 plist = pp;
4203 while (plist != NULL) {
4204 pp = plist;
4205 page_sub(&plist, pp);
4206 hat_setmod(pp);
4207 page_io_unlock(pp);
4208 page_unlock(pp);
4210 rp->r_flags |= RDIRTY;
4211 mutex_exit(&rp->r_statelock);
4212 if (offp)
4213 *offp = io_off;
4214 if (lenp)
4215 *lenp = io_len;
4216 return (0);
4218 mutex_exit(&rp->r_statelock);
4221 if (flags & B_ASYNC) {
4222 error = nfs_async_putapage(vp, pp, io_off, io_len, flags, cr,
4223 nfs_sync_putapage);
4224 } else
4225 error = nfs_sync_putapage(vp, pp, io_off, io_len, flags, cr);
4227 if (offp)
4228 *offp = io_off;
4229 if (lenp)
4230 *lenp = io_len;
4231 return (error);
4234 static int
4235 nfs_sync_putapage(vnode_t *vp, page_t *pp, uoff_t io_off, size_t io_len,
4236 int flags, cred_t *cr)
4238 int error;
4239 rnode_t *rp;
4241 flags |= B_WRITE;
4243 ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
4244 error = nfs_rdwrlbn(vp, pp, io_off, io_len, flags, cr);
4246 rp = VTOR(vp);
4248 if ((error == ENOSPC || error == EDQUOT || error == EACCES) &&
4249 (flags & (B_INVAL|B_FORCE)) != (B_INVAL|B_FORCE)) {
4250 if (!(rp->r_flags & ROUTOFSPACE)) {
4251 mutex_enter(&rp->r_statelock);
4252 rp->r_flags |= ROUTOFSPACE;
4253 mutex_exit(&rp->r_statelock);
4255 flags |= B_ERROR;
4256 pvn_write_done(pp, flags);
4258 * If this was not an async thread, then try again to
4259 * write out the pages, but this time, also destroy
4260 * them whether or not the write is successful. This
4261 * will prevent memory from filling up with these
4262 * pages and destroying them is the only alternative
4263 * if they can't be written out.
4265 * Don't do this if this is an async thread because
4266 * when the pages are unlocked in pvn_write_done,
4267 * some other thread could have come along, locked
4268 * them, and queued for an async thread. It would be
4269 * possible for all of the async threads to be tied
4270 * up waiting to lock the pages again and they would
4271 * all already be locked and waiting for an async
4272 * thread to handle them. Deadlock.
4274 if (!(flags & B_ASYNC)) {
4275 error = nfs_putpage(vp, io_off, io_len,
4276 B_INVAL | B_FORCE, cr, NULL);
4278 } else {
4279 if (error)
4280 flags |= B_ERROR;
4281 else if (rp->r_flags & ROUTOFSPACE) {
4282 mutex_enter(&rp->r_statelock);
4283 rp->r_flags &= ~ROUTOFSPACE;
4284 mutex_exit(&rp->r_statelock);
4286 pvn_write_done(pp, flags);
4289 return (error);
4292 /* ARGSUSED */
4293 static int
4294 nfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
4295 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
4296 caller_context_t *ct)
4298 struct segvn_crargs vn_a;
4299 int error;
4300 rnode_t *rp;
4301 struct vattr va;
4303 if (nfs_zone() != VTOMI(vp)->mi_zone)
4304 return (EIO);
4306 if (vp->v_flag & VNOMAP)
4307 return (ENOSYS);
4309 if (off > MAXOFF32_T)
4310 return (EFBIG);
4312 if (off < 0 || off + len < 0)
4313 return (ENXIO);
4315 if (vp->v_type != VREG)
4316 return (ENODEV);
4319 * If there is cached data and if close-to-open consistency
4320 * checking is not turned off and if the file system is not
4321 * mounted readonly, then force an over the wire getattr.
4322 * Otherwise, just invoke nfsgetattr to get a copy of the
4323 * attributes. The attribute cache will be used unless it
4324 * is timed out and if it is, then an over the wire getattr
4325 * will be issued.
4327 va.va_mask = VATTR_ALL;
4328 if (vn_has_cached_data(vp) &&
4329 !(VTOMI(vp)->mi_flags & MI_NOCTO) && !vn_is_readonly(vp))
4330 error = nfs_getattr_otw(vp, &va, cr);
4331 else
4332 error = nfsgetattr(vp, &va, cr);
4333 if (error)
4334 return (error);
4337 * Check to see if the vnode is currently marked as not cachable.
4338 * This means portions of the file are locked (through fop_frlock).
4339 * In this case the map request must be refused. We use
4340 * rp->r_lkserlock to avoid a race with concurrent lock requests.
4342 rp = VTOR(vp);
4345 * Atomically increment r_inmap after acquiring r_rwlock. The
4346 * idea here is to acquire r_rwlock to block read/write and
4347 * not to protect r_inmap. r_inmap will inform nfs_read/write()
4348 * that we are in nfs_map(). Now, r_rwlock is acquired in order
4349 * and we can prevent the deadlock that would have occurred
4350 * when nfs_addmap() would have acquired it out of order.
4352 * Since we are not protecting r_inmap by any lock, we do not
4353 * hold any lock when we decrement it. We atomically decrement
4354 * r_inmap after we release r_lkserlock.
4357 if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, INTR(vp)))
4358 return (EINTR);
4359 atomic_inc_uint(&rp->r_inmap);
4360 nfs_rw_exit(&rp->r_rwlock);
4362 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR(vp))) {
4363 atomic_dec_uint(&rp->r_inmap);
4364 return (EINTR);
4366 if (vp->v_flag & VNOCACHE) {
4367 error = EAGAIN;
4368 goto done;
4372 * Don't allow concurrent locks and mapping if mandatory locking is
4373 * enabled.
4375 if ((flk_has_remote_locks(vp) || lm_has_sleep(vp)) &&
4376 MANDLOCK(vp, va.va_mode)) {
4377 error = EAGAIN;
4378 goto done;
4381 as_rangelock(as);
4382 error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
4383 if (error != 0) {
4384 as_rangeunlock(as);
4385 goto done;
4388 vn_a.vp = vp;
4389 vn_a.offset = off;
4390 vn_a.type = (flags & MAP_TYPE);
4391 vn_a.prot = (uchar_t)prot;
4392 vn_a.maxprot = (uchar_t)maxprot;
4393 vn_a.flags = (flags & ~MAP_TYPE);
4394 vn_a.cred = cr;
4395 vn_a.amp = NULL;
4396 vn_a.szc = 0;
4397 vn_a.lgrp_mem_policy_flags = 0;
4399 error = as_map(as, *addrp, len, segvn_create, &vn_a);
4400 as_rangeunlock(as);
4402 done:
4403 nfs_rw_exit(&rp->r_lkserlock);
4404 atomic_dec_uint(&rp->r_inmap);
4405 return (error);
4408 /* ARGSUSED */
4409 static int
4410 nfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
4411 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
4412 caller_context_t *ct)
4414 rnode_t *rp;
4416 if (vp->v_flag & VNOMAP)
4417 return (ENOSYS);
4418 if (nfs_zone() != VTOMI(vp)->mi_zone)
4419 return (EIO);
4421 rp = VTOR(vp);
4422 atomic_add_long((ulong_t *)&rp->r_mapcnt, btopr(len));
4424 return (0);
4427 /* ARGSUSED */
4428 static int
4429 nfs_frlock(vnode_t *vp, int cmd, struct flock64 *bfp, int flag, offset_t offset,
4430 struct flk_callback *flk_cbp, cred_t *cr, caller_context_t *ct)
4432 netobj lm_fh;
4433 int rc;
4434 uoff_t start, end;
4435 rnode_t *rp;
4436 int error = 0, intr = INTR(vp);
4438 /* check for valid cmd parameter */
4439 if (cmd != F_GETLK && cmd != F_SETLK && cmd != F_SETLKW)
4440 return (EINVAL);
4441 if (nfs_zone() != VTOMI(vp)->mi_zone)
4442 return (EIO);
4444 /* Verify l_type. */
4445 switch (bfp->l_type) {
4446 case F_RDLCK:
4447 if (cmd != F_GETLK && !(flag & FREAD))
4448 return (EBADF);
4449 break;
4450 case F_WRLCK:
4451 if (cmd != F_GETLK && !(flag & FWRITE))
4452 return (EBADF);
4453 break;
4454 case F_UNLCK:
4455 intr = 0;
4456 break;
4458 default:
4459 return (EINVAL);
4462 /* check the validity of the lock range */
4463 if (rc = flk_convert_lock_data(vp, bfp, &start, &end, offset))
4464 return (rc);
4465 if (rc = flk_check_lock_data(start, end, MAXOFF32_T))
4466 return (rc);
4469 * If the filesystem is mounted using local locking, pass the
4470 * request off to the local locking code.
4472 if (VTOMI(vp)->mi_flags & MI_LLOCK) {
4473 if (offset > MAXOFF32_T)
4474 return (EFBIG);
4475 if (cmd == F_SETLK || cmd == F_SETLKW) {
4477 * For complete safety, we should be holding
4478 * r_lkserlock. However, we can't call
4479 * lm_safelock and then fs_frlock while
4480 * holding r_lkserlock, so just invoke
4481 * lm_safelock and expect that this will
4482 * catch enough of the cases.
4484 if (!lm_safelock(vp, bfp, cr))
4485 return (EAGAIN);
4487 return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct));
4490 rp = VTOR(vp);
4493 * Check whether the given lock request can proceed, given the
4494 * current file mappings.
4496 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_WRITER, intr))
4497 return (EINTR);
4498 if (cmd == F_SETLK || cmd == F_SETLKW) {
4499 if (!lm_safelock(vp, bfp, cr)) {
4500 rc = EAGAIN;
4501 goto done;
4506 * Flush the cache after waiting for async I/O to finish. For new
4507 * locks, this is so that the process gets the latest bits from the
4508 * server. For unlocks, this is so that other clients see the
4509 * latest bits once the file has been unlocked. If currently dirty
4510 * pages can't be flushed, then don't allow a lock to be set. But
4511 * allow unlocks to succeed, to avoid having orphan locks on the
4512 * server.
4514 if (cmd != F_GETLK) {
4515 mutex_enter(&rp->r_statelock);
4516 while (rp->r_count > 0) {
4517 if (intr) {
4518 klwp_t *lwp = ttolwp(curthread);
4520 if (lwp != NULL)
4521 lwp->lwp_nostop++;
4522 if (cv_wait_sig(&rp->r_cv, &rp->r_statelock)
4523 == 0) {
4524 if (lwp != NULL)
4525 lwp->lwp_nostop--;
4526 rc = EINTR;
4527 break;
4529 if (lwp != NULL)
4530 lwp->lwp_nostop--;
4531 } else
4532 cv_wait(&rp->r_cv, &rp->r_statelock);
4534 mutex_exit(&rp->r_statelock);
4535 if (rc != 0)
4536 goto done;
4537 error = nfs_putpage(vp, 0, 0, B_INVAL, cr, ct);
4538 if (error) {
4539 if (error == ENOSPC || error == EDQUOT) {
4540 mutex_enter(&rp->r_statelock);
4541 if (!rp->r_error)
4542 rp->r_error = error;
4543 mutex_exit(&rp->r_statelock);
4545 if (bfp->l_type != F_UNLCK) {
4546 rc = ENOLCK;
4547 goto done;
4552 lm_fh.n_len = sizeof (fhandle_t);
4553 lm_fh.n_bytes = (char *)VTOFH(vp);
4556 * Call the lock manager to do the real work of contacting
4557 * the server and obtaining the lock.
4559 rc = lm_frlock(vp, cmd, bfp, flag, offset, cr, &lm_fh, flk_cbp);
4561 if (rc == 0)
4562 nfs_lockcompletion(vp, cmd);
4564 done:
4565 nfs_rw_exit(&rp->r_lkserlock);
4566 return (rc);
4570 * Free storage space associated with the specified vnode. The portion
4571 * to be freed is specified by bfp->l_start and bfp->l_len (already
4572 * normalized to a "whence" of 0).
4574 * This is an experimental facility whose continued existence is not
4575 * guaranteed. Currently, we only support the special case
4576 * of l_len == 0, meaning free to end of file.
4578 /* ARGSUSED */
4579 static int
4580 nfs_space(vnode_t *vp, int cmd, struct flock64 *bfp, int flag,
4581 offset_t offset, cred_t *cr, caller_context_t *ct)
4583 int error;
4585 ASSERT(vp->v_type == VREG);
4586 if (cmd != F_FREESP)
4587 return (EINVAL);
4589 if (offset > MAXOFF32_T)
4590 return (EFBIG);
4592 if ((bfp->l_start > MAXOFF32_T) || (bfp->l_end > MAXOFF32_T) ||
4593 (bfp->l_len > MAXOFF32_T))
4594 return (EFBIG);
4596 if (nfs_zone() != VTOMI(vp)->mi_zone)
4597 return (EIO);
4599 error = convoff(vp, bfp, 0, offset);
4600 if (!error) {
4601 ASSERT(bfp->l_start >= 0);
4602 if (bfp->l_len == 0) {
4603 struct vattr va;
4606 * ftruncate should not change the ctime and
4607 * mtime if we truncate the file to its
4608 * previous size.
4610 va.va_mask = VATTR_SIZE;
4611 error = nfsgetattr(vp, &va, cr);
4612 if (error || va.va_size == bfp->l_start)
4613 return (error);
4614 va.va_mask = VATTR_SIZE;
4615 va.va_size = bfp->l_start;
4616 error = nfssetattr(vp, &va, 0, cr);
4618 if (error == 0 && bfp->l_start == 0)
4619 vnevent_truncate(vp, ct);
4620 } else
4621 error = EINVAL;
4624 return (error);
4627 /* ARGSUSED */
4628 static int
4629 nfs_realvp(vnode_t *vp, vnode_t **vpp, caller_context_t *ct)
4632 return (EINVAL);
4636 * Setup and add an address space callback to do the work of the delmap call.
4637 * The callback will (and must be) deleted in the actual callback function.
4639 * This is done in order to take care of the problem that we have with holding
4640 * the address space's a_lock for a long period of time (e.g. if the NFS server
4641 * is down). Callbacks will be executed in the address space code while the
4642 * a_lock is not held. Holding the address space's a_lock causes things such
4643 * as ps and fork to hang because they are trying to acquire this lock as well.
4645 /* ARGSUSED */
4646 static int
4647 nfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
4648 size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr,
4649 caller_context_t *ct)
4651 int caller_found;
4652 int error;
4653 rnode_t *rp;
4654 nfs_delmap_args_t *dmapp;
4655 nfs_delmapcall_t *delmap_call;
4657 if (vp->v_flag & VNOMAP)
4658 return (ENOSYS);
4660 * A process may not change zones if it has NFS pages mmap'ed
4661 * in, so we can't legitimately get here from the wrong zone.
4663 ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
4665 rp = VTOR(vp);
4668 * The way that the address space of this process deletes its mapping
4669 * of this file is via the following call chains:
4670 * - as_free()->segop_unmap()/segvn_unmap()->fop_delmap()/nfs_delmap()
4671 * - as_unmap()->segop_unmap()/segvn_unmap()->fop_delmap()/nfs_delmap()
4673 * With the use of address space callbacks we are allowed to drop the
4674 * address space lock, a_lock, while executing the NFS operations that
4675 * need to go over the wire. Returning EAGAIN to the caller of this
4676 * function is what drives the execution of the callback that we add
4677 * below. The callback will be executed by the address space code
4678 * after dropping the a_lock. When the callback is finished, since
4679 * we dropped the a_lock, it must be re-acquired and segvn_unmap()
4680 * is called again on the same segment to finish the rest of the work
4681 * that needs to happen during unmapping.
4683 * This action of calling back into the segment driver causes
4684 * nfs_delmap() to get called again, but since the callback was
4685 * already executed at this point, it already did the work and there
4686 * is nothing left for us to do.
4688 * To Summarize:
4689 * - The first time nfs_delmap is called by the current thread is when
4690 * we add the caller associated with this delmap to the delmap caller
4691 * list, add the callback, and return EAGAIN.
4692 * - The second time in this call chain when nfs_delmap is called we
4693 * will find this caller in the delmap caller list and realize there
4694 * is no more work to do thus removing this caller from the list and
4695 * returning the error that was set in the callback execution.
4697 caller_found = nfs_find_and_delete_delmapcall(rp, &error);
4698 if (caller_found) {
4700 * 'error' is from the actual delmap operations. To avoid
4701 * hangs, we need to handle the return of EAGAIN differently
4702 * since this is what drives the callback execution.
4703 * In this case, we don't want to return EAGAIN and do the
4704 * callback execution because there are none to execute.
4706 if (error == EAGAIN)
4707 return (0);
4708 else
4709 return (error);
4712 /* current caller was not in the list */
4713 delmap_call = nfs_init_delmapcall();
4715 mutex_enter(&rp->r_statelock);
4716 list_insert_tail(&rp->r_indelmap, delmap_call);
4717 mutex_exit(&rp->r_statelock);
4719 dmapp = kmem_alloc(sizeof (nfs_delmap_args_t), KM_SLEEP);
4721 dmapp->vp = vp;
4722 dmapp->off = off;
4723 dmapp->addr = addr;
4724 dmapp->len = len;
4725 dmapp->prot = prot;
4726 dmapp->maxprot = maxprot;
4727 dmapp->flags = flags;
4728 dmapp->cr = cr;
4729 dmapp->caller = delmap_call;
4731 error = as_add_callback(as, nfs_delmap_callback, dmapp,
4732 AS_UNMAP_EVENT, addr, len, KM_SLEEP);
4734 return (error ? error : EAGAIN);
4738 * Remove some pages from an mmap'd vnode. Just update the
4739 * count of pages. If doing close-to-open, then flush all
4740 * of the pages associated with this file. Otherwise, start
4741 * an asynchronous page flush to write out any dirty pages.
4742 * This will also associate a credential with the rnode which
4743 * can be used to write the pages.
4745 /* ARGSUSED */
4746 static void
4747 nfs_delmap_callback(struct as *as, void *arg, uint_t event)
4749 int error;
4750 rnode_t *rp;
4751 mntinfo_t *mi;
4752 nfs_delmap_args_t *dmapp = (nfs_delmap_args_t *)arg;
4754 rp = VTOR(dmapp->vp);
4755 mi = VTOMI(dmapp->vp);
4757 atomic_add_long((ulong_t *)&rp->r_mapcnt, -btopr(dmapp->len));
4758 ASSERT(rp->r_mapcnt >= 0);
4761 * Initiate a page flush if there are pages, the file system
4762 * was not mounted readonly, the segment was mapped shared, and
4763 * the pages themselves were writeable.
4765 if (vn_has_cached_data(dmapp->vp) && !vn_is_readonly(dmapp->vp) &&
4766 dmapp->flags == MAP_SHARED && (dmapp->maxprot & PROT_WRITE)) {
4767 mutex_enter(&rp->r_statelock);
4768 rp->r_flags |= RDIRTY;
4769 mutex_exit(&rp->r_statelock);
4771 * If this is a cross-zone access a sync putpage won't work, so
4772 * the best we can do is try an async putpage. That seems
4773 * better than something more draconian such as discarding the
4774 * dirty pages.
4776 if ((mi->mi_flags & MI_NOCTO) ||
4777 nfs_zone() != mi->mi_zone)
4778 error = nfs_putpage(dmapp->vp, dmapp->off, dmapp->len,
4779 B_ASYNC, dmapp->cr, NULL);
4780 else
4781 error = nfs_putpage(dmapp->vp, dmapp->off, dmapp->len,
4782 0, dmapp->cr, NULL);
4783 if (!error) {
4784 mutex_enter(&rp->r_statelock);
4785 error = rp->r_error;
4786 rp->r_error = 0;
4787 mutex_exit(&rp->r_statelock);
4789 } else
4790 error = 0;
4792 if ((rp->r_flags & RDIRECTIO) || (mi->mi_flags & MI_DIRECTIO))
4793 (void) nfs_putpage(dmapp->vp, dmapp->off, dmapp->len,
4794 B_INVAL, dmapp->cr, NULL);
4796 dmapp->caller->error = error;
4797 (void) as_delete_callback(as, arg);
4798 kmem_free(dmapp, sizeof (nfs_delmap_args_t));
4801 /* ARGSUSED */
4802 static int
4803 nfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
4804 caller_context_t *ct)
4806 int error = 0;
4808 if (nfs_zone() != VTOMI(vp)->mi_zone)
4809 return (EIO);
4811 * This looks a little weird because it's written in a general
4812 * manner but we make little use of cases. If cntl() ever gets
4813 * widely used, the outer switch will make more sense.
4816 switch (cmd) {
4819 * Large file spec - need to base answer new query with
4820 * hardcoded constant based on the protocol.
4822 case _PC_FILESIZEBITS:
4823 *valp = 32;
4824 return (0);
4826 case _PC_LINK_MAX:
4827 case _PC_NAME_MAX:
4828 case _PC_PATH_MAX:
4829 case _PC_SYMLINK_MAX:
4830 case _PC_CHOWN_RESTRICTED:
4831 case _PC_NO_TRUNC: {
4832 mntinfo_t *mi;
4833 struct pathcnf *pc;
4835 if ((mi = VTOMI(vp)) == NULL || (pc = mi->mi_pathconf) == NULL)
4836 return (EINVAL);
4837 error = _PC_ISSET(cmd, pc->pc_mask); /* error or bool */
4838 switch (cmd) {
4839 case _PC_LINK_MAX:
4840 *valp = pc->pc_link_max;
4841 break;
4842 case _PC_NAME_MAX:
4843 *valp = pc->pc_name_max;
4844 break;
4845 case _PC_PATH_MAX:
4846 case _PC_SYMLINK_MAX:
4847 *valp = pc->pc_path_max;
4848 break;
4849 case _PC_CHOWN_RESTRICTED:
4851 * if we got here, error is really a boolean which
4852 * indicates whether cmd is set or not.
4854 *valp = error ? 1 : 0; /* see above */
4855 error = 0;
4856 break;
4857 case _PC_NO_TRUNC:
4859 * if we got here, error is really a boolean which
4860 * indicates whether cmd is set or not.
4862 *valp = error ? 1 : 0; /* see above */
4863 error = 0;
4864 break;
4866 return (error ? EINVAL : 0);
4869 case _PC_XATTR_EXISTS:
4870 *valp = 0;
4871 if (vp->v_vfsp->vfs_flag & VFS_XATTR) {
4872 vnode_t *avp;
4873 rnode_t *rp;
4874 mntinfo_t *mi = VTOMI(vp);
4876 if (!(mi->mi_flags & MI_EXTATTR))
4877 return (0);
4879 rp = VTOR(vp);
4880 if (nfs_rw_enter_sig(&rp->r_rwlock, RW_READER,
4881 INTR(vp)))
4882 return (EINTR);
4884 error = nfslookup_dnlc(vp, XATTR_DIR_NAME, &avp, cr);
4885 if (error || avp == NULL)
4886 error = acl_getxattrdir2(vp, &avp, 0, cr, 0);
4888 nfs_rw_exit(&rp->r_rwlock);
4890 if (error == 0 && avp != NULL) {
4891 error = do_xattr_exists_check(avp, valp, cr);
4892 VN_RELE(avp);
4895 return (error ? EINVAL : 0);
4897 case _PC_ACL_ENABLED:
4898 *valp = _ACL_ACLENT_ENABLED;
4899 return (0);
4901 default:
4902 return (EINVAL);
4907 * Called by async thread to do synchronous pageio. Do the i/o, wait
4908 * for it to complete, and cleanup the page list when done.
4910 static int
4911 nfs_sync_pageio(vnode_t *vp, page_t *pp, uoff_t io_off, size_t io_len,
4912 int flags, cred_t *cr)
4914 int error;
4916 ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
4917 error = nfs_rdwrlbn(vp, pp, io_off, io_len, flags, cr);
4918 if (flags & B_READ)
4919 pvn_read_done(pp, (error ? B_ERROR : 0) | flags);
4920 else
4921 pvn_write_done(pp, (error ? B_ERROR : 0) | flags);
4922 return (error);
4925 /* ARGSUSED */
4926 static int
4927 nfs_pageio(vnode_t *vp, page_t *pp, uoff_t io_off, size_t io_len,
4928 int flags, cred_t *cr, caller_context_t *ct)
4930 int error;
4931 rnode_t *rp;
4933 if (pp == NULL)
4934 return (EINVAL);
4936 if (io_off > MAXOFF32_T)
4937 return (EFBIG);
4938 if (nfs_zone() != VTOMI(vp)->mi_zone)
4939 return (EIO);
4940 rp = VTOR(vp);
4941 mutex_enter(&rp->r_statelock);
4942 rp->r_count++;
4943 mutex_exit(&rp->r_statelock);
4945 if (flags & B_ASYNC) {
4946 error = nfs_async_pageio(vp, pp, io_off, io_len, flags, cr,
4947 nfs_sync_pageio);
4948 } else
4949 error = nfs_rdwrlbn(vp, pp, io_off, io_len, flags, cr);
4950 mutex_enter(&rp->r_statelock);
4951 rp->r_count--;
4952 cv_broadcast(&rp->r_cv);
4953 mutex_exit(&rp->r_statelock);
4954 return (error);
4957 /* ARGSUSED */
4958 static int
4959 nfs_setsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr,
4960 caller_context_t *ct)
4962 int error;
4963 mntinfo_t *mi;
4965 mi = VTOMI(vp);
4967 if (nfs_zone() != mi->mi_zone)
4968 return (EIO);
4969 if (mi->mi_flags & MI_ACL) {
4970 error = acl_setacl2(vp, vsecattr, flag, cr);
4971 if (mi->mi_flags & MI_ACL)
4972 return (error);
4975 return (ENOSYS);
4978 /* ARGSUSED */
4979 static int
4980 nfs_getsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr,
4981 caller_context_t *ct)
4983 int error;
4984 mntinfo_t *mi;
4986 mi = VTOMI(vp);
4988 if (nfs_zone() != mi->mi_zone)
4989 return (EIO);
4990 if (mi->mi_flags & MI_ACL) {
4991 error = acl_getacl2(vp, vsecattr, flag, cr);
4992 if (mi->mi_flags & MI_ACL)
4993 return (error);
4996 return (fs_fab_acl(vp, vsecattr, flag, cr, ct));
4999 /* ARGSUSED */
5000 static int
5001 nfs_shrlock(vnode_t *vp, int cmd, struct shrlock *shr, int flag, cred_t *cr,
5002 caller_context_t *ct)
5004 int error;
5005 struct shrlock nshr;
5006 struct nfs_owner nfs_owner;
5007 netobj lm_fh;
5009 if (nfs_zone() != VTOMI(vp)->mi_zone)
5010 return (EIO);
5013 * check for valid cmd parameter
5015 if (cmd != F_SHARE && cmd != F_UNSHARE && cmd != F_HASREMOTELOCKS)
5016 return (EINVAL);
5019 * Check access permissions
5021 if (cmd == F_SHARE &&
5022 (((shr->s_access & F_RDACC) && !(flag & FREAD)) ||
5023 ((shr->s_access & F_WRACC) && !(flag & FWRITE))))
5024 return (EBADF);
5027 * If the filesystem is mounted using local locking, pass the
5028 * request off to the local share code.
5030 if (VTOMI(vp)->mi_flags & MI_LLOCK)
5031 return (fs_shrlock(vp, cmd, shr, flag, cr, ct));
5033 switch (cmd) {
5034 case F_SHARE:
5035 case F_UNSHARE:
5036 lm_fh.n_len = sizeof (fhandle_t);
5037 lm_fh.n_bytes = (char *)VTOFH(vp);
5040 * If passed an owner that is too large to fit in an
5041 * nfs_owner it is likely a recursive call from the
5042 * lock manager client and pass it straight through. If
5043 * it is not a nfs_owner then simply return an error.
5045 if (shr->s_own_len > sizeof (nfs_owner.lowner)) {
5046 if (((struct nfs_owner *)shr->s_owner)->magic !=
5047 NFS_OWNER_MAGIC)
5048 return (EINVAL);
5050 if (error = lm_shrlock(vp, cmd, shr, flag, &lm_fh)) {
5051 error = set_errno(error);
5053 return (error);
5056 * Remote share reservations owner is a combination of
5057 * a magic number, hostname, and the local owner
5059 bzero(&nfs_owner, sizeof (nfs_owner));
5060 nfs_owner.magic = NFS_OWNER_MAGIC;
5061 (void) strncpy(nfs_owner.hname, uts_nodename(),
5062 sizeof (nfs_owner.hname));
5063 bcopy(shr->s_owner, nfs_owner.lowner, shr->s_own_len);
5064 nshr.s_access = shr->s_access;
5065 nshr.s_deny = shr->s_deny;
5066 nshr.s_sysid = 0;
5067 nshr.s_pid = ttoproc(curthread)->p_pid;
5068 nshr.s_own_len = sizeof (nfs_owner);
5069 nshr.s_owner = (caddr_t)&nfs_owner;
5071 if (error = lm_shrlock(vp, cmd, &nshr, flag, &lm_fh)) {
5072 error = set_errno(error);
5075 break;
5077 case F_HASREMOTELOCKS:
5079 * NFS client can't store remote locks itself
5081 shr->s_access = 0;
5082 error = 0;
5083 break;
5085 default:
5086 error = EINVAL;
5087 break;
5090 return (error);