nfs_clvnops.c revision 1.2.10.3 1 1.2.10.2 tls /* $NetBSD: nfs_clvnops.c,v 1.2.10.3 2017/12/03 11:38:42 jdolecek Exp $ */
2 1.2.10.2 tls /*-
3 1.2.10.2 tls * Copyright (c) 1989, 1993
4 1.2.10.2 tls * The Regents of the University of California. All rights reserved.
5 1.2.10.2 tls *
6 1.2.10.2 tls * This code is derived from software contributed to Berkeley by
7 1.2.10.2 tls * Rick Macklem at The University of Guelph.
8 1.2.10.2 tls *
9 1.2.10.2 tls * Redistribution and use in source and binary forms, with or without
10 1.2.10.2 tls * modification, are permitted provided that the following conditions
11 1.2.10.2 tls * are met:
12 1.2.10.2 tls * 1. Redistributions of source code must retain the above copyright
13 1.2.10.2 tls * notice, this list of conditions and the following disclaimer.
14 1.2.10.2 tls * 2. Redistributions in binary form must reproduce the above copyright
15 1.2.10.2 tls * notice, this list of conditions and the following disclaimer in the
16 1.2.10.2 tls * documentation and/or other materials provided with the distribution.
17 1.2.10.2 tls * 4. Neither the name of the University nor the names of its contributors
18 1.2.10.2 tls * may be used to endorse or promote products derived from this software
19 1.2.10.2 tls * without specific prior written permission.
20 1.2.10.2 tls *
21 1.2.10.2 tls * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.2.10.2 tls * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.2.10.2 tls * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.2.10.2 tls * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.2.10.2 tls * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.2.10.2 tls * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.2.10.2 tls * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.2.10.2 tls * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.2.10.2 tls * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.2.10.2 tls * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.2.10.2 tls * SUCH DAMAGE.
32 1.2.10.2 tls *
33 1.2.10.2 tls * from nfs_vnops.c 8.16 (Berkeley) 5/27/95
34 1.2.10.2 tls */
35 1.2.10.2 tls
36 1.2.10.2 tls #include <sys/cdefs.h>
37 1.2.10.3 jdolecek /* __FBSDID("FreeBSD: head/sys/fs/nfsclient/nfs_clvnops.c 304026 2016-08-12 22:44:59Z rmacklem "); */
38 1.2.10.2 tls __RCSID("$NetBSD: nfs_clvnops.c,v 1.2.10.3 2017/12/03 11:38:42 jdolecek Exp $");
39 1.2.10.2 tls
40 1.2.10.2 tls /*
41 1.2.10.2 tls * vnode op calls for Sun NFS version 2, 3 and 4
42 1.2.10.2 tls */
43 1.2.10.2 tls
44 1.2.10.3 jdolecek #ifdef _KERNEL_OPT
45 1.2.10.3 jdolecek #include "opt_dtrace.h"
46 1.2.10.2 tls #include "opt_inet.h"
47 1.2.10.3 jdolecek #endif
48 1.2.10.2 tls
49 1.2.10.2 tls #include <sys/param.h>
50 1.2.10.2 tls #include <sys/kernel.h>
51 1.2.10.2 tls #include <sys/systm.h>
52 1.2.10.2 tls #include <sys/resourcevar.h>
53 1.2.10.2 tls #include <sys/proc.h>
54 1.2.10.2 tls #include <sys/mount.h>
55 1.2.10.2 tls #include <sys/bio.h>
56 1.2.10.2 tls #include <sys/buf.h>
57 1.2.10.2 tls #include <sys/jail.h>
58 1.2.10.2 tls #include <sys/malloc.h>
59 1.2.10.2 tls #include <sys/mbuf.h>
60 1.2.10.2 tls #include <sys/namei.h>
61 1.2.10.2 tls #include <sys/socket.h>
62 1.2.10.2 tls #include <sys/vnode.h>
63 1.2.10.2 tls #include <sys/dirent.h>
64 1.2.10.2 tls #include <sys/fcntl.h>
65 1.2.10.2 tls #include <sys/lockf.h>
66 1.2.10.2 tls #include <sys/stat.h>
67 1.2.10.2 tls #include <sys/sysctl.h>
68 1.2.10.2 tls #include <sys/signalvar.h>
69 1.2.10.2 tls
70 1.2.10.2 tls #include <vm/vm.h>
71 1.2.10.2 tls #include <vm/vm_extern.h>
72 1.2.10.2 tls #include <vm/vm_object.h>
73 1.2.10.2 tls
74 1.2.10.3 jdolecek #include <fs/nfs/common/nfsport.h>
75 1.2.10.3 jdolecek #include <fs/nfs/client/nfsnode.h>
76 1.2.10.3 jdolecek #include <fs/nfs/client/nfsmount.h>
77 1.2.10.3 jdolecek #include <fs/nfs/client/nfs.h>
78 1.2.10.3 jdolecek #include <fs/nfs/client/nfs_kdtrace.h>
79 1.2.10.2 tls
80 1.2.10.2 tls #include <net/if.h>
81 1.2.10.2 tls #include <netinet/in.h>
82 1.2.10.2 tls #include <netinet/in_var.h>
83 1.2.10.2 tls
84 1.2.10.3 jdolecek #include <fs/nfs/common/nfs_lock.h>
85 1.2.10.2 tls
86 1.2.10.2 tls #ifdef KDTRACE_HOOKS
87 1.2.10.2 tls #include <sys/dtrace_bsd.h>
88 1.2.10.2 tls
89 1.2.10.2 tls dtrace_nfsclient_accesscache_flush_probe_func_t
90 1.2.10.2 tls dtrace_nfscl_accesscache_flush_done_probe;
91 1.2.10.2 tls uint32_t nfscl_accesscache_flush_done_id;
92 1.2.10.2 tls
93 1.2.10.2 tls dtrace_nfsclient_accesscache_get_probe_func_t
94 1.2.10.2 tls dtrace_nfscl_accesscache_get_hit_probe,
95 1.2.10.2 tls dtrace_nfscl_accesscache_get_miss_probe;
96 1.2.10.2 tls uint32_t nfscl_accesscache_get_hit_id;
97 1.2.10.2 tls uint32_t nfscl_accesscache_get_miss_id;
98 1.2.10.2 tls
99 1.2.10.2 tls dtrace_nfsclient_accesscache_load_probe_func_t
100 1.2.10.2 tls dtrace_nfscl_accesscache_load_done_probe;
101 1.2.10.2 tls uint32_t nfscl_accesscache_load_done_id;
102 1.2.10.2 tls #endif /* !KDTRACE_HOOKS */
103 1.2.10.2 tls
104 1.2.10.2 tls /* Defs */
105 1.2.10.2 tls #define TRUE 1
106 1.2.10.2 tls #define FALSE 0
107 1.2.10.2 tls
108 1.2.10.3 jdolecek extern struct nfsstatsv1 nfsstatsv1;
109 1.2.10.2 tls extern int nfsrv_useacl;
110 1.2.10.2 tls extern int nfscl_debuglevel;
111 1.2.10.2 tls MALLOC_DECLARE(M_NEWNFSREQ);
112 1.2.10.2 tls
113 1.2.10.2 tls /*
114 1.2.10.2 tls * Ifdef for FreeBSD-current merged buffer cache. It is unfortunate that these
115 1.2.10.2 tls * calls are not in getblk() and brelse() so that they would not be necessary
116 1.2.10.2 tls * here.
117 1.2.10.2 tls */
118 1.2.10.2 tls #ifndef B_VMIO
119 1.2.10.2 tls #define vfs_busy_pages(bp, f)
120 1.2.10.2 tls #endif
121 1.2.10.2 tls
122 1.2.10.2 tls static vop_read_t nfsfifo_read;
123 1.2.10.2 tls static vop_write_t nfsfifo_write;
124 1.2.10.2 tls static vop_close_t nfsfifo_close;
125 1.2.10.2 tls static int nfs_setattrrpc(struct vnode *, struct vattr *, struct ucred *,
126 1.2.10.2 tls struct thread *);
127 1.2.10.2 tls static vop_lookup_t nfs_lookup;
128 1.2.10.2 tls static vop_create_t nfs_create;
129 1.2.10.2 tls static vop_mknod_t nfs_mknod;
130 1.2.10.2 tls static vop_open_t nfs_open;
131 1.2.10.2 tls static vop_pathconf_t nfs_pathconf;
132 1.2.10.2 tls static vop_close_t nfs_close;
133 1.2.10.2 tls static vop_access_t nfs_access;
134 1.2.10.2 tls static vop_getattr_t nfs_getattr;
135 1.2.10.2 tls static vop_setattr_t nfs_setattr;
136 1.2.10.2 tls static vop_read_t nfs_read;
137 1.2.10.2 tls static vop_fsync_t nfs_fsync;
138 1.2.10.2 tls static vop_remove_t nfs_remove;
139 1.2.10.2 tls static vop_link_t nfs_link;
140 1.2.10.2 tls static vop_rename_t nfs_rename;
141 1.2.10.2 tls static vop_mkdir_t nfs_mkdir;
142 1.2.10.2 tls static vop_rmdir_t nfs_rmdir;
143 1.2.10.2 tls static vop_symlink_t nfs_symlink;
144 1.2.10.2 tls static vop_readdir_t nfs_readdir;
145 1.2.10.2 tls static vop_strategy_t nfs_strategy;
146 1.2.10.2 tls static int nfs_lookitup(struct vnode *, char *, int,
147 1.2.10.2 tls struct ucred *, struct thread *, struct nfsnode **);
148 1.2.10.2 tls static int nfs_sillyrename(struct vnode *, struct vnode *,
149 1.2.10.2 tls struct componentname *);
150 1.2.10.2 tls static vop_access_t nfsspec_access;
151 1.2.10.2 tls static vop_readlink_t nfs_readlink;
152 1.2.10.2 tls static vop_print_t nfs_print;
153 1.2.10.2 tls static vop_advlock_t nfs_advlock;
154 1.2.10.2 tls static vop_advlockasync_t nfs_advlockasync;
155 1.2.10.2 tls static vop_getacl_t nfs_getacl;
156 1.2.10.2 tls static vop_setacl_t nfs_setacl;
157 1.2.10.2 tls
158 1.2.10.2 tls /*
159 1.2.10.2 tls * Global vfs data structures for nfs
160 1.2.10.2 tls */
161 1.2.10.2 tls struct vop_vector newnfs_vnodeops = {
162 1.2.10.2 tls .vop_default = &default_vnodeops,
163 1.2.10.2 tls .vop_access = nfs_access,
164 1.2.10.2 tls .vop_advlock = nfs_advlock,
165 1.2.10.2 tls .vop_advlockasync = nfs_advlockasync,
166 1.2.10.2 tls .vop_close = nfs_close,
167 1.2.10.2 tls .vop_create = nfs_create,
168 1.2.10.2 tls .vop_fsync = nfs_fsync,
169 1.2.10.2 tls .vop_getattr = nfs_getattr,
170 1.2.10.2 tls .vop_getpages = ncl_getpages,
171 1.2.10.2 tls .vop_putpages = ncl_putpages,
172 1.2.10.2 tls .vop_inactive = ncl_inactive,
173 1.2.10.2 tls .vop_link = nfs_link,
174 1.2.10.2 tls .vop_lookup = nfs_lookup,
175 1.2.10.2 tls .vop_mkdir = nfs_mkdir,
176 1.2.10.2 tls .vop_mknod = nfs_mknod,
177 1.2.10.2 tls .vop_open = nfs_open,
178 1.2.10.2 tls .vop_pathconf = nfs_pathconf,
179 1.2.10.2 tls .vop_print = nfs_print,
180 1.2.10.2 tls .vop_read = nfs_read,
181 1.2.10.2 tls .vop_readdir = nfs_readdir,
182 1.2.10.2 tls .vop_readlink = nfs_readlink,
183 1.2.10.2 tls .vop_reclaim = ncl_reclaim,
184 1.2.10.2 tls .vop_remove = nfs_remove,
185 1.2.10.2 tls .vop_rename = nfs_rename,
186 1.2.10.2 tls .vop_rmdir = nfs_rmdir,
187 1.2.10.2 tls .vop_setattr = nfs_setattr,
188 1.2.10.2 tls .vop_strategy = nfs_strategy,
189 1.2.10.2 tls .vop_symlink = nfs_symlink,
190 1.2.10.2 tls .vop_write = ncl_write,
191 1.2.10.2 tls .vop_getacl = nfs_getacl,
192 1.2.10.2 tls .vop_setacl = nfs_setacl,
193 1.2.10.2 tls };
194 1.2.10.2 tls
195 1.2.10.2 tls struct vop_vector newnfs_fifoops = {
196 1.2.10.2 tls .vop_default = &fifo_specops,
197 1.2.10.2 tls .vop_access = nfsspec_access,
198 1.2.10.2 tls .vop_close = nfsfifo_close,
199 1.2.10.2 tls .vop_fsync = nfs_fsync,
200 1.2.10.2 tls .vop_getattr = nfs_getattr,
201 1.2.10.2 tls .vop_inactive = ncl_inactive,
202 1.2.10.2 tls .vop_print = nfs_print,
203 1.2.10.2 tls .vop_read = nfsfifo_read,
204 1.2.10.2 tls .vop_reclaim = ncl_reclaim,
205 1.2.10.2 tls .vop_setattr = nfs_setattr,
206 1.2.10.2 tls .vop_write = nfsfifo_write,
207 1.2.10.2 tls };
208 1.2.10.2 tls
209 1.2.10.2 tls static int nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp,
210 1.2.10.2 tls struct componentname *cnp, struct vattr *vap);
211 1.2.10.2 tls static int nfs_removerpc(struct vnode *dvp, struct vnode *vp, char *name,
212 1.2.10.2 tls int namelen, struct ucred *cred, struct thread *td);
213 1.2.10.2 tls static int nfs_renamerpc(struct vnode *fdvp, struct vnode *fvp,
214 1.2.10.2 tls char *fnameptr, int fnamelen, struct vnode *tdvp, struct vnode *tvp,
215 1.2.10.2 tls char *tnameptr, int tnamelen, struct ucred *cred, struct thread *td);
216 1.2.10.2 tls static int nfs_renameit(struct vnode *sdvp, struct vnode *svp,
217 1.2.10.2 tls struct componentname *scnp, struct sillyrename *sp);
218 1.2.10.2 tls
219 1.2.10.2 tls /*
220 1.2.10.2 tls * Global variables
221 1.2.10.2 tls */
222 1.2.10.2 tls #define DIRHDSIZ (sizeof (struct dirent) - (MAXNAMLEN + 1))
223 1.2.10.2 tls
224 1.2.10.2 tls SYSCTL_DECL(_vfs_nfs);
225 1.2.10.2 tls
226 1.2.10.2 tls static int nfsaccess_cache_timeout = NFS_MAXATTRTIMO;
227 1.2.10.2 tls SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_timeout, CTLFLAG_RW,
228 1.2.10.2 tls &nfsaccess_cache_timeout, 0, "NFS ACCESS cache timeout");
229 1.2.10.2 tls
230 1.2.10.2 tls static int nfs_prime_access_cache = 0;
231 1.2.10.2 tls SYSCTL_INT(_vfs_nfs, OID_AUTO, prime_access_cache, CTLFLAG_RW,
232 1.2.10.2 tls &nfs_prime_access_cache, 0,
233 1.2.10.2 tls "Prime NFS ACCESS cache when fetching attributes");
234 1.2.10.2 tls
235 1.2.10.2 tls static int newnfs_commit_on_close = 0;
236 1.2.10.2 tls SYSCTL_INT(_vfs_nfs, OID_AUTO, commit_on_close, CTLFLAG_RW,
237 1.2.10.2 tls &newnfs_commit_on_close, 0, "write+commit on close, else only write");
238 1.2.10.2 tls
239 1.2.10.2 tls static int nfs_clean_pages_on_close = 1;
240 1.2.10.2 tls SYSCTL_INT(_vfs_nfs, OID_AUTO, clean_pages_on_close, CTLFLAG_RW,
241 1.2.10.2 tls &nfs_clean_pages_on_close, 0, "NFS clean dirty pages on close");
242 1.2.10.2 tls
243 1.2.10.2 tls int newnfs_directio_enable = 0;
244 1.2.10.2 tls SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs_directio_enable, CTLFLAG_RW,
245 1.2.10.2 tls &newnfs_directio_enable, 0, "Enable NFS directio");
246 1.2.10.2 tls
247 1.2.10.2 tls int nfs_keep_dirty_on_error;
248 1.2.10.2 tls SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs_keep_dirty_on_error, CTLFLAG_RW,
249 1.2.10.2 tls &nfs_keep_dirty_on_error, 0, "Retry pageout if error returned");
250 1.2.10.2 tls
251 1.2.10.2 tls /*
252 1.2.10.2 tls * This sysctl allows other processes to mmap a file that has been opened
253 1.2.10.2 tls * O_DIRECT by a process. In general, having processes mmap the file while
254 1.2.10.2 tls * Direct IO is in progress can lead to Data Inconsistencies. But, we allow
255 1.2.10.2 tls * this by default to prevent DoS attacks - to prevent a malicious user from
256 1.2.10.2 tls * opening up files O_DIRECT preventing other users from mmap'ing these
257 1.2.10.2 tls * files. "Protected" environments where stricter consistency guarantees are
258 1.2.10.2 tls * required can disable this knob. The process that opened the file O_DIRECT
259 1.2.10.2 tls * cannot mmap() the file, because mmap'ed IO on an O_DIRECT open() is not
260 1.2.10.2 tls * meaningful.
261 1.2.10.2 tls */
262 1.2.10.2 tls int newnfs_directio_allow_mmap = 1;
263 1.2.10.2 tls SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs_directio_allow_mmap, CTLFLAG_RW,
264 1.2.10.2 tls &newnfs_directio_allow_mmap, 0, "Enable mmaped IO on file with O_DIRECT opens");
265 1.2.10.2 tls
266 1.2.10.2 tls #define NFSACCESS_ALL (NFSACCESS_READ | NFSACCESS_MODIFY \
267 1.2.10.2 tls | NFSACCESS_EXTEND | NFSACCESS_EXECUTE \
268 1.2.10.2 tls | NFSACCESS_DELETE | NFSACCESS_LOOKUP)
269 1.2.10.2 tls
270 1.2.10.2 tls /*
271 1.2.10.2 tls * SMP Locking Note :
272 1.2.10.2 tls * The list of locks after the description of the lock is the ordering
273 1.2.10.2 tls * of other locks acquired with the lock held.
274 1.2.10.2 tls * np->n_mtx : Protects the fields in the nfsnode.
275 1.2.10.2 tls VM Object Lock
276 1.2.10.2 tls VI_MTX (acquired indirectly)
277 1.2.10.2 tls * nmp->nm_mtx : Protects the fields in the nfsmount.
278 1.2.10.2 tls rep->r_mtx
279 1.2.10.2 tls * ncl_iod_mutex : Global lock, protects shared nfsiod state.
280 1.2.10.2 tls * nfs_reqq_mtx : Global lock, protects the nfs_reqq list.
281 1.2.10.2 tls nmp->nm_mtx
282 1.2.10.2 tls rep->r_mtx
283 1.2.10.2 tls * rep->r_mtx : Protects the fields in an nfsreq.
284 1.2.10.2 tls */
285 1.2.10.2 tls
286 1.2.10.2 tls static int
287 1.2.10.2 tls nfs34_access_otw(struct vnode *vp, int wmode, struct thread *td,
288 1.2.10.2 tls struct ucred *cred, u_int32_t *retmode)
289 1.2.10.2 tls {
290 1.2.10.2 tls int error = 0, attrflag, i, lrupos;
291 1.2.10.2 tls u_int32_t rmode;
292 1.2.10.2 tls struct nfsnode *np = VTONFS(vp);
293 1.2.10.2 tls struct nfsvattr nfsva;
294 1.2.10.2 tls
295 1.2.10.2 tls error = nfsrpc_accessrpc(vp, wmode, cred, td, &nfsva, &attrflag,
296 1.2.10.2 tls &rmode, NULL);
297 1.2.10.2 tls if (attrflag)
298 1.2.10.2 tls (void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
299 1.2.10.2 tls if (!error) {
300 1.2.10.2 tls lrupos = 0;
301 1.2.10.2 tls mtx_lock(&np->n_mtx);
302 1.2.10.2 tls for (i = 0; i < NFS_ACCESSCACHESIZE; i++) {
303 1.2.10.2 tls if (np->n_accesscache[i].uid == cred->cr_uid) {
304 1.2.10.2 tls np->n_accesscache[i].mode = rmode;
305 1.2.10.2 tls np->n_accesscache[i].stamp = time_second;
306 1.2.10.2 tls break;
307 1.2.10.2 tls }
308 1.2.10.2 tls if (i > 0 && np->n_accesscache[i].stamp <
309 1.2.10.2 tls np->n_accesscache[lrupos].stamp)
310 1.2.10.2 tls lrupos = i;
311 1.2.10.2 tls }
312 1.2.10.2 tls if (i == NFS_ACCESSCACHESIZE) {
313 1.2.10.2 tls np->n_accesscache[lrupos].uid = cred->cr_uid;
314 1.2.10.2 tls np->n_accesscache[lrupos].mode = rmode;
315 1.2.10.2 tls np->n_accesscache[lrupos].stamp = time_second;
316 1.2.10.2 tls }
317 1.2.10.2 tls mtx_unlock(&np->n_mtx);
318 1.2.10.2 tls if (retmode != NULL)
319 1.2.10.2 tls *retmode = rmode;
320 1.2.10.2 tls KDTRACE_NFS_ACCESSCACHE_LOAD_DONE(vp, cred->cr_uid, rmode, 0);
321 1.2.10.2 tls } else if (NFS_ISV4(vp)) {
322 1.2.10.2 tls error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
323 1.2.10.2 tls }
324 1.2.10.2 tls #ifdef KDTRACE_HOOKS
325 1.2.10.2 tls if (error != 0)
326 1.2.10.2 tls KDTRACE_NFS_ACCESSCACHE_LOAD_DONE(vp, cred->cr_uid, 0,
327 1.2.10.2 tls error);
328 1.2.10.2 tls #endif
329 1.2.10.2 tls return (error);
330 1.2.10.2 tls }
331 1.2.10.2 tls
332 1.2.10.2 tls /*
333 1.2.10.2 tls * nfs access vnode op.
334 1.2.10.2 tls * For nfs version 2, just return ok. File accesses may fail later.
335 1.2.10.2 tls * For nfs version 3, use the access rpc to check accessibility. If file modes
336 1.2.10.2 tls * are changed on the server, accesses might still fail later.
337 1.2.10.2 tls */
338 1.2.10.2 tls static int
339 1.2.10.2 tls nfs_access(struct vop_access_args *ap)
340 1.2.10.2 tls {
341 1.2.10.2 tls struct vnode *vp = ap->a_vp;
342 1.2.10.2 tls int error = 0, i, gotahit;
343 1.2.10.2 tls u_int32_t mode, wmode, rmode;
344 1.2.10.2 tls int v34 = NFS_ISV34(vp);
345 1.2.10.2 tls struct nfsnode *np = VTONFS(vp);
346 1.2.10.2 tls
347 1.2.10.2 tls /*
348 1.2.10.2 tls * Disallow write attempts on filesystems mounted read-only;
349 1.2.10.2 tls * unless the file is a socket, fifo, or a block or character
350 1.2.10.2 tls * device resident on the filesystem.
351 1.2.10.2 tls */
352 1.2.10.2 tls if ((ap->a_accmode & (VWRITE | VAPPEND | VWRITE_NAMED_ATTRS |
353 1.2.10.2 tls VDELETE_CHILD | VWRITE_ATTRIBUTES | VDELETE | VWRITE_ACL |
354 1.2.10.2 tls VWRITE_OWNER)) != 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) != 0) {
355 1.2.10.2 tls switch (vp->v_type) {
356 1.2.10.2 tls case VREG:
357 1.2.10.2 tls case VDIR:
358 1.2.10.2 tls case VLNK:
359 1.2.10.2 tls return (EROFS);
360 1.2.10.2 tls default:
361 1.2.10.2 tls break;
362 1.2.10.2 tls }
363 1.2.10.2 tls }
364 1.2.10.2 tls /*
365 1.2.10.2 tls * For nfs v3 or v4, check to see if we have done this recently, and if
366 1.2.10.2 tls * so return our cached result instead of making an ACCESS call.
367 1.2.10.2 tls * If not, do an access rpc, otherwise you are stuck emulating
368 1.2.10.2 tls * ufs_access() locally using the vattr. This may not be correct,
369 1.2.10.2 tls * since the server may apply other access criteria such as
370 1.2.10.2 tls * client uid-->server uid mapping that we do not know about.
371 1.2.10.2 tls */
372 1.2.10.2 tls if (v34) {
373 1.2.10.2 tls if (ap->a_accmode & VREAD)
374 1.2.10.2 tls mode = NFSACCESS_READ;
375 1.2.10.2 tls else
376 1.2.10.2 tls mode = 0;
377 1.2.10.2 tls if (vp->v_type != VDIR) {
378 1.2.10.2 tls if (ap->a_accmode & VWRITE)
379 1.2.10.2 tls mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND);
380 1.2.10.2 tls if (ap->a_accmode & VAPPEND)
381 1.2.10.2 tls mode |= NFSACCESS_EXTEND;
382 1.2.10.2 tls if (ap->a_accmode & VEXEC)
383 1.2.10.2 tls mode |= NFSACCESS_EXECUTE;
384 1.2.10.2 tls if (ap->a_accmode & VDELETE)
385 1.2.10.2 tls mode |= NFSACCESS_DELETE;
386 1.2.10.2 tls } else {
387 1.2.10.2 tls if (ap->a_accmode & VWRITE)
388 1.2.10.2 tls mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND);
389 1.2.10.2 tls if (ap->a_accmode & VAPPEND)
390 1.2.10.2 tls mode |= NFSACCESS_EXTEND;
391 1.2.10.2 tls if (ap->a_accmode & VEXEC)
392 1.2.10.2 tls mode |= NFSACCESS_LOOKUP;
393 1.2.10.2 tls if (ap->a_accmode & VDELETE)
394 1.2.10.2 tls mode |= NFSACCESS_DELETE;
395 1.2.10.2 tls if (ap->a_accmode & VDELETE_CHILD)
396 1.2.10.2 tls mode |= NFSACCESS_MODIFY;
397 1.2.10.2 tls }
398 1.2.10.2 tls /* XXX safety belt, only make blanket request if caching */
399 1.2.10.2 tls if (nfsaccess_cache_timeout > 0) {
400 1.2.10.2 tls wmode = NFSACCESS_READ | NFSACCESS_MODIFY |
401 1.2.10.2 tls NFSACCESS_EXTEND | NFSACCESS_EXECUTE |
402 1.2.10.2 tls NFSACCESS_DELETE | NFSACCESS_LOOKUP;
403 1.2.10.2 tls } else {
404 1.2.10.2 tls wmode = mode;
405 1.2.10.2 tls }
406 1.2.10.2 tls
407 1.2.10.2 tls /*
408 1.2.10.2 tls * Does our cached result allow us to give a definite yes to
409 1.2.10.2 tls * this request?
410 1.2.10.2 tls */
411 1.2.10.2 tls gotahit = 0;
412 1.2.10.2 tls mtx_lock(&np->n_mtx);
413 1.2.10.2 tls for (i = 0; i < NFS_ACCESSCACHESIZE; i++) {
414 1.2.10.2 tls if (ap->a_cred->cr_uid == np->n_accesscache[i].uid) {
415 1.2.10.2 tls if (time_second < (np->n_accesscache[i].stamp
416 1.2.10.2 tls + nfsaccess_cache_timeout) &&
417 1.2.10.2 tls (np->n_accesscache[i].mode & mode) == mode) {
418 1.2.10.3 jdolecek NFSINCRGLOBAL(nfsstatsv1.accesscache_hits);
419 1.2.10.2 tls gotahit = 1;
420 1.2.10.2 tls }
421 1.2.10.2 tls break;
422 1.2.10.2 tls }
423 1.2.10.2 tls }
424 1.2.10.2 tls mtx_unlock(&np->n_mtx);
425 1.2.10.2 tls #ifdef KDTRACE_HOOKS
426 1.2.10.2 tls if (gotahit != 0)
427 1.2.10.2 tls KDTRACE_NFS_ACCESSCACHE_GET_HIT(vp,
428 1.2.10.2 tls ap->a_cred->cr_uid, mode);
429 1.2.10.2 tls else
430 1.2.10.2 tls KDTRACE_NFS_ACCESSCACHE_GET_MISS(vp,
431 1.2.10.2 tls ap->a_cred->cr_uid, mode);
432 1.2.10.2 tls #endif
433 1.2.10.2 tls if (gotahit == 0) {
434 1.2.10.2 tls /*
435 1.2.10.2 tls * Either a no, or a don't know. Go to the wire.
436 1.2.10.2 tls */
437 1.2.10.3 jdolecek NFSINCRGLOBAL(nfsstatsv1.accesscache_misses);
438 1.2.10.2 tls error = nfs34_access_otw(vp, wmode, ap->a_td,
439 1.2.10.2 tls ap->a_cred, &rmode);
440 1.2.10.2 tls if (!error &&
441 1.2.10.2 tls (rmode & mode) != mode)
442 1.2.10.2 tls error = EACCES;
443 1.2.10.2 tls }
444 1.2.10.2 tls return (error);
445 1.2.10.2 tls } else {
446 1.2.10.2 tls if ((error = nfsspec_access(ap)) != 0) {
447 1.2.10.2 tls return (error);
448 1.2.10.2 tls }
449 1.2.10.2 tls /*
450 1.2.10.2 tls * Attempt to prevent a mapped root from accessing a file
451 1.2.10.2 tls * which it shouldn't. We try to read a byte from the file
452 1.2.10.2 tls * if the user is root and the file is not zero length.
453 1.2.10.2 tls * After calling nfsspec_access, we should have the correct
454 1.2.10.2 tls * file size cached.
455 1.2.10.2 tls */
456 1.2.10.2 tls mtx_lock(&np->n_mtx);
457 1.2.10.2 tls if (ap->a_cred->cr_uid == 0 && (ap->a_accmode & VREAD)
458 1.2.10.2 tls && VTONFS(vp)->n_size > 0) {
459 1.2.10.2 tls struct iovec aiov;
460 1.2.10.2 tls struct uio auio;
461 1.2.10.2 tls char buf[1];
462 1.2.10.2 tls
463 1.2.10.2 tls mtx_unlock(&np->n_mtx);
464 1.2.10.2 tls aiov.iov_base = buf;
465 1.2.10.2 tls aiov.iov_len = 1;
466 1.2.10.2 tls auio.uio_iov = &aiov;
467 1.2.10.2 tls auio.uio_iovcnt = 1;
468 1.2.10.2 tls auio.uio_offset = 0;
469 1.2.10.2 tls auio.uio_resid = 1;
470 1.2.10.2 tls auio.uio_segflg = UIO_SYSSPACE;
471 1.2.10.2 tls auio.uio_rw = UIO_READ;
472 1.2.10.2 tls auio.uio_td = ap->a_td;
473 1.2.10.2 tls
474 1.2.10.2 tls if (vp->v_type == VREG)
475 1.2.10.2 tls error = ncl_readrpc(vp, &auio, ap->a_cred);
476 1.2.10.2 tls else if (vp->v_type == VDIR) {
477 1.2.10.2 tls char* bp;
478 1.2.10.2 tls bp = malloc(NFS_DIRBLKSIZ, M_TEMP, M_WAITOK);
479 1.2.10.2 tls aiov.iov_base = bp;
480 1.2.10.2 tls aiov.iov_len = auio.uio_resid = NFS_DIRBLKSIZ;
481 1.2.10.2 tls error = ncl_readdirrpc(vp, &auio, ap->a_cred,
482 1.2.10.2 tls ap->a_td);
483 1.2.10.2 tls free(bp, M_TEMP);
484 1.2.10.2 tls } else if (vp->v_type == VLNK)
485 1.2.10.2 tls error = ncl_readlinkrpc(vp, &auio, ap->a_cred);
486 1.2.10.2 tls else
487 1.2.10.2 tls error = EACCES;
488 1.2.10.2 tls } else
489 1.2.10.2 tls mtx_unlock(&np->n_mtx);
490 1.2.10.2 tls return (error);
491 1.2.10.2 tls }
492 1.2.10.2 tls }
493 1.2.10.2 tls
494 1.2.10.2 tls
495 1.2.10.2 tls /*
496 1.2.10.2 tls * nfs open vnode op
497 1.2.10.2 tls * Check to see if the type is ok
498 1.2.10.2 tls * and that deletion is not in progress.
499 1.2.10.2 tls * For paged in text files, you will need to flush the page cache
500 1.2.10.2 tls * if consistency is lost.
501 1.2.10.2 tls */
502 1.2.10.2 tls /* ARGSUSED */
503 1.2.10.2 tls static int
504 1.2.10.2 tls nfs_open(struct vop_open_args *ap)
505 1.2.10.2 tls {
506 1.2.10.2 tls struct vnode *vp = ap->a_vp;
507 1.2.10.2 tls struct nfsnode *np = VTONFS(vp);
508 1.2.10.2 tls struct vattr vattr;
509 1.2.10.2 tls int error;
510 1.2.10.2 tls int fmode = ap->a_mode;
511 1.2.10.2 tls struct ucred *cred;
512 1.2.10.2 tls
513 1.2.10.2 tls if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK)
514 1.2.10.2 tls return (EOPNOTSUPP);
515 1.2.10.2 tls
516 1.2.10.2 tls /*
517 1.2.10.2 tls * For NFSv4, we need to do the Open Op before cache validation,
518 1.2.10.2 tls * so that we conform to RFC3530 Sec. 9.3.1.
519 1.2.10.2 tls */
520 1.2.10.2 tls if (NFS_ISV4(vp)) {
521 1.2.10.2 tls error = nfsrpc_open(vp, fmode, ap->a_cred, ap->a_td);
522 1.2.10.2 tls if (error) {
523 1.2.10.2 tls error = nfscl_maperr(ap->a_td, error, (uid_t)0,
524 1.2.10.2 tls (gid_t)0);
525 1.2.10.2 tls return (error);
526 1.2.10.2 tls }
527 1.2.10.2 tls }
528 1.2.10.2 tls
529 1.2.10.2 tls /*
530 1.2.10.2 tls * Now, if this Open will be doing reading, re-validate/flush the
531 1.2.10.2 tls * cache, so that Close/Open coherency is maintained.
532 1.2.10.2 tls */
533 1.2.10.2 tls mtx_lock(&np->n_mtx);
534 1.2.10.2 tls if (np->n_flag & NMODIFIED) {
535 1.2.10.2 tls mtx_unlock(&np->n_mtx);
536 1.2.10.2 tls error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
537 1.2.10.2 tls if (error == EINTR || error == EIO) {
538 1.2.10.2 tls if (NFS_ISV4(vp))
539 1.2.10.2 tls (void) nfsrpc_close(vp, 0, ap->a_td);
540 1.2.10.2 tls return (error);
541 1.2.10.2 tls }
542 1.2.10.2 tls mtx_lock(&np->n_mtx);
543 1.2.10.2 tls np->n_attrstamp = 0;
544 1.2.10.2 tls KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
545 1.2.10.2 tls if (vp->v_type == VDIR)
546 1.2.10.2 tls np->n_direofoffset = 0;
547 1.2.10.2 tls mtx_unlock(&np->n_mtx);
548 1.2.10.2 tls error = VOP_GETATTR(vp, &vattr, ap->a_cred);
549 1.2.10.2 tls if (error) {
550 1.2.10.2 tls if (NFS_ISV4(vp))
551 1.2.10.2 tls (void) nfsrpc_close(vp, 0, ap->a_td);
552 1.2.10.2 tls return (error);
553 1.2.10.2 tls }
554 1.2.10.2 tls mtx_lock(&np->n_mtx);
555 1.2.10.2 tls np->n_mtime = vattr.va_mtime;
556 1.2.10.2 tls if (NFS_ISV4(vp))
557 1.2.10.2 tls np->n_change = vattr.va_filerev;
558 1.2.10.2 tls } else {
559 1.2.10.2 tls mtx_unlock(&np->n_mtx);
560 1.2.10.2 tls error = VOP_GETATTR(vp, &vattr, ap->a_cred);
561 1.2.10.2 tls if (error) {
562 1.2.10.2 tls if (NFS_ISV4(vp))
563 1.2.10.2 tls (void) nfsrpc_close(vp, 0, ap->a_td);
564 1.2.10.2 tls return (error);
565 1.2.10.2 tls }
566 1.2.10.2 tls mtx_lock(&np->n_mtx);
567 1.2.10.2 tls if ((NFS_ISV4(vp) && np->n_change != vattr.va_filerev) ||
568 1.2.10.2 tls NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime)) {
569 1.2.10.2 tls if (vp->v_type == VDIR)
570 1.2.10.2 tls np->n_direofoffset = 0;
571 1.2.10.2 tls mtx_unlock(&np->n_mtx);
572 1.2.10.2 tls error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
573 1.2.10.2 tls if (error == EINTR || error == EIO) {
574 1.2.10.2 tls if (NFS_ISV4(vp))
575 1.2.10.2 tls (void) nfsrpc_close(vp, 0, ap->a_td);
576 1.2.10.2 tls return (error);
577 1.2.10.2 tls }
578 1.2.10.2 tls mtx_lock(&np->n_mtx);
579 1.2.10.2 tls np->n_mtime = vattr.va_mtime;
580 1.2.10.2 tls if (NFS_ISV4(vp))
581 1.2.10.2 tls np->n_change = vattr.va_filerev;
582 1.2.10.2 tls }
583 1.2.10.2 tls }
584 1.2.10.2 tls
585 1.2.10.2 tls /*
586 1.2.10.2 tls * If the object has >= 1 O_DIRECT active opens, we disable caching.
587 1.2.10.2 tls */
588 1.2.10.2 tls if (newnfs_directio_enable && (fmode & O_DIRECT) &&
589 1.2.10.2 tls (vp->v_type == VREG)) {
590 1.2.10.2 tls if (np->n_directio_opens == 0) {
591 1.2.10.2 tls mtx_unlock(&np->n_mtx);
592 1.2.10.2 tls error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
593 1.2.10.2 tls if (error) {
594 1.2.10.2 tls if (NFS_ISV4(vp))
595 1.2.10.2 tls (void) nfsrpc_close(vp, 0, ap->a_td);
596 1.2.10.2 tls return (error);
597 1.2.10.2 tls }
598 1.2.10.2 tls mtx_lock(&np->n_mtx);
599 1.2.10.2 tls np->n_flag |= NNONCACHE;
600 1.2.10.2 tls }
601 1.2.10.2 tls np->n_directio_opens++;
602 1.2.10.2 tls }
603 1.2.10.2 tls
604 1.2.10.2 tls /* If opened for writing via NFSv4.1 or later, mark that for pNFS. */
605 1.2.10.2 tls if (NFSHASPNFS(VFSTONFS(vp->v_mount)) && (fmode & FWRITE) != 0)
606 1.2.10.2 tls np->n_flag |= NWRITEOPENED;
607 1.2.10.2 tls
608 1.2.10.2 tls /*
609 1.2.10.2 tls * If this is an open for writing, capture a reference to the
610 1.2.10.2 tls * credentials, so they can be used by ncl_putpages(). Using
611 1.2.10.2 tls * these write credentials is preferable to the credentials of
612 1.2.10.2 tls * whatever thread happens to be doing the VOP_PUTPAGES() since
613 1.2.10.2 tls * the write RPCs are less likely to fail with EACCES.
614 1.2.10.2 tls */
615 1.2.10.2 tls if ((fmode & FWRITE) != 0) {
616 1.2.10.2 tls cred = np->n_writecred;
617 1.2.10.2 tls np->n_writecred = crhold(ap->a_cred);
618 1.2.10.2 tls } else
619 1.2.10.2 tls cred = NULL;
620 1.2.10.2 tls mtx_unlock(&np->n_mtx);
621 1.2.10.2 tls
622 1.2.10.2 tls if (cred != NULL)
623 1.2.10.2 tls crfree(cred);
624 1.2.10.2 tls vnode_create_vobject(vp, vattr.va_size, ap->a_td);
625 1.2.10.2 tls return (0);
626 1.2.10.2 tls }
627 1.2.10.2 tls
628 1.2.10.2 tls /*
629 1.2.10.2 tls * nfs close vnode op
630 1.2.10.2 tls * What an NFS client should do upon close after writing is a debatable issue.
631 1.2.10.2 tls * Most NFS clients push delayed writes to the server upon close, basically for
632 1.2.10.2 tls * two reasons:
633 1.2.10.2 tls * 1 - So that any write errors may be reported back to the client process
634 1.2.10.2 tls * doing the close system call. By far the two most likely errors are
635 1.2.10.2 tls * NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
636 1.2.10.2 tls * 2 - To put a worst case upper bound on cache inconsistency between
637 1.2.10.2 tls * multiple clients for the file.
638 1.2.10.2 tls * There is also a consistency problem for Version 2 of the protocol w.r.t.
639 1.2.10.2 tls * not being able to tell if other clients are writing a file concurrently,
640 1.2.10.2 tls * since there is no way of knowing if the changed modify time in the reply
641 1.2.10.2 tls * is only due to the write for this client.
642 1.2.10.2 tls * (NFS Version 3 provides weak cache consistency data in the reply that
643 1.2.10.2 tls * should be sufficient to detect and handle this case.)
644 1.2.10.2 tls *
645 1.2.10.2 tls * The current code does the following:
646 1.2.10.2 tls * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
647 1.2.10.2 tls * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
648 1.2.10.2 tls * or commit them (this satisfies 1 and 2 except for the
649 1.2.10.2 tls * case where the server crashes after this close but
650 1.2.10.2 tls * before the commit RPC, which is felt to be "good
651 1.2.10.2 tls * enough". Changing the last argument to ncl_flush() to
652 1.2.10.2 tls * a 1 would force a commit operation, if it is felt a
653 1.2.10.2 tls * commit is necessary now.
654 1.2.10.2 tls * for NFS Version 4 - flush the dirty buffers and commit them, if
655 1.2.10.2 tls * nfscl_mustflush() says this is necessary.
656 1.2.10.2 tls * It is necessary if there is no write delegation held,
657 1.2.10.2 tls * in order to satisfy open/close coherency.
658 1.2.10.2 tls * If the file isn't cached on local stable storage,
659 1.2.10.2 tls * it may be necessary in order to detect "out of space"
660 1.2.10.2 tls * errors from the server, if the write delegation
661 1.2.10.2 tls * issued by the server doesn't allow the file to grow.
662 1.2.10.2 tls */
663 1.2.10.2 tls /* ARGSUSED */
664 1.2.10.2 tls static int
665 1.2.10.2 tls nfs_close(struct vop_close_args *ap)
666 1.2.10.2 tls {
667 1.2.10.2 tls struct vnode *vp = ap->a_vp;
668 1.2.10.2 tls struct nfsnode *np = VTONFS(vp);
669 1.2.10.2 tls struct nfsvattr nfsva;
670 1.2.10.2 tls struct ucred *cred;
671 1.2.10.2 tls int error = 0, ret, localcred = 0;
672 1.2.10.2 tls int fmode = ap->a_fflag;
673 1.2.10.2 tls
674 1.2.10.2 tls if ((vp->v_mount->mnt_kern_flag & MNTK_UNMOUNTF))
675 1.2.10.2 tls return (0);
676 1.2.10.2 tls /*
677 1.2.10.2 tls * During shutdown, a_cred isn't valid, so just use root.
678 1.2.10.2 tls */
679 1.2.10.2 tls if (ap->a_cred == NOCRED) {
680 1.2.10.2 tls cred = newnfs_getcred();
681 1.2.10.2 tls localcred = 1;
682 1.2.10.2 tls } else {
683 1.2.10.2 tls cred = ap->a_cred;
684 1.2.10.2 tls }
685 1.2.10.2 tls if (vp->v_type == VREG) {
686 1.2.10.2 tls /*
687 1.2.10.2 tls * Examine and clean dirty pages, regardless of NMODIFIED.
688 1.2.10.2 tls * This closes a major hole in close-to-open consistency.
689 1.2.10.2 tls * We want to push out all dirty pages (and buffers) on
690 1.2.10.2 tls * close, regardless of whether they were dirtied by
691 1.2.10.2 tls * mmap'ed writes or via write().
692 1.2.10.2 tls */
693 1.2.10.2 tls if (nfs_clean_pages_on_close && vp->v_object) {
694 1.2.10.2 tls VM_OBJECT_WLOCK(vp->v_object);
695 1.2.10.2 tls vm_object_page_clean(vp->v_object, 0, 0, 0);
696 1.2.10.2 tls VM_OBJECT_WUNLOCK(vp->v_object);
697 1.2.10.2 tls }
698 1.2.10.2 tls mtx_lock(&np->n_mtx);
699 1.2.10.2 tls if (np->n_flag & NMODIFIED) {
700 1.2.10.2 tls mtx_unlock(&np->n_mtx);
701 1.2.10.2 tls if (NFS_ISV3(vp)) {
702 1.2.10.2 tls /*
703 1.2.10.2 tls * Under NFSv3 we have dirty buffers to dispose of. We
704 1.2.10.2 tls * must flush them to the NFS server. We have the option
705 1.2.10.2 tls * of waiting all the way through the commit rpc or just
706 1.2.10.2 tls * waiting for the initial write. The default is to only
707 1.2.10.2 tls * wait through the initial write so the data is in the
708 1.2.10.2 tls * server's cache, which is roughly similar to the state
709 1.2.10.2 tls * a standard disk subsystem leaves the file in on close().
710 1.2.10.2 tls *
711 1.2.10.2 tls * We cannot clear the NMODIFIED bit in np->n_flag due to
712 1.2.10.2 tls * potential races with other processes, and certainly
713 1.2.10.2 tls * cannot clear it if we don't commit.
714 1.2.10.2 tls * These races occur when there is no longer the old
715 1.2.10.2 tls * traditional vnode locking implemented for Vnode Ops.
716 1.2.10.2 tls */
717 1.2.10.2 tls int cm = newnfs_commit_on_close ? 1 : 0;
718 1.2.10.2 tls error = ncl_flush(vp, MNT_WAIT, cred, ap->a_td, cm, 0);
719 1.2.10.2 tls /* np->n_flag &= ~NMODIFIED; */
720 1.2.10.2 tls } else if (NFS_ISV4(vp)) {
721 1.2.10.2 tls if (nfscl_mustflush(vp) != 0) {
722 1.2.10.2 tls int cm = newnfs_commit_on_close ? 1 : 0;
723 1.2.10.2 tls error = ncl_flush(vp, MNT_WAIT, cred, ap->a_td,
724 1.2.10.2 tls cm, 0);
725 1.2.10.2 tls /*
726 1.2.10.2 tls * as above w.r.t races when clearing
727 1.2.10.2 tls * NMODIFIED.
728 1.2.10.2 tls * np->n_flag &= ~NMODIFIED;
729 1.2.10.2 tls */
730 1.2.10.2 tls }
731 1.2.10.2 tls } else
732 1.2.10.2 tls error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
733 1.2.10.2 tls mtx_lock(&np->n_mtx);
734 1.2.10.2 tls }
735 1.2.10.2 tls /*
736 1.2.10.2 tls * Invalidate the attribute cache in all cases.
737 1.2.10.2 tls * An open is going to fetch fresh attrs any way, other procs
738 1.2.10.2 tls * on this node that have file open will be forced to do an
739 1.2.10.2 tls * otw attr fetch, but this is safe.
740 1.2.10.2 tls * --> A user found that their RPC count dropped by 20% when
741 1.2.10.2 tls * this was commented out and I can't see any requirement
742 1.2.10.2 tls * for it, so I've disabled it when negative lookups are
743 1.2.10.2 tls * enabled. (What does this have to do with negative lookup
744 1.2.10.2 tls * caching? Well nothing, except it was reported by the
745 1.2.10.2 tls * same user that needed negative lookup caching and I wanted
746 1.2.10.2 tls * there to be a way to disable it to see if it
747 1.2.10.2 tls * is the cause of some caching/coherency issue that might
748 1.2.10.2 tls * crop up.)
749 1.2.10.2 tls */
750 1.2.10.2 tls if (VFSTONFS(vp->v_mount)->nm_negnametimeo == 0) {
751 1.2.10.2 tls np->n_attrstamp = 0;
752 1.2.10.2 tls KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
753 1.2.10.2 tls }
754 1.2.10.2 tls if (np->n_flag & NWRITEERR) {
755 1.2.10.2 tls np->n_flag &= ~NWRITEERR;
756 1.2.10.2 tls error = np->n_error;
757 1.2.10.2 tls }
758 1.2.10.2 tls mtx_unlock(&np->n_mtx);
759 1.2.10.2 tls }
760 1.2.10.2 tls
761 1.2.10.2 tls if (NFS_ISV4(vp)) {
762 1.2.10.2 tls /*
763 1.2.10.2 tls * Get attributes so "change" is up to date.
764 1.2.10.2 tls */
765 1.2.10.3 jdolecek if (error == 0 && nfscl_mustflush(vp) != 0 &&
766 1.2.10.3 jdolecek vp->v_type == VREG &&
767 1.2.10.3 jdolecek (VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOCTO) == 0) {
768 1.2.10.2 tls ret = nfsrpc_getattr(vp, cred, ap->a_td, &nfsva,
769 1.2.10.2 tls NULL);
770 1.2.10.2 tls if (!ret) {
771 1.2.10.2 tls np->n_change = nfsva.na_filerev;
772 1.2.10.2 tls (void) nfscl_loadattrcache(&vp, &nfsva, NULL,
773 1.2.10.2 tls NULL, 0, 0);
774 1.2.10.2 tls }
775 1.2.10.2 tls }
776 1.2.10.2 tls
777 1.2.10.2 tls /*
778 1.2.10.2 tls * and do the close.
779 1.2.10.2 tls */
780 1.2.10.2 tls ret = nfsrpc_close(vp, 0, ap->a_td);
781 1.2.10.2 tls if (!error && ret)
782 1.2.10.2 tls error = ret;
783 1.2.10.2 tls if (error)
784 1.2.10.2 tls error = nfscl_maperr(ap->a_td, error, (uid_t)0,
785 1.2.10.2 tls (gid_t)0);
786 1.2.10.2 tls }
787 1.2.10.2 tls if (newnfs_directio_enable)
788 1.2.10.2 tls KASSERT((np->n_directio_asyncwr == 0),
789 1.2.10.2 tls ("nfs_close: dirty unflushed (%d) directio buffers\n",
790 1.2.10.2 tls np->n_directio_asyncwr));
791 1.2.10.2 tls if (newnfs_directio_enable && (fmode & O_DIRECT) && (vp->v_type == VREG)) {
792 1.2.10.2 tls mtx_lock(&np->n_mtx);
793 1.2.10.2 tls KASSERT((np->n_directio_opens > 0),
794 1.2.10.2 tls ("nfs_close: unexpectedly value (0) of n_directio_opens\n"));
795 1.2.10.2 tls np->n_directio_opens--;
796 1.2.10.2 tls if (np->n_directio_opens == 0)
797 1.2.10.2 tls np->n_flag &= ~NNONCACHE;
798 1.2.10.2 tls mtx_unlock(&np->n_mtx);
799 1.2.10.2 tls }
800 1.2.10.2 tls if (localcred)
801 1.2.10.2 tls NFSFREECRED(cred);
802 1.2.10.2 tls return (error);
803 1.2.10.2 tls }
804 1.2.10.2 tls
805 1.2.10.2 tls /*
806 1.2.10.2 tls * nfs getattr call from vfs.
807 1.2.10.2 tls */
808 1.2.10.2 tls static int
809 1.2.10.2 tls nfs_getattr(struct vop_getattr_args *ap)
810 1.2.10.2 tls {
811 1.2.10.2 tls struct vnode *vp = ap->a_vp;
812 1.2.10.2 tls struct thread *td = curthread; /* XXX */
813 1.2.10.2 tls struct nfsnode *np = VTONFS(vp);
814 1.2.10.2 tls int error = 0;
815 1.2.10.2 tls struct nfsvattr nfsva;
816 1.2.10.2 tls struct vattr *vap = ap->a_vap;
817 1.2.10.2 tls struct vattr vattr;
818 1.2.10.2 tls
819 1.2.10.2 tls /*
820 1.2.10.2 tls * Update local times for special files.
821 1.2.10.2 tls */
822 1.2.10.2 tls mtx_lock(&np->n_mtx);
823 1.2.10.2 tls if (np->n_flag & (NACC | NUPD))
824 1.2.10.2 tls np->n_flag |= NCHG;
825 1.2.10.2 tls mtx_unlock(&np->n_mtx);
826 1.2.10.2 tls /*
827 1.2.10.2 tls * First look in the cache.
828 1.2.10.2 tls */
829 1.2.10.2 tls if (ncl_getattrcache(vp, &vattr) == 0) {
830 1.2.10.2 tls vap->va_type = vattr.va_type;
831 1.2.10.2 tls vap->va_mode = vattr.va_mode;
832 1.2.10.2 tls vap->va_nlink = vattr.va_nlink;
833 1.2.10.2 tls vap->va_uid = vattr.va_uid;
834 1.2.10.2 tls vap->va_gid = vattr.va_gid;
835 1.2.10.2 tls vap->va_fsid = vattr.va_fsid;
836 1.2.10.2 tls vap->va_fileid = vattr.va_fileid;
837 1.2.10.2 tls vap->va_size = vattr.va_size;
838 1.2.10.2 tls vap->va_blocksize = vattr.va_blocksize;
839 1.2.10.2 tls vap->va_atime = vattr.va_atime;
840 1.2.10.2 tls vap->va_mtime = vattr.va_mtime;
841 1.2.10.2 tls vap->va_ctime = vattr.va_ctime;
842 1.2.10.2 tls vap->va_gen = vattr.va_gen;
843 1.2.10.2 tls vap->va_flags = vattr.va_flags;
844 1.2.10.2 tls vap->va_rdev = vattr.va_rdev;
845 1.2.10.2 tls vap->va_bytes = vattr.va_bytes;
846 1.2.10.2 tls vap->va_filerev = vattr.va_filerev;
847 1.2.10.2 tls /*
848 1.2.10.2 tls * Get the local modify time for the case of a write
849 1.2.10.2 tls * delegation.
850 1.2.10.2 tls */
851 1.2.10.2 tls nfscl_deleggetmodtime(vp, &vap->va_mtime);
852 1.2.10.2 tls return (0);
853 1.2.10.2 tls }
854 1.2.10.2 tls
855 1.2.10.2 tls if (NFS_ISV34(vp) && nfs_prime_access_cache &&
856 1.2.10.2 tls nfsaccess_cache_timeout > 0) {
857 1.2.10.3 jdolecek NFSINCRGLOBAL(nfsstatsv1.accesscache_misses);
858 1.2.10.2 tls nfs34_access_otw(vp, NFSACCESS_ALL, td, ap->a_cred, NULL);
859 1.2.10.2 tls if (ncl_getattrcache(vp, ap->a_vap) == 0) {
860 1.2.10.2 tls nfscl_deleggetmodtime(vp, &ap->a_vap->va_mtime);
861 1.2.10.2 tls return (0);
862 1.2.10.2 tls }
863 1.2.10.2 tls }
864 1.2.10.2 tls error = nfsrpc_getattr(vp, ap->a_cred, td, &nfsva, NULL);
865 1.2.10.2 tls if (!error)
866 1.2.10.2 tls error = nfscl_loadattrcache(&vp, &nfsva, vap, NULL, 0, 0);
867 1.2.10.2 tls if (!error) {
868 1.2.10.2 tls /*
869 1.2.10.2 tls * Get the local modify time for the case of a write
870 1.2.10.2 tls * delegation.
871 1.2.10.2 tls */
872 1.2.10.2 tls nfscl_deleggetmodtime(vp, &vap->va_mtime);
873 1.2.10.2 tls } else if (NFS_ISV4(vp)) {
874 1.2.10.2 tls error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
875 1.2.10.2 tls }
876 1.2.10.2 tls return (error);
877 1.2.10.2 tls }
878 1.2.10.2 tls
879 1.2.10.2 tls /*
880 1.2.10.2 tls * nfs setattr call.
881 1.2.10.2 tls */
882 1.2.10.2 tls static int
883 1.2.10.2 tls nfs_setattr(struct vop_setattr_args *ap)
884 1.2.10.2 tls {
885 1.2.10.2 tls struct vnode *vp = ap->a_vp;
886 1.2.10.2 tls struct nfsnode *np = VTONFS(vp);
887 1.2.10.2 tls struct thread *td = curthread; /* XXX */
888 1.2.10.2 tls struct vattr *vap = ap->a_vap;
889 1.2.10.2 tls int error = 0;
890 1.2.10.2 tls u_quad_t tsize;
891 1.2.10.2 tls
892 1.2.10.2 tls #ifndef nolint
893 1.2.10.2 tls tsize = (u_quad_t)0;
894 1.2.10.2 tls #endif
895 1.2.10.2 tls
896 1.2.10.2 tls /*
897 1.2.10.2 tls * Setting of flags and marking of atimes are not supported.
898 1.2.10.2 tls */
899 1.2.10.2 tls if (vap->va_flags != VNOVAL)
900 1.2.10.2 tls return (EOPNOTSUPP);
901 1.2.10.2 tls
902 1.2.10.2 tls /*
903 1.2.10.2 tls * Disallow write attempts if the filesystem is mounted read-only.
904 1.2.10.2 tls */
905 1.2.10.2 tls if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
906 1.2.10.2 tls vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
907 1.2.10.2 tls vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
908 1.2.10.2 tls (vp->v_mount->mnt_flag & MNT_RDONLY))
909 1.2.10.2 tls return (EROFS);
910 1.2.10.2 tls if (vap->va_size != VNOVAL) {
911 1.2.10.2 tls switch (vp->v_type) {
912 1.2.10.2 tls case VDIR:
913 1.2.10.2 tls return (EISDIR);
914 1.2.10.2 tls case VCHR:
915 1.2.10.2 tls case VBLK:
916 1.2.10.2 tls case VSOCK:
917 1.2.10.2 tls case VFIFO:
918 1.2.10.2 tls if (vap->va_mtime.tv_sec == VNOVAL &&
919 1.2.10.2 tls vap->va_atime.tv_sec == VNOVAL &&
920 1.2.10.2 tls vap->va_mode == (mode_t)VNOVAL &&
921 1.2.10.2 tls vap->va_uid == (uid_t)VNOVAL &&
922 1.2.10.2 tls vap->va_gid == (gid_t)VNOVAL)
923 1.2.10.2 tls return (0);
924 1.2.10.2 tls vap->va_size = VNOVAL;
925 1.2.10.2 tls break;
926 1.2.10.2 tls default:
927 1.2.10.2 tls /*
928 1.2.10.2 tls * Disallow write attempts if the filesystem is
929 1.2.10.2 tls * mounted read-only.
930 1.2.10.2 tls */
931 1.2.10.2 tls if (vp->v_mount->mnt_flag & MNT_RDONLY)
932 1.2.10.2 tls return (EROFS);
933 1.2.10.2 tls /*
934 1.2.10.2 tls * We run vnode_pager_setsize() early (why?),
935 1.2.10.2 tls * we must set np->n_size now to avoid vinvalbuf
936 1.2.10.2 tls * V_SAVE races that might setsize a lower
937 1.2.10.2 tls * value.
938 1.2.10.2 tls */
939 1.2.10.2 tls mtx_lock(&np->n_mtx);
940 1.2.10.2 tls tsize = np->n_size;
941 1.2.10.2 tls mtx_unlock(&np->n_mtx);
942 1.2.10.2 tls error = ncl_meta_setsize(vp, ap->a_cred, td,
943 1.2.10.2 tls vap->va_size);
944 1.2.10.2 tls mtx_lock(&np->n_mtx);
945 1.2.10.2 tls if (np->n_flag & NMODIFIED) {
946 1.2.10.2 tls tsize = np->n_size;
947 1.2.10.2 tls mtx_unlock(&np->n_mtx);
948 1.2.10.2 tls if (vap->va_size == 0)
949 1.2.10.2 tls error = ncl_vinvalbuf(vp, 0, td, 1);
950 1.2.10.2 tls else
951 1.2.10.2 tls error = ncl_vinvalbuf(vp, V_SAVE, td, 1);
952 1.2.10.2 tls if (error) {
953 1.2.10.2 tls vnode_pager_setsize(vp, tsize);
954 1.2.10.2 tls return (error);
955 1.2.10.2 tls }
956 1.2.10.2 tls /*
957 1.2.10.2 tls * Call nfscl_delegmodtime() to set the modify time
958 1.2.10.2 tls * locally, as required.
959 1.2.10.2 tls */
960 1.2.10.2 tls nfscl_delegmodtime(vp);
961 1.2.10.2 tls } else
962 1.2.10.2 tls mtx_unlock(&np->n_mtx);
963 1.2.10.2 tls /*
964 1.2.10.2 tls * np->n_size has already been set to vap->va_size
965 1.2.10.2 tls * in ncl_meta_setsize(). We must set it again since
966 1.2.10.2 tls * nfs_loadattrcache() could be called through
967 1.2.10.2 tls * ncl_meta_setsize() and could modify np->n_size.
968 1.2.10.2 tls */
969 1.2.10.2 tls mtx_lock(&np->n_mtx);
970 1.2.10.2 tls np->n_vattr.na_size = np->n_size = vap->va_size;
971 1.2.10.2 tls mtx_unlock(&np->n_mtx);
972 1.2.10.3 jdolecek }
973 1.2.10.2 tls } else {
974 1.2.10.2 tls mtx_lock(&np->n_mtx);
975 1.2.10.2 tls if ((vap->va_mtime.tv_sec != VNOVAL || vap->va_atime.tv_sec != VNOVAL) &&
976 1.2.10.2 tls (np->n_flag & NMODIFIED) && vp->v_type == VREG) {
977 1.2.10.2 tls mtx_unlock(&np->n_mtx);
978 1.2.10.2 tls if ((error = ncl_vinvalbuf(vp, V_SAVE, td, 1)) != 0 &&
979 1.2.10.2 tls (error == EINTR || error == EIO))
980 1.2.10.2 tls return (error);
981 1.2.10.2 tls } else
982 1.2.10.2 tls mtx_unlock(&np->n_mtx);
983 1.2.10.2 tls }
984 1.2.10.2 tls error = nfs_setattrrpc(vp, vap, ap->a_cred, td);
985 1.2.10.2 tls if (error && vap->va_size != VNOVAL) {
986 1.2.10.2 tls mtx_lock(&np->n_mtx);
987 1.2.10.2 tls np->n_size = np->n_vattr.na_size = tsize;
988 1.2.10.2 tls vnode_pager_setsize(vp, tsize);
989 1.2.10.2 tls mtx_unlock(&np->n_mtx);
990 1.2.10.2 tls }
991 1.2.10.2 tls return (error);
992 1.2.10.2 tls }
993 1.2.10.2 tls
994 1.2.10.2 tls /*
995 1.2.10.2 tls * Do an nfs setattr rpc.
996 1.2.10.2 tls */
997 1.2.10.2 tls static int
998 1.2.10.2 tls nfs_setattrrpc(struct vnode *vp, struct vattr *vap, struct ucred *cred,
999 1.2.10.2 tls struct thread *td)
1000 1.2.10.2 tls {
1001 1.2.10.2 tls struct nfsnode *np = VTONFS(vp);
1002 1.2.10.2 tls int error, ret, attrflag, i;
1003 1.2.10.2 tls struct nfsvattr nfsva;
1004 1.2.10.2 tls
1005 1.2.10.2 tls if (NFS_ISV34(vp)) {
1006 1.2.10.2 tls mtx_lock(&np->n_mtx);
1007 1.2.10.2 tls for (i = 0; i < NFS_ACCESSCACHESIZE; i++)
1008 1.2.10.2 tls np->n_accesscache[i].stamp = 0;
1009 1.2.10.2 tls np->n_flag |= NDELEGMOD;
1010 1.2.10.2 tls mtx_unlock(&np->n_mtx);
1011 1.2.10.2 tls KDTRACE_NFS_ACCESSCACHE_FLUSH_DONE(vp);
1012 1.2.10.2 tls }
1013 1.2.10.2 tls error = nfsrpc_setattr(vp, vap, NULL, cred, td, &nfsva, &attrflag,
1014 1.2.10.2 tls NULL);
1015 1.2.10.2 tls if (attrflag) {
1016 1.2.10.2 tls ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
1017 1.2.10.2 tls if (ret && !error)
1018 1.2.10.2 tls error = ret;
1019 1.2.10.2 tls }
1020 1.2.10.2 tls if (error && NFS_ISV4(vp))
1021 1.2.10.2 tls error = nfscl_maperr(td, error, vap->va_uid, vap->va_gid);
1022 1.2.10.2 tls return (error);
1023 1.2.10.2 tls }
1024 1.2.10.2 tls
1025 1.2.10.2 tls /*
1026 1.2.10.2 tls * nfs lookup call, one step at a time...
1027 1.2.10.2 tls * First look in cache
1028 1.2.10.2 tls * If not found, unlock the directory nfsnode and do the rpc
1029 1.2.10.2 tls */
1030 1.2.10.2 tls static int
1031 1.2.10.2 tls nfs_lookup(struct vop_lookup_args *ap)
1032 1.2.10.2 tls {
1033 1.2.10.2 tls struct componentname *cnp = ap->a_cnp;
1034 1.2.10.2 tls struct vnode *dvp = ap->a_dvp;
1035 1.2.10.2 tls struct vnode **vpp = ap->a_vpp;
1036 1.2.10.2 tls struct mount *mp = dvp->v_mount;
1037 1.2.10.2 tls int flags = cnp->cn_flags;
1038 1.2.10.2 tls struct vnode *newvp;
1039 1.2.10.2 tls struct nfsmount *nmp;
1040 1.2.10.2 tls struct nfsnode *np, *newnp;
1041 1.2.10.2 tls int error = 0, attrflag, dattrflag, ltype, ncticks;
1042 1.2.10.2 tls struct thread *td = cnp->cn_thread;
1043 1.2.10.2 tls struct nfsfh *nfhp;
1044 1.2.10.2 tls struct nfsvattr dnfsva, nfsva;
1045 1.2.10.2 tls struct vattr vattr;
1046 1.2.10.2 tls struct timespec nctime;
1047 1.2.10.2 tls
1048 1.2.10.2 tls *vpp = NULLVP;
1049 1.2.10.2 tls if ((flags & ISLASTCN) && (mp->mnt_flag & MNT_RDONLY) &&
1050 1.2.10.2 tls (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
1051 1.2.10.2 tls return (EROFS);
1052 1.2.10.2 tls if (dvp->v_type != VDIR)
1053 1.2.10.2 tls return (ENOTDIR);
1054 1.2.10.2 tls nmp = VFSTONFS(mp);
1055 1.2.10.2 tls np = VTONFS(dvp);
1056 1.2.10.2 tls
1057 1.2.10.2 tls /* For NFSv4, wait until any remove is done. */
1058 1.2.10.2 tls mtx_lock(&np->n_mtx);
1059 1.2.10.2 tls while (NFSHASNFSV4(nmp) && (np->n_flag & NREMOVEINPROG)) {
1060 1.2.10.2 tls np->n_flag |= NREMOVEWANT;
1061 1.2.10.2 tls (void) msleep((caddr_t)np, &np->n_mtx, PZERO, "nfslkup", 0);
1062 1.2.10.2 tls }
1063 1.2.10.2 tls mtx_unlock(&np->n_mtx);
1064 1.2.10.2 tls
1065 1.2.10.2 tls if ((error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td)) != 0)
1066 1.2.10.2 tls return (error);
1067 1.2.10.2 tls error = cache_lookup(dvp, vpp, cnp, &nctime, &ncticks);
1068 1.2.10.2 tls if (error > 0 && error != ENOENT)
1069 1.2.10.2 tls return (error);
1070 1.2.10.2 tls if (error == -1) {
1071 1.2.10.2 tls /*
1072 1.2.10.2 tls * Lookups of "." are special and always return the
1073 1.2.10.2 tls * current directory. cache_lookup() already handles
1074 1.2.10.2 tls * associated locking bookkeeping, etc.
1075 1.2.10.2 tls */
1076 1.2.10.2 tls if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
1077 1.2.10.2 tls /* XXX: Is this really correct? */
1078 1.2.10.2 tls if (cnp->cn_nameiop != LOOKUP &&
1079 1.2.10.2 tls (flags & ISLASTCN))
1080 1.2.10.2 tls cnp->cn_flags |= SAVENAME;
1081 1.2.10.2 tls return (0);
1082 1.2.10.2 tls }
1083 1.2.10.2 tls
1084 1.2.10.2 tls /*
1085 1.2.10.2 tls * We only accept a positive hit in the cache if the
1086 1.2.10.2 tls * change time of the file matches our cached copy.
1087 1.2.10.2 tls * Otherwise, we discard the cache entry and fallback
1088 1.2.10.2 tls * to doing a lookup RPC. We also only trust cache
1089 1.2.10.2 tls * entries for less than nm_nametimeo seconds.
1090 1.2.10.2 tls *
1091 1.2.10.2 tls * To better handle stale file handles and attributes,
1092 1.2.10.2 tls * clear the attribute cache of this node if it is a
1093 1.2.10.2 tls * leaf component, part of an open() call, and not
1094 1.2.10.2 tls * locally modified before fetching the attributes.
1095 1.2.10.2 tls * This should allow stale file handles to be detected
1096 1.2.10.2 tls * here where we can fall back to a LOOKUP RPC to
1097 1.2.10.2 tls * recover rather than having nfs_open() detect the
1098 1.2.10.2 tls * stale file handle and failing open(2) with ESTALE.
1099 1.2.10.2 tls */
1100 1.2.10.2 tls newvp = *vpp;
1101 1.2.10.2 tls newnp = VTONFS(newvp);
1102 1.2.10.2 tls if (!(nmp->nm_flag & NFSMNT_NOCTO) &&
1103 1.2.10.2 tls (flags & (ISLASTCN | ISOPEN)) == (ISLASTCN | ISOPEN) &&
1104 1.2.10.2 tls !(newnp->n_flag & NMODIFIED)) {
1105 1.2.10.2 tls mtx_lock(&newnp->n_mtx);
1106 1.2.10.2 tls newnp->n_attrstamp = 0;
1107 1.2.10.2 tls KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(newvp);
1108 1.2.10.2 tls mtx_unlock(&newnp->n_mtx);
1109 1.2.10.2 tls }
1110 1.2.10.2 tls if (nfscl_nodeleg(newvp, 0) == 0 ||
1111 1.2.10.2 tls ((u_int)(ticks - ncticks) < (nmp->nm_nametimeo * hz) &&
1112 1.2.10.2 tls VOP_GETATTR(newvp, &vattr, cnp->cn_cred) == 0 &&
1113 1.2.10.2 tls timespeccmp(&vattr.va_ctime, &nctime, ==))) {
1114 1.2.10.3 jdolecek NFSINCRGLOBAL(nfsstatsv1.lookupcache_hits);
1115 1.2.10.2 tls if (cnp->cn_nameiop != LOOKUP &&
1116 1.2.10.2 tls (flags & ISLASTCN))
1117 1.2.10.2 tls cnp->cn_flags |= SAVENAME;
1118 1.2.10.2 tls return (0);
1119 1.2.10.2 tls }
1120 1.2.10.2 tls cache_purge(newvp);
1121 1.2.10.2 tls if (dvp != newvp)
1122 1.2.10.2 tls vput(newvp);
1123 1.2.10.2 tls else
1124 1.2.10.2 tls vrele(newvp);
1125 1.2.10.2 tls *vpp = NULLVP;
1126 1.2.10.2 tls } else if (error == ENOENT) {
1127 1.2.10.2 tls if (dvp->v_iflag & VI_DOOMED)
1128 1.2.10.2 tls return (ENOENT);
1129 1.2.10.2 tls /*
1130 1.2.10.2 tls * We only accept a negative hit in the cache if the
1131 1.2.10.2 tls * modification time of the parent directory matches
1132 1.2.10.2 tls * the cached copy in the name cache entry.
1133 1.2.10.2 tls * Otherwise, we discard all of the negative cache
1134 1.2.10.2 tls * entries for this directory. We also only trust
1135 1.2.10.2 tls * negative cache entries for up to nm_negnametimeo
1136 1.2.10.2 tls * seconds.
1137 1.2.10.2 tls */
1138 1.2.10.2 tls if ((u_int)(ticks - ncticks) < (nmp->nm_negnametimeo * hz) &&
1139 1.2.10.2 tls VOP_GETATTR(dvp, &vattr, cnp->cn_cred) == 0 &&
1140 1.2.10.2 tls timespeccmp(&vattr.va_mtime, &nctime, ==)) {
1141 1.2.10.3 jdolecek NFSINCRGLOBAL(nfsstatsv1.lookupcache_hits);
1142 1.2.10.2 tls return (ENOENT);
1143 1.2.10.2 tls }
1144 1.2.10.2 tls cache_purge_negative(dvp);
1145 1.2.10.2 tls }
1146 1.2.10.2 tls
1147 1.2.10.2 tls error = 0;
1148 1.2.10.2 tls newvp = NULLVP;
1149 1.2.10.3 jdolecek NFSINCRGLOBAL(nfsstatsv1.lookupcache_misses);
1150 1.2.10.2 tls error = nfsrpc_lookup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1151 1.2.10.2 tls cnp->cn_cred, td, &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag,
1152 1.2.10.2 tls NULL);
1153 1.2.10.2 tls if (dattrflag)
1154 1.2.10.2 tls (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1155 1.2.10.2 tls if (error) {
1156 1.2.10.2 tls if (newvp != NULLVP) {
1157 1.2.10.2 tls vput(newvp);
1158 1.2.10.2 tls *vpp = NULLVP;
1159 1.2.10.2 tls }
1160 1.2.10.2 tls
1161 1.2.10.2 tls if (error != ENOENT) {
1162 1.2.10.2 tls if (NFS_ISV4(dvp))
1163 1.2.10.2 tls error = nfscl_maperr(td, error, (uid_t)0,
1164 1.2.10.2 tls (gid_t)0);
1165 1.2.10.2 tls return (error);
1166 1.2.10.2 tls }
1167 1.2.10.2 tls
1168 1.2.10.2 tls /* The requested file was not found. */
1169 1.2.10.2 tls if ((cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME) &&
1170 1.2.10.2 tls (flags & ISLASTCN)) {
1171 1.2.10.2 tls /*
1172 1.2.10.2 tls * XXX: UFS does a full VOP_ACCESS(dvp,
1173 1.2.10.2 tls * VWRITE) here instead of just checking
1174 1.2.10.2 tls * MNT_RDONLY.
1175 1.2.10.2 tls */
1176 1.2.10.2 tls if (mp->mnt_flag & MNT_RDONLY)
1177 1.2.10.2 tls return (EROFS);
1178 1.2.10.2 tls cnp->cn_flags |= SAVENAME;
1179 1.2.10.2 tls return (EJUSTRETURN);
1180 1.2.10.2 tls }
1181 1.2.10.2 tls
1182 1.2.10.3 jdolecek if ((cnp->cn_flags & MAKEENTRY) != 0 && dattrflag) {
1183 1.2.10.2 tls /*
1184 1.2.10.2 tls * Cache the modification time of the parent
1185 1.2.10.2 tls * directory from the post-op attributes in
1186 1.2.10.2 tls * the name cache entry. The negative cache
1187 1.2.10.2 tls * entry will be ignored once the directory
1188 1.2.10.2 tls * has changed. Don't bother adding the entry
1189 1.2.10.2 tls * if the directory has already changed.
1190 1.2.10.2 tls */
1191 1.2.10.2 tls mtx_lock(&np->n_mtx);
1192 1.2.10.2 tls if (timespeccmp(&np->n_vattr.na_mtime,
1193 1.2.10.2 tls &dnfsva.na_mtime, ==)) {
1194 1.2.10.2 tls mtx_unlock(&np->n_mtx);
1195 1.2.10.2 tls cache_enter_time(dvp, NULL, cnp,
1196 1.2.10.2 tls &dnfsva.na_mtime, NULL);
1197 1.2.10.2 tls } else
1198 1.2.10.2 tls mtx_unlock(&np->n_mtx);
1199 1.2.10.2 tls }
1200 1.2.10.2 tls return (ENOENT);
1201 1.2.10.2 tls }
1202 1.2.10.2 tls
1203 1.2.10.2 tls /*
1204 1.2.10.2 tls * Handle RENAME case...
1205 1.2.10.2 tls */
1206 1.2.10.2 tls if (cnp->cn_nameiop == RENAME && (flags & ISLASTCN)) {
1207 1.2.10.2 tls if (NFS_CMPFH(np, nfhp->nfh_fh, nfhp->nfh_len)) {
1208 1.2.10.2 tls FREE((caddr_t)nfhp, M_NFSFH);
1209 1.2.10.2 tls return (EISDIR);
1210 1.2.10.2 tls }
1211 1.2.10.2 tls error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np, NULL,
1212 1.2.10.2 tls LK_EXCLUSIVE);
1213 1.2.10.2 tls if (error)
1214 1.2.10.2 tls return (error);
1215 1.2.10.2 tls newvp = NFSTOV(np);
1216 1.2.10.2 tls if (attrflag)
1217 1.2.10.2 tls (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1218 1.2.10.2 tls 0, 1);
1219 1.2.10.2 tls *vpp = newvp;
1220 1.2.10.2 tls cnp->cn_flags |= SAVENAME;
1221 1.2.10.2 tls return (0);
1222 1.2.10.2 tls }
1223 1.2.10.2 tls
1224 1.2.10.2 tls if (flags & ISDOTDOT) {
1225 1.2.10.2 tls ltype = NFSVOPISLOCKED(dvp);
1226 1.2.10.2 tls error = vfs_busy(mp, MBF_NOWAIT);
1227 1.2.10.2 tls if (error != 0) {
1228 1.2.10.2 tls vfs_ref(mp);
1229 1.2.10.2 tls NFSVOPUNLOCK(dvp, 0);
1230 1.2.10.2 tls error = vfs_busy(mp, 0);
1231 1.2.10.2 tls NFSVOPLOCK(dvp, ltype | LK_RETRY);
1232 1.2.10.2 tls vfs_rel(mp);
1233 1.2.10.2 tls if (error == 0 && (dvp->v_iflag & VI_DOOMED)) {
1234 1.2.10.2 tls vfs_unbusy(mp);
1235 1.2.10.2 tls error = ENOENT;
1236 1.2.10.2 tls }
1237 1.2.10.2 tls if (error != 0)
1238 1.2.10.2 tls return (error);
1239 1.2.10.2 tls }
1240 1.2.10.2 tls NFSVOPUNLOCK(dvp, 0);
1241 1.2.10.2 tls error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np, NULL,
1242 1.2.10.2 tls cnp->cn_lkflags);
1243 1.2.10.2 tls if (error == 0)
1244 1.2.10.2 tls newvp = NFSTOV(np);
1245 1.2.10.2 tls vfs_unbusy(mp);
1246 1.2.10.2 tls if (newvp != dvp)
1247 1.2.10.2 tls NFSVOPLOCK(dvp, ltype | LK_RETRY);
1248 1.2.10.2 tls if (dvp->v_iflag & VI_DOOMED) {
1249 1.2.10.2 tls if (error == 0) {
1250 1.2.10.2 tls if (newvp == dvp)
1251 1.2.10.2 tls vrele(newvp);
1252 1.2.10.2 tls else
1253 1.2.10.2 tls vput(newvp);
1254 1.2.10.2 tls }
1255 1.2.10.2 tls error = ENOENT;
1256 1.2.10.2 tls }
1257 1.2.10.2 tls if (error != 0)
1258 1.2.10.2 tls return (error);
1259 1.2.10.2 tls if (attrflag)
1260 1.2.10.2 tls (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1261 1.2.10.2 tls 0, 1);
1262 1.2.10.2 tls } else if (NFS_CMPFH(np, nfhp->nfh_fh, nfhp->nfh_len)) {
1263 1.2.10.2 tls FREE((caddr_t)nfhp, M_NFSFH);
1264 1.2.10.2 tls VREF(dvp);
1265 1.2.10.2 tls newvp = dvp;
1266 1.2.10.2 tls if (attrflag)
1267 1.2.10.2 tls (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1268 1.2.10.2 tls 0, 1);
1269 1.2.10.2 tls } else {
1270 1.2.10.2 tls error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np, NULL,
1271 1.2.10.2 tls cnp->cn_lkflags);
1272 1.2.10.2 tls if (error)
1273 1.2.10.2 tls return (error);
1274 1.2.10.2 tls newvp = NFSTOV(np);
1275 1.2.10.2 tls if (attrflag)
1276 1.2.10.2 tls (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1277 1.2.10.2 tls 0, 1);
1278 1.2.10.2 tls else if ((flags & (ISLASTCN | ISOPEN)) == (ISLASTCN | ISOPEN) &&
1279 1.2.10.2 tls !(np->n_flag & NMODIFIED)) {
1280 1.2.10.2 tls /*
1281 1.2.10.2 tls * Flush the attribute cache when opening a
1282 1.2.10.2 tls * leaf node to ensure that fresh attributes
1283 1.2.10.2 tls * are fetched in nfs_open() since we did not
1284 1.2.10.2 tls * fetch attributes from the LOOKUP reply.
1285 1.2.10.2 tls */
1286 1.2.10.2 tls mtx_lock(&np->n_mtx);
1287 1.2.10.2 tls np->n_attrstamp = 0;
1288 1.2.10.2 tls KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(newvp);
1289 1.2.10.2 tls mtx_unlock(&np->n_mtx);
1290 1.2.10.2 tls }
1291 1.2.10.2 tls }
1292 1.2.10.2 tls if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
1293 1.2.10.2 tls cnp->cn_flags |= SAVENAME;
1294 1.2.10.2 tls if ((cnp->cn_flags & MAKEENTRY) &&
1295 1.2.10.2 tls (cnp->cn_nameiop != DELETE || !(flags & ISLASTCN)) &&
1296 1.2.10.2 tls attrflag != 0 && (newvp->v_type != VDIR || dattrflag != 0))
1297 1.2.10.2 tls cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime,
1298 1.2.10.2 tls newvp->v_type != VDIR ? NULL : &dnfsva.na_ctime);
1299 1.2.10.2 tls *vpp = newvp;
1300 1.2.10.2 tls return (0);
1301 1.2.10.2 tls }
1302 1.2.10.2 tls
1303 1.2.10.2 tls /*
1304 1.2.10.2 tls * nfs read call.
1305 1.2.10.2 tls * Just call ncl_bioread() to do the work.
1306 1.2.10.2 tls */
1307 1.2.10.2 tls static int
1308 1.2.10.2 tls nfs_read(struct vop_read_args *ap)
1309 1.2.10.2 tls {
1310 1.2.10.2 tls struct vnode *vp = ap->a_vp;
1311 1.2.10.2 tls
1312 1.2.10.2 tls switch (vp->v_type) {
1313 1.2.10.2 tls case VREG:
1314 1.2.10.2 tls return (ncl_bioread(vp, ap->a_uio, ap->a_ioflag, ap->a_cred));
1315 1.2.10.2 tls case VDIR:
1316 1.2.10.2 tls return (EISDIR);
1317 1.2.10.2 tls default:
1318 1.2.10.2 tls return (EOPNOTSUPP);
1319 1.2.10.2 tls }
1320 1.2.10.2 tls }
1321 1.2.10.2 tls
1322 1.2.10.2 tls /*
1323 1.2.10.2 tls * nfs readlink call
1324 1.2.10.2 tls */
1325 1.2.10.2 tls static int
1326 1.2.10.2 tls nfs_readlink(struct vop_readlink_args *ap)
1327 1.2.10.2 tls {
1328 1.2.10.2 tls struct vnode *vp = ap->a_vp;
1329 1.2.10.2 tls
1330 1.2.10.2 tls if (vp->v_type != VLNK)
1331 1.2.10.2 tls return (EINVAL);
1332 1.2.10.2 tls return (ncl_bioread(vp, ap->a_uio, 0, ap->a_cred));
1333 1.2.10.2 tls }
1334 1.2.10.2 tls
1335 1.2.10.2 tls /*
1336 1.2.10.2 tls * Do a readlink rpc.
1337 1.2.10.2 tls * Called by ncl_doio() from below the buffer cache.
1338 1.2.10.2 tls */
1339 1.2.10.2 tls int
1340 1.2.10.2 tls ncl_readlinkrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1341 1.2.10.2 tls {
1342 1.2.10.2 tls int error, ret, attrflag;
1343 1.2.10.2 tls struct nfsvattr nfsva;
1344 1.2.10.2 tls
1345 1.2.10.2 tls error = nfsrpc_readlink(vp, uiop, cred, uiop->uio_td, &nfsva,
1346 1.2.10.2 tls &attrflag, NULL);
1347 1.2.10.2 tls if (attrflag) {
1348 1.2.10.2 tls ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
1349 1.2.10.2 tls if (ret && !error)
1350 1.2.10.2 tls error = ret;
1351 1.2.10.2 tls }
1352 1.2.10.2 tls if (error && NFS_ISV4(vp))
1353 1.2.10.2 tls error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1354 1.2.10.2 tls return (error);
1355 1.2.10.2 tls }
1356 1.2.10.2 tls
1357 1.2.10.2 tls /*
1358 1.2.10.2 tls * nfs read rpc call
1359 1.2.10.2 tls * Ditto above
1360 1.2.10.2 tls */
1361 1.2.10.2 tls int
1362 1.2.10.2 tls ncl_readrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1363 1.2.10.2 tls {
1364 1.2.10.2 tls int error, ret, attrflag;
1365 1.2.10.2 tls struct nfsvattr nfsva;
1366 1.2.10.2 tls struct nfsmount *nmp;
1367 1.2.10.2 tls
1368 1.2.10.2 tls nmp = VFSTONFS(vnode_mount(vp));
1369 1.2.10.2 tls error = EIO;
1370 1.2.10.2 tls attrflag = 0;
1371 1.2.10.2 tls if (NFSHASPNFS(nmp))
1372 1.2.10.2 tls error = nfscl_doiods(vp, uiop, NULL, NULL,
1373 1.2.10.2 tls NFSV4OPEN_ACCESSREAD, cred, uiop->uio_td);
1374 1.2.10.2 tls NFSCL_DEBUG(4, "readrpc: aft doiods=%d\n", error);
1375 1.2.10.2 tls if (error != 0)
1376 1.2.10.2 tls error = nfsrpc_read(vp, uiop, cred, uiop->uio_td, &nfsva,
1377 1.2.10.2 tls &attrflag, NULL);
1378 1.2.10.2 tls if (attrflag) {
1379 1.2.10.2 tls ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
1380 1.2.10.2 tls if (ret && !error)
1381 1.2.10.2 tls error = ret;
1382 1.2.10.2 tls }
1383 1.2.10.2 tls if (error && NFS_ISV4(vp))
1384 1.2.10.2 tls error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1385 1.2.10.2 tls return (error);
1386 1.2.10.2 tls }
1387 1.2.10.2 tls
1388 1.2.10.2 tls /*
1389 1.2.10.2 tls * nfs write call
1390 1.2.10.2 tls */
1391 1.2.10.2 tls int
1392 1.2.10.2 tls ncl_writerpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
1393 1.2.10.2 tls int *iomode, int *must_commit, int called_from_strategy)
1394 1.2.10.2 tls {
1395 1.2.10.2 tls struct nfsvattr nfsva;
1396 1.2.10.2 tls int error, attrflag, ret;
1397 1.2.10.2 tls struct nfsmount *nmp;
1398 1.2.10.2 tls
1399 1.2.10.2 tls nmp = VFSTONFS(vnode_mount(vp));
1400 1.2.10.2 tls error = EIO;
1401 1.2.10.2 tls attrflag = 0;
1402 1.2.10.2 tls if (NFSHASPNFS(nmp))
1403 1.2.10.2 tls error = nfscl_doiods(vp, uiop, iomode, must_commit,
1404 1.2.10.2 tls NFSV4OPEN_ACCESSWRITE, cred, uiop->uio_td);
1405 1.2.10.2 tls NFSCL_DEBUG(4, "writerpc: aft doiods=%d\n", error);
1406 1.2.10.2 tls if (error != 0)
1407 1.2.10.2 tls error = nfsrpc_write(vp, uiop, iomode, must_commit, cred,
1408 1.2.10.2 tls uiop->uio_td, &nfsva, &attrflag, NULL,
1409 1.2.10.2 tls called_from_strategy);
1410 1.2.10.2 tls if (attrflag) {
1411 1.2.10.2 tls if (VTONFS(vp)->n_flag & ND_NFSV4)
1412 1.2.10.2 tls ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 1,
1413 1.2.10.2 tls 1);
1414 1.2.10.2 tls else
1415 1.2.10.2 tls ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0,
1416 1.2.10.2 tls 1);
1417 1.2.10.2 tls if (ret && !error)
1418 1.2.10.2 tls error = ret;
1419 1.2.10.2 tls }
1420 1.2.10.2 tls if (DOINGASYNC(vp))
1421 1.2.10.2 tls *iomode = NFSWRITE_FILESYNC;
1422 1.2.10.2 tls if (error && NFS_ISV4(vp))
1423 1.2.10.2 tls error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1424 1.2.10.2 tls return (error);
1425 1.2.10.2 tls }
1426 1.2.10.2 tls
1427 1.2.10.2 tls /*
1428 1.2.10.2 tls * nfs mknod rpc
1429 1.2.10.2 tls * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1430 1.2.10.2 tls * mode set to specify the file type and the size field for rdev.
1431 1.2.10.2 tls */
1432 1.2.10.2 tls static int
1433 1.2.10.2 tls nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1434 1.2.10.2 tls struct vattr *vap)
1435 1.2.10.2 tls {
1436 1.2.10.2 tls struct nfsvattr nfsva, dnfsva;
1437 1.2.10.2 tls struct vnode *newvp = NULL;
1438 1.2.10.2 tls struct nfsnode *np = NULL, *dnp;
1439 1.2.10.2 tls struct nfsfh *nfhp;
1440 1.2.10.2 tls struct vattr vattr;
1441 1.2.10.2 tls int error = 0, attrflag, dattrflag;
1442 1.2.10.2 tls u_int32_t rdev;
1443 1.2.10.2 tls
1444 1.2.10.2 tls if (vap->va_type == VCHR || vap->va_type == VBLK)
1445 1.2.10.2 tls rdev = vap->va_rdev;
1446 1.2.10.2 tls else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1447 1.2.10.2 tls rdev = 0xffffffff;
1448 1.2.10.2 tls else
1449 1.2.10.2 tls return (EOPNOTSUPP);
1450 1.2.10.2 tls if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)))
1451 1.2.10.2 tls return (error);
1452 1.2.10.2 tls error = nfsrpc_mknod(dvp, cnp->cn_nameptr, cnp->cn_namelen, vap,
1453 1.2.10.2 tls rdev, vap->va_type, cnp->cn_cred, cnp->cn_thread, &dnfsva,
1454 1.2.10.2 tls &nfsva, &nfhp, &attrflag, &dattrflag, NULL);
1455 1.2.10.2 tls if (!error) {
1456 1.2.10.2 tls if (!nfhp)
1457 1.2.10.2 tls (void) nfsrpc_lookup(dvp, cnp->cn_nameptr,
1458 1.2.10.2 tls cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread,
1459 1.2.10.2 tls &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag,
1460 1.2.10.2 tls NULL);
1461 1.2.10.2 tls if (nfhp)
1462 1.2.10.2 tls error = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp,
1463 1.2.10.2 tls cnp->cn_thread, &np, NULL, LK_EXCLUSIVE);
1464 1.2.10.2 tls }
1465 1.2.10.2 tls if (dattrflag)
1466 1.2.10.2 tls (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1467 1.2.10.2 tls if (!error) {
1468 1.2.10.2 tls newvp = NFSTOV(np);
1469 1.2.10.2 tls if (attrflag != 0) {
1470 1.2.10.2 tls error = nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1471 1.2.10.2 tls 0, 1);
1472 1.2.10.2 tls if (error != 0)
1473 1.2.10.2 tls vput(newvp);
1474 1.2.10.2 tls }
1475 1.2.10.2 tls }
1476 1.2.10.2 tls if (!error) {
1477 1.2.10.2 tls *vpp = newvp;
1478 1.2.10.2 tls } else if (NFS_ISV4(dvp)) {
1479 1.2.10.2 tls error = nfscl_maperr(cnp->cn_thread, error, vap->va_uid,
1480 1.2.10.2 tls vap->va_gid);
1481 1.2.10.2 tls }
1482 1.2.10.2 tls dnp = VTONFS(dvp);
1483 1.2.10.2 tls mtx_lock(&dnp->n_mtx);
1484 1.2.10.2 tls dnp->n_flag |= NMODIFIED;
1485 1.2.10.2 tls if (!dattrflag) {
1486 1.2.10.2 tls dnp->n_attrstamp = 0;
1487 1.2.10.2 tls KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
1488 1.2.10.2 tls }
1489 1.2.10.2 tls mtx_unlock(&dnp->n_mtx);
1490 1.2.10.2 tls return (error);
1491 1.2.10.2 tls }
1492 1.2.10.2 tls
1493 1.2.10.2 tls /*
1494 1.2.10.2 tls * nfs mknod vop
1495 1.2.10.2 tls * just call nfs_mknodrpc() to do the work.
1496 1.2.10.2 tls */
1497 1.2.10.2 tls /* ARGSUSED */
1498 1.2.10.2 tls static int
1499 1.2.10.2 tls nfs_mknod(struct vop_mknod_args *ap)
1500 1.2.10.2 tls {
1501 1.2.10.2 tls return (nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap));
1502 1.2.10.2 tls }
1503 1.2.10.2 tls
1504 1.2.10.2 tls static struct mtx nfs_cverf_mtx;
1505 1.2.10.2 tls MTX_SYSINIT(nfs_cverf_mtx, &nfs_cverf_mtx, "NFS create verifier mutex",
1506 1.2.10.2 tls MTX_DEF);
1507 1.2.10.2 tls
1508 1.2.10.2 tls static nfsquad_t
1509 1.2.10.2 tls nfs_get_cverf(void)
1510 1.2.10.2 tls {
1511 1.2.10.2 tls static nfsquad_t cverf;
1512 1.2.10.2 tls nfsquad_t ret;
1513 1.2.10.2 tls static int cverf_initialized = 0;
1514 1.2.10.2 tls
1515 1.2.10.2 tls mtx_lock(&nfs_cverf_mtx);
1516 1.2.10.2 tls if (cverf_initialized == 0) {
1517 1.2.10.2 tls cverf.lval[0] = arc4random();
1518 1.2.10.2 tls cverf.lval[1] = arc4random();
1519 1.2.10.2 tls cverf_initialized = 1;
1520 1.2.10.2 tls } else
1521 1.2.10.2 tls cverf.qval++;
1522 1.2.10.2 tls ret = cverf;
1523 1.2.10.2 tls mtx_unlock(&nfs_cverf_mtx);
1524 1.2.10.2 tls
1525 1.2.10.2 tls return (ret);
1526 1.2.10.2 tls }
1527 1.2.10.2 tls
1528 1.2.10.2 tls /*
1529 1.2.10.2 tls * nfs file create call
1530 1.2.10.2 tls */
1531 1.2.10.2 tls static int
1532 1.2.10.2 tls nfs_create(struct vop_create_args *ap)
1533 1.2.10.2 tls {
1534 1.2.10.2 tls struct vnode *dvp = ap->a_dvp;
1535 1.2.10.2 tls struct vattr *vap = ap->a_vap;
1536 1.2.10.2 tls struct componentname *cnp = ap->a_cnp;
1537 1.2.10.2 tls struct nfsnode *np = NULL, *dnp;
1538 1.2.10.2 tls struct vnode *newvp = NULL;
1539 1.2.10.2 tls struct nfsmount *nmp;
1540 1.2.10.2 tls struct nfsvattr dnfsva, nfsva;
1541 1.2.10.2 tls struct nfsfh *nfhp;
1542 1.2.10.2 tls nfsquad_t cverf;
1543 1.2.10.2 tls int error = 0, attrflag, dattrflag, fmode = 0;
1544 1.2.10.2 tls struct vattr vattr;
1545 1.2.10.2 tls
1546 1.2.10.2 tls /*
1547 1.2.10.2 tls * Oops, not for me..
1548 1.2.10.2 tls */
1549 1.2.10.2 tls if (vap->va_type == VSOCK)
1550 1.2.10.2 tls return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
1551 1.2.10.2 tls
1552 1.2.10.2 tls if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)))
1553 1.2.10.2 tls return (error);
1554 1.2.10.2 tls if (vap->va_vaflags & VA_EXCLUSIVE)
1555 1.2.10.2 tls fmode |= O_EXCL;
1556 1.2.10.2 tls dnp = VTONFS(dvp);
1557 1.2.10.2 tls nmp = VFSTONFS(vnode_mount(dvp));
1558 1.2.10.2 tls again:
1559 1.2.10.2 tls /* For NFSv4, wait until any remove is done. */
1560 1.2.10.2 tls mtx_lock(&dnp->n_mtx);
1561 1.2.10.2 tls while (NFSHASNFSV4(nmp) && (dnp->n_flag & NREMOVEINPROG)) {
1562 1.2.10.2 tls dnp->n_flag |= NREMOVEWANT;
1563 1.2.10.2 tls (void) msleep((caddr_t)dnp, &dnp->n_mtx, PZERO, "nfscrt", 0);
1564 1.2.10.2 tls }
1565 1.2.10.2 tls mtx_unlock(&dnp->n_mtx);
1566 1.2.10.2 tls
1567 1.2.10.2 tls cverf = nfs_get_cverf();
1568 1.2.10.2 tls error = nfsrpc_create(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1569 1.2.10.2 tls vap, cverf, fmode, cnp->cn_cred, cnp->cn_thread, &dnfsva, &nfsva,
1570 1.2.10.2 tls &nfhp, &attrflag, &dattrflag, NULL);
1571 1.2.10.2 tls if (!error) {
1572 1.2.10.2 tls if (nfhp == NULL)
1573 1.2.10.2 tls (void) nfsrpc_lookup(dvp, cnp->cn_nameptr,
1574 1.2.10.2 tls cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread,
1575 1.2.10.2 tls &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag,
1576 1.2.10.2 tls NULL);
1577 1.2.10.2 tls if (nfhp != NULL)
1578 1.2.10.2 tls error = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp,
1579 1.2.10.2 tls cnp->cn_thread, &np, NULL, LK_EXCLUSIVE);
1580 1.2.10.2 tls }
1581 1.2.10.2 tls if (dattrflag)
1582 1.2.10.2 tls (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1583 1.2.10.2 tls if (!error) {
1584 1.2.10.2 tls newvp = NFSTOV(np);
1585 1.2.10.2 tls if (attrflag == 0)
1586 1.2.10.2 tls error = nfsrpc_getattr(newvp, cnp->cn_cred,
1587 1.2.10.2 tls cnp->cn_thread, &nfsva, NULL);
1588 1.2.10.2 tls if (error == 0)
1589 1.2.10.2 tls error = nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1590 1.2.10.2 tls 0, 1);
1591 1.2.10.2 tls }
1592 1.2.10.2 tls if (error) {
1593 1.2.10.2 tls if (newvp != NULL) {
1594 1.2.10.2 tls vput(newvp);
1595 1.2.10.2 tls newvp = NULL;
1596 1.2.10.2 tls }
1597 1.2.10.2 tls if (NFS_ISV34(dvp) && (fmode & O_EXCL) &&
1598 1.2.10.2 tls error == NFSERR_NOTSUPP) {
1599 1.2.10.2 tls fmode &= ~O_EXCL;
1600 1.2.10.2 tls goto again;
1601 1.2.10.2 tls }
1602 1.2.10.2 tls } else if (NFS_ISV34(dvp) && (fmode & O_EXCL)) {
1603 1.2.10.2 tls if (nfscl_checksattr(vap, &nfsva)) {
1604 1.2.10.2 tls error = nfsrpc_setattr(newvp, vap, NULL, cnp->cn_cred,
1605 1.2.10.2 tls cnp->cn_thread, &nfsva, &attrflag, NULL);
1606 1.2.10.2 tls if (error && (vap->va_uid != (uid_t)VNOVAL ||
1607 1.2.10.2 tls vap->va_gid != (gid_t)VNOVAL)) {
1608 1.2.10.2 tls /* try again without setting uid/gid */
1609 1.2.10.2 tls vap->va_uid = (uid_t)VNOVAL;
1610 1.2.10.2 tls vap->va_gid = (uid_t)VNOVAL;
1611 1.2.10.2 tls error = nfsrpc_setattr(newvp, vap, NULL,
1612 1.2.10.2 tls cnp->cn_cred, cnp->cn_thread, &nfsva,
1613 1.2.10.2 tls &attrflag, NULL);
1614 1.2.10.2 tls }
1615 1.2.10.2 tls if (attrflag)
1616 1.2.10.2 tls (void) nfscl_loadattrcache(&newvp, &nfsva, NULL,
1617 1.2.10.2 tls NULL, 0, 1);
1618 1.2.10.2 tls if (error != 0)
1619 1.2.10.2 tls vput(newvp);
1620 1.2.10.2 tls }
1621 1.2.10.2 tls }
1622 1.2.10.2 tls if (!error) {
1623 1.2.10.2 tls if ((cnp->cn_flags & MAKEENTRY) && attrflag)
1624 1.2.10.2 tls cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime,
1625 1.2.10.2 tls NULL);
1626 1.2.10.2 tls *ap->a_vpp = newvp;
1627 1.2.10.2 tls } else if (NFS_ISV4(dvp)) {
1628 1.2.10.2 tls error = nfscl_maperr(cnp->cn_thread, error, vap->va_uid,
1629 1.2.10.2 tls vap->va_gid);
1630 1.2.10.2 tls }
1631 1.2.10.2 tls mtx_lock(&dnp->n_mtx);
1632 1.2.10.2 tls dnp->n_flag |= NMODIFIED;
1633 1.2.10.2 tls if (!dattrflag) {
1634 1.2.10.2 tls dnp->n_attrstamp = 0;
1635 1.2.10.2 tls KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
1636 1.2.10.2 tls }
1637 1.2.10.2 tls mtx_unlock(&dnp->n_mtx);
1638 1.2.10.2 tls return (error);
1639 1.2.10.2 tls }
1640 1.2.10.2 tls
1641 1.2.10.2 tls /*
1642 1.2.10.2 tls * nfs file remove call
1643 1.2.10.2 tls * To try and make nfs semantics closer to ufs semantics, a file that has
1644 1.2.10.2 tls * other processes using the vnode is renamed instead of removed and then
1645 1.2.10.2 tls * removed later on the last close.
1646 1.2.10.2 tls * - If v_usecount > 1
1647 1.2.10.2 tls * If a rename is not already in the works
1648 1.2.10.2 tls * call nfs_sillyrename() to set it up
1649 1.2.10.2 tls * else
1650 1.2.10.2 tls * do the remove rpc
1651 1.2.10.2 tls */
1652 1.2.10.2 tls static int
1653 1.2.10.2 tls nfs_remove(struct vop_remove_args *ap)
1654 1.2.10.2 tls {
1655 1.2.10.2 tls struct vnode *vp = ap->a_vp;
1656 1.2.10.2 tls struct vnode *dvp = ap->a_dvp;
1657 1.2.10.2 tls struct componentname *cnp = ap->a_cnp;
1658 1.2.10.2 tls struct nfsnode *np = VTONFS(vp);
1659 1.2.10.2 tls int error = 0;
1660 1.2.10.2 tls struct vattr vattr;
1661 1.2.10.2 tls
1662 1.2.10.2 tls KASSERT((cnp->cn_flags & HASBUF) != 0, ("nfs_remove: no name"));
1663 1.2.10.2 tls KASSERT(vrefcnt(vp) > 0, ("nfs_remove: bad v_usecount"));
1664 1.2.10.2 tls if (vp->v_type == VDIR)
1665 1.2.10.2 tls error = EPERM;
1666 1.2.10.2 tls else if (vrefcnt(vp) == 1 || (np->n_sillyrename &&
1667 1.2.10.2 tls VOP_GETATTR(vp, &vattr, cnp->cn_cred) == 0 &&
1668 1.2.10.2 tls vattr.va_nlink > 1)) {
1669 1.2.10.2 tls /*
1670 1.2.10.2 tls * Purge the name cache so that the chance of a lookup for
1671 1.2.10.2 tls * the name succeeding while the remove is in progress is
1672 1.2.10.2 tls * minimized. Without node locking it can still happen, such
1673 1.2.10.2 tls * that an I/O op returns ESTALE, but since you get this if
1674 1.2.10.2 tls * another host removes the file..
1675 1.2.10.2 tls */
1676 1.2.10.2 tls cache_purge(vp);
1677 1.2.10.2 tls /*
1678 1.2.10.2 tls * throw away biocache buffers, mainly to avoid
1679 1.2.10.2 tls * unnecessary delayed writes later.
1680 1.2.10.2 tls */
1681 1.2.10.2 tls error = ncl_vinvalbuf(vp, 0, cnp->cn_thread, 1);
1682 1.2.10.2 tls /* Do the rpc */
1683 1.2.10.2 tls if (error != EINTR && error != EIO)
1684 1.2.10.2 tls error = nfs_removerpc(dvp, vp, cnp->cn_nameptr,
1685 1.2.10.2 tls cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread);
1686 1.2.10.2 tls /*
1687 1.2.10.2 tls * Kludge City: If the first reply to the remove rpc is lost..
1688 1.2.10.2 tls * the reply to the retransmitted request will be ENOENT
1689 1.2.10.2 tls * since the file was in fact removed
1690 1.2.10.2 tls * Therefore, we cheat and return success.
1691 1.2.10.2 tls */
1692 1.2.10.2 tls if (error == ENOENT)
1693 1.2.10.2 tls error = 0;
1694 1.2.10.2 tls } else if (!np->n_sillyrename)
1695 1.2.10.2 tls error = nfs_sillyrename(dvp, vp, cnp);
1696 1.2.10.2 tls mtx_lock(&np->n_mtx);
1697 1.2.10.2 tls np->n_attrstamp = 0;
1698 1.2.10.2 tls mtx_unlock(&np->n_mtx);
1699 1.2.10.2 tls KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
1700 1.2.10.2 tls return (error);
1701 1.2.10.2 tls }
1702 1.2.10.2 tls
1703 1.2.10.2 tls /*
1704 1.2.10.2 tls * nfs file remove rpc called from nfs_inactive
1705 1.2.10.2 tls */
1706 1.2.10.2 tls int
1707 1.2.10.2 tls ncl_removeit(struct sillyrename *sp, struct vnode *vp)
1708 1.2.10.2 tls {
1709 1.2.10.2 tls /*
1710 1.2.10.2 tls * Make sure that the directory vnode is still valid.
1711 1.2.10.2 tls * XXX we should lock sp->s_dvp here.
1712 1.2.10.2 tls */
1713 1.2.10.2 tls if (sp->s_dvp->v_type == VBAD)
1714 1.2.10.2 tls return (0);
1715 1.2.10.2 tls return (nfs_removerpc(sp->s_dvp, vp, sp->s_name, sp->s_namlen,
1716 1.2.10.2 tls sp->s_cred, NULL));
1717 1.2.10.2 tls }
1718 1.2.10.2 tls
1719 1.2.10.2 tls /*
1720 1.2.10.2 tls * Nfs remove rpc, called from nfs_remove() and ncl_removeit().
1721 1.2.10.2 tls */
1722 1.2.10.2 tls static int
1723 1.2.10.2 tls nfs_removerpc(struct vnode *dvp, struct vnode *vp, char *name,
1724 1.2.10.2 tls int namelen, struct ucred *cred, struct thread *td)
1725 1.2.10.2 tls {
1726 1.2.10.2 tls struct nfsvattr dnfsva;
1727 1.2.10.2 tls struct nfsnode *dnp = VTONFS(dvp);
1728 1.2.10.2 tls int error = 0, dattrflag;
1729 1.2.10.2 tls
1730 1.2.10.2 tls mtx_lock(&dnp->n_mtx);
1731 1.2.10.2 tls dnp->n_flag |= NREMOVEINPROG;
1732 1.2.10.2 tls mtx_unlock(&dnp->n_mtx);
1733 1.2.10.2 tls error = nfsrpc_remove(dvp, name, namelen, vp, cred, td, &dnfsva,
1734 1.2.10.2 tls &dattrflag, NULL);
1735 1.2.10.2 tls mtx_lock(&dnp->n_mtx);
1736 1.2.10.2 tls if ((dnp->n_flag & NREMOVEWANT)) {
1737 1.2.10.2 tls dnp->n_flag &= ~(NREMOVEWANT | NREMOVEINPROG);
1738 1.2.10.2 tls mtx_unlock(&dnp->n_mtx);
1739 1.2.10.2 tls wakeup((caddr_t)dnp);
1740 1.2.10.2 tls } else {
1741 1.2.10.2 tls dnp->n_flag &= ~NREMOVEINPROG;
1742 1.2.10.2 tls mtx_unlock(&dnp->n_mtx);
1743 1.2.10.2 tls }
1744 1.2.10.2 tls if (dattrflag)
1745 1.2.10.2 tls (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1746 1.2.10.2 tls mtx_lock(&dnp->n_mtx);
1747 1.2.10.2 tls dnp->n_flag |= NMODIFIED;
1748 1.2.10.2 tls if (!dattrflag) {
1749 1.2.10.2 tls dnp->n_attrstamp = 0;
1750 1.2.10.2 tls KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
1751 1.2.10.2 tls }
1752 1.2.10.2 tls mtx_unlock(&dnp->n_mtx);
1753 1.2.10.2 tls if (error && NFS_ISV4(dvp))
1754 1.2.10.2 tls error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
1755 1.2.10.2 tls return (error);
1756 1.2.10.2 tls }
1757 1.2.10.2 tls
1758 1.2.10.2 tls /*
1759 1.2.10.2 tls * nfs file rename call
1760 1.2.10.2 tls */
1761 1.2.10.2 tls static int
1762 1.2.10.2 tls nfs_rename(struct vop_rename_args *ap)
1763 1.2.10.2 tls {
1764 1.2.10.2 tls struct vnode *fvp = ap->a_fvp;
1765 1.2.10.2 tls struct vnode *tvp = ap->a_tvp;
1766 1.2.10.2 tls struct vnode *fdvp = ap->a_fdvp;
1767 1.2.10.2 tls struct vnode *tdvp = ap->a_tdvp;
1768 1.2.10.2 tls struct componentname *tcnp = ap->a_tcnp;
1769 1.2.10.2 tls struct componentname *fcnp = ap->a_fcnp;
1770 1.2.10.2 tls struct nfsnode *fnp = VTONFS(ap->a_fvp);
1771 1.2.10.2 tls struct nfsnode *tdnp = VTONFS(ap->a_tdvp);
1772 1.2.10.2 tls struct nfsv4node *newv4 = NULL;
1773 1.2.10.2 tls int error;
1774 1.2.10.2 tls
1775 1.2.10.2 tls KASSERT((tcnp->cn_flags & HASBUF) != 0 &&
1776 1.2.10.2 tls (fcnp->cn_flags & HASBUF) != 0, ("nfs_rename: no name"));
1777 1.2.10.2 tls /* Check for cross-device rename */
1778 1.2.10.2 tls if ((fvp->v_mount != tdvp->v_mount) ||
1779 1.2.10.2 tls (tvp && (fvp->v_mount != tvp->v_mount))) {
1780 1.2.10.2 tls error = EXDEV;
1781 1.2.10.2 tls goto out;
1782 1.2.10.2 tls }
1783 1.2.10.2 tls
1784 1.2.10.2 tls if (fvp == tvp) {
1785 1.2.10.3 jdolecek printf("nfs_rename: fvp == tvp (can't happen)\n");
1786 1.2.10.2 tls error = 0;
1787 1.2.10.2 tls goto out;
1788 1.2.10.2 tls }
1789 1.2.10.2 tls if ((error = NFSVOPLOCK(fvp, LK_EXCLUSIVE)) != 0)
1790 1.2.10.2 tls goto out;
1791 1.2.10.2 tls
1792 1.2.10.2 tls /*
1793 1.2.10.2 tls * We have to flush B_DELWRI data prior to renaming
1794 1.2.10.2 tls * the file. If we don't, the delayed-write buffers
1795 1.2.10.2 tls * can be flushed out later after the file has gone stale
1796 1.2.10.2 tls * under NFSV3. NFSV2 does not have this problem because
1797 1.2.10.2 tls * ( as far as I can tell ) it flushes dirty buffers more
1798 1.2.10.2 tls * often.
1799 1.2.10.2 tls *
1800 1.2.10.2 tls * Skip the rename operation if the fsync fails, this can happen
1801 1.2.10.2 tls * due to the server's volume being full, when we pushed out data
1802 1.2.10.2 tls * that was written back to our cache earlier. Not checking for
1803 1.2.10.2 tls * this condition can result in potential (silent) data loss.
1804 1.2.10.2 tls */
1805 1.2.10.2 tls error = VOP_FSYNC(fvp, MNT_WAIT, fcnp->cn_thread);
1806 1.2.10.2 tls NFSVOPUNLOCK(fvp, 0);
1807 1.2.10.2 tls if (!error && tvp)
1808 1.2.10.2 tls error = VOP_FSYNC(tvp, MNT_WAIT, tcnp->cn_thread);
1809 1.2.10.2 tls if (error)
1810 1.2.10.2 tls goto out;
1811 1.2.10.2 tls
1812 1.2.10.2 tls /*
1813 1.2.10.2 tls * If the tvp exists and is in use, sillyrename it before doing the
1814 1.2.10.2 tls * rename of the new file over it.
1815 1.2.10.2 tls * XXX Can't sillyrename a directory.
1816 1.2.10.2 tls */
1817 1.2.10.2 tls if (tvp && vrefcnt(tvp) > 1 && !VTONFS(tvp)->n_sillyrename &&
1818 1.2.10.2 tls tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
1819 1.2.10.2 tls vput(tvp);
1820 1.2.10.2 tls tvp = NULL;
1821 1.2.10.2 tls }
1822 1.2.10.2 tls
1823 1.2.10.2 tls error = nfs_renamerpc(fdvp, fvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1824 1.2.10.2 tls tdvp, tvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1825 1.2.10.2 tls tcnp->cn_thread);
1826 1.2.10.2 tls
1827 1.2.10.2 tls if (error == 0 && NFS_ISV4(tdvp)) {
1828 1.2.10.2 tls /*
1829 1.2.10.2 tls * For NFSv4, check to see if it is the same name and
1830 1.2.10.2 tls * replace the name, if it is different.
1831 1.2.10.2 tls */
1832 1.2.10.2 tls MALLOC(newv4, struct nfsv4node *,
1833 1.2.10.2 tls sizeof (struct nfsv4node) +
1834 1.2.10.2 tls tdnp->n_fhp->nfh_len + tcnp->cn_namelen - 1,
1835 1.2.10.2 tls M_NFSV4NODE, M_WAITOK);
1836 1.2.10.2 tls mtx_lock(&tdnp->n_mtx);
1837 1.2.10.2 tls mtx_lock(&fnp->n_mtx);
1838 1.2.10.2 tls if (fnp->n_v4 != NULL && fvp->v_type == VREG &&
1839 1.2.10.2 tls (fnp->n_v4->n4_namelen != tcnp->cn_namelen ||
1840 1.2.10.2 tls NFSBCMP(tcnp->cn_nameptr, NFS4NODENAME(fnp->n_v4),
1841 1.2.10.2 tls tcnp->cn_namelen) ||
1842 1.2.10.2 tls tdnp->n_fhp->nfh_len != fnp->n_v4->n4_fhlen ||
1843 1.2.10.2 tls NFSBCMP(tdnp->n_fhp->nfh_fh, fnp->n_v4->n4_data,
1844 1.2.10.2 tls tdnp->n_fhp->nfh_len))) {
1845 1.2.10.2 tls #ifdef notdef
1846 1.2.10.2 tls { char nnn[100]; int nnnl;
1847 1.2.10.2 tls nnnl = (tcnp->cn_namelen < 100) ? tcnp->cn_namelen : 99;
1848 1.2.10.2 tls bcopy(tcnp->cn_nameptr, nnn, nnnl);
1849 1.2.10.2 tls nnn[nnnl] = '\0';
1850 1.2.10.2 tls printf("ren replace=%s\n",nnn);
1851 1.2.10.2 tls }
1852 1.2.10.2 tls #endif
1853 1.2.10.2 tls FREE((caddr_t)fnp->n_v4, M_NFSV4NODE);
1854 1.2.10.2 tls fnp->n_v4 = newv4;
1855 1.2.10.2 tls newv4 = NULL;
1856 1.2.10.2 tls fnp->n_v4->n4_fhlen = tdnp->n_fhp->nfh_len;
1857 1.2.10.2 tls fnp->n_v4->n4_namelen = tcnp->cn_namelen;
1858 1.2.10.2 tls NFSBCOPY(tdnp->n_fhp->nfh_fh, fnp->n_v4->n4_data,
1859 1.2.10.2 tls tdnp->n_fhp->nfh_len);
1860 1.2.10.2 tls NFSBCOPY(tcnp->cn_nameptr,
1861 1.2.10.2 tls NFS4NODENAME(fnp->n_v4), tcnp->cn_namelen);
1862 1.2.10.2 tls }
1863 1.2.10.2 tls mtx_unlock(&tdnp->n_mtx);
1864 1.2.10.2 tls mtx_unlock(&fnp->n_mtx);
1865 1.2.10.2 tls if (newv4 != NULL)
1866 1.2.10.2 tls FREE((caddr_t)newv4, M_NFSV4NODE);
1867 1.2.10.2 tls }
1868 1.2.10.2 tls
1869 1.2.10.2 tls if (fvp->v_type == VDIR) {
1870 1.2.10.2 tls if (tvp != NULL && tvp->v_type == VDIR)
1871 1.2.10.2 tls cache_purge(tdvp);
1872 1.2.10.2 tls cache_purge(fdvp);
1873 1.2.10.2 tls }
1874 1.2.10.2 tls
1875 1.2.10.2 tls out:
1876 1.2.10.2 tls if (tdvp == tvp)
1877 1.2.10.2 tls vrele(tdvp);
1878 1.2.10.2 tls else
1879 1.2.10.2 tls vput(tdvp);
1880 1.2.10.2 tls if (tvp)
1881 1.2.10.2 tls vput(tvp);
1882 1.2.10.2 tls vrele(fdvp);
1883 1.2.10.2 tls vrele(fvp);
1884 1.2.10.2 tls /*
1885 1.2.10.2 tls * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1886 1.2.10.2 tls */
1887 1.2.10.2 tls if (error == ENOENT)
1888 1.2.10.2 tls error = 0;
1889 1.2.10.2 tls return (error);
1890 1.2.10.2 tls }
1891 1.2.10.2 tls
1892 1.2.10.2 tls /*
1893 1.2.10.2 tls * nfs file rename rpc called from nfs_remove() above
1894 1.2.10.2 tls */
1895 1.2.10.2 tls static int
1896 1.2.10.2 tls nfs_renameit(struct vnode *sdvp, struct vnode *svp, struct componentname *scnp,
1897 1.2.10.2 tls struct sillyrename *sp)
1898 1.2.10.2 tls {
1899 1.2.10.2 tls
1900 1.2.10.2 tls return (nfs_renamerpc(sdvp, svp, scnp->cn_nameptr, scnp->cn_namelen,
1901 1.2.10.2 tls sdvp, NULL, sp->s_name, sp->s_namlen, scnp->cn_cred,
1902 1.2.10.2 tls scnp->cn_thread));
1903 1.2.10.2 tls }
1904 1.2.10.2 tls
1905 1.2.10.2 tls /*
1906 1.2.10.2 tls * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
1907 1.2.10.2 tls */
1908 1.2.10.2 tls static int
1909 1.2.10.2 tls nfs_renamerpc(struct vnode *fdvp, struct vnode *fvp, char *fnameptr,
1910 1.2.10.2 tls int fnamelen, struct vnode *tdvp, struct vnode *tvp, char *tnameptr,
1911 1.2.10.2 tls int tnamelen, struct ucred *cred, struct thread *td)
1912 1.2.10.2 tls {
1913 1.2.10.2 tls struct nfsvattr fnfsva, tnfsva;
1914 1.2.10.2 tls struct nfsnode *fdnp = VTONFS(fdvp);
1915 1.2.10.2 tls struct nfsnode *tdnp = VTONFS(tdvp);
1916 1.2.10.2 tls int error = 0, fattrflag, tattrflag;
1917 1.2.10.2 tls
1918 1.2.10.2 tls error = nfsrpc_rename(fdvp, fvp, fnameptr, fnamelen, tdvp, tvp,
1919 1.2.10.2 tls tnameptr, tnamelen, cred, td, &fnfsva, &tnfsva, &fattrflag,
1920 1.2.10.2 tls &tattrflag, NULL, NULL);
1921 1.2.10.2 tls mtx_lock(&fdnp->n_mtx);
1922 1.2.10.2 tls fdnp->n_flag |= NMODIFIED;
1923 1.2.10.2 tls if (fattrflag != 0) {
1924 1.2.10.2 tls mtx_unlock(&fdnp->n_mtx);
1925 1.2.10.2 tls (void) nfscl_loadattrcache(&fdvp, &fnfsva, NULL, NULL, 0, 1);
1926 1.2.10.2 tls } else {
1927 1.2.10.2 tls fdnp->n_attrstamp = 0;
1928 1.2.10.2 tls mtx_unlock(&fdnp->n_mtx);
1929 1.2.10.2 tls KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(fdvp);
1930 1.2.10.2 tls }
1931 1.2.10.2 tls mtx_lock(&tdnp->n_mtx);
1932 1.2.10.2 tls tdnp->n_flag |= NMODIFIED;
1933 1.2.10.2 tls if (tattrflag != 0) {
1934 1.2.10.2 tls mtx_unlock(&tdnp->n_mtx);
1935 1.2.10.2 tls (void) nfscl_loadattrcache(&tdvp, &tnfsva, NULL, NULL, 0, 1);
1936 1.2.10.2 tls } else {
1937 1.2.10.2 tls tdnp->n_attrstamp = 0;
1938 1.2.10.2 tls mtx_unlock(&tdnp->n_mtx);
1939 1.2.10.2 tls KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(tdvp);
1940 1.2.10.2 tls }
1941 1.2.10.2 tls if (error && NFS_ISV4(fdvp))
1942 1.2.10.2 tls error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
1943 1.2.10.2 tls return (error);
1944 1.2.10.2 tls }
1945 1.2.10.2 tls
1946 1.2.10.2 tls /*
1947 1.2.10.2 tls * nfs hard link create call
1948 1.2.10.2 tls */
1949 1.2.10.2 tls static int
1950 1.2.10.2 tls nfs_link(struct vop_link_args *ap)
1951 1.2.10.2 tls {
1952 1.2.10.2 tls struct vnode *vp = ap->a_vp;
1953 1.2.10.2 tls struct vnode *tdvp = ap->a_tdvp;
1954 1.2.10.2 tls struct componentname *cnp = ap->a_cnp;
1955 1.2.10.2 tls struct nfsnode *np, *tdnp;
1956 1.2.10.2 tls struct nfsvattr nfsva, dnfsva;
1957 1.2.10.2 tls int error = 0, attrflag, dattrflag;
1958 1.2.10.2 tls
1959 1.2.10.2 tls /*
1960 1.2.10.2 tls * Push all writes to the server, so that the attribute cache
1961 1.2.10.2 tls * doesn't get "out of sync" with the server.
1962 1.2.10.2 tls * XXX There should be a better way!
1963 1.2.10.2 tls */
1964 1.2.10.2 tls VOP_FSYNC(vp, MNT_WAIT, cnp->cn_thread);
1965 1.2.10.2 tls
1966 1.2.10.2 tls error = nfsrpc_link(tdvp, vp, cnp->cn_nameptr, cnp->cn_namelen,
1967 1.2.10.2 tls cnp->cn_cred, cnp->cn_thread, &dnfsva, &nfsva, &attrflag,
1968 1.2.10.2 tls &dattrflag, NULL);
1969 1.2.10.2 tls tdnp = VTONFS(tdvp);
1970 1.2.10.2 tls mtx_lock(&tdnp->n_mtx);
1971 1.2.10.2 tls tdnp->n_flag |= NMODIFIED;
1972 1.2.10.2 tls if (dattrflag != 0) {
1973 1.2.10.2 tls mtx_unlock(&tdnp->n_mtx);
1974 1.2.10.2 tls (void) nfscl_loadattrcache(&tdvp, &dnfsva, NULL, NULL, 0, 1);
1975 1.2.10.2 tls } else {
1976 1.2.10.2 tls tdnp->n_attrstamp = 0;
1977 1.2.10.2 tls mtx_unlock(&tdnp->n_mtx);
1978 1.2.10.2 tls KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(tdvp);
1979 1.2.10.2 tls }
1980 1.2.10.2 tls if (attrflag)
1981 1.2.10.2 tls (void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
1982 1.2.10.2 tls else {
1983 1.2.10.2 tls np = VTONFS(vp);
1984 1.2.10.2 tls mtx_lock(&np->n_mtx);
1985 1.2.10.2 tls np->n_attrstamp = 0;
1986 1.2.10.2 tls mtx_unlock(&np->n_mtx);
1987 1.2.10.2 tls KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
1988 1.2.10.2 tls }
1989 1.2.10.2 tls /*
1990 1.2.10.2 tls * If negative lookup caching is enabled, I might as well
1991 1.2.10.2 tls * add an entry for this node. Not necessary for correctness,
1992 1.2.10.2 tls * but if negative caching is enabled, then the system
1993 1.2.10.2 tls * must care about lookup caching hit rate, so...
1994 1.2.10.2 tls */
1995 1.2.10.2 tls if (VFSTONFS(vp->v_mount)->nm_negnametimeo != 0 &&
1996 1.2.10.2 tls (cnp->cn_flags & MAKEENTRY) && attrflag != 0 && error == 0) {
1997 1.2.10.2 tls cache_enter_time(tdvp, vp, cnp, &nfsva.na_ctime, NULL);
1998 1.2.10.2 tls }
1999 1.2.10.2 tls if (error && NFS_ISV4(vp))
2000 1.2.10.2 tls error = nfscl_maperr(cnp->cn_thread, error, (uid_t)0,
2001 1.2.10.2 tls (gid_t)0);
2002 1.2.10.2 tls return (error);
2003 1.2.10.2 tls }
2004 1.2.10.2 tls
2005 1.2.10.2 tls /*
2006 1.2.10.2 tls * nfs symbolic link create call
2007 1.2.10.2 tls */
2008 1.2.10.2 tls static int
2009 1.2.10.2 tls nfs_symlink(struct vop_symlink_args *ap)
2010 1.2.10.2 tls {
2011 1.2.10.2 tls struct vnode *dvp = ap->a_dvp;
2012 1.2.10.2 tls struct vattr *vap = ap->a_vap;
2013 1.2.10.2 tls struct componentname *cnp = ap->a_cnp;
2014 1.2.10.2 tls struct nfsvattr nfsva, dnfsva;
2015 1.2.10.2 tls struct nfsfh *nfhp;
2016 1.2.10.2 tls struct nfsnode *np = NULL, *dnp;
2017 1.2.10.2 tls struct vnode *newvp = NULL;
2018 1.2.10.2 tls int error = 0, attrflag, dattrflag, ret;
2019 1.2.10.2 tls
2020 1.2.10.2 tls vap->va_type = VLNK;
2021 1.2.10.2 tls error = nfsrpc_symlink(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2022 1.2.10.2 tls ap->a_target, vap, cnp->cn_cred, cnp->cn_thread, &dnfsva,
2023 1.2.10.2 tls &nfsva, &nfhp, &attrflag, &dattrflag, NULL);
2024 1.2.10.2 tls if (nfhp) {
2025 1.2.10.2 tls ret = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp, cnp->cn_thread,
2026 1.2.10.2 tls &np, NULL, LK_EXCLUSIVE);
2027 1.2.10.2 tls if (!ret)
2028 1.2.10.2 tls newvp = NFSTOV(np);
2029 1.2.10.2 tls else if (!error)
2030 1.2.10.2 tls error = ret;
2031 1.2.10.2 tls }
2032 1.2.10.2 tls if (newvp != NULL) {
2033 1.2.10.2 tls if (attrflag)
2034 1.2.10.2 tls (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
2035 1.2.10.2 tls 0, 1);
2036 1.2.10.2 tls } else if (!error) {
2037 1.2.10.2 tls /*
2038 1.2.10.2 tls * If we do not have an error and we could not extract the
2039 1.2.10.2 tls * newvp from the response due to the request being NFSv2, we
2040 1.2.10.2 tls * have to do a lookup in order to obtain a newvp to return.
2041 1.2.10.2 tls */
2042 1.2.10.2 tls error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2043 1.2.10.2 tls cnp->cn_cred, cnp->cn_thread, &np);
2044 1.2.10.2 tls if (!error)
2045 1.2.10.2 tls newvp = NFSTOV(np);
2046 1.2.10.2 tls }
2047 1.2.10.2 tls if (error) {
2048 1.2.10.2 tls if (newvp)
2049 1.2.10.2 tls vput(newvp);
2050 1.2.10.2 tls if (NFS_ISV4(dvp))
2051 1.2.10.2 tls error = nfscl_maperr(cnp->cn_thread, error,
2052 1.2.10.2 tls vap->va_uid, vap->va_gid);
2053 1.2.10.2 tls } else {
2054 1.2.10.2 tls *ap->a_vpp = newvp;
2055 1.2.10.2 tls }
2056 1.2.10.2 tls
2057 1.2.10.2 tls dnp = VTONFS(dvp);
2058 1.2.10.2 tls mtx_lock(&dnp->n_mtx);
2059 1.2.10.2 tls dnp->n_flag |= NMODIFIED;
2060 1.2.10.2 tls if (dattrflag != 0) {
2061 1.2.10.2 tls mtx_unlock(&dnp->n_mtx);
2062 1.2.10.2 tls (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
2063 1.2.10.2 tls } else {
2064 1.2.10.2 tls dnp->n_attrstamp = 0;
2065 1.2.10.2 tls mtx_unlock(&dnp->n_mtx);
2066 1.2.10.2 tls KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2067 1.2.10.2 tls }
2068 1.2.10.2 tls /*
2069 1.2.10.2 tls * If negative lookup caching is enabled, I might as well
2070 1.2.10.2 tls * add an entry for this node. Not necessary for correctness,
2071 1.2.10.2 tls * but if negative caching is enabled, then the system
2072 1.2.10.2 tls * must care about lookup caching hit rate, so...
2073 1.2.10.2 tls */
2074 1.2.10.2 tls if (VFSTONFS(dvp->v_mount)->nm_negnametimeo != 0 &&
2075 1.2.10.2 tls (cnp->cn_flags & MAKEENTRY) && attrflag != 0 && error == 0) {
2076 1.2.10.2 tls cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime, NULL);
2077 1.2.10.2 tls }
2078 1.2.10.2 tls return (error);
2079 1.2.10.2 tls }
2080 1.2.10.2 tls
2081 1.2.10.2 tls /*
2082 1.2.10.2 tls * nfs make dir call
2083 1.2.10.2 tls */
2084 1.2.10.2 tls static int
2085 1.2.10.2 tls nfs_mkdir(struct vop_mkdir_args *ap)
2086 1.2.10.2 tls {
2087 1.2.10.2 tls struct vnode *dvp = ap->a_dvp;
2088 1.2.10.2 tls struct vattr *vap = ap->a_vap;
2089 1.2.10.2 tls struct componentname *cnp = ap->a_cnp;
2090 1.2.10.2 tls struct nfsnode *np = NULL, *dnp;
2091 1.2.10.2 tls struct vnode *newvp = NULL;
2092 1.2.10.2 tls struct vattr vattr;
2093 1.2.10.2 tls struct nfsfh *nfhp;
2094 1.2.10.2 tls struct nfsvattr nfsva, dnfsva;
2095 1.2.10.2 tls int error = 0, attrflag, dattrflag, ret;
2096 1.2.10.2 tls
2097 1.2.10.2 tls if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)) != 0)
2098 1.2.10.2 tls return (error);
2099 1.2.10.2 tls vap->va_type = VDIR;
2100 1.2.10.2 tls error = nfsrpc_mkdir(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2101 1.2.10.2 tls vap, cnp->cn_cred, cnp->cn_thread, &dnfsva, &nfsva, &nfhp,
2102 1.2.10.2 tls &attrflag, &dattrflag, NULL);
2103 1.2.10.2 tls dnp = VTONFS(dvp);
2104 1.2.10.2 tls mtx_lock(&dnp->n_mtx);
2105 1.2.10.2 tls dnp->n_flag |= NMODIFIED;
2106 1.2.10.2 tls if (dattrflag != 0) {
2107 1.2.10.2 tls mtx_unlock(&dnp->n_mtx);
2108 1.2.10.2 tls (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
2109 1.2.10.2 tls } else {
2110 1.2.10.2 tls dnp->n_attrstamp = 0;
2111 1.2.10.2 tls mtx_unlock(&dnp->n_mtx);
2112 1.2.10.2 tls KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2113 1.2.10.2 tls }
2114 1.2.10.2 tls if (nfhp) {
2115 1.2.10.2 tls ret = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp, cnp->cn_thread,
2116 1.2.10.2 tls &np, NULL, LK_EXCLUSIVE);
2117 1.2.10.2 tls if (!ret) {
2118 1.2.10.2 tls newvp = NFSTOV(np);
2119 1.2.10.2 tls if (attrflag)
2120 1.2.10.2 tls (void) nfscl_loadattrcache(&newvp, &nfsva, NULL,
2121 1.2.10.2 tls NULL, 0, 1);
2122 1.2.10.2 tls } else if (!error)
2123 1.2.10.2 tls error = ret;
2124 1.2.10.2 tls }
2125 1.2.10.2 tls if (!error && newvp == NULL) {
2126 1.2.10.2 tls error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2127 1.2.10.2 tls cnp->cn_cred, cnp->cn_thread, &np);
2128 1.2.10.2 tls if (!error) {
2129 1.2.10.2 tls newvp = NFSTOV(np);
2130 1.2.10.2 tls if (newvp->v_type != VDIR)
2131 1.2.10.2 tls error = EEXIST;
2132 1.2.10.2 tls }
2133 1.2.10.2 tls }
2134 1.2.10.2 tls if (error) {
2135 1.2.10.2 tls if (newvp)
2136 1.2.10.2 tls vput(newvp);
2137 1.2.10.2 tls if (NFS_ISV4(dvp))
2138 1.2.10.2 tls error = nfscl_maperr(cnp->cn_thread, error,
2139 1.2.10.2 tls vap->va_uid, vap->va_gid);
2140 1.2.10.2 tls } else {
2141 1.2.10.2 tls /*
2142 1.2.10.2 tls * If negative lookup caching is enabled, I might as well
2143 1.2.10.2 tls * add an entry for this node. Not necessary for correctness,
2144 1.2.10.2 tls * but if negative caching is enabled, then the system
2145 1.2.10.2 tls * must care about lookup caching hit rate, so...
2146 1.2.10.2 tls */
2147 1.2.10.2 tls if (VFSTONFS(dvp->v_mount)->nm_negnametimeo != 0 &&
2148 1.2.10.2 tls (cnp->cn_flags & MAKEENTRY) &&
2149 1.2.10.2 tls attrflag != 0 && dattrflag != 0)
2150 1.2.10.2 tls cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime,
2151 1.2.10.2 tls &dnfsva.na_ctime);
2152 1.2.10.2 tls *ap->a_vpp = newvp;
2153 1.2.10.2 tls }
2154 1.2.10.2 tls return (error);
2155 1.2.10.2 tls }
2156 1.2.10.2 tls
2157 1.2.10.2 tls /*
2158 1.2.10.2 tls * nfs remove directory call
2159 1.2.10.2 tls */
2160 1.2.10.2 tls static int
2161 1.2.10.2 tls nfs_rmdir(struct vop_rmdir_args *ap)
2162 1.2.10.2 tls {
2163 1.2.10.2 tls struct vnode *vp = ap->a_vp;
2164 1.2.10.2 tls struct vnode *dvp = ap->a_dvp;
2165 1.2.10.2 tls struct componentname *cnp = ap->a_cnp;
2166 1.2.10.2 tls struct nfsnode *dnp;
2167 1.2.10.2 tls struct nfsvattr dnfsva;
2168 1.2.10.2 tls int error, dattrflag;
2169 1.2.10.2 tls
2170 1.2.10.2 tls if (dvp == vp)
2171 1.2.10.2 tls return (EINVAL);
2172 1.2.10.2 tls error = nfsrpc_rmdir(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2173 1.2.10.2 tls cnp->cn_cred, cnp->cn_thread, &dnfsva, &dattrflag, NULL);
2174 1.2.10.2 tls dnp = VTONFS(dvp);
2175 1.2.10.2 tls mtx_lock(&dnp->n_mtx);
2176 1.2.10.2 tls dnp->n_flag |= NMODIFIED;
2177 1.2.10.2 tls if (dattrflag != 0) {
2178 1.2.10.2 tls mtx_unlock(&dnp->n_mtx);
2179 1.2.10.2 tls (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
2180 1.2.10.2 tls } else {
2181 1.2.10.2 tls dnp->n_attrstamp = 0;
2182 1.2.10.2 tls mtx_unlock(&dnp->n_mtx);
2183 1.2.10.2 tls KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2184 1.2.10.2 tls }
2185 1.2.10.2 tls
2186 1.2.10.2 tls cache_purge(dvp);
2187 1.2.10.2 tls cache_purge(vp);
2188 1.2.10.2 tls if (error && NFS_ISV4(dvp))
2189 1.2.10.2 tls error = nfscl_maperr(cnp->cn_thread, error, (uid_t)0,
2190 1.2.10.2 tls (gid_t)0);
2191 1.2.10.2 tls /*
2192 1.2.10.2 tls * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2193 1.2.10.2 tls */
2194 1.2.10.2 tls if (error == ENOENT)
2195 1.2.10.2 tls error = 0;
2196 1.2.10.2 tls return (error);
2197 1.2.10.2 tls }
2198 1.2.10.2 tls
2199 1.2.10.2 tls /*
2200 1.2.10.2 tls * nfs readdir call
2201 1.2.10.2 tls */
2202 1.2.10.2 tls static int
2203 1.2.10.2 tls nfs_readdir(struct vop_readdir_args *ap)
2204 1.2.10.2 tls {
2205 1.2.10.2 tls struct vnode *vp = ap->a_vp;
2206 1.2.10.2 tls struct nfsnode *np = VTONFS(vp);
2207 1.2.10.2 tls struct uio *uio = ap->a_uio;
2208 1.2.10.3 jdolecek ssize_t tresid, left;
2209 1.2.10.2 tls int error = 0;
2210 1.2.10.2 tls struct vattr vattr;
2211 1.2.10.2 tls
2212 1.2.10.2 tls if (ap->a_eofflag != NULL)
2213 1.2.10.2 tls *ap->a_eofflag = 0;
2214 1.2.10.2 tls if (vp->v_type != VDIR)
2215 1.2.10.2 tls return(EPERM);
2216 1.2.10.2 tls
2217 1.2.10.2 tls /*
2218 1.2.10.2 tls * First, check for hit on the EOF offset cache
2219 1.2.10.2 tls */
2220 1.2.10.2 tls if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2221 1.2.10.2 tls (np->n_flag & NMODIFIED) == 0) {
2222 1.2.10.2 tls if (VOP_GETATTR(vp, &vattr, ap->a_cred) == 0) {
2223 1.2.10.2 tls mtx_lock(&np->n_mtx);
2224 1.2.10.2 tls if ((NFS_ISV4(vp) && np->n_change == vattr.va_filerev) ||
2225 1.2.10.2 tls !NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime)) {
2226 1.2.10.2 tls mtx_unlock(&np->n_mtx);
2227 1.2.10.3 jdolecek NFSINCRGLOBAL(nfsstatsv1.direofcache_hits);
2228 1.2.10.2 tls if (ap->a_eofflag != NULL)
2229 1.2.10.2 tls *ap->a_eofflag = 1;
2230 1.2.10.2 tls return (0);
2231 1.2.10.2 tls } else
2232 1.2.10.2 tls mtx_unlock(&np->n_mtx);
2233 1.2.10.2 tls }
2234 1.2.10.2 tls }
2235 1.2.10.2 tls
2236 1.2.10.2 tls /*
2237 1.2.10.3 jdolecek * NFS always guarantees that directory entries don't straddle
2238 1.2.10.3 jdolecek * DIRBLKSIZ boundaries. As such, we need to limit the size
2239 1.2.10.3 jdolecek * to an exact multiple of DIRBLKSIZ, to avoid copying a partial
2240 1.2.10.3 jdolecek * directory entry.
2241 1.2.10.3 jdolecek */
2242 1.2.10.3 jdolecek left = uio->uio_resid % DIRBLKSIZ;
2243 1.2.10.3 jdolecek if (left == uio->uio_resid)
2244 1.2.10.3 jdolecek return (EINVAL);
2245 1.2.10.3 jdolecek uio->uio_resid -= left;
2246 1.2.10.3 jdolecek
2247 1.2.10.3 jdolecek /*
2248 1.2.10.2 tls * Call ncl_bioread() to do the real work.
2249 1.2.10.2 tls */
2250 1.2.10.2 tls tresid = uio->uio_resid;
2251 1.2.10.2 tls error = ncl_bioread(vp, uio, 0, ap->a_cred);
2252 1.2.10.2 tls
2253 1.2.10.2 tls if (!error && uio->uio_resid == tresid) {
2254 1.2.10.3 jdolecek NFSINCRGLOBAL(nfsstatsv1.direofcache_misses);
2255 1.2.10.2 tls if (ap->a_eofflag != NULL)
2256 1.2.10.2 tls *ap->a_eofflag = 1;
2257 1.2.10.2 tls }
2258 1.2.10.3 jdolecek
2259 1.2.10.3 jdolecek /* Add the partial DIRBLKSIZ (left) back in. */
2260 1.2.10.3 jdolecek uio->uio_resid += left;
2261 1.2.10.2 tls return (error);
2262 1.2.10.2 tls }
2263 1.2.10.2 tls
2264 1.2.10.2 tls /*
2265 1.2.10.2 tls * Readdir rpc call.
2266 1.2.10.2 tls * Called from below the buffer cache by ncl_doio().
2267 1.2.10.2 tls */
2268 1.2.10.2 tls int
2269 1.2.10.2 tls ncl_readdirrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
2270 1.2.10.2 tls struct thread *td)
2271 1.2.10.2 tls {
2272 1.2.10.2 tls struct nfsvattr nfsva;
2273 1.2.10.2 tls nfsuint64 *cookiep, cookie;
2274 1.2.10.2 tls struct nfsnode *dnp = VTONFS(vp);
2275 1.2.10.2 tls struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2276 1.2.10.2 tls int error = 0, eof, attrflag;
2277 1.2.10.2 tls
2278 1.2.10.2 tls KASSERT(uiop->uio_iovcnt == 1 &&
2279 1.2.10.2 tls (uiop->uio_offset & (DIRBLKSIZ - 1)) == 0 &&
2280 1.2.10.2 tls (uiop->uio_resid & (DIRBLKSIZ - 1)) == 0,
2281 1.2.10.2 tls ("nfs readdirrpc bad uio"));
2282 1.2.10.2 tls
2283 1.2.10.2 tls /*
2284 1.2.10.2 tls * If there is no cookie, assume directory was stale.
2285 1.2.10.2 tls */
2286 1.2.10.2 tls ncl_dircookie_lock(dnp);
2287 1.2.10.2 tls cookiep = ncl_getcookie(dnp, uiop->uio_offset, 0);
2288 1.2.10.2 tls if (cookiep) {
2289 1.2.10.2 tls cookie = *cookiep;
2290 1.2.10.2 tls ncl_dircookie_unlock(dnp);
2291 1.2.10.2 tls } else {
2292 1.2.10.2 tls ncl_dircookie_unlock(dnp);
2293 1.2.10.2 tls return (NFSERR_BAD_COOKIE);
2294 1.2.10.2 tls }
2295 1.2.10.2 tls
2296 1.2.10.2 tls if (NFSHASNFSV3(nmp) && !NFSHASGOTFSINFO(nmp))
2297 1.2.10.2 tls (void)ncl_fsinfo(nmp, vp, cred, td);
2298 1.2.10.2 tls
2299 1.2.10.2 tls error = nfsrpc_readdir(vp, uiop, &cookie, cred, td, &nfsva,
2300 1.2.10.2 tls &attrflag, &eof, NULL);
2301 1.2.10.2 tls if (attrflag)
2302 1.2.10.2 tls (void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
2303 1.2.10.2 tls
2304 1.2.10.2 tls if (!error) {
2305 1.2.10.2 tls /*
2306 1.2.10.2 tls * We are now either at the end of the directory or have filled
2307 1.2.10.2 tls * the block.
2308 1.2.10.2 tls */
2309 1.2.10.2 tls if (eof)
2310 1.2.10.2 tls dnp->n_direofoffset = uiop->uio_offset;
2311 1.2.10.2 tls else {
2312 1.2.10.2 tls if (uiop->uio_resid > 0)
2313 1.2.10.3 jdolecek printf("EEK! readdirrpc resid > 0\n");
2314 1.2.10.2 tls ncl_dircookie_lock(dnp);
2315 1.2.10.2 tls cookiep = ncl_getcookie(dnp, uiop->uio_offset, 1);
2316 1.2.10.2 tls *cookiep = cookie;
2317 1.2.10.2 tls ncl_dircookie_unlock(dnp);
2318 1.2.10.2 tls }
2319 1.2.10.2 tls } else if (NFS_ISV4(vp)) {
2320 1.2.10.2 tls error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2321 1.2.10.2 tls }
2322 1.2.10.2 tls return (error);
2323 1.2.10.2 tls }
2324 1.2.10.2 tls
2325 1.2.10.2 tls /*
2326 1.2.10.2 tls * NFS V3 readdir plus RPC. Used in place of ncl_readdirrpc().
2327 1.2.10.2 tls */
2328 1.2.10.2 tls int
2329 1.2.10.2 tls ncl_readdirplusrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
2330 1.2.10.2 tls struct thread *td)
2331 1.2.10.2 tls {
2332 1.2.10.2 tls struct nfsvattr nfsva;
2333 1.2.10.2 tls nfsuint64 *cookiep, cookie;
2334 1.2.10.2 tls struct nfsnode *dnp = VTONFS(vp);
2335 1.2.10.2 tls struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2336 1.2.10.2 tls int error = 0, attrflag, eof;
2337 1.2.10.2 tls
2338 1.2.10.2 tls KASSERT(uiop->uio_iovcnt == 1 &&
2339 1.2.10.2 tls (uiop->uio_offset & (DIRBLKSIZ - 1)) == 0 &&
2340 1.2.10.2 tls (uiop->uio_resid & (DIRBLKSIZ - 1)) == 0,
2341 1.2.10.2 tls ("nfs readdirplusrpc bad uio"));
2342 1.2.10.2 tls
2343 1.2.10.2 tls /*
2344 1.2.10.2 tls * If there is no cookie, assume directory was stale.
2345 1.2.10.2 tls */
2346 1.2.10.2 tls ncl_dircookie_lock(dnp);
2347 1.2.10.2 tls cookiep = ncl_getcookie(dnp, uiop->uio_offset, 0);
2348 1.2.10.2 tls if (cookiep) {
2349 1.2.10.2 tls cookie = *cookiep;
2350 1.2.10.2 tls ncl_dircookie_unlock(dnp);
2351 1.2.10.2 tls } else {
2352 1.2.10.2 tls ncl_dircookie_unlock(dnp);
2353 1.2.10.2 tls return (NFSERR_BAD_COOKIE);
2354 1.2.10.2 tls }
2355 1.2.10.2 tls
2356 1.2.10.2 tls if (NFSHASNFSV3(nmp) && !NFSHASGOTFSINFO(nmp))
2357 1.2.10.2 tls (void)ncl_fsinfo(nmp, vp, cred, td);
2358 1.2.10.2 tls error = nfsrpc_readdirplus(vp, uiop, &cookie, cred, td, &nfsva,
2359 1.2.10.2 tls &attrflag, &eof, NULL);
2360 1.2.10.2 tls if (attrflag)
2361 1.2.10.2 tls (void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
2362 1.2.10.2 tls
2363 1.2.10.2 tls if (!error) {
2364 1.2.10.2 tls /*
2365 1.2.10.2 tls * We are now either at end of the directory or have filled the
2366 1.2.10.2 tls * the block.
2367 1.2.10.2 tls */
2368 1.2.10.2 tls if (eof)
2369 1.2.10.2 tls dnp->n_direofoffset = uiop->uio_offset;
2370 1.2.10.2 tls else {
2371 1.2.10.2 tls if (uiop->uio_resid > 0)
2372 1.2.10.3 jdolecek printf("EEK! readdirplusrpc resid > 0\n");
2373 1.2.10.2 tls ncl_dircookie_lock(dnp);
2374 1.2.10.2 tls cookiep = ncl_getcookie(dnp, uiop->uio_offset, 1);
2375 1.2.10.2 tls *cookiep = cookie;
2376 1.2.10.2 tls ncl_dircookie_unlock(dnp);
2377 1.2.10.2 tls }
2378 1.2.10.2 tls } else if (NFS_ISV4(vp)) {
2379 1.2.10.2 tls error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2380 1.2.10.2 tls }
2381 1.2.10.2 tls return (error);
2382 1.2.10.2 tls }
2383 1.2.10.2 tls
2384 1.2.10.2 tls /*
2385 1.2.10.2 tls * Silly rename. To make the NFS filesystem that is stateless look a little
2386 1.2.10.2 tls * more like the "ufs" a remove of an active vnode is translated to a rename
2387 1.2.10.2 tls * to a funny looking filename that is removed by nfs_inactive on the
2388 1.2.10.2 tls * nfsnode. There is the potential for another process on a different client
2389 1.2.10.2 tls * to create the same funny name between the nfs_lookitup() fails and the
2390 1.2.10.2 tls * nfs_rename() completes, but...
2391 1.2.10.2 tls */
2392 1.2.10.2 tls static int
2393 1.2.10.2 tls nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2394 1.2.10.2 tls {
2395 1.2.10.2 tls struct sillyrename *sp;
2396 1.2.10.2 tls struct nfsnode *np;
2397 1.2.10.2 tls int error;
2398 1.2.10.2 tls short pid;
2399 1.2.10.2 tls unsigned int lticks;
2400 1.2.10.2 tls
2401 1.2.10.2 tls cache_purge(dvp);
2402 1.2.10.2 tls np = VTONFS(vp);
2403 1.2.10.2 tls KASSERT(vp->v_type != VDIR, ("nfs: sillyrename dir"));
2404 1.2.10.2 tls MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename),
2405 1.2.10.2 tls M_NEWNFSREQ, M_WAITOK);
2406 1.2.10.2 tls sp->s_cred = crhold(cnp->cn_cred);
2407 1.2.10.2 tls sp->s_dvp = dvp;
2408 1.2.10.2 tls VREF(dvp);
2409 1.2.10.2 tls
2410 1.2.10.2 tls /*
2411 1.2.10.2 tls * Fudge together a funny name.
2412 1.2.10.3 jdolecek * Changing the format of the funny name to accommodate more
2413 1.2.10.2 tls * sillynames per directory.
2414 1.2.10.2 tls * The name is now changed to .nfs.<ticks>.<pid>.4, where ticks is
2415 1.2.10.2 tls * CPU ticks since boot.
2416 1.2.10.2 tls */
2417 1.2.10.2 tls pid = cnp->cn_thread->td_proc->p_pid;
2418 1.2.10.2 tls lticks = (unsigned int)ticks;
2419 1.2.10.2 tls for ( ; ; ) {
2420 1.2.10.2 tls sp->s_namlen = snprintf(sp->s_name, sizeof(sp->s_name),
2421 1.2.10.2 tls ".nfs.%08x.%04x4.4", lticks,
2422 1.2.10.2 tls pid);
2423 1.2.10.2 tls if (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2424 1.2.10.2 tls cnp->cn_thread, NULL))
2425 1.2.10.2 tls break;
2426 1.2.10.2 tls lticks++;
2427 1.2.10.2 tls }
2428 1.2.10.2 tls error = nfs_renameit(dvp, vp, cnp, sp);
2429 1.2.10.2 tls if (error)
2430 1.2.10.2 tls goto bad;
2431 1.2.10.2 tls error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2432 1.2.10.2 tls cnp->cn_thread, &np);
2433 1.2.10.2 tls np->n_sillyrename = sp;
2434 1.2.10.2 tls return (0);
2435 1.2.10.2 tls bad:
2436 1.2.10.2 tls vrele(sp->s_dvp);
2437 1.2.10.2 tls crfree(sp->s_cred);
2438 1.2.10.2 tls free((caddr_t)sp, M_NEWNFSREQ);
2439 1.2.10.2 tls return (error);
2440 1.2.10.2 tls }
2441 1.2.10.2 tls
2442 1.2.10.2 tls /*
2443 1.2.10.2 tls * Look up a file name and optionally either update the file handle or
2444 1.2.10.2 tls * allocate an nfsnode, depending on the value of npp.
2445 1.2.10.2 tls * npp == NULL --> just do the lookup
2446 1.2.10.2 tls * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2447 1.2.10.2 tls * handled too
2448 1.2.10.2 tls * *npp != NULL --> update the file handle in the vnode
2449 1.2.10.2 tls */
2450 1.2.10.2 tls static int
2451 1.2.10.2 tls nfs_lookitup(struct vnode *dvp, char *name, int len, struct ucred *cred,
2452 1.2.10.2 tls struct thread *td, struct nfsnode **npp)
2453 1.2.10.2 tls {
2454 1.2.10.2 tls struct vnode *newvp = NULL, *vp;
2455 1.2.10.2 tls struct nfsnode *np, *dnp = VTONFS(dvp);
2456 1.2.10.2 tls struct nfsfh *nfhp, *onfhp;
2457 1.2.10.2 tls struct nfsvattr nfsva, dnfsva;
2458 1.2.10.2 tls struct componentname cn;
2459 1.2.10.2 tls int error = 0, attrflag, dattrflag;
2460 1.2.10.2 tls u_int hash;
2461 1.2.10.2 tls
2462 1.2.10.2 tls error = nfsrpc_lookup(dvp, name, len, cred, td, &dnfsva, &nfsva,
2463 1.2.10.2 tls &nfhp, &attrflag, &dattrflag, NULL);
2464 1.2.10.2 tls if (dattrflag)
2465 1.2.10.2 tls (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
2466 1.2.10.2 tls if (npp && !error) {
2467 1.2.10.2 tls if (*npp != NULL) {
2468 1.2.10.2 tls np = *npp;
2469 1.2.10.2 tls vp = NFSTOV(np);
2470 1.2.10.2 tls /*
2471 1.2.10.2 tls * For NFSv4, check to see if it is the same name and
2472 1.2.10.2 tls * replace the name, if it is different.
2473 1.2.10.2 tls */
2474 1.2.10.2 tls if (np->n_v4 != NULL && nfsva.na_type == VREG &&
2475 1.2.10.2 tls (np->n_v4->n4_namelen != len ||
2476 1.2.10.2 tls NFSBCMP(name, NFS4NODENAME(np->n_v4), len) ||
2477 1.2.10.2 tls dnp->n_fhp->nfh_len != np->n_v4->n4_fhlen ||
2478 1.2.10.2 tls NFSBCMP(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
2479 1.2.10.2 tls dnp->n_fhp->nfh_len))) {
2480 1.2.10.2 tls #ifdef notdef
2481 1.2.10.2 tls { char nnn[100]; int nnnl;
2482 1.2.10.2 tls nnnl = (len < 100) ? len : 99;
2483 1.2.10.2 tls bcopy(name, nnn, nnnl);
2484 1.2.10.2 tls nnn[nnnl] = '\0';
2485 1.2.10.2 tls printf("replace=%s\n",nnn);
2486 1.2.10.2 tls }
2487 1.2.10.2 tls #endif
2488 1.2.10.2 tls FREE((caddr_t)np->n_v4, M_NFSV4NODE);
2489 1.2.10.2 tls MALLOC(np->n_v4, struct nfsv4node *,
2490 1.2.10.2 tls sizeof (struct nfsv4node) +
2491 1.2.10.2 tls dnp->n_fhp->nfh_len + len - 1,
2492 1.2.10.2 tls M_NFSV4NODE, M_WAITOK);
2493 1.2.10.2 tls np->n_v4->n4_fhlen = dnp->n_fhp->nfh_len;
2494 1.2.10.2 tls np->n_v4->n4_namelen = len;
2495 1.2.10.2 tls NFSBCOPY(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
2496 1.2.10.2 tls dnp->n_fhp->nfh_len);
2497 1.2.10.2 tls NFSBCOPY(name, NFS4NODENAME(np->n_v4), len);
2498 1.2.10.2 tls }
2499 1.2.10.2 tls hash = fnv_32_buf(nfhp->nfh_fh, nfhp->nfh_len,
2500 1.2.10.2 tls FNV1_32_INIT);
2501 1.2.10.2 tls onfhp = np->n_fhp;
2502 1.2.10.2 tls /*
2503 1.2.10.2 tls * Rehash node for new file handle.
2504 1.2.10.2 tls */
2505 1.2.10.2 tls vfs_hash_rehash(vp, hash);
2506 1.2.10.2 tls np->n_fhp = nfhp;
2507 1.2.10.2 tls if (onfhp != NULL)
2508 1.2.10.2 tls FREE((caddr_t)onfhp, M_NFSFH);
2509 1.2.10.2 tls newvp = NFSTOV(np);
2510 1.2.10.2 tls } else if (NFS_CMPFH(dnp, nfhp->nfh_fh, nfhp->nfh_len)) {
2511 1.2.10.2 tls FREE((caddr_t)nfhp, M_NFSFH);
2512 1.2.10.2 tls VREF(dvp);
2513 1.2.10.2 tls newvp = dvp;
2514 1.2.10.2 tls } else {
2515 1.2.10.2 tls cn.cn_nameptr = name;
2516 1.2.10.2 tls cn.cn_namelen = len;
2517 1.2.10.2 tls error = nfscl_nget(dvp->v_mount, dvp, nfhp, &cn, td,
2518 1.2.10.2 tls &np, NULL, LK_EXCLUSIVE);
2519 1.2.10.2 tls if (error)
2520 1.2.10.2 tls return (error);
2521 1.2.10.2 tls newvp = NFSTOV(np);
2522 1.2.10.2 tls }
2523 1.2.10.2 tls if (!attrflag && *npp == NULL) {
2524 1.2.10.2 tls if (newvp == dvp)
2525 1.2.10.2 tls vrele(newvp);
2526 1.2.10.2 tls else
2527 1.2.10.2 tls vput(newvp);
2528 1.2.10.2 tls return (ENOENT);
2529 1.2.10.2 tls }
2530 1.2.10.2 tls if (attrflag)
2531 1.2.10.2 tls (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
2532 1.2.10.2 tls 0, 1);
2533 1.2.10.2 tls }
2534 1.2.10.2 tls if (npp && *npp == NULL) {
2535 1.2.10.2 tls if (error) {
2536 1.2.10.2 tls if (newvp) {
2537 1.2.10.2 tls if (newvp == dvp)
2538 1.2.10.2 tls vrele(newvp);
2539 1.2.10.2 tls else
2540 1.2.10.2 tls vput(newvp);
2541 1.2.10.2 tls }
2542 1.2.10.2 tls } else
2543 1.2.10.2 tls *npp = np;
2544 1.2.10.2 tls }
2545 1.2.10.2 tls if (error && NFS_ISV4(dvp))
2546 1.2.10.2 tls error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2547 1.2.10.2 tls return (error);
2548 1.2.10.2 tls }
2549 1.2.10.2 tls
2550 1.2.10.2 tls /*
2551 1.2.10.2 tls * Nfs Version 3 and 4 commit rpc
2552 1.2.10.2 tls */
2553 1.2.10.2 tls int
2554 1.2.10.2 tls ncl_commit(struct vnode *vp, u_quad_t offset, int cnt, struct ucred *cred,
2555 1.2.10.2 tls struct thread *td)
2556 1.2.10.2 tls {
2557 1.2.10.2 tls struct nfsvattr nfsva;
2558 1.2.10.2 tls struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2559 1.2.10.2 tls int error, attrflag;
2560 1.2.10.2 tls
2561 1.2.10.2 tls mtx_lock(&nmp->nm_mtx);
2562 1.2.10.2 tls if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0) {
2563 1.2.10.2 tls mtx_unlock(&nmp->nm_mtx);
2564 1.2.10.2 tls return (0);
2565 1.2.10.2 tls }
2566 1.2.10.2 tls mtx_unlock(&nmp->nm_mtx);
2567 1.2.10.2 tls error = nfsrpc_commit(vp, offset, cnt, cred, td, &nfsva,
2568 1.2.10.2 tls &attrflag, NULL);
2569 1.2.10.2 tls if (attrflag != 0)
2570 1.2.10.2 tls (void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL,
2571 1.2.10.2 tls 0, 1);
2572 1.2.10.2 tls if (error != 0 && NFS_ISV4(vp))
2573 1.2.10.2 tls error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2574 1.2.10.2 tls return (error);
2575 1.2.10.2 tls }
2576 1.2.10.2 tls
2577 1.2.10.2 tls /*
2578 1.2.10.2 tls * Strategy routine.
2579 1.2.10.2 tls * For async requests when nfsiod(s) are running, queue the request by
2580 1.2.10.2 tls * calling ncl_asyncio(), otherwise just all ncl_doio() to do the
2581 1.2.10.2 tls * request.
2582 1.2.10.2 tls */
2583 1.2.10.2 tls static int
2584 1.2.10.2 tls nfs_strategy(struct vop_strategy_args *ap)
2585 1.2.10.2 tls {
2586 1.2.10.2 tls struct buf *bp = ap->a_bp;
2587 1.2.10.2 tls struct ucred *cr;
2588 1.2.10.2 tls
2589 1.2.10.2 tls KASSERT(!(bp->b_flags & B_DONE),
2590 1.2.10.2 tls ("nfs_strategy: buffer %p unexpectedly marked B_DONE", bp));
2591 1.2.10.2 tls BUF_ASSERT_HELD(bp);
2592 1.2.10.2 tls
2593 1.2.10.2 tls if (bp->b_iocmd == BIO_READ)
2594 1.2.10.2 tls cr = bp->b_rcred;
2595 1.2.10.2 tls else
2596 1.2.10.2 tls cr = bp->b_wcred;
2597 1.2.10.2 tls
2598 1.2.10.2 tls /*
2599 1.2.10.2 tls * If the op is asynchronous and an i/o daemon is waiting
2600 1.2.10.2 tls * queue the request, wake it up and wait for completion
2601 1.2.10.2 tls * otherwise just do it ourselves.
2602 1.2.10.2 tls */
2603 1.2.10.2 tls if ((bp->b_flags & B_ASYNC) == 0 ||
2604 1.2.10.2 tls ncl_asyncio(VFSTONFS(ap->a_vp->v_mount), bp, NOCRED, curthread))
2605 1.2.10.2 tls (void) ncl_doio(ap->a_vp, bp, cr, curthread, 1);
2606 1.2.10.2 tls return (0);
2607 1.2.10.2 tls }
2608 1.2.10.2 tls
2609 1.2.10.2 tls /*
2610 1.2.10.2 tls * fsync vnode op. Just call ncl_flush() with commit == 1.
2611 1.2.10.2 tls */
2612 1.2.10.2 tls /* ARGSUSED */
2613 1.2.10.2 tls static int
2614 1.2.10.2 tls nfs_fsync(struct vop_fsync_args *ap)
2615 1.2.10.2 tls {
2616 1.2.10.2 tls
2617 1.2.10.2 tls if (ap->a_vp->v_type != VREG) {
2618 1.2.10.2 tls /*
2619 1.2.10.2 tls * For NFS, metadata is changed synchronously on the server,
2620 1.2.10.2 tls * so there is nothing to flush. Also, ncl_flush() clears
2621 1.2.10.2 tls * the NMODIFIED flag and that shouldn't be done here for
2622 1.2.10.2 tls * directories.
2623 1.2.10.2 tls */
2624 1.2.10.2 tls return (0);
2625 1.2.10.2 tls }
2626 1.2.10.2 tls return (ncl_flush(ap->a_vp, ap->a_waitfor, NULL, ap->a_td, 1, 0));
2627 1.2.10.2 tls }
2628 1.2.10.2 tls
2629 1.2.10.2 tls /*
2630 1.2.10.2 tls * Flush all the blocks associated with a vnode.
2631 1.2.10.2 tls * Walk through the buffer pool and push any dirty pages
2632 1.2.10.2 tls * associated with the vnode.
2633 1.2.10.2 tls * If the called_from_renewthread argument is TRUE, it has been called
2634 1.2.10.2 tls * from the NFSv4 renew thread and, as such, cannot block indefinitely
2635 1.2.10.2 tls * waiting for a buffer write to complete.
2636 1.2.10.2 tls */
2637 1.2.10.2 tls int
2638 1.2.10.2 tls ncl_flush(struct vnode *vp, int waitfor, struct ucred *cred, struct thread *td,
2639 1.2.10.2 tls int commit, int called_from_renewthread)
2640 1.2.10.2 tls {
2641 1.2.10.2 tls struct nfsnode *np = VTONFS(vp);
2642 1.2.10.2 tls struct buf *bp;
2643 1.2.10.2 tls int i;
2644 1.2.10.2 tls struct buf *nbp;
2645 1.2.10.2 tls struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2646 1.2.10.2 tls int error = 0, slptimeo = 0, slpflag = 0, retv, bvecpos;
2647 1.2.10.2 tls int passone = 1, trycnt = 0;
2648 1.2.10.2 tls u_quad_t off, endoff, toff;
2649 1.2.10.2 tls struct ucred* wcred = NULL;
2650 1.2.10.2 tls struct buf **bvec = NULL;
2651 1.2.10.2 tls struct bufobj *bo;
2652 1.2.10.2 tls #ifndef NFS_COMMITBVECSIZ
2653 1.2.10.2 tls #define NFS_COMMITBVECSIZ 20
2654 1.2.10.2 tls #endif
2655 1.2.10.2 tls struct buf *bvec_on_stack[NFS_COMMITBVECSIZ];
2656 1.2.10.2 tls int bvecsize = 0, bveccount;
2657 1.2.10.2 tls
2658 1.2.10.2 tls if (called_from_renewthread != 0)
2659 1.2.10.2 tls slptimeo = hz;
2660 1.2.10.2 tls if (nmp->nm_flag & NFSMNT_INT)
2661 1.2.10.2 tls slpflag = PCATCH;
2662 1.2.10.2 tls if (!commit)
2663 1.2.10.2 tls passone = 0;
2664 1.2.10.2 tls bo = &vp->v_bufobj;
2665 1.2.10.2 tls /*
2666 1.2.10.2 tls * A b_flags == (B_DELWRI | B_NEEDCOMMIT) block has been written to the
2667 1.2.10.2 tls * server, but has not been committed to stable storage on the server
2668 1.2.10.2 tls * yet. On the first pass, the byte range is worked out and the commit
2669 1.2.10.2 tls * rpc is done. On the second pass, ncl_writebp() is called to do the
2670 1.2.10.2 tls * job.
2671 1.2.10.2 tls */
2672 1.2.10.2 tls again:
2673 1.2.10.2 tls off = (u_quad_t)-1;
2674 1.2.10.2 tls endoff = 0;
2675 1.2.10.2 tls bvecpos = 0;
2676 1.2.10.2 tls if (NFS_ISV34(vp) && commit) {
2677 1.2.10.2 tls if (bvec != NULL && bvec != bvec_on_stack)
2678 1.2.10.2 tls free(bvec, M_TEMP);
2679 1.2.10.2 tls /*
2680 1.2.10.2 tls * Count up how many buffers waiting for a commit.
2681 1.2.10.2 tls */
2682 1.2.10.2 tls bveccount = 0;
2683 1.2.10.2 tls BO_LOCK(bo);
2684 1.2.10.2 tls TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
2685 1.2.10.2 tls if (!BUF_ISLOCKED(bp) &&
2686 1.2.10.2 tls (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
2687 1.2.10.2 tls == (B_DELWRI | B_NEEDCOMMIT))
2688 1.2.10.2 tls bveccount++;
2689 1.2.10.2 tls }
2690 1.2.10.2 tls /*
2691 1.2.10.2 tls * Allocate space to remember the list of bufs to commit. It is
2692 1.2.10.2 tls * important to use M_NOWAIT here to avoid a race with nfs_write.
2693 1.2.10.2 tls * If we can't get memory (for whatever reason), we will end up
2694 1.2.10.2 tls * committing the buffers one-by-one in the loop below.
2695 1.2.10.2 tls */
2696 1.2.10.2 tls if (bveccount > NFS_COMMITBVECSIZ) {
2697 1.2.10.2 tls /*
2698 1.2.10.2 tls * Release the vnode interlock to avoid a lock
2699 1.2.10.2 tls * order reversal.
2700 1.2.10.2 tls */
2701 1.2.10.2 tls BO_UNLOCK(bo);
2702 1.2.10.2 tls bvec = (struct buf **)
2703 1.2.10.2 tls malloc(bveccount * sizeof(struct buf *),
2704 1.2.10.2 tls M_TEMP, M_NOWAIT);
2705 1.2.10.2 tls BO_LOCK(bo);
2706 1.2.10.2 tls if (bvec == NULL) {
2707 1.2.10.2 tls bvec = bvec_on_stack;
2708 1.2.10.2 tls bvecsize = NFS_COMMITBVECSIZ;
2709 1.2.10.2 tls } else
2710 1.2.10.2 tls bvecsize = bveccount;
2711 1.2.10.2 tls } else {
2712 1.2.10.2 tls bvec = bvec_on_stack;
2713 1.2.10.2 tls bvecsize = NFS_COMMITBVECSIZ;
2714 1.2.10.2 tls }
2715 1.2.10.2 tls TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
2716 1.2.10.2 tls if (bvecpos >= bvecsize)
2717 1.2.10.2 tls break;
2718 1.2.10.2 tls if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
2719 1.2.10.2 tls nbp = TAILQ_NEXT(bp, b_bobufs);
2720 1.2.10.2 tls continue;
2721 1.2.10.2 tls }
2722 1.2.10.2 tls if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
2723 1.2.10.2 tls (B_DELWRI | B_NEEDCOMMIT)) {
2724 1.2.10.2 tls BUF_UNLOCK(bp);
2725 1.2.10.2 tls nbp = TAILQ_NEXT(bp, b_bobufs);
2726 1.2.10.2 tls continue;
2727 1.2.10.2 tls }
2728 1.2.10.2 tls BO_UNLOCK(bo);
2729 1.2.10.2 tls bremfree(bp);
2730 1.2.10.2 tls /*
2731 1.2.10.2 tls * Work out if all buffers are using the same cred
2732 1.2.10.2 tls * so we can deal with them all with one commit.
2733 1.2.10.2 tls *
2734 1.2.10.2 tls * NOTE: we are not clearing B_DONE here, so we have
2735 1.2.10.2 tls * to do it later on in this routine if we intend to
2736 1.2.10.2 tls * initiate I/O on the bp.
2737 1.2.10.2 tls *
2738 1.2.10.2 tls * Note: to avoid loopback deadlocks, we do not
2739 1.2.10.2 tls * assign b_runningbufspace.
2740 1.2.10.2 tls */
2741 1.2.10.2 tls if (wcred == NULL)
2742 1.2.10.2 tls wcred = bp->b_wcred;
2743 1.2.10.2 tls else if (wcred != bp->b_wcred)
2744 1.2.10.2 tls wcred = NOCRED;
2745 1.2.10.2 tls vfs_busy_pages(bp, 1);
2746 1.2.10.2 tls
2747 1.2.10.2 tls BO_LOCK(bo);
2748 1.2.10.2 tls /*
2749 1.2.10.2 tls * bp is protected by being locked, but nbp is not
2750 1.2.10.2 tls * and vfs_busy_pages() may sleep. We have to
2751 1.2.10.2 tls * recalculate nbp.
2752 1.2.10.2 tls */
2753 1.2.10.2 tls nbp = TAILQ_NEXT(bp, b_bobufs);
2754 1.2.10.2 tls
2755 1.2.10.2 tls /*
2756 1.2.10.2 tls * A list of these buffers is kept so that the
2757 1.2.10.2 tls * second loop knows which buffers have actually
2758 1.2.10.2 tls * been committed. This is necessary, since there
2759 1.2.10.2 tls * may be a race between the commit rpc and new
2760 1.2.10.2 tls * uncommitted writes on the file.
2761 1.2.10.2 tls */
2762 1.2.10.2 tls bvec[bvecpos++] = bp;
2763 1.2.10.2 tls toff = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2764 1.2.10.2 tls bp->b_dirtyoff;
2765 1.2.10.2 tls if (toff < off)
2766 1.2.10.2 tls off = toff;
2767 1.2.10.2 tls toff += (u_quad_t)(bp->b_dirtyend - bp->b_dirtyoff);
2768 1.2.10.2 tls if (toff > endoff)
2769 1.2.10.2 tls endoff = toff;
2770 1.2.10.2 tls }
2771 1.2.10.2 tls BO_UNLOCK(bo);
2772 1.2.10.2 tls }
2773 1.2.10.2 tls if (bvecpos > 0) {
2774 1.2.10.2 tls /*
2775 1.2.10.2 tls * Commit data on the server, as required.
2776 1.2.10.2 tls * If all bufs are using the same wcred, then use that with
2777 1.2.10.2 tls * one call for all of them, otherwise commit each one
2778 1.2.10.2 tls * separately.
2779 1.2.10.2 tls */
2780 1.2.10.2 tls if (wcred != NOCRED)
2781 1.2.10.2 tls retv = ncl_commit(vp, off, (int)(endoff - off),
2782 1.2.10.2 tls wcred, td);
2783 1.2.10.2 tls else {
2784 1.2.10.2 tls retv = 0;
2785 1.2.10.2 tls for (i = 0; i < bvecpos; i++) {
2786 1.2.10.2 tls off_t off, size;
2787 1.2.10.2 tls bp = bvec[i];
2788 1.2.10.2 tls off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2789 1.2.10.2 tls bp->b_dirtyoff;
2790 1.2.10.2 tls size = (u_quad_t)(bp->b_dirtyend
2791 1.2.10.2 tls - bp->b_dirtyoff);
2792 1.2.10.2 tls retv = ncl_commit(vp, off, (int)size,
2793 1.2.10.2 tls bp->b_wcred, td);
2794 1.2.10.2 tls if (retv) break;
2795 1.2.10.2 tls }
2796 1.2.10.2 tls }
2797 1.2.10.2 tls
2798 1.2.10.2 tls if (retv == NFSERR_STALEWRITEVERF)
2799 1.2.10.2 tls ncl_clearcommit(vp->v_mount);
2800 1.2.10.2 tls
2801 1.2.10.2 tls /*
2802 1.2.10.2 tls * Now, either mark the blocks I/O done or mark the
2803 1.2.10.2 tls * blocks dirty, depending on whether the commit
2804 1.2.10.2 tls * succeeded.
2805 1.2.10.2 tls */
2806 1.2.10.2 tls for (i = 0; i < bvecpos; i++) {
2807 1.2.10.2 tls bp = bvec[i];
2808 1.2.10.2 tls bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
2809 1.2.10.2 tls if (retv) {
2810 1.2.10.2 tls /*
2811 1.2.10.2 tls * Error, leave B_DELWRI intact
2812 1.2.10.2 tls */
2813 1.2.10.2 tls vfs_unbusy_pages(bp);
2814 1.2.10.2 tls brelse(bp);
2815 1.2.10.2 tls } else {
2816 1.2.10.2 tls /*
2817 1.2.10.2 tls * Success, remove B_DELWRI ( bundirty() ).
2818 1.2.10.2 tls *
2819 1.2.10.2 tls * b_dirtyoff/b_dirtyend seem to be NFS
2820 1.2.10.2 tls * specific. We should probably move that
2821 1.2.10.2 tls * into bundirty(). XXX
2822 1.2.10.2 tls */
2823 1.2.10.2 tls bufobj_wref(bo);
2824 1.2.10.2 tls bp->b_flags |= B_ASYNC;
2825 1.2.10.2 tls bundirty(bp);
2826 1.2.10.2 tls bp->b_flags &= ~B_DONE;
2827 1.2.10.2 tls bp->b_ioflags &= ~BIO_ERROR;
2828 1.2.10.2 tls bp->b_dirtyoff = bp->b_dirtyend = 0;
2829 1.2.10.2 tls bufdone(bp);
2830 1.2.10.2 tls }
2831 1.2.10.2 tls }
2832 1.2.10.2 tls }
2833 1.2.10.2 tls
2834 1.2.10.2 tls /*
2835 1.2.10.2 tls * Start/do any write(s) that are required.
2836 1.2.10.2 tls */
2837 1.2.10.2 tls loop:
2838 1.2.10.2 tls BO_LOCK(bo);
2839 1.2.10.2 tls TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
2840 1.2.10.2 tls if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
2841 1.2.10.2 tls if (waitfor != MNT_WAIT || passone)
2842 1.2.10.2 tls continue;
2843 1.2.10.2 tls
2844 1.2.10.2 tls error = BUF_TIMELOCK(bp,
2845 1.2.10.2 tls LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
2846 1.2.10.2 tls BO_LOCKPTR(bo), "nfsfsync", slpflag, slptimeo);
2847 1.2.10.2 tls if (error == 0) {
2848 1.2.10.2 tls BUF_UNLOCK(bp);
2849 1.2.10.2 tls goto loop;
2850 1.2.10.2 tls }
2851 1.2.10.2 tls if (error == ENOLCK) {
2852 1.2.10.2 tls error = 0;
2853 1.2.10.2 tls goto loop;
2854 1.2.10.2 tls }
2855 1.2.10.2 tls if (called_from_renewthread != 0) {
2856 1.2.10.2 tls /*
2857 1.2.10.2 tls * Return EIO so the flush will be retried
2858 1.2.10.2 tls * later.
2859 1.2.10.2 tls */
2860 1.2.10.2 tls error = EIO;
2861 1.2.10.2 tls goto done;
2862 1.2.10.2 tls }
2863 1.2.10.2 tls if (newnfs_sigintr(nmp, td)) {
2864 1.2.10.2 tls error = EINTR;
2865 1.2.10.2 tls goto done;
2866 1.2.10.2 tls }
2867 1.2.10.2 tls if (slpflag == PCATCH) {
2868 1.2.10.2 tls slpflag = 0;
2869 1.2.10.2 tls slptimeo = 2 * hz;
2870 1.2.10.2 tls }
2871 1.2.10.2 tls goto loop;
2872 1.2.10.2 tls }
2873 1.2.10.2 tls if ((bp->b_flags & B_DELWRI) == 0)
2874 1.2.10.2 tls panic("nfs_fsync: not dirty");
2875 1.2.10.2 tls if ((passone || !commit) && (bp->b_flags & B_NEEDCOMMIT)) {
2876 1.2.10.2 tls BUF_UNLOCK(bp);
2877 1.2.10.2 tls continue;
2878 1.2.10.2 tls }
2879 1.2.10.2 tls BO_UNLOCK(bo);
2880 1.2.10.2 tls bremfree(bp);
2881 1.2.10.2 tls if (passone || !commit)
2882 1.2.10.2 tls bp->b_flags |= B_ASYNC;
2883 1.2.10.2 tls else
2884 1.2.10.2 tls bp->b_flags |= B_ASYNC;
2885 1.2.10.2 tls bwrite(bp);
2886 1.2.10.2 tls if (newnfs_sigintr(nmp, td)) {
2887 1.2.10.2 tls error = EINTR;
2888 1.2.10.2 tls goto done;
2889 1.2.10.2 tls }
2890 1.2.10.2 tls goto loop;
2891 1.2.10.2 tls }
2892 1.2.10.2 tls if (passone) {
2893 1.2.10.2 tls passone = 0;
2894 1.2.10.2 tls BO_UNLOCK(bo);
2895 1.2.10.2 tls goto again;
2896 1.2.10.2 tls }
2897 1.2.10.2 tls if (waitfor == MNT_WAIT) {
2898 1.2.10.2 tls while (bo->bo_numoutput) {
2899 1.2.10.2 tls error = bufobj_wwait(bo, slpflag, slptimeo);
2900 1.2.10.2 tls if (error) {
2901 1.2.10.2 tls BO_UNLOCK(bo);
2902 1.2.10.2 tls if (called_from_renewthread != 0) {
2903 1.2.10.2 tls /*
2904 1.2.10.2 tls * Return EIO so that the flush will be
2905 1.2.10.2 tls * retried later.
2906 1.2.10.2 tls */
2907 1.2.10.2 tls error = EIO;
2908 1.2.10.2 tls goto done;
2909 1.2.10.2 tls }
2910 1.2.10.2 tls error = newnfs_sigintr(nmp, td);
2911 1.2.10.2 tls if (error)
2912 1.2.10.2 tls goto done;
2913 1.2.10.2 tls if (slpflag == PCATCH) {
2914 1.2.10.2 tls slpflag = 0;
2915 1.2.10.2 tls slptimeo = 2 * hz;
2916 1.2.10.2 tls }
2917 1.2.10.2 tls BO_LOCK(bo);
2918 1.2.10.2 tls }
2919 1.2.10.2 tls }
2920 1.2.10.2 tls if (bo->bo_dirty.bv_cnt != 0 && commit) {
2921 1.2.10.2 tls BO_UNLOCK(bo);
2922 1.2.10.2 tls goto loop;
2923 1.2.10.2 tls }
2924 1.2.10.2 tls /*
2925 1.2.10.2 tls * Wait for all the async IO requests to drain
2926 1.2.10.2 tls */
2927 1.2.10.2 tls BO_UNLOCK(bo);
2928 1.2.10.2 tls mtx_lock(&np->n_mtx);
2929 1.2.10.2 tls while (np->n_directio_asyncwr > 0) {
2930 1.2.10.2 tls np->n_flag |= NFSYNCWAIT;
2931 1.2.10.2 tls error = newnfs_msleep(td, &np->n_directio_asyncwr,
2932 1.2.10.2 tls &np->n_mtx, slpflag | (PRIBIO + 1),
2933 1.2.10.2 tls "nfsfsync", 0);
2934 1.2.10.2 tls if (error) {
2935 1.2.10.2 tls if (newnfs_sigintr(nmp, td)) {
2936 1.2.10.2 tls mtx_unlock(&np->n_mtx);
2937 1.2.10.2 tls error = EINTR;
2938 1.2.10.2 tls goto done;
2939 1.2.10.2 tls }
2940 1.2.10.2 tls }
2941 1.2.10.2 tls }
2942 1.2.10.2 tls mtx_unlock(&np->n_mtx);
2943 1.2.10.2 tls } else
2944 1.2.10.2 tls BO_UNLOCK(bo);
2945 1.2.10.2 tls if (NFSHASPNFS(nmp)) {
2946 1.2.10.2 tls nfscl_layoutcommit(vp, td);
2947 1.2.10.2 tls /*
2948 1.2.10.2 tls * Invalidate the attribute cache, since writes to a DS
2949 1.2.10.2 tls * won't update the size attribute.
2950 1.2.10.2 tls */
2951 1.2.10.2 tls mtx_lock(&np->n_mtx);
2952 1.2.10.2 tls np->n_attrstamp = 0;
2953 1.2.10.2 tls } else
2954 1.2.10.2 tls mtx_lock(&np->n_mtx);
2955 1.2.10.2 tls if (np->n_flag & NWRITEERR) {
2956 1.2.10.2 tls error = np->n_error;
2957 1.2.10.2 tls np->n_flag &= ~NWRITEERR;
2958 1.2.10.2 tls }
2959 1.2.10.2 tls if (commit && bo->bo_dirty.bv_cnt == 0 &&
2960 1.2.10.2 tls bo->bo_numoutput == 0 && np->n_directio_asyncwr == 0)
2961 1.2.10.2 tls np->n_flag &= ~NMODIFIED;
2962 1.2.10.2 tls mtx_unlock(&np->n_mtx);
2963 1.2.10.2 tls done:
2964 1.2.10.2 tls if (bvec != NULL && bvec != bvec_on_stack)
2965 1.2.10.2 tls free(bvec, M_TEMP);
2966 1.2.10.2 tls if (error == 0 && commit != 0 && waitfor == MNT_WAIT &&
2967 1.2.10.2 tls (bo->bo_dirty.bv_cnt != 0 || bo->bo_numoutput != 0 ||
2968 1.2.10.2 tls np->n_directio_asyncwr != 0) && trycnt++ < 5) {
2969 1.2.10.2 tls /* try, try again... */
2970 1.2.10.2 tls passone = 1;
2971 1.2.10.2 tls wcred = NULL;
2972 1.2.10.2 tls bvec = NULL;
2973 1.2.10.2 tls bvecsize = 0;
2974 1.2.10.2 tls printf("try%d\n", trycnt);
2975 1.2.10.2 tls goto again;
2976 1.2.10.2 tls }
2977 1.2.10.2 tls return (error);
2978 1.2.10.2 tls }
2979 1.2.10.2 tls
2980 1.2.10.2 tls /*
2981 1.2.10.2 tls * NFS advisory byte-level locks.
2982 1.2.10.2 tls */
2983 1.2.10.2 tls static int
2984 1.2.10.2 tls nfs_advlock(struct vop_advlock_args *ap)
2985 1.2.10.2 tls {
2986 1.2.10.2 tls struct vnode *vp = ap->a_vp;
2987 1.2.10.2 tls struct ucred *cred;
2988 1.2.10.2 tls struct nfsnode *np = VTONFS(ap->a_vp);
2989 1.2.10.2 tls struct proc *p = (struct proc *)ap->a_id;
2990 1.2.10.2 tls struct thread *td = curthread; /* XXX */
2991 1.2.10.2 tls struct vattr va;
2992 1.2.10.2 tls int ret, error = EOPNOTSUPP;
2993 1.2.10.2 tls u_quad_t size;
2994 1.2.10.2 tls
2995 1.2.10.2 tls if (NFS_ISV4(vp) && (ap->a_flags & (F_POSIX | F_FLOCK)) != 0) {
2996 1.2.10.2 tls if (vp->v_type != VREG)
2997 1.2.10.2 tls return (EINVAL);
2998 1.2.10.2 tls if ((ap->a_flags & F_POSIX) != 0)
2999 1.2.10.2 tls cred = p->p_ucred;
3000 1.2.10.2 tls else
3001 1.2.10.2 tls cred = td->td_ucred;
3002 1.2.10.2 tls NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
3003 1.2.10.2 tls if (vp->v_iflag & VI_DOOMED) {
3004 1.2.10.2 tls NFSVOPUNLOCK(vp, 0);
3005 1.2.10.2 tls return (EBADF);
3006 1.2.10.2 tls }
3007 1.2.10.2 tls
3008 1.2.10.2 tls /*
3009 1.2.10.2 tls * If this is unlocking a write locked region, flush and
3010 1.2.10.2 tls * commit them before unlocking. This is required by
3011 1.2.10.2 tls * RFC3530 Sec. 9.3.2.
3012 1.2.10.2 tls */
3013 1.2.10.2 tls if (ap->a_op == F_UNLCK &&
3014 1.2.10.2 tls nfscl_checkwritelocked(vp, ap->a_fl, cred, td, ap->a_id,
3015 1.2.10.2 tls ap->a_flags))
3016 1.2.10.2 tls (void) ncl_flush(vp, MNT_WAIT, cred, td, 1, 0);
3017 1.2.10.2 tls
3018 1.2.10.2 tls /*
3019 1.2.10.2 tls * Loop around doing the lock op, while a blocking lock
3020 1.2.10.2 tls * must wait for the lock op to succeed.
3021 1.2.10.2 tls */
3022 1.2.10.2 tls do {
3023 1.2.10.2 tls ret = nfsrpc_advlock(vp, np->n_size, ap->a_op,
3024 1.2.10.2 tls ap->a_fl, 0, cred, td, ap->a_id, ap->a_flags);
3025 1.2.10.2 tls if (ret == NFSERR_DENIED && (ap->a_flags & F_WAIT) &&
3026 1.2.10.2 tls ap->a_op == F_SETLK) {
3027 1.2.10.2 tls NFSVOPUNLOCK(vp, 0);
3028 1.2.10.2 tls error = nfs_catnap(PZERO | PCATCH, ret,
3029 1.2.10.2 tls "ncladvl");
3030 1.2.10.2 tls if (error)
3031 1.2.10.2 tls return (EINTR);
3032 1.2.10.2 tls NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
3033 1.2.10.2 tls if (vp->v_iflag & VI_DOOMED) {
3034 1.2.10.2 tls NFSVOPUNLOCK(vp, 0);
3035 1.2.10.2 tls return (EBADF);
3036 1.2.10.2 tls }
3037 1.2.10.2 tls }
3038 1.2.10.2 tls } while (ret == NFSERR_DENIED && (ap->a_flags & F_WAIT) &&
3039 1.2.10.2 tls ap->a_op == F_SETLK);
3040 1.2.10.2 tls if (ret == NFSERR_DENIED) {
3041 1.2.10.2 tls NFSVOPUNLOCK(vp, 0);
3042 1.2.10.2 tls return (EAGAIN);
3043 1.2.10.2 tls } else if (ret == EINVAL || ret == EBADF || ret == EINTR) {
3044 1.2.10.2 tls NFSVOPUNLOCK(vp, 0);
3045 1.2.10.2 tls return (ret);
3046 1.2.10.2 tls } else if (ret != 0) {
3047 1.2.10.2 tls NFSVOPUNLOCK(vp, 0);
3048 1.2.10.2 tls return (EACCES);
3049 1.2.10.2 tls }
3050 1.2.10.2 tls
3051 1.2.10.2 tls /*
3052 1.2.10.2 tls * Now, if we just got a lock, invalidate data in the buffer
3053 1.2.10.2 tls * cache, as required, so that the coherency conforms with
3054 1.2.10.2 tls * RFC3530 Sec. 9.3.2.
3055 1.2.10.2 tls */
3056 1.2.10.2 tls if (ap->a_op == F_SETLK) {
3057 1.2.10.2 tls if ((np->n_flag & NMODIFIED) == 0) {
3058 1.2.10.2 tls np->n_attrstamp = 0;
3059 1.2.10.2 tls KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
3060 1.2.10.2 tls ret = VOP_GETATTR(vp, &va, cred);
3061 1.2.10.2 tls }
3062 1.2.10.2 tls if ((np->n_flag & NMODIFIED) || ret ||
3063 1.2.10.2 tls np->n_change != va.va_filerev) {
3064 1.2.10.2 tls (void) ncl_vinvalbuf(vp, V_SAVE, td, 1);
3065 1.2.10.2 tls np->n_attrstamp = 0;
3066 1.2.10.2 tls KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
3067 1.2.10.2 tls ret = VOP_GETATTR(vp, &va, cred);
3068 1.2.10.2 tls if (!ret) {
3069 1.2.10.2 tls np->n_mtime = va.va_mtime;
3070 1.2.10.2 tls np->n_change = va.va_filerev;
3071 1.2.10.2 tls }
3072 1.2.10.2 tls }
3073 1.2.10.3 jdolecek /* Mark that a file lock has been acquired. */
3074 1.2.10.3 jdolecek mtx_lock(&np->n_mtx);
3075 1.2.10.3 jdolecek np->n_flag |= NHASBEENLOCKED;
3076 1.2.10.3 jdolecek mtx_unlock(&np->n_mtx);
3077 1.2.10.2 tls }
3078 1.2.10.2 tls NFSVOPUNLOCK(vp, 0);
3079 1.2.10.2 tls return (0);
3080 1.2.10.2 tls } else if (!NFS_ISV4(vp)) {
3081 1.2.10.2 tls error = NFSVOPLOCK(vp, LK_SHARED);
3082 1.2.10.2 tls if (error)
3083 1.2.10.2 tls return (error);
3084 1.2.10.2 tls if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) {
3085 1.2.10.2 tls size = VTONFS(vp)->n_size;
3086 1.2.10.2 tls NFSVOPUNLOCK(vp, 0);
3087 1.2.10.2 tls error = lf_advlock(ap, &(vp->v_lockf), size);
3088 1.2.10.2 tls } else {
3089 1.2.10.2 tls if (nfs_advlock_p != NULL)
3090 1.2.10.2 tls error = nfs_advlock_p(ap);
3091 1.2.10.2 tls else {
3092 1.2.10.2 tls NFSVOPUNLOCK(vp, 0);
3093 1.2.10.2 tls error = ENOLCK;
3094 1.2.10.2 tls }
3095 1.2.10.2 tls }
3096 1.2.10.3 jdolecek if (error == 0 && ap->a_op == F_SETLK) {
3097 1.2.10.3 jdolecek error = NFSVOPLOCK(vp, LK_SHARED);
3098 1.2.10.3 jdolecek if (error == 0) {
3099 1.2.10.3 jdolecek /* Mark that a file lock has been acquired. */
3100 1.2.10.3 jdolecek mtx_lock(&np->n_mtx);
3101 1.2.10.3 jdolecek np->n_flag |= NHASBEENLOCKED;
3102 1.2.10.3 jdolecek mtx_unlock(&np->n_mtx);
3103 1.2.10.3 jdolecek NFSVOPUNLOCK(vp, 0);
3104 1.2.10.3 jdolecek }
3105 1.2.10.3 jdolecek }
3106 1.2.10.2 tls }
3107 1.2.10.2 tls return (error);
3108 1.2.10.2 tls }
3109 1.2.10.2 tls
3110 1.2.10.2 tls /*
3111 1.2.10.2 tls * NFS advisory byte-level locks.
3112 1.2.10.2 tls */
3113 1.2.10.2 tls static int
3114 1.2.10.2 tls nfs_advlockasync(struct vop_advlockasync_args *ap)
3115 1.2.10.2 tls {
3116 1.2.10.2 tls struct vnode *vp = ap->a_vp;
3117 1.2.10.2 tls u_quad_t size;
3118 1.2.10.2 tls int error;
3119 1.2.10.2 tls
3120 1.2.10.2 tls if (NFS_ISV4(vp))
3121 1.2.10.2 tls return (EOPNOTSUPP);
3122 1.2.10.2 tls error = NFSVOPLOCK(vp, LK_SHARED);
3123 1.2.10.2 tls if (error)
3124 1.2.10.2 tls return (error);
3125 1.2.10.2 tls if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) {
3126 1.2.10.2 tls size = VTONFS(vp)->n_size;
3127 1.2.10.2 tls NFSVOPUNLOCK(vp, 0);
3128 1.2.10.2 tls error = lf_advlockasync(ap, &(vp->v_lockf), size);
3129 1.2.10.2 tls } else {
3130 1.2.10.2 tls NFSVOPUNLOCK(vp, 0);
3131 1.2.10.2 tls error = EOPNOTSUPP;
3132 1.2.10.2 tls }
3133 1.2.10.2 tls return (error);
3134 1.2.10.2 tls }
3135 1.2.10.2 tls
3136 1.2.10.2 tls /*
3137 1.2.10.2 tls * Print out the contents of an nfsnode.
3138 1.2.10.2 tls */
3139 1.2.10.2 tls static int
3140 1.2.10.2 tls nfs_print(struct vop_print_args *ap)
3141 1.2.10.2 tls {
3142 1.2.10.2 tls struct vnode *vp = ap->a_vp;
3143 1.2.10.2 tls struct nfsnode *np = VTONFS(vp);
3144 1.2.10.2 tls
3145 1.2.10.3 jdolecek printf("\tfileid %ld fsid 0x%x", np->n_vattr.na_fileid,
3146 1.2.10.3 jdolecek np->n_vattr.na_fsid);
3147 1.2.10.2 tls if (vp->v_type == VFIFO)
3148 1.2.10.2 tls fifo_printinfo(vp);
3149 1.2.10.2 tls printf("\n");
3150 1.2.10.2 tls return (0);
3151 1.2.10.2 tls }
3152 1.2.10.2 tls
3153 1.2.10.2 tls /*
3154 1.2.10.2 tls * This is the "real" nfs::bwrite(struct buf*).
3155 1.2.10.2 tls * We set B_CACHE if this is a VMIO buffer.
3156 1.2.10.2 tls */
3157 1.2.10.2 tls int
3158 1.2.10.2 tls ncl_writebp(struct buf *bp, int force __unused, struct thread *td)
3159 1.2.10.2 tls {
3160 1.2.10.2 tls int s;
3161 1.2.10.2 tls int oldflags = bp->b_flags;
3162 1.2.10.2 tls #if 0
3163 1.2.10.2 tls int retv = 1;
3164 1.2.10.2 tls off_t off;
3165 1.2.10.2 tls #endif
3166 1.2.10.2 tls
3167 1.2.10.2 tls BUF_ASSERT_HELD(bp);
3168 1.2.10.2 tls
3169 1.2.10.2 tls if (bp->b_flags & B_INVAL) {
3170 1.2.10.2 tls brelse(bp);
3171 1.2.10.2 tls return(0);
3172 1.2.10.2 tls }
3173 1.2.10.2 tls
3174 1.2.10.2 tls bp->b_flags |= B_CACHE;
3175 1.2.10.2 tls
3176 1.2.10.2 tls /*
3177 1.2.10.2 tls * Undirty the bp. We will redirty it later if the I/O fails.
3178 1.2.10.2 tls */
3179 1.2.10.2 tls
3180 1.2.10.2 tls s = splbio();
3181 1.2.10.2 tls bundirty(bp);
3182 1.2.10.2 tls bp->b_flags &= ~B_DONE;
3183 1.2.10.2 tls bp->b_ioflags &= ~BIO_ERROR;
3184 1.2.10.2 tls bp->b_iocmd = BIO_WRITE;
3185 1.2.10.2 tls
3186 1.2.10.2 tls bufobj_wref(bp->b_bufobj);
3187 1.2.10.2 tls curthread->td_ru.ru_oublock++;
3188 1.2.10.2 tls splx(s);
3189 1.2.10.2 tls
3190 1.2.10.2 tls /*
3191 1.2.10.2 tls * Note: to avoid loopback deadlocks, we do not
3192 1.2.10.2 tls * assign b_runningbufspace.
3193 1.2.10.2 tls */
3194 1.2.10.2 tls vfs_busy_pages(bp, 1);
3195 1.2.10.2 tls
3196 1.2.10.2 tls BUF_KERNPROC(bp);
3197 1.2.10.2 tls bp->b_iooffset = dbtob(bp->b_blkno);
3198 1.2.10.2 tls bstrategy(bp);
3199 1.2.10.2 tls
3200 1.2.10.2 tls if( (oldflags & B_ASYNC) == 0) {
3201 1.2.10.2 tls int rtval = bufwait(bp);
3202 1.2.10.2 tls
3203 1.2.10.2 tls if (oldflags & B_DELWRI) {
3204 1.2.10.2 tls s = splbio();
3205 1.2.10.2 tls reassignbuf(bp);
3206 1.2.10.2 tls splx(s);
3207 1.2.10.2 tls }
3208 1.2.10.2 tls brelse(bp);
3209 1.2.10.2 tls return (rtval);
3210 1.2.10.2 tls }
3211 1.2.10.2 tls
3212 1.2.10.2 tls return (0);
3213 1.2.10.2 tls }
3214 1.2.10.2 tls
3215 1.2.10.2 tls /*
3216 1.2.10.2 tls * nfs special file access vnode op.
3217 1.2.10.2 tls * Essentially just get vattr and then imitate iaccess() since the device is
3218 1.2.10.2 tls * local to the client.
3219 1.2.10.2 tls */
3220 1.2.10.2 tls static int
3221 1.2.10.2 tls nfsspec_access(struct vop_access_args *ap)
3222 1.2.10.2 tls {
3223 1.2.10.2 tls struct vattr *vap;
3224 1.2.10.2 tls struct ucred *cred = ap->a_cred;
3225 1.2.10.2 tls struct vnode *vp = ap->a_vp;
3226 1.2.10.2 tls accmode_t accmode = ap->a_accmode;
3227 1.2.10.2 tls struct vattr vattr;
3228 1.2.10.2 tls int error;
3229 1.2.10.2 tls
3230 1.2.10.2 tls /*
3231 1.2.10.2 tls * Disallow write attempts on filesystems mounted read-only;
3232 1.2.10.2 tls * unless the file is a socket, fifo, or a block or character
3233 1.2.10.2 tls * device resident on the filesystem.
3234 1.2.10.2 tls */
3235 1.2.10.2 tls if ((accmode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3236 1.2.10.2 tls switch (vp->v_type) {
3237 1.2.10.2 tls case VREG:
3238 1.2.10.2 tls case VDIR:
3239 1.2.10.2 tls case VLNK:
3240 1.2.10.2 tls return (EROFS);
3241 1.2.10.2 tls default:
3242 1.2.10.2 tls break;
3243 1.2.10.2 tls }
3244 1.2.10.2 tls }
3245 1.2.10.2 tls vap = &vattr;
3246 1.2.10.2 tls error = VOP_GETATTR(vp, vap, cred);
3247 1.2.10.2 tls if (error)
3248 1.2.10.2 tls goto out;
3249 1.2.10.2 tls error = vaccess(vp->v_type, vap->va_mode, vap->va_uid, vap->va_gid,
3250 1.2.10.2 tls accmode, cred, NULL);
3251 1.2.10.2 tls out:
3252 1.2.10.2 tls return error;
3253 1.2.10.2 tls }
3254 1.2.10.2 tls
3255 1.2.10.2 tls /*
3256 1.2.10.2 tls * Read wrapper for fifos.
3257 1.2.10.2 tls */
3258 1.2.10.2 tls static int
3259 1.2.10.2 tls nfsfifo_read(struct vop_read_args *ap)
3260 1.2.10.2 tls {
3261 1.2.10.2 tls struct nfsnode *np = VTONFS(ap->a_vp);
3262 1.2.10.2 tls int error;
3263 1.2.10.2 tls
3264 1.2.10.2 tls /*
3265 1.2.10.2 tls * Set access flag.
3266 1.2.10.2 tls */
3267 1.2.10.2 tls mtx_lock(&np->n_mtx);
3268 1.2.10.2 tls np->n_flag |= NACC;
3269 1.2.10.2 tls vfs_timestamp(&np->n_atim);
3270 1.2.10.2 tls mtx_unlock(&np->n_mtx);
3271 1.2.10.2 tls error = fifo_specops.vop_read(ap);
3272 1.2.10.2 tls return error;
3273 1.2.10.2 tls }
3274 1.2.10.2 tls
3275 1.2.10.2 tls /*
3276 1.2.10.2 tls * Write wrapper for fifos.
3277 1.2.10.2 tls */
3278 1.2.10.2 tls static int
3279 1.2.10.2 tls nfsfifo_write(struct vop_write_args *ap)
3280 1.2.10.2 tls {
3281 1.2.10.2 tls struct nfsnode *np = VTONFS(ap->a_vp);
3282 1.2.10.2 tls
3283 1.2.10.2 tls /*
3284 1.2.10.2 tls * Set update flag.
3285 1.2.10.2 tls */
3286 1.2.10.2 tls mtx_lock(&np->n_mtx);
3287 1.2.10.2 tls np->n_flag |= NUPD;
3288 1.2.10.2 tls vfs_timestamp(&np->n_mtim);
3289 1.2.10.2 tls mtx_unlock(&np->n_mtx);
3290 1.2.10.2 tls return(fifo_specops.vop_write(ap));
3291 1.2.10.2 tls }
3292 1.2.10.2 tls
3293 1.2.10.2 tls /*
3294 1.2.10.2 tls * Close wrapper for fifos.
3295 1.2.10.2 tls *
3296 1.2.10.2 tls * Update the times on the nfsnode then do fifo close.
3297 1.2.10.2 tls */
3298 1.2.10.2 tls static int
3299 1.2.10.2 tls nfsfifo_close(struct vop_close_args *ap)
3300 1.2.10.2 tls {
3301 1.2.10.2 tls struct vnode *vp = ap->a_vp;
3302 1.2.10.2 tls struct nfsnode *np = VTONFS(vp);
3303 1.2.10.2 tls struct vattr vattr;
3304 1.2.10.2 tls struct timespec ts;
3305 1.2.10.2 tls
3306 1.2.10.2 tls mtx_lock(&np->n_mtx);
3307 1.2.10.2 tls if (np->n_flag & (NACC | NUPD)) {
3308 1.2.10.2 tls vfs_timestamp(&ts);
3309 1.2.10.2 tls if (np->n_flag & NACC)
3310 1.2.10.2 tls np->n_atim = ts;
3311 1.2.10.2 tls if (np->n_flag & NUPD)
3312 1.2.10.2 tls np->n_mtim = ts;
3313 1.2.10.2 tls np->n_flag |= NCHG;
3314 1.2.10.2 tls if (vrefcnt(vp) == 1 &&
3315 1.2.10.2 tls (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3316 1.2.10.2 tls VATTR_NULL(&vattr);
3317 1.2.10.2 tls if (np->n_flag & NACC)
3318 1.2.10.2 tls vattr.va_atime = np->n_atim;
3319 1.2.10.2 tls if (np->n_flag & NUPD)
3320 1.2.10.2 tls vattr.va_mtime = np->n_mtim;
3321 1.2.10.2 tls mtx_unlock(&np->n_mtx);
3322 1.2.10.2 tls (void)VOP_SETATTR(vp, &vattr, ap->a_cred);
3323 1.2.10.2 tls goto out;
3324 1.2.10.2 tls }
3325 1.2.10.2 tls }
3326 1.2.10.2 tls mtx_unlock(&np->n_mtx);
3327 1.2.10.2 tls out:
3328 1.2.10.2 tls return (fifo_specops.vop_close(ap));
3329 1.2.10.2 tls }
3330 1.2.10.2 tls
3331 1.2.10.2 tls /*
3332 1.2.10.2 tls * Just call ncl_writebp() with the force argument set to 1.
3333 1.2.10.2 tls *
3334 1.2.10.2 tls * NOTE: B_DONE may or may not be set in a_bp on call.
3335 1.2.10.2 tls */
3336 1.2.10.2 tls static int
3337 1.2.10.2 tls nfs_bwrite(struct buf *bp)
3338 1.2.10.2 tls {
3339 1.2.10.2 tls
3340 1.2.10.2 tls return (ncl_writebp(bp, 1, curthread));
3341 1.2.10.2 tls }
3342 1.2.10.2 tls
3343 1.2.10.2 tls struct buf_ops buf_ops_newnfs = {
3344 1.2.10.2 tls .bop_name = "buf_ops_nfs",
3345 1.2.10.2 tls .bop_write = nfs_bwrite,
3346 1.2.10.2 tls .bop_strategy = bufstrategy,
3347 1.2.10.2 tls .bop_sync = bufsync,
3348 1.2.10.2 tls .bop_bdflush = bufbdflush,
3349 1.2.10.2 tls };
3350 1.2.10.2 tls
3351 1.2.10.2 tls static int
3352 1.2.10.2 tls nfs_getacl(struct vop_getacl_args *ap)
3353 1.2.10.2 tls {
3354 1.2.10.2 tls int error;
3355 1.2.10.2 tls
3356 1.2.10.2 tls if (ap->a_type != ACL_TYPE_NFS4)
3357 1.2.10.2 tls return (EOPNOTSUPP);
3358 1.2.10.2 tls error = nfsrpc_getacl(ap->a_vp, ap->a_cred, ap->a_td, ap->a_aclp,
3359 1.2.10.2 tls NULL);
3360 1.2.10.2 tls if (error > NFSERR_STALE) {
3361 1.2.10.2 tls (void) nfscl_maperr(ap->a_td, error, (uid_t)0, (gid_t)0);
3362 1.2.10.2 tls error = EPERM;
3363 1.2.10.2 tls }
3364 1.2.10.2 tls return (error);
3365 1.2.10.2 tls }
3366 1.2.10.2 tls
3367 1.2.10.2 tls static int
3368 1.2.10.2 tls nfs_setacl(struct vop_setacl_args *ap)
3369 1.2.10.2 tls {
3370 1.2.10.2 tls int error;
3371 1.2.10.2 tls
3372 1.2.10.2 tls if (ap->a_type != ACL_TYPE_NFS4)
3373 1.2.10.2 tls return (EOPNOTSUPP);
3374 1.2.10.2 tls error = nfsrpc_setacl(ap->a_vp, ap->a_cred, ap->a_td, ap->a_aclp,
3375 1.2.10.2 tls NULL);
3376 1.2.10.2 tls if (error > NFSERR_STALE) {
3377 1.2.10.2 tls (void) nfscl_maperr(ap->a_td, error, (uid_t)0, (gid_t)0);
3378 1.2.10.2 tls error = EPERM;
3379 1.2.10.2 tls }
3380 1.2.10.2 tls return (error);
3381 1.2.10.2 tls }
3382 1.2.10.2 tls
3383 1.2.10.2 tls /*
3384 1.2.10.2 tls * Return POSIX pathconf information applicable to nfs filesystems.
3385 1.2.10.2 tls */
3386 1.2.10.2 tls static int
3387 1.2.10.2 tls nfs_pathconf(struct vop_pathconf_args *ap)
3388 1.2.10.2 tls {
3389 1.2.10.2 tls struct nfsv3_pathconf pc;
3390 1.2.10.2 tls struct nfsvattr nfsva;
3391 1.2.10.2 tls struct vnode *vp = ap->a_vp;
3392 1.2.10.2 tls struct thread *td = curthread;
3393 1.2.10.2 tls int attrflag, error;
3394 1.2.10.2 tls
3395 1.2.10.3 jdolecek if ((NFS_ISV34(vp) && (ap->a_name == _PC_LINK_MAX ||
3396 1.2.10.2 tls ap->a_name == _PC_NAME_MAX || ap->a_name == _PC_CHOWN_RESTRICTED ||
3397 1.2.10.3 jdolecek ap->a_name == _PC_NO_TRUNC)) ||
3398 1.2.10.3 jdolecek (NFS_ISV4(vp) && ap->a_name == _PC_ACL_NFS4)) {
3399 1.2.10.2 tls /*
3400 1.2.10.2 tls * Since only the above 4 a_names are returned by the NFSv3
3401 1.2.10.2 tls * Pathconf RPC, there is no point in doing it for others.
3402 1.2.10.3 jdolecek * For NFSv4, the Pathconf RPC (actually a Getattr Op.) can
3403 1.2.10.3 jdolecek * be used for _PC_NFS4_ACL as well.
3404 1.2.10.2 tls */
3405 1.2.10.2 tls error = nfsrpc_pathconf(vp, &pc, td->td_ucred, td, &nfsva,
3406 1.2.10.2 tls &attrflag, NULL);
3407 1.2.10.2 tls if (attrflag != 0)
3408 1.2.10.2 tls (void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0,
3409 1.2.10.2 tls 1);
3410 1.2.10.2 tls if (error != 0)
3411 1.2.10.2 tls return (error);
3412 1.2.10.2 tls } else {
3413 1.2.10.2 tls /*
3414 1.2.10.2 tls * For NFSv2 (or NFSv3 when not one of the above 4 a_names),
3415 1.2.10.2 tls * just fake them.
3416 1.2.10.2 tls */
3417 1.2.10.2 tls pc.pc_linkmax = LINK_MAX;
3418 1.2.10.2 tls pc.pc_namemax = NFS_MAXNAMLEN;
3419 1.2.10.2 tls pc.pc_notrunc = 1;
3420 1.2.10.2 tls pc.pc_chownrestricted = 1;
3421 1.2.10.2 tls pc.pc_caseinsensitive = 0;
3422 1.2.10.2 tls pc.pc_casepreserving = 1;
3423 1.2.10.2 tls error = 0;
3424 1.2.10.2 tls }
3425 1.2.10.2 tls switch (ap->a_name) {
3426 1.2.10.2 tls case _PC_LINK_MAX:
3427 1.2.10.2 tls *ap->a_retval = pc.pc_linkmax;
3428 1.2.10.2 tls break;
3429 1.2.10.2 tls case _PC_NAME_MAX:
3430 1.2.10.2 tls *ap->a_retval = pc.pc_namemax;
3431 1.2.10.2 tls break;
3432 1.2.10.2 tls case _PC_PATH_MAX:
3433 1.2.10.2 tls *ap->a_retval = PATH_MAX;
3434 1.2.10.2 tls break;
3435 1.2.10.2 tls case _PC_PIPE_BUF:
3436 1.2.10.2 tls *ap->a_retval = PIPE_BUF;
3437 1.2.10.2 tls break;
3438 1.2.10.2 tls case _PC_CHOWN_RESTRICTED:
3439 1.2.10.2 tls *ap->a_retval = pc.pc_chownrestricted;
3440 1.2.10.2 tls break;
3441 1.2.10.2 tls case _PC_NO_TRUNC:
3442 1.2.10.2 tls *ap->a_retval = pc.pc_notrunc;
3443 1.2.10.2 tls break;
3444 1.2.10.2 tls case _PC_ACL_EXTENDED:
3445 1.2.10.2 tls *ap->a_retval = 0;
3446 1.2.10.2 tls break;
3447 1.2.10.2 tls case _PC_ACL_NFS4:
3448 1.2.10.2 tls if (NFS_ISV4(vp) && nfsrv_useacl != 0 && attrflag != 0 &&
3449 1.2.10.2 tls NFSISSET_ATTRBIT(&nfsva.na_suppattr, NFSATTRBIT_ACL))
3450 1.2.10.2 tls *ap->a_retval = 1;
3451 1.2.10.2 tls else
3452 1.2.10.2 tls *ap->a_retval = 0;
3453 1.2.10.2 tls break;
3454 1.2.10.2 tls case _PC_ACL_PATH_MAX:
3455 1.2.10.2 tls if (NFS_ISV4(vp))
3456 1.2.10.2 tls *ap->a_retval = ACL_MAX_ENTRIES;
3457 1.2.10.2 tls else
3458 1.2.10.2 tls *ap->a_retval = 3;
3459 1.2.10.2 tls break;
3460 1.2.10.2 tls case _PC_MAC_PRESENT:
3461 1.2.10.2 tls *ap->a_retval = 0;
3462 1.2.10.2 tls break;
3463 1.2.10.2 tls case _PC_ASYNC_IO:
3464 1.2.10.2 tls /* _PC_ASYNC_IO should have been handled by upper layers. */
3465 1.2.10.2 tls KASSERT(0, ("_PC_ASYNC_IO should not get here"));
3466 1.2.10.2 tls error = EINVAL;
3467 1.2.10.2 tls break;
3468 1.2.10.2 tls case _PC_PRIO_IO:
3469 1.2.10.2 tls *ap->a_retval = 0;
3470 1.2.10.2 tls break;
3471 1.2.10.2 tls case _PC_SYNC_IO:
3472 1.2.10.2 tls *ap->a_retval = 0;
3473 1.2.10.2 tls break;
3474 1.2.10.2 tls case _PC_ALLOC_SIZE_MIN:
3475 1.2.10.2 tls *ap->a_retval = vp->v_mount->mnt_stat.f_bsize;
3476 1.2.10.2 tls break;
3477 1.2.10.2 tls case _PC_FILESIZEBITS:
3478 1.2.10.2 tls if (NFS_ISV34(vp))
3479 1.2.10.2 tls *ap->a_retval = 64;
3480 1.2.10.2 tls else
3481 1.2.10.2 tls *ap->a_retval = 32;
3482 1.2.10.2 tls break;
3483 1.2.10.2 tls case _PC_REC_INCR_XFER_SIZE:
3484 1.2.10.2 tls *ap->a_retval = vp->v_mount->mnt_stat.f_iosize;
3485 1.2.10.2 tls break;
3486 1.2.10.2 tls case _PC_REC_MAX_XFER_SIZE:
3487 1.2.10.2 tls *ap->a_retval = -1; /* means ``unlimited'' */
3488 1.2.10.2 tls break;
3489 1.2.10.2 tls case _PC_REC_MIN_XFER_SIZE:
3490 1.2.10.2 tls *ap->a_retval = vp->v_mount->mnt_stat.f_iosize;
3491 1.2.10.2 tls break;
3492 1.2.10.2 tls case _PC_REC_XFER_ALIGN:
3493 1.2.10.2 tls *ap->a_retval = PAGE_SIZE;
3494 1.2.10.2 tls break;
3495 1.2.10.2 tls case _PC_SYMLINK_MAX:
3496 1.2.10.2 tls *ap->a_retval = NFS_MAXPATHLEN;
3497 1.2.10.2 tls break;
3498 1.2.10.2 tls
3499 1.2.10.2 tls default:
3500 1.2.10.2 tls error = EINVAL;
3501 1.2.10.2 tls break;
3502 1.2.10.2 tls }
3503 1.2.10.2 tls return (error);
3504 1.2.10.2 tls }
3505 1.2.10.2 tls
3506