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]
22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
27 * Copyright (c) 1983,1984,1985,1986,1987,1988,1989 AT&T.
28 * All rights reserved.
32 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
33 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
36 #include <sys/param.h>
37 #include <sys/types.h>
38 #include <sys/systm.h>
41 #include <sys/vnode.h>
44 #include <sys/filio.h>
48 #include <sys/pathname.h>
49 #include <sys/dirent.h>
50 #include <sys/debug.h>
51 #include <sys/vmsystm.h>
52 #include <sys/fcntl.h>
53 #include <sys/flock.h>
55 #include <sys/errno.h>
56 #include <sys/strsubr.h>
57 #include <sys/sysmacros.h>
59 #include <sys/cmn_err.h>
60 #include <sys/pathconf.h>
61 #include <sys/utsname.h>
64 #include <sys/systeminfo.h>
65 #include <sys/atomic.h>
66 #include <sys/policy.h>
70 #include <rpc/types.h>
73 #include <rpc/rpc_rdma.h>
76 #include <nfs/nfs_clnt.h>
77 #include <nfs/rnode.h>
78 #include <nfs/nfs_acl.h>
86 #include <vm/seg_map.h>
87 #include <vm/seg_kpm.h>
88 #include <vm/seg_vn.h>
90 #include <sys/fs_subr.h>
94 static int nfs3_rdwrlbn(vnode_t
*, page_t
*, uoff_t
, size_t, int,
96 static int nfs3write(vnode_t
*, caddr_t
, uoff_t
, int, cred_t
*,
98 static int nfs3read(vnode_t
*, caddr_t
, offset_t
, int, size_t *, cred_t
*);
99 static int nfs3setattr(vnode_t
*, struct vattr
*, int, cred_t
*);
100 static int nfs3_accessx(void *, int, cred_t
*);
101 static int nfs3lookup_dnlc(vnode_t
*, char *, vnode_t
**, cred_t
*);
102 static int nfs3lookup_otw(vnode_t
*, char *, vnode_t
**, cred_t
*, int);
103 static int nfs3create(vnode_t
*, char *, struct vattr
*, enum vcexcl
,
104 int, vnode_t
**, cred_t
*);
105 static int nfs3excl_create_settimes(vnode_t
*, struct vattr
*, cred_t
*);
106 static int nfs3mknod(vnode_t
*, char *, struct vattr
*, enum vcexcl
,
107 int, vnode_t
**, cred_t
*);
108 static int nfs3rename(vnode_t
*, char *, vnode_t
*, char *, cred_t
*,
110 static int do_nfs3readdir(vnode_t
*, rddir_cache
*, cred_t
*);
111 static void nfs3readdir(vnode_t
*, rddir_cache
*, cred_t
*);
112 static void nfs3readdirplus(vnode_t
*, rddir_cache
*, cred_t
*);
113 static int nfs3_bio(struct buf
*, stable_how
*, cred_t
*);
114 static int nfs3_getapage(vnode_t
*, uoff_t
, size_t, uint_t
*,
115 page_t
*[], size_t, struct seg
*, caddr_t
,
116 enum seg_rw
, cred_t
*);
117 static void nfs3_readahead(vnode_t
*, uoff_t
, caddr_t
, struct seg
*,
119 static int nfs3_sync_putapage(vnode_t
*, page_t
*, uoff_t
, size_t,
121 static int nfs3_sync_pageio(vnode_t
*, page_t
*, uoff_t
, size_t,
123 static int nfs3_commit(vnode_t
*, offset3
, count3
, cred_t
*);
124 static void nfs3_set_mod(vnode_t
*);
125 static void nfs3_get_commit(vnode_t
*);
126 static void nfs3_get_commit_range(vnode_t
*, uoff_t
, size_t);
127 static int nfs3_putpage_commit(vnode_t
*, offset_t
, size_t, cred_t
*);
128 static int nfs3_commit_vp(vnode_t
*, uoff_t
, size_t, cred_t
*);
129 static int nfs3_sync_commit(vnode_t
*, page_t
*, offset3
, count3
,
131 static void nfs3_async_commit(vnode_t
*, page_t
*, offset3
, count3
,
133 static void nfs3_delmap_callback(struct as
*, void *, uint_t
);
136 * Error flags used to pass information about certain special errors
137 * which need to be handled specially.
140 #define NFS_VERF_MISMATCH -97
142 /* ALIGN64 aligns the given buffer and adjust buffer size to 64 bit */
143 #define ALIGN64(x, ptr, sz) \
144 x = ((uintptr_t)(ptr)) & (sizeof (uint64_t) - 1); \
146 x = sizeof (uint64_t) - (x); \
152 * These are the vnode ops routines which implement the vnode interface to
153 * the networked file system. These routines just take their parameters,
154 * make them look networkish by putting the right info into interface structs,
155 * and then calling the appropriate remote routine(s) to do the work.
157 * Note on directory name lookup cacheing: If we detect a stale fhandle,
158 * we purge the directory cache relative to that vnode. This way, the
159 * user won't get burned by the cache repeatedly. See <nfs/rnode.h> for
160 * more details on rnode locking.
163 static int nfs3_open(vnode_t
**, int, cred_t
*, caller_context_t
*);
164 static int nfs3_close(vnode_t
*, int, int, offset_t
, cred_t
*,
166 static int nfs3_read(vnode_t
*, struct uio
*, int, cred_t
*,
168 static int nfs3_write(vnode_t
*, struct uio
*, int, cred_t
*,
170 static int nfs3_ioctl(vnode_t
*, int, intptr_t, int, cred_t
*, int *,
172 static int nfs3_getattr(vnode_t
*, struct vattr
*, int, cred_t
*,
174 static int nfs3_setattr(vnode_t
*, struct vattr
*, int, cred_t
*,
176 static int nfs3_access(vnode_t
*, int, int, cred_t
*, caller_context_t
*);
177 static int nfs3_readlink(vnode_t
*, struct uio
*, cred_t
*,
179 static int nfs3_fsync(vnode_t
*, int, cred_t
*, caller_context_t
*);
180 static void nfs3_inactive(vnode_t
*, cred_t
*, caller_context_t
*);
181 static int nfs3_lookup(vnode_t
*, char *, vnode_t
**,
182 struct pathname
*, int, vnode_t
*, cred_t
*,
183 caller_context_t
*, int *, pathname_t
*);
184 static int nfs3_create(vnode_t
*, char *, struct vattr
*, enum vcexcl
,
185 int, vnode_t
**, cred_t
*, int, caller_context_t
*,
187 static int nfs3_remove(vnode_t
*, char *, cred_t
*, caller_context_t
*,
189 static int nfs3_link(vnode_t
*, vnode_t
*, char *, cred_t
*,
190 caller_context_t
*, int);
191 static int nfs3_rename(vnode_t
*, char *, vnode_t
*, char *, cred_t
*,
192 caller_context_t
*, int);
193 static int nfs3_mkdir(vnode_t
*, char *, struct vattr
*, vnode_t
**,
194 cred_t
*, caller_context_t
*, int, vsecattr_t
*);
195 static int nfs3_rmdir(vnode_t
*, char *, vnode_t
*, cred_t
*,
196 caller_context_t
*, int);
197 static int nfs3_symlink(vnode_t
*, char *, struct vattr
*, char *,
198 cred_t
*, caller_context_t
*, int);
199 static int nfs3_readdir(vnode_t
*, struct uio
*, cred_t
*, int *,
200 caller_context_t
*, int);
201 static int nfs3_fid(vnode_t
*, fid_t
*, caller_context_t
*);
202 static int nfs3_rwlock(vnode_t
*, int, caller_context_t
*);
203 static void nfs3_rwunlock(vnode_t
*, int, caller_context_t
*);
204 static int nfs3_seek(vnode_t
*, offset_t
, offset_t
*, caller_context_t
*);
205 static int nfs3_getpage(vnode_t
*, offset_t
, size_t, uint_t
*,
206 page_t
*[], size_t, struct seg
*, caddr_t
,
207 enum seg_rw
, cred_t
*, caller_context_t
*);
208 static int nfs3_putpage(vnode_t
*, offset_t
, size_t, int, cred_t
*,
210 static int nfs3_map(vnode_t
*, offset_t
, struct as
*, caddr_t
*, size_t,
211 uchar_t
, uchar_t
, uint_t
, cred_t
*, caller_context_t
*);
212 static int nfs3_addmap(vnode_t
*, offset_t
, struct as
*, caddr_t
, size_t,
213 uchar_t
, uchar_t
, uint_t
, cred_t
*, caller_context_t
*);
214 static int nfs3_frlock(vnode_t
*, int, struct flock64
*, int, offset_t
,
215 struct flk_callback
*, cred_t
*, caller_context_t
*);
216 static int nfs3_space(vnode_t
*, int, struct flock64
*, int, offset_t
,
217 cred_t
*, caller_context_t
*);
218 static int nfs3_realvp(vnode_t
*, vnode_t
**, caller_context_t
*);
219 static int nfs3_delmap(vnode_t
*, offset_t
, struct as
*, caddr_t
, size_t,
220 uint_t
, uint_t
, uint_t
, cred_t
*, caller_context_t
*);
221 static int nfs3_pathconf(vnode_t
*, int, ulong_t
*, cred_t
*,
223 static int nfs3_pageio(vnode_t
*, page_t
*, uoff_t
, size_t, int,
224 cred_t
*, caller_context_t
*);
225 static void nfs3_dispose(vnode_t
*, page_t
*, int, int, cred_t
*,
227 static int nfs3_setsecattr(vnode_t
*, vsecattr_t
*, int, cred_t
*,
229 static int nfs3_getsecattr(vnode_t
*, vsecattr_t
*, int, cred_t
*,
231 static int nfs3_shrlock(vnode_t
*, int, struct shrlock
*, int, cred_t
*,
234 const struct vnodeops nfs3_vnodeops
= {
236 .vop_open
= nfs3_open
,
237 .vop_close
= nfs3_close
,
238 .vop_read
= nfs3_read
,
239 .vop_write
= nfs3_write
,
240 .vop_ioctl
= nfs3_ioctl
,
241 .vop_getattr
= nfs3_getattr
,
242 .vop_setattr
= nfs3_setattr
,
243 .vop_access
= nfs3_access
,
244 .vop_lookup
= nfs3_lookup
,
245 .vop_create
= nfs3_create
,
246 .vop_remove
= nfs3_remove
,
247 .vop_link
= nfs3_link
,
248 .vop_rename
= nfs3_rename
,
249 .vop_mkdir
= nfs3_mkdir
,
250 .vop_rmdir
= nfs3_rmdir
,
251 .vop_readdir
= nfs3_readdir
,
252 .vop_symlink
= nfs3_symlink
,
253 .vop_readlink
= nfs3_readlink
,
254 .vop_fsync
= nfs3_fsync
,
255 .vop_inactive
= nfs3_inactive
,
257 .vop_rwlock
= nfs3_rwlock
,
258 .vop_rwunlock
= nfs3_rwunlock
,
259 .vop_seek
= nfs3_seek
,
260 .vop_frlock
= nfs3_frlock
,
261 .vop_space
= nfs3_space
,
262 .vop_realvp
= nfs3_realvp
,
263 .vop_getpage
= nfs3_getpage
,
264 .vop_putpage
= nfs3_putpage
,
266 .vop_addmap
= nfs3_addmap
,
267 .vop_delmap
= nfs3_delmap
,
268 /* no separate nfs3_dump */
269 .vop_dump
= nfs_dump
,
270 .vop_pathconf
= nfs3_pathconf
,
271 .vop_pageio
= nfs3_pageio
,
272 .vop_dispose
= nfs3_dispose
,
273 .vop_setsecattr
= nfs3_setsecattr
,
274 .vop_getsecattr
= nfs3_getsecattr
,
275 .vop_shrlock
= nfs3_shrlock
,
276 .vop_vnevent
= fs_vnevent_support
,
280 * XXX: This is referenced in modstubs.s
282 const struct vnodeops
*
283 nfs3_getvnodeops(void)
285 return (&nfs3_vnodeops
);
290 nfs3_open(vnode_t
**vpp
, int flag
, cred_t
*cr
, caller_context_t
*ct
)
298 if (nfs_zone() != VTOMI(vp
)->mi_zone
)
301 mutex_enter(&rp
->r_statelock
);
302 if (rp
->r_cred
== NULL
) {
306 mutex_exit(&rp
->r_statelock
);
309 * If there is no cached data or if close-to-open
310 * consistency checking is turned off, we can avoid
311 * the over the wire getattr. Otherwise, if the
312 * file system is mounted readonly, then just verify
313 * the caches are up to date using the normal mechanism.
314 * Else, if the file is not mmap'd, then just mark
315 * the attributes as timed out. They will be refreshed
316 * and the caches validated prior to being used.
317 * Else, the file system is mounted writeable so
318 * force an over the wire GETATTR in order to ensure
319 * that all cached data is valid.
321 if (vp
->v_count
> 1 ||
322 ((vn_has_cached_data(vp
) || HAVE_RDDIR_CACHE(rp
)) &&
323 !(VTOMI(vp
)->mi_flags
& MI_NOCTO
))) {
324 if (vn_is_readonly(vp
))
325 error
= nfs3_validate_caches(vp
, cr
);
326 else if (rp
->r_mapcnt
== 0 && vp
->v_count
== 1) {
330 va
.va_mask
= VATTR_ALL
;
331 error
= nfs3_getattr_otw(vp
, &va
, cr
);
341 nfs3_close(vnode_t
*vp
, int flag
, int count
, offset_t offset
, cred_t
*cr
,
342 caller_context_t
*ct
)
349 * zone_enter(2) prevents processes from changing zones with NFS files
350 * open; if we happen to get here from the wrong zone we can't do
351 * anything over the wire.
353 if (VTOMI(vp
)->mi_zone
!= nfs_zone()) {
355 * We could attempt to clean up locks, except we're sure
356 * that the current process didn't acquire any locks on
357 * the file: any attempt to lock a file belong to another zone
358 * will fail, and one can't lock an NFS file and then change
359 * zones, as that fails too.
361 * Returning an error here is the sane thing to do. A
362 * subsequent call to VN_RELE() which translates to a
363 * nfs3_inactive() will clean up state: if the zone of the
364 * vnode's origin is still alive and kicking, an async worker
365 * thread will handle the request (from the correct zone), and
366 * everything (minus the commit and final nfs3_getattr_otw()
367 * call) should be OK. If the zone is going away
368 * nfs_async_inactive() will throw away cached pages inline.
374 * If we are using local locking for this filesystem, then
375 * release all of the SYSV style record locks. Otherwise,
376 * we are doing network locking and we need to release all
377 * of the network locks. All of the locks held by this
378 * process on this file are released no matter what the
379 * incoming reference count is.
381 if (VTOMI(vp
)->mi_flags
& MI_LLOCK
) {
382 cleanlocks(vp
, ttoproc(curthread
)->p_pid
, 0);
383 cleanshares(vp
, ttoproc(curthread
)->p_pid
);
385 nfs_lockrelease(vp
, flag
, offset
, cr
);
391 * If the file has been `unlinked', then purge the
392 * DNLC so that this vnode will get reycled quicker
393 * and the .nfs* file on the server will get removed.
396 if (rp
->r_unldvp
!= NULL
)
400 * If the file was open for write and there are pages,
401 * then if the file system was mounted using the "no-close-
402 * to-open" semantics, then start an asynchronous flush
403 * of the all of the pages in the file.
404 * else the file system was not mounted using the "no-close-
405 * to-open" semantics, then do a synchronous flush and
406 * commit of all of the dirty and uncommitted pages.
408 * The asynchronous flush of the pages in the "nocto" path
409 * mostly just associates a cred pointer with the rnode so
410 * writes which happen later will have a better chance of
411 * working. It also starts the data being written to the
412 * server, but without unnecessarily delaying the application.
414 if ((flag
& FWRITE
) && vn_has_cached_data(vp
)) {
415 if (VTOMI(vp
)->mi_flags
& MI_NOCTO
) {
416 error
= nfs3_putpage(vp
, 0, 0, B_ASYNC
,
421 error
= nfs3_putpage_commit(vp
, 0, 0, cr
);
423 mutex_enter(&rp
->r_statelock
);
426 mutex_exit(&rp
->r_statelock
);
429 mutex_enter(&rp
->r_statelock
);
432 mutex_exit(&rp
->r_statelock
);
436 * If RWRITEATTR is set, then issue an over the wire GETATTR to
437 * refresh the attribute cache with a set of attributes which
438 * weren't returned from a WRITE. This will enable the close-
439 * to-open processing to work.
441 if (rp
->r_flags
& RWRITEATTR
)
442 (void) nfs3_getattr_otw(vp
, &va
, cr
);
449 nfs3_directio_read(vnode_t
*vp
, struct uio
*uiop
, cred_t
*cr
)
463 ASSERT(nfs_zone() == VTOMI(vp
)->mi_zone
);
464 sv_hostname
= VTOR(vp
)->r_server
->sv_hostname
;
467 args
.file
= *VTOFH3(vp
);
469 fi
.fhp
= (caddr_t
)&args
.file
;
470 fi
.copyproc
= nfs3copyfh
;
471 fi
.lookupproc
= nfs3lookup
;
472 fi
.xattrdirproc
= acl_getxattrdir3
;
478 offset
= uiop
->uio_loffset
;
479 count
= uiop
->uio_resid
;
482 if (mi
->mi_io_kstats
) {
483 mutex_enter(&mi
->mi_lock
);
484 kstat_runq_enter(KSTAT_IO_PTR(mi
->mi_io_kstats
));
485 mutex_exit(&mi
->mi_lock
);
489 tsize
= MIN(mi
->mi_tsize
, count
);
490 args
.offset
= (offset3
)offset
;
491 args
.count
= (count3
)tsize
;
492 res
.size
= (uint_t
)tsize
;
493 args
.res_uiop
= uiop
;
494 args
.res_data_val_alt
= NULL
;
496 error
= rfs3call(mi
, NFSPROC3_READ
,
497 xdr_READ3args
, (caddr_t
)&args
,
498 xdr_READ3uiores
, (caddr_t
)&res
, cr
,
499 &douprintf
, &res
.status
, 0, &fi
);
500 } while (error
== ENFS_TRYAGAIN
);
502 if (mi
->mi_io_kstats
) {
503 mutex_enter(&mi
->mi_lock
);
504 kstat_runq_exit(KSTAT_IO_PTR(mi
->mi_io_kstats
));
505 mutex_exit(&mi
->mi_lock
);
511 error
= geterrno3(res
.status
);
515 if (res
.count
!= res
.size
) {
516 zcmn_err(getzoneid(), CE_WARN
,
517 "nfs3_directio_read: server %s returned incorrect amount",
523 if (mi
->mi_io_kstats
) {
524 mutex_enter(&mi
->mi_lock
);
525 KSTAT_IO_PTR(mi
->mi_io_kstats
)->reads
++;
526 KSTAT_IO_PTR(mi
->mi_io_kstats
)->nread
+= res
.count
;
527 mutex_exit(&mi
->mi_lock
);
529 lwp_stat_update(LWP_STAT_INBLK
, 1);
530 } while (count
&& !res
.eof
);
537 nfs3_read(vnode_t
*vp
, struct uio
*uiop
, int ioflag
, cred_t
*cr
,
538 caller_context_t
*ct
)
553 ASSERT(nfs_rw_lock_held(&rp
->r_rwlock
, RW_READER
));
555 if (nfs_zone() != mi
->mi_zone
)
558 if (vp
->v_type
!= VREG
)
561 if (uiop
->uio_resid
== 0)
564 if (uiop
->uio_loffset
< 0 || uiop
->uio_loffset
+ uiop
->uio_resid
< 0)
568 * Bypass VM if caching has been disabled (e.g., locking) or if
569 * using client-side direct I/O and the file is not mmap'd and
570 * there are no cached pages.
572 if ((vp
->v_flag
& VNOCACHE
) ||
573 (((rp
->r_flags
& RDIRECTIO
) || (mi
->mi_flags
& MI_DIRECTIO
)) &&
574 rp
->r_mapcnt
== 0 && rp
->r_inmap
== 0 &&
575 !vn_has_cached_data(vp
))) {
576 return (nfs3_directio_read(vp
, uiop
, cr
));
580 off
= uiop
->uio_loffset
& MAXBMASK
; /* mapping offset */
581 on
= uiop
->uio_loffset
& MAXBOFFSET
; /* Relative offset */
582 n
= MIN(MAXBSIZE
- on
, uiop
->uio_resid
);
584 error
= nfs3_validate_caches(vp
, cr
);
588 mutex_enter(&rp
->r_statelock
);
589 while (rp
->r_flags
& RINCACHEPURGE
) {
590 if (!cv_wait_sig(&rp
->r_cv
, &rp
->r_statelock
)) {
591 mutex_exit(&rp
->r_statelock
);
595 diff
= rp
->r_size
- uiop
->uio_loffset
;
596 mutex_exit(&rp
->r_statelock
);
606 error
= vpm_data_copy(vp
, off
+ on
, n
, uiop
,
609 base
= segmap_getmapflt(segkmap
, vp
, off
+ on
, n
, 1,
612 error
= uiomove(base
+ on
, n
, UIO_READ
, uiop
);
617 * If read a whole block or read to eof,
618 * won't need this buffer again soon.
620 mutex_enter(&rp
->r_statelock
);
621 if (n
+ on
== MAXBSIZE
||
622 uiop
->uio_loffset
== rp
->r_size
)
626 mutex_exit(&rp
->r_statelock
);
628 error
= vpm_sync_pages(vp
, off
, n
, flags
);
630 error
= segmap_release(segkmap
, base
, flags
);
634 (void) vpm_sync_pages(vp
, off
, n
, 0);
636 (void) segmap_release(segkmap
, base
, 0);
639 } while (!error
&& uiop
->uio_resid
> 0);
646 nfs3_write(vnode_t
*vp
, struct uio
*uiop
, int ioflag
, cred_t
*cr
,
647 caller_context_t
*ct
)
649 rlim_t limit
= uiop
->uio_llimit
;
665 if (vp
->v_type
!= VREG
)
669 if (nfs_zone() != mi
->mi_zone
)
671 if (uiop
->uio_resid
== 0)
674 if (ioflag
& FAPPEND
) {
678 * Must serialize if appending.
680 if (nfs_rw_lock_held(&rp
->r_rwlock
, RW_READER
)) {
681 nfs_rw_exit(&rp
->r_rwlock
);
682 if (nfs_rw_enter_sig(&rp
->r_rwlock
, RW_WRITER
,
687 va
.va_mask
= VATTR_SIZE
;
688 error
= nfs3getattr(vp
, &va
, cr
);
691 uiop
->uio_loffset
= va
.va_size
;
694 offset
= uiop
->uio_loffset
+ uiop
->uio_resid
;
696 if (uiop
->uio_loffset
< 0 || offset
< 0)
699 if (limit
== RLIM_INFINITY
|| limit
> MAXOFFSET_T
)
703 * Check to make sure that the process will not exceed
704 * its limit on file size. It is okay to write up to
705 * the limit, but not beyond. Thus, the write which
706 * reaches the limit will be short and the next write
707 * will return an error.
710 if (offset
> limit
) {
711 remainder
= offset
- limit
;
712 uiop
->uio_resid
= limit
- uiop
->uio_loffset
;
713 if (uiop
->uio_resid
<= 0) {
714 proc_t
*p
= ttoproc(curthread
);
716 uiop
->uio_resid
+= remainder
;
717 mutex_enter(&p
->p_lock
);
718 (void) rctl_action(rctlproc_legacy
[RLIMIT_FSIZE
],
719 p
->p_rctls
, p
, RCA_UNSAFE_SIGINFO
);
720 mutex_exit(&p
->p_lock
);
725 if (nfs_rw_enter_sig(&rp
->r_lkserlock
, RW_READER
, INTR(vp
)))
729 * Bypass VM if caching has been disabled (e.g., locking) or if
730 * using client-side direct I/O and the file is not mmap'd and
731 * there are no cached pages.
733 if ((vp
->v_flag
& VNOCACHE
) ||
734 (((rp
->r_flags
& RDIRECTIO
) || (mi
->mi_flags
& MI_DIRECTIO
)) &&
735 rp
->r_mapcnt
== 0 && rp
->r_inmap
== 0 &&
736 !vn_has_cached_data(vp
))) {
740 stable_how stab_comm
;
743 if (rp
->r_flags
& RSTALE
) {
744 resid
= uiop
->uio_resid
;
745 offset
= uiop
->uio_loffset
;
748 * A close may have cleared r_error, if so,
749 * propagate ESTALE error return properly
755 bufsize
= MIN(uiop
->uio_resid
, mi
->mi_stsize
);
756 base
= kmem_alloc(bufsize
, KM_SLEEP
);
759 stab_comm
= DATA_SYNC
;
761 stab_comm
= FILE_SYNC
;
762 resid
= uiop
->uio_resid
;
763 offset
= uiop
->uio_loffset
;
764 count
= MIN(uiop
->uio_resid
, bufsize
);
765 org_offset
= uiop
->uio_loffset
;
766 error
= uiomove(base
, count
, UIO_WRITE
, uiop
);
768 error
= nfs3write(vp
, base
, org_offset
,
769 count
, cr
, &stab_comm
);
771 } while (!error
&& uiop
->uio_resid
> 0);
772 kmem_free(base
, bufsize
);
777 bsize
= vp
->v_vfsp
->vfs_bsize
;
780 off
= uiop
->uio_loffset
& MAXBMASK
; /* mapping offset */
781 on
= uiop
->uio_loffset
& MAXBOFFSET
; /* Relative offset */
782 n
= MIN(MAXBSIZE
- on
, uiop
->uio_resid
);
784 resid
= uiop
->uio_resid
;
785 offset
= uiop
->uio_loffset
;
787 if (rp
->r_flags
& RSTALE
) {
790 * A close may have cleared r_error, if so,
791 * propagate ESTALE error return properly
799 * Don't create dirty pages faster than they
800 * can be cleaned so that the system doesn't
801 * get imbalanced. If the async queue is
802 * maxed out, then wait for it to drain before
803 * creating more dirty pages. Also, wait for
804 * any threads doing pagewalks in the vop_getattr
805 * entry points so that they don't block for
808 mutex_enter(&rp
->r_statelock
);
809 while ((mi
->mi_max_threads
!= 0 &&
810 rp
->r_awcount
> 2 * mi
->mi_max_threads
) ||
813 klwp_t
*lwp
= ttolwp(curthread
);
817 if (!cv_wait_sig(&rp
->r_cv
, &rp
->r_statelock
)) {
818 mutex_exit(&rp
->r_statelock
);
827 cv_wait(&rp
->r_cv
, &rp
->r_statelock
);
829 mutex_exit(&rp
->r_statelock
);
832 * Touch the page and fault it in if it is not in core
833 * before segmap_getmapflt or vpm_data_copy can lock it.
834 * This is to avoid the deadlock if the buffer is mapped
835 * to the same file through mmap which we want to write.
837 uio_prefaultpages((long)n
, uiop
);
841 * It will use kpm mappings, so no need to
844 error
= writerp(rp
, NULL
, n
, uiop
, 0);
847 int pon
= uiop
->uio_loffset
& PAGEOFFSET
;
848 size_t pn
= MIN(PAGESIZE
- pon
,
852 mutex_enter(&rp
->r_statelock
);
853 pagecreate
= (pon
== 0) && (pn
== PAGESIZE
||
854 uiop
->uio_loffset
+ pn
>= rp
->r_size
);
855 mutex_exit(&rp
->r_statelock
);
857 base
= segmap_getmapflt(segkmap
, vp
, off
+ on
,
858 pn
, !pagecreate
, S_WRITE
);
860 error
= writerp(rp
, base
+ pon
, n
, uiop
,
864 base
= segmap_getmapflt(segkmap
, vp
, off
+ on
,
866 error
= writerp(rp
, base
+ on
, n
, uiop
, 0);
871 if (mi
->mi_flags
& MI_NOAC
)
873 else if ((uiop
->uio_loffset
% bsize
) == 0 ||
876 * Have written a whole block.
877 * Start an asynchronous write
878 * and mark the buffer to
879 * indicate that it won't be
882 flags
= SM_WRITE
| SM_ASYNC
| SM_DONTNEED
;
885 if ((ioflag
& (FSYNC
|FDSYNC
)) ||
886 (rp
->r_flags
& ROUTOFSPACE
)) {
891 error
= vpm_sync_pages(vp
, off
, n
, flags
);
893 error
= segmap_release(segkmap
, base
, flags
);
897 (void) vpm_sync_pages(vp
, off
, n
, 0);
899 (void) segmap_release(segkmap
, base
, 0);
902 * In the event that we got an access error while
903 * faulting in a page for a write-only file just
909 } while (!error
&& uiop
->uio_resid
> 0);
913 uiop
->uio_resid
= resid
+ remainder
;
914 uiop
->uio_loffset
= offset
;
916 uiop
->uio_resid
+= remainder
;
918 nfs_rw_exit(&rp
->r_lkserlock
);
924 * Flags are composed of {B_ASYNC, B_INVAL, B_FREE, B_DONTNEED}
927 nfs3_rdwrlbn(vnode_t
*vp
, page_t
*pp
, uoff_t off
, size_t len
,
928 int flags
, cred_t
*cr
)
934 stable_how stab_comm
;
936 ASSERT(nfs_zone() == VTOMI(vp
)->mi_zone
);
937 bp
= pageio_setup(pp
, len
, vp
, flags
);
941 * pageio_setup should have set b_addr to 0. This
942 * is correct since we want to do I/O on a page
943 * boundary. bp_mapin will use this addr to calculate
944 * an offset, and then set b_addr to the kernel virtual
945 * address it allocated for us.
947 ASSERT(bp
->b_un
.b_addr
== 0);
951 bp
->b_lblkno
= lbtodb(off
);
953 bp
->b_offset
= (offset_t
)off
;
957 * Calculate the desired level of stability to write data
958 * on the server and then mark all of the pages to reflect
961 if ((flags
& (B_WRITE
|B_ASYNC
)) == (B_WRITE
|B_ASYNC
) &&
963 stab_comm
= UNSTABLE
;
964 fsdata
= C_DELAYCOMMIT
;
966 stab_comm
= FILE_SYNC
;
972 pp
->p_fsdata
= fsdata
;
973 } while ((pp
= pp
->p_next
) != savepp
);
975 error
= nfs3_bio(bp
, &stab_comm
, cr
);
981 * If the server wrote pages in a more stable fashion than
982 * was requested, then clear all of the marks in the pages
983 * indicating that COMMIT operations were required.
985 if (stab_comm
!= UNSTABLE
&& fsdata
== C_DELAYCOMMIT
) {
987 pp
->p_fsdata
= C_NOCOMMIT
;
988 } while ((pp
= pp
->p_next
) != savepp
);
995 * Write to file. Writes to remote server in largest size
996 * chunks that the server can handle. Write is synchronous.
999 nfs3write(vnode_t
*vp
, caddr_t base
, uoff_t offset
, int count
, cred_t
*cr
,
1000 stable_how
*stab_comm
)
1013 ASSERT(nfs_zone() == mi
->mi_zone
);
1015 args
.file
= *VTOFH3(vp
);
1016 args
.stable
= *stab_comm
;
1018 *stab_comm
= FILE_SYNC
;
1023 if ((vp
->v_flag
& VNOCACHE
) ||
1024 (rp
->r_flags
& RDIRECTIO
) ||
1025 (mi
->mi_flags
& MI_DIRECTIO
))
1026 tsize
= MIN(mi
->mi_stsize
, count
);
1028 tsize
= MIN(mi
->mi_curwrite
, count
);
1029 args
.offset
= (offset3
)offset
;
1030 args
.count
= (count3
)tsize
;
1031 args
.data
.data_len
= (uint_t
)tsize
;
1032 args
.data
.data_val
= base
;
1034 if (mi
->mi_io_kstats
) {
1035 mutex_enter(&mi
->mi_lock
);
1036 kstat_runq_enter(KSTAT_IO_PTR(mi
->mi_io_kstats
));
1037 mutex_exit(&mi
->mi_lock
);
1041 error
= rfs3call(mi
, NFSPROC3_WRITE
,
1042 xdr_WRITE3args
, (caddr_t
)&args
,
1043 xdr_WRITE3res
, (caddr_t
)&res
, cr
,
1044 &douprintf
, &res
.status
, 0, NULL
);
1045 } while (error
== ENFS_TRYAGAIN
);
1046 if (mi
->mi_io_kstats
) {
1047 mutex_enter(&mi
->mi_lock
);
1048 kstat_runq_exit(KSTAT_IO_PTR(mi
->mi_io_kstats
));
1049 mutex_exit(&mi
->mi_lock
);
1054 error
= geterrno3(res
.status
);
1056 if (res
.resok
.count
> args
.count
) {
1057 zcmn_err(getzoneid(), CE_WARN
,
1058 "nfs3write: server %s wrote %u, "
1060 rp
->r_server
->sv_hostname
,
1061 res
.resok
.count
, args
.count
);
1064 if (res
.resok
.committed
== UNSTABLE
) {
1065 *stab_comm
= UNSTABLE
;
1066 if (args
.stable
== DATA_SYNC
||
1067 args
.stable
== FILE_SYNC
) {
1068 zcmn_err(getzoneid(), CE_WARN
,
1069 "nfs3write: server %s did not commit to stable storage",
1070 rp
->r_server
->sv_hostname
);
1074 tsize
= (int)res
.resok
.count
;
1078 if (mi
->mi_io_kstats
) {
1079 mutex_enter(&mi
->mi_lock
);
1080 KSTAT_IO_PTR(mi
->mi_io_kstats
)->writes
++;
1081 KSTAT_IO_PTR(mi
->mi_io_kstats
)->nwritten
+=
1083 mutex_exit(&mi
->mi_lock
);
1085 lwp_stat_update(LWP_STAT_OUBLK
, 1);
1086 mutex_enter(&rp
->r_statelock
);
1087 if (rp
->r_flags
& RHAVEVERF
) {
1088 if (rp
->r_verf
!= res
.resok
.verf
) {
1090 rp
->r_verf
= res
.resok
.verf
;
1092 * If the data was written UNSTABLE,
1093 * then might as well stop because
1094 * the whole block will have to get
1097 if (*stab_comm
== UNSTABLE
) {
1098 mutex_exit(&rp
->r_statelock
);
1103 rp
->r_verf
= res
.resok
.verf
;
1104 rp
->r_flags
|= RHAVEVERF
;
1107 * Mark the attribute cache as timed out and
1108 * set RWRITEATTR to indicate that the file
1109 * was modified with a WRITE operation and
1110 * that the attributes can not be trusted.
1112 PURGE_ATTRCACHE_LOCKED(rp
);
1113 rp
->r_flags
|= RWRITEATTR
;
1114 mutex_exit(&rp
->r_statelock
);
1116 } while (!error
&& count
);
1122 * Read from a file. Reads data in largest chunks our interface can handle.
1125 nfs3read(vnode_t
*vp
, caddr_t base
, offset_t offset
, int count
,
1126 size_t *residp
, cred_t
*cr
)
1141 ASSERT(nfs_zone() == mi
->mi_zone
);
1144 args
.file
= *VTOFH3(vp
);
1146 fi
.fhp
= (caddr_t
)&args
.file
;
1147 fi
.copyproc
= nfs3copyfh
;
1148 fi
.lookupproc
= nfs3lookup
;
1149 fi
.xattrdirproc
= acl_getxattrdir3
;
1151 res
.pov
.fres
.vp
= vp
;
1152 res
.pov
.fres
.vap
= &va
;
1157 if (mi
->mi_io_kstats
) {
1158 mutex_enter(&mi
->mi_lock
);
1159 kstat_runq_enter(KSTAT_IO_PTR(mi
->mi_io_kstats
));
1160 mutex_exit(&mi
->mi_lock
);
1164 if ((vp
->v_flag
& VNOCACHE
) ||
1165 (rp
->r_flags
& RDIRECTIO
) ||
1166 (mi
->mi_flags
& MI_DIRECTIO
))
1167 tsize
= MIN(mi
->mi_tsize
, count
);
1169 tsize
= MIN(mi
->mi_curread
, count
);
1170 res
.data
.data_val
= base
;
1171 res
.data
.data_len
= tsize
;
1172 args
.offset
= (offset3
)offset
;
1173 args
.count
= (count3
)tsize
;
1174 args
.res_uiop
= NULL
;
1175 args
.res_data_val_alt
= base
;
1178 error
= rfs3call(mi
, NFSPROC3_READ
,
1179 xdr_READ3args
, (caddr_t
)&args
,
1180 xdr_READ3vres
, (caddr_t
)&res
, cr
,
1181 &douprintf
, &res
.status
, 0, &fi
);
1182 } while (error
== ENFS_TRYAGAIN
);
1184 if (mi
->mi_io_kstats
) {
1185 mutex_enter(&mi
->mi_lock
);
1186 kstat_runq_exit(KSTAT_IO_PTR(mi
->mi_io_kstats
));
1187 mutex_exit(&mi
->mi_lock
);
1193 error
= geterrno3(res
.status
);
1197 if (res
.count
!= res
.data
.data_len
) {
1198 zcmn_err(getzoneid(), CE_WARN
,
1199 "nfs3read: server %s returned incorrect amount",
1200 rp
->r_server
->sv_hostname
);
1207 offset
+= res
.count
;
1208 if (mi
->mi_io_kstats
) {
1209 mutex_enter(&mi
->mi_lock
);
1210 KSTAT_IO_PTR(mi
->mi_io_kstats
)->reads
++;
1211 KSTAT_IO_PTR(mi
->mi_io_kstats
)->nread
+= res
.count
;
1212 mutex_exit(&mi
->mi_lock
);
1214 lwp_stat_update(LWP_STAT_INBLK
, 1);
1215 } while (count
&& !res
.eof
);
1217 if (res
.pov
.attributes
) {
1218 mutex_enter(&rp
->r_statelock
);
1219 if (!CACHE_VALID(rp
, va
.va_mtime
, va
.va_size
)) {
1220 mutex_exit(&rp
->r_statelock
);
1221 PURGE_ATTRCACHE(vp
);
1223 if (rp
->r_mtime
<= t
)
1224 nfs_attrcache_va(vp
, &va
);
1225 mutex_exit(&rp
->r_statelock
);
1234 nfs3_ioctl(vnode_t
*vp
, int cmd
, intptr_t arg
, int flag
, cred_t
*cr
, int *rvalp
,
1235 caller_context_t
*ct
)
1238 if (nfs_zone() != VTOMI(vp
)->mi_zone
)
1242 return (nfs_directio(vp
, (int)arg
, cr
));
1250 nfs3_getattr(vnode_t
*vp
, struct vattr
*vap
, int flags
, cred_t
*cr
,
1251 caller_context_t
*ct
)
1256 if (nfs_zone() != VTOMI(vp
)->mi_zone
)
1259 * If it has been specified that the return value will
1260 * just be used as a hint, and we are only being asked
1261 * for size, fsid or rdevid, then return the client's
1262 * notion of these values without checking to make sure
1263 * that the attribute cache is up to date.
1264 * The whole point is to avoid an over the wire GETATTR
1268 if (flags
& ATTR_HINT
) {
1270 (vap
->va_mask
& (VATTR_SIZE
| VATTR_FSID
| VATTR_RDEV
))) {
1271 mutex_enter(&rp
->r_statelock
);
1272 if (vap
->va_mask
| VATTR_SIZE
)
1273 vap
->va_size
= rp
->r_size
;
1274 if (vap
->va_mask
| VATTR_FSID
)
1275 vap
->va_fsid
= rp
->r_attr
.va_fsid
;
1276 if (vap
->va_mask
| VATTR_RDEV
)
1277 vap
->va_rdev
= rp
->r_attr
.va_rdev
;
1278 mutex_exit(&rp
->r_statelock
);
1284 * Only need to flush pages if asking for the mtime
1285 * and if there any dirty pages or any outstanding
1286 * asynchronous (write) requests for this file.
1288 if (vap
->va_mask
& VATTR_MTIME
) {
1289 if (vn_has_cached_data(vp
) &&
1290 ((rp
->r_flags
& RDIRTY
) || rp
->r_awcount
> 0)) {
1291 mutex_enter(&rp
->r_statelock
);
1293 mutex_exit(&rp
->r_statelock
);
1294 error
= nfs3_putpage(vp
, 0, 0, 0, cr
, ct
);
1295 mutex_enter(&rp
->r_statelock
);
1296 if (error
&& (error
== ENOSPC
|| error
== EDQUOT
)) {
1298 rp
->r_error
= error
;
1300 if (--rp
->r_gcount
== 0)
1301 cv_broadcast(&rp
->r_cv
);
1302 mutex_exit(&rp
->r_statelock
);
1306 return (nfs3getattr(vp
, vap
, cr
));
1311 nfs3_setattr(vnode_t
*vp
, struct vattr
*vap
, int flags
, cred_t
*cr
,
1312 caller_context_t
*ct
)
1317 if (vap
->va_mask
& VATTR_NOSET
)
1319 if (nfs_zone() != VTOMI(vp
)->mi_zone
)
1322 va
.va_mask
= VATTR_UID
| VATTR_MODE
;
1323 error
= nfs3getattr(vp
, &va
, cr
);
1327 error
= secpolicy_vnode_setattr(cr
, vp
, vap
, &va
, flags
, nfs3_accessx
,
1332 error
= nfs3setattr(vp
, vap
, flags
, cr
);
1334 if (error
== 0 && (vap
->va_mask
& VATTR_SIZE
) && vap
->va_size
== 0)
1335 vnevent_truncate(vp
, ct
);
1341 nfs3setattr(vnode_t
*vp
, struct vattr
*vap
, int flags
, cred_t
*cr
)
1354 ASSERT(nfs_zone() == VTOMI(vp
)->mi_zone
);
1355 mask
= vap
->va_mask
;
1360 * Only need to flush pages if there are any pages and
1361 * if the file is marked as dirty in some fashion. The
1362 * file must be flushed so that we can accurately
1363 * determine the size of the file and the cached data
1364 * after the SETATTR returns. A file is considered to
1365 * be dirty if it is either marked with RDIRTY, has
1366 * outstanding i/o's active, or is mmap'd. In this
1367 * last case, we can't tell whether there are dirty
1368 * pages, so we flush just to be sure.
1370 if (vn_has_cached_data(vp
) &&
1371 ((rp
->r_flags
& RDIRTY
) ||
1373 rp
->r_mapcnt
> 0)) {
1374 ASSERT(vp
->v_type
!= VCHR
);
1375 error
= nfs3_putpage(vp
, 0, 0, 0, cr
, NULL
);
1376 if (error
&& (error
== ENOSPC
|| error
== EDQUOT
)) {
1377 mutex_enter(&rp
->r_statelock
);
1379 rp
->r_error
= error
;
1380 mutex_exit(&rp
->r_statelock
);
1384 args
.object
= *RTOFH3(rp
);
1386 * If the intent is for the server to set the times,
1387 * there is no point in have the mask indicating set mtime or
1388 * atime, because the vap values may be junk, and so result
1389 * in an overflow error. Remove these flags from the vap mask
1390 * before calling in this case, and restore them afterwards.
1392 if ((mask
& (VATTR_ATIME
| VATTR_MTIME
)) && !(flags
& ATTR_UTIME
)) {
1393 /* Use server times, so don't set the args time fields */
1394 vap
->va_mask
&= ~(VATTR_ATIME
| VATTR_MTIME
);
1395 error
= vattr_to_sattr3(vap
, &args
.new_attributes
);
1396 vap
->va_mask
|= (mask
& (VATTR_ATIME
| VATTR_MTIME
));
1397 if (mask
& VATTR_ATIME
) {
1398 args
.new_attributes
.atime
.set_it
= SET_TO_SERVER_TIME
;
1400 if (mask
& VATTR_MTIME
) {
1401 args
.new_attributes
.mtime
.set_it
= SET_TO_SERVER_TIME
;
1404 /* Either do not set times or use the client specified times */
1405 error
= vattr_to_sattr3(vap
, &args
.new_attributes
);
1409 /* req time field(s) overflow - return immediately */
1413 va
.va_mask
= VATTR_MODE
| VATTR_CTIME
;
1414 error
= nfs3getattr(vp
, &va
, cr
);
1420 if (mask
& VATTR_SIZE
) {
1421 args
.guard
.check
= TRUE
;
1422 args
.guard
.obj_ctime
.seconds
= va
.va_ctime
.tv_sec
;
1423 args
.guard
.obj_ctime
.nseconds
= va
.va_ctime
.tv_nsec
;
1425 args
.guard
.check
= FALSE
;
1431 error
= rfs3call(VTOMI(vp
), NFSPROC3_SETATTR
,
1432 xdr_SETATTR3args
, (caddr_t
)&args
,
1433 xdr_SETATTR3res
, (caddr_t
)&res
, cr
,
1434 &douprintf
, &res
.status
, 0, NULL
);
1437 * Purge the access cache and ACL cache if changing either the
1438 * owner of the file, the group owner, or the mode. These may
1439 * change the access permissions of the file, so purge old
1440 * information and start over again.
1442 if (mask
& (VATTR_UID
| VATTR_GID
| VATTR_MODE
)) {
1443 (void) nfs_access_purge_rp(rp
);
1444 if (rp
->r_secattr
!= NULL
) {
1445 mutex_enter(&rp
->r_statelock
);
1446 vsp
= rp
->r_secattr
;
1447 rp
->r_secattr
= NULL
;
1448 mutex_exit(&rp
->r_statelock
);
1455 PURGE_ATTRCACHE(vp
);
1459 error
= geterrno3(res
.status
);
1462 * If changing the size of the file, invalidate
1463 * any local cached data which is no longer part
1464 * of the file. We also possibly invalidate the
1465 * last page in the file. We could use
1466 * pvn_vpzero(), but this would mark the page as
1467 * modified and require it to be written back to
1468 * the server for no particularly good reason.
1469 * This way, if we access it, then we bring it
1470 * back in. A read should be cheaper than a
1473 if (mask
& VATTR_SIZE
) {
1474 nfs_invalidate_pages(vp
,
1475 (vap
->va_size
& PAGEMASK
), cr
);
1477 nfs3_cache_wcc_data(vp
, &res
.resok
.obj_wcc
, t
, cr
);
1479 * Some servers will change the mode to clear the setuid
1480 * and setgid bits when changing the uid or gid. The
1481 * client needs to compensate appropriately.
1483 if (mask
& (VATTR_UID
| VATTR_GID
)) {
1486 va
.va_mask
= VATTR_MODE
;
1487 terror
= nfs3getattr(vp
, &va
, cr
);
1489 (((mask
& VATTR_MODE
) && va
.va_mode
!= vap
->va_mode
) ||
1490 (!(mask
& VATTR_MODE
) && va
.va_mode
!= omode
))) {
1491 va
.va_mask
= VATTR_MODE
;
1492 if (mask
& VATTR_MODE
)
1493 va
.va_mode
= vap
->va_mode
;
1496 (void) nfs3setattr(vp
, &va
, 0, cr
);
1500 nfs3_cache_wcc_data(vp
, &res
.resfail
.obj_wcc
, t
, cr
);
1502 * If we got back a "not synchronized" error, then
1503 * we need to retry with a new guard value. The
1504 * guard value used is the change time. If the
1505 * server returned post_op_attr, then we can just
1506 * retry because we have the latest attributes.
1507 * Otherwise, we issue a GETATTR to get the latest
1508 * attributes and then retry. If we couldn't get
1509 * the attributes this way either, then we give
1510 * up because we can't complete the operation as
1513 if (res
.status
== NFS3ERR_NOT_SYNC
) {
1514 va
.va_mask
= VATTR_CTIME
;
1515 if (nfs3getattr(vp
, &va
, cr
) == 0)
1518 PURGE_STALE_FH(error
, vp
, cr
);
1525 nfs3_accessx(void *vp
, int mode
, cred_t
*cr
)
1527 ASSERT(nfs_zone() == VTOMI((vnode_t
*)vp
)->mi_zone
);
1528 return (nfs3_access(vp
, mode
, 0, cr
, NULL
));
1533 nfs3_access(vnode_t
*vp
, int mode
, int flags
, cred_t
*cr
, caller_context_t
*ct
)
1541 cred_t
*cred
, *ncr
, *ncrfree
= NULL
;
1543 nfs_access_type_t cacc
;
1547 if (nfs_zone() != VTOMI(vp
)->mi_zone
)
1550 acc
|= ACCESS3_READ
;
1551 if (mode
& VWRITE
) {
1552 if (vn_is_readonly(vp
) && !IS_DEVVP(vp
))
1554 if (vp
->v_type
== VDIR
)
1555 acc
|= ACCESS3_DELETE
;
1556 acc
|= ACCESS3_MODIFY
| ACCESS3_EXTEND
;
1559 if (vp
->v_type
== VDIR
)
1560 acc
|= ACCESS3_LOOKUP
;
1562 acc
|= ACCESS3_EXECUTE
;
1566 args
.object
= *VTOFH3(vp
);
1567 if (vp
->v_type
== VDIR
) {
1568 args
.access
= ACCESS3_READ
| ACCESS3_DELETE
| ACCESS3_MODIFY
|
1569 ACCESS3_EXTEND
| ACCESS3_LOOKUP
;
1571 args
.access
= ACCESS3_READ
| ACCESS3_MODIFY
| ACCESS3_EXTEND
|
1575 fi
.fhp
= (caddr_t
)&args
.object
;
1576 fi
.copyproc
= nfs3copyfh
;
1577 fi
.lookupproc
= nfs3lookup
;
1578 fi
.xattrdirproc
= acl_getxattrdir3
;
1582 * ncr and ncrfree both initially
1583 * point to the memory area returned
1585 * ncrfree not NULL when exiting means
1586 * that we need to release it
1588 ncr
= crnetadjust(cred
);
1591 if (rp
->r_acache
!= NULL
) {
1592 cacc
= nfs_access_check(rp
, acc
, cred
);
1593 if (cacc
== NFS_ACCESS_ALLOWED
) {
1594 if (ncrfree
!= NULL
)
1598 if (cacc
== NFS_ACCESS_DENIED
) {
1600 * If the cred can be adjusted, try again
1601 * with the new cred.
1608 if (ncrfree
!= NULL
)
1618 error
= rfs3call(VTOMI(vp
), NFSPROC3_ACCESS
,
1619 xdr_ACCESS3args
, (caddr_t
)&args
,
1620 xdr_ACCESS3res
, (caddr_t
)&res
, cred
,
1621 &douprintf
, &res
.status
, 0, &fi
);
1624 if (ncrfree
!= NULL
)
1629 error
= geterrno3(res
.status
);
1631 nfs3_cache_post_op_attr(vp
, &res
.resok
.obj_attributes
, t
, cr
);
1632 nfs_access_cache(rp
, args
.access
, res
.resok
.access
, cred
);
1634 * we just cached results with cred; if cred is the
1635 * adjusted credentials from crnetadjust, we do not want
1636 * to release them before exiting: hence setting ncrfree
1641 if ((acc
& res
.resok
.access
) != acc
) {
1643 * If the cred can be adjusted, try again
1644 * with the new cred.
1654 nfs3_cache_post_op_attr(vp
, &res
.resfail
.obj_attributes
, t
, cr
);
1655 PURGE_STALE_FH(error
, vp
, cr
);
1658 if (ncrfree
!= NULL
)
1664 static int nfs3_do_symlink_cache
= 1;
1668 nfs3_readlink(vnode_t
*vp
, struct uio
*uiop
, cred_t
*cr
, caller_context_t
*ct
)
1673 nfspath3 resdata_backup
;
1681 * Can't readlink anything other than a symbolic link.
1683 if (vp
->v_type
!= VLNK
)
1685 if (nfs_zone() != VTOMI(vp
)->mi_zone
)
1689 if (nfs3_do_symlink_cache
&& rp
->r_symlink
.contents
!= NULL
) {
1690 error
= nfs3_validate_caches(vp
, cr
);
1693 mutex_enter(&rp
->r_statelock
);
1694 if (rp
->r_symlink
.contents
!= NULL
) {
1695 error
= uiomove(rp
->r_symlink
.contents
,
1696 rp
->r_symlink
.len
, UIO_READ
, uiop
);
1697 mutex_exit(&rp
->r_statelock
);
1700 mutex_exit(&rp
->r_statelock
);
1703 args
.symlink
= *VTOFH3(vp
);
1705 fi
.fhp
= (caddr_t
)&args
.symlink
;
1706 fi
.copyproc
= nfs3copyfh
;
1707 fi
.lookupproc
= nfs3lookup
;
1708 fi
.xattrdirproc
= acl_getxattrdir3
;
1710 res
.resok
.data
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
1712 resdata_backup
= res
.resok
.data
;
1718 error
= rfs3call(VTOMI(vp
), NFSPROC3_READLINK
,
1719 xdr_READLINK3args
, (caddr_t
)&args
,
1720 xdr_READLINK3res
, (caddr_t
)&res
, cr
,
1721 &douprintf
, &res
.status
, 0, &fi
);
1723 if (res
.resok
.data
== nfs3nametoolong
)
1727 kmem_free(resdata_backup
, MAXPATHLEN
);
1731 error
= geterrno3(res
.status
);
1733 nfs3_cache_post_op_attr(vp
, &res
.resok
.symlink_attributes
, t
,
1735 len
= strlen(res
.resok
.data
);
1736 error
= uiomove(res
.resok
.data
, len
, UIO_READ
, uiop
);
1737 if (nfs3_do_symlink_cache
&& rp
->r_symlink
.contents
== NULL
) {
1738 mutex_enter(&rp
->r_statelock
);
1739 if (rp
->r_symlink
.contents
== NULL
) {
1740 rp
->r_symlink
.contents
= res
.resok
.data
;
1741 rp
->r_symlink
.len
= len
;
1742 rp
->r_symlink
.size
= MAXPATHLEN
;
1743 mutex_exit(&rp
->r_statelock
);
1745 mutex_exit(&rp
->r_statelock
);
1747 kmem_free((void *)res
.resok
.data
, MAXPATHLEN
);
1750 kmem_free((void *)res
.resok
.data
, MAXPATHLEN
);
1753 nfs3_cache_post_op_attr(vp
,
1754 &res
.resfail
.symlink_attributes
, t
, cr
);
1755 PURGE_STALE_FH(error
, vp
, cr
);
1757 kmem_free((void *)res
.resok
.data
, MAXPATHLEN
);
1762 * The over the wire error for attempting to readlink something
1763 * other than a symbolic link is ENXIO. However, we need to
1764 * return EINVAL instead of ENXIO, so we map it here.
1766 return (error
== ENXIO
? EINVAL
: error
);
1770 * Flush local dirty pages to stable storage on the server.
1772 * If FNODSYNC is specified, then there is nothing to do because
1773 * metadata changes are not cached on the client before being
1774 * sent to the server.
1778 nfs3_fsync(vnode_t
*vp
, int syncflag
, cred_t
*cr
, caller_context_t
*ct
)
1782 if ((syncflag
& FNODSYNC
) || IS_SWAPVP(vp
))
1784 if (nfs_zone() != VTOMI(vp
)->mi_zone
)
1787 error
= nfs3_putpage_commit(vp
, 0, 0, cr
);
1789 error
= VTOR(vp
)->r_error
;
1794 * Weirdness: if the file was removed or the target of a rename
1795 * operation while it was open, it got renamed instead. Here we
1796 * remove the renamed file.
1800 nfs3_inactive(vnode_t
*vp
, cred_t
*cr
, caller_context_t
*ct
)
1804 ASSERT(vp
!= DNLC_NO_VNODE
);
1807 * If this is coming from the wrong zone, we let someone in the right
1808 * zone take care of it asynchronously. We can get here due to
1809 * VN_RELE() being called from pageout() or fsflush(). This call may
1810 * potentially turn into an expensive no-op if, for instance, v_count
1811 * gets incremented in the meantime, but it's still correct.
1813 if (nfs_zone() != VTOMI(vp
)->mi_zone
) {
1814 nfs_async_inactive(vp
, cr
, nfs3_inactive
);
1820 if (rp
->r_unldvp
!= NULL
) {
1822 * Save the vnode pointer for the directory where the
1823 * unlinked-open file got renamed, then set it to NULL
1824 * to prevent another thread from getting here before
1825 * we're done with the remove. While we have the
1826 * statelock, make local copies of the pertinent rnode
1827 * fields. If we weren't to do this in an atomic way, the
1828 * the unl* fields could become inconsistent with respect
1829 * to each other due to a race condition between this
1830 * code and nfs_remove(). See bug report 1034328.
1832 mutex_enter(&rp
->r_statelock
);
1833 if (rp
->r_unldvp
!= NULL
) {
1843 unldvp
= rp
->r_unldvp
;
1844 rp
->r_unldvp
= NULL
;
1845 unlname
= rp
->r_unlname
;
1846 rp
->r_unlname
= NULL
;
1847 unlcred
= rp
->r_unlcred
;
1848 rp
->r_unlcred
= NULL
;
1849 mutex_exit(&rp
->r_statelock
);
1852 * If there are any dirty pages left, then flush
1853 * them. This is unfortunate because they just
1854 * may get thrown away during the remove operation,
1855 * but we have to do this for correctness.
1857 if (vn_has_cached_data(vp
) &&
1858 ((rp
->r_flags
& RDIRTY
) || rp
->r_count
> 0)) {
1859 ASSERT(vp
->v_type
!= VCHR
);
1860 error
= nfs3_putpage(vp
, 0, 0, 0,
1863 mutex_enter(&rp
->r_statelock
);
1865 rp
->r_error
= error
;
1866 mutex_exit(&rp
->r_statelock
);
1871 * Do the remove operation on the renamed file
1873 setdiropargs3(&args
.object
, unlname
, unldvp
);
1879 error
= rfs3call(VTOMI(unldvp
), NFSPROC3_REMOVE
,
1880 xdr_diropargs3
, (caddr_t
)&args
,
1881 xdr_REMOVE3res
, (caddr_t
)&res
, unlcred
,
1882 &douprintf
, &res
.status
, 0, NULL
);
1885 PURGE_ATTRCACHE(unldvp
);
1887 error
= geterrno3(res
.status
);
1889 nfs3_cache_wcc_data(unldvp
,
1890 &res
.resok
.dir_wcc
, t
, cr
);
1891 if (HAVE_RDDIR_CACHE(VTOR(unldvp
)))
1892 nfs_purge_rddir_cache(unldvp
);
1894 nfs3_cache_wcc_data(unldvp
,
1895 &res
.resfail
.dir_wcc
, t
, cr
);
1896 PURGE_STALE_FH(error
, unldvp
, cr
);
1901 * Release stuff held for the remove
1904 kmem_free(unlname
, MAXNAMELEN
);
1908 mutex_exit(&rp
->r_statelock
);
1915 * Remote file system operations having to do with directory manipulation.
1920 nfs3_lookup(vnode_t
*dvp
, char *nm
, vnode_t
**vpp
, struct pathname
*pnp
,
1921 int flags
, vnode_t
*rdir
, cred_t
*cr
, caller_context_t
*ct
,
1922 int *direntflags
, pathname_t
*realpnp
)
1926 vnode_t
*avp
= NULL
;
1929 if (nfs_zone() != VTOMI(dvp
)->mi_zone
)
1935 * Are we looking up extended attributes? If so, "dvp" is
1936 * the file or directory for which we want attributes, and
1937 * we need a lookup of the hidden attribute directory
1938 * before we lookup the rest of the path.
1940 if (flags
& LOOKUP_XATTR
) {
1941 bool_t cflag
= ((flags
& CREATE_XATTR_DIR
) != 0);
1945 if (!(mi
->mi_flags
& MI_EXTATTR
))
1948 if (nfs_rw_enter_sig(&drp
->r_rwlock
, RW_READER
, INTR(dvp
)))
1951 (void) nfs3lookup_dnlc(dvp
, XATTR_DIR_NAME
, &avp
, cr
);
1953 error
= acl_getxattrdir3(dvp
, &avp
, cflag
, cr
, 0);
1957 nfs_rw_exit(&drp
->r_rwlock
);
1960 if (mi
->mi_flags
& MI_EXTATTR
)
1968 if (nfs_rw_enter_sig(&drp
->r_rwlock
, RW_READER
, INTR(dvp
))) {
1973 error
= nfs3lookup(dvp
, nm
, vpp
, pnp
, flags
, rdir
, cr
, 0);
1975 nfs_rw_exit(&drp
->r_rwlock
);
1978 * If vnode is a device, create special vnode.
1980 if (!error
&& IS_DEVVP(*vpp
)) {
1982 *vpp
= specvp(vp
, vp
->v_rdev
, vp
->v_type
, cr
);
1993 static int nfs3_lookup_neg_cache
= 1;
1996 static int nfs3_lookup_dnlc_hits
= 0;
1997 static int nfs3_lookup_dnlc_misses
= 0;
1998 static int nfs3_lookup_dnlc_neg_hits
= 0;
1999 static int nfs3_lookup_dnlc_disappears
= 0;
2000 static int nfs3_lookup_dnlc_lookups
= 0;
2005 nfs3lookup(vnode_t
*dvp
, char *nm
, vnode_t
**vpp
, struct pathname
*pnp
,
2006 int flags
, vnode_t
*rdir
, cred_t
*cr
, int rfscall_flags
)
2011 ASSERT(nfs_zone() == VTOMI(dvp
)->mi_zone
);
2013 * If lookup is for "", just return dvp. Don't need
2014 * to send it over the wire, look it up in the dnlc,
2015 * or perform any access checks.
2024 * Can't do lookups in non-directories.
2026 if (dvp
->v_type
!= VDIR
)
2030 * If we're called with RFSCALL_SOFT, it's important that
2031 * the only rfscall is one we make directly; if we permit
2032 * an access call because we're looking up "." or validating
2033 * a dnlc hit, we'll deadlock because that rfscall will not
2034 * have the RFSCALL_SOFT set.
2036 if (rfscall_flags
& RFSCALL_SOFT
)
2040 * If lookup is for ".", just return dvp. Don't need
2041 * to send it over the wire or look it up in the dnlc,
2042 * just need to check access.
2044 if (strcmp(nm
, ".") == 0) {
2045 error
= nfs3_access(dvp
, VEXEC
, 0, cr
, NULL
);
2054 if (!(drp
->r_flags
& RLOOKUP
)) {
2055 mutex_enter(&drp
->r_statelock
);
2056 drp
->r_flags
|= RLOOKUP
;
2057 mutex_exit(&drp
->r_statelock
);
2061 * Lookup this name in the DNLC. If there was a valid entry,
2062 * then return the results of the lookup.
2064 error
= nfs3lookup_dnlc(dvp
, nm
, vpp
, cr
);
2065 if (error
|| *vpp
!= NULL
)
2069 error
= nfs3lookup_otw(dvp
, nm
, vpp
, cr
, rfscall_flags
);
2075 nfs3lookup_dnlc(vnode_t
*dvp
, char *nm
, vnode_t
**vpp
, cred_t
*cr
)
2080 ASSERT(*nm
!= '\0');
2081 ASSERT(nfs_zone() == VTOMI(dvp
)->mi_zone
);
2083 * Lookup this name in the DNLC. If successful, then validate
2084 * the caches and then recheck the DNLC. The DNLC is rechecked
2085 * just in case this entry got invalidated during the call
2086 * to nfs3_validate_caches.
2088 * An assumption is being made that it is safe to say that a
2089 * file exists which may not on the server. Any operations to
2090 * the server will fail with ESTALE.
2093 nfs3_lookup_dnlc_lookups
++;
2095 vp
= dnlc_lookup(dvp
, nm
);
2098 if (vp
== DNLC_NO_VNODE
&& !vn_is_readonly(dvp
)) {
2099 PURGE_ATTRCACHE(dvp
);
2101 error
= nfs3_validate_caches(dvp
, cr
);
2104 vp
= dnlc_lookup(dvp
, nm
);
2106 error
= nfs3_access(dvp
, VEXEC
, 0, cr
, NULL
);
2111 if (vp
== DNLC_NO_VNODE
) {
2114 nfs3_lookup_dnlc_neg_hits
++;
2120 nfs3_lookup_dnlc_hits
++;
2125 nfs3_lookup_dnlc_disappears
++;
2130 nfs3_lookup_dnlc_misses
++;
2139 nfs3lookup_otw(vnode_t
*dvp
, char *nm
, vnode_t
**vpp
, cred_t
*cr
,
2147 struct vattr dvattr
;
2152 ASSERT(*nm
!= '\0');
2153 ASSERT(dvp
->v_type
== VDIR
);
2154 ASSERT(nfs_zone() == VTOMI(dvp
)->mi_zone
);
2156 setdiropargs3(&args
.what
, nm
, dvp
);
2159 fi
.fhp
= (caddr_t
)&args
.what
.dir
;
2160 fi
.copyproc
= nfs3copyfh
;
2161 fi
.lookupproc
= nfs3lookup
;
2162 fi
.xattrdirproc
= acl_getxattrdir3
;
2163 res
.obj_attributes
.fres
.vp
= dvp
;
2164 res
.obj_attributes
.fres
.vap
= &vattr
;
2165 res
.dir_attributes
.fres
.vp
= dvp
;
2166 res
.dir_attributes
.fres
.vap
= &dvattr
;
2172 error
= rfs3call(VTOMI(dvp
), NFSPROC3_LOOKUP
,
2173 xdr_diropargs3
, (caddr_t
)&args
,
2174 xdr_LOOKUP3vres
, (caddr_t
)&res
, cr
,
2175 &douprintf
, &res
.status
, rfscall_flags
, &fi
);
2180 nfs3_cache_post_op_vattr(dvp
, &res
.dir_attributes
, t
, cr
);
2182 error
= geterrno3(res
.status
);
2184 PURGE_STALE_FH(error
, dvp
, cr
);
2185 if (error
== ENOENT
&& nfs3_lookup_neg_cache
)
2186 dnlc_enter(dvp
, nm
, DNLC_NO_VNODE
);
2190 if (res
.obj_attributes
.attributes
) {
2191 vp
= makenfs3node_va(&res
.object
, res
.obj_attributes
.fres
.vap
,
2192 dvp
->v_vfsp
, t
, cr
, VTOR(dvp
)->r_path
, nm
);
2194 vp
= makenfs3node_va(&res
.object
, NULL
,
2195 dvp
->v_vfsp
, t
, cr
, VTOR(dvp
)->r_path
, nm
);
2196 if (vp
->v_type
== VNON
) {
2197 vattr
.va_mask
= VATTR_TYPE
;
2198 error
= nfs3getattr(vp
, &vattr
, cr
);
2203 vp
->v_type
= vattr
.va_type
;
2207 if (!(rfscall_flags
& RFSCALL_SOFT
))
2208 dnlc_update(dvp
, nm
, vp
);
2216 static int nfs3_create_misses
= 0;
2221 nfs3_create(vnode_t
*dvp
, char *nm
, struct vattr
*va
, enum vcexcl exclusive
,
2222 int mode
, vnode_t
**vpp
, cred_t
*cr
, int flags
, caller_context_t
*ct
,
2233 if (nfs_zone() != VTOMI(dvp
)->mi_zone
)
2235 if (nfs_rw_enter_sig(&drp
->r_rwlock
, RW_WRITER
, INTR(dvp
)))
2240 * We make a copy of the attributes because the caller does not
2241 * expect us to change what va points to.
2246 * If the pathname is "", just use dvp. Don't need
2247 * to send it over the wire, look it up in the dnlc,
2248 * or perform any access checks.
2255 * If the pathname is ".", just use dvp. Don't need
2256 * to send it over the wire or look it up in the dnlc,
2257 * just need to check access.
2259 } else if (strcmp(nm
, ".") == 0) {
2260 error
= nfs3_access(dvp
, VEXEC
, 0, cr
, ct
);
2262 nfs_rw_exit(&drp
->r_rwlock
);
2268 * We need to go over the wire, just to be sure whether the
2269 * file exists or not. Using the DNLC can be dangerous in
2270 * this case when making a decision regarding existence.
2273 error
= nfs3lookup_otw(dvp
, nm
, &vp
, cr
, 0);
2276 if (exclusive
== EXCL
)
2278 else if (vp
->v_type
== VDIR
&& (mode
& VWRITE
))
2282 * If vnode is a device, create special vnode.
2286 vp
= specvp(vp
, vp
->v_rdev
, vp
->v_type
, cr
);
2289 if (!(error
= fop_access(vp
, mode
, 0, cr
, ct
))) {
2290 if ((vattr
.va_mask
& VATTR_SIZE
) &&
2291 vp
->v_type
== VREG
) {
2292 vattr
.va_mask
= VATTR_SIZE
;
2293 error
= nfs3setattr(vp
, &vattr
, 0, cr
);
2296 * Existing file was truncated; emit a
2299 vnevent_create(vp
, ct
);
2303 nfs_rw_exit(&drp
->r_rwlock
);
2313 dnlc_remove(dvp
, nm
);
2316 * Decide what the group-id of the created file should be.
2317 * Set it in attribute list as advisory...
2319 error
= setdirgid(dvp
, &vattr
.va_gid
, cr
);
2321 nfs_rw_exit(&drp
->r_rwlock
);
2324 vattr
.va_mask
|= VATTR_GID
;
2326 ASSERT(vattr
.va_mask
& VATTR_TYPE
);
2327 if (vattr
.va_type
== VREG
) {
2328 ASSERT(vattr
.va_mask
& VATTR_MODE
);
2329 if (MANDMODE(vattr
.va_mode
)) {
2330 nfs_rw_exit(&drp
->r_rwlock
);
2333 error
= nfs3create(dvp
, nm
, &vattr
, exclusive
, mode
, vpp
, cr
);
2335 * If this is not an exclusive create, then the CREATE
2336 * request will be made with the GUARDED mode set. This
2337 * means that the server will return EEXIST if the file
2338 * exists. The file could exist because of a retransmitted
2339 * request. In this case, we recover by starting over and
2340 * checking to see whether the file exists. This second
2341 * time through it should and a CREATE request will not be
2344 * This handles the problem of a dangling CREATE request
2345 * which contains attributes which indicate that the file
2346 * should be truncated. This retransmitted request could
2347 * possibly truncate valid data in the file if not caught
2348 * by the duplicate request mechanism on the server or if
2349 * not caught by other means. The scenario is:
2351 * Client transmits CREATE request with size = 0
2352 * Client times out, retransmits request.
2353 * Response to the first request arrives from the server
2354 * and the client proceeds on.
2355 * Client writes data to the file.
2356 * The server now processes retransmitted CREATE request
2357 * and truncates file.
2359 * The use of the GUARDED CREATE request prevents this from
2360 * happening because the retransmitted CREATE would fail
2361 * with EEXIST and would not truncate the file.
2363 if (error
== EEXIST
&& exclusive
== NONEXCL
) {
2365 nfs3_create_misses
++;
2369 nfs_rw_exit(&drp
->r_rwlock
);
2372 error
= nfs3mknod(dvp
, nm
, &vattr
, exclusive
, mode
, vpp
, cr
);
2373 nfs_rw_exit(&drp
->r_rwlock
);
2379 nfs3create(vnode_t
*dvp
, char *nm
, struct vattr
*va
, enum vcexcl exclusive
,
2380 int mode
, vnode_t
**vpp
, cred_t
*cr
)
2393 ASSERT(nfs_zone() == VTOMI(dvp
)->mi_zone
);
2394 setdiropargs3(&args
.where
, nm
, dvp
);
2395 if (exclusive
== EXCL
) {
2396 args
.how
.mode
= EXCLUSIVE
;
2398 * Construct the create verifier. This verifier needs
2399 * to be unique between different clients. It also needs
2400 * to vary for each exclusive create request generated
2401 * from the client to the server.
2403 * The first attempt is made to use the hostid and a
2404 * unique number on the client. If the hostid has not
2405 * been set, the high resolution time that the exclusive
2406 * create request is being made is used. This will work
2407 * unless two different clients, both with the hostid
2408 * not set, attempt an exclusive create request on the
2409 * same file, at exactly the same clock time. The
2410 * chances of this happening seem small enough to be
2413 verfp
= (nfstime3
*)&args
.how
.createhow3_u
.verf
;
2414 verfp
->seconds
= zone_get_hostid(NULL
);
2415 if (verfp
->seconds
!= 0)
2416 verfp
->nseconds
= newnum();
2419 verfp
->seconds
= now
.tv_sec
;
2420 verfp
->nseconds
= now
.tv_nsec
;
2423 * Since the server will use this value for the mtime,
2424 * make sure that it can't overflow. Zero out the MSB.
2425 * The actual value does not matter here, only its uniqeness.
2427 verfp
->seconds
%= INT32_MAX
;
2430 * Issue the non-exclusive create in guarded mode. This
2431 * may result in some false EEXIST responses for
2432 * retransmitted requests, but these will be handled at
2433 * a higher level. By using GUARDED, duplicate requests
2434 * to do file truncation and possible access problems
2437 args
.how
.mode
= GUARDED
;
2438 error
= vattr_to_sattr3(va
,
2439 &args
.how
.createhow3_u
.obj_attributes
);
2441 /* req time field(s) overflow - return immediately */
2450 error
= rfs3call(VTOMI(dvp
), NFSPROC3_CREATE
,
2451 xdr_CREATE3args
, (caddr_t
)&args
,
2452 xdr_CREATE3res
, (caddr_t
)&res
, cr
,
2453 &douprintf
, &res
.status
, 0, NULL
);
2456 PURGE_ATTRCACHE(dvp
);
2460 error
= geterrno3(res
.status
);
2462 nfs3_cache_wcc_data(dvp
, &res
.resok
.dir_wcc
, t
, cr
);
2463 if (HAVE_RDDIR_CACHE(VTOR(dvp
)))
2464 nfs_purge_rddir_cache(dvp
);
2467 * On exclusive create the times need to be explicitly
2468 * set to clear any potential verifier that may be stored
2469 * in one of these fields (see comment below). This
2470 * is done here to cover the case where no post op attrs
2471 * were returned or a 'invalid' time was returned in
2474 if (exclusive
== EXCL
)
2475 va
->va_mask
|= (VATTR_MTIME
| VATTR_ATIME
);
2477 if (!res
.resok
.obj
.handle_follows
) {
2478 error
= nfs3lookup(dvp
, nm
, &vp
, NULL
, 0, NULL
, cr
, 0);
2482 if (res
.resok
.obj_attributes
.attributes
) {
2483 vp
= makenfs3node(&res
.resok
.obj
.handle
,
2484 &res
.resok
.obj_attributes
.attr
,
2485 dvp
->v_vfsp
, t
, cr
, NULL
, NULL
);
2487 vp
= makenfs3node(&res
.resok
.obj
.handle
, NULL
,
2488 dvp
->v_vfsp
, t
, cr
, NULL
, NULL
);
2491 * On an exclusive create, it is possible
2492 * that attributes were returned but those
2493 * postop attributes failed to decode
2494 * properly. If this is the case,
2495 * then most likely the atime or mtime
2496 * were invalid for our client; this
2497 * is caused by the server storing the
2498 * create verifier in one of the time
2499 * fields(most likely mtime).
2500 * So... we are going to setattr just the
2501 * atime/mtime to clear things up.
2503 if (exclusive
== EXCL
) {
2505 nfs3excl_create_settimes(vp
,
2508 * Setting the times failed.
2509 * Remove the file and return
2513 (void) nfs3_remove(dvp
,
2520 * This handles the non-exclusive case
2521 * and the exclusive case where no post op
2522 * attrs were returned.
2524 if (vp
->v_type
== VNON
) {
2525 vattr
.va_mask
= VATTR_TYPE
;
2526 error
= nfs3getattr(vp
, &vattr
, cr
);
2531 vp
->v_type
= vattr
.va_type
;
2534 dnlc_update(dvp
, nm
, vp
);
2537 if (exclusive
== EXCL
&&
2538 (va
->va_mask
& ~(VATTR_GID
| VATTR_SIZE
))) {
2540 * If doing an exclusive create, then generate
2541 * a SETATTR to set the initial attributes.
2542 * Try to set the mtime and the atime to the
2543 * server's current time. It is somewhat
2544 * expected that these fields will be used to
2545 * store the exclusive create cookie. If not,
2546 * server implementors will need to know that
2547 * a SETATTR will follow an exclusive create
2548 * and the cookie should be destroyed if
2549 * appropriate. This work may have been done
2550 * earlier in this function if post op attrs
2551 * were not available.
2553 * The VATTR_GID and VATTR_SIZE bits are turned off
2554 * so that the SETATTR request will not attempt
2555 * to process these. The gid will be set
2556 * separately if appropriate. The size is turned
2557 * off because it is assumed that a new file will
2558 * be created empty and if the file wasn't empty,
2559 * then the exclusive create will have failed
2560 * because the file must have existed already.
2561 * Therefore, no truncate operation is needed.
2563 va
->va_mask
&= ~(VATTR_GID
| VATTR_SIZE
);
2564 error
= nfs3setattr(vp
, va
, 0, cr
);
2567 * Couldn't correct the attributes of
2568 * the newly created file and the
2569 * attributes are wrong. Remove the
2570 * file and return an error to the
2574 (void) nfs3_remove(dvp
, nm
, cr
, NULL
, 0);
2580 if (va
->va_gid
!= rp
->r_attr
.va_gid
) {
2582 * If the gid on the file isn't right, then
2583 * generate a SETATTR to attempt to change
2584 * it. This may or may not work, depending
2585 * upon the server's semantics for allowing
2586 * file ownership changes.
2588 va
->va_mask
= VATTR_GID
;
2589 (void) nfs3setattr(vp
, va
, 0, cr
);
2593 * If vnode is a device create special vnode
2596 *vpp
= specvp(vp
, vp
->v_rdev
, vp
->v_type
, cr
);
2601 nfs3_cache_wcc_data(dvp
, &res
.resfail
.dir_wcc
, t
, cr
);
2602 PURGE_STALE_FH(error
, dvp
, cr
);
2609 * Special setattr function to take care of rest of atime/mtime
2610 * after successful exclusive create. This function exists to avoid
2611 * handling attributes from the server; exclusive the atime/mtime fields
2612 * may be 'invalid' in client's view and therefore can not be trusted.
2615 nfs3excl_create_settimes(vnode_t
*vp
, struct vattr
*vap
, cred_t
*cr
)
2625 ASSERT(nfs_zone() == VTOMI(vp
)->mi_zone
);
2626 /* save the caller's mask so that it can be reset later */
2627 mask
= vap
->va_mask
;
2631 args
.object
= *RTOFH3(rp
);
2632 args
.guard
.check
= FALSE
;
2634 /* Use the mask to initialize the arguments */
2636 error
= vattr_to_sattr3(vap
, &args
.new_attributes
);
2638 /* We want to set just atime/mtime on this request */
2639 args
.new_attributes
.atime
.set_it
= SET_TO_SERVER_TIME
;
2640 args
.new_attributes
.mtime
.set_it
= SET_TO_SERVER_TIME
;
2646 error
= rfs3call(VTOMI(vp
), NFSPROC3_SETATTR
,
2647 xdr_SETATTR3args
, (caddr_t
)&args
,
2648 xdr_SETATTR3res
, (caddr_t
)&res
, cr
,
2649 &douprintf
, &res
.status
, 0, NULL
);
2652 vap
->va_mask
= mask
;
2656 error
= geterrno3(res
.status
);
2659 * It is important to pick up the attributes.
2660 * Since this is the exclusive create path, the
2661 * attributes on the initial create were ignored
2662 * and we need these to have the correct info.
2664 nfs3_cache_wcc_data(vp
, &res
.resok
.obj_wcc
, t
, cr
);
2666 * No need to do the atime/mtime work again so clear
2669 mask
&= ~(VATTR_ATIME
| VATTR_MTIME
);
2671 nfs3_cache_wcc_data(vp
, &res
.resfail
.obj_wcc
, t
, cr
);
2674 vap
->va_mask
= mask
;
2681 nfs3mknod(vnode_t
*dvp
, char *nm
, struct vattr
*va
, enum vcexcl exclusive
,
2682 int mode
, vnode_t
**vpp
, cred_t
*cr
)
2692 ASSERT(nfs_zone() == VTOMI(dvp
)->mi_zone
);
2693 switch (va
->va_type
) {
2696 setdiropargs3(&args
.where
, nm
, dvp
);
2697 args
.what
.type
= (va
->va_type
== VCHR
) ? NF3CHR
: NF3BLK
;
2698 error
= vattr_to_sattr3(va
,
2699 &args
.what
.mknoddata3_u
.device
.dev_attributes
);
2701 /* req time field(s) overflow - return immediately */
2704 args
.what
.mknoddata3_u
.device
.spec
.specdata1
=
2705 getmajor(va
->va_rdev
);
2706 args
.what
.mknoddata3_u
.device
.spec
.specdata2
=
2707 getminor(va
->va_rdev
);
2712 setdiropargs3(&args
.where
, nm
, dvp
);
2713 args
.what
.type
= (va
->va_type
== VFIFO
) ? NF3FIFO
: NF3SOCK
;
2714 error
= vattr_to_sattr3(va
,
2715 &args
.what
.mknoddata3_u
.pipe_attributes
);
2717 /* req time field(s) overflow - return immediately */
2730 error
= rfs3call(VTOMI(dvp
), NFSPROC3_MKNOD
,
2731 xdr_MKNOD3args
, (caddr_t
)&args
,
2732 xdr_MKNOD3res
, (caddr_t
)&res
, cr
,
2733 &douprintf
, &res
.status
, 0, NULL
);
2736 PURGE_ATTRCACHE(dvp
);
2740 error
= geterrno3(res
.status
);
2742 nfs3_cache_wcc_data(dvp
, &res
.resok
.dir_wcc
, t
, cr
);
2743 if (HAVE_RDDIR_CACHE(VTOR(dvp
)))
2744 nfs_purge_rddir_cache(dvp
);
2746 if (!res
.resok
.obj
.handle_follows
) {
2747 error
= nfs3lookup(dvp
, nm
, &vp
, NULL
, 0, NULL
, cr
, 0);
2751 if (res
.resok
.obj_attributes
.attributes
) {
2752 vp
= makenfs3node(&res
.resok
.obj
.handle
,
2753 &res
.resok
.obj_attributes
.attr
,
2754 dvp
->v_vfsp
, t
, cr
, NULL
, NULL
);
2756 vp
= makenfs3node(&res
.resok
.obj
.handle
, NULL
,
2757 dvp
->v_vfsp
, t
, cr
, NULL
, NULL
);
2758 if (vp
->v_type
== VNON
) {
2759 vattr
.va_mask
= VATTR_TYPE
;
2760 error
= nfs3getattr(vp
, &vattr
, cr
);
2765 vp
->v_type
= vattr
.va_type
;
2769 dnlc_update(dvp
, nm
, vp
);
2772 if (va
->va_gid
!= VTOR(vp
)->r_attr
.va_gid
) {
2773 va
->va_mask
= VATTR_GID
;
2774 (void) nfs3setattr(vp
, va
, 0, cr
);
2778 * If vnode is a device create special vnode
2781 *vpp
= specvp(vp
, vp
->v_rdev
, vp
->v_type
, cr
);
2786 nfs3_cache_wcc_data(dvp
, &res
.resfail
.dir_wcc
, t
, cr
);
2787 PURGE_STALE_FH(error
, dvp
, cr
);
2793 * Weirdness: if the vnode to be removed is open
2794 * we rename it instead of removing it and nfs_inactive
2795 * will remove the new name.
2799 nfs3_remove(vnode_t
*dvp
, char *nm
, cred_t
*cr
, caller_context_t
*ct
, int flags
)
2811 if (nfs_zone() != VTOMI(dvp
)->mi_zone
)
2814 if (nfs_rw_enter_sig(&drp
->r_rwlock
, RW_WRITER
, INTR(dvp
)))
2817 error
= nfs3lookup(dvp
, nm
, &vp
, NULL
, 0, NULL
, cr
, 0);
2819 nfs_rw_exit(&drp
->r_rwlock
);
2823 if (vp
->v_type
== VDIR
&& secpolicy_fs_linkdir(cr
, dvp
->v_vfsp
)) {
2825 nfs_rw_exit(&drp
->r_rwlock
);
2830 * First just remove the entry from the name cache, as it
2831 * is most likely the only entry for this vp.
2833 dnlc_remove(dvp
, nm
);
2836 * If the file has a v_count > 1 then there may be more than one
2837 * entry in the name cache due multiple links or an open file,
2838 * but we don't have the real reference count so flush all
2841 if (vp
->v_count
> 1)
2845 * Now we have the real reference count on the vnode
2848 mutex_enter(&rp
->r_statelock
);
2849 if (vp
->v_count
> 1 &&
2850 (rp
->r_unldvp
== NULL
|| strcmp(nm
, rp
->r_unlname
) == 0)) {
2851 mutex_exit(&rp
->r_statelock
);
2852 tmpname
= newname();
2853 error
= nfs3rename(dvp
, nm
, dvp
, tmpname
, cr
, ct
);
2855 kmem_free(tmpname
, MAXNAMELEN
);
2857 mutex_enter(&rp
->r_statelock
);
2858 if (rp
->r_unldvp
== NULL
) {
2861 if (rp
->r_unlcred
!= NULL
)
2862 crfree(rp
->r_unlcred
);
2865 rp
->r_unlname
= tmpname
;
2867 kmem_free(rp
->r_unlname
, MAXNAMELEN
);
2868 rp
->r_unlname
= tmpname
;
2870 mutex_exit(&rp
->r_statelock
);
2873 mutex_exit(&rp
->r_statelock
);
2875 * We need to flush any dirty pages which happen to
2876 * be hanging around before removing the file. This
2877 * shouldn't happen very often and mostly on file
2878 * systems mounted "nocto".
2880 if (vn_has_cached_data(vp
) &&
2881 ((rp
->r_flags
& RDIRTY
) || rp
->r_count
> 0)) {
2882 error
= nfs3_putpage(vp
, 0, 0, 0, cr
, ct
);
2883 if (error
&& (error
== ENOSPC
|| error
== EDQUOT
)) {
2884 mutex_enter(&rp
->r_statelock
);
2886 rp
->r_error
= error
;
2887 mutex_exit(&rp
->r_statelock
);
2891 setdiropargs3(&args
.object
, nm
, dvp
);
2897 error
= rfs3call(VTOMI(dvp
), NFSPROC3_REMOVE
,
2898 xdr_diropargs3
, (caddr_t
)&args
,
2899 xdr_REMOVE3res
, (caddr_t
)&res
, cr
,
2900 &douprintf
, &res
.status
, 0, NULL
);
2903 * The xattr dir may be gone after last attr is removed,
2904 * so flush it from dnlc.
2906 if (dvp
->v_flag
& V_XATTRDIR
)
2909 PURGE_ATTRCACHE(vp
);
2912 PURGE_ATTRCACHE(dvp
);
2914 error
= geterrno3(res
.status
);
2916 nfs3_cache_wcc_data(dvp
, &res
.resok
.dir_wcc
, t
,
2918 if (HAVE_RDDIR_CACHE(drp
))
2919 nfs_purge_rddir_cache(dvp
);
2921 nfs3_cache_wcc_data(dvp
, &res
.resfail
.dir_wcc
,
2923 PURGE_STALE_FH(error
, dvp
, cr
);
2929 vnevent_remove(vp
, dvp
, nm
, ct
);
2933 nfs_rw_exit(&drp
->r_rwlock
);
2940 nfs3_link(vnode_t
*tdvp
, vnode_t
*svp
, char *tnm
, cred_t
*cr
,
2941 caller_context_t
*ct
, int flags
)
2952 if (nfs_zone() != VTOMI(tdvp
)->mi_zone
)
2954 if (fop_realvp(svp
, &realvp
, ct
) == 0)
2959 if (!(mi
->mi_flags
& MI_LINK
))
2960 return (EOPNOTSUPP
);
2962 args
.file
= *VTOFH3(svp
);
2963 setdiropargs3(&args
.link
, tnm
, tdvp
);
2966 if (nfs_rw_enter_sig(&tdrp
->r_rwlock
, RW_WRITER
, INTR(tdvp
)))
2969 dnlc_remove(tdvp
, tnm
);
2975 error
= rfs3call(mi
, NFSPROC3_LINK
,
2976 xdr_LINK3args
, (caddr_t
)&args
,
2977 xdr_LINK3res
, (caddr_t
)&res
, cr
,
2978 &douprintf
, &res
.status
, 0, NULL
);
2981 PURGE_ATTRCACHE(tdvp
);
2982 PURGE_ATTRCACHE(svp
);
2983 nfs_rw_exit(&tdrp
->r_rwlock
);
2987 error
= geterrno3(res
.status
);
2990 nfs3_cache_post_op_attr(svp
, &res
.resok
.file_attributes
, t
, cr
);
2991 nfs3_cache_wcc_data(tdvp
, &res
.resok
.linkdir_wcc
, t
, cr
);
2992 if (HAVE_RDDIR_CACHE(tdrp
))
2993 nfs_purge_rddir_cache(tdvp
);
2994 dnlc_update(tdvp
, tnm
, svp
);
2996 nfs3_cache_post_op_attr(svp
, &res
.resfail
.file_attributes
, t
,
2998 nfs3_cache_wcc_data(tdvp
, &res
.resfail
.linkdir_wcc
, t
, cr
);
2999 if (error
== EOPNOTSUPP
) {
3000 mutex_enter(&mi
->mi_lock
);
3001 mi
->mi_flags
&= ~MI_LINK
;
3002 mutex_exit(&mi
->mi_lock
);
3006 nfs_rw_exit(&tdrp
->r_rwlock
);
3010 * Notify the source file of this link operation.
3012 vnevent_link(svp
, ct
);
3019 nfs3_rename(vnode_t
*odvp
, char *onm
, vnode_t
*ndvp
, char *nnm
, cred_t
*cr
,
3020 caller_context_t
*ct
, int flags
)
3024 if (nfs_zone() != VTOMI(odvp
)->mi_zone
)
3026 if (fop_realvp(ndvp
, &realvp
, ct
) == 0)
3029 return (nfs3rename(odvp
, onm
, ndvp
, nnm
, cr
, ct
));
3033 * nfs3rename does the real work of renaming in NFS Version 3.
3036 nfs3rename(vnode_t
*odvp
, char *onm
, vnode_t
*ndvp
, char *nnm
, cred_t
*cr
,
3037 caller_context_t
*ct
)
3043 vnode_t
*nvp
= NULL
;
3044 vnode_t
*ovp
= NULL
;
3051 ASSERT(nfs_zone() == VTOMI(odvp
)->mi_zone
);
3053 if (strcmp(onm
, ".") == 0 || strcmp(onm
, "..") == 0 ||
3054 strcmp(nnm
, ".") == 0 || strcmp(nnm
, "..") == 0)
3059 if ((intptr_t)odrp
< (intptr_t)ndrp
) {
3060 if (nfs_rw_enter_sig(&odrp
->r_rwlock
, RW_WRITER
, INTR(odvp
)))
3062 if (nfs_rw_enter_sig(&ndrp
->r_rwlock
, RW_WRITER
, INTR(ndvp
))) {
3063 nfs_rw_exit(&odrp
->r_rwlock
);
3067 if (nfs_rw_enter_sig(&ndrp
->r_rwlock
, RW_WRITER
, INTR(ndvp
)))
3069 if (nfs_rw_enter_sig(&odrp
->r_rwlock
, RW_WRITER
, INTR(odvp
))) {
3070 nfs_rw_exit(&ndrp
->r_rwlock
);
3076 * Lookup the target file. If it exists, it needs to be
3077 * checked to see whether it is a mount point and whether
3078 * it is active (open).
3080 error
= nfs3lookup(ndvp
, nnm
, &nvp
, NULL
, 0, NULL
, cr
, 0);
3083 * If this file has been mounted on, then just
3084 * return busy because renaming to it would remove
3085 * the mounted file system from the name space.
3087 if (vn_mountedvfs(nvp
) != NULL
) {
3089 nfs_rw_exit(&odrp
->r_rwlock
);
3090 nfs_rw_exit(&ndrp
->r_rwlock
);
3095 * Purge the name cache of all references to this vnode
3096 * so that we can check the reference count to infer
3097 * whether it is active or not.
3100 * First just remove the entry from the name cache, as it
3101 * is most likely the only entry for this vp.
3103 dnlc_remove(ndvp
, nnm
);
3105 * If the file has a v_count > 1 then there may be more
3106 * than one entry in the name cache due multiple links
3107 * or an open file, but we don't have the real reference
3108 * count so flush all possible entries.
3110 if (nvp
->v_count
> 1)
3114 * If the vnode is active and is not a directory,
3115 * arrange to rename it to a
3116 * temporary file so that it will continue to be
3117 * accessible. This implements the "unlink-open-file"
3118 * semantics for the target of a rename operation.
3119 * Before doing this though, make sure that the
3120 * source and target files are not already the same.
3122 if (nvp
->v_count
> 1 && nvp
->v_type
!= VDIR
) {
3124 * Lookup the source name.
3126 error
= nfs3lookup(odvp
, onm
, &ovp
, NULL
, 0, NULL
,
3130 * The source name *should* already exist.
3134 nfs_rw_exit(&odrp
->r_rwlock
);
3135 nfs_rw_exit(&ndrp
->r_rwlock
);
3140 * Compare the two vnodes. If they are the same,
3141 * just release all held vnodes and return success.
3146 nfs_rw_exit(&odrp
->r_rwlock
);
3147 nfs_rw_exit(&ndrp
->r_rwlock
);
3152 * Can't mix and match directories and non-
3153 * directories in rename operations. We already
3154 * know that the target is not a directory. If
3155 * the source is a directory, return an error.
3157 if (ovp
->v_type
== VDIR
) {
3160 nfs_rw_exit(&odrp
->r_rwlock
);
3161 nfs_rw_exit(&ndrp
->r_rwlock
);
3166 * The target file exists, is not the same as
3167 * the source file, and is active. Link it
3168 * to a temporary filename to avoid having
3169 * the server removing the file completely.
3171 tmpname
= newname();
3172 error
= nfs3_link(ndvp
, nvp
, tmpname
, cr
, NULL
, 0);
3173 if (error
== EOPNOTSUPP
) {
3174 error
= nfs3_rename(ndvp
, nnm
, ndvp
, tmpname
,
3178 kmem_free(tmpname
, MAXNAMELEN
);
3181 nfs_rw_exit(&odrp
->r_rwlock
);
3182 nfs_rw_exit(&ndrp
->r_rwlock
);
3186 mutex_enter(&rp
->r_statelock
);
3187 if (rp
->r_unldvp
== NULL
) {
3189 rp
->r_unldvp
= ndvp
;
3190 if (rp
->r_unlcred
!= NULL
)
3191 crfree(rp
->r_unlcred
);
3194 rp
->r_unlname
= tmpname
;
3196 kmem_free(rp
->r_unlname
, MAXNAMELEN
);
3197 rp
->r_unlname
= tmpname
;
3199 mutex_exit(&rp
->r_statelock
);
3205 * When renaming directories to be a subdirectory of a
3206 * different parent, the dnlc entry for ".." will no
3207 * longer be valid, so it must be removed.
3209 * We do a lookup here to determine whether we are renaming
3210 * a directory and we need to check if we are renaming
3211 * an unlinked file. This might have already been done
3212 * in previous code, so we check ovp == NULL to avoid
3216 error
= nfs3lookup(odvp
, onm
, &ovp
, NULL
, 0, NULL
, cr
, 0);
3218 * The source name *should* already exist.
3221 nfs_rw_exit(&odrp
->r_rwlock
);
3222 nfs_rw_exit(&ndrp
->r_rwlock
);
3228 ASSERT(ovp
!= NULL
);
3231 dnlc_remove(odvp
, onm
);
3232 dnlc_remove(ndvp
, nnm
);
3234 setdiropargs3(&args
.from
, onm
, odvp
);
3235 setdiropargs3(&args
.to
, nnm
, ndvp
);
3241 error
= rfs3call(VTOMI(odvp
), NFSPROC3_RENAME
,
3242 xdr_RENAME3args
, (caddr_t
)&args
,
3243 xdr_RENAME3res
, (caddr_t
)&res
, cr
,
3244 &douprintf
, &res
.status
, 0, NULL
);
3247 PURGE_ATTRCACHE(odvp
);
3248 PURGE_ATTRCACHE(ndvp
);
3250 nfs_rw_exit(&odrp
->r_rwlock
);
3251 nfs_rw_exit(&ndrp
->r_rwlock
);
3258 error
= geterrno3(res
.status
);
3261 nfs3_cache_wcc_data(odvp
, &res
.resok
.fromdir_wcc
, t
, cr
);
3262 if (HAVE_RDDIR_CACHE(odrp
))
3263 nfs_purge_rddir_cache(odvp
);
3265 nfs3_cache_wcc_data(ndvp
, &res
.resok
.todir_wcc
, t
, cr
);
3266 if (HAVE_RDDIR_CACHE(ndrp
))
3267 nfs_purge_rddir_cache(ndvp
);
3270 * when renaming directories to be a subdirectory of a
3271 * different parent, the dnlc entry for ".." will no
3272 * longer be valid, so it must be removed
3276 if (ovp
->v_type
== VDIR
) {
3277 dnlc_remove(ovp
, "..");
3278 if (HAVE_RDDIR_CACHE(rp
))
3279 nfs_purge_rddir_cache(ovp
);
3284 * If we are renaming the unlinked file, update the
3285 * r_unldvp and r_unlname as needed.
3287 mutex_enter(&rp
->r_statelock
);
3288 if (rp
->r_unldvp
!= NULL
) {
3289 if (strcmp(rp
->r_unlname
, onm
) == 0) {
3290 (void) strncpy(rp
->r_unlname
, nnm
, MAXNAMELEN
);
3291 rp
->r_unlname
[MAXNAMELEN
- 1] = '\0';
3293 if (ndvp
!= rp
->r_unldvp
) {
3294 VN_RELE(rp
->r_unldvp
);
3295 rp
->r_unldvp
= ndvp
;
3300 mutex_exit(&rp
->r_statelock
);
3302 nfs3_cache_wcc_data(odvp
, &res
.resfail
.fromdir_wcc
, t
, cr
);
3304 nfs3_cache_wcc_data(ndvp
, &res
.resfail
.todir_wcc
, t
,
3308 * System V defines rename to return EEXIST, not
3309 * ENOTEMPTY if the target directory is not empty.
3310 * Over the wire, the error is NFSERR_ENOTEMPTY
3311 * which geterrno maps to ENOTEMPTY.
3313 if (error
== ENOTEMPTY
)
3319 vnevent_rename_dest(nvp
, ndvp
, nnm
, ct
);
3322 vnevent_rename_dest_dir(ndvp
, ct
);
3323 ASSERT(ovp
!= NULL
);
3324 vnevent_rename_src(ovp
, odvp
, onm
, ct
);
3332 nfs_rw_exit(&odrp
->r_rwlock
);
3333 nfs_rw_exit(&ndrp
->r_rwlock
);
3340 nfs3_mkdir(vnode_t
*dvp
, char *nm
, struct vattr
*va
, vnode_t
**vpp
, cred_t
*cr
,
3341 caller_context_t
*ct
, int flags
, vsecattr_t
*vsecp
)
3352 if (nfs_zone() != VTOMI(dvp
)->mi_zone
)
3354 setdiropargs3(&args
.where
, nm
, dvp
);
3357 * Decide what the group-id and set-gid bit of the created directory
3358 * should be. May have to do a setattr to get the gid right.
3360 error
= setdirgid(dvp
, &va
->va_gid
, cr
);
3363 error
= setdirmode(dvp
, &va
->va_mode
, cr
);
3366 va
->va_mask
|= VATTR_MODE
|VATTR_GID
;
3368 error
= vattr_to_sattr3(va
, &args
.attributes
);
3370 /* req time field(s) overflow - return immediately */
3375 if (nfs_rw_enter_sig(&drp
->r_rwlock
, RW_WRITER
, INTR(dvp
)))
3378 dnlc_remove(dvp
, nm
);
3384 error
= rfs3call(VTOMI(dvp
), NFSPROC3_MKDIR
,
3385 xdr_MKDIR3args
, (caddr_t
)&args
,
3386 xdr_MKDIR3res
, (caddr_t
)&res
, cr
,
3387 &douprintf
, &res
.status
, 0, NULL
);
3390 PURGE_ATTRCACHE(dvp
);
3391 nfs_rw_exit(&drp
->r_rwlock
);
3395 error
= geterrno3(res
.status
);
3397 nfs3_cache_wcc_data(dvp
, &res
.resok
.dir_wcc
, t
, cr
);
3398 if (HAVE_RDDIR_CACHE(drp
))
3399 nfs_purge_rddir_cache(dvp
);
3401 if (!res
.resok
.obj
.handle_follows
) {
3402 error
= nfs3lookup(dvp
, nm
, &vp
, NULL
, 0, NULL
, cr
, 0);
3404 nfs_rw_exit(&drp
->r_rwlock
);
3408 if (res
.resok
.obj_attributes
.attributes
) {
3409 vp
= makenfs3node(&res
.resok
.obj
.handle
,
3410 &res
.resok
.obj_attributes
.attr
,
3411 dvp
->v_vfsp
, t
, cr
, NULL
, NULL
);
3413 vp
= makenfs3node(&res
.resok
.obj
.handle
, NULL
,
3414 dvp
->v_vfsp
, t
, cr
, NULL
, NULL
);
3415 if (vp
->v_type
== VNON
) {
3416 vattr
.va_mask
= VATTR_TYPE
;
3417 error
= nfs3getattr(vp
, &vattr
, cr
);
3420 nfs_rw_exit(&drp
->r_rwlock
);
3423 vp
->v_type
= vattr
.va_type
;
3426 dnlc_update(dvp
, nm
, vp
);
3428 if (va
->va_gid
!= VTOR(vp
)->r_attr
.va_gid
) {
3429 va
->va_mask
= VATTR_GID
;
3430 (void) nfs3setattr(vp
, va
, 0, cr
);
3434 nfs3_cache_wcc_data(dvp
, &res
.resfail
.dir_wcc
, t
, cr
);
3435 PURGE_STALE_FH(error
, dvp
, cr
);
3438 nfs_rw_exit(&drp
->r_rwlock
);
3445 nfs3_rmdir(vnode_t
*dvp
, char *nm
, vnode_t
*cdir
, cred_t
*cr
,
3446 caller_context_t
*ct
, int flags
)
3456 if (nfs_zone() != VTOMI(dvp
)->mi_zone
)
3459 if (nfs_rw_enter_sig(&drp
->r_rwlock
, RW_WRITER
, INTR(dvp
)))
3463 * Attempt to prevent a rmdir(".") from succeeding.
3465 error
= nfs3lookup(dvp
, nm
, &vp
, NULL
, 0, NULL
, cr
, 0);
3467 nfs_rw_exit(&drp
->r_rwlock
);
3473 nfs_rw_exit(&drp
->r_rwlock
);
3477 setdiropargs3(&args
.object
, nm
, dvp
);
3480 * First just remove the entry from the name cache, as it
3481 * is most likely an entry for this vp.
3483 dnlc_remove(dvp
, nm
);
3486 * If there vnode reference count is greater than one, then
3487 * there may be additional references in the DNLC which will
3488 * need to be purged. First, trying removing the entry for
3489 * the parent directory and see if that removes the additional
3490 * reference(s). If that doesn't do it, then use dnlc_purge_vp
3491 * to completely remove any references to the directory which
3492 * might still exist in the DNLC.
3494 if (vp
->v_count
> 1) {
3495 dnlc_remove(vp
, "..");
3496 if (vp
->v_count
> 1)
3504 error
= rfs3call(VTOMI(dvp
), NFSPROC3_RMDIR
,
3505 xdr_diropargs3
, (caddr_t
)&args
,
3506 xdr_RMDIR3res
, (caddr_t
)&res
, cr
,
3507 &douprintf
, &res
.status
, 0, NULL
);
3509 PURGE_ATTRCACHE(vp
);
3512 PURGE_ATTRCACHE(dvp
);
3514 nfs_rw_exit(&drp
->r_rwlock
);
3518 error
= geterrno3(res
.status
);
3520 nfs3_cache_wcc_data(dvp
, &res
.resok
.dir_wcc
, t
, cr
);
3521 if (HAVE_RDDIR_CACHE(drp
))
3522 nfs_purge_rddir_cache(dvp
);
3523 if (HAVE_RDDIR_CACHE(VTOR(vp
)))
3524 nfs_purge_rddir_cache(vp
);
3526 nfs3_cache_wcc_data(dvp
, &res
.resfail
.dir_wcc
, t
, cr
);
3527 PURGE_STALE_FH(error
, dvp
, cr
);
3529 * System V defines rmdir to return EEXIST, not
3530 * ENOTEMPTY if the directory is not empty. Over
3531 * the wire, the error is NFSERR_ENOTEMPTY which
3532 * geterrno maps to ENOTEMPTY.
3534 if (error
== ENOTEMPTY
)
3539 vnevent_rmdir(vp
, dvp
, nm
, ct
);
3543 nfs_rw_exit(&drp
->r_rwlock
);
3550 nfs3_symlink(vnode_t
*dvp
, char *lnm
, struct vattr
*tva
, char *tnm
, cred_t
*cr
,
3551 caller_context_t
*ct
, int flags
)
3566 if (nfs_zone() != mi
->mi_zone
)
3568 if (!(mi
->mi_flags
& MI_SYMLINK
))
3569 return (EOPNOTSUPP
);
3571 setdiropargs3(&args
.where
, lnm
, dvp
);
3572 error
= vattr_to_sattr3(tva
, &args
.symlink
.symlink_attributes
);
3574 /* req time field(s) overflow - return immediately */
3577 args
.symlink
.symlink_data
= tnm
;
3580 if (nfs_rw_enter_sig(&drp
->r_rwlock
, RW_WRITER
, INTR(dvp
)))
3583 dnlc_remove(dvp
, lnm
);
3589 error
= rfs3call(mi
, NFSPROC3_SYMLINK
,
3590 xdr_SYMLINK3args
, (caddr_t
)&args
,
3591 xdr_SYMLINK3res
, (caddr_t
)&res
, cr
,
3592 &douprintf
, &res
.status
, 0, NULL
);
3595 PURGE_ATTRCACHE(dvp
);
3596 nfs_rw_exit(&drp
->r_rwlock
);
3600 error
= geterrno3(res
.status
);
3602 nfs3_cache_wcc_data(dvp
, &res
.resok
.dir_wcc
, t
, cr
);
3603 if (HAVE_RDDIR_CACHE(drp
))
3604 nfs_purge_rddir_cache(dvp
);
3606 if (res
.resok
.obj
.handle_follows
) {
3607 if (res
.resok
.obj_attributes
.attributes
) {
3608 vp
= makenfs3node(&res
.resok
.obj
.handle
,
3609 &res
.resok
.obj_attributes
.attr
,
3610 dvp
->v_vfsp
, t
, cr
, NULL
, NULL
);
3612 vp
= makenfs3node(&res
.resok
.obj
.handle
, NULL
,
3613 dvp
->v_vfsp
, t
, cr
, NULL
, NULL
);
3617 dnlc_update(dvp
, lnm
, vp
);
3619 if (nfs3_do_symlink_cache
&&
3620 rp
->r_symlink
.contents
== NULL
) {
3622 contents
= kmem_alloc(MAXPATHLEN
,
3625 if (contents
!= NULL
) {
3626 mutex_enter(&rp
->r_statelock
);
3627 if (rp
->r_symlink
.contents
== NULL
) {
3628 rp
->r_symlink
.len
= strlen(tnm
);
3629 bcopy(tnm
, contents
,
3631 rp
->r_symlink
.contents
=
3633 rp
->r_symlink
.size
= MAXPATHLEN
;
3634 mutex_exit(&rp
->r_statelock
);
3636 mutex_exit(&rp
->r_statelock
);
3637 kmem_free((void *)contents
,
3645 nfs3_cache_wcc_data(dvp
, &res
.resfail
.dir_wcc
, t
, cr
);
3646 PURGE_STALE_FH(error
, dvp
, cr
);
3647 if (error
== EOPNOTSUPP
) {
3648 mutex_enter(&mi
->mi_lock
);
3649 mi
->mi_flags
&= ~MI_SYMLINK
;
3650 mutex_exit(&mi
->mi_lock
);
3654 nfs_rw_exit(&drp
->r_rwlock
);
3660 static int nfs3_readdir_cache_hits
= 0;
3661 static int nfs3_readdir_cache_shorts
= 0;
3662 static int nfs3_readdir_cache_waits
= 0;
3663 static int nfs3_readdir_cache_misses
= 0;
3664 static int nfs3_readdir_readahead
= 0;
3667 static int nfs3_shrinkreaddir
= 0;
3670 * Read directory entries.
3671 * There are some weird things to look out for here. The uio_loffset
3672 * field is either 0 or it is the offset returned from a previous
3673 * readdir. It is an opaque value used by the server to find the
3674 * correct directory block to read. The count field is the number
3675 * of blocks to read on the server. This is advisory only, the server
3676 * may return only one block's worth of entries. Entries may be compressed
3681 nfs3_readdir(vnode_t
*vp
, struct uio
*uiop
, cred_t
*cr
, int *eofp
,
3682 caller_context_t
*ct
, int flags
)
3697 if (nfs_zone() != VTOMI(vp
)->mi_zone
)
3701 ASSERT(nfs_rw_lock_held(&rp
->r_rwlock
, RW_READER
));
3704 * Make sure that the directory cache is valid.
3706 if (HAVE_RDDIR_CACHE(rp
)) {
3707 if (nfs_disable_rddir_cache
) {
3709 * Setting nfs_disable_rddir_cache in /etc/system
3710 * allows interoperability with servers that do not
3711 * properly update the attributes of directories.
3712 * Any cached information gets purged before an
3713 * access is made to it.
3715 nfs_purge_rddir_cache(vp
);
3717 error
= nfs3_validate_caches(vp
, cr
);
3724 * It is possible that some servers may not be able to correctly
3725 * handle a large READDIR or READDIRPLUS request due to bugs in
3726 * their implementation. In order to continue to interoperate
3727 * with them, this workaround is provided to limit the maximum
3728 * size of a READDIRPLUS request to 1024. In any case, the request
3729 * size is limited to MAXBSIZE.
3731 count
= MIN(uiop
->uio_iov
->iov_len
,
3732 nfs3_shrinkreaddir
? 1024 : MAXBSIZE
);
3740 * Short circuit last readdir which always returns 0 bytes.
3741 * This can be done after the directory has been read through
3742 * completely at least once. This will set r_direof which
3743 * can be used to find the value of the last cookie.
3745 mutex_enter(&rp
->r_statelock
);
3746 if (rp
->r_direof
!= NULL
&&
3747 uiop
->uio_loffset
== rp
->r_direof
->nfs3_ncookie
) {
3748 mutex_exit(&rp
->r_statelock
);
3750 nfs3_readdir_cache_shorts
++;
3755 rddir_cache_rele(nrdc
);
3759 * Look for a cache entry. Cache entries are identified
3760 * by the NFS cookie value and the byte count requested.
3762 srdc
.nfs3_cookie
= uiop
->uio_loffset
;
3763 srdc
.buflen
= count
;
3764 rdc
= avl_find(&rp
->r_dir
, &srdc
, &where
);
3766 rddir_cache_hold(rdc
);
3768 * If the cache entry is in the process of being
3769 * filled in, wait until this completes. The
3770 * RDDIRWAIT bit is set to indicate that someone
3771 * is waiting and then the thread currently
3772 * filling the entry is done, it should do a
3773 * cv_broadcast to wakeup all of the threads
3774 * waiting for it to finish.
3776 if (rdc
->flags
& RDDIR
) {
3777 nfs_rw_exit(&rp
->r_rwlock
);
3778 rdc
->flags
|= RDDIRWAIT
;
3780 nfs3_readdir_cache_waits
++;
3782 if (!cv_wait_sig(&rdc
->cv
, &rp
->r_statelock
)) {
3784 * We got interrupted, probably
3785 * the user typed ^C or an alarm
3786 * fired. We free the new entry
3787 * if we allocated one.
3789 mutex_exit(&rp
->r_statelock
);
3790 (void) nfs_rw_enter_sig(&rp
->r_rwlock
,
3792 rddir_cache_rele(rdc
);
3794 rddir_cache_rele(nrdc
);
3797 mutex_exit(&rp
->r_statelock
);
3798 (void) nfs_rw_enter_sig(&rp
->r_rwlock
,
3800 rddir_cache_rele(rdc
);
3804 * Check to see if a readdir is required to
3805 * fill the entry. If so, mark this entry
3806 * as being filled, remove our reference,
3807 * and branch to the code to fill the entry.
3809 if (rdc
->flags
& RDDIRREQ
) {
3810 rdc
->flags
&= ~RDDIRREQ
;
3811 rdc
->flags
|= RDDIR
;
3813 rddir_cache_rele(nrdc
);
3815 mutex_exit(&rp
->r_statelock
);
3820 nfs3_readdir_cache_hits
++;
3823 * If an error occurred while attempting
3824 * to fill the cache entry, just return it.
3828 mutex_exit(&rp
->r_statelock
);
3829 rddir_cache_rele(rdc
);
3831 rddir_cache_rele(nrdc
);
3836 * The cache entry is complete and good,
3837 * copyout the dirent structs to the calling
3840 error
= uiomove(rdc
->entries
, rdc
->entlen
, UIO_READ
, uiop
);
3843 * If no error occurred during the copyout,
3844 * update the offset in the uio struct to
3845 * contain the value of the next cookie
3846 * and set the eof value appropriately.
3849 uiop
->uio_loffset
= rdc
->nfs3_ncookie
;
3855 * Decide whether to do readahead.
3857 * Don't if have already read to the end of
3858 * directory. There is nothing more to read.
3860 * Don't if the application is not doing
3861 * lookups in the directory. The readahead
3862 * is only effective if the application can
3863 * be doing work while an async thread is
3864 * handling the over the wire request.
3868 doreadahead
= FALSE
;
3869 } else if (!(rp
->r_flags
& RLOOKUP
))
3870 doreadahead
= FALSE
;
3875 mutex_exit(&rp
->r_statelock
);
3876 rddir_cache_rele(rdc
);
3878 rddir_cache_rele(nrdc
);
3883 * Check to see whether we found an entry
3884 * for the readahead. If so, we don't need
3885 * to do anything further, so free the new
3886 * entry if one was allocated. Otherwise,
3887 * allocate a new entry, add it to the cache,
3888 * and then initiate an asynchronous readdir
3889 * operation to fill it.
3891 srdc
.nfs3_cookie
= rdc
->nfs3_ncookie
;
3892 srdc
.buflen
= count
;
3893 rrdc
= avl_find(&rp
->r_dir
, &srdc
, &where
);
3896 rddir_cache_rele(nrdc
);
3901 rrdc
= rddir_cache_alloc(KM_NOSLEEP
);
3904 rrdc
->nfs3_cookie
= rdc
->nfs3_ncookie
;
3905 rrdc
->buflen
= count
;
3906 avl_insert(&rp
->r_dir
, rrdc
, where
);
3907 rddir_cache_hold(rrdc
);
3908 mutex_exit(&rp
->r_statelock
);
3909 rddir_cache_rele(rdc
);
3911 nfs3_readdir_readahead
++;
3913 nfs_async_readdir(vp
, rrdc
, cr
, do_nfs3readdir
);
3918 mutex_exit(&rp
->r_statelock
);
3919 rddir_cache_rele(rdc
);
3924 * Didn't find an entry in the cache. Construct a new empty
3925 * entry and link it into the cache. Other processes attempting
3926 * to access this entry will need to wait until it is filled in.
3928 * Since kmem_alloc may block, another pass through the cache
3929 * will need to be taken to make sure that another process
3930 * hasn't already added an entry to the cache for this request.
3933 mutex_exit(&rp
->r_statelock
);
3934 nrdc
= rddir_cache_alloc(KM_SLEEP
);
3935 nrdc
->nfs3_cookie
= uiop
->uio_loffset
;
3936 nrdc
->buflen
= count
;
3941 * Add this entry to the cache.
3943 avl_insert(&rp
->r_dir
, nrdc
, where
);
3944 rddir_cache_hold(nrdc
);
3945 mutex_exit(&rp
->r_statelock
);
3950 nfs3_readdir_cache_misses
++;
3953 * Do the readdir. This routine decides whether to use
3954 * READDIR or READDIRPLUS.
3956 error
= do_nfs3readdir(vp
, nrdc
, cr
);
3959 * If this operation failed, just return the error which occurred.
3965 * Since the RPC operation will have taken sometime and blocked
3966 * this process, another pass through the cache will need to be
3967 * taken to find the correct cache entry. It is possible that
3968 * the correct cache entry will not be there (although one was
3969 * added) because the directory changed during the RPC operation
3970 * and the readdir cache was flushed. In this case, just start
3971 * over. It is hoped that this will not happen too often... :-)
3979 do_nfs3readdir(vnode_t
*vp
, rddir_cache
*rdc
, cred_t
*cr
)
3987 ASSERT(nfs_zone() == mi
->mi_zone
);
3989 * Issue the proper request.
3991 * If the server does not support READDIRPLUS, then use READDIR.
3994 * Issue a READDIRPLUS if reading to fill an empty cache or if
3995 * an application has performed a lookup in the directory which
3996 * required an over the wire lookup. The use of READDIRPLUS
3997 * will help to (re)populate the DNLC.
3999 if (!(mi
->mi_flags
& MI_READDIRONLY
) &&
4000 (rp
->r_flags
& (RLOOKUP
| RREADDIRPLUS
))) {
4001 if (rp
->r_flags
& RREADDIRPLUS
) {
4002 mutex_enter(&rp
->r_statelock
);
4003 rp
->r_flags
&= ~RREADDIRPLUS
;
4004 mutex_exit(&rp
->r_statelock
);
4006 nfs3readdirplus(vp
, rdc
, cr
);
4007 if (rdc
->error
== EOPNOTSUPP
)
4008 nfs3readdir(vp
, rdc
, cr
);
4010 nfs3readdir(vp
, rdc
, cr
);
4012 mutex_enter(&rp
->r_statelock
);
4013 rdc
->flags
&= ~RDDIR
;
4014 if (rdc
->flags
& RDDIRWAIT
) {
4015 rdc
->flags
&= ~RDDIRWAIT
;
4016 cv_broadcast(&rdc
->cv
);
4020 rdc
->flags
|= RDDIRREQ
;
4021 mutex_exit(&rp
->r_statelock
);
4023 rddir_cache_rele(rdc
);
4029 nfs3readdir(vnode_t
*vp
, rddir_cache
*rdc
, cred_t
*cr
)
4037 failinfo_t fi
, *fip
= NULL
;
4043 ASSERT(nfs_zone() == mi
->mi_zone
);
4045 args
.dir
= *RTOFH3(rp
);
4046 args
.cookie
= (cookie3
)rdc
->nfs3_cookie
;
4047 args
.cookieverf
= rp
->r_cookieverf
;
4048 args
.count
= rdc
->buflen
;
4051 * NFS client failover support
4052 * suppress failover unless we have a zero cookie
4054 if (args
.cookie
== (cookie3
) 0) {
4056 fi
.fhp
= (caddr_t
)&args
.dir
;
4057 fi
.copyproc
= nfs3copyfh
;
4058 fi
.lookupproc
= nfs3lookup
;
4059 fi
.xattrdirproc
= acl_getxattrdir3
;
4064 rdc
->entries
= rddir_cache_buf_alloc(rdc
->buflen
, KM_SLEEP
);
4066 rdc
->entries
= kmem_alloc(rdc
->buflen
, KM_SLEEP
);
4069 res
.entries
= (dirent_t
*)rdc
->entries
;
4070 res
.entries_size
= rdc
->buflen
;
4071 res
.dir_attributes
.fres
.vap
= &dva
;
4072 res
.dir_attributes
.fres
.vp
= vp
;
4073 res
.loff
= rdc
->nfs3_cookie
;
4077 if (mi
->mi_io_kstats
) {
4078 mutex_enter(&mi
->mi_lock
);
4079 kstat_runq_enter(KSTAT_IO_PTR(mi
->mi_io_kstats
));
4080 mutex_exit(&mi
->mi_lock
);
4085 error
= rfs3call(VTOMI(vp
), NFSPROC3_READDIR
,
4086 xdr_READDIR3args
, (caddr_t
)&args
,
4087 xdr_READDIR3vres
, (caddr_t
)&res
, cr
,
4088 &douprintf
, &res
.status
, 0, fip
);
4090 if (mi
->mi_io_kstats
) {
4091 mutex_enter(&mi
->mi_lock
);
4092 kstat_runq_exit(KSTAT_IO_PTR(mi
->mi_io_kstats
));
4093 mutex_exit(&mi
->mi_lock
);
4099 nfs3_cache_post_op_vattr(vp
, &res
.dir_attributes
, t
, cr
);
4101 error
= geterrno3(res
.status
);
4103 PURGE_STALE_FH(error
, vp
, cr
);
4107 if (mi
->mi_io_kstats
) {
4108 mutex_enter(&mi
->mi_lock
);
4109 KSTAT_IO_PTR(mi
->mi_io_kstats
)->reads
++;
4110 KSTAT_IO_PTR(mi
->mi_io_kstats
)->nread
+= res
.size
;
4111 mutex_exit(&mi
->mi_lock
);
4114 rdc
->nfs3_ncookie
= res
.loff
;
4115 rp
->r_cookieverf
= res
.cookieverf
;
4116 rdc
->eof
= res
.eof
? 1 : 0;
4117 rdc
->entlen
= res
.size
;
4118 ASSERT(rdc
->entlen
<= rdc
->buflen
);
4123 kmem_free(rdc
->entries
, rdc
->buflen
);
4124 rdc
->entries
= NULL
;
4129 * Read directory entries.
4130 * There are some weird things to look out for here. The uio_loffset
4131 * field is either 0 or it is the offset returned from a previous
4132 * readdir. It is an opaque value used by the server to find the
4133 * correct directory block to read. The count field is the number
4134 * of blocks to read on the server. This is advisory only, the server
4135 * may return only one block's worth of entries. Entries may be compressed
4139 nfs3readdirplus(vnode_t
*vp
, rddir_cache
*rdc
, cred_t
*cr
)
4142 READDIRPLUS3args args
;
4143 READDIRPLUS3vres res
;
4148 failinfo_t fi
, *fip
= NULL
;
4152 ASSERT(nfs_zone() == mi
->mi_zone
);
4154 args
.dir
= *RTOFH3(rp
);
4155 args
.cookie
= (cookie3
)rdc
->nfs3_cookie
;
4156 args
.cookieverf
= rp
->r_cookieverf
;
4157 args
.dircount
= rdc
->buflen
;
4158 args
.maxcount
= mi
->mi_tsize
;
4161 * NFS client failover support
4162 * suppress failover unless we have a zero cookie
4164 if (args
.cookie
== (cookie3
)0) {
4166 fi
.fhp
= (caddr_t
)&args
.dir
;
4167 fi
.copyproc
= nfs3copyfh
;
4168 fi
.lookupproc
= nfs3lookup
;
4169 fi
.xattrdirproc
= acl_getxattrdir3
;
4174 rdc
->entries
= rddir_cache_buf_alloc(rdc
->buflen
, KM_SLEEP
);
4176 rdc
->entries
= kmem_alloc(rdc
->buflen
, KM_SLEEP
);
4179 res
.entries
= (dirent_t
*)rdc
->entries
;
4180 res
.entries_size
= rdc
->buflen
;
4181 res
.dir_attributes
.fres
.vap
= &dva
;
4182 res
.dir_attributes
.fres
.vp
= vp
;
4183 res
.loff
= rdc
->nfs3_cookie
;
4184 res
.credentials
= cr
;
4188 if (mi
->mi_io_kstats
) {
4189 mutex_enter(&mi
->mi_lock
);
4190 kstat_runq_enter(KSTAT_IO_PTR(mi
->mi_io_kstats
));
4191 mutex_exit(&mi
->mi_lock
);
4194 res
.time
= gethrtime();
4196 error
= rfs3call(mi
, NFSPROC3_READDIRPLUS
,
4197 xdr_READDIRPLUS3args
, (caddr_t
)&args
,
4198 xdr_READDIRPLUS3vres
, (caddr_t
)&res
, cr
,
4199 &douprintf
, &res
.status
, 0, fip
);
4201 if (mi
->mi_io_kstats
) {
4202 mutex_enter(&mi
->mi_lock
);
4203 kstat_runq_exit(KSTAT_IO_PTR(mi
->mi_io_kstats
));
4204 mutex_exit(&mi
->mi_lock
);
4211 nfs3_cache_post_op_vattr(vp
, &res
.dir_attributes
, res
.time
, cr
);
4213 error
= geterrno3(res
.status
);
4215 PURGE_STALE_FH(error
, vp
, cr
);
4216 if (error
== EOPNOTSUPP
) {
4217 mutex_enter(&mi
->mi_lock
);
4218 mi
->mi_flags
|= MI_READDIRONLY
;
4219 mutex_exit(&mi
->mi_lock
);
4224 if (mi
->mi_io_kstats
) {
4225 mutex_enter(&mi
->mi_lock
);
4226 KSTAT_IO_PTR(mi
->mi_io_kstats
)->reads
++;
4227 KSTAT_IO_PTR(mi
->mi_io_kstats
)->nread
+= res
.size
;
4228 mutex_exit(&mi
->mi_lock
);
4231 rdc
->nfs3_ncookie
= res
.loff
;
4232 rp
->r_cookieverf
= res
.cookieverf
;
4233 rdc
->eof
= res
.eof
? 1 : 0;
4234 rdc
->entlen
= res
.size
;
4235 ASSERT(rdc
->entlen
<= rdc
->buflen
);
4241 kmem_free(rdc
->entries
, rdc
->buflen
);
4242 rdc
->entries
= NULL
;
4247 static int nfs3_bio_do_stop
= 0;
4251 nfs3_bio(struct buf
*bp
, stable_how
*stab_comm
, cred_t
*cr
)
4253 rnode_t
*rp
= VTOR(bp
->b_vp
);
4259 ASSERT(nfs_zone() == VTOMI(bp
->b_vp
)->mi_zone
);
4260 offset
= ldbtob(bp
->b_lblkno
);
4262 DTRACE_IO1(start
, struct buf
*, bp
);
4264 if (bp
->b_flags
& B_READ
) {
4265 mutex_enter(&rp
->r_statelock
);
4266 if (rp
->r_cred
!= NULL
) {
4275 mutex_exit(&rp
->r_statelock
);
4277 error
= bp
->b_error
= nfs3read(bp
->b_vp
, bp
->b_un
.b_addr
,
4278 offset
, bp
->b_bcount
, &bp
->b_resid
, cred
);
4283 * Didn't get it all because we hit EOF,
4284 * zero all the memory beyond the EOF.
4286 /* bzero(rdaddr + */
4287 bzero(bp
->b_un
.b_addr
+
4288 bp
->b_bcount
- bp
->b_resid
, bp
->b_resid
);
4290 mutex_enter(&rp
->r_statelock
);
4291 if (bp
->b_resid
== bp
->b_bcount
&&
4292 offset
>= rp
->r_size
) {
4294 * We didn't read anything at all as we are
4295 * past EOF. Return an error indicator back
4296 * but don't destroy the pages (yet).
4300 mutex_exit(&rp
->r_statelock
);
4301 } else if (error
== EACCES
) {
4302 mutex_enter(&rp
->r_statelock
);
4304 if (rp
->r_cred
!= NULL
)
4310 mutex_exit(&rp
->r_statelock
);
4313 mutex_exit(&rp
->r_statelock
);
4316 if (!(rp
->r_flags
& RSTALE
)) {
4317 mutex_enter(&rp
->r_statelock
);
4318 if (rp
->r_cred
!= NULL
) {
4327 mutex_exit(&rp
->r_statelock
);
4329 mutex_enter(&rp
->r_statelock
);
4330 count
= MIN(bp
->b_bcount
, rp
->r_size
- offset
);
4331 mutex_exit(&rp
->r_statelock
);
4333 cmn_err(CE_PANIC
, "nfs3_bio: write count < 0");
4336 zcmn_err(getzoneid(), CE_WARN
,
4337 "nfs3_bio: zero length write at %lld",
4339 nfs_printfhandle(&rp
->r_fh
);
4340 if (nfs3_bio_do_stop
)
4341 debug_enter("nfs3_bio");
4344 error
= nfs3write(bp
->b_vp
, bp
->b_un
.b_addr
, offset
,
4345 count
, cred
, stab_comm
);
4346 if (error
== EACCES
) {
4347 mutex_enter(&rp
->r_statelock
);
4349 if (rp
->r_cred
!= NULL
)
4356 mutex_exit(&rp
->r_statelock
);
4359 mutex_exit(&rp
->r_statelock
);
4361 bp
->b_error
= error
;
4362 if (error
&& error
!= EINTR
) {
4364 * Don't print EDQUOT errors on the console.
4365 * Don't print asynchronous EACCES errors.
4366 * Don't print EFBIG errors.
4367 * Print all other write errors.
4369 if (error
!= EDQUOT
&& error
!= EFBIG
&&
4371 !(bp
->b_flags
& B_ASYNC
)))
4372 nfs_write_error(bp
->b_vp
, error
, cred
);
4374 * Update r_error and r_flags as appropriate.
4375 * If the error was ESTALE, then mark the
4376 * rnode as not being writeable and save
4377 * the error status. Otherwise, save any
4378 * errors which occur from asynchronous
4379 * page invalidations. Any errors occurring
4380 * from other operations should be saved
4383 mutex_enter(&rp
->r_statelock
);
4384 if (error
== ESTALE
) {
4385 rp
->r_flags
|= RSTALE
;
4387 rp
->r_error
= error
;
4388 } else if (!rp
->r_error
&&
4390 (B_INVAL
|B_FORCE
|B_ASYNC
)) ==
4391 (B_INVAL
|B_FORCE
|B_ASYNC
)) {
4392 rp
->r_error
= error
;
4394 mutex_exit(&rp
->r_statelock
);
4398 error
= rp
->r_error
;
4400 * A close may have cleared r_error, if so,
4401 * propagate ESTALE error return properly
4408 if (error
!= 0 && error
!= NFS_EOF
)
4409 bp
->b_flags
|= B_ERROR
;
4411 DTRACE_IO1(done
, struct buf
*, bp
);
4418 nfs3_fid(vnode_t
*vp
, fid_t
*fidp
, caller_context_t
*ct
)
4422 if (nfs_zone() != VTOMI(vp
)->mi_zone
)
4426 if (fidp
->fid_len
< (ushort_t
)rp
->r_fh
.fh_len
) {
4427 fidp
->fid_len
= rp
->r_fh
.fh_len
;
4430 fidp
->fid_len
= rp
->r_fh
.fh_len
;
4431 bcopy(rp
->r_fh
.fh_buf
, fidp
->fid_data
, fidp
->fid_len
);
4437 nfs3_rwlock(vnode_t
*vp
, int write_lock
, caller_context_t
*ctp
)
4439 rnode_t
*rp
= VTOR(vp
);
4442 (void) nfs_rw_enter_sig(&rp
->r_rwlock
, RW_READER
, FALSE
);
4443 return (V_WRITELOCK_FALSE
);
4446 if ((rp
->r_flags
& RDIRECTIO
) || (VTOMI(vp
)->mi_flags
& MI_DIRECTIO
)) {
4447 (void) nfs_rw_enter_sig(&rp
->r_rwlock
, RW_READER
, FALSE
);
4448 if (rp
->r_mapcnt
== 0 && !vn_has_cached_data(vp
))
4449 return (V_WRITELOCK_FALSE
);
4450 nfs_rw_exit(&rp
->r_rwlock
);
4453 (void) nfs_rw_enter_sig(&rp
->r_rwlock
, RW_WRITER
, FALSE
);
4454 return (V_WRITELOCK_TRUE
);
4459 nfs3_rwunlock(vnode_t
*vp
, int write_lock
, caller_context_t
*ctp
)
4461 rnode_t
*rp
= VTOR(vp
);
4463 nfs_rw_exit(&rp
->r_rwlock
);
4468 nfs3_seek(vnode_t
*vp
, offset_t ooff
, offset_t
*noffp
, caller_context_t
*ct
)
4472 * Because we stuff the readdir cookie into the offset field
4473 * someone may attempt to do an lseek with the cookie which
4474 * we want to succeed.
4476 if (vp
->v_type
== VDIR
)
4484 * number of nfs3_bsize blocks to read ahead.
4486 static int nfs3_nra
= 4;
4489 static int nfs3_lostpage
= 0; /* number of times we lost original page */
4493 * Return all the pages from [off..off+len) in file
4497 nfs3_getpage(vnode_t
*vp
, offset_t off
, size_t len
, uint_t
*protp
,
4498 page_t
*pl
[], size_t plsz
, struct seg
*seg
, caddr_t addr
,
4499 enum seg_rw rw
, cred_t
*cr
, caller_context_t
*ct
)
4505 if (vp
->v_flag
& VNOMAP
)
4508 if (nfs_zone() != VTOMI(vp
)->mi_zone
)
4514 * Now valididate that the caches are up to date.
4516 error
= nfs3_validate_caches(vp
, cr
);
4523 mutex_enter(&rp
->r_statelock
);
4526 * Don't create dirty pages faster than they
4527 * can be cleaned so that the system doesn't
4528 * get imbalanced. If the async queue is
4529 * maxed out, then wait for it to drain before
4530 * creating more dirty pages. Also, wait for
4531 * any threads doing pagewalks in the vop_getattr
4532 * entry points so that they don't block for
4535 if (rw
== S_CREATE
) {
4536 while ((mi
->mi_max_threads
!= 0 &&
4537 rp
->r_awcount
> 2 * mi
->mi_max_threads
) ||
4539 cv_wait(&rp
->r_cv
, &rp
->r_statelock
);
4543 * If we are getting called as a side effect of an nfs_write()
4544 * operation the local file size might not be extended yet.
4545 * In this case we want to be able to return pages of zeroes.
4547 if (off
+ len
> rp
->r_size
+ PAGEOFFSET
&& seg
!= segkmap
) {
4548 mutex_exit(&rp
->r_statelock
);
4549 return (EFAULT
); /* beyond EOF */
4552 mutex_exit(&rp
->r_statelock
);
4554 error
= pvn_getpages(nfs3_getapage
, vp
, off
, len
, protp
,
4555 pl
, plsz
, seg
, addr
, rw
, cr
);
4559 nfs_purge_caches(vp
, NFS_NOPURGE_DNLC
, cr
);
4562 PURGE_STALE_FH(error
, vp
, cr
);
4569 * Called from pvn_getpages to get a particular page.
4573 nfs3_getapage(vnode_t
*vp
, uoff_t off
, size_t len
, uint_t
*protp
,
4574 page_t
*pl
[], size_t plsz
, struct seg
*seg
, caddr_t addr
,
4575 enum seg_rw rw
, cred_t
*cr
)
4589 int readahead_issued
= 0;
4590 int ra_window
; /* readahead window */
4594 if (nfs_zone() != VTOMI(vp
)->mi_zone
)
4597 bsize
= MAX(vp
->v_vfsp
->vfs_bsize
, PAGESIZE
);
4609 blkoff
= lbn
* bsize
;
4612 * Queueing up the readahead before doing the synchronous read
4613 * results in a significant increase in read throughput because
4614 * of the increased parallelism between the async threads and
4615 * the process context.
4617 if ((off
& ((vp
->v_vfsp
->vfs_bsize
) - 1)) == 0 &&
4619 !(vp
->v_flag
& VNOCACHE
)) {
4620 mutex_enter(&rp
->r_statelock
);
4623 * Calculate the number of readaheads to do.
4624 * a) No readaheads at offset = 0.
4625 * b) Do maximum(nfs3_nra) readaheads when the readahead
4627 * c) Do readaheads between 1 to (nfs3_nra - 1) depending
4628 * upon how far the readahead window is open or close.
4629 * d) No readaheads if rp->r_nextr is not within the scope
4630 * of the readahead window (random i/o).
4635 else if (blkoff
== rp
->r_nextr
)
4636 readahead
= nfs3_nra
;
4637 else if (rp
->r_nextr
> blkoff
&&
4638 ((ra_window
= (rp
->r_nextr
- blkoff
) / bsize
)
4640 readahead
= nfs3_nra
- ra_window
;
4644 rablkoff
= rp
->r_nextr
;
4645 while (readahead
> 0 && rablkoff
+ bsize
< rp
->r_size
) {
4646 mutex_exit(&rp
->r_statelock
);
4647 if (nfs_async_readahead(vp
, rablkoff
+ bsize
,
4648 addr
+ (rablkoff
+ bsize
- off
), seg
, cr
,
4649 nfs3_readahead
) < 0) {
4650 mutex_enter(&rp
->r_statelock
);
4656 * Indicate that we did a readahead so
4657 * readahead offset is not updated
4658 * by the synchronous read below.
4660 readahead_issued
= 1;
4661 mutex_enter(&rp
->r_statelock
);
4663 * set readahead offset to
4664 * offset of last async readahead
4667 rp
->r_nextr
= rablkoff
;
4669 mutex_exit(&rp
->r_statelock
);
4673 if ((pagefound
= page_exists(&vp
->v_object
, off
)) == NULL
) {
4675 (void) nfs_async_readahead(vp
, blkoff
, addr
, seg
, cr
,
4677 } else if (rw
== S_CREATE
) {
4679 * Block for this page is not allocated, or the offset
4680 * is beyond the current allocation size, or we're
4681 * allocating a swap slot and the page was not found,
4682 * so allocate it and return a zero page.
4684 if ((pp
= page_create_va(&vp
->v_object
, off
,
4685 PAGESIZE
, PG_WAIT
, seg
, addr
)) == NULL
)
4686 cmn_err(CE_PANIC
, "nfs3_getapage: page_create");
4688 mutex_enter(&rp
->r_statelock
);
4689 rp
->r_nextr
= off
+ PAGESIZE
;
4690 mutex_exit(&rp
->r_statelock
);
4693 * Need to go to server to get a BLOCK, exception to
4694 * that being while reading at offset = 0 or doing
4695 * random i/o, in that case read only a PAGE.
4697 mutex_enter(&rp
->r_statelock
);
4698 if (blkoff
< rp
->r_size
&&
4699 blkoff
+ bsize
>= rp
->r_size
) {
4701 * If only a block or less is left in
4702 * the file, read all that is remaining.
4704 if (rp
->r_size
<= off
) {
4706 * Trying to access beyond EOF,
4707 * set up to get at least one page.
4709 blksize
= off
+ PAGESIZE
- blkoff
;
4711 blksize
= rp
->r_size
- blkoff
;
4712 } else if ((off
== 0) ||
4713 (off
!= rp
->r_nextr
&& !readahead_issued
)) {
4715 blkoff
= off
; /* block = page here */
4718 mutex_exit(&rp
->r_statelock
);
4720 pp
= pvn_read_kluster(vp
, off
, seg
, addr
, &io_off
,
4721 &io_len
, blkoff
, blksize
, 0);
4724 * Some other thread has entered the page,
4731 * Now round the request size up to page boundaries.
4732 * This ensures that the entire page will be
4733 * initialized to zeroes if EOF is encountered.
4735 io_len
= ptob(btopr(io_len
));
4737 bp
= pageio_setup(pp
, io_len
, vp
, B_READ
);
4741 * pageio_setup should have set b_addr to 0. This
4742 * is correct since we want to do I/O on a page
4743 * boundary. bp_mapin will use this addr to calculate
4744 * an offset, and then set b_addr to the kernel virtual
4745 * address it allocated for us.
4747 ASSERT(bp
->b_un
.b_addr
== 0);
4751 bp
->b_lblkno
= lbtodb(io_off
);
4753 bp
->b_offset
= (offset_t
)off
;
4757 * If doing a write beyond what we believe is EOF,
4758 * don't bother trying to read the pages from the
4759 * server, we'll just zero the pages here. We
4760 * don't check that the rw flag is S_WRITE here
4761 * because some implementations may attempt a
4762 * read access to the buffer before copying data.
4764 mutex_enter(&rp
->r_statelock
);
4765 if (io_off
>= rp
->r_size
&& seg
== segkmap
) {
4766 mutex_exit(&rp
->r_statelock
);
4767 bzero(bp
->b_un
.b_addr
, io_len
);
4769 mutex_exit(&rp
->r_statelock
);
4770 error
= nfs3_bio(bp
, NULL
, cr
);
4774 * Unmap the buffer before freeing it.
4781 pp
->p_fsdata
= C_NOCOMMIT
;
4782 } while ((pp
= pp
->p_next
) != savepp
);
4784 if (error
== NFS_EOF
) {
4786 * If doing a write system call just return
4787 * zeroed pages, else user tried to get pages
4788 * beyond EOF, return error. We don't check
4789 * that the rw flag is S_WRITE here because
4790 * some implementations may attempt a read
4791 * access to the buffer before copying data.
4799 if (!readahead_issued
&& !error
) {
4800 mutex_enter(&rp
->r_statelock
);
4801 rp
->r_nextr
= io_off
+ io_len
;
4802 mutex_exit(&rp
->r_statelock
);
4813 pvn_read_done(pp
, B_ERROR
);
4818 se_t se
= (rw
== S_CREATE
? SE_EXCL
: SE_SHARED
);
4821 * Page exists in the cache, acquire the appropriate lock.
4822 * If this fails, start all over again.
4824 if ((pp
= page_lookup(&vp
->v_object
, off
, se
)) == NULL
) {
4836 pvn_plist_init(pp
, pl
, plsz
, off
, io_len
, rw
);
4842 nfs3_readahead(vnode_t
*vp
, uoff_t blkoff
, caddr_t addr
, struct seg
*seg
,
4850 uint_t bsize
, blksize
;
4851 rnode_t
*rp
= VTOR(vp
);
4854 ASSERT(nfs_zone() == VTOMI(vp
)->mi_zone
);
4855 bsize
= MAX(vp
->v_vfsp
->vfs_bsize
, PAGESIZE
);
4857 mutex_enter(&rp
->r_statelock
);
4858 if (blkoff
< rp
->r_size
&& blkoff
+ bsize
> rp
->r_size
) {
4860 * If less than a block left in file read less
4863 blksize
= rp
->r_size
- blkoff
;
4866 mutex_exit(&rp
->r_statelock
);
4868 pp
= pvn_read_kluster(vp
, blkoff
, segkmap
, addr
,
4869 &io_off
, &io_len
, blkoff
, blksize
, 1);
4871 * The isra flag passed to the kluster function is 1, we may have
4872 * gotten a return value of NULL for a variety of reasons (# of free
4873 * pages < minfree, someone entered the page on the vnode etc). In all
4874 * cases, we want to punt on the readahead.
4880 * Now round the request size up to page boundaries.
4881 * This ensures that the entire page will be
4882 * initialized to zeroes if EOF is encountered.
4884 io_len
= ptob(btopr(io_len
));
4886 bp
= pageio_setup(pp
, io_len
, vp
, B_READ
);
4890 * pageio_setup should have set b_addr to 0. This is correct since
4891 * we want to do I/O on a page boundary. bp_mapin() will use this addr
4892 * to calculate an offset, and then set b_addr to the kernel virtual
4893 * address it allocated for us.
4895 ASSERT(bp
->b_un
.b_addr
== 0);
4899 bp
->b_lblkno
= lbtodb(io_off
);
4901 bp
->b_offset
= (offset_t
)blkoff
;
4905 * If doing a write beyond what we believe is EOF, don't bother trying
4906 * to read the pages from the server, we'll just zero the pages here.
4907 * We don't check that the rw flag is S_WRITE here because some
4908 * implementations may attempt a read access to the buffer before
4911 mutex_enter(&rp
->r_statelock
);
4912 if (io_off
>= rp
->r_size
&& seg
== segkmap
) {
4913 mutex_exit(&rp
->r_statelock
);
4914 bzero(bp
->b_un
.b_addr
, io_len
);
4917 mutex_exit(&rp
->r_statelock
);
4918 error
= nfs3_bio(bp
, NULL
, cr
);
4919 if (error
== NFS_EOF
)
4924 * Unmap the buffer before freeing it.
4931 pp
->p_fsdata
= C_NOCOMMIT
;
4932 } while ((pp
= pp
->p_next
) != savepp
);
4934 pvn_read_done(pp
, error
? B_READ
| B_ERROR
: B_READ
);
4937 * In case of error set readahead offset
4938 * to the lowest offset.
4939 * pvn_read_done() calls VN_DISPOSE to destroy the pages
4941 if (error
&& rp
->r_nextr
> io_off
) {
4942 mutex_enter(&rp
->r_statelock
);
4943 if (rp
->r_nextr
> io_off
)
4944 rp
->r_nextr
= io_off
;
4945 mutex_exit(&rp
->r_statelock
);
4950 * Flags are composed of {B_INVAL, B_FREE, B_DONTNEED, B_FORCE}
4951 * If len == 0, do from off to EOF.
4953 * The normal cases should be len == 0 && off == 0 (entire vp list),
4954 * len == MAXBSIZE (from segmap_release actions), and len == PAGESIZE
4959 nfs3_putpage(vnode_t
*vp
, offset_t off
, size_t len
, int flags
, cred_t
*cr
,
4960 caller_context_t
*ct
)
4968 * XXX - Why should this check be made here?
4970 if (vp
->v_flag
& VNOMAP
)
4972 if (len
== 0 && !(flags
& B_INVAL
) && vn_is_readonly(vp
))
4974 if (!(flags
& B_ASYNC
) && nfs_zone() != VTOMI(vp
)->mi_zone
)
4978 mutex_enter(&rp
->r_statelock
);
4980 mutex_exit(&rp
->r_statelock
);
4981 error
= nfs_putpages(vp
, off
, len
, flags
, cr
);
4982 mutex_enter(&rp
->r_statelock
);
4984 cv_broadcast(&rp
->r_cv
);
4985 mutex_exit(&rp
->r_statelock
);
4991 * Write out a single page, possibly klustering adjacent dirty pages.
4994 nfs3_putapage(vnode_t
*vp
, page_t
*pp
, uoff_t
*offp
, size_t *lenp
,
4995 int flags
, cred_t
*cr
)
5005 ASSERT(!vn_is_readonly(vp
));
5008 ASSERT((flags
& B_ASYNC
) || nfs_zone() == VTOMI(vp
)->mi_zone
);
5011 ASSERT(rp
->r_count
> 0);
5013 bsize
= MAX(vp
->v_vfsp
->vfs_bsize
, PAGESIZE
);
5014 lbn
= pp
->p_offset
/ bsize
;
5015 lbn_off
= lbn
* bsize
;
5018 * Find a kluster that fits in one block, or in
5019 * one page if pages are bigger than blocks. If
5020 * there is less file space allocated than a whole
5021 * page, we'll shorten the i/o request below.
5023 pp
= pvn_write_kluster(vp
, pp
, &io_off
, &io_len
, lbn_off
,
5024 roundup(bsize
, PAGESIZE
), flags
);
5027 * pvn_write_kluster shouldn't have returned a page with offset
5028 * behind the original page we were given. Verify that.
5030 ASSERT((pp
->p_offset
/ bsize
) >= lbn
);
5033 * Now pp will have the list of kept dirty pages marked for
5034 * write back. It will also handle invalidation and freeing
5035 * of pages that are not dirty. Check for page length rounding
5038 if (io_off
+ io_len
> lbn_off
+ bsize
) {
5039 ASSERT((io_off
+ io_len
) - (lbn_off
+ bsize
) < PAGESIZE
);
5040 io_len
= lbn_off
+ bsize
- io_off
;
5043 * The RMODINPROGRESS flag makes sure that nfs(3)_bio() sees a
5044 * consistent value of r_size. RMODINPROGRESS is set in writerp().
5045 * When RMODINPROGRESS is set it indicates that a uiomove() is in
5046 * progress and the r_size has not been made consistent with the
5047 * new size of the file. When the uiomove() completes the r_size is
5048 * updated and the RMODINPROGRESS flag is cleared.
5050 * The RMODINPROGRESS flag makes sure that nfs(3)_bio() sees a
5051 * consistent value of r_size. Without this handshaking, it is
5052 * possible that nfs(3)_bio() picks up the old value of r_size
5053 * before the uiomove() in writerp() completes. This will result
5054 * in the write through nfs(3)_bio() being dropped.
5056 * More precisely, there is a window between the time the uiomove()
5057 * completes and the time the r_size is updated. If a fop_putpage()
5058 * operation intervenes in this window, the page will be picked up,
5059 * because it is dirty (it will be unlocked, unless it was
5060 * pagecreate'd). When the page is picked up as dirty, the dirty
5061 * bit is reset (pvn_getdirty()). In nfs(3)write(), r_size is
5062 * checked. This will still be the old size. Therefore the page will
5063 * not be written out. When segmap_release() calls fop_putpage(),
5064 * the page will be found to be clean and the write will be dropped.
5066 if (rp
->r_flags
& RMODINPROGRESS
) {
5067 mutex_enter(&rp
->r_statelock
);
5068 if ((rp
->r_flags
& RMODINPROGRESS
) &&
5069 rp
->r_modaddr
+ MAXBSIZE
> io_off
&&
5070 rp
->r_modaddr
< io_off
+ io_len
) {
5073 * A write is in progress for this region of the file.
5074 * If we did not detect RMODINPROGRESS here then this
5075 * path through nfs_putapage() would eventually go to
5076 * nfs(3)_bio() and may not write out all of the data
5077 * in the pages. We end up losing data. So we decide
5078 * to set the modified bit on each page in the page
5079 * list and mark the rnode with RDIRTY. This write
5080 * will be restarted at some later time.
5083 while (plist
!= NULL
) {
5085 page_sub(&plist
, pp
);
5090 rp
->r_flags
|= RDIRTY
;
5091 mutex_exit(&rp
->r_statelock
);
5098 mutex_exit(&rp
->r_statelock
);
5101 if (flags
& B_ASYNC
) {
5102 error
= nfs_async_putapage(vp
, pp
, io_off
, io_len
, flags
, cr
,
5103 nfs3_sync_putapage
);
5105 error
= nfs3_sync_putapage(vp
, pp
, io_off
, io_len
, flags
, cr
);
5115 nfs3_sync_putapage(vnode_t
*vp
, page_t
*pp
, uoff_t io_off
, size_t io_len
,
5116 int flags
, cred_t
*cr
)
5121 ASSERT(nfs_zone() == VTOMI(vp
)->mi_zone
);
5125 error
= nfs3_rdwrlbn(vp
, pp
, io_off
, io_len
, flags
, cr
);
5129 if ((error
== ENOSPC
|| error
== EDQUOT
|| error
== EFBIG
||
5131 (flags
& (B_INVAL
|B_FORCE
)) != (B_INVAL
|B_FORCE
)) {
5132 if (!(rp
->r_flags
& ROUTOFSPACE
)) {
5133 mutex_enter(&rp
->r_statelock
);
5134 rp
->r_flags
|= ROUTOFSPACE
;
5135 mutex_exit(&rp
->r_statelock
);
5138 pvn_write_done(pp
, flags
);
5140 * If this was not an async thread, then try again to
5141 * write out the pages, but this time, also destroy
5142 * them whether or not the write is successful. This
5143 * will prevent memory from filling up with these
5144 * pages and destroying them is the only alternative
5145 * if they can't be written out.
5147 * Don't do this if this is an async thread because
5148 * when the pages are unlocked in pvn_write_done,
5149 * some other thread could have come along, locked
5150 * them, and queued for an async thread. It would be
5151 * possible for all of the async threads to be tied
5152 * up waiting to lock the pages again and they would
5153 * all already be locked and waiting for an async
5154 * thread to handle them. Deadlock.
5156 if (!(flags
& B_ASYNC
)) {
5157 error
= nfs3_putpage(vp
, io_off
, io_len
,
5158 B_INVAL
| B_FORCE
, cr
, NULL
);
5163 else if (rp
->r_flags
& ROUTOFSPACE
) {
5164 mutex_enter(&rp
->r_statelock
);
5165 rp
->r_flags
&= ~ROUTOFSPACE
;
5166 mutex_exit(&rp
->r_statelock
);
5168 pvn_write_done(pp
, flags
);
5169 if (freemem
< desfree
)
5170 (void) nfs3_commit_vp(vp
, 0, 0, cr
);
5178 nfs3_map(vnode_t
*vp
, offset_t off
, struct as
*as
, caddr_t
*addrp
,
5179 size_t len
, uchar_t prot
, uchar_t maxprot
, uint_t flags
,
5180 cred_t
*cr
, caller_context_t
*ct
)
5182 struct segvn_crargs vn_a
;
5187 if (nfs_zone() != VTOMI(vp
)->mi_zone
)
5190 if (vp
->v_flag
& VNOMAP
)
5193 if (off
< 0 || off
+ len
< 0)
5196 if (vp
->v_type
!= VREG
)
5200 * If there is cached data and if close-to-open consistency
5201 * checking is not turned off and if the file system is not
5202 * mounted readonly, then force an over the wire getattr.
5203 * Otherwise, just invoke nfs3getattr to get a copy of the
5204 * attributes. The attribute cache will be used unless it
5205 * is timed out and if it is, then an over the wire getattr
5208 va
.va_mask
= VATTR_ALL
;
5209 if (vn_has_cached_data(vp
) &&
5210 !(VTOMI(vp
)->mi_flags
& MI_NOCTO
) && !vn_is_readonly(vp
))
5211 error
= nfs3_getattr_otw(vp
, &va
, cr
);
5213 error
= nfs3getattr(vp
, &va
, cr
);
5218 * Check to see if the vnode is currently marked as not cachable.
5219 * This means portions of the file are locked (through fop_frlock).
5220 * In this case the map request must be refused. We use
5221 * rp->r_lkserlock to avoid a race with concurrent lock requests.
5226 * Atomically increment r_inmap after acquiring r_rwlock. The
5227 * idea here is to acquire r_rwlock to block read/write and
5228 * not to protect r_inmap. r_inmap will inform nfs3_read/write()
5229 * that we are in nfs3_map(). Now, r_rwlock is acquired in order
5230 * and we can prevent the deadlock that would have occurred
5231 * when nfs3_addmap() would have acquired it out of order.
5233 * Since we are not protecting r_inmap by any lock, we do not
5234 * hold any lock when we decrement it. We atomically decrement
5235 * r_inmap after we release r_lkserlock.
5238 if (nfs_rw_enter_sig(&rp
->r_rwlock
, RW_WRITER
, INTR(vp
)))
5240 atomic_inc_uint(&rp
->r_inmap
);
5241 nfs_rw_exit(&rp
->r_rwlock
);
5243 if (nfs_rw_enter_sig(&rp
->r_lkserlock
, RW_READER
, INTR(vp
))) {
5244 atomic_dec_uint(&rp
->r_inmap
);
5248 if (vp
->v_flag
& VNOCACHE
) {
5254 * Don't allow concurrent locks and mapping if mandatory locking is
5257 if ((flk_has_remote_locks(vp
) || lm_has_sleep(vp
)) &&
5258 MANDLOCK(vp
, va
.va_mode
)) {
5264 error
= choose_addr(as
, addrp
, len
, off
, ADDR_VACALIGN
, flags
);
5272 vn_a
.type
= (flags
& MAP_TYPE
);
5273 vn_a
.prot
= (uchar_t
)prot
;
5274 vn_a
.maxprot
= (uchar_t
)maxprot
;
5275 vn_a
.flags
= (flags
& ~MAP_TYPE
);
5279 vn_a
.lgrp_mem_policy_flags
= 0;
5281 error
= as_map(as
, *addrp
, len
, segvn_create
, &vn_a
);
5285 nfs_rw_exit(&rp
->r_lkserlock
);
5286 atomic_dec_uint(&rp
->r_inmap
);
5292 nfs3_addmap(vnode_t
*vp
, offset_t off
, struct as
*as
, caddr_t addr
,
5293 size_t len
, uchar_t prot
, uchar_t maxprot
, uint_t flags
,
5294 cred_t
*cr
, caller_context_t
*ct
)
5298 if (vp
->v_flag
& VNOMAP
)
5300 if (nfs_zone() != VTOMI(vp
)->mi_zone
)
5304 atomic_add_long((ulong_t
*)&rp
->r_mapcnt
, btopr(len
));
5311 nfs3_frlock(vnode_t
*vp
, int cmd
, struct flock64
*bfp
, int flag
,
5312 offset_t offset
, struct flk_callback
*flk_cbp
, cred_t
*cr
,
5313 caller_context_t
*ct
)
5319 int error
= 0, intr
= INTR(vp
);
5321 if (nfs_zone() != VTOMI(vp
)->mi_zone
)
5323 /* check for valid cmd parameter */
5324 if (cmd
!= F_GETLK
&& cmd
!= F_SETLK
&& cmd
!= F_SETLKW
)
5327 /* Verify l_type. */
5328 switch (bfp
->l_type
) {
5330 if (cmd
!= F_GETLK
&& !(flag
& FREAD
))
5334 if (cmd
!= F_GETLK
&& !(flag
& FWRITE
))
5345 /* check the validity of the lock range */
5346 if (rc
= flk_convert_lock_data(vp
, bfp
, &start
, &end
, offset
))
5348 if (rc
= flk_check_lock_data(start
, end
, MAXEND
))
5352 * If the filesystem is mounted using local locking, pass the
5353 * request off to the local locking code.
5355 if (VTOMI(vp
)->mi_flags
& MI_LLOCK
) {
5356 if (cmd
== F_SETLK
|| cmd
== F_SETLKW
) {
5358 * For complete safety, we should be holding
5359 * r_lkserlock. However, we can't call
5360 * lm_safelock and then fs_frlock while
5361 * holding r_lkserlock, so just invoke
5362 * lm_safelock and expect that this will
5363 * catch enough of the cases.
5365 if (!lm_safelock(vp
, bfp
, cr
))
5368 return (fs_frlock(vp
, cmd
, bfp
, flag
, offset
, flk_cbp
, cr
, ct
));
5374 * Check whether the given lock request can proceed, given the
5375 * current file mappings.
5377 if (nfs_rw_enter_sig(&rp
->r_lkserlock
, RW_WRITER
, intr
))
5379 if (cmd
== F_SETLK
|| cmd
== F_SETLKW
) {
5380 if (!lm_safelock(vp
, bfp
, cr
)) {
5387 * Flush the cache after waiting for async I/O to finish. For new
5388 * locks, this is so that the process gets the latest bits from the
5389 * server. For unlocks, this is so that other clients see the
5390 * latest bits once the file has been unlocked. If currently dirty
5391 * pages can't be flushed, then don't allow a lock to be set. But
5392 * allow unlocks to succeed, to avoid having orphan locks on the
5395 if (cmd
!= F_GETLK
) {
5396 mutex_enter(&rp
->r_statelock
);
5397 while (rp
->r_count
> 0) {
5399 klwp_t
*lwp
= ttolwp(curthread
);
5403 if (cv_wait_sig(&rp
->r_cv
,
5404 &rp
->r_statelock
) == 0) {
5413 cv_wait(&rp
->r_cv
, &rp
->r_statelock
);
5415 mutex_exit(&rp
->r_statelock
);
5418 error
= nfs3_putpage(vp
, 0, 0, B_INVAL
, cr
, ct
);
5420 if (error
== ENOSPC
|| error
== EDQUOT
) {
5421 mutex_enter(&rp
->r_statelock
);
5423 rp
->r_error
= error
;
5424 mutex_exit(&rp
->r_statelock
);
5426 if (bfp
->l_type
!= F_UNLCK
) {
5433 lm_fh3
.n_len
= VTOFH3(vp
)->fh3_length
;
5434 lm_fh3
.n_bytes
= (char *)&(VTOFH3(vp
)->fh3_u
.data
);
5437 * Call the lock manager to do the real work of contacting
5438 * the server and obtaining the lock.
5440 rc
= lm4_frlock(vp
, cmd
, bfp
, flag
, offset
, cr
, &lm_fh3
, flk_cbp
);
5443 nfs_lockcompletion(vp
, cmd
);
5446 nfs_rw_exit(&rp
->r_lkserlock
);
5451 * Free storage space associated with the specified vnode. The portion
5452 * to be freed is specified by bfp->l_start and bfp->l_len (already
5453 * normalized to a "whence" of 0).
5455 * This is an experimental facility whose continued existence is not
5456 * guaranteed. Currently, we only support the special case
5457 * of l_len == 0, meaning free to end of file.
5461 nfs3_space(vnode_t
*vp
, int cmd
, struct flock64
*bfp
, int flag
,
5462 offset_t offset
, cred_t
*cr
, caller_context_t
*ct
)
5466 ASSERT(vp
->v_type
== VREG
);
5467 if (cmd
!= F_FREESP
)
5469 if (nfs_zone() != VTOMI(vp
)->mi_zone
)
5472 error
= convoff(vp
, bfp
, 0, offset
);
5474 ASSERT(bfp
->l_start
>= 0);
5475 if (bfp
->l_len
== 0) {
5479 * ftruncate should not change the ctime and
5480 * mtime if we truncate the file to its
5483 va
.va_mask
= VATTR_SIZE
;
5484 error
= nfs3getattr(vp
, &va
, cr
);
5485 if (error
|| va
.va_size
== bfp
->l_start
)
5487 va
.va_mask
= VATTR_SIZE
;
5488 va
.va_size
= bfp
->l_start
;
5489 error
= nfs3setattr(vp
, &va
, 0, cr
);
5491 if (error
== 0 && bfp
->l_start
== 0)
5492 vnevent_truncate(vp
, ct
);
5502 nfs3_realvp(vnode_t
*vp
, vnode_t
**vpp
, caller_context_t
*ct
)
5509 * Setup and add an address space callback to do the work of the delmap call.
5510 * The callback will (and must be) deleted in the actual callback function.
5512 * This is done in order to take care of the problem that we have with holding
5513 * the address space's a_lock for a long period of time (e.g. if the NFS server
5514 * is down). Callbacks will be executed in the address space code while the
5515 * a_lock is not held. Holding the address space's a_lock causes things such
5516 * as ps and fork to hang because they are trying to acquire this lock as well.
5520 nfs3_delmap(vnode_t
*vp
, offset_t off
, struct as
*as
, caddr_t addr
,
5521 size_t len
, uint_t prot
, uint_t maxprot
, uint_t flags
,
5522 cred_t
*cr
, caller_context_t
*ct
)
5527 nfs_delmap_args_t
*dmapp
;
5528 nfs_delmapcall_t
*delmap_call
;
5530 if (vp
->v_flag
& VNOMAP
)
5533 * A process may not change zones if it has NFS pages mmap'ed
5534 * in, so we can't legitimately get here from the wrong zone.
5536 ASSERT(nfs_zone() == VTOMI(vp
)->mi_zone
);
5541 * The way that the address space of this process deletes its mapping
5542 * of this file is via the following call chains:
5543 * - as_free()->segop_unmap()/segvn_unmap()->fop_delmap()/nfs3_delmap()
5544 * - as_unmap()->segop_unmap()/segvn_unmap()->fop_delmap()/nfs3_delmap()
5546 * With the use of address space callbacks we are allowed to drop the
5547 * address space lock, a_lock, while executing the NFS operations that
5548 * need to go over the wire. Returning EAGAIN to the caller of this
5549 * function is what drives the execution of the callback that we add
5550 * below. The callback will be executed by the address space code
5551 * after dropping the a_lock. When the callback is finished, since
5552 * we dropped the a_lock, it must be re-acquired and segvn_unmap()
5553 * is called again on the same segment to finish the rest of the work
5554 * that needs to happen during unmapping.
5556 * This action of calling back into the segment driver causes
5557 * nfs3_delmap() to get called again, but since the callback was
5558 * already executed at this point, it already did the work and there
5559 * is nothing left for us to do.
5562 * - The first time nfs3_delmap is called by the current thread is when
5563 * we add the caller associated with this delmap to the delmap caller
5564 * list, add the callback, and return EAGAIN.
5565 * - The second time in this call chain when nfs3_delmap is called we
5566 * will find this caller in the delmap caller list and realize there
5567 * is no more work to do thus removing this caller from the list and
5568 * returning the error that was set in the callback execution.
5570 caller_found
= nfs_find_and_delete_delmapcall(rp
, &error
);
5573 * 'error' is from the actual delmap operations. To avoid
5574 * hangs, we need to handle the return of EAGAIN differently
5575 * since this is what drives the callback execution.
5576 * In this case, we don't want to return EAGAIN and do the
5577 * callback execution because there are none to execute.
5579 if (error
== EAGAIN
)
5585 /* current caller was not in the list */
5586 delmap_call
= nfs_init_delmapcall();
5588 mutex_enter(&rp
->r_statelock
);
5589 list_insert_tail(&rp
->r_indelmap
, delmap_call
);
5590 mutex_exit(&rp
->r_statelock
);
5592 dmapp
= kmem_alloc(sizeof (nfs_delmap_args_t
), KM_SLEEP
);
5599 dmapp
->maxprot
= maxprot
;
5600 dmapp
->flags
= flags
;
5602 dmapp
->caller
= delmap_call
;
5604 error
= as_add_callback(as
, nfs3_delmap_callback
, dmapp
,
5605 AS_UNMAP_EVENT
, addr
, len
, KM_SLEEP
);
5607 return (error
? error
: EAGAIN
);
5611 * Remove some pages from an mmap'd vnode. Just update the
5612 * count of pages. If doing close-to-open, then flush and
5613 * commit all of the pages associated with this file.
5614 * Otherwise, start an asynchronous page flush to write out
5615 * any dirty pages. This will also associate a credential
5616 * with the rnode which can be used to write the pages.
5620 nfs3_delmap_callback(struct as
*as
, void *arg
, uint_t event
)
5625 nfs_delmap_args_t
*dmapp
= (nfs_delmap_args_t
*)arg
;
5627 rp
= VTOR(dmapp
->vp
);
5628 mi
= VTOMI(dmapp
->vp
);
5630 atomic_add_long((ulong_t
*)&rp
->r_mapcnt
, -btopr(dmapp
->len
));
5631 ASSERT(rp
->r_mapcnt
>= 0);
5634 * Initiate a page flush and potential commit if there are
5635 * pages, the file system was not mounted readonly, the segment
5636 * was mapped shared, and the pages themselves were writeable.
5638 if (vn_has_cached_data(dmapp
->vp
) && !vn_is_readonly(dmapp
->vp
) &&
5639 dmapp
->flags
== MAP_SHARED
&& (dmapp
->maxprot
& PROT_WRITE
)) {
5640 mutex_enter(&rp
->r_statelock
);
5641 rp
->r_flags
|= RDIRTY
;
5642 mutex_exit(&rp
->r_statelock
);
5644 * If this is a cross-zone access a sync putpage won't work, so
5645 * the best we can do is try an async putpage. That seems
5646 * better than something more draconian such as discarding the
5649 if ((mi
->mi_flags
& MI_NOCTO
) ||
5650 nfs_zone() != mi
->mi_zone
)
5651 error
= nfs3_putpage(dmapp
->vp
, dmapp
->off
, dmapp
->len
,
5652 B_ASYNC
, dmapp
->cr
, NULL
);
5654 error
= nfs3_putpage_commit(dmapp
->vp
, dmapp
->off
,
5655 dmapp
->len
, dmapp
->cr
);
5657 mutex_enter(&rp
->r_statelock
);
5658 error
= rp
->r_error
;
5660 mutex_exit(&rp
->r_statelock
);
5665 if ((rp
->r_flags
& RDIRECTIO
) || (mi
->mi_flags
& MI_DIRECTIO
))
5666 (void) nfs3_putpage(dmapp
->vp
, dmapp
->off
, dmapp
->len
,
5667 B_INVAL
, dmapp
->cr
, NULL
);
5669 dmapp
->caller
->error
= error
;
5670 (void) as_delete_callback(as
, arg
);
5671 kmem_free(dmapp
, sizeof (nfs_delmap_args_t
));
5674 static int nfs3_pathconf_disable_cache
= 0;
5677 static int nfs3_pathconf_cache_hits
= 0;
5678 static int nfs3_pathconf_cache_misses
= 0;
5683 nfs3_pathconf(vnode_t
*vp
, int cmd
, ulong_t
*valp
, cred_t
*cr
,
5684 caller_context_t
*ct
)
5694 if (nfs_zone() != VTOMI(vp
)->mi_zone
)
5697 * Large file spec - need to base answer on info stored
5698 * on original FSINFO response.
5700 if (cmd
== _PC_FILESIZEBITS
) {
5701 unsigned long long ll
;
5704 ll
= VTOMI(vp
)->mi_maxfilesize
;
5711 if (ll
& 0xffffffff00000000) {
5714 if (ll
& 0xffff0000) {
5734 if (cmd
== _PC_ACL_ENABLED
) {
5735 *valp
= _ACL_ACLENT_ENABLED
;
5739 if (cmd
== _PC_XATTR_EXISTS
) {
5742 if (vp
->v_vfsp
->vfs_flag
& VFS_XATTR
) {
5746 mntinfo_t
*mi
= VTOMI(vp
);
5748 if (!(mi
->mi_flags
& MI_EXTATTR
))
5752 if (nfs_rw_enter_sig(&rp
->r_rwlock
, RW_READER
,
5756 error
= nfs3lookup_dnlc(vp
, XATTR_DIR_NAME
, &avp
, cr
);
5757 if (error
|| avp
== NULL
)
5758 error
= acl_getxattrdir3(vp
, &avp
, 0, cr
, 0);
5760 nfs_rw_exit(&rp
->r_rwlock
);
5762 if (error
== 0 && avp
!= NULL
) {
5763 error
= do_xattr_exists_check(avp
, valp
, cr
);
5765 } else if (error
== ENOENT
) {
5774 if (rp
->r_pathconf
!= NULL
) {
5775 mutex_enter(&rp
->r_statelock
);
5776 if (rp
->r_pathconf
!= NULL
&& nfs3_pathconf_disable_cache
) {
5777 kmem_free(rp
->r_pathconf
, sizeof (*rp
->r_pathconf
));
5778 rp
->r_pathconf
= NULL
;
5780 if (rp
->r_pathconf
!= NULL
) {
5784 *valp
= rp
->r_pathconf
->link_max
;
5787 *valp
= rp
->r_pathconf
->name_max
;
5790 case _PC_SYMLINK_MAX
:
5793 case _PC_CHOWN_RESTRICTED
:
5794 *valp
= rp
->r_pathconf
->chown_restricted
;
5797 *valp
= rp
->r_pathconf
->no_trunc
;
5803 mutex_exit(&rp
->r_statelock
);
5805 nfs3_pathconf_cache_hits
++;
5809 mutex_exit(&rp
->r_statelock
);
5812 nfs3_pathconf_cache_misses
++;
5815 args
.object
= *VTOFH3(vp
);
5817 fi
.fhp
= (caddr_t
)&args
.object
;
5818 fi
.copyproc
= nfs3copyfh
;
5819 fi
.lookupproc
= nfs3lookup
;
5820 fi
.xattrdirproc
= acl_getxattrdir3
;
5826 error
= rfs3call(VTOMI(vp
), NFSPROC3_PATHCONF
,
5827 xdr_nfs_fh3
, (caddr_t
)&args
,
5828 xdr_PATHCONF3res
, (caddr_t
)&res
, cr
,
5829 &douprintf
, &res
.status
, 0, &fi
);
5834 error
= geterrno3(res
.status
);
5837 nfs3_cache_post_op_attr(vp
, &res
.resok
.obj_attributes
, t
, cr
);
5838 if (!nfs3_pathconf_disable_cache
) {
5839 mutex_enter(&rp
->r_statelock
);
5840 if (rp
->r_pathconf
== NULL
) {
5841 rp
->r_pathconf
= kmem_alloc(
5842 sizeof (*rp
->r_pathconf
), KM_NOSLEEP
);
5843 if (rp
->r_pathconf
!= NULL
)
5844 *rp
->r_pathconf
= res
.resok
.info
;
5846 mutex_exit(&rp
->r_statelock
);
5850 *valp
= res
.resok
.info
.link_max
;
5853 *valp
= res
.resok
.info
.name_max
;
5856 case _PC_SYMLINK_MAX
:
5859 case _PC_CHOWN_RESTRICTED
:
5860 *valp
= res
.resok
.info
.chown_restricted
;
5863 *valp
= res
.resok
.info
.no_trunc
;
5869 nfs3_cache_post_op_attr(vp
, &res
.resfail
.obj_attributes
, t
, cr
);
5870 PURGE_STALE_FH(error
, vp
, cr
);
5877 * Called by async thread to do synchronous pageio. Do the i/o, wait
5878 * for it to complete, and cleanup the page list when done.
5881 nfs3_sync_pageio(vnode_t
*vp
, page_t
*pp
, uoff_t io_off
, size_t io_len
,
5882 int flags
, cred_t
*cr
)
5886 ASSERT(nfs_zone() == VTOMI(vp
)->mi_zone
);
5887 error
= nfs3_rdwrlbn(vp
, pp
, io_off
, io_len
, flags
, cr
);
5889 pvn_read_done(pp
, (error
? B_ERROR
: 0) | flags
);
5891 pvn_write_done(pp
, (error
? B_ERROR
: 0) | flags
);
5897 nfs3_pageio(vnode_t
*vp
, page_t
*pp
, uoff_t io_off
, size_t io_len
,
5898 int flags
, cred_t
*cr
, caller_context_t
*ct
)
5905 if (!(flags
& B_ASYNC
) && nfs_zone() != VTOMI(vp
)->mi_zone
)
5909 mutex_enter(&rp
->r_statelock
);
5911 mutex_exit(&rp
->r_statelock
);
5913 if (flags
& B_ASYNC
) {
5914 error
= nfs_async_pageio(vp
, pp
, io_off
, io_len
, flags
, cr
,
5917 error
= nfs3_rdwrlbn(vp
, pp
, io_off
, io_len
, flags
, cr
);
5918 mutex_enter(&rp
->r_statelock
);
5920 cv_broadcast(&rp
->r_cv
);
5921 mutex_exit(&rp
->r_statelock
);
5927 nfs3_dispose(vnode_t
*vp
, page_t
*pp
, int fl
, int dn
, cred_t
*cr
,
5928 caller_context_t
*ct
)
5939 * We should get called with fl equal to either B_FREE or
5940 * B_INVAL. Any other value is illegal.
5942 * The page that we are either supposed to free or destroy
5943 * should be exclusive locked and its io lock should not
5946 ASSERT(fl
== B_FREE
|| fl
== B_INVAL
);
5947 ASSERT((PAGE_EXCL(pp
) && !page_iolock_assert(pp
)) || panicstr
);
5951 * If the page doesn't need to be committed or we shouldn't
5952 * even bother attempting to commit it, then just make sure
5953 * that the p_fsdata byte is clear and then either free or
5954 * destroy the page as appropriate.
5956 if (pp
->p_fsdata
== C_NOCOMMIT
|| (rp
->r_flags
& RSTALE
)) {
5957 pp
->p_fsdata
= C_NOCOMMIT
;
5961 page_destroy(pp
, dn
);
5966 * If there is a page invalidation operation going on, then
5967 * if this is one of the pages being destroyed, then just
5968 * clear the p_fsdata byte and then either free or destroy
5969 * the page as appropriate.
5971 mutex_enter(&rp
->r_statelock
);
5972 if ((rp
->r_flags
& RTRUNCATE
) && pp
->p_offset
>= rp
->r_truncaddr
) {
5973 mutex_exit(&rp
->r_statelock
);
5974 pp
->p_fsdata
= C_NOCOMMIT
;
5978 page_destroy(pp
, dn
);
5983 * If we are freeing this page and someone else is already
5984 * waiting to do a commit, then just unlock the page and
5985 * return. That other thread will take care of commiting
5986 * this page. The page can be freed sometime after the
5987 * commit has finished. Otherwise, if the page is marked
5988 * as delay commit, then we may be getting called from
5989 * pvn_write_done, one page at a time. This could result
5990 * in one commit per page, so we end up doing lots of small
5991 * commits instead of fewer larger commits. This is bad,
5992 * we want do as few commits as possible.
5995 if (rp
->r_flags
& RCOMMITWAIT
) {
5997 mutex_exit(&rp
->r_statelock
);
6000 if (pp
->p_fsdata
== C_DELAYCOMMIT
) {
6001 pp
->p_fsdata
= C_COMMIT
;
6003 mutex_exit(&rp
->r_statelock
);
6009 * Check to see if there is a signal which would prevent an
6010 * attempt to commit the pages from being successful. If so,
6011 * then don't bother with all of the work to gather pages and
6012 * generate the unsuccessful RPC. Just return from here and
6013 * let the page be committed at some later time.
6015 sigintr(&smask
, VTOMI(vp
)->mi_flags
& MI_INT
);
6016 if (ttolwp(curthread
) != NULL
&& ISSIG(curthread
, JUSTLOOKING
)) {
6019 mutex_exit(&rp
->r_statelock
);
6025 * We are starting to need to commit pages, so let's try
6026 * to commit as many as possible at once to reduce the
6029 * Set the `commit inprogress' state bit. We must
6030 * first wait until any current one finishes. Then
6031 * we initialize the c_pages list with this page.
6033 while (rp
->r_flags
& RCOMMIT
) {
6034 rp
->r_flags
|= RCOMMITWAIT
;
6035 cv_wait(&rp
->r_commit
.c_cv
, &rp
->r_statelock
);
6036 rp
->r_flags
&= ~RCOMMITWAIT
;
6038 rp
->r_flags
|= RCOMMIT
;
6039 mutex_exit(&rp
->r_statelock
);
6040 ASSERT(rp
->r_commit
.c_pages
== NULL
);
6041 rp
->r_commit
.c_pages
= pp
;
6042 rp
->r_commit
.c_commbase
= (offset3
)pp
->p_offset
;
6043 rp
->r_commit
.c_commlen
= PAGESIZE
;
6046 * Gather together all other pages which can be committed.
6047 * They will all be chained off r_commit.c_pages.
6049 nfs3_get_commit(vp
);
6052 * Clear the `commit inprogress' status and disconnect
6053 * the list of pages to be committed from the rnode.
6054 * At this same time, we also save the starting offset
6055 * and length of data to be committed on the server.
6057 plist
= rp
->r_commit
.c_pages
;
6058 rp
->r_commit
.c_pages
= NULL
;
6059 offset
= rp
->r_commit
.c_commbase
;
6060 len
= rp
->r_commit
.c_commlen
;
6061 mutex_enter(&rp
->r_statelock
);
6062 rp
->r_flags
&= ~RCOMMIT
;
6063 cv_broadcast(&rp
->r_commit
.c_cv
);
6064 mutex_exit(&rp
->r_statelock
);
6066 if (curproc
== proc_pageout
|| curproc
== proc_fsflush
||
6067 nfs_zone() != VTOMI(vp
)->mi_zone
) {
6068 nfs_async_commit(vp
, plist
, offset
, len
, cr
, nfs3_async_commit
);
6073 * Actually generate the COMMIT3 over the wire operation.
6075 error
= nfs3_commit(vp
, offset
, len
, cr
);
6078 * If we got an error during the commit, just unlock all
6079 * of the pages. The pages will get retransmitted to the
6080 * server during a putpage operation.
6083 while (plist
!= NULL
) {
6085 page_sub(&plist
, pptr
);
6092 * We've tried as hard as we can to commit the data to stable
6093 * storage on the server. We release the rest of the pages
6094 * and clear the commit required state. They will be put
6095 * onto the tail of the cachelist if they are nolonger
6098 while (plist
!= pp
) {
6100 page_sub(&plist
, pptr
);
6101 pptr
->p_fsdata
= C_NOCOMMIT
;
6102 (void) page_release(pptr
, 1);
6106 * It is possible that nfs3_commit didn't return error but
6107 * some other thread has modified the page we are going
6109 * In this case we need to rewrite the page. Do an explicit check
6110 * before attempting to free/destroy the page. If modified, needs to
6111 * be rewritten so unlock the page and return.
6113 if (hat_ismod(pp
)) {
6114 pp
->p_fsdata
= C_NOCOMMIT
;
6120 * Now, as appropriate, either free or destroy the page
6121 * that we were called with.
6123 pp
->p_fsdata
= C_NOCOMMIT
;
6127 page_destroy(pp
, dn
);
6131 nfs3_commit(vnode_t
*vp
, offset3 offset
, count3 count
, cred_t
*cr
)
6141 ASSERT(nfs_zone() == VTOMI(vp
)->mi_zone
);
6143 mutex_enter(&rp
->r_statelock
);
6144 if (rp
->r_cred
!= NULL
) {
6153 mutex_exit(&rp
->r_statelock
);
6155 args
.file
= *VTOFH3(vp
);
6156 args
.offset
= offset
;
6161 error
= rfs3call(VTOMI(vp
), NFSPROC3_COMMIT
,
6162 xdr_COMMIT3args
, (caddr_t
)&args
,
6163 xdr_COMMIT3res
, (caddr_t
)&res
, cred
,
6164 &douprintf
, &res
.status
, 0, NULL
);
6171 error
= geterrno3(res
.status
);
6173 ASSERT(rp
->r_flags
& RHAVEVERF
);
6174 mutex_enter(&rp
->r_statelock
);
6175 if (rp
->r_verf
== res
.resok
.verf
) {
6176 mutex_exit(&rp
->r_statelock
);
6180 rp
->r_verf
= res
.resok
.verf
;
6181 mutex_exit(&rp
->r_statelock
);
6182 error
= NFS_VERF_MISMATCH
;
6184 if (error
== EACCES
) {
6185 mutex_enter(&rp
->r_statelock
);
6187 if (rp
->r_cred
!= NULL
)
6193 mutex_exit(&rp
->r_statelock
);
6196 mutex_exit(&rp
->r_statelock
);
6199 * Can't do a PURGE_STALE_FH here because this
6200 * can cause a deadlock. nfs3_commit can
6201 * be called from nfs3_dispose which can be called
6202 * indirectly via pvn_vplist_dirty. PURGE_STALE_FH
6203 * can call back to pvn_vplist_dirty.
6205 if (error
== ESTALE
) {
6206 mutex_enter(&rp
->r_statelock
);
6207 rp
->r_flags
|= RSTALE
;
6209 rp
->r_error
= error
;
6210 mutex_exit(&rp
->r_statelock
);
6211 PURGE_ATTRCACHE(vp
);
6213 mutex_enter(&rp
->r_statelock
);
6215 rp
->r_error
= error
;
6216 mutex_exit(&rp
->r_statelock
);
6224 nfs3_set_mod(vnode_t
*vp
)
6226 ASSERT(nfs_zone() == VTOMI(vp
)->mi_zone
);
6228 pvn_vplist_setdirty(vp
, nfs_setmod_check
);
6232 * This routine is used to gather together a page list of the pages
6233 * which are to be committed on the server. This routine must not
6234 * be called if the calling thread holds any locked pages.
6236 * The calling thread must have set RCOMMIT. This bit is used to
6237 * serialize access to the commit structure in the rnode. As long
6238 * as the thread has set RCOMMIT, then it can manipulate the commit
6239 * structure without requiring any other locks.
6242 nfs3_get_commit(vnode_t
*vp
)
6249 ASSERT(rp
->r_flags
& RCOMMIT
);
6251 vmobject_lock(&vp
->v_object
);
6254 * Step through all of the pages associated with this vnode
6255 * looking for pages which need to be committed.
6257 for (pp
= vmobject_get_head(&vp
->v_object
);
6259 pp
= vmobject_get_next(&vp
->v_object
, pp
)) {
6260 /* Skip marker pages. */
6261 if (PP_ISPVN_TAG(pp
))
6265 * If this page does not need to be committed or is
6266 * modified, then just skip it.
6268 if (pp
->p_fsdata
== C_NOCOMMIT
|| hat_ismod(pp
))
6272 * Attempt to lock the page. If we can't, then
6273 * someone else is messing with it and we will
6276 if (!page_trylock(pp
, SE_EXCL
))
6280 * If this page does not need to be committed or is
6281 * modified, then just skip it. Recheck now that
6282 * the page is locked.
6284 if (pp
->p_fsdata
== C_NOCOMMIT
|| hat_ismod(pp
)) {
6289 if (PP_ISFREE(pp
)) {
6290 cmn_err(CE_PANIC
, "nfs3_get_commit: %p is free",
6295 * The page needs to be committed and we locked it.
6296 * Update the base and length parameters and add it
6299 if (rp
->r_commit
.c_pages
== NULL
) {
6300 rp
->r_commit
.c_commbase
= (offset3
)pp
->p_offset
;
6301 rp
->r_commit
.c_commlen
= PAGESIZE
;
6302 } else if (pp
->p_offset
< rp
->r_commit
.c_commbase
) {
6303 rp
->r_commit
.c_commlen
= rp
->r_commit
.c_commbase
-
6304 (offset3
)pp
->p_offset
+ rp
->r_commit
.c_commlen
;
6305 rp
->r_commit
.c_commbase
= (offset3
)pp
->p_offset
;
6306 } else if ((rp
->r_commit
.c_commbase
+ rp
->r_commit
.c_commlen
)
6308 rp
->r_commit
.c_commlen
= (offset3
)pp
->p_offset
-
6309 rp
->r_commit
.c_commbase
+ PAGESIZE
;
6311 page_add(&rp
->r_commit
.c_pages
, pp
);
6314 vmobject_unlock(&vp
->v_object
);
6318 * This routine is used to gather together a page list of the pages
6319 * which are to be committed on the server. This routine must not
6320 * be called if the calling thread holds any locked pages.
6322 * The calling thread must have set RCOMMIT. This bit is used to
6323 * serialize access to the commit structure in the rnode. As long
6324 * as the thread has set RCOMMIT, then it can manipulate the commit
6325 * structure without requiring any other locks.
6328 nfs3_get_commit_range(vnode_t
*vp
, uoff_t soff
, size_t len
)
6340 ASSERT(rp
->r_flags
& RCOMMIT
);
6341 ASSERT(nfs_zone() == VTOMI(vp
)->mi_zone
);
6344 * If there are no pages associated with this vnode, then
6347 if (!vn_has_cached_data(vp
))
6351 * Calculate the ending offset.
6355 for (off
= soff
; off
< end
; off
+= PAGESIZE
) {
6357 * Lookup each page by vp, offset.
6359 if ((pp
= page_lookup_nowait(&vp
->v_object
, off
, SE_EXCL
)) == NULL
)
6363 * If this page does not need to be committed or is
6364 * modified, then just skip it.
6366 if (pp
->p_fsdata
== C_NOCOMMIT
|| hat_ismod(pp
)) {
6371 ASSERT(PP_ISFREE(pp
) == 0);
6374 * The page needs to be committed and we locked it.
6375 * Update the base and length parameters and add it
6378 if (rp
->r_commit
.c_pages
== NULL
) {
6379 rp
->r_commit
.c_commbase
= (offset3
)pp
->p_offset
;
6380 rp
->r_commit
.c_commlen
= PAGESIZE
;
6382 rp
->r_commit
.c_commlen
= (offset3
)pp
->p_offset
-
6383 rp
->r_commit
.c_commbase
+ PAGESIZE
;
6385 page_add(&rp
->r_commit
.c_pages
, pp
);
6390 nfs3_putpage_commit(vnode_t
*vp
, offset_t poff
, size_t plen
, cred_t
*cr
)
6393 writeverf3 write_verf
;
6394 rnode_t
*rp
= VTOR(vp
);
6396 ASSERT(nfs_zone() == VTOMI(vp
)->mi_zone
);
6398 * Flush the data portion of the file and then commit any
6399 * portions which need to be committed. This may need to
6400 * be done twice if the server has changed state since
6401 * data was last written. The data will need to be
6402 * rewritten to the server and then a new commit done.
6404 * In fact, this may need to be done several times if the
6405 * server is having problems and crashing while we are
6406 * attempting to do this.
6411 * Do a flush based on the poff and plen arguments. This
6412 * will asynchronously write out any modified pages in the
6413 * range specified by (poff, plen). This starts all of the
6414 * i/o operations which will be waited for in the next
6415 * call to nfs3_putpage
6418 mutex_enter(&rp
->r_statelock
);
6419 write_verf
= rp
->r_verf
;
6420 mutex_exit(&rp
->r_statelock
);
6422 error
= nfs3_putpage(vp
, poff
, plen
, B_ASYNC
, cr
, NULL
);
6423 if (error
== EAGAIN
)
6427 * Do a flush based on the poff and plen arguments. This
6428 * will synchronously write out any modified pages in the
6429 * range specified by (poff, plen) and wait until all of
6430 * the asynchronous i/o's in that range are done as well.
6433 error
= nfs3_putpage(vp
, poff
, plen
, 0, cr
, NULL
);
6438 mutex_enter(&rp
->r_statelock
);
6439 if (rp
->r_verf
!= write_verf
) {
6440 mutex_exit(&rp
->r_statelock
);
6443 mutex_exit(&rp
->r_statelock
);
6446 * Now commit any pages which might need to be committed.
6447 * If the error, NFS_VERF_MISMATCH, is returned, then
6448 * start over with the flush operation.
6451 error
= nfs3_commit_vp(vp
, poff
, plen
, cr
);
6453 if (error
== NFS_VERF_MISMATCH
)
6460 nfs3_commit_vp(vnode_t
*vp
, uoff_t poff
, size_t plen
, cred_t
*cr
)
6470 if (nfs_zone() != VTOMI(vp
)->mi_zone
)
6473 * Set the `commit inprogress' state bit. We must
6474 * first wait until any current one finishes.
6476 mutex_enter(&rp
->r_statelock
);
6477 while (rp
->r_flags
& RCOMMIT
) {
6478 rp
->r_flags
|= RCOMMITWAIT
;
6479 cv_wait(&rp
->r_commit
.c_cv
, &rp
->r_statelock
);
6480 rp
->r_flags
&= ~RCOMMITWAIT
;
6482 rp
->r_flags
|= RCOMMIT
;
6483 mutex_exit(&rp
->r_statelock
);
6486 * Gather together all of the pages which need to be
6490 nfs3_get_commit(vp
);
6492 nfs3_get_commit_range(vp
, poff
, plen
);
6495 * Clear the `commit inprogress' bit and disconnect the
6496 * page list which was gathered together in nfs3_get_commit.
6498 plist
= rp
->r_commit
.c_pages
;
6499 rp
->r_commit
.c_pages
= NULL
;
6500 offset
= rp
->r_commit
.c_commbase
;
6501 len
= rp
->r_commit
.c_commlen
;
6502 mutex_enter(&rp
->r_statelock
);
6503 rp
->r_flags
&= ~RCOMMIT
;
6504 cv_broadcast(&rp
->r_commit
.c_cv
);
6505 mutex_exit(&rp
->r_statelock
);
6508 * If any pages need to be committed, commit them and
6509 * then unlock them so that they can be freed some
6512 if (plist
!= NULL
) {
6514 * No error occurred during the flush portion
6515 * of this operation, so now attempt to commit
6516 * the data to stable storage on the server.
6518 * This will unlock all of the pages on the list.
6520 return (nfs3_sync_commit(vp
, plist
, offset
, len
, cr
));
6526 nfs3_sync_commit(vnode_t
*vp
, page_t
*plist
, offset3 offset
, count3 count
,
6532 ASSERT(nfs_zone() == VTOMI(vp
)->mi_zone
);
6533 error
= nfs3_commit(vp
, offset
, count
, cr
);
6536 * If we got an error, then just unlock all of the pages
6540 while (plist
!= NULL
) {
6542 page_sub(&plist
, pp
);
6548 * We've tried as hard as we can to commit the data to stable
6549 * storage on the server. We just unlock the pages and clear
6550 * the commit required state. They will get freed later.
6552 while (plist
!= NULL
) {
6554 page_sub(&plist
, pp
);
6555 pp
->p_fsdata
= C_NOCOMMIT
;
6563 nfs3_async_commit(vnode_t
*vp
, page_t
*plist
, offset3 offset
, count3 count
,
6566 ASSERT(nfs_zone() == VTOMI(vp
)->mi_zone
);
6567 (void) nfs3_sync_commit(vp
, plist
, offset
, count
, cr
);
6572 nfs3_setsecattr(vnode_t
*vp
, vsecattr_t
*vsecattr
, int flag
, cred_t
*cr
,
6573 caller_context_t
*ct
)
6580 if (nfs_zone() != mi
->mi_zone
)
6583 if (mi
->mi_flags
& MI_ACL
) {
6584 error
= acl_setacl3(vp
, vsecattr
, flag
, cr
);
6585 if (mi
->mi_flags
& MI_ACL
)
6594 nfs3_getsecattr(vnode_t
*vp
, vsecattr_t
*vsecattr
, int flag
, cred_t
*cr
,
6595 caller_context_t
*ct
)
6602 if (nfs_zone() != mi
->mi_zone
)
6605 if (mi
->mi_flags
& MI_ACL
) {
6606 error
= acl_getacl3(vp
, vsecattr
, flag
, cr
);
6607 if (mi
->mi_flags
& MI_ACL
)
6611 return (fs_fab_acl(vp
, vsecattr
, flag
, cr
, ct
));
6616 nfs3_shrlock(vnode_t
*vp
, int cmd
, struct shrlock
*shr
, int flag
, cred_t
*cr
,
6617 caller_context_t
*ct
)
6620 struct shrlock nshr
;
6621 struct nfs_owner nfs_owner
;
6624 if (nfs_zone() != VTOMI(vp
)->mi_zone
)
6628 * check for valid cmd parameter
6630 if (cmd
!= F_SHARE
&& cmd
!= F_UNSHARE
&& cmd
!= F_HASREMOTELOCKS
)
6634 * Check access permissions
6636 if (cmd
== F_SHARE
&&
6637 (((shr
->s_access
& F_RDACC
) && !(flag
& FREAD
)) ||
6638 ((shr
->s_access
& F_WRACC
) && !(flag
& FWRITE
))))
6642 * If the filesystem is mounted using local locking, pass the
6643 * request off to the local share code.
6645 if (VTOMI(vp
)->mi_flags
& MI_LLOCK
)
6646 return (fs_shrlock(vp
, cmd
, shr
, flag
, cr
, ct
));
6651 lm_fh3
.n_len
= VTOFH3(vp
)->fh3_length
;
6652 lm_fh3
.n_bytes
= (char *)&(VTOFH3(vp
)->fh3_u
.data
);
6655 * If passed an owner that is too large to fit in an
6656 * nfs_owner it is likely a recursive call from the
6657 * lock manager client and pass it straight through. If
6658 * it is not a nfs_owner then simply return an error.
6660 if (shr
->s_own_len
> sizeof (nfs_owner
.lowner
)) {
6661 if (((struct nfs_owner
*)shr
->s_owner
)->magic
!=
6665 if (error
= lm4_shrlock(vp
, cmd
, shr
, flag
, &lm_fh3
)) {
6666 error
= set_errno(error
);
6671 * Remote share reservations owner is a combination of
6672 * a magic number, hostname, and the local owner
6674 bzero(&nfs_owner
, sizeof (nfs_owner
));
6675 nfs_owner
.magic
= NFS_OWNER_MAGIC
;
6676 (void) strncpy(nfs_owner
.hname
, uts_nodename(),
6677 sizeof (nfs_owner
.hname
));
6678 bcopy(shr
->s_owner
, nfs_owner
.lowner
, shr
->s_own_len
);
6679 nshr
.s_access
= shr
->s_access
;
6680 nshr
.s_deny
= shr
->s_deny
;
6682 nshr
.s_pid
= ttoproc(curthread
)->p_pid
;
6683 nshr
.s_own_len
= sizeof (nfs_owner
);
6684 nshr
.s_owner
= (caddr_t
)&nfs_owner
;
6686 if (error
= lm4_shrlock(vp
, cmd
, &nshr
, flag
, &lm_fh3
)) {
6687 error
= set_errno(error
);
6692 case F_HASREMOTELOCKS
:
6694 * NFS client can't store remote locks itself