Merge commit 'ea01a15a654b9e1c7b37d958f4d1911882ed7781'
[unleashed.git] / kernel / fs / nfs / nfs_vnops.c
blob664c6b1e58e0deeffdc1a165eb9a9902ef03d7f8
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 = AT_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 = AT_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 if (vap->va_mask ==
1099 (vap->va_mask & (AT_SIZE | AT_FSID | AT_RDEV))) {
1100 mutex_enter(&rp->r_statelock);
1101 if (vap->va_mask | AT_SIZE)
1102 vap->va_size = rp->r_size;
1103 if (vap->va_mask | AT_FSID)
1104 vap->va_fsid = rp->r_attr.va_fsid;
1105 if (vap->va_mask | AT_RDEV)
1106 vap->va_rdev = rp->r_attr.va_rdev;
1107 mutex_exit(&rp->r_statelock);
1108 return (0);
1113 * Only need to flush pages if asking for the mtime
1114 * and if there any dirty pages or any outstanding
1115 * asynchronous (write) requests for this file.
1117 if (vap->va_mask & AT_MTIME) {
1118 if (vn_has_cached_data(vp) &&
1119 ((rp->r_flags & RDIRTY) || rp->r_awcount > 0)) {
1120 mutex_enter(&rp->r_statelock);
1121 rp->r_gcount++;
1122 mutex_exit(&rp->r_statelock);
1123 error = nfs_putpage(vp, 0, 0, 0, cr, ct);
1124 mutex_enter(&rp->r_statelock);
1125 if (error && (error == ENOSPC || error == EDQUOT)) {
1126 if (!rp->r_error)
1127 rp->r_error = error;
1129 if (--rp->r_gcount == 0)
1130 cv_broadcast(&rp->r_cv);
1131 mutex_exit(&rp->r_statelock);
1135 return (nfsgetattr(vp, vap, cr));
1138 /*ARGSUSED4*/
1139 static int
1140 nfs_setattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr,
1141 caller_context_t *ct)
1143 int error;
1144 uint_t mask;
1145 struct vattr va;
1147 mask = vap->va_mask;
1149 if (mask & AT_NOSET)
1150 return (EINVAL);
1152 if ((mask & AT_SIZE) &&
1153 vap->va_type == VREG &&
1154 vap->va_size > MAXOFF32_T)
1155 return (EFBIG);
1157 if (nfs_zone() != VTOMI(vp)->mi_zone)
1158 return (EIO);
1160 va.va_mask = AT_UID | AT_MODE;
1162 error = nfsgetattr(vp, &va, cr);
1163 if (error)
1164 return (error);
1166 error = secpolicy_vnode_setattr(cr, vp, vap, &va, flags, nfs_accessx,
1167 vp);
1169 if (error)
1170 return (error);
1172 error = nfssetattr(vp, vap, flags, cr);
1174 if (error == 0 && (mask & AT_SIZE) && vap->va_size == 0)
1175 vnevent_truncate(vp, ct);
1177 return (error);
1180 static int
1181 nfssetattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr)
1183 int error;
1184 uint_t mask;
1185 struct nfssaargs args;
1186 struct nfsattrstat ns;
1187 int douprintf;
1188 rnode_t *rp;
1189 struct vattr va;
1190 mode_t omode;
1191 mntinfo_t *mi;
1192 vsecattr_t *vsp;
1193 hrtime_t t;
1195 mask = vap->va_mask;
1197 ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
1199 rp = VTOR(vp);
1202 * Only need to flush pages if there are any pages and
1203 * if the file is marked as dirty in some fashion. The
1204 * file must be flushed so that we can accurately
1205 * determine the size of the file and the cached data
1206 * after the SETATTR returns. A file is considered to
1207 * be dirty if it is either marked with RDIRTY, has
1208 * outstanding i/o's active, or is mmap'd. In this
1209 * last case, we can't tell whether there are dirty
1210 * pages, so we flush just to be sure.
1212 if (vn_has_cached_data(vp) &&
1213 ((rp->r_flags & RDIRTY) ||
1214 rp->r_count > 0 ||
1215 rp->r_mapcnt > 0)) {
1216 ASSERT(vp->v_type != VCHR);
1217 error = nfs_putpage(vp, 0, 0, 0, cr, NULL);
1218 if (error && (error == ENOSPC || error == EDQUOT)) {
1219 mutex_enter(&rp->r_statelock);
1220 if (!rp->r_error)
1221 rp->r_error = error;
1222 mutex_exit(&rp->r_statelock);
1227 * If the system call was utime(2) or utimes(2) and the
1228 * application did not specify the times, then set the
1229 * mtime nanosecond field to 1 billion. This will get
1230 * translated from 1 billion nanoseconds to 1 million
1231 * microseconds in the over the wire request. The
1232 * server will use 1 million in the microsecond field
1233 * to tell whether both the mtime and atime should be
1234 * set to the server's current time.
1236 * This is an overload of the protocol and should be
1237 * documented in the NFS Version 2 protocol specification.
1239 if ((mask & AT_MTIME) && !(flags & ATTR_UTIME)) {
1240 vap->va_mtime.tv_nsec = 1000000000;
1241 if (NFS_TIME_T_OK(vap->va_mtime.tv_sec) &&
1242 NFS_TIME_T_OK(vap->va_atime.tv_sec)) {
1243 error = vattr_to_sattr(vap, &args.saa_sa);
1244 } else {
1246 * Use server times. vap time values will not be used.
1247 * To ensure no time overflow, make sure vap has
1248 * valid values, but retain the original values.
1250 timestruc_t mtime = vap->va_mtime;
1251 timestruc_t atime = vap->va_atime;
1252 time_t now;
1254 now = gethrestime_sec();
1255 if (NFS_TIME_T_OK(now)) {
1256 /* Just in case server does not know of this */
1257 vap->va_mtime.tv_sec = now;
1258 vap->va_atime.tv_sec = now;
1259 } else {
1260 vap->va_mtime.tv_sec = 0;
1261 vap->va_atime.tv_sec = 0;
1263 error = vattr_to_sattr(vap, &args.saa_sa);
1264 /* set vap times back on */
1265 vap->va_mtime = mtime;
1266 vap->va_atime = atime;
1268 } else {
1269 /* Either do not set times or use the client specified times */
1270 error = vattr_to_sattr(vap, &args.saa_sa);
1272 if (error) {
1273 /* req time field(s) overflow - return immediately */
1274 return (error);
1276 args.saa_fh = *VTOFH(vp);
1278 va.va_mask = AT_MODE;
1279 error = nfsgetattr(vp, &va, cr);
1280 if (error)
1281 return (error);
1282 omode = va.va_mode;
1284 mi = VTOMI(vp);
1286 douprintf = 1;
1288 t = gethrtime();
1290 error = rfs2call(mi, RFS_SETATTR,
1291 xdr_saargs, (caddr_t)&args,
1292 xdr_attrstat, (caddr_t)&ns, cr,
1293 &douprintf, &ns.ns_status, 0, NULL);
1296 * Purge the access cache and ACL cache if changing either the
1297 * owner of the file, the group owner, or the mode. These may
1298 * change the access permissions of the file, so purge old
1299 * information and start over again.
1301 if ((mask & (AT_UID | AT_GID | AT_MODE)) && (mi->mi_flags & MI_ACL)) {
1302 (void) nfs_access_purge_rp(rp);
1303 if (rp->r_secattr != NULL) {
1304 mutex_enter(&rp->r_statelock);
1305 vsp = rp->r_secattr;
1306 rp->r_secattr = NULL;
1307 mutex_exit(&rp->r_statelock);
1308 if (vsp != NULL)
1309 nfs_acl_free(vsp);
1313 if (!error) {
1314 error = geterrno(ns.ns_status);
1315 if (!error) {
1317 * If changing the size of the file, invalidate
1318 * any local cached data which is no longer part
1319 * of the file. We also possibly invalidate the
1320 * last page in the file. We could use
1321 * pvn_vpzero(), but this would mark the page as
1322 * modified and require it to be written back to
1323 * the server for no particularly good reason.
1324 * This way, if we access it, then we bring it
1325 * back in. A read should be cheaper than a
1326 * write.
1328 if (mask & AT_SIZE) {
1329 nfs_invalidate_pages(vp,
1330 (vap->va_size & PAGEMASK), cr);
1332 (void) nfs_cache_fattr(vp, &ns.ns_attr, &va, t, cr);
1334 * If NFS_ACL is supported on the server, then the
1335 * attributes returned by server may have minimal
1336 * permissions sometimes denying access to users having
1337 * proper access. To get the proper attributes, mark
1338 * the attributes as expired so that they will be
1339 * regotten via the NFS_ACL GETATTR2 procedure.
1341 if (mi->mi_flags & MI_ACL) {
1342 PURGE_ATTRCACHE(vp);
1345 * This next check attempts to deal with NFS
1346 * servers which can not handle increasing
1347 * the size of the file via setattr. Most
1348 * of these servers do not return an error,
1349 * but do not change the size of the file.
1350 * Hence, this check and then attempt to set
1351 * the file size by writing 1 byte at the
1352 * offset of the end of the file that we need.
1354 if ((mask & AT_SIZE) &&
1355 ns.ns_attr.na_size < (uint32_t)vap->va_size) {
1356 char zb = '\0';
1358 error = nfswrite(vp, &zb,
1359 vap->va_size - sizeof (zb),
1360 sizeof (zb), cr);
1363 * Some servers will change the mode to clear the setuid
1364 * and setgid bits when changing the uid or gid. The
1365 * client needs to compensate appropriately.
1367 if (mask & (AT_UID | AT_GID)) {
1368 int terror;
1370 va.va_mask = AT_MODE;
1371 terror = nfsgetattr(vp, &va, cr);
1372 if (!terror &&
1373 (((mask & AT_MODE) &&
1374 va.va_mode != vap->va_mode) ||
1375 (!(mask & AT_MODE) &&
1376 va.va_mode != omode))) {
1377 va.va_mask = AT_MODE;
1378 if (mask & AT_MODE)
1379 va.va_mode = vap->va_mode;
1380 else
1381 va.va_mode = omode;
1382 (void) nfssetattr(vp, &va, 0, cr);
1385 } else {
1386 PURGE_ATTRCACHE(vp);
1387 PURGE_STALE_FH(error, vp, cr);
1389 } else {
1390 PURGE_ATTRCACHE(vp);
1393 return (error);
1396 static int
1397 nfs_accessx(void *vp, int mode, cred_t *cr)
1399 ASSERT(nfs_zone() == VTOMI((vnode_t *)vp)->mi_zone);
1400 return (nfs_access(vp, mode, 0, cr, NULL));
1403 /* ARGSUSED */
1404 static int
1405 nfs_access(vnode_t *vp, int mode, int flags, cred_t *cr, caller_context_t *ct)
1407 struct vattr va;
1408 int error;
1409 mntinfo_t *mi;
1410 int shift = 0;
1412 mi = VTOMI(vp);
1414 if (nfs_zone() != mi->mi_zone)
1415 return (EIO);
1416 if (mi->mi_flags & MI_ACL) {
1417 error = acl_access2(vp, mode, flags, cr);
1418 if (mi->mi_flags & MI_ACL)
1419 return (error);
1422 va.va_mask = AT_MODE | AT_UID | AT_GID;
1423 error = nfsgetattr(vp, &va, cr);
1424 if (error)
1425 return (error);
1428 * Disallow write attempts on read-only
1429 * file systems, unless the file is a
1430 * device node.
1432 if ((mode & VWRITE) && vn_is_readonly(vp) && !IS_DEVVP(vp))
1433 return (EROFS);
1436 * Disallow attempts to access mandatory lock files.
1438 if ((mode & (VWRITE | VREAD | VEXEC)) &&
1439 MANDLOCK(vp, va.va_mode))
1440 return (EACCES);
1443 * Access check is based on only
1444 * one of owner, group, public.
1445 * If not owner, then check group.
1446 * If not a member of the group,
1447 * then check public access.
1449 if (crgetuid(cr) != va.va_uid) {
1450 shift += 3;
1451 if (!groupmember(va.va_gid, cr))
1452 shift += 3;
1455 return (secpolicy_vnode_access2(cr, vp, va.va_uid,
1456 va.va_mode << shift, mode));
1459 static int nfs_do_symlink_cache = 1;
1461 /* ARGSUSED */
1462 static int
1463 nfs_readlink(vnode_t *vp, struct uio *uiop, cred_t *cr, caller_context_t *ct)
1465 int error;
1466 struct nfsrdlnres rl;
1467 rnode_t *rp;
1468 int douprintf;
1469 failinfo_t fi;
1472 * We want to be consistent with UFS semantics so we will return
1473 * EINVAL instead of ENXIO. This violates the XNFS spec and
1474 * the RFC 1094, which are wrong any way. BUGID 1138002.
1476 if (vp->v_type != VLNK)
1477 return (EINVAL);
1479 if (nfs_zone() != VTOMI(vp)->mi_zone)
1480 return (EIO);
1482 rp = VTOR(vp);
1483 if (nfs_do_symlink_cache && rp->r_symlink.contents != NULL) {
1484 error = nfs_validate_caches(vp, cr);
1485 if (error)
1486 return (error);
1487 mutex_enter(&rp->r_statelock);
1488 if (rp->r_symlink.contents != NULL) {
1489 error = uiomove(rp->r_symlink.contents,
1490 rp->r_symlink.len, UIO_READ, uiop);
1491 mutex_exit(&rp->r_statelock);
1492 return (error);
1494 mutex_exit(&rp->r_statelock);
1498 rl.rl_data = kmem_alloc(NFS_MAXPATHLEN, KM_SLEEP);
1500 fi.vp = vp;
1501 fi.fhp = NULL; /* no need to update, filehandle not copied */
1502 fi.copyproc = nfscopyfh;
1503 fi.lookupproc = nfslookup;
1504 fi.xattrdirproc = acl_getxattrdir2;
1506 douprintf = 1;
1508 error = rfs2call(VTOMI(vp), RFS_READLINK,
1509 xdr_readlink, (caddr_t)VTOFH(vp),
1510 xdr_rdlnres, (caddr_t)&rl, cr,
1511 &douprintf, &rl.rl_status, 0, &fi);
1513 if (error) {
1515 kmem_free((void *)rl.rl_data, NFS_MAXPATHLEN);
1516 return (error);
1519 error = geterrno(rl.rl_status);
1520 if (!error) {
1521 error = uiomove(rl.rl_data, (int)rl.rl_count, UIO_READ, uiop);
1522 if (nfs_do_symlink_cache && rp->r_symlink.contents == NULL) {
1523 mutex_enter(&rp->r_statelock);
1524 if (rp->r_symlink.contents == NULL) {
1525 rp->r_symlink.contents = rl.rl_data;
1526 rp->r_symlink.len = (int)rl.rl_count;
1527 rp->r_symlink.size = NFS_MAXPATHLEN;
1528 mutex_exit(&rp->r_statelock);
1529 } else {
1530 mutex_exit(&rp->r_statelock);
1532 kmem_free((void *)rl.rl_data,
1533 NFS_MAXPATHLEN);
1535 } else {
1537 kmem_free((void *)rl.rl_data, NFS_MAXPATHLEN);
1539 } else {
1540 PURGE_STALE_FH(error, vp, cr);
1542 kmem_free((void *)rl.rl_data, NFS_MAXPATHLEN);
1546 * Conform to UFS semantics (see comment above)
1548 return (error == ENXIO ? EINVAL : error);
1552 * Flush local dirty pages to stable storage on the server.
1554 * If FNODSYNC is specified, then there is nothing to do because
1555 * metadata changes are not cached on the client before being
1556 * sent to the server.
1558 /* ARGSUSED */
1559 static int
1560 nfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
1562 int error;
1564 if ((syncflag & FNODSYNC) || IS_SWAPVP(vp))
1565 return (0);
1567 if (nfs_zone() != VTOMI(vp)->mi_zone)
1568 return (EIO);
1570 error = nfs_putpage(vp, 0, 0, 0, cr, ct);
1571 if (!error)
1572 error = VTOR(vp)->r_error;
1573 return (error);
1578 * Weirdness: if the file was removed or the target of a rename
1579 * operation while it was open, it got renamed instead. Here we
1580 * remove the renamed file.
1582 /* ARGSUSED */
1583 static void
1584 nfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
1586 rnode_t *rp;
1588 ASSERT(vp != DNLC_NO_VNODE);
1591 * If this is coming from the wrong zone, we let someone in the right
1592 * zone take care of it asynchronously. We can get here due to
1593 * VN_RELE() being called from pageout() or fsflush(). This call may
1594 * potentially turn into an expensive no-op if, for instance, v_count
1595 * gets incremented in the meantime, but it's still correct.
1597 if (nfs_zone() != VTOMI(vp)->mi_zone) {
1598 nfs_async_inactive(vp, cr, nfs_inactive);
1599 return;
1602 rp = VTOR(vp);
1603 redo:
1604 if (rp->r_unldvp != NULL) {
1606 * Save the vnode pointer for the directory where the
1607 * unlinked-open file got renamed, then set it to NULL
1608 * to prevent another thread from getting here before
1609 * we're done with the remove. While we have the
1610 * statelock, make local copies of the pertinent rnode
1611 * fields. If we weren't to do this in an atomic way, the
1612 * the unl* fields could become inconsistent with respect
1613 * to each other due to a race condition between this
1614 * code and nfs_remove(). See bug report 1034328.
1616 mutex_enter(&rp->r_statelock);
1617 if (rp->r_unldvp != NULL) {
1618 vnode_t *unldvp;
1619 char *unlname;
1620 cred_t *unlcred;
1621 struct nfsdiropargs da;
1622 enum nfsstat status;
1623 int douprintf;
1624 int error;
1626 unldvp = rp->r_unldvp;
1627 rp->r_unldvp = NULL;
1628 unlname = rp->r_unlname;
1629 rp->r_unlname = NULL;
1630 unlcred = rp->r_unlcred;
1631 rp->r_unlcred = NULL;
1632 mutex_exit(&rp->r_statelock);
1635 * If there are any dirty pages left, then flush
1636 * them. This is unfortunate because they just
1637 * may get thrown away during the remove operation,
1638 * but we have to do this for correctness.
1640 if (vn_has_cached_data(vp) &&
1641 ((rp->r_flags & RDIRTY) || rp->r_count > 0)) {
1642 ASSERT(vp->v_type != VCHR);
1643 error = nfs_putpage(vp, 0, 0, 0,
1644 cr, ct);
1645 if (error) {
1646 mutex_enter(&rp->r_statelock);
1647 if (!rp->r_error)
1648 rp->r_error = error;
1649 mutex_exit(&rp->r_statelock);
1654 * Do the remove operation on the renamed file
1656 setdiropargs(&da, unlname, unldvp);
1658 douprintf = 1;
1660 (void) rfs2call(VTOMI(unldvp), RFS_REMOVE,
1661 xdr_diropargs, (caddr_t)&da,
1662 xdr_enum, (caddr_t)&status, unlcred,
1663 &douprintf, &status, 0, NULL);
1665 if (HAVE_RDDIR_CACHE(VTOR(unldvp)))
1666 nfs_purge_rddir_cache(unldvp);
1667 PURGE_ATTRCACHE(unldvp);
1670 * Release stuff held for the remove
1672 VN_RELE(unldvp);
1673 kmem_free(unlname, MAXNAMELEN);
1674 crfree(unlcred);
1675 goto redo;
1677 mutex_exit(&rp->r_statelock);
1680 rp_addfree(rp, cr);
1684 * Remote file system operations having to do with directory manipulation.
1687 /* ARGSUSED */
1688 static int
1689 nfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
1690 int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
1691 int *direntflags, pathname_t *realpnp)
1693 int error;
1694 vnode_t *vp;
1695 vnode_t *avp = NULL;
1696 rnode_t *drp;
1698 if (nfs_zone() != VTOMI(dvp)->mi_zone)
1699 return (EPERM);
1701 drp = VTOR(dvp);
1704 * Are we looking up extended attributes? If so, "dvp" is
1705 * the file or directory for which we want attributes, and
1706 * we need a lookup of the hidden attribute directory
1707 * before we lookup the rest of the path.
1709 if (flags & LOOKUP_XATTR) {
1710 bool_t cflag = ((flags & CREATE_XATTR_DIR) != 0);
1711 mntinfo_t *mi;
1713 mi = VTOMI(dvp);
1714 if (!(mi->mi_flags & MI_EXTATTR))
1715 return (EINVAL);
1717 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR(dvp)))
1718 return (EINTR);
1720 (void) nfslookup_dnlc(dvp, XATTR_DIR_NAME, &avp, cr);
1721 if (avp == NULL)
1722 error = acl_getxattrdir2(dvp, &avp, cflag, cr, 0);
1723 else
1724 error = 0;
1726 nfs_rw_exit(&drp->r_rwlock);
1728 if (error) {
1729 if (mi->mi_flags & MI_EXTATTR)
1730 return (error);
1731 return (EINVAL);
1733 dvp = avp;
1734 drp = VTOR(dvp);
1737 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR(dvp))) {
1738 error = EINTR;
1739 goto out;
1742 error = nfslookup(dvp, nm, vpp, pnp, flags, rdir, cr, 0);
1744 nfs_rw_exit(&drp->r_rwlock);
1747 * If vnode is a device, create special vnode.
1749 if (!error && IS_DEVVP(*vpp)) {
1750 vp = *vpp;
1751 *vpp = specvp(vp, vp->v_rdev, vp->v_type, cr);
1752 VN_RELE(vp);
1755 out:
1756 if (avp != NULL)
1757 VN_RELE(avp);
1759 return (error);
1762 static int nfs_lookup_neg_cache = 1;
1764 #ifdef DEBUG
1765 static int nfs_lookup_dnlc_hits = 0;
1766 static int nfs_lookup_dnlc_misses = 0;
1767 static int nfs_lookup_dnlc_neg_hits = 0;
1768 static int nfs_lookup_dnlc_disappears = 0;
1769 static int nfs_lookup_dnlc_lookups = 0;
1770 #endif
1772 /* ARGSUSED */
1774 nfslookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
1775 int flags, vnode_t *rdir, cred_t *cr, int rfscall_flags)
1777 int error;
1779 ASSERT(nfs_zone() == VTOMI(dvp)->mi_zone);
1782 * If lookup is for "", just return dvp. Don't need
1783 * to send it over the wire, look it up in the dnlc,
1784 * or perform any access checks.
1786 if (*nm == '\0') {
1787 VN_HOLD(dvp);
1788 *vpp = dvp;
1789 return (0);
1793 * Can't do lookups in non-directories.
1795 if (dvp->v_type != VDIR)
1796 return (ENOTDIR);
1799 * If we're called with RFSCALL_SOFT, it's important that
1800 * the only rfscall is one we make directly; if we permit
1801 * an access call because we're looking up "." or validating
1802 * a dnlc hit, we'll deadlock because that rfscall will not
1803 * have the RFSCALL_SOFT set.
1805 if (rfscall_flags & RFSCALL_SOFT)
1806 goto callit;
1809 * If lookup is for ".", just return dvp. Don't need
1810 * to send it over the wire or look it up in the dnlc,
1811 * just need to check access.
1813 if (strcmp(nm, ".") == 0) {
1814 error = nfs_access(dvp, VEXEC, 0, cr, NULL);
1815 if (error)
1816 return (error);
1817 VN_HOLD(dvp);
1818 *vpp = dvp;
1819 return (0);
1823 * Lookup this name in the DNLC. If there was a valid entry,
1824 * then return the results of the lookup.
1826 error = nfslookup_dnlc(dvp, nm, vpp, cr);
1827 if (error || *vpp != NULL)
1828 return (error);
1830 callit:
1831 error = nfslookup_otw(dvp, nm, vpp, cr, rfscall_flags);
1833 return (error);
1836 static int
1837 nfslookup_dnlc(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr)
1839 int error;
1840 vnode_t *vp;
1842 ASSERT(*nm != '\0');
1843 ASSERT(nfs_zone() == VTOMI(dvp)->mi_zone);
1846 * Lookup this name in the DNLC. If successful, then validate
1847 * the caches and then recheck the DNLC. The DNLC is rechecked
1848 * just in case this entry got invalidated during the call
1849 * to nfs_validate_caches.
1851 * An assumption is being made that it is safe to say that a
1852 * file exists which may not on the server. Any operations to
1853 * the server will fail with ESTALE.
1855 #ifdef DEBUG
1856 nfs_lookup_dnlc_lookups++;
1857 #endif
1858 vp = dnlc_lookup(dvp, nm);
1859 if (vp != NULL) {
1860 VN_RELE(vp);
1861 if (vp == DNLC_NO_VNODE && !vn_is_readonly(dvp)) {
1862 PURGE_ATTRCACHE(dvp);
1864 error = nfs_validate_caches(dvp, cr);
1865 if (error)
1866 return (error);
1867 vp = dnlc_lookup(dvp, nm);
1868 if (vp != NULL) {
1869 error = nfs_access(dvp, VEXEC, 0, cr, NULL);
1870 if (error) {
1871 VN_RELE(vp);
1872 return (error);
1874 if (vp == DNLC_NO_VNODE) {
1875 VN_RELE(vp);
1876 #ifdef DEBUG
1877 nfs_lookup_dnlc_neg_hits++;
1878 #endif
1879 return (ENOENT);
1881 *vpp = vp;
1882 #ifdef DEBUG
1883 nfs_lookup_dnlc_hits++;
1884 #endif
1885 return (0);
1887 #ifdef DEBUG
1888 nfs_lookup_dnlc_disappears++;
1889 #endif
1891 #ifdef DEBUG
1892 else
1893 nfs_lookup_dnlc_misses++;
1894 #endif
1896 *vpp = NULL;
1898 return (0);
1901 static int
1902 nfslookup_otw(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr,
1903 int rfscall_flags)
1905 int error;
1906 struct nfsdiropargs da;
1907 struct nfsdiropres dr;
1908 int douprintf;
1909 failinfo_t fi;
1910 hrtime_t t;
1912 ASSERT(*nm != '\0');
1913 ASSERT(dvp->v_type == VDIR);
1914 ASSERT(nfs_zone() == VTOMI(dvp)->mi_zone);
1916 setdiropargs(&da, nm, dvp);
1918 fi.vp = dvp;
1919 fi.fhp = NULL; /* no need to update, filehandle not copied */
1920 fi.copyproc = nfscopyfh;
1921 fi.lookupproc = nfslookup;
1922 fi.xattrdirproc = acl_getxattrdir2;
1924 douprintf = 1;
1926 t = gethrtime();
1928 error = rfs2call(VTOMI(dvp), RFS_LOOKUP,
1929 xdr_diropargs, (caddr_t)&da,
1930 xdr_diropres, (caddr_t)&dr, cr,
1931 &douprintf, &dr.dr_status, rfscall_flags, &fi);
1933 if (!error) {
1934 error = geterrno(dr.dr_status);
1935 if (!error) {
1936 *vpp = makenfsnode(&dr.dr_fhandle, &dr.dr_attr,
1937 dvp->v_vfsp, t, cr, VTOR(dvp)->r_path, nm);
1939 * If NFS_ACL is supported on the server, then the
1940 * attributes returned by server may have minimal
1941 * permissions sometimes denying access to users having
1942 * proper access. To get the proper attributes, mark
1943 * the attributes as expired so that they will be
1944 * regotten via the NFS_ACL GETATTR2 procedure.
1946 if (VTOMI(*vpp)->mi_flags & MI_ACL) {
1947 PURGE_ATTRCACHE(*vpp);
1949 if (!(rfscall_flags & RFSCALL_SOFT))
1950 dnlc_update(dvp, nm, *vpp);
1951 } else {
1952 PURGE_STALE_FH(error, dvp, cr);
1953 if (error == ENOENT && nfs_lookup_neg_cache)
1954 dnlc_enter(dvp, nm, DNLC_NO_VNODE);
1958 return (error);
1961 /* ARGSUSED */
1962 static int
1963 nfs_create(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive,
1964 int mode, vnode_t **vpp, cred_t *cr, int lfaware, caller_context_t *ct,
1965 vsecattr_t *vsecp)
1967 int error;
1968 struct nfscreatargs args;
1969 struct nfsdiropres dr;
1970 int douprintf;
1971 vnode_t *vp;
1972 rnode_t *rp;
1973 struct vattr vattr;
1974 rnode_t *drp;
1975 vnode_t *tempvp;
1976 hrtime_t t;
1978 drp = VTOR(dvp);
1980 if (nfs_zone() != VTOMI(dvp)->mi_zone)
1981 return (EPERM);
1982 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR(dvp)))
1983 return (EINTR);
1986 * We make a copy of the attributes because the caller does not
1987 * expect us to change what va points to.
1989 vattr = *va;
1992 * If the pathname is "", just use dvp. Don't need
1993 * to send it over the wire, look it up in the dnlc,
1994 * or perform any access checks.
1996 if (*nm == '\0') {
1997 error = 0;
1998 VN_HOLD(dvp);
1999 vp = dvp;
2001 * If the pathname is ".", just use dvp. Don't need
2002 * to send it over the wire or look it up in the dnlc,
2003 * just need to check access.
2005 } else if (strcmp(nm, ".") == 0) {
2006 error = nfs_access(dvp, VEXEC, 0, cr, ct);
2007 if (error) {
2008 nfs_rw_exit(&drp->r_rwlock);
2009 return (error);
2011 VN_HOLD(dvp);
2012 vp = dvp;
2014 * We need to go over the wire, just to be sure whether the
2015 * file exists or not. Using the DNLC can be dangerous in
2016 * this case when making a decision regarding existence.
2018 } else {
2019 error = nfslookup_otw(dvp, nm, &vp, cr, 0);
2021 if (!error) {
2022 if (exclusive == EXCL)
2023 error = EEXIST;
2024 else if (vp->v_type == VDIR && (mode & VWRITE))
2025 error = EISDIR;
2026 else {
2028 * If vnode is a device, create special vnode.
2030 if (IS_DEVVP(vp)) {
2031 tempvp = vp;
2032 vp = specvp(vp, vp->v_rdev, vp->v_type, cr);
2033 VN_RELE(tempvp);
2035 if (!(error = fop_access(vp, mode, 0, cr, ct))) {
2036 if ((vattr.va_mask & AT_SIZE) &&
2037 vp->v_type == VREG) {
2038 vattr.va_mask = AT_SIZE;
2039 error = nfssetattr(vp, &vattr, 0, cr);
2041 if (!error) {
2043 * Existing file was truncated;
2044 * emit a create event.
2046 vnevent_create(vp, ct);
2051 nfs_rw_exit(&drp->r_rwlock);
2052 if (error) {
2053 VN_RELE(vp);
2054 } else {
2055 *vpp = vp;
2057 return (error);
2060 ASSERT(vattr.va_mask & AT_TYPE);
2061 if (vattr.va_type == VREG) {
2062 ASSERT(vattr.va_mask & AT_MODE);
2063 if (MANDMODE(vattr.va_mode)) {
2064 nfs_rw_exit(&drp->r_rwlock);
2065 return (EACCES);
2069 dnlc_remove(dvp, nm);
2071 setdiropargs(&args.ca_da, nm, dvp);
2074 * Decide what the group-id of the created file should be.
2075 * Set it in attribute list as advisory...then do a setattr
2076 * if the server didn't get it right the first time.
2078 error = setdirgid(dvp, &vattr.va_gid, cr);
2079 if (error) {
2080 nfs_rw_exit(&drp->r_rwlock);
2081 return (error);
2083 vattr.va_mask |= AT_GID;
2086 * This is a completely gross hack to make mknod
2087 * work over the wire until we can wack the protocol
2089 #define IFCHR 0020000 /* character special */
2090 #define IFBLK 0060000 /* block special */
2091 #define IFSOCK 0140000 /* socket */
2094 * dev_t is uint_t in 5.x and short in 4.x. Both 4.x
2095 * supports 8 bit majors. 5.x supports 14 bit majors. 5.x supports 18
2096 * bits in the minor number where 4.x supports 8 bits. If the 5.x
2097 * minor/major numbers <= 8 bits long, compress the device
2098 * number before sending it. Otherwise, the 4.x server will not
2099 * create the device with the correct device number and nothing can be
2100 * done about this.
2102 if (vattr.va_type == VCHR || vattr.va_type == VBLK) {
2103 dev_t d = vattr.va_rdev;
2104 dev32_t dev32;
2106 if (vattr.va_type == VCHR)
2107 vattr.va_mode |= IFCHR;
2108 else
2109 vattr.va_mode |= IFBLK;
2111 (void) cmpldev(&dev32, d);
2112 if (dev32 & ~((SO4_MAXMAJ << L_BITSMINOR32) | SO4_MAXMIN))
2113 vattr.va_size = (uoff_t)dev32;
2114 else
2115 vattr.va_size = (uoff_t)nfsv2_cmpdev(d);
2117 vattr.va_mask |= AT_MODE|AT_SIZE;
2118 } else if (vattr.va_type == VFIFO) {
2119 vattr.va_mode |= IFCHR; /* xtra kludge for namedpipe */
2120 vattr.va_size = (uoff_t)NFS_FIFO_DEV; /* blech */
2121 vattr.va_mask |= AT_MODE|AT_SIZE;
2122 } else if (vattr.va_type == VSOCK) {
2123 vattr.va_mode |= IFSOCK;
2125 * To avoid triggering bugs in the servers set AT_SIZE
2126 * (all other RFS_CREATE calls set this).
2128 vattr.va_size = 0;
2129 vattr.va_mask |= AT_MODE|AT_SIZE;
2132 args.ca_sa = &args.ca_sa_buf;
2133 error = vattr_to_sattr(&vattr, args.ca_sa);
2134 if (error) {
2135 /* req time field(s) overflow - return immediately */
2136 nfs_rw_exit(&drp->r_rwlock);
2137 return (error);
2140 douprintf = 1;
2142 t = gethrtime();
2144 error = rfs2call(VTOMI(dvp), RFS_CREATE,
2145 xdr_creatargs, (caddr_t)&args,
2146 xdr_diropres, (caddr_t)&dr, cr,
2147 &douprintf, &dr.dr_status, 0, NULL);
2149 PURGE_ATTRCACHE(dvp); /* mod time changed */
2151 if (!error) {
2152 error = geterrno(dr.dr_status);
2153 if (!error) {
2154 if (HAVE_RDDIR_CACHE(drp))
2155 nfs_purge_rddir_cache(dvp);
2156 vp = makenfsnode(&dr.dr_fhandle, &dr.dr_attr,
2157 dvp->v_vfsp, t, cr, NULL, NULL);
2159 * If NFS_ACL is supported on the server, then the
2160 * attributes returned by server may have minimal
2161 * permissions sometimes denying access to users having
2162 * proper access. To get the proper attributes, mark
2163 * the attributes as expired so that they will be
2164 * regotten via the NFS_ACL GETATTR2 procedure.
2166 if (VTOMI(vp)->mi_flags & MI_ACL) {
2167 PURGE_ATTRCACHE(vp);
2169 dnlc_update(dvp, nm, vp);
2170 rp = VTOR(vp);
2171 if (vattr.va_size == 0) {
2172 mutex_enter(&rp->r_statelock);
2173 rp->r_size = 0;
2174 mutex_exit(&rp->r_statelock);
2175 if (vn_has_cached_data(vp)) {
2176 ASSERT(vp->v_type != VCHR);
2177 nfs_invalidate_pages(vp,
2178 0, cr);
2183 * Make sure the gid was set correctly.
2184 * If not, try to set it (but don't lose
2185 * any sleep over it).
2187 if (vattr.va_gid != rp->r_attr.va_gid) {
2188 vattr.va_mask = AT_GID;
2189 (void) nfssetattr(vp, &vattr, 0, cr);
2193 * If vnode is a device create special vnode
2195 if (IS_DEVVP(vp)) {
2196 *vpp = specvp(vp, vp->v_rdev, vp->v_type, cr);
2197 VN_RELE(vp);
2198 } else
2199 *vpp = vp;
2200 } else {
2201 PURGE_STALE_FH(error, dvp, cr);
2205 nfs_rw_exit(&drp->r_rwlock);
2207 return (error);
2211 * Weirdness: if the vnode to be removed is open
2212 * we rename it instead of removing it and nfs_inactive
2213 * will remove the new name.
2215 /* ARGSUSED */
2216 static int
2217 nfs_remove(vnode_t *dvp, char *nm, cred_t *cr, caller_context_t *ct, int flags)
2219 int error;
2220 struct nfsdiropargs da;
2221 enum nfsstat status;
2222 vnode_t *vp;
2223 char *tmpname;
2224 int douprintf;
2225 rnode_t *rp;
2226 rnode_t *drp;
2228 if (nfs_zone() != VTOMI(dvp)->mi_zone)
2229 return (EPERM);
2230 drp = VTOR(dvp);
2231 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR(dvp)))
2232 return (EINTR);
2234 error = nfslookup(dvp, nm, &vp, NULL, 0, NULL, cr, 0);
2235 if (error) {
2236 nfs_rw_exit(&drp->r_rwlock);
2237 return (error);
2240 if (vp->v_type == VDIR && secpolicy_fs_linkdir(cr, dvp->v_vfsp)) {
2241 VN_RELE(vp);
2242 nfs_rw_exit(&drp->r_rwlock);
2243 return (EPERM);
2247 * First just remove the entry from the name cache, as it
2248 * is most likely the only entry for this vp.
2250 dnlc_remove(dvp, nm);
2253 * If the file has a v_count > 1 then there may be more than one
2254 * entry in the name cache due multiple links or an open file,
2255 * but we don't have the real reference count so flush all
2256 * possible entries.
2258 if (vp->v_count > 1)
2259 dnlc_purge_vp(vp);
2262 * Now we have the real reference count on the vnode
2264 rp = VTOR(vp);
2265 mutex_enter(&rp->r_statelock);
2266 if (vp->v_count > 1 &&
2267 (rp->r_unldvp == NULL || strcmp(nm, rp->r_unlname) == 0)) {
2268 mutex_exit(&rp->r_statelock);
2269 tmpname = newname();
2270 error = nfsrename(dvp, nm, dvp, tmpname, cr, ct);
2271 if (error)
2272 kmem_free(tmpname, MAXNAMELEN);
2273 else {
2274 mutex_enter(&rp->r_statelock);
2275 if (rp->r_unldvp == NULL) {
2276 VN_HOLD(dvp);
2277 rp->r_unldvp = dvp;
2278 if (rp->r_unlcred != NULL)
2279 crfree(rp->r_unlcred);
2280 crhold(cr);
2281 rp->r_unlcred = cr;
2282 rp->r_unlname = tmpname;
2283 } else {
2284 kmem_free(rp->r_unlname, MAXNAMELEN);
2285 rp->r_unlname = tmpname;
2287 mutex_exit(&rp->r_statelock);
2289 } else {
2290 mutex_exit(&rp->r_statelock);
2292 * We need to flush any dirty pages which happen to
2293 * be hanging around before removing the file. This
2294 * shouldn't happen very often and mostly on file
2295 * systems mounted "nocto".
2297 if (vn_has_cached_data(vp) &&
2298 ((rp->r_flags & RDIRTY) || rp->r_count > 0)) {
2299 error = nfs_putpage(vp, 0, 0, 0, cr, ct);
2300 if (error && (error == ENOSPC || error == EDQUOT)) {
2301 mutex_enter(&rp->r_statelock);
2302 if (!rp->r_error)
2303 rp->r_error = error;
2304 mutex_exit(&rp->r_statelock);
2308 setdiropargs(&da, nm, dvp);
2310 douprintf = 1;
2312 error = rfs2call(VTOMI(dvp), RFS_REMOVE,
2313 xdr_diropargs, (caddr_t)&da,
2314 xdr_enum, (caddr_t)&status, cr,
2315 &douprintf, &status, 0, NULL);
2318 * The xattr dir may be gone after last attr is removed,
2319 * so flush it from dnlc.
2321 if (dvp->v_flag & V_XATTRDIR)
2322 dnlc_purge_vp(dvp);
2324 PURGE_ATTRCACHE(dvp); /* mod time changed */
2325 PURGE_ATTRCACHE(vp); /* link count changed */
2327 if (!error) {
2328 error = geterrno(status);
2329 if (!error) {
2330 if (HAVE_RDDIR_CACHE(drp))
2331 nfs_purge_rddir_cache(dvp);
2332 } else {
2333 PURGE_STALE_FH(error, dvp, cr);
2338 if (error == 0) {
2339 vnevent_remove(vp, dvp, nm, ct);
2341 VN_RELE(vp);
2343 nfs_rw_exit(&drp->r_rwlock);
2345 return (error);
2348 /* ARGSUSED */
2349 static int
2350 nfs_link(vnode_t *tdvp, vnode_t *svp, char *tnm, cred_t *cr,
2351 caller_context_t *ct, int flags)
2353 int error;
2354 struct nfslinkargs args;
2355 enum nfsstat status;
2356 vnode_t *realvp;
2357 int douprintf;
2358 rnode_t *tdrp;
2360 if (nfs_zone() != VTOMI(tdvp)->mi_zone)
2361 return (EPERM);
2362 if (fop_realvp(svp, &realvp, ct) == 0)
2363 svp = realvp;
2365 args.la_from = VTOFH(svp);
2366 setdiropargs(&args.la_to, tnm, tdvp);
2368 tdrp = VTOR(tdvp);
2369 if (nfs_rw_enter_sig(&tdrp->r_rwlock, RW_WRITER, INTR(tdvp)))
2370 return (EINTR);
2372 dnlc_remove(tdvp, tnm);
2374 douprintf = 1;
2376 error = rfs2call(VTOMI(svp), RFS_LINK,
2377 xdr_linkargs, (caddr_t)&args,
2378 xdr_enum, (caddr_t)&status, cr,
2379 &douprintf, &status, 0, NULL);
2381 PURGE_ATTRCACHE(tdvp); /* mod time changed */
2382 PURGE_ATTRCACHE(svp); /* link count changed */
2384 if (!error) {
2385 error = geterrno(status);
2386 if (!error) {
2387 if (HAVE_RDDIR_CACHE(tdrp))
2388 nfs_purge_rddir_cache(tdvp);
2392 nfs_rw_exit(&tdrp->r_rwlock);
2394 if (!error) {
2396 * Notify the source file of this link operation.
2398 vnevent_link(svp, ct);
2400 return (error);
2403 /* ARGSUSED */
2404 static int
2405 nfs_rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr,
2406 caller_context_t *ct, int flags)
2408 vnode_t *realvp;
2410 if (nfs_zone() != VTOMI(odvp)->mi_zone)
2411 return (EPERM);
2412 if (fop_realvp(ndvp, &realvp, ct) == 0)
2413 ndvp = realvp;
2415 return (nfsrename(odvp, onm, ndvp, nnm, cr, ct));
2419 * nfsrename does the real work of renaming in NFS Version 2.
2421 static int
2422 nfsrename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr,
2423 caller_context_t *ct)
2425 int error;
2426 enum nfsstat status;
2427 struct nfsrnmargs args;
2428 int douprintf;
2429 vnode_t *nvp = NULL;
2430 vnode_t *ovp = NULL;
2431 char *tmpname;
2432 rnode_t *rp;
2433 rnode_t *odrp;
2434 rnode_t *ndrp;
2436 ASSERT(nfs_zone() == VTOMI(odvp)->mi_zone);
2437 if (strcmp(onm, ".") == 0 || strcmp(onm, "..") == 0 ||
2438 strcmp(nnm, ".") == 0 || strcmp(nnm, "..") == 0)
2439 return (EINVAL);
2441 odrp = VTOR(odvp);
2442 ndrp = VTOR(ndvp);
2443 if ((intptr_t)odrp < (intptr_t)ndrp) {
2444 if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR(odvp)))
2445 return (EINTR);
2446 if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR(ndvp))) {
2447 nfs_rw_exit(&odrp->r_rwlock);
2448 return (EINTR);
2450 } else {
2451 if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR(ndvp)))
2452 return (EINTR);
2453 if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR(odvp))) {
2454 nfs_rw_exit(&ndrp->r_rwlock);
2455 return (EINTR);
2460 * Lookup the target file. If it exists, it needs to be
2461 * checked to see whether it is a mount point and whether
2462 * it is active (open).
2464 error = nfslookup(ndvp, nnm, &nvp, NULL, 0, NULL, cr, 0);
2465 if (!error) {
2467 * If this file has been mounted on, then just
2468 * return busy because renaming to it would remove
2469 * the mounted file system from the name space.
2471 if (vn_mountedvfs(nvp) != NULL) {
2472 VN_RELE(nvp);
2473 nfs_rw_exit(&odrp->r_rwlock);
2474 nfs_rw_exit(&ndrp->r_rwlock);
2475 return (EBUSY);
2479 * Purge the name cache of all references to this vnode
2480 * so that we can check the reference count to infer
2481 * whether it is active or not.
2484 * First just remove the entry from the name cache, as it
2485 * is most likely the only entry for this vp.
2487 dnlc_remove(ndvp, nnm);
2489 * If the file has a v_count > 1 then there may be more
2490 * than one entry in the name cache due multiple links
2491 * or an open file, but we don't have the real reference
2492 * count so flush all possible entries.
2494 if (nvp->v_count > 1)
2495 dnlc_purge_vp(nvp);
2498 * If the vnode is active and is not a directory,
2499 * arrange to rename it to a
2500 * temporary file so that it will continue to be
2501 * accessible. This implements the "unlink-open-file"
2502 * semantics for the target of a rename operation.
2503 * Before doing this though, make sure that the
2504 * source and target files are not already the same.
2506 if (nvp->v_count > 1 && nvp->v_type != VDIR) {
2508 * Lookup the source name.
2510 error = nfslookup(odvp, onm, &ovp, NULL, 0, NULL,
2511 cr, 0);
2514 * The source name *should* already exist.
2516 if (error) {
2517 VN_RELE(nvp);
2518 nfs_rw_exit(&odrp->r_rwlock);
2519 nfs_rw_exit(&ndrp->r_rwlock);
2520 return (error);
2524 * Compare the two vnodes. If they are the same,
2525 * just release all held vnodes and return success.
2527 if (ovp == nvp) {
2528 VN_RELE(ovp);
2529 VN_RELE(nvp);
2530 nfs_rw_exit(&odrp->r_rwlock);
2531 nfs_rw_exit(&ndrp->r_rwlock);
2532 return (0);
2536 * Can't mix and match directories and non-
2537 * directories in rename operations. We already
2538 * know that the target is not a directory. If
2539 * the source is a directory, return an error.
2541 if (ovp->v_type == VDIR) {
2542 VN_RELE(ovp);
2543 VN_RELE(nvp);
2544 nfs_rw_exit(&odrp->r_rwlock);
2545 nfs_rw_exit(&ndrp->r_rwlock);
2546 return (ENOTDIR);
2550 * The target file exists, is not the same as
2551 * the source file, and is active. Link it
2552 * to a temporary filename to avoid having
2553 * the server removing the file completely.
2555 tmpname = newname();
2556 error = nfs_link(ndvp, nvp, tmpname, cr, NULL, 0);
2557 if (error == EOPNOTSUPP) {
2558 error = nfs_rename(ndvp, nnm, ndvp, tmpname,
2559 cr, NULL, 0);
2561 if (error) {
2562 kmem_free(tmpname, MAXNAMELEN);
2563 VN_RELE(ovp);
2564 VN_RELE(nvp);
2565 nfs_rw_exit(&odrp->r_rwlock);
2566 nfs_rw_exit(&ndrp->r_rwlock);
2567 return (error);
2569 rp = VTOR(nvp);
2570 mutex_enter(&rp->r_statelock);
2571 if (rp->r_unldvp == NULL) {
2572 VN_HOLD(ndvp);
2573 rp->r_unldvp = ndvp;
2574 if (rp->r_unlcred != NULL)
2575 crfree(rp->r_unlcred);
2576 crhold(cr);
2577 rp->r_unlcred = cr;
2578 rp->r_unlname = tmpname;
2579 } else {
2580 kmem_free(rp->r_unlname, MAXNAMELEN);
2581 rp->r_unlname = tmpname;
2583 mutex_exit(&rp->r_statelock);
2587 if (ovp == NULL) {
2589 * When renaming directories to be a subdirectory of a
2590 * different parent, the dnlc entry for ".." will no
2591 * longer be valid, so it must be removed.
2593 * We do a lookup here to determine whether we are renaming
2594 * a directory and we need to check if we are renaming
2595 * an unlinked file. This might have already been done
2596 * in previous code, so we check ovp == NULL to avoid
2597 * doing it twice.
2600 error = nfslookup(odvp, onm, &ovp, NULL, 0, NULL, cr, 0);
2603 * The source name *should* already exist.
2605 if (error) {
2606 nfs_rw_exit(&odrp->r_rwlock);
2607 nfs_rw_exit(&ndrp->r_rwlock);
2608 if (nvp) {
2609 VN_RELE(nvp);
2611 return (error);
2613 ASSERT(ovp != NULL);
2616 dnlc_remove(odvp, onm);
2617 dnlc_remove(ndvp, nnm);
2619 setdiropargs(&args.rna_from, onm, odvp);
2620 setdiropargs(&args.rna_to, nnm, ndvp);
2622 douprintf = 1;
2624 error = rfs2call(VTOMI(odvp), RFS_RENAME,
2625 xdr_rnmargs, (caddr_t)&args,
2626 xdr_enum, (caddr_t)&status, cr,
2627 &douprintf, &status, 0, NULL);
2629 PURGE_ATTRCACHE(odvp); /* mod time changed */
2630 PURGE_ATTRCACHE(ndvp); /* mod time changed */
2632 if (!error) {
2633 error = geterrno(status);
2634 if (!error) {
2635 if (HAVE_RDDIR_CACHE(odrp))
2636 nfs_purge_rddir_cache(odvp);
2637 if (HAVE_RDDIR_CACHE(ndrp))
2638 nfs_purge_rddir_cache(ndvp);
2640 * when renaming directories to be a subdirectory of a
2641 * different parent, the dnlc entry for ".." will no
2642 * longer be valid, so it must be removed
2644 rp = VTOR(ovp);
2645 if (ndvp != odvp) {
2646 if (ovp->v_type == VDIR) {
2647 dnlc_remove(ovp, "..");
2648 if (HAVE_RDDIR_CACHE(rp))
2649 nfs_purge_rddir_cache(ovp);
2654 * If we are renaming the unlinked file, update the
2655 * r_unldvp and r_unlname as needed.
2657 mutex_enter(&rp->r_statelock);
2658 if (rp->r_unldvp != NULL) {
2659 if (strcmp(rp->r_unlname, onm) == 0) {
2660 (void) strncpy(rp->r_unlname,
2661 nnm, MAXNAMELEN);
2662 rp->r_unlname[MAXNAMELEN - 1] = '\0';
2664 if (ndvp != rp->r_unldvp) {
2665 VN_RELE(rp->r_unldvp);
2666 rp->r_unldvp = ndvp;
2667 VN_HOLD(ndvp);
2671 mutex_exit(&rp->r_statelock);
2672 } else {
2674 * System V defines rename to return EEXIST, not
2675 * ENOTEMPTY if the target directory is not empty.
2676 * Over the wire, the error is NFSERR_ENOTEMPTY
2677 * which geterrno maps to ENOTEMPTY.
2679 if (error == ENOTEMPTY)
2680 error = EEXIST;
2684 if (error == 0) {
2685 if (nvp)
2686 vnevent_rename_dest(nvp, ndvp, nnm, ct);
2688 if (odvp != ndvp)
2689 vnevent_rename_dest_dir(ndvp, ct);
2691 ASSERT(ovp != NULL);
2692 vnevent_rename_src(ovp, odvp, onm, ct);
2695 if (nvp) {
2696 VN_RELE(nvp);
2698 VN_RELE(ovp);
2700 nfs_rw_exit(&odrp->r_rwlock);
2701 nfs_rw_exit(&ndrp->r_rwlock);
2703 return (error);
2706 /* ARGSUSED */
2707 static int
2708 nfs_mkdir(vnode_t *dvp, char *nm, struct vattr *va, vnode_t **vpp, cred_t *cr,
2709 caller_context_t *ct, int flags, vsecattr_t *vsecp)
2711 int error;
2712 struct nfscreatargs args;
2713 struct nfsdiropres dr;
2714 int douprintf;
2715 rnode_t *drp;
2716 hrtime_t t;
2718 if (nfs_zone() != VTOMI(dvp)->mi_zone)
2719 return (EPERM);
2721 setdiropargs(&args.ca_da, nm, dvp);
2724 * Decide what the group-id and set-gid bit of the created directory
2725 * should be. May have to do a setattr to get the gid right.
2727 error = setdirgid(dvp, &va->va_gid, cr);
2728 if (error)
2729 return (error);
2730 error = setdirmode(dvp, &va->va_mode, cr);
2731 if (error)
2732 return (error);
2733 va->va_mask |= AT_MODE|AT_GID;
2735 args.ca_sa = &args.ca_sa_buf;
2736 error = vattr_to_sattr(va, args.ca_sa);
2737 if (error) {
2738 /* req time field(s) overflow - return immediately */
2739 return (error);
2742 drp = VTOR(dvp);
2743 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR(dvp)))
2744 return (EINTR);
2746 dnlc_remove(dvp, nm);
2748 douprintf = 1;
2750 t = gethrtime();
2752 error = rfs2call(VTOMI(dvp), RFS_MKDIR,
2753 xdr_creatargs, (caddr_t)&args,
2754 xdr_diropres, (caddr_t)&dr, cr,
2755 &douprintf, &dr.dr_status, 0, NULL);
2757 PURGE_ATTRCACHE(dvp); /* mod time changed */
2759 if (!error) {
2760 error = geterrno(dr.dr_status);
2761 if (!error) {
2762 if (HAVE_RDDIR_CACHE(drp))
2763 nfs_purge_rddir_cache(dvp);
2765 * The attributes returned by RFS_MKDIR can not
2766 * be depended upon, so mark the attribute cache
2767 * as purged. A subsequent GETATTR will get the
2768 * correct attributes from the server.
2770 *vpp = makenfsnode(&dr.dr_fhandle, &dr.dr_attr,
2771 dvp->v_vfsp, t, cr, NULL, NULL);
2772 PURGE_ATTRCACHE(*vpp);
2773 dnlc_update(dvp, nm, *vpp);
2776 * Make sure the gid was set correctly.
2777 * If not, try to set it (but don't lose
2778 * any sleep over it).
2780 if (va->va_gid != VTOR(*vpp)->r_attr.va_gid) {
2781 va->va_mask = AT_GID;
2782 (void) nfssetattr(*vpp, va, 0, cr);
2784 } else {
2785 PURGE_STALE_FH(error, dvp, cr);
2789 nfs_rw_exit(&drp->r_rwlock);
2791 return (error);
2794 /* ARGSUSED */
2795 static int
2796 nfs_rmdir(vnode_t *dvp, char *nm, vnode_t *cdir, cred_t *cr,
2797 caller_context_t *ct, int flags)
2799 int error;
2800 enum nfsstat status;
2801 struct nfsdiropargs da;
2802 vnode_t *vp;
2803 int douprintf;
2804 rnode_t *drp;
2806 if (nfs_zone() != VTOMI(dvp)->mi_zone)
2807 return (EPERM);
2808 drp = VTOR(dvp);
2809 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR(dvp)))
2810 return (EINTR);
2813 * Attempt to prevent a rmdir(".") from succeeding.
2815 error = nfslookup(dvp, nm, &vp, NULL, 0, NULL, cr, 0);
2816 if (error) {
2817 nfs_rw_exit(&drp->r_rwlock);
2818 return (error);
2821 if (vp == cdir) {
2822 VN_RELE(vp);
2823 nfs_rw_exit(&drp->r_rwlock);
2824 return (EINVAL);
2827 setdiropargs(&da, nm, dvp);
2830 * First just remove the entry from the name cache, as it
2831 * is most likely an entry for this vp.
2833 dnlc_remove(dvp, nm);
2836 * If there vnode reference count is greater than one, then
2837 * there may be additional references in the DNLC which will
2838 * need to be purged. First, trying removing the entry for
2839 * the parent directory and see if that removes the additional
2840 * reference(s). If that doesn't do it, then use dnlc_purge_vp
2841 * to completely remove any references to the directory which
2842 * might still exist in the DNLC.
2844 if (vp->v_count > 1) {
2845 dnlc_remove(vp, "..");
2846 if (vp->v_count > 1)
2847 dnlc_purge_vp(vp);
2850 douprintf = 1;
2852 error = rfs2call(VTOMI(dvp), RFS_RMDIR,
2853 xdr_diropargs, (caddr_t)&da,
2854 xdr_enum, (caddr_t)&status, cr,
2855 &douprintf, &status, 0, NULL);
2857 PURGE_ATTRCACHE(dvp); /* mod time changed */
2859 if (error) {
2860 VN_RELE(vp);
2861 nfs_rw_exit(&drp->r_rwlock);
2862 return (error);
2865 error = geterrno(status);
2866 if (!error) {
2867 if (HAVE_RDDIR_CACHE(drp))
2868 nfs_purge_rddir_cache(dvp);
2869 if (HAVE_RDDIR_CACHE(VTOR(vp)))
2870 nfs_purge_rddir_cache(vp);
2871 } else {
2872 PURGE_STALE_FH(error, dvp, cr);
2874 * System V defines rmdir to return EEXIST, not
2875 * ENOTEMPTY if the directory is not empty. Over
2876 * the wire, the error is NFSERR_ENOTEMPTY which
2877 * geterrno maps to ENOTEMPTY.
2879 if (error == ENOTEMPTY)
2880 error = EEXIST;
2883 if (error == 0) {
2884 vnevent_rmdir(vp, dvp, nm, ct);
2886 VN_RELE(vp);
2888 nfs_rw_exit(&drp->r_rwlock);
2890 return (error);
2893 /* ARGSUSED */
2894 static int
2895 nfs_symlink(vnode_t *dvp, char *lnm, struct vattr *tva, char *tnm, cred_t *cr,
2896 caller_context_t *ct, int flags)
2898 int error;
2899 struct nfsslargs args;
2900 enum nfsstat status;
2901 int douprintf;
2902 rnode_t *drp;
2904 if (nfs_zone() != VTOMI(dvp)->mi_zone)
2905 return (EPERM);
2906 setdiropargs(&args.sla_from, lnm, dvp);
2907 args.sla_sa = &args.sla_sa_buf;
2908 error = vattr_to_sattr(tva, args.sla_sa);
2909 if (error) {
2910 /* req time field(s) overflow - return immediately */
2911 return (error);
2913 args.sla_tnm = tnm;
2915 drp = VTOR(dvp);
2916 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR(dvp)))
2917 return (EINTR);
2919 dnlc_remove(dvp, lnm);
2921 douprintf = 1;
2923 error = rfs2call(VTOMI(dvp), RFS_SYMLINK,
2924 xdr_slargs, (caddr_t)&args,
2925 xdr_enum, (caddr_t)&status, cr,
2926 &douprintf, &status, 0, NULL);
2928 PURGE_ATTRCACHE(dvp); /* mod time changed */
2930 if (!error) {
2931 error = geterrno(status);
2932 if (!error) {
2933 if (HAVE_RDDIR_CACHE(drp))
2934 nfs_purge_rddir_cache(dvp);
2935 } else {
2936 PURGE_STALE_FH(error, dvp, cr);
2940 nfs_rw_exit(&drp->r_rwlock);
2942 return (error);
2945 #ifdef DEBUG
2946 static int nfs_readdir_cache_hits = 0;
2947 static int nfs_readdir_cache_shorts = 0;
2948 static int nfs_readdir_cache_waits = 0;
2949 static int nfs_readdir_cache_misses = 0;
2950 static int nfs_readdir_readahead = 0;
2951 #endif
2953 static int nfs_shrinkreaddir = 0;
2956 * Read directory entries.
2957 * There are some weird things to look out for here. The uio_offset
2958 * field is either 0 or it is the offset returned from a previous
2959 * readdir. It is an opaque value used by the server to find the
2960 * correct directory block to read. The count field is the number
2961 * of blocks to read on the server. This is advisory only, the server
2962 * may return only one block's worth of entries. Entries may be compressed
2963 * on the server.
2965 /* ARGSUSED */
2966 static int
2967 nfs_readdir(vnode_t *vp, struct uio *uiop, cred_t *cr, int *eofp,
2968 caller_context_t *ct, int flags)
2970 int error;
2971 size_t count;
2972 rnode_t *rp;
2973 rddir_cache *rdc;
2974 rddir_cache *nrdc;
2975 rddir_cache *rrdc;
2976 #ifdef DEBUG
2977 int missed;
2978 #endif
2979 rddir_cache srdc;
2980 avl_index_t where;
2982 rp = VTOR(vp);
2984 ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER));
2985 if (nfs_zone() != VTOMI(vp)->mi_zone)
2986 return (EIO);
2988 * Make sure that the directory cache is valid.
2990 if (HAVE_RDDIR_CACHE(rp)) {
2991 if (nfs_disable_rddir_cache) {
2993 * Setting nfs_disable_rddir_cache in /etc/system
2994 * allows interoperability with servers that do not
2995 * properly update the attributes of directories.
2996 * Any cached information gets purged before an
2997 * access is made to it.
2999 nfs_purge_rddir_cache(vp);
3000 } else {
3001 error = nfs_validate_caches(vp, cr);
3002 if (error)
3003 return (error);
3008 * UGLINESS: SunOS 3.2 servers apparently cannot always handle an
3009 * RFS_READDIR request with rda_count set to more than 0x400. So
3010 * we reduce the request size here purely for compatibility.
3012 * In general, this is no longer required. However, if a server
3013 * is discovered which can not handle requests larger than 1024,
3014 * nfs_shrinkreaddir can be set to 1 to enable this backwards
3015 * compatibility.
3017 * In any case, the request size is limited to NFS_MAXDATA bytes.
3019 count = MIN(uiop->uio_iov->iov_len,
3020 nfs_shrinkreaddir ? 0x400 : NFS_MAXDATA);
3022 nrdc = NULL;
3023 #ifdef DEBUG
3024 missed = 0;
3025 #endif
3026 top:
3028 * Short circuit last readdir which always returns 0 bytes.
3029 * This can be done after the directory has been read through
3030 * completely at least once. This will set r_direof which
3031 * can be used to find the value of the last cookie.
3033 mutex_enter(&rp->r_statelock);
3034 if (rp->r_direof != NULL &&
3035 uiop->uio_offset == rp->r_direof->nfs_ncookie) {
3036 mutex_exit(&rp->r_statelock);
3037 #ifdef DEBUG
3038 nfs_readdir_cache_shorts++;
3039 #endif
3040 if (eofp)
3041 *eofp = 1;
3042 if (nrdc != NULL)
3043 rddir_cache_rele(nrdc);
3044 return (0);
3047 * Look for a cache entry. Cache entries are identified
3048 * by the NFS cookie value and the byte count requested.
3050 srdc.nfs_cookie = uiop->uio_offset;
3051 srdc.buflen = count;
3052 rdc = avl_find(&rp->r_dir, &srdc, &where);
3053 if (rdc != NULL) {
3054 rddir_cache_hold(rdc);
3056 * If the cache entry is in the process of being
3057 * filled in, wait until this completes. The
3058 * RDDIRWAIT bit is set to indicate that someone
3059 * is waiting and then the thread currently
3060 * filling the entry is done, it should do a
3061 * cv_broadcast to wakeup all of the threads
3062 * waiting for it to finish.
3064 if (rdc->flags & RDDIR) {
3065 nfs_rw_exit(&rp->r_rwlock);
3066 rdc->flags |= RDDIRWAIT;
3067 #ifdef DEBUG
3068 nfs_readdir_cache_waits++;
3069 #endif
3070 if (!cv_wait_sig(&rdc->cv, &rp->r_statelock)) {
3072 * We got interrupted, probably
3073 * the user typed ^C or an alarm
3074 * fired. We free the new entry
3075 * if we allocated one.
3077 mutex_exit(&rp->r_statelock);
3078 (void) nfs_rw_enter_sig(&rp->r_rwlock,
3079 RW_READER, FALSE);
3080 rddir_cache_rele(rdc);
3081 if (nrdc != NULL)
3082 rddir_cache_rele(nrdc);
3083 return (EINTR);
3085 mutex_exit(&rp->r_statelock);
3086 (void) nfs_rw_enter_sig(&rp->r_rwlock,
3087 RW_READER, FALSE);
3088 rddir_cache_rele(rdc);
3089 goto top;
3092 * Check to see if a readdir is required to
3093 * fill the entry. If so, mark this entry
3094 * as being filled, remove our reference,
3095 * and branch to the code to fill the entry.
3097 if (rdc->flags & RDDIRREQ) {
3098 rdc->flags &= ~RDDIRREQ;
3099 rdc->flags |= RDDIR;
3100 if (nrdc != NULL)
3101 rddir_cache_rele(nrdc);
3102 nrdc = rdc;
3103 mutex_exit(&rp->r_statelock);
3104 goto bottom;
3106 #ifdef DEBUG
3107 if (!missed)
3108 nfs_readdir_cache_hits++;
3109 #endif
3111 * If an error occurred while attempting
3112 * to fill the cache entry, just return it.
3114 if (rdc->error) {
3115 error = rdc->error;
3116 mutex_exit(&rp->r_statelock);
3117 rddir_cache_rele(rdc);
3118 if (nrdc != NULL)
3119 rddir_cache_rele(nrdc);
3120 return (error);
3124 * The cache entry is complete and good,
3125 * copyout the dirent structs to the calling
3126 * thread.
3128 error = uiomove(rdc->entries, rdc->entlen, UIO_READ, uiop);
3131 * If no error occurred during the copyout,
3132 * update the offset in the uio struct to
3133 * contain the value of the next cookie
3134 * and set the eof value appropriately.
3136 if (!error) {
3137 uiop->uio_offset = rdc->nfs_ncookie;
3138 if (eofp)
3139 *eofp = rdc->eof;
3143 * Decide whether to do readahead. Don't if
3144 * have already read to the end of directory.
3146 if (rdc->eof) {
3147 rp->r_direof = rdc;
3148 mutex_exit(&rp->r_statelock);
3149 rddir_cache_rele(rdc);
3150 if (nrdc != NULL)
3151 rddir_cache_rele(nrdc);
3152 return (error);
3156 * Check to see whether we found an entry
3157 * for the readahead. If so, we don't need
3158 * to do anything further, so free the new
3159 * entry if one was allocated. Otherwise,
3160 * allocate a new entry, add it to the cache,
3161 * and then initiate an asynchronous readdir
3162 * operation to fill it.
3164 srdc.nfs_cookie = rdc->nfs_ncookie;
3165 srdc.buflen = count;
3166 rrdc = avl_find(&rp->r_dir, &srdc, &where);
3167 if (rrdc != NULL) {
3168 if (nrdc != NULL)
3169 rddir_cache_rele(nrdc);
3170 } else {
3171 if (nrdc != NULL)
3172 rrdc = nrdc;
3173 else {
3174 rrdc = rddir_cache_alloc(KM_NOSLEEP);
3176 if (rrdc != NULL) {
3177 rrdc->nfs_cookie = rdc->nfs_ncookie;
3178 rrdc->buflen = count;
3179 avl_insert(&rp->r_dir, rrdc, where);
3180 rddir_cache_hold(rrdc);
3181 mutex_exit(&rp->r_statelock);
3182 rddir_cache_rele(rdc);
3183 #ifdef DEBUG
3184 nfs_readdir_readahead++;
3185 #endif
3186 nfs_async_readdir(vp, rrdc, cr, nfsreaddir);
3187 return (error);
3191 mutex_exit(&rp->r_statelock);
3192 rddir_cache_rele(rdc);
3193 return (error);
3197 * Didn't find an entry in the cache. Construct a new empty
3198 * entry and link it into the cache. Other processes attempting
3199 * to access this entry will need to wait until it is filled in.
3201 * Since kmem_alloc may block, another pass through the cache
3202 * will need to be taken to make sure that another process
3203 * hasn't already added an entry to the cache for this request.
3205 if (nrdc == NULL) {
3206 mutex_exit(&rp->r_statelock);
3207 nrdc = rddir_cache_alloc(KM_SLEEP);
3208 nrdc->nfs_cookie = uiop->uio_offset;
3209 nrdc->buflen = count;
3210 goto top;
3214 * Add this entry to the cache.
3216 avl_insert(&rp->r_dir, nrdc, where);
3217 rddir_cache_hold(nrdc);
3218 mutex_exit(&rp->r_statelock);
3220 bottom:
3221 #ifdef DEBUG
3222 missed = 1;
3223 nfs_readdir_cache_misses++;
3224 #endif
3226 * Do the readdir.
3228 error = nfsreaddir(vp, nrdc, cr);
3231 * If this operation failed, just return the error which occurred.
3233 if (error != 0)
3234 return (error);
3237 * Since the RPC operation will have taken sometime and blocked
3238 * this process, another pass through the cache will need to be
3239 * taken to find the correct cache entry. It is possible that
3240 * the correct cache entry will not be there (although one was
3241 * added) because the directory changed during the RPC operation
3242 * and the readdir cache was flushed. In this case, just start
3243 * over. It is hoped that this will not happen too often... :-)
3245 nrdc = NULL;
3246 goto top;
3247 /* NOTREACHED */
3250 static int
3251 nfsreaddir(vnode_t *vp, rddir_cache *rdc, cred_t *cr)
3253 int error;
3254 struct nfsrddirargs rda;
3255 struct nfsrddirres rd;
3256 rnode_t *rp;
3257 mntinfo_t *mi;
3258 uint_t count;
3259 int douprintf;
3260 failinfo_t fi, *fip;
3262 ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
3263 count = rdc->buflen;
3265 rp = VTOR(vp);
3266 mi = VTOMI(vp);
3268 rda.rda_fh = *VTOFH(vp);
3269 rda.rda_offset = rdc->nfs_cookie;
3272 * NFS client failover support
3273 * suppress failover unless we have a zero cookie
3275 if (rdc->nfs_cookie == (off_t)0) {
3276 fi.vp = vp;
3277 fi.fhp = (caddr_t)&rda.rda_fh;
3278 fi.copyproc = nfscopyfh;
3279 fi.lookupproc = nfslookup;
3280 fi.xattrdirproc = acl_getxattrdir2;
3281 fip = &fi;
3282 } else {
3283 fip = NULL;
3286 rd.rd_entries = kmem_alloc(rdc->buflen, KM_SLEEP);
3287 rd.rd_size = count;
3288 rd.rd_offset = rda.rda_offset;
3290 douprintf = 1;
3292 if (mi->mi_io_kstats) {
3293 mutex_enter(&mi->mi_lock);
3294 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
3295 mutex_exit(&mi->mi_lock);
3298 do {
3299 rda.rda_count = MIN(count, mi->mi_curread);
3300 error = rfs2call(mi, RFS_READDIR,
3301 xdr_rddirargs, (caddr_t)&rda,
3302 xdr_getrddirres, (caddr_t)&rd, cr,
3303 &douprintf, &rd.rd_status, 0, fip);
3304 } while (error == ENFS_TRYAGAIN);
3306 if (mi->mi_io_kstats) {
3307 mutex_enter(&mi->mi_lock);
3308 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
3309 mutex_exit(&mi->mi_lock);
3313 * Since we are actually doing a READDIR RPC, we must have
3314 * exclusive access to the cache entry being filled. Thus,
3315 * it is safe to update all fields except for the flags
3316 * field. The r_statelock in the rnode must be held to
3317 * prevent two different threads from simultaneously
3318 * attempting to update the flags field. This can happen
3319 * if we are turning off RDDIR and the other thread is
3320 * trying to set RDDIRWAIT.
3322 ASSERT(rdc->flags & RDDIR);
3323 if (!error) {
3324 error = geterrno(rd.rd_status);
3325 if (!error) {
3326 rdc->nfs_ncookie = rd.rd_offset;
3327 rdc->eof = rd.rd_eof ? 1 : 0;
3328 rdc->entlen = rd.rd_size;
3329 ASSERT(rdc->entlen <= rdc->buflen);
3330 #ifdef DEBUG
3331 rdc->entries = rddir_cache_buf_alloc(rdc->buflen,
3332 KM_SLEEP);
3333 #else
3334 rdc->entries = kmem_alloc(rdc->buflen, KM_SLEEP);
3335 #endif
3336 bcopy(rd.rd_entries, rdc->entries, rdc->entlen);
3337 rdc->error = 0;
3338 if (mi->mi_io_kstats) {
3339 mutex_enter(&mi->mi_lock);
3340 KSTAT_IO_PTR(mi->mi_io_kstats)->reads++;
3341 KSTAT_IO_PTR(mi->mi_io_kstats)->nread +=
3342 rd.rd_size;
3343 mutex_exit(&mi->mi_lock);
3345 } else {
3346 PURGE_STALE_FH(error, vp, cr);
3349 if (error) {
3350 rdc->entries = NULL;
3351 rdc->error = error;
3353 kmem_free(rd.rd_entries, rdc->buflen);
3355 mutex_enter(&rp->r_statelock);
3356 rdc->flags &= ~RDDIR;
3357 if (rdc->flags & RDDIRWAIT) {
3358 rdc->flags &= ~RDDIRWAIT;
3359 cv_broadcast(&rdc->cv);
3361 if (error)
3362 rdc->flags |= RDDIRREQ;
3363 mutex_exit(&rp->r_statelock);
3365 rddir_cache_rele(rdc);
3367 return (error);
3370 #ifdef DEBUG
3371 static int nfs_bio_do_stop = 0;
3372 #endif
3374 static int
3375 nfs_bio(struct buf *bp, cred_t *cr)
3377 rnode_t *rp = VTOR(bp->b_vp);
3378 int count;
3379 int error;
3380 cred_t *cred;
3381 uint_t offset;
3383 DTRACE_IO1(start, struct buf *, bp);
3385 ASSERT(nfs_zone() == VTOMI(bp->b_vp)->mi_zone);
3386 offset = dbtob(bp->b_blkno);
3388 if (bp->b_flags & B_READ) {
3389 mutex_enter(&rp->r_statelock);
3390 if (rp->r_cred != NULL) {
3391 cred = rp->r_cred;
3392 crhold(cred);
3393 } else {
3394 rp->r_cred = cr;
3395 crhold(cr);
3396 cred = cr;
3397 crhold(cred);
3399 mutex_exit(&rp->r_statelock);
3400 read_again:
3401 error = bp->b_error = nfsread(bp->b_vp, bp->b_un.b_addr,
3402 offset, bp->b_bcount, &bp->b_resid, cred);
3404 crfree(cred);
3405 if (!error) {
3406 if (bp->b_resid) {
3408 * Didn't get it all because we hit EOF,
3409 * zero all the memory beyond the EOF.
3411 /* bzero(rdaddr + */
3412 bzero(bp->b_un.b_addr +
3413 bp->b_bcount - bp->b_resid, bp->b_resid);
3415 mutex_enter(&rp->r_statelock);
3416 if (bp->b_resid == bp->b_bcount &&
3417 offset >= rp->r_size) {
3419 * We didn't read anything at all as we are
3420 * past EOF. Return an error indicator back
3421 * but don't destroy the pages (yet).
3423 error = NFS_EOF;
3425 mutex_exit(&rp->r_statelock);
3426 } else if (error == EACCES) {
3427 mutex_enter(&rp->r_statelock);
3428 if (cred != cr) {
3429 if (rp->r_cred != NULL)
3430 crfree(rp->r_cred);
3431 rp->r_cred = cr;
3432 crhold(cr);
3433 cred = cr;
3434 crhold(cred);
3435 mutex_exit(&rp->r_statelock);
3436 goto read_again;
3438 mutex_exit(&rp->r_statelock);
3440 } else {
3441 if (!(rp->r_flags & RSTALE)) {
3442 mutex_enter(&rp->r_statelock);
3443 if (rp->r_cred != NULL) {
3444 cred = rp->r_cred;
3445 crhold(cred);
3446 } else {
3447 rp->r_cred = cr;
3448 crhold(cr);
3449 cred = cr;
3450 crhold(cred);
3452 mutex_exit(&rp->r_statelock);
3453 write_again:
3454 mutex_enter(&rp->r_statelock);
3455 count = MIN(bp->b_bcount, rp->r_size - offset);
3456 mutex_exit(&rp->r_statelock);
3457 if (count < 0)
3458 cmn_err(CE_PANIC, "nfs_bio: write count < 0");
3459 #ifdef DEBUG
3460 if (count == 0) {
3461 zcmn_err(getzoneid(), CE_WARN,
3462 "nfs_bio: zero length write at %d",
3463 offset);
3464 nfs_printfhandle(&rp->r_fh);
3465 if (nfs_bio_do_stop)
3466 debug_enter("nfs_bio");
3468 #endif
3469 error = nfswrite(bp->b_vp, bp->b_un.b_addr, offset,
3470 count, cred);
3471 if (error == EACCES) {
3472 mutex_enter(&rp->r_statelock);
3473 if (cred != cr) {
3474 if (rp->r_cred != NULL)
3475 crfree(rp->r_cred);
3476 rp->r_cred = cr;
3477 crhold(cr);
3478 crfree(cred);
3479 cred = cr;
3480 crhold(cred);
3481 mutex_exit(&rp->r_statelock);
3482 goto write_again;
3484 mutex_exit(&rp->r_statelock);
3486 bp->b_error = error;
3487 if (error && error != EINTR) {
3489 * Don't print EDQUOT errors on the console.
3490 * Don't print asynchronous EACCES errors.
3491 * Don't print EFBIG errors.
3492 * Print all other write errors.
3494 if (error != EDQUOT && error != EFBIG &&
3495 (error != EACCES ||
3496 !(bp->b_flags & B_ASYNC)))
3497 nfs_write_error(bp->b_vp, error, cred);
3499 * Update r_error and r_flags as appropriate.
3500 * If the error was ESTALE, then mark the
3501 * rnode as not being writeable and save
3502 * the error status. Otherwise, save any
3503 * errors which occur from asynchronous
3504 * page invalidations. Any errors occurring
3505 * from other operations should be saved
3506 * by the caller.
3508 mutex_enter(&rp->r_statelock);
3509 if (error == ESTALE) {
3510 rp->r_flags |= RSTALE;
3511 if (!rp->r_error)
3512 rp->r_error = error;
3513 } else if (!rp->r_error &&
3514 (bp->b_flags &
3515 (B_INVAL|B_FORCE|B_ASYNC)) ==
3516 (B_INVAL|B_FORCE|B_ASYNC)) {
3517 rp->r_error = error;
3519 mutex_exit(&rp->r_statelock);
3521 crfree(cred);
3522 } else {
3523 error = rp->r_error;
3525 * A close may have cleared r_error, if so,
3526 * propagate ESTALE error return properly
3528 if (error == 0)
3529 error = ESTALE;
3533 if (error != 0 && error != NFS_EOF)
3534 bp->b_flags |= B_ERROR;
3536 DTRACE_IO1(done, struct buf *, bp);
3538 return (error);
3541 /* ARGSUSED */
3542 static int
3543 nfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
3545 struct nfs_fid *fp;
3546 rnode_t *rp;
3548 rp = VTOR(vp);
3550 if (fidp->fid_len < (sizeof (struct nfs_fid) - sizeof (short))) {
3551 fidp->fid_len = sizeof (struct nfs_fid) - sizeof (short);
3552 return (ENOSPC);
3554 fp = (struct nfs_fid *)fidp;
3555 fp->nf_pad = 0;
3556 fp->nf_len = sizeof (struct nfs_fid) - sizeof (short);
3557 bcopy(rp->r_fh.fh_buf, fp->nf_data, NFS_FHSIZE);
3558 return (0);
3561 /* ARGSUSED2 */
3562 static int
3563 nfs_rwlock(vnode_t *vp, int write_lock, caller_context_t *ctp)
3565 rnode_t *rp = VTOR(vp);
3567 if (!write_lock) {
3568 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE);
3569 return (V_WRITELOCK_FALSE);
3572 if ((rp->r_flags & RDIRECTIO) || (VTOMI(vp)->mi_flags & MI_DIRECTIO)) {
3573 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE);
3574 if (rp->r_mapcnt == 0 && !vn_has_cached_data(vp))
3575 return (V_WRITELOCK_FALSE);
3576 nfs_rw_exit(&rp->r_rwlock);
3579 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, FALSE);
3580 return (V_WRITELOCK_TRUE);
3583 /* ARGSUSED */
3584 static void
3585 nfs_rwunlock(vnode_t *vp, int write_lock, caller_context_t *ctp)
3587 rnode_t *rp = VTOR(vp);
3589 nfs_rw_exit(&rp->r_rwlock);
3592 /* ARGSUSED */
3593 static int
3594 nfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp, caller_context_t *ct)
3598 * Because we stuff the readdir cookie into the offset field
3599 * someone may attempt to do an lseek with the cookie which
3600 * we want to succeed.
3602 if (vp->v_type == VDIR)
3603 return (0);
3604 if (*noffp < 0 || *noffp > MAXOFF32_T)
3605 return (EINVAL);
3606 return (0);
3610 * number of NFS_MAXDATA blocks to read ahead
3611 * optimized for 100 base-T.
3613 static int nfs_nra = 4;
3615 #ifdef DEBUG
3616 static int nfs_lostpage = 0; /* number of times we lost original page */
3617 #endif
3620 * Return all the pages from [off..off+len) in file
3622 /* ARGSUSED */
3623 static int
3624 nfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
3625 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
3626 enum seg_rw rw, cred_t *cr, caller_context_t *ct)
3628 rnode_t *rp;
3629 int error;
3630 mntinfo_t *mi;
3632 if (vp->v_flag & VNOMAP)
3633 return (ENOSYS);
3635 ASSERT(off <= MAXOFF32_T);
3636 if (nfs_zone() != VTOMI(vp)->mi_zone)
3637 return (EIO);
3638 if (protp != NULL)
3639 *protp = PROT_ALL;
3642 * Now valididate that the caches are up to date.
3644 error = nfs_validate_caches(vp, cr);
3645 if (error)
3646 return (error);
3648 rp = VTOR(vp);
3649 mi = VTOMI(vp);
3650 retry:
3651 mutex_enter(&rp->r_statelock);
3654 * Don't create dirty pages faster than they
3655 * can be cleaned so that the system doesn't
3656 * get imbalanced. If the async queue is
3657 * maxed out, then wait for it to drain before
3658 * creating more dirty pages. Also, wait for
3659 * any threads doing pagewalks in the vop_getattr
3660 * entry points so that they don't block for
3661 * long periods.
3663 if (rw == S_CREATE) {
3664 while ((mi->mi_max_threads != 0 &&
3665 rp->r_awcount > 2 * mi->mi_max_threads) ||
3666 rp->r_gcount > 0)
3667 cv_wait(&rp->r_cv, &rp->r_statelock);
3671 * If we are getting called as a side effect of an nfs_write()
3672 * operation the local file size might not be extended yet.
3673 * In this case we want to be able to return pages of zeroes.
3675 if (off + len > rp->r_size + PAGEOFFSET && seg != segkmap) {
3676 mutex_exit(&rp->r_statelock);
3677 return (EFAULT); /* beyond EOF */
3680 mutex_exit(&rp->r_statelock);
3682 error = pvn_getpages(nfs_getapage, vp, off, len, protp, pl, plsz,
3683 seg, addr, rw, cr);
3685 switch (error) {
3686 case NFS_EOF:
3687 nfs_purge_caches(vp, NFS_NOPURGE_DNLC, cr);
3688 goto retry;
3689 case ESTALE:
3690 PURGE_STALE_FH(error, vp, cr);
3693 return (error);
3697 * Called from pvn_getpages to get a particular page.
3699 /* ARGSUSED */
3700 static int
3701 nfs_getapage(vnode_t *vp, uoff_t off, size_t len, uint_t *protp,
3702 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
3703 enum seg_rw rw, cred_t *cr)
3705 rnode_t *rp;
3706 uint_t bsize;
3707 struct buf *bp;
3708 page_t *pp;
3709 uoff_t lbn;
3710 uoff_t io_off;
3711 uoff_t blkoff;
3712 uoff_t rablkoff;
3713 size_t io_len;
3714 uint_t blksize;
3715 int error;
3716 int readahead;
3717 int readahead_issued = 0;
3718 int ra_window; /* readahead window */
3719 page_t *pagefound;
3721 if (nfs_zone() != VTOMI(vp)->mi_zone)
3722 return (EIO);
3723 rp = VTOR(vp);
3724 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE);
3726 reread:
3727 bp = NULL;
3728 pp = NULL;
3729 pagefound = NULL;
3731 if (pl != NULL)
3732 pl[0] = NULL;
3734 error = 0;
3735 lbn = off / bsize;
3736 blkoff = lbn * bsize;
3739 * Queueing up the readahead before doing the synchronous read
3740 * results in a significant increase in read throughput because
3741 * of the increased parallelism between the async threads and
3742 * the process context.
3744 if ((off & ((vp->v_vfsp->vfs_bsize) - 1)) == 0 &&
3745 rw != S_CREATE &&
3746 !(vp->v_flag & VNOCACHE)) {
3747 mutex_enter(&rp->r_statelock);
3750 * Calculate the number of readaheads to do.
3751 * a) No readaheads at offset = 0.
3752 * b) Do maximum(nfs_nra) readaheads when the readahead
3753 * window is closed.
3754 * c) Do readaheads between 1 to (nfs_nra - 1) depending
3755 * upon how far the readahead window is open or close.
3756 * d) No readaheads if rp->r_nextr is not within the scope
3757 * of the readahead window (random i/o).
3760 if (off == 0)
3761 readahead = 0;
3762 else if (blkoff == rp->r_nextr)
3763 readahead = nfs_nra;
3764 else if (rp->r_nextr > blkoff &&
3765 ((ra_window = (rp->r_nextr - blkoff) / bsize)
3766 <= (nfs_nra - 1)))
3767 readahead = nfs_nra - ra_window;
3768 else
3769 readahead = 0;
3771 rablkoff = rp->r_nextr;
3772 while (readahead > 0 && rablkoff + bsize < rp->r_size) {
3773 mutex_exit(&rp->r_statelock);
3774 if (nfs_async_readahead(vp, rablkoff + bsize,
3775 addr + (rablkoff + bsize - off), seg, cr,
3776 nfs_readahead) < 0) {
3777 mutex_enter(&rp->r_statelock);
3778 break;
3780 readahead--;
3781 rablkoff += bsize;
3783 * Indicate that we did a readahead so
3784 * readahead offset is not updated
3785 * by the synchronous read below.
3787 readahead_issued = 1;
3788 mutex_enter(&rp->r_statelock);
3790 * set readahead offset to
3791 * offset of last async readahead
3792 * request.
3794 rp->r_nextr = rablkoff;
3796 mutex_exit(&rp->r_statelock);
3799 again:
3800 if ((pagefound = page_exists(&vp->v_object, off)) == NULL) {
3801 if (pl == NULL) {
3802 (void) nfs_async_readahead(vp, blkoff, addr, seg, cr,
3803 nfs_readahead);
3804 } else if (rw == S_CREATE) {
3806 * Block for this page is not allocated, or the offset
3807 * is beyond the current allocation size, or we're
3808 * allocating a swap slot and the page was not found,
3809 * so allocate it and return a zero page.
3811 if ((pp = page_create_va(&vp->v_object, off,
3812 PAGESIZE, PG_WAIT, seg, addr)) == NULL)
3813 cmn_err(CE_PANIC, "nfs_getapage: page_create");
3814 io_len = PAGESIZE;
3815 mutex_enter(&rp->r_statelock);
3816 rp->r_nextr = off + PAGESIZE;
3817 mutex_exit(&rp->r_statelock);
3818 } else {
3820 * Need to go to server to get a BLOCK, exception to
3821 * that being while reading at offset = 0 or doing
3822 * random i/o, in that case read only a PAGE.
3824 mutex_enter(&rp->r_statelock);
3825 if (blkoff < rp->r_size &&
3826 blkoff + bsize >= rp->r_size) {
3828 * If only a block or less is left in
3829 * the file, read all that is remaining.
3831 if (rp->r_size <= off) {
3833 * Trying to access beyond EOF,
3834 * set up to get at least one page.
3836 blksize = off + PAGESIZE - blkoff;
3837 } else
3838 blksize = rp->r_size - blkoff;
3839 } else if ((off == 0) ||
3840 (off != rp->r_nextr && !readahead_issued)) {
3841 blksize = PAGESIZE;
3842 blkoff = off; /* block = page here */
3843 } else
3844 blksize = bsize;
3845 mutex_exit(&rp->r_statelock);
3847 pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
3848 &io_len, blkoff, blksize, 0);
3851 * Some other thread has entered the page,
3852 * so just use it.
3854 if (pp == NULL)
3855 goto again;
3858 * Now round the request size up to page boundaries.
3859 * This ensures that the entire page will be
3860 * initialized to zeroes if EOF is encountered.
3862 io_len = ptob(btopr(io_len));
3864 bp = pageio_setup(pp, io_len, vp, B_READ);
3865 ASSERT(bp != NULL);
3868 * pageio_setup should have set b_addr to 0. This
3869 * is correct since we want to do I/O on a page
3870 * boundary. bp_mapin will use this addr to calculate
3871 * an offset, and then set b_addr to the kernel virtual
3872 * address it allocated for us.
3874 ASSERT(bp->b_un.b_addr == 0);
3876 bp->b_edev = 0;
3877 bp->b_dev = 0;
3878 bp->b_lblkno = lbtodb(io_off);
3879 bp->b_file = vp;
3880 bp->b_offset = (offset_t)off;
3881 bp_mapin(bp);
3884 * If doing a write beyond what we believe is EOF,
3885 * don't bother trying to read the pages from the
3886 * server, we'll just zero the pages here. We
3887 * don't check that the rw flag is S_WRITE here
3888 * because some implementations may attempt a
3889 * read access to the buffer before copying data.
3891 mutex_enter(&rp->r_statelock);
3892 if (io_off >= rp->r_size && seg == segkmap) {
3893 mutex_exit(&rp->r_statelock);
3894 bzero(bp->b_un.b_addr, io_len);
3895 } else {
3896 mutex_exit(&rp->r_statelock);
3897 error = nfs_bio(bp, cr);
3901 * Unmap the buffer before freeing it.
3903 bp_mapout(bp);
3904 pageio_done(bp);
3906 if (error == NFS_EOF) {
3908 * If doing a write system call just return
3909 * zeroed pages, else user tried to get pages
3910 * beyond EOF, return error. We don't check
3911 * that the rw flag is S_WRITE here because
3912 * some implementations may attempt a read
3913 * access to the buffer before copying data.
3915 if (seg == segkmap)
3916 error = 0;
3917 else
3918 error = EFAULT;
3921 if (!readahead_issued && !error) {
3922 mutex_enter(&rp->r_statelock);
3923 rp->r_nextr = io_off + io_len;
3924 mutex_exit(&rp->r_statelock);
3929 out:
3930 if (pl == NULL)
3931 return (error);
3933 if (error) {
3934 if (pp != NULL)
3935 pvn_read_done(pp, B_ERROR);
3936 return (error);
3939 if (pagefound) {
3940 se_t se = (rw == S_CREATE ? SE_EXCL : SE_SHARED);
3943 * Page exists in the cache, acquire the appropriate lock.
3944 * If this fails, start all over again.
3946 if ((pp = page_lookup(&vp->v_object, off, se)) == NULL) {
3947 #ifdef DEBUG
3948 nfs_lostpage++;
3949 #endif
3950 goto reread;
3952 pl[0] = pp;
3953 pl[1] = NULL;
3954 return (0);
3957 if (pp != NULL)
3958 pvn_plist_init(pp, pl, plsz, off, io_len, rw);
3960 return (error);
3963 static void
3964 nfs_readahead(vnode_t *vp, uoff_t blkoff, caddr_t addr, struct seg *seg,
3965 cred_t *cr)
3967 int error;
3968 page_t *pp;
3969 uoff_t io_off;
3970 size_t io_len;
3971 struct buf *bp;
3972 uint_t bsize, blksize;
3973 rnode_t *rp = VTOR(vp);
3975 ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
3977 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE);
3979 mutex_enter(&rp->r_statelock);
3980 if (blkoff < rp->r_size && blkoff + bsize > rp->r_size) {
3982 * If less than a block left in file read less
3983 * than a block.
3985 blksize = rp->r_size - blkoff;
3986 } else
3987 blksize = bsize;
3988 mutex_exit(&rp->r_statelock);
3990 pp = pvn_read_kluster(vp, blkoff, segkmap, addr,
3991 &io_off, &io_len, blkoff, blksize, 1);
3993 * The isra flag passed to the kluster function is 1, we may have
3994 * gotten a return value of NULL for a variety of reasons (# of free
3995 * pages < minfree, someone entered the page on the vnode etc). In all
3996 * cases, we want to punt on the readahead.
3998 if (pp == NULL)
3999 return;
4002 * Now round the request size up to page boundaries.
4003 * This ensures that the entire page will be
4004 * initialized to zeroes if EOF is encountered.
4006 io_len = ptob(btopr(io_len));
4008 bp = pageio_setup(pp, io_len, vp, B_READ);
4009 ASSERT(bp != NULL);
4012 * pageio_setup should have set b_addr to 0. This is correct since
4013 * we want to do I/O on a page boundary. bp_mapin() will use this addr
4014 * to calculate an offset, and then set b_addr to the kernel virtual
4015 * address it allocated for us.
4017 ASSERT(bp->b_un.b_addr == 0);
4019 bp->b_edev = 0;
4020 bp->b_dev = 0;
4021 bp->b_lblkno = lbtodb(io_off);
4022 bp->b_file = vp;
4023 bp->b_offset = (offset_t)blkoff;
4024 bp_mapin(bp);
4027 * If doing a write beyond what we believe is EOF, don't bother trying
4028 * to read the pages from the server, we'll just zero the pages here.
4029 * We don't check that the rw flag is S_WRITE here because some
4030 * implementations may attempt a read access to the buffer before
4031 * copying data.
4033 mutex_enter(&rp->r_statelock);
4034 if (io_off >= rp->r_size && seg == segkmap) {
4035 mutex_exit(&rp->r_statelock);
4036 bzero(bp->b_un.b_addr, io_len);
4037 error = 0;
4038 } else {
4039 mutex_exit(&rp->r_statelock);
4040 error = nfs_bio(bp, cr);
4041 if (error == NFS_EOF)
4042 error = 0;
4046 * Unmap the buffer before freeing it.
4048 bp_mapout(bp);
4049 pageio_done(bp);
4051 pvn_read_done(pp, error ? B_READ | B_ERROR : B_READ);
4054 * In case of error set readahead offset
4055 * to the lowest offset.
4056 * pvn_read_done() calls VN_DISPOSE to destroy the pages
4058 if (error && rp->r_nextr > io_off) {
4059 mutex_enter(&rp->r_statelock);
4060 if (rp->r_nextr > io_off)
4061 rp->r_nextr = io_off;
4062 mutex_exit(&rp->r_statelock);
4067 * Flags are composed of {B_INVAL, B_FREE, B_DONTNEED, B_FORCE}
4068 * If len == 0, do from off to EOF.
4070 * The normal cases should be len == 0 && off == 0 (entire vp list),
4071 * len == MAXBSIZE (from segmap_release actions), and len == PAGESIZE
4072 * (from pageout).
4074 /* ARGSUSED */
4075 static int
4076 nfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr,
4077 caller_context_t *ct)
4079 int error;
4080 rnode_t *rp;
4082 ASSERT(cr != NULL);
4085 * XXX - Why should this check be made here?
4087 if (vp->v_flag & VNOMAP)
4088 return (ENOSYS);
4090 if (len == 0 && !(flags & B_INVAL) && vn_is_readonly(vp))
4091 return (0);
4093 if (!(flags & B_ASYNC) && nfs_zone() != VTOMI(vp)->mi_zone)
4094 return (EIO);
4095 ASSERT(off <= MAXOFF32_T);
4097 rp = VTOR(vp);
4098 mutex_enter(&rp->r_statelock);
4099 rp->r_count++;
4100 mutex_exit(&rp->r_statelock);
4101 error = nfs_putpages(vp, off, len, flags, cr);
4102 mutex_enter(&rp->r_statelock);
4103 rp->r_count--;
4104 cv_broadcast(&rp->r_cv);
4105 mutex_exit(&rp->r_statelock);
4107 return (error);
4111 * Write out a single page, possibly klustering adjacent dirty pages.
4114 nfs_putapage(vnode_t *vp, page_t *pp, uoff_t *offp, size_t *lenp,
4115 int flags, cred_t *cr)
4117 uoff_t io_off;
4118 uoff_t lbn_off;
4119 uoff_t lbn;
4120 size_t io_len;
4121 uint_t bsize;
4122 int error;
4123 rnode_t *rp;
4125 ASSERT(!vn_is_readonly(vp));
4126 ASSERT(pp != NULL);
4127 ASSERT(cr != NULL);
4128 ASSERT((flags & B_ASYNC) || nfs_zone() == VTOMI(vp)->mi_zone);
4130 rp = VTOR(vp);
4131 ASSERT(rp->r_count > 0);
4133 ASSERT(pp->p_offset <= MAXOFF32_T);
4135 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE);
4136 lbn = pp->p_offset / bsize;
4137 lbn_off = lbn * bsize;
4140 * Find a kluster that fits in one block, or in
4141 * one page if pages are bigger than blocks. If
4142 * there is less file space allocated than a whole
4143 * page, we'll shorten the i/o request below.
4145 pp = pvn_write_kluster(vp, pp, &io_off, &io_len, lbn_off,
4146 roundup(bsize, PAGESIZE), flags);
4149 * pvn_write_kluster shouldn't have returned a page with offset
4150 * behind the original page we were given. Verify that.
4152 ASSERT((pp->p_offset / bsize) >= lbn);
4155 * Now pp will have the list of kept dirty pages marked for
4156 * write back. It will also handle invalidation and freeing
4157 * of pages that are not dirty. Check for page length rounding
4158 * problems.
4160 if (io_off + io_len > lbn_off + bsize) {
4161 ASSERT((io_off + io_len) - (lbn_off + bsize) < PAGESIZE);
4162 io_len = lbn_off + bsize - io_off;
4165 * The RMODINPROGRESS flag makes sure that nfs(3)_bio() sees a
4166 * consistent value of r_size. RMODINPROGRESS is set in writerp().
4167 * When RMODINPROGRESS is set it indicates that a uiomove() is in
4168 * progress and the r_size has not been made consistent with the
4169 * new size of the file. When the uiomove() completes the r_size is
4170 * updated and the RMODINPROGRESS flag is cleared.
4172 * The RMODINPROGRESS flag makes sure that nfs(3)_bio() sees a
4173 * consistent value of r_size. Without this handshaking, it is
4174 * possible that nfs(3)_bio() picks up the old value of r_size
4175 * before the uiomove() in writerp() completes. This will result
4176 * in the write through nfs(3)_bio() being dropped.
4178 * More precisely, there is a window between the time the uiomove()
4179 * completes and the time the r_size is updated. If a fop_putpage()
4180 * operation intervenes in this window, the page will be picked up,
4181 * because it is dirty (it will be unlocked, unless it was
4182 * pagecreate'd). When the page is picked up as dirty, the dirty
4183 * bit is reset (pvn_getdirty()). In nfs(3)write(), r_size is
4184 * checked. This will still be the old size. Therefore the page will
4185 * not be written out. When segmap_release() calls fop_putpage(),
4186 * the page will be found to be clean and the write will be dropped.
4188 if (rp->r_flags & RMODINPROGRESS) {
4189 mutex_enter(&rp->r_statelock);
4190 if ((rp->r_flags & RMODINPROGRESS) &&
4191 rp->r_modaddr + MAXBSIZE > io_off &&
4192 rp->r_modaddr < io_off + io_len) {
4193 page_t *plist;
4195 * A write is in progress for this region of the file.
4196 * If we did not detect RMODINPROGRESS here then this
4197 * path through nfs_putapage() would eventually go to
4198 * nfs(3)_bio() and may not write out all of the data
4199 * in the pages. We end up losing data. So we decide
4200 * to set the modified bit on each page in the page
4201 * list and mark the rnode with RDIRTY. This write
4202 * will be restarted at some later time.
4204 plist = pp;
4205 while (plist != NULL) {
4206 pp = plist;
4207 page_sub(&plist, pp);
4208 hat_setmod(pp);
4209 page_io_unlock(pp);
4210 page_unlock(pp);
4212 rp->r_flags |= RDIRTY;
4213 mutex_exit(&rp->r_statelock);
4214 if (offp)
4215 *offp = io_off;
4216 if (lenp)
4217 *lenp = io_len;
4218 return (0);
4220 mutex_exit(&rp->r_statelock);
4223 if (flags & B_ASYNC) {
4224 error = nfs_async_putapage(vp, pp, io_off, io_len, flags, cr,
4225 nfs_sync_putapage);
4226 } else
4227 error = nfs_sync_putapage(vp, pp, io_off, io_len, flags, cr);
4229 if (offp)
4230 *offp = io_off;
4231 if (lenp)
4232 *lenp = io_len;
4233 return (error);
4236 static int
4237 nfs_sync_putapage(vnode_t *vp, page_t *pp, uoff_t io_off, size_t io_len,
4238 int flags, cred_t *cr)
4240 int error;
4241 rnode_t *rp;
4243 flags |= B_WRITE;
4245 ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
4246 error = nfs_rdwrlbn(vp, pp, io_off, io_len, flags, cr);
4248 rp = VTOR(vp);
4250 if ((error == ENOSPC || error == EDQUOT || error == EACCES) &&
4251 (flags & (B_INVAL|B_FORCE)) != (B_INVAL|B_FORCE)) {
4252 if (!(rp->r_flags & ROUTOFSPACE)) {
4253 mutex_enter(&rp->r_statelock);
4254 rp->r_flags |= ROUTOFSPACE;
4255 mutex_exit(&rp->r_statelock);
4257 flags |= B_ERROR;
4258 pvn_write_done(pp, flags);
4260 * If this was not an async thread, then try again to
4261 * write out the pages, but this time, also destroy
4262 * them whether or not the write is successful. This
4263 * will prevent memory from filling up with these
4264 * pages and destroying them is the only alternative
4265 * if they can't be written out.
4267 * Don't do this if this is an async thread because
4268 * when the pages are unlocked in pvn_write_done,
4269 * some other thread could have come along, locked
4270 * them, and queued for an async thread. It would be
4271 * possible for all of the async threads to be tied
4272 * up waiting to lock the pages again and they would
4273 * all already be locked and waiting for an async
4274 * thread to handle them. Deadlock.
4276 if (!(flags & B_ASYNC)) {
4277 error = nfs_putpage(vp, io_off, io_len,
4278 B_INVAL | B_FORCE, cr, NULL);
4280 } else {
4281 if (error)
4282 flags |= B_ERROR;
4283 else if (rp->r_flags & ROUTOFSPACE) {
4284 mutex_enter(&rp->r_statelock);
4285 rp->r_flags &= ~ROUTOFSPACE;
4286 mutex_exit(&rp->r_statelock);
4288 pvn_write_done(pp, flags);
4291 return (error);
4294 /* ARGSUSED */
4295 static int
4296 nfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
4297 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
4298 caller_context_t *ct)
4300 struct segvn_crargs vn_a;
4301 int error;
4302 rnode_t *rp;
4303 struct vattr va;
4305 if (nfs_zone() != VTOMI(vp)->mi_zone)
4306 return (EIO);
4308 if (vp->v_flag & VNOMAP)
4309 return (ENOSYS);
4311 if (off > MAXOFF32_T)
4312 return (EFBIG);
4314 if (off < 0 || off + len < 0)
4315 return (ENXIO);
4317 if (vp->v_type != VREG)
4318 return (ENODEV);
4321 * If there is cached data and if close-to-open consistency
4322 * checking is not turned off and if the file system is not
4323 * mounted readonly, then force an over the wire getattr.
4324 * Otherwise, just invoke nfsgetattr to get a copy of the
4325 * attributes. The attribute cache will be used unless it
4326 * is timed out and if it is, then an over the wire getattr
4327 * will be issued.
4329 va.va_mask = AT_ALL;
4330 if (vn_has_cached_data(vp) &&
4331 !(VTOMI(vp)->mi_flags & MI_NOCTO) && !vn_is_readonly(vp))
4332 error = nfs_getattr_otw(vp, &va, cr);
4333 else
4334 error = nfsgetattr(vp, &va, cr);
4335 if (error)
4336 return (error);
4339 * Check to see if the vnode is currently marked as not cachable.
4340 * This means portions of the file are locked (through fop_frlock).
4341 * In this case the map request must be refused. We use
4342 * rp->r_lkserlock to avoid a race with concurrent lock requests.
4344 rp = VTOR(vp);
4347 * Atomically increment r_inmap after acquiring r_rwlock. The
4348 * idea here is to acquire r_rwlock to block read/write and
4349 * not to protect r_inmap. r_inmap will inform nfs_read/write()
4350 * that we are in nfs_map(). Now, r_rwlock is acquired in order
4351 * and we can prevent the deadlock that would have occurred
4352 * when nfs_addmap() would have acquired it out of order.
4354 * Since we are not protecting r_inmap by any lock, we do not
4355 * hold any lock when we decrement it. We atomically decrement
4356 * r_inmap after we release r_lkserlock.
4359 if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, INTR(vp)))
4360 return (EINTR);
4361 atomic_inc_uint(&rp->r_inmap);
4362 nfs_rw_exit(&rp->r_rwlock);
4364 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR(vp))) {
4365 atomic_dec_uint(&rp->r_inmap);
4366 return (EINTR);
4368 if (vp->v_flag & VNOCACHE) {
4369 error = EAGAIN;
4370 goto done;
4374 * Don't allow concurrent locks and mapping if mandatory locking is
4375 * enabled.
4377 if ((flk_has_remote_locks(vp) || lm_has_sleep(vp)) &&
4378 MANDLOCK(vp, va.va_mode)) {
4379 error = EAGAIN;
4380 goto done;
4383 as_rangelock(as);
4384 error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
4385 if (error != 0) {
4386 as_rangeunlock(as);
4387 goto done;
4390 vn_a.vp = vp;
4391 vn_a.offset = off;
4392 vn_a.type = (flags & MAP_TYPE);
4393 vn_a.prot = (uchar_t)prot;
4394 vn_a.maxprot = (uchar_t)maxprot;
4395 vn_a.flags = (flags & ~MAP_TYPE);
4396 vn_a.cred = cr;
4397 vn_a.amp = NULL;
4398 vn_a.szc = 0;
4399 vn_a.lgrp_mem_policy_flags = 0;
4401 error = as_map(as, *addrp, len, segvn_create, &vn_a);
4402 as_rangeunlock(as);
4404 done:
4405 nfs_rw_exit(&rp->r_lkserlock);
4406 atomic_dec_uint(&rp->r_inmap);
4407 return (error);
4410 /* ARGSUSED */
4411 static int
4412 nfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
4413 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
4414 caller_context_t *ct)
4416 rnode_t *rp;
4418 if (vp->v_flag & VNOMAP)
4419 return (ENOSYS);
4420 if (nfs_zone() != VTOMI(vp)->mi_zone)
4421 return (EIO);
4423 rp = VTOR(vp);
4424 atomic_add_long((ulong_t *)&rp->r_mapcnt, btopr(len));
4426 return (0);
4429 /* ARGSUSED */
4430 static int
4431 nfs_frlock(vnode_t *vp, int cmd, struct flock64 *bfp, int flag, offset_t offset,
4432 struct flk_callback *flk_cbp, cred_t *cr, caller_context_t *ct)
4434 netobj lm_fh;
4435 int rc;
4436 uoff_t start, end;
4437 rnode_t *rp;
4438 int error = 0, intr = INTR(vp);
4440 /* check for valid cmd parameter */
4441 if (cmd != F_GETLK && cmd != F_SETLK && cmd != F_SETLKW)
4442 return (EINVAL);
4443 if (nfs_zone() != VTOMI(vp)->mi_zone)
4444 return (EIO);
4446 /* Verify l_type. */
4447 switch (bfp->l_type) {
4448 case F_RDLCK:
4449 if (cmd != F_GETLK && !(flag & FREAD))
4450 return (EBADF);
4451 break;
4452 case F_WRLCK:
4453 if (cmd != F_GETLK && !(flag & FWRITE))
4454 return (EBADF);
4455 break;
4456 case F_UNLCK:
4457 intr = 0;
4458 break;
4460 default:
4461 return (EINVAL);
4464 /* check the validity of the lock range */
4465 if (rc = flk_convert_lock_data(vp, bfp, &start, &end, offset))
4466 return (rc);
4467 if (rc = flk_check_lock_data(start, end, MAXOFF32_T))
4468 return (rc);
4471 * If the filesystem is mounted using local locking, pass the
4472 * request off to the local locking code.
4474 if (VTOMI(vp)->mi_flags & MI_LLOCK) {
4475 if (offset > MAXOFF32_T)
4476 return (EFBIG);
4477 if (cmd == F_SETLK || cmd == F_SETLKW) {
4479 * For complete safety, we should be holding
4480 * r_lkserlock. However, we can't call
4481 * lm_safelock and then fs_frlock while
4482 * holding r_lkserlock, so just invoke
4483 * lm_safelock and expect that this will
4484 * catch enough of the cases.
4486 if (!lm_safelock(vp, bfp, cr))
4487 return (EAGAIN);
4489 return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct));
4492 rp = VTOR(vp);
4495 * Check whether the given lock request can proceed, given the
4496 * current file mappings.
4498 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_WRITER, intr))
4499 return (EINTR);
4500 if (cmd == F_SETLK || cmd == F_SETLKW) {
4501 if (!lm_safelock(vp, bfp, cr)) {
4502 rc = EAGAIN;
4503 goto done;
4508 * Flush the cache after waiting for async I/O to finish. For new
4509 * locks, this is so that the process gets the latest bits from the
4510 * server. For unlocks, this is so that other clients see the
4511 * latest bits once the file has been unlocked. If currently dirty
4512 * pages can't be flushed, then don't allow a lock to be set. But
4513 * allow unlocks to succeed, to avoid having orphan locks on the
4514 * server.
4516 if (cmd != F_GETLK) {
4517 mutex_enter(&rp->r_statelock);
4518 while (rp->r_count > 0) {
4519 if (intr) {
4520 klwp_t *lwp = ttolwp(curthread);
4522 if (lwp != NULL)
4523 lwp->lwp_nostop++;
4524 if (cv_wait_sig(&rp->r_cv, &rp->r_statelock)
4525 == 0) {
4526 if (lwp != NULL)
4527 lwp->lwp_nostop--;
4528 rc = EINTR;
4529 break;
4531 if (lwp != NULL)
4532 lwp->lwp_nostop--;
4533 } else
4534 cv_wait(&rp->r_cv, &rp->r_statelock);
4536 mutex_exit(&rp->r_statelock);
4537 if (rc != 0)
4538 goto done;
4539 error = nfs_putpage(vp, 0, 0, B_INVAL, cr, ct);
4540 if (error) {
4541 if (error == ENOSPC || error == EDQUOT) {
4542 mutex_enter(&rp->r_statelock);
4543 if (!rp->r_error)
4544 rp->r_error = error;
4545 mutex_exit(&rp->r_statelock);
4547 if (bfp->l_type != F_UNLCK) {
4548 rc = ENOLCK;
4549 goto done;
4554 lm_fh.n_len = sizeof (fhandle_t);
4555 lm_fh.n_bytes = (char *)VTOFH(vp);
4558 * Call the lock manager to do the real work of contacting
4559 * the server and obtaining the lock.
4561 rc = lm_frlock(vp, cmd, bfp, flag, offset, cr, &lm_fh, flk_cbp);
4563 if (rc == 0)
4564 nfs_lockcompletion(vp, cmd);
4566 done:
4567 nfs_rw_exit(&rp->r_lkserlock);
4568 return (rc);
4572 * Free storage space associated with the specified vnode. The portion
4573 * to be freed is specified by bfp->l_start and bfp->l_len (already
4574 * normalized to a "whence" of 0).
4576 * This is an experimental facility whose continued existence is not
4577 * guaranteed. Currently, we only support the special case
4578 * of l_len == 0, meaning free to end of file.
4580 /* ARGSUSED */
4581 static int
4582 nfs_space(vnode_t *vp, int cmd, struct flock64 *bfp, int flag,
4583 offset_t offset, cred_t *cr, caller_context_t *ct)
4585 int error;
4587 ASSERT(vp->v_type == VREG);
4588 if (cmd != F_FREESP)
4589 return (EINVAL);
4591 if (offset > MAXOFF32_T)
4592 return (EFBIG);
4594 if ((bfp->l_start > MAXOFF32_T) || (bfp->l_end > MAXOFF32_T) ||
4595 (bfp->l_len > MAXOFF32_T))
4596 return (EFBIG);
4598 if (nfs_zone() != VTOMI(vp)->mi_zone)
4599 return (EIO);
4601 error = convoff(vp, bfp, 0, offset);
4602 if (!error) {
4603 ASSERT(bfp->l_start >= 0);
4604 if (bfp->l_len == 0) {
4605 struct vattr va;
4608 * ftruncate should not change the ctime and
4609 * mtime if we truncate the file to its
4610 * previous size.
4612 va.va_mask = AT_SIZE;
4613 error = nfsgetattr(vp, &va, cr);
4614 if (error || va.va_size == bfp->l_start)
4615 return (error);
4616 va.va_mask = AT_SIZE;
4617 va.va_size = bfp->l_start;
4618 error = nfssetattr(vp, &va, 0, cr);
4620 if (error == 0 && bfp->l_start == 0)
4621 vnevent_truncate(vp, ct);
4622 } else
4623 error = EINVAL;
4626 return (error);
4629 /* ARGSUSED */
4630 static int
4631 nfs_realvp(vnode_t *vp, vnode_t **vpp, caller_context_t *ct)
4634 return (EINVAL);
4638 * Setup and add an address space callback to do the work of the delmap call.
4639 * The callback will (and must be) deleted in the actual callback function.
4641 * This is done in order to take care of the problem that we have with holding
4642 * the address space's a_lock for a long period of time (e.g. if the NFS server
4643 * is down). Callbacks will be executed in the address space code while the
4644 * a_lock is not held. Holding the address space's a_lock causes things such
4645 * as ps and fork to hang because they are trying to acquire this lock as well.
4647 /* ARGSUSED */
4648 static int
4649 nfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
4650 size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr,
4651 caller_context_t *ct)
4653 int caller_found;
4654 int error;
4655 rnode_t *rp;
4656 nfs_delmap_args_t *dmapp;
4657 nfs_delmapcall_t *delmap_call;
4659 if (vp->v_flag & VNOMAP)
4660 return (ENOSYS);
4662 * A process may not change zones if it has NFS pages mmap'ed
4663 * in, so we can't legitimately get here from the wrong zone.
4665 ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
4667 rp = VTOR(vp);
4670 * The way that the address space of this process deletes its mapping
4671 * of this file is via the following call chains:
4672 * - as_free()->segop_unmap()/segvn_unmap()->fop_delmap()/nfs_delmap()
4673 * - as_unmap()->segop_unmap()/segvn_unmap()->fop_delmap()/nfs_delmap()
4675 * With the use of address space callbacks we are allowed to drop the
4676 * address space lock, a_lock, while executing the NFS operations that
4677 * need to go over the wire. Returning EAGAIN to the caller of this
4678 * function is what drives the execution of the callback that we add
4679 * below. The callback will be executed by the address space code
4680 * after dropping the a_lock. When the callback is finished, since
4681 * we dropped the a_lock, it must be re-acquired and segvn_unmap()
4682 * is called again on the same segment to finish the rest of the work
4683 * that needs to happen during unmapping.
4685 * This action of calling back into the segment driver causes
4686 * nfs_delmap() to get called again, but since the callback was
4687 * already executed at this point, it already did the work and there
4688 * is nothing left for us to do.
4690 * To Summarize:
4691 * - The first time nfs_delmap is called by the current thread is when
4692 * we add the caller associated with this delmap to the delmap caller
4693 * list, add the callback, and return EAGAIN.
4694 * - The second time in this call chain when nfs_delmap is called we
4695 * will find this caller in the delmap caller list and realize there
4696 * is no more work to do thus removing this caller from the list and
4697 * returning the error that was set in the callback execution.
4699 caller_found = nfs_find_and_delete_delmapcall(rp, &error);
4700 if (caller_found) {
4702 * 'error' is from the actual delmap operations. To avoid
4703 * hangs, we need to handle the return of EAGAIN differently
4704 * since this is what drives the callback execution.
4705 * In this case, we don't want to return EAGAIN and do the
4706 * callback execution because there are none to execute.
4708 if (error == EAGAIN)
4709 return (0);
4710 else
4711 return (error);
4714 /* current caller was not in the list */
4715 delmap_call = nfs_init_delmapcall();
4717 mutex_enter(&rp->r_statelock);
4718 list_insert_tail(&rp->r_indelmap, delmap_call);
4719 mutex_exit(&rp->r_statelock);
4721 dmapp = kmem_alloc(sizeof (nfs_delmap_args_t), KM_SLEEP);
4723 dmapp->vp = vp;
4724 dmapp->off = off;
4725 dmapp->addr = addr;
4726 dmapp->len = len;
4727 dmapp->prot = prot;
4728 dmapp->maxprot = maxprot;
4729 dmapp->flags = flags;
4730 dmapp->cr = cr;
4731 dmapp->caller = delmap_call;
4733 error = as_add_callback(as, nfs_delmap_callback, dmapp,
4734 AS_UNMAP_EVENT, addr, len, KM_SLEEP);
4736 return (error ? error : EAGAIN);
4740 * Remove some pages from an mmap'd vnode. Just update the
4741 * count of pages. If doing close-to-open, then flush all
4742 * of the pages associated with this file. Otherwise, start
4743 * an asynchronous page flush to write out any dirty pages.
4744 * This will also associate a credential with the rnode which
4745 * can be used to write the pages.
4747 /* ARGSUSED */
4748 static void
4749 nfs_delmap_callback(struct as *as, void *arg, uint_t event)
4751 int error;
4752 rnode_t *rp;
4753 mntinfo_t *mi;
4754 nfs_delmap_args_t *dmapp = (nfs_delmap_args_t *)arg;
4756 rp = VTOR(dmapp->vp);
4757 mi = VTOMI(dmapp->vp);
4759 atomic_add_long((ulong_t *)&rp->r_mapcnt, -btopr(dmapp->len));
4760 ASSERT(rp->r_mapcnt >= 0);
4763 * Initiate a page flush if there are pages, the file system
4764 * was not mounted readonly, the segment was mapped shared, and
4765 * the pages themselves were writeable.
4767 if (vn_has_cached_data(dmapp->vp) && !vn_is_readonly(dmapp->vp) &&
4768 dmapp->flags == MAP_SHARED && (dmapp->maxprot & PROT_WRITE)) {
4769 mutex_enter(&rp->r_statelock);
4770 rp->r_flags |= RDIRTY;
4771 mutex_exit(&rp->r_statelock);
4773 * If this is a cross-zone access a sync putpage won't work, so
4774 * the best we can do is try an async putpage. That seems
4775 * better than something more draconian such as discarding the
4776 * dirty pages.
4778 if ((mi->mi_flags & MI_NOCTO) ||
4779 nfs_zone() != mi->mi_zone)
4780 error = nfs_putpage(dmapp->vp, dmapp->off, dmapp->len,
4781 B_ASYNC, dmapp->cr, NULL);
4782 else
4783 error = nfs_putpage(dmapp->vp, dmapp->off, dmapp->len,
4784 0, dmapp->cr, NULL);
4785 if (!error) {
4786 mutex_enter(&rp->r_statelock);
4787 error = rp->r_error;
4788 rp->r_error = 0;
4789 mutex_exit(&rp->r_statelock);
4791 } else
4792 error = 0;
4794 if ((rp->r_flags & RDIRECTIO) || (mi->mi_flags & MI_DIRECTIO))
4795 (void) nfs_putpage(dmapp->vp, dmapp->off, dmapp->len,
4796 B_INVAL, dmapp->cr, NULL);
4798 dmapp->caller->error = error;
4799 (void) as_delete_callback(as, arg);
4800 kmem_free(dmapp, sizeof (nfs_delmap_args_t));
4803 /* ARGSUSED */
4804 static int
4805 nfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
4806 caller_context_t *ct)
4808 int error = 0;
4810 if (nfs_zone() != VTOMI(vp)->mi_zone)
4811 return (EIO);
4813 * This looks a little weird because it's written in a general
4814 * manner but we make little use of cases. If cntl() ever gets
4815 * widely used, the outer switch will make more sense.
4818 switch (cmd) {
4821 * Large file spec - need to base answer new query with
4822 * hardcoded constant based on the protocol.
4824 case _PC_FILESIZEBITS:
4825 *valp = 32;
4826 return (0);
4828 case _PC_LINK_MAX:
4829 case _PC_NAME_MAX:
4830 case _PC_PATH_MAX:
4831 case _PC_SYMLINK_MAX:
4832 case _PC_CHOWN_RESTRICTED:
4833 case _PC_NO_TRUNC: {
4834 mntinfo_t *mi;
4835 struct pathcnf *pc;
4837 if ((mi = VTOMI(vp)) == NULL || (pc = mi->mi_pathconf) == NULL)
4838 return (EINVAL);
4839 error = _PC_ISSET(cmd, pc->pc_mask); /* error or bool */
4840 switch (cmd) {
4841 case _PC_LINK_MAX:
4842 *valp = pc->pc_link_max;
4843 break;
4844 case _PC_NAME_MAX:
4845 *valp = pc->pc_name_max;
4846 break;
4847 case _PC_PATH_MAX:
4848 case _PC_SYMLINK_MAX:
4849 *valp = pc->pc_path_max;
4850 break;
4851 case _PC_CHOWN_RESTRICTED:
4853 * if we got here, error is really a boolean which
4854 * indicates whether cmd is set or not.
4856 *valp = error ? 1 : 0; /* see above */
4857 error = 0;
4858 break;
4859 case _PC_NO_TRUNC:
4861 * if we got here, error is really a boolean which
4862 * indicates whether cmd is set or not.
4864 *valp = error ? 1 : 0; /* see above */
4865 error = 0;
4866 break;
4868 return (error ? EINVAL : 0);
4871 case _PC_XATTR_EXISTS:
4872 *valp = 0;
4873 if (vp->v_vfsp->vfs_flag & VFS_XATTR) {
4874 vnode_t *avp;
4875 rnode_t *rp;
4876 mntinfo_t *mi = VTOMI(vp);
4878 if (!(mi->mi_flags & MI_EXTATTR))
4879 return (0);
4881 rp = VTOR(vp);
4882 if (nfs_rw_enter_sig(&rp->r_rwlock, RW_READER,
4883 INTR(vp)))
4884 return (EINTR);
4886 error = nfslookup_dnlc(vp, XATTR_DIR_NAME, &avp, cr);
4887 if (error || avp == NULL)
4888 error = acl_getxattrdir2(vp, &avp, 0, cr, 0);
4890 nfs_rw_exit(&rp->r_rwlock);
4892 if (error == 0 && avp != NULL) {
4893 error = do_xattr_exists_check(avp, valp, cr);
4894 VN_RELE(avp);
4897 return (error ? EINVAL : 0);
4899 case _PC_ACL_ENABLED:
4900 *valp = _ACL_ACLENT_ENABLED;
4901 return (0);
4903 default:
4904 return (EINVAL);
4909 * Called by async thread to do synchronous pageio. Do the i/o, wait
4910 * for it to complete, and cleanup the page list when done.
4912 static int
4913 nfs_sync_pageio(vnode_t *vp, page_t *pp, uoff_t io_off, size_t io_len,
4914 int flags, cred_t *cr)
4916 int error;
4918 ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
4919 error = nfs_rdwrlbn(vp, pp, io_off, io_len, flags, cr);
4920 if (flags & B_READ)
4921 pvn_read_done(pp, (error ? B_ERROR : 0) | flags);
4922 else
4923 pvn_write_done(pp, (error ? B_ERROR : 0) | flags);
4924 return (error);
4927 /* ARGSUSED */
4928 static int
4929 nfs_pageio(vnode_t *vp, page_t *pp, uoff_t io_off, size_t io_len,
4930 int flags, cred_t *cr, caller_context_t *ct)
4932 int error;
4933 rnode_t *rp;
4935 if (pp == NULL)
4936 return (EINVAL);
4938 if (io_off > MAXOFF32_T)
4939 return (EFBIG);
4940 if (nfs_zone() != VTOMI(vp)->mi_zone)
4941 return (EIO);
4942 rp = VTOR(vp);
4943 mutex_enter(&rp->r_statelock);
4944 rp->r_count++;
4945 mutex_exit(&rp->r_statelock);
4947 if (flags & B_ASYNC) {
4948 error = nfs_async_pageio(vp, pp, io_off, io_len, flags, cr,
4949 nfs_sync_pageio);
4950 } else
4951 error = nfs_rdwrlbn(vp, pp, io_off, io_len, flags, cr);
4952 mutex_enter(&rp->r_statelock);
4953 rp->r_count--;
4954 cv_broadcast(&rp->r_cv);
4955 mutex_exit(&rp->r_statelock);
4956 return (error);
4959 /* ARGSUSED */
4960 static int
4961 nfs_setsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr,
4962 caller_context_t *ct)
4964 int error;
4965 mntinfo_t *mi;
4967 mi = VTOMI(vp);
4969 if (nfs_zone() != mi->mi_zone)
4970 return (EIO);
4971 if (mi->mi_flags & MI_ACL) {
4972 error = acl_setacl2(vp, vsecattr, flag, cr);
4973 if (mi->mi_flags & MI_ACL)
4974 return (error);
4977 return (ENOSYS);
4980 /* ARGSUSED */
4981 static int
4982 nfs_getsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr,
4983 caller_context_t *ct)
4985 int error;
4986 mntinfo_t *mi;
4988 mi = VTOMI(vp);
4990 if (nfs_zone() != mi->mi_zone)
4991 return (EIO);
4992 if (mi->mi_flags & MI_ACL) {
4993 error = acl_getacl2(vp, vsecattr, flag, cr);
4994 if (mi->mi_flags & MI_ACL)
4995 return (error);
4998 return (fs_fab_acl(vp, vsecattr, flag, cr, ct));
5001 /* ARGSUSED */
5002 static int
5003 nfs_shrlock(vnode_t *vp, int cmd, struct shrlock *shr, int flag, cred_t *cr,
5004 caller_context_t *ct)
5006 int error;
5007 struct shrlock nshr;
5008 struct nfs_owner nfs_owner;
5009 netobj lm_fh;
5011 if (nfs_zone() != VTOMI(vp)->mi_zone)
5012 return (EIO);
5015 * check for valid cmd parameter
5017 if (cmd != F_SHARE && cmd != F_UNSHARE && cmd != F_HASREMOTELOCKS)
5018 return (EINVAL);
5021 * Check access permissions
5023 if (cmd == F_SHARE &&
5024 (((shr->s_access & F_RDACC) && !(flag & FREAD)) ||
5025 ((shr->s_access & F_WRACC) && !(flag & FWRITE))))
5026 return (EBADF);
5029 * If the filesystem is mounted using local locking, pass the
5030 * request off to the local share code.
5032 if (VTOMI(vp)->mi_flags & MI_LLOCK)
5033 return (fs_shrlock(vp, cmd, shr, flag, cr, ct));
5035 switch (cmd) {
5036 case F_SHARE:
5037 case F_UNSHARE:
5038 lm_fh.n_len = sizeof (fhandle_t);
5039 lm_fh.n_bytes = (char *)VTOFH(vp);
5042 * If passed an owner that is too large to fit in an
5043 * nfs_owner it is likely a recursive call from the
5044 * lock manager client and pass it straight through. If
5045 * it is not a nfs_owner then simply return an error.
5047 if (shr->s_own_len > sizeof (nfs_owner.lowner)) {
5048 if (((struct nfs_owner *)shr->s_owner)->magic !=
5049 NFS_OWNER_MAGIC)
5050 return (EINVAL);
5052 if (error = lm_shrlock(vp, cmd, shr, flag, &lm_fh)) {
5053 error = set_errno(error);
5055 return (error);
5058 * Remote share reservations owner is a combination of
5059 * a magic number, hostname, and the local owner
5061 bzero(&nfs_owner, sizeof (nfs_owner));
5062 nfs_owner.magic = NFS_OWNER_MAGIC;
5063 (void) strncpy(nfs_owner.hname, uts_nodename(),
5064 sizeof (nfs_owner.hname));
5065 bcopy(shr->s_owner, nfs_owner.lowner, shr->s_own_len);
5066 nshr.s_access = shr->s_access;
5067 nshr.s_deny = shr->s_deny;
5068 nshr.s_sysid = 0;
5069 nshr.s_pid = ttoproc(curthread)->p_pid;
5070 nshr.s_own_len = sizeof (nfs_owner);
5071 nshr.s_owner = (caddr_t)&nfs_owner;
5073 if (error = lm_shrlock(vp, cmd, &nshr, flag, &lm_fh)) {
5074 error = set_errno(error);
5077 break;
5079 case F_HASREMOTELOCKS:
5081 * NFS client can't store remote locks itself
5083 shr->s_access = 0;
5084 error = 0;
5085 break;
5087 default:
5088 error = EINVAL;
5089 break;
5092 return (error);