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