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.1/sys/nfsclient/nfs_vnops.c 157839 2006-04-18 14:15:50Z jon $");
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_off(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 if (v3) {
828 nfsm_postop_attr(dvp, attrflag);
829 m_freem(mrep);
830 }
831 goto nfsmout;
832 }
833 nfsm_getfh(fhp, fhsize, v3);
834
835 /*
836 * Handle RENAME case...
837 */
838 if (cnp->cn_nameiop == RENAME && (flags & ISLASTCN)) {
839 if (NFS_CMPFH(np, fhp, fhsize)) {
840 m_freem(mrep);
841 return (EISDIR);
842 }
843 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
844 if (error) {
845 m_freem(mrep);
846 return (error);
847 }
848 newvp = NFSTOV(np);
849 if (v3) {
850 nfsm_postop_attr(newvp, attrflag);
851 nfsm_postop_attr(dvp, attrflag);
852 } else
853 nfsm_loadattr(newvp, NULL);
854 *vpp = newvp;
855 m_freem(mrep);
856 cnp->cn_flags |= SAVENAME;
857 return (0);
858 }
859
860 if (flags & ISDOTDOT) {
861 VOP_UNLOCK(dvp, 0, td);
862 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
863 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, td);
864 if (error)
865 return (error);
866 newvp = NFSTOV(np);
867 } else if (NFS_CMPFH(np, fhp, fhsize)) {
868 VREF(dvp);
869 newvp = dvp;
870 } else {
871 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
872 if (error) {
873 m_freem(mrep);
874 return (error);
875 }
876 newvp = NFSTOV(np);
877 }
878 if (v3) {
879 nfsm_postop_attr(newvp, attrflag);
880 nfsm_postop_attr(dvp, attrflag);
881 } else
882 nfsm_loadattr(newvp, NULL);
883 if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
884 cnp->cn_flags |= SAVENAME;
885 if ((cnp->cn_flags & MAKEENTRY) &&
886 (cnp->cn_nameiop != DELETE || !(flags & ISLASTCN))) {
887 np->n_ctime = np->n_vattr.va_ctime.tv_sec;
888 cache_enter(dvp, newvp, cnp);
889 }
890 *vpp = newvp;
891 m_freem(mrep);
892 nfsmout:
893 if (error) {
894 if (newvp != NULLVP) {
895 vput(newvp);
896 *vpp = NULLVP;
897 }
898 if ((cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME) &&
899 (flags & ISLASTCN) && error == ENOENT) {
900 if (dvp->v_mount->mnt_flag & MNT_RDONLY)
901 error = EROFS;
902 else
903 error = EJUSTRETURN;
904 }
905 if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
906 cnp->cn_flags |= SAVENAME;
907 }
908 return (error);
909 }
910
911 /*
912 * nfs read call.
913 * Just call nfs_bioread() to do the work.
914 */
915 static int
916 nfs_read(struct vop_read_args *ap)
917 {
918 struct vnode *vp = ap->a_vp;
919
920 switch (vp->v_type) {
921 case VREG:
922 return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag, ap->a_cred));
923 case VDIR:
924 return (EISDIR);
925 default:
926 return (EOPNOTSUPP);
927 }
928 }
929
930 /*
931 * nfs readlink call
932 */
933 static int
934 nfs_readlink(struct vop_readlink_args *ap)
935 {
936 struct vnode *vp = ap->a_vp;
937
938 if (vp->v_type != VLNK)
939 return (EINVAL);
940 return (nfs_bioread(vp, ap->a_uio, 0, ap->a_cred));
941 }
942
943 /*
944 * Do a readlink rpc.
945 * Called by nfs_doio() from below the buffer cache.
946 */
947 int
948 nfs_readlinkrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
949 {
950 caddr_t bpos, dpos;
951 int error = 0, len, attrflag;
952 struct mbuf *mreq, *mrep, *md, *mb;
953 int v3 = NFS_ISV3(vp);
954
955 nfsstats.rpccnt[NFSPROC_READLINK]++;
956 mreq = nfsm_reqhead(vp, NFSPROC_READLINK, NFSX_FH(v3));
957 mb = mreq;
958 bpos = mtod(mb, caddr_t);
959 nfsm_fhtom(vp, v3);
960 nfsm_request(vp, NFSPROC_READLINK, uiop->uio_td, cred);
961 if (v3)
962 nfsm_postop_attr(vp, attrflag);
963 if (!error) {
964 nfsm_strsiz(len, NFS_MAXPATHLEN);
965 if (len == NFS_MAXPATHLEN) {
966 struct nfsnode *np = VTONFS(vp);
967 if (np->n_size && np->n_size < NFS_MAXPATHLEN)
968 len = np->n_size;
969 }
970 nfsm_mtouio(uiop, len);
971 }
972 m_freem(mrep);
973 nfsmout:
974 return (error);
975 }
976
977 /*
978 * nfs read rpc call
979 * Ditto above
980 */
981 int
982 nfs_readrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
983 {
984 u_int32_t *tl;
985 caddr_t bpos, dpos;
986 struct mbuf *mreq, *mrep, *md, *mb;
987 struct nfsmount *nmp;
988 int error = 0, len, retlen, tsiz, eof, attrflag;
989 int v3 = NFS_ISV3(vp);
990
991 #ifndef nolint
992 eof = 0;
993 #endif
994 nmp = VFSTONFS(vp->v_mount);
995 tsiz = uiop->uio_resid;
996 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
997 return (EFBIG);
998 while (tsiz > 0) {
999 nfsstats.rpccnt[NFSPROC_READ]++;
1000 len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
1001 mreq = nfsm_reqhead(vp, NFSPROC_READ, NFSX_FH(v3) + NFSX_UNSIGNED * 3);
1002 mb = mreq;
1003 bpos = mtod(mb, caddr_t);
1004 nfsm_fhtom(vp, v3);
1005 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED * 3);
1006 if (v3) {
1007 txdr_hyper(uiop->uio_offset, tl);
1008 *(tl + 2) = txdr_unsigned(len);
1009 } else {
1010 *tl++ = txdr_unsigned(uiop->uio_offset);
1011 *tl++ = txdr_unsigned(len);
1012 *tl = 0;
1013 }
1014 nfsm_request(vp, NFSPROC_READ, uiop->uio_td, cred);
1015 if (v3) {
1016 nfsm_postop_attr(vp, attrflag);
1017 if (error) {
1018 m_freem(mrep);
1019 goto nfsmout;
1020 }
1021 tl = nfsm_dissect(u_int32_t *, 2 * NFSX_UNSIGNED);
1022 eof = fxdr_unsigned(int, *(tl + 1));
1023 } else
1024 nfsm_loadattr(vp, NULL);
1025 nfsm_strsiz(retlen, nmp->nm_rsize);
1026 nfsm_mtouio(uiop, retlen);
1027 m_freem(mrep);
1028 tsiz -= retlen;
1029 if (v3) {
1030 if (eof || retlen == 0) {
1031 tsiz = 0;
1032 }
1033 } else if (retlen < len) {
1034 tsiz = 0;
1035 }
1036 }
1037 nfsmout:
1038 return (error);
1039 }
1040
1041 /*
1042 * nfs write call
1043 */
1044 int
1045 nfs_writerpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
1046 int *iomode, int *must_commit)
1047 {
1048 u_int32_t *tl;
1049 int32_t backup;
1050 caddr_t bpos, dpos;
1051 struct mbuf *mreq, *mrep, *md, *mb;
1052 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1053 int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR, rlen, commit;
1054 int v3 = NFS_ISV3(vp), committed = NFSV3WRITE_FILESYNC;
1055
1056 #ifndef DIAGNOSTIC
1057 if (uiop->uio_iovcnt != 1)
1058 panic("nfs: writerpc iovcnt > 1");
1059 #endif
1060 *must_commit = 0;
1061 tsiz = uiop->uio_resid;
1062 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1063 return (EFBIG);
1064 while (tsiz > 0) {
1065 nfsstats.rpccnt[NFSPROC_WRITE]++;
1066 len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz;
1067 mreq = nfsm_reqhead(vp, NFSPROC_WRITE,
1068 NFSX_FH(v3) + 5 * NFSX_UNSIGNED + nfsm_rndup(len));
1069 mb = mreq;
1070 bpos = mtod(mb, caddr_t);
1071 nfsm_fhtom(vp, v3);
1072 if (v3) {
1073 tl = nfsm_build(u_int32_t *, 5 * NFSX_UNSIGNED);
1074 txdr_hyper(uiop->uio_offset, tl);
1075 tl += 2;
1076 *tl++ = txdr_unsigned(len);
1077 *tl++ = txdr_unsigned(*iomode);
1078 *tl = txdr_unsigned(len);
1079 } else {
1080 u_int32_t x;
1081
1082 tl = nfsm_build(u_int32_t *, 4 * NFSX_UNSIGNED);
1083 /* Set both "begin" and "current" to non-garbage. */
1084 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1085 *tl++ = x; /* "begin offset" */
1086 *tl++ = x; /* "current offset" */
1087 x = txdr_unsigned(len);
1088 *tl++ = x; /* total to this offset */
1089 *tl = x; /* size of this write */
1090 }
1091 nfsm_uiotom(uiop, len);
1092 nfsm_request(vp, NFSPROC_WRITE, uiop->uio_td, cred);
1093 if (v3) {
1094 wccflag = NFSV3_WCCCHK;
1095 nfsm_wcc_data(vp, wccflag);
1096 if (!error) {
1097 tl = nfsm_dissect(u_int32_t *, 2 * NFSX_UNSIGNED
1098 + NFSX_V3WRITEVERF);
1099 rlen = fxdr_unsigned(int, *tl++);
1100 if (rlen == 0) {
1101 error = NFSERR_IO;
1102 m_freem(mrep);
1103 break;
1104 } else if (rlen < len) {
1105 backup = len - rlen;
1106 uiop->uio_iov->iov_base =
1107 (char *)uiop->uio_iov->iov_base -
1108 backup;
1109 uiop->uio_iov->iov_len += backup;
1110 uiop->uio_offset -= backup;
1111 uiop->uio_resid += backup;
1112 len = rlen;
1113 }
1114 commit = fxdr_unsigned(int, *tl++);
1115
1116 /*
1117 * Return the lowest committment level
1118 * obtained by any of the RPCs.
1119 */
1120 if (committed == NFSV3WRITE_FILESYNC)
1121 committed = commit;
1122 else if (committed == NFSV3WRITE_DATASYNC &&
1123 commit == NFSV3WRITE_UNSTABLE)
1124 committed = commit;
1125 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0){
1126 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1127 NFSX_V3WRITEVERF);
1128 nmp->nm_state |= NFSSTA_HASWRITEVERF;
1129 } else if (bcmp((caddr_t)tl,
1130 (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF)) {
1131 *must_commit = 1;
1132 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1133 NFSX_V3WRITEVERF);
1134 }
1135 }
1136 } else
1137 nfsm_loadattr(vp, NULL);
1138 if (wccflag)
1139 VTONFS(vp)->n_mtime = VTONFS(vp)->n_vattr.va_mtime;
1140 m_freem(mrep);
1141 if (error)
1142 break;
1143 tsiz -= len;
1144 }
1145 nfsmout:
1146 if (vp->v_mount->mnt_flag & MNT_ASYNC)
1147 committed = NFSV3WRITE_FILESYNC;
1148 *iomode = committed;
1149 if (error)
1150 uiop->uio_resid = tsiz;
1151 return (error);
1152 }
1153
1154 /*
1155 * nfs mknod rpc
1156 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1157 * mode set to specify the file type and the size field for rdev.
1158 */
1159 static int
1160 nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1161 struct vattr *vap)
1162 {
1163 struct nfsv2_sattr *sp;
1164 u_int32_t *tl;
1165 struct vnode *newvp = NULL;
1166 struct nfsnode *np = NULL;
1167 struct vattr vattr;
1168 caddr_t bpos, dpos;
1169 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0;
1170 struct mbuf *mreq, *mrep, *md, *mb;
1171 u_int32_t rdev;
1172 int v3 = NFS_ISV3(dvp);
1173
1174 if (vap->va_type == VCHR || vap->va_type == VBLK)
1175 rdev = txdr_unsigned(vap->va_rdev);
1176 else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1177 rdev = nfs_xdrneg1;
1178 else {
1179 return (EOPNOTSUPP);
1180 }
1181 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred, cnp->cn_thread)) != 0) {
1182 return (error);
1183 }
1184 nfsstats.rpccnt[NFSPROC_MKNOD]++;
1185 mreq = nfsm_reqhead(dvp, NFSPROC_MKNOD, NFSX_FH(v3) + 4 * NFSX_UNSIGNED +
1186 + nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1187 mb = mreq;
1188 bpos = mtod(mb, caddr_t);
1189 nfsm_fhtom(dvp, v3);
1190 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1191 if (v3) {
1192 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
1193 *tl++ = vtonfsv3_type(vap->va_type);
1194 nfsm_v3attrbuild(vap, FALSE);
1195 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1196 tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED);
1197 *tl++ = txdr_unsigned(umajor(vap->va_rdev));
1198 *tl = txdr_unsigned(uminor(vap->va_rdev));
1199 }
1200 } else {
1201 sp = nfsm_build(struct nfsv2_sattr *, NFSX_V2SATTR);
1202 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1203 sp->sa_uid = nfs_xdrneg1;
1204 sp->sa_gid = nfs_xdrneg1;
1205 sp->sa_size = rdev;
1206 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1207 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1208 }
1209 nfsm_request(dvp, NFSPROC_MKNOD, cnp->cn_thread, cnp->cn_cred);
1210 if (!error) {
1211 nfsm_mtofh(dvp, newvp, v3, gotvp);
1212 if (!gotvp) {
1213 if (newvp) {
1214 vput(newvp);
1215 newvp = NULL;
1216 }
1217 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1218 cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread, &np);
1219 if (!error)
1220 newvp = NFSTOV(np);
1221 }
1222 }
1223 if (v3)
1224 nfsm_wcc_data(dvp, wccflag);
1225 m_freem(mrep);
1226 nfsmout:
1227 if (error) {
1228 if (newvp)
1229 vput(newvp);
1230 } else {
1231 if (cnp->cn_flags & MAKEENTRY)
1232 cache_enter(dvp, newvp, cnp);
1233 *vpp = newvp;
1234 }
1235 VTONFS(dvp)->n_flag |= NMODIFIED;
1236 if (!wccflag)
1237 VTONFS(dvp)->n_attrstamp = 0;
1238 return (error);
1239 }
1240
1241 /*
1242 * nfs mknod vop
1243 * just call nfs_mknodrpc() to do the work.
1244 */
1245 /* ARGSUSED */
1246 static int
1247 nfs_mknod(struct vop_mknod_args *ap)
1248 {
1249
1250 return (nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap));
1251 }
1252
1253 static u_long create_verf;
1254 /*
1255 * nfs file create call
1256 */
1257 static int
1258 nfs_create(struct vop_create_args *ap)
1259 {
1260 struct vnode *dvp = ap->a_dvp;
1261 struct vattr *vap = ap->a_vap;
1262 struct componentname *cnp = ap->a_cnp;
1263 struct nfsv2_sattr *sp;
1264 u_int32_t *tl;
1265 struct nfsnode *np = NULL;
1266 struct vnode *newvp = NULL;
1267 caddr_t bpos, dpos;
1268 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0, fmode = 0;
1269 struct mbuf *mreq, *mrep, *md, *mb;
1270 struct vattr vattr;
1271 int v3 = NFS_ISV3(dvp);
1272
1273 /*
1274 * Oops, not for me..
1275 */
1276 if (vap->va_type == VSOCK)
1277 return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
1278
1279 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred, cnp->cn_thread)) != 0) {
1280 return (error);
1281 }
1282 if (vap->va_vaflags & VA_EXCLUSIVE)
1283 fmode |= O_EXCL;
1284 again:
1285 nfsstats.rpccnt[NFSPROC_CREATE]++;
1286 mreq = nfsm_reqhead(dvp, NFSPROC_CREATE, NFSX_FH(v3) + 2 * NFSX_UNSIGNED +
1287 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1288 mb = mreq;
1289 bpos = mtod(mb, caddr_t);
1290 nfsm_fhtom(dvp, v3);
1291 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1292 if (v3) {
1293 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
1294 if (fmode & O_EXCL) {
1295 *tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE);
1296 tl = nfsm_build(u_int32_t *, NFSX_V3CREATEVERF);
1297 #ifdef INET
1298 if (!TAILQ_EMPTY(&in_ifaddrhead))
1299 *tl++ = IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr.s_addr;
1300 else
1301 #endif
1302 *tl++ = create_verf;
1303 *tl = ++create_verf;
1304 } else {
1305 *tl = txdr_unsigned(NFSV3CREATE_UNCHECKED);
1306 nfsm_v3attrbuild(vap, FALSE);
1307 }
1308 } else {
1309 sp = nfsm_build(struct nfsv2_sattr *, NFSX_V2SATTR);
1310 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1311 sp->sa_uid = nfs_xdrneg1;
1312 sp->sa_gid = nfs_xdrneg1;
1313 sp->sa_size = 0;
1314 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1315 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1316 }
1317 nfsm_request(dvp, NFSPROC_CREATE, cnp->cn_thread, cnp->cn_cred);
1318 if (!error) {
1319 nfsm_mtofh(dvp, newvp, v3, gotvp);
1320 if (!gotvp) {
1321 if (newvp) {
1322 vput(newvp);
1323 newvp = NULL;
1324 }
1325 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1326 cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread, &np);
1327 if (!error)
1328 newvp = NFSTOV(np);
1329 }
1330 }
1331 if (v3)
1332 nfsm_wcc_data(dvp, wccflag);
1333 m_freem(mrep);
1334 nfsmout:
1335 if (error) {
1336 if (v3 && (fmode & O_EXCL) && error == NFSERR_NOTSUPP) {
1337 fmode &= ~O_EXCL;
1338 goto again;
1339 }
1340 if (newvp)
1341 vput(newvp);
1342 } else if (v3 && (fmode & O_EXCL)) {
1343 /*
1344 * We are normally called with only a partially initialized
1345 * VAP. Since the NFSv3 spec says that server may use the
1346 * file attributes to store the verifier, the spec requires
1347 * us to do a SETATTR RPC. FreeBSD servers store the verifier
1348 * in atime, but we can't really assume that all servers will
1349 * so we ensure that our SETATTR sets both atime and mtime.
1350 */
1351 if (vap->va_mtime.tv_sec == VNOVAL)
1352 vfs_timestamp(&vap->va_mtime);
1353 if (vap->va_atime.tv_sec == VNOVAL)
1354 vap->va_atime = vap->va_mtime;
1355 error = nfs_setattrrpc(newvp, vap, cnp->cn_cred, cnp->cn_thread);
1356 }
1357 if (!error) {
1358 if (cnp->cn_flags & MAKEENTRY)
1359 cache_enter(dvp, newvp, cnp);
1360 *ap->a_vpp = newvp;
1361 }
1362 VTONFS(dvp)->n_flag |= NMODIFIED;
1363 if (!wccflag)
1364 VTONFS(dvp)->n_attrstamp = 0;
1365 return (error);
1366 }
1367
1368 /*
1369 * nfs file remove call
1370 * To try and make nfs semantics closer to ufs semantics, a file that has
1371 * other processes using the vnode is renamed instead of removed and then
1372 * removed later on the last close.
1373 * - If v_usecount > 1
1374 * If a rename is not already in the works
1375 * call nfs_sillyrename() to set it up
1376 * else
1377 * do the remove rpc
1378 */
1379 static int
1380 nfs_remove(struct vop_remove_args *ap)
1381 {
1382 struct vnode *vp = ap->a_vp;
1383 struct vnode *dvp = ap->a_dvp;
1384 struct componentname *cnp = ap->a_cnp;
1385 struct nfsnode *np = VTONFS(vp);
1386 int error = 0;
1387 struct vattr vattr;
1388
1389 #ifndef DIAGNOSTIC
1390 if ((cnp->cn_flags & HASBUF) == 0)
1391 panic("nfs_remove: no name");
1392 if (vrefcnt(vp) < 1)
1393 panic("nfs_remove: bad v_usecount");
1394 #endif
1395 if (vp->v_type == VDIR)
1396 error = EPERM;
1397 else if (vrefcnt(vp) == 1 || (np->n_sillyrename &&
1398 VOP_GETATTR(vp, &vattr, cnp->cn_cred, cnp->cn_thread) == 0 &&
1399 vattr.va_nlink > 1)) {
1400 /*
1401 * Purge the name cache so that the chance of a lookup for
1402 * the name succeeding while the remove is in progress is
1403 * minimized. Without node locking it can still happen, such
1404 * that an I/O op returns ESTALE, but since you get this if
1405 * another host removes the file..
1406 */
1407 cache_purge(vp);
1408 /*
1409 * throw away biocache buffers, mainly to avoid
1410 * unnecessary delayed writes later.
1411 */
1412 error = nfs_vinvalbuf(vp, 0, cnp->cn_thread, 1);
1413 /* Do the rpc */
1414 if (error != EINTR && error != EIO)
1415 error = nfs_removerpc(dvp, cnp->cn_nameptr,
1416 cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread);
1417 /*
1418 * Kludge City: If the first reply to the remove rpc is lost..
1419 * the reply to the retransmitted request will be ENOENT
1420 * since the file was in fact removed
1421 * Therefore, we cheat and return success.
1422 */
1423 if (error == ENOENT)
1424 error = 0;
1425 } else if (!np->n_sillyrename)
1426 error = nfs_sillyrename(dvp, vp, cnp);
1427 np->n_attrstamp = 0;
1428 return (error);
1429 }
1430
1431 /*
1432 * nfs file remove rpc called from nfs_inactive
1433 */
1434 int
1435 nfs_removeit(struct sillyrename *sp)
1436 {
1437
1438 /*
1439 * Make sure that the directory vnode is still valid.
1440 * XXX we should lock sp->s_dvp here.
1441 */
1442 if (sp->s_dvp->v_type == VBAD)
1443 return (0);
1444 return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen, sp->s_cred,
1445 NULL));
1446 }
1447
1448 /*
1449 * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
1450 */
1451 static int
1452 nfs_removerpc(struct vnode *dvp, const char *name, int namelen,
1453 struct ucred *cred, struct thread *td)
1454 {
1455 caddr_t bpos, dpos;
1456 int error = 0, wccflag = NFSV3_WCCRATTR;
1457 struct mbuf *mreq, *mrep, *md, *mb;
1458 int v3 = NFS_ISV3(dvp);
1459
1460 nfsstats.rpccnt[NFSPROC_REMOVE]++;
1461 mreq = nfsm_reqhead(dvp, NFSPROC_REMOVE,
1462 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
1463 mb = mreq;
1464 bpos = mtod(mb, caddr_t);
1465 nfsm_fhtom(dvp, v3);
1466 nfsm_strtom(name, namelen, NFS_MAXNAMLEN);
1467 nfsm_request(dvp, NFSPROC_REMOVE, td, cred);
1468 if (v3)
1469 nfsm_wcc_data(dvp, wccflag);
1470 m_freem(mrep);
1471 nfsmout:
1472 VTONFS(dvp)->n_flag |= NMODIFIED;
1473 if (!wccflag)
1474 VTONFS(dvp)->n_attrstamp = 0;
1475 return (error);
1476 }
1477
1478 /*
1479 * nfs file rename call
1480 */
1481 static int
1482 nfs_rename(struct vop_rename_args *ap)
1483 {
1484 struct vnode *fvp = ap->a_fvp;
1485 struct vnode *tvp = ap->a_tvp;
1486 struct vnode *fdvp = ap->a_fdvp;
1487 struct vnode *tdvp = ap->a_tdvp;
1488 struct componentname *tcnp = ap->a_tcnp;
1489 struct componentname *fcnp = ap->a_fcnp;
1490 int error;
1491
1492 #ifndef DIAGNOSTIC
1493 if ((tcnp->cn_flags & HASBUF) == 0 ||
1494 (fcnp->cn_flags & HASBUF) == 0)
1495 panic("nfs_rename: no name");
1496 #endif
1497 /* Check for cross-device rename */
1498 if ((fvp->v_mount != tdvp->v_mount) ||
1499 (tvp && (fvp->v_mount != tvp->v_mount))) {
1500 error = EXDEV;
1501 goto out;
1502 }
1503
1504 if (fvp == tvp) {
1505 printf("nfs_rename: fvp == tvp (can't happen)\n");
1506 error = 0;
1507 goto out;
1508 }
1509 if ((error = vn_lock(fvp, LK_EXCLUSIVE, fcnp->cn_thread)) != 0)
1510 goto out;
1511
1512 /*
1513 * We have to flush B_DELWRI data prior to renaming
1514 * the file. If we don't, the delayed-write buffers
1515 * can be flushed out later after the file has gone stale
1516 * under NFSV3. NFSV2 does not have this problem because
1517 * ( as far as I can tell ) it flushes dirty buffers more
1518 * often.
1519 *
1520 * Skip the rename operation if the fsync fails, this can happen
1521 * due to the server's volume being full, when we pushed out data
1522 * that was written back to our cache earlier. Not checking for
1523 * this condition can result in potential (silent) data loss.
1524 */
1525 error = VOP_FSYNC(fvp, MNT_WAIT, fcnp->cn_thread);
1526 VOP_UNLOCK(fvp, 0, fcnp->cn_thread);
1527 if (!error && tvp)
1528 error = VOP_FSYNC(tvp, MNT_WAIT, tcnp->cn_thread);
1529 if (error)
1530 goto out;
1531
1532 /*
1533 * If the tvp exists and is in use, sillyrename it before doing the
1534 * rename of the new file over it.
1535 * XXX Can't sillyrename a directory.
1536 */
1537 if (tvp && vrefcnt(tvp) > 1 && !VTONFS(tvp)->n_sillyrename &&
1538 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
1539 vput(tvp);
1540 tvp = NULL;
1541 }
1542
1543 error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1544 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1545 tcnp->cn_thread);
1546
1547 if (fvp->v_type == VDIR) {
1548 if (tvp != NULL && tvp->v_type == VDIR)
1549 cache_purge(tdvp);
1550 cache_purge(fdvp);
1551 }
1552
1553 out:
1554 if (tdvp == tvp)
1555 vrele(tdvp);
1556 else
1557 vput(tdvp);
1558 if (tvp)
1559 vput(tvp);
1560 vrele(fdvp);
1561 vrele(fvp);
1562 /*
1563 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1564 */
1565 if (error == ENOENT)
1566 error = 0;
1567 return (error);
1568 }
1569
1570 /*
1571 * nfs file rename rpc called from nfs_remove() above
1572 */
1573 static int
1574 nfs_renameit(struct vnode *sdvp, struct componentname *scnp,
1575 struct sillyrename *sp)
1576 {
1577
1578 return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen, sdvp,
1579 sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_thread));
1580 }
1581
1582 /*
1583 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
1584 */
1585 static int
1586 nfs_renamerpc(struct vnode *fdvp, const char *fnameptr, int fnamelen,
1587 struct vnode *tdvp, const char *tnameptr, int tnamelen, struct ucred *cred,
1588 struct thread *td)
1589 {
1590 caddr_t bpos, dpos;
1591 int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR;
1592 struct mbuf *mreq, *mrep, *md, *mb;
1593 int v3 = NFS_ISV3(fdvp);
1594
1595 nfsstats.rpccnt[NFSPROC_RENAME]++;
1596 mreq = nfsm_reqhead(fdvp, NFSPROC_RENAME,
1597 (NFSX_FH(v3) + NFSX_UNSIGNED)*2 + nfsm_rndup(fnamelen) +
1598 nfsm_rndup(tnamelen));
1599 mb = mreq;
1600 bpos = mtod(mb, caddr_t);
1601 nfsm_fhtom(fdvp, v3);
1602 nfsm_strtom(fnameptr, fnamelen, NFS_MAXNAMLEN);
1603 nfsm_fhtom(tdvp, v3);
1604 nfsm_strtom(tnameptr, tnamelen, NFS_MAXNAMLEN);
1605 nfsm_request(fdvp, NFSPROC_RENAME, td, cred);
1606 if (v3) {
1607 nfsm_wcc_data(fdvp, fwccflag);
1608 nfsm_wcc_data(tdvp, twccflag);
1609 }
1610 m_freem(mrep);
1611 nfsmout:
1612 VTONFS(fdvp)->n_flag |= NMODIFIED;
1613 VTONFS(tdvp)->n_flag |= NMODIFIED;
1614 if (!fwccflag)
1615 VTONFS(fdvp)->n_attrstamp = 0;
1616 if (!twccflag)
1617 VTONFS(tdvp)->n_attrstamp = 0;
1618 return (error);
1619 }
1620
1621 /*
1622 * nfs hard link create call
1623 */
1624 static int
1625 nfs_link(struct vop_link_args *ap)
1626 {
1627 struct vnode *vp = ap->a_vp;
1628 struct vnode *tdvp = ap->a_tdvp;
1629 struct componentname *cnp = ap->a_cnp;
1630 caddr_t bpos, dpos;
1631 int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0;
1632 struct mbuf *mreq, *mrep, *md, *mb;
1633 int v3;
1634
1635 if (vp->v_mount != tdvp->v_mount) {
1636 return (EXDEV);
1637 }
1638
1639 /*
1640 * Push all writes to the server, so that the attribute cache
1641 * doesn't get "out of sync" with the server.
1642 * XXX There should be a better way!
1643 */
1644 VOP_FSYNC(vp, MNT_WAIT, cnp->cn_thread);
1645
1646 v3 = NFS_ISV3(vp);
1647 nfsstats.rpccnt[NFSPROC_LINK]++;
1648 mreq = nfsm_reqhead(vp, NFSPROC_LINK,
1649 NFSX_FH(v3)*2 + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
1650 mb = mreq;
1651 bpos = mtod(mb, caddr_t);
1652 nfsm_fhtom(vp, v3);
1653 nfsm_fhtom(tdvp, v3);
1654 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1655 nfsm_request(vp, NFSPROC_LINK, cnp->cn_thread, cnp->cn_cred);
1656 if (v3) {
1657 nfsm_postop_attr(vp, attrflag);
1658 nfsm_wcc_data(tdvp, wccflag);
1659 }
1660 m_freem(mrep);
1661 nfsmout:
1662 VTONFS(tdvp)->n_flag |= NMODIFIED;
1663 if (!attrflag)
1664 VTONFS(vp)->n_attrstamp = 0;
1665 if (!wccflag)
1666 VTONFS(tdvp)->n_attrstamp = 0;
1667 /*
1668 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
1669 */
1670 if (error == EEXIST)
1671 error = 0;
1672 return (error);
1673 }
1674
1675 /*
1676 * nfs symbolic link create call
1677 */
1678 static int
1679 nfs_symlink(struct vop_symlink_args *ap)
1680 {
1681 struct vnode *dvp = ap->a_dvp;
1682 struct vattr *vap = ap->a_vap;
1683 struct componentname *cnp = ap->a_cnp;
1684 struct nfsv2_sattr *sp;
1685 caddr_t bpos, dpos;
1686 int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp;
1687 struct mbuf *mreq, *mrep, *md, *mb;
1688 struct vnode *newvp = NULL;
1689 int v3 = NFS_ISV3(dvp);
1690
1691 nfsstats.rpccnt[NFSPROC_SYMLINK]++;
1692 slen = strlen(ap->a_target);
1693 mreq = nfsm_reqhead(dvp, NFSPROC_SYMLINK, NFSX_FH(v3) + 2*NFSX_UNSIGNED +
1694 nfsm_rndup(cnp->cn_namelen) + nfsm_rndup(slen) + NFSX_SATTR(v3));
1695 mb = mreq;
1696 bpos = mtod(mb, caddr_t);
1697 nfsm_fhtom(dvp, v3);
1698 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1699 if (v3) {
1700 nfsm_v3attrbuild(vap, FALSE);
1701 }
1702 nfsm_strtom(ap->a_target, slen, NFS_MAXPATHLEN);
1703 if (!v3) {
1704 sp = nfsm_build(struct nfsv2_sattr *, NFSX_V2SATTR);
1705 sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode);
1706 sp->sa_uid = nfs_xdrneg1;
1707 sp->sa_gid = nfs_xdrneg1;
1708 sp->sa_size = nfs_xdrneg1;
1709 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1710 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1711 }
1712
1713 /*
1714 * Issue the NFS request and get the rpc response.
1715 *
1716 * Only NFSv3 responses returning an error of 0 actually return
1717 * a file handle that can be converted into newvp without having
1718 * to do an extra lookup rpc.
1719 */
1720 nfsm_request(dvp, NFSPROC_SYMLINK, cnp->cn_thread, cnp->cn_cred);
1721 if (v3) {
1722 if (error == 0)
1723 nfsm_mtofh(dvp, newvp, v3, gotvp);
1724 nfsm_wcc_data(dvp, wccflag);
1725 }
1726
1727 /*
1728 * out code jumps -> here, mrep is also freed.
1729 */
1730
1731 m_freem(mrep);
1732 nfsmout:
1733
1734 /*
1735 * If we get an EEXIST error, silently convert it to no-error
1736 * in case of an NFS retry.
1737 */
1738 if (error == EEXIST)
1739 error = 0;
1740
1741 /*
1742 * If we do not have (or no longer have) an error, and we could
1743 * not extract the newvp from the response due to the request being
1744 * NFSv2 or the error being EEXIST. We have to do a lookup in order
1745 * to obtain a newvp to return.
1746 */
1747 if (error == 0 && newvp == NULL) {
1748 struct nfsnode *np = NULL;
1749
1750 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1751 cnp->cn_cred, cnp->cn_thread, &np);
1752 if (!error)
1753 newvp = NFSTOV(np);
1754 }
1755 if (error) {
1756 if (newvp)
1757 vput(newvp);
1758 } else {
1759 *ap->a_vpp = newvp;
1760 }
1761 VTONFS(dvp)->n_flag |= NMODIFIED;
1762 if (!wccflag)
1763 VTONFS(dvp)->n_attrstamp = 0;
1764 return (error);
1765 }
1766
1767 /*
1768 * nfs make dir call
1769 */
1770 static int
1771 nfs_mkdir(struct vop_mkdir_args *ap)
1772 {
1773 struct vnode *dvp = ap->a_dvp;
1774 struct vattr *vap = ap->a_vap;
1775 struct componentname *cnp = ap->a_cnp;
1776 struct nfsv2_sattr *sp;
1777 int len;
1778 struct nfsnode *np = NULL;
1779 struct vnode *newvp = NULL;
1780 caddr_t bpos, dpos;
1781 int error = 0, wccflag = NFSV3_WCCRATTR;
1782 int gotvp = 0;
1783 struct mbuf *mreq, *mrep, *md, *mb;
1784 struct vattr vattr;
1785 int v3 = NFS_ISV3(dvp);
1786
1787 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred, cnp->cn_thread)) != 0) {
1788 return (error);
1789 }
1790 len = cnp->cn_namelen;
1791 nfsstats.rpccnt[NFSPROC_MKDIR]++;
1792 mreq = nfsm_reqhead(dvp, NFSPROC_MKDIR,
1793 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len) + NFSX_SATTR(v3));
1794 mb = mreq;
1795 bpos = mtod(mb, caddr_t);
1796 nfsm_fhtom(dvp, v3);
1797 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
1798 if (v3) {
1799 nfsm_v3attrbuild(vap, FALSE);
1800 } else {
1801 sp = nfsm_build(struct nfsv2_sattr *, NFSX_V2SATTR);
1802 sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode);
1803 sp->sa_uid = nfs_xdrneg1;
1804 sp->sa_gid = nfs_xdrneg1;
1805 sp->sa_size = nfs_xdrneg1;
1806 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1807 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1808 }
1809 nfsm_request(dvp, NFSPROC_MKDIR, cnp->cn_thread, cnp->cn_cred);
1810 if (!error)
1811 nfsm_mtofh(dvp, newvp, v3, gotvp);
1812 if (v3)
1813 nfsm_wcc_data(dvp, wccflag);
1814 m_freem(mrep);
1815 nfsmout:
1816 VTONFS(dvp)->n_flag |= NMODIFIED;
1817 if (!wccflag)
1818 VTONFS(dvp)->n_attrstamp = 0;
1819 /*
1820 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
1821 * if we can succeed in looking up the directory.
1822 */
1823 if (error == EEXIST || (!error && !gotvp)) {
1824 if (newvp) {
1825 vput(newvp);
1826 newvp = NULL;
1827 }
1828 error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred,
1829 cnp->cn_thread, &np);
1830 if (!error) {
1831 newvp = NFSTOV(np);
1832 if (newvp->v_type != VDIR)
1833 error = EEXIST;
1834 }
1835 }
1836 if (error) {
1837 if (newvp)
1838 vput(newvp);
1839 } else
1840 *ap->a_vpp = newvp;
1841 return (error);
1842 }
1843
1844 /*
1845 * nfs remove directory call
1846 */
1847 static int
1848 nfs_rmdir(struct vop_rmdir_args *ap)
1849 {
1850 struct vnode *vp = ap->a_vp;
1851 struct vnode *dvp = ap->a_dvp;
1852 struct componentname *cnp = ap->a_cnp;
1853 caddr_t bpos, dpos;
1854 int error = 0, wccflag = NFSV3_WCCRATTR;
1855 struct mbuf *mreq, *mrep, *md, *mb;
1856 int v3 = NFS_ISV3(dvp);
1857
1858 if (dvp == vp)
1859 return (EINVAL);
1860 nfsstats.rpccnt[NFSPROC_RMDIR]++;
1861 mreq = nfsm_reqhead(dvp, NFSPROC_RMDIR,
1862 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
1863 mb = mreq;
1864 bpos = mtod(mb, caddr_t);
1865 nfsm_fhtom(dvp, v3);
1866 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1867 nfsm_request(dvp, NFSPROC_RMDIR, cnp->cn_thread, cnp->cn_cred);
1868 if (v3)
1869 nfsm_wcc_data(dvp, wccflag);
1870 m_freem(mrep);
1871 nfsmout:
1872 VTONFS(dvp)->n_flag |= NMODIFIED;
1873 if (!wccflag)
1874 VTONFS(dvp)->n_attrstamp = 0;
1875 cache_purge(dvp);
1876 cache_purge(vp);
1877 /*
1878 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
1879 */
1880 if (error == ENOENT)
1881 error = 0;
1882 return (error);
1883 }
1884
1885 /*
1886 * nfs readdir call
1887 */
1888 static int
1889 nfs_readdir(struct vop_readdir_args *ap)
1890 {
1891 struct vnode *vp = ap->a_vp;
1892 struct nfsnode *np = VTONFS(vp);
1893 struct uio *uio = ap->a_uio;
1894 int tresid, error;
1895 struct vattr vattr;
1896
1897 if (vp->v_type != VDIR)
1898 return (EPERM);
1899 /*
1900 * First, check for hit on the EOF offset cache
1901 */
1902 if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
1903 (np->n_flag & NMODIFIED) == 0) {
1904 if (VOP_GETATTR(vp, &vattr, ap->a_cred, uio->uio_td) == 0 &&
1905 !NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime)) {
1906 nfsstats.direofcache_hits++;
1907 return (0);
1908 }
1909 }
1910
1911 /*
1912 * Call nfs_bioread() to do the real work.
1913 */
1914 tresid = uio->uio_resid;
1915 error = nfs_bioread(vp, uio, 0, ap->a_cred);
1916
1917 if (!error && uio->uio_resid == tresid)
1918 nfsstats.direofcache_misses++;
1919 return (error);
1920 }
1921
1922 /*
1923 * Readdir rpc call.
1924 * Called from below the buffer cache by nfs_doio().
1925 */
1926 int
1927 nfs_readdirrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1928 {
1929 int len, left;
1930 struct dirent *dp = NULL;
1931 u_int32_t *tl;
1932 caddr_t cp;
1933 nfsuint64 *cookiep;
1934 caddr_t bpos, dpos;
1935 struct mbuf *mreq, *mrep, *md, *mb;
1936 nfsuint64 cookie;
1937 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1938 struct nfsnode *dnp = VTONFS(vp);
1939 u_quad_t fileno;
1940 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
1941 int attrflag;
1942 int v3 = NFS_ISV3(vp);
1943
1944 #ifndef DIAGNOSTIC
1945 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
1946 (uiop->uio_resid & (DIRBLKSIZ - 1)))
1947 panic("nfs readdirrpc bad uio");
1948 #endif
1949
1950 /*
1951 * If there is no cookie, assume directory was stale.
1952 */
1953 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
1954 if (cookiep)
1955 cookie = *cookiep;
1956 else
1957 return (NFSERR_BAD_COOKIE);
1958 /*
1959 * Loop around doing readdir rpc's of size nm_readdirsize
1960 * truncated to a multiple of DIRBLKSIZ.
1961 * The stopping criteria is EOF or buffer full.
1962 */
1963 while (more_dirs && bigenough) {
1964 nfsstats.rpccnt[NFSPROC_READDIR]++;
1965 mreq = nfsm_reqhead(vp, NFSPROC_READDIR, NFSX_FH(v3) +
1966 NFSX_READDIR(v3));
1967 mb = mreq;
1968 bpos = mtod(mb, caddr_t);
1969 nfsm_fhtom(vp, v3);
1970 if (v3) {
1971 tl = nfsm_build(u_int32_t *, 5 * NFSX_UNSIGNED);
1972 *tl++ = cookie.nfsuquad[0];
1973 *tl++ = cookie.nfsuquad[1];
1974 *tl++ = dnp->n_cookieverf.nfsuquad[0];
1975 *tl++ = dnp->n_cookieverf.nfsuquad[1];
1976 } else {
1977 tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED);
1978 *tl++ = cookie.nfsuquad[0];
1979 }
1980 *tl = txdr_unsigned(nmp->nm_readdirsize);
1981 nfsm_request(vp, NFSPROC_READDIR, uiop->uio_td, cred);
1982 if (v3) {
1983 nfsm_postop_attr(vp, attrflag);
1984 if (!error) {
1985 tl = nfsm_dissect(u_int32_t *,
1986 2 * NFSX_UNSIGNED);
1987 dnp->n_cookieverf.nfsuquad[0] = *tl++;
1988 dnp->n_cookieverf.nfsuquad[1] = *tl;
1989 } else {
1990 m_freem(mrep);
1991 goto nfsmout;
1992 }
1993 }
1994 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
1995 more_dirs = fxdr_unsigned(int, *tl);
1996
1997 /* loop thru the dir entries, doctoring them to 4bsd form */
1998 while (more_dirs && bigenough) {
1999 if (v3) {
2000 tl = nfsm_dissect(u_int32_t *,
2001 3 * NFSX_UNSIGNED);
2002 fileno = fxdr_hyper(tl);
2003 len = fxdr_unsigned(int, *(tl + 2));
2004 } else {
2005 tl = nfsm_dissect(u_int32_t *,
2006 2 * NFSX_UNSIGNED);
2007 fileno = fxdr_unsigned(u_quad_t, *tl++);
2008 len = fxdr_unsigned(int, *tl);
2009 }
2010 if (len <= 0 || len > NFS_MAXNAMLEN) {
2011 error = EBADRPC;
2012 m_freem(mrep);
2013 goto nfsmout;
2014 }
2015 tlen = nfsm_rndup(len);
2016 if (tlen == len)
2017 tlen += 4; /* To ensure null termination */
2018 left = DIRBLKSIZ - blksiz;
2019 if ((tlen + DIRHDSIZ) > left) {
2020 dp->d_reclen += left;
2021 uiop->uio_iov->iov_base =
2022 (char *)uiop->uio_iov->iov_base + left;
2023 uiop->uio_iov->iov_len -= left;
2024 uiop->uio_offset += left;
2025 uiop->uio_resid -= left;
2026 blksiz = 0;
2027 }
2028 if ((tlen + DIRHDSIZ) > uiop->uio_resid)
2029 bigenough = 0;
2030 if (bigenough) {
2031 dp = (struct dirent *)uiop->uio_iov->iov_base;
2032 dp->d_fileno = (int)fileno;
2033 dp->d_namlen = len;
2034 dp->d_reclen = tlen + DIRHDSIZ;
2035 dp->d_type = DT_UNKNOWN;
2036 blksiz += dp->d_reclen;
2037 if (blksiz == DIRBLKSIZ)
2038 blksiz = 0;
2039 uiop->uio_offset += DIRHDSIZ;
2040 uiop->uio_resid -= DIRHDSIZ;
2041 uiop->uio_iov->iov_base =
2042 (char *)uiop->uio_iov->iov_base + DIRHDSIZ;
2043 uiop->uio_iov->iov_len -= DIRHDSIZ;
2044 nfsm_mtouio(uiop, len);
2045 cp = uiop->uio_iov->iov_base;
2046 tlen -= len;
2047 *cp = '\0'; /* null terminate */
2048 uiop->uio_iov->iov_base =
2049 (char *)uiop->uio_iov->iov_base + tlen;
2050 uiop->uio_iov->iov_len -= tlen;
2051 uiop->uio_offset += tlen;
2052 uiop->uio_resid -= tlen;
2053 } else
2054 nfsm_adv(nfsm_rndup(len));
2055 if (v3) {
2056 tl = nfsm_dissect(u_int32_t *,
2057 3 * NFSX_UNSIGNED);
2058 } else {
2059 tl = nfsm_dissect(u_int32_t *,
2060 2 * NFSX_UNSIGNED);
2061 }
2062 if (bigenough) {
2063 cookie.nfsuquad[0] = *tl++;
2064 if (v3)
2065 cookie.nfsuquad[1] = *tl++;
2066 } else if (v3)
2067 tl += 2;
2068 else
2069 tl++;
2070 more_dirs = fxdr_unsigned(int, *tl);
2071 }
2072 /*
2073 * If at end of rpc data, get the eof boolean
2074 */
2075 if (!more_dirs) {
2076 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2077 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2078 }
2079 m_freem(mrep);
2080 }
2081 /*
2082 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2083 * by increasing d_reclen for the last record.
2084 */
2085 if (blksiz > 0) {
2086 left = DIRBLKSIZ - blksiz;
2087 dp->d_reclen += left;
2088 uiop->uio_iov->iov_base =
2089 (char *)uiop->uio_iov->iov_base + left;
2090 uiop->uio_iov->iov_len -= left;
2091 uiop->uio_offset += left;
2092 uiop->uio_resid -= left;
2093 }
2094
2095 /*
2096 * We are now either at the end of the directory or have filled the
2097 * block.
2098 */
2099 if (bigenough)
2100 dnp->n_direofoffset = uiop->uio_offset;
2101 else {
2102 if (uiop->uio_resid > 0)
2103 printf("EEK! readdirrpc resid > 0\n");
2104 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2105 *cookiep = cookie;
2106 }
2107 nfsmout:
2108 return (error);
2109 }
2110
2111 /*
2112 * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
2113 */
2114 int
2115 nfs_readdirplusrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
2116 {
2117 int len, left;
2118 struct dirent *dp;
2119 u_int32_t *tl;
2120 caddr_t cp;
2121 struct vnode *newvp;
2122 nfsuint64 *cookiep;
2123 caddr_t bpos, dpos, dpossav1, dpossav2;
2124 struct mbuf *mreq, *mrep, *md, *mb, *mdsav1, *mdsav2;
2125 struct nameidata nami, *ndp = &nami;
2126 struct componentname *cnp = &ndp->ni_cnd;
2127 nfsuint64 cookie;
2128 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2129 struct nfsnode *dnp = VTONFS(vp), *np;
2130 nfsfh_t *fhp;
2131 u_quad_t fileno;
2132 int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
2133 int attrflag, fhsize;
2134
2135 #ifndef nolint
2136 dp = NULL;
2137 #endif
2138 #ifndef DIAGNOSTIC
2139 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2140 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2141 panic("nfs readdirplusrpc bad uio");
2142 #endif
2143 ndp->ni_dvp = vp;
2144 newvp = NULLVP;
2145
2146 /*
2147 * If there is no cookie, assume directory was stale.
2148 */
2149 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2150 if (cookiep)
2151 cookie = *cookiep;
2152 else
2153 return (NFSERR_BAD_COOKIE);
2154 /*
2155 * Loop around doing readdir rpc's of size nm_readdirsize
2156 * truncated to a multiple of DIRBLKSIZ.
2157 * The stopping criteria is EOF or buffer full.
2158 */
2159 while (more_dirs && bigenough) {
2160 nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
2161 mreq = nfsm_reqhead(vp, NFSPROC_READDIRPLUS,
2162 NFSX_FH(1) + 6 * NFSX_UNSIGNED);
2163 mb = mreq;
2164 bpos = mtod(mb, caddr_t);
2165 nfsm_fhtom(vp, 1);
2166 tl = nfsm_build(u_int32_t *, 6 * NFSX_UNSIGNED);
2167 *tl++ = cookie.nfsuquad[0];
2168 *tl++ = cookie.nfsuquad[1];
2169 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2170 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2171 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
2172 *tl = txdr_unsigned(nmp->nm_rsize);
2173 nfsm_request(vp, NFSPROC_READDIRPLUS, uiop->uio_td, cred);
2174 nfsm_postop_attr(vp, attrflag);
2175 if (error) {
2176 m_freem(mrep);
2177 goto nfsmout;
2178 }
2179 tl = nfsm_dissect(u_int32_t *, 3 * NFSX_UNSIGNED);
2180 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2181 dnp->n_cookieverf.nfsuquad[1] = *tl++;
2182 more_dirs = fxdr_unsigned(int, *tl);
2183
2184 /* loop thru the dir entries, doctoring them to 4bsd form */
2185 while (more_dirs && bigenough) {
2186 tl = nfsm_dissect(u_int32_t *, 3 * NFSX_UNSIGNED);
2187 fileno = fxdr_hyper(tl);
2188 len = fxdr_unsigned(int, *(tl + 2));
2189 if (len <= 0 || len > NFS_MAXNAMLEN) {
2190 error = EBADRPC;
2191 m_freem(mrep);
2192 goto nfsmout;
2193 }
2194 tlen = nfsm_rndup(len);
2195 if (tlen == len)
2196 tlen += 4; /* To ensure null termination*/
2197 left = DIRBLKSIZ - blksiz;
2198 if ((tlen + DIRHDSIZ) > left) {
2199 dp->d_reclen += left;
2200 uiop->uio_iov->iov_base =
2201 (char *)uiop->uio_iov->iov_base + left;
2202 uiop->uio_iov->iov_len -= left;
2203 uiop->uio_offset += left;
2204 uiop->uio_resid -= left;
2205 blksiz = 0;
2206 }
2207 if ((tlen + DIRHDSIZ) > uiop->uio_resid)
2208 bigenough = 0;
2209 if (bigenough) {
2210 dp = (struct dirent *)uiop->uio_iov->iov_base;
2211 dp->d_fileno = (int)fileno;
2212 dp->d_namlen = len;
2213 dp->d_reclen = tlen + DIRHDSIZ;
2214 dp->d_type = DT_UNKNOWN;
2215 blksiz += dp->d_reclen;
2216 if (blksiz == DIRBLKSIZ)
2217 blksiz = 0;
2218 uiop->uio_offset += DIRHDSIZ;
2219 uiop->uio_resid -= DIRHDSIZ;
2220 uiop->uio_iov->iov_base =
2221 (char *)uiop->uio_iov->iov_base + DIRHDSIZ;
2222 uiop->uio_iov->iov_len -= DIRHDSIZ;
2223 cnp->cn_nameptr = uiop->uio_iov->iov_base;
2224 cnp->cn_namelen = len;
2225 nfsm_mtouio(uiop, len);
2226 cp = uiop->uio_iov->iov_base;
2227 tlen -= len;
2228 *cp = '\0';
2229 uiop->uio_iov->iov_base =
2230 (char *)uiop->uio_iov->iov_base + tlen;
2231 uiop->uio_iov->iov_len -= tlen;
2232 uiop->uio_offset += tlen;
2233 uiop->uio_resid -= tlen;
2234 } else
2235 nfsm_adv(nfsm_rndup(len));
2236 tl = nfsm_dissect(u_int32_t *, 3 * NFSX_UNSIGNED);
2237 if (bigenough) {
2238 cookie.nfsuquad[0] = *tl++;
2239 cookie.nfsuquad[1] = *tl++;
2240 } else
2241 tl += 2;
2242
2243 /*
2244 * Since the attributes are before the file handle
2245 * (sigh), we must skip over the attributes and then
2246 * come back and get them.
2247 */
2248 attrflag = fxdr_unsigned(int, *tl);
2249 if (attrflag) {
2250 dpossav1 = dpos;
2251 mdsav1 = md;
2252 nfsm_adv(NFSX_V3FATTR);
2253 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2254 doit = fxdr_unsigned(int, *tl);
2255 /*
2256 * Skip loading the attrs for "..". There's a
2257 * race between loading the attrs here and
2258 * lookups that look for the directory currently
2259 * being read (in the parent). We try to acquire
2260 * the exclusive lock on ".." here, owning the
2261 * lock on the directory being read. Lookup will
2262 * hold the lock on ".." and try to acquire the
2263 * lock on the directory being read.
2264 *
2265 * There are other ways of fixing this, one would
2266 * be to do a trylock on the ".." vnode and skip
2267 * loading the attrs on ".." if it happens to be
2268 * locked by another process. But skipping the
2269 * attrload on ".." seems the easiest option.
2270 */
2271 if (strcmp(dp->d_name, "..") == 0) {
2272 doit = 0;
2273 /*
2274 * We've already skipped over the attrs,
2275 * skip over the filehandle. And store d_type
2276 * as VDIR.
2277 */
2278 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2279 i = fxdr_unsigned(int, *tl);
2280 nfsm_adv(nfsm_rndup(i));
2281 dp->d_type = IFTODT(VTTOIF(VDIR));
2282 }
2283 if (doit) {
2284 nfsm_getfh(fhp, fhsize, 1);
2285 if (NFS_CMPFH(dnp, fhp, fhsize)) {
2286 VREF(vp);
2287 newvp = vp;
2288 np = dnp;
2289 } else {
2290 error = nfs_nget(vp->v_mount, fhp,
2291 fhsize, &np);
2292 if (error)
2293 doit = 0;
2294 else
2295 newvp = NFSTOV(np);
2296 }
2297 }
2298 if (doit && bigenough) {
2299 dpossav2 = dpos;
2300 dpos = dpossav1;
2301 mdsav2 = md;
2302 md = mdsav1;
2303 nfsm_loadattr(newvp, NULL);
2304 dpos = dpossav2;
2305 md = mdsav2;
2306 dp->d_type =
2307 IFTODT(VTTOIF(np->n_vattr.va_type));
2308 ndp->ni_vp = newvp;
2309 cache_enter(ndp->ni_dvp, ndp->ni_vp, cnp);
2310 }
2311 } else {
2312 /* Just skip over the file handle */
2313 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2314 i = fxdr_unsigned(int, *tl);
2315 if (i) {
2316 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2317 fhsize = fxdr_unsigned(int, *tl);
2318 nfsm_adv(nfsm_rndup(fhsize));
2319 }
2320 }
2321 if (newvp != NULLVP) {
2322 if (newvp == vp)
2323 vrele(newvp);
2324 else
2325 vput(newvp);
2326 newvp = NULLVP;
2327 }
2328 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2329 more_dirs = fxdr_unsigned(int, *tl);
2330 }
2331 /*
2332 * If at end of rpc data, get the eof boolean
2333 */
2334 if (!more_dirs) {
2335 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2336 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2337 }
2338 m_freem(mrep);
2339 }
2340 /*
2341 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2342 * by increasing d_reclen for the last record.
2343 */
2344 if (blksiz > 0) {
2345 left = DIRBLKSIZ - blksiz;
2346 dp->d_reclen += left;
2347 uiop->uio_iov->iov_base =
2348 (char *)uiop->uio_iov->iov_base + left;
2349 uiop->uio_iov->iov_len -= left;
2350 uiop->uio_offset += left;
2351 uiop->uio_resid -= left;
2352 }
2353
2354 /*
2355 * We are now either at the end of the directory or have filled the
2356 * block.
2357 */
2358 if (bigenough)
2359 dnp->n_direofoffset = uiop->uio_offset;
2360 else {
2361 if (uiop->uio_resid > 0)
2362 printf("EEK! readdirplusrpc resid > 0\n");
2363 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2364 *cookiep = cookie;
2365 }
2366 nfsmout:
2367 if (newvp != NULLVP) {
2368 if (newvp == vp)
2369 vrele(newvp);
2370 else
2371 vput(newvp);
2372 newvp = NULLVP;
2373 }
2374 return (error);
2375 }
2376
2377 /*
2378 * Silly rename. To make the NFS filesystem that is stateless look a little
2379 * more like the "ufs" a remove of an active vnode is translated to a rename
2380 * to a funny looking filename that is removed by nfs_inactive on the
2381 * nfsnode. There is the potential for another process on a different client
2382 * to create the same funny name between the nfs_lookitup() fails and the
2383 * nfs_rename() completes, but...
2384 */
2385 static int
2386 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2387 {
2388 struct sillyrename *sp;
2389 struct nfsnode *np;
2390 int error;
2391 short pid;
2392 unsigned int lticks;
2393
2394 cache_purge(dvp);
2395 np = VTONFS(vp);
2396 #ifndef DIAGNOSTIC
2397 if (vp->v_type == VDIR)
2398 panic("nfs: sillyrename dir");
2399 #endif
2400 MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename),
2401 M_NFSREQ, M_WAITOK);
2402 sp->s_cred = crhold(cnp->cn_cred);
2403 sp->s_dvp = dvp;
2404 sp->s_removeit = nfs_removeit;
2405 VREF(dvp);
2406
2407 /*
2408 * Fudge together a funny name.
2409 * Changing the format of the funny name to accomodate more
2410 * sillynames per directory.
2411 * The name is now changed to .nfs.<ticks>.<pid>.4, where ticks is
2412 * CPU ticks since boot.
2413 */
2414 pid = cnp->cn_thread->td_proc->p_pid;
2415 lticks = (unsigned int)ticks;
2416 for ( ; ; ) {
2417 sp->s_namlen = sprintf(sp->s_name,
2418 ".nfs.%08x.%04x4.4", lticks,
2419 pid);
2420 if (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2421 cnp->cn_thread, NULL))
2422 break;
2423 lticks++;
2424 }
2425 error = nfs_renameit(dvp, cnp, sp);
2426 if (error)
2427 goto bad;
2428 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2429 cnp->cn_thread, &np);
2430 np->n_sillyrename = sp;
2431 return (0);
2432 bad:
2433 vrele(sp->s_dvp);
2434 crfree(sp->s_cred);
2435 free((caddr_t)sp, M_NFSREQ);
2436 return (error);
2437 }
2438
2439 /*
2440 * Look up a file name and optionally either update the file handle or
2441 * allocate an nfsnode, depending on the value of npp.
2442 * npp == NULL --> just do the lookup
2443 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2444 * handled too
2445 * *npp != NULL --> update the file handle in the vnode
2446 */
2447 static int
2448 nfs_lookitup(struct vnode *dvp, const char *name, int len, struct ucred *cred,
2449 struct thread *td, struct nfsnode **npp)
2450 {
2451 struct vnode *newvp = NULL;
2452 struct nfsnode *np, *dnp = VTONFS(dvp);
2453 caddr_t bpos, dpos;
2454 int error = 0, fhlen, attrflag;
2455 struct mbuf *mreq, *mrep, *md, *mb;
2456 nfsfh_t *nfhp;
2457 int v3 = NFS_ISV3(dvp);
2458
2459 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
2460 mreq = nfsm_reqhead(dvp, NFSPROC_LOOKUP,
2461 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
2462 mb = mreq;
2463 bpos = mtod(mb, caddr_t);
2464 nfsm_fhtom(dvp, v3);
2465 nfsm_strtom(name, len, NFS_MAXNAMLEN);
2466 nfsm_request(dvp, NFSPROC_LOOKUP, td, cred);
2467 if (npp && !error) {
2468 nfsm_getfh(nfhp, fhlen, v3);
2469 if (*npp) {
2470 np = *npp;
2471 if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
2472 free((caddr_t)np->n_fhp, M_NFSBIGFH);
2473 np->n_fhp = &np->n_fh;
2474 } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH)
2475 np->n_fhp =(nfsfh_t *)malloc(fhlen, M_NFSBIGFH, M_WAITOK);
2476 bcopy((caddr_t)nfhp, (caddr_t)np->n_fhp, fhlen);
2477 np->n_fhsize = fhlen;
2478 newvp = NFSTOV(np);
2479 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
2480 VREF(dvp);
2481 newvp = dvp;
2482 } else {
2483 error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np);
2484 if (error) {
2485 m_freem(mrep);
2486 return (error);
2487 }
2488 newvp = NFSTOV(np);
2489 }
2490 if (v3) {
2491 nfsm_postop_attr(newvp, attrflag);
2492 if (!attrflag && *npp == NULL) {
2493 m_freem(mrep);
2494 if (newvp == dvp)
2495 vrele(newvp);
2496 else
2497 vput(newvp);
2498 return (ENOENT);
2499 }
2500 } else
2501 nfsm_loadattr(newvp, NULL);
2502 }
2503 m_freem(mrep);
2504 nfsmout:
2505 if (npp && *npp == NULL) {
2506 if (error) {
2507 if (newvp) {
2508 if (newvp == dvp)
2509 vrele(newvp);
2510 else
2511 vput(newvp);
2512 }
2513 } else
2514 *npp = np;
2515 }
2516 return (error);
2517 }
2518
2519 /*
2520 * Nfs Version 3 commit rpc
2521 */
2522 int
2523 nfs_commit(struct vnode *vp, u_quad_t offset, int cnt, struct ucred *cred,
2524 struct thread *td)
2525 {
2526 u_int32_t *tl;
2527 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2528 caddr_t bpos, dpos;
2529 int error = 0, wccflag = NFSV3_WCCRATTR;
2530 struct mbuf *mreq, *mrep, *md, *mb;
2531
2532 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0)
2533 return (0);
2534 nfsstats.rpccnt[NFSPROC_COMMIT]++;
2535 mreq = nfsm_reqhead(vp, NFSPROC_COMMIT, NFSX_FH(1));
2536 mb = mreq;
2537 bpos = mtod(mb, caddr_t);
2538 nfsm_fhtom(vp, 1);
2539 tl = nfsm_build(u_int32_t *, 3 * NFSX_UNSIGNED);
2540 txdr_hyper(offset, tl);
2541 tl += 2;
2542 *tl = txdr_unsigned(cnt);
2543 nfsm_request(vp, NFSPROC_COMMIT, td, cred);
2544 nfsm_wcc_data(vp, wccflag);
2545 if (!error) {
2546 tl = nfsm_dissect(u_int32_t *, NFSX_V3WRITEVERF);
2547 if (bcmp((caddr_t)nmp->nm_verf, (caddr_t)tl,
2548 NFSX_V3WRITEVERF)) {
2549 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
2550 NFSX_V3WRITEVERF);
2551 error = NFSERR_STALEWRITEVERF;
2552 }
2553 }
2554 m_freem(mrep);
2555 nfsmout:
2556 return (error);
2557 }
2558
2559 /*
2560 * Strategy routine.
2561 * For async requests when nfsiod(s) are running, queue the request by
2562 * calling nfs_asyncio(), otherwise just all nfs_doio() to do the
2563 * request.
2564 */
2565 static int
2566 nfs_strategy(struct vop_strategy_args *ap)
2567 {
2568 struct buf *bp = ap->a_bp;
2569 struct ucred *cr;
2570 struct thread *td;
2571
2572 KASSERT(!(bp->b_flags & B_DONE), ("nfs_strategy: buffer %p unexpectedly marked B_DONE", bp));
2573 KASSERT(BUF_REFCNT(bp) > 0, ("nfs_strategy: buffer %p not locked", bp));
2574
2575 if (bp->b_flags & B_ASYNC)
2576 td = NULL;
2577 else
2578 td = curthread; /* XXX */
2579
2580 if (bp->b_iocmd == BIO_READ)
2581 cr = bp->b_rcred;
2582 else
2583 cr = bp->b_wcred;
2584
2585 /*
2586 * If the op is asynchronous and an i/o daemon is waiting
2587 * queue the request, wake it up and wait for completion
2588 * otherwise just do it ourselves.
2589 */
2590 if ((bp->b_flags & B_ASYNC) == 0 ||
2591 nfs_asyncio(VFSTONFS(ap->a_vp->v_mount), bp, NOCRED, td))
2592 (void)nfs_doio(ap->a_vp, bp, cr, td);
2593 return (0);
2594 }
2595
2596 /*
2597 * fsync vnode op. Just call nfs_flush() with commit == 1.
2598 */
2599 /* ARGSUSED */
2600 static int
2601 nfs_fsync(struct vop_fsync_args *ap)
2602 {
2603
2604 return (nfs_flush(ap->a_vp, ap->a_waitfor, ap->a_td, 1));
2605 }
2606
2607 /*
2608 * Flush all the blocks associated with a vnode.
2609 * Walk through the buffer pool and push any dirty pages
2610 * associated with the vnode.
2611 */
2612 static int
2613 nfs_flush(struct vnode *vp, int waitfor, struct thread *td,
2614 int commit)
2615 {
2616 struct nfsnode *np = VTONFS(vp);
2617 struct buf *bp;
2618 int i;
2619 struct buf *nbp;
2620 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2621 int s, error = 0, slptimeo = 0, slpflag = 0, retv, bvecpos;
2622 int passone = 1;
2623 u_quad_t off, endoff, toff;
2624 struct ucred* wcred = NULL;
2625 struct buf **bvec = NULL;
2626 #ifndef NFS_COMMITBVECSIZ
2627 #define NFS_COMMITBVECSIZ 20
2628 #endif
2629 struct buf *bvec_on_stack[NFS_COMMITBVECSIZ];
2630 int bvecsize = 0, bveccount;
2631
2632 if (nmp->nm_flag & NFSMNT_INT)
2633 slpflag = PCATCH;
2634 if (!commit)
2635 passone = 0;
2636 /*
2637 * A b_flags == (B_DELWRI | B_NEEDCOMMIT) block has been written to the
2638 * server, but has not been committed to stable storage on the server
2639 * yet. On the first pass, the byte range is worked out and the commit
2640 * rpc is done. On the second pass, nfs_writebp() is called to do the
2641 * job.
2642 */
2643 again:
2644 off = (u_quad_t)-1;
2645 endoff = 0;
2646 bvecpos = 0;
2647 if (NFS_ISV3(vp) && commit) {
2648 s = splbio();
2649 if (bvec != NULL && bvec != bvec_on_stack)
2650 free(bvec, M_TEMP);
2651 /*
2652 * Count up how many buffers waiting for a commit.
2653 */
2654 bveccount = 0;
2655 VI_LOCK(vp);
2656 TAILQ_FOREACH_SAFE(bp, &vp->v_bufobj.bo_dirty.bv_hd, b_bobufs, nbp) {
2657 if (BUF_REFCNT(bp) == 0 &&
2658 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
2659 == (B_DELWRI | B_NEEDCOMMIT))
2660 bveccount++;
2661 }
2662 /*
2663 * Allocate space to remember the list of bufs to commit. It is
2664 * important to use M_NOWAIT here to avoid a race with nfs_write.
2665 * If we can't get memory (for whatever reason), we will end up
2666 * committing the buffers one-by-one in the loop below.
2667 */
2668 if (bveccount > NFS_COMMITBVECSIZ) {
2669 /*
2670 * Release the vnode interlock to avoid a lock
2671 * order reversal.
2672 */
2673 VI_UNLOCK(vp);
2674 bvec = (struct buf **)
2675 malloc(bveccount * sizeof(struct buf *),
2676 M_TEMP, M_NOWAIT);
2677 VI_LOCK(vp);
2678 if (bvec == NULL) {
2679 bvec = bvec_on_stack;
2680 bvecsize = NFS_COMMITBVECSIZ;
2681 } else
2682 bvecsize = bveccount;
2683 } else {
2684 bvec = bvec_on_stack;
2685 bvecsize = NFS_COMMITBVECSIZ;
2686 }
2687 TAILQ_FOREACH_SAFE(bp, &vp->v_bufobj.bo_dirty.bv_hd, b_bobufs, nbp) {
2688 if (bvecpos >= bvecsize)
2689 break;
2690 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
2691 nbp = TAILQ_NEXT(bp, b_bobufs);
2692 continue;
2693 }
2694 if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
2695 (B_DELWRI | B_NEEDCOMMIT)) {
2696 BUF_UNLOCK(bp);
2697 nbp = TAILQ_NEXT(bp, b_bobufs);
2698 continue;
2699 }
2700 VI_UNLOCK(vp);
2701 bremfree(bp);
2702 /*
2703 * Work out if all buffers are using the same cred
2704 * so we can deal with them all with one commit.
2705 *
2706 * NOTE: we are not clearing B_DONE here, so we have
2707 * to do it later on in this routine if we intend to
2708 * initiate I/O on the bp.
2709 *
2710 * Note: to avoid loopback deadlocks, we do not
2711 * assign b_runningbufspace.
2712 */
2713 if (wcred == NULL)
2714 wcred = bp->b_wcred;
2715 else if (wcred != bp->b_wcred)
2716 wcred = NOCRED;
2717 vfs_busy_pages(bp, 1);
2718
2719 VI_LOCK(vp);
2720 /*
2721 * bp is protected by being locked, but nbp is not
2722 * and vfs_busy_pages() may sleep. We have to
2723 * recalculate nbp.
2724 */
2725 nbp = TAILQ_NEXT(bp, b_bobufs);
2726
2727 /*
2728 * A list of these buffers is kept so that the
2729 * second loop knows which buffers have actually
2730 * been committed. This is necessary, since there
2731 * may be a race between the commit rpc and new
2732 * uncommitted writes on the file.
2733 */
2734 bvec[bvecpos++] = bp;
2735 toff = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2736 bp->b_dirtyoff;
2737 if (toff < off)
2738 off = toff;
2739 toff += (u_quad_t)(bp->b_dirtyend - bp->b_dirtyoff);
2740 if (toff > endoff)
2741 endoff = toff;
2742 }
2743 splx(s);
2744 VI_UNLOCK(vp);
2745 }
2746 if (bvecpos > 0) {
2747 /*
2748 * Commit data on the server, as required.
2749 * If all bufs are using the same wcred, then use that with
2750 * one call for all of them, otherwise commit each one
2751 * separately.
2752 */
2753 if (wcred != NOCRED)
2754 retv = nfs_commit(vp, off, (int)(endoff - off),
2755 wcred, td);
2756 else {
2757 retv = 0;
2758 for (i = 0; i < bvecpos; i++) {
2759 off_t off, size;
2760 bp = bvec[i];
2761 off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2762 bp->b_dirtyoff;
2763 size = (u_quad_t)(bp->b_dirtyend
2764 - bp->b_dirtyoff);
2765 retv = nfs_commit(vp, off, (int)size,
2766 bp->b_wcred, td);
2767 if (retv) break;
2768 }
2769 }
2770
2771 if (retv == NFSERR_STALEWRITEVERF)
2772 nfs_clearcommit(vp->v_mount);
2773
2774 /*
2775 * Now, either mark the blocks I/O done or mark the
2776 * blocks dirty, depending on whether the commit
2777 * succeeded.
2778 */
2779 for (i = 0; i < bvecpos; i++) {
2780 bp = bvec[i];
2781 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
2782 if (retv) {
2783 /*
2784 * Error, leave B_DELWRI intact
2785 */
2786 vfs_unbusy_pages(bp);
2787 brelse(bp);
2788 } else {
2789 /*
2790 * Success, remove B_DELWRI ( bundirty() ).
2791 *
2792 * b_dirtyoff/b_dirtyend seem to be NFS
2793 * specific. We should probably move that
2794 * into bundirty(). XXX
2795 */
2796 s = splbio();
2797 bufobj_wref(&vp->v_bufobj);
2798 bp->b_flags |= B_ASYNC;
2799 bundirty(bp);
2800 bp->b_flags &= ~B_DONE;
2801 bp->b_ioflags &= ~BIO_ERROR;
2802 bp->b_dirtyoff = bp->b_dirtyend = 0;
2803 splx(s);
2804 bufdone(bp);
2805 }
2806 }
2807 }
2808
2809 /*
2810 * Start/do any write(s) that are required.
2811 */
2812 loop:
2813 s = splbio();
2814 VI_LOCK(vp);
2815 TAILQ_FOREACH_SAFE(bp, &vp->v_bufobj.bo_dirty.bv_hd, b_bobufs, nbp) {
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 if (nfs_sigintr(nmp, NULL, td)) {
2829 error = EINTR;
2830 goto done;
2831 }
2832 if (slpflag == PCATCH) {
2833 slpflag = 0;
2834 slptimeo = 2 * hz;
2835 }
2836 goto loop;
2837 }
2838 if ((bp->b_flags & B_DELWRI) == 0)
2839 panic("nfs_fsync: not dirty");
2840 if ((passone || !commit) && (bp->b_flags & B_NEEDCOMMIT)) {
2841 BUF_UNLOCK(bp);
2842 continue;
2843 }
2844 VI_UNLOCK(vp);
2845 bremfree(bp);
2846 if (passone || !commit)
2847 bp->b_flags |= B_ASYNC;
2848 else
2849 bp->b_flags |= B_ASYNC;
2850 splx(s);
2851 bwrite(bp);
2852 if (nfs_sigintr(nmp, NULL, td)) {
2853 error = EINTR;
2854 goto done;
2855 }
2856 goto loop;
2857 }
2858 splx(s);
2859 if (passone) {
2860 passone = 0;
2861 VI_UNLOCK(vp);
2862 goto again;
2863 }
2864 if (waitfor == MNT_WAIT) {
2865 while (vp->v_bufobj.bo_numoutput) {
2866 error = bufobj_wwait(&vp->v_bufobj, slpflag, slptimeo);
2867 if (error) {
2868 VI_UNLOCK(vp);
2869 error = nfs_sigintr(nmp, NULL, td);
2870 if (error)
2871 goto done;
2872 if (slpflag == PCATCH) {
2873 slpflag = 0;
2874 slptimeo = 2 * hz;
2875 }
2876 VI_LOCK(vp);
2877 }
2878 }
2879 if (vp->v_bufobj.bo_dirty.bv_cnt != 0 && commit) {
2880 VI_UNLOCK(vp);
2881 goto loop;
2882 }
2883 }
2884 VI_UNLOCK(vp);
2885 if (np->n_flag & NWRITEERR) {
2886 error = np->n_error;
2887 np->n_flag &= ~NWRITEERR;
2888 }
2889 if (commit && vp->v_bufobj.bo_dirty.bv_cnt == 0)
2890 np->n_flag &= ~NMODIFIED;
2891 done:
2892 if (bvec != NULL && bvec != bvec_on_stack)
2893 free(bvec, M_TEMP);
2894 return (error);
2895 }
2896
2897 /*
2898 * NFS advisory byte-level locks.
2899 */
2900 static int
2901 nfs_advlock(struct vop_advlock_args *ap)
2902 {
2903
2904 if ((VFSTONFS(ap->a_vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) {
2905 struct nfsnode *np = VTONFS(ap->a_vp);
2906
2907 return (lf_advlock(ap, &(np->n_lockf), np->n_size));
2908 }
2909 return (nfs_dolock(ap));
2910 }
2911
2912 /*
2913 * Print out the contents of an nfsnode.
2914 */
2915 static int
2916 nfs_print(struct vop_print_args *ap)
2917 {
2918 struct vnode *vp = ap->a_vp;
2919 struct nfsnode *np = VTONFS(vp);
2920
2921 printf("\tfileid %ld fsid 0x%x",
2922 np->n_vattr.va_fileid, np->n_vattr.va_fsid);
2923 if (vp->v_type == VFIFO)
2924 fifo_printinfo(vp);
2925 printf("\n");
2926 return (0);
2927 }
2928
2929 /*
2930 * This is the "real" nfs::bwrite(struct buf*).
2931 * We set B_CACHE if this is a VMIO buffer.
2932 */
2933 int
2934 nfs_writebp(struct buf *bp, int force __unused, struct thread *td)
2935 {
2936 int s;
2937 int oldflags = bp->b_flags;
2938 #if 0
2939 int retv = 1;
2940 off_t off;
2941 #endif
2942
2943 if (BUF_REFCNT(bp) == 0)
2944 panic("bwrite: buffer is not locked???");
2945
2946 if (bp->b_flags & B_INVAL) {
2947 brelse(bp);
2948 return(0);
2949 }
2950
2951 bp->b_flags |= B_CACHE;
2952
2953 /*
2954 * Undirty the bp. We will redirty it later if the I/O fails.
2955 */
2956
2957 s = splbio();
2958 bundirty(bp);
2959 bp->b_flags &= ~B_DONE;
2960 bp->b_ioflags &= ~BIO_ERROR;
2961 bp->b_iocmd = BIO_WRITE;
2962
2963 bufobj_wref(bp->b_bufobj);
2964 curthread->td_proc->p_stats->p_ru.ru_oublock++;
2965 splx(s);
2966
2967 /*
2968 * Note: to avoid loopback deadlocks, we do not
2969 * assign b_runningbufspace.
2970 */
2971 vfs_busy_pages(bp, 1);
2972
2973 BUF_KERNPROC(bp);
2974 bp->b_iooffset = dbtob(bp->b_blkno);
2975 bstrategy(bp);
2976
2977 if( (oldflags & B_ASYNC) == 0) {
2978 int rtval = bufwait(bp);
2979
2980 if (oldflags & B_DELWRI) {
2981 s = splbio();
2982 reassignbuf(bp);
2983 splx(s);
2984 }
2985
2986 brelse(bp);
2987 return (rtval);
2988 }
2989
2990 return (0);
2991 }
2992
2993 /*
2994 * nfs special file access vnode op.
2995 * Essentially just get vattr and then imitate iaccess() since the device is
2996 * local to the client.
2997 */
2998 static int
2999 nfsspec_access(struct vop_access_args *ap)
3000 {
3001 struct vattr *vap;
3002 struct ucred *cred = ap->a_cred;
3003 struct vnode *vp = ap->a_vp;
3004 mode_t mode = ap->a_mode;
3005 struct vattr vattr;
3006 int error;
3007
3008 /*
3009 * Disallow write attempts on filesystems mounted read-only;
3010 * unless the file is a socket, fifo, or a block or character
3011 * device resident on the filesystem.
3012 */
3013 if ((mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3014 switch (vp->v_type) {
3015 case VREG:
3016 case VDIR:
3017 case VLNK:
3018 return (EROFS);
3019 default:
3020 break;
3021 }
3022 }
3023 vap = &vattr;
3024 error = VOP_GETATTR(vp, vap, cred, ap->a_td);
3025 if (error)
3026 return (error);
3027 return (vaccess(vp->v_type, vap->va_mode, vap->va_uid, vap->va_gid,
3028 mode, cred, NULL));
3029 }
3030
3031 /*
3032 * Read wrapper for fifos.
3033 */
3034 static int
3035 nfsfifo_read(struct vop_read_args *ap)
3036 {
3037 struct nfsnode *np = VTONFS(ap->a_vp);
3038
3039 /*
3040 * Set access flag.
3041 */
3042 np->n_flag |= NACC;
3043 getnanotime(&np->n_atim);
3044 return (fifo_specops.vop_read(ap));
3045 }
3046
3047 /*
3048 * Write wrapper for fifos.
3049 */
3050 static int
3051 nfsfifo_write(struct vop_write_args *ap)
3052 {
3053 struct nfsnode *np = VTONFS(ap->a_vp);
3054
3055 /*
3056 * Set update flag.
3057 */
3058 np->n_flag |= NUPD;
3059 getnanotime(&np->n_mtim);
3060 return (fifo_specops.vop_write(ap));
3061 }
3062
3063 /*
3064 * Close wrapper for fifos.
3065 *
3066 * Update the times on the nfsnode then do fifo close.
3067 */
3068 static int
3069 nfsfifo_close(struct vop_close_args *ap)
3070 {
3071 struct vnode *vp = ap->a_vp;
3072 struct nfsnode *np = VTONFS(vp);
3073 struct vattr vattr;
3074 struct timespec ts;
3075
3076 if (np->n_flag & (NACC | NUPD)) {
3077 getnanotime(&ts);
3078 if (np->n_flag & NACC)
3079 np->n_atim = ts;
3080 if (np->n_flag & NUPD)
3081 np->n_mtim = ts;
3082 np->n_flag |= NCHG;
3083 if (vrefcnt(vp) == 1 &&
3084 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3085 VATTR_NULL(&vattr);
3086 if (np->n_flag & NACC)
3087 vattr.va_atime = np->n_atim;
3088 if (np->n_flag & NUPD)
3089 vattr.va_mtime = np->n_mtim;
3090 (void)VOP_SETATTR(vp, &vattr, ap->a_cred, ap->a_td);
3091 }
3092 }
3093 return (fifo_specops.vop_close(ap));
3094 }
3095
3096 /*
3097 * Just call nfs_writebp() with the force argument set to 1.
3098 *
3099 * NOTE: B_DONE may or may not be set in a_bp on call.
3100 */
3101 static int
3102 nfs_bwrite(struct buf *bp)
3103 {
3104
3105 return (nfs_writebp(bp, 1, curthread));
3106 }
3107
3108 struct buf_ops buf_ops_nfs = {
3109 .bop_name = "buf_ops_nfs",
3110 .bop_write = nfs_bwrite,
3111 .bop_strategy = bufstrategy,
3112 .bop_sync = bufsync,
3113 };
Cache object: 1e9da4253c962c3d36866fec92d53541
|