nfs_nfsdport.c revision 1.1 1 1.1 dholland /* $NetBSD: nfs_nfsdport.c,v 1.1 2013/09/30 07:19:57 dholland Exp $ */
2 1.1 dholland /*-
3 1.1 dholland * Copyright (c) 1989, 1993
4 1.1 dholland * The Regents of the University of California. All rights reserved.
5 1.1 dholland *
6 1.1 dholland * This code is derived from software contributed to Berkeley by
7 1.1 dholland * Rick Macklem at The University of Guelph.
8 1.1 dholland *
9 1.1 dholland * Redistribution and use in source and binary forms, with or without
10 1.1 dholland * modification, are permitted provided that the following conditions
11 1.1 dholland * are met:
12 1.1 dholland * 1. Redistributions of source code must retain the above copyright
13 1.1 dholland * notice, this list of conditions and the following disclaimer.
14 1.1 dholland * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 dholland * notice, this list of conditions and the following disclaimer in the
16 1.1 dholland * documentation and/or other materials provided with the distribution.
17 1.1 dholland * 4. Neither the name of the University nor the names of its contributors
18 1.1 dholland * may be used to endorse or promote products derived from this software
19 1.1 dholland * without specific prior written permission.
20 1.1 dholland *
21 1.1 dholland * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.1 dholland * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 dholland * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 dholland * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.1 dholland * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 dholland * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 dholland * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 dholland * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 dholland * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 dholland * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 dholland * SUCH DAMAGE.
32 1.1 dholland *
33 1.1 dholland */
34 1.1 dholland
35 1.1 dholland #include <sys/cdefs.h>
36 1.1 dholland /* __FBSDID("FreeBSD: head/sys/fs/nfsserver/nfs_nfsdport.c 255219 2013-09-05 00:09:56Z pjd "); */
37 1.1 dholland __RCSID("$NetBSD: nfs_nfsdport.c,v 1.1 2013/09/30 07:19:57 dholland Exp $");
38 1.1 dholland
39 1.1 dholland #include <sys/capability.h>
40 1.1 dholland
41 1.1 dholland /*
42 1.1 dholland * Functions that perform the vfs operations required by the routines in
43 1.1 dholland * nfsd_serv.c. It is hoped that this change will make the server more
44 1.1 dholland * portable.
45 1.1 dholland */
46 1.1 dholland
47 1.1 dholland #include <fs/nfs/nfsport.h>
48 1.1 dholland #include <sys/hash.h>
49 1.1 dholland #include <sys/sysctl.h>
50 1.1 dholland #include <nlm/nlm_prot.h>
51 1.1 dholland #include <nlm/nlm.h>
52 1.1 dholland
53 1.1 dholland FEATURE(nfsd, "NFSv4 server");
54 1.1 dholland
55 1.1 dholland extern u_int32_t newnfs_true, newnfs_false, newnfs_xdrneg1;
56 1.1 dholland extern int nfsrv_useacl;
57 1.1 dholland extern int newnfs_numnfsd;
58 1.1 dholland extern struct mount nfsv4root_mnt;
59 1.1 dholland extern struct nfsrv_stablefirst nfsrv_stablefirst;
60 1.1 dholland extern void (*nfsd_call_servertimer)(void);
61 1.1 dholland extern SVCPOOL *nfsrvd_pool;
62 1.1 dholland extern struct nfsv4lock nfsd_suspend_lock;
63 1.1 dholland struct vfsoptlist nfsv4root_opt, nfsv4root_newopt;
64 1.1 dholland NFSDLOCKMUTEX;
65 1.1 dholland struct nfsrchash_bucket nfsrchash_table[NFSRVCACHE_HASHSIZE];
66 1.1 dholland struct mtx nfsrc_udpmtx;
67 1.1 dholland struct mtx nfs_v4root_mutex;
68 1.1 dholland struct nfsrvfh nfs_rootfh, nfs_pubfh;
69 1.1 dholland int nfs_pubfhset = 0, nfs_rootfhset = 0;
70 1.1 dholland struct proc *nfsd_master_proc = NULL;
71 1.1 dholland static pid_t nfsd_master_pid = (pid_t)-1;
72 1.1 dholland static char nfsd_master_comm[MAXCOMLEN + 1];
73 1.1 dholland static struct timeval nfsd_master_start;
74 1.1 dholland static uint32_t nfsv4_sysid = 0;
75 1.1 dholland
76 1.1 dholland static int nfssvc_srvcall(struct thread *, struct nfssvc_args *,
77 1.1 dholland struct ucred *);
78 1.1 dholland
79 1.1 dholland int nfsrv_enable_crossmntpt = 1;
80 1.1 dholland static int nfs_commit_blks;
81 1.1 dholland static int nfs_commit_miss;
82 1.1 dholland extern int nfsrv_issuedelegs;
83 1.1 dholland extern int nfsrv_dolocallocks;
84 1.1 dholland
85 1.1 dholland SYSCTL_NODE(_vfs, OID_AUTO, nfsd, CTLFLAG_RW, 0, "New NFS server");
86 1.1 dholland SYSCTL_INT(_vfs_nfsd, OID_AUTO, mirrormnt, CTLFLAG_RW,
87 1.1 dholland &nfsrv_enable_crossmntpt, 0, "Enable nfsd to cross mount points");
88 1.1 dholland SYSCTL_INT(_vfs_nfsd, OID_AUTO, commit_blks, CTLFLAG_RW, &nfs_commit_blks,
89 1.1 dholland 0, "");
90 1.1 dholland SYSCTL_INT(_vfs_nfsd, OID_AUTO, commit_miss, CTLFLAG_RW, &nfs_commit_miss,
91 1.1 dholland 0, "");
92 1.1 dholland SYSCTL_INT(_vfs_nfsd, OID_AUTO, issue_delegations, CTLFLAG_RW,
93 1.1 dholland &nfsrv_issuedelegs, 0, "Enable nfsd to issue delegations");
94 1.1 dholland SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_locallocks, CTLFLAG_RW,
95 1.1 dholland &nfsrv_dolocallocks, 0, "Enable nfsd to acquire local locks on files");
96 1.1 dholland
97 1.1 dholland #define MAX_REORDERED_RPC 16
98 1.1 dholland #define NUM_HEURISTIC 1031
99 1.1 dholland #define NHUSE_INIT 64
100 1.1 dholland #define NHUSE_INC 16
101 1.1 dholland #define NHUSE_MAX 2048
102 1.1 dholland
103 1.1 dholland static struct nfsheur {
104 1.1 dholland struct vnode *nh_vp; /* vp to match (unreferenced pointer) */
105 1.1 dholland off_t nh_nextoff; /* next offset for sequential detection */
106 1.1 dholland int nh_use; /* use count for selection */
107 1.1 dholland int nh_seqcount; /* heuristic */
108 1.1 dholland } nfsheur[NUM_HEURISTIC];
109 1.1 dholland
110 1.1 dholland
111 1.1 dholland /*
112 1.1 dholland * Heuristic to detect sequential operation.
113 1.1 dholland */
114 1.1 dholland static struct nfsheur *
115 1.1 dholland nfsrv_sequential_heuristic(struct uio *uio, struct vnode *vp)
116 1.1 dholland {
117 1.1 dholland struct nfsheur *nh;
118 1.1 dholland int hi, try;
119 1.1 dholland
120 1.1 dholland /* Locate best candidate. */
121 1.1 dholland try = 32;
122 1.1 dholland hi = ((int)(vm_offset_t)vp / sizeof(struct vnode)) % NUM_HEURISTIC;
123 1.1 dholland nh = &nfsheur[hi];
124 1.1 dholland while (try--) {
125 1.1 dholland if (nfsheur[hi].nh_vp == vp) {
126 1.1 dholland nh = &nfsheur[hi];
127 1.1 dholland break;
128 1.1 dholland }
129 1.1 dholland if (nfsheur[hi].nh_use > 0)
130 1.1 dholland --nfsheur[hi].nh_use;
131 1.1 dholland hi = (hi + 1) % NUM_HEURISTIC;
132 1.1 dholland if (nfsheur[hi].nh_use < nh->nh_use)
133 1.1 dholland nh = &nfsheur[hi];
134 1.1 dholland }
135 1.1 dholland
136 1.1 dholland /* Initialize hint if this is a new file. */
137 1.1 dholland if (nh->nh_vp != vp) {
138 1.1 dholland nh->nh_vp = vp;
139 1.1 dholland nh->nh_nextoff = uio->uio_offset;
140 1.1 dholland nh->nh_use = NHUSE_INIT;
141 1.1 dholland if (uio->uio_offset == 0)
142 1.1 dholland nh->nh_seqcount = 4;
143 1.1 dholland else
144 1.1 dholland nh->nh_seqcount = 1;
145 1.1 dholland }
146 1.1 dholland
147 1.1 dholland /* Calculate heuristic. */
148 1.1 dholland if ((uio->uio_offset == 0 && nh->nh_seqcount > 0) ||
149 1.1 dholland uio->uio_offset == nh->nh_nextoff) {
150 1.1 dholland /* See comments in vfs_vnops.c:sequential_heuristic(). */
151 1.1 dholland nh->nh_seqcount += howmany(uio->uio_resid, 16384);
152 1.1 dholland if (nh->nh_seqcount > IO_SEQMAX)
153 1.1 dholland nh->nh_seqcount = IO_SEQMAX;
154 1.1 dholland } else if (qabs(uio->uio_offset - nh->nh_nextoff) <= MAX_REORDERED_RPC *
155 1.1 dholland imax(vp->v_mount->mnt_stat.f_iosize, uio->uio_resid)) {
156 1.1 dholland /* Probably a reordered RPC, leave seqcount alone. */
157 1.1 dholland } else if (nh->nh_seqcount > 1) {
158 1.1 dholland nh->nh_seqcount /= 2;
159 1.1 dholland } else {
160 1.1 dholland nh->nh_seqcount = 0;
161 1.1 dholland }
162 1.1 dholland nh->nh_use += NHUSE_INC;
163 1.1 dholland if (nh->nh_use > NHUSE_MAX)
164 1.1 dholland nh->nh_use = NHUSE_MAX;
165 1.1 dholland return (nh);
166 1.1 dholland }
167 1.1 dholland
168 1.1 dholland /*
169 1.1 dholland * Get attributes into nfsvattr structure.
170 1.1 dholland */
171 1.1 dholland int
172 1.1 dholland nfsvno_getattr(struct vnode *vp, struct nfsvattr *nvap, struct ucred *cred,
173 1.1 dholland struct thread *p, int vpislocked)
174 1.1 dholland {
175 1.1 dholland int error, lockedit = 0;
176 1.1 dholland
177 1.1 dholland if (vpislocked == 0) {
178 1.1 dholland /*
179 1.1 dholland * When vpislocked == 0, the vnode is either exclusively
180 1.1 dholland * locked by this thread or not locked by this thread.
181 1.1 dholland * As such, shared lock it, if not exclusively locked.
182 1.1 dholland */
183 1.1 dholland if (NFSVOPISLOCKED(vp) != LK_EXCLUSIVE) {
184 1.1 dholland lockedit = 1;
185 1.1 dholland NFSVOPLOCK(vp, LK_SHARED | LK_RETRY);
186 1.1 dholland }
187 1.1 dholland }
188 1.1 dholland error = VOP_GETATTR(vp, &nvap->na_vattr, cred);
189 1.1 dholland if (lockedit != 0)
190 1.1 dholland NFSVOPUNLOCK(vp, 0);
191 1.1 dholland
192 1.1 dholland NFSEXITCODE(error);
193 1.1 dholland return (error);
194 1.1 dholland }
195 1.1 dholland
196 1.1 dholland /*
197 1.1 dholland * Get a file handle for a vnode.
198 1.1 dholland */
199 1.1 dholland int
200 1.1 dholland nfsvno_getfh(struct vnode *vp, fhandle_t *fhp, struct thread *p)
201 1.1 dholland {
202 1.1 dholland int error;
203 1.1 dholland
204 1.1 dholland NFSBZERO((caddr_t)fhp, sizeof(fhandle_t));
205 1.1 dholland fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
206 1.1 dholland error = VOP_VPTOFH(vp, &fhp->fh_fid);
207 1.1 dholland
208 1.1 dholland NFSEXITCODE(error);
209 1.1 dholland return (error);
210 1.1 dholland }
211 1.1 dholland
212 1.1 dholland /*
213 1.1 dholland * Perform access checking for vnodes obtained from file handles that would
214 1.1 dholland * refer to files already opened by a Unix client. You cannot just use
215 1.1 dholland * vn_writechk() and VOP_ACCESSX() for two reasons.
216 1.1 dholland * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write
217 1.1 dholland * case.
218 1.1 dholland * 2 - The owner is to be given access irrespective of mode bits for some
219 1.1 dholland * operations, so that processes that chmod after opening a file don't
220 1.1 dholland * break.
221 1.1 dholland */
222 1.1 dholland int
223 1.1 dholland nfsvno_accchk(struct vnode *vp, accmode_t accmode, struct ucred *cred,
224 1.1 dholland struct nfsexstuff *exp, struct thread *p, int override, int vpislocked,
225 1.1 dholland u_int32_t *supportedtypep)
226 1.1 dholland {
227 1.1 dholland struct vattr vattr;
228 1.1 dholland int error = 0, getret = 0;
229 1.1 dholland
230 1.1 dholland if (vpislocked == 0) {
231 1.1 dholland if (NFSVOPLOCK(vp, LK_SHARED) != 0) {
232 1.1 dholland error = EPERM;
233 1.1 dholland goto out;
234 1.1 dholland }
235 1.1 dholland }
236 1.1 dholland if (accmode & VWRITE) {
237 1.1 dholland /* Just vn_writechk() changed to check rdonly */
238 1.1 dholland /*
239 1.1 dholland * Disallow write attempts on read-only file systems;
240 1.1 dholland * unless the file is a socket or a block or character
241 1.1 dholland * device resident on the file system.
242 1.1 dholland */
243 1.1 dholland if (NFSVNO_EXRDONLY(exp) ||
244 1.1 dholland (vp->v_mount->mnt_flag & MNT_RDONLY)) {
245 1.1 dholland switch (vp->v_type) {
246 1.1 dholland case VREG:
247 1.1 dholland case VDIR:
248 1.1 dholland case VLNK:
249 1.1 dholland error = EROFS;
250 1.1 dholland default:
251 1.1 dholland break;
252 1.1 dholland }
253 1.1 dholland }
254 1.1 dholland /*
255 1.1 dholland * If there's shared text associated with
256 1.1 dholland * the inode, try to free it up once. If
257 1.1 dholland * we fail, we can't allow writing.
258 1.1 dholland */
259 1.1 dholland if (VOP_IS_TEXT(vp) && error == 0)
260 1.1 dholland error = ETXTBSY;
261 1.1 dholland }
262 1.1 dholland if (error != 0) {
263 1.1 dholland if (vpislocked == 0)
264 1.1 dholland NFSVOPUNLOCK(vp, 0);
265 1.1 dholland goto out;
266 1.1 dholland }
267 1.1 dholland
268 1.1 dholland /*
269 1.1 dholland * Should the override still be applied when ACLs are enabled?
270 1.1 dholland */
271 1.1 dholland error = VOP_ACCESSX(vp, accmode, cred, p);
272 1.1 dholland if (error != 0 && (accmode & (VDELETE | VDELETE_CHILD))) {
273 1.1 dholland /*
274 1.1 dholland * Try again with VEXPLICIT_DENY, to see if the test for
275 1.1 dholland * deletion is supported.
276 1.1 dholland */
277 1.1 dholland error = VOP_ACCESSX(vp, accmode | VEXPLICIT_DENY, cred, p);
278 1.1 dholland if (error == 0) {
279 1.1 dholland if (vp->v_type == VDIR) {
280 1.1 dholland accmode &= ~(VDELETE | VDELETE_CHILD);
281 1.1 dholland accmode |= VWRITE;
282 1.1 dholland error = VOP_ACCESSX(vp, accmode, cred, p);
283 1.1 dholland } else if (supportedtypep != NULL) {
284 1.1 dholland *supportedtypep &= ~NFSACCESS_DELETE;
285 1.1 dholland }
286 1.1 dholland }
287 1.1 dholland }
288 1.1 dholland
289 1.1 dholland /*
290 1.1 dholland * Allow certain operations for the owner (reads and writes
291 1.1 dholland * on files that are already open).
292 1.1 dholland */
293 1.1 dholland if (override != NFSACCCHK_NOOVERRIDE &&
294 1.1 dholland (error == EPERM || error == EACCES)) {
295 1.1 dholland if (cred->cr_uid == 0 && (override & NFSACCCHK_ALLOWROOT))
296 1.1 dholland error = 0;
297 1.1 dholland else if (override & NFSACCCHK_ALLOWOWNER) {
298 1.1 dholland getret = VOP_GETATTR(vp, &vattr, cred);
299 1.1 dholland if (getret == 0 && cred->cr_uid == vattr.va_uid)
300 1.1 dholland error = 0;
301 1.1 dholland }
302 1.1 dholland }
303 1.1 dholland if (vpislocked == 0)
304 1.1 dholland NFSVOPUNLOCK(vp, 0);
305 1.1 dholland
306 1.1 dholland out:
307 1.1 dholland NFSEXITCODE(error);
308 1.1 dholland return (error);
309 1.1 dholland }
310 1.1 dholland
311 1.1 dholland /*
312 1.1 dholland * Set attribute(s) vnop.
313 1.1 dholland */
314 1.1 dholland int
315 1.1 dholland nfsvno_setattr(struct vnode *vp, struct nfsvattr *nvap, struct ucred *cred,
316 1.1 dholland struct thread *p, struct nfsexstuff *exp)
317 1.1 dholland {
318 1.1 dholland int error;
319 1.1 dholland
320 1.1 dholland error = VOP_SETATTR(vp, &nvap->na_vattr, cred);
321 1.1 dholland NFSEXITCODE(error);
322 1.1 dholland return (error);
323 1.1 dholland }
324 1.1 dholland
325 1.1 dholland /*
326 1.1 dholland * Set up nameidata for a lookup() call and do it.
327 1.1 dholland */
328 1.1 dholland int
329 1.1 dholland nfsvno_namei(struct nfsrv_descript *nd, struct nameidata *ndp,
330 1.1 dholland struct vnode *dp, int islocked, struct nfsexstuff *exp, struct thread *p,
331 1.1 dholland struct vnode **retdirp)
332 1.1 dholland {
333 1.1 dholland struct componentname *cnp = &ndp->ni_cnd;
334 1.1 dholland int i;
335 1.1 dholland struct iovec aiov;
336 1.1 dholland struct uio auio;
337 1.1 dholland int lockleaf = (cnp->cn_flags & LOCKLEAF) != 0, linklen;
338 1.1 dholland int error = 0, crossmnt;
339 1.1 dholland char *cp;
340 1.1 dholland
341 1.1 dholland *retdirp = NULL;
342 1.1 dholland cnp->cn_nameptr = cnp->cn_pnbuf;
343 1.1 dholland ndp->ni_strictrelative = 0;
344 1.1 dholland /*
345 1.1 dholland * Extract and set starting directory.
346 1.1 dholland */
347 1.1 dholland if (dp->v_type != VDIR) {
348 1.1 dholland if (islocked)
349 1.1 dholland vput(dp);
350 1.1 dholland else
351 1.1 dholland vrele(dp);
352 1.1 dholland nfsvno_relpathbuf(ndp);
353 1.1 dholland error = ENOTDIR;
354 1.1 dholland goto out1;
355 1.1 dholland }
356 1.1 dholland if (islocked)
357 1.1 dholland NFSVOPUNLOCK(dp, 0);
358 1.1 dholland VREF(dp);
359 1.1 dholland *retdirp = dp;
360 1.1 dholland if (NFSVNO_EXRDONLY(exp))
361 1.1 dholland cnp->cn_flags |= RDONLY;
362 1.1 dholland ndp->ni_segflg = UIO_SYSSPACE;
363 1.1 dholland crossmnt = 1;
364 1.1 dholland
365 1.1 dholland if (nd->nd_flag & ND_PUBLOOKUP) {
366 1.1 dholland ndp->ni_loopcnt = 0;
367 1.1 dholland if (cnp->cn_pnbuf[0] == '/') {
368 1.1 dholland vrele(dp);
369 1.1 dholland /*
370 1.1 dholland * Check for degenerate pathnames here, since lookup()
371 1.1 dholland * panics on them.
372 1.1 dholland */
373 1.1 dholland for (i = 1; i < ndp->ni_pathlen; i++)
374 1.1 dholland if (cnp->cn_pnbuf[i] != '/')
375 1.1 dholland break;
376 1.1 dholland if (i == ndp->ni_pathlen) {
377 1.1 dholland error = NFSERR_ACCES;
378 1.1 dholland goto out;
379 1.1 dholland }
380 1.1 dholland dp = rootvnode;
381 1.1 dholland VREF(dp);
382 1.1 dholland }
383 1.1 dholland } else if ((nfsrv_enable_crossmntpt == 0 && NFSVNO_EXPORTED(exp)) ||
384 1.1 dholland (nd->nd_flag & ND_NFSV4) == 0) {
385 1.1 dholland /*
386 1.1 dholland * Only cross mount points for NFSv4 when doing a
387 1.1 dholland * mount while traversing the file system above
388 1.1 dholland * the mount point, unless nfsrv_enable_crossmntpt is set.
389 1.1 dholland */
390 1.1 dholland cnp->cn_flags |= NOCROSSMOUNT;
391 1.1 dholland crossmnt = 0;
392 1.1 dholland }
393 1.1 dholland
394 1.1 dholland /*
395 1.1 dholland * Initialize for scan, set ni_startdir and bump ref on dp again
396 1.1 dholland * because lookup() will dereference ni_startdir.
397 1.1 dholland */
398 1.1 dholland
399 1.1 dholland cnp->cn_thread = p;
400 1.1 dholland ndp->ni_startdir = dp;
401 1.1 dholland ndp->ni_rootdir = rootvnode;
402 1.1 dholland ndp->ni_topdir = NULL;
403 1.1 dholland
404 1.1 dholland if (!lockleaf)
405 1.1 dholland cnp->cn_flags |= LOCKLEAF;
406 1.1 dholland for (;;) {
407 1.1 dholland cnp->cn_nameptr = cnp->cn_pnbuf;
408 1.1 dholland /*
409 1.1 dholland * Call lookup() to do the real work. If an error occurs,
410 1.1 dholland * ndp->ni_vp and ni_dvp are left uninitialized or NULL and
411 1.1 dholland * we do not have to dereference anything before returning.
412 1.1 dholland * In either case ni_startdir will be dereferenced and NULLed
413 1.1 dholland * out.
414 1.1 dholland */
415 1.1 dholland error = lookup(ndp);
416 1.1 dholland if (error)
417 1.1 dholland break;
418 1.1 dholland
419 1.1 dholland /*
420 1.1 dholland * Check for encountering a symbolic link. Trivial
421 1.1 dholland * termination occurs if no symlink encountered.
422 1.1 dholland */
423 1.1 dholland if ((cnp->cn_flags & ISSYMLINK) == 0) {
424 1.1 dholland if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0)
425 1.1 dholland nfsvno_relpathbuf(ndp);
426 1.1 dholland if (ndp->ni_vp && !lockleaf)
427 1.1 dholland NFSVOPUNLOCK(ndp->ni_vp, 0);
428 1.1 dholland break;
429 1.1 dholland }
430 1.1 dholland
431 1.1 dholland /*
432 1.1 dholland * Validate symlink
433 1.1 dholland */
434 1.1 dholland if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
435 1.1 dholland NFSVOPUNLOCK(ndp->ni_dvp, 0);
436 1.1 dholland if (!(nd->nd_flag & ND_PUBLOOKUP)) {
437 1.1 dholland error = EINVAL;
438 1.1 dholland goto badlink2;
439 1.1 dholland }
440 1.1 dholland
441 1.1 dholland if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
442 1.1 dholland error = ELOOP;
443 1.1 dholland goto badlink2;
444 1.1 dholland }
445 1.1 dholland if (ndp->ni_pathlen > 1)
446 1.1 dholland cp = uma_zalloc(namei_zone, M_WAITOK);
447 1.1 dholland else
448 1.1 dholland cp = cnp->cn_pnbuf;
449 1.1 dholland aiov.iov_base = cp;
450 1.1 dholland aiov.iov_len = MAXPATHLEN;
451 1.1 dholland auio.uio_iov = &aiov;
452 1.1 dholland auio.uio_iovcnt = 1;
453 1.1 dholland auio.uio_offset = 0;
454 1.1 dholland auio.uio_rw = UIO_READ;
455 1.1 dholland auio.uio_segflg = UIO_SYSSPACE;
456 1.1 dholland auio.uio_td = NULL;
457 1.1 dholland auio.uio_resid = MAXPATHLEN;
458 1.1 dholland error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
459 1.1 dholland if (error) {
460 1.1 dholland badlink1:
461 1.1 dholland if (ndp->ni_pathlen > 1)
462 1.1 dholland uma_zfree(namei_zone, cp);
463 1.1 dholland badlink2:
464 1.1 dholland vrele(ndp->ni_dvp);
465 1.1 dholland vput(ndp->ni_vp);
466 1.1 dholland break;
467 1.1 dholland }
468 1.1 dholland linklen = MAXPATHLEN - auio.uio_resid;
469 1.1 dholland if (linklen == 0) {
470 1.1 dholland error = ENOENT;
471 1.1 dholland goto badlink1;
472 1.1 dholland }
473 1.1 dholland if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
474 1.1 dholland error = ENAMETOOLONG;
475 1.1 dholland goto badlink1;
476 1.1 dholland }
477 1.1 dholland
478 1.1 dholland /*
479 1.1 dholland * Adjust or replace path
480 1.1 dholland */
481 1.1 dholland if (ndp->ni_pathlen > 1) {
482 1.1 dholland NFSBCOPY(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
483 1.1 dholland uma_zfree(namei_zone, cnp->cn_pnbuf);
484 1.1 dholland cnp->cn_pnbuf = cp;
485 1.1 dholland } else
486 1.1 dholland cnp->cn_pnbuf[linklen] = '\0';
487 1.1 dholland ndp->ni_pathlen += linklen;
488 1.1 dholland
489 1.1 dholland /*
490 1.1 dholland * Cleanup refs for next loop and check if root directory
491 1.1 dholland * should replace current directory. Normally ni_dvp
492 1.1 dholland * becomes the new base directory and is cleaned up when
493 1.1 dholland * we loop. Explicitly null pointers after invalidation
494 1.1 dholland * to clarify operation.
495 1.1 dholland */
496 1.1 dholland vput(ndp->ni_vp);
497 1.1 dholland ndp->ni_vp = NULL;
498 1.1 dholland
499 1.1 dholland if (cnp->cn_pnbuf[0] == '/') {
500 1.1 dholland vrele(ndp->ni_dvp);
501 1.1 dholland ndp->ni_dvp = ndp->ni_rootdir;
502 1.1 dholland VREF(ndp->ni_dvp);
503 1.1 dholland }
504 1.1 dholland ndp->ni_startdir = ndp->ni_dvp;
505 1.1 dholland ndp->ni_dvp = NULL;
506 1.1 dholland }
507 1.1 dholland if (!lockleaf)
508 1.1 dholland cnp->cn_flags &= ~LOCKLEAF;
509 1.1 dholland
510 1.1 dholland out:
511 1.1 dholland if (error) {
512 1.1 dholland nfsvno_relpathbuf(ndp);
513 1.1 dholland ndp->ni_vp = NULL;
514 1.1 dholland ndp->ni_dvp = NULL;
515 1.1 dholland ndp->ni_startdir = NULL;
516 1.1 dholland } else if ((ndp->ni_cnd.cn_flags & (WANTPARENT|LOCKPARENT)) == 0) {
517 1.1 dholland ndp->ni_dvp = NULL;
518 1.1 dholland }
519 1.1 dholland
520 1.1 dholland out1:
521 1.1 dholland NFSEXITCODE2(error, nd);
522 1.1 dholland return (error);
523 1.1 dholland }
524 1.1 dholland
525 1.1 dholland /*
526 1.1 dholland * Set up a pathname buffer and return a pointer to it and, optionally
527 1.1 dholland * set a hash pointer.
528 1.1 dholland */
529 1.1 dholland void
530 1.1 dholland nfsvno_setpathbuf(struct nameidata *ndp, char **bufpp, u_long **hashpp)
531 1.1 dholland {
532 1.1 dholland struct componentname *cnp = &ndp->ni_cnd;
533 1.1 dholland
534 1.1 dholland cnp->cn_flags |= (NOMACCHECK | HASBUF);
535 1.1 dholland cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
536 1.1 dholland if (hashpp != NULL)
537 1.1 dholland *hashpp = NULL;
538 1.1 dholland *bufpp = cnp->cn_pnbuf;
539 1.1 dholland }
540 1.1 dholland
541 1.1 dholland /*
542 1.1 dholland * Release the above path buffer, if not released by nfsvno_namei().
543 1.1 dholland */
544 1.1 dholland void
545 1.1 dholland nfsvno_relpathbuf(struct nameidata *ndp)
546 1.1 dholland {
547 1.1 dholland
548 1.1 dholland if ((ndp->ni_cnd.cn_flags & HASBUF) == 0)
549 1.1 dholland panic("nfsrelpath");
550 1.1 dholland uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
551 1.1 dholland ndp->ni_cnd.cn_flags &= ~HASBUF;
552 1.1 dholland }
553 1.1 dholland
554 1.1 dholland /*
555 1.1 dholland * Readlink vnode op into an mbuf list.
556 1.1 dholland */
557 1.1 dholland int
558 1.1 dholland nfsvno_readlink(struct vnode *vp, struct ucred *cred, struct thread *p,
559 1.1 dholland struct mbuf **mpp, struct mbuf **mpendp, int *lenp)
560 1.1 dholland {
561 1.1 dholland struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN];
562 1.1 dholland struct iovec *ivp = iv;
563 1.1 dholland struct uio io, *uiop = &io;
564 1.1 dholland struct mbuf *mp, *mp2 = NULL, *mp3 = NULL;
565 1.1 dholland int i, len, tlen, error = 0;
566 1.1 dholland
567 1.1 dholland len = 0;
568 1.1 dholland i = 0;
569 1.1 dholland while (len < NFS_MAXPATHLEN) {
570 1.1 dholland NFSMGET(mp);
571 1.1 dholland MCLGET(mp, M_WAITOK);
572 1.1 dholland mp->m_len = NFSMSIZ(mp);
573 1.1 dholland if (len == 0) {
574 1.1 dholland mp3 = mp2 = mp;
575 1.1 dholland } else {
576 1.1 dholland mp2->m_next = mp;
577 1.1 dholland mp2 = mp;
578 1.1 dholland }
579 1.1 dholland if ((len + mp->m_len) > NFS_MAXPATHLEN) {
580 1.1 dholland mp->m_len = NFS_MAXPATHLEN - len;
581 1.1 dholland len = NFS_MAXPATHLEN;
582 1.1 dholland } else {
583 1.1 dholland len += mp->m_len;
584 1.1 dholland }
585 1.1 dholland ivp->iov_base = mtod(mp, caddr_t);
586 1.1 dholland ivp->iov_len = mp->m_len;
587 1.1 dholland i++;
588 1.1 dholland ivp++;
589 1.1 dholland }
590 1.1 dholland uiop->uio_iov = iv;
591 1.1 dholland uiop->uio_iovcnt = i;
592 1.1 dholland uiop->uio_offset = 0;
593 1.1 dholland uiop->uio_resid = len;
594 1.1 dholland uiop->uio_rw = UIO_READ;
595 1.1 dholland uiop->uio_segflg = UIO_SYSSPACE;
596 1.1 dholland uiop->uio_td = NULL;
597 1.1 dholland error = VOP_READLINK(vp, uiop, cred);
598 1.1 dholland if (error) {
599 1.1 dholland m_freem(mp3);
600 1.1 dholland *lenp = 0;
601 1.1 dholland goto out;
602 1.1 dholland }
603 1.1 dholland if (uiop->uio_resid > 0) {
604 1.1 dholland len -= uiop->uio_resid;
605 1.1 dholland tlen = NFSM_RNDUP(len);
606 1.1 dholland nfsrv_adj(mp3, NFS_MAXPATHLEN - tlen, tlen - len);
607 1.1 dholland }
608 1.1 dholland *lenp = len;
609 1.1 dholland *mpp = mp3;
610 1.1 dholland *mpendp = mp;
611 1.1 dholland
612 1.1 dholland out:
613 1.1 dholland NFSEXITCODE(error);
614 1.1 dholland return (error);
615 1.1 dholland }
616 1.1 dholland
617 1.1 dholland /*
618 1.1 dholland * Read vnode op call into mbuf list.
619 1.1 dholland */
620 1.1 dholland int
621 1.1 dholland nfsvno_read(struct vnode *vp, off_t off, int cnt, struct ucred *cred,
622 1.1 dholland struct thread *p, struct mbuf **mpp, struct mbuf **mpendp)
623 1.1 dholland {
624 1.1 dholland struct mbuf *m;
625 1.1 dholland int i;
626 1.1 dholland struct iovec *iv;
627 1.1 dholland struct iovec *iv2;
628 1.1 dholland int error = 0, len, left, siz, tlen, ioflag = 0;
629 1.1 dholland struct mbuf *m2 = NULL, *m3;
630 1.1 dholland struct uio io, *uiop = &io;
631 1.1 dholland struct nfsheur *nh;
632 1.1 dholland
633 1.1 dholland len = left = NFSM_RNDUP(cnt);
634 1.1 dholland m3 = NULL;
635 1.1 dholland /*
636 1.1 dholland * Generate the mbuf list with the uio_iov ref. to it.
637 1.1 dholland */
638 1.1 dholland i = 0;
639 1.1 dholland while (left > 0) {
640 1.1 dholland NFSMGET(m);
641 1.1 dholland MCLGET(m, M_WAITOK);
642 1.1 dholland m->m_len = 0;
643 1.1 dholland siz = min(M_TRAILINGSPACE(m), left);
644 1.1 dholland left -= siz;
645 1.1 dholland i++;
646 1.1 dholland if (m3)
647 1.1 dholland m2->m_next = m;
648 1.1 dholland else
649 1.1 dholland m3 = m;
650 1.1 dholland m2 = m;
651 1.1 dholland }
652 1.1 dholland MALLOC(iv, struct iovec *, i * sizeof (struct iovec),
653 1.1 dholland M_TEMP, M_WAITOK);
654 1.1 dholland uiop->uio_iov = iv2 = iv;
655 1.1 dholland m = m3;
656 1.1 dholland left = len;
657 1.1 dholland i = 0;
658 1.1 dholland while (left > 0) {
659 1.1 dholland if (m == NULL)
660 1.1 dholland panic("nfsvno_read iov");
661 1.1 dholland siz = min(M_TRAILINGSPACE(m), left);
662 1.1 dholland if (siz > 0) {
663 1.1 dholland iv->iov_base = mtod(m, caddr_t) + m->m_len;
664 1.1 dholland iv->iov_len = siz;
665 1.1 dholland m->m_len += siz;
666 1.1 dholland left -= siz;
667 1.1 dholland iv++;
668 1.1 dholland i++;
669 1.1 dholland }
670 1.1 dholland m = m->m_next;
671 1.1 dholland }
672 1.1 dholland uiop->uio_iovcnt = i;
673 1.1 dholland uiop->uio_offset = off;
674 1.1 dholland uiop->uio_resid = len;
675 1.1 dholland uiop->uio_rw = UIO_READ;
676 1.1 dholland uiop->uio_segflg = UIO_SYSSPACE;
677 1.1 dholland nh = nfsrv_sequential_heuristic(uiop, vp);
678 1.1 dholland ioflag |= nh->nh_seqcount << IO_SEQSHIFT;
679 1.1 dholland error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred);
680 1.1 dholland FREE((caddr_t)iv2, M_TEMP);
681 1.1 dholland if (error) {
682 1.1 dholland m_freem(m3);
683 1.1 dholland *mpp = NULL;
684 1.1 dholland goto out;
685 1.1 dholland }
686 1.1 dholland nh->nh_nextoff = uiop->uio_offset;
687 1.1 dholland tlen = len - uiop->uio_resid;
688 1.1 dholland cnt = cnt < tlen ? cnt : tlen;
689 1.1 dholland tlen = NFSM_RNDUP(cnt);
690 1.1 dholland if (tlen == 0) {
691 1.1 dholland m_freem(m3);
692 1.1 dholland m3 = NULL;
693 1.1 dholland } else if (len != tlen || tlen != cnt)
694 1.1 dholland nfsrv_adj(m3, len - tlen, tlen - cnt);
695 1.1 dholland *mpp = m3;
696 1.1 dholland *mpendp = m2;
697 1.1 dholland
698 1.1 dholland out:
699 1.1 dholland NFSEXITCODE(error);
700 1.1 dholland return (error);
701 1.1 dholland }
702 1.1 dholland
703 1.1 dholland /*
704 1.1 dholland * Write vnode op from an mbuf list.
705 1.1 dholland */
706 1.1 dholland int
707 1.1 dholland nfsvno_write(struct vnode *vp, off_t off, int retlen, int cnt, int stable,
708 1.1 dholland struct mbuf *mp, char *cp, struct ucred *cred, struct thread *p)
709 1.1 dholland {
710 1.1 dholland struct iovec *ivp;
711 1.1 dholland int i, len;
712 1.1 dholland struct iovec *iv;
713 1.1 dholland int ioflags, error;
714 1.1 dholland struct uio io, *uiop = &io;
715 1.1 dholland struct nfsheur *nh;
716 1.1 dholland
717 1.1 dholland MALLOC(ivp, struct iovec *, cnt * sizeof (struct iovec), M_TEMP,
718 1.1 dholland M_WAITOK);
719 1.1 dholland uiop->uio_iov = iv = ivp;
720 1.1 dholland uiop->uio_iovcnt = cnt;
721 1.1 dholland i = mtod(mp, caddr_t) + mp->m_len - cp;
722 1.1 dholland len = retlen;
723 1.1 dholland while (len > 0) {
724 1.1 dholland if (mp == NULL)
725 1.1 dholland panic("nfsvno_write");
726 1.1 dholland if (i > 0) {
727 1.1 dholland i = min(i, len);
728 1.1 dholland ivp->iov_base = cp;
729 1.1 dholland ivp->iov_len = i;
730 1.1 dholland ivp++;
731 1.1 dholland len -= i;
732 1.1 dholland }
733 1.1 dholland mp = mp->m_next;
734 1.1 dholland if (mp) {
735 1.1 dholland i = mp->m_len;
736 1.1 dholland cp = mtod(mp, caddr_t);
737 1.1 dholland }
738 1.1 dholland }
739 1.1 dholland
740 1.1 dholland if (stable == NFSWRITE_UNSTABLE)
741 1.1 dholland ioflags = IO_NODELOCKED;
742 1.1 dholland else
743 1.1 dholland ioflags = (IO_SYNC | IO_NODELOCKED);
744 1.1 dholland uiop->uio_resid = retlen;
745 1.1 dholland uiop->uio_rw = UIO_WRITE;
746 1.1 dholland uiop->uio_segflg = UIO_SYSSPACE;
747 1.1 dholland NFSUIOPROC(uiop, p);
748 1.1 dholland uiop->uio_offset = off;
749 1.1 dholland nh = nfsrv_sequential_heuristic(uiop, vp);
750 1.1 dholland ioflags |= nh->nh_seqcount << IO_SEQSHIFT;
751 1.1 dholland error = VOP_WRITE(vp, uiop, ioflags, cred);
752 1.1 dholland if (error == 0)
753 1.1 dholland nh->nh_nextoff = uiop->uio_offset;
754 1.1 dholland FREE((caddr_t)iv, M_TEMP);
755 1.1 dholland
756 1.1 dholland NFSEXITCODE(error);
757 1.1 dholland return (error);
758 1.1 dholland }
759 1.1 dholland
760 1.1 dholland /*
761 1.1 dholland * Common code for creating a regular file (plus special files for V2).
762 1.1 dholland */
763 1.1 dholland int
764 1.1 dholland nfsvno_createsub(struct nfsrv_descript *nd, struct nameidata *ndp,
765 1.1 dholland struct vnode **vpp, struct nfsvattr *nvap, int *exclusive_flagp,
766 1.1 dholland int32_t *cverf, NFSDEV_T rdev, struct thread *p, struct nfsexstuff *exp)
767 1.1 dholland {
768 1.1 dholland u_quad_t tempsize;
769 1.1 dholland int error;
770 1.1 dholland
771 1.1 dholland error = nd->nd_repstat;
772 1.1 dholland if (!error && ndp->ni_vp == NULL) {
773 1.1 dholland if (nvap->na_type == VREG || nvap->na_type == VSOCK) {
774 1.1 dholland vrele(ndp->ni_startdir);
775 1.1 dholland error = VOP_CREATE(ndp->ni_dvp,
776 1.1 dholland &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr);
777 1.1 dholland vput(ndp->ni_dvp);
778 1.1 dholland nfsvno_relpathbuf(ndp);
779 1.1 dholland if (!error) {
780 1.1 dholland if (*exclusive_flagp) {
781 1.1 dholland *exclusive_flagp = 0;
782 1.1 dholland NFSVNO_ATTRINIT(nvap);
783 1.1 dholland nvap->na_atime.tv_sec = cverf[0];
784 1.1 dholland nvap->na_atime.tv_nsec = cverf[1];
785 1.1 dholland error = VOP_SETATTR(ndp->ni_vp,
786 1.1 dholland &nvap->na_vattr, nd->nd_cred);
787 1.1 dholland }
788 1.1 dholland }
789 1.1 dholland /*
790 1.1 dholland * NFS V2 Only. nfsrvd_mknod() does this for V3.
791 1.1 dholland * (This implies, just get out on an error.)
792 1.1 dholland */
793 1.1 dholland } else if (nvap->na_type == VCHR || nvap->na_type == VBLK ||
794 1.1 dholland nvap->na_type == VFIFO) {
795 1.1 dholland if (nvap->na_type == VCHR && rdev == 0xffffffff)
796 1.1 dholland nvap->na_type = VFIFO;
797 1.1 dholland if (nvap->na_type != VFIFO &&
798 1.1 dholland (error = priv_check_cred(nd->nd_cred,
799 1.1 dholland PRIV_VFS_MKNOD_DEV, 0))) {
800 1.1 dholland vrele(ndp->ni_startdir);
801 1.1 dholland nfsvno_relpathbuf(ndp);
802 1.1 dholland vput(ndp->ni_dvp);
803 1.1 dholland goto out;
804 1.1 dholland }
805 1.1 dholland nvap->na_rdev = rdev;
806 1.1 dholland error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp,
807 1.1 dholland &ndp->ni_cnd, &nvap->na_vattr);
808 1.1 dholland vput(ndp->ni_dvp);
809 1.1 dholland nfsvno_relpathbuf(ndp);
810 1.1 dholland vrele(ndp->ni_startdir);
811 1.1 dholland if (error)
812 1.1 dholland goto out;
813 1.1 dholland } else {
814 1.1 dholland vrele(ndp->ni_startdir);
815 1.1 dholland nfsvno_relpathbuf(ndp);
816 1.1 dholland vput(ndp->ni_dvp);
817 1.1 dholland error = ENXIO;
818 1.1 dholland goto out;
819 1.1 dholland }
820 1.1 dholland *vpp = ndp->ni_vp;
821 1.1 dholland } else {
822 1.1 dholland /*
823 1.1 dholland * Handle cases where error is already set and/or
824 1.1 dholland * the file exists.
825 1.1 dholland * 1 - clean up the lookup
826 1.1 dholland * 2 - iff !error and na_size set, truncate it
827 1.1 dholland */
828 1.1 dholland vrele(ndp->ni_startdir);
829 1.1 dholland nfsvno_relpathbuf(ndp);
830 1.1 dholland *vpp = ndp->ni_vp;
831 1.1 dholland if (ndp->ni_dvp == *vpp)
832 1.1 dholland vrele(ndp->ni_dvp);
833 1.1 dholland else
834 1.1 dholland vput(ndp->ni_dvp);
835 1.1 dholland if (!error && nvap->na_size != VNOVAL) {
836 1.1 dholland error = nfsvno_accchk(*vpp, VWRITE,
837 1.1 dholland nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
838 1.1 dholland NFSACCCHK_VPISLOCKED, NULL);
839 1.1 dholland if (!error) {
840 1.1 dholland tempsize = nvap->na_size;
841 1.1 dholland NFSVNO_ATTRINIT(nvap);
842 1.1 dholland nvap->na_size = tempsize;
843 1.1 dholland error = VOP_SETATTR(*vpp,
844 1.1 dholland &nvap->na_vattr, nd->nd_cred);
845 1.1 dholland }
846 1.1 dholland }
847 1.1 dholland if (error)
848 1.1 dholland vput(*vpp);
849 1.1 dholland }
850 1.1 dholland
851 1.1 dholland out:
852 1.1 dholland NFSEXITCODE(error);
853 1.1 dholland return (error);
854 1.1 dholland }
855 1.1 dholland
856 1.1 dholland /*
857 1.1 dholland * Do a mknod vnode op.
858 1.1 dholland */
859 1.1 dholland int
860 1.1 dholland nfsvno_mknod(struct nameidata *ndp, struct nfsvattr *nvap, struct ucred *cred,
861 1.1 dholland struct thread *p)
862 1.1 dholland {
863 1.1 dholland int error = 0;
864 1.1 dholland enum vtype vtyp;
865 1.1 dholland
866 1.1 dholland vtyp = nvap->na_type;
867 1.1 dholland /*
868 1.1 dholland * Iff doesn't exist, create it.
869 1.1 dholland */
870 1.1 dholland if (ndp->ni_vp) {
871 1.1 dholland vrele(ndp->ni_startdir);
872 1.1 dholland nfsvno_relpathbuf(ndp);
873 1.1 dholland vput(ndp->ni_dvp);
874 1.1 dholland vrele(ndp->ni_vp);
875 1.1 dholland error = EEXIST;
876 1.1 dholland goto out;
877 1.1 dholland }
878 1.1 dholland if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
879 1.1 dholland vrele(ndp->ni_startdir);
880 1.1 dholland nfsvno_relpathbuf(ndp);
881 1.1 dholland vput(ndp->ni_dvp);
882 1.1 dholland error = NFSERR_BADTYPE;
883 1.1 dholland goto out;
884 1.1 dholland }
885 1.1 dholland if (vtyp == VSOCK) {
886 1.1 dholland vrele(ndp->ni_startdir);
887 1.1 dholland error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
888 1.1 dholland &ndp->ni_cnd, &nvap->na_vattr);
889 1.1 dholland vput(ndp->ni_dvp);
890 1.1 dholland nfsvno_relpathbuf(ndp);
891 1.1 dholland } else {
892 1.1 dholland if (nvap->na_type != VFIFO &&
893 1.1 dholland (error = priv_check_cred(cred, PRIV_VFS_MKNOD_DEV, 0))) {
894 1.1 dholland vrele(ndp->ni_startdir);
895 1.1 dholland nfsvno_relpathbuf(ndp);
896 1.1 dholland vput(ndp->ni_dvp);
897 1.1 dholland goto out;
898 1.1 dholland }
899 1.1 dholland error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp,
900 1.1 dholland &ndp->ni_cnd, &nvap->na_vattr);
901 1.1 dholland vput(ndp->ni_dvp);
902 1.1 dholland nfsvno_relpathbuf(ndp);
903 1.1 dholland vrele(ndp->ni_startdir);
904 1.1 dholland /*
905 1.1 dholland * Since VOP_MKNOD returns the ni_vp, I can't
906 1.1 dholland * see any reason to do the lookup.
907 1.1 dholland */
908 1.1 dholland }
909 1.1 dholland
910 1.1 dholland out:
911 1.1 dholland NFSEXITCODE(error);
912 1.1 dholland return (error);
913 1.1 dholland }
914 1.1 dholland
915 1.1 dholland /*
916 1.1 dholland * Mkdir vnode op.
917 1.1 dholland */
918 1.1 dholland int
919 1.1 dholland nfsvno_mkdir(struct nameidata *ndp, struct nfsvattr *nvap, uid_t saved_uid,
920 1.1 dholland struct ucred *cred, struct thread *p, struct nfsexstuff *exp)
921 1.1 dholland {
922 1.1 dholland int error = 0;
923 1.1 dholland
924 1.1 dholland if (ndp->ni_vp != NULL) {
925 1.1 dholland if (ndp->ni_dvp == ndp->ni_vp)
926 1.1 dholland vrele(ndp->ni_dvp);
927 1.1 dholland else
928 1.1 dholland vput(ndp->ni_dvp);
929 1.1 dholland vrele(ndp->ni_vp);
930 1.1 dholland nfsvno_relpathbuf(ndp);
931 1.1 dholland error = EEXIST;
932 1.1 dholland goto out;
933 1.1 dholland }
934 1.1 dholland error = VOP_MKDIR(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd,
935 1.1 dholland &nvap->na_vattr);
936 1.1 dholland vput(ndp->ni_dvp);
937 1.1 dholland nfsvno_relpathbuf(ndp);
938 1.1 dholland
939 1.1 dholland out:
940 1.1 dholland NFSEXITCODE(error);
941 1.1 dholland return (error);
942 1.1 dholland }
943 1.1 dholland
944 1.1 dholland /*
945 1.1 dholland * symlink vnode op.
946 1.1 dholland */
947 1.1 dholland int
948 1.1 dholland nfsvno_symlink(struct nameidata *ndp, struct nfsvattr *nvap, char *pathcp,
949 1.1 dholland int pathlen, int not_v2, uid_t saved_uid, struct ucred *cred, struct thread *p,
950 1.1 dholland struct nfsexstuff *exp)
951 1.1 dholland {
952 1.1 dholland int error = 0;
953 1.1 dholland
954 1.1 dholland if (ndp->ni_vp) {
955 1.1 dholland vrele(ndp->ni_startdir);
956 1.1 dholland nfsvno_relpathbuf(ndp);
957 1.1 dholland if (ndp->ni_dvp == ndp->ni_vp)
958 1.1 dholland vrele(ndp->ni_dvp);
959 1.1 dholland else
960 1.1 dholland vput(ndp->ni_dvp);
961 1.1 dholland vrele(ndp->ni_vp);
962 1.1 dholland error = EEXIST;
963 1.1 dholland goto out;
964 1.1 dholland }
965 1.1 dholland
966 1.1 dholland error = VOP_SYMLINK(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd,
967 1.1 dholland &nvap->na_vattr, pathcp);
968 1.1 dholland vput(ndp->ni_dvp);
969 1.1 dholland vrele(ndp->ni_startdir);
970 1.1 dholland nfsvno_relpathbuf(ndp);
971 1.1 dholland /*
972 1.1 dholland * Although FreeBSD still had the lookup code in
973 1.1 dholland * it for 7/current, there doesn't seem to be any
974 1.1 dholland * point, since VOP_SYMLINK() returns the ni_vp.
975 1.1 dholland * Just vput it for v2.
976 1.1 dholland */
977 1.1 dholland if (!not_v2 && !error)
978 1.1 dholland vput(ndp->ni_vp);
979 1.1 dholland
980 1.1 dholland out:
981 1.1 dholland NFSEXITCODE(error);
982 1.1 dholland return (error);
983 1.1 dholland }
984 1.1 dholland
985 1.1 dholland /*
986 1.1 dholland * Parse symbolic link arguments.
987 1.1 dholland * This function has an ugly side effect. It will MALLOC() an area for
988 1.1 dholland * the symlink and set iov_base to point to it, only if it succeeds.
989 1.1 dholland * So, if it returns with uiop->uio_iov->iov_base != NULL, that must
990 1.1 dholland * be FREE'd later.
991 1.1 dholland */
992 1.1 dholland int
993 1.1 dholland nfsvno_getsymlink(struct nfsrv_descript *nd, struct nfsvattr *nvap,
994 1.1 dholland struct thread *p, char **pathcpp, int *lenp)
995 1.1 dholland {
996 1.1 dholland u_int32_t *tl;
997 1.1 dholland char *pathcp = NULL;
998 1.1 dholland int error = 0, len;
999 1.1 dholland struct nfsv2_sattr *sp;
1000 1.1 dholland
1001 1.1 dholland *pathcpp = NULL;
1002 1.1 dholland *lenp = 0;
1003 1.1 dholland if ((nd->nd_flag & ND_NFSV3) &&
1004 1.1 dholland (error = nfsrv_sattr(nd, nvap, NULL, NULL, p)))
1005 1.1 dholland goto nfsmout;
1006 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1007 1.1 dholland len = fxdr_unsigned(int, *tl);
1008 1.1 dholland if (len > NFS_MAXPATHLEN || len <= 0) {
1009 1.1 dholland error = EBADRPC;
1010 1.1 dholland goto nfsmout;
1011 1.1 dholland }
1012 1.1 dholland MALLOC(pathcp, caddr_t, len + 1, M_TEMP, M_WAITOK);
1013 1.1 dholland error = nfsrv_mtostr(nd, pathcp, len);
1014 1.1 dholland if (error)
1015 1.1 dholland goto nfsmout;
1016 1.1 dholland if (nd->nd_flag & ND_NFSV2) {
1017 1.1 dholland NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1018 1.1 dholland nvap->na_mode = fxdr_unsigned(u_int16_t, sp->sa_mode);
1019 1.1 dholland }
1020 1.1 dholland *pathcpp = pathcp;
1021 1.1 dholland *lenp = len;
1022 1.1 dholland NFSEXITCODE2(0, nd);
1023 1.1 dholland return (0);
1024 1.1 dholland nfsmout:
1025 1.1 dholland if (pathcp)
1026 1.1 dholland free(pathcp, M_TEMP);
1027 1.1 dholland NFSEXITCODE2(error, nd);
1028 1.1 dholland return (error);
1029 1.1 dholland }
1030 1.1 dholland
1031 1.1 dholland /*
1032 1.1 dholland * Remove a non-directory object.
1033 1.1 dholland */
1034 1.1 dholland int
1035 1.1 dholland nfsvno_removesub(struct nameidata *ndp, int is_v4, struct ucred *cred,
1036 1.1 dholland struct thread *p, struct nfsexstuff *exp)
1037 1.1 dholland {
1038 1.1 dholland struct vnode *vp;
1039 1.1 dholland int error = 0;
1040 1.1 dholland
1041 1.1 dholland vp = ndp->ni_vp;
1042 1.1 dholland if (vp->v_type == VDIR)
1043 1.1 dholland error = NFSERR_ISDIR;
1044 1.1 dholland else if (is_v4)
1045 1.1 dholland error = nfsrv_checkremove(vp, 1, p);
1046 1.1 dholland if (!error)
1047 1.1 dholland error = VOP_REMOVE(ndp->ni_dvp, vp, &ndp->ni_cnd);
1048 1.1 dholland if (ndp->ni_dvp == vp)
1049 1.1 dholland vrele(ndp->ni_dvp);
1050 1.1 dholland else
1051 1.1 dholland vput(ndp->ni_dvp);
1052 1.1 dholland vput(vp);
1053 1.1 dholland if ((ndp->ni_cnd.cn_flags & SAVENAME) != 0)
1054 1.1 dholland nfsvno_relpathbuf(ndp);
1055 1.1 dholland NFSEXITCODE(error);
1056 1.1 dholland return (error);
1057 1.1 dholland }
1058 1.1 dholland
1059 1.1 dholland /*
1060 1.1 dholland * Remove a directory.
1061 1.1 dholland */
1062 1.1 dholland int
1063 1.1 dholland nfsvno_rmdirsub(struct nameidata *ndp, int is_v4, struct ucred *cred,
1064 1.1 dholland struct thread *p, struct nfsexstuff *exp)
1065 1.1 dholland {
1066 1.1 dholland struct vnode *vp;
1067 1.1 dholland int error = 0;
1068 1.1 dholland
1069 1.1 dholland vp = ndp->ni_vp;
1070 1.1 dholland if (vp->v_type != VDIR) {
1071 1.1 dholland error = ENOTDIR;
1072 1.1 dholland goto out;
1073 1.1 dholland }
1074 1.1 dholland /*
1075 1.1 dholland * No rmdir "." please.
1076 1.1 dholland */
1077 1.1 dholland if (ndp->ni_dvp == vp) {
1078 1.1 dholland error = EINVAL;
1079 1.1 dholland goto out;
1080 1.1 dholland }
1081 1.1 dholland /*
1082 1.1 dholland * The root of a mounted filesystem cannot be deleted.
1083 1.1 dholland */
1084 1.1 dholland if (vp->v_vflag & VV_ROOT)
1085 1.1 dholland error = EBUSY;
1086 1.1 dholland out:
1087 1.1 dholland if (!error)
1088 1.1 dholland error = VOP_RMDIR(ndp->ni_dvp, vp, &ndp->ni_cnd);
1089 1.1 dholland if (ndp->ni_dvp == vp)
1090 1.1 dholland vrele(ndp->ni_dvp);
1091 1.1 dholland else
1092 1.1 dholland vput(ndp->ni_dvp);
1093 1.1 dholland vput(vp);
1094 1.1 dholland if ((ndp->ni_cnd.cn_flags & SAVENAME) != 0)
1095 1.1 dholland nfsvno_relpathbuf(ndp);
1096 1.1 dholland NFSEXITCODE(error);
1097 1.1 dholland return (error);
1098 1.1 dholland }
1099 1.1 dholland
1100 1.1 dholland /*
1101 1.1 dholland * Rename vnode op.
1102 1.1 dholland */
1103 1.1 dholland int
1104 1.1 dholland nfsvno_rename(struct nameidata *fromndp, struct nameidata *tondp,
1105 1.1 dholland u_int32_t ndstat, u_int32_t ndflag, struct ucred *cred, struct thread *p)
1106 1.1 dholland {
1107 1.1 dholland struct vnode *fvp, *tvp, *tdvp;
1108 1.1 dholland int error = 0;
1109 1.1 dholland
1110 1.1 dholland fvp = fromndp->ni_vp;
1111 1.1 dholland if (ndstat) {
1112 1.1 dholland vrele(fromndp->ni_dvp);
1113 1.1 dholland vrele(fvp);
1114 1.1 dholland error = ndstat;
1115 1.1 dholland goto out1;
1116 1.1 dholland }
1117 1.1 dholland tdvp = tondp->ni_dvp;
1118 1.1 dholland tvp = tondp->ni_vp;
1119 1.1 dholland if (tvp != NULL) {
1120 1.1 dholland if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
1121 1.1 dholland error = (ndflag & ND_NFSV2) ? EISDIR : EEXIST;
1122 1.1 dholland goto out;
1123 1.1 dholland } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
1124 1.1 dholland error = (ndflag & ND_NFSV2) ? ENOTDIR : EEXIST;
1125 1.1 dholland goto out;
1126 1.1 dholland }
1127 1.1 dholland if (tvp->v_type == VDIR && tvp->v_mountedhere) {
1128 1.1 dholland error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1129 1.1 dholland goto out;
1130 1.1 dholland }
1131 1.1 dholland
1132 1.1 dholland /*
1133 1.1 dholland * A rename to '.' or '..' results in a prematurely
1134 1.1 dholland * unlocked vnode on FreeBSD5, so I'm just going to fail that
1135 1.1 dholland * here.
1136 1.1 dholland */
1137 1.1 dholland if ((tondp->ni_cnd.cn_namelen == 1 &&
1138 1.1 dholland tondp->ni_cnd.cn_nameptr[0] == '.') ||
1139 1.1 dholland (tondp->ni_cnd.cn_namelen == 2 &&
1140 1.1 dholland tondp->ni_cnd.cn_nameptr[0] == '.' &&
1141 1.1 dholland tondp->ni_cnd.cn_nameptr[1] == '.')) {
1142 1.1 dholland error = EINVAL;
1143 1.1 dholland goto out;
1144 1.1 dholland }
1145 1.1 dholland }
1146 1.1 dholland if (fvp->v_type == VDIR && fvp->v_mountedhere) {
1147 1.1 dholland error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1148 1.1 dholland goto out;
1149 1.1 dholland }
1150 1.1 dholland if (fvp->v_mount != tdvp->v_mount) {
1151 1.1 dholland error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1152 1.1 dholland goto out;
1153 1.1 dholland }
1154 1.1 dholland if (fvp == tdvp) {
1155 1.1 dholland error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EINVAL;
1156 1.1 dholland goto out;
1157 1.1 dholland }
1158 1.1 dholland if (fvp == tvp) {
1159 1.1 dholland /*
1160 1.1 dholland * If source and destination are the same, there is nothing to
1161 1.1 dholland * do. Set error to -1 to indicate this.
1162 1.1 dholland */
1163 1.1 dholland error = -1;
1164 1.1 dholland goto out;
1165 1.1 dholland }
1166 1.1 dholland if (ndflag & ND_NFSV4) {
1167 1.1 dholland if (NFSVOPLOCK(fvp, LK_EXCLUSIVE) == 0) {
1168 1.1 dholland error = nfsrv_checkremove(fvp, 0, p);
1169 1.1 dholland NFSVOPUNLOCK(fvp, 0);
1170 1.1 dholland } else
1171 1.1 dholland error = EPERM;
1172 1.1 dholland if (tvp && !error)
1173 1.1 dholland error = nfsrv_checkremove(tvp, 1, p);
1174 1.1 dholland } else {
1175 1.1 dholland /*
1176 1.1 dholland * For NFSv2 and NFSv3, try to get rid of the delegation, so
1177 1.1 dholland * that the NFSv4 client won't be confused by the rename.
1178 1.1 dholland * Since nfsd_recalldelegation() can only be called on an
1179 1.1 dholland * unlocked vnode at this point and fvp is the file that will
1180 1.1 dholland * still exist after the rename, just do fvp.
1181 1.1 dholland */
1182 1.1 dholland nfsd_recalldelegation(fvp, p);
1183 1.1 dholland }
1184 1.1 dholland out:
1185 1.1 dholland if (!error) {
1186 1.1 dholland error = VOP_RENAME(fromndp->ni_dvp, fromndp->ni_vp,
1187 1.1 dholland &fromndp->ni_cnd, tondp->ni_dvp, tondp->ni_vp,
1188 1.1 dholland &tondp->ni_cnd);
1189 1.1 dholland } else {
1190 1.1 dholland if (tdvp == tvp)
1191 1.1 dholland vrele(tdvp);
1192 1.1 dholland else
1193 1.1 dholland vput(tdvp);
1194 1.1 dholland if (tvp)
1195 1.1 dholland vput(tvp);
1196 1.1 dholland vrele(fromndp->ni_dvp);
1197 1.1 dholland vrele(fvp);
1198 1.1 dholland if (error == -1)
1199 1.1 dholland error = 0;
1200 1.1 dholland }
1201 1.1 dholland vrele(tondp->ni_startdir);
1202 1.1 dholland nfsvno_relpathbuf(tondp);
1203 1.1 dholland out1:
1204 1.1 dholland vrele(fromndp->ni_startdir);
1205 1.1 dholland nfsvno_relpathbuf(fromndp);
1206 1.1 dholland NFSEXITCODE(error);
1207 1.1 dholland return (error);
1208 1.1 dholland }
1209 1.1 dholland
1210 1.1 dholland /*
1211 1.1 dholland * Link vnode op.
1212 1.1 dholland */
1213 1.1 dholland int
1214 1.1 dholland nfsvno_link(struct nameidata *ndp, struct vnode *vp, struct ucred *cred,
1215 1.1 dholland struct thread *p, struct nfsexstuff *exp)
1216 1.1 dholland {
1217 1.1 dholland struct vnode *xp;
1218 1.1 dholland int error = 0;
1219 1.1 dholland
1220 1.1 dholland xp = ndp->ni_vp;
1221 1.1 dholland if (xp != NULL) {
1222 1.1 dholland error = EEXIST;
1223 1.1 dholland } else {
1224 1.1 dholland xp = ndp->ni_dvp;
1225 1.1 dholland if (vp->v_mount != xp->v_mount)
1226 1.1 dholland error = EXDEV;
1227 1.1 dholland }
1228 1.1 dholland if (!error) {
1229 1.1 dholland NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
1230 1.1 dholland if ((vp->v_iflag & VI_DOOMED) == 0)
1231 1.1 dholland error = VOP_LINK(ndp->ni_dvp, vp, &ndp->ni_cnd);
1232 1.1 dholland else
1233 1.1 dholland error = EPERM;
1234 1.1 dholland if (ndp->ni_dvp == vp)
1235 1.1 dholland vrele(ndp->ni_dvp);
1236 1.1 dholland else
1237 1.1 dholland vput(ndp->ni_dvp);
1238 1.1 dholland NFSVOPUNLOCK(vp, 0);
1239 1.1 dholland } else {
1240 1.1 dholland if (ndp->ni_dvp == ndp->ni_vp)
1241 1.1 dholland vrele(ndp->ni_dvp);
1242 1.1 dholland else
1243 1.1 dholland vput(ndp->ni_dvp);
1244 1.1 dholland if (ndp->ni_vp)
1245 1.1 dholland vrele(ndp->ni_vp);
1246 1.1 dholland }
1247 1.1 dholland nfsvno_relpathbuf(ndp);
1248 1.1 dholland NFSEXITCODE(error);
1249 1.1 dholland return (error);
1250 1.1 dholland }
1251 1.1 dholland
1252 1.1 dholland /*
1253 1.1 dholland * Do the fsync() appropriate for the commit.
1254 1.1 dholland */
1255 1.1 dholland int
1256 1.1 dholland nfsvno_fsync(struct vnode *vp, u_int64_t off, int cnt, struct ucred *cred,
1257 1.1 dholland struct thread *td)
1258 1.1 dholland {
1259 1.1 dholland int error = 0;
1260 1.1 dholland
1261 1.1 dholland /*
1262 1.1 dholland * RFC 1813 3.3.21: if count is 0, a flush from offset to the end of
1263 1.1 dholland * file is done. At this time VOP_FSYNC does not accept offset and
1264 1.1 dholland * byte count parameters so call VOP_FSYNC the whole file for now.
1265 1.1 dholland * The same is true for NFSv4: RFC 3530 Sec. 14.2.3.
1266 1.1 dholland */
1267 1.1 dholland if (cnt == 0 || cnt > MAX_COMMIT_COUNT) {
1268 1.1 dholland /*
1269 1.1 dholland * Give up and do the whole thing
1270 1.1 dholland */
1271 1.1 dholland if (vp->v_object &&
1272 1.1 dholland (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
1273 1.1 dholland VM_OBJECT_WLOCK(vp->v_object);
1274 1.1 dholland vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
1275 1.1 dholland VM_OBJECT_WUNLOCK(vp->v_object);
1276 1.1 dholland }
1277 1.1 dholland error = VOP_FSYNC(vp, MNT_WAIT, td);
1278 1.1 dholland } else {
1279 1.1 dholland /*
1280 1.1 dholland * Locate and synchronously write any buffers that fall
1281 1.1 dholland * into the requested range. Note: we are assuming that
1282 1.1 dholland * f_iosize is a power of 2.
1283 1.1 dholland */
1284 1.1 dholland int iosize = vp->v_mount->mnt_stat.f_iosize;
1285 1.1 dholland int iomask = iosize - 1;
1286 1.1 dholland struct bufobj *bo;
1287 1.1 dholland daddr_t lblkno;
1288 1.1 dholland
1289 1.1 dholland /*
1290 1.1 dholland * Align to iosize boundry, super-align to page boundry.
1291 1.1 dholland */
1292 1.1 dholland if (off & iomask) {
1293 1.1 dholland cnt += off & iomask;
1294 1.1 dholland off &= ~(u_quad_t)iomask;
1295 1.1 dholland }
1296 1.1 dholland if (off & PAGE_MASK) {
1297 1.1 dholland cnt += off & PAGE_MASK;
1298 1.1 dholland off &= ~(u_quad_t)PAGE_MASK;
1299 1.1 dholland }
1300 1.1 dholland lblkno = off / iosize;
1301 1.1 dholland
1302 1.1 dholland if (vp->v_object &&
1303 1.1 dholland (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
1304 1.1 dholland VM_OBJECT_WLOCK(vp->v_object);
1305 1.1 dholland vm_object_page_clean(vp->v_object, off, off + cnt,
1306 1.1 dholland OBJPC_SYNC);
1307 1.1 dholland VM_OBJECT_WUNLOCK(vp->v_object);
1308 1.1 dholland }
1309 1.1 dholland
1310 1.1 dholland bo = &vp->v_bufobj;
1311 1.1 dholland BO_LOCK(bo);
1312 1.1 dholland while (cnt > 0) {
1313 1.1 dholland struct buf *bp;
1314 1.1 dholland
1315 1.1 dholland /*
1316 1.1 dholland * If we have a buffer and it is marked B_DELWRI we
1317 1.1 dholland * have to lock and write it. Otherwise the prior
1318 1.1 dholland * write is assumed to have already been committed.
1319 1.1 dholland *
1320 1.1 dholland * gbincore() can return invalid buffers now so we
1321 1.1 dholland * have to check that bit as well (though B_DELWRI
1322 1.1 dholland * should not be set if B_INVAL is set there could be
1323 1.1 dholland * a race here since we haven't locked the buffer).
1324 1.1 dholland */
1325 1.1 dholland if ((bp = gbincore(&vp->v_bufobj, lblkno)) != NULL) {
1326 1.1 dholland if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL |
1327 1.1 dholland LK_INTERLOCK, BO_LOCKPTR(bo)) == ENOLCK) {
1328 1.1 dholland BO_LOCK(bo);
1329 1.1 dholland continue; /* retry */
1330 1.1 dholland }
1331 1.1 dholland if ((bp->b_flags & (B_DELWRI|B_INVAL)) ==
1332 1.1 dholland B_DELWRI) {
1333 1.1 dholland bremfree(bp);
1334 1.1 dholland bp->b_flags &= ~B_ASYNC;
1335 1.1 dholland bwrite(bp);
1336 1.1 dholland ++nfs_commit_miss;
1337 1.1 dholland } else
1338 1.1 dholland BUF_UNLOCK(bp);
1339 1.1 dholland BO_LOCK(bo);
1340 1.1 dholland }
1341 1.1 dholland ++nfs_commit_blks;
1342 1.1 dholland if (cnt < iosize)
1343 1.1 dholland break;
1344 1.1 dholland cnt -= iosize;
1345 1.1 dholland ++lblkno;
1346 1.1 dholland }
1347 1.1 dholland BO_UNLOCK(bo);
1348 1.1 dholland }
1349 1.1 dholland NFSEXITCODE(error);
1350 1.1 dholland return (error);
1351 1.1 dholland }
1352 1.1 dholland
1353 1.1 dholland /*
1354 1.1 dholland * Statfs vnode op.
1355 1.1 dholland */
1356 1.1 dholland int
1357 1.1 dholland nfsvno_statfs(struct vnode *vp, struct statfs *sf)
1358 1.1 dholland {
1359 1.1 dholland int error;
1360 1.1 dholland
1361 1.1 dholland error = VFS_STATFS(vp->v_mount, sf);
1362 1.1 dholland if (error == 0) {
1363 1.1 dholland /*
1364 1.1 dholland * Since NFS handles these values as unsigned on the
1365 1.1 dholland * wire, there is no way to represent negative values,
1366 1.1 dholland * so set them to 0. Without this, they will appear
1367 1.1 dholland * to be very large positive values for clients like
1368 1.1 dholland * Solaris10.
1369 1.1 dholland */
1370 1.1 dholland if (sf->f_bavail < 0)
1371 1.1 dholland sf->f_bavail = 0;
1372 1.1 dholland if (sf->f_ffree < 0)
1373 1.1 dholland sf->f_ffree = 0;
1374 1.1 dholland }
1375 1.1 dholland NFSEXITCODE(error);
1376 1.1 dholland return (error);
1377 1.1 dholland }
1378 1.1 dholland
1379 1.1 dholland /*
1380 1.1 dholland * Do the vnode op stuff for Open. Similar to nfsvno_createsub(), but
1381 1.1 dholland * must handle nfsrv_opencheck() calls after any other access checks.
1382 1.1 dholland */
1383 1.1 dholland void
1384 1.1 dholland nfsvno_open(struct nfsrv_descript *nd, struct nameidata *ndp,
1385 1.1 dholland nfsquad_t clientid, nfsv4stateid_t *stateidp, struct nfsstate *stp,
1386 1.1 dholland int *exclusive_flagp, struct nfsvattr *nvap, int32_t *cverf, int create,
1387 1.1 dholland NFSACL_T *aclp, nfsattrbit_t *attrbitp, struct ucred *cred, struct thread *p,
1388 1.1 dholland struct nfsexstuff *exp, struct vnode **vpp)
1389 1.1 dholland {
1390 1.1 dholland struct vnode *vp = NULL;
1391 1.1 dholland u_quad_t tempsize;
1392 1.1 dholland struct nfsexstuff nes;
1393 1.1 dholland
1394 1.1 dholland if (ndp->ni_vp == NULL)
1395 1.1 dholland nd->nd_repstat = nfsrv_opencheck(clientid,
1396 1.1 dholland stateidp, stp, NULL, nd, p, nd->nd_repstat);
1397 1.1 dholland if (!nd->nd_repstat) {
1398 1.1 dholland if (ndp->ni_vp == NULL) {
1399 1.1 dholland vrele(ndp->ni_startdir);
1400 1.1 dholland nd->nd_repstat = VOP_CREATE(ndp->ni_dvp,
1401 1.1 dholland &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr);
1402 1.1 dholland vput(ndp->ni_dvp);
1403 1.1 dholland nfsvno_relpathbuf(ndp);
1404 1.1 dholland if (!nd->nd_repstat) {
1405 1.1 dholland if (*exclusive_flagp) {
1406 1.1 dholland *exclusive_flagp = 0;
1407 1.1 dholland NFSVNO_ATTRINIT(nvap);
1408 1.1 dholland nvap->na_atime.tv_sec = cverf[0];
1409 1.1 dholland nvap->na_atime.tv_nsec = cverf[1];
1410 1.1 dholland nd->nd_repstat = VOP_SETATTR(ndp->ni_vp,
1411 1.1 dholland &nvap->na_vattr, cred);
1412 1.1 dholland } else {
1413 1.1 dholland nfsrv_fixattr(nd, ndp->ni_vp, nvap,
1414 1.1 dholland aclp, p, attrbitp, exp);
1415 1.1 dholland }
1416 1.1 dholland }
1417 1.1 dholland vp = ndp->ni_vp;
1418 1.1 dholland } else {
1419 1.1 dholland if (ndp->ni_startdir)
1420 1.1 dholland vrele(ndp->ni_startdir);
1421 1.1 dholland nfsvno_relpathbuf(ndp);
1422 1.1 dholland vp = ndp->ni_vp;
1423 1.1 dholland if (create == NFSV4OPEN_CREATE) {
1424 1.1 dholland if (ndp->ni_dvp == vp)
1425 1.1 dholland vrele(ndp->ni_dvp);
1426 1.1 dholland else
1427 1.1 dholland vput(ndp->ni_dvp);
1428 1.1 dholland }
1429 1.1 dholland if (NFSVNO_ISSETSIZE(nvap) && vp->v_type == VREG) {
1430 1.1 dholland if (ndp->ni_cnd.cn_flags & RDONLY)
1431 1.1 dholland NFSVNO_SETEXRDONLY(&nes);
1432 1.1 dholland else
1433 1.1 dholland NFSVNO_EXINIT(&nes);
1434 1.1 dholland nd->nd_repstat = nfsvno_accchk(vp,
1435 1.1 dholland VWRITE, cred, &nes, p,
1436 1.1 dholland NFSACCCHK_NOOVERRIDE,
1437 1.1 dholland NFSACCCHK_VPISLOCKED, NULL);
1438 1.1 dholland nd->nd_repstat = nfsrv_opencheck(clientid,
1439 1.1 dholland stateidp, stp, vp, nd, p, nd->nd_repstat);
1440 1.1 dholland if (!nd->nd_repstat) {
1441 1.1 dholland tempsize = nvap->na_size;
1442 1.1 dholland NFSVNO_ATTRINIT(nvap);
1443 1.1 dholland nvap->na_size = tempsize;
1444 1.1 dholland nd->nd_repstat = VOP_SETATTR(vp,
1445 1.1 dholland &nvap->na_vattr, cred);
1446 1.1 dholland }
1447 1.1 dholland } else if (vp->v_type == VREG) {
1448 1.1 dholland nd->nd_repstat = nfsrv_opencheck(clientid,
1449 1.1 dholland stateidp, stp, vp, nd, p, nd->nd_repstat);
1450 1.1 dholland }
1451 1.1 dholland }
1452 1.1 dholland } else {
1453 1.1 dholland if (ndp->ni_cnd.cn_flags & HASBUF)
1454 1.1 dholland nfsvno_relpathbuf(ndp);
1455 1.1 dholland if (ndp->ni_startdir && create == NFSV4OPEN_CREATE) {
1456 1.1 dholland vrele(ndp->ni_startdir);
1457 1.1 dholland if (ndp->ni_dvp == ndp->ni_vp)
1458 1.1 dholland vrele(ndp->ni_dvp);
1459 1.1 dholland else
1460 1.1 dholland vput(ndp->ni_dvp);
1461 1.1 dholland if (ndp->ni_vp)
1462 1.1 dholland vput(ndp->ni_vp);
1463 1.1 dholland }
1464 1.1 dholland }
1465 1.1 dholland *vpp = vp;
1466 1.1 dholland
1467 1.1 dholland NFSEXITCODE2(0, nd);
1468 1.1 dholland }
1469 1.1 dholland
1470 1.1 dholland /*
1471 1.1 dholland * Updates the file rev and sets the mtime and ctime
1472 1.1 dholland * to the current clock time, returning the va_filerev and va_Xtime
1473 1.1 dholland * values.
1474 1.1 dholland */
1475 1.1 dholland void
1476 1.1 dholland nfsvno_updfilerev(struct vnode *vp, struct nfsvattr *nvap,
1477 1.1 dholland struct ucred *cred, struct thread *p)
1478 1.1 dholland {
1479 1.1 dholland struct vattr va;
1480 1.1 dholland
1481 1.1 dholland VATTR_NULL(&va);
1482 1.1 dholland vfs_timestamp(&va.va_mtime);
1483 1.1 dholland (void) VOP_SETATTR(vp, &va, cred);
1484 1.1 dholland (void) nfsvno_getattr(vp, nvap, cred, p, 1);
1485 1.1 dholland }
1486 1.1 dholland
1487 1.1 dholland /*
1488 1.1 dholland * Glue routine to nfsv4_fillattr().
1489 1.1 dholland */
1490 1.1 dholland int
1491 1.1 dholland nfsvno_fillattr(struct nfsrv_descript *nd, struct mount *mp, struct vnode *vp,
1492 1.1 dholland struct nfsvattr *nvap, fhandle_t *fhp, int rderror, nfsattrbit_t *attrbitp,
1493 1.1 dholland struct ucred *cred, struct thread *p, int isdgram, int reterr,
1494 1.1 dholland int supports_nfsv4acls, int at_root, uint64_t mounted_on_fileno)
1495 1.1 dholland {
1496 1.1 dholland int error;
1497 1.1 dholland
1498 1.1 dholland error = nfsv4_fillattr(nd, mp, vp, NULL, &nvap->na_vattr, fhp, rderror,
1499 1.1 dholland attrbitp, cred, p, isdgram, reterr, supports_nfsv4acls, at_root,
1500 1.1 dholland mounted_on_fileno);
1501 1.1 dholland NFSEXITCODE2(0, nd);
1502 1.1 dholland return (error);
1503 1.1 dholland }
1504 1.1 dholland
1505 1.1 dholland /* Since the Readdir vnode ops vary, put the entire functions in here. */
1506 1.1 dholland /*
1507 1.1 dholland * nfs readdir service
1508 1.1 dholland * - mallocs what it thinks is enough to read
1509 1.1 dholland * count rounded up to a multiple of DIRBLKSIZ <= NFS_MAXREADDIR
1510 1.1 dholland * - calls VOP_READDIR()
1511 1.1 dholland * - loops around building the reply
1512 1.1 dholland * if the output generated exceeds count break out of loop
1513 1.1 dholland * The NFSM_CLGET macro is used here so that the reply will be packed
1514 1.1 dholland * tightly in mbuf clusters.
1515 1.1 dholland * - it trims out records with d_fileno == 0
1516 1.1 dholland * this doesn't matter for Unix clients, but they might confuse clients
1517 1.1 dholland * for other os'.
1518 1.1 dholland * - it trims out records with d_type == DT_WHT
1519 1.1 dholland * these cannot be seen through NFS (unless we extend the protocol)
1520 1.1 dholland * The alternate call nfsrvd_readdirplus() does lookups as well.
1521 1.1 dholland * PS: The NFS protocol spec. does not clarify what the "count" byte
1522 1.1 dholland * argument is a count of.. just name strings and file id's or the
1523 1.1 dholland * entire reply rpc or ...
1524 1.1 dholland * I tried just file name and id sizes and it confused the Sun client,
1525 1.1 dholland * so I am using the full rpc size now. The "paranoia.." comment refers
1526 1.1 dholland * to including the status longwords that are not a part of the dir.
1527 1.1 dholland * "entry" structures, but are in the rpc.
1528 1.1 dholland */
1529 1.1 dholland int
1530 1.1 dholland nfsrvd_readdir(struct nfsrv_descript *nd, int isdgram,
1531 1.1 dholland struct vnode *vp, struct thread *p, struct nfsexstuff *exp)
1532 1.1 dholland {
1533 1.1 dholland struct dirent *dp;
1534 1.1 dholland u_int32_t *tl;
1535 1.1 dholland int dirlen;
1536 1.1 dholland char *cpos, *cend, *rbuf;
1537 1.1 dholland struct nfsvattr at;
1538 1.1 dholland int nlen, error = 0, getret = 1;
1539 1.1 dholland int siz, cnt, fullsiz, eofflag, ncookies;
1540 1.1 dholland u_int64_t off, toff, verf;
1541 1.1 dholland u_long *cookies = NULL, *cookiep;
1542 1.1 dholland struct uio io;
1543 1.1 dholland struct iovec iv;
1544 1.1 dholland int not_zfs;
1545 1.1 dholland
1546 1.1 dholland if (nd->nd_repstat) {
1547 1.1 dholland nfsrv_postopattr(nd, getret, &at);
1548 1.1 dholland goto out;
1549 1.1 dholland }
1550 1.1 dholland if (nd->nd_flag & ND_NFSV2) {
1551 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1552 1.1 dholland off = fxdr_unsigned(u_quad_t, *tl++);
1553 1.1 dholland } else {
1554 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1555 1.1 dholland off = fxdr_hyper(tl);
1556 1.1 dholland tl += 2;
1557 1.1 dholland verf = fxdr_hyper(tl);
1558 1.1 dholland tl += 2;
1559 1.1 dholland }
1560 1.1 dholland toff = off;
1561 1.1 dholland cnt = fxdr_unsigned(int, *tl);
1562 1.1 dholland if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0)
1563 1.1 dholland cnt = NFS_SRVMAXDATA(nd);
1564 1.1 dholland siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
1565 1.1 dholland fullsiz = siz;
1566 1.1 dholland if (nd->nd_flag & ND_NFSV3) {
1567 1.1 dholland nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd->nd_cred,
1568 1.1 dholland p, 1);
1569 1.1 dholland #if 0
1570 1.1 dholland /*
1571 1.1 dholland * va_filerev is not sufficient as a cookie verifier,
1572 1.1 dholland * since it is not supposed to change when entries are
1573 1.1 dholland * removed/added unless that offset cookies returned to
1574 1.1 dholland * the client are no longer valid.
1575 1.1 dholland */
1576 1.1 dholland if (!nd->nd_repstat && toff && verf != at.na_filerev)
1577 1.1 dholland nd->nd_repstat = NFSERR_BAD_COOKIE;
1578 1.1 dholland #endif
1579 1.1 dholland }
1580 1.1 dholland if (!nd->nd_repstat && vp->v_type != VDIR)
1581 1.1 dholland nd->nd_repstat = NFSERR_NOTDIR;
1582 1.1 dholland if (nd->nd_repstat == 0 && cnt == 0) {
1583 1.1 dholland if (nd->nd_flag & ND_NFSV2)
1584 1.1 dholland /* NFSv2 does not have NFSERR_TOOSMALL */
1585 1.1 dholland nd->nd_repstat = EPERM;
1586 1.1 dholland else
1587 1.1 dholland nd->nd_repstat = NFSERR_TOOSMALL;
1588 1.1 dholland }
1589 1.1 dholland if (!nd->nd_repstat)
1590 1.1 dholland nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
1591 1.1 dholland nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
1592 1.1 dholland NFSACCCHK_VPISLOCKED, NULL);
1593 1.1 dholland if (nd->nd_repstat) {
1594 1.1 dholland vput(vp);
1595 1.1 dholland if (nd->nd_flag & ND_NFSV3)
1596 1.1 dholland nfsrv_postopattr(nd, getret, &at);
1597 1.1 dholland goto out;
1598 1.1 dholland }
1599 1.1 dholland not_zfs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "zfs");
1600 1.1 dholland MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
1601 1.1 dholland again:
1602 1.1 dholland eofflag = 0;
1603 1.1 dholland if (cookies) {
1604 1.1 dholland free((caddr_t)cookies, M_TEMP);
1605 1.1 dholland cookies = NULL;
1606 1.1 dholland }
1607 1.1 dholland
1608 1.1 dholland iv.iov_base = rbuf;
1609 1.1 dholland iv.iov_len = siz;
1610 1.1 dholland io.uio_iov = &iv;
1611 1.1 dholland io.uio_iovcnt = 1;
1612 1.1 dholland io.uio_offset = (off_t)off;
1613 1.1 dholland io.uio_resid = siz;
1614 1.1 dholland io.uio_segflg = UIO_SYSSPACE;
1615 1.1 dholland io.uio_rw = UIO_READ;
1616 1.1 dholland io.uio_td = NULL;
1617 1.1 dholland nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies,
1618 1.1 dholland &cookies);
1619 1.1 dholland off = (u_int64_t)io.uio_offset;
1620 1.1 dholland if (io.uio_resid)
1621 1.1 dholland siz -= io.uio_resid;
1622 1.1 dholland
1623 1.1 dholland if (!cookies && !nd->nd_repstat)
1624 1.1 dholland nd->nd_repstat = NFSERR_PERM;
1625 1.1 dholland if (nd->nd_flag & ND_NFSV3) {
1626 1.1 dholland getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1);
1627 1.1 dholland if (!nd->nd_repstat)
1628 1.1 dholland nd->nd_repstat = getret;
1629 1.1 dholland }
1630 1.1 dholland
1631 1.1 dholland /*
1632 1.1 dholland * Handles the failed cases. nd->nd_repstat == 0 past here.
1633 1.1 dholland */
1634 1.1 dholland if (nd->nd_repstat) {
1635 1.1 dholland vput(vp);
1636 1.1 dholland free((caddr_t)rbuf, M_TEMP);
1637 1.1 dholland if (cookies)
1638 1.1 dholland free((caddr_t)cookies, M_TEMP);
1639 1.1 dholland if (nd->nd_flag & ND_NFSV3)
1640 1.1 dholland nfsrv_postopattr(nd, getret, &at);
1641 1.1 dholland goto out;
1642 1.1 dholland }
1643 1.1 dholland /*
1644 1.1 dholland * If nothing read, return eof
1645 1.1 dholland * rpc reply
1646 1.1 dholland */
1647 1.1 dholland if (siz == 0) {
1648 1.1 dholland vput(vp);
1649 1.1 dholland if (nd->nd_flag & ND_NFSV2) {
1650 1.1 dholland NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1651 1.1 dholland } else {
1652 1.1 dholland nfsrv_postopattr(nd, getret, &at);
1653 1.1 dholland NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1654 1.1 dholland txdr_hyper(at.na_filerev, tl);
1655 1.1 dholland tl += 2;
1656 1.1 dholland }
1657 1.1 dholland *tl++ = newnfs_false;
1658 1.1 dholland *tl = newnfs_true;
1659 1.1 dholland FREE((caddr_t)rbuf, M_TEMP);
1660 1.1 dholland FREE((caddr_t)cookies, M_TEMP);
1661 1.1 dholland goto out;
1662 1.1 dholland }
1663 1.1 dholland
1664 1.1 dholland /*
1665 1.1 dholland * Check for degenerate cases of nothing useful read.
1666 1.1 dholland * If so go try again
1667 1.1 dholland */
1668 1.1 dholland cpos = rbuf;
1669 1.1 dholland cend = rbuf + siz;
1670 1.1 dholland dp = (struct dirent *)cpos;
1671 1.1 dholland cookiep = cookies;
1672 1.1 dholland
1673 1.1 dholland /*
1674 1.1 dholland * For some reason FreeBSD's ufs_readdir() chooses to back the
1675 1.1 dholland * directory offset up to a block boundary, so it is necessary to
1676 1.1 dholland * skip over the records that precede the requested offset. This
1677 1.1 dholland * requires the assumption that file offset cookies monotonically
1678 1.1 dholland * increase.
1679 1.1 dholland * Since the offset cookies don't monotonically increase for ZFS,
1680 1.1 dholland * this is not done when ZFS is the file system.
1681 1.1 dholland */
1682 1.1 dholland while (cpos < cend && ncookies > 0 &&
1683 1.1 dholland (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
1684 1.1 dholland (not_zfs != 0 && ((u_quad_t)(*cookiep)) <= toff))) {
1685 1.1 dholland cpos += dp->d_reclen;
1686 1.1 dholland dp = (struct dirent *)cpos;
1687 1.1 dholland cookiep++;
1688 1.1 dholland ncookies--;
1689 1.1 dholland }
1690 1.1 dholland if (cpos >= cend || ncookies == 0) {
1691 1.1 dholland siz = fullsiz;
1692 1.1 dholland toff = off;
1693 1.1 dholland goto again;
1694 1.1 dholland }
1695 1.1 dholland vput(vp);
1696 1.1 dholland
1697 1.1 dholland /*
1698 1.1 dholland * dirlen is the size of the reply, including all XDR and must
1699 1.1 dholland * not exceed cnt. For NFSv2, RFC1094 didn't clearly indicate
1700 1.1 dholland * if the XDR should be included in "count", but to be safe, we do.
1701 1.1 dholland * (Include the two booleans at the end of the reply in dirlen now.)
1702 1.1 dholland */
1703 1.1 dholland if (nd->nd_flag & ND_NFSV3) {
1704 1.1 dholland nfsrv_postopattr(nd, getret, &at);
1705 1.1 dholland NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1706 1.1 dholland txdr_hyper(at.na_filerev, tl);
1707 1.1 dholland dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED;
1708 1.1 dholland } else {
1709 1.1 dholland dirlen = 2 * NFSX_UNSIGNED;
1710 1.1 dholland }
1711 1.1 dholland
1712 1.1 dholland /* Loop through the records and build reply */
1713 1.1 dholland while (cpos < cend && ncookies > 0) {
1714 1.1 dholland nlen = dp->d_namlen;
1715 1.1 dholland if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
1716 1.1 dholland nlen <= NFS_MAXNAMLEN) {
1717 1.1 dholland if (nd->nd_flag & ND_NFSV3)
1718 1.1 dholland dirlen += (6*NFSX_UNSIGNED + NFSM_RNDUP(nlen));
1719 1.1 dholland else
1720 1.1 dholland dirlen += (4*NFSX_UNSIGNED + NFSM_RNDUP(nlen));
1721 1.1 dholland if (dirlen > cnt) {
1722 1.1 dholland eofflag = 0;
1723 1.1 dholland break;
1724 1.1 dholland }
1725 1.1 dholland
1726 1.1 dholland /*
1727 1.1 dholland * Build the directory record xdr from
1728 1.1 dholland * the dirent entry.
1729 1.1 dholland */
1730 1.1 dholland if (nd->nd_flag & ND_NFSV3) {
1731 1.1 dholland NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1732 1.1 dholland *tl++ = newnfs_true;
1733 1.1 dholland *tl++ = 0;
1734 1.1 dholland } else {
1735 1.1 dholland NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1736 1.1 dholland *tl++ = newnfs_true;
1737 1.1 dholland }
1738 1.1 dholland *tl = txdr_unsigned(dp->d_fileno);
1739 1.1 dholland (void) nfsm_strtom(nd, dp->d_name, nlen);
1740 1.1 dholland if (nd->nd_flag & ND_NFSV3) {
1741 1.1 dholland NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1742 1.1 dholland *tl++ = 0;
1743 1.1 dholland } else
1744 1.1 dholland NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1745 1.1 dholland *tl = txdr_unsigned(*cookiep);
1746 1.1 dholland }
1747 1.1 dholland cpos += dp->d_reclen;
1748 1.1 dholland dp = (struct dirent *)cpos;
1749 1.1 dholland cookiep++;
1750 1.1 dholland ncookies--;
1751 1.1 dholland }
1752 1.1 dholland if (cpos < cend)
1753 1.1 dholland eofflag = 0;
1754 1.1 dholland NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1755 1.1 dholland *tl++ = newnfs_false;
1756 1.1 dholland if (eofflag)
1757 1.1 dholland *tl = newnfs_true;
1758 1.1 dholland else
1759 1.1 dholland *tl = newnfs_false;
1760 1.1 dholland FREE((caddr_t)rbuf, M_TEMP);
1761 1.1 dholland FREE((caddr_t)cookies, M_TEMP);
1762 1.1 dholland
1763 1.1 dholland out:
1764 1.1 dholland NFSEXITCODE2(0, nd);
1765 1.1 dholland return (0);
1766 1.1 dholland nfsmout:
1767 1.1 dholland vput(vp);
1768 1.1 dholland NFSEXITCODE2(error, nd);
1769 1.1 dholland return (error);
1770 1.1 dholland }
1771 1.1 dholland
1772 1.1 dholland /*
1773 1.1 dholland * Readdirplus for V3 and Readdir for V4.
1774 1.1 dholland */
1775 1.1 dholland int
1776 1.1 dholland nfsrvd_readdirplus(struct nfsrv_descript *nd, int isdgram,
1777 1.1 dholland struct vnode *vp, struct thread *p, struct nfsexstuff *exp)
1778 1.1 dholland {
1779 1.1 dholland struct dirent *dp;
1780 1.1 dholland u_int32_t *tl;
1781 1.1 dholland int dirlen;
1782 1.1 dholland char *cpos, *cend, *rbuf;
1783 1.1 dholland struct vnode *nvp;
1784 1.1 dholland fhandle_t nfh;
1785 1.1 dholland struct nfsvattr nva, at, *nvap = &nva;
1786 1.1 dholland struct mbuf *mb0, *mb1;
1787 1.1 dholland struct nfsreferral *refp;
1788 1.1 dholland int nlen, r, error = 0, getret = 1, usevget = 1;
1789 1.1 dholland int siz, cnt, fullsiz, eofflag, ncookies, entrycnt;
1790 1.1 dholland caddr_t bpos0, bpos1;
1791 1.1 dholland u_int64_t off, toff, verf;
1792 1.1 dholland u_long *cookies = NULL, *cookiep;
1793 1.1 dholland nfsattrbit_t attrbits, rderrbits, savbits;
1794 1.1 dholland struct uio io;
1795 1.1 dholland struct iovec iv;
1796 1.1 dholland struct componentname cn;
1797 1.1 dholland int at_root, needs_unbusy, not_zfs, supports_nfsv4acls;
1798 1.1 dholland struct mount *mp, *new_mp;
1799 1.1 dholland uint64_t mounted_on_fileno;
1800 1.1 dholland
1801 1.1 dholland if (nd->nd_repstat) {
1802 1.1 dholland nfsrv_postopattr(nd, getret, &at);
1803 1.1 dholland goto out;
1804 1.1 dholland }
1805 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
1806 1.1 dholland off = fxdr_hyper(tl);
1807 1.1 dholland toff = off;
1808 1.1 dholland tl += 2;
1809 1.1 dholland verf = fxdr_hyper(tl);
1810 1.1 dholland tl += 2;
1811 1.1 dholland siz = fxdr_unsigned(int, *tl++);
1812 1.1 dholland cnt = fxdr_unsigned(int, *tl);
1813 1.1 dholland
1814 1.1 dholland /*
1815 1.1 dholland * Use the server's maximum data transfer size as the upper bound
1816 1.1 dholland * on reply datalen.
1817 1.1 dholland */
1818 1.1 dholland if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0)
1819 1.1 dholland cnt = NFS_SRVMAXDATA(nd);
1820 1.1 dholland
1821 1.1 dholland /*
1822 1.1 dholland * siz is a "hint" of how much directory information (name, fileid,
1823 1.1 dholland * cookie) should be in the reply. At least one client "hints" 0,
1824 1.1 dholland * so I set it to cnt for that case. I also round it up to the
1825 1.1 dholland * next multiple of DIRBLKSIZ.
1826 1.1 dholland */
1827 1.1 dholland if (siz <= 0)
1828 1.1 dholland siz = cnt;
1829 1.1 dholland siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
1830 1.1 dholland
1831 1.1 dholland if (nd->nd_flag & ND_NFSV4) {
1832 1.1 dholland error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
1833 1.1 dholland if (error)
1834 1.1 dholland goto nfsmout;
1835 1.1 dholland NFSSET_ATTRBIT(&savbits, &attrbits);
1836 1.1 dholland NFSCLRNOTFILLABLE_ATTRBIT(&attrbits);
1837 1.1 dholland NFSZERO_ATTRBIT(&rderrbits);
1838 1.1 dholland NFSSETBIT_ATTRBIT(&rderrbits, NFSATTRBIT_RDATTRERROR);
1839 1.1 dholland } else {
1840 1.1 dholland NFSZERO_ATTRBIT(&attrbits);
1841 1.1 dholland }
1842 1.1 dholland fullsiz = siz;
1843 1.1 dholland nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1);
1844 1.1 dholland if (!nd->nd_repstat) {
1845 1.1 dholland if (off && verf != at.na_filerev) {
1846 1.1 dholland /*
1847 1.1 dholland * va_filerev is not sufficient as a cookie verifier,
1848 1.1 dholland * since it is not supposed to change when entries are
1849 1.1 dholland * removed/added unless that offset cookies returned to
1850 1.1 dholland * the client are no longer valid.
1851 1.1 dholland */
1852 1.1 dholland #if 0
1853 1.1 dholland if (nd->nd_flag & ND_NFSV4) {
1854 1.1 dholland nd->nd_repstat = NFSERR_NOTSAME;
1855 1.1 dholland } else {
1856 1.1 dholland nd->nd_repstat = NFSERR_BAD_COOKIE;
1857 1.1 dholland }
1858 1.1 dholland #endif
1859 1.1 dholland } else if ((nd->nd_flag & ND_NFSV4) && off == 0 && verf != 0) {
1860 1.1 dholland nd->nd_repstat = NFSERR_BAD_COOKIE;
1861 1.1 dholland }
1862 1.1 dholland }
1863 1.1 dholland if (!nd->nd_repstat && vp->v_type != VDIR)
1864 1.1 dholland nd->nd_repstat = NFSERR_NOTDIR;
1865 1.1 dholland if (!nd->nd_repstat && cnt == 0)
1866 1.1 dholland nd->nd_repstat = NFSERR_TOOSMALL;
1867 1.1 dholland if (!nd->nd_repstat)
1868 1.1 dholland nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
1869 1.1 dholland nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
1870 1.1 dholland NFSACCCHK_VPISLOCKED, NULL);
1871 1.1 dholland if (nd->nd_repstat) {
1872 1.1 dholland vput(vp);
1873 1.1 dholland if (nd->nd_flag & ND_NFSV3)
1874 1.1 dholland nfsrv_postopattr(nd, getret, &at);
1875 1.1 dholland goto out;
1876 1.1 dholland }
1877 1.1 dholland not_zfs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "zfs");
1878 1.1 dholland
1879 1.1 dholland MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
1880 1.1 dholland again:
1881 1.1 dholland eofflag = 0;
1882 1.1 dholland if (cookies) {
1883 1.1 dholland free((caddr_t)cookies, M_TEMP);
1884 1.1 dholland cookies = NULL;
1885 1.1 dholland }
1886 1.1 dholland
1887 1.1 dholland iv.iov_base = rbuf;
1888 1.1 dholland iv.iov_len = siz;
1889 1.1 dholland io.uio_iov = &iv;
1890 1.1 dholland io.uio_iovcnt = 1;
1891 1.1 dholland io.uio_offset = (off_t)off;
1892 1.1 dholland io.uio_resid = siz;
1893 1.1 dholland io.uio_segflg = UIO_SYSSPACE;
1894 1.1 dholland io.uio_rw = UIO_READ;
1895 1.1 dholland io.uio_td = NULL;
1896 1.1 dholland nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies,
1897 1.1 dholland &cookies);
1898 1.1 dholland off = (u_int64_t)io.uio_offset;
1899 1.1 dholland if (io.uio_resid)
1900 1.1 dholland siz -= io.uio_resid;
1901 1.1 dholland
1902 1.1 dholland getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1);
1903 1.1 dholland
1904 1.1 dholland if (!cookies && !nd->nd_repstat)
1905 1.1 dholland nd->nd_repstat = NFSERR_PERM;
1906 1.1 dholland if (!nd->nd_repstat)
1907 1.1 dholland nd->nd_repstat = getret;
1908 1.1 dholland if (nd->nd_repstat) {
1909 1.1 dholland vput(vp);
1910 1.1 dholland if (cookies)
1911 1.1 dholland free((caddr_t)cookies, M_TEMP);
1912 1.1 dholland free((caddr_t)rbuf, M_TEMP);
1913 1.1 dholland if (nd->nd_flag & ND_NFSV3)
1914 1.1 dholland nfsrv_postopattr(nd, getret, &at);
1915 1.1 dholland goto out;
1916 1.1 dholland }
1917 1.1 dholland /*
1918 1.1 dholland * If nothing read, return eof
1919 1.1 dholland * rpc reply
1920 1.1 dholland */
1921 1.1 dholland if (siz == 0) {
1922 1.1 dholland vput(vp);
1923 1.1 dholland if (nd->nd_flag & ND_NFSV3)
1924 1.1 dholland nfsrv_postopattr(nd, getret, &at);
1925 1.1 dholland NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1926 1.1 dholland txdr_hyper(at.na_filerev, tl);
1927 1.1 dholland tl += 2;
1928 1.1 dholland *tl++ = newnfs_false;
1929 1.1 dholland *tl = newnfs_true;
1930 1.1 dholland free((caddr_t)cookies, M_TEMP);
1931 1.1 dholland free((caddr_t)rbuf, M_TEMP);
1932 1.1 dholland goto out;
1933 1.1 dholland }
1934 1.1 dholland
1935 1.1 dholland /*
1936 1.1 dholland * Check for degenerate cases of nothing useful read.
1937 1.1 dholland * If so go try again
1938 1.1 dholland */
1939 1.1 dholland cpos = rbuf;
1940 1.1 dholland cend = rbuf + siz;
1941 1.1 dholland dp = (struct dirent *)cpos;
1942 1.1 dholland cookiep = cookies;
1943 1.1 dholland
1944 1.1 dholland /*
1945 1.1 dholland * For some reason FreeBSD's ufs_readdir() chooses to back the
1946 1.1 dholland * directory offset up to a block boundary, so it is necessary to
1947 1.1 dholland * skip over the records that precede the requested offset. This
1948 1.1 dholland * requires the assumption that file offset cookies monotonically
1949 1.1 dholland * increase.
1950 1.1 dholland * Since the offset cookies don't monotonically increase for ZFS,
1951 1.1 dholland * this is not done when ZFS is the file system.
1952 1.1 dholland */
1953 1.1 dholland while (cpos < cend && ncookies > 0 &&
1954 1.1 dholland (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
1955 1.1 dholland (not_zfs != 0 && ((u_quad_t)(*cookiep)) <= toff) ||
1956 1.1 dholland ((nd->nd_flag & ND_NFSV4) &&
1957 1.1 dholland ((dp->d_namlen == 1 && dp->d_name[0] == '.') ||
1958 1.1 dholland (dp->d_namlen==2 && dp->d_name[0]=='.' && dp->d_name[1]=='.'))))) {
1959 1.1 dholland cpos += dp->d_reclen;
1960 1.1 dholland dp = (struct dirent *)cpos;
1961 1.1 dholland cookiep++;
1962 1.1 dholland ncookies--;
1963 1.1 dholland }
1964 1.1 dholland if (cpos >= cend || ncookies == 0) {
1965 1.1 dholland siz = fullsiz;
1966 1.1 dholland toff = off;
1967 1.1 dholland goto again;
1968 1.1 dholland }
1969 1.1 dholland
1970 1.1 dholland /*
1971 1.1 dholland * Busy the file system so that the mount point won't go away
1972 1.1 dholland * and, as such, VFS_VGET() can be used safely.
1973 1.1 dholland */
1974 1.1 dholland mp = vp->v_mount;
1975 1.1 dholland vfs_ref(mp);
1976 1.1 dholland NFSVOPUNLOCK(vp, 0);
1977 1.1 dholland nd->nd_repstat = vfs_busy(mp, 0);
1978 1.1 dholland vfs_rel(mp);
1979 1.1 dholland if (nd->nd_repstat != 0) {
1980 1.1 dholland vrele(vp);
1981 1.1 dholland free(cookies, M_TEMP);
1982 1.1 dholland free(rbuf, M_TEMP);
1983 1.1 dholland if (nd->nd_flag & ND_NFSV3)
1984 1.1 dholland nfsrv_postopattr(nd, getret, &at);
1985 1.1 dholland goto out;
1986 1.1 dholland }
1987 1.1 dholland
1988 1.1 dholland /*
1989 1.1 dholland * Save this position, in case there is an error before one entry
1990 1.1 dholland * is created.
1991 1.1 dholland */
1992 1.1 dholland mb0 = nd->nd_mb;
1993 1.1 dholland bpos0 = nd->nd_bpos;
1994 1.1 dholland
1995 1.1 dholland /*
1996 1.1 dholland * Fill in the first part of the reply.
1997 1.1 dholland * dirlen is the reply length in bytes and cannot exceed cnt.
1998 1.1 dholland * (Include the two booleans at the end of the reply in dirlen now,
1999 1.1 dholland * so we recognize when we have exceeded cnt.)
2000 1.1 dholland */
2001 1.1 dholland if (nd->nd_flag & ND_NFSV3) {
2002 1.1 dholland dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED;
2003 1.1 dholland nfsrv_postopattr(nd, getret, &at);
2004 1.1 dholland } else {
2005 1.1 dholland dirlen = NFSX_VERF + 2 * NFSX_UNSIGNED;
2006 1.1 dholland }
2007 1.1 dholland NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2008 1.1 dholland txdr_hyper(at.na_filerev, tl);
2009 1.1 dholland
2010 1.1 dholland /*
2011 1.1 dholland * Save this position, in case there is an empty reply needed.
2012 1.1 dholland */
2013 1.1 dholland mb1 = nd->nd_mb;
2014 1.1 dholland bpos1 = nd->nd_bpos;
2015 1.1 dholland
2016 1.1 dholland /* Loop through the records and build reply */
2017 1.1 dholland entrycnt = 0;
2018 1.1 dholland while (cpos < cend && ncookies > 0 && dirlen < cnt) {
2019 1.1 dholland nlen = dp->d_namlen;
2020 1.1 dholland if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
2021 1.1 dholland nlen <= NFS_MAXNAMLEN &&
2022 1.1 dholland ((nd->nd_flag & ND_NFSV3) || nlen > 2 ||
2023 1.1 dholland (nlen==2 && (dp->d_name[0]!='.' || dp->d_name[1]!='.'))
2024 1.1 dholland || (nlen == 1 && dp->d_name[0] != '.'))) {
2025 1.1 dholland /*
2026 1.1 dholland * Save the current position in the reply, in case
2027 1.1 dholland * this entry exceeds cnt.
2028 1.1 dholland */
2029 1.1 dholland mb1 = nd->nd_mb;
2030 1.1 dholland bpos1 = nd->nd_bpos;
2031 1.1 dholland
2032 1.1 dholland /*
2033 1.1 dholland * For readdir_and_lookup get the vnode using
2034 1.1 dholland * the file number.
2035 1.1 dholland */
2036 1.1 dholland nvp = NULL;
2037 1.1 dholland refp = NULL;
2038 1.1 dholland r = 0;
2039 1.1 dholland at_root = 0;
2040 1.1 dholland needs_unbusy = 0;
2041 1.1 dholland new_mp = mp;
2042 1.1 dholland mounted_on_fileno = (uint64_t)dp->d_fileno;
2043 1.1 dholland if ((nd->nd_flag & ND_NFSV3) ||
2044 1.1 dholland NFSNONZERO_ATTRBIT(&savbits)) {
2045 1.1 dholland if (nd->nd_flag & ND_NFSV4)
2046 1.1 dholland refp = nfsv4root_getreferral(NULL,
2047 1.1 dholland vp, dp->d_fileno);
2048 1.1 dholland if (refp == NULL) {
2049 1.1 dholland if (usevget)
2050 1.1 dholland r = VFS_VGET(mp, dp->d_fileno,
2051 1.1 dholland LK_SHARED, &nvp);
2052 1.1 dholland else
2053 1.1 dholland r = EOPNOTSUPP;
2054 1.1 dholland if (r == EOPNOTSUPP) {
2055 1.1 dholland if (usevget) {
2056 1.1 dholland usevget = 0;
2057 1.1 dholland cn.cn_nameiop = LOOKUP;
2058 1.1 dholland cn.cn_lkflags =
2059 1.1 dholland LK_SHARED |
2060 1.1 dholland LK_RETRY;
2061 1.1 dholland cn.cn_cred =
2062 1.1 dholland nd->nd_cred;
2063 1.1 dholland cn.cn_thread = p;
2064 1.1 dholland }
2065 1.1 dholland cn.cn_nameptr = dp->d_name;
2066 1.1 dholland cn.cn_namelen = nlen;
2067 1.1 dholland cn.cn_flags = ISLASTCN |
2068 1.1 dholland NOFOLLOW | LOCKLEAF;
2069 1.1 dholland if (nlen == 2 &&
2070 1.1 dholland dp->d_name[0] == '.' &&
2071 1.1 dholland dp->d_name[1] == '.')
2072 1.1 dholland cn.cn_flags |=
2073 1.1 dholland ISDOTDOT;
2074 1.1 dholland if (NFSVOPLOCK(vp, LK_SHARED)
2075 1.1 dholland != 0) {
2076 1.1 dholland nd->nd_repstat = EPERM;
2077 1.1 dholland break;
2078 1.1 dholland }
2079 1.1 dholland if ((vp->v_vflag & VV_ROOT) != 0
2080 1.1 dholland && (cn.cn_flags & ISDOTDOT)
2081 1.1 dholland != 0) {
2082 1.1 dholland vref(vp);
2083 1.1 dholland nvp = vp;
2084 1.1 dholland r = 0;
2085 1.1 dholland } else {
2086 1.1 dholland r = VOP_LOOKUP(vp, &nvp,
2087 1.1 dholland &cn);
2088 1.1 dholland if (vp != nvp)
2089 1.1 dholland NFSVOPUNLOCK(vp,
2090 1.1 dholland 0);
2091 1.1 dholland }
2092 1.1 dholland }
2093 1.1 dholland
2094 1.1 dholland /*
2095 1.1 dholland * For NFSv4, check to see if nvp is
2096 1.1 dholland * a mount point and get the mount
2097 1.1 dholland * point vnode, as required.
2098 1.1 dholland */
2099 1.1 dholland if (r == 0 &&
2100 1.1 dholland nfsrv_enable_crossmntpt != 0 &&
2101 1.1 dholland (nd->nd_flag & ND_NFSV4) != 0 &&
2102 1.1 dholland nvp->v_type == VDIR &&
2103 1.1 dholland nvp->v_mountedhere != NULL) {
2104 1.1 dholland new_mp = nvp->v_mountedhere;
2105 1.1 dholland r = vfs_busy(new_mp, 0);
2106 1.1 dholland vput(nvp);
2107 1.1 dholland nvp = NULL;
2108 1.1 dholland if (r == 0) {
2109 1.1 dholland r = VFS_ROOT(new_mp,
2110 1.1 dholland LK_SHARED, &nvp);
2111 1.1 dholland needs_unbusy = 1;
2112 1.1 dholland if (r == 0)
2113 1.1 dholland at_root = 1;
2114 1.1 dholland }
2115 1.1 dholland }
2116 1.1 dholland }
2117 1.1 dholland if (!r) {
2118 1.1 dholland if (refp == NULL &&
2119 1.1 dholland ((nd->nd_flag & ND_NFSV3) ||
2120 1.1 dholland NFSNONZERO_ATTRBIT(&attrbits))) {
2121 1.1 dholland r = nfsvno_getfh(nvp, &nfh, p);
2122 1.1 dholland if (!r)
2123 1.1 dholland r = nfsvno_getattr(nvp, nvap,
2124 1.1 dholland nd->nd_cred, p, 1);
2125 1.1 dholland }
2126 1.1 dholland } else {
2127 1.1 dholland nvp = NULL;
2128 1.1 dholland }
2129 1.1 dholland if (r) {
2130 1.1 dholland if (!NFSISSET_ATTRBIT(&attrbits,
2131 1.1 dholland NFSATTRBIT_RDATTRERROR)) {
2132 1.1 dholland if (nvp != NULL)
2133 1.1 dholland vput(nvp);
2134 1.1 dholland if (needs_unbusy != 0)
2135 1.1 dholland vfs_unbusy(new_mp);
2136 1.1 dholland nd->nd_repstat = r;
2137 1.1 dholland break;
2138 1.1 dholland }
2139 1.1 dholland }
2140 1.1 dholland }
2141 1.1 dholland
2142 1.1 dholland /*
2143 1.1 dholland * Build the directory record xdr
2144 1.1 dholland */
2145 1.1 dholland if (nd->nd_flag & ND_NFSV3) {
2146 1.1 dholland NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2147 1.1 dholland *tl++ = newnfs_true;
2148 1.1 dholland *tl++ = 0;
2149 1.1 dholland *tl = txdr_unsigned(dp->d_fileno);
2150 1.1 dholland dirlen += nfsm_strtom(nd, dp->d_name, nlen);
2151 1.1 dholland NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2152 1.1 dholland *tl++ = 0;
2153 1.1 dholland *tl = txdr_unsigned(*cookiep);
2154 1.1 dholland nfsrv_postopattr(nd, 0, nvap);
2155 1.1 dholland dirlen += nfsm_fhtom(nd,(u_int8_t *)&nfh,0,1);
2156 1.1 dholland dirlen += (5*NFSX_UNSIGNED+NFSX_V3POSTOPATTR);
2157 1.1 dholland if (nvp != NULL)
2158 1.1 dholland vput(nvp);
2159 1.1 dholland } else {
2160 1.1 dholland NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2161 1.1 dholland *tl++ = newnfs_true;
2162 1.1 dholland *tl++ = 0;
2163 1.1 dholland *tl = txdr_unsigned(*cookiep);
2164 1.1 dholland dirlen += nfsm_strtom(nd, dp->d_name, nlen);
2165 1.1 dholland if (nvp != NULL) {
2166 1.1 dholland supports_nfsv4acls =
2167 1.1 dholland nfs_supportsnfsv4acls(nvp);
2168 1.1 dholland NFSVOPUNLOCK(nvp, 0);
2169 1.1 dholland } else
2170 1.1 dholland supports_nfsv4acls = 0;
2171 1.1 dholland if (refp != NULL) {
2172 1.1 dholland dirlen += nfsrv_putreferralattr(nd,
2173 1.1 dholland &savbits, refp, 0,
2174 1.1 dholland &nd->nd_repstat);
2175 1.1 dholland if (nd->nd_repstat) {
2176 1.1 dholland if (nvp != NULL)
2177 1.1 dholland vrele(nvp);
2178 1.1 dholland if (needs_unbusy != 0)
2179 1.1 dholland vfs_unbusy(new_mp);
2180 1.1 dholland break;
2181 1.1 dholland }
2182 1.1 dholland } else if (r) {
2183 1.1 dholland dirlen += nfsvno_fillattr(nd, new_mp,
2184 1.1 dholland nvp, nvap, &nfh, r, &rderrbits,
2185 1.1 dholland nd->nd_cred, p, isdgram, 0,
2186 1.1 dholland supports_nfsv4acls, at_root,
2187 1.1 dholland mounted_on_fileno);
2188 1.1 dholland } else {
2189 1.1 dholland dirlen += nfsvno_fillattr(nd, new_mp,
2190 1.1 dholland nvp, nvap, &nfh, r, &attrbits,
2191 1.1 dholland nd->nd_cred, p, isdgram, 0,
2192 1.1 dholland supports_nfsv4acls, at_root,
2193 1.1 dholland mounted_on_fileno);
2194 1.1 dholland }
2195 1.1 dholland if (nvp != NULL)
2196 1.1 dholland vrele(nvp);
2197 1.1 dholland dirlen += (3 * NFSX_UNSIGNED);
2198 1.1 dholland }
2199 1.1 dholland if (needs_unbusy != 0)
2200 1.1 dholland vfs_unbusy(new_mp);
2201 1.1 dholland if (dirlen <= cnt)
2202 1.1 dholland entrycnt++;
2203 1.1 dholland }
2204 1.1 dholland cpos += dp->d_reclen;
2205 1.1 dholland dp = (struct dirent *)cpos;
2206 1.1 dholland cookiep++;
2207 1.1 dholland ncookies--;
2208 1.1 dholland }
2209 1.1 dholland vrele(vp);
2210 1.1 dholland vfs_unbusy(mp);
2211 1.1 dholland
2212 1.1 dholland /*
2213 1.1 dholland * If dirlen > cnt, we must strip off the last entry. If that
2214 1.1 dholland * results in an empty reply, report NFSERR_TOOSMALL.
2215 1.1 dholland */
2216 1.1 dholland if (dirlen > cnt || nd->nd_repstat) {
2217 1.1 dholland if (!nd->nd_repstat && entrycnt == 0)
2218 1.1 dholland nd->nd_repstat = NFSERR_TOOSMALL;
2219 1.1 dholland if (nd->nd_repstat)
2220 1.1 dholland newnfs_trimtrailing(nd, mb0, bpos0);
2221 1.1 dholland else
2222 1.1 dholland newnfs_trimtrailing(nd, mb1, bpos1);
2223 1.1 dholland eofflag = 0;
2224 1.1 dholland } else if (cpos < cend)
2225 1.1 dholland eofflag = 0;
2226 1.1 dholland if (!nd->nd_repstat) {
2227 1.1 dholland NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2228 1.1 dholland *tl++ = newnfs_false;
2229 1.1 dholland if (eofflag)
2230 1.1 dholland *tl = newnfs_true;
2231 1.1 dholland else
2232 1.1 dholland *tl = newnfs_false;
2233 1.1 dholland }
2234 1.1 dholland FREE((caddr_t)cookies, M_TEMP);
2235 1.1 dholland FREE((caddr_t)rbuf, M_TEMP);
2236 1.1 dholland
2237 1.1 dholland out:
2238 1.1 dholland NFSEXITCODE2(0, nd);
2239 1.1 dholland return (0);
2240 1.1 dholland nfsmout:
2241 1.1 dholland vput(vp);
2242 1.1 dholland NFSEXITCODE2(error, nd);
2243 1.1 dholland return (error);
2244 1.1 dholland }
2245 1.1 dholland
2246 1.1 dholland /*
2247 1.1 dholland * Get the settable attributes out of the mbuf list.
2248 1.1 dholland * (Return 0 or EBADRPC)
2249 1.1 dholland */
2250 1.1 dholland int
2251 1.1 dholland nfsrv_sattr(struct nfsrv_descript *nd, struct nfsvattr *nvap,
2252 1.1 dholland nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p)
2253 1.1 dholland {
2254 1.1 dholland u_int32_t *tl;
2255 1.1 dholland struct nfsv2_sattr *sp;
2256 1.1 dholland int error = 0, toclient = 0;
2257 1.1 dholland
2258 1.1 dholland switch (nd->nd_flag & (ND_NFSV2 | ND_NFSV3 | ND_NFSV4)) {
2259 1.1 dholland case ND_NFSV2:
2260 1.1 dholland NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2261 1.1 dholland /*
2262 1.1 dholland * Some old clients didn't fill in the high order 16bits.
2263 1.1 dholland * --> check the low order 2 bytes for 0xffff
2264 1.1 dholland */
2265 1.1 dholland if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
2266 1.1 dholland nvap->na_mode = nfstov_mode(sp->sa_mode);
2267 1.1 dholland if (sp->sa_uid != newnfs_xdrneg1)
2268 1.1 dholland nvap->na_uid = fxdr_unsigned(uid_t, sp->sa_uid);
2269 1.1 dholland if (sp->sa_gid != newnfs_xdrneg1)
2270 1.1 dholland nvap->na_gid = fxdr_unsigned(gid_t, sp->sa_gid);
2271 1.1 dholland if (sp->sa_size != newnfs_xdrneg1)
2272 1.1 dholland nvap->na_size = fxdr_unsigned(u_quad_t, sp->sa_size);
2273 1.1 dholland if (sp->sa_atime.nfsv2_sec != newnfs_xdrneg1) {
2274 1.1 dholland #ifdef notyet
2275 1.1 dholland fxdr_nfsv2time(&sp->sa_atime, &nvap->na_atime);
2276 1.1 dholland #else
2277 1.1 dholland nvap->na_atime.tv_sec =
2278 1.1 dholland fxdr_unsigned(u_int32_t,sp->sa_atime.nfsv2_sec);
2279 1.1 dholland nvap->na_atime.tv_nsec = 0;
2280 1.1 dholland #endif
2281 1.1 dholland }
2282 1.1 dholland if (sp->sa_mtime.nfsv2_sec != newnfs_xdrneg1)
2283 1.1 dholland fxdr_nfsv2time(&sp->sa_mtime, &nvap->na_mtime);
2284 1.1 dholland break;
2285 1.1 dholland case ND_NFSV3:
2286 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2287 1.1 dholland if (*tl == newnfs_true) {
2288 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2289 1.1 dholland nvap->na_mode = nfstov_mode(*tl);
2290 1.1 dholland }
2291 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2292 1.1 dholland if (*tl == newnfs_true) {
2293 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2294 1.1 dholland nvap->na_uid = fxdr_unsigned(uid_t, *tl);
2295 1.1 dholland }
2296 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2297 1.1 dholland if (*tl == newnfs_true) {
2298 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2299 1.1 dholland nvap->na_gid = fxdr_unsigned(gid_t, *tl);
2300 1.1 dholland }
2301 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2302 1.1 dholland if (*tl == newnfs_true) {
2303 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2304 1.1 dholland nvap->na_size = fxdr_hyper(tl);
2305 1.1 dholland }
2306 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2307 1.1 dholland switch (fxdr_unsigned(int, *tl)) {
2308 1.1 dholland case NFSV3SATTRTIME_TOCLIENT:
2309 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2310 1.1 dholland fxdr_nfsv3time(tl, &nvap->na_atime);
2311 1.1 dholland toclient = 1;
2312 1.1 dholland break;
2313 1.1 dholland case NFSV3SATTRTIME_TOSERVER:
2314 1.1 dholland vfs_timestamp(&nvap->na_atime);
2315 1.1 dholland nvap->na_vaflags |= VA_UTIMES_NULL;
2316 1.1 dholland break;
2317 1.1 dholland };
2318 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2319 1.1 dholland switch (fxdr_unsigned(int, *tl)) {
2320 1.1 dholland case NFSV3SATTRTIME_TOCLIENT:
2321 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2322 1.1 dholland fxdr_nfsv3time(tl, &nvap->na_mtime);
2323 1.1 dholland nvap->na_vaflags &= ~VA_UTIMES_NULL;
2324 1.1 dholland break;
2325 1.1 dholland case NFSV3SATTRTIME_TOSERVER:
2326 1.1 dholland vfs_timestamp(&nvap->na_mtime);
2327 1.1 dholland if (!toclient)
2328 1.1 dholland nvap->na_vaflags |= VA_UTIMES_NULL;
2329 1.1 dholland break;
2330 1.1 dholland };
2331 1.1 dholland break;
2332 1.1 dholland case ND_NFSV4:
2333 1.1 dholland error = nfsv4_sattr(nd, nvap, attrbitp, aclp, p);
2334 1.1 dholland };
2335 1.1 dholland nfsmout:
2336 1.1 dholland NFSEXITCODE2(error, nd);
2337 1.1 dholland return (error);
2338 1.1 dholland }
2339 1.1 dholland
2340 1.1 dholland /*
2341 1.1 dholland * Handle the setable attributes for V4.
2342 1.1 dholland * Returns NFSERR_BADXDR if it can't be parsed, 0 otherwise.
2343 1.1 dholland */
2344 1.1 dholland int
2345 1.1 dholland nfsv4_sattr(struct nfsrv_descript *nd, struct nfsvattr *nvap,
2346 1.1 dholland nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p)
2347 1.1 dholland {
2348 1.1 dholland u_int32_t *tl;
2349 1.1 dholland int attrsum = 0;
2350 1.1 dholland int i, j;
2351 1.1 dholland int error, attrsize, bitpos, aclsize, aceerr, retnotsup = 0;
2352 1.1 dholland int toclient = 0;
2353 1.1 dholland u_char *cp, namestr[NFSV4_SMALLSTR + 1];
2354 1.1 dholland uid_t uid;
2355 1.1 dholland gid_t gid;
2356 1.1 dholland
2357 1.1 dholland error = nfsrv_getattrbits(nd, attrbitp, NULL, &retnotsup);
2358 1.1 dholland if (error)
2359 1.1 dholland goto nfsmout;
2360 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2361 1.1 dholland attrsize = fxdr_unsigned(int, *tl);
2362 1.1 dholland
2363 1.1 dholland /*
2364 1.1 dholland * Loop around getting the setable attributes. If an unsupported
2365 1.1 dholland * one is found, set nd_repstat == NFSERR_ATTRNOTSUPP and return.
2366 1.1 dholland */
2367 1.1 dholland if (retnotsup) {
2368 1.1 dholland nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2369 1.1 dholland bitpos = NFSATTRBIT_MAX;
2370 1.1 dholland } else {
2371 1.1 dholland bitpos = 0;
2372 1.1 dholland }
2373 1.1 dholland for (; bitpos < NFSATTRBIT_MAX; bitpos++) {
2374 1.1 dholland if (attrsum > attrsize) {
2375 1.1 dholland error = NFSERR_BADXDR;
2376 1.1 dholland goto nfsmout;
2377 1.1 dholland }
2378 1.1 dholland if (NFSISSET_ATTRBIT(attrbitp, bitpos))
2379 1.1 dholland switch (bitpos) {
2380 1.1 dholland case NFSATTRBIT_SIZE:
2381 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER);
2382 1.1 dholland nvap->na_size = fxdr_hyper(tl);
2383 1.1 dholland attrsum += NFSX_HYPER;
2384 1.1 dholland break;
2385 1.1 dholland case NFSATTRBIT_ACL:
2386 1.1 dholland error = nfsrv_dissectacl(nd, aclp, &aceerr, &aclsize,
2387 1.1 dholland p);
2388 1.1 dholland if (error)
2389 1.1 dholland goto nfsmout;
2390 1.1 dholland if (aceerr && !nd->nd_repstat)
2391 1.1 dholland nd->nd_repstat = aceerr;
2392 1.1 dholland attrsum += aclsize;
2393 1.1 dholland break;
2394 1.1 dholland case NFSATTRBIT_ARCHIVE:
2395 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2396 1.1 dholland if (!nd->nd_repstat)
2397 1.1 dholland nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2398 1.1 dholland attrsum += NFSX_UNSIGNED;
2399 1.1 dholland break;
2400 1.1 dholland case NFSATTRBIT_HIDDEN:
2401 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2402 1.1 dholland if (!nd->nd_repstat)
2403 1.1 dholland nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2404 1.1 dholland attrsum += NFSX_UNSIGNED;
2405 1.1 dholland break;
2406 1.1 dholland case NFSATTRBIT_MIMETYPE:
2407 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2408 1.1 dholland i = fxdr_unsigned(int, *tl);
2409 1.1 dholland error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
2410 1.1 dholland if (error)
2411 1.1 dholland goto nfsmout;
2412 1.1 dholland if (!nd->nd_repstat)
2413 1.1 dholland nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2414 1.1 dholland attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(i));
2415 1.1 dholland break;
2416 1.1 dholland case NFSATTRBIT_MODE:
2417 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2418 1.1 dholland nvap->na_mode = nfstov_mode(*tl);
2419 1.1 dholland attrsum += NFSX_UNSIGNED;
2420 1.1 dholland break;
2421 1.1 dholland case NFSATTRBIT_OWNER:
2422 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2423 1.1 dholland j = fxdr_unsigned(int, *tl);
2424 1.1 dholland if (j < 0) {
2425 1.1 dholland error = NFSERR_BADXDR;
2426 1.1 dholland goto nfsmout;
2427 1.1 dholland }
2428 1.1 dholland if (j > NFSV4_SMALLSTR)
2429 1.1 dholland cp = malloc(j + 1, M_NFSSTRING, M_WAITOK);
2430 1.1 dholland else
2431 1.1 dholland cp = namestr;
2432 1.1 dholland error = nfsrv_mtostr(nd, cp, j);
2433 1.1 dholland if (error) {
2434 1.1 dholland if (j > NFSV4_SMALLSTR)
2435 1.1 dholland free(cp, M_NFSSTRING);
2436 1.1 dholland goto nfsmout;
2437 1.1 dholland }
2438 1.1 dholland if (!nd->nd_repstat) {
2439 1.1 dholland nd->nd_repstat = nfsv4_strtouid(nd, cp, j, &uid,
2440 1.1 dholland p);
2441 1.1 dholland if (!nd->nd_repstat)
2442 1.1 dholland nvap->na_uid = uid;
2443 1.1 dholland }
2444 1.1 dholland if (j > NFSV4_SMALLSTR)
2445 1.1 dholland free(cp, M_NFSSTRING);
2446 1.1 dholland attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j));
2447 1.1 dholland break;
2448 1.1 dholland case NFSATTRBIT_OWNERGROUP:
2449 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2450 1.1 dholland j = fxdr_unsigned(int, *tl);
2451 1.1 dholland if (j < 0) {
2452 1.1 dholland error = NFSERR_BADXDR;
2453 1.1 dholland goto nfsmout;
2454 1.1 dholland }
2455 1.1 dholland if (j > NFSV4_SMALLSTR)
2456 1.1 dholland cp = malloc(j + 1, M_NFSSTRING, M_WAITOK);
2457 1.1 dholland else
2458 1.1 dholland cp = namestr;
2459 1.1 dholland error = nfsrv_mtostr(nd, cp, j);
2460 1.1 dholland if (error) {
2461 1.1 dholland if (j > NFSV4_SMALLSTR)
2462 1.1 dholland free(cp, M_NFSSTRING);
2463 1.1 dholland goto nfsmout;
2464 1.1 dholland }
2465 1.1 dholland if (!nd->nd_repstat) {
2466 1.1 dholland nd->nd_repstat = nfsv4_strtogid(nd, cp, j, &gid,
2467 1.1 dholland p);
2468 1.1 dholland if (!nd->nd_repstat)
2469 1.1 dholland nvap->na_gid = gid;
2470 1.1 dholland }
2471 1.1 dholland if (j > NFSV4_SMALLSTR)
2472 1.1 dholland free(cp, M_NFSSTRING);
2473 1.1 dholland attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j));
2474 1.1 dholland break;
2475 1.1 dholland case NFSATTRBIT_SYSTEM:
2476 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2477 1.1 dholland if (!nd->nd_repstat)
2478 1.1 dholland nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2479 1.1 dholland attrsum += NFSX_UNSIGNED;
2480 1.1 dholland break;
2481 1.1 dholland case NFSATTRBIT_TIMEACCESSSET:
2482 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2483 1.1 dholland attrsum += NFSX_UNSIGNED;
2484 1.1 dholland if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) {
2485 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2486 1.1 dholland fxdr_nfsv4time(tl, &nvap->na_atime);
2487 1.1 dholland toclient = 1;
2488 1.1 dholland attrsum += NFSX_V4TIME;
2489 1.1 dholland } else {
2490 1.1 dholland vfs_timestamp(&nvap->na_atime);
2491 1.1 dholland nvap->na_vaflags |= VA_UTIMES_NULL;
2492 1.1 dholland }
2493 1.1 dholland break;
2494 1.1 dholland case NFSATTRBIT_TIMEBACKUP:
2495 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2496 1.1 dholland if (!nd->nd_repstat)
2497 1.1 dholland nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2498 1.1 dholland attrsum += NFSX_V4TIME;
2499 1.1 dholland break;
2500 1.1 dholland case NFSATTRBIT_TIMECREATE:
2501 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2502 1.1 dholland if (!nd->nd_repstat)
2503 1.1 dholland nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2504 1.1 dholland attrsum += NFSX_V4TIME;
2505 1.1 dholland break;
2506 1.1 dholland case NFSATTRBIT_TIMEMODIFYSET:
2507 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2508 1.1 dholland attrsum += NFSX_UNSIGNED;
2509 1.1 dholland if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) {
2510 1.1 dholland NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2511 1.1 dholland fxdr_nfsv4time(tl, &nvap->na_mtime);
2512 1.1 dholland nvap->na_vaflags &= ~VA_UTIMES_NULL;
2513 1.1 dholland attrsum += NFSX_V4TIME;
2514 1.1 dholland } else {
2515 1.1 dholland vfs_timestamp(&nvap->na_mtime);
2516 1.1 dholland if (!toclient)
2517 1.1 dholland nvap->na_vaflags |= VA_UTIMES_NULL;
2518 1.1 dholland }
2519 1.1 dholland break;
2520 1.1 dholland default:
2521 1.1 dholland nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2522 1.1 dholland /*
2523 1.1 dholland * set bitpos so we drop out of the loop.
2524 1.1 dholland */
2525 1.1 dholland bitpos = NFSATTRBIT_MAX;
2526 1.1 dholland break;
2527 1.1 dholland };
2528 1.1 dholland }
2529 1.1 dholland
2530 1.1 dholland /*
2531 1.1 dholland * some clients pad the attrlist, so we need to skip over the
2532 1.1 dholland * padding.
2533 1.1 dholland */
2534 1.1 dholland if (attrsum > attrsize) {
2535 1.1 dholland error = NFSERR_BADXDR;
2536 1.1 dholland } else {
2537 1.1 dholland attrsize = NFSM_RNDUP(attrsize);
2538 1.1 dholland if (attrsum < attrsize)
2539 1.1 dholland error = nfsm_advance(nd, attrsize - attrsum, -1);
2540 1.1 dholland }
2541 1.1 dholland nfsmout:
2542 1.1 dholland NFSEXITCODE2(error, nd);
2543 1.1 dholland return (error);
2544 1.1 dholland }
2545 1.1 dholland
2546 1.1 dholland /*
2547 1.1 dholland * Check/setup export credentials.
2548 1.1 dholland */
2549 1.1 dholland int
2550 1.1 dholland nfsd_excred(struct nfsrv_descript *nd, struct nfsexstuff *exp,
2551 1.1 dholland struct ucred *credanon)
2552 1.1 dholland {
2553 1.1 dholland int error = 0;
2554 1.1 dholland
2555 1.1 dholland /*
2556 1.1 dholland * Check/setup credentials.
2557 1.1 dholland */
2558 1.1 dholland if (nd->nd_flag & ND_GSS)
2559 1.1 dholland exp->nes_exflag &= ~MNT_EXPORTANON;
2560 1.1 dholland
2561 1.1 dholland /*
2562 1.1 dholland * Check to see if the operation is allowed for this security flavor.
2563 1.1 dholland * RFC2623 suggests that the NFSv3 Fsinfo RPC be allowed to
2564 1.1 dholland * AUTH_NONE or AUTH_SYS for file systems requiring RPCSEC_GSS.
2565 1.1 dholland * Also, allow Secinfo, so that it can acquire the correct flavor(s).
2566 1.1 dholland */
2567 1.1 dholland if (nfsvno_testexp(nd, exp) &&
2568 1.1 dholland nd->nd_procnum != NFSV4OP_SECINFO &&
2569 1.1 dholland nd->nd_procnum != NFSPROC_FSINFO) {
2570 1.1 dholland if (nd->nd_flag & ND_NFSV4)
2571 1.1 dholland error = NFSERR_WRONGSEC;
2572 1.1 dholland else
2573 1.1 dholland error = (NFSERR_AUTHERR | AUTH_TOOWEAK);
2574 1.1 dholland goto out;
2575 1.1 dholland }
2576 1.1 dholland
2577 1.1 dholland /*
2578 1.1 dholland * Check to see if the file system is exported V4 only.
2579 1.1 dholland */
2580 1.1 dholland if (NFSVNO_EXV4ONLY(exp) && !(nd->nd_flag & ND_NFSV4)) {
2581 1.1 dholland error = NFSERR_PROGNOTV4;
2582 1.1 dholland goto out;
2583 1.1 dholland }
2584 1.1 dholland
2585 1.1 dholland /*
2586 1.1 dholland * Now, map the user credentials.
2587 1.1 dholland * (Note that ND_AUTHNONE will only be set for an NFSv3
2588 1.1 dholland * Fsinfo RPC. If set for anything else, this code might need
2589 1.1 dholland * to change.)
2590 1.1 dholland */
2591 1.1 dholland if (NFSVNO_EXPORTED(exp) &&
2592 1.1 dholland ((!(nd->nd_flag & ND_GSS) && nd->nd_cred->cr_uid == 0) ||
2593 1.1 dholland NFSVNO_EXPORTANON(exp) ||
2594 1.1 dholland (nd->nd_flag & ND_AUTHNONE))) {
2595 1.1 dholland nd->nd_cred->cr_uid = credanon->cr_uid;
2596 1.1 dholland nd->nd_cred->cr_gid = credanon->cr_gid;
2597 1.1 dholland crsetgroups(nd->nd_cred, credanon->cr_ngroups,
2598 1.1 dholland credanon->cr_groups);
2599 1.1 dholland }
2600 1.1 dholland
2601 1.1 dholland out:
2602 1.1 dholland NFSEXITCODE2(error, nd);
2603 1.1 dholland return (error);
2604 1.1 dholland }
2605 1.1 dholland
2606 1.1 dholland /*
2607 1.1 dholland * Check exports.
2608 1.1 dholland */
2609 1.1 dholland int
2610 1.1 dholland nfsvno_checkexp(struct mount *mp, struct sockaddr *nam, struct nfsexstuff *exp,
2611 1.1 dholland struct ucred **credp)
2612 1.1 dholland {
2613 1.1 dholland int i, error, *secflavors;
2614 1.1 dholland
2615 1.1 dholland error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp,
2616 1.1 dholland &exp->nes_numsecflavor, &secflavors);
2617 1.1 dholland if (error) {
2618 1.1 dholland if (nfs_rootfhset) {
2619 1.1 dholland exp->nes_exflag = 0;
2620 1.1 dholland exp->nes_numsecflavor = 0;
2621 1.1 dholland error = 0;
2622 1.1 dholland }
2623 1.1 dholland } else {
2624 1.1 dholland /* Copy the security flavors. */
2625 1.1 dholland for (i = 0; i < exp->nes_numsecflavor; i++)
2626 1.1 dholland exp->nes_secflavors[i] = secflavors[i];
2627 1.1 dholland }
2628 1.1 dholland NFSEXITCODE(error);
2629 1.1 dholland return (error);
2630 1.1 dholland }
2631 1.1 dholland
2632 1.1 dholland /*
2633 1.1 dholland * Get a vnode for a file handle and export stuff.
2634 1.1 dholland */
2635 1.1 dholland int
2636 1.1 dholland nfsvno_fhtovp(struct mount *mp, fhandle_t *fhp, struct sockaddr *nam,
2637 1.1 dholland int lktype, struct vnode **vpp, struct nfsexstuff *exp,
2638 1.1 dholland struct ucred **credp)
2639 1.1 dholland {
2640 1.1 dholland int i, error, *secflavors;
2641 1.1 dholland
2642 1.1 dholland *credp = NULL;
2643 1.1 dholland exp->nes_numsecflavor = 0;
2644 1.1 dholland error = VFS_FHTOVP(mp, &fhp->fh_fid, lktype, vpp);
2645 1.1 dholland if (error != 0)
2646 1.1 dholland /* Make sure the server replies ESTALE to the client. */
2647 1.1 dholland error = ESTALE;
2648 1.1 dholland if (nam && !error) {
2649 1.1 dholland error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp,
2650 1.1 dholland &exp->nes_numsecflavor, &secflavors);
2651 1.1 dholland if (error) {
2652 1.1 dholland if (nfs_rootfhset) {
2653 1.1 dholland exp->nes_exflag = 0;
2654 1.1 dholland exp->nes_numsecflavor = 0;
2655 1.1 dholland error = 0;
2656 1.1 dholland } else {
2657 1.1 dholland vput(*vpp);
2658 1.1 dholland }
2659 1.1 dholland } else {
2660 1.1 dholland /* Copy the security flavors. */
2661 1.1 dholland for (i = 0; i < exp->nes_numsecflavor; i++)
2662 1.1 dholland exp->nes_secflavors[i] = secflavors[i];
2663 1.1 dholland }
2664 1.1 dholland }
2665 1.1 dholland NFSEXITCODE(error);
2666 1.1 dholland return (error);
2667 1.1 dholland }
2668 1.1 dholland
2669 1.1 dholland /*
2670 1.1 dholland * nfsd_fhtovp() - convert a fh to a vnode ptr
2671 1.1 dholland * - look up fsid in mount list (if not found ret error)
2672 1.1 dholland * - get vp and export rights by calling nfsvno_fhtovp()
2673 1.1 dholland * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
2674 1.1 dholland * for AUTH_SYS
2675 1.1 dholland * - if mpp != NULL, return the mount point so that it can
2676 1.1 dholland * be used for vn_finished_write() by the caller
2677 1.1 dholland */
2678 1.1 dholland void
2679 1.1 dholland nfsd_fhtovp(struct nfsrv_descript *nd, struct nfsrvfh *nfp, int lktype,
2680 1.1 dholland struct vnode **vpp, struct nfsexstuff *exp,
2681 1.1 dholland struct mount **mpp, int startwrite, struct thread *p)
2682 1.1 dholland {
2683 1.1 dholland struct mount *mp;
2684 1.1 dholland struct ucred *credanon;
2685 1.1 dholland fhandle_t *fhp;
2686 1.1 dholland
2687 1.1 dholland fhp = (fhandle_t *)nfp->nfsrvfh_data;
2688 1.1 dholland /*
2689 1.1 dholland * Check for the special case of the nfsv4root_fh.
2690 1.1 dholland */
2691 1.1 dholland mp = vfs_busyfs(&fhp->fh_fsid);
2692 1.1 dholland if (mpp != NULL)
2693 1.1 dholland *mpp = mp;
2694 1.1 dholland if (mp == NULL) {
2695 1.1 dholland *vpp = NULL;
2696 1.1 dholland nd->nd_repstat = ESTALE;
2697 1.1 dholland goto out;
2698 1.1 dholland }
2699 1.1 dholland
2700 1.1 dholland if (startwrite) {
2701 1.1 dholland vn_start_write(NULL, mpp, V_WAIT);
2702 1.1 dholland if (lktype == LK_SHARED && !(MNT_SHARED_WRITES(mp)))
2703 1.1 dholland lktype = LK_EXCLUSIVE;
2704 1.1 dholland }
2705 1.1 dholland nd->nd_repstat = nfsvno_fhtovp(mp, fhp, nd->nd_nam, lktype, vpp, exp,
2706 1.1 dholland &credanon);
2707 1.1 dholland vfs_unbusy(mp);
2708 1.1 dholland
2709 1.1 dholland /*
2710 1.1 dholland * For NFSv4 without a pseudo root fs, unexported file handles
2711 1.1 dholland * can be returned, so that Lookup works everywhere.
2712 1.1 dholland */
2713 1.1 dholland if (!nd->nd_repstat && exp->nes_exflag == 0 &&
2714 1.1 dholland !(nd->nd_flag & ND_NFSV4)) {
2715 1.1 dholland vput(*vpp);
2716 1.1 dholland nd->nd_repstat = EACCES;
2717 1.1 dholland }
2718 1.1 dholland
2719 1.1 dholland /*
2720 1.1 dholland * Personally, I've never seen any point in requiring a
2721 1.1 dholland * reserved port#, since only in the rare case where the
2722 1.1 dholland * clients are all boxes with secure system priviledges,
2723 1.1 dholland * does it provide any enhanced security, but... some people
2724 1.1 dholland * believe it to be useful and keep putting this code back in.
2725 1.1 dholland * (There is also some "security checker" out there that
2726 1.1 dholland * complains if the nfs server doesn't enforce this.)
2727 1.1 dholland * However, note the following:
2728 1.1 dholland * RFC3530 (NFSv4) specifies that a reserved port# not be
2729 1.1 dholland * required.
2730 1.1 dholland * RFC2623 recommends that, if a reserved port# is checked for,
2731 1.1 dholland * that there be a way to turn that off--> ifdef'd.
2732 1.1 dholland */
2733 1.1 dholland #ifdef NFS_REQRSVPORT
2734 1.1 dholland if (!nd->nd_repstat) {
2735 1.1 dholland struct sockaddr_in *saddr;
2736 1.1 dholland struct sockaddr_in6 *saddr6;
2737 1.1 dholland
2738 1.1 dholland saddr = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in *);
2739 1.1 dholland saddr6 = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in6 *);
2740 1.1 dholland if (!(nd->nd_flag & ND_NFSV4) &&
2741 1.1 dholland ((saddr->sin_family == AF_INET &&
2742 1.1 dholland ntohs(saddr->sin_port) >= IPPORT_RESERVED) ||
2743 1.1 dholland (saddr6->sin6_family == AF_INET6 &&
2744 1.1 dholland ntohs(saddr6->sin6_port) >= IPPORT_RESERVED))) {
2745 1.1 dholland vput(*vpp);
2746 1.1 dholland nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
2747 1.1 dholland }
2748 1.1 dholland }
2749 1.1 dholland #endif /* NFS_REQRSVPORT */
2750 1.1 dholland
2751 1.1 dholland /*
2752 1.1 dholland * Check/setup credentials.
2753 1.1 dholland */
2754 1.1 dholland if (!nd->nd_repstat) {
2755 1.1 dholland nd->nd_saveduid = nd->nd_cred->cr_uid;
2756 1.1 dholland nd->nd_repstat = nfsd_excred(nd, exp, credanon);
2757 1.1 dholland if (nd->nd_repstat)
2758 1.1 dholland vput(*vpp);
2759 1.1 dholland }
2760 1.1 dholland if (credanon != NULL)
2761 1.1 dholland crfree(credanon);
2762 1.1 dholland if (nd->nd_repstat) {
2763 1.1 dholland if (startwrite)
2764 1.1 dholland vn_finished_write(mp);
2765 1.1 dholland *vpp = NULL;
2766 1.1 dholland if (mpp != NULL)
2767 1.1 dholland *mpp = NULL;
2768 1.1 dholland }
2769 1.1 dholland
2770 1.1 dholland out:
2771 1.1 dholland NFSEXITCODE2(0, nd);
2772 1.1 dholland }
2773 1.1 dholland
2774 1.1 dholland /*
2775 1.1 dholland * glue for fp.
2776 1.1 dholland */
2777 1.1 dholland static int
2778 1.1 dholland fp_getfvp(struct thread *p, int fd, struct file **fpp, struct vnode **vpp)
2779 1.1 dholland {
2780 1.1 dholland struct filedesc *fdp;
2781 1.1 dholland struct file *fp;
2782 1.1 dholland int error = 0;
2783 1.1 dholland
2784 1.1 dholland fdp = p->td_proc->p_fd;
2785 1.1 dholland if (fd < 0 || fd >= fdp->fd_nfiles ||
2786 1.1 dholland (fp = fdp->fd_ofiles[fd].fde_file) == NULL) {
2787 1.1 dholland error = EBADF;
2788 1.1 dholland goto out;
2789 1.1 dholland }
2790 1.1 dholland *fpp = fp;
2791 1.1 dholland
2792 1.1 dholland out:
2793 1.1 dholland NFSEXITCODE(error);
2794 1.1 dholland return (error);
2795 1.1 dholland }
2796 1.1 dholland
2797 1.1 dholland /*
2798 1.1 dholland * Called from nfssvc() to update the exports list. Just call
2799 1.1 dholland * vfs_export(). This has to be done, since the v4 root fake fs isn't
2800 1.1 dholland * in the mount list.
2801 1.1 dholland */
2802 1.1 dholland int
2803 1.1 dholland nfsrv_v4rootexport(void *argp, struct ucred *cred, struct thread *p)
2804 1.1 dholland {
2805 1.1 dholland struct nfsex_args *nfsexargp = (struct nfsex_args *)argp;
2806 1.1 dholland int error = 0;
2807 1.1 dholland struct nameidata nd;
2808 1.1 dholland fhandle_t fh;
2809 1.1 dholland
2810 1.1 dholland error = vfs_export(&nfsv4root_mnt, &nfsexargp->export);
2811 1.1 dholland if ((nfsexargp->export.ex_flags & MNT_DELEXPORT) != 0)
2812 1.1 dholland nfs_rootfhset = 0;
2813 1.1 dholland else if (error == 0) {
2814 1.1 dholland if (nfsexargp->fspec == NULL) {
2815 1.1 dholland error = EPERM;
2816 1.1 dholland goto out;
2817 1.1 dholland }
2818 1.1 dholland /*
2819 1.1 dholland * If fspec != NULL, this is the v4root path.
2820 1.1 dholland */
2821 1.1 dholland NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE,
2822 1.1 dholland nfsexargp->fspec, p);
2823 1.1 dholland if ((error = namei(&nd)) != 0)
2824 1.1 dholland goto out;
2825 1.1 dholland error = nfsvno_getfh(nd.ni_vp, &fh, p);
2826 1.1 dholland vrele(nd.ni_vp);
2827 1.1 dholland if (!error) {
2828 1.1 dholland nfs_rootfh.nfsrvfh_len = NFSX_MYFH;
2829 1.1 dholland NFSBCOPY((caddr_t)&fh,
2830 1.1 dholland nfs_rootfh.nfsrvfh_data,
2831 1.1 dholland sizeof (fhandle_t));
2832 1.1 dholland nfs_rootfhset = 1;
2833 1.1 dholland }
2834 1.1 dholland }
2835 1.1 dholland
2836 1.1 dholland out:
2837 1.1 dholland NFSEXITCODE(error);
2838 1.1 dholland return (error);
2839 1.1 dholland }
2840 1.1 dholland
2841 1.1 dholland /*
2842 1.1 dholland * Get the tcp socket sequence numbers we need.
2843 1.1 dholland * (Maybe this should be moved to the tcp sources?)
2844 1.1 dholland */
2845 1.1 dholland int
2846 1.1 dholland nfsrv_getsocksndseq(struct socket *so, tcp_seq *maxp, tcp_seq *unap)
2847 1.1 dholland {
2848 1.1 dholland struct inpcb *inp;
2849 1.1 dholland struct tcpcb *tp;
2850 1.1 dholland int error = 0;
2851 1.1 dholland
2852 1.1 dholland inp = sotoinpcb(so);
2853 1.1 dholland KASSERT(inp != NULL, ("nfsrv_getsocksndseq: inp == NULL"));
2854 1.1 dholland INP_RLOCK(inp);
2855 1.1 dholland if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
2856 1.1 dholland INP_RUNLOCK(inp);
2857 1.1 dholland error = EPIPE;
2858 1.1 dholland goto out;
2859 1.1 dholland }
2860 1.1 dholland tp = intotcpcb(inp);
2861 1.1 dholland if (tp->t_state != TCPS_ESTABLISHED) {
2862 1.1 dholland INP_RUNLOCK(inp);
2863 1.1 dholland error = EPIPE;
2864 1.1 dholland goto out;
2865 1.1 dholland }
2866 1.1 dholland *maxp = tp->snd_max;
2867 1.1 dholland *unap = tp->snd_una;
2868 1.1 dholland INP_RUNLOCK(inp);
2869 1.1 dholland
2870 1.1 dholland out:
2871 1.1 dholland NFSEXITCODE(error);
2872 1.1 dholland return (error);
2873 1.1 dholland }
2874 1.1 dholland
2875 1.1 dholland /*
2876 1.1 dholland * This function needs to test to see if the system is near its limit
2877 1.1 dholland * for memory allocation via malloc() or mget() and return True iff
2878 1.1 dholland * either of these resources are near their limit.
2879 1.1 dholland * XXX (For now, this is just a stub.)
2880 1.1 dholland */
2881 1.1 dholland int nfsrv_testmalloclimit = 0;
2882 1.1 dholland int
2883 1.1 dholland nfsrv_mallocmget_limit(void)
2884 1.1 dholland {
2885 1.1 dholland static int printmesg = 0;
2886 1.1 dholland static int testval = 1;
2887 1.1 dholland
2888 1.1 dholland if (nfsrv_testmalloclimit && (testval++ % 1000) == 0) {
2889 1.1 dholland if ((printmesg++ % 100) == 0)
2890 1.1 dholland printf("nfsd: malloc/mget near limit\n");
2891 1.1 dholland return (1);
2892 1.1 dholland }
2893 1.1 dholland return (0);
2894 1.1 dholland }
2895 1.1 dholland
2896 1.1 dholland /*
2897 1.1 dholland * BSD specific initialization of a mount point.
2898 1.1 dholland */
2899 1.1 dholland void
2900 1.1 dholland nfsd_mntinit(void)
2901 1.1 dholland {
2902 1.1 dholland static int inited = 0;
2903 1.1 dholland
2904 1.1 dholland if (inited)
2905 1.1 dholland return;
2906 1.1 dholland inited = 1;
2907 1.1 dholland nfsv4root_mnt.mnt_flag = (MNT_RDONLY | MNT_EXPORTED);
2908 1.1 dholland TAILQ_INIT(&nfsv4root_mnt.mnt_nvnodelist);
2909 1.1 dholland TAILQ_INIT(&nfsv4root_mnt.mnt_activevnodelist);
2910 1.1 dholland nfsv4root_mnt.mnt_export = NULL;
2911 1.1 dholland TAILQ_INIT(&nfsv4root_opt);
2912 1.1 dholland TAILQ_INIT(&nfsv4root_newopt);
2913 1.1 dholland nfsv4root_mnt.mnt_opt = &nfsv4root_opt;
2914 1.1 dholland nfsv4root_mnt.mnt_optnew = &nfsv4root_newopt;
2915 1.1 dholland nfsv4root_mnt.mnt_nvnodelistsize = 0;
2916 1.1 dholland nfsv4root_mnt.mnt_activevnodelistsize = 0;
2917 1.1 dholland }
2918 1.1 dholland
2919 1.1 dholland /*
2920 1.1 dholland * Get a vnode for a file handle, without checking exports, etc.
2921 1.1 dholland */
2922 1.1 dholland struct vnode *
2923 1.1 dholland nfsvno_getvp(fhandle_t *fhp)
2924 1.1 dholland {
2925 1.1 dholland struct mount *mp;
2926 1.1 dholland struct vnode *vp;
2927 1.1 dholland int error;
2928 1.1 dholland
2929 1.1 dholland mp = vfs_busyfs(&fhp->fh_fsid);
2930 1.1 dholland if (mp == NULL)
2931 1.1 dholland return (NULL);
2932 1.1 dholland error = VFS_FHTOVP(mp, &fhp->fh_fid, LK_EXCLUSIVE, &vp);
2933 1.1 dholland vfs_unbusy(mp);
2934 1.1 dholland if (error)
2935 1.1 dholland return (NULL);
2936 1.1 dholland return (vp);
2937 1.1 dholland }
2938 1.1 dholland
2939 1.1 dholland /*
2940 1.1 dholland * Do a local VOP_ADVLOCK().
2941 1.1 dholland */
2942 1.1 dholland int
2943 1.1 dholland nfsvno_advlock(struct vnode *vp, int ftype, u_int64_t first,
2944 1.1 dholland u_int64_t end, struct thread *td)
2945 1.1 dholland {
2946 1.1 dholland int error = 0;
2947 1.1 dholland struct flock fl;
2948 1.1 dholland u_int64_t tlen;
2949 1.1 dholland
2950 1.1 dholland if (nfsrv_dolocallocks == 0)
2951 1.1 dholland goto out;
2952 1.1 dholland
2953 1.1 dholland /* Check for VI_DOOMED here, so that VOP_ADVLOCK() isn't performed. */
2954 1.1 dholland if ((vp->v_iflag & VI_DOOMED) != 0) {
2955 1.1 dholland error = EPERM;
2956 1.1 dholland goto out;
2957 1.1 dholland }
2958 1.1 dholland
2959 1.1 dholland fl.l_whence = SEEK_SET;
2960 1.1 dholland fl.l_type = ftype;
2961 1.1 dholland fl.l_start = (off_t)first;
2962 1.1 dholland if (end == NFS64BITSSET) {
2963 1.1 dholland fl.l_len = 0;
2964 1.1 dholland } else {
2965 1.1 dholland tlen = end - first;
2966 1.1 dholland fl.l_len = (off_t)tlen;
2967 1.1 dholland }
2968 1.1 dholland /*
2969 1.1 dholland * For FreeBSD8, the l_pid and l_sysid must be set to the same
2970 1.1 dholland * values for all calls, so that all locks will be held by the
2971 1.1 dholland * nfsd server. (The nfsd server handles conflicts between the
2972 1.1 dholland * various clients.)
2973 1.1 dholland * Since an NFSv4 lockowner is a ClientID plus an array of up to 1024
2974 1.1 dholland * bytes, so it can't be put in l_sysid.
2975 1.1 dholland */
2976 1.1 dholland if (nfsv4_sysid == 0)
2977 1.1 dholland nfsv4_sysid = nlm_acquire_next_sysid();
2978 1.1 dholland fl.l_pid = (pid_t)0;
2979 1.1 dholland fl.l_sysid = (int)nfsv4_sysid;
2980 1.1 dholland
2981 1.1 dholland NFSVOPUNLOCK(vp, 0);
2982 1.1 dholland if (ftype == F_UNLCK)
2983 1.1 dholland error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_UNLCK, &fl,
2984 1.1 dholland (F_POSIX | F_REMOTE));
2985 1.1 dholland else
2986 1.1 dholland error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_SETLK, &fl,
2987 1.1 dholland (F_POSIX | F_REMOTE));
2988 1.1 dholland NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
2989 1.1 dholland
2990 1.1 dholland out:
2991 1.1 dholland NFSEXITCODE(error);
2992 1.1 dholland return (error);
2993 1.1 dholland }
2994 1.1 dholland
2995 1.1 dholland /*
2996 1.1 dholland * Check the nfsv4 root exports.
2997 1.1 dholland */
2998 1.1 dholland int
2999 1.1 dholland nfsvno_v4rootexport(struct nfsrv_descript *nd)
3000 1.1 dholland {
3001 1.1 dholland struct ucred *credanon;
3002 1.1 dholland int exflags, error = 0, numsecflavor, *secflavors, i;
3003 1.1 dholland
3004 1.1 dholland error = vfs_stdcheckexp(&nfsv4root_mnt, nd->nd_nam, &exflags,
3005 1.1 dholland &credanon, &numsecflavor, &secflavors);
3006 1.1 dholland if (error) {
3007 1.1 dholland error = NFSERR_PROGUNAVAIL;
3008 1.1 dholland goto out;
3009 1.1 dholland }
3010 1.1 dholland if (credanon != NULL)
3011 1.1 dholland crfree(credanon);
3012 1.1 dholland for (i = 0; i < numsecflavor; i++) {
3013 1.1 dholland if (secflavors[i] == AUTH_SYS)
3014 1.1 dholland nd->nd_flag |= ND_EXAUTHSYS;
3015 1.1 dholland else if (secflavors[i] == RPCSEC_GSS_KRB5)
3016 1.1 dholland nd->nd_flag |= ND_EXGSS;
3017 1.1 dholland else if (secflavors[i] == RPCSEC_GSS_KRB5I)
3018 1.1 dholland nd->nd_flag |= ND_EXGSSINTEGRITY;
3019 1.1 dholland else if (secflavors[i] == RPCSEC_GSS_KRB5P)
3020 1.1 dholland nd->nd_flag |= ND_EXGSSPRIVACY;
3021 1.1 dholland }
3022 1.1 dholland
3023 1.1 dholland out:
3024 1.1 dholland NFSEXITCODE(error);
3025 1.1 dholland return (error);
3026 1.1 dholland }
3027 1.1 dholland
3028 1.1 dholland /*
3029 1.1 dholland * Nfs server psuedo system call for the nfsd's
3030 1.1 dholland */
3031 1.1 dholland /*
3032 1.1 dholland * MPSAFE
3033 1.1 dholland */
3034 1.1 dholland static int
3035 1.1 dholland nfssvc_nfsd(struct thread *td, struct nfssvc_args *uap)
3036 1.1 dholland {
3037 1.1 dholland struct file *fp;
3038 1.1 dholland struct nfsd_addsock_args sockarg;
3039 1.1 dholland struct nfsd_nfsd_args nfsdarg;
3040 1.1 dholland cap_rights_t rights;
3041 1.1 dholland int error;
3042 1.1 dholland
3043 1.1 dholland if (uap->flag & NFSSVC_NFSDADDSOCK) {
3044 1.1 dholland error = copyin(uap->argp, (caddr_t)&sockarg, sizeof (sockarg));
3045 1.1 dholland if (error)
3046 1.1 dholland goto out;
3047 1.1 dholland /*
3048 1.1 dholland * Since we don't know what rights might be required,
3049 1.1 dholland * pretend that we need them all. It is better to be too
3050 1.1 dholland * careful than too reckless.
3051 1.1 dholland */
3052 1.1 dholland error = fget(td, sockarg.sock,
3053 1.1 dholland cap_rights_init(&rights, CAP_SOCK_SERVER), &fp);
3054 1.1 dholland if (error != 0)
3055 1.1 dholland goto out;
3056 1.1 dholland if (fp->f_type != DTYPE_SOCKET) {
3057 1.1 dholland fdrop(fp, td);
3058 1.1 dholland error = EPERM;
3059 1.1 dholland goto out;
3060 1.1 dholland }
3061 1.1 dholland error = nfsrvd_addsock(fp);
3062 1.1 dholland fdrop(fp, td);
3063 1.1 dholland } else if (uap->flag & NFSSVC_NFSDNFSD) {
3064 1.1 dholland if (uap->argp == NULL) {
3065 1.1 dholland error = EINVAL;
3066 1.1 dholland goto out;
3067 1.1 dholland }
3068 1.1 dholland error = copyin(uap->argp, (caddr_t)&nfsdarg,
3069 1.1 dholland sizeof (nfsdarg));
3070 1.1 dholland if (error)
3071 1.1 dholland goto out;
3072 1.1 dholland error = nfsrvd_nfsd(td, &nfsdarg);
3073 1.1 dholland } else {
3074 1.1 dholland error = nfssvc_srvcall(td, uap, td->td_ucred);
3075 1.1 dholland }
3076 1.1 dholland
3077 1.1 dholland out:
3078 1.1 dholland NFSEXITCODE(error);
3079 1.1 dholland return (error);
3080 1.1 dholland }
3081 1.1 dholland
3082 1.1 dholland static int
3083 1.1 dholland nfssvc_srvcall(struct thread *p, struct nfssvc_args *uap, struct ucred *cred)
3084 1.1 dholland {
3085 1.1 dholland struct nfsex_args export;
3086 1.1 dholland struct file *fp = NULL;
3087 1.1 dholland int stablefd, len;
3088 1.1 dholland struct nfsd_clid adminrevoke;
3089 1.1 dholland struct nfsd_dumplist dumplist;
3090 1.1 dholland struct nfsd_dumpclients *dumpclients;
3091 1.1 dholland struct nfsd_dumplocklist dumplocklist;
3092 1.1 dholland struct nfsd_dumplocks *dumplocks;
3093 1.1 dholland struct nameidata nd;
3094 1.1 dholland vnode_t vp;
3095 1.1 dholland int error = EINVAL, igotlock;
3096 1.1 dholland struct proc *procp;
3097 1.1 dholland static int suspend_nfsd = 0;
3098 1.1 dholland
3099 1.1 dholland if (uap->flag & NFSSVC_PUBLICFH) {
3100 1.1 dholland NFSBZERO((caddr_t)&nfs_pubfh.nfsrvfh_data,
3101 1.1 dholland sizeof (fhandle_t));
3102 1.1 dholland error = copyin(uap->argp,
3103 1.1 dholland &nfs_pubfh.nfsrvfh_data, sizeof (fhandle_t));
3104 1.1 dholland if (!error)
3105 1.1 dholland nfs_pubfhset = 1;
3106 1.1 dholland } else if (uap->flag & NFSSVC_V4ROOTEXPORT) {
3107 1.1 dholland error = copyin(uap->argp,(caddr_t)&export,
3108 1.1 dholland sizeof (struct nfsex_args));
3109 1.1 dholland if (!error)
3110 1.1 dholland error = nfsrv_v4rootexport(&export, cred, p);
3111 1.1 dholland } else if (uap->flag & NFSSVC_NOPUBLICFH) {
3112 1.1 dholland nfs_pubfhset = 0;
3113 1.1 dholland error = 0;
3114 1.1 dholland } else if (uap->flag & NFSSVC_STABLERESTART) {
3115 1.1 dholland error = copyin(uap->argp, (caddr_t)&stablefd,
3116 1.1 dholland sizeof (int));
3117 1.1 dholland if (!error)
3118 1.1 dholland error = fp_getfvp(p, stablefd, &fp, &vp);
3119 1.1 dholland if (!error && (NFSFPFLAG(fp) & (FREAD | FWRITE)) != (FREAD | FWRITE))
3120 1.1 dholland error = EBADF;
3121 1.1 dholland if (!error && newnfs_numnfsd != 0)
3122 1.1 dholland error = EPERM;
3123 1.1 dholland if (!error) {
3124 1.1 dholland nfsrv_stablefirst.nsf_fp = fp;
3125 1.1 dholland nfsrv_setupstable(p);
3126 1.1 dholland }
3127 1.1 dholland } else if (uap->flag & NFSSVC_ADMINREVOKE) {
3128 1.1 dholland error = copyin(uap->argp, (caddr_t)&adminrevoke,
3129 1.1 dholland sizeof (struct nfsd_clid));
3130 1.1 dholland if (!error)
3131 1.1 dholland error = nfsrv_adminrevoke(&adminrevoke, p);
3132 1.1 dholland } else if (uap->flag & NFSSVC_DUMPCLIENTS) {
3133 1.1 dholland error = copyin(uap->argp, (caddr_t)&dumplist,
3134 1.1 dholland sizeof (struct nfsd_dumplist));
3135 1.1 dholland if (!error && (dumplist.ndl_size < 1 ||
3136 1.1 dholland dumplist.ndl_size > NFSRV_MAXDUMPLIST))
3137 1.1 dholland error = EPERM;
3138 1.1 dholland if (!error) {
3139 1.1 dholland len = sizeof (struct nfsd_dumpclients) * dumplist.ndl_size;
3140 1.1 dholland dumpclients = (struct nfsd_dumpclients *)malloc(len,
3141 1.1 dholland M_TEMP, M_WAITOK);
3142 1.1 dholland nfsrv_dumpclients(dumpclients, dumplist.ndl_size);
3143 1.1 dholland error = copyout(dumpclients,
3144 1.1 dholland CAST_USER_ADDR_T(dumplist.ndl_list), len);
3145 1.1 dholland free((caddr_t)dumpclients, M_TEMP);
3146 1.1 dholland }
3147 1.1 dholland } else if (uap->flag & NFSSVC_DUMPLOCKS) {
3148 1.1 dholland error = copyin(uap->argp, (caddr_t)&dumplocklist,
3149 1.1 dholland sizeof (struct nfsd_dumplocklist));
3150 1.1 dholland if (!error && (dumplocklist.ndllck_size < 1 ||
3151 1.1 dholland dumplocklist.ndllck_size > NFSRV_MAXDUMPLIST))
3152 1.1 dholland error = EPERM;
3153 1.1 dholland if (!error)
3154 1.1 dholland error = nfsrv_lookupfilename(&nd,
3155 1.1 dholland dumplocklist.ndllck_fname, p);
3156 1.1 dholland if (!error) {
3157 1.1 dholland len = sizeof (struct nfsd_dumplocks) *
3158 1.1 dholland dumplocklist.ndllck_size;
3159 1.1 dholland dumplocks = (struct nfsd_dumplocks *)malloc(len,
3160 1.1 dholland M_TEMP, M_WAITOK);
3161 1.1 dholland nfsrv_dumplocks(nd.ni_vp, dumplocks,
3162 1.1 dholland dumplocklist.ndllck_size, p);
3163 1.1 dholland vput(nd.ni_vp);
3164 1.1 dholland error = copyout(dumplocks,
3165 1.1 dholland CAST_USER_ADDR_T(dumplocklist.ndllck_list), len);
3166 1.1 dholland free((caddr_t)dumplocks, M_TEMP);
3167 1.1 dholland }
3168 1.1 dholland } else if (uap->flag & NFSSVC_BACKUPSTABLE) {
3169 1.1 dholland procp = p->td_proc;
3170 1.1 dholland PROC_LOCK(procp);
3171 1.1 dholland nfsd_master_pid = procp->p_pid;
3172 1.1 dholland bcopy(procp->p_comm, nfsd_master_comm, MAXCOMLEN + 1);
3173 1.1 dholland nfsd_master_start = procp->p_stats->p_start;
3174 1.1 dholland nfsd_master_proc = procp;
3175 1.1 dholland PROC_UNLOCK(procp);
3176 1.1 dholland } else if ((uap->flag & NFSSVC_SUSPENDNFSD) != 0) {
3177 1.1 dholland NFSLOCKV4ROOTMUTEX();
3178 1.1 dholland if (suspend_nfsd == 0) {
3179 1.1 dholland /* Lock out all nfsd threads */
3180 1.1 dholland do {
3181 1.1 dholland igotlock = nfsv4_lock(&nfsd_suspend_lock, 1,
3182 1.1 dholland NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
3183 1.1 dholland } while (igotlock == 0 && suspend_nfsd == 0);
3184 1.1 dholland suspend_nfsd = 1;
3185 1.1 dholland }
3186 1.1 dholland NFSUNLOCKV4ROOTMUTEX();
3187 1.1 dholland error = 0;
3188 1.1 dholland } else if ((uap->flag & NFSSVC_RESUMENFSD) != 0) {
3189 1.1 dholland NFSLOCKV4ROOTMUTEX();
3190 1.1 dholland if (suspend_nfsd != 0) {
3191 1.1 dholland nfsv4_unlock(&nfsd_suspend_lock, 0);
3192 1.1 dholland suspend_nfsd = 0;
3193 1.1 dholland }
3194 1.1 dholland NFSUNLOCKV4ROOTMUTEX();
3195 1.1 dholland error = 0;
3196 1.1 dholland }
3197 1.1 dholland
3198 1.1 dholland NFSEXITCODE(error);
3199 1.1 dholland return (error);
3200 1.1 dholland }
3201 1.1 dholland
3202 1.1 dholland /*
3203 1.1 dholland * Check exports.
3204 1.1 dholland * Returns 0 if ok, 1 otherwise.
3205 1.1 dholland */
3206 1.1 dholland int
3207 1.1 dholland nfsvno_testexp(struct nfsrv_descript *nd, struct nfsexstuff *exp)
3208 1.1 dholland {
3209 1.1 dholland int i;
3210 1.1 dholland
3211 1.1 dholland /*
3212 1.1 dholland * This seems odd, but allow the case where the security flavor
3213 1.1 dholland * list is empty. This happens when NFSv4 is traversing non-exported
3214 1.1 dholland * file systems. Exported file systems should always have a non-empty
3215 1.1 dholland * security flavor list.
3216 1.1 dholland */
3217 1.1 dholland if (exp->nes_numsecflavor == 0)
3218 1.1 dholland return (0);
3219 1.1 dholland
3220 1.1 dholland for (i = 0; i < exp->nes_numsecflavor; i++) {
3221 1.1 dholland /*
3222 1.1 dholland * The tests for privacy and integrity must be first,
3223 1.1 dholland * since ND_GSS is set for everything but AUTH_SYS.
3224 1.1 dholland */
3225 1.1 dholland if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5P &&
3226 1.1 dholland (nd->nd_flag & ND_GSSPRIVACY))
3227 1.1 dholland return (0);
3228 1.1 dholland if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5I &&
3229 1.1 dholland (nd->nd_flag & ND_GSSINTEGRITY))
3230 1.1 dholland return (0);
3231 1.1 dholland if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5 &&
3232 1.1 dholland (nd->nd_flag & ND_GSS))
3233 1.1 dholland return (0);
3234 1.1 dholland if (exp->nes_secflavors[i] == AUTH_SYS &&
3235 1.1 dholland (nd->nd_flag & ND_GSS) == 0)
3236 1.1 dholland return (0);
3237 1.1 dholland }
3238 1.1 dholland return (1);
3239 1.1 dholland }
3240 1.1 dholland
3241 1.1 dholland /*
3242 1.1 dholland * Calculate a hash value for the fid in a file handle.
3243 1.1 dholland */
3244 1.1 dholland uint32_t
3245 1.1 dholland nfsrv_hashfh(fhandle_t *fhp)
3246 1.1 dholland {
3247 1.1 dholland uint32_t hashval;
3248 1.1 dholland
3249 1.1 dholland hashval = hash32_buf(&fhp->fh_fid, sizeof(struct fid), 0);
3250 1.1 dholland return (hashval);
3251 1.1 dholland }
3252 1.1 dholland
3253 1.1 dholland /*
3254 1.1 dholland * Signal the userland master nfsd to backup the stable restart file.
3255 1.1 dholland */
3256 1.1 dholland void
3257 1.1 dholland nfsrv_backupstable(void)
3258 1.1 dholland {
3259 1.1 dholland struct proc *procp;
3260 1.1 dholland
3261 1.1 dholland if (nfsd_master_proc != NULL) {
3262 1.1 dholland procp = pfind(nfsd_master_pid);
3263 1.1 dholland /* Try to make sure it is the correct process. */
3264 1.1 dholland if (procp == nfsd_master_proc &&
3265 1.1 dholland procp->p_stats->p_start.tv_sec ==
3266 1.1 dholland nfsd_master_start.tv_sec &&
3267 1.1 dholland procp->p_stats->p_start.tv_usec ==
3268 1.1 dholland nfsd_master_start.tv_usec &&
3269 1.1 dholland strcmp(procp->p_comm, nfsd_master_comm) == 0)
3270 1.1 dholland kern_psignal(procp, SIGUSR2);
3271 1.1 dholland else
3272 1.1 dholland nfsd_master_proc = NULL;
3273 1.1 dholland
3274 1.1 dholland if (procp != NULL)
3275 1.1 dholland PROC_UNLOCK(procp);
3276 1.1 dholland }
3277 1.1 dholland }
3278 1.1 dholland
3279 1.1 dholland extern int (*nfsd_call_nfsd)(struct thread *, struct nfssvc_args *);
3280 1.1 dholland
3281 1.1 dholland /*
3282 1.1 dholland * Called once to initialize data structures...
3283 1.1 dholland */
3284 1.1 dholland static int
3285 1.1 dholland nfsd_modevent(module_t mod, int type, void *data)
3286 1.1 dholland {
3287 1.1 dholland int error = 0, i;
3288 1.1 dholland static int loaded = 0;
3289 1.1 dholland
3290 1.1 dholland switch (type) {
3291 1.1 dholland case MOD_LOAD:
3292 1.1 dholland if (loaded)
3293 1.1 dholland goto out;
3294 1.1 dholland newnfs_portinit();
3295 1.1 dholland for (i = 0; i < NFSRVCACHE_HASHSIZE; i++) {
3296 1.1 dholland snprintf(nfsrchash_table[i].lock_name,
3297 1.1 dholland sizeof(nfsrchash_table[i].lock_name), "nfsrc_tcp%d",
3298 1.1 dholland i);
3299 1.1 dholland mtx_init(&nfsrchash_table[i].mtx,
3300 1.1 dholland nfsrchash_table[i].lock_name, NULL, MTX_DEF);
3301 1.1 dholland }
3302 1.1 dholland mtx_init(&nfsrc_udpmtx, "nfs_udpcache_mutex", NULL, MTX_DEF);
3303 1.1 dholland mtx_init(&nfs_v4root_mutex, "nfs_v4root_mutex", NULL, MTX_DEF);
3304 1.1 dholland mtx_init(&nfsv4root_mnt.mnt_mtx, "struct mount mtx", NULL,
3305 1.1 dholland MTX_DEF);
3306 1.1 dholland lockinit(&nfsv4root_mnt.mnt_explock, PVFS, "explock", 0, 0);
3307 1.1 dholland nfsrvd_initcache();
3308 1.1 dholland nfsd_init();
3309 1.1 dholland NFSD_LOCK();
3310 1.1 dholland nfsrvd_init(0);
3311 1.1 dholland NFSD_UNLOCK();
3312 1.1 dholland nfsd_mntinit();
3313 1.1 dholland #ifdef VV_DISABLEDELEG
3314 1.1 dholland vn_deleg_ops.vndeleg_recall = nfsd_recalldelegation;
3315 1.1 dholland vn_deleg_ops.vndeleg_disable = nfsd_disabledelegation;
3316 1.1 dholland #endif
3317 1.1 dholland nfsd_call_servertimer = nfsrv_servertimer;
3318 1.1 dholland nfsd_call_nfsd = nfssvc_nfsd;
3319 1.1 dholland loaded = 1;
3320 1.1 dholland break;
3321 1.1 dholland
3322 1.1 dholland case MOD_UNLOAD:
3323 1.1 dholland if (newnfs_numnfsd != 0) {
3324 1.1 dholland error = EBUSY;
3325 1.1 dholland break;
3326 1.1 dholland }
3327 1.1 dholland
3328 1.1 dholland #ifdef VV_DISABLEDELEG
3329 1.1 dholland vn_deleg_ops.vndeleg_recall = NULL;
3330 1.1 dholland vn_deleg_ops.vndeleg_disable = NULL;
3331 1.1 dholland #endif
3332 1.1 dholland nfsd_call_servertimer = NULL;
3333 1.1 dholland nfsd_call_nfsd = NULL;
3334 1.1 dholland
3335 1.1 dholland /* Clean out all NFSv4 state. */
3336 1.1 dholland nfsrv_throwawayallstate(curthread);
3337 1.1 dholland
3338 1.1 dholland /* Clean the NFS server reply cache */
3339 1.1 dholland nfsrvd_cleancache();
3340 1.1 dholland
3341 1.1 dholland /* Free up the krpc server pool. */
3342 1.1 dholland if (nfsrvd_pool != NULL)
3343 1.1 dholland svcpool_destroy(nfsrvd_pool);
3344 1.1 dholland
3345 1.1 dholland /* and get rid of the locks */
3346 1.1 dholland for (i = 0; i < NFSRVCACHE_HASHSIZE; i++)
3347 1.1 dholland mtx_destroy(&nfsrchash_table[i].mtx);
3348 1.1 dholland mtx_destroy(&nfsrc_udpmtx);
3349 1.1 dholland mtx_destroy(&nfs_v4root_mutex);
3350 1.1 dholland mtx_destroy(&nfsv4root_mnt.mnt_mtx);
3351 1.1 dholland lockdestroy(&nfsv4root_mnt.mnt_explock);
3352 1.1 dholland loaded = 0;
3353 1.1 dholland break;
3354 1.1 dholland default:
3355 1.1 dholland error = EOPNOTSUPP;
3356 1.1 dholland break;
3357 1.1 dholland }
3358 1.1 dholland
3359 1.1 dholland out:
3360 1.1 dholland NFSEXITCODE(error);
3361 1.1 dholland return (error);
3362 1.1 dholland }
3363 1.1 dholland static moduledata_t nfsd_mod = {
3364 1.1 dholland "nfsd",
3365 1.1 dholland nfsd_modevent,
3366 1.1 dholland NULL,
3367 1.1 dholland };
3368 1.1 dholland DECLARE_MODULE(nfsd, nfsd_mod, SI_SUB_VFS, SI_ORDER_ANY);
3369 1.1 dholland
3370 1.1 dholland /* So that loader and kldload(2) can find us, wherever we are.. */
3371 1.1 dholland MODULE_VERSION(nfsd, 1);
3372 1.1 dholland MODULE_DEPEND(nfsd, nfscommon, 1, 1, 1);
3373 1.1 dholland MODULE_DEPEND(nfsd, nfslock, 1, 1, 1);
3374 1.1 dholland MODULE_DEPEND(nfsd, nfslockd, 1, 1, 1);
3375 1.1 dholland MODULE_DEPEND(nfsd, krpc, 1, 1, 1);
3376 1.1 dholland MODULE_DEPEND(nfsd, nfssvc, 1, 1, 1);
3377 1.1 dholland
3378