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