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