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