nfs_vnops.c revision 1.132 1 /* $NetBSD: nfs_vnops.c,v 1.132 2001/05/14 18:51:33 fvdl 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 err2 = vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
844 if (err2 != 0) {
845 *vpp = NULLVP;
846 return err2;
847 }
848 cnp->cn_flags &= ~PDIRUNLOCK;
849 }
850
851 err2 = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, cnp->cn_proc);
852 if (err2 != 0) {
853 if (error == 0) {
854 if (*vpp != dvp)
855 vput(*vpp);
856 else
857 vrele(*vpp);
858 }
859 *vpp = NULLVP;
860 return err2;
861 }
862
863 if (error == ENOENT) {
864 if (!VOP_GETATTR(dvp, &vattr, cnp->cn_cred,
865 cnp->cn_proc) && vattr.va_mtime.tv_sec ==
866 VTONFS(dvp)->n_nctime)
867 return ENOENT;
868 cache_purge(dvp);
869 np->n_nctime = 0;
870 goto dorpc;
871 }
872
873 newvp = *vpp;
874 if (!VOP_GETATTR(newvp, &vattr, cnp->cn_cred, cnp->cn_proc)
875 && vattr.va_ctime.tv_sec == VTONFS(newvp)->n_ctime)
876 {
877 nfsstats.lookupcache_hits++;
878 if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
879 cnp->cn_flags |= SAVENAME;
880 if ((!lockparent || !(flags & ISLASTCN)) &&
881 newvp != dvp)
882 VOP_UNLOCK(dvp, 0);
883 return (0);
884 }
885 cache_purge(newvp);
886 if (newvp != dvp)
887 vput(newvp);
888 else
889 vrele(newvp);
890 *vpp = NULLVP;
891 }
892 dorpc:
893 error = 0;
894 newvp = NULLVP;
895 nfsstats.lookupcache_misses++;
896 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
897 len = cnp->cn_namelen;
898 nfsm_reqhead(dvp, NFSPROC_LOOKUP,
899 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
900 nfsm_fhtom(dvp, v3);
901 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
902 nfsm_request(dvp, NFSPROC_LOOKUP, cnp->cn_proc, cnp->cn_cred);
903 if (error) {
904 nfsm_postop_attr(dvp, attrflag);
905 m_freem(mrep);
906 goto nfsmout;
907 }
908 nfsm_getfh(fhp, fhsize, v3);
909
910 /*
911 * Handle RENAME case...
912 */
913 if (cnp->cn_nameiop == RENAME && wantparent && (flags & ISLASTCN)) {
914 if (NFS_CMPFH(np, fhp, fhsize)) {
915 m_freem(mrep);
916 return (EISDIR);
917 }
918 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
919 if (error) {
920 m_freem(mrep);
921 return error;
922 }
923 newvp = NFSTOV(np);
924 if (v3) {
925 nfsm_postop_attr(newvp, attrflag);
926 nfsm_postop_attr(dvp, attrflag);
927 } else
928 nfsm_loadattr(newvp, (struct vattr *)0);
929 *vpp = newvp;
930 m_freem(mrep);
931 cnp->cn_flags |= SAVENAME;
932 if (!lockparent) {
933 VOP_UNLOCK(dvp, 0);
934 cnp->cn_flags |= PDIRUNLOCK;
935 }
936 return (0);
937 }
938
939 /*
940 * The postop attr handling is duplicated for each if case,
941 * because it should be done while dvp is locked (unlocking
942 * dvp is different for each case).
943 */
944
945 if (NFS_CMPFH(np, fhp, fhsize)) {
946 /*
947 * "." lookup
948 */
949 VREF(dvp);
950 newvp = dvp;
951 if (v3) {
952 nfsm_postop_attr(newvp, attrflag);
953 nfsm_postop_attr(dvp, attrflag);
954 } else
955 nfsm_loadattr(newvp, (struct vattr *)0);
956 } else if (flags & ISDOTDOT) {
957 /*
958 * ".." lookup
959 */
960 VOP_UNLOCK(dvp, 0);
961 cnp->cn_flags |= PDIRUNLOCK;
962
963 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
964 if (error) {
965 if (vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY) == 0)
966 cnp->cn_flags &= ~PDIRUNLOCK;
967 m_freem(mrep);
968 return error;
969 }
970 newvp = NFSTOV(np);
971
972 if (v3) {
973 nfsm_postop_attr(newvp, attrflag);
974 nfsm_postop_attr(dvp, attrflag);
975 } else
976 nfsm_loadattr(newvp, (struct vattr *)0);
977
978 if (lockparent && (flags & ISLASTCN)) {
979 if ((error = vn_lock(dvp, LK_EXCLUSIVE))) {
980 m_freem(mrep);
981 vput(newvp);
982 return error;
983 }
984 cnp->cn_flags &= ~PDIRUNLOCK;
985 }
986 } else {
987 /*
988 * Other lookups.
989 */
990 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
991 if (error) {
992 m_freem(mrep);
993 return error;
994 }
995 newvp = NFSTOV(np);
996 if (v3) {
997 nfsm_postop_attr(newvp, attrflag);
998 nfsm_postop_attr(dvp, attrflag);
999 } else
1000 nfsm_loadattr(newvp, (struct vattr *)0);
1001 if (!lockparent || !(flags & ISLASTCN)) {
1002 VOP_UNLOCK(dvp, 0);
1003 cnp->cn_flags |= PDIRUNLOCK;
1004 }
1005 }
1006 if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
1007 cnp->cn_flags |= SAVENAME;
1008 if ((cnp->cn_flags & MAKEENTRY) &&
1009 (cnp->cn_nameiop != DELETE || !(flags & ISLASTCN))) {
1010 np->n_ctime = np->n_vattr->va_ctime.tv_sec;
1011 cache_enter(dvp, newvp, cnp);
1012 }
1013 *vpp = newvp;
1014 nfsm_reqdone;
1015 if (error) {
1016 /*
1017 * We get here only because of errors returned by
1018 * the RPC. Otherwise we'll have returned above
1019 * (the nfsm_* macros will jump to nfsm_reqdone
1020 * on error).
1021 */
1022 if (error == ENOENT && (cnp->cn_flags & MAKEENTRY) &&
1023 cnp->cn_nameiop != CREATE) {
1024 if (VTONFS(dvp)->n_nctime == 0)
1025 VTONFS(dvp)->n_nctime =
1026 VTONFS(dvp)->n_vattr->va_mtime.tv_sec;
1027 cache_enter(dvp, NULL, cnp);
1028 }
1029 if (newvp != NULLVP) {
1030 vrele(newvp);
1031 if (newvp != dvp)
1032 VOP_UNLOCK(newvp, 0);
1033 }
1034 if ((cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME) &&
1035 (flags & ISLASTCN) && error == ENOENT) {
1036 if (dvp->v_mount->mnt_flag & MNT_RDONLY)
1037 error = EROFS;
1038 else
1039 error = EJUSTRETURN;
1040 }
1041 if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
1042 cnp->cn_flags |= SAVENAME;
1043 *vpp = NULL;
1044 }
1045
1046 return error;
1047 }
1048
1049 /*
1050 * nfs read call.
1051 * Just call nfs_bioread() to do the work.
1052 */
1053 int
1054 nfs_read(v)
1055 void *v;
1056 {
1057 struct vop_read_args /* {
1058 struct vnode *a_vp;
1059 struct uio *a_uio;
1060 int a_ioflag;
1061 struct ucred *a_cred;
1062 } */ *ap = v;
1063 struct vnode *vp = ap->a_vp;
1064
1065 if (vp->v_type != VREG)
1066 return (EPERM);
1067 return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag, ap->a_cred, 0));
1068 }
1069
1070 /*
1071 * nfs readlink call
1072 */
1073 int
1074 nfs_readlink(v)
1075 void *v;
1076 {
1077 struct vop_readlink_args /* {
1078 struct vnode *a_vp;
1079 struct uio *a_uio;
1080 struct ucred *a_cred;
1081 } */ *ap = v;
1082 struct vnode *vp = ap->a_vp;
1083
1084 if (vp->v_type != VLNK)
1085 return (EPERM);
1086 return (nfs_bioread(vp, ap->a_uio, 0, ap->a_cred, 0));
1087 }
1088
1089 /*
1090 * Do a readlink rpc.
1091 * Called by nfs_doio() from below the buffer cache.
1092 */
1093 int
1094 nfs_readlinkrpc(vp, uiop, cred)
1095 struct vnode *vp;
1096 struct uio *uiop;
1097 struct ucred *cred;
1098 {
1099 u_int32_t *tl;
1100 caddr_t cp;
1101 int32_t t1, t2;
1102 caddr_t bpos, dpos, cp2;
1103 int error = 0, len, attrflag;
1104 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1105 const int v3 = NFS_ISV3(vp);
1106
1107 nfsstats.rpccnt[NFSPROC_READLINK]++;
1108 nfsm_reqhead(vp, NFSPROC_READLINK, NFSX_FH(v3));
1109 nfsm_fhtom(vp, v3);
1110 nfsm_request(vp, NFSPROC_READLINK, uiop->uio_procp, cred);
1111 if (v3)
1112 nfsm_postop_attr(vp, attrflag);
1113 if (!error) {
1114 nfsm_strsiz(len, NFS_MAXPATHLEN);
1115 nfsm_mtouio(uiop, len);
1116 }
1117 nfsm_reqdone;
1118 return (error);
1119 }
1120
1121 /*
1122 * nfs read rpc call
1123 * Ditto above
1124 */
1125 int
1126 nfs_readrpc(vp, uiop)
1127 struct vnode *vp;
1128 struct uio *uiop;
1129 {
1130 u_int32_t *tl;
1131 caddr_t cp;
1132 int32_t t1, t2;
1133 caddr_t bpos, dpos, cp2;
1134 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1135 struct nfsmount *nmp;
1136 int error = 0, len, retlen, tsiz, eof, attrflag;
1137 const int v3 = NFS_ISV3(vp);
1138
1139 #ifndef nolint
1140 eof = 0;
1141 #endif
1142 nmp = VFSTONFS(vp->v_mount);
1143 tsiz = uiop->uio_resid;
1144 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1145 return (EFBIG);
1146 while (tsiz > 0) {
1147 nfsstats.rpccnt[NFSPROC_READ]++;
1148 len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
1149 nfsm_reqhead(vp, NFSPROC_READ, NFSX_FH(v3) + NFSX_UNSIGNED * 3);
1150 nfsm_fhtom(vp, v3);
1151 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED * 3);
1152 if (v3) {
1153 txdr_hyper(uiop->uio_offset, tl);
1154 *(tl + 2) = txdr_unsigned(len);
1155 } else {
1156 *tl++ = txdr_unsigned(uiop->uio_offset);
1157 *tl++ = txdr_unsigned(len);
1158 *tl = 0;
1159 }
1160 nfsm_request(vp, NFSPROC_READ, uiop->uio_procp,
1161 VTONFS(vp)->n_rcred);
1162 if (v3) {
1163 nfsm_postop_attr(vp, attrflag);
1164 if (error) {
1165 m_freem(mrep);
1166 goto nfsmout;
1167 }
1168 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1169 eof = fxdr_unsigned(int, *(tl + 1));
1170 } else
1171 nfsm_loadattr(vp, (struct vattr *)0);
1172 nfsm_strsiz(retlen, nmp->nm_rsize);
1173 nfsm_mtouio(uiop, retlen);
1174 m_freem(mrep);
1175 tsiz -= retlen;
1176 if (v3) {
1177 if (eof || retlen == 0)
1178 tsiz = 0;
1179 } else if (retlen < len)
1180 tsiz = 0;
1181 }
1182 nfsmout:
1183 return (error);
1184 }
1185
1186 /*
1187 * nfs write call
1188 */
1189 int
1190 nfs_writerpc(vp, uiop, iomode, must_commit)
1191 struct vnode *vp;
1192 struct uio *uiop;
1193 int *iomode, *must_commit;
1194 {
1195 u_int32_t *tl;
1196 caddr_t cp;
1197 int32_t t1, t2, backup;
1198 caddr_t bpos, dpos, cp2;
1199 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1200 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1201 int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR, rlen, commit;
1202 const int v3 = NFS_ISV3(vp);
1203 int committed = NFSV3WRITE_FILESYNC;
1204
1205 if (vp->v_mount->mnt_flag & MNT_RDONLY) {
1206 panic("writerpc readonly vp %p", vp);
1207 }
1208
1209 #ifndef DIAGNOSTIC
1210 if (uiop->uio_iovcnt != 1)
1211 panic("nfs: writerpc iovcnt > 1");
1212 #endif
1213 *must_commit = 0;
1214 tsiz = uiop->uio_resid;
1215 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1216 return (EFBIG);
1217 while (tsiz > 0) {
1218 nfsstats.rpccnt[NFSPROC_WRITE]++;
1219 len = min(tsiz, nmp->nm_wsize);
1220 nfsm_reqhead(vp, NFSPROC_WRITE,
1221 NFSX_FH(v3) + 5 * NFSX_UNSIGNED + nfsm_rndup(len));
1222 nfsm_fhtom(vp, v3);
1223 if (v3) {
1224 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1225 txdr_hyper(uiop->uio_offset, tl);
1226 tl += 2;
1227 *tl++ = txdr_unsigned(len);
1228 *tl++ = txdr_unsigned(*iomode);
1229 *tl = txdr_unsigned(len);
1230 } else {
1231 u_int32_t x;
1232
1233 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1234 /* Set both "begin" and "current" to non-garbage. */
1235 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1236 *tl++ = x; /* "begin offset" */
1237 *tl++ = x; /* "current offset" */
1238 x = txdr_unsigned(len);
1239 *tl++ = x; /* total to this offset */
1240 *tl = x; /* size of this write */
1241
1242 }
1243 nfsm_uiotom(uiop, len);
1244 nfsm_request(vp, NFSPROC_WRITE, uiop->uio_procp,
1245 VTONFS(vp)->n_wcred);
1246 if (v3) {
1247 wccflag = NFSV3_WCCCHK;
1248 nfsm_wcc_data(vp, wccflag);
1249 if (!error) {
1250 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED
1251 + NFSX_V3WRITEVERF);
1252 rlen = fxdr_unsigned(int, *tl++);
1253 if (rlen == 0) {
1254 error = NFSERR_IO;
1255 m_freem(mrep);
1256 break;
1257 } else if (rlen < len) {
1258 backup = len - rlen;
1259 (caddr_t)uiop->uio_iov->iov_base -=
1260 backup;
1261 uiop->uio_iov->iov_len += backup;
1262 uiop->uio_offset -= backup;
1263 uiop->uio_resid += backup;
1264 len = rlen;
1265 }
1266 commit = fxdr_unsigned(int, *tl++);
1267
1268 /*
1269 * Return the lowest committment level
1270 * obtained by any of the RPCs.
1271 */
1272 if (committed == NFSV3WRITE_FILESYNC)
1273 committed = commit;
1274 else if (committed == NFSV3WRITE_DATASYNC &&
1275 commit == NFSV3WRITE_UNSTABLE)
1276 committed = commit;
1277 if ((nmp->nm_iflag & NFSMNT_HASWRITEVERF) == 0){
1278 memcpy((caddr_t)nmp->nm_verf, (caddr_t)tl,
1279 NFSX_V3WRITEVERF);
1280 nmp->nm_iflag |= NFSMNT_HASWRITEVERF;
1281 } else if (memcmp((caddr_t)tl,
1282 (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF)) {
1283 *must_commit = 1;
1284 memcpy((caddr_t)nmp->nm_verf, (caddr_t)tl,
1285 NFSX_V3WRITEVERF);
1286 }
1287 }
1288 } else
1289 nfsm_loadattr(vp, (struct vattr *)0);
1290 if (wccflag)
1291 VTONFS(vp)->n_mtime = VTONFS(vp)->n_vattr->va_mtime.tv_sec;
1292 m_freem(mrep);
1293 if (error)
1294 break;
1295 tsiz -= len;
1296 }
1297 nfsmout:
1298 *iomode = committed;
1299 if (error)
1300 uiop->uio_resid = tsiz;
1301 return (error);
1302 }
1303
1304 /*
1305 * nfs mknod rpc
1306 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1307 * mode set to specify the file type and the size field for rdev.
1308 */
1309 int
1310 nfs_mknodrpc(dvp, vpp, cnp, vap)
1311 struct vnode *dvp;
1312 struct vnode **vpp;
1313 struct componentname *cnp;
1314 struct vattr *vap;
1315 {
1316 struct nfsv2_sattr *sp;
1317 u_int32_t *tl;
1318 caddr_t cp;
1319 int32_t t1, t2;
1320 struct vnode *newvp = (struct vnode *)0;
1321 struct nfsnode *np;
1322 char *cp2;
1323 caddr_t bpos, dpos;
1324 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0;
1325 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1326 u_int32_t rdev;
1327 const int v3 = NFS_ISV3(dvp);
1328
1329 if (vap->va_type == VCHR || vap->va_type == VBLK)
1330 rdev = txdr_unsigned(vap->va_rdev);
1331 else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1332 rdev = nfs_xdrneg1;
1333 else {
1334 VOP_ABORTOP(dvp, cnp);
1335 vput(dvp);
1336 return (EOPNOTSUPP);
1337 }
1338 nfsstats.rpccnt[NFSPROC_MKNOD]++;
1339 nfsm_reqhead(dvp, NFSPROC_MKNOD, NFSX_FH(v3) + 4 * NFSX_UNSIGNED +
1340 + nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1341 nfsm_fhtom(dvp, v3);
1342 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1343 if (v3) {
1344 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1345 *tl++ = vtonfsv3_type(vap->va_type);
1346 nfsm_v3attrbuild(vap, FALSE);
1347 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1348 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1349 *tl++ = txdr_unsigned(major(vap->va_rdev));
1350 *tl = txdr_unsigned(minor(vap->va_rdev));
1351 }
1352 } else {
1353 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1354 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1355 sp->sa_uid = nfs_xdrneg1;
1356 sp->sa_gid = nfs_xdrneg1;
1357 sp->sa_size = rdev;
1358 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1359 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1360 }
1361 nfsm_request(dvp, NFSPROC_MKNOD, cnp->cn_proc, cnp->cn_cred);
1362 if (!error) {
1363 nfsm_mtofh(dvp, newvp, v3, gotvp);
1364 if (!gotvp) {
1365 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1366 cnp->cn_namelen, cnp->cn_cred, cnp->cn_proc, &np);
1367 if (!error)
1368 newvp = NFSTOV(np);
1369 }
1370 }
1371 if (v3)
1372 nfsm_wcc_data(dvp, wccflag);
1373 nfsm_reqdone;
1374 if (error) {
1375 if (newvp)
1376 vput(newvp);
1377 } else {
1378 if (cnp->cn_flags & MAKEENTRY)
1379 cache_enter(dvp, newvp, cnp);
1380 *vpp = newvp;
1381 }
1382 PNBUF_PUT(cnp->cn_pnbuf);
1383 VTONFS(dvp)->n_flag |= NMODIFIED;
1384 if (!wccflag)
1385 VTONFS(dvp)->n_attrstamp = 0;
1386 vput(dvp);
1387 return (error);
1388 }
1389
1390 /*
1391 * nfs mknod vop
1392 * just call nfs_mknodrpc() to do the work.
1393 */
1394 /* ARGSUSED */
1395 int
1396 nfs_mknod(v)
1397 void *v;
1398 {
1399 struct vop_mknod_args /* {
1400 struct vnode *a_dvp;
1401 struct vnode **a_vpp;
1402 struct componentname *a_cnp;
1403 struct vattr *a_vap;
1404 } */ *ap = v;
1405 struct vnode *newvp;
1406 int error;
1407
1408 error = nfs_mknodrpc(ap->a_dvp, &newvp, ap->a_cnp, ap->a_vap);
1409 if (!error)
1410 vput(newvp);
1411 return (error);
1412 }
1413
1414 static u_long create_verf;
1415 /*
1416 * nfs file create call
1417 */
1418 int
1419 nfs_create(v)
1420 void *v;
1421 {
1422 struct vop_create_args /* {
1423 struct vnode *a_dvp;
1424 struct vnode **a_vpp;
1425 struct componentname *a_cnp;
1426 struct vattr *a_vap;
1427 } */ *ap = v;
1428 struct vnode *dvp = ap->a_dvp;
1429 struct vattr *vap = ap->a_vap;
1430 struct componentname *cnp = ap->a_cnp;
1431 struct nfsv2_sattr *sp;
1432 u_int32_t *tl;
1433 caddr_t cp;
1434 int32_t t1, t2;
1435 struct nfsnode *np = (struct nfsnode *)0;
1436 struct vnode *newvp = (struct vnode *)0;
1437 caddr_t bpos, dpos, cp2;
1438 int error, wccflag = NFSV3_WCCRATTR, gotvp = 0, fmode = 0;
1439 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1440 const int v3 = NFS_ISV3(dvp);
1441
1442 /*
1443 * Oops, not for me..
1444 */
1445 if (vap->va_type == VSOCK)
1446 return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
1447
1448 #ifdef VA_EXCLUSIVE
1449 if (vap->va_vaflags & VA_EXCLUSIVE)
1450 fmode |= O_EXCL;
1451 #endif
1452 again:
1453 error = 0;
1454 nfsstats.rpccnt[NFSPROC_CREATE]++;
1455 nfsm_reqhead(dvp, NFSPROC_CREATE, NFSX_FH(v3) + 2 * NFSX_UNSIGNED +
1456 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1457 nfsm_fhtom(dvp, v3);
1458 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1459 if (v3) {
1460 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1461 if (fmode & O_EXCL) {
1462 *tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE);
1463 nfsm_build(tl, u_int32_t *, NFSX_V3CREATEVERF);
1464 #ifdef INET
1465 if (in_ifaddr.tqh_first)
1466 *tl++ = in_ifaddr.tqh_first->ia_addr.sin_addr.s_addr;
1467 else
1468 *tl++ = create_verf;
1469 #else
1470 *tl++ = create_verf;
1471 #endif
1472 *tl = ++create_verf;
1473 } else {
1474 *tl = txdr_unsigned(NFSV3CREATE_UNCHECKED);
1475 nfsm_v3attrbuild(vap, FALSE);
1476 }
1477 } else {
1478 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1479 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1480 sp->sa_uid = nfs_xdrneg1;
1481 sp->sa_gid = nfs_xdrneg1;
1482 sp->sa_size = 0;
1483 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1484 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1485 }
1486 nfsm_request(dvp, NFSPROC_CREATE, cnp->cn_proc, cnp->cn_cred);
1487 if (!error) {
1488 nfsm_mtofh(dvp, newvp, v3, gotvp);
1489 if (!gotvp) {
1490 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1491 cnp->cn_namelen, cnp->cn_cred, cnp->cn_proc, &np);
1492 if (!error)
1493 newvp = NFSTOV(np);
1494 }
1495 }
1496 if (v3)
1497 nfsm_wcc_data(dvp, wccflag);
1498 nfsm_reqdone;
1499 if (error) {
1500 if (v3 && (fmode & O_EXCL) && error == NFSERR_NOTSUPP) {
1501 fmode &= ~O_EXCL;
1502 goto again;
1503 }
1504 if (newvp)
1505 vput(newvp);
1506 } else if (v3 && (fmode & O_EXCL))
1507 error = nfs_setattrrpc(newvp, vap, cnp->cn_cred, cnp->cn_proc);
1508 if (!error) {
1509 if (cnp->cn_flags & MAKEENTRY)
1510 cache_enter(dvp, newvp, cnp);
1511 *ap->a_vpp = newvp;
1512 }
1513 PNBUF_PUT(cnp->cn_pnbuf);
1514 VTONFS(dvp)->n_flag |= NMODIFIED;
1515 if (!wccflag)
1516 VTONFS(dvp)->n_attrstamp = 0;
1517 vput(dvp);
1518 return (error);
1519 }
1520
1521 /*
1522 * nfs file remove call
1523 * To try and make nfs semantics closer to ufs semantics, a file that has
1524 * other processes using the vnode is renamed instead of removed and then
1525 * removed later on the last close.
1526 * - If v_usecount > 1
1527 * If a rename is not already in the works
1528 * call nfs_sillyrename() to set it up
1529 * else
1530 * do the remove rpc
1531 */
1532 int
1533 nfs_remove(v)
1534 void *v;
1535 {
1536 struct vop_remove_args /* {
1537 struct vnodeop_desc *a_desc;
1538 struct vnode * a_dvp;
1539 struct vnode * a_vp;
1540 struct componentname * a_cnp;
1541 } */ *ap = v;
1542 struct vnode *vp = ap->a_vp;
1543 struct vnode *dvp = ap->a_dvp;
1544 struct componentname *cnp = ap->a_cnp;
1545 struct nfsnode *np = VTONFS(vp);
1546 int error = 0;
1547 struct vattr vattr;
1548
1549 #ifndef DIAGNOSTIC
1550 if ((cnp->cn_flags & HASBUF) == 0)
1551 panic("nfs_remove: no name");
1552 if (vp->v_usecount < 1)
1553 panic("nfs_remove: bad v_usecount");
1554 #endif
1555 if (vp->v_type == VDIR)
1556 error = EPERM;
1557 else if (vp->v_usecount == 1 || (np->n_sillyrename &&
1558 VOP_GETATTR(vp, &vattr, cnp->cn_cred, cnp->cn_proc) == 0 &&
1559 vattr.va_nlink > 1)) {
1560 /*
1561 * Purge the name cache so that the chance of a lookup for
1562 * the name succeeding while the remove is in progress is
1563 * minimized. Without node locking it can still happen, such
1564 * that an I/O op returns ESTALE, but since you get this if
1565 * another host removes the file..
1566 */
1567 cache_purge(vp);
1568 /*
1569 * throw away biocache buffers, mainly to avoid
1570 * unnecessary delayed writes later.
1571 */
1572 error = nfs_vinvalbuf(vp, 0, cnp->cn_cred, cnp->cn_proc, 1);
1573 /* Do the rpc */
1574 if (error != EINTR)
1575 error = nfs_removerpc(dvp, cnp->cn_nameptr,
1576 cnp->cn_namelen, cnp->cn_cred, cnp->cn_proc);
1577 /*
1578 * Kludge City: If the first reply to the remove rpc is lost..
1579 * the reply to the retransmitted request will be ENOENT
1580 * since the file was in fact removed
1581 * Therefore, we cheat and return success.
1582 */
1583 if (error == ENOENT)
1584 error = 0;
1585 } else if (!np->n_sillyrename)
1586 error = nfs_sillyrename(dvp, vp, cnp);
1587 PNBUF_PUT(cnp->cn_pnbuf);
1588 np->n_attrstamp = 0;
1589 vput(dvp);
1590 vput(vp);
1591 return (error);
1592 }
1593
1594 /*
1595 * nfs file remove rpc called from nfs_inactive
1596 */
1597 int
1598 nfs_removeit(sp)
1599 struct sillyrename *sp;
1600 {
1601
1602 return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen, sp->s_cred,
1603 (struct proc *)0));
1604 }
1605
1606 /*
1607 * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
1608 */
1609 int
1610 nfs_removerpc(dvp, name, namelen, cred, proc)
1611 struct vnode *dvp;
1612 const char *name;
1613 int namelen;
1614 struct ucred *cred;
1615 struct proc *proc;
1616 {
1617 u_int32_t *tl;
1618 caddr_t cp;
1619 int32_t t1, t2;
1620 caddr_t bpos, dpos, cp2;
1621 int error = 0, wccflag = NFSV3_WCCRATTR;
1622 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1623 const int v3 = NFS_ISV3(dvp);
1624
1625 nfsstats.rpccnt[NFSPROC_REMOVE]++;
1626 nfsm_reqhead(dvp, NFSPROC_REMOVE,
1627 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
1628 nfsm_fhtom(dvp, v3);
1629 nfsm_strtom(name, namelen, NFS_MAXNAMLEN);
1630 nfsm_request(dvp, NFSPROC_REMOVE, proc, cred);
1631 if (v3)
1632 nfsm_wcc_data(dvp, wccflag);
1633 nfsm_reqdone;
1634 VTONFS(dvp)->n_flag |= NMODIFIED;
1635 if (!wccflag)
1636 VTONFS(dvp)->n_attrstamp = 0;
1637 return (error);
1638 }
1639
1640 /*
1641 * nfs file rename call
1642 */
1643 int
1644 nfs_rename(v)
1645 void *v;
1646 {
1647 struct vop_rename_args /* {
1648 struct vnode *a_fdvp;
1649 struct vnode *a_fvp;
1650 struct componentname *a_fcnp;
1651 struct vnode *a_tdvp;
1652 struct vnode *a_tvp;
1653 struct componentname *a_tcnp;
1654 } */ *ap = v;
1655 struct vnode *fvp = ap->a_fvp;
1656 struct vnode *tvp = ap->a_tvp;
1657 struct vnode *fdvp = ap->a_fdvp;
1658 struct vnode *tdvp = ap->a_tdvp;
1659 struct componentname *tcnp = ap->a_tcnp;
1660 struct componentname *fcnp = ap->a_fcnp;
1661 int error;
1662
1663 #ifndef DIAGNOSTIC
1664 if ((tcnp->cn_flags & HASBUF) == 0 ||
1665 (fcnp->cn_flags & HASBUF) == 0)
1666 panic("nfs_rename: no name");
1667 #endif
1668 /* Check for cross-device rename */
1669 if ((fvp->v_mount != tdvp->v_mount) ||
1670 (tvp && (fvp->v_mount != tvp->v_mount))) {
1671 error = EXDEV;
1672 goto out;
1673 }
1674
1675 /*
1676 * If the tvp exists and is in use, sillyrename it before doing the
1677 * rename of the new file over it.
1678 */
1679 if (tvp && tvp->v_usecount > 1 && !VTONFS(tvp)->n_sillyrename &&
1680 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
1681 vput(tvp);
1682 tvp = NULL;
1683 }
1684
1685 error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1686 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1687 tcnp->cn_proc);
1688
1689 if (fvp->v_type == VDIR) {
1690 if (tvp != NULL && tvp->v_type == VDIR)
1691 cache_purge(tdvp);
1692 cache_purge(fdvp);
1693 }
1694 out:
1695 if (tdvp == tvp)
1696 vrele(tdvp);
1697 else
1698 vput(tdvp);
1699 if (tvp)
1700 vput(tvp);
1701 vrele(fdvp);
1702 vrele(fvp);
1703 /*
1704 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1705 */
1706 if (error == ENOENT)
1707 error = 0;
1708 return (error);
1709 }
1710
1711 /*
1712 * nfs file rename rpc called from nfs_remove() above
1713 */
1714 int
1715 nfs_renameit(sdvp, scnp, sp)
1716 struct vnode *sdvp;
1717 struct componentname *scnp;
1718 struct sillyrename *sp;
1719 {
1720 return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen,
1721 sdvp, sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_proc));
1722 }
1723
1724 /*
1725 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
1726 */
1727 int
1728 nfs_renamerpc(fdvp, fnameptr, fnamelen, tdvp, tnameptr, tnamelen, cred, proc)
1729 struct vnode *fdvp;
1730 const char *fnameptr;
1731 int fnamelen;
1732 struct vnode *tdvp;
1733 const char *tnameptr;
1734 int tnamelen;
1735 struct ucred *cred;
1736 struct proc *proc;
1737 {
1738 u_int32_t *tl;
1739 caddr_t cp;
1740 int32_t t1, t2;
1741 caddr_t bpos, dpos, cp2;
1742 int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR;
1743 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1744 const int v3 = NFS_ISV3(fdvp);
1745
1746 nfsstats.rpccnt[NFSPROC_RENAME]++;
1747 nfsm_reqhead(fdvp, NFSPROC_RENAME,
1748 (NFSX_FH(v3) + NFSX_UNSIGNED)*2 + nfsm_rndup(fnamelen) +
1749 nfsm_rndup(tnamelen));
1750 nfsm_fhtom(fdvp, v3);
1751 nfsm_strtom(fnameptr, fnamelen, NFS_MAXNAMLEN);
1752 nfsm_fhtom(tdvp, v3);
1753 nfsm_strtom(tnameptr, tnamelen, NFS_MAXNAMLEN);
1754 nfsm_request(fdvp, NFSPROC_RENAME, proc, cred);
1755 if (v3) {
1756 nfsm_wcc_data(fdvp, fwccflag);
1757 nfsm_wcc_data(tdvp, twccflag);
1758 }
1759 nfsm_reqdone;
1760 VTONFS(fdvp)->n_flag |= NMODIFIED;
1761 VTONFS(tdvp)->n_flag |= NMODIFIED;
1762 if (!fwccflag)
1763 VTONFS(fdvp)->n_attrstamp = 0;
1764 if (!twccflag)
1765 VTONFS(tdvp)->n_attrstamp = 0;
1766 return (error);
1767 }
1768
1769 /*
1770 * nfs hard link create call
1771 */
1772 int
1773 nfs_link(v)
1774 void *v;
1775 {
1776 struct vop_link_args /* {
1777 struct vnode *a_dvp;
1778 struct vnode *a_vp;
1779 struct componentname *a_cnp;
1780 } */ *ap = v;
1781 struct vnode *vp = ap->a_vp;
1782 struct vnode *dvp = ap->a_dvp;
1783 struct componentname *cnp = ap->a_cnp;
1784 u_int32_t *tl;
1785 caddr_t cp;
1786 int32_t t1, t2;
1787 caddr_t bpos, dpos, cp2;
1788 int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0;
1789 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1790 /* XXX Should be const and initialised? */
1791 int v3;
1792
1793 if (dvp->v_mount != vp->v_mount) {
1794 VOP_ABORTOP(vp, cnp);
1795 vput(dvp);
1796 return (EXDEV);
1797 }
1798 if (dvp != vp) {
1799 error = vn_lock(vp, LK_EXCLUSIVE);
1800 if (error != 0) {
1801 VOP_ABORTOP(dvp, cnp);
1802 vput(dvp);
1803 return error;
1804 }
1805 }
1806
1807 /*
1808 * Push all writes to the server, so that the attribute cache
1809 * doesn't get "out of sync" with the server.
1810 * XXX There should be a better way!
1811 */
1812 VOP_FSYNC(vp, cnp->cn_cred, FSYNC_WAIT, 0, 0, cnp->cn_proc);
1813
1814 v3 = NFS_ISV3(vp);
1815 nfsstats.rpccnt[NFSPROC_LINK]++;
1816 nfsm_reqhead(vp, NFSPROC_LINK,
1817 NFSX_FH(v3)*2 + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
1818 nfsm_fhtom(vp, v3);
1819 nfsm_fhtom(dvp, v3);
1820 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1821 nfsm_request(vp, NFSPROC_LINK, cnp->cn_proc, cnp->cn_cred);
1822 if (v3) {
1823 nfsm_postop_attr(vp, attrflag);
1824 nfsm_wcc_data(dvp, wccflag);
1825 }
1826 nfsm_reqdone;
1827 PNBUF_PUT(cnp->cn_pnbuf);
1828 VTONFS(dvp)->n_flag |= NMODIFIED;
1829 if (!attrflag)
1830 VTONFS(vp)->n_attrstamp = 0;
1831 if (!wccflag)
1832 VTONFS(dvp)->n_attrstamp = 0;
1833 if (dvp != vp)
1834 VOP_UNLOCK(vp, 0);
1835 vput(dvp);
1836 /*
1837 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
1838 */
1839 if (error == EEXIST)
1840 error = 0;
1841 return (error);
1842 }
1843
1844 /*
1845 * nfs symbolic link create call
1846 */
1847 int
1848 nfs_symlink(v)
1849 void *v;
1850 {
1851 struct vop_symlink_args /* {
1852 struct vnode *a_dvp;
1853 struct vnode **a_vpp;
1854 struct componentname *a_cnp;
1855 struct vattr *a_vap;
1856 char *a_target;
1857 } */ *ap = v;
1858 struct vnode *dvp = ap->a_dvp;
1859 struct vattr *vap = ap->a_vap;
1860 struct componentname *cnp = ap->a_cnp;
1861 struct nfsv2_sattr *sp;
1862 u_int32_t *tl;
1863 caddr_t cp;
1864 int32_t t1, t2;
1865 caddr_t bpos, dpos, cp2;
1866 int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp;
1867 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1868 struct vnode *newvp = (struct vnode *)0;
1869 const int v3 = NFS_ISV3(dvp);
1870
1871 nfsstats.rpccnt[NFSPROC_SYMLINK]++;
1872 slen = strlen(ap->a_target);
1873 nfsm_reqhead(dvp, NFSPROC_SYMLINK, NFSX_FH(v3) + 2*NFSX_UNSIGNED +
1874 nfsm_rndup(cnp->cn_namelen) + nfsm_rndup(slen) + NFSX_SATTR(v3));
1875 nfsm_fhtom(dvp, v3);
1876 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1877 if (v3)
1878 nfsm_v3attrbuild(vap, FALSE);
1879 nfsm_strtom(ap->a_target, slen, NFS_MAXPATHLEN);
1880 if (!v3) {
1881 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1882 sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode);
1883 sp->sa_uid = nfs_xdrneg1;
1884 sp->sa_gid = nfs_xdrneg1;
1885 sp->sa_size = nfs_xdrneg1;
1886 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1887 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1888 }
1889 nfsm_request(dvp, NFSPROC_SYMLINK, cnp->cn_proc, cnp->cn_cred);
1890 if (v3) {
1891 if (!error)
1892 nfsm_mtofh(dvp, newvp, v3, gotvp);
1893 nfsm_wcc_data(dvp, wccflag);
1894 }
1895 nfsm_reqdone;
1896 if (newvp)
1897 vput(newvp);
1898 PNBUF_PUT(cnp->cn_pnbuf);
1899 VTONFS(dvp)->n_flag |= NMODIFIED;
1900 if (!wccflag)
1901 VTONFS(dvp)->n_attrstamp = 0;
1902 vput(dvp);
1903 /*
1904 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
1905 */
1906 if (error == EEXIST)
1907 error = 0;
1908 return (error);
1909 }
1910
1911 /*
1912 * nfs make dir call
1913 */
1914 int
1915 nfs_mkdir(v)
1916 void *v;
1917 {
1918 struct vop_mkdir_args /* {
1919 struct vnode *a_dvp;
1920 struct vnode **a_vpp;
1921 struct componentname *a_cnp;
1922 struct vattr *a_vap;
1923 } */ *ap = v;
1924 struct vnode *dvp = ap->a_dvp;
1925 struct vattr *vap = ap->a_vap;
1926 struct componentname *cnp = ap->a_cnp;
1927 struct nfsv2_sattr *sp;
1928 u_int32_t *tl;
1929 caddr_t cp;
1930 int32_t t1, t2;
1931 int len;
1932 struct nfsnode *np = (struct nfsnode *)0;
1933 struct vnode *newvp = (struct vnode *)0;
1934 caddr_t bpos, dpos, cp2;
1935 int error = 0, wccflag = NFSV3_WCCRATTR;
1936 int gotvp = 0;
1937 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1938 const int v3 = NFS_ISV3(dvp);
1939
1940 len = cnp->cn_namelen;
1941 nfsstats.rpccnt[NFSPROC_MKDIR]++;
1942 nfsm_reqhead(dvp, NFSPROC_MKDIR,
1943 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len) + NFSX_SATTR(v3));
1944 nfsm_fhtom(dvp, v3);
1945 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
1946 if (v3) {
1947 nfsm_v3attrbuild(vap, FALSE);
1948 } else {
1949 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1950 sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode);
1951 sp->sa_uid = nfs_xdrneg1;
1952 sp->sa_gid = nfs_xdrneg1;
1953 sp->sa_size = nfs_xdrneg1;
1954 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1955 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1956 }
1957 nfsm_request(dvp, NFSPROC_MKDIR, cnp->cn_proc, cnp->cn_cred);
1958 if (!error)
1959 nfsm_mtofh(dvp, newvp, v3, gotvp);
1960 if (v3)
1961 nfsm_wcc_data(dvp, wccflag);
1962 nfsm_reqdone;
1963 VTONFS(dvp)->n_flag |= NMODIFIED;
1964 if (!wccflag)
1965 VTONFS(dvp)->n_attrstamp = 0;
1966 /*
1967 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
1968 * if we can succeed in looking up the directory.
1969 */
1970 if (error == EEXIST || (!error && !gotvp)) {
1971 if (newvp) {
1972 vput(newvp);
1973 newvp = (struct vnode *)0;
1974 }
1975 error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred,
1976 cnp->cn_proc, &np);
1977 if (!error) {
1978 newvp = NFSTOV(np);
1979 if (newvp->v_type != VDIR)
1980 error = EEXIST;
1981 }
1982 }
1983 if (error) {
1984 if (newvp)
1985 vput(newvp);
1986 } else {
1987 if (cnp->cn_flags & MAKEENTRY)
1988 cache_enter(dvp, newvp, cnp);
1989 *ap->a_vpp = newvp;
1990 }
1991 PNBUF_PUT(cnp->cn_pnbuf);
1992 vput(dvp);
1993 return (error);
1994 }
1995
1996 /*
1997 * nfs remove directory call
1998 */
1999 int
2000 nfs_rmdir(v)
2001 void *v;
2002 {
2003 struct vop_rmdir_args /* {
2004 struct vnode *a_dvp;
2005 struct vnode *a_vp;
2006 struct componentname *a_cnp;
2007 } */ *ap = v;
2008 struct vnode *vp = ap->a_vp;
2009 struct vnode *dvp = ap->a_dvp;
2010 struct componentname *cnp = ap->a_cnp;
2011 u_int32_t *tl;
2012 caddr_t cp;
2013 int32_t t1, t2;
2014 caddr_t bpos, dpos, cp2;
2015 int error = 0, wccflag = NFSV3_WCCRATTR;
2016 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2017 const int v3 = NFS_ISV3(dvp);
2018
2019 if (dvp == vp) {
2020 vrele(dvp);
2021 vput(dvp);
2022 PNBUF_PUT(cnp->cn_pnbuf);
2023 return (EINVAL);
2024 }
2025 nfsstats.rpccnt[NFSPROC_RMDIR]++;
2026 nfsm_reqhead(dvp, NFSPROC_RMDIR,
2027 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
2028 nfsm_fhtom(dvp, v3);
2029 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
2030 nfsm_request(dvp, NFSPROC_RMDIR, cnp->cn_proc, cnp->cn_cred);
2031 if (v3)
2032 nfsm_wcc_data(dvp, wccflag);
2033 nfsm_reqdone;
2034 PNBUF_PUT(cnp->cn_pnbuf);
2035 VTONFS(dvp)->n_flag |= NMODIFIED;
2036 if (!wccflag)
2037 VTONFS(dvp)->n_attrstamp = 0;
2038 cache_purge(dvp);
2039 cache_purge(vp);
2040 vput(vp);
2041 vput(dvp);
2042 /*
2043 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2044 */
2045 if (error == ENOENT)
2046 error = 0;
2047 return (error);
2048 }
2049
2050 /*
2051 * nfs readdir call
2052 */
2053 int
2054 nfs_readdir(v)
2055 void *v;
2056 {
2057 struct vop_readdir_args /* {
2058 struct vnode *a_vp;
2059 struct uio *a_uio;
2060 struct ucred *a_cred;
2061 int *a_eofflag;
2062 off_t **a_cookies;
2063 int *a_ncookies;
2064 } */ *ap = v;
2065 struct vnode *vp = ap->a_vp;
2066 struct uio *uio = ap->a_uio;
2067 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2068 char *base = uio->uio_iov->iov_base;
2069 int tresid, error;
2070 size_t count, lost;
2071 struct dirent *dp;
2072 off_t *cookies = NULL;
2073 int ncookies = 0, nc;
2074
2075 if (vp->v_type != VDIR)
2076 return (EPERM);
2077
2078 lost = uio->uio_resid & (NFS_DIRFRAGSIZ - 1);
2079 count = uio->uio_resid - lost;
2080 if (count <= 0)
2081 return (EINVAL);
2082
2083 /*
2084 * Call nfs_bioread() to do the real work.
2085 */
2086 tresid = uio->uio_resid = count;
2087 error = nfs_bioread(vp, uio, 0, ap->a_cred,
2088 ap->a_cookies ? NFSBIO_CACHECOOKIES : 0);
2089
2090 if (!error && ap->a_cookies) {
2091 ncookies = count / 16;
2092 cookies = malloc(sizeof (off_t) * ncookies, M_TEMP, M_WAITOK);
2093 *ap->a_cookies = cookies;
2094 }
2095
2096 if (!error && uio->uio_resid == tresid) {
2097 uio->uio_resid += lost;
2098 nfsstats.direofcache_misses++;
2099 if (ap->a_cookies)
2100 *ap->a_ncookies = 0;
2101 *ap->a_eofflag = 1;
2102 return (0);
2103 }
2104
2105 if (!error && ap->a_cookies) {
2106 /*
2107 * Only the NFS server and emulations use cookies, and they
2108 * load the directory block into system space, so we can
2109 * just look at it directly.
2110 */
2111 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
2112 panic("nfs_readdir: lost in space");
2113 for (nc = 0; ncookies-- &&
2114 base < (char *)uio->uio_iov->iov_base; nc++){
2115 dp = (struct dirent *) base;
2116 if (dp->d_reclen == 0)
2117 break;
2118 if (nmp->nm_flag & NFSMNT_XLATECOOKIE)
2119 *(cookies++) = (off_t)NFS_GETCOOKIE32(dp);
2120 else
2121 *(cookies++) = NFS_GETCOOKIE(dp);
2122 base += dp->d_reclen;
2123 }
2124 uio->uio_resid +=
2125 ((caddr_t)uio->uio_iov->iov_base - base);
2126 uio->uio_iov->iov_len +=
2127 ((caddr_t)uio->uio_iov->iov_base - base);
2128 uio->uio_iov->iov_base = base;
2129 *ap->a_ncookies = nc;
2130 }
2131
2132 uio->uio_resid += lost;
2133 *ap->a_eofflag = 0;
2134 return (error);
2135 }
2136
2137 /*
2138 * Readdir rpc call.
2139 * Called from below the buffer cache by nfs_doio().
2140 */
2141 int
2142 nfs_readdirrpc(vp, uiop, cred)
2143 struct vnode *vp;
2144 struct uio *uiop;
2145 struct ucred *cred;
2146 {
2147 int len, left;
2148 struct dirent *dp = NULL;
2149 u_int32_t *tl;
2150 caddr_t cp;
2151 int32_t t1, t2;
2152 caddr_t bpos, dpos, cp2;
2153 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2154 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2155 struct nfsnode *dnp = VTONFS(vp);
2156 u_quad_t fileno;
2157 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2158 int attrflag, nrpcs = 0, reclen;
2159 const int v3 = NFS_ISV3(vp);
2160 nfsquad_t cookie;
2161
2162 #ifdef DIAGNOSTIC
2163 /*
2164 * Should be called from buffer cache, so only amount of
2165 * NFS_DIRBLKSIZ will be requested.
2166 */
2167 if (uiop->uio_iovcnt != 1 || (uiop->uio_resid & (NFS_DIRBLKSIZ - 1)))
2168 panic("nfs readdirrpc bad uio");
2169 #endif
2170
2171 /*
2172 * Loop around doing readdir rpc's of size nm_readdirsize
2173 * truncated to a multiple of NFS_DIRFRAGSIZ.
2174 * The stopping criteria is EOF or buffer full.
2175 */
2176 while (more_dirs && bigenough) {
2177 /*
2178 * Heuristic: don't bother to do another RPC to further
2179 * fill up this block if there is not much room left. (< 50%
2180 * of the readdir RPC size). This wastes some buffer space
2181 * but can save up to 50% in RPC calls.
2182 */
2183 if (nrpcs > 0 && uiop->uio_resid < (nmp->nm_readdirsize / 2)) {
2184 bigenough = 0;
2185 break;
2186 }
2187 nfsstats.rpccnt[NFSPROC_READDIR]++;
2188 nfsm_reqhead(vp, NFSPROC_READDIR, NFSX_FH(v3) +
2189 NFSX_READDIR(v3));
2190 nfsm_fhtom(vp, v3);
2191 if (v3) {
2192 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2193 cookie.qval = uiop->uio_offset;
2194 if (nmp->nm_iflag & NFSMNT_SWAPCOOKIE) {
2195 txdr_swapcookie3(uiop->uio_offset, tl);
2196 } else {
2197 txdr_cookie3(uiop->uio_offset, tl);
2198 }
2199 tl += 2;
2200 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2201 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2202 } else {
2203 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2204 *tl++ = txdr_unsigned(uiop->uio_offset);
2205 }
2206 *tl = txdr_unsigned(nmp->nm_readdirsize);
2207 nfsm_request(vp, NFSPROC_READDIR, uiop->uio_procp, cred);
2208 nrpcs++;
2209 if (v3) {
2210 nfsm_postop_attr(vp, attrflag);
2211 if (!error) {
2212 nfsm_dissect(tl, u_int32_t *,
2213 2 * NFSX_UNSIGNED);
2214 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2215 dnp->n_cookieverf.nfsuquad[1] = *tl;
2216 } else {
2217 m_freem(mrep);
2218 goto nfsmout;
2219 }
2220 }
2221 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2222 more_dirs = fxdr_unsigned(int, *tl);
2223
2224 /* loop thru the dir entries, doctoring them to 4bsd form */
2225 while (more_dirs && bigenough) {
2226 if (v3) {
2227 nfsm_dissect(tl, u_int32_t *,
2228 3 * NFSX_UNSIGNED);
2229 fileno = fxdr_hyper(tl);
2230 len = fxdr_unsigned(int, *(tl + 2));
2231 } else {
2232 nfsm_dissect(tl, u_int32_t *,
2233 2 * NFSX_UNSIGNED);
2234 fileno = fxdr_unsigned(u_quad_t, *tl++);
2235 len = fxdr_unsigned(int, *tl);
2236 }
2237 if (len <= 0 || len > NFS_MAXNAMLEN) {
2238 error = EBADRPC;
2239 m_freem(mrep);
2240 goto nfsmout;
2241 }
2242 tlen = nfsm_rndup(len);
2243 if (tlen == len)
2244 tlen += 4; /* To ensure null termination */
2245 tlen += sizeof (off_t) + sizeof (int);
2246 reclen = ALIGN(tlen + DIRHDSIZ);
2247 tlen = reclen - DIRHDSIZ;
2248 left = NFS_DIRFRAGSIZ - blksiz;
2249 if (reclen > left) {
2250 dp->d_reclen += left;
2251 (caddr_t)uiop->uio_iov->iov_base += left;
2252 uiop->uio_iov->iov_len -= left;
2253 uiop->uio_resid -= left;
2254 blksiz = 0;
2255 NFS_STASHCOOKIE(dp, uiop->uio_offset);
2256 }
2257 if (reclen > uiop->uio_resid)
2258 bigenough = 0;
2259 if (bigenough) {
2260 dp = (struct dirent *)uiop->uio_iov->iov_base;
2261 dp->d_fileno = (int)fileno;
2262 dp->d_namlen = len;
2263 dp->d_reclen = reclen;
2264 dp->d_type = DT_UNKNOWN;
2265 blksiz += dp->d_reclen;
2266 if (blksiz == NFS_DIRFRAGSIZ)
2267 blksiz = 0;
2268 uiop->uio_resid -= DIRHDSIZ;
2269 (caddr_t)uiop->uio_iov->iov_base += DIRHDSIZ;
2270 uiop->uio_iov->iov_len -= DIRHDSIZ;
2271 nfsm_mtouio(uiop, len);
2272 cp = uiop->uio_iov->iov_base;
2273 tlen -= len;
2274 *cp = '\0'; /* null terminate */
2275 (caddr_t)uiop->uio_iov->iov_base += tlen;
2276 uiop->uio_iov->iov_len -= tlen;
2277 uiop->uio_resid -= tlen;
2278 } else
2279 nfsm_adv(nfsm_rndup(len));
2280 if (v3) {
2281 nfsm_dissect(tl, u_int32_t *,
2282 3 * NFSX_UNSIGNED);
2283 } else {
2284 nfsm_dissect(tl, u_int32_t *,
2285 2 * NFSX_UNSIGNED);
2286 }
2287 if (bigenough) {
2288 if (v3) {
2289 if (nmp->nm_iflag & NFSMNT_SWAPCOOKIE)
2290 uiop->uio_offset =
2291 fxdr_swapcookie3(tl);
2292 else
2293 uiop->uio_offset =
2294 fxdr_cookie3(tl);
2295 }
2296 else {
2297 uiop->uio_offset =
2298 fxdr_unsigned(off_t, *tl);
2299 }
2300 NFS_STASHCOOKIE(dp, uiop->uio_offset);
2301 }
2302 if (v3)
2303 tl += 2;
2304 else
2305 tl++;
2306 more_dirs = fxdr_unsigned(int, *tl);
2307 }
2308 /*
2309 * If at end of rpc data, get the eof boolean
2310 */
2311 if (!more_dirs) {
2312 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2313 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2314 }
2315 m_freem(mrep);
2316 }
2317 /*
2318 * Fill last record, iff any, out to a multiple of NFS_DIRFRAGSIZ
2319 * by increasing d_reclen for the last record.
2320 */
2321 if (blksiz > 0) {
2322 left = NFS_DIRFRAGSIZ - blksiz;
2323 dp->d_reclen += left;
2324 NFS_STASHCOOKIE(dp, uiop->uio_offset);
2325 (caddr_t)uiop->uio_iov->iov_base += left;
2326 uiop->uio_iov->iov_len -= left;
2327 uiop->uio_resid -= left;
2328 }
2329
2330 /*
2331 * We are now either at the end of the directory or have filled the
2332 * block.
2333 */
2334 if (bigenough)
2335 dnp->n_direofoffset = uiop->uio_offset;
2336 nfsmout:
2337 return (error);
2338 }
2339
2340 /*
2341 * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
2342 */
2343 int
2344 nfs_readdirplusrpc(vp, uiop, cred)
2345 struct vnode *vp;
2346 struct uio *uiop;
2347 struct ucred *cred;
2348 {
2349 int len, left;
2350 struct dirent *dp = NULL;
2351 u_int32_t *tl;
2352 caddr_t cp;
2353 int32_t t1, t2;
2354 struct vnode *newvp;
2355 caddr_t bpos, dpos, cp2;
2356 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2357 struct nameidata nami, *ndp = &nami;
2358 struct componentname *cnp = &ndp->ni_cnd;
2359 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2360 struct nfsnode *dnp = VTONFS(vp), *np;
2361 const unsigned char *hcp;
2362 nfsfh_t *fhp;
2363 u_quad_t fileno;
2364 int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
2365 int attrflag, fhsize, nrpcs = 0, reclen;
2366 struct nfs_fattr fattr, *fp;
2367
2368 #ifdef DIAGNOSTIC
2369 if (uiop->uio_iovcnt != 1 || (uiop->uio_resid & (NFS_DIRBLKSIZ - 1)))
2370 panic("nfs readdirplusrpc bad uio");
2371 #endif
2372 ndp->ni_dvp = vp;
2373 newvp = NULLVP;
2374
2375 /*
2376 * Loop around doing readdir rpc's of size nm_readdirsize
2377 * truncated to a multiple of NFS_DIRFRAGSIZ.
2378 * The stopping criteria is EOF or buffer full.
2379 */
2380 while (more_dirs && bigenough) {
2381 if (nrpcs > 0 && uiop->uio_resid < (nmp->nm_readdirsize / 2)) {
2382 bigenough = 0;
2383 break;
2384 }
2385 nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
2386 nfsm_reqhead(vp, NFSPROC_READDIRPLUS,
2387 NFSX_FH(1) + 6 * NFSX_UNSIGNED);
2388 nfsm_fhtom(vp, 1);
2389 nfsm_build(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
2390 if (nmp->nm_iflag & NFSMNT_SWAPCOOKIE) {
2391 txdr_swapcookie3(uiop->uio_offset, tl);
2392 } else {
2393 txdr_cookie3(uiop->uio_offset, tl);
2394 }
2395 tl += 2;
2396 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2397 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2398 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
2399 *tl = txdr_unsigned(nmp->nm_rsize);
2400 nfsm_request(vp, NFSPROC_READDIRPLUS, uiop->uio_procp, cred);
2401 nfsm_postop_attr(vp, attrflag);
2402 if (error) {
2403 m_freem(mrep);
2404 goto nfsmout;
2405 }
2406 nrpcs++;
2407 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2408 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2409 dnp->n_cookieverf.nfsuquad[1] = *tl++;
2410 more_dirs = fxdr_unsigned(int, *tl);
2411
2412 /* loop thru the dir entries, doctoring them to 4bsd form */
2413 while (more_dirs && bigenough) {
2414 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2415 fileno = fxdr_hyper(tl);
2416 len = fxdr_unsigned(int, *(tl + 2));
2417 if (len <= 0 || len > NFS_MAXNAMLEN) {
2418 error = EBADRPC;
2419 m_freem(mrep);
2420 goto nfsmout;
2421 }
2422 tlen = nfsm_rndup(len);
2423 if (tlen == len)
2424 tlen += 4; /* To ensure null termination*/
2425 tlen += sizeof (off_t) + sizeof (int);
2426 reclen = ALIGN(tlen + DIRHDSIZ);
2427 tlen = reclen - DIRHDSIZ;
2428 left = NFS_DIRFRAGSIZ - blksiz;
2429 if (reclen > left) {
2430 /*
2431 * DIRFRAGSIZ is aligned, no need to align
2432 * again here.
2433 */
2434 dp->d_reclen += left;
2435 (caddr_t)uiop->uio_iov->iov_base += left;
2436 uiop->uio_iov->iov_len -= left;
2437 uiop->uio_resid -= left;
2438 NFS_STASHCOOKIE(dp, uiop->uio_offset);
2439 blksiz = 0;
2440 }
2441 if (reclen > uiop->uio_resid)
2442 bigenough = 0;
2443 if (bigenough) {
2444 dp = (struct dirent *)uiop->uio_iov->iov_base;
2445 dp->d_fileno = (int)fileno;
2446 dp->d_namlen = len;
2447 dp->d_reclen = reclen;
2448 dp->d_type = DT_UNKNOWN;
2449 blksiz += dp->d_reclen;
2450 if (blksiz == NFS_DIRFRAGSIZ)
2451 blksiz = 0;
2452 uiop->uio_resid -= DIRHDSIZ;
2453 (caddr_t)uiop->uio_iov->iov_base += DIRHDSIZ;
2454 uiop->uio_iov->iov_len -= DIRHDSIZ;
2455 cnp->cn_nameptr = uiop->uio_iov->iov_base;
2456 cnp->cn_namelen = len;
2457 nfsm_mtouio(uiop, len);
2458 cp = uiop->uio_iov->iov_base;
2459 tlen -= len;
2460 *cp = '\0';
2461 (caddr_t)uiop->uio_iov->iov_base += tlen;
2462 uiop->uio_iov->iov_len -= tlen;
2463 uiop->uio_resid -= tlen;
2464 } else
2465 nfsm_adv(nfsm_rndup(len));
2466 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2467 if (bigenough) {
2468 if (nmp->nm_iflag & NFSMNT_SWAPCOOKIE)
2469 uiop->uio_offset =
2470 fxdr_swapcookie3(tl);
2471 else
2472 uiop->uio_offset =
2473 fxdr_cookie3(tl);
2474 NFS_STASHCOOKIE(dp, uiop->uio_offset);
2475 }
2476 tl += 2;
2477
2478 /*
2479 * Since the attributes are before the file handle
2480 * (sigh), we must skip over the attributes and then
2481 * come back and get them.
2482 */
2483 attrflag = fxdr_unsigned(int, *tl);
2484 if (attrflag) {
2485 nfsm_dissect(fp, struct nfs_fattr *, NFSX_V3FATTR);
2486 memcpy(&fattr, fp, NFSX_V3FATTR);
2487 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2488 doit = fxdr_unsigned(int, *tl);
2489 if (doit) {
2490 nfsm_getfh(fhp, fhsize, 1);
2491 if (NFS_CMPFH(dnp, fhp, fhsize)) {
2492 VREF(vp);
2493 newvp = vp;
2494 np = dnp;
2495 } else {
2496 error = nfs_nget(vp->v_mount, fhp,
2497 fhsize, &np);
2498 if (!error)
2499 newvp = NFSTOV(np);
2500 }
2501 if (!error) {
2502 nfs_loadattrcache(&newvp, &fattr, 0);
2503 dp->d_type =
2504 IFTODT(VTTOIF(np->n_vattr->va_type));
2505 ndp->ni_vp = newvp;
2506 cnp->cn_hash = 0;
2507 for (hcp = cnp->cn_nameptr, i = 1; i <= len;
2508 i++, hcp++)
2509 cnp->cn_hash += *hcp * i;
2510 if (cnp->cn_namelen <= NCHNAMLEN)
2511 cache_enter(ndp->ni_dvp, ndp->ni_vp,
2512 cnp);
2513 }
2514 }
2515 } else {
2516 /* Just skip over the file handle */
2517 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2518 i = fxdr_unsigned(int, *tl);
2519 nfsm_adv(nfsm_rndup(i));
2520 }
2521 if (newvp != NULLVP) {
2522 vrele(newvp);
2523 if (newvp != vp)
2524 VOP_UNLOCK(vp, 0);
2525 newvp = NULLVP;
2526 }
2527 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2528 more_dirs = fxdr_unsigned(int, *tl);
2529 }
2530 /*
2531 * If at end of rpc data, get the eof boolean
2532 */
2533 if (!more_dirs) {
2534 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2535 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2536 }
2537 m_freem(mrep);
2538 }
2539 /*
2540 * Fill last record, iff any, out to a multiple of NFS_DIRFRAGSIZ
2541 * by increasing d_reclen for the last record.
2542 */
2543 if (blksiz > 0) {
2544 left = NFS_DIRFRAGSIZ - blksiz;
2545 dp->d_reclen += left;
2546 NFS_STASHCOOKIE(dp, uiop->uio_offset);
2547 (caddr_t)uiop->uio_iov->iov_base += left;
2548 uiop->uio_iov->iov_len -= left;
2549 uiop->uio_resid -= left;
2550 }
2551
2552 /*
2553 * We are now either at the end of the directory or have filled the
2554 * block.
2555 */
2556 if (bigenough)
2557 dnp->n_direofoffset = uiop->uio_offset;
2558 nfsmout:
2559 if (newvp != NULLVP) {
2560 vrele(newvp);
2561 if (newvp != vp)
2562 VOP_UNLOCK(vp, 0);
2563 }
2564 return (error);
2565 }
2566 static char hextoasc[] = "0123456789abcdef";
2567
2568 /*
2569 * Silly rename. To make the NFS filesystem that is stateless look a little
2570 * more like the "ufs" a remove of an active vnode is translated to a rename
2571 * to a funny looking filename that is removed by nfs_inactive on the
2572 * nfsnode. There is the potential for another process on a different client
2573 * to create the same funny name between the nfs_lookitup() fails and the
2574 * nfs_rename() completes, but...
2575 */
2576 int
2577 nfs_sillyrename(dvp, vp, cnp)
2578 struct vnode *dvp, *vp;
2579 struct componentname *cnp;
2580 {
2581 struct sillyrename *sp;
2582 struct nfsnode *np;
2583 int error;
2584 short pid;
2585
2586 cache_purge(dvp);
2587 np = VTONFS(vp);
2588 #ifndef DIAGNOSTIC
2589 if (vp->v_type == VDIR)
2590 panic("nfs: sillyrename dir");
2591 #endif
2592 MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename),
2593 M_NFSREQ, M_WAITOK);
2594 sp->s_cred = crdup(cnp->cn_cred);
2595 sp->s_dvp = dvp;
2596 VREF(dvp);
2597
2598 /* Fudge together a funny name */
2599 pid = cnp->cn_proc->p_pid;
2600 memcpy(sp->s_name, ".nfsAxxxx4.4", 13);
2601 sp->s_namlen = 12;
2602 sp->s_name[8] = hextoasc[pid & 0xf];
2603 sp->s_name[7] = hextoasc[(pid >> 4) & 0xf];
2604 sp->s_name[6] = hextoasc[(pid >> 8) & 0xf];
2605 sp->s_name[5] = hextoasc[(pid >> 12) & 0xf];
2606
2607 /* Try lookitups until we get one that isn't there */
2608 while (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2609 cnp->cn_proc, (struct nfsnode **)0) == 0) {
2610 sp->s_name[4]++;
2611 if (sp->s_name[4] > 'z') {
2612 error = EINVAL;
2613 goto bad;
2614 }
2615 }
2616 error = nfs_renameit(dvp, cnp, sp);
2617 if (error)
2618 goto bad;
2619 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2620 cnp->cn_proc, &np);
2621 np->n_sillyrename = sp;
2622 return (0);
2623 bad:
2624 vrele(sp->s_dvp);
2625 crfree(sp->s_cred);
2626 free((caddr_t)sp, M_NFSREQ);
2627 return (error);
2628 }
2629
2630 /*
2631 * Look up a file name and optionally either update the file handle or
2632 * allocate an nfsnode, depending on the value of npp.
2633 * npp == NULL --> just do the lookup
2634 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2635 * handled too
2636 * *npp != NULL --> update the file handle in the vnode
2637 */
2638 int
2639 nfs_lookitup(dvp, name, len, cred, procp, npp)
2640 struct vnode *dvp;
2641 const char *name;
2642 int len;
2643 struct ucred *cred;
2644 struct proc *procp;
2645 struct nfsnode **npp;
2646 {
2647 u_int32_t *tl;
2648 caddr_t cp;
2649 int32_t t1, t2;
2650 struct vnode *newvp = (struct vnode *)0;
2651 struct nfsnode *np, *dnp = VTONFS(dvp);
2652 caddr_t bpos, dpos, cp2;
2653 int error = 0, fhlen, attrflag;
2654 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2655 nfsfh_t *nfhp;
2656 const int v3 = NFS_ISV3(dvp);
2657
2658 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
2659 nfsm_reqhead(dvp, NFSPROC_LOOKUP,
2660 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
2661 nfsm_fhtom(dvp, v3);
2662 nfsm_strtom(name, len, NFS_MAXNAMLEN);
2663 nfsm_request(dvp, NFSPROC_LOOKUP, procp, cred);
2664 if (npp && !error) {
2665 nfsm_getfh(nfhp, fhlen, v3);
2666 if (*npp) {
2667 np = *npp;
2668 if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
2669 free((caddr_t)np->n_fhp, M_NFSBIGFH);
2670 np->n_fhp = &np->n_fh;
2671 } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH)
2672 np->n_fhp =(nfsfh_t *)malloc(fhlen,M_NFSBIGFH,M_WAITOK);
2673 memcpy((caddr_t)np->n_fhp, (caddr_t)nfhp, fhlen);
2674 np->n_fhsize = fhlen;
2675 newvp = NFSTOV(np);
2676 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
2677 VREF(dvp);
2678 newvp = dvp;
2679 } else {
2680 error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np);
2681 if (error) {
2682 m_freem(mrep);
2683 return (error);
2684 }
2685 newvp = NFSTOV(np);
2686 }
2687 if (v3) {
2688 nfsm_postop_attr(newvp, attrflag);
2689 if (!attrflag && *npp == NULL) {
2690 m_freem(mrep);
2691 vput(newvp);
2692 return (ENOENT);
2693 }
2694 } else
2695 nfsm_loadattr(newvp, (struct vattr *)0);
2696 }
2697 nfsm_reqdone;
2698 if (npp && *npp == NULL) {
2699 if (error) {
2700 if (newvp)
2701 vput(newvp);
2702 } else
2703 *npp = np;
2704 }
2705 return (error);
2706 }
2707
2708 /*
2709 * Nfs Version 3 commit rpc
2710 */
2711 int
2712 nfs_commit(vp, offset, cnt, procp)
2713 struct vnode *vp;
2714 off_t offset;
2715 uint32_t cnt;
2716 struct proc *procp;
2717 {
2718 caddr_t cp;
2719 u_int32_t *tl;
2720 int32_t t1, t2;
2721 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2722 caddr_t bpos, dpos, cp2;
2723 int error = 0, wccflag = NFSV3_WCCRATTR;
2724 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2725
2726 #ifdef fvdl_debug
2727 printf("commit %lu - %lu\n", (unsigned long)offset,
2728 (unsigned long)(offset + cnt));
2729 #endif
2730
2731 if ((nmp->nm_iflag & NFSMNT_HASWRITEVERF) == 0)
2732 return (0);
2733 nfsstats.rpccnt[NFSPROC_COMMIT]++;
2734 nfsm_reqhead(vp, NFSPROC_COMMIT, NFSX_FH(1));
2735 nfsm_fhtom(vp, 1);
2736 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2737 txdr_hyper(offset, tl);
2738 tl += 2;
2739 *tl = txdr_unsigned(cnt);
2740 nfsm_request(vp, NFSPROC_COMMIT, procp, VTONFS(vp)->n_wcred);
2741 nfsm_wcc_data(vp, wccflag);
2742 if (!error) {
2743 nfsm_dissect(tl, u_int32_t *, NFSX_V3WRITEVERF);
2744 if (memcmp((caddr_t)nmp->nm_verf, (caddr_t)tl,
2745 NFSX_V3WRITEVERF)) {
2746 memcpy((caddr_t)nmp->nm_verf, (caddr_t)tl,
2747 NFSX_V3WRITEVERF);
2748 error = NFSERR_STALEWRITEVERF;
2749 }
2750 }
2751 nfsm_reqdone;
2752 return (error);
2753 }
2754
2755 /*
2756 * Kludge City..
2757 * - make nfs_bmap() essentially a no-op that does no translation
2758 * - do nfs_strategy() by doing I/O with nfs_readrpc/nfs_writerpc
2759 * (Maybe I could use the process's page mapping, but I was concerned that
2760 * Kernel Write might not be enabled and also figured copyout() would do
2761 * a lot more work than memcpy() and also it currently happens in the
2762 * context of the swapper process (2).
2763 */
2764 int
2765 nfs_bmap(v)
2766 void *v;
2767 {
2768 struct vop_bmap_args /* {
2769 struct vnode *a_vp;
2770 daddr_t a_bn;
2771 struct vnode **a_vpp;
2772 daddr_t *a_bnp;
2773 int *a_runp;
2774 } */ *ap = v;
2775 struct vnode *vp = ap->a_vp;
2776
2777 /*
2778 * XXX vpp should be returned unlocked?
2779 */
2780 if (ap->a_vpp != NULL)
2781 *ap->a_vpp = vp;
2782 if (ap->a_bnp != NULL)
2783 *ap->a_bnp = ap->a_bn * btodb(vp->v_mount->mnt_stat.f_iosize);
2784 return (0);
2785 }
2786
2787 /*
2788 * Strategy routine.
2789 * For async requests when nfsiod(s) are running, queue the request by
2790 * calling nfs_asyncio(), otherwise just all nfs_doio() to do the
2791 * request.
2792 */
2793 int
2794 nfs_strategy(v)
2795 void *v;
2796 {
2797 struct vop_strategy_args *ap = v;
2798 struct buf *bp = ap->a_bp;
2799 struct proc *p;
2800 int error = 0;
2801
2802 if ((bp->b_flags & (B_PHYS|B_ASYNC)) == (B_PHYS|B_ASYNC))
2803 panic("nfs physio/async");
2804 if (bp->b_flags & B_ASYNC)
2805 p = NULL;
2806 else
2807 p = curproc; /* XXX */
2808
2809 /*
2810 * If the op is asynchronous and an i/o daemon is waiting
2811 * queue the request, wake it up and wait for completion
2812 * otherwise just do it ourselves.
2813 */
2814
2815 if ((bp->b_flags & B_ASYNC) == 0 ||
2816 nfs_asyncio(bp))
2817 error = nfs_doio(bp, p);
2818 return (error);
2819 }
2820
2821 /*
2822 * Mmap a file
2823 *
2824 * NB Currently unsupported.
2825 */
2826 /* ARGSUSED */
2827 int
2828 nfs_mmap(v)
2829 void *v;
2830 {
2831 #if 0
2832 struct vop_mmap_args /* {
2833 struct vnode *a_vp;
2834 int a_fflags;
2835 struct ucred *a_cred;
2836 struct proc *a_p;
2837 } */ *ap = v;
2838 #endif
2839
2840 return (EINVAL);
2841 }
2842
2843 /*
2844 * fsync vnode op. Just call nfs_flush() with commit == 1.
2845 */
2846 /* ARGSUSED */
2847 int
2848 nfs_fsync(v)
2849 void *v;
2850 {
2851 struct vop_fsync_args /* {
2852 struct vnodeop_desc *a_desc;
2853 struct vnode * a_vp;
2854 struct ucred * a_cred;
2855 int a_flags;
2856 off_t offlo;
2857 off_t offhi;
2858 struct proc * a_p;
2859 } */ *ap = v;
2860
2861 return (nfs_flush(ap->a_vp, ap->a_cred,
2862 (ap->a_flags & FSYNC_WAIT) != 0 ? MNT_WAIT : 0, ap->a_p, 1));
2863 }
2864
2865 /*
2866 * Flush all the data associated with a vnode.
2867 */
2868 int
2869 nfs_flush(vp, cred, waitfor, p, commit)
2870 struct vnode *vp;
2871 struct ucred *cred;
2872 int waitfor;
2873 struct proc *p;
2874 int commit;
2875 {
2876 struct uvm_object *uobj = &vp->v_uvm.u_obj;
2877 struct nfsnode *np = VTONFS(vp);
2878 int error;
2879 int flushflags = PGO_ALLPAGES|PGO_CLEANIT|PGO_SYNCIO;
2880 int rv;
2881 UVMHIST_FUNC("nfs_flush"); UVMHIST_CALLED(ubchist);
2882
2883 error = 0;
2884 simple_lock(&uobj->vmobjlock);
2885 rv = (uobj->pgops->pgo_flush)(uobj, 0, 0, flushflags);
2886 simple_unlock(&uobj->vmobjlock);
2887 if (!rv) {
2888 error = EIO;
2889 }
2890 if (np->n_flag & NWRITEERR) {
2891 error = np->n_error;
2892 np->n_flag &= ~NWRITEERR;
2893 }
2894 UVMHIST_LOG(ubchist, "returning %d", error,0,0,0);
2895 return (error);
2896 }
2897
2898 /*
2899 * Return POSIX pathconf information applicable to nfs.
2900 *
2901 * N.B. The NFS V2 protocol doesn't support this RPC.
2902 */
2903 /* ARGSUSED */
2904 int
2905 nfs_pathconf(v)
2906 void *v;
2907 {
2908 struct vop_pathconf_args /* {
2909 struct vnode *a_vp;
2910 int a_name;
2911 register_t *a_retval;
2912 } */ *ap = v;
2913 struct nfsv3_pathconf *pcp;
2914 struct vnode *vp = ap->a_vp;
2915 struct nfsmount *nmp;
2916 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2917 int32_t t1, t2;
2918 u_int32_t *tl;
2919 caddr_t bpos, dpos, cp, cp2;
2920 int error = 0, attrflag;
2921 unsigned int l;
2922 u_int64_t maxsize;
2923 const int v3 = NFS_ISV3(vp);
2924
2925 switch (ap->a_name) {
2926 /* Names that can be resolved locally. */
2927 case _PC_PIPE_BUF:
2928 *ap->a_retval = PIPE_BUF;
2929 break;
2930 case _PC_SYNC_IO:
2931 *ap->a_retval = 1;
2932 break;
2933 /* Names that cannot be resolved locally; do an RPC, if possible. */
2934 case _PC_LINK_MAX:
2935 case _PC_NAME_MAX:
2936 case _PC_CHOWN_RESTRICTED:
2937 case _PC_NO_TRUNC:
2938 if (!v3) {
2939 error = EINVAL;
2940 break;
2941 }
2942 nfsstats.rpccnt[NFSPROC_PATHCONF]++;
2943 nfsm_reqhead(vp, NFSPROC_PATHCONF, NFSX_FH(1));
2944 nfsm_fhtom(vp, 1);
2945 nfsm_request(vp, NFSPROC_PATHCONF,
2946 curproc, curproc->p_ucred); /* XXX */
2947 nfsm_postop_attr(vp, attrflag);
2948 if (!error) {
2949 nfsm_dissect(pcp, struct nfsv3_pathconf *,
2950 NFSX_V3PATHCONF);
2951 switch (ap->a_name) {
2952 case _PC_LINK_MAX:
2953 *ap->a_retval =
2954 fxdr_unsigned(register_t, pcp->pc_linkmax);
2955 break;
2956 case _PC_NAME_MAX:
2957 *ap->a_retval =
2958 fxdr_unsigned(register_t, pcp->pc_namemax);
2959 break;
2960 case _PC_CHOWN_RESTRICTED:
2961 *ap->a_retval =
2962 (pcp->pc_chownrestricted == nfs_true);
2963 break;
2964 case _PC_NO_TRUNC:
2965 *ap->a_retval =
2966 (pcp->pc_notrunc == nfs_true);
2967 break;
2968 }
2969 }
2970 nfsm_reqdone;
2971 break;
2972 case _PC_FILESIZEBITS:
2973 if (v3) {
2974 nmp = VFSTONFS(vp->v_mount);
2975 if ((nmp->nm_iflag & NFSMNT_GOTFSINFO) == 0)
2976 if ((error = nfs_fsinfo(nmp, vp,
2977 curproc->p_ucred, curproc)) != 0) /* XXX */
2978 break;
2979 for (l = 0, maxsize = nmp->nm_maxfilesize;
2980 (maxsize >> l) > 0; l++)
2981 ;
2982 *ap->a_retval = l + 1;
2983 } else {
2984 *ap->a_retval = 32; /* NFS V2 limitation */
2985 }
2986 break;
2987 default:
2988 error = EINVAL;
2989 break;
2990 }
2991
2992 return (error);
2993 }
2994
2995 /*
2996 * NFS advisory byte-level locks.
2997 */
2998 int
2999 nfs_advlock(v)
3000 void *v;
3001 {
3002 struct vop_advlock_args /* {
3003 struct vnode *a_vp;
3004 caddr_t a_id;
3005 int a_op;
3006 struct flock *a_fl;
3007 int a_flags;
3008 } */ *ap = v;
3009 struct nfsnode *np = VTONFS(ap->a_vp);
3010
3011 return lf_advlock(ap, &np->n_lockf, np->n_size);
3012 }
3013
3014 /*
3015 * Print out the contents of an nfsnode.
3016 */
3017 int
3018 nfs_print(v)
3019 void *v;
3020 {
3021 struct vop_print_args /* {
3022 struct vnode *a_vp;
3023 } */ *ap = v;
3024 struct vnode *vp = ap->a_vp;
3025 struct nfsnode *np = VTONFS(vp);
3026
3027 printf("tag VT_NFS, fileid %ld fsid 0x%lx",
3028 np->n_vattr->va_fileid, np->n_vattr->va_fsid);
3029 if (vp->v_type == VFIFO)
3030 fifo_printinfo(vp);
3031 printf("\n");
3032 return (0);
3033 }
3034
3035 /*
3036 * NFS file truncation.
3037 */
3038 int
3039 nfs_truncate(v)
3040 void *v;
3041 {
3042 #if 0
3043 struct vop_truncate_args /* {
3044 struct vnode *a_vp;
3045 off_t a_length;
3046 int a_flags;
3047 struct ucred *a_cred;
3048 struct proc *a_p;
3049 } */ *ap = v;
3050 #endif
3051
3052 /* Use nfs_setattr */
3053 return (EOPNOTSUPP);
3054 }
3055
3056 /*
3057 * NFS update.
3058 */
3059 int
3060 nfs_update(v)
3061 void *v;
3062 #if 0
3063 struct vop_update_args /* {
3064 struct vnode *a_vp;
3065 struct timespec *a_ta;
3066 struct timespec *a_tm;
3067 int a_waitfor;
3068 } */ *ap = v;
3069 #endif
3070 {
3071
3072 /* Use nfs_setattr */
3073 return (EOPNOTSUPP);
3074 }
3075
3076 /*
3077 * Just call bwrite().
3078 */
3079 int
3080 nfs_bwrite(v)
3081 void *v;
3082 {
3083 struct vop_bwrite_args /* {
3084 struct vnode *a_bp;
3085 } */ *ap = v;
3086
3087 return (bwrite(ap->a_bp));
3088 }
3089
3090 /*
3091 * nfs special file access vnode op.
3092 * Essentially just get vattr and then imitate iaccess() since the device is
3093 * local to the client.
3094 */
3095 int
3096 nfsspec_access(v)
3097 void *v;
3098 {
3099 struct vop_access_args /* {
3100 struct vnode *a_vp;
3101 int a_mode;
3102 struct ucred *a_cred;
3103 struct proc *a_p;
3104 } */ *ap = v;
3105 struct vattr va;
3106 struct vnode *vp = ap->a_vp;
3107 int error;
3108
3109 error = VOP_GETATTR(vp, &va, ap->a_cred, ap->a_p);
3110 if (error)
3111 return (error);
3112
3113 /*
3114 * Disallow write attempts on filesystems mounted read-only;
3115 * unless the file is a socket, fifo, or a block or character
3116 * device resident on the filesystem.
3117 */
3118 if ((ap->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3119 switch (vp->v_type) {
3120 case VREG:
3121 case VDIR:
3122 case VLNK:
3123 return (EROFS);
3124 default:
3125 break;
3126 }
3127 }
3128
3129 return (vaccess(va.va_type, va.va_mode,
3130 va.va_uid, va.va_gid, ap->a_mode, ap->a_cred));
3131 }
3132
3133 /*
3134 * Read wrapper for special devices.
3135 */
3136 int
3137 nfsspec_read(v)
3138 void *v;
3139 {
3140 struct vop_read_args /* {
3141 struct vnode *a_vp;
3142 struct uio *a_uio;
3143 int a_ioflag;
3144 struct ucred *a_cred;
3145 } */ *ap = v;
3146 struct nfsnode *np = VTONFS(ap->a_vp);
3147
3148 /*
3149 * Set access flag.
3150 */
3151 np->n_flag |= NACC;
3152 np->n_atim.tv_sec = time.tv_sec;
3153 np->n_atim.tv_nsec = time.tv_usec * 1000;
3154 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_read), ap));
3155 }
3156
3157 /*
3158 * Write wrapper for special devices.
3159 */
3160 int
3161 nfsspec_write(v)
3162 void *v;
3163 {
3164 struct vop_write_args /* {
3165 struct vnode *a_vp;
3166 struct uio *a_uio;
3167 int a_ioflag;
3168 struct ucred *a_cred;
3169 } */ *ap = v;
3170 struct nfsnode *np = VTONFS(ap->a_vp);
3171
3172 /*
3173 * Set update flag.
3174 */
3175 np->n_flag |= NUPD;
3176 np->n_mtim.tv_sec = time.tv_sec;
3177 np->n_mtim.tv_nsec = time.tv_usec * 1000;
3178 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_write), ap));
3179 }
3180
3181 /*
3182 * Close wrapper for special devices.
3183 *
3184 * Update the times on the nfsnode then do device close.
3185 */
3186 int
3187 nfsspec_close(v)
3188 void *v;
3189 {
3190 struct vop_close_args /* {
3191 struct vnode *a_vp;
3192 int a_fflag;
3193 struct ucred *a_cred;
3194 struct proc *a_p;
3195 } */ *ap = v;
3196 struct vnode *vp = ap->a_vp;
3197 struct nfsnode *np = VTONFS(vp);
3198 struct vattr vattr;
3199
3200 if (np->n_flag & (NACC | NUPD)) {
3201 np->n_flag |= NCHG;
3202 if (vp->v_usecount == 1 &&
3203 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3204 VATTR_NULL(&vattr);
3205 if (np->n_flag & NACC)
3206 vattr.va_atime = np->n_atim;
3207 if (np->n_flag & NUPD)
3208 vattr.va_mtime = np->n_mtim;
3209 (void)VOP_SETATTR(vp, &vattr, ap->a_cred, ap->a_p);
3210 }
3211 }
3212 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_close), ap));
3213 }
3214
3215 /*
3216 * Read wrapper for fifos.
3217 */
3218 int
3219 nfsfifo_read(v)
3220 void *v;
3221 {
3222 struct vop_read_args /* {
3223 struct vnode *a_vp;
3224 struct uio *a_uio;
3225 int a_ioflag;
3226 struct ucred *a_cred;
3227 } */ *ap = v;
3228 struct nfsnode *np = VTONFS(ap->a_vp);
3229
3230 /*
3231 * Set access flag.
3232 */
3233 np->n_flag |= NACC;
3234 np->n_atim.tv_sec = time.tv_sec;
3235 np->n_atim.tv_nsec = time.tv_usec * 1000;
3236 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_read), ap));
3237 }
3238
3239 /*
3240 * Write wrapper for fifos.
3241 */
3242 int
3243 nfsfifo_write(v)
3244 void *v;
3245 {
3246 struct vop_write_args /* {
3247 struct vnode *a_vp;
3248 struct uio *a_uio;
3249 int a_ioflag;
3250 struct ucred *a_cred;
3251 } */ *ap = v;
3252 struct nfsnode *np = VTONFS(ap->a_vp);
3253
3254 /*
3255 * Set update flag.
3256 */
3257 np->n_flag |= NUPD;
3258 np->n_mtim.tv_sec = time.tv_sec;
3259 np->n_mtim.tv_nsec = time.tv_usec * 1000;
3260 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_write), ap));
3261 }
3262
3263 /*
3264 * Close wrapper for fifos.
3265 *
3266 * Update the times on the nfsnode then do fifo close.
3267 */
3268 int
3269 nfsfifo_close(v)
3270 void *v;
3271 {
3272 struct vop_close_args /* {
3273 struct vnode *a_vp;
3274 int a_fflag;
3275 struct ucred *a_cred;
3276 struct proc *a_p;
3277 } */ *ap = v;
3278 struct vnode *vp = ap->a_vp;
3279 struct nfsnode *np = VTONFS(vp);
3280 struct vattr vattr;
3281
3282 if (np->n_flag & (NACC | NUPD)) {
3283 if (np->n_flag & NACC) {
3284 np->n_atim.tv_sec = time.tv_sec;
3285 np->n_atim.tv_nsec = time.tv_usec * 1000;
3286 }
3287 if (np->n_flag & NUPD) {
3288 np->n_mtim.tv_sec = time.tv_sec;
3289 np->n_mtim.tv_nsec = time.tv_usec * 1000;
3290 }
3291 np->n_flag |= NCHG;
3292 if (vp->v_usecount == 1 &&
3293 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3294 VATTR_NULL(&vattr);
3295 if (np->n_flag & NACC)
3296 vattr.va_atime = np->n_atim;
3297 if (np->n_flag & NUPD)
3298 vattr.va_mtime = np->n_mtim;
3299 (void)VOP_SETATTR(vp, &vattr, ap->a_cred, ap->a_p);
3300 }
3301 }
3302 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_close), ap));
3303 }
3304