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