1 /* $FreeBSD$ */
2 /* $Id: nfs_vnops.c,v 1.45 2003/11/05 14:59:02 rees Exp $ */
3
4 /*-
5 * copyright (c) 2003
6 * the regents of the university of michigan
7 * all rights reserved
8 *
9 * permission is granted to use, copy, create derivative works and redistribute
10 * this software and such derivative works for any purpose, so long as the name
11 * of the university of michigan is not used in any advertising or publicity
12 * pertaining to the use or distribution of this software without specific,
13 * written prior authorization. if the above copyright notice or any other
14 * identification of the university of michigan is included in any copy of any
15 * portion of this software, then the disclaimer below must also be included.
16 *
17 * this software is provided as is, without representation from the university
18 * of michigan as to its fitness for any purpose, and without warranty by the
19 * university of michigan of any kind, either express or implied, including
20 * without limitation the implied warranties of merchantability and fitness for
21 * a particular purpose. the regents of the university of michigan shall not be
22 * liable for any damages, including special, indirect, incidental, or
23 * consequential damages, with respect to any claim arising out of or in
24 * connection with the use of the software, even if it has been or is hereafter
25 * advised of the possibility of such damages.
26 */
27
28 /*
29 * Copyright (c) 1989, 1993
30 * The Regents of the University of California. All rights reserved.
31 *
32 * This code is derived from software contributed to Berkeley by
33 * Rick Macklem at The University of Guelph.
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 4. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
46 *
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * SUCH DAMAGE.
58 *
59 * @(#)nfs_vnops.c 8.16 (Berkeley) 5/27/95
60 */
61
62 #include <sys/cdefs.h>
63 __FBSDID("$FreeBSD$");
64
65 /*
66 * vnode op calls for Sun NFS version 2 and 3
67 */
68
69 #include "opt_inet.h"
70
71 #include <sys/param.h>
72 #include <sys/kernel.h>
73 #include <sys/systm.h>
74 #include <sys/resourcevar.h>
75 #include <sys/proc.h>
76 #include <sys/mount.h>
77 #include <sys/bio.h>
78 #include <sys/buf.h>
79 #include <sys/malloc.h>
80 #include <sys/mbuf.h>
81 #include <sys/namei.h>
82 #include <sys/socket.h>
83 #include <sys/vnode.h>
84 #include <sys/dirent.h>
85 #include <sys/fcntl.h>
86 #include <sys/lockf.h>
87 #include <sys/stat.h>
88 #include <sys/sysctl.h>
89 #include <sys/lockmgr.h>
90 #include <sys/signalvar.h>
91
92 #include <vm/vm.h>
93 #include <vm/vm_extern.h>
94
95 #include <fs/fifofs/fifo.h>
96
97 #include <rpc/rpcclnt.h>
98
99 #include <nfs/rpcv2.h>
100 #include <nfs/nfsproto.h>
101 #include <nfsclient/nfs.h>
102 #include <nfs4client/nfs4.h>
103 #include <nfsclient/nfsnode.h>
104 #include <nfsclient/nfsmount.h>
105 #include <nfsclient/nfs_lock.h>
106 #include <nfs/xdr_subs.h>
107 #include <nfsclient/nfsm_subs.h>
108
109 #include <net/if.h>
110 #include <netinet/in.h>
111 #include <netinet/in_var.h>
112
113 /* NFSv4 */
114 #include <nfs4client/nfs4m_subs.h>
115 #include <nfs4client/nfs4_vn.h>
116
117 /* Defs */
118 #define TRUE 1
119 #define FALSE 0
120
121 /*
122 * Ifdef for FreeBSD-current merged buffer cache. It is unfortunate that these
123 * calls are not in getblk() and brelse() so that they would not be necessary
124 * here.
125 */
126 #ifndef B_VMIO
127 #define vfs_busy_pages(bp, f)
128 #endif
129
130 static int nfs4_flush(struct vnode *, int, struct thread *,
131 int);
132 static int nfs4_setattrrpc(struct vnode *, struct vattr *, struct ucred *,
133 struct thread *);
134 static int nfs4_closerpc(struct vnode *, struct ucred *, struct thread *, int);
135
136 static vop_lookup_t nfs4_lookup;
137 static vop_create_t nfs4_create;
138 static vop_mknod_t nfs4_mknod;
139 static vop_open_t nfs4_open;
140 static vop_close_t nfs4_close;
141 static vop_access_t nfs4_access;
142 static vop_getattr_t nfs4_getattr;
143 static vop_setattr_t nfs4_setattr;
144 static vop_read_t nfs4_read;
145 static vop_fsync_t nfs4_fsync;
146 static vop_remove_t nfs4_remove;
147 static vop_link_t nfs4_link;
148 static vop_rename_t nfs4_rename;
149 static vop_mkdir_t nfs4_mkdir;
150 static vop_rmdir_t nfs4_rmdir;
151 static vop_symlink_t nfs4_symlink;
152 static vop_readdir_t nfs4_readdir;
153 static vop_strategy_t nfs4_strategy;
154 static int nfs4_lookitup(struct vnode *, const char *, int,
155 struct ucred *, struct thread *, struct nfsnode **);
156 static int nfs4_sillyrename(struct vnode *, struct vnode *,
157 struct componentname *);
158 static vop_readlink_t nfs4_readlink;
159 static vop_print_t nfs4_print;
160 static vop_advlock_t nfs4_advlock;
161 static vop_advlockasync_t nfs4_advlockasync;
162
163 /*
164 * Global vfs data structures for nfs
165 */
166 struct vop_vector nfs4_vnodeops = {
167 .vop_default = &default_vnodeops,
168 .vop_access = nfs4_access,
169 .vop_advlock = nfs4_advlock,
170 .vop_advlockasync = nfs4_advlockasync,
171 .vop_close = nfs4_close,
172 .vop_create = nfs4_create,
173 .vop_fsync = nfs4_fsync,
174 .vop_getattr = nfs4_getattr,
175 .vop_getpages = nfs_getpages,
176 .vop_putpages = nfs_putpages,
177 .vop_inactive = nfs_inactive,
178 .vop_lease = VOP_NULL,
179 .vop_link = nfs4_link,
180 .vop_lookup = nfs4_lookup,
181 .vop_mkdir = nfs4_mkdir,
182 .vop_mknod = nfs4_mknod,
183 .vop_open = nfs4_open,
184 .vop_print = nfs4_print,
185 .vop_read = nfs4_read,
186 .vop_readdir = nfs4_readdir,
187 .vop_readlink = nfs4_readlink,
188 .vop_reclaim = nfs_reclaim,
189 .vop_remove = nfs4_remove,
190 .vop_rename = nfs4_rename,
191 .vop_rmdir = nfs4_rmdir,
192 .vop_setattr = nfs4_setattr,
193 .vop_strategy = nfs4_strategy,
194 .vop_symlink = nfs4_symlink,
195 .vop_write = nfs_write,
196 };
197
198 static int nfs4_removerpc(struct vnode *dvp, const char *name, int namelen,
199 struct ucred *cred, struct thread *td);
200 static int nfs4_renamerpc(struct vnode *fdvp, const char *fnameptr,
201 int fnamelen, struct vnode *tdvp,
202 const char *tnameptr, int tnamelen,
203 struct ucred *cred, struct thread *td);
204 static int nfs4_renameit(struct vnode *sdvp, struct componentname *scnp,
205 struct sillyrename *sp);
206 static int nfs4_openrpc(struct vnode *, struct vnode **,
207 struct componentname *, int, struct vattr *);
208 static int nfs4_open_confirm(struct vnode *vp, struct nfs4_compound *cpp,
209 struct nfs4_oparg_open *openap,
210 struct nfs4_oparg_getfh *gfh,
211 struct ucred *cred, struct thread *td);
212 static int nfs4_createrpc(struct vnode *, struct vnode **,
213 struct componentname *, nfstype,
214 struct vattr *, char *);
215
216 /*
217 * Global variables
218 */
219 struct nfs4_lowner nfs4_masterlowner;
220
221 #define DIRHDSIZ (sizeof (struct dirent) - (MAXNAMLEN + 1))
222
223 SYSCTL_DECL(_vfs_nfs4);
224
225 static int nfsaccess_cache_timeout = NFS_MAXATTRTIMO;
226 SYSCTL_INT(_vfs_nfs4, OID_AUTO, access_cache_timeout, CTLFLAG_RW,
227 &nfsaccess_cache_timeout, 0, "NFS ACCESS cache timeout");
228
229 static int nfsv3_commit_on_close = 0;
230 SYSCTL_INT(_vfs_nfs4, OID_AUTO, nfsv3_commit_on_close, CTLFLAG_RW,
231 &nfsv3_commit_on_close, 0, "write+commit on close, else only write");
232 #if 0
233 SYSCTL_INT(_vfs_nfs4, OID_AUTO, access_cache_hits, CTLFLAG_RD,
234 &nfsstats.accesscache_hits, 0, "NFS ACCESS cache hit count");
235
236 SYSCTL_INT(_vfs_nfs4, OID_AUTO, access_cache_misses, CTLFLAG_RD,
237 &nfsstats.accesscache_misses, 0, "NFS ACCESS cache miss count");
238 #endif
239
240 #define NFSV3ACCESS_ALL (NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY \
241 | NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE \
242 | NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP)
243 static int
244 nfs3_access_otw(struct vnode *vp, int wmode, struct thread *td,
245 struct ucred *cred)
246 {
247 const int v3 = 1;
248 u_int32_t *tl;
249 int error = 0, attrflag;
250
251 return (0);
252
253 struct mbuf *mreq, *mrep = NULL, *md, *mb;
254 caddr_t bpos, dpos;
255 u_int32_t rmode;
256 struct nfsnode *np = VTONFS(vp);
257
258 nfsstats.rpccnt[NFSPROC_ACCESS]++;
259 mreq = nfsm_reqhead(vp, NFSPROC_ACCESS, NFSX_FH(v3) + NFSX_UNSIGNED);
260 mb = mreq;
261 bpos = mtod(mb, caddr_t);
262 nfsm_fhtom(vp, v3);
263 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
264 *tl = txdr_unsigned(wmode);
265 nfsm_request(vp, NFSPROC_ACCESS, td, cred);
266 nfsm_postop_attr(vp, attrflag);
267 if (!error) {
268 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
269 rmode = fxdr_unsigned(u_int32_t, *tl);
270 np->n_mode = rmode;
271 np->n_modeuid = cred->cr_uid;
272 np->n_modestamp = time_second;
273 }
274 m_freem(mrep);
275 nfsmout:
276 return error;
277 }
278
279 /*
280 * nfs access vnode op.
281 * For nfs version 2, just return ok. File accesses may fail later.
282 * For nfs version 3, use the access rpc to check accessibility. If file modes
283 * are changed on the server, accesses might still fail later.
284 */
285 static int
286 nfs4_access(struct vop_access_args *ap)
287 {
288 struct vnode *vp = ap->a_vp;
289 int error = 0;
290 u_int32_t mode, wmode;
291 int v3 = NFS_ISV3(vp); /* v3 \in v4 */
292 struct nfsnode *np = VTONFS(vp);
293 caddr_t bpos, dpos;
294 struct mbuf *mreq, *mrep = NULL, *md, *mb;
295 struct nfs4_compound cp;
296 struct nfs4_oparg_access acc;
297 struct thread *td = ap->a_td;
298 struct ucred *cred = ap->a_cred;
299
300 /*
301 * Disallow write attempts on filesystems mounted read-only;
302 * unless the file is a socket, fifo, or a block or character
303 * device resident on the filesystem.
304 */
305 if ((ap->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
306 switch (vp->v_type) {
307 case VREG:
308 case VDIR:
309 case VLNK:
310 return (EROFS);
311 default:
312 break;
313 }
314 }
315 /*
316 * For nfs v3, check to see if we have done this recently, and if
317 * so return our cached result instead of making an ACCESS call.
318 * If not, do an access rpc, otherwise you are stuck emulating
319 * ufs_access() locally using the vattr. This may not be correct,
320 * since the server may apply other access criteria such as
321 * client uid-->server uid mapping that we do not know about.
322 */
323 /* XXX Disable this for now; needs fixing of _access_otw() */
324 if (0 && v3) {
325 if (ap->a_mode & VREAD)
326 mode = NFSV3ACCESS_READ;
327 else
328 mode = 0;
329 if (vp->v_type != VDIR) {
330 if (ap->a_mode & VWRITE)
331 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
332 if (ap->a_mode & VEXEC)
333 mode |= NFSV3ACCESS_EXECUTE;
334 } else {
335 if (ap->a_mode & VWRITE)
336 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
337 NFSV3ACCESS_DELETE);
338 if (ap->a_mode & VEXEC)
339 mode |= NFSV3ACCESS_LOOKUP;
340 }
341 /* XXX safety belt, only make blanket request if caching */
342 if (nfsaccess_cache_timeout > 0) {
343 wmode = NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY |
344 NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE |
345 NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP;
346 } else {
347 wmode = mode;
348 }
349
350 /*
351 * Does our cached result allow us to give a definite yes to
352 * this request?
353 */
354 if ((time_second < (np->n_modestamp + nfsaccess_cache_timeout)) &&
355 (ap->a_cred->cr_uid == np->n_modeuid) &&
356 ((np->n_mode & mode) == mode)) {
357 nfsstats.accesscache_hits++;
358 } else {
359 /*
360 * Either a no, or a don't know. Go to the wire.
361 */
362 nfsstats.accesscache_misses++;
363 error = nfs3_access_otw(vp, wmode, ap->a_td,ap->a_cred);
364 if (!error) {
365 if ((np->n_mode & mode) != mode) {
366 error = EACCES;
367 }
368 }
369 }
370 return (error);
371 }
372
373 /* XXX use generic access code here? */
374 mode = ap->a_mode & VREAD ? NFSV4ACCESS_READ : 0;
375 if (vp->v_type == VDIR) {
376 if (ap->a_mode & VWRITE)
377 mode |= NFSV4ACCESS_MODIFY | NFSV4ACCESS_EXTEND | NFSV4ACCESS_DELETE;
378 if (ap->a_mode & VEXEC)
379 mode |= NFSV4ACCESS_LOOKUP;
380 } else {
381 if (ap->a_mode & VWRITE)
382 mode |= NFSV4ACCESS_MODIFY | NFSV4ACCESS_EXTEND;
383 if (ap->a_mode & VEXEC)
384 mode |= NFSV4ACCESS_EXECUTE;
385 }
386
387 nfs_v4initcompound(&cp);
388 acc.mode = mode;
389
390 mreq = nfsm_reqhead(vp, NFSV4PROC_COMPOUND, 0);
391 mb = mreq;
392 bpos = mtod(mb, caddr_t);
393
394 nfsm_v4build_compound(&cp, "nfs4_access()");
395 nfsm_v4build_putfh(&cp, vp);
396 nfsm_v4build_access(&cp, &acc);
397 nfsm_v4build_finalize(&cp);
398
399 nfsm_request(vp, NFSV4PROC_COMPOUND, td, cred);
400 if (error != 0)
401 goto nfsmout;
402
403 nfsm_v4dissect_compound(&cp);
404 nfsm_v4dissect_putfh(&cp);
405 nfsm_v4dissect_access(&cp, &acc);
406
407 if ((acc.rmode & mode) != mode)
408 error = EACCES;
409
410 nfsmout:
411 error = nfs_v4postop(&cp, error);
412
413 if (mrep != NULL)
414 m_freem(mrep);
415
416 return (error);
417 }
418
419 static int
420 nfs4_openrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
421 int flags, struct vattr *vap)
422 {
423 struct vnode *vp = *vpp;
424 struct nfs4_oparg_getattr getattr;
425 struct nfs4_oparg_getfh getfh;
426 struct nfs4_oparg_open opena;
427 struct nfs4_compound cp;
428 caddr_t bpos, dpos;
429 int error = 0;
430 struct mbuf *mreq, *mrep = NULL, *md, *mb;
431 struct ucred *cred = cnp->cn_cred;
432 struct thread *td = cnp->cn_thread;
433 struct nfs4_fctx xfc, *fcp;
434 struct nfsnode *np;
435
436 if (vp == NULL) {
437 /* Create a new file */
438 np = NULL;
439 fcp = &xfc;
440 bzero(fcp, sizeof(*fcp));
441 } else {
442 np = VTONFS(vp);
443 fcp = flags & FWRITE ? &np->n_wfc : &np->n_rfc;
444 }
445
446 /*
447 * Since we are currently only one lockowner; we only open the
448 * file once each for reading and writing.
449 */
450 if (fcp->refcnt++ != 0) {
451 *vpp = vp;
452 /*printf("not opening %s\n", np->n_name != NULL ? np->n_name : "");*/
453 return (0);
454 }
455
456 fcp->lop = &nfs4_masterlowner;
457 fcp->np = np;
458
459 nfs_v4initcompound(&cp);
460 cp.nmp = VFSTONFS(dvp->v_mount);
461
462 opena.ctype = NCLNULL;
463 opena.flags = flags;
464 opena.vap = vap;
465 opena.fcp = fcp; /* For lockowner */
466 opena.cnp = cnp;
467
468 getattr.bm = &nfsv4_getattrbm;
469
470 mreq = nfsm_reqhead(vp, NFSV4PROC_COMPOUND, 0);
471 mb = mreq;
472 bpos = mtod(mb, caddr_t);
473
474 nfsm_v4build_compound(&cp, "nfs4_openrpc()");
475 nfsm_v4build_putfh(&cp, dvp);
476 nfsm_v4build_open(&cp, &opena);
477 nfsm_v4build_getattr(&cp, &getattr);
478 nfsm_v4build_getfh(&cp, &getfh);
479 nfsm_v4build_finalize(&cp);
480
481 nfsm_request(vp != NULL ? vp : dvp, NFSV4PROC_COMPOUND, td, cred);
482 if (error != 0)
483 goto nfsmout;
484
485 nfsm_v4dissect_compound(&cp);
486 nfsm_v4dissect_putfh(&cp);
487 nfsm_v4dissect_open(&cp, &opena);
488 nfsm_v4dissect_getattr(&cp, &getattr);
489 nfsm_v4dissect_getfh(&cp, &getfh);
490
491 error = nfs_v4postop(&cp, error);
492
493 if (opena.rflags & NFSV4OPENRES_CONFIRM) {
494 error = nfs4_open_confirm(vp ? vp : dvp, &cp, &opena, &getfh, cred, td);
495 if (error != 0)
496 goto nfsmout;
497 }
498
499 if (vp == NULL) {
500 /* New file */
501 error = nfs_nget(dvp->v_mount, &getfh.fh_val,
502 getfh.fh_len, &np, LK_EXCLUSIVE);
503 if (error != 0)
504 goto nfsmout;
505
506 vp = NFSTOV(np);
507 np->n_dvp = dvp;
508 np->n_namelen = cnp->cn_namelen; /* XXX memory leaks on these; track! */
509 if (np->n_name != NULL)
510 FREE(np->n_name, M_NFSREQ);
511 MALLOC(np->n_name, u_char *, np->n_namelen + 1, M_NFSREQ, M_WAITOK);
512 bcopy(cnp->cn_nameptr, np->n_name, np->n_namelen);
513 np->n_name[np->n_namelen] = '\0';
514 if (flags & FWRITE)
515 np->n_wfc = *fcp;
516 else
517 np->n_rfc = *fcp;
518
519 /*printf("opened new file %s\n", np->n_name);*/
520
521 nfs4_vnop_loadattrcache(vp, &getattr.fa, NULL);
522 *vpp = vp;
523 } else {
524 /*printf("openend \"old\" %s\n", np->n_name != NULL ? np->n_name : "");*/
525
526 if (flags & O_TRUNC && np->n_size != 0) {
527 struct vattr va;
528
529 VATTR_NULL(&va);
530 va.va_size = 0;
531 error = nfs4_setattrrpc(vp, &va,
532 cnp->cn_cred, cnp->cn_thread);
533 }
534 np->n_attrstamp = 0;
535 }
536
537 nfsmout:
538 if (mrep != NULL)
539 m_freem(mrep);
540
541 return (error);
542 }
543
544 static int
545 nfs4_open_confirm(struct vnode *vp, struct nfs4_compound *cpp,
546 struct nfs4_oparg_open *openap, struct nfs4_oparg_getfh *gfh,
547 struct ucred *cred, struct thread *td)
548 {
549 caddr_t bpos, dpos;
550 int error = 0;
551 struct mbuf *mreq, *mrep = NULL, *md, *mb;
552
553 nfs_v4initcompound(cpp);
554 cpp->nmp = VFSTONFS(vp->v_mount);
555
556 mreq = nfsm_reqhead(vp, NFSV4PROC_COMPOUND, 0);
557 mb = mreq;
558 bpos = mtod(mb, caddr_t);
559
560 nfsm_v4build_compound(cpp, "nfs4_open_confirm()");
561 nfsm_v4build_putfh_nv(cpp, gfh);
562 nfsm_v4build_open_confirm(cpp, openap);
563 nfsm_v4build_finalize(cpp);
564
565 nfsm_request(vp, NFSV4PROC_COMPOUND, td, cred);
566 if (error != 0)
567 goto nfsmout;
568
569 nfsm_v4dissect_compound(cpp);
570 nfsm_v4dissect_putfh(cpp);
571 nfsm_v4dissect_open_confirm(cpp, openap);
572
573 nfsmout:
574 error = nfs_v4postop(cpp, error);
575
576 if (mrep != NULL)
577 m_freem(mrep);
578
579 return (error);
580 }
581
582
583 /*
584 * nfs open vnode op
585 * Check to see if the type is ok
586 * and that deletion is not in progress.
587 * For paged in text files, you will need to flush the page cache
588 * if consistency is lost.
589 */
590 /* ARGSUSED */
591 static int
592 nfs4_open(struct vop_open_args *ap)
593 {
594 struct vnode *vp = ap->a_vp;
595 struct nfsnode *np = VTONFS(vp);
596 enum vtype vtype = vp->v_type;
597 int mode = ap->a_mode;
598 struct componentname cn;
599
600 if (vtype != VREG) {
601 if (vtype != VDIR && vtype != VLNK) {
602 #ifdef DIAGNOSTIC
603 printf("open eacces vtyp=%d\n", vp->v_type);
604 #endif
605 return (EACCES);
606 } else
607 return (0);
608 }
609
610 if (np->n_flag & NCREATED) {
611 np->n_flag &= ~NCREATED;
612 return (0);
613 }
614
615 cn.cn_nameptr = np->n_name;
616 cn.cn_namelen = np->n_namelen;
617 cn.cn_cred = ap->a_cred;
618 cn.cn_thread = ap->a_td;
619
620 return (nfs4_openrpc(np->n_dvp, &vp, &cn, mode, NULL));
621 }
622
623 static int
624 nfs4_closerpc(struct vnode *vp, struct ucred *cred, struct thread *td, int flags)
625 {
626 caddr_t bpos, dpos;
627 int error = 0;
628 struct mbuf *mreq, *mrep = NULL, *md, *mb;
629 struct nfs4_fctx *fcp;
630 struct nfs4_compound cp;
631 struct nfsnode *np = VTONFS(vp);
632
633 fcp = flags & FWRITE ? &np->n_wfc : &np->n_rfc;
634
635 nfs_v4initcompound(&cp);
636
637 if (--fcp->refcnt != 0)
638 return (0);
639
640 /*printf("closing %s\n", np->n_name != NULL ? np->n_name : "");*/
641
642 cp.fcp = fcp;
643
644 mreq = nfsm_reqhead(vp, NFSV4PROC_COMPOUND, 0);
645 mb = mreq;
646 bpos = mtod(mb, caddr_t);
647
648 nfsm_v4build_compound(&cp, "nfs4_closerpc()");
649 nfsm_v4build_putfh(&cp, vp);
650 nfsm_v4build_close(&cp, fcp);
651 nfsm_v4build_finalize(&cp);
652
653 nfsm_request(vp, NFSV4PROC_COMPOUND, td, cred);
654 if (error != 0)
655 goto nfsmout;
656
657 nfsm_v4dissect_compound(&cp);
658 nfsm_v4dissect_putfh(&cp);
659 nfsm_v4dissect_close(&cp, fcp);
660
661 nfsmout:
662 error = nfs_v4postop(&cp, error);
663
664 if (mrep != NULL)
665 m_freem(mrep);
666
667 return (error);
668 }
669
670 /*
671 * nfs close vnode op
672 * play it safe for now (see comments in v2/v3 nfs_close regarding dirty buffers)
673 */
674 /* ARGSUSED */
675 static int
676 nfs4_close(struct vop_close_args *ap)
677 {
678 struct vnode *vp = ap->a_vp;
679 struct nfsnode *np = VTONFS(vp);
680 int error = 0;
681
682 if (vp->v_type != VREG)
683 return (0);
684
685 if (np->n_flag & NMODIFIED) {
686 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
687 np->n_attrstamp = 0;
688 }
689
690 error = nfs4_closerpc(vp, ap->a_cred, ap->a_td, ap->a_fflag);
691
692 if (!error && np->n_flag & NWRITEERR) {
693 np->n_flag &= ~NWRITEERR;
694 error = np->n_error;
695 }
696 return (error);
697 }
698
699 /*
700 * nfs getattr call from vfs.
701 */
702 static int
703 nfs4_getattr(struct vop_getattr_args *ap)
704 {
705 struct vnode *vp = ap->a_vp;
706 struct nfsnode *np = VTONFS(vp);
707 caddr_t bpos, dpos;
708 int error = 0;
709 struct mbuf *mreq, *mrep = NULL, *md, *mb;
710 struct nfs4_oparg_getattr ga;
711 struct nfs4_compound cp;
712
713 /*
714 * Update local times for special files.
715 */
716 if (np->n_flag & (NACC | NUPD))
717 np->n_flag |= NCHG;
718 /*
719 * First look in the cache.
720 */
721 if (nfs_getattrcache(vp, ap->a_vap) == 0)
722 return (0);
723
724 nfsstats.rpccnt[NFSPROC_GETATTR]++;
725
726 mreq = nfsm_reqhead(vp, NFSV4PROC_COMPOUND, NFSX_FH(1));
727 mb = mreq;
728 bpos = mtod(mb, caddr_t);
729
730 ga.bm = &nfsv4_getattrbm;
731 nfs_v4initcompound(&cp);
732
733 nfsm_v4build_compound(&cp, "nfs4_getattr()");
734 nfsm_v4build_putfh(&cp, vp);
735 nfsm_v4build_getattr(&cp, &ga);
736 nfsm_v4build_finalize(&cp);
737
738 nfsm_request(vp, NFSV4PROC_COMPOUND, ap->a_td, ap->a_cred);
739 if (error != 0)
740 goto nfsmout;
741
742 nfsm_v4dissect_compound(&cp);
743 nfsm_v4dissect_putfh(&cp);
744 nfsm_v4dissect_getattr(&cp, &ga);
745
746 nfs4_vnop_loadattrcache(vp, &ga.fa, ap->a_vap);
747
748 nfsmout:
749 error = nfs_v4postop(&cp, error);
750
751 if (mrep != NULL)
752 m_freem(mrep);
753 return (error);
754 }
755
756 /*
757 * nfs setattr call.
758 */
759 static int
760 nfs4_setattr(struct vop_setattr_args *ap)
761 {
762 struct vnode *vp = ap->a_vp;
763 struct nfsnode *np = VTONFS(vp);
764 struct vattr *vap = ap->a_vap;
765 int error = 0;
766 u_quad_t tsize;
767
768 #ifndef nolint
769 tsize = (u_quad_t)0;
770 #endif
771
772 /*
773 * Setting of flags is not supported.
774 */
775 if (vap->va_flags != VNOVAL)
776 return (EOPNOTSUPP);
777
778 /*
779 * Disallow write attempts if the filesystem is mounted read-only.
780 */
781 if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
782 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
783 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
784 (vp->v_mount->mnt_flag & MNT_RDONLY))
785 return (EROFS);
786 if (vap->va_size != VNOVAL) {
787 switch (vp->v_type) {
788 case VDIR:
789 return (EISDIR);
790 case VCHR:
791 case VBLK:
792 case VSOCK:
793 case VFIFO:
794 if (vap->va_mtime.tv_sec == VNOVAL &&
795 vap->va_atime.tv_sec == VNOVAL &&
796 vap->va_mode == (mode_t)VNOVAL &&
797 vap->va_uid == (uid_t)VNOVAL &&
798 vap->va_gid == (gid_t)VNOVAL)
799 return (0);
800 vap->va_size = VNOVAL;
801 break;
802 default:
803 /*
804 * Disallow write attempts if the filesystem is
805 * mounted read-only.
806 */
807 if (vp->v_mount->mnt_flag & MNT_RDONLY)
808 return (EROFS);
809
810 /*
811 * We run vnode_pager_setsize() early (why?),
812 * we must set np->n_size now to avoid vinvalbuf
813 * V_SAVE races that might setsize a lower
814 * value.
815 */
816
817 tsize = np->n_size;
818 error = nfs_meta_setsize(vp, ap->a_cred,
819 ap->a_td, vap->va_size);
820
821 if (np->n_flag & NMODIFIED) {
822 if (vap->va_size == 0)
823 error = nfs_vinvalbuf(vp, 0, ap->a_td, 1);
824 else
825 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
826 if (error) {
827 vnode_pager_setsize(vp, np->n_size);
828 return (error);
829 }
830 }
831 /*
832 * np->n_size has already been set to vap->va_size
833 * in nfs_meta_setsize(). We must set it again since
834 * nfs_loadattrcache() could be called through
835 * nfs_meta_setsize() and could modify np->n_size.
836 */
837 np->n_vattr.va_size = np->n_size = vap->va_size;
838 };
839 } else if ((vap->va_mtime.tv_sec != VNOVAL ||
840 vap->va_atime.tv_sec != VNOVAL) && (np->n_flag & NMODIFIED) &&
841 vp->v_type == VREG &&
842 (error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1)) == EINTR)
843 return (error);
844
845 if (vap->va_size != VNOVAL && np->n_wfc.refcnt == 0) {
846 /* Have to open the file before we can truncate it */
847 struct componentname cn;
848
849 cn.cn_nameptr = np->n_name;
850 cn.cn_namelen = np->n_namelen;
851 cn.cn_cred = ap->a_cred;
852 cn.cn_thread = ap->a_td;
853 error = nfs4_openrpc(np->n_dvp, &vp, &cn, FWRITE, NULL);
854 if (error)
855 return error;
856 np->n_flag |= NTRUNCATE;
857 }
858
859 error = nfs4_setattrrpc(vp, vap, ap->a_cred, ap->a_td);
860 if (error && vap->va_size != VNOVAL) {
861 np->n_size = np->n_vattr.va_size = tsize;
862 vnode_pager_setsize(vp, np->n_size);
863 }
864 return (error);
865 }
866
867 /*
868 * Do an nfs setattr rpc.
869 */
870 static int
871 nfs4_setattrrpc(struct vnode *vp, struct vattr *vap, struct ucred *cred,
872 struct thread *td)
873 {
874 caddr_t bpos, dpos;
875 int error = 0;
876 struct mbuf *mreq, *mrep = NULL, *md, *mb;
877 struct nfs4_compound cp;
878 struct nfs4_oparg_getattr ga;
879 struct nfsnode *np = VTONFS(vp);
880 struct nfs4_fctx *fcp;
881
882 nfsstats.rpccnt[NFSPROC_SETATTR]++;
883 mreq = nfsm_reqhead(vp, NFSV4PROC_COMPOUND, 0);
884 mb = mreq;
885 bpos = mtod(mb, caddr_t);
886
887 ga.bm = &nfsv4_getattrbm;
888 fcp = (vap->va_size != VNOVAL) ? &np->n_wfc : NULL;
889 nfs_v4initcompound(&cp);
890
891 nfsm_v4build_compound(&cp, "nfs4_setattrrpc");
892 nfsm_v4build_putfh(&cp, vp);
893 nfsm_v4build_setattr(&cp, vap, fcp);
894 nfsm_v4build_getattr(&cp, &ga);
895 nfsm_v4build_finalize(&cp);
896
897 nfsm_request(vp, NFSV4PROC_COMPOUND, td, cred);
898 if (error != 0)
899 goto nfsmout;
900
901 nfsm_v4dissect_compound(&cp);
902 nfsm_v4dissect_putfh(&cp);
903 nfsm_v4dissect_setattr(&cp);
904 nfsm_v4dissect_getattr(&cp, &ga);
905
906 nfs4_vnop_loadattrcache(vp, &ga.fa, NULL);
907
908 /* TODO: do the settatr and close in a single compound rpc */
909 if (np->n_flag & NTRUNCATE) {
910 error = nfs4_closerpc(vp, cred, td, FWRITE);
911 np->n_flag &= ~NTRUNCATE;
912 }
913
914 nfsmout:
915 error = nfs_v4postop(&cp, error);
916
917 if (mrep != NULL)
918 m_freem(mrep);
919
920 return (error);
921 }
922
923 /*
924 * nfs lookup call, one step at a time...
925 * First look in cache
926 * If not found, unlock the directory nfsnode and do the rpc
927 */
928 static int
929 nfs4_lookup(struct vop_lookup_args *ap)
930 {
931 struct componentname *cnp = ap->a_cnp;
932 struct vnode *dvp = ap->a_dvp;
933 struct vnode **vpp = ap->a_vpp;
934 int isdot, flags = cnp->cn_flags;
935 struct vnode *newvp;
936 struct nfsmount *nmp;
937 caddr_t bpos, dpos;
938 struct mbuf *mreq, *mrep = NULL, *md, *mb;
939 long len;
940 nfsfh_t *fhp;
941 struct nfsnode *np;
942 int error = 0, fhsize;
943 struct thread *td = cnp->cn_thread;
944 struct nfs4_compound cp;
945 struct nfs4_oparg_getattr ga, dga;
946 struct nfs4_oparg_lookup l;
947 struct nfs4_oparg_getfh gfh;
948
949 *vpp = NULLVP;
950 if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
951 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
952 return (EROFS);
953 if (dvp->v_type != VDIR)
954 return (ENOTDIR);
955 nmp = VFSTONFS(dvp->v_mount);
956 np = VTONFS(dvp);
957
958 isdot = cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.';
959
960 if ((error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td)) != 0) {
961 *vpp = NULLVP;
962 return (error);
963 }
964 if ((error = cache_lookup(dvp, vpp, cnp)) && error != ENOENT) {
965 struct vattr vattr;
966
967 newvp = *vpp;
968 if (!VOP_GETATTR(newvp, &vattr, cnp->cn_cred, td)
969 && vattr.va_ctime.tv_sec == VTONFS(newvp)->n_ctime) {
970 nfsstats.lookupcache_hits++;
971 if (cnp->cn_nameiop != LOOKUP &&
972 (flags & ISLASTCN))
973 cnp->cn_flags |= SAVENAME;
974 return (0);
975 }
976 cache_purge(newvp);
977 if (newvp != dvp)
978 vput(newvp);
979 else
980 vrele(newvp);
981 }
982
983 error = 0;
984 newvp = NULLVP;
985 nfsstats.lookupcache_misses++;
986 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
987
988 len = cnp->cn_namelen;
989 mreq = nfsm_reqhead(NULL, NFSV4PROC_COMPOUND, 0);
990 mb = mreq;
991 bpos = mtod(mb, caddr_t);
992
993 ga.bm = &nfsv4_getattrbm;
994 dga.bm = &nfsv4_getattrbm;
995 nfs_v4initcompound(&cp);
996
997 nfsm_v4build_compound(&cp, "nfs4_lookup()");
998 nfsm_v4build_putfh(&cp, dvp);
999 nfsm_v4build_getattr(&cp, &dga);
1000 if (flags & ISDOTDOT)
1001 nfsm_v4build_lookupp(&cp);
1002 else if (!isdot) {
1003 l.name = cnp->cn_nameptr;
1004 l.namelen = len;
1005 nfsm_v4build_lookup(&cp, &l);
1006 }
1007 nfsm_v4build_getattr(&cp, &ga);
1008 nfsm_v4build_getfh(&cp, &gfh);
1009 nfsm_v4build_finalize(&cp);
1010
1011 nfsm_request(dvp, NFSV4PROC_COMPOUND, cnp->cn_thread, cnp->cn_cred);
1012 if (error != 0)
1013 goto nfsmout;
1014
1015 nfsm_v4dissect_compound(&cp);
1016 nfsm_v4dissect_putfh(&cp);
1017 nfsm_v4dissect_getattr(&cp, &dga);
1018 if (flags & ISDOTDOT)
1019 nfsm_v4dissect_lookupp(&cp);
1020 else if (!isdot)
1021 nfsm_v4dissect_lookup(&cp);
1022 nfsm_v4dissect_getattr(&cp, &ga);
1023 nfsm_v4dissect_getfh(&cp, &gfh);
1024
1025 nfs4_vnop_loadattrcache(dvp, &dga.fa, NULL);
1026 fhp = &gfh.fh_val;
1027 fhsize = gfh.fh_len;
1028
1029 /*
1030 * Handle RENAME case...
1031 */
1032 if (cnp->cn_nameiop == RENAME && (flags & ISLASTCN)) {
1033 if (NFS_CMPFH(np, fhp, fhsize))
1034 return (EISDIR);
1035
1036 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np, LK_EXCLUSIVE);
1037 if (error)
1038 return (error);
1039
1040 newvp = NFSTOV(np);
1041
1042 nfs4_vnop_loadattrcache(newvp, &ga.fa, NULL);
1043
1044 *vpp = newvp;
1045 cnp->cn_flags |= SAVENAME;
1046 return (0);
1047 }
1048
1049 if (flags & ISDOTDOT) {
1050 VOP_UNLOCK(dvp, 0, td);
1051
1052 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np, LK_EXCLUSIVE);
1053 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, td);
1054 if (error)
1055 return (error);
1056 newvp = NFSTOV(np);
1057
1058 nfs4_vnop_loadattrcache(newvp, &ga.fa, NULL);
1059 } else if (NFS_CMPFH(np, fhp, fhsize)) {
1060 VREF(dvp);
1061 newvp = dvp;
1062 } else {
1063 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np, LK_EXCLUSIVE);
1064 if (error)
1065 return (error);
1066 newvp = NFSTOV(np);
1067
1068 /* Fill in np used by open. */
1069 np->n_dvp = dvp;
1070 np->n_namelen = cnp->cn_namelen;
1071 if (np->n_name != NULL)
1072 FREE(np->n_name, M_NFSREQ);
1073 MALLOC(np->n_name, u_char *, np->n_namelen + 1, M_NFSREQ, M_WAITOK);
1074 bcopy(cnp->cn_nameptr, np->n_name, np->n_namelen);
1075 np->n_name[np->n_namelen] = '\0';
1076
1077 nfs4_vnop_loadattrcache(newvp, &ga.fa, NULL);
1078 }
1079
1080 if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
1081 cnp->cn_flags |= SAVENAME;
1082 if ((cnp->cn_flags & MAKEENTRY) &&
1083 (cnp->cn_nameiop != DELETE || !(flags & ISLASTCN))) {
1084 np->n_ctime = np->n_vattr.va_ctime.tv_sec;
1085 cache_enter(dvp, newvp, cnp);
1086 }
1087 *vpp = newvp;
1088 m_freem(mrep);
1089 nfsmout:
1090 error = nfs_v4postop(&cp, error);
1091
1092 if (error) {
1093 if (newvp != NULLVP) {
1094 vrele(newvp);
1095 *vpp = NULLVP;
1096 }
1097 if ((cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME) &&
1098 (flags & ISLASTCN) && error == ENOENT) {
1099 if (dvp->v_mount->mnt_flag & MNT_RDONLY)
1100 error = EROFS;
1101 else
1102 error = EJUSTRETURN;
1103 }
1104 if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
1105 cnp->cn_flags |= SAVENAME;
1106 }
1107
1108 return (error);
1109 }
1110
1111 /*
1112 * nfs read call.
1113 * Just call nfs_bioread() to do the work.
1114 */
1115 static int
1116 nfs4_read(struct vop_read_args *ap)
1117 {
1118 struct vnode *vp = ap->a_vp;
1119
1120 switch (vp->v_type) {
1121 case VREG:
1122 return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag, ap->a_cred));
1123 case VDIR:
1124 return (EISDIR);
1125 default:
1126 return (EOPNOTSUPP);
1127 }
1128 }
1129
1130 /*
1131 * nfs readlink call
1132 */
1133 static int
1134 nfs4_readlink(struct vop_readlink_args *ap)
1135 {
1136 struct vnode *vp = ap->a_vp;
1137
1138 if (vp->v_type != VLNK)
1139 return (EINVAL);
1140 return (nfs_bioread(vp, ap->a_uio, 0, ap->a_cred));
1141 }
1142
1143 /*
1144 * Do a readlink rpc.
1145 * Called by nfs_doio() from below the buffer cache.
1146 */
1147 int
1148 nfs4_readlinkrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1149 {
1150 caddr_t bpos, dpos;
1151 int error = 0;
1152 struct mbuf *mreq, *mrep = NULL, *md, *mb;
1153 struct nfs4_compound cp;
1154
1155 nfsstats.rpccnt[NFSPROC_READLINK]++;
1156
1157 mreq = nfsm_reqhead(vp, NFSV4PROC_COMPOUND, 0);
1158 mb = mreq;
1159 bpos = mtod(mb, caddr_t);
1160
1161 nfs_v4initcompound(&cp);
1162
1163 nfsm_v4build_compound(&cp, "nfs4_readlinkrpc()");
1164 nfsm_v4build_putfh(&cp, vp);
1165 nfsm_v4build_readlink(&cp);
1166 nfsm_v4build_finalize(&cp);
1167
1168 nfsm_request(vp, NFSV4PROC_COMPOUND, uiop->uio_td, cred);
1169 if (error != 0)
1170 goto nfsmout;
1171
1172 nfsm_v4dissect_compound(&cp);
1173 nfsm_v4dissect_putfh(&cp);
1174 nfsm_v4dissect_readlink(&cp, uiop);
1175
1176 nfsmout:
1177 error = nfs_v4postop(&cp, error);
1178
1179 if (mrep != NULL)
1180 m_freem(mrep);
1181 return (error);
1182 }
1183
1184 /*
1185 * nfs read rpc call
1186 * Ditto above
1187 */
1188 int
1189 nfs4_readrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1190 {
1191 caddr_t bpos, dpos;
1192 struct mbuf *mreq, *mrep = NULL, *md, *mb;
1193 struct nfsmount *nmp;
1194 int error = 0, len, tsiz;
1195 struct nfs4_compound cp;
1196 struct nfs4_oparg_read read;
1197 struct nfsnode *np = VTONFS(vp);
1198
1199 nmp = VFSTONFS(vp->v_mount);
1200 tsiz = uiop->uio_resid;
1201 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1202 return (EFBIG);
1203
1204 if (tsiz == 0)
1205 return (0);
1206
1207 read.uiop = uiop;
1208 read.fcp = np->n_rfc.refcnt > 0 ? &np->n_rfc : &np->n_wfc;
1209
1210 while (tsiz > 0) {
1211 nfsstats.rpccnt[NFSPROC_READ]++;
1212 len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
1213
1214 read.off = uiop->uio_offset;
1215 read.maxcnt = len;
1216 nfs_v4initcompound(&cp);
1217
1218 mreq = nfsm_reqhead(vp, NFSV4PROC_COMPOUND, 0);
1219 mb = mreq;
1220 bpos = mtod(mb, caddr_t);
1221
1222 nfsm_v4build_compound(&cp, "nfs4_readrpc()");
1223 nfsm_v4build_putfh(&cp, vp);
1224 nfsm_v4build_read(&cp, &read);
1225 nfsm_v4build_finalize(&cp);
1226
1227 nfsm_request(vp, NFSV4PROC_COMPOUND, uiop->uio_td, cred);
1228 if (error != 0) {
1229 error = nfs_v4postop(&cp, error);
1230 goto nfsmout;
1231 }
1232
1233 nfsm_v4dissect_compound(&cp);
1234 nfsm_v4dissect_putfh(&cp);
1235 nfsm_v4dissect_read(&cp, &read);
1236
1237 if (read.eof || read.retlen == 0)
1238 tsiz = 0;
1239 else
1240 tsiz -= read.retlen;
1241
1242 error = nfs_v4postop(&cp, error);
1243
1244 m_freem(mrep);
1245 mrep = NULL;
1246 }
1247 nfsmout:
1248 if (mrep != NULL)
1249 m_freem(mrep);
1250
1251 return (error);
1252 }
1253
1254 /*
1255 * nfs write call
1256 */
1257 int
1258 nfs4_writerpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
1259 int *iomode, int *must_commit)
1260 {
1261 int32_t backup;
1262 caddr_t bpos, dpos;
1263 struct mbuf *mreq, *mrep = NULL, *md, *mb;
1264 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1265 int error = 0, len, tsiz, wccflag = 1, rlen;
1266 struct nfs4_compound cp;
1267 struct nfs4_oparg_write write;
1268 nfsv4stablehow commit, committed = NSHFILESYNC;
1269 caddr_t verf;
1270 struct nfsnode *np = VTONFS(vp);
1271
1272 #ifndef DIAGNOSTIC
1273 if (uiop->uio_iovcnt != 1)
1274 panic("nfs: writerpc iovcnt > 1");
1275 #endif
1276 *must_commit = 0;
1277 tsiz = uiop->uio_resid;
1278 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1279 return (EFBIG);
1280
1281 if (tsiz == 0)
1282 return (0);
1283
1284 write.stable = (nfsv4stablehow)*iomode;
1285 write.uiop = uiop;
1286 write.fcp = &np->n_wfc;
1287
1288 while (tsiz > 0) {
1289 nfsstats.rpccnt[NFSPROC_WRITE]++;
1290 len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz;
1291
1292 write.off = uiop->uio_offset;
1293 write.cnt = len;
1294 nfs_v4initcompound(&cp);
1295
1296 mreq = nfsm_reqhead(vp, NFSV4PROC_COMPOUND, 0);
1297 mb = mreq;
1298 bpos = mtod(mb, caddr_t);
1299
1300 nfsm_v4build_compound(&cp, "nfs4_writerpc()");
1301 nfsm_v4build_putfh(&cp, vp);
1302 nfsm_v4build_write(&cp, &write);
1303 nfsm_v4build_finalize(&cp);
1304
1305 nfsm_request(vp, NFSV4PROC_COMPOUND, uiop->uio_td, cred);
1306 if (error != 0) {
1307 error = nfs_v4postop(&cp, error);
1308 goto nfsmout;
1309 }
1310
1311 nfsm_v4dissect_compound(&cp);
1312 nfsm_v4dissect_putfh(&cp);
1313 nfsm_v4dissect_write(&cp, &write);
1314
1315 rlen = write.retlen;
1316 if (rlen == 0) {
1317 error = NFSERR_IO;
1318 break;
1319 } else if (rlen < len) {
1320 backup = len - rlen;
1321 uiop->uio_iov->iov_base =
1322 (char *)uiop->uio_iov->iov_base - backup;
1323 uiop->uio_iov->iov_len += backup;
1324 uiop->uio_offset -= backup;
1325 uiop->uio_resid += backup;
1326 len = rlen;
1327 }
1328
1329 commit = write.committed;
1330
1331 if (committed == NSHFILESYNC ||
1332 (committed = NSHDATASYNC && commit == NSHUNSTABLE))
1333 committed = commit;
1334
1335 verf = (caddr_t)write.wverf;
1336
1337 if ((nmp->nm_flag & NFSSTA_HASWRITEVERF) == 0) {
1338 bcopy(verf, nmp->nm_verf, NFSX_V4VERF);
1339 nmp->nm_flag |= NFSMNT_HASWRITEVERF;
1340 } else if (bcmp(verf, nmp->nm_verf, NFSX_V4VERF)) {
1341 *must_commit = 1;
1342 bcopy(verf, nmp->nm_verf, NFSX_V4VERF);
1343 }
1344
1345 /* XXX wccflag */
1346 if (wccflag)
1347 VTONFS(vp)->n_mtime = VTONFS(vp)->n_vattr.va_mtime;
1348
1349 error = nfs_v4postop(&cp, error);
1350
1351 m_freem(mrep);
1352 mrep = NULL;
1353 if (error)
1354 break;
1355 tsiz -= len;
1356 }
1357 nfsmout:
1358 if (mrep != NULL)
1359 m_freem(mrep);
1360 *iomode = committed;
1361 if (error)
1362 uiop->uio_resid = tsiz;
1363 return (error);
1364 }
1365
1366 /* ARGSUSED */
1367 static int
1368 nfs4_mknod(struct vop_mknod_args *ap)
1369 {
1370 struct vattr *vap = ap->a_vap;
1371 struct vnode *newvp = NULL;
1372 int error;
1373
1374 error = nfs4_createrpc(ap->a_dvp, &newvp,
1375 ap->a_cnp, (nfstype)vap->va_type, vap, NULL);
1376
1377 /* XXX - is this actually referenced here? */
1378 if (error == 0) {
1379 *ap->a_vpp = newvp;
1380 vrele(newvp);
1381 }
1382
1383 return (error);
1384 }
1385
1386 static int
1387 nfs4_createrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1388 nfstype ftype, struct vattr *vap, char *linktarget)
1389 {
1390 struct nfsnode *dnp = VTONFS(dvp);
1391 struct nfsnode *np = NULL;
1392 struct vnode *newvp = NULL;
1393 struct nfs4_compound cp;
1394 struct nfs4_oparg_create c;
1395 struct nfs4_oparg_getattr ga;
1396 struct nfs4_oparg_getfh gfh;
1397 caddr_t bpos, dpos;
1398 struct mbuf *mreq, *mrep = NULL, *md, *mb;
1399 int error = 0;
1400
1401 nfsstats.rpccnt[NFSPROC_CREATE]++;
1402
1403 mreq = nfsm_reqhead(dvp, NFSV4PROC_COMPOUND, 0);
1404 mb = mreq;
1405 bpos = mtod(mb, caddr_t);
1406
1407 bzero(&c, sizeof(c));
1408 bzero(&ga, sizeof(ga));
1409
1410 c.type = ftype;
1411 c.vap = vap;
1412 c.linktext = linktarget;
1413 c.name = cnp->cn_nameptr;
1414 c.namelen = cnp->cn_namelen;
1415
1416 ga.bm = &nfsv4_getattrbm;
1417 nfs_v4initcompound(&cp);
1418
1419 nfsm_v4build_compound(&cp, "nfs4_createrpc()");
1420 nfsm_v4build_putfh(&cp, dvp);
1421 nfsm_v4build_create(&cp, &c);
1422 nfsm_v4build_getattr(&cp, &ga);
1423 nfsm_v4build_getfh(&cp, &gfh);
1424 nfsm_v4build_finalize(&cp);
1425
1426 nfsm_request(dvp, NFSV4PROC_COMPOUND, cnp->cn_thread, cnp->cn_cred);
1427 if (error != 0)
1428 goto nfsmout;
1429
1430 nfsm_v4dissect_compound(&cp);
1431 nfsm_v4dissect_putfh(&cp);
1432 nfsm_v4dissect_create(&cp, &c);
1433 nfsm_v4dissect_getattr(&cp, &ga);
1434 nfsm_v4dissect_getfh(&cp, &gfh);
1435
1436 error = nfs_nget(dvp->v_mount, &gfh.fh_val, gfh.fh_len, &np, LK_EXCLUSIVE);
1437 if (error != 0)
1438 goto nfsmout;
1439
1440 newvp = NFSTOV(np);
1441 nfs4_vnop_loadattrcache(newvp, &ga.fa, NULL);
1442
1443 if (cnp->cn_flags & MAKEENTRY)
1444 cache_enter(dvp, newvp, cnp);
1445
1446 dnp->n_flag |= NMODIFIED;
1447 dnp->n_attrstamp = 0;
1448
1449 nfsmout:
1450 error = nfs_v4postop(&cp, error);
1451
1452 if (mrep != NULL)
1453 m_freem(mrep);
1454
1455 /* XXX */
1456 /*FREE(cnp->cn_pnbuf, M_NAMEI);*/
1457 if (error != 0 && newvp != NULL)
1458 vput(newvp);
1459 else if (error == 0)
1460 *vpp = newvp;
1461
1462 return (error);
1463 }
1464
1465 static int
1466 nfs4_renamerpc(struct vnode *fdvp, const char *fnameptr, int fnamelen,
1467 struct vnode *tdvp, const char *tnameptr, int tnamelen,
1468 struct ucred *cred, struct thread *td)
1469 {
1470
1471 struct nfsnode *fnp = VTONFS(fdvp), *tnp = VTONFS(tdvp);
1472 caddr_t bpos, dpos;
1473 struct mbuf *mreq, *mrep = NULL, *md, *mb;
1474 struct nfs4_compound cp;
1475 struct nfs4_oparg_rename r;
1476 int error = 0;
1477
1478 nfsstats.rpccnt[NFSPROC_RENAME]++;
1479
1480 r.fname = fnameptr;
1481 r.fnamelen = fnamelen;
1482 r.tname = tnameptr;
1483 r.tnamelen = tnamelen;
1484 nfs_v4initcompound(&cp);
1485
1486 mreq = nfsm_reqhead(fdvp, NFSV4PROC_COMPOUND, 0);
1487 mb = mreq;
1488 bpos = mtod(mb, caddr_t);
1489
1490 nfsm_v4build_compound(&cp, "nfs4_renamerpc()");
1491 nfsm_v4build_putfh(&cp, fdvp);
1492 nfsm_v4build_savefh(&cp);
1493 nfsm_v4build_putfh(&cp, tdvp);
1494 nfsm_v4build_rename(&cp, &r);
1495 nfsm_v4build_finalize(&cp);
1496
1497 nfsm_request(fdvp, NFSV4PROC_COMPOUND, td, cred);
1498 if (error != 0)
1499 goto nfsmout;
1500
1501 nfsm_v4dissect_compound(&cp);
1502 nfsm_v4dissect_putfh(&cp);
1503 nfsm_v4dissect_savefh(&cp);
1504 nfsm_v4dissect_putfh(&cp);
1505 nfsm_v4dissect_rename(&cp);
1506
1507 /* XXX should this always be performed? */
1508 fnp->n_flag |= NMODIFIED;
1509 tnp->n_flag |= NMODIFIED;
1510 fnp->n_attrstamp = tnp->n_attrstamp = 0;
1511
1512 nfsmout:
1513 error = nfs_v4postop(&cp, error);
1514
1515 if (mrep != NULL)
1516 m_freem(mrep);
1517
1518 return (error);
1519 }
1520
1521 /*
1522 * nfs file create call
1523 */
1524 static int
1525 nfs4_create(struct vop_create_args *ap)
1526 {
1527 struct vnode *dvp = ap->a_dvp;
1528 struct vattr *vap = ap->a_vap;
1529 struct nfsnode *dnp = VTONFS(dvp);
1530 struct componentname *cnp = ap->a_cnp;
1531 struct vnode *newvp = NULL;
1532 int error = 0, fmode = (O_CREAT | FREAD | FWRITE);
1533 struct vattr vattr;
1534
1535 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred, cnp->cn_thread)) != 0)
1536 return (error);
1537
1538 if (vap->va_vaflags & VA_EXCLUSIVE)
1539 fmode |= O_EXCL;
1540
1541 error = nfs4_openrpc(dvp, &newvp, cnp, fmode, vap);
1542 if (error != 0)
1543 goto out;
1544
1545 VTONFS(newvp)->n_flag |= NCREATED;
1546
1547 if (cnp->cn_flags & MAKEENTRY)
1548 cache_enter(dvp, newvp, cnp);
1549
1550 *ap->a_vpp = newvp;
1551
1552 dnp->n_flag |= NMODIFIED;
1553 dnp->n_attrstamp = 0; /* XXX; wccflag */
1554
1555 out:
1556 return (error);
1557 }
1558
1559 /*
1560 * nfs file remove call
1561 * To try and make nfs semantics closer to ufs semantics, a file that has
1562 * other processes using the vnode is renamed instead of removed and then
1563 * removed later on the last close.
1564 * - If v_usecount > 1
1565 * If a rename is not already in the works
1566 * call nfs4_sillyrename() to set it up
1567 * else
1568 * do the remove rpc
1569 */
1570 static int
1571 nfs4_remove(struct vop_remove_args *ap)
1572 {
1573 struct vnode *vp = ap->a_vp;
1574 struct vnode *dvp = ap->a_dvp;
1575 struct componentname *cnp = ap->a_cnp;
1576 struct nfsnode *np = VTONFS(vp);
1577 int error = 0;
1578 struct vattr vattr;
1579
1580 #ifndef DIAGNOSTIC
1581 if ((cnp->cn_flags & HASBUF) == 0)
1582 panic("nfs4_remove: no name");
1583 if (vrefcnt(vp) < 1)
1584 panic("nfs4_remove: bad v_usecount");
1585 #endif
1586 if (vp->v_type == VDIR)
1587 error = EPERM;
1588 else if (vrefcnt(vp) == 1 || (np->n_sillyrename &&
1589 VOP_GETATTR(vp, &vattr, cnp->cn_cred, cnp->cn_thread) == 0 &&
1590 vattr.va_nlink > 1)) {
1591 /*
1592 * Purge the name cache so that the chance of a lookup for
1593 * the name succeeding while the remove is in progress is
1594 * minimized. Without node locking it can still happen, such
1595 * that an I/O op returns ESTALE, but since you get this if
1596 * another host removes the file..
1597 */
1598 cache_purge(vp);
1599 /*
1600 * throw away biocache buffers, mainly to avoid
1601 * unnecessary delayed writes later.
1602 */
1603 error = nfs_vinvalbuf(vp, 0, cnp->cn_thread, 1);
1604 /* Do the rpc */
1605 if (error != EINTR)
1606 error = nfs4_removerpc(dvp, cnp->cn_nameptr,
1607 cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread);
1608 /*
1609 * Kludge City: If the first reply to the remove rpc is lost..
1610 * the reply to the retransmitted request will be ENOENT
1611 * since the file was in fact removed
1612 * Therefore, we cheat and return success.
1613 */
1614 if (error == ENOENT)
1615 error = 0;
1616 } else if (!np->n_sillyrename)
1617 error = nfs4_sillyrename(dvp, vp, cnp);
1618 np->n_attrstamp = 0;
1619 return (error);
1620 }
1621
1622 /*
1623 * nfs file remove rpc called from nfs_inactive
1624 */
1625 int
1626 nfs4_removeit(struct sillyrename *sp)
1627 {
1628 /*
1629 * Make sure that the directory vnode is still valid.
1630 * XXX we should lock sp->s_dvp here.
1631 */
1632 if (sp->s_dvp->v_type == VBAD)
1633 return (0);
1634 return (nfs4_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen, sp->s_cred,
1635 NULL));
1636 }
1637
1638 /*
1639 * Nfs remove rpc, called from nfs4_remove() and nfs4_removeit().
1640 */
1641 static int
1642 nfs4_removerpc(struct vnode *dvp, const char *name, int namelen,
1643 struct ucred *cred, struct thread *td)
1644 {
1645 caddr_t bpos, dpos;
1646 int error = 0;
1647 struct mbuf *mreq, *mrep = NULL, *md, *mb;
1648 struct nfs4_compound cp;
1649
1650 nfsstats.rpccnt[NFSPROC_REMOVE]++;
1651
1652 mreq = nfsm_reqhead(dvp, NFSV4PROC_COMPOUND, 0);
1653 mb = mreq;
1654 bpos = mtod(mb, caddr_t);
1655
1656 nfs_v4initcompound(&cp);
1657
1658 nfsm_v4build_compound(&cp, "nfs4_removerpc()");
1659 nfsm_v4build_putfh(&cp, dvp);
1660 nfsm_v4build_remove(&cp, name, namelen);
1661 nfsm_v4build_finalize(&cp);
1662
1663 nfsm_request(dvp, NFSV4PROC_COMPOUND, td, cred);
1664 if (error != 0)
1665 goto nfsmout;
1666
1667 nfsm_v4dissect_compound(&cp);
1668 nfsm_v4dissect_putfh(&cp);
1669 nfsm_v4dissect_remove(&cp);
1670
1671 nfsmout:
1672 error = nfs_v4postop(&cp, error);
1673
1674 if (mrep != NULL)
1675 m_freem(mrep);
1676
1677 VTONFS(dvp)->n_flag |= NMODIFIED;
1678 VTONFS(dvp)->n_attrstamp = 0; /* XXX wccflag */
1679
1680 return (error);
1681 }
1682
1683 /*
1684 * nfs file rename call
1685 */
1686 static int
1687 nfs4_rename(struct vop_rename_args *ap)
1688 {
1689 struct vnode *fvp = ap->a_fvp;
1690 struct vnode *tvp = ap->a_tvp;
1691 struct vnode *fdvp = ap->a_fdvp;
1692 struct vnode *tdvp = ap->a_tdvp;
1693 struct componentname *tcnp = ap->a_tcnp;
1694 struct componentname *fcnp = ap->a_fcnp;
1695 int error;
1696
1697 #ifndef DIAGNOSTIC
1698 if ((tcnp->cn_flags & HASBUF) == 0 ||
1699 (fcnp->cn_flags & HASBUF) == 0)
1700 panic("nfs4_rename: no name");
1701 #endif
1702 /* Check for cross-device rename */
1703 if ((fvp->v_mount != tdvp->v_mount) ||
1704 (tvp && (fvp->v_mount != tvp->v_mount))) {
1705 error = EXDEV;
1706 goto out;
1707 }
1708
1709 if (fvp == tvp) {
1710 printf("nfs4_rename: fvp == tvp (can't happen)\n");
1711 error = 0;
1712 goto out;
1713 }
1714 if ((error = vn_lock(fvp, LK_EXCLUSIVE, fcnp->cn_thread)) != 0)
1715 goto out;
1716
1717 /*
1718 * We have to flush B_DELWRI data prior to renaming
1719 * the file. If we don't, the delayed-write buffers
1720 * can be flushed out later after the file has gone stale
1721 * under NFSV3. NFSV2 does not have this problem because
1722 * ( as far as I can tell ) it flushes dirty buffers more
1723 * often.
1724 */
1725 VOP_FSYNC(fvp, MNT_WAIT, fcnp->cn_thread);
1726 VOP_UNLOCK(fvp, 0, fcnp->cn_thread);
1727 if (tvp)
1728 VOP_FSYNC(tvp, MNT_WAIT, tcnp->cn_thread);
1729
1730 /*
1731 * If the tvp exists and is in use, sillyrename it before doing the
1732 * rename of the new file over it.
1733 * XXX Can't sillyrename a directory.
1734 */
1735 if (tvp && vrefcnt(tvp) > 1 && !VTONFS(tvp)->n_sillyrename &&
1736 tvp->v_type != VDIR && !nfs4_sillyrename(tdvp, tvp, tcnp)) {
1737 vput(tvp);
1738 tvp = NULL;
1739 }
1740
1741 error = nfs4_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1742 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1743 tcnp->cn_thread);
1744
1745 if (fvp->v_type == VDIR) {
1746 if (tvp != NULL && tvp->v_type == VDIR)
1747 cache_purge(tdvp);
1748 cache_purge(fdvp);
1749 }
1750
1751 out:
1752 if (tdvp == tvp)
1753 vrele(tdvp);
1754 else
1755 vput(tdvp);
1756 if (tvp)
1757 vput(tvp);
1758 vrele(fdvp);
1759 vrele(fvp);
1760 /*
1761 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1762 */
1763 if (error == ENOENT)
1764 error = 0;
1765 return (error);
1766 }
1767
1768 /*
1769 * nfs file rename rpc called from nfs4_remove() above
1770 */
1771 static int
1772 nfs4_renameit(struct vnode *sdvp, struct componentname *scnp,
1773 struct sillyrename *sp)
1774 {
1775 return (nfs4_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen, sdvp,
1776 sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_thread));
1777 }
1778
1779 /*
1780 * nfs hard link create call
1781 */
1782 static int
1783 nfs4_link(struct vop_link_args *ap)
1784 {
1785 struct vnode *vp = ap->a_vp;
1786 struct vnode *tdvp = ap->a_tdvp;
1787 struct componentname *cnp = ap->a_cnp;
1788 caddr_t bpos, dpos;
1789 int error = 0;
1790 struct mbuf *mreq, *mrep = NULL, *md, *mb;
1791 struct nfs4_compound cp;
1792 struct nfs4_oparg_link l;
1793
1794 if (vp->v_mount != tdvp->v_mount) {
1795 return (EXDEV);
1796 }
1797
1798 /*
1799 * Push all writes to the server, so that the attribute cache
1800 * doesn't get "out of sync" with the server.
1801 * XXX There should be a better way!
1802 */
1803 VOP_FSYNC(vp, MNT_WAIT, cnp->cn_thread);
1804
1805 nfsstats.rpccnt[NFSPROC_LINK]++;
1806
1807 l.name = cnp->cn_nameptr;
1808 l.namelen = cnp->cn_namelen;
1809 nfs_v4initcompound(&cp);
1810
1811 mreq = nfsm_reqhead(vp, NFSV4PROC_COMPOUND, 0);
1812 mb = mreq;
1813 bpos = mtod(mb, caddr_t);
1814
1815 nfsm_v4build_compound(&cp, "nfs4_link()");
1816 nfsm_v4build_putfh(&cp, vp);
1817 nfsm_v4build_savefh(&cp);
1818 nfsm_v4build_putfh(&cp, tdvp);
1819 nfsm_v4build_link(&cp, &l);
1820 nfsm_v4build_finalize(&cp);
1821
1822 nfsm_request(vp, NFSV4PROC_COMPOUND, cnp->cn_thread, cnp->cn_cred);
1823 if (error != 0)
1824 goto nfsmout;
1825
1826 nfsm_v4dissect_compound(&cp);
1827 nfsm_v4dissect_putfh(&cp);
1828 nfsm_v4dissect_savefh(&cp);
1829 nfsm_v4dissect_putfh(&cp);
1830 nfsm_v4dissect_link(&cp);
1831
1832 VTONFS(tdvp)->n_flag |= NMODIFIED;
1833 VTONFS(vp)->n_attrstamp = 0;
1834 VTONFS(tdvp)->n_attrstamp = 0;
1835
1836 nfsmout:
1837 error = nfs_v4postop(&cp, error);
1838
1839 if (mrep != NULL)
1840 m_freem(mrep);
1841
1842 return (error);
1843 }
1844
1845 /*
1846 * nfs symbolic link create call
1847 */
1848 static int
1849 nfs4_symlink(struct vop_symlink_args *ap)
1850 {
1851 struct vnode *dvp = ap->a_dvp;
1852 int error = 0;
1853 struct vnode *newvp = NULL;
1854
1855 nfsstats.rpccnt[NFSPROC_SYMLINK]++;
1856
1857 error = nfs4_createrpc(ap->a_dvp, &newvp, ap->a_cnp, NFLNK,
1858 ap->a_vap, ap->a_target);
1859
1860 if (error != 0 && newvp != NULL)
1861 vput(newvp);
1862 else if (error == 0)
1863 *ap->a_vpp = newvp;
1864
1865 VTONFS(dvp)->n_flag |= NMODIFIED;
1866 VTONFS(dvp)->n_attrstamp = 0; /* XXX wccflags */
1867
1868 return (error);
1869 }
1870
1871 /*
1872 * nfs make dir call
1873 */
1874 static int
1875 nfs4_mkdir(struct vop_mkdir_args *ap)
1876 {
1877 return (nfs4_createrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, NFDIR,
1878 ap->a_vap, NULL));
1879 }
1880
1881 /*
1882 * nfs remove directory call
1883 */
1884 static int
1885 nfs4_rmdir(struct vop_rmdir_args *ap)
1886 {
1887 struct vnode *vp = ap->a_vp;
1888 struct vnode *dvp = ap->a_dvp;
1889 struct nfsnode *dnp = VTONFS(dvp);
1890 struct componentname *cnp = ap->a_cnp;
1891 int error = 0;
1892
1893 if (dvp == vp)
1894 return (EINVAL);
1895
1896 error = (nfs4_removerpc(dvp, cnp->cn_nameptr, cnp->cn_namelen, cnp->cn_cred,
1897 NULL));
1898 if (error)
1899 return (error);
1900
1901 dnp->n_flag |= NMODIFIED;
1902 dnp->n_attrstamp = 0;
1903 cache_purge(dvp);
1904 cache_purge(vp);
1905
1906 return (error);
1907 }
1908
1909 /*
1910 * nfs readdir call
1911 */
1912 static int
1913 nfs4_readdir(struct vop_readdir_args *ap)
1914 {
1915 struct vnode *vp = ap->a_vp;
1916 struct nfsnode *np = VTONFS(vp);
1917 struct uio *uio = ap->a_uio;
1918 int tresid, error;
1919 struct vattr vattr;
1920
1921 if (vp->v_type != VDIR)
1922 return (EPERM);
1923 /*
1924 * First, check for hit on the EOF offset cache
1925 */
1926 if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
1927 (np->n_flag & NMODIFIED) == 0) {
1928 if (VOP_GETATTR(vp, &vattr, ap->a_cred, uio->uio_td) == 0 &&
1929 !NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime)) {
1930 nfsstats.direofcache_hits++;
1931 return (0);
1932 }
1933 }
1934
1935 /*
1936 * Call nfs_bioread() to do the real work.
1937 */
1938 tresid = uio->uio_resid;
1939 error = nfs_bioread(vp, uio, 0, ap->a_cred);
1940
1941 if (!error && uio->uio_resid == tresid)
1942 nfsstats.direofcache_misses++;
1943 return (error);
1944 }
1945
1946 static u_char fty_to_dty[] = {
1947 DT_UNKNOWN, /* NFNON */
1948 DT_REG, /* NFREG */
1949 DT_DIR, /* NFDIR */
1950 DT_BLK, /* NFBLK */
1951 DT_CHR, /* NFCHR */
1952 DT_LNK, /* NFLNK */
1953 DT_SOCK, /* NFSOCK */
1954 DT_FIFO, /* NFFIFO */
1955 DT_UNKNOWN, /* NFATTRDIT */
1956 DT_UNKNOWN, /* NFNAMEDATTR */
1957 DT_UNKNOWN, /* NFBAD */
1958 };
1959
1960 /*
1961 * Readdir rpc call.
1962 * Called from below the buffer cache by nfs_doio().
1963 */
1964 int
1965 nfs4_readdirrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1966 {
1967 int len, left;
1968 struct dirent *dp = NULL;
1969 u_int32_t *tl;
1970 caddr_t p;
1971 uint64_t *cookiep;
1972 caddr_t bpos, dpos;
1973 struct mbuf *mreq, *mrep = NULL, *md, *mb;
1974 uint64_t cookie;
1975 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1976 struct nfsnode *dnp = VTONFS(vp);
1977 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
1978 struct nfs4_compound cp;
1979 struct nfs4_oparg_readdir readdir;
1980 struct nfsv4_fattr fattr;
1981 u_int fty;
1982
1983 #ifndef DIAGNOSTIC
1984 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
1985 (uiop->uio_resid & (DIRBLKSIZ - 1)))
1986 panic("nfs readdirrpc bad uio");
1987 #endif
1988
1989 /*
1990 * If there is no cookie, assume directory was stale.
1991 */
1992 cookiep = nfs4_getcookie(dnp, uiop->uio_offset, 0);
1993 if (cookiep)
1994 cookie = *cookiep;
1995 else
1996 return (NFSERR_BAD_COOKIE);
1997
1998 /* Generate fake entries for "." and ".." */
1999 while (cookie < 2 && bigenough) {
2000 cookie++;
2001 len = 4 + DIRHDSIZ;
2002
2003 if (len > uiop->uio_resid) {
2004 bigenough = 0;
2005 break;
2006 }
2007 dp = (struct dirent *)uiop->uio_iov->iov_base;
2008
2009 dp->d_namlen = cookie;
2010 dp->d_reclen = len;
2011 dp->d_type = DT_DIR;
2012 if (cookie == 1)
2013 dp->d_fileno = dnp->n_vattr.va_fileid; /* XXX has problems with pynfs virtualhandles */
2014 else
2015 dp->d_fileno = dnp->n_dvp != NULL ?
2016 VTONFS(dnp->n_dvp)->n_vattr.va_fileid : cookie;
2017
2018 p = dp->d_name;
2019 *p++ = '.';
2020 if (cookie == 2)
2021 *p++ = '.';
2022 *p = '\0';
2023
2024 blksiz += len;
2025 if (blksiz == DIRBLKSIZ)
2026 blksiz = 0;
2027 uiop->uio_offset += len;
2028 uiop->uio_resid -= len;
2029 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + len;
2030 uiop->uio_iov->iov_len -= len;
2031 }
2032
2033 if (cookie == 2)
2034 cookie = 0;
2035
2036 /* This is sort of ugly, to prevent v4postop() from acting weird */
2037 bzero(&cp, sizeof(cp));
2038
2039 /*
2040 * Loop around doing readdir rpc's of size nm_readdirsize
2041 * truncated to a multiple of DIRBLKSIZ.
2042 * The stopping criteria is EOF or buffer full.
2043 */
2044 /*
2045 * XXX this is sort of ugly for nfsv4; we don't maintain the
2046 * strict abstraction, but do the decoding inline. that's ok.
2047 */
2048 while (more_dirs && bigenough) {
2049 nfsstats.rpccnt[NFSPROC_READDIR]++;
2050
2051 mreq = nfsm_reqhead(vp, NFSV4PROC_COMPOUND, 0);
2052 mb = mreq;
2053 bpos = mtod(mb, caddr_t);
2054
2055 readdir.cnt = nmp->nm_readdirsize;
2056 readdir.cookie = cookie;
2057 readdir.bm = &nfsv4_readdirbm;
2058 if (cookie == 0)
2059 bzero(&readdir.verf, sizeof(readdir.verf));
2060 else
2061 bcopy(&dnp->n_cookieverf, &readdir.verf,
2062 sizeof(readdir.verf));
2063
2064 nfs_v4initcompound(&cp);
2065
2066 nfsm_v4build_compound(&cp, "nfs4_readdirrpc()");
2067 nfsm_v4build_putfh(&cp, vp);
2068 nfsm_v4build_readdir(&cp, &readdir);
2069 nfsm_v4build_finalize(&cp);
2070
2071 nfsm_request(vp, NFSV4PROC_COMPOUND, uiop->uio_td, cred);
2072 if (error != 0)
2073 goto nfsmout;
2074
2075 nfsm_v4dissect_compound(&cp);
2076 nfsm_v4dissect_putfh(&cp);
2077
2078 /*
2079 * XXX - Readdir gets handled inline like in
2080 * NFSv{2,3}. This is a nasty inconsistency and
2081 * should be fixed.
2082 */
2083
2084 tl = nfsm_dissect(uint32_t *, 5 * NFSX_UNSIGNED);
2085 if (fxdr_unsigned(uint32_t, *tl++) != NFSV4OP_READDIR) {
2086 error = EBADRPC;
2087 goto nfsmout;
2088 }
2089 if (fxdr_unsigned(uint32_t, *tl++) != 0) {
2090 error = EBADRPC;
2091 goto nfsmout;
2092 }
2093
2094 bcopy(tl, &dnp->n_cookieverf, NFSX_V4VERF);
2095 tl += 2;
2096 more_dirs = fxdr_unsigned(int, *tl++);
2097
2098 /* loop thru the dir entries, doctoring them to 4bsd form */
2099 while (more_dirs && bigenough) {
2100 tl = nfsm_dissect(uint32_t *, 3 * NFSX_UNSIGNED);
2101 cookie = fxdr_hyper(tl);
2102 tl += 2;
2103 /* XXX cookie sanity check */
2104 len = fxdr_unsigned(int, *tl++);
2105 if (len <= 0 || len > NFS_MAXNAMLEN) {
2106 error = EBADRPC;
2107 goto nfsmout;
2108 }
2109 tlen = nfsm_rndup(len);
2110 if (tlen == len)
2111 tlen += 4; /* To ensure null termination */
2112 left = DIRBLKSIZ - blksiz;
2113 if ((tlen + DIRHDSIZ) > left) {
2114 dp->d_reclen += left;
2115 uiop->uio_iov->iov_base =
2116 (char *)uiop->uio_iov->iov_base + left;
2117 uiop->uio_iov->iov_len -= left;
2118 uiop->uio_offset += left;
2119 uiop->uio_resid -= left;
2120 blksiz = 0;
2121 }
2122 if ((tlen + DIRHDSIZ) > uiop->uio_resid)
2123 bigenough = 0;
2124 if (bigenough) {
2125 dp = (struct dirent *)uiop->uio_iov->iov_base;
2126
2127 dp->d_namlen = len;
2128 dp->d_reclen = tlen + DIRHDSIZ;
2129
2130 blksiz += dp->d_reclen;
2131 if (blksiz == DIRBLKSIZ)
2132 blksiz = 0;
2133 uiop->uio_offset += DIRHDSIZ;
2134 uiop->uio_resid -= DIRHDSIZ;
2135 uiop->uio_iov->iov_base =
2136 (char *)uiop->uio_iov->iov_base + DIRHDSIZ;
2137 uiop->uio_iov->iov_len -= DIRHDSIZ;
2138
2139 /* Copy name */
2140 nfsm_mtouio(uiop, len);
2141 p = uiop->uio_iov->iov_base;
2142 tlen -= len;
2143 *p = '\0'; /* null terminate */
2144 /* printf("nfs4_readdirrpc: name: \"%s\" cookie %d\n",
2145 p - len, (int) cookie);*/
2146 uiop->uio_iov->iov_base =
2147 (char *)uiop->uio_iov->iov_base + tlen;
2148 uiop->uio_iov->iov_len -= tlen;
2149 uiop->uio_offset += tlen;
2150 uiop->uio_resid -= tlen;
2151
2152 /* Copy attributes */
2153 nfsm_v4dissect_attrs(&fattr);
2154
2155 dp->d_fileno = nfs_v4fileid4_to_fileid(
2156 fattr.fa4_valid & FA4V_FILEID &&
2157 fattr.fa4_fileid ?
2158 fattr.fa4_fileid : cookie);
2159
2160 fty = (u_int)fattr.fa4_type;
2161 dp->d_type = fattr.fa4_valid & FA4V_TYPE &&
2162 (fty < sizeof(fty_to_dty)) ?
2163 fty_to_dty[fty] : DT_UNKNOWN;
2164 } else
2165 nfsm_adv(nfsm_rndup(len));
2166
2167 tl = nfsm_dissect(uint32_t *, NFSX_UNSIGNED);
2168 more_dirs = fxdr_unsigned(int, *tl++);
2169 }
2170 /*
2171 * If at end of rpc data, get the eof boolean
2172 */
2173 if (!more_dirs) {
2174 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2175 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2176 }
2177
2178 error = nfs_v4postop(&cp, error);
2179
2180 m_freem(mrep);
2181 mrep = NULL;
2182 }
2183 /*
2184 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2185 * by increasing d_reclen for the last record.
2186 */
2187 if (blksiz > 0) {
2188 left = DIRBLKSIZ - blksiz;
2189 dp->d_reclen += left;
2190 uiop->uio_iov->iov_base =
2191 (char *)uiop->uio_iov->iov_base + left;
2192 uiop->uio_iov->iov_len -= left;
2193 uiop->uio_offset += left;
2194 uiop->uio_resid -= left;
2195 }
2196
2197 /*
2198 * We are now either at the end of the directory or have filled the
2199 * block.
2200 */
2201 if (bigenough)
2202 dnp->n_direofoffset = uiop->uio_offset;
2203 else {
2204 if (uiop->uio_resid > 0)
2205 printf("EEK! readdirrpc resid > 0\n");
2206 cookiep = nfs4_getcookie(dnp, uiop->uio_offset, 1);
2207 *cookiep = cookie;
2208 }
2209 nfsmout:
2210 if (mrep != NULL)
2211 m_freem(mrep);
2212 return (error);
2213 }
2214
2215 /*
2216 * Silly rename. To make the NFS filesystem that is stateless look a little
2217 * more like the "ufs" a remove of an active vnode is translated to a rename
2218 * to a funny looking filename that is removed by nfs_inactive on the
2219 * nfsnode. There is the potential for another process on a different client
2220 * to create the same funny name between the nfs_lookitup() fails and the
2221 * nfs_rename() completes, but...
2222 */
2223 static int
2224 nfs4_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2225 {
2226 struct sillyrename *sp;
2227 struct nfsnode *np;
2228 int error;
2229 short pid;
2230
2231 cache_purge(dvp);
2232 np = VTONFS(vp);
2233 #ifndef DIAGNOSTIC
2234 if (vp->v_type == VDIR)
2235 panic("nfs: sillyrename dir");
2236 #endif
2237 MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename),
2238 M_NFSREQ, M_WAITOK);
2239 sp->s_cred = crhold(cnp->cn_cred);
2240 sp->s_dvp = dvp;
2241 sp->s_removeit = nfs4_removeit;
2242 VREF(dvp);
2243
2244 /* Fudge together a funny name */
2245 pid = cnp->cn_thread->td_proc->p_pid;
2246 sp->s_namlen = sprintf(sp->s_name, ".nfsA%04x4.4", pid);
2247
2248 /* Try lookitups until we get one that isn't there */
2249 while (nfs4_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2250 cnp->cn_thread, NULL) == 0) {
2251 sp->s_name[4]++;
2252 if (sp->s_name[4] > 'z') {
2253 error = EINVAL;
2254 goto bad;
2255 }
2256 }
2257 error = nfs4_renameit(dvp, cnp, sp);
2258 if (error)
2259 goto bad;
2260 error = nfs4_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2261 cnp->cn_thread, &np);
2262 np->n_sillyrename = sp;
2263 return (0);
2264 bad:
2265 vrele(sp->s_dvp);
2266 crfree(sp->s_cred);
2267 free((caddr_t)sp, M_NFSREQ);
2268 return (error);
2269 }
2270
2271 /*
2272 * Look up a file name and optionally either update the file handle or
2273 * allocate an nfsnode, depending on the value of npp.
2274 * npp == NULL --> just do the lookup
2275 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2276 * handled too
2277 * *npp != NULL --> update the file handle in the vnode
2278 */
2279 static int
2280 nfs4_lookitup(struct vnode *dvp, const char *name, int len, struct ucred *cred,
2281 struct thread *td, struct nfsnode **npp)
2282 {
2283 struct vnode *newvp = NULL;
2284 struct nfsnode *np, *dnp = VTONFS(dvp);
2285 caddr_t bpos, dpos;
2286 int error = 0, fhlen;
2287 struct mbuf *mreq, *mrep = NULL, *md, *mb;
2288 nfsfh_t *nfhp;
2289 struct nfs4_compound cp;
2290 struct nfs4_oparg_lookup l;
2291 struct nfs4_oparg_getfh gfh;
2292 struct nfs4_oparg_getattr ga;
2293
2294 nfsstats.rpccnt[NFSPROC_RENAME]++;
2295
2296 mreq = nfsm_reqhead(dvp, NFSV4PROC_COMPOUND, 0);
2297 mb = mreq;
2298 bpos = mtod(mb, caddr_t);
2299
2300 l.name = name;
2301 l.namelen = len;
2302
2303 nfs_v4initcompound(&cp);
2304
2305 ga.bm = &nfsv4_getattrbm;
2306
2307 nfsm_v4build_compound(&cp, "nfs4_renamerpc()");
2308 nfsm_v4build_putfh(&cp, dvp);
2309 nfsm_v4build_lookup(&cp, &l);
2310 nfsm_v4build_getfh(&cp, &gfh);
2311 nfsm_v4build_getattr(&cp, &ga);
2312
2313 nfsm_request(dvp, NFSV4PROC_COMPOUND, td, cred);
2314 if (error != 0)
2315 goto nfsmout;
2316
2317 nfsm_v4dissect_compound(&cp);
2318 nfsm_v4dissect_putfh(&cp);
2319 nfsm_v4dissect_lookup(&cp);
2320 nfsm_v4dissect_getfh(&cp, &gfh);
2321 nfsm_v4dissect_getattr(&cp, &ga);
2322
2323 if (npp != NULL && error == 0) {
2324 nfhp = &gfh.fh_val;
2325 fhlen = gfh.fh_len;
2326
2327 if (*npp != NULL) {
2328 np = *npp;
2329 if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
2330 free((caddr_t)np->n_fhp, M_NFSBIGFH);
2331 np->n_fhp = &np->n_fh;
2332 } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH)
2333 np->n_fhp =(nfsfh_t *)malloc(fhlen, M_NFSBIGFH, M_WAITOK);
2334 bcopy((caddr_t)nfhp, (caddr_t)np->n_fhp, fhlen);
2335 np->n_fhsize = fhlen;
2336 newvp = NFSTOV(np);
2337 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
2338 VREF(dvp);
2339 newvp = dvp;
2340 } else {
2341 error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np, LK_EXCLUSIVE);
2342 if (error) {
2343 m_freem(mrep);
2344 return (error);
2345 }
2346 newvp = NFSTOV(np);
2347 }
2348
2349 if (newvp != dvp) {
2350 np->n_dvp = dvp;
2351 np->n_namelen = len;
2352 if (np->n_name != NULL)
2353 FREE(np->n_name, M_NFSREQ);
2354 MALLOC(np->n_name, u_char *,
2355 np->n_namelen + 1, M_NFSREQ, M_WAITOK);
2356 memcpy(np->n_name, name, len);
2357 np->n_name[len] = '\0';
2358 }
2359 nfs4_vnop_loadattrcache(newvp, &ga.fa, NULL);
2360 }
2361
2362 nfsmout:
2363 error = nfs_v4postop(&cp, error);
2364
2365 if (mrep != NULL)
2366 m_freem(mrep);
2367 if (npp && *npp == NULL) {
2368 if (error) {
2369 if (newvp) {
2370 if (newvp == dvp)
2371 vrele(newvp);
2372 else
2373 vput(newvp);
2374 }
2375 } else
2376 *npp = np;
2377 }
2378
2379
2380 return (error);
2381 }
2382
2383 /*
2384 * Nfs Version 3 commit rpc
2385 */
2386 int
2387 nfs4_commit(struct vnode *vp, u_quad_t offset, int cnt, struct ucred *cred,
2388 struct thread *td)
2389 {
2390 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2391 caddr_t bpos, dpos;
2392 int error = 0;
2393 struct mbuf *mreq, *mrep = NULL, *md, *mb;
2394 struct nfs4_compound cp;
2395 struct nfs4_oparg_commit commit;
2396
2397 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0)
2398 return (0);
2399 nfsstats.rpccnt[NFSPROC_COMMIT]++;
2400
2401 mreq = nfsm_reqhead(vp, NFSV4PROC_COMPOUND, 0);
2402 mb = mreq;
2403 bpos = mtod(mb, caddr_t);
2404
2405 commit.start = offset;
2406 commit.len = cnt;
2407
2408 nfs_v4initcompound(&cp);
2409
2410 nfsm_v4build_compound(&cp, "nfs4_commit()");
2411 nfsm_v4build_putfh(&cp, vp);
2412 nfsm_v4build_commit(&cp, &commit);
2413 nfsm_v4build_finalize(&cp);
2414
2415 nfsm_request(vp, NFSV4PROC_COMPOUND, td, cred);
2416 if (error != 0)
2417 goto nfsmout;
2418
2419 nfsm_v4dissect_compound(&cp);
2420 nfsm_v4dissect_putfh(&cp);
2421 nfsm_v4dissect_commit(&cp, &commit);
2422
2423 /* XXX */
2424 /* nfsm_wcc_data(vp, wccflag);*/
2425 if (bcmp(nmp->nm_verf, commit.verf, NFSX_V4VERF)) {
2426 bcopy(commit.verf, nmp->nm_verf, NFSX_V4VERF);
2427 error = NFSERR_STALEWRITEVERF;
2428 }
2429
2430 nfsmout:
2431 error = nfs_v4postop(&cp, error);
2432
2433 if (mrep == NULL)
2434 m_freem(mrep);
2435 return (error);
2436 }
2437
2438 /*
2439 * Strategy routine.
2440 * For async requests when nfsiod(s) are running, queue the request by
2441 * calling nfs_asyncio(), otherwise just all nfs_doio() to do the
2442 * request.
2443 */
2444 static int
2445 nfs4_strategy(struct vop_strategy_args *ap)
2446 {
2447 struct buf *bp = ap->a_bp;
2448 struct ucred *cr;
2449 int error = 0;
2450
2451 KASSERT(!(bp->b_flags & B_DONE), ("nfs4_strategy: buffer %p unexpectedly marked B_DONE", bp));
2452 KASSERT(BUF_REFCNT(bp) > 0, ("nfs4_strategy: buffer %p not locked", bp));
2453
2454 if (bp->b_iocmd == BIO_READ)
2455 cr = bp->b_rcred;
2456 else
2457 cr = bp->b_wcred;
2458
2459 /*
2460 * If the op is asynchronous and an i/o daemon is waiting
2461 * queue the request, wake it up and wait for completion
2462 * otherwise just do it ourselves.
2463 */
2464 if ((bp->b_flags & B_ASYNC) == 0 ||
2465 nfs_asyncio(VFSTONFS(ap->a_vp->v_mount), bp, NOCRED, curthread))
2466 error = nfs_doio(ap->a_vp, bp, cr, curthread);
2467 return (error);
2468 }
2469
2470 /*
2471 * fsync vnode op. Just call nfs4_flush() with commit == 1.
2472 */
2473 /* ARGSUSED */
2474 static int
2475 nfs4_fsync(struct vop_fsync_args *ap)
2476 {
2477 return (nfs4_flush(ap->a_vp, ap->a_waitfor, ap->a_td, 1));
2478 }
2479
2480 /*
2481 * Flush all the blocks associated with a vnode.
2482 * Walk through the buffer pool and push any dirty pages
2483 * associated with the vnode.
2484 */
2485 static int
2486 nfs4_flush(struct vnode *vp, int waitfor, struct thread *td,
2487 int commit)
2488 {
2489 struct nfsnode *np = VTONFS(vp);
2490 struct buf *bp;
2491 int i;
2492 struct buf *nbp;
2493 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2494 int s, error = 0, slptimeo = 0, slpflag = 0, retv, bvecpos;
2495 int passone = 1;
2496 u_quad_t off, endoff, toff;
2497 struct ucred* wcred = NULL;
2498 struct buf **bvec = NULL;
2499 #ifndef NFS_COMMITBVECSIZ
2500 #define NFS_COMMITBVECSIZ 20
2501 #endif
2502 struct buf *bvec_on_stack[NFS_COMMITBVECSIZ];
2503 int bvecsize = 0, bveccount;
2504
2505 if (nmp->nm_flag & NFSMNT_INT)
2506 slpflag = PCATCH;
2507 if (!commit)
2508 passone = 0;
2509 /*
2510 * A b_flags == (B_DELWRI | B_NEEDCOMMIT) block has been written to the
2511 * server, but nas not been committed to stable storage on the server
2512 * yet. On the first pass, the byte range is worked out and the commit
2513 * rpc is done. On the second pass, nfs_writebp() is called to do the
2514 * job.
2515 */
2516 again:
2517 off = (u_quad_t)-1;
2518 endoff = 0;
2519 bvecpos = 0;
2520 if (NFS_ISV3(vp) && commit) {
2521 s = splbio();
2522 if (bvec != NULL && bvec != bvec_on_stack)
2523 free(bvec, M_TEMP);
2524 /*
2525 * Count up how many buffers waiting for a commit.
2526 */
2527 bveccount = 0;
2528 VI_LOCK(vp);
2529 TAILQ_FOREACH_SAFE(bp, &vp->v_bufobj.bo_dirty.bv_hd, b_bobufs, nbp) {
2530 if (BUF_REFCNT(bp) == 0 &&
2531 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
2532 == (B_DELWRI | B_NEEDCOMMIT))
2533 bveccount++;
2534 }
2535 /*
2536 * Allocate space to remember the list of bufs to commit. It is
2537 * important to use M_NOWAIT here to avoid a race with nfs4_write.
2538 * If we can't get memory (for whatever reason), we will end up
2539 * committing the buffers one-by-one in the loop below.
2540 */
2541 if (bveccount > NFS_COMMITBVECSIZ) {
2542 /*
2543 * Release the vnode interlock to avoid a lock
2544 * order reversal.
2545 */
2546 VI_UNLOCK(vp);
2547 bvec = (struct buf **)
2548 malloc(bveccount * sizeof(struct buf *),
2549 M_TEMP, M_NOWAIT);
2550 VI_LOCK(vp);
2551 if (bvec == NULL) {
2552 bvec = bvec_on_stack;
2553 bvecsize = NFS_COMMITBVECSIZ;
2554 } else
2555 bvecsize = bveccount;
2556 } else {
2557 bvec = bvec_on_stack;
2558 bvecsize = NFS_COMMITBVECSIZ;
2559 }
2560 TAILQ_FOREACH_SAFE(bp, &vp->v_bufobj.bo_dirty.bv_hd, b_bobufs, nbp) {
2561 if (bvecpos >= bvecsize)
2562 break;
2563 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
2564 nbp = TAILQ_NEXT(bp, b_bobufs);
2565 continue;
2566 }
2567 if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
2568 (B_DELWRI | B_NEEDCOMMIT)) {
2569 BUF_UNLOCK(bp);
2570 nbp = TAILQ_NEXT(bp, b_bobufs);
2571 continue;
2572 }
2573 VI_UNLOCK(vp);
2574 bremfree(bp);
2575 /*
2576 * Work out if all buffers are using the same cred
2577 * so we can deal with them all with one commit.
2578 *
2579 * NOTE: we are not clearing B_DONE here, so we have
2580 * to do it later on in this routine if we intend to
2581 * initiate I/O on the bp.
2582 *
2583 * Note: to avoid loopback deadlocks, we do not
2584 * assign b_runningbufspace.
2585 */
2586 if (wcred == NULL)
2587 wcred = bp->b_wcred;
2588 else if (wcred != bp->b_wcred)
2589 wcred = NOCRED;
2590 vfs_busy_pages(bp, 1);
2591
2592 VI_LOCK(vp);
2593 /*
2594 * bp is protected by being locked, but nbp is not
2595 * and vfs_busy_pages() may sleep. We have to
2596 * recalculate nbp.
2597 */
2598 nbp = TAILQ_NEXT(bp, b_bobufs);
2599
2600 /*
2601 * A list of these buffers is kept so that the
2602 * second loop knows which buffers have actually
2603 * been committed. This is necessary, since there
2604 * may be a race between the commit rpc and new
2605 * uncommitted writes on the file.
2606 */
2607 bvec[bvecpos++] = bp;
2608 toff = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2609 bp->b_dirtyoff;
2610 if (toff < off)
2611 off = toff;
2612 toff += (u_quad_t)(bp->b_dirtyend - bp->b_dirtyoff);
2613 if (toff > endoff)
2614 endoff = toff;
2615 }
2616 splx(s);
2617 VI_UNLOCK(vp);
2618 }
2619 if (bvecpos > 0) {
2620 /*
2621 * Commit data on the server, as required.
2622 * If all bufs are using the same wcred, then use that with
2623 * one call for all of them, otherwise commit each one
2624 * separately.
2625 */
2626 if (wcred != NOCRED)
2627 retv = nfs4_commit(vp, off, (int)(endoff - off),
2628 wcred, td);
2629 else {
2630 retv = 0;
2631 for (i = 0; i < bvecpos; i++) {
2632 off_t off, size;
2633 bp = bvec[i];
2634 off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2635 bp->b_dirtyoff;
2636 size = (u_quad_t)(bp->b_dirtyend
2637 - bp->b_dirtyoff);
2638 retv = nfs4_commit(vp, off, (int)size,
2639 bp->b_wcred, td);
2640 if (retv) break;
2641 }
2642 }
2643
2644 if (retv == NFSERR_STALEWRITEVERF)
2645 nfs_clearcommit(vp->v_mount);
2646
2647 /*
2648 * Now, either mark the blocks I/O done or mark the
2649 * blocks dirty, depending on whether the commit
2650 * succeeded.
2651 */
2652 for (i = 0; i < bvecpos; i++) {
2653 bp = bvec[i];
2654 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
2655 if (retv) {
2656 /*
2657 * Error, leave B_DELWRI intact
2658 */
2659 vfs_unbusy_pages(bp);
2660 brelse(bp);
2661 } else {
2662 /*
2663 * Success, remove B_DELWRI ( bundirty() ).
2664 *
2665 * b_dirtyoff/b_dirtyend seem to be NFS
2666 * specific. We should probably move that
2667 * into bundirty(). XXX
2668 */
2669 s = splbio();
2670 bufobj_wref(&vp->v_bufobj);
2671 bp->b_flags |= B_ASYNC;
2672 bundirty(bp);
2673 bp->b_flags &= ~B_DONE;
2674 bp->b_ioflags &= ~BIO_ERROR;
2675 bp->b_dirtyoff = bp->b_dirtyend = 0;
2676 splx(s);
2677 bufdone(bp);
2678 }
2679 }
2680 }
2681
2682 /*
2683 * Start/do any write(s) that are required.
2684 */
2685 loop:
2686 s = splbio();
2687 VI_LOCK(vp);
2688 TAILQ_FOREACH_SAFE(bp, &vp->v_bufobj.bo_dirty.bv_hd, b_bobufs, nbp) {
2689 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
2690 if (waitfor != MNT_WAIT || passone)
2691 continue;
2692
2693 error = BUF_TIMELOCK(bp,
2694 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
2695 VI_MTX(vp), "nfsfsync", slpflag, slptimeo);
2696 splx(s);
2697 if (error == 0)
2698 panic("nfs4_fsync: inconsistent lock");
2699 if (error == ENOLCK)
2700 goto loop;
2701 if (nfs4_sigintr(nmp, NULL, td)) {
2702 error = EINTR;
2703 goto done;
2704 }
2705 if (slpflag == PCATCH) {
2706 slpflag = 0;
2707 slptimeo = 2 * hz;
2708 }
2709 goto loop;
2710 }
2711 if ((bp->b_flags & B_DELWRI) == 0)
2712 panic("nfs4_fsync: not dirty");
2713 if ((passone || !commit) && (bp->b_flags & B_NEEDCOMMIT)) {
2714 BUF_UNLOCK(bp);
2715 continue;
2716 }
2717 VI_UNLOCK(vp);
2718 bremfree(bp);
2719 if (passone || !commit)
2720 bp->b_flags |= B_ASYNC;
2721 else
2722 bp->b_flags |= B_ASYNC;
2723 splx(s);
2724 bwrite(bp);
2725 goto loop;
2726 }
2727 splx(s);
2728 if (passone) {
2729 passone = 0;
2730 VI_UNLOCK(vp);
2731 goto again;
2732 }
2733 if (waitfor == MNT_WAIT) {
2734 while (vp->v_bufobj.bo_numoutput) {
2735 error = bufobj_wwait(&vp->v_bufobj, slpflag, slptimeo);
2736 if (error) {
2737 VI_UNLOCK(vp);
2738 if (nfs4_sigintr(nmp, NULL, td)) {
2739 error = EINTR;
2740 goto done;
2741 }
2742 if (slpflag == PCATCH) {
2743 slpflag = 0;
2744 slptimeo = 2 * hz;
2745 }
2746 VI_LOCK(vp);
2747 }
2748 }
2749 if (vp->v_bufobj.bo_dirty.bv_cnt > 0 && commit) {
2750 VI_UNLOCK(vp);
2751 goto loop;
2752 }
2753 }
2754 VI_UNLOCK(vp);
2755 if (np->n_flag & NWRITEERR) {
2756 error = np->n_error;
2757 np->n_flag &= ~NWRITEERR;
2758 }
2759 done:
2760 if (bvec != NULL && bvec != bvec_on_stack)
2761 free(bvec, M_TEMP);
2762 return (error);
2763 }
2764
2765 /*
2766 * NFS advisory byte-level locks.
2767 */
2768 static int
2769 nfs4_advlock(struct vop_advlock_args *ap)
2770 {
2771 return (EPERM);
2772
2773 if ((VFSTONFS(ap->a_vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) {
2774 struct nfsnode *np = VTONFS(ap->a_vp);
2775
2776 return (lf_advlock(ap, &(np->n_lockf), np->n_size));
2777 }
2778 return (nfs_dolock(ap));
2779 }
2780
2781 /*
2782 * NFS advisory byte-level locks.
2783 */
2784 static int
2785 nfs4_advlockasync(struct vop_advlockasync_args *ap)
2786 {
2787 return (EPERM);
2788
2789 if ((VFSTONFS(ap->a_vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) {
2790 struct nfsnode *np = VTONFS(ap->a_vp);
2791
2792 return (lf_advlockasync(ap, &(np->n_lockf), np->n_size));
2793 }
2794 return (EOPNOTSUPP);
2795 }
2796
2797 /*
2798 * Print out the contents of an nfsnode.
2799 */
2800 static int
2801 nfs4_print(struct vop_print_args *ap)
2802 {
2803 struct vnode *vp = ap->a_vp;
2804 struct nfsnode *np = VTONFS(vp);
2805
2806 printf("\tfileid %ld fsid 0x%x",
2807 np->n_vattr.va_fileid, np->n_vattr.va_fsid);
2808 if (vp->v_type == VFIFO)
2809 fifo_printinfo(vp);
2810 printf("\n");
2811 return (0);
2812 }
2813
2814 /*
2815 * This is the "real" nfs::bwrite(struct buf*).
2816 * We set B_CACHE if this is a VMIO buffer.
2817 */
2818 int
2819 nfs4_writebp(struct buf *bp, int force __unused, struct thread *td)
2820 {
2821 int s;
2822 int oldflags = bp->b_flags;
2823 #if 0
2824 int retv = 1;
2825 off_t off;
2826 #endif
2827
2828 if (BUF_REFCNT(bp) == 0)
2829 panic("bwrite: buffer is not locked???");
2830
2831 if (bp->b_flags & B_INVAL) {
2832 brelse(bp);
2833 return(0);
2834 }
2835
2836 bp->b_flags |= B_CACHE;
2837
2838 /*
2839 * Undirty the bp. We will redirty it later if the I/O fails.
2840 */
2841
2842 s = splbio();
2843 bundirty(bp);
2844 bp->b_flags &= ~B_DONE;
2845 bp->b_ioflags &= ~BIO_ERROR;
2846 bp->b_iocmd = BIO_WRITE;
2847
2848 bufobj_wref(bp->b_bufobj);
2849 curthread->td_proc->p_stats->p_ru.ru_oublock++;
2850 splx(s);
2851
2852 /*
2853 * Note: to avoid loopback deadlocks, we do not
2854 * assign b_runningbufspace.
2855 */
2856 vfs_busy_pages(bp, 1);
2857
2858 BUF_KERNPROC(bp);
2859 bp->b_iooffset = dbtob(bp->b_blkno);
2860 bstrategy(bp);
2861
2862 if( (oldflags & B_ASYNC) == 0) {
2863 int rtval = bufwait(bp);
2864
2865 if (oldflags & B_DELWRI) {
2866 s = splbio();
2867 reassignbuf(bp);
2868 splx(s);
2869 }
2870
2871 brelse(bp);
2872 return (rtval);
2873 }
2874
2875 return (0);
2876 }
2877
2878 /*
2879 * Just call nfs_writebp() with the force argument set to 1.
2880 *
2881 * NOTE: B_DONE may or may not be set in a_bp on call.
2882 */
2883 static int
2884 nfs4_bwrite(struct buf *bp)
2885 {
2886
2887 return (nfs4_writebp(bp, 1, curthread));
2888 }
2889
2890 struct buf_ops buf_ops_nfs4 = {
2891 .bop_name = "buf_ops_nfs4",
2892 .bop_write = nfs4_bwrite,
2893 .bop_strategy = bufstrategy,
2894 .bop_sync = bufsync,
2895 .bop_bdflush = bufbdflush,
2896 };
Cache object: 1c7c91dbac31b1f22f4597d8ba91bb9c
|