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