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