1 /*
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)nfs_vnops.c 8.16 (Berkeley) 5/27/95
37 */
38
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD: releng/5.0/sys/nfsclient/nfs_vnops.c 104908 2002-10-11 14:58:34Z mike $");
41
42 /*
43 * vnode op calls for Sun NFS version 2 and 3
44 */
45
46 #include "opt_inet.h"
47
48 #include <sys/param.h>
49 #include <sys/kernel.h>
50 #include <sys/systm.h>
51 #include <sys/resourcevar.h>
52 #include <sys/proc.h>
53 #include <sys/mount.h>
54 #include <sys/bio.h>
55 #include <sys/buf.h>
56 #include <sys/malloc.h>
57 #include <sys/mbuf.h>
58 #include <sys/namei.h>
59 #include <sys/socket.h>
60 #include <sys/vnode.h>
61 #include <sys/dirent.h>
62 #include <sys/fcntl.h>
63 #include <sys/lockf.h>
64 #include <sys/stat.h>
65 #include <sys/sysctl.h>
66
67 #include <vm/vm.h>
68 #include <vm/vm_extern.h>
69
70 #include <fs/fifofs/fifo.h>
71
72 #include <nfs/rpcv2.h>
73 #include <nfs/nfsproto.h>
74 #include <nfsclient/nfs.h>
75 #include <nfsclient/nfsnode.h>
76 #include <nfsclient/nfsmount.h>
77 #include <nfsclient/nfs_lock.h>
78 #include <nfs/xdr_subs.h>
79 #include <nfsclient/nfsm_subs.h>
80
81 #include <net/if.h>
82 #include <netinet/in.h>
83 #include <netinet/in_var.h>
84
85 /* Defs */
86 #define TRUE 1
87 #define FALSE 0
88
89 /*
90 * Ifdef for FreeBSD-current merged buffer cache. It is unfortunate that these
91 * calls are not in getblk() and brelse() so that they would not be necessary
92 * here.
93 */
94 #ifndef B_VMIO
95 #define vfs_busy_pages(bp, f)
96 #endif
97
98 static int nfsspec_read(struct vop_read_args *);
99 static int nfsspec_write(struct vop_write_args *);
100 static int nfsfifo_read(struct vop_read_args *);
101 static int nfsfifo_write(struct vop_write_args *);
102 static int nfsspec_close(struct vop_close_args *);
103 static int nfsfifo_close(struct vop_close_args *);
104 static int nfs_flush(struct vnode *, struct ucred *, int, struct thread *,
105 int);
106 static int nfs_setattrrpc(struct vnode *, struct vattr *, struct ucred *,
107 struct thread *);
108 static int nfs_lookup(struct vop_lookup_args *);
109 static int nfs_create(struct vop_create_args *);
110 static int nfs_mknod(struct vop_mknod_args *);
111 static int nfs_open(struct vop_open_args *);
112 static int nfs_close(struct vop_close_args *);
113 static int nfs_access(struct vop_access_args *);
114 static int nfs_getattr(struct vop_getattr_args *);
115 static int nfs_setattr(struct vop_setattr_args *);
116 static int nfs_read(struct vop_read_args *);
117 static int nfs_fsync(struct vop_fsync_args *);
118 static int nfs_remove(struct vop_remove_args *);
119 static int nfs_link(struct vop_link_args *);
120 static int nfs_rename(struct vop_rename_args *);
121 static int nfs_mkdir(struct vop_mkdir_args *);
122 static int nfs_rmdir(struct vop_rmdir_args *);
123 static int nfs_symlink(struct vop_symlink_args *);
124 static int nfs_readdir(struct vop_readdir_args *);
125 static int nfs_strategy(struct vop_strategy_args *);
126 static int nfs_lookitup(struct vnode *, const char *, int,
127 struct ucred *, struct thread *, struct nfsnode **);
128 static int nfs_sillyrename(struct vnode *, struct vnode *,
129 struct componentname *);
130 static int nfsspec_access(struct vop_access_args *);
131 static int nfs_readlink(struct vop_readlink_args *);
132 static int nfs_print(struct vop_print_args *);
133 static int nfs_advlock(struct vop_advlock_args *);
134
135 /*
136 * Global vfs data structures for nfs
137 */
138 vop_t **nfsv2_vnodeop_p;
139 static struct vnodeopv_entry_desc nfsv2_vnodeop_entries[] = {
140 { &vop_default_desc, (vop_t *) vop_defaultop },
141 { &vop_access_desc, (vop_t *) nfs_access },
142 { &vop_advlock_desc, (vop_t *) nfs_advlock },
143 { &vop_close_desc, (vop_t *) nfs_close },
144 { &vop_create_desc, (vop_t *) nfs_create },
145 { &vop_fsync_desc, (vop_t *) nfs_fsync },
146 { &vop_getattr_desc, (vop_t *) nfs_getattr },
147 { &vop_getpages_desc, (vop_t *) nfs_getpages },
148 { &vop_putpages_desc, (vop_t *) nfs_putpages },
149 { &vop_inactive_desc, (vop_t *) nfs_inactive },
150 { &vop_islocked_desc, (vop_t *) vop_stdislocked },
151 { &vop_lease_desc, (vop_t *) vop_null },
152 { &vop_link_desc, (vop_t *) nfs_link },
153 { &vop_lock_desc, (vop_t *) vop_sharedlock },
154 { &vop_lookup_desc, (vop_t *) nfs_lookup },
155 { &vop_mkdir_desc, (vop_t *) nfs_mkdir },
156 { &vop_mknod_desc, (vop_t *) nfs_mknod },
157 { &vop_open_desc, (vop_t *) nfs_open },
158 { &vop_print_desc, (vop_t *) nfs_print },
159 { &vop_read_desc, (vop_t *) nfs_read },
160 { &vop_readdir_desc, (vop_t *) nfs_readdir },
161 { &vop_readlink_desc, (vop_t *) nfs_readlink },
162 { &vop_reclaim_desc, (vop_t *) nfs_reclaim },
163 { &vop_remove_desc, (vop_t *) nfs_remove },
164 { &vop_rename_desc, (vop_t *) nfs_rename },
165 { &vop_rmdir_desc, (vop_t *) nfs_rmdir },
166 { &vop_setattr_desc, (vop_t *) nfs_setattr },
167 { &vop_strategy_desc, (vop_t *) nfs_strategy },
168 { &vop_symlink_desc, (vop_t *) nfs_symlink },
169 { &vop_unlock_desc, (vop_t *) vop_stdunlock },
170 { &vop_write_desc, (vop_t *) nfs_write },
171 { NULL, NULL }
172 };
173 static struct vnodeopv_desc nfsv2_vnodeop_opv_desc =
174 { &nfsv2_vnodeop_p, nfsv2_vnodeop_entries };
175 VNODEOP_SET(nfsv2_vnodeop_opv_desc);
176
177 /*
178 * Special device vnode ops
179 */
180 vop_t **spec_nfsv2nodeop_p;
181 static struct vnodeopv_entry_desc nfsv2_specop_entries[] = {
182 { &vop_default_desc, (vop_t *) spec_vnoperate },
183 { &vop_access_desc, (vop_t *) nfsspec_access },
184 { &vop_close_desc, (vop_t *) nfsspec_close },
185 { &vop_fsync_desc, (vop_t *) nfs_fsync },
186 { &vop_getattr_desc, (vop_t *) nfs_getattr },
187 { &vop_islocked_desc, (vop_t *) vop_stdislocked },
188 { &vop_inactive_desc, (vop_t *) nfs_inactive },
189 { &vop_lock_desc, (vop_t *) vop_sharedlock },
190 { &vop_print_desc, (vop_t *) nfs_print },
191 { &vop_read_desc, (vop_t *) nfsspec_read },
192 { &vop_reclaim_desc, (vop_t *) nfs_reclaim },
193 { &vop_setattr_desc, (vop_t *) nfs_setattr },
194 { &vop_unlock_desc, (vop_t *) vop_stdunlock },
195 { &vop_write_desc, (vop_t *) nfsspec_write },
196 { NULL, NULL }
197 };
198 static struct vnodeopv_desc spec_nfsv2nodeop_opv_desc =
199 { &spec_nfsv2nodeop_p, nfsv2_specop_entries };
200 VNODEOP_SET(spec_nfsv2nodeop_opv_desc);
201
202 vop_t **fifo_nfsv2nodeop_p;
203 static struct vnodeopv_entry_desc nfsv2_fifoop_entries[] = {
204 { &vop_default_desc, (vop_t *) fifo_vnoperate },
205 { &vop_access_desc, (vop_t *) nfsspec_access },
206 { &vop_close_desc, (vop_t *) nfsfifo_close },
207 { &vop_fsync_desc, (vop_t *) nfs_fsync },
208 { &vop_getattr_desc, (vop_t *) nfs_getattr },
209 { &vop_inactive_desc, (vop_t *) nfs_inactive },
210 { &vop_islocked_desc, (vop_t *) vop_stdislocked },
211 { &vop_lock_desc, (vop_t *) vop_sharedlock },
212 { &vop_print_desc, (vop_t *) nfs_print },
213 { &vop_read_desc, (vop_t *) nfsfifo_read },
214 { &vop_reclaim_desc, (vop_t *) nfs_reclaim },
215 { &vop_setattr_desc, (vop_t *) nfs_setattr },
216 { &vop_unlock_desc, (vop_t *) vop_stdunlock },
217 { &vop_write_desc, (vop_t *) nfsfifo_write },
218 { NULL, NULL }
219 };
220 static struct vnodeopv_desc fifo_nfsv2nodeop_opv_desc =
221 { &fifo_nfsv2nodeop_p, nfsv2_fifoop_entries };
222 VNODEOP_SET(fifo_nfsv2nodeop_opv_desc);
223
224 static int nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp,
225 struct componentname *cnp, struct vattr *vap);
226 static int nfs_removerpc(struct vnode *dvp, const char *name, int namelen,
227 struct ucred *cred, struct thread *td);
228 static int nfs_renamerpc(struct vnode *fdvp, const char *fnameptr,
229 int fnamelen, struct vnode *tdvp,
230 const char *tnameptr, int tnamelen,
231 struct ucred *cred, struct thread *td);
232 static int nfs_renameit(struct vnode *sdvp, struct componentname *scnp,
233 struct sillyrename *sp);
234
235 /*
236 * Global variables
237 */
238 struct proc *nfs_iodwant[NFS_MAXASYNCDAEMON];
239 struct nfsmount *nfs_iodmount[NFS_MAXASYNCDAEMON];
240 int nfs_numasync = 0;
241 #define DIRHDSIZ (sizeof (struct dirent) - (MAXNAMLEN + 1))
242
243 SYSCTL_DECL(_vfs_nfs);
244
245 static int nfsaccess_cache_timeout = NFS_MAXATTRTIMO;
246 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_timeout, CTLFLAG_RW,
247 &nfsaccess_cache_timeout, 0, "NFS ACCESS cache timeout");
248
249 static int nfsv3_commit_on_close = 0;
250 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfsv3_commit_on_close, CTLFLAG_RW,
251 &nfsv3_commit_on_close, 0, "write+commit on close, else only write");
252 #if 0
253 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_hits, CTLFLAG_RD,
254 &nfsstats.accesscache_hits, 0, "NFS ACCESS cache hit count");
255
256 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_misses, CTLFLAG_RD,
257 &nfsstats.accesscache_misses, 0, "NFS ACCESS cache miss count");
258 #endif
259
260 #define NFSV3ACCESS_ALL (NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY \
261 | NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE \
262 | NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP)
263 static int
264 nfs3_access_otw(struct vnode *vp, int wmode, struct thread *td,
265 struct ucred *cred)
266 {
267 const int v3 = 1;
268 u_int32_t *tl;
269 int error = 0, attrflag;
270
271 struct mbuf *mreq, *mrep, *md, *mb;
272 caddr_t bpos, dpos;
273 u_int32_t rmode;
274 struct nfsnode *np = VTONFS(vp);
275
276 nfsstats.rpccnt[NFSPROC_ACCESS]++;
277 mreq = nfsm_reqhead(vp, NFSPROC_ACCESS, NFSX_FH(v3) + NFSX_UNSIGNED);
278 mb = mreq;
279 bpos = mtod(mb, caddr_t);
280 nfsm_fhtom(vp, v3);
281 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
282 *tl = txdr_unsigned(wmode);
283 nfsm_request(vp, NFSPROC_ACCESS, td, cred);
284 nfsm_postop_attr(vp, attrflag);
285 if (!error) {
286 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
287 rmode = fxdr_unsigned(u_int32_t, *tl);
288 np->n_mode = rmode;
289 np->n_modeuid = cred->cr_uid;
290 np->n_modestamp = time_second;
291 }
292 m_freem(mrep);
293 nfsmout:
294 return error;
295 }
296
297 /*
298 * nfs access vnode op.
299 * For nfs version 2, just return ok. File accesses may fail later.
300 * For nfs version 3, use the access rpc to check accessibility. If file modes
301 * are changed on the server, accesses might still fail later.
302 */
303 static int
304 nfs_access(struct vop_access_args *ap)
305 {
306 struct vnode *vp = ap->a_vp;
307 int error = 0;
308 u_int32_t mode, wmode;
309 int v3 = NFS_ISV3(vp);
310 struct nfsnode *np = VTONFS(vp);
311
312 /*
313 * Disallow write attempts on filesystems mounted read-only;
314 * unless the file is a socket, fifo, or a block or character
315 * device resident on the filesystem.
316 */
317 if ((ap->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
318 switch (vp->v_type) {
319 case VREG:
320 case VDIR:
321 case VLNK:
322 return (EROFS);
323 default:
324 break;
325 }
326 }
327 /*
328 * For nfs v3, check to see if we have done this recently, and if
329 * so return our cached result instead of making an ACCESS call.
330 * If not, do an access rpc, otherwise you are stuck emulating
331 * ufs_access() locally using the vattr. This may not be correct,
332 * since the server may apply other access criteria such as
333 * client uid-->server uid mapping that we do not know about.
334 */
335 if (v3) {
336 if (ap->a_mode & VREAD)
337 mode = NFSV3ACCESS_READ;
338 else
339 mode = 0;
340 if (vp->v_type != VDIR) {
341 if (ap->a_mode & VWRITE)
342 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
343 if (ap->a_mode & VEXEC)
344 mode |= NFSV3ACCESS_EXECUTE;
345 } else {
346 if (ap->a_mode & VWRITE)
347 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
348 NFSV3ACCESS_DELETE);
349 if (ap->a_mode & VEXEC)
350 mode |= NFSV3ACCESS_LOOKUP;
351 }
352 /* XXX safety belt, only make blanket request if caching */
353 if (nfsaccess_cache_timeout > 0) {
354 wmode = NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY |
355 NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE |
356 NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP;
357 } else {
358 wmode = mode;
359 }
360
361 /*
362 * Does our cached result allow us to give a definite yes to
363 * this request?
364 */
365 if ((time_second < (np->n_modestamp + nfsaccess_cache_timeout)) &&
366 (ap->a_cred->cr_uid == np->n_modeuid) &&
367 ((np->n_mode & mode) == mode)) {
368 nfsstats.accesscache_hits++;
369 } else {
370 /*
371 * Either a no, or a don't know. Go to the wire.
372 */
373 nfsstats.accesscache_misses++;
374 error = nfs3_access_otw(vp, wmode, ap->a_td,ap->a_cred);
375 if (!error) {
376 if ((np->n_mode & mode) != mode) {
377 error = EACCES;
378 }
379 }
380 }
381 return (error);
382 } else {
383 if ((error = nfsspec_access(ap)) != 0)
384 return (error);
385
386 /*
387 * Attempt to prevent a mapped root from accessing a file
388 * which it shouldn't. We try to read a byte from the file
389 * if the user is root and the file is not zero length.
390 * After calling nfsspec_access, we should have the correct
391 * file size cached.
392 */
393 if (ap->a_cred->cr_uid == 0 && (ap->a_mode & VREAD)
394 && VTONFS(vp)->n_size > 0) {
395 struct iovec aiov;
396 struct uio auio;
397 char buf[1];
398
399 aiov.iov_base = buf;
400 aiov.iov_len = 1;
401 auio.uio_iov = &aiov;
402 auio.uio_iovcnt = 1;
403 auio.uio_offset = 0;
404 auio.uio_resid = 1;
405 auio.uio_segflg = UIO_SYSSPACE;
406 auio.uio_rw = UIO_READ;
407 auio.uio_td = ap->a_td;
408
409 if (vp->v_type == VREG)
410 error = nfs_readrpc(vp, &auio, ap->a_cred);
411 else if (vp->v_type == VDIR) {
412 char* bp;
413 bp = malloc(NFS_DIRBLKSIZ, M_TEMP, M_WAITOK);
414 aiov.iov_base = bp;
415 aiov.iov_len = auio.uio_resid = NFS_DIRBLKSIZ;
416 error = nfs_readdirrpc(vp, &auio, ap->a_cred);
417 free(bp, M_TEMP);
418 } else if (vp->v_type == VLNK)
419 error = nfs_readlinkrpc(vp, &auio, ap->a_cred);
420 else
421 error = EACCES;
422 }
423 return (error);
424 }
425 }
426
427 /*
428 * nfs open vnode op
429 * Check to see if the type is ok
430 * and that deletion is not in progress.
431 * For paged in text files, you will need to flush the page cache
432 * if consistency is lost.
433 */
434 /* ARGSUSED */
435 static int
436 nfs_open(struct vop_open_args *ap)
437 {
438 struct vnode *vp = ap->a_vp;
439 struct nfsnode *np = VTONFS(vp);
440 struct vattr vattr;
441 int error;
442
443 if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK) {
444 #ifdef DIAGNOSTIC
445 printf("open eacces vtyp=%d\n", vp->v_type);
446 #endif
447 return (EACCES);
448 }
449 /*
450 * Get a valid lease. If cached data is stale, flush it.
451 */
452 if (np->n_flag & NMODIFIED) {
453 if ((error = nfs_vinvalbuf(vp, V_SAVE, ap->a_cred,
454 ap->a_td, 1)) == EINTR)
455 return (error);
456 np->n_attrstamp = 0;
457 if (vp->v_type == VDIR)
458 np->n_direofoffset = 0;
459 error = VOP_GETATTR(vp, &vattr, ap->a_cred, ap->a_td);
460 if (error)
461 return (error);
462 np->n_mtime = vattr.va_mtime.tv_sec;
463 } else {
464 error = VOP_GETATTR(vp, &vattr, ap->a_cred, ap->a_td);
465 if (error)
466 return (error);
467 if (np->n_mtime != vattr.va_mtime.tv_sec) {
468 if (vp->v_type == VDIR)
469 np->n_direofoffset = 0;
470 if ((error = nfs_vinvalbuf(vp, V_SAVE,
471 ap->a_cred, ap->a_td, 1)) == EINTR)
472 return (error);
473 np->n_mtime = vattr.va_mtime.tv_sec;
474 }
475 }
476 np->n_attrstamp = 0; /* For Open/Close consistency */
477 return (0);
478 }
479
480 /*
481 * nfs close vnode op
482 * What an NFS client should do upon close after writing is a debatable issue.
483 * Most NFS clients push delayed writes to the server upon close, basically for
484 * two reasons:
485 * 1 - So that any write errors may be reported back to the client process
486 * doing the close system call. By far the two most likely errors are
487 * NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
488 * 2 - To put a worst case upper bound on cache inconsistency between
489 * multiple clients for the file.
490 * There is also a consistency problem for Version 2 of the protocol w.r.t.
491 * not being able to tell if other clients are writing a file concurrently,
492 * since there is no way of knowing if the changed modify time in the reply
493 * is only due to the write for this client.
494 * (NFS Version 3 provides weak cache consistency data in the reply that
495 * should be sufficient to detect and handle this case.)
496 *
497 * The current code does the following:
498 * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
499 * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
500 * or commit them (this satisfies 1 and 2 except for the
501 * case where the server crashes after this close but
502 * before the commit RPC, which is felt to be "good
503 * enough". Changing the last argument to nfs_flush() to
504 * a 1 would force a commit operation, if it is felt a
505 * commit is necessary now.
506 */
507 /* ARGSUSED */
508 static int
509 nfs_close(struct vop_close_args *ap)
510 {
511 struct vnode *vp = ap->a_vp;
512 struct nfsnode *np = VTONFS(vp);
513 int error = 0;
514
515 if (vp->v_type == VREG) {
516 if (np->n_flag & NMODIFIED) {
517 if (NFS_ISV3(vp)) {
518 /*
519 * Under NFSv3 we have dirty buffers to dispose of. We
520 * must flush them to the NFS server. We have the option
521 * of waiting all the way through the commit rpc or just
522 * waiting for the initial write. The default is to only
523 * wait through the initial write so the data is in the
524 * server's cache, which is roughly similar to the state
525 * a standard disk subsystem leaves the file in on close().
526 *
527 * We cannot clear the NMODIFIED bit in np->n_flag due to
528 * potential races with other processes, and certainly
529 * cannot clear it if we don't commit.
530 */
531 int cm = nfsv3_commit_on_close ? 1 : 0;
532 error = nfs_flush(vp, ap->a_cred, MNT_WAIT, ap->a_td, cm);
533 /* np->n_flag &= ~NMODIFIED; */
534 } else {
535 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_cred, ap->a_td, 1);
536 }
537 np->n_attrstamp = 0;
538 }
539 if (np->n_flag & NWRITEERR) {
540 np->n_flag &= ~NWRITEERR;
541 error = np->n_error;
542 }
543 }
544 return (error);
545 }
546
547 /*
548 * nfs getattr call from vfs.
549 */
550 static int
551 nfs_getattr(struct vop_getattr_args *ap)
552 {
553 struct vnode *vp = ap->a_vp;
554 struct nfsnode *np = VTONFS(vp);
555 caddr_t bpos, dpos;
556 int error = 0;
557 struct mbuf *mreq, *mrep, *md, *mb;
558 int v3 = NFS_ISV3(vp);
559
560 /*
561 * Update local times for special files.
562 */
563 if (np->n_flag & (NACC | NUPD))
564 np->n_flag |= NCHG;
565 /*
566 * First look in the cache.
567 */
568 if (nfs_getattrcache(vp, ap->a_vap) == 0)
569 return (0);
570
571 if (v3 && nfsaccess_cache_timeout > 0) {
572 nfsstats.accesscache_misses++;
573 nfs3_access_otw(vp, NFSV3ACCESS_ALL, ap->a_td, ap->a_cred);
574 if (nfs_getattrcache(vp, ap->a_vap) == 0)
575 return (0);
576 }
577
578 nfsstats.rpccnt[NFSPROC_GETATTR]++;
579 mreq = nfsm_reqhead(vp, NFSPROC_GETATTR, NFSX_FH(v3));
580 mb = mreq;
581 bpos = mtod(mb, caddr_t);
582 nfsm_fhtom(vp, v3);
583 nfsm_request(vp, NFSPROC_GETATTR, ap->a_td, ap->a_cred);
584 if (!error) {
585 nfsm_loadattr(vp, ap->a_vap);
586 }
587 m_freem(mrep);
588 nfsmout:
589 return (error);
590 }
591
592 /*
593 * nfs setattr call.
594 */
595 static int
596 nfs_setattr(struct vop_setattr_args *ap)
597 {
598 struct vnode *vp = ap->a_vp;
599 struct nfsnode *np = VTONFS(vp);
600 struct vattr *vap = ap->a_vap;
601 int error = 0;
602 u_quad_t tsize;
603
604 #ifndef nolint
605 tsize = (u_quad_t)0;
606 #endif
607
608 /*
609 * Setting of flags is not supported.
610 */
611 if (vap->va_flags != VNOVAL)
612 return (EOPNOTSUPP);
613
614 /*
615 * Disallow write attempts if the filesystem is mounted read-only.
616 */
617 if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
618 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
619 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
620 (vp->v_mount->mnt_flag & MNT_RDONLY))
621 return (EROFS);
622 if (vap->va_size != VNOVAL) {
623 switch (vp->v_type) {
624 case VDIR:
625 return (EISDIR);
626 case VCHR:
627 case VBLK:
628 case VSOCK:
629 case VFIFO:
630 if (vap->va_mtime.tv_sec == VNOVAL &&
631 vap->va_atime.tv_sec == VNOVAL &&
632 vap->va_mode == (mode_t)VNOVAL &&
633 vap->va_uid == (uid_t)VNOVAL &&
634 vap->va_gid == (gid_t)VNOVAL)
635 return (0);
636 vap->va_size = VNOVAL;
637 break;
638 default:
639 /*
640 * Disallow write attempts if the filesystem is
641 * mounted read-only.
642 */
643 if (vp->v_mount->mnt_flag & MNT_RDONLY)
644 return (EROFS);
645
646 /*
647 * We run vnode_pager_setsize() early (why?),
648 * we must set np->n_size now to avoid vinvalbuf
649 * V_SAVE races that might setsize a lower
650 * value.
651 */
652
653 tsize = np->n_size;
654 error = nfs_meta_setsize(vp, ap->a_cred,
655 ap->a_td, vap->va_size);
656
657 if (np->n_flag & NMODIFIED) {
658 if (vap->va_size == 0)
659 error = nfs_vinvalbuf(vp, 0,
660 ap->a_cred, ap->a_td, 1);
661 else
662 error = nfs_vinvalbuf(vp, V_SAVE,
663 ap->a_cred, ap->a_td, 1);
664 if (error) {
665 vnode_pager_setsize(vp, np->n_size);
666 return (error);
667 }
668 }
669 np->n_vattr.va_size = vap->va_size;
670 };
671 } else if ((vap->va_mtime.tv_sec != VNOVAL ||
672 vap->va_atime.tv_sec != VNOVAL) && (np->n_flag & NMODIFIED) &&
673 vp->v_type == VREG &&
674 (error = nfs_vinvalbuf(vp, V_SAVE, ap->a_cred,
675 ap->a_td, 1)) == EINTR)
676 return (error);
677 error = nfs_setattrrpc(vp, vap, ap->a_cred, ap->a_td);
678 if (error && vap->va_size != VNOVAL) {
679 np->n_size = np->n_vattr.va_size = tsize;
680 vnode_pager_setsize(vp, np->n_size);
681 }
682 return (error);
683 }
684
685 /*
686 * Do an nfs setattr rpc.
687 */
688 static int
689 nfs_setattrrpc(struct vnode *vp, struct vattr *vap, struct ucred *cred,
690 struct thread *td)
691 {
692 struct nfsv2_sattr *sp;
693 caddr_t bpos, dpos;
694 u_int32_t *tl;
695 int error = 0, wccflag = NFSV3_WCCRATTR;
696 struct mbuf *mreq, *mrep, *md, *mb;
697 int v3 = NFS_ISV3(vp);
698
699 nfsstats.rpccnt[NFSPROC_SETATTR]++;
700 mreq = nfsm_reqhead(vp, NFSPROC_SETATTR, NFSX_FH(v3) + NFSX_SATTR(v3));
701 mb = mreq;
702 bpos = mtod(mb, caddr_t);
703 nfsm_fhtom(vp, v3);
704 if (v3) {
705 nfsm_v3attrbuild(vap, TRUE);
706 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
707 *tl = nfs_false;
708 } else {
709 sp = nfsm_build(struct nfsv2_sattr *, NFSX_V2SATTR);
710 if (vap->va_mode == (mode_t)VNOVAL)
711 sp->sa_mode = nfs_xdrneg1;
712 else
713 sp->sa_mode = vtonfsv2_mode(vp->v_type, vap->va_mode);
714 if (vap->va_uid == (uid_t)VNOVAL)
715 sp->sa_uid = nfs_xdrneg1;
716 else
717 sp->sa_uid = txdr_unsigned(vap->va_uid);
718 if (vap->va_gid == (gid_t)VNOVAL)
719 sp->sa_gid = nfs_xdrneg1;
720 else
721 sp->sa_gid = txdr_unsigned(vap->va_gid);
722 sp->sa_size = txdr_unsigned(vap->va_size);
723 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
724 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
725 }
726 nfsm_request(vp, NFSPROC_SETATTR, td, cred);
727 if (v3) {
728 nfsm_wcc_data(vp, wccflag);
729 } else
730 nfsm_loadattr(vp, NULL);
731 m_freem(mrep);
732 nfsmout:
733 return (error);
734 }
735
736 /*
737 * nfs lookup call, one step at a time...
738 * First look in cache
739 * If not found, unlock the directory nfsnode and do the rpc
740 */
741 static int
742 nfs_lookup(struct vop_lookup_args *ap)
743 {
744 struct componentname *cnp = ap->a_cnp;
745 struct vnode *dvp = ap->a_dvp;
746 struct vnode **vpp = ap->a_vpp;
747 int flags = cnp->cn_flags;
748 struct vnode *newvp;
749 struct nfsmount *nmp;
750 caddr_t bpos, dpos;
751 struct mbuf *mreq, *mrep, *md, *mb;
752 long len;
753 nfsfh_t *fhp;
754 struct nfsnode *np;
755 int lockparent, wantparent, error = 0, attrflag, fhsize;
756 int v3 = NFS_ISV3(dvp);
757 struct thread *td = cnp->cn_thread;
758
759 *vpp = NULLVP;
760 cnp->cn_flags &= ~PDIRUNLOCK;
761 if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
762 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
763 return (EROFS);
764 if (dvp->v_type != VDIR)
765 return (ENOTDIR);
766 lockparent = flags & LOCKPARENT;
767 wantparent = flags & (LOCKPARENT|WANTPARENT);
768 nmp = VFSTONFS(dvp->v_mount);
769 np = VTONFS(dvp);
770 if ((error = cache_lookup(dvp, vpp, cnp)) && error != ENOENT) {
771 struct vattr vattr;
772 int vpid;
773
774 if ((error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td)) != 0) {
775 *vpp = NULLVP;
776 return (error);
777 }
778
779 vhold(*vpp);
780 newvp = *vpp;
781 vpid = newvp->v_id;
782 /*
783 * See the comment starting `Step through' in ufs/ufs_lookup.c
784 * for an explanation of the locking protocol
785 */
786 if (dvp == newvp) {
787 VREF(newvp);
788 error = 0;
789 } else if (flags & ISDOTDOT) {
790 VOP_UNLOCK(dvp, 0, td);
791 cnp->cn_flags |= PDIRUNLOCK;
792 error = vget(newvp, LK_EXCLUSIVE, td);
793 if (!error && lockparent && (flags & ISLASTCN)) {
794 error = vn_lock(dvp, LK_EXCLUSIVE, td);
795 if (error == 0)
796 cnp->cn_flags &= ~PDIRUNLOCK;
797 }
798 } else {
799 error = vget(newvp, LK_EXCLUSIVE, td);
800 if (!lockparent || error || !(flags & ISLASTCN)) {
801 VOP_UNLOCK(dvp, 0, td);
802 cnp->cn_flags |= PDIRUNLOCK;
803 }
804 }
805 if (!error) {
806 if (vpid == newvp->v_id) {
807 if (!VOP_GETATTR(newvp, &vattr, cnp->cn_cred, td)
808 && vattr.va_ctime.tv_sec == VTONFS(newvp)->n_ctime) {
809 nfsstats.lookupcache_hits++;
810 if (cnp->cn_nameiop != LOOKUP &&
811 (flags & ISLASTCN))
812 cnp->cn_flags |= SAVENAME;
813 vdrop(newvp);
814 return (0);
815 }
816 cache_purge(newvp);
817 }
818 vput(newvp);
819 if (lockparent && dvp != newvp && (flags & ISLASTCN))
820 VOP_UNLOCK(dvp, 0, td);
821 }
822 vdrop(newvp);
823 error = vn_lock(dvp, LK_EXCLUSIVE, td);
824 *vpp = NULLVP;
825 if (error) {
826 cnp->cn_flags |= PDIRUNLOCK;
827 return (error);
828 }
829 cnp->cn_flags &= ~PDIRUNLOCK;
830 }
831 error = 0;
832 newvp = NULLVP;
833 nfsstats.lookupcache_misses++;
834 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
835 len = cnp->cn_namelen;
836 mreq = nfsm_reqhead(dvp, NFSPROC_LOOKUP,
837 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
838 mb = mreq;
839 bpos = mtod(mb, caddr_t);
840 nfsm_fhtom(dvp, v3);
841 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
842 nfsm_request(dvp, NFSPROC_LOOKUP, cnp->cn_thread, cnp->cn_cred);
843 if (error) {
844 nfsm_postop_attr(dvp, attrflag);
845 m_freem(mrep);
846 goto nfsmout;
847 }
848 nfsm_getfh(fhp, fhsize, v3);
849
850 /*
851 * Handle RENAME case...
852 */
853 if (cnp->cn_nameiop == RENAME && wantparent && (flags & ISLASTCN)) {
854 if (NFS_CMPFH(np, fhp, fhsize)) {
855 m_freem(mrep);
856 return (EISDIR);
857 }
858 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
859 if (error) {
860 m_freem(mrep);
861 return (error);
862 }
863 newvp = NFSTOV(np);
864 if (v3) {
865 nfsm_postop_attr(newvp, attrflag);
866 nfsm_postop_attr(dvp, attrflag);
867 } else
868 nfsm_loadattr(newvp, NULL);
869 *vpp = newvp;
870 m_freem(mrep);
871 cnp->cn_flags |= SAVENAME;
872 if (!lockparent) {
873 VOP_UNLOCK(dvp, 0, td);
874 cnp->cn_flags |= PDIRUNLOCK;
875 }
876 return (0);
877 }
878
879 if (flags & ISDOTDOT) {
880 VOP_UNLOCK(dvp, 0, td);
881 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
882 if (error) {
883 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, td);
884 return (error);
885 }
886 newvp = NFSTOV(np);
887 if (lockparent && (flags & ISLASTCN)) {
888 error = vn_lock(dvp, LK_EXCLUSIVE, td);
889 if (error) {
890 cnp->cn_flags |= PDIRUNLOCK;
891 vput(newvp);
892 return (error);
893 }
894 } else
895 cnp->cn_flags |= PDIRUNLOCK;
896 } else if (NFS_CMPFH(np, fhp, fhsize)) {
897 VREF(dvp);
898 newvp = dvp;
899 } else {
900 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
901 if (error) {
902 m_freem(mrep);
903 return (error);
904 }
905 if (!lockparent || !(flags & ISLASTCN)) {
906 cnp->cn_flags |= PDIRUNLOCK;
907 VOP_UNLOCK(dvp, 0, td);
908 }
909 newvp = NFSTOV(np);
910 }
911 if (v3) {
912 nfsm_postop_attr(newvp, attrflag);
913 nfsm_postop_attr(dvp, attrflag);
914 } else
915 nfsm_loadattr(newvp, NULL);
916 if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
917 cnp->cn_flags |= SAVENAME;
918 if ((cnp->cn_flags & MAKEENTRY) &&
919 (cnp->cn_nameiop != DELETE || !(flags & ISLASTCN))) {
920 np->n_ctime = np->n_vattr.va_ctime.tv_sec;
921 cache_enter(dvp, newvp, cnp);
922 }
923 *vpp = newvp;
924 m_freem(mrep);
925 nfsmout:
926 if (error) {
927 if (newvp != NULLVP) {
928 vrele(newvp);
929 *vpp = NULLVP;
930 }
931 if ((cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME) &&
932 (flags & ISLASTCN) && error == ENOENT) {
933 if (!lockparent) {
934 VOP_UNLOCK(dvp, 0, td);
935 cnp->cn_flags |= PDIRUNLOCK;
936 }
937 if (dvp->v_mount->mnt_flag & MNT_RDONLY)
938 error = EROFS;
939 else
940 error = EJUSTRETURN;
941 }
942 if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
943 cnp->cn_flags |= SAVENAME;
944 }
945 return (error);
946 }
947
948 /*
949 * nfs read call.
950 * Just call nfs_bioread() to do the work.
951 */
952 static int
953 nfs_read(struct vop_read_args *ap)
954 {
955 struct vnode *vp = ap->a_vp;
956
957 if (vp->v_type != VREG)
958 return (EPERM);
959 return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag, ap->a_cred));
960 }
961
962 /*
963 * nfs readlink call
964 */
965 static int
966 nfs_readlink(struct vop_readlink_args *ap)
967 {
968 struct vnode *vp = ap->a_vp;
969
970 if (vp->v_type != VLNK)
971 return (EINVAL);
972 return (nfs_bioread(vp, ap->a_uio, 0, ap->a_cred));
973 }
974
975 /*
976 * Do a readlink rpc.
977 * Called by nfs_doio() from below the buffer cache.
978 */
979 int
980 nfs_readlinkrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
981 {
982 caddr_t bpos, dpos;
983 int error = 0, len, attrflag;
984 struct mbuf *mreq, *mrep, *md, *mb;
985 int v3 = NFS_ISV3(vp);
986
987 nfsstats.rpccnt[NFSPROC_READLINK]++;
988 mreq = nfsm_reqhead(vp, NFSPROC_READLINK, NFSX_FH(v3));
989 mb = mreq;
990 bpos = mtod(mb, caddr_t);
991 nfsm_fhtom(vp, v3);
992 nfsm_request(vp, NFSPROC_READLINK, uiop->uio_td, cred);
993 if (v3)
994 nfsm_postop_attr(vp, attrflag);
995 if (!error) {
996 nfsm_strsiz(len, NFS_MAXPATHLEN);
997 if (len == NFS_MAXPATHLEN) {
998 struct nfsnode *np = VTONFS(vp);
999 if (np->n_size && np->n_size < NFS_MAXPATHLEN)
1000 len = np->n_size;
1001 }
1002 nfsm_mtouio(uiop, len);
1003 }
1004 m_freem(mrep);
1005 nfsmout:
1006 return (error);
1007 }
1008
1009 /*
1010 * nfs read rpc call
1011 * Ditto above
1012 */
1013 int
1014 nfs_readrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1015 {
1016 u_int32_t *tl;
1017 caddr_t bpos, dpos;
1018 struct mbuf *mreq, *mrep, *md, *mb;
1019 struct nfsmount *nmp;
1020 int error = 0, len, retlen, tsiz, eof, attrflag;
1021 int v3 = NFS_ISV3(vp);
1022
1023 #ifndef nolint
1024 eof = 0;
1025 #endif
1026 nmp = VFSTONFS(vp->v_mount);
1027 tsiz = uiop->uio_resid;
1028 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1029 return (EFBIG);
1030 while (tsiz > 0) {
1031 nfsstats.rpccnt[NFSPROC_READ]++;
1032 len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
1033 mreq = nfsm_reqhead(vp, NFSPROC_READ, NFSX_FH(v3) + NFSX_UNSIGNED * 3);
1034 mb = mreq;
1035 bpos = mtod(mb, caddr_t);
1036 nfsm_fhtom(vp, v3);
1037 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED * 3);
1038 if (v3) {
1039 txdr_hyper(uiop->uio_offset, tl);
1040 *(tl + 2) = txdr_unsigned(len);
1041 } else {
1042 *tl++ = txdr_unsigned(uiop->uio_offset);
1043 *tl++ = txdr_unsigned(len);
1044 *tl = 0;
1045 }
1046 nfsm_request(vp, NFSPROC_READ, uiop->uio_td, cred);
1047 if (v3) {
1048 nfsm_postop_attr(vp, attrflag);
1049 if (error) {
1050 m_freem(mrep);
1051 goto nfsmout;
1052 }
1053 tl = nfsm_dissect(u_int32_t *, 2 * NFSX_UNSIGNED);
1054 eof = fxdr_unsigned(int, *(tl + 1));
1055 } else
1056 nfsm_loadattr(vp, NULL);
1057 nfsm_strsiz(retlen, nmp->nm_rsize);
1058 nfsm_mtouio(uiop, retlen);
1059 m_freem(mrep);
1060 tsiz -= retlen;
1061 if (v3) {
1062 if (eof || retlen == 0) {
1063 tsiz = 0;
1064 }
1065 } else if (retlen < len) {
1066 tsiz = 0;
1067 }
1068 }
1069 nfsmout:
1070 return (error);
1071 }
1072
1073 /*
1074 * nfs write call
1075 */
1076 int
1077 nfs_writerpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
1078 int *iomode, int *must_commit)
1079 {
1080 u_int32_t *tl;
1081 int32_t backup;
1082 caddr_t bpos, dpos;
1083 struct mbuf *mreq, *mrep, *md, *mb;
1084 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1085 int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR, rlen, commit;
1086 int v3 = NFS_ISV3(vp), committed = NFSV3WRITE_FILESYNC;
1087
1088 #ifndef DIAGNOSTIC
1089 if (uiop->uio_iovcnt != 1)
1090 panic("nfs: writerpc iovcnt > 1");
1091 #endif
1092 *must_commit = 0;
1093 tsiz = uiop->uio_resid;
1094 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1095 return (EFBIG);
1096 while (tsiz > 0) {
1097 nfsstats.rpccnt[NFSPROC_WRITE]++;
1098 len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz;
1099 mreq = nfsm_reqhead(vp, NFSPROC_WRITE,
1100 NFSX_FH(v3) + 5 * NFSX_UNSIGNED + nfsm_rndup(len));
1101 mb = mreq;
1102 bpos = mtod(mb, caddr_t);
1103 nfsm_fhtom(vp, v3);
1104 if (v3) {
1105 tl = nfsm_build(u_int32_t *, 5 * NFSX_UNSIGNED);
1106 txdr_hyper(uiop->uio_offset, tl);
1107 tl += 2;
1108 *tl++ = txdr_unsigned(len);
1109 *tl++ = txdr_unsigned(*iomode);
1110 *tl = txdr_unsigned(len);
1111 } else {
1112 u_int32_t x;
1113
1114 tl = nfsm_build(u_int32_t *, 4 * NFSX_UNSIGNED);
1115 /* Set both "begin" and "current" to non-garbage. */
1116 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1117 *tl++ = x; /* "begin offset" */
1118 *tl++ = x; /* "current offset" */
1119 x = txdr_unsigned(len);
1120 *tl++ = x; /* total to this offset */
1121 *tl = x; /* size of this write */
1122 }
1123 nfsm_uiotom(uiop, len);
1124 nfsm_request(vp, NFSPROC_WRITE, uiop->uio_td, cred);
1125 if (v3) {
1126 wccflag = NFSV3_WCCCHK;
1127 nfsm_wcc_data(vp, wccflag);
1128 if (!error) {
1129 tl = nfsm_dissect(u_int32_t *, 2 * NFSX_UNSIGNED
1130 + NFSX_V3WRITEVERF);
1131 rlen = fxdr_unsigned(int, *tl++);
1132 if (rlen == 0) {
1133 error = NFSERR_IO;
1134 m_freem(mrep);
1135 break;
1136 } else if (rlen < len) {
1137 backup = len - rlen;
1138 uiop->uio_iov->iov_base =
1139 (char *)uiop->uio_iov->iov_base -
1140 backup;
1141 uiop->uio_iov->iov_len += backup;
1142 uiop->uio_offset -= backup;
1143 uiop->uio_resid += backup;
1144 len = rlen;
1145 }
1146 commit = fxdr_unsigned(int, *tl++);
1147
1148 /*
1149 * Return the lowest committment level
1150 * obtained by any of the RPCs.
1151 */
1152 if (committed == NFSV3WRITE_FILESYNC)
1153 committed = commit;
1154 else if (committed == NFSV3WRITE_DATASYNC &&
1155 commit == NFSV3WRITE_UNSTABLE)
1156 committed = commit;
1157 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0){
1158 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1159 NFSX_V3WRITEVERF);
1160 nmp->nm_state |= NFSSTA_HASWRITEVERF;
1161 } else if (bcmp((caddr_t)tl,
1162 (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF)) {
1163 *must_commit = 1;
1164 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1165 NFSX_V3WRITEVERF);
1166 }
1167 }
1168 } else
1169 nfsm_loadattr(vp, NULL);
1170 if (wccflag)
1171 VTONFS(vp)->n_mtime = VTONFS(vp)->n_vattr.va_mtime.tv_sec;
1172 m_freem(mrep);
1173 if (error)
1174 break;
1175 tsiz -= len;
1176 }
1177 nfsmout:
1178 if (vp->v_mount->mnt_flag & MNT_ASYNC)
1179 committed = NFSV3WRITE_FILESYNC;
1180 *iomode = committed;
1181 if (error)
1182 uiop->uio_resid = tsiz;
1183 return (error);
1184 }
1185
1186 /*
1187 * nfs mknod rpc
1188 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1189 * mode set to specify the file type and the size field for rdev.
1190 */
1191 static int
1192 nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1193 struct vattr *vap)
1194 {
1195 struct nfsv2_sattr *sp;
1196 u_int32_t *tl;
1197 struct vnode *newvp = NULL;
1198 struct nfsnode *np = NULL;
1199 struct vattr vattr;
1200 caddr_t bpos, dpos;
1201 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0;
1202 struct mbuf *mreq, *mrep, *md, *mb;
1203 u_int32_t rdev;
1204 int v3 = NFS_ISV3(dvp);
1205
1206 if (vap->va_type == VCHR || vap->va_type == VBLK)
1207 rdev = txdr_unsigned(vap->va_rdev);
1208 else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1209 rdev = nfs_xdrneg1;
1210 else {
1211 return (EOPNOTSUPP);
1212 }
1213 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred, cnp->cn_thread)) != 0) {
1214 return (error);
1215 }
1216 nfsstats.rpccnt[NFSPROC_MKNOD]++;
1217 mreq = nfsm_reqhead(dvp, NFSPROC_MKNOD, NFSX_FH(v3) + 4 * NFSX_UNSIGNED +
1218 + nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1219 mb = mreq;
1220 bpos = mtod(mb, caddr_t);
1221 nfsm_fhtom(dvp, v3);
1222 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1223 if (v3) {
1224 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
1225 *tl++ = vtonfsv3_type(vap->va_type);
1226 nfsm_v3attrbuild(vap, FALSE);
1227 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1228 tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED);
1229 *tl++ = txdr_unsigned(umajor(vap->va_rdev));
1230 *tl = txdr_unsigned(uminor(vap->va_rdev));
1231 }
1232 } else {
1233 sp = nfsm_build(struct nfsv2_sattr *, NFSX_V2SATTR);
1234 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1235 sp->sa_uid = nfs_xdrneg1;
1236 sp->sa_gid = nfs_xdrneg1;
1237 sp->sa_size = rdev;
1238 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1239 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1240 }
1241 nfsm_request(dvp, NFSPROC_MKNOD, cnp->cn_thread, cnp->cn_cred);
1242 if (!error) {
1243 nfsm_mtofh(dvp, newvp, v3, gotvp);
1244 if (!gotvp) {
1245 if (newvp) {
1246 vput(newvp);
1247 newvp = NULL;
1248 }
1249 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1250 cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread, &np);
1251 if (!error)
1252 newvp = NFSTOV(np);
1253 }
1254 }
1255 if (v3)
1256 nfsm_wcc_data(dvp, wccflag);
1257 m_freem(mrep);
1258 nfsmout:
1259 if (error) {
1260 if (newvp)
1261 vput(newvp);
1262 } else {
1263 if (cnp->cn_flags & MAKEENTRY)
1264 cache_enter(dvp, newvp, cnp);
1265 *vpp = newvp;
1266 }
1267 VTONFS(dvp)->n_flag |= NMODIFIED;
1268 if (!wccflag)
1269 VTONFS(dvp)->n_attrstamp = 0;
1270 return (error);
1271 }
1272
1273 /*
1274 * nfs mknod vop
1275 * just call nfs_mknodrpc() to do the work.
1276 */
1277 /* ARGSUSED */
1278 static int
1279 nfs_mknod(struct vop_mknod_args *ap)
1280 {
1281
1282 return nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap);
1283 }
1284
1285 static u_long create_verf;
1286 /*
1287 * nfs file create call
1288 */
1289 static int
1290 nfs_create(struct vop_create_args *ap)
1291 {
1292 struct vnode *dvp = ap->a_dvp;
1293 struct vattr *vap = ap->a_vap;
1294 struct componentname *cnp = ap->a_cnp;
1295 struct nfsv2_sattr *sp;
1296 u_int32_t *tl;
1297 struct nfsnode *np = NULL;
1298 struct vnode *newvp = NULL;
1299 caddr_t bpos, dpos;
1300 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0, fmode = 0;
1301 struct mbuf *mreq, *mrep, *md, *mb;
1302 struct vattr vattr;
1303 int v3 = NFS_ISV3(dvp);
1304
1305 /*
1306 * Oops, not for me..
1307 */
1308 if (vap->va_type == VSOCK)
1309 return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
1310
1311 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred, cnp->cn_thread)) != 0) {
1312 return (error);
1313 }
1314 if (vap->va_vaflags & VA_EXCLUSIVE)
1315 fmode |= O_EXCL;
1316 again:
1317 nfsstats.rpccnt[NFSPROC_CREATE]++;
1318 mreq = nfsm_reqhead(dvp, NFSPROC_CREATE, NFSX_FH(v3) + 2 * NFSX_UNSIGNED +
1319 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1320 mb = mreq;
1321 bpos = mtod(mb, caddr_t);
1322 nfsm_fhtom(dvp, v3);
1323 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1324 if (v3) {
1325 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
1326 if (fmode & O_EXCL) {
1327 *tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE);
1328 tl = nfsm_build(u_int32_t *, NFSX_V3CREATEVERF);
1329 #ifdef INET
1330 if (!TAILQ_EMPTY(&in_ifaddrhead))
1331 *tl++ = IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr.s_addr;
1332 else
1333 #endif
1334 *tl++ = create_verf;
1335 *tl = ++create_verf;
1336 } else {
1337 *tl = txdr_unsigned(NFSV3CREATE_UNCHECKED);
1338 nfsm_v3attrbuild(vap, FALSE);
1339 }
1340 } else {
1341 sp = nfsm_build(struct nfsv2_sattr *, NFSX_V2SATTR);
1342 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1343 sp->sa_uid = nfs_xdrneg1;
1344 sp->sa_gid = nfs_xdrneg1;
1345 sp->sa_size = 0;
1346 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1347 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1348 }
1349 nfsm_request(dvp, NFSPROC_CREATE, cnp->cn_thread, cnp->cn_cred);
1350 if (!error) {
1351 nfsm_mtofh(dvp, newvp, v3, gotvp);
1352 if (!gotvp) {
1353 if (newvp) {
1354 vput(newvp);
1355 newvp = NULL;
1356 }
1357 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1358 cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread, &np);
1359 if (!error)
1360 newvp = NFSTOV(np);
1361 }
1362 }
1363 if (v3)
1364 nfsm_wcc_data(dvp, wccflag);
1365 m_freem(mrep);
1366 nfsmout:
1367 if (error) {
1368 if (v3 && (fmode & O_EXCL) && error == NFSERR_NOTSUPP) {
1369 fmode &= ~O_EXCL;
1370 goto again;
1371 }
1372 if (newvp)
1373 vput(newvp);
1374 } else if (v3 && (fmode & O_EXCL)) {
1375 /*
1376 * We are normally called with only a partially initialized
1377 * VAP. Since the NFSv3 spec says that server may use the
1378 * file attributes to store the verifier, the spec requires
1379 * us to do a SETATTR RPC. FreeBSD servers store the verifier
1380 * in atime, but we can't really assume that all servers will
1381 * so we ensure that our SETATTR sets both atime and mtime.
1382 */
1383 if (vap->va_mtime.tv_sec == VNOVAL)
1384 vfs_timestamp(&vap->va_mtime);
1385 if (vap->va_atime.tv_sec == VNOVAL)
1386 vap->va_atime = vap->va_mtime;
1387 error = nfs_setattrrpc(newvp, vap, cnp->cn_cred, cnp->cn_thread);
1388 }
1389 if (!error) {
1390 if (cnp->cn_flags & MAKEENTRY)
1391 cache_enter(dvp, newvp, cnp);
1392 *ap->a_vpp = newvp;
1393 }
1394 VTONFS(dvp)->n_flag |= NMODIFIED;
1395 if (!wccflag)
1396 VTONFS(dvp)->n_attrstamp = 0;
1397 return (error);
1398 }
1399
1400 /*
1401 * nfs file remove call
1402 * To try and make nfs semantics closer to ufs semantics, a file that has
1403 * other processes using the vnode is renamed instead of removed and then
1404 * removed later on the last close.
1405 * - If v_usecount > 1
1406 * If a rename is not already in the works
1407 * call nfs_sillyrename() to set it up
1408 * else
1409 * do the remove rpc
1410 */
1411 static int
1412 nfs_remove(struct vop_remove_args *ap)
1413 {
1414 struct vnode *vp = ap->a_vp;
1415 struct vnode *dvp = ap->a_dvp;
1416 struct componentname *cnp = ap->a_cnp;
1417 struct nfsnode *np = VTONFS(vp);
1418 int error = 0;
1419 struct vattr vattr;
1420
1421 #ifndef DIAGNOSTIC
1422 if ((cnp->cn_flags & HASBUF) == 0)
1423 panic("nfs_remove: no name");
1424 if (vrefcnt(vp) < 1)
1425 panic("nfs_remove: bad v_usecount");
1426 #endif
1427 if (vp->v_type == VDIR)
1428 error = EPERM;
1429 else if (vrefcnt(vp) == 1 || (np->n_sillyrename &&
1430 VOP_GETATTR(vp, &vattr, cnp->cn_cred, cnp->cn_thread) == 0 &&
1431 vattr.va_nlink > 1)) {
1432 /*
1433 * Purge the name cache so that the chance of a lookup for
1434 * the name succeeding while the remove is in progress is
1435 * minimized. Without node locking it can still happen, such
1436 * that an I/O op returns ESTALE, but since you get this if
1437 * another host removes the file..
1438 */
1439 cache_purge(vp);
1440 /*
1441 * throw away biocache buffers, mainly to avoid
1442 * unnecessary delayed writes later.
1443 */
1444 error = nfs_vinvalbuf(vp, 0, cnp->cn_cred, cnp->cn_thread, 1);
1445 /* Do the rpc */
1446 if (error != EINTR)
1447 error = nfs_removerpc(dvp, cnp->cn_nameptr,
1448 cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread);
1449 /*
1450 * Kludge City: If the first reply to the remove rpc is lost..
1451 * the reply to the retransmitted request will be ENOENT
1452 * since the file was in fact removed
1453 * Therefore, we cheat and return success.
1454 */
1455 if (error == ENOENT)
1456 error = 0;
1457 } else if (!np->n_sillyrename)
1458 error = nfs_sillyrename(dvp, vp, cnp);
1459 np->n_attrstamp = 0;
1460 return (error);
1461 }
1462
1463 /*
1464 * nfs file remove rpc called from nfs_inactive
1465 */
1466 int
1467 nfs_removeit(struct sillyrename *sp)
1468 {
1469
1470 return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen, sp->s_cred,
1471 NULL));
1472 }
1473
1474 /*
1475 * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
1476 */
1477 static int
1478 nfs_removerpc(struct vnode *dvp, const char *name, int namelen,
1479 struct ucred *cred, struct thread *td)
1480 {
1481 caddr_t bpos, dpos;
1482 int error = 0, wccflag = NFSV3_WCCRATTR;
1483 struct mbuf *mreq, *mrep, *md, *mb;
1484 int v3 = NFS_ISV3(dvp);
1485
1486 nfsstats.rpccnt[NFSPROC_REMOVE]++;
1487 mreq = nfsm_reqhead(dvp, NFSPROC_REMOVE,
1488 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
1489 mb = mreq;
1490 bpos = mtod(mb, caddr_t);
1491 nfsm_fhtom(dvp, v3);
1492 nfsm_strtom(name, namelen, NFS_MAXNAMLEN);
1493 nfsm_request(dvp, NFSPROC_REMOVE, td, cred);
1494 if (v3)
1495 nfsm_wcc_data(dvp, wccflag);
1496 m_freem(mrep);
1497 nfsmout:
1498 VTONFS(dvp)->n_flag |= NMODIFIED;
1499 if (!wccflag)
1500 VTONFS(dvp)->n_attrstamp = 0;
1501 return (error);
1502 }
1503
1504 /*
1505 * nfs file rename call
1506 */
1507 static int
1508 nfs_rename(struct vop_rename_args *ap)
1509 {
1510 struct vnode *fvp = ap->a_fvp;
1511 struct vnode *tvp = ap->a_tvp;
1512 struct vnode *fdvp = ap->a_fdvp;
1513 struct vnode *tdvp = ap->a_tdvp;
1514 struct componentname *tcnp = ap->a_tcnp;
1515 struct componentname *fcnp = ap->a_fcnp;
1516 int error;
1517
1518 #ifndef DIAGNOSTIC
1519 if ((tcnp->cn_flags & HASBUF) == 0 ||
1520 (fcnp->cn_flags & HASBUF) == 0)
1521 panic("nfs_rename: no name");
1522 #endif
1523 /* Check for cross-device rename */
1524 if ((fvp->v_mount != tdvp->v_mount) ||
1525 (tvp && (fvp->v_mount != tvp->v_mount))) {
1526 error = EXDEV;
1527 goto out;
1528 }
1529
1530 /*
1531 * We have to flush B_DELWRI data prior to renaming
1532 * the file. If we don't, the delayed-write buffers
1533 * can be flushed out later after the file has gone stale
1534 * under NFSV3. NFSV2 does not have this problem because
1535 * ( as far as I can tell ) it flushes dirty buffers more
1536 * often.
1537 */
1538
1539 VOP_FSYNC(fvp, fcnp->cn_cred, MNT_WAIT, fcnp->cn_thread);
1540 if (tvp)
1541 VOP_FSYNC(tvp, tcnp->cn_cred, MNT_WAIT, tcnp->cn_thread);
1542
1543 /*
1544 * If the tvp exists and is in use, sillyrename it before doing the
1545 * rename of the new file over it.
1546 * XXX Can't sillyrename a directory.
1547 */
1548 if (tvp && vrefcnt(tvp) > 1 && !VTONFS(tvp)->n_sillyrename &&
1549 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
1550 vput(tvp);
1551 tvp = NULL;
1552 }
1553
1554 error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1555 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1556 tcnp->cn_thread);
1557
1558 if (fvp->v_type == VDIR) {
1559 if (tvp != NULL && tvp->v_type == VDIR)
1560 cache_purge(tdvp);
1561 cache_purge(fdvp);
1562 }
1563
1564 out:
1565 if (tdvp == tvp)
1566 vrele(tdvp);
1567 else
1568 vput(tdvp);
1569 if (tvp)
1570 vput(tvp);
1571 vrele(fdvp);
1572 vrele(fvp);
1573 /*
1574 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1575 */
1576 if (error == ENOENT)
1577 error = 0;
1578 return (error);
1579 }
1580
1581 /*
1582 * nfs file rename rpc called from nfs_remove() above
1583 */
1584 static int
1585 nfs_renameit(struct vnode *sdvp, struct componentname *scnp,
1586 struct sillyrename *sp)
1587 {
1588
1589 return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen, sdvp,
1590 sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_thread));
1591 }
1592
1593 /*
1594 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
1595 */
1596 static int
1597 nfs_renamerpc(struct vnode *fdvp, const char *fnameptr, int fnamelen,
1598 struct vnode *tdvp, const char *tnameptr, int tnamelen, struct ucred *cred,
1599 struct thread *td)
1600 {
1601 caddr_t bpos, dpos;
1602 int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR;
1603 struct mbuf *mreq, *mrep, *md, *mb;
1604 int v3 = NFS_ISV3(fdvp);
1605
1606 nfsstats.rpccnt[NFSPROC_RENAME]++;
1607 mreq = nfsm_reqhead(fdvp, NFSPROC_RENAME,
1608 (NFSX_FH(v3) + NFSX_UNSIGNED)*2 + nfsm_rndup(fnamelen) +
1609 nfsm_rndup(tnamelen));
1610 mb = mreq;
1611 bpos = mtod(mb, caddr_t);
1612 nfsm_fhtom(fdvp, v3);
1613 nfsm_strtom(fnameptr, fnamelen, NFS_MAXNAMLEN);
1614 nfsm_fhtom(tdvp, v3);
1615 nfsm_strtom(tnameptr, tnamelen, NFS_MAXNAMLEN);
1616 nfsm_request(fdvp, NFSPROC_RENAME, td, cred);
1617 if (v3) {
1618 nfsm_wcc_data(fdvp, fwccflag);
1619 nfsm_wcc_data(tdvp, twccflag);
1620 }
1621 m_freem(mrep);
1622 nfsmout:
1623 VTONFS(fdvp)->n_flag |= NMODIFIED;
1624 VTONFS(tdvp)->n_flag |= NMODIFIED;
1625 if (!fwccflag)
1626 VTONFS(fdvp)->n_attrstamp = 0;
1627 if (!twccflag)
1628 VTONFS(tdvp)->n_attrstamp = 0;
1629 return (error);
1630 }
1631
1632 /*
1633 * nfs hard link create call
1634 */
1635 static int
1636 nfs_link(struct vop_link_args *ap)
1637 {
1638 struct vnode *vp = ap->a_vp;
1639 struct vnode *tdvp = ap->a_tdvp;
1640 struct componentname *cnp = ap->a_cnp;
1641 caddr_t bpos, dpos;
1642 int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0;
1643 struct mbuf *mreq, *mrep, *md, *mb;
1644 int v3;
1645
1646 if (vp->v_mount != tdvp->v_mount) {
1647 return (EXDEV);
1648 }
1649
1650 /*
1651 * Push all writes to the server, so that the attribute cache
1652 * doesn't get "out of sync" with the server.
1653 * XXX There should be a better way!
1654 */
1655 VOP_FSYNC(vp, cnp->cn_cred, MNT_WAIT, cnp->cn_thread);
1656
1657 v3 = NFS_ISV3(vp);
1658 nfsstats.rpccnt[NFSPROC_LINK]++;
1659 mreq = nfsm_reqhead(vp, NFSPROC_LINK,
1660 NFSX_FH(v3)*2 + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
1661 mb = mreq;
1662 bpos = mtod(mb, caddr_t);
1663 nfsm_fhtom(vp, v3);
1664 nfsm_fhtom(tdvp, v3);
1665 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1666 nfsm_request(vp, NFSPROC_LINK, cnp->cn_thread, cnp->cn_cred);
1667 if (v3) {
1668 nfsm_postop_attr(vp, attrflag);
1669 nfsm_wcc_data(tdvp, wccflag);
1670 }
1671 m_freem(mrep);
1672 nfsmout:
1673 VTONFS(tdvp)->n_flag |= NMODIFIED;
1674 if (!attrflag)
1675 VTONFS(vp)->n_attrstamp = 0;
1676 if (!wccflag)
1677 VTONFS(tdvp)->n_attrstamp = 0;
1678 /*
1679 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
1680 */
1681 if (error == EEXIST)
1682 error = 0;
1683 return (error);
1684 }
1685
1686 /*
1687 * nfs symbolic link create call
1688 */
1689 static int
1690 nfs_symlink(struct vop_symlink_args *ap)
1691 {
1692 struct vnode *dvp = ap->a_dvp;
1693 struct vattr *vap = ap->a_vap;
1694 struct componentname *cnp = ap->a_cnp;
1695 struct nfsv2_sattr *sp;
1696 caddr_t bpos, dpos;
1697 int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp;
1698 struct mbuf *mreq, *mrep, *md, *mb;
1699 struct vnode *newvp = NULL;
1700 int v3 = NFS_ISV3(dvp);
1701
1702 nfsstats.rpccnt[NFSPROC_SYMLINK]++;
1703 slen = strlen(ap->a_target);
1704 mreq = nfsm_reqhead(dvp, NFSPROC_SYMLINK, NFSX_FH(v3) + 2*NFSX_UNSIGNED +
1705 nfsm_rndup(cnp->cn_namelen) + nfsm_rndup(slen) + NFSX_SATTR(v3));
1706 mb = mreq;
1707 bpos = mtod(mb, caddr_t);
1708 nfsm_fhtom(dvp, v3);
1709 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1710 if (v3) {
1711 nfsm_v3attrbuild(vap, FALSE);
1712 }
1713 nfsm_strtom(ap->a_target, slen, NFS_MAXPATHLEN);
1714 if (!v3) {
1715 sp = nfsm_build(struct nfsv2_sattr *, NFSX_V2SATTR);
1716 sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode);
1717 sp->sa_uid = nfs_xdrneg1;
1718 sp->sa_gid = nfs_xdrneg1;
1719 sp->sa_size = nfs_xdrneg1;
1720 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1721 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1722 }
1723
1724 /*
1725 * Issue the NFS request and get the rpc response.
1726 *
1727 * Only NFSv3 responses returning an error of 0 actually return
1728 * a file handle that can be converted into newvp without having
1729 * to do an extra lookup rpc.
1730 */
1731 nfsm_request(dvp, NFSPROC_SYMLINK, cnp->cn_thread, cnp->cn_cred);
1732 if (v3) {
1733 if (error == 0)
1734 nfsm_mtofh(dvp, newvp, v3, gotvp);
1735 nfsm_wcc_data(dvp, wccflag);
1736 }
1737
1738 /*
1739 * out code jumps -> here, mrep is also freed.
1740 */
1741
1742 m_freem(mrep);
1743 nfsmout:
1744
1745 /*
1746 * If we get an EEXIST error, silently convert it to no-error
1747 * in case of an NFS retry.
1748 */
1749 if (error == EEXIST)
1750 error = 0;
1751
1752 /*
1753 * If we do not have (or no longer have) an error, and we could
1754 * not extract the newvp from the response due to the request being
1755 * NFSv2 or the error being EEXIST. We have to do a lookup in order
1756 * to obtain a newvp to return.
1757 */
1758 if (error == 0 && newvp == NULL) {
1759 struct nfsnode *np = NULL;
1760
1761 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1762 cnp->cn_cred, cnp->cn_thread, &np);
1763 if (!error)
1764 newvp = NFSTOV(np);
1765 }
1766 if (error) {
1767 if (newvp)
1768 vput(newvp);
1769 } else {
1770 *ap->a_vpp = newvp;
1771 }
1772 VTONFS(dvp)->n_flag |= NMODIFIED;
1773 if (!wccflag)
1774 VTONFS(dvp)->n_attrstamp = 0;
1775 return (error);
1776 }
1777
1778 /*
1779 * nfs make dir call
1780 */
1781 static int
1782 nfs_mkdir(struct vop_mkdir_args *ap)
1783 {
1784 struct vnode *dvp = ap->a_dvp;
1785 struct vattr *vap = ap->a_vap;
1786 struct componentname *cnp = ap->a_cnp;
1787 struct nfsv2_sattr *sp;
1788 int len;
1789 struct nfsnode *np = NULL;
1790 struct vnode *newvp = NULL;
1791 caddr_t bpos, dpos;
1792 int error = 0, wccflag = NFSV3_WCCRATTR;
1793 int gotvp = 0;
1794 struct mbuf *mreq, *mrep, *md, *mb;
1795 struct vattr vattr;
1796 int v3 = NFS_ISV3(dvp);
1797
1798 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred, cnp->cn_thread)) != 0) {
1799 return (error);
1800 }
1801 len = cnp->cn_namelen;
1802 nfsstats.rpccnt[NFSPROC_MKDIR]++;
1803 mreq = nfsm_reqhead(dvp, NFSPROC_MKDIR,
1804 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len) + NFSX_SATTR(v3));
1805 mb = mreq;
1806 bpos = mtod(mb, caddr_t);
1807 nfsm_fhtom(dvp, v3);
1808 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
1809 if (v3) {
1810 nfsm_v3attrbuild(vap, FALSE);
1811 } else {
1812 sp = nfsm_build(struct nfsv2_sattr *, NFSX_V2SATTR);
1813 sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode);
1814 sp->sa_uid = nfs_xdrneg1;
1815 sp->sa_gid = nfs_xdrneg1;
1816 sp->sa_size = nfs_xdrneg1;
1817 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1818 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1819 }
1820 nfsm_request(dvp, NFSPROC_MKDIR, cnp->cn_thread, cnp->cn_cred);
1821 if (!error)
1822 nfsm_mtofh(dvp, newvp, v3, gotvp);
1823 if (v3)
1824 nfsm_wcc_data(dvp, wccflag);
1825 m_freem(mrep);
1826 nfsmout:
1827 VTONFS(dvp)->n_flag |= NMODIFIED;
1828 if (!wccflag)
1829 VTONFS(dvp)->n_attrstamp = 0;
1830 /*
1831 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
1832 * if we can succeed in looking up the directory.
1833 */
1834 if (error == EEXIST || (!error && !gotvp)) {
1835 if (newvp) {
1836 vrele(newvp);
1837 newvp = NULL;
1838 }
1839 error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred,
1840 cnp->cn_thread, &np);
1841 if (!error) {
1842 newvp = NFSTOV(np);
1843 if (newvp->v_type != VDIR)
1844 error = EEXIST;
1845 }
1846 }
1847 if (error) {
1848 if (newvp)
1849 vrele(newvp);
1850 } else
1851 *ap->a_vpp = newvp;
1852 return (error);
1853 }
1854
1855 /*
1856 * nfs remove directory call
1857 */
1858 static int
1859 nfs_rmdir(struct vop_rmdir_args *ap)
1860 {
1861 struct vnode *vp = ap->a_vp;
1862 struct vnode *dvp = ap->a_dvp;
1863 struct componentname *cnp = ap->a_cnp;
1864 caddr_t bpos, dpos;
1865 int error = 0, wccflag = NFSV3_WCCRATTR;
1866 struct mbuf *mreq, *mrep, *md, *mb;
1867 int v3 = NFS_ISV3(dvp);
1868
1869 if (dvp == vp)
1870 return (EINVAL);
1871 nfsstats.rpccnt[NFSPROC_RMDIR]++;
1872 mreq = nfsm_reqhead(dvp, NFSPROC_RMDIR,
1873 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
1874 mb = mreq;
1875 bpos = mtod(mb, caddr_t);
1876 nfsm_fhtom(dvp, v3);
1877 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1878 nfsm_request(dvp, NFSPROC_RMDIR, cnp->cn_thread, cnp->cn_cred);
1879 if (v3)
1880 nfsm_wcc_data(dvp, wccflag);
1881 m_freem(mrep);
1882 nfsmout:
1883 VTONFS(dvp)->n_flag |= NMODIFIED;
1884 if (!wccflag)
1885 VTONFS(dvp)->n_attrstamp = 0;
1886 cache_purge(dvp);
1887 cache_purge(vp);
1888 /*
1889 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
1890 */
1891 if (error == ENOENT)
1892 error = 0;
1893 return (error);
1894 }
1895
1896 /*
1897 * nfs readdir call
1898 */
1899 static int
1900 nfs_readdir(struct vop_readdir_args *ap)
1901 {
1902 struct vnode *vp = ap->a_vp;
1903 struct nfsnode *np = VTONFS(vp);
1904 struct uio *uio = ap->a_uio;
1905 int tresid, error;
1906 struct vattr vattr;
1907
1908 if (vp->v_type != VDIR)
1909 return (EPERM);
1910 /*
1911 * First, check for hit on the EOF offset cache
1912 */
1913 if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
1914 (np->n_flag & NMODIFIED) == 0) {
1915 if (VOP_GETATTR(vp, &vattr, ap->a_cred, uio->uio_td) == 0 &&
1916 np->n_mtime == vattr.va_mtime.tv_sec) {
1917 nfsstats.direofcache_hits++;
1918 return (0);
1919 }
1920 }
1921
1922 /*
1923 * Call nfs_bioread() to do the real work.
1924 */
1925 tresid = uio->uio_resid;
1926 error = nfs_bioread(vp, uio, 0, ap->a_cred);
1927
1928 if (!error && uio->uio_resid == tresid)
1929 nfsstats.direofcache_misses++;
1930 return (error);
1931 }
1932
1933 /*
1934 * Readdir rpc call.
1935 * Called from below the buffer cache by nfs_doio().
1936 */
1937 int
1938 nfs_readdirrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1939 {
1940 int len, left;
1941 struct dirent *dp = NULL;
1942 u_int32_t *tl;
1943 caddr_t cp;
1944 nfsuint64 *cookiep;
1945 caddr_t bpos, dpos;
1946 struct mbuf *mreq, *mrep, *md, *mb;
1947 nfsuint64 cookie;
1948 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1949 struct nfsnode *dnp = VTONFS(vp);
1950 u_quad_t fileno;
1951 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
1952 int attrflag;
1953 int v3 = NFS_ISV3(vp);
1954
1955 #ifndef DIAGNOSTIC
1956 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
1957 (uiop->uio_resid & (DIRBLKSIZ - 1)))
1958 panic("nfs readdirrpc bad uio");
1959 #endif
1960
1961 /*
1962 * If there is no cookie, assume directory was stale.
1963 */
1964 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
1965 if (cookiep)
1966 cookie = *cookiep;
1967 else
1968 return (NFSERR_BAD_COOKIE);
1969 /*
1970 * Loop around doing readdir rpc's of size nm_readdirsize
1971 * truncated to a multiple of DIRBLKSIZ.
1972 * The stopping criteria is EOF or buffer full.
1973 */
1974 while (more_dirs && bigenough) {
1975 nfsstats.rpccnt[NFSPROC_READDIR]++;
1976 mreq = nfsm_reqhead(vp, NFSPROC_READDIR, NFSX_FH(v3) +
1977 NFSX_READDIR(v3));
1978 mb = mreq;
1979 bpos = mtod(mb, caddr_t);
1980 nfsm_fhtom(vp, v3);
1981 if (v3) {
1982 tl = nfsm_build(u_int32_t *, 5 * NFSX_UNSIGNED);
1983 *tl++ = cookie.nfsuquad[0];
1984 *tl++ = cookie.nfsuquad[1];
1985 *tl++ = dnp->n_cookieverf.nfsuquad[0];
1986 *tl++ = dnp->n_cookieverf.nfsuquad[1];
1987 } else {
1988 tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED);
1989 *tl++ = cookie.nfsuquad[0];
1990 }
1991 *tl = txdr_unsigned(nmp->nm_readdirsize);
1992 nfsm_request(vp, NFSPROC_READDIR, uiop->uio_td, cred);
1993 if (v3) {
1994 nfsm_postop_attr(vp, attrflag);
1995 if (!error) {
1996 tl = nfsm_dissect(u_int32_t *,
1997 2 * NFSX_UNSIGNED);
1998 dnp->n_cookieverf.nfsuquad[0] = *tl++;
1999 dnp->n_cookieverf.nfsuquad[1] = *tl;
2000 } else {
2001 m_freem(mrep);
2002 goto nfsmout;
2003 }
2004 }
2005 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2006 more_dirs = fxdr_unsigned(int, *tl);
2007
2008 /* loop thru the dir entries, doctoring them to 4bsd form */
2009 while (more_dirs && bigenough) {
2010 if (v3) {
2011 tl = nfsm_dissect(u_int32_t *,
2012 3 * NFSX_UNSIGNED);
2013 fileno = fxdr_hyper(tl);
2014 len = fxdr_unsigned(int, *(tl + 2));
2015 } else {
2016 tl = nfsm_dissect(u_int32_t *,
2017 2 * NFSX_UNSIGNED);
2018 fileno = fxdr_unsigned(u_quad_t, *tl++);
2019 len = fxdr_unsigned(int, *tl);
2020 }
2021 if (len <= 0 || len > NFS_MAXNAMLEN) {
2022 error = EBADRPC;
2023 m_freem(mrep);
2024 goto nfsmout;
2025 }
2026 tlen = nfsm_rndup(len);
2027 if (tlen == len)
2028 tlen += 4; /* To ensure null termination */
2029 left = DIRBLKSIZ - blksiz;
2030 if ((tlen + DIRHDSIZ) > left) {
2031 dp->d_reclen += left;
2032 uiop->uio_iov->iov_base =
2033 (char *)uiop->uio_iov->iov_base + left;
2034 uiop->uio_iov->iov_len -= left;
2035 uiop->uio_offset += left;
2036 uiop->uio_resid -= left;
2037 blksiz = 0;
2038 }
2039 if ((tlen + DIRHDSIZ) > uiop->uio_resid)
2040 bigenough = 0;
2041 if (bigenough) {
2042 dp = (struct dirent *)uiop->uio_iov->iov_base;
2043 dp->d_fileno = (int)fileno;
2044 dp->d_namlen = len;
2045 dp->d_reclen = tlen + DIRHDSIZ;
2046 dp->d_type = DT_UNKNOWN;
2047 blksiz += dp->d_reclen;
2048 if (blksiz == DIRBLKSIZ)
2049 blksiz = 0;
2050 uiop->uio_offset += DIRHDSIZ;
2051 uiop->uio_resid -= DIRHDSIZ;
2052 uiop->uio_iov->iov_base =
2053 (char *)uiop->uio_iov->iov_base + DIRHDSIZ;
2054 uiop->uio_iov->iov_len -= DIRHDSIZ;
2055 nfsm_mtouio(uiop, len);
2056 cp = uiop->uio_iov->iov_base;
2057 tlen -= len;
2058 *cp = '\0'; /* null terminate */
2059 uiop->uio_iov->iov_base =
2060 (char *)uiop->uio_iov->iov_base + tlen;
2061 uiop->uio_iov->iov_len -= tlen;
2062 uiop->uio_offset += tlen;
2063 uiop->uio_resid -= tlen;
2064 } else
2065 nfsm_adv(nfsm_rndup(len));
2066 if (v3) {
2067 tl = nfsm_dissect(u_int32_t *,
2068 3 * NFSX_UNSIGNED);
2069 } else {
2070 tl = nfsm_dissect(u_int32_t *,
2071 2 * NFSX_UNSIGNED);
2072 }
2073 if (bigenough) {
2074 cookie.nfsuquad[0] = *tl++;
2075 if (v3)
2076 cookie.nfsuquad[1] = *tl++;
2077 } else if (v3)
2078 tl += 2;
2079 else
2080 tl++;
2081 more_dirs = fxdr_unsigned(int, *tl);
2082 }
2083 /*
2084 * If at end of rpc data, get the eof boolean
2085 */
2086 if (!more_dirs) {
2087 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2088 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2089 }
2090 m_freem(mrep);
2091 }
2092 /*
2093 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2094 * by increasing d_reclen for the last record.
2095 */
2096 if (blksiz > 0) {
2097 left = DIRBLKSIZ - blksiz;
2098 dp->d_reclen += left;
2099 uiop->uio_iov->iov_base =
2100 (char *)uiop->uio_iov->iov_base + left;
2101 uiop->uio_iov->iov_len -= left;
2102 uiop->uio_offset += left;
2103 uiop->uio_resid -= left;
2104 }
2105
2106 /*
2107 * We are now either at the end of the directory or have filled the
2108 * block.
2109 */
2110 if (bigenough)
2111 dnp->n_direofoffset = uiop->uio_offset;
2112 else {
2113 if (uiop->uio_resid > 0)
2114 printf("EEK! readdirrpc resid > 0\n");
2115 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2116 *cookiep = cookie;
2117 }
2118 nfsmout:
2119 return (error);
2120 }
2121
2122 /*
2123 * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
2124 */
2125 int
2126 nfs_readdirplusrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
2127 {
2128 int len, left;
2129 struct dirent *dp;
2130 u_int32_t *tl;
2131 caddr_t cp;
2132 struct vnode *newvp;
2133 nfsuint64 *cookiep;
2134 caddr_t bpos, dpos, dpossav1, dpossav2;
2135 struct mbuf *mreq, *mrep, *md, *mb, *mdsav1, *mdsav2;
2136 struct nameidata nami, *ndp = &nami;
2137 struct componentname *cnp = &ndp->ni_cnd;
2138 nfsuint64 cookie;
2139 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2140 struct nfsnode *dnp = VTONFS(vp), *np;
2141 nfsfh_t *fhp;
2142 u_quad_t fileno;
2143 int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
2144 int attrflag, fhsize;
2145
2146 #ifndef nolint
2147 dp = NULL;
2148 #endif
2149 #ifndef DIAGNOSTIC
2150 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2151 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2152 panic("nfs readdirplusrpc bad uio");
2153 #endif
2154 ndp->ni_dvp = vp;
2155 newvp = NULLVP;
2156
2157 /*
2158 * If there is no cookie, assume directory was stale.
2159 */
2160 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2161 if (cookiep)
2162 cookie = *cookiep;
2163 else
2164 return (NFSERR_BAD_COOKIE);
2165 /*
2166 * Loop around doing readdir rpc's of size nm_readdirsize
2167 * truncated to a multiple of DIRBLKSIZ.
2168 * The stopping criteria is EOF or buffer full.
2169 */
2170 while (more_dirs && bigenough) {
2171 nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
2172 mreq = nfsm_reqhead(vp, NFSPROC_READDIRPLUS,
2173 NFSX_FH(1) + 6 * NFSX_UNSIGNED);
2174 mb = mreq;
2175 bpos = mtod(mb, caddr_t);
2176 nfsm_fhtom(vp, 1);
2177 tl = nfsm_build(u_int32_t *, 6 * NFSX_UNSIGNED);
2178 *tl++ = cookie.nfsuquad[0];
2179 *tl++ = cookie.nfsuquad[1];
2180 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2181 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2182 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
2183 *tl = txdr_unsigned(nmp->nm_rsize);
2184 nfsm_request(vp, NFSPROC_READDIRPLUS, uiop->uio_td, cred);
2185 nfsm_postop_attr(vp, attrflag);
2186 if (error) {
2187 m_freem(mrep);
2188 goto nfsmout;
2189 }
2190 tl = nfsm_dissect(u_int32_t *, 3 * NFSX_UNSIGNED);
2191 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2192 dnp->n_cookieverf.nfsuquad[1] = *tl++;
2193 more_dirs = fxdr_unsigned(int, *tl);
2194
2195 /* loop thru the dir entries, doctoring them to 4bsd form */
2196 while (more_dirs && bigenough) {
2197 tl = nfsm_dissect(u_int32_t *, 3 * NFSX_UNSIGNED);
2198 fileno = fxdr_hyper(tl);
2199 len = fxdr_unsigned(int, *(tl + 2));
2200 if (len <= 0 || len > NFS_MAXNAMLEN) {
2201 error = EBADRPC;
2202 m_freem(mrep);
2203 goto nfsmout;
2204 }
2205 tlen = nfsm_rndup(len);
2206 if (tlen == len)
2207 tlen += 4; /* To ensure null termination*/
2208 left = DIRBLKSIZ - blksiz;
2209 if ((tlen + DIRHDSIZ) > left) {
2210 dp->d_reclen += left;
2211 uiop->uio_iov->iov_base =
2212 (char *)uiop->uio_iov->iov_base + left;
2213 uiop->uio_iov->iov_len -= left;
2214 uiop->uio_offset += left;
2215 uiop->uio_resid -= left;
2216 blksiz = 0;
2217 }
2218 if ((tlen + DIRHDSIZ) > uiop->uio_resid)
2219 bigenough = 0;
2220 if (bigenough) {
2221 dp = (struct dirent *)uiop->uio_iov->iov_base;
2222 dp->d_fileno = (int)fileno;
2223 dp->d_namlen = len;
2224 dp->d_reclen = tlen + DIRHDSIZ;
2225 dp->d_type = DT_UNKNOWN;
2226 blksiz += dp->d_reclen;
2227 if (blksiz == DIRBLKSIZ)
2228 blksiz = 0;
2229 uiop->uio_offset += DIRHDSIZ;
2230 uiop->uio_resid -= DIRHDSIZ;
2231 uiop->uio_iov->iov_base =
2232 (char *)uiop->uio_iov->iov_base + DIRHDSIZ;
2233 uiop->uio_iov->iov_len -= DIRHDSIZ;
2234 cnp->cn_nameptr = uiop->uio_iov->iov_base;
2235 cnp->cn_namelen = len;
2236 nfsm_mtouio(uiop, len);
2237 cp = uiop->uio_iov->iov_base;
2238 tlen -= len;
2239 *cp = '\0';
2240 uiop->uio_iov->iov_base =
2241 (char *)uiop->uio_iov->iov_base + tlen;
2242 uiop->uio_iov->iov_len -= tlen;
2243 uiop->uio_offset += tlen;
2244 uiop->uio_resid -= tlen;
2245 } else
2246 nfsm_adv(nfsm_rndup(len));
2247 tl = nfsm_dissect(u_int32_t *, 3 * NFSX_UNSIGNED);
2248 if (bigenough) {
2249 cookie.nfsuquad[0] = *tl++;
2250 cookie.nfsuquad[1] = *tl++;
2251 } else
2252 tl += 2;
2253
2254 /*
2255 * Since the attributes are before the file handle
2256 * (sigh), we must skip over the attributes and then
2257 * come back and get them.
2258 */
2259 attrflag = fxdr_unsigned(int, *tl);
2260 if (attrflag) {
2261 dpossav1 = dpos;
2262 mdsav1 = md;
2263 nfsm_adv(NFSX_V3FATTR);
2264 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2265 doit = fxdr_unsigned(int, *tl);
2266 if (doit) {
2267 nfsm_getfh(fhp, fhsize, 1);
2268 if (NFS_CMPFH(dnp, fhp, fhsize)) {
2269 VREF(vp);
2270 newvp = vp;
2271 np = dnp;
2272 } else {
2273 error = nfs_nget(vp->v_mount, fhp,
2274 fhsize, &np);
2275 if (error)
2276 doit = 0;
2277 else
2278 newvp = NFSTOV(np);
2279 }
2280 }
2281 if (doit && bigenough) {
2282 dpossav2 = dpos;
2283 dpos = dpossav1;
2284 mdsav2 = md;
2285 md = mdsav1;
2286 nfsm_loadattr(newvp, NULL);
2287 dpos = dpossav2;
2288 md = mdsav2;
2289 dp->d_type =
2290 IFTODT(VTTOIF(np->n_vattr.va_type));
2291 ndp->ni_vp = newvp;
2292 cache_enter(ndp->ni_dvp, ndp->ni_vp, cnp);
2293 }
2294 } else {
2295 /* Just skip over the file handle */
2296 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2297 i = fxdr_unsigned(int, *tl);
2298 nfsm_adv(nfsm_rndup(i));
2299 }
2300 if (newvp != NULLVP) {
2301 if (newvp == vp)
2302 vrele(newvp);
2303 else
2304 vput(newvp);
2305 newvp = NULLVP;
2306 }
2307 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2308 more_dirs = fxdr_unsigned(int, *tl);
2309 }
2310 /*
2311 * If at end of rpc data, get the eof boolean
2312 */
2313 if (!more_dirs) {
2314 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2315 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2316 }
2317 m_freem(mrep);
2318 }
2319 /*
2320 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2321 * by increasing d_reclen for the last record.
2322 */
2323 if (blksiz > 0) {
2324 left = DIRBLKSIZ - blksiz;
2325 dp->d_reclen += left;
2326 uiop->uio_iov->iov_base =
2327 (char *)uiop->uio_iov->iov_base + left;
2328 uiop->uio_iov->iov_len -= left;
2329 uiop->uio_offset += left;
2330 uiop->uio_resid -= left;
2331 }
2332
2333 /*
2334 * We are now either at the end of the directory or have filled the
2335 * block.
2336 */
2337 if (bigenough)
2338 dnp->n_direofoffset = uiop->uio_offset;
2339 else {
2340 if (uiop->uio_resid > 0)
2341 printf("EEK! readdirplusrpc resid > 0\n");
2342 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2343 *cookiep = cookie;
2344 }
2345 nfsmout:
2346 if (newvp != NULLVP) {
2347 if (newvp == vp)
2348 vrele(newvp);
2349 else
2350 vput(newvp);
2351 newvp = NULLVP;
2352 }
2353 return (error);
2354 }
2355
2356 /*
2357 * Silly rename. To make the NFS filesystem that is stateless look a little
2358 * more like the "ufs" a remove of an active vnode is translated to a rename
2359 * to a funny looking filename that is removed by nfs_inactive on the
2360 * nfsnode. There is the potential for another process on a different client
2361 * to create the same funny name between the nfs_lookitup() fails and the
2362 * nfs_rename() completes, but...
2363 */
2364 static int
2365 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2366 {
2367 struct sillyrename *sp;
2368 struct nfsnode *np;
2369 int error;
2370 short pid;
2371
2372 cache_purge(dvp);
2373 np = VTONFS(vp);
2374 #ifndef DIAGNOSTIC
2375 if (vp->v_type == VDIR)
2376 panic("nfs: sillyrename dir");
2377 #endif
2378 MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename),
2379 M_NFSREQ, M_WAITOK);
2380 sp->s_cred = crhold(cnp->cn_cred);
2381 sp->s_dvp = dvp;
2382 VREF(dvp);
2383
2384 /* Fudge together a funny name */
2385 pid = cnp->cn_thread->td_proc->p_pid;
2386 sp->s_namlen = sprintf(sp->s_name, ".nfsA%04x4.4", pid);
2387
2388 /* Try lookitups until we get one that isn't there */
2389 while (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2390 cnp->cn_thread, NULL) == 0) {
2391 sp->s_name[4]++;
2392 if (sp->s_name[4] > 'z') {
2393 error = EINVAL;
2394 goto bad;
2395 }
2396 }
2397 error = nfs_renameit(dvp, cnp, sp);
2398 if (error)
2399 goto bad;
2400 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2401 cnp->cn_thread, &np);
2402 np->n_sillyrename = sp;
2403 return (0);
2404 bad:
2405 vrele(sp->s_dvp);
2406 crfree(sp->s_cred);
2407 free((caddr_t)sp, M_NFSREQ);
2408 return (error);
2409 }
2410
2411 /*
2412 * Look up a file name and optionally either update the file handle or
2413 * allocate an nfsnode, depending on the value of npp.
2414 * npp == NULL --> just do the lookup
2415 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2416 * handled too
2417 * *npp != NULL --> update the file handle in the vnode
2418 */
2419 static int
2420 nfs_lookitup(struct vnode *dvp, const char *name, int len, struct ucred *cred,
2421 struct thread *td, struct nfsnode **npp)
2422 {
2423 struct vnode *newvp = NULL;
2424 struct nfsnode *np, *dnp = VTONFS(dvp);
2425 caddr_t bpos, dpos;
2426 int error = 0, fhlen, attrflag;
2427 struct mbuf *mreq, *mrep, *md, *mb;
2428 nfsfh_t *nfhp;
2429 int v3 = NFS_ISV3(dvp);
2430
2431 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
2432 mreq = nfsm_reqhead(dvp, NFSPROC_LOOKUP,
2433 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
2434 mb = mreq;
2435 bpos = mtod(mb, caddr_t);
2436 nfsm_fhtom(dvp, v3);
2437 nfsm_strtom(name, len, NFS_MAXNAMLEN);
2438 nfsm_request(dvp, NFSPROC_LOOKUP, td, cred);
2439 if (npp && !error) {
2440 nfsm_getfh(nfhp, fhlen, v3);
2441 if (*npp) {
2442 np = *npp;
2443 if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
2444 free((caddr_t)np->n_fhp, M_NFSBIGFH);
2445 np->n_fhp = &np->n_fh;
2446 } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH)
2447 np->n_fhp =(nfsfh_t *)malloc(fhlen, M_NFSBIGFH, M_WAITOK);
2448 bcopy((caddr_t)nfhp, (caddr_t)np->n_fhp, fhlen);
2449 np->n_fhsize = fhlen;
2450 newvp = NFSTOV(np);
2451 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
2452 VREF(dvp);
2453 newvp = dvp;
2454 } else {
2455 error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np);
2456 if (error) {
2457 m_freem(mrep);
2458 return (error);
2459 }
2460 newvp = NFSTOV(np);
2461 }
2462 if (v3) {
2463 nfsm_postop_attr(newvp, attrflag);
2464 if (!attrflag && *npp == NULL) {
2465 m_freem(mrep);
2466 if (newvp == dvp)
2467 vrele(newvp);
2468 else
2469 vput(newvp);
2470 return (ENOENT);
2471 }
2472 } else
2473 nfsm_loadattr(newvp, NULL);
2474 }
2475 m_freem(mrep);
2476 nfsmout:
2477 if (npp && *npp == NULL) {
2478 if (error) {
2479 if (newvp) {
2480 if (newvp == dvp)
2481 vrele(newvp);
2482 else
2483 vput(newvp);
2484 }
2485 } else
2486 *npp = np;
2487 }
2488 return (error);
2489 }
2490
2491 /*
2492 * Nfs Version 3 commit rpc
2493 */
2494 int
2495 nfs_commit(struct vnode *vp, u_quad_t offset, int cnt, struct ucred *cred,
2496 struct thread *td)
2497 {
2498 u_int32_t *tl;
2499 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2500 caddr_t bpos, dpos;
2501 int error = 0, wccflag = NFSV3_WCCRATTR;
2502 struct mbuf *mreq, *mrep, *md, *mb;
2503
2504 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0)
2505 return (0);
2506 nfsstats.rpccnt[NFSPROC_COMMIT]++;
2507 mreq = nfsm_reqhead(vp, NFSPROC_COMMIT, NFSX_FH(1));
2508 mb = mreq;
2509 bpos = mtod(mb, caddr_t);
2510 nfsm_fhtom(vp, 1);
2511 tl = nfsm_build(u_int32_t *, 3 * NFSX_UNSIGNED);
2512 txdr_hyper(offset, tl);
2513 tl += 2;
2514 *tl = txdr_unsigned(cnt);
2515 nfsm_request(vp, NFSPROC_COMMIT, td, cred);
2516 nfsm_wcc_data(vp, wccflag);
2517 if (!error) {
2518 tl = nfsm_dissect(u_int32_t *, NFSX_V3WRITEVERF);
2519 if (bcmp((caddr_t)nmp->nm_verf, (caddr_t)tl,
2520 NFSX_V3WRITEVERF)) {
2521 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
2522 NFSX_V3WRITEVERF);
2523 error = NFSERR_STALEWRITEVERF;
2524 }
2525 }
2526 m_freem(mrep);
2527 nfsmout:
2528 return (error);
2529 }
2530
2531 /*
2532 * Strategy routine.
2533 * For async requests when nfsiod(s) are running, queue the request by
2534 * calling nfs_asyncio(), otherwise just all nfs_doio() to do the
2535 * request.
2536 */
2537 static int
2538 nfs_strategy(struct vop_strategy_args *ap)
2539 {
2540 struct buf *bp = ap->a_bp;
2541 struct ucred *cr;
2542 struct thread *td;
2543 int error = 0;
2544
2545 KASSERT(!(bp->b_flags & B_DONE), ("nfs_strategy: buffer %p unexpectedly marked B_DONE", bp));
2546 KASSERT(BUF_REFCNT(bp) > 0, ("nfs_strategy: buffer %p not locked", bp));
2547
2548 if (bp->b_flags & B_PHYS)
2549 panic("nfs physio");
2550
2551 if (bp->b_flags & B_ASYNC)
2552 td = NULL;
2553 else
2554 td = curthread; /* XXX */
2555
2556 if (bp->b_iocmd == BIO_READ)
2557 cr = bp->b_rcred;
2558 else
2559 cr = bp->b_wcred;
2560
2561 /*
2562 * If the op is asynchronous and an i/o daemon is waiting
2563 * queue the request, wake it up and wait for completion
2564 * otherwise just do it ourselves.
2565 */
2566 if ((bp->b_flags & B_ASYNC) == 0 ||
2567 nfs_asyncio(bp, NOCRED, td))
2568 error = nfs_doio(bp, cr, td);
2569 return (error);
2570 }
2571
2572 /*
2573 * fsync vnode op. Just call nfs_flush() with commit == 1.
2574 */
2575 /* ARGSUSED */
2576 static int
2577 nfs_fsync(struct vop_fsync_args *ap)
2578 {
2579
2580 return (nfs_flush(ap->a_vp, ap->a_cred, ap->a_waitfor, ap->a_td, 1));
2581 }
2582
2583 /*
2584 * Flush all the blocks associated with a vnode.
2585 * Walk through the buffer pool and push any dirty pages
2586 * associated with the vnode.
2587 */
2588 static int
2589 nfs_flush(struct vnode *vp, struct ucred *cred, int waitfor, struct thread *td,
2590 int commit)
2591 {
2592 struct nfsnode *np = VTONFS(vp);
2593 struct buf *bp;
2594 int i;
2595 struct buf *nbp;
2596 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2597 int s, error = 0, slptimeo = 0, slpflag = 0, retv, bvecpos;
2598 int passone = 1;
2599 u_quad_t off, endoff, toff;
2600 struct ucred* wcred = NULL;
2601 struct buf **bvec = NULL;
2602 #ifndef NFS_COMMITBVECSIZ
2603 #define NFS_COMMITBVECSIZ 20
2604 #endif
2605 struct buf *bvec_on_stack[NFS_COMMITBVECSIZ];
2606 int bvecsize = 0, bveccount;
2607
2608 if (nmp->nm_flag & NFSMNT_INT)
2609 slpflag = PCATCH;
2610 if (!commit)
2611 passone = 0;
2612 /*
2613 * A b_flags == (B_DELWRI | B_NEEDCOMMIT) block has been written to the
2614 * server, but nas not been committed to stable storage on the server
2615 * yet. On the first pass, the byte range is worked out and the commit
2616 * rpc is done. On the second pass, nfs_writebp() is called to do the
2617 * job.
2618 */
2619 again:
2620 off = (u_quad_t)-1;
2621 endoff = 0;
2622 bvecpos = 0;
2623 if (NFS_ISV3(vp) && commit) {
2624 s = splbio();
2625 /*
2626 * Count up how many buffers waiting for a commit.
2627 */
2628 bveccount = 0;
2629 VI_LOCK(vp);
2630 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
2631 nbp = TAILQ_NEXT(bp, b_vnbufs);
2632 if (BUF_REFCNT(bp) == 0 &&
2633 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
2634 == (B_DELWRI | B_NEEDCOMMIT))
2635 bveccount++;
2636 }
2637 /*
2638 * Allocate space to remember the list of bufs to commit. It is
2639 * important to use M_NOWAIT here to avoid a race with nfs_write.
2640 * If we can't get memory (for whatever reason), we will end up
2641 * committing the buffers one-by-one in the loop below.
2642 */
2643 if (bvec != NULL && bvec != bvec_on_stack)
2644 free(bvec, M_TEMP);
2645 if (bveccount > NFS_COMMITBVECSIZ) {
2646 bvec = (struct buf **)
2647 malloc(bveccount * sizeof(struct buf *),
2648 M_TEMP, M_NOWAIT);
2649 if (bvec == NULL) {
2650 bvec = bvec_on_stack;
2651 bvecsize = NFS_COMMITBVECSIZ;
2652 } else
2653 bvecsize = bveccount;
2654 } else {
2655 bvec = bvec_on_stack;
2656 bvecsize = NFS_COMMITBVECSIZ;
2657 }
2658 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
2659 if (bvecpos >= bvecsize)
2660 break;
2661 VI_UNLOCK(vp);
2662 if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
2663 (B_DELWRI | B_NEEDCOMMIT) ||
2664 BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
2665 VI_LOCK(vp);
2666 nbp = TAILQ_NEXT(bp, b_vnbufs);
2667 continue;
2668 }
2669 bremfree(bp);
2670 /*
2671 * Work out if all buffers are using the same cred
2672 * so we can deal with them all with one commit.
2673 *
2674 * NOTE: we are not clearing B_DONE here, so we have
2675 * to do it later on in this routine if we intend to
2676 * initiate I/O on the bp.
2677 *
2678 * Note: to avoid loopback deadlocks, we do not
2679 * assign b_runningbufspace.
2680 */
2681 if (wcred == NULL)
2682 wcred = bp->b_wcred;
2683 else if (wcred != bp->b_wcred)
2684 wcred = NOCRED;
2685 bp->b_flags |= B_WRITEINPROG;
2686 vfs_busy_pages(bp, 1);
2687
2688 VI_LOCK(vp);
2689 /*
2690 * bp is protected by being locked, but nbp is not
2691 * and vfs_busy_pages() may sleep. We have to
2692 * recalculate nbp.
2693 */
2694 nbp = TAILQ_NEXT(bp, b_vnbufs);
2695
2696 /*
2697 * A list of these buffers is kept so that the
2698 * second loop knows which buffers have actually
2699 * been committed. This is necessary, since there
2700 * may be a race between the commit rpc and new
2701 * uncommitted writes on the file.
2702 */
2703 bvec[bvecpos++] = bp;
2704 toff = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2705 bp->b_dirtyoff;
2706 if (toff < off)
2707 off = toff;
2708 toff += (u_quad_t)(bp->b_dirtyend - bp->b_dirtyoff);
2709 if (toff > endoff)
2710 endoff = toff;
2711 }
2712 splx(s);
2713 VI_UNLOCK(vp);
2714 }
2715 if (bvecpos > 0) {
2716 /*
2717 * Commit data on the server, as required.
2718 * If all bufs are using the same wcred, then use that with
2719 * one call for all of them, otherwise commit each one
2720 * separately.
2721 */
2722 if (wcred != NOCRED)
2723 retv = nfs_commit(vp, off, (int)(endoff - off),
2724 wcred, td);
2725 else {
2726 retv = 0;
2727 for (i = 0; i < bvecpos; i++) {
2728 off_t off, size;
2729 bp = bvec[i];
2730 off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2731 bp->b_dirtyoff;
2732 size = (u_quad_t)(bp->b_dirtyend
2733 - bp->b_dirtyoff);
2734 retv = nfs_commit(vp, off, (int)size,
2735 bp->b_wcred, td);
2736 if (retv) break;
2737 }
2738 }
2739
2740 if (retv == NFSERR_STALEWRITEVERF)
2741 nfs_clearcommit(vp->v_mount);
2742
2743 /*
2744 * Now, either mark the blocks I/O done or mark the
2745 * blocks dirty, depending on whether the commit
2746 * succeeded.
2747 */
2748 for (i = 0; i < bvecpos; i++) {
2749 bp = bvec[i];
2750 bp->b_flags &= ~(B_NEEDCOMMIT | B_WRITEINPROG | B_CLUSTEROK);
2751 if (retv) {
2752 /*
2753 * Error, leave B_DELWRI intact
2754 */
2755 vfs_unbusy_pages(bp);
2756 brelse(bp);
2757 } else {
2758 /*
2759 * Success, remove B_DELWRI ( bundirty() ).
2760 *
2761 * b_dirtyoff/b_dirtyend seem to be NFS
2762 * specific. We should probably move that
2763 * into bundirty(). XXX
2764 */
2765 s = splbio();
2766 VI_LOCK(vp);
2767 vp->v_numoutput++;
2768 VI_UNLOCK(vp);
2769 bp->b_flags |= B_ASYNC;
2770 bundirty(bp);
2771 bp->b_flags &= ~B_DONE;
2772 bp->b_ioflags &= ~BIO_ERROR;
2773 bp->b_dirtyoff = bp->b_dirtyend = 0;
2774 splx(s);
2775 bufdone(bp);
2776 }
2777 }
2778 }
2779
2780 /*
2781 * Start/do any write(s) that are required.
2782 */
2783 loop:
2784 s = splbio();
2785 VI_LOCK(vp);
2786 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
2787 nbp = TAILQ_NEXT(bp, b_vnbufs);
2788 VI_UNLOCK(vp);
2789 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
2790 if (waitfor != MNT_WAIT || passone) {
2791 VI_LOCK(vp);
2792 continue;
2793 }
2794 error = BUF_TIMELOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL,
2795 "nfsfsync", slpflag, slptimeo);
2796 splx(s);
2797 if (error == 0)
2798 panic("nfs_fsync: inconsistent lock");
2799 if (error == ENOLCK)
2800 goto loop;
2801 if (nfs_sigintr(nmp, NULL, td)) {
2802 error = EINTR;
2803 goto done;
2804 }
2805 if (slpflag == PCATCH) {
2806 slpflag = 0;
2807 slptimeo = 2 * hz;
2808 }
2809 goto loop;
2810 }
2811 if ((bp->b_flags & B_DELWRI) == 0)
2812 panic("nfs_fsync: not dirty");
2813 if ((passone || !commit) && (bp->b_flags & B_NEEDCOMMIT)) {
2814 BUF_UNLOCK(bp);
2815 VI_LOCK(vp);
2816 continue;
2817 }
2818 bremfree(bp);
2819 if (passone || !commit)
2820 bp->b_flags |= B_ASYNC;
2821 else
2822 bp->b_flags |= B_ASYNC | B_WRITEINPROG;
2823 splx(s);
2824 BUF_WRITE(bp);
2825 goto loop;
2826 }
2827 splx(s);
2828 if (passone) {
2829 passone = 0;
2830 VI_UNLOCK(vp);
2831 goto again;
2832 }
2833 if (waitfor == MNT_WAIT) {
2834 while (vp->v_numoutput) {
2835 vp->v_iflag |= VI_BWAIT;
2836 error = msleep((caddr_t)&vp->v_numoutput, VI_MTX(vp),
2837 slpflag | (PRIBIO + 1), "nfsfsync", slptimeo);
2838 if (error) {
2839 if (nfs_sigintr(nmp, NULL, td)) {
2840 VI_UNLOCK(vp);
2841 error = EINTR;
2842 goto done;
2843 }
2844 if (slpflag == PCATCH) {
2845 slpflag = 0;
2846 slptimeo = 2 * hz;
2847 }
2848 }
2849 }
2850 if (!TAILQ_EMPTY(&vp->v_dirtyblkhd) && commit) {
2851 VI_UNLOCK(vp);
2852 goto loop;
2853 }
2854 }
2855 VI_UNLOCK(vp);
2856 if (np->n_flag & NWRITEERR) {
2857 error = np->n_error;
2858 np->n_flag &= ~NWRITEERR;
2859 }
2860 done:
2861 if (bvec != NULL && bvec != bvec_on_stack)
2862 free(bvec, M_TEMP);
2863 return (error);
2864 }
2865
2866 /*
2867 * NFS advisory byte-level locks.
2868 */
2869 static int
2870 nfs_advlock(struct vop_advlock_args *ap)
2871 {
2872
2873 if ((VFSTONFS(ap->a_vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) {
2874 struct nfsnode *np = VTONFS(ap->a_vp);
2875
2876 return (lf_advlock(ap, &(np->n_lockf), np->n_size));
2877 }
2878 return (nfs_dolock(ap));
2879 }
2880
2881 /*
2882 * Print out the contents of an nfsnode.
2883 */
2884 static int
2885 nfs_print(struct vop_print_args *ap)
2886 {
2887 struct vnode *vp = ap->a_vp;
2888 struct nfsnode *np = VTONFS(vp);
2889
2890 printf("tag %s fileid %ld fsid 0x%x",
2891 vp->v_tag, np->n_vattr.va_fileid, np->n_vattr.va_fsid);
2892 if (vp->v_type == VFIFO)
2893 fifo_printinfo(vp);
2894 printf("\n");
2895 return (0);
2896 }
2897
2898 /*
2899 * This is the "real" nfs::bwrite(struct buf*).
2900 * B_WRITEINPROG isn't set unless the force flag is one and it
2901 * handles the B_NEEDCOMMIT flag.
2902 * We set B_CACHE if this is a VMIO buffer.
2903 */
2904 int
2905 nfs_writebp(struct buf *bp, int force, struct thread *td)
2906 {
2907 int s;
2908 int oldflags = bp->b_flags;
2909 #if 0
2910 int retv = 1;
2911 off_t off;
2912 #endif
2913
2914 if (BUF_REFCNT(bp) == 0)
2915 panic("bwrite: buffer is not locked???");
2916
2917 if (bp->b_flags & B_INVAL) {
2918 brelse(bp);
2919 return(0);
2920 }
2921
2922 bp->b_flags |= B_CACHE;
2923
2924 /*
2925 * Undirty the bp. We will redirty it later if the I/O fails.
2926 */
2927
2928 s = splbio();
2929 bundirty(bp);
2930 bp->b_flags &= ~B_DONE;
2931 bp->b_ioflags &= ~BIO_ERROR;
2932 bp->b_iocmd = BIO_WRITE;
2933
2934 VI_LOCK(bp->b_vp);
2935 bp->b_vp->v_numoutput++;
2936 VI_UNLOCK(bp->b_vp);
2937 curthread->td_proc->p_stats->p_ru.ru_oublock++;
2938 splx(s);
2939
2940 /*
2941 * Note: to avoid loopback deadlocks, we do not
2942 * assign b_runningbufspace.
2943 */
2944 vfs_busy_pages(bp, 1);
2945
2946 if (force)
2947 bp->b_flags |= B_WRITEINPROG;
2948 BUF_KERNPROC(bp);
2949 BUF_STRATEGY(bp);
2950
2951 if( (oldflags & B_ASYNC) == 0) {
2952 int rtval = bufwait(bp);
2953
2954 if (oldflags & B_DELWRI) {
2955 s = splbio();
2956 reassignbuf(bp, bp->b_vp);
2957 splx(s);
2958 }
2959
2960 brelse(bp);
2961 return (rtval);
2962 }
2963
2964 return (0);
2965 }
2966
2967 /*
2968 * nfs special file access vnode op.
2969 * Essentially just get vattr and then imitate iaccess() since the device is
2970 * local to the client.
2971 */
2972 static int
2973 nfsspec_access(struct vop_access_args *ap)
2974 {
2975 struct vattr *vap;
2976 gid_t *gp;
2977 struct ucred *cred = ap->a_cred;
2978 struct vnode *vp = ap->a_vp;
2979 mode_t mode = ap->a_mode;
2980 struct vattr vattr;
2981 int i;
2982 int error;
2983
2984 /*
2985 * Disallow write attempts on filesystems mounted read-only;
2986 * unless the file is a socket, fifo, or a block or character
2987 * device resident on the filesystem.
2988 */
2989 if ((mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
2990 switch (vp->v_type) {
2991 case VREG:
2992 case VDIR:
2993 case VLNK:
2994 return (EROFS);
2995 default:
2996 break;
2997 }
2998 }
2999 /*
3000 * If you're the super-user,
3001 * you always get access.
3002 */
3003 if (cred->cr_uid == 0)
3004 return (0);
3005 vap = &vattr;
3006 error = VOP_GETATTR(vp, vap, cred, ap->a_td);
3007 if (error)
3008 return (error);
3009 /*
3010 * Access check is based on only one of owner, group, public.
3011 * If not owner, then check group. If not a member of the
3012 * group, then check public access.
3013 */
3014 if (cred->cr_uid != vap->va_uid) {
3015 mode >>= 3;
3016 gp = cred->cr_groups;
3017 for (i = 0; i < cred->cr_ngroups; i++, gp++)
3018 if (vap->va_gid == *gp)
3019 goto found;
3020 mode >>= 3;
3021 found:
3022 ;
3023 }
3024 error = (vap->va_mode & mode) == mode ? 0 : EACCES;
3025 return (error);
3026 }
3027
3028 /*
3029 * Read wrapper for special devices.
3030 */
3031 static int
3032 nfsspec_read(struct vop_read_args *ap)
3033 {
3034 struct nfsnode *np = VTONFS(ap->a_vp);
3035
3036 /*
3037 * Set access flag.
3038 */
3039 np->n_flag |= NACC;
3040 getnanotime(&np->n_atim);
3041 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_read), ap));
3042 }
3043
3044 /*
3045 * Write wrapper for special devices.
3046 */
3047 static int
3048 nfsspec_write(struct vop_write_args *ap)
3049 {
3050 struct nfsnode *np = VTONFS(ap->a_vp);
3051
3052 /*
3053 * Set update flag.
3054 */
3055 np->n_flag |= NUPD;
3056 getnanotime(&np->n_mtim);
3057 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_write), ap));
3058 }
3059
3060 /*
3061 * Close wrapper for special devices.
3062 *
3063 * Update the times on the nfsnode then do device close.
3064 */
3065 static int
3066 nfsspec_close(struct vop_close_args *ap)
3067 {
3068 struct vnode *vp = ap->a_vp;
3069 struct nfsnode *np = VTONFS(vp);
3070 struct vattr vattr;
3071
3072 if (np->n_flag & (NACC | NUPD)) {
3073 np->n_flag |= NCHG;
3074 if (vrefcnt(vp) == 1 &&
3075 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3076 VATTR_NULL(&vattr);
3077 if (np->n_flag & NACC)
3078 vattr.va_atime = np->n_atim;
3079 if (np->n_flag & NUPD)
3080 vattr.va_mtime = np->n_mtim;
3081 (void)VOP_SETATTR(vp, &vattr, ap->a_cred, ap->a_td);
3082 }
3083 }
3084 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_close), ap));
3085 }
3086
3087 /*
3088 * Read wrapper for fifos.
3089 */
3090 static int
3091 nfsfifo_read(struct vop_read_args *ap)
3092 {
3093 struct nfsnode *np = VTONFS(ap->a_vp);
3094
3095 /*
3096 * Set access flag.
3097 */
3098 np->n_flag |= NACC;
3099 getnanotime(&np->n_atim);
3100 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_read), ap));
3101 }
3102
3103 /*
3104 * Write wrapper for fifos.
3105 */
3106 static int
3107 nfsfifo_write(struct vop_write_args *ap)
3108 {
3109 struct nfsnode *np = VTONFS(ap->a_vp);
3110
3111 /*
3112 * Set update flag.
3113 */
3114 np->n_flag |= NUPD;
3115 getnanotime(&np->n_mtim);
3116 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_write), ap));
3117 }
3118
3119 /*
3120 * Close wrapper for fifos.
3121 *
3122 * Update the times on the nfsnode then do fifo close.
3123 */
3124 static int
3125 nfsfifo_close(struct vop_close_args *ap)
3126 {
3127 struct vnode *vp = ap->a_vp;
3128 struct nfsnode *np = VTONFS(vp);
3129 struct vattr vattr;
3130 struct timespec ts;
3131
3132 if (np->n_flag & (NACC | NUPD)) {
3133 getnanotime(&ts);
3134 if (np->n_flag & NACC)
3135 np->n_atim = ts;
3136 if (np->n_flag & NUPD)
3137 np->n_mtim = ts;
3138 np->n_flag |= NCHG;
3139 if (vrefcnt(vp) == 1 &&
3140 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3141 VATTR_NULL(&vattr);
3142 if (np->n_flag & NACC)
3143 vattr.va_atime = np->n_atim;
3144 if (np->n_flag & NUPD)
3145 vattr.va_mtime = np->n_mtim;
3146 (void)VOP_SETATTR(vp, &vattr, ap->a_cred, ap->a_td);
3147 }
3148 }
3149 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_close), ap));
3150 }
3151
Cache object: 0066fb20d516be9f2ca9aefa75c67504
|