nfs_subs.c revision 1.80 1 1.80 thorpej /* $NetBSD: nfs_subs.c,v 1.80 2000/08/03 06:15:05 thorpej Exp $ */
2 1.14 cgd
3 1.1 cgd /*
4 1.12 mycroft * Copyright (c) 1989, 1993
5 1.12 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * This code is derived from software contributed to Berkeley by
8 1.1 cgd * Rick Macklem at The University of Guelph.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer in the
17 1.1 cgd * documentation and/or other materials provided with the distribution.
18 1.1 cgd * 3. All advertising materials mentioning features or use of this software
19 1.1 cgd * must display the following acknowledgement:
20 1.1 cgd * This product includes software developed by the University of
21 1.1 cgd * California, Berkeley and its contributors.
22 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
23 1.1 cgd * may be used to endorse or promote products derived from this software
24 1.1 cgd * without specific prior written permission.
25 1.1 cgd *
26 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 1.1 cgd * SUCH DAMAGE.
37 1.1 cgd *
38 1.25 fvdl * @(#)nfs_subs.c 8.8 (Berkeley) 5/22/95
39 1.1 cgd */
40 1.1 cgd
41 1.55 thorpej #include "fs_nfs.h"
42 1.61 thorpej #include "opt_nfsserver.h"
43 1.62 jonathan #include "opt_iso.h"
44 1.76 fvdl #include "opt_inet.h"
45 1.25 fvdl
46 1.1 cgd /*
47 1.1 cgd * These functions support the macros and help fiddle mbuf chains for
48 1.1 cgd * the nfs op functions. They do things like create the rpc header and
49 1.1 cgd * copy data between mbuf chains and uio lists.
50 1.1 cgd */
51 1.9 mycroft #include <sys/param.h>
52 1.9 mycroft #include <sys/proc.h>
53 1.9 mycroft #include <sys/systm.h>
54 1.9 mycroft #include <sys/kernel.h>
55 1.9 mycroft #include <sys/mount.h>
56 1.9 mycroft #include <sys/vnode.h>
57 1.9 mycroft #include <sys/namei.h>
58 1.9 mycroft #include <sys/mbuf.h>
59 1.12 mycroft #include <sys/socket.h>
60 1.12 mycroft #include <sys/stat.h>
61 1.25 fvdl #include <sys/malloc.h>
62 1.30 fvdl #include <sys/time.h>
63 1.43 fvdl #include <sys/dirent.h>
64 1.1 cgd
65 1.51 mrg #include <uvm/uvm_extern.h>
66 1.51 mrg
67 1.9 mycroft #include <nfs/rpcv2.h>
68 1.25 fvdl #include <nfs/nfsproto.h>
69 1.9 mycroft #include <nfs/nfsnode.h>
70 1.9 mycroft #include <nfs/nfs.h>
71 1.9 mycroft #include <nfs/xdr_subs.h>
72 1.9 mycroft #include <nfs/nfsm_subs.h>
73 1.12 mycroft #include <nfs/nfsmount.h>
74 1.12 mycroft #include <nfs/nqnfs.h>
75 1.12 mycroft #include <nfs/nfsrtt.h>
76 1.24 christos #include <nfs/nfs_var.h>
77 1.12 mycroft
78 1.12 mycroft #include <miscfs/specfs/specdev.h>
79 1.24 christos
80 1.12 mycroft #include <netinet/in.h>
81 1.12 mycroft #ifdef ISO
82 1.12 mycroft #include <netiso/iso.h>
83 1.12 mycroft #endif
84 1.1 cgd
85 1.1 cgd /*
86 1.1 cgd * Data items converted to xdr at startup, since they are constant
87 1.1 cgd * This is kinda hokey, but may save a little time doing byte swaps
88 1.1 cgd */
89 1.22 cgd u_int32_t nfs_xdrneg1;
90 1.22 cgd u_int32_t rpc_call, rpc_vers, rpc_reply, rpc_msgdenied, rpc_autherr,
91 1.25 fvdl rpc_mismatch, rpc_auth_unix, rpc_msgaccepted,
92 1.12 mycroft rpc_auth_kerb;
93 1.25 fvdl u_int32_t nfs_prog, nqnfs_prog, nfs_true, nfs_false;
94 1.12 mycroft
95 1.1 cgd /* And other global data */
96 1.22 cgd static u_int32_t nfs_xid = 0;
97 1.25 fvdl nfstype nfsv2_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFNON,
98 1.25 fvdl NFCHR, NFNON };
99 1.25 fvdl nfstype nfsv3_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFSOCK,
100 1.25 fvdl NFFIFO, NFNON };
101 1.25 fvdl enum vtype nv2tov_type[8] = { VNON, VREG, VDIR, VBLK, VCHR, VLNK, VNON, VNON };
102 1.25 fvdl enum vtype nv3tov_type[8]={ VNON, VREG, VDIR, VBLK, VCHR, VLNK, VSOCK, VFIFO };
103 1.25 fvdl int nfs_ticks;
104 1.25 fvdl
105 1.35 thorpej /* NFS client/server stats. */
106 1.35 thorpej struct nfsstats nfsstats;
107 1.35 thorpej
108 1.25 fvdl /*
109 1.25 fvdl * Mapping of old NFS Version 2 RPC numbers to generic numbers.
110 1.25 fvdl */
111 1.25 fvdl int nfsv3_procid[NFS_NPROCS] = {
112 1.25 fvdl NFSPROC_NULL,
113 1.25 fvdl NFSPROC_GETATTR,
114 1.25 fvdl NFSPROC_SETATTR,
115 1.25 fvdl NFSPROC_NOOP,
116 1.25 fvdl NFSPROC_LOOKUP,
117 1.25 fvdl NFSPROC_READLINK,
118 1.25 fvdl NFSPROC_READ,
119 1.25 fvdl NFSPROC_NOOP,
120 1.25 fvdl NFSPROC_WRITE,
121 1.25 fvdl NFSPROC_CREATE,
122 1.25 fvdl NFSPROC_REMOVE,
123 1.25 fvdl NFSPROC_RENAME,
124 1.25 fvdl NFSPROC_LINK,
125 1.25 fvdl NFSPROC_SYMLINK,
126 1.25 fvdl NFSPROC_MKDIR,
127 1.25 fvdl NFSPROC_RMDIR,
128 1.25 fvdl NFSPROC_READDIR,
129 1.25 fvdl NFSPROC_FSSTAT,
130 1.25 fvdl NFSPROC_NOOP,
131 1.25 fvdl NFSPROC_NOOP,
132 1.25 fvdl NFSPROC_NOOP,
133 1.25 fvdl NFSPROC_NOOP,
134 1.25 fvdl NFSPROC_NOOP,
135 1.25 fvdl NFSPROC_NOOP,
136 1.25 fvdl NFSPROC_NOOP,
137 1.25 fvdl NFSPROC_NOOP
138 1.25 fvdl };
139 1.25 fvdl
140 1.25 fvdl /*
141 1.25 fvdl * and the reverse mapping from generic to Version 2 procedure numbers
142 1.25 fvdl */
143 1.25 fvdl int nfsv2_procid[NFS_NPROCS] = {
144 1.25 fvdl NFSV2PROC_NULL,
145 1.25 fvdl NFSV2PROC_GETATTR,
146 1.25 fvdl NFSV2PROC_SETATTR,
147 1.25 fvdl NFSV2PROC_LOOKUP,
148 1.25 fvdl NFSV2PROC_NOOP,
149 1.25 fvdl NFSV2PROC_READLINK,
150 1.25 fvdl NFSV2PROC_READ,
151 1.25 fvdl NFSV2PROC_WRITE,
152 1.25 fvdl NFSV2PROC_CREATE,
153 1.25 fvdl NFSV2PROC_MKDIR,
154 1.25 fvdl NFSV2PROC_SYMLINK,
155 1.25 fvdl NFSV2PROC_CREATE,
156 1.25 fvdl NFSV2PROC_REMOVE,
157 1.25 fvdl NFSV2PROC_RMDIR,
158 1.25 fvdl NFSV2PROC_RENAME,
159 1.25 fvdl NFSV2PROC_LINK,
160 1.25 fvdl NFSV2PROC_READDIR,
161 1.25 fvdl NFSV2PROC_NOOP,
162 1.25 fvdl NFSV2PROC_STATFS,
163 1.25 fvdl NFSV2PROC_NOOP,
164 1.25 fvdl NFSV2PROC_NOOP,
165 1.25 fvdl NFSV2PROC_NOOP,
166 1.25 fvdl NFSV2PROC_NOOP,
167 1.25 fvdl NFSV2PROC_NOOP,
168 1.25 fvdl NFSV2PROC_NOOP,
169 1.25 fvdl NFSV2PROC_NOOP,
170 1.25 fvdl };
171 1.25 fvdl
172 1.25 fvdl /*
173 1.25 fvdl * Maps errno values to nfs error numbers.
174 1.25 fvdl * Use NFSERR_IO as the catch all for ones not specifically defined in
175 1.25 fvdl * RFC 1094.
176 1.25 fvdl */
177 1.25 fvdl static u_char nfsrv_v2errmap[ELAST] = {
178 1.25 fvdl NFSERR_PERM, NFSERR_NOENT, NFSERR_IO, NFSERR_IO, NFSERR_IO,
179 1.25 fvdl NFSERR_NXIO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
180 1.25 fvdl NFSERR_IO, NFSERR_IO, NFSERR_ACCES, NFSERR_IO, NFSERR_IO,
181 1.25 fvdl NFSERR_IO, NFSERR_EXIST, NFSERR_IO, NFSERR_NODEV, NFSERR_NOTDIR,
182 1.25 fvdl NFSERR_ISDIR, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
183 1.25 fvdl NFSERR_IO, NFSERR_FBIG, NFSERR_NOSPC, NFSERR_IO, NFSERR_ROFS,
184 1.25 fvdl NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
185 1.25 fvdl NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
186 1.25 fvdl NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
187 1.25 fvdl NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
188 1.25 fvdl NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
189 1.25 fvdl NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
190 1.25 fvdl NFSERR_IO, NFSERR_IO, NFSERR_NAMETOL, NFSERR_IO, NFSERR_IO,
191 1.25 fvdl NFSERR_NOTEMPTY, NFSERR_IO, NFSERR_IO, NFSERR_DQUOT, NFSERR_STALE,
192 1.25 fvdl NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
193 1.25 fvdl NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
194 1.52 mikel NFSERR_IO, NFSERR_IO,
195 1.25 fvdl };
196 1.25 fvdl
197 1.25 fvdl /*
198 1.25 fvdl * Maps errno values to nfs error numbers.
199 1.25 fvdl * Although it is not obvious whether or not NFS clients really care if
200 1.25 fvdl * a returned error value is in the specified list for the procedure, the
201 1.25 fvdl * safest thing to do is filter them appropriately. For Version 2, the
202 1.25 fvdl * X/Open XNFS document is the only specification that defines error values
203 1.25 fvdl * for each RPC (The RFC simply lists all possible error values for all RPCs),
204 1.25 fvdl * so I have decided to not do this for Version 2.
205 1.25 fvdl * The first entry is the default error return and the rest are the valid
206 1.25 fvdl * errors for that RPC in increasing numeric order.
207 1.25 fvdl */
208 1.25 fvdl static short nfsv3err_null[] = {
209 1.25 fvdl 0,
210 1.25 fvdl 0,
211 1.25 fvdl };
212 1.25 fvdl
213 1.25 fvdl static short nfsv3err_getattr[] = {
214 1.25 fvdl NFSERR_IO,
215 1.25 fvdl NFSERR_IO,
216 1.25 fvdl NFSERR_STALE,
217 1.25 fvdl NFSERR_BADHANDLE,
218 1.25 fvdl NFSERR_SERVERFAULT,
219 1.25 fvdl 0,
220 1.25 fvdl };
221 1.25 fvdl
222 1.25 fvdl static short nfsv3err_setattr[] = {
223 1.25 fvdl NFSERR_IO,
224 1.25 fvdl NFSERR_PERM,
225 1.25 fvdl NFSERR_IO,
226 1.25 fvdl NFSERR_ACCES,
227 1.25 fvdl NFSERR_INVAL,
228 1.25 fvdl NFSERR_NOSPC,
229 1.25 fvdl NFSERR_ROFS,
230 1.25 fvdl NFSERR_DQUOT,
231 1.25 fvdl NFSERR_STALE,
232 1.25 fvdl NFSERR_BADHANDLE,
233 1.25 fvdl NFSERR_NOT_SYNC,
234 1.25 fvdl NFSERR_SERVERFAULT,
235 1.25 fvdl 0,
236 1.25 fvdl };
237 1.25 fvdl
238 1.25 fvdl static short nfsv3err_lookup[] = {
239 1.25 fvdl NFSERR_IO,
240 1.25 fvdl NFSERR_NOENT,
241 1.25 fvdl NFSERR_IO,
242 1.25 fvdl NFSERR_ACCES,
243 1.25 fvdl NFSERR_NOTDIR,
244 1.25 fvdl NFSERR_NAMETOL,
245 1.25 fvdl NFSERR_STALE,
246 1.25 fvdl NFSERR_BADHANDLE,
247 1.25 fvdl NFSERR_SERVERFAULT,
248 1.25 fvdl 0,
249 1.25 fvdl };
250 1.25 fvdl
251 1.25 fvdl static short nfsv3err_access[] = {
252 1.25 fvdl NFSERR_IO,
253 1.25 fvdl NFSERR_IO,
254 1.25 fvdl NFSERR_STALE,
255 1.25 fvdl NFSERR_BADHANDLE,
256 1.25 fvdl NFSERR_SERVERFAULT,
257 1.25 fvdl 0,
258 1.25 fvdl };
259 1.25 fvdl
260 1.25 fvdl static short nfsv3err_readlink[] = {
261 1.25 fvdl NFSERR_IO,
262 1.25 fvdl NFSERR_IO,
263 1.25 fvdl NFSERR_ACCES,
264 1.25 fvdl NFSERR_INVAL,
265 1.25 fvdl NFSERR_STALE,
266 1.25 fvdl NFSERR_BADHANDLE,
267 1.25 fvdl NFSERR_NOTSUPP,
268 1.25 fvdl NFSERR_SERVERFAULT,
269 1.25 fvdl 0,
270 1.25 fvdl };
271 1.25 fvdl
272 1.25 fvdl static short nfsv3err_read[] = {
273 1.25 fvdl NFSERR_IO,
274 1.25 fvdl NFSERR_IO,
275 1.25 fvdl NFSERR_NXIO,
276 1.25 fvdl NFSERR_ACCES,
277 1.25 fvdl NFSERR_INVAL,
278 1.25 fvdl NFSERR_STALE,
279 1.25 fvdl NFSERR_BADHANDLE,
280 1.25 fvdl NFSERR_SERVERFAULT,
281 1.67 fvdl NFSERR_JUKEBOX,
282 1.25 fvdl 0,
283 1.25 fvdl };
284 1.25 fvdl
285 1.25 fvdl static short nfsv3err_write[] = {
286 1.25 fvdl NFSERR_IO,
287 1.25 fvdl NFSERR_IO,
288 1.25 fvdl NFSERR_ACCES,
289 1.25 fvdl NFSERR_INVAL,
290 1.25 fvdl NFSERR_FBIG,
291 1.25 fvdl NFSERR_NOSPC,
292 1.25 fvdl NFSERR_ROFS,
293 1.25 fvdl NFSERR_DQUOT,
294 1.25 fvdl NFSERR_STALE,
295 1.25 fvdl NFSERR_BADHANDLE,
296 1.25 fvdl NFSERR_SERVERFAULT,
297 1.68 fvdl NFSERR_JUKEBOX,
298 1.25 fvdl 0,
299 1.25 fvdl };
300 1.25 fvdl
301 1.25 fvdl static short nfsv3err_create[] = {
302 1.25 fvdl NFSERR_IO,
303 1.25 fvdl NFSERR_IO,
304 1.25 fvdl NFSERR_ACCES,
305 1.25 fvdl NFSERR_EXIST,
306 1.25 fvdl NFSERR_NOTDIR,
307 1.25 fvdl NFSERR_NOSPC,
308 1.25 fvdl NFSERR_ROFS,
309 1.25 fvdl NFSERR_NAMETOL,
310 1.25 fvdl NFSERR_DQUOT,
311 1.25 fvdl NFSERR_STALE,
312 1.25 fvdl NFSERR_BADHANDLE,
313 1.25 fvdl NFSERR_NOTSUPP,
314 1.25 fvdl NFSERR_SERVERFAULT,
315 1.25 fvdl 0,
316 1.25 fvdl };
317 1.25 fvdl
318 1.25 fvdl static short nfsv3err_mkdir[] = {
319 1.25 fvdl NFSERR_IO,
320 1.25 fvdl NFSERR_IO,
321 1.25 fvdl NFSERR_ACCES,
322 1.25 fvdl NFSERR_EXIST,
323 1.25 fvdl NFSERR_NOTDIR,
324 1.25 fvdl NFSERR_NOSPC,
325 1.25 fvdl NFSERR_ROFS,
326 1.25 fvdl NFSERR_NAMETOL,
327 1.25 fvdl NFSERR_DQUOT,
328 1.25 fvdl NFSERR_STALE,
329 1.25 fvdl NFSERR_BADHANDLE,
330 1.25 fvdl NFSERR_NOTSUPP,
331 1.25 fvdl NFSERR_SERVERFAULT,
332 1.25 fvdl 0,
333 1.25 fvdl };
334 1.25 fvdl
335 1.25 fvdl static short nfsv3err_symlink[] = {
336 1.25 fvdl NFSERR_IO,
337 1.25 fvdl NFSERR_IO,
338 1.25 fvdl NFSERR_ACCES,
339 1.25 fvdl NFSERR_EXIST,
340 1.25 fvdl NFSERR_NOTDIR,
341 1.25 fvdl NFSERR_NOSPC,
342 1.25 fvdl NFSERR_ROFS,
343 1.25 fvdl NFSERR_NAMETOL,
344 1.25 fvdl NFSERR_DQUOT,
345 1.25 fvdl NFSERR_STALE,
346 1.25 fvdl NFSERR_BADHANDLE,
347 1.25 fvdl NFSERR_NOTSUPP,
348 1.25 fvdl NFSERR_SERVERFAULT,
349 1.25 fvdl 0,
350 1.25 fvdl };
351 1.25 fvdl
352 1.25 fvdl static short nfsv3err_mknod[] = {
353 1.25 fvdl NFSERR_IO,
354 1.25 fvdl NFSERR_IO,
355 1.25 fvdl NFSERR_ACCES,
356 1.25 fvdl NFSERR_EXIST,
357 1.25 fvdl NFSERR_NOTDIR,
358 1.25 fvdl NFSERR_NOSPC,
359 1.25 fvdl NFSERR_ROFS,
360 1.25 fvdl NFSERR_NAMETOL,
361 1.25 fvdl NFSERR_DQUOT,
362 1.25 fvdl NFSERR_STALE,
363 1.25 fvdl NFSERR_BADHANDLE,
364 1.25 fvdl NFSERR_NOTSUPP,
365 1.25 fvdl NFSERR_SERVERFAULT,
366 1.25 fvdl NFSERR_BADTYPE,
367 1.25 fvdl 0,
368 1.25 fvdl };
369 1.25 fvdl
370 1.25 fvdl static short nfsv3err_remove[] = {
371 1.25 fvdl NFSERR_IO,
372 1.25 fvdl NFSERR_NOENT,
373 1.25 fvdl NFSERR_IO,
374 1.25 fvdl NFSERR_ACCES,
375 1.25 fvdl NFSERR_NOTDIR,
376 1.25 fvdl NFSERR_ROFS,
377 1.25 fvdl NFSERR_NAMETOL,
378 1.25 fvdl NFSERR_STALE,
379 1.25 fvdl NFSERR_BADHANDLE,
380 1.25 fvdl NFSERR_SERVERFAULT,
381 1.25 fvdl 0,
382 1.25 fvdl };
383 1.25 fvdl
384 1.25 fvdl static short nfsv3err_rmdir[] = {
385 1.25 fvdl NFSERR_IO,
386 1.25 fvdl NFSERR_NOENT,
387 1.25 fvdl NFSERR_IO,
388 1.25 fvdl NFSERR_ACCES,
389 1.25 fvdl NFSERR_EXIST,
390 1.25 fvdl NFSERR_NOTDIR,
391 1.25 fvdl NFSERR_INVAL,
392 1.25 fvdl NFSERR_ROFS,
393 1.25 fvdl NFSERR_NAMETOL,
394 1.25 fvdl NFSERR_NOTEMPTY,
395 1.25 fvdl NFSERR_STALE,
396 1.25 fvdl NFSERR_BADHANDLE,
397 1.25 fvdl NFSERR_NOTSUPP,
398 1.25 fvdl NFSERR_SERVERFAULT,
399 1.25 fvdl 0,
400 1.25 fvdl };
401 1.25 fvdl
402 1.25 fvdl static short nfsv3err_rename[] = {
403 1.25 fvdl NFSERR_IO,
404 1.25 fvdl NFSERR_NOENT,
405 1.25 fvdl NFSERR_IO,
406 1.25 fvdl NFSERR_ACCES,
407 1.25 fvdl NFSERR_EXIST,
408 1.25 fvdl NFSERR_XDEV,
409 1.25 fvdl NFSERR_NOTDIR,
410 1.25 fvdl NFSERR_ISDIR,
411 1.25 fvdl NFSERR_INVAL,
412 1.25 fvdl NFSERR_NOSPC,
413 1.25 fvdl NFSERR_ROFS,
414 1.25 fvdl NFSERR_MLINK,
415 1.25 fvdl NFSERR_NAMETOL,
416 1.25 fvdl NFSERR_NOTEMPTY,
417 1.25 fvdl NFSERR_DQUOT,
418 1.25 fvdl NFSERR_STALE,
419 1.25 fvdl NFSERR_BADHANDLE,
420 1.25 fvdl NFSERR_NOTSUPP,
421 1.25 fvdl NFSERR_SERVERFAULT,
422 1.25 fvdl 0,
423 1.25 fvdl };
424 1.25 fvdl
425 1.25 fvdl static short nfsv3err_link[] = {
426 1.25 fvdl NFSERR_IO,
427 1.25 fvdl NFSERR_IO,
428 1.25 fvdl NFSERR_ACCES,
429 1.25 fvdl NFSERR_EXIST,
430 1.25 fvdl NFSERR_XDEV,
431 1.25 fvdl NFSERR_NOTDIR,
432 1.25 fvdl NFSERR_INVAL,
433 1.25 fvdl NFSERR_NOSPC,
434 1.25 fvdl NFSERR_ROFS,
435 1.25 fvdl NFSERR_MLINK,
436 1.25 fvdl NFSERR_NAMETOL,
437 1.25 fvdl NFSERR_DQUOT,
438 1.25 fvdl NFSERR_STALE,
439 1.25 fvdl NFSERR_BADHANDLE,
440 1.25 fvdl NFSERR_NOTSUPP,
441 1.25 fvdl NFSERR_SERVERFAULT,
442 1.25 fvdl 0,
443 1.25 fvdl };
444 1.25 fvdl
445 1.25 fvdl static short nfsv3err_readdir[] = {
446 1.25 fvdl NFSERR_IO,
447 1.25 fvdl NFSERR_IO,
448 1.25 fvdl NFSERR_ACCES,
449 1.25 fvdl NFSERR_NOTDIR,
450 1.25 fvdl NFSERR_STALE,
451 1.25 fvdl NFSERR_BADHANDLE,
452 1.25 fvdl NFSERR_BAD_COOKIE,
453 1.25 fvdl NFSERR_TOOSMALL,
454 1.25 fvdl NFSERR_SERVERFAULT,
455 1.25 fvdl 0,
456 1.25 fvdl };
457 1.25 fvdl
458 1.25 fvdl static short nfsv3err_readdirplus[] = {
459 1.25 fvdl NFSERR_IO,
460 1.25 fvdl NFSERR_IO,
461 1.25 fvdl NFSERR_ACCES,
462 1.25 fvdl NFSERR_NOTDIR,
463 1.25 fvdl NFSERR_STALE,
464 1.25 fvdl NFSERR_BADHANDLE,
465 1.25 fvdl NFSERR_BAD_COOKIE,
466 1.25 fvdl NFSERR_NOTSUPP,
467 1.25 fvdl NFSERR_TOOSMALL,
468 1.25 fvdl NFSERR_SERVERFAULT,
469 1.25 fvdl 0,
470 1.25 fvdl };
471 1.25 fvdl
472 1.25 fvdl static short nfsv3err_fsstat[] = {
473 1.25 fvdl NFSERR_IO,
474 1.25 fvdl NFSERR_IO,
475 1.25 fvdl NFSERR_STALE,
476 1.25 fvdl NFSERR_BADHANDLE,
477 1.25 fvdl NFSERR_SERVERFAULT,
478 1.25 fvdl 0,
479 1.25 fvdl };
480 1.25 fvdl
481 1.25 fvdl static short nfsv3err_fsinfo[] = {
482 1.25 fvdl NFSERR_STALE,
483 1.25 fvdl NFSERR_STALE,
484 1.25 fvdl NFSERR_BADHANDLE,
485 1.25 fvdl NFSERR_SERVERFAULT,
486 1.25 fvdl 0,
487 1.25 fvdl };
488 1.25 fvdl
489 1.25 fvdl static short nfsv3err_pathconf[] = {
490 1.25 fvdl NFSERR_STALE,
491 1.25 fvdl NFSERR_STALE,
492 1.25 fvdl NFSERR_BADHANDLE,
493 1.25 fvdl NFSERR_SERVERFAULT,
494 1.25 fvdl 0,
495 1.25 fvdl };
496 1.25 fvdl
497 1.25 fvdl static short nfsv3err_commit[] = {
498 1.25 fvdl NFSERR_IO,
499 1.25 fvdl NFSERR_IO,
500 1.25 fvdl NFSERR_STALE,
501 1.25 fvdl NFSERR_BADHANDLE,
502 1.25 fvdl NFSERR_SERVERFAULT,
503 1.25 fvdl 0,
504 1.25 fvdl };
505 1.25 fvdl
506 1.25 fvdl static short *nfsrv_v3errmap[] = {
507 1.25 fvdl nfsv3err_null,
508 1.25 fvdl nfsv3err_getattr,
509 1.25 fvdl nfsv3err_setattr,
510 1.25 fvdl nfsv3err_lookup,
511 1.25 fvdl nfsv3err_access,
512 1.25 fvdl nfsv3err_readlink,
513 1.25 fvdl nfsv3err_read,
514 1.25 fvdl nfsv3err_write,
515 1.25 fvdl nfsv3err_create,
516 1.25 fvdl nfsv3err_mkdir,
517 1.25 fvdl nfsv3err_symlink,
518 1.25 fvdl nfsv3err_mknod,
519 1.25 fvdl nfsv3err_remove,
520 1.25 fvdl nfsv3err_rmdir,
521 1.25 fvdl nfsv3err_rename,
522 1.25 fvdl nfsv3err_link,
523 1.25 fvdl nfsv3err_readdir,
524 1.25 fvdl nfsv3err_readdirplus,
525 1.25 fvdl nfsv3err_fsstat,
526 1.25 fvdl nfsv3err_fsinfo,
527 1.25 fvdl nfsv3err_pathconf,
528 1.25 fvdl nfsv3err_commit,
529 1.25 fvdl };
530 1.25 fvdl
531 1.12 mycroft extern struct nfsrtt nfsrtt;
532 1.12 mycroft extern time_t nqnfsstarttime;
533 1.12 mycroft extern int nqsrv_clockskew;
534 1.12 mycroft extern int nqsrv_writeslack;
535 1.12 mycroft extern int nqsrv_maxlease;
536 1.25 fvdl extern int nqnfs_piggy[NFS_NPROCS];
537 1.25 fvdl extern struct nfsnodehashhead *nfsnodehashtbl;
538 1.25 fvdl extern u_long nfsnodehash;
539 1.1 cgd
540 1.18 mycroft LIST_HEAD(nfsnodehashhead, nfsnode);
541 1.46 fvdl u_long nfsdirhashmask;
542 1.18 mycroft
543 1.43 fvdl int nfs_webnamei __P((struct nameidata *, struct vnode *, struct proc *));
544 1.43 fvdl
545 1.1 cgd /*
546 1.1 cgd * Create the header for an rpc request packet
547 1.1 cgd * The hsiz is the size of the rest of the nfs request header.
548 1.1 cgd * (just used to decide if a cluster is a good idea)
549 1.1 cgd */
550 1.12 mycroft struct mbuf *
551 1.12 mycroft nfsm_reqh(vp, procid, hsiz, bposp)
552 1.12 mycroft struct vnode *vp;
553 1.1 cgd u_long procid;
554 1.1 cgd int hsiz;
555 1.12 mycroft caddr_t *bposp;
556 1.12 mycroft {
557 1.75 augustss struct mbuf *mb;
558 1.75 augustss u_int32_t *tl;
559 1.75 augustss caddr_t bpos;
560 1.12 mycroft struct mbuf *mb2;
561 1.12 mycroft struct nfsmount *nmp;
562 1.12 mycroft int nqflag;
563 1.12 mycroft
564 1.12 mycroft MGET(mb, M_WAIT, MT_DATA);
565 1.12 mycroft if (hsiz >= MINCLSIZE)
566 1.12 mycroft MCLGET(mb, M_WAIT);
567 1.12 mycroft mb->m_len = 0;
568 1.12 mycroft bpos = mtod(mb, caddr_t);
569 1.12 mycroft
570 1.12 mycroft /*
571 1.12 mycroft * For NQNFS, add lease request.
572 1.12 mycroft */
573 1.12 mycroft if (vp) {
574 1.12 mycroft nmp = VFSTONFS(vp->v_mount);
575 1.12 mycroft if (nmp->nm_flag & NFSMNT_NQNFS) {
576 1.12 mycroft nqflag = NQNFS_NEEDLEASE(vp, procid);
577 1.12 mycroft if (nqflag) {
578 1.22 cgd nfsm_build(tl, u_int32_t *, 2*NFSX_UNSIGNED);
579 1.12 mycroft *tl++ = txdr_unsigned(nqflag);
580 1.12 mycroft *tl = txdr_unsigned(nmp->nm_leaseterm);
581 1.12 mycroft } else {
582 1.22 cgd nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
583 1.12 mycroft *tl = 0;
584 1.12 mycroft }
585 1.12 mycroft }
586 1.12 mycroft }
587 1.12 mycroft /* Finally, return values */
588 1.12 mycroft *bposp = bpos;
589 1.12 mycroft return (mb);
590 1.12 mycroft }
591 1.12 mycroft
592 1.12 mycroft /*
593 1.12 mycroft * Build the RPC header and fill in the authorization info.
594 1.12 mycroft * The authorization string argument is only used when the credentials
595 1.12 mycroft * come from outside of the kernel.
596 1.12 mycroft * Returns the head of the mbuf list.
597 1.12 mycroft */
598 1.12 mycroft struct mbuf *
599 1.25 fvdl nfsm_rpchead(cr, nmflag, procid, auth_type, auth_len, auth_str, verf_len,
600 1.25 fvdl verf_str, mrest, mrest_len, mbp, xidp)
601 1.75 augustss struct ucred *cr;
602 1.25 fvdl int nmflag;
603 1.12 mycroft int procid;
604 1.12 mycroft int auth_type;
605 1.12 mycroft int auth_len;
606 1.12 mycroft char *auth_str;
607 1.25 fvdl int verf_len;
608 1.25 fvdl char *verf_str;
609 1.12 mycroft struct mbuf *mrest;
610 1.12 mycroft int mrest_len;
611 1.12 mycroft struct mbuf **mbp;
612 1.22 cgd u_int32_t *xidp;
613 1.1 cgd {
614 1.75 augustss struct mbuf *mb;
615 1.75 augustss u_int32_t *tl;
616 1.75 augustss caddr_t bpos;
617 1.75 augustss int i;
618 1.12 mycroft struct mbuf *mreq, *mb2;
619 1.12 mycroft int siz, grpsiz, authsiz;
620 1.30 fvdl struct timeval tv;
621 1.30 fvdl static u_int32_t base;
622 1.1 cgd
623 1.12 mycroft authsiz = nfsm_rndup(auth_len);
624 1.12 mycroft MGETHDR(mb, M_WAIT, MT_DATA);
625 1.25 fvdl if ((authsiz + 10 * NFSX_UNSIGNED) >= MINCLSIZE) {
626 1.12 mycroft MCLGET(mb, M_WAIT);
627 1.25 fvdl } else if ((authsiz + 10 * NFSX_UNSIGNED) < MHLEN) {
628 1.25 fvdl MH_ALIGN(mb, authsiz + 10 * NFSX_UNSIGNED);
629 1.12 mycroft } else {
630 1.25 fvdl MH_ALIGN(mb, 8 * NFSX_UNSIGNED);
631 1.1 cgd }
632 1.12 mycroft mb->m_len = 0;
633 1.12 mycroft mreq = mb;
634 1.12 mycroft bpos = mtod(mb, caddr_t);
635 1.12 mycroft
636 1.12 mycroft /*
637 1.12 mycroft * First the RPC header.
638 1.12 mycroft */
639 1.25 fvdl nfsm_build(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
640 1.30 fvdl
641 1.30 fvdl /*
642 1.30 fvdl * derive initial xid from system time
643 1.30 fvdl * XXX time is invalid if root not yet mounted
644 1.30 fvdl */
645 1.30 fvdl if (!base && (rootvp)) {
646 1.30 fvdl microtime(&tv);
647 1.30 fvdl base = tv.tv_sec << 12;
648 1.30 fvdl nfs_xid = base;
649 1.30 fvdl }
650 1.30 fvdl /*
651 1.30 fvdl * Skip zero xid if it should ever happen.
652 1.30 fvdl */
653 1.12 mycroft if (++nfs_xid == 0)
654 1.12 mycroft nfs_xid++;
655 1.30 fvdl
656 1.12 mycroft *tl++ = *xidp = txdr_unsigned(nfs_xid);
657 1.1 cgd *tl++ = rpc_call;
658 1.1 cgd *tl++ = rpc_vers;
659 1.25 fvdl if (nmflag & NFSMNT_NQNFS) {
660 1.12 mycroft *tl++ = txdr_unsigned(NQNFS_PROG);
661 1.25 fvdl *tl++ = txdr_unsigned(NQNFS_VER3);
662 1.1 cgd } else {
663 1.12 mycroft *tl++ = txdr_unsigned(NFS_PROG);
664 1.25 fvdl if (nmflag & NFSMNT_NFSV3)
665 1.25 fvdl *tl++ = txdr_unsigned(NFS_VER3);
666 1.25 fvdl else
667 1.25 fvdl *tl++ = txdr_unsigned(NFS_VER2);
668 1.1 cgd }
669 1.25 fvdl if (nmflag & NFSMNT_NFSV3)
670 1.25 fvdl *tl++ = txdr_unsigned(procid);
671 1.25 fvdl else
672 1.25 fvdl *tl++ = txdr_unsigned(nfsv2_procid[procid]);
673 1.12 mycroft
674 1.12 mycroft /*
675 1.12 mycroft * And then the authorization cred.
676 1.12 mycroft */
677 1.12 mycroft *tl++ = txdr_unsigned(auth_type);
678 1.12 mycroft *tl = txdr_unsigned(authsiz);
679 1.12 mycroft switch (auth_type) {
680 1.12 mycroft case RPCAUTH_UNIX:
681 1.22 cgd nfsm_build(tl, u_int32_t *, auth_len);
682 1.12 mycroft *tl++ = 0; /* stamp ?? */
683 1.12 mycroft *tl++ = 0; /* NULL hostname */
684 1.12 mycroft *tl++ = txdr_unsigned(cr->cr_uid);
685 1.19 jtc *tl++ = txdr_unsigned(cr->cr_gid);
686 1.12 mycroft grpsiz = (auth_len >> 2) - 5;
687 1.12 mycroft *tl++ = txdr_unsigned(grpsiz);
688 1.20 mycroft for (i = 0; i < grpsiz; i++)
689 1.12 mycroft *tl++ = txdr_unsigned(cr->cr_groups[i]);
690 1.12 mycroft break;
691 1.25 fvdl case RPCAUTH_KERB4:
692 1.12 mycroft siz = auth_len;
693 1.12 mycroft while (siz > 0) {
694 1.12 mycroft if (M_TRAILINGSPACE(mb) == 0) {
695 1.12 mycroft MGET(mb2, M_WAIT, MT_DATA);
696 1.12 mycroft if (siz >= MINCLSIZE)
697 1.12 mycroft MCLGET(mb2, M_WAIT);
698 1.12 mycroft mb->m_next = mb2;
699 1.12 mycroft mb = mb2;
700 1.12 mycroft mb->m_len = 0;
701 1.12 mycroft bpos = mtod(mb, caddr_t);
702 1.12 mycroft }
703 1.12 mycroft i = min(siz, M_TRAILINGSPACE(mb));
704 1.63 perry memcpy(bpos, auth_str, i);
705 1.12 mycroft mb->m_len += i;
706 1.12 mycroft auth_str += i;
707 1.12 mycroft bpos += i;
708 1.12 mycroft siz -= i;
709 1.12 mycroft }
710 1.12 mycroft if ((siz = (nfsm_rndup(auth_len) - auth_len)) > 0) {
711 1.12 mycroft for (i = 0; i < siz; i++)
712 1.12 mycroft *bpos++ = '\0';
713 1.12 mycroft mb->m_len += siz;
714 1.12 mycroft }
715 1.12 mycroft break;
716 1.12 mycroft };
717 1.25 fvdl
718 1.25 fvdl /*
719 1.25 fvdl * And the verifier...
720 1.25 fvdl */
721 1.25 fvdl nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
722 1.25 fvdl if (verf_str) {
723 1.25 fvdl *tl++ = txdr_unsigned(RPCAUTH_KERB4);
724 1.25 fvdl *tl = txdr_unsigned(verf_len);
725 1.25 fvdl siz = verf_len;
726 1.25 fvdl while (siz > 0) {
727 1.25 fvdl if (M_TRAILINGSPACE(mb) == 0) {
728 1.25 fvdl MGET(mb2, M_WAIT, MT_DATA);
729 1.25 fvdl if (siz >= MINCLSIZE)
730 1.25 fvdl MCLGET(mb2, M_WAIT);
731 1.25 fvdl mb->m_next = mb2;
732 1.25 fvdl mb = mb2;
733 1.25 fvdl mb->m_len = 0;
734 1.25 fvdl bpos = mtod(mb, caddr_t);
735 1.25 fvdl }
736 1.25 fvdl i = min(siz, M_TRAILINGSPACE(mb));
737 1.63 perry memcpy(bpos, verf_str, i);
738 1.25 fvdl mb->m_len += i;
739 1.25 fvdl verf_str += i;
740 1.25 fvdl bpos += i;
741 1.25 fvdl siz -= i;
742 1.25 fvdl }
743 1.25 fvdl if ((siz = (nfsm_rndup(verf_len) - verf_len)) > 0) {
744 1.25 fvdl for (i = 0; i < siz; i++)
745 1.25 fvdl *bpos++ = '\0';
746 1.25 fvdl mb->m_len += siz;
747 1.25 fvdl }
748 1.25 fvdl } else {
749 1.25 fvdl *tl++ = txdr_unsigned(RPCAUTH_NULL);
750 1.25 fvdl *tl = 0;
751 1.25 fvdl }
752 1.12 mycroft mb->m_next = mrest;
753 1.25 fvdl mreq->m_pkthdr.len = authsiz + 10 * NFSX_UNSIGNED + mrest_len;
754 1.12 mycroft mreq->m_pkthdr.rcvif = (struct ifnet *)0;
755 1.12 mycroft *mbp = mb;
756 1.1 cgd return (mreq);
757 1.1 cgd }
758 1.1 cgd
759 1.1 cgd /*
760 1.1 cgd * copies mbuf chain to the uio scatter/gather list
761 1.1 cgd */
762 1.24 christos int
763 1.1 cgd nfsm_mbuftouio(mrep, uiop, siz, dpos)
764 1.1 cgd struct mbuf **mrep;
765 1.75 augustss struct uio *uiop;
766 1.1 cgd int siz;
767 1.1 cgd caddr_t *dpos;
768 1.1 cgd {
769 1.75 augustss char *mbufcp, *uiocp;
770 1.75 augustss int xfer, left, len;
771 1.75 augustss struct mbuf *mp;
772 1.1 cgd long uiosiz, rem;
773 1.1 cgd int error = 0;
774 1.1 cgd
775 1.1 cgd mp = *mrep;
776 1.1 cgd mbufcp = *dpos;
777 1.1 cgd len = mtod(mp, caddr_t)+mp->m_len-mbufcp;
778 1.1 cgd rem = nfsm_rndup(siz)-siz;
779 1.1 cgd while (siz > 0) {
780 1.1 cgd if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
781 1.1 cgd return (EFBIG);
782 1.1 cgd left = uiop->uio_iov->iov_len;
783 1.1 cgd uiocp = uiop->uio_iov->iov_base;
784 1.1 cgd if (left > siz)
785 1.1 cgd left = siz;
786 1.1 cgd uiosiz = left;
787 1.1 cgd while (left > 0) {
788 1.1 cgd while (len == 0) {
789 1.1 cgd mp = mp->m_next;
790 1.1 cgd if (mp == NULL)
791 1.1 cgd return (EBADRPC);
792 1.1 cgd mbufcp = mtod(mp, caddr_t);
793 1.1 cgd len = mp->m_len;
794 1.1 cgd }
795 1.1 cgd xfer = (left > len) ? len : left;
796 1.1 cgd #ifdef notdef
797 1.1 cgd /* Not Yet.. */
798 1.1 cgd if (uiop->uio_iov->iov_op != NULL)
799 1.1 cgd (*(uiop->uio_iov->iov_op))
800 1.1 cgd (mbufcp, uiocp, xfer);
801 1.1 cgd else
802 1.1 cgd #endif
803 1.1 cgd if (uiop->uio_segflg == UIO_SYSSPACE)
804 1.63 perry memcpy(uiocp, mbufcp, xfer);
805 1.1 cgd else
806 1.1 cgd copyout(mbufcp, uiocp, xfer);
807 1.1 cgd left -= xfer;
808 1.1 cgd len -= xfer;
809 1.1 cgd mbufcp += xfer;
810 1.1 cgd uiocp += xfer;
811 1.1 cgd uiop->uio_offset += xfer;
812 1.1 cgd uiop->uio_resid -= xfer;
813 1.1 cgd }
814 1.1 cgd if (uiop->uio_iov->iov_len <= siz) {
815 1.1 cgd uiop->uio_iovcnt--;
816 1.1 cgd uiop->uio_iov++;
817 1.1 cgd } else {
818 1.58 kleink (caddr_t)uiop->uio_iov->iov_base += uiosiz;
819 1.1 cgd uiop->uio_iov->iov_len -= uiosiz;
820 1.1 cgd }
821 1.1 cgd siz -= uiosiz;
822 1.1 cgd }
823 1.1 cgd *dpos = mbufcp;
824 1.1 cgd *mrep = mp;
825 1.1 cgd if (rem > 0) {
826 1.1 cgd if (len < rem)
827 1.1 cgd error = nfs_adv(mrep, dpos, rem, len);
828 1.1 cgd else
829 1.1 cgd *dpos += rem;
830 1.1 cgd }
831 1.1 cgd return (error);
832 1.1 cgd }
833 1.1 cgd
834 1.1 cgd /*
835 1.29 fvdl * copies a uio scatter/gather list to an mbuf chain.
836 1.29 fvdl * NOTE: can ony handle iovcnt == 1
837 1.1 cgd */
838 1.24 christos int
839 1.1 cgd nfsm_uiotombuf(uiop, mq, siz, bpos)
840 1.75 augustss struct uio *uiop;
841 1.1 cgd struct mbuf **mq;
842 1.1 cgd int siz;
843 1.1 cgd caddr_t *bpos;
844 1.1 cgd {
845 1.75 augustss char *uiocp;
846 1.75 augustss struct mbuf *mp, *mp2;
847 1.75 augustss int xfer, left, mlen;
848 1.1 cgd int uiosiz, clflg, rem;
849 1.1 cgd char *cp;
850 1.1 cgd
851 1.29 fvdl #ifdef DIAGNOSTIC
852 1.29 fvdl if (uiop->uio_iovcnt != 1)
853 1.29 fvdl panic("nfsm_uiotombuf: iovcnt != 1");
854 1.29 fvdl #endif
855 1.29 fvdl
856 1.1 cgd if (siz > MLEN) /* or should it >= MCLBYTES ?? */
857 1.1 cgd clflg = 1;
858 1.1 cgd else
859 1.1 cgd clflg = 0;
860 1.1 cgd rem = nfsm_rndup(siz)-siz;
861 1.12 mycroft mp = mp2 = *mq;
862 1.1 cgd while (siz > 0) {
863 1.1 cgd left = uiop->uio_iov->iov_len;
864 1.1 cgd uiocp = uiop->uio_iov->iov_base;
865 1.1 cgd if (left > siz)
866 1.1 cgd left = siz;
867 1.1 cgd uiosiz = left;
868 1.1 cgd while (left > 0) {
869 1.12 mycroft mlen = M_TRAILINGSPACE(mp);
870 1.12 mycroft if (mlen == 0) {
871 1.12 mycroft MGET(mp, M_WAIT, MT_DATA);
872 1.12 mycroft if (clflg)
873 1.12 mycroft MCLGET(mp, M_WAIT);
874 1.12 mycroft mp->m_len = 0;
875 1.12 mycroft mp2->m_next = mp;
876 1.12 mycroft mp2 = mp;
877 1.12 mycroft mlen = M_TRAILINGSPACE(mp);
878 1.12 mycroft }
879 1.12 mycroft xfer = (left > mlen) ? mlen : left;
880 1.1 cgd #ifdef notdef
881 1.1 cgd /* Not Yet.. */
882 1.1 cgd if (uiop->uio_iov->iov_op != NULL)
883 1.1 cgd (*(uiop->uio_iov->iov_op))
884 1.12 mycroft (uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
885 1.1 cgd else
886 1.1 cgd #endif
887 1.1 cgd if (uiop->uio_segflg == UIO_SYSSPACE)
888 1.63 perry memcpy(mtod(mp, caddr_t)+mp->m_len, uiocp, xfer);
889 1.1 cgd else
890 1.12 mycroft copyin(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
891 1.12 mycroft mp->m_len += xfer;
892 1.1 cgd left -= xfer;
893 1.1 cgd uiocp += xfer;
894 1.1 cgd uiop->uio_offset += xfer;
895 1.1 cgd uiop->uio_resid -= xfer;
896 1.1 cgd }
897 1.58 kleink (caddr_t)uiop->uio_iov->iov_base += uiosiz;
898 1.29 fvdl uiop->uio_iov->iov_len -= uiosiz;
899 1.1 cgd siz -= uiosiz;
900 1.1 cgd }
901 1.1 cgd if (rem > 0) {
902 1.12 mycroft if (rem > M_TRAILINGSPACE(mp)) {
903 1.1 cgd MGET(mp, M_WAIT, MT_DATA);
904 1.1 cgd mp->m_len = 0;
905 1.1 cgd mp2->m_next = mp;
906 1.1 cgd }
907 1.1 cgd cp = mtod(mp, caddr_t)+mp->m_len;
908 1.1 cgd for (left = 0; left < rem; left++)
909 1.1 cgd *cp++ = '\0';
910 1.1 cgd mp->m_len += rem;
911 1.1 cgd *bpos = cp;
912 1.1 cgd } else
913 1.1 cgd *bpos = mtod(mp, caddr_t)+mp->m_len;
914 1.1 cgd *mq = mp;
915 1.1 cgd return (0);
916 1.1 cgd }
917 1.1 cgd
918 1.1 cgd /*
919 1.39 fvdl * Get at least "siz" bytes of correctly aligned data.
920 1.39 fvdl * When called the mbuf pointers are not necessarily correct,
921 1.39 fvdl * dsosp points to what ought to be in m_data and left contains
922 1.39 fvdl * what ought to be in m_len.
923 1.12 mycroft * This is used by the macros nfsm_dissect and nfsm_dissecton for tough
924 1.1 cgd * cases. (The macros use the vars. dpos and dpos2)
925 1.1 cgd */
926 1.24 christos int
927 1.12 mycroft nfsm_disct(mdp, dposp, siz, left, cp2)
928 1.1 cgd struct mbuf **mdp;
929 1.1 cgd caddr_t *dposp;
930 1.1 cgd int siz;
931 1.1 cgd int left;
932 1.1 cgd caddr_t *cp2;
933 1.1 cgd {
934 1.75 augustss struct mbuf *m1, *m2;
935 1.39 fvdl struct mbuf *havebuf = NULL;
936 1.39 fvdl caddr_t src = *dposp;
937 1.39 fvdl caddr_t dst;
938 1.39 fvdl int len;
939 1.39 fvdl
940 1.39 fvdl #ifdef DEBUG
941 1.39 fvdl if (left < 0)
942 1.39 fvdl panic("nfsm_disct: left < 0");
943 1.39 fvdl #endif
944 1.39 fvdl m1 = *mdp;
945 1.39 fvdl /*
946 1.39 fvdl * Skip through the mbuf chain looking for an mbuf with
947 1.39 fvdl * some data. If the first mbuf found has enough data
948 1.39 fvdl * and it is correctly aligned return it.
949 1.39 fvdl */
950 1.1 cgd while (left == 0) {
951 1.39 fvdl havebuf = m1;
952 1.39 fvdl *mdp = m1 = m1->m_next;
953 1.39 fvdl if (m1 == NULL)
954 1.1 cgd return (EBADRPC);
955 1.39 fvdl src = mtod(m1, caddr_t);
956 1.39 fvdl left = m1->m_len;
957 1.39 fvdl /*
958 1.39 fvdl * If we start a new mbuf and it is big enough
959 1.39 fvdl * and correctly aligned just return it, don't
960 1.39 fvdl * do any pull up.
961 1.39 fvdl */
962 1.39 fvdl if (left >= siz && nfsm_aligned(src)) {
963 1.39 fvdl *cp2 = src;
964 1.39 fvdl *dposp = src + siz;
965 1.39 fvdl return (0);
966 1.39 fvdl }
967 1.1 cgd }
968 1.39 fvdl if (m1->m_flags & M_EXT) {
969 1.39 fvdl if (havebuf) {
970 1.39 fvdl /* If the first mbuf with data has external data
971 1.39 fvdl * and there is a previous empty mbuf use it
972 1.39 fvdl * to move the data into.
973 1.39 fvdl */
974 1.39 fvdl m2 = m1;
975 1.39 fvdl *mdp = m1 = havebuf;
976 1.39 fvdl if (m1->m_flags & M_EXT) {
977 1.41 thorpej MEXTREMOVE(m1);
978 1.1 cgd }
979 1.39 fvdl } else {
980 1.39 fvdl /*
981 1.39 fvdl * If the first mbuf has a external data
982 1.39 fvdl * and there is no previous empty mbuf
983 1.39 fvdl * allocate a new mbuf and move the external
984 1.39 fvdl * data to the new mbuf. Also make the first
985 1.39 fvdl * mbuf look empty.
986 1.39 fvdl */
987 1.39 fvdl m2 = m_get(M_WAIT, MT_DATA);
988 1.39 fvdl m2->m_ext = m1->m_ext;
989 1.39 fvdl m2->m_data = src;
990 1.39 fvdl m2->m_len = left;
991 1.41 thorpej MCLADDREFERENCE(m1, m2);
992 1.41 thorpej MEXTREMOVE(m1);
993 1.39 fvdl m2->m_next = m1->m_next;
994 1.39 fvdl m1->m_next = m2;
995 1.1 cgd }
996 1.39 fvdl m1->m_len = 0;
997 1.39 fvdl dst = m1->m_dat;
998 1.39 fvdl } else {
999 1.39 fvdl /*
1000 1.39 fvdl * If the first mbuf has no external data
1001 1.39 fvdl * move the data to the front of the mbuf.
1002 1.39 fvdl */
1003 1.39 fvdl if ((dst = m1->m_dat) != src)
1004 1.63 perry memmove(dst, src, left);
1005 1.39 fvdl dst += left;
1006 1.39 fvdl m1->m_len = left;
1007 1.39 fvdl m2 = m1->m_next;
1008 1.1 cgd }
1009 1.39 fvdl m1->m_flags &= ~M_PKTHDR;
1010 1.39 fvdl *cp2 = m1->m_data = m1->m_dat; /* data is at beginning of buffer */
1011 1.39 fvdl *dposp = mtod(m1, caddr_t) + siz;
1012 1.39 fvdl /*
1013 1.39 fvdl * Loop through mbufs pulling data up into first mbuf until
1014 1.39 fvdl * the first mbuf is full or there is no more data to
1015 1.39 fvdl * pullup.
1016 1.39 fvdl */
1017 1.39 fvdl while ((len = (MLEN - m1->m_len)) != 0 && m2) {
1018 1.39 fvdl if ((len = min(len, m2->m_len)) != 0)
1019 1.63 perry memcpy(dst, m2->m_data, len);
1020 1.39 fvdl m1->m_len += len;
1021 1.39 fvdl dst += len;
1022 1.39 fvdl m2->m_data += len;
1023 1.39 fvdl m2->m_len -= len;
1024 1.39 fvdl m2 = m2->m_next;
1025 1.39 fvdl }
1026 1.39 fvdl if (m1->m_len < siz)
1027 1.39 fvdl return (EBADRPC);
1028 1.1 cgd return (0);
1029 1.1 cgd }
1030 1.1 cgd
1031 1.1 cgd /*
1032 1.1 cgd * Advance the position in the mbuf chain.
1033 1.1 cgd */
1034 1.24 christos int
1035 1.1 cgd nfs_adv(mdp, dposp, offs, left)
1036 1.1 cgd struct mbuf **mdp;
1037 1.1 cgd caddr_t *dposp;
1038 1.1 cgd int offs;
1039 1.1 cgd int left;
1040 1.1 cgd {
1041 1.75 augustss struct mbuf *m;
1042 1.75 augustss int s;
1043 1.1 cgd
1044 1.1 cgd m = *mdp;
1045 1.1 cgd s = left;
1046 1.1 cgd while (s < offs) {
1047 1.1 cgd offs -= s;
1048 1.1 cgd m = m->m_next;
1049 1.1 cgd if (m == NULL)
1050 1.1 cgd return (EBADRPC);
1051 1.1 cgd s = m->m_len;
1052 1.1 cgd }
1053 1.1 cgd *mdp = m;
1054 1.1 cgd *dposp = mtod(m, caddr_t)+offs;
1055 1.1 cgd return (0);
1056 1.1 cgd }
1057 1.1 cgd
1058 1.1 cgd /*
1059 1.1 cgd * Copy a string into mbufs for the hard cases...
1060 1.1 cgd */
1061 1.24 christos int
1062 1.1 cgd nfsm_strtmbuf(mb, bpos, cp, siz)
1063 1.1 cgd struct mbuf **mb;
1064 1.1 cgd char **bpos;
1065 1.33 cgd const char *cp;
1066 1.1 cgd long siz;
1067 1.1 cgd {
1068 1.75 augustss struct mbuf *m1 = NULL, *m2;
1069 1.1 cgd long left, xfer, len, tlen;
1070 1.22 cgd u_int32_t *tl;
1071 1.1 cgd int putsize;
1072 1.1 cgd
1073 1.1 cgd putsize = 1;
1074 1.1 cgd m2 = *mb;
1075 1.12 mycroft left = M_TRAILINGSPACE(m2);
1076 1.1 cgd if (left > 0) {
1077 1.22 cgd tl = ((u_int32_t *)(*bpos));
1078 1.1 cgd *tl++ = txdr_unsigned(siz);
1079 1.1 cgd putsize = 0;
1080 1.1 cgd left -= NFSX_UNSIGNED;
1081 1.1 cgd m2->m_len += NFSX_UNSIGNED;
1082 1.1 cgd if (left > 0) {
1083 1.63 perry memcpy((caddr_t) tl, cp, left);
1084 1.1 cgd siz -= left;
1085 1.1 cgd cp += left;
1086 1.1 cgd m2->m_len += left;
1087 1.1 cgd left = 0;
1088 1.1 cgd }
1089 1.1 cgd }
1090 1.12 mycroft /* Loop around adding mbufs */
1091 1.1 cgd while (siz > 0) {
1092 1.1 cgd MGET(m1, M_WAIT, MT_DATA);
1093 1.1 cgd if (siz > MLEN)
1094 1.1 cgd MCLGET(m1, M_WAIT);
1095 1.1 cgd m1->m_len = NFSMSIZ(m1);
1096 1.1 cgd m2->m_next = m1;
1097 1.1 cgd m2 = m1;
1098 1.22 cgd tl = mtod(m1, u_int32_t *);
1099 1.1 cgd tlen = 0;
1100 1.1 cgd if (putsize) {
1101 1.1 cgd *tl++ = txdr_unsigned(siz);
1102 1.1 cgd m1->m_len -= NFSX_UNSIGNED;
1103 1.1 cgd tlen = NFSX_UNSIGNED;
1104 1.1 cgd putsize = 0;
1105 1.1 cgd }
1106 1.1 cgd if (siz < m1->m_len) {
1107 1.1 cgd len = nfsm_rndup(siz);
1108 1.1 cgd xfer = siz;
1109 1.1 cgd if (xfer < len)
1110 1.1 cgd *(tl+(xfer>>2)) = 0;
1111 1.1 cgd } else {
1112 1.1 cgd xfer = len = m1->m_len;
1113 1.1 cgd }
1114 1.63 perry memcpy((caddr_t) tl, cp, xfer);
1115 1.1 cgd m1->m_len = len+tlen;
1116 1.1 cgd siz -= xfer;
1117 1.1 cgd cp += xfer;
1118 1.1 cgd }
1119 1.1 cgd *mb = m1;
1120 1.1 cgd *bpos = mtod(m1, caddr_t)+m1->m_len;
1121 1.1 cgd return (0);
1122 1.1 cgd }
1123 1.1 cgd
1124 1.49 fvdl /*
1125 1.49 fvdl * Directory caching routines. They work as follows:
1126 1.49 fvdl * - a cache is maintained per VDIR nfsnode.
1127 1.49 fvdl * - for each offset cookie that is exported to userspace, and can
1128 1.49 fvdl * thus be thrown back at us as an offset to VOP_READDIR, store
1129 1.49 fvdl * information in the cache.
1130 1.49 fvdl * - cached are:
1131 1.49 fvdl * - cookie itself
1132 1.49 fvdl * - blocknumber (essentially just a search key in the buffer cache)
1133 1.49 fvdl * - entry number in block.
1134 1.49 fvdl * - offset cookie of block in which this entry is stored
1135 1.49 fvdl * - 32 bit cookie if NFSMNT_XLATECOOKIE is used.
1136 1.49 fvdl * - entries are looked up in a hash table
1137 1.49 fvdl * - also maintained is an LRU list of entries, used to determine
1138 1.49 fvdl * which ones to delete if the cache grows too large.
1139 1.49 fvdl * - if 32 <-> 64 translation mode is requested for a filesystem,
1140 1.49 fvdl * the cache also functions as a translation table
1141 1.49 fvdl * - in the translation case, invalidating the cache does not mean
1142 1.49 fvdl * flushing it, but just marking entries as invalid, except for
1143 1.49 fvdl * the <64bit cookie, 32bitcookie> pair which is still valid, to
1144 1.49 fvdl * still be able to use the cache as a translation table.
1145 1.49 fvdl * - 32 bit cookies are uniquely created by combining the hash table
1146 1.49 fvdl * entry value, and one generation count per hash table entry,
1147 1.49 fvdl * incremented each time an entry is appended to the chain.
1148 1.49 fvdl * - the cache is invalidated each time a direcory is modified
1149 1.49 fvdl * - sanity checks are also done; if an entry in a block turns
1150 1.49 fvdl * out not to have a matching cookie, the cache is invalidated
1151 1.49 fvdl * and a new block starting from the wanted offset is fetched from
1152 1.49 fvdl * the server.
1153 1.49 fvdl * - directory entries as read from the server are extended to contain
1154 1.49 fvdl * the 64bit and, optionally, the 32bit cookies, for sanity checking
1155 1.49 fvdl * the cache and exporting them to userspace through the cookie
1156 1.49 fvdl * argument to VOP_READDIR.
1157 1.49 fvdl */
1158 1.49 fvdl
1159 1.46 fvdl u_long
1160 1.46 fvdl nfs_dirhash(off)
1161 1.46 fvdl off_t off;
1162 1.46 fvdl {
1163 1.46 fvdl int i;
1164 1.46 fvdl char *cp = (char *)&off;
1165 1.46 fvdl u_long sum = 0L;
1166 1.46 fvdl
1167 1.46 fvdl for (i = 0 ; i < sizeof (off); i++)
1168 1.46 fvdl sum += *cp++;
1169 1.46 fvdl
1170 1.46 fvdl return sum;
1171 1.46 fvdl }
1172 1.46 fvdl
1173 1.49 fvdl void
1174 1.49 fvdl nfs_initdircache(vp)
1175 1.49 fvdl struct vnode *vp;
1176 1.49 fvdl {
1177 1.49 fvdl struct nfsnode *np = VTONFS(vp);
1178 1.49 fvdl struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1179 1.49 fvdl
1180 1.49 fvdl np->n_dircachesize = 0;
1181 1.49 fvdl np->n_dblkno = 1;
1182 1.49 fvdl np->n_dircache =
1183 1.53 chs hashinit(NFS_DIRHASHSIZ, M_NFSDIROFF, M_WAITOK, &nfsdirhashmask);
1184 1.49 fvdl TAILQ_INIT(&np->n_dirchain);
1185 1.49 fvdl if (nmp->nm_flag & NFSMNT_XLATECOOKIE) {
1186 1.49 fvdl MALLOC(np->n_dirgens, unsigned *,
1187 1.49 fvdl NFS_DIRHASHSIZ * sizeof (unsigned), M_NFSDIROFF,
1188 1.49 fvdl M_WAITOK);
1189 1.63 perry memset((caddr_t)np->n_dirgens, 0,
1190 1.49 fvdl NFS_DIRHASHSIZ * sizeof (unsigned));
1191 1.49 fvdl }
1192 1.49 fvdl }
1193 1.49 fvdl
1194 1.49 fvdl static struct nfsdircache dzero = {0, 0, {0, 0}, {0, 0}, 0, 0, 0};
1195 1.46 fvdl
1196 1.46 fvdl struct nfsdircache *
1197 1.49 fvdl nfs_searchdircache(vp, off, do32, hashent)
1198 1.46 fvdl struct vnode *vp;
1199 1.46 fvdl off_t off;
1200 1.49 fvdl int do32;
1201 1.49 fvdl int *hashent;
1202 1.49 fvdl {
1203 1.49 fvdl struct nfsdirhashhead *ndhp;
1204 1.49 fvdl struct nfsdircache *ndp = NULL;
1205 1.49 fvdl struct nfsnode *np = VTONFS(vp);
1206 1.49 fvdl unsigned ent;
1207 1.49 fvdl
1208 1.49 fvdl /*
1209 1.49 fvdl * Zero is always a valid cookie.
1210 1.49 fvdl */
1211 1.49 fvdl if (off == 0)
1212 1.49 fvdl return &dzero;
1213 1.49 fvdl
1214 1.49 fvdl /*
1215 1.49 fvdl * We use a 32bit cookie as search key, directly reconstruct
1216 1.49 fvdl * the hashentry. Else use the hashfunction.
1217 1.49 fvdl */
1218 1.49 fvdl if (do32) {
1219 1.49 fvdl ent = (u_int32_t)off >> 24;
1220 1.49 fvdl if (ent >= NFS_DIRHASHSIZ)
1221 1.49 fvdl return NULL;
1222 1.49 fvdl ndhp = &np->n_dircache[ent];
1223 1.49 fvdl } else {
1224 1.49 fvdl ndhp = NFSDIRHASH(np, off);
1225 1.49 fvdl }
1226 1.49 fvdl
1227 1.49 fvdl if (hashent)
1228 1.49 fvdl *hashent = (int)(ndhp - np->n_dircache);
1229 1.49 fvdl if (do32) {
1230 1.49 fvdl for (ndp = ndhp->lh_first; ndp; ndp = ndp->dc_hash.le_next) {
1231 1.49 fvdl if (ndp->dc_cookie32 == (u_int32_t)off) {
1232 1.49 fvdl /*
1233 1.49 fvdl * An invalidated entry will become the
1234 1.49 fvdl * start of a new block fetched from
1235 1.49 fvdl * the server.
1236 1.49 fvdl */
1237 1.49 fvdl if (ndp->dc_blkno == -1) {
1238 1.49 fvdl ndp->dc_blkcookie = ndp->dc_cookie;
1239 1.49 fvdl ndp->dc_blkno = np->n_dblkno++;
1240 1.49 fvdl ndp->dc_entry = 0;
1241 1.49 fvdl }
1242 1.49 fvdl break;
1243 1.49 fvdl }
1244 1.49 fvdl }
1245 1.49 fvdl } else {
1246 1.49 fvdl for (ndp = ndhp->lh_first; ndp; ndp = ndp->dc_hash.le_next)
1247 1.49 fvdl if (ndp->dc_cookie == off)
1248 1.49 fvdl break;
1249 1.49 fvdl }
1250 1.49 fvdl return ndp;
1251 1.49 fvdl }
1252 1.49 fvdl
1253 1.49 fvdl
1254 1.49 fvdl struct nfsdircache *
1255 1.49 fvdl nfs_enterdircache(vp, off, blkoff, en, blkno)
1256 1.49 fvdl struct vnode *vp;
1257 1.49 fvdl off_t off, blkoff;
1258 1.46 fvdl daddr_t blkno;
1259 1.49 fvdl int en;
1260 1.46 fvdl {
1261 1.46 fvdl struct nfsnode *np = VTONFS(vp);
1262 1.46 fvdl struct nfsdirhashhead *ndhp;
1263 1.49 fvdl struct nfsdircache *ndp = NULL, *first;
1264 1.49 fvdl struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1265 1.49 fvdl int hashent, gen, overwrite;
1266 1.46 fvdl
1267 1.49 fvdl if (!np->n_dircache)
1268 1.49 fvdl /*
1269 1.49 fvdl * XXX would like to do this in nfs_nget but vtype
1270 1.49 fvdl * isn't known at that time.
1271 1.49 fvdl */
1272 1.49 fvdl nfs_initdircache(vp);
1273 1.50 fvdl
1274 1.50 fvdl /*
1275 1.50 fvdl * XXX refuse entries for offset 0. amd(8) erroneously sets
1276 1.50 fvdl * cookie 0 for the '.' entry, making this necessary. This
1277 1.50 fvdl * isn't so bad, as 0 is a special case anyway.
1278 1.50 fvdl */
1279 1.50 fvdl if (off == 0)
1280 1.50 fvdl return &dzero;
1281 1.49 fvdl
1282 1.49 fvdl ndp = nfs_searchdircache(vp, off, 0, &hashent);
1283 1.49 fvdl
1284 1.49 fvdl if (ndp && ndp->dc_blkno != -1) {
1285 1.49 fvdl /*
1286 1.49 fvdl * Overwriting an old entry. Check if it's the same.
1287 1.49 fvdl * If so, just return. If not, remove the old entry.
1288 1.49 fvdl */
1289 1.49 fvdl if (ndp->dc_blkcookie == blkoff && ndp->dc_entry == en)
1290 1.49 fvdl return ndp;
1291 1.49 fvdl TAILQ_REMOVE(&np->n_dirchain, ndp, dc_chain);
1292 1.49 fvdl LIST_REMOVE(ndp, dc_hash);
1293 1.49 fvdl FREE(ndp, M_NFSDIROFF);
1294 1.49 fvdl ndp = 0;
1295 1.46 fvdl }
1296 1.46 fvdl
1297 1.49 fvdl ndhp = &np->n_dircache[hashent];
1298 1.46 fvdl
1299 1.49 fvdl if (!ndp) {
1300 1.49 fvdl MALLOC(ndp, struct nfsdircache *, sizeof (*ndp), M_NFSDIROFF,
1301 1.49 fvdl M_WAITOK);
1302 1.49 fvdl overwrite = 0;
1303 1.49 fvdl if (nmp->nm_flag & NFSMNT_XLATECOOKIE) {
1304 1.49 fvdl /*
1305 1.49 fvdl * We're allocating a new entry, so bump the
1306 1.49 fvdl * generation number.
1307 1.49 fvdl */
1308 1.49 fvdl gen = ++np->n_dirgens[hashent];
1309 1.49 fvdl if (gen == 0) {
1310 1.49 fvdl np->n_dirgens[hashent]++;
1311 1.49 fvdl gen++;
1312 1.49 fvdl }
1313 1.49 fvdl ndp->dc_cookie32 = (hashent << 24) | (gen & 0xffffff);
1314 1.49 fvdl }
1315 1.49 fvdl } else
1316 1.49 fvdl overwrite = 1;
1317 1.46 fvdl
1318 1.46 fvdl /*
1319 1.46 fvdl * If the entry number is 0, we are at the start of a new block, so
1320 1.46 fvdl * allocate a new blocknumber.
1321 1.46 fvdl */
1322 1.46 fvdl if (en == 0)
1323 1.46 fvdl ndp->dc_blkno = np->n_dblkno++;
1324 1.46 fvdl else
1325 1.46 fvdl ndp->dc_blkno = blkno;
1326 1.49 fvdl
1327 1.49 fvdl ndp->dc_cookie = off;
1328 1.49 fvdl ndp->dc_blkcookie = blkoff;
1329 1.46 fvdl ndp->dc_entry = en;
1330 1.46 fvdl
1331 1.49 fvdl if (overwrite)
1332 1.49 fvdl return ndp;
1333 1.49 fvdl
1334 1.46 fvdl /*
1335 1.46 fvdl * If the maximum directory cookie cache size has been reached
1336 1.46 fvdl * for this node, take one off the front. The idea is that
1337 1.46 fvdl * directories are typically read front-to-back once, so that
1338 1.46 fvdl * the oldest entries can be thrown away without much performance
1339 1.46 fvdl * loss.
1340 1.46 fvdl */
1341 1.46 fvdl if (np->n_dircachesize == NFS_MAXDIRCACHE) {
1342 1.46 fvdl first = np->n_dirchain.tqh_first;
1343 1.46 fvdl TAILQ_REMOVE(&np->n_dirchain, first, dc_chain);
1344 1.46 fvdl LIST_REMOVE(first, dc_hash);
1345 1.46 fvdl FREE(first, M_NFSDIROFF);
1346 1.46 fvdl } else
1347 1.46 fvdl np->n_dircachesize++;
1348 1.46 fvdl
1349 1.46 fvdl LIST_INSERT_HEAD(ndhp, ndp, dc_hash);
1350 1.46 fvdl TAILQ_INSERT_TAIL(&np->n_dirchain, ndp, dc_chain);
1351 1.46 fvdl return ndp;
1352 1.46 fvdl }
1353 1.46 fvdl
1354 1.46 fvdl void
1355 1.49 fvdl nfs_invaldircache(vp, forcefree)
1356 1.46 fvdl struct vnode *vp;
1357 1.49 fvdl int forcefree;
1358 1.46 fvdl {
1359 1.46 fvdl struct nfsnode *np = VTONFS(vp);
1360 1.46 fvdl struct nfsdircache *ndp = NULL;
1361 1.49 fvdl struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1362 1.46 fvdl
1363 1.46 fvdl #ifdef DIAGNOSTIC
1364 1.46 fvdl if (vp->v_type != VDIR)
1365 1.46 fvdl panic("nfs: invaldircache: not dir");
1366 1.46 fvdl #endif
1367 1.46 fvdl
1368 1.46 fvdl if (!np->n_dircache)
1369 1.46 fvdl return;
1370 1.46 fvdl
1371 1.49 fvdl if (!(nmp->nm_flag & NFSMNT_XLATECOOKIE) || forcefree) {
1372 1.49 fvdl while ((ndp = np->n_dirchain.tqh_first)) {
1373 1.49 fvdl TAILQ_REMOVE(&np->n_dirchain, ndp, dc_chain);
1374 1.49 fvdl LIST_REMOVE(ndp, dc_hash);
1375 1.49 fvdl FREE(ndp, M_NFSDIROFF);
1376 1.49 fvdl }
1377 1.49 fvdl np->n_dircachesize = 0;
1378 1.49 fvdl if (forcefree && np->n_dirgens) {
1379 1.49 fvdl FREE(np->n_dirgens, M_NFSDIROFF);
1380 1.49 fvdl }
1381 1.49 fvdl } else {
1382 1.49 fvdl for (ndp = np->n_dirchain.tqh_first; ndp;
1383 1.49 fvdl ndp = ndp->dc_chain.tqe_next)
1384 1.49 fvdl ndp->dc_blkno = -1;
1385 1.46 fvdl }
1386 1.46 fvdl
1387 1.49 fvdl np->n_dblkno = 1;
1388 1.46 fvdl }
1389 1.46 fvdl
1390 1.1 cgd /*
1391 1.35 thorpej * Called once before VFS init to initialize shared and
1392 1.35 thorpej * server-specific data structures.
1393 1.1 cgd */
1394 1.24 christos void
1395 1.1 cgd nfs_init()
1396 1.1 cgd {
1397 1.1 cgd
1398 1.77 mrg #if !defined(alpha) && !defined(_LP64) && defined(DIAGNOSTIC)
1399 1.25 fvdl /*
1400 1.25 fvdl * Check to see if major data structures haven't bloated.
1401 1.25 fvdl */
1402 1.25 fvdl if (sizeof (struct nfsnode) > NFS_NODEALLOC) {
1403 1.32 christos printf("struct nfsnode bloated (> %dbytes)\n", NFS_NODEALLOC);
1404 1.32 christos printf("Try reducing NFS_SMALLFH\n");
1405 1.25 fvdl }
1406 1.25 fvdl if (sizeof (struct nfssvc_sock) > NFS_SVCALLOC) {
1407 1.32 christos printf("struct nfssvc_sock bloated (> %dbytes)\n",NFS_SVCALLOC);
1408 1.32 christos printf("Try reducing NFS_UIDHASHSIZ\n");
1409 1.25 fvdl }
1410 1.25 fvdl if (sizeof (struct nfsuid) > NFS_UIDALLOC) {
1411 1.32 christos printf("struct nfsuid bloated (> %dbytes)\n",NFS_UIDALLOC);
1412 1.32 christos printf("Try unionizing the nu_nickname and nu_flag fields\n");
1413 1.25 fvdl }
1414 1.25 fvdl #endif
1415 1.25 fvdl
1416 1.12 mycroft nfsrtt.pos = 0;
1417 1.1 cgd rpc_vers = txdr_unsigned(RPC_VER2);
1418 1.1 cgd rpc_call = txdr_unsigned(RPC_CALL);
1419 1.1 cgd rpc_reply = txdr_unsigned(RPC_REPLY);
1420 1.1 cgd rpc_msgdenied = txdr_unsigned(RPC_MSGDENIED);
1421 1.1 cgd rpc_msgaccepted = txdr_unsigned(RPC_MSGACCEPTED);
1422 1.1 cgd rpc_mismatch = txdr_unsigned(RPC_MISMATCH);
1423 1.12 mycroft rpc_autherr = txdr_unsigned(RPC_AUTHERR);
1424 1.1 cgd rpc_auth_unix = txdr_unsigned(RPCAUTH_UNIX);
1425 1.25 fvdl rpc_auth_kerb = txdr_unsigned(RPCAUTH_KERB4);
1426 1.1 cgd nfs_prog = txdr_unsigned(NFS_PROG);
1427 1.25 fvdl nqnfs_prog = txdr_unsigned(NQNFS_PROG);
1428 1.1 cgd nfs_true = txdr_unsigned(TRUE);
1429 1.1 cgd nfs_false = txdr_unsigned(FALSE);
1430 1.12 mycroft nfs_xdrneg1 = txdr_unsigned(-1);
1431 1.25 fvdl nfs_ticks = (hz * NFS_TICKINTVL + 500) / 1000;
1432 1.25 fvdl if (nfs_ticks < 1)
1433 1.25 fvdl nfs_ticks = 1;
1434 1.2 glass #ifdef NFSSERVER
1435 1.12 mycroft nfsrv_init(0); /* Init server data structures */
1436 1.1 cgd nfsrv_initcache(); /* Init the server request cache */
1437 1.36 fvdl #endif /* NFSSERVER */
1438 1.12 mycroft
1439 1.12 mycroft /*
1440 1.37 fvdl * Initialize the nqnfs data structures.
1441 1.12 mycroft */
1442 1.12 mycroft if (nqnfsstarttime == 0) {
1443 1.12 mycroft nqnfsstarttime = boottime.tv_sec + nqsrv_maxlease
1444 1.12 mycroft + nqsrv_clockskew + nqsrv_writeslack;
1445 1.12 mycroft NQLOADNOVRAM(nqnfsstarttime);
1446 1.18 mycroft CIRCLEQ_INIT(&nqtimerhead);
1447 1.53 chs nqfhhashtbl = hashinit(NQLCHSZ, M_NQLEASE, M_WAITOK, &nqfhhash);
1448 1.12 mycroft }
1449 1.12 mycroft
1450 1.1 cgd /*
1451 1.1 cgd * Initialize reply list and start timer
1452 1.1 cgd */
1453 1.16 mycroft TAILQ_INIT(&nfs_reqq);
1454 1.24 christos nfs_timer(NULL);
1455 1.1 cgd }
1456 1.1 cgd
1457 1.38 thorpej #ifdef NFS
1458 1.35 thorpej /*
1459 1.35 thorpej * Called once at VFS init to initialize client-specific data structures.
1460 1.35 thorpej */
1461 1.35 thorpej void
1462 1.35 thorpej nfs_vfs_init()
1463 1.35 thorpej {
1464 1.75 augustss int i;
1465 1.35 thorpej
1466 1.35 thorpej /* Ensure async daemons disabled */
1467 1.35 thorpej for (i = 0; i < NFS_MAXASYNCDAEMON; i++) {
1468 1.35 thorpej nfs_iodwant[i] = (struct proc *)0;
1469 1.35 thorpej nfs_iodmount[i] = (struct nfsmount *)0;
1470 1.35 thorpej }
1471 1.35 thorpej nfs_nhinit(); /* Init the nfsnode table */
1472 1.73 jdolecek }
1473 1.73 jdolecek
1474 1.73 jdolecek void
1475 1.73 jdolecek nfs_vfs_done()
1476 1.73 jdolecek {
1477 1.73 jdolecek nfs_nhdone();
1478 1.35 thorpej }
1479 1.35 thorpej
1480 1.1 cgd /*
1481 1.12 mycroft * Attribute cache routines.
1482 1.12 mycroft * nfs_loadattrcache() - loads or updates the cache contents from attributes
1483 1.12 mycroft * that are on the mbuf list
1484 1.12 mycroft * nfs_getattrcache() - returns valid attributes if found in cache, returns
1485 1.12 mycroft * error otherwise
1486 1.1 cgd */
1487 1.12 mycroft
1488 1.12 mycroft /*
1489 1.12 mycroft * Load the attribute cache (that lives in the nfsnode entry) with
1490 1.12 mycroft * the values on the mbuf list and
1491 1.12 mycroft * Iff vap not NULL
1492 1.12 mycroft * copy the attributes to *vaper
1493 1.12 mycroft */
1494 1.24 christos int
1495 1.45 fvdl nfsm_loadattrcache(vpp, mdp, dposp, vaper)
1496 1.12 mycroft struct vnode **vpp;
1497 1.12 mycroft struct mbuf **mdp;
1498 1.12 mycroft caddr_t *dposp;
1499 1.12 mycroft struct vattr *vaper;
1500 1.1 cgd {
1501 1.75 augustss int32_t t1;
1502 1.25 fvdl caddr_t cp2;
1503 1.25 fvdl int error = 0;
1504 1.12 mycroft struct mbuf *md;
1505 1.45 fvdl int v3 = NFS_ISV3(*vpp);
1506 1.1 cgd
1507 1.12 mycroft md = *mdp;
1508 1.25 fvdl t1 = (mtod(md, caddr_t) + md->m_len) - *dposp;
1509 1.25 fvdl error = nfsm_disct(mdp, dposp, NFSX_FATTR(v3), t1, &cp2);
1510 1.24 christos if (error)
1511 1.12 mycroft return (error);
1512 1.45 fvdl return nfs_loadattrcache(vpp, (struct nfs_fattr *)cp2, vaper);
1513 1.45 fvdl }
1514 1.45 fvdl
1515 1.45 fvdl int
1516 1.45 fvdl nfs_loadattrcache(vpp, fp, vaper)
1517 1.45 fvdl struct vnode **vpp;
1518 1.45 fvdl struct nfs_fattr *fp;
1519 1.45 fvdl struct vattr *vaper;
1520 1.45 fvdl {
1521 1.75 augustss struct vnode *vp = *vpp;
1522 1.75 augustss struct vattr *vap;
1523 1.45 fvdl int v3 = NFS_ISV3(vp);
1524 1.45 fvdl enum vtype vtyp;
1525 1.45 fvdl u_short vmode;
1526 1.45 fvdl struct timespec mtime;
1527 1.45 fvdl struct vnode *nvp;
1528 1.45 fvdl int32_t rdev;
1529 1.75 augustss struct nfsnode *np;
1530 1.45 fvdl extern int (**spec_nfsv2nodeop_p) __P((void *));
1531 1.45 fvdl
1532 1.25 fvdl if (v3) {
1533 1.25 fvdl vtyp = nfsv3tov_type(fp->fa_type);
1534 1.25 fvdl vmode = fxdr_unsigned(u_short, fp->fa_mode);
1535 1.71 is rdev = makedev(fxdr_unsigned(u_int32_t, fp->fa3_rdev.specdata1),
1536 1.71 is fxdr_unsigned(u_int32_t, fp->fa3_rdev.specdata2));
1537 1.25 fvdl fxdr_nfsv3time(&fp->fa3_mtime, &mtime);
1538 1.12 mycroft } else {
1539 1.25 fvdl vtyp = nfsv2tov_type(fp->fa_type);
1540 1.25 fvdl vmode = fxdr_unsigned(u_short, fp->fa_mode);
1541 1.25 fvdl if (vtyp == VNON || vtyp == VREG)
1542 1.25 fvdl vtyp = IFTOVT(vmode);
1543 1.25 fvdl rdev = fxdr_unsigned(int32_t, fp->fa2_rdev);
1544 1.25 fvdl fxdr_nfsv2time(&fp->fa2_mtime, &mtime);
1545 1.25 fvdl
1546 1.25 fvdl /*
1547 1.25 fvdl * Really ugly NFSv2 kludge.
1548 1.25 fvdl */
1549 1.25 fvdl if (vtyp == VCHR && rdev == 0xffffffff)
1550 1.25 fvdl vtyp = VFIFO;
1551 1.12 mycroft }
1552 1.25 fvdl
1553 1.12 mycroft /*
1554 1.12 mycroft * If v_type == VNON it is a new node, so fill in the v_type,
1555 1.12 mycroft * n_mtime fields. Check to see if it represents a special
1556 1.12 mycroft * device, and if so, check for a possible alias. Once the
1557 1.12 mycroft * correct vnode has been obtained, fill in the rest of the
1558 1.12 mycroft * information.
1559 1.12 mycroft */
1560 1.12 mycroft np = VTONFS(vp);
1561 1.28 fvdl if (vp->v_type != vtyp) {
1562 1.25 fvdl vp->v_type = vtyp;
1563 1.12 mycroft if (vp->v_type == VFIFO) {
1564 1.24 christos extern int (**fifo_nfsv2nodeop_p) __P((void *));
1565 1.12 mycroft vp->v_op = fifo_nfsv2nodeop_p;
1566 1.12 mycroft }
1567 1.12 mycroft if (vp->v_type == VCHR || vp->v_type == VBLK) {
1568 1.12 mycroft vp->v_op = spec_nfsv2nodeop_p;
1569 1.24 christos nvp = checkalias(vp, (dev_t)rdev, vp->v_mount);
1570 1.24 christos if (nvp) {
1571 1.12 mycroft /*
1572 1.12 mycroft * Discard unneeded vnode, but save its nfsnode.
1573 1.25 fvdl * Since the nfsnode does not have a lock, its
1574 1.25 fvdl * vnode lock has to be carried over.
1575 1.12 mycroft */
1576 1.12 mycroft nvp->v_data = vp->v_data;
1577 1.12 mycroft vp->v_data = NULL;
1578 1.12 mycroft vp->v_op = spec_vnodeop_p;
1579 1.70 wrstuden vput(vp);
1580 1.12 mycroft vgone(vp);
1581 1.70 wrstuden /*
1582 1.70 wrstuden * XXX When nfs starts locking, we need to
1583 1.70 wrstuden * lock the new node here.
1584 1.70 wrstuden */
1585 1.12 mycroft /*
1586 1.12 mycroft * Reinitialize aliased node.
1587 1.12 mycroft */
1588 1.12 mycroft np->n_vnode = nvp;
1589 1.12 mycroft *vpp = vp = nvp;
1590 1.12 mycroft }
1591 1.12 mycroft }
1592 1.23 jtc np->n_mtime = mtime.tv_sec;
1593 1.12 mycroft }
1594 1.49 fvdl vap = np->n_vattr;
1595 1.12 mycroft vap->va_type = vtyp;
1596 1.42 mycroft vap->va_mode = vmode & ALLPERMS;
1597 1.12 mycroft vap->va_rdev = (dev_t)rdev;
1598 1.12 mycroft vap->va_mtime = mtime;
1599 1.12 mycroft vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
1600 1.72 fvdl switch (vtyp) {
1601 1.72 fvdl case VDIR:
1602 1.72 fvdl vap->va_blocksize = NFS_DIRFRAGSIZ;
1603 1.72 fvdl break;
1604 1.72 fvdl case VBLK:
1605 1.72 fvdl vap->va_blocksize = BLKDEV_IOSIZE;
1606 1.72 fvdl break;
1607 1.72 fvdl case VCHR:
1608 1.72 fvdl vap->va_blocksize = MAXBSIZE;
1609 1.72 fvdl break;
1610 1.72 fvdl default:
1611 1.72 fvdl vap->va_blocksize = v3 ? vp->v_mount->mnt_stat.f_iosize :
1612 1.72 fvdl fxdr_unsigned(int32_t, fp->fa2_blocksize);
1613 1.72 fvdl break;
1614 1.72 fvdl }
1615 1.25 fvdl if (v3) {
1616 1.25 fvdl vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
1617 1.25 fvdl vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
1618 1.25 fvdl vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
1619 1.66 fair vap->va_size = fxdr_hyper(&fp->fa3_size);
1620 1.66 fair vap->va_bytes = fxdr_hyper(&fp->fa3_used);
1621 1.25 fvdl vap->va_fileid = fxdr_unsigned(int32_t,
1622 1.25 fvdl fp->fa3_fileid.nfsuquad[1]);
1623 1.25 fvdl fxdr_nfsv3time(&fp->fa3_atime, &vap->va_atime);
1624 1.25 fvdl fxdr_nfsv3time(&fp->fa3_ctime, &vap->va_ctime);
1625 1.25 fvdl vap->va_flags = 0;
1626 1.25 fvdl vap->va_filerev = 0;
1627 1.12 mycroft } else {
1628 1.25 fvdl vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
1629 1.25 fvdl vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
1630 1.25 fvdl vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
1631 1.25 fvdl vap->va_size = fxdr_unsigned(u_int32_t, fp->fa2_size);
1632 1.25 fvdl vap->va_bytes = fxdr_unsigned(int32_t, fp->fa2_blocks)
1633 1.25 fvdl * NFS_FABLKSIZE;
1634 1.25 fvdl vap->va_fileid = fxdr_unsigned(int32_t, fp->fa2_fileid);
1635 1.25 fvdl fxdr_nfsv2time(&fp->fa2_atime, &vap->va_atime);
1636 1.12 mycroft vap->va_flags = 0;
1637 1.25 fvdl vap->va_ctime.tv_sec = fxdr_unsigned(u_int32_t,
1638 1.25 fvdl fp->fa2_ctime.nfsv2_sec);
1639 1.25 fvdl vap->va_ctime.tv_nsec = 0;
1640 1.25 fvdl vap->va_gen = fxdr_unsigned(u_int32_t,fp->fa2_ctime.nfsv2_usec);
1641 1.12 mycroft vap->va_filerev = 0;
1642 1.12 mycroft }
1643 1.12 mycroft if (vap->va_size != np->n_size) {
1644 1.12 mycroft if (vap->va_type == VREG) {
1645 1.12 mycroft if (np->n_flag & NMODIFIED) {
1646 1.12 mycroft if (vap->va_size < np->n_size)
1647 1.12 mycroft vap->va_size = np->n_size;
1648 1.12 mycroft else
1649 1.12 mycroft np->n_size = vap->va_size;
1650 1.12 mycroft } else
1651 1.12 mycroft np->n_size = vap->va_size;
1652 1.51 mrg uvm_vnp_setsize(vp, np->n_size);
1653 1.12 mycroft } else
1654 1.12 mycroft np->n_size = vap->va_size;
1655 1.12 mycroft }
1656 1.12 mycroft np->n_attrstamp = time.tv_sec;
1657 1.12 mycroft if (vaper != NULL) {
1658 1.63 perry memcpy((caddr_t)vaper, (caddr_t)vap, sizeof(*vap));
1659 1.12 mycroft if (np->n_flag & NCHG) {
1660 1.25 fvdl if (np->n_flag & NACC)
1661 1.25 fvdl vaper->va_atime = np->n_atim;
1662 1.25 fvdl if (np->n_flag & NUPD)
1663 1.25 fvdl vaper->va_mtime = np->n_mtim;
1664 1.12 mycroft }
1665 1.12 mycroft }
1666 1.12 mycroft return (0);
1667 1.12 mycroft }
1668 1.12 mycroft
1669 1.12 mycroft /*
1670 1.12 mycroft * Check the time stamp
1671 1.12 mycroft * If the cache is valid, copy contents to *vap and return 0
1672 1.12 mycroft * otherwise return an error
1673 1.12 mycroft */
1674 1.24 christos int
1675 1.12 mycroft nfs_getattrcache(vp, vaper)
1676 1.75 augustss struct vnode *vp;
1677 1.12 mycroft struct vattr *vaper;
1678 1.12 mycroft {
1679 1.75 augustss struct nfsnode *np = VTONFS(vp);
1680 1.75 augustss struct vattr *vap;
1681 1.12 mycroft
1682 1.25 fvdl if ((time.tv_sec - np->n_attrstamp) >= NFS_ATTRTIMEO(np)) {
1683 1.12 mycroft nfsstats.attrcache_misses++;
1684 1.12 mycroft return (ENOENT);
1685 1.12 mycroft }
1686 1.12 mycroft nfsstats.attrcache_hits++;
1687 1.49 fvdl vap = np->n_vattr;
1688 1.12 mycroft if (vap->va_size != np->n_size) {
1689 1.12 mycroft if (vap->va_type == VREG) {
1690 1.12 mycroft if (np->n_flag & NMODIFIED) {
1691 1.12 mycroft if (vap->va_size < np->n_size)
1692 1.12 mycroft vap->va_size = np->n_size;
1693 1.12 mycroft else
1694 1.12 mycroft np->n_size = vap->va_size;
1695 1.12 mycroft } else
1696 1.12 mycroft np->n_size = vap->va_size;
1697 1.51 mrg uvm_vnp_setsize(vp, np->n_size);
1698 1.12 mycroft } else
1699 1.12 mycroft np->n_size = vap->va_size;
1700 1.12 mycroft }
1701 1.63 perry memcpy((caddr_t)vaper, (caddr_t)vap, sizeof(struct vattr));
1702 1.12 mycroft if (np->n_flag & NCHG) {
1703 1.25 fvdl if (np->n_flag & NACC)
1704 1.25 fvdl vaper->va_atime = np->n_atim;
1705 1.25 fvdl if (np->n_flag & NUPD)
1706 1.25 fvdl vaper->va_mtime = np->n_mtim;
1707 1.1 cgd }
1708 1.12 mycroft return (0);
1709 1.1 cgd }
1710 1.48 fvdl
1711 1.48 fvdl /*
1712 1.48 fvdl * Heuristic to see if the server XDR encodes directory cookies or not.
1713 1.48 fvdl * it is not supposed to, but a lot of servers may do this. Also, since
1714 1.48 fvdl * most/all servers will implement V2 as well, it is expected that they
1715 1.48 fvdl * may return just 32 bits worth of cookie information, so we need to
1716 1.48 fvdl * find out in which 32 bits this information is available. We do this
1717 1.48 fvdl * to avoid trouble with emulated binaries that can't handle 64 bit
1718 1.48 fvdl * directory offsets.
1719 1.48 fvdl */
1720 1.48 fvdl
1721 1.48 fvdl void
1722 1.48 fvdl nfs_cookieheuristic(vp, flagp, p, cred)
1723 1.48 fvdl struct vnode *vp;
1724 1.48 fvdl int *flagp;
1725 1.48 fvdl struct proc *p;
1726 1.48 fvdl struct ucred *cred;
1727 1.48 fvdl {
1728 1.48 fvdl struct uio auio;
1729 1.48 fvdl struct iovec aiov;
1730 1.48 fvdl caddr_t buf, cp;
1731 1.48 fvdl struct dirent *dp;
1732 1.57 fvdl off_t *cookies = NULL, *cop;
1733 1.48 fvdl int error, eof, nc, len;
1734 1.48 fvdl
1735 1.48 fvdl MALLOC(buf, caddr_t, NFS_DIRFRAGSIZ, M_TEMP, M_WAITOK);
1736 1.48 fvdl
1737 1.48 fvdl aiov.iov_base = buf;
1738 1.48 fvdl aiov.iov_len = NFS_DIRFRAGSIZ;
1739 1.48 fvdl auio.uio_iov = &aiov;
1740 1.48 fvdl auio.uio_iovcnt = 1;
1741 1.48 fvdl auio.uio_rw = UIO_READ;
1742 1.48 fvdl auio.uio_segflg = UIO_SYSSPACE;
1743 1.48 fvdl auio.uio_procp = p;
1744 1.48 fvdl auio.uio_resid = NFS_DIRFRAGSIZ;
1745 1.48 fvdl auio.uio_offset = 0;
1746 1.48 fvdl
1747 1.56 fvdl error = VOP_READDIR(vp, &auio, cred, &eof, &cookies, &nc);
1748 1.48 fvdl
1749 1.48 fvdl len = NFS_DIRFRAGSIZ - auio.uio_resid;
1750 1.48 fvdl if (error || len == 0) {
1751 1.48 fvdl FREE(buf, M_TEMP);
1752 1.57 fvdl if (cookies)
1753 1.80 thorpej free(cookies, M_TEMP);
1754 1.48 fvdl return;
1755 1.48 fvdl }
1756 1.48 fvdl
1757 1.48 fvdl /*
1758 1.48 fvdl * Find the first valid entry and look at its offset cookie.
1759 1.48 fvdl */
1760 1.48 fvdl
1761 1.48 fvdl cp = buf;
1762 1.48 fvdl for (cop = cookies; len > 0; len -= dp->d_reclen) {
1763 1.48 fvdl dp = (struct dirent *)cp;
1764 1.48 fvdl if (dp->d_fileno != 0 && len >= dp->d_reclen) {
1765 1.48 fvdl if ((*cop >> 32) != 0 && (*cop & 0xffffffffLL) == 0) {
1766 1.48 fvdl *flagp |= NFSMNT_SWAPCOOKIE;
1767 1.49 fvdl nfs_invaldircache(vp, 0);
1768 1.48 fvdl nfs_vinvalbuf(vp, 0, cred, p, 1);
1769 1.48 fvdl }
1770 1.48 fvdl break;
1771 1.48 fvdl }
1772 1.48 fvdl cop++;
1773 1.48 fvdl cp += dp->d_reclen;
1774 1.48 fvdl }
1775 1.48 fvdl
1776 1.48 fvdl FREE(buf, M_TEMP);
1777 1.80 thorpej free(cookies, M_TEMP);
1778 1.48 fvdl }
1779 1.38 thorpej #endif /* NFS */
1780 1.1 cgd
1781 1.1 cgd /*
1782 1.43 fvdl * Set up nameidata for a lookup() call and do it.
1783 1.43 fvdl *
1784 1.43 fvdl * If pubflag is set, this call is done for a lookup operation on the
1785 1.43 fvdl * public filehandle. In that case we allow crossing mountpoints and
1786 1.43 fvdl * absolute pathnames. However, the caller is expected to check that
1787 1.43 fvdl * the lookup result is within the public fs, and deny access if
1788 1.43 fvdl * it is not.
1789 1.1 cgd */
1790 1.24 christos int
1791 1.43 fvdl nfs_namei(ndp, fhp, len, slp, nam, mdp, dposp, retdirp, p, kerbflag, pubflag)
1792 1.75 augustss struct nameidata *ndp;
1793 1.1 cgd fhandle_t *fhp;
1794 1.1 cgd int len;
1795 1.12 mycroft struct nfssvc_sock *slp;
1796 1.12 mycroft struct mbuf *nam;
1797 1.1 cgd struct mbuf **mdp;
1798 1.1 cgd caddr_t *dposp;
1799 1.25 fvdl struct vnode **retdirp;
1800 1.1 cgd struct proc *p;
1801 1.43 fvdl int kerbflag, pubflag;
1802 1.1 cgd {
1803 1.75 augustss int i, rem;
1804 1.75 augustss struct mbuf *md;
1805 1.75 augustss char *fromcp, *tocp, *cp;
1806 1.43 fvdl struct iovec aiov;
1807 1.43 fvdl struct uio auio;
1808 1.1 cgd struct vnode *dp;
1809 1.43 fvdl int error, rdonly, linklen;
1810 1.12 mycroft struct componentname *cnp = &ndp->ni_cnd;
1811 1.1 cgd
1812 1.25 fvdl *retdirp = (struct vnode *)0;
1813 1.12 mycroft MALLOC(cnp->cn_pnbuf, char *, len + 1, M_NAMEI, M_WAITOK);
1814 1.1 cgd /*
1815 1.1 cgd * Copy the name from the mbuf list to ndp->ni_pnbuf
1816 1.1 cgd * and set the various ndp fields appropriately.
1817 1.1 cgd */
1818 1.1 cgd fromcp = *dposp;
1819 1.12 mycroft tocp = cnp->cn_pnbuf;
1820 1.1 cgd md = *mdp;
1821 1.1 cgd rem = mtod(md, caddr_t) + md->m_len - fromcp;
1822 1.1 cgd for (i = 0; i < len; i++) {
1823 1.1 cgd while (rem == 0) {
1824 1.1 cgd md = md->m_next;
1825 1.1 cgd if (md == NULL) {
1826 1.1 cgd error = EBADRPC;
1827 1.1 cgd goto out;
1828 1.1 cgd }
1829 1.1 cgd fromcp = mtod(md, caddr_t);
1830 1.1 cgd rem = md->m_len;
1831 1.1 cgd }
1832 1.43 fvdl if (*fromcp == '\0' || (!pubflag && *fromcp == '/')) {
1833 1.25 fvdl error = EACCES;
1834 1.1 cgd goto out;
1835 1.1 cgd }
1836 1.1 cgd *tocp++ = *fromcp++;
1837 1.1 cgd rem--;
1838 1.1 cgd }
1839 1.1 cgd *tocp = '\0';
1840 1.1 cgd *mdp = md;
1841 1.1 cgd *dposp = fromcp;
1842 1.1 cgd len = nfsm_rndup(len)-len;
1843 1.1 cgd if (len > 0) {
1844 1.1 cgd if (rem >= len)
1845 1.1 cgd *dposp += len;
1846 1.24 christos else if ((error = nfs_adv(mdp, dposp, len, rem)) != 0)
1847 1.1 cgd goto out;
1848 1.1 cgd }
1849 1.43 fvdl
1850 1.1 cgd /*
1851 1.1 cgd * Extract and set starting directory.
1852 1.1 cgd */
1853 1.24 christos error = nfsrv_fhtovp(fhp, FALSE, &dp, ndp->ni_cnd.cn_cred, slp,
1854 1.43 fvdl nam, &rdonly, kerbflag, pubflag);
1855 1.24 christos if (error)
1856 1.1 cgd goto out;
1857 1.1 cgd if (dp->v_type != VDIR) {
1858 1.1 cgd vrele(dp);
1859 1.1 cgd error = ENOTDIR;
1860 1.1 cgd goto out;
1861 1.1 cgd }
1862 1.43 fvdl
1863 1.43 fvdl if (rdonly)
1864 1.43 fvdl cnp->cn_flags |= RDONLY;
1865 1.43 fvdl
1866 1.43 fvdl *retdirp = dp;
1867 1.43 fvdl
1868 1.43 fvdl if (pubflag) {
1869 1.43 fvdl /*
1870 1.43 fvdl * Oh joy. For WebNFS, handle those pesky '%' escapes,
1871 1.43 fvdl * and the 'native path' indicator.
1872 1.43 fvdl */
1873 1.43 fvdl MALLOC(cp, char *, MAXPATHLEN, M_NAMEI, M_WAITOK);
1874 1.43 fvdl fromcp = cnp->cn_pnbuf;
1875 1.43 fvdl tocp = cp;
1876 1.43 fvdl if ((unsigned char)*fromcp >= WEBNFS_SPECCHAR_START) {
1877 1.43 fvdl switch ((unsigned char)*fromcp) {
1878 1.43 fvdl case WEBNFS_NATIVE_CHAR:
1879 1.43 fvdl /*
1880 1.43 fvdl * 'Native' path for us is the same
1881 1.43 fvdl * as a path according to the NFS spec,
1882 1.43 fvdl * just skip the escape char.
1883 1.43 fvdl */
1884 1.43 fvdl fromcp++;
1885 1.43 fvdl break;
1886 1.43 fvdl /*
1887 1.43 fvdl * More may be added in the future, range 0x80-0xff
1888 1.43 fvdl */
1889 1.43 fvdl default:
1890 1.43 fvdl error = EIO;
1891 1.43 fvdl FREE(cp, M_NAMEI);
1892 1.43 fvdl goto out;
1893 1.43 fvdl }
1894 1.43 fvdl }
1895 1.43 fvdl /*
1896 1.43 fvdl * Translate the '%' escapes, URL-style.
1897 1.43 fvdl */
1898 1.43 fvdl while (*fromcp != '\0') {
1899 1.43 fvdl if (*fromcp == WEBNFS_ESC_CHAR) {
1900 1.43 fvdl if (fromcp[1] != '\0' && fromcp[2] != '\0') {
1901 1.43 fvdl fromcp++;
1902 1.43 fvdl *tocp++ = HEXSTRTOI(fromcp);
1903 1.43 fvdl fromcp += 2;
1904 1.43 fvdl continue;
1905 1.43 fvdl } else {
1906 1.43 fvdl error = ENOENT;
1907 1.43 fvdl FREE(cp, M_NAMEI);
1908 1.43 fvdl goto out;
1909 1.43 fvdl }
1910 1.43 fvdl } else
1911 1.43 fvdl *tocp++ = *fromcp++;
1912 1.43 fvdl }
1913 1.43 fvdl *tocp = '\0';
1914 1.43 fvdl FREE(cnp->cn_pnbuf, M_NAMEI);
1915 1.43 fvdl cnp->cn_pnbuf = cp;
1916 1.43 fvdl }
1917 1.43 fvdl
1918 1.43 fvdl ndp->ni_pathlen = (tocp - cnp->cn_pnbuf) + 1;
1919 1.43 fvdl ndp->ni_segflg = UIO_SYSSPACE;
1920 1.43 fvdl
1921 1.43 fvdl if (pubflag) {
1922 1.43 fvdl ndp->ni_rootdir = rootvnode;
1923 1.43 fvdl ndp->ni_loopcnt = 0;
1924 1.43 fvdl if (cnp->cn_pnbuf[0] == '/')
1925 1.43 fvdl dp = rootvnode;
1926 1.43 fvdl } else {
1927 1.43 fvdl cnp->cn_flags |= NOCROSSMOUNT;
1928 1.43 fvdl }
1929 1.43 fvdl
1930 1.43 fvdl cnp->cn_proc = p;
1931 1.25 fvdl VREF(dp);
1932 1.43 fvdl
1933 1.43 fvdl for (;;) {
1934 1.43 fvdl cnp->cn_nameptr = cnp->cn_pnbuf;
1935 1.1 cgd ndp->ni_startdir = dp;
1936 1.1 cgd /*
1937 1.1 cgd * And call lookup() to do the real work
1938 1.1 cgd */
1939 1.25 fvdl error = lookup(ndp);
1940 1.25 fvdl if (error)
1941 1.43 fvdl break;
1942 1.1 cgd /*
1943 1.1 cgd * Check for encountering a symbolic link
1944 1.1 cgd */
1945 1.43 fvdl if ((cnp->cn_flags & ISSYMLINK) == 0) {
1946 1.43 fvdl if (cnp->cn_flags & (SAVENAME | SAVESTART)) {
1947 1.43 fvdl cnp->cn_flags |= HASBUF;
1948 1.43 fvdl return (0);
1949 1.43 fvdl }
1950 1.43 fvdl break;
1951 1.43 fvdl } else {
1952 1.12 mycroft if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
1953 1.56 fvdl VOP_UNLOCK(ndp->ni_dvp, 0);
1954 1.43 fvdl if (!pubflag) {
1955 1.43 fvdl vrele(ndp->ni_dvp);
1956 1.43 fvdl vput(ndp->ni_vp);
1957 1.43 fvdl ndp->ni_vp = NULL;
1958 1.43 fvdl error = EINVAL;
1959 1.43 fvdl break;
1960 1.43 fvdl }
1961 1.43 fvdl
1962 1.43 fvdl if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
1963 1.43 fvdl error = ELOOP;
1964 1.43 fvdl break;
1965 1.43 fvdl }
1966 1.43 fvdl if (ndp->ni_pathlen > 1)
1967 1.43 fvdl MALLOC(cp, char *, MAXPATHLEN, M_NAMEI, M_WAITOK);
1968 1.1 cgd else
1969 1.43 fvdl cp = cnp->cn_pnbuf;
1970 1.43 fvdl aiov.iov_base = cp;
1971 1.43 fvdl aiov.iov_len = MAXPATHLEN;
1972 1.43 fvdl auio.uio_iov = &aiov;
1973 1.43 fvdl auio.uio_iovcnt = 1;
1974 1.43 fvdl auio.uio_offset = 0;
1975 1.43 fvdl auio.uio_rw = UIO_READ;
1976 1.43 fvdl auio.uio_segflg = UIO_SYSSPACE;
1977 1.43 fvdl auio.uio_procp = (struct proc *)0;
1978 1.43 fvdl auio.uio_resid = MAXPATHLEN;
1979 1.43 fvdl error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
1980 1.43 fvdl if (error) {
1981 1.43 fvdl badlink:
1982 1.43 fvdl if (ndp->ni_pathlen > 1)
1983 1.43 fvdl FREE(cp, M_NAMEI);
1984 1.43 fvdl break;
1985 1.43 fvdl }
1986 1.43 fvdl linklen = MAXPATHLEN - auio.uio_resid;
1987 1.43 fvdl if (linklen == 0) {
1988 1.43 fvdl error = ENOENT;
1989 1.43 fvdl goto badlink;
1990 1.43 fvdl }
1991 1.43 fvdl if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
1992 1.43 fvdl error = ENAMETOOLONG;
1993 1.43 fvdl goto badlink;
1994 1.43 fvdl }
1995 1.43 fvdl if (ndp->ni_pathlen > 1) {
1996 1.63 perry memcpy(cp + linklen, ndp->ni_next, ndp->ni_pathlen);
1997 1.43 fvdl FREE(cnp->cn_pnbuf, M_NAMEI);
1998 1.43 fvdl cnp->cn_pnbuf = cp;
1999 1.43 fvdl } else
2000 1.43 fvdl cnp->cn_pnbuf[linklen] = '\0';
2001 1.43 fvdl ndp->ni_pathlen += linklen;
2002 1.1 cgd vput(ndp->ni_vp);
2003 1.43 fvdl dp = ndp->ni_dvp;
2004 1.43 fvdl /*
2005 1.43 fvdl * Check if root directory should replace current directory.
2006 1.43 fvdl */
2007 1.43 fvdl if (cnp->cn_pnbuf[0] == '/') {
2008 1.43 fvdl vrele(dp);
2009 1.43 fvdl dp = ndp->ni_rootdir;
2010 1.43 fvdl VREF(dp);
2011 1.43 fvdl }
2012 1.1 cgd }
2013 1.43 fvdl }
2014 1.1 cgd out:
2015 1.12 mycroft FREE(cnp->cn_pnbuf, M_NAMEI);
2016 1.1 cgd return (error);
2017 1.1 cgd }
2018 1.1 cgd
2019 1.1 cgd /*
2020 1.1 cgd * A fiddled version of m_adj() that ensures null fill to a long
2021 1.1 cgd * boundary and only trims off the back end
2022 1.1 cgd */
2023 1.12 mycroft void
2024 1.1 cgd nfsm_adj(mp, len, nul)
2025 1.1 cgd struct mbuf *mp;
2026 1.75 augustss int len;
2027 1.1 cgd int nul;
2028 1.1 cgd {
2029 1.75 augustss struct mbuf *m;
2030 1.75 augustss int count, i;
2031 1.75 augustss char *cp;
2032 1.1 cgd
2033 1.1 cgd /*
2034 1.1 cgd * Trim from tail. Scan the mbuf chain,
2035 1.1 cgd * calculating its length and finding the last mbuf.
2036 1.1 cgd * If the adjustment only affects this mbuf, then just
2037 1.1 cgd * adjust and return. Otherwise, rescan and truncate
2038 1.1 cgd * after the remaining size.
2039 1.1 cgd */
2040 1.1 cgd count = 0;
2041 1.1 cgd m = mp;
2042 1.1 cgd for (;;) {
2043 1.1 cgd count += m->m_len;
2044 1.1 cgd if (m->m_next == (struct mbuf *)0)
2045 1.1 cgd break;
2046 1.1 cgd m = m->m_next;
2047 1.1 cgd }
2048 1.1 cgd if (m->m_len > len) {
2049 1.1 cgd m->m_len -= len;
2050 1.1 cgd if (nul > 0) {
2051 1.1 cgd cp = mtod(m, caddr_t)+m->m_len-nul;
2052 1.1 cgd for (i = 0; i < nul; i++)
2053 1.1 cgd *cp++ = '\0';
2054 1.1 cgd }
2055 1.1 cgd return;
2056 1.1 cgd }
2057 1.1 cgd count -= len;
2058 1.1 cgd if (count < 0)
2059 1.1 cgd count = 0;
2060 1.1 cgd /*
2061 1.1 cgd * Correct length for chain is "count".
2062 1.1 cgd * Find the mbuf with last data, adjust its length,
2063 1.1 cgd * and toss data from remaining mbufs on chain.
2064 1.1 cgd */
2065 1.1 cgd for (m = mp; m; m = m->m_next) {
2066 1.1 cgd if (m->m_len >= count) {
2067 1.1 cgd m->m_len = count;
2068 1.1 cgd if (nul > 0) {
2069 1.1 cgd cp = mtod(m, caddr_t)+m->m_len-nul;
2070 1.1 cgd for (i = 0; i < nul; i++)
2071 1.1 cgd *cp++ = '\0';
2072 1.1 cgd }
2073 1.1 cgd break;
2074 1.1 cgd }
2075 1.1 cgd count -= m->m_len;
2076 1.1 cgd }
2077 1.25 fvdl for (m = m->m_next;m;m = m->m_next)
2078 1.1 cgd m->m_len = 0;
2079 1.1 cgd }
2080 1.1 cgd
2081 1.1 cgd /*
2082 1.25 fvdl * Make these functions instead of macros, so that the kernel text size
2083 1.25 fvdl * doesn't get too big...
2084 1.25 fvdl */
2085 1.25 fvdl void
2086 1.25 fvdl nfsm_srvwcc(nfsd, before_ret, before_vap, after_ret, after_vap, mbp, bposp)
2087 1.25 fvdl struct nfsrv_descript *nfsd;
2088 1.25 fvdl int before_ret;
2089 1.75 augustss struct vattr *before_vap;
2090 1.25 fvdl int after_ret;
2091 1.25 fvdl struct vattr *after_vap;
2092 1.25 fvdl struct mbuf **mbp;
2093 1.25 fvdl char **bposp;
2094 1.25 fvdl {
2095 1.75 augustss struct mbuf *mb = *mbp, *mb2;
2096 1.75 augustss char *bpos = *bposp;
2097 1.75 augustss u_int32_t *tl;
2098 1.25 fvdl
2099 1.25 fvdl if (before_ret) {
2100 1.25 fvdl nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
2101 1.25 fvdl *tl = nfs_false;
2102 1.25 fvdl } else {
2103 1.25 fvdl nfsm_build(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
2104 1.25 fvdl *tl++ = nfs_true;
2105 1.66 fair txdr_hyper(before_vap->va_size, tl);
2106 1.25 fvdl tl += 2;
2107 1.25 fvdl txdr_nfsv3time(&(before_vap->va_mtime), tl);
2108 1.25 fvdl tl += 2;
2109 1.25 fvdl txdr_nfsv3time(&(before_vap->va_ctime), tl);
2110 1.25 fvdl }
2111 1.25 fvdl *bposp = bpos;
2112 1.25 fvdl *mbp = mb;
2113 1.25 fvdl nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp);
2114 1.25 fvdl }
2115 1.25 fvdl
2116 1.25 fvdl void
2117 1.25 fvdl nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp)
2118 1.25 fvdl struct nfsrv_descript *nfsd;
2119 1.25 fvdl int after_ret;
2120 1.25 fvdl struct vattr *after_vap;
2121 1.25 fvdl struct mbuf **mbp;
2122 1.25 fvdl char **bposp;
2123 1.25 fvdl {
2124 1.75 augustss struct mbuf *mb = *mbp, *mb2;
2125 1.75 augustss char *bpos = *bposp;
2126 1.75 augustss u_int32_t *tl;
2127 1.75 augustss struct nfs_fattr *fp;
2128 1.25 fvdl
2129 1.25 fvdl if (after_ret) {
2130 1.25 fvdl nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
2131 1.25 fvdl *tl = nfs_false;
2132 1.25 fvdl } else {
2133 1.25 fvdl nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_V3FATTR);
2134 1.25 fvdl *tl++ = nfs_true;
2135 1.25 fvdl fp = (struct nfs_fattr *)tl;
2136 1.25 fvdl nfsm_srvfattr(nfsd, after_vap, fp);
2137 1.25 fvdl }
2138 1.25 fvdl *mbp = mb;
2139 1.25 fvdl *bposp = bpos;
2140 1.25 fvdl }
2141 1.25 fvdl
2142 1.25 fvdl void
2143 1.25 fvdl nfsm_srvfattr(nfsd, vap, fp)
2144 1.75 augustss struct nfsrv_descript *nfsd;
2145 1.75 augustss struct vattr *vap;
2146 1.75 augustss struct nfs_fattr *fp;
2147 1.25 fvdl {
2148 1.25 fvdl
2149 1.25 fvdl fp->fa_nlink = txdr_unsigned(vap->va_nlink);
2150 1.25 fvdl fp->fa_uid = txdr_unsigned(vap->va_uid);
2151 1.25 fvdl fp->fa_gid = txdr_unsigned(vap->va_gid);
2152 1.25 fvdl if (nfsd->nd_flag & ND_NFSV3) {
2153 1.25 fvdl fp->fa_type = vtonfsv3_type(vap->va_type);
2154 1.25 fvdl fp->fa_mode = vtonfsv3_mode(vap->va_mode);
2155 1.66 fair txdr_hyper(vap->va_size, &fp->fa3_size);
2156 1.66 fair txdr_hyper(vap->va_bytes, &fp->fa3_used);
2157 1.25 fvdl fp->fa3_rdev.specdata1 = txdr_unsigned(major(vap->va_rdev));
2158 1.25 fvdl fp->fa3_rdev.specdata2 = txdr_unsigned(minor(vap->va_rdev));
2159 1.25 fvdl fp->fa3_fsid.nfsuquad[0] = 0;
2160 1.25 fvdl fp->fa3_fsid.nfsuquad[1] = txdr_unsigned(vap->va_fsid);
2161 1.25 fvdl fp->fa3_fileid.nfsuquad[0] = 0;
2162 1.25 fvdl fp->fa3_fileid.nfsuquad[1] = txdr_unsigned(vap->va_fileid);
2163 1.25 fvdl txdr_nfsv3time(&vap->va_atime, &fp->fa3_atime);
2164 1.25 fvdl txdr_nfsv3time(&vap->va_mtime, &fp->fa3_mtime);
2165 1.25 fvdl txdr_nfsv3time(&vap->va_ctime, &fp->fa3_ctime);
2166 1.25 fvdl } else {
2167 1.25 fvdl fp->fa_type = vtonfsv2_type(vap->va_type);
2168 1.25 fvdl fp->fa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
2169 1.25 fvdl fp->fa2_size = txdr_unsigned(vap->va_size);
2170 1.25 fvdl fp->fa2_blocksize = txdr_unsigned(vap->va_blocksize);
2171 1.25 fvdl if (vap->va_type == VFIFO)
2172 1.25 fvdl fp->fa2_rdev = 0xffffffff;
2173 1.25 fvdl else
2174 1.25 fvdl fp->fa2_rdev = txdr_unsigned(vap->va_rdev);
2175 1.25 fvdl fp->fa2_blocks = txdr_unsigned(vap->va_bytes / NFS_FABLKSIZE);
2176 1.25 fvdl fp->fa2_fsid = txdr_unsigned(vap->va_fsid);
2177 1.25 fvdl fp->fa2_fileid = txdr_unsigned(vap->va_fileid);
2178 1.25 fvdl txdr_nfsv2time(&vap->va_atime, &fp->fa2_atime);
2179 1.25 fvdl txdr_nfsv2time(&vap->va_mtime, &fp->fa2_mtime);
2180 1.25 fvdl txdr_nfsv2time(&vap->va_ctime, &fp->fa2_ctime);
2181 1.25 fvdl }
2182 1.25 fvdl }
2183 1.25 fvdl
2184 1.25 fvdl /*
2185 1.1 cgd * nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked)
2186 1.1 cgd * - look up fsid in mount list (if not found ret error)
2187 1.12 mycroft * - get vp and export rights by calling VFS_FHTOVP()
2188 1.12 mycroft * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
2189 1.1 cgd * - if not lockflag unlock it with VOP_UNLOCK()
2190 1.1 cgd */
2191 1.24 christos int
2192 1.43 fvdl nfsrv_fhtovp(fhp, lockflag, vpp, cred, slp, nam, rdonlyp, kerbflag, pubflag)
2193 1.1 cgd fhandle_t *fhp;
2194 1.1 cgd int lockflag;
2195 1.1 cgd struct vnode **vpp;
2196 1.1 cgd struct ucred *cred;
2197 1.12 mycroft struct nfssvc_sock *slp;
2198 1.12 mycroft struct mbuf *nam;
2199 1.12 mycroft int *rdonlyp;
2200 1.25 fvdl int kerbflag;
2201 1.1 cgd {
2202 1.75 augustss struct mount *mp;
2203 1.75 augustss int i;
2204 1.12 mycroft struct ucred *credanon;
2205 1.12 mycroft int error, exflags;
2206 1.40 fvdl struct sockaddr_in *saddr;
2207 1.1 cgd
2208 1.12 mycroft *vpp = (struct vnode *)0;
2209 1.43 fvdl
2210 1.43 fvdl if (nfs_ispublicfh(fhp)) {
2211 1.43 fvdl if (!pubflag || !nfs_pub.np_valid)
2212 1.43 fvdl return (ESTALE);
2213 1.43 fvdl fhp = &nfs_pub.np_handle;
2214 1.43 fvdl }
2215 1.43 fvdl
2216 1.25 fvdl mp = vfs_getvfs(&fhp->fh_fsid);
2217 1.25 fvdl if (!mp)
2218 1.1 cgd return (ESTALE);
2219 1.65 wrstuden error = VFS_CHECKEXP(mp, nam, &exflags, &credanon);
2220 1.64 wrstuden if (error)
2221 1.64 wrstuden return (error);
2222 1.64 wrstuden error = VFS_FHTOVP(mp, &fhp->fh_fid, vpp);
2223 1.24 christos if (error)
2224 1.12 mycroft return (error);
2225 1.40 fvdl
2226 1.43 fvdl if (!(exflags & (MNT_EXNORESPORT|MNT_EXPUBLIC))) {
2227 1.40 fvdl saddr = mtod(nam, struct sockaddr_in *);
2228 1.76 fvdl if ((saddr->sin_family == AF_INET) &&
2229 1.40 fvdl ntohs(saddr->sin_port) >= IPPORT_RESERVED) {
2230 1.40 fvdl vput(*vpp);
2231 1.40 fvdl return (NFSERR_AUTHERR | AUTH_TOOWEAK);
2232 1.40 fvdl }
2233 1.76 fvdl #ifdef INET6
2234 1.76 fvdl if ((saddr->sin_family == AF_INET6) &&
2235 1.76 fvdl ntohs(saddr->sin_port) >= IPV6PORT_RESERVED) {
2236 1.76 fvdl vput(*vpp);
2237 1.76 fvdl return (NFSERR_AUTHERR | AUTH_TOOWEAK);
2238 1.76 fvdl }
2239 1.76 fvdl #endif
2240 1.40 fvdl }
2241 1.12 mycroft /*
2242 1.12 mycroft * Check/setup credentials.
2243 1.12 mycroft */
2244 1.12 mycroft if (exflags & MNT_EXKERB) {
2245 1.25 fvdl if (!kerbflag) {
2246 1.12 mycroft vput(*vpp);
2247 1.25 fvdl return (NFSERR_AUTHERR | AUTH_TOOWEAK);
2248 1.12 mycroft }
2249 1.25 fvdl } else if (kerbflag) {
2250 1.25 fvdl vput(*vpp);
2251 1.25 fvdl return (NFSERR_AUTHERR | AUTH_TOOWEAK);
2252 1.12 mycroft } else if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON)) {
2253 1.12 mycroft cred->cr_uid = credanon->cr_uid;
2254 1.19 jtc cred->cr_gid = credanon->cr_gid;
2255 1.12 mycroft for (i = 0; i < credanon->cr_ngroups && i < NGROUPS; i++)
2256 1.12 mycroft cred->cr_groups[i] = credanon->cr_groups[i];
2257 1.15 mycroft cred->cr_ngroups = i;
2258 1.12 mycroft }
2259 1.12 mycroft if (exflags & MNT_EXRDONLY)
2260 1.12 mycroft *rdonlyp = 1;
2261 1.12 mycroft else
2262 1.12 mycroft *rdonlyp = 0;
2263 1.1 cgd if (!lockflag)
2264 1.56 fvdl VOP_UNLOCK(*vpp, 0);
2265 1.1 cgd return (0);
2266 1.43 fvdl }
2267 1.43 fvdl
2268 1.43 fvdl /*
2269 1.43 fvdl * WebNFS: check if a filehandle is a public filehandle. For v3, this
2270 1.43 fvdl * means a length of 0, for v2 it means all zeroes. nfsm_srvmtofh has
2271 1.43 fvdl * transformed this to all zeroes in both cases, so check for it.
2272 1.43 fvdl */
2273 1.43 fvdl int
2274 1.43 fvdl nfs_ispublicfh(fhp)
2275 1.43 fvdl fhandle_t *fhp;
2276 1.43 fvdl {
2277 1.43 fvdl char *cp = (char *)fhp;
2278 1.43 fvdl int i;
2279 1.43 fvdl
2280 1.43 fvdl for (i = 0; i < NFSX_V3FH; i++)
2281 1.43 fvdl if (*cp++ != 0)
2282 1.43 fvdl return (FALSE);
2283 1.43 fvdl return (TRUE);
2284 1.1 cgd }
2285 1.1 cgd
2286 1.1 cgd /*
2287 1.12 mycroft * This function compares two net addresses by family and returns TRUE
2288 1.12 mycroft * if they are the same host.
2289 1.12 mycroft * If there is any doubt, return FALSE.
2290 1.12 mycroft * The AF_INET family is handled as a special case so that address mbufs
2291 1.12 mycroft * don't need to be saved to store "struct in_addr", which is only 4 bytes.
2292 1.1 cgd */
2293 1.24 christos int
2294 1.12 mycroft netaddr_match(family, haddr, nam)
2295 1.12 mycroft int family;
2296 1.12 mycroft union nethostaddr *haddr;
2297 1.12 mycroft struct mbuf *nam;
2298 1.1 cgd {
2299 1.75 augustss struct sockaddr_in *inetaddr;
2300 1.1 cgd
2301 1.12 mycroft switch (family) {
2302 1.12 mycroft case AF_INET:
2303 1.12 mycroft inetaddr = mtod(nam, struct sockaddr_in *);
2304 1.12 mycroft if (inetaddr->sin_family == AF_INET &&
2305 1.12 mycroft inetaddr->sin_addr.s_addr == haddr->had_inetaddr)
2306 1.12 mycroft return (1);
2307 1.12 mycroft break;
2308 1.76 fvdl #ifdef INET6
2309 1.76 fvdl case AF_INET6:
2310 1.76 fvdl {
2311 1.76 fvdl struct sockaddr_in6 *sin6_1, *sin6_2;
2312 1.76 fvdl
2313 1.76 fvdl sin6_1 = mtod(nam, struct sockaddr_in6 *);
2314 1.76 fvdl sin6_2 = mtod(haddr->had_nam, struct sockaddr_in6 *);
2315 1.76 fvdl if (sin6_1->sin6_family == AF_INET6 &&
2316 1.76 fvdl IN6_ARE_ADDR_EQUAL(&sin6_1->sin6_addr, &sin6_2->sin6_addr))
2317 1.76 fvdl return 1;
2318 1.76 fvdl }
2319 1.76 fvdl #endif
2320 1.12 mycroft #ifdef ISO
2321 1.12 mycroft case AF_ISO:
2322 1.12 mycroft {
2323 1.75 augustss struct sockaddr_iso *isoaddr1, *isoaddr2;
2324 1.12 mycroft
2325 1.12 mycroft isoaddr1 = mtod(nam, struct sockaddr_iso *);
2326 1.12 mycroft isoaddr2 = mtod(haddr->had_nam, struct sockaddr_iso *);
2327 1.12 mycroft if (isoaddr1->siso_family == AF_ISO &&
2328 1.12 mycroft isoaddr1->siso_nlen > 0 &&
2329 1.12 mycroft isoaddr1->siso_nlen == isoaddr2->siso_nlen &&
2330 1.12 mycroft SAME_ISOADDR(isoaddr1, isoaddr2))
2331 1.12 mycroft return (1);
2332 1.12 mycroft break;
2333 1.12 mycroft }
2334 1.12 mycroft #endif /* ISO */
2335 1.12 mycroft default:
2336 1.12 mycroft break;
2337 1.12 mycroft };
2338 1.12 mycroft return (0);
2339 1.25 fvdl }
2340 1.25 fvdl
2341 1.25 fvdl
2342 1.25 fvdl /*
2343 1.25 fvdl * The write verifier has changed (probably due to a server reboot), so all
2344 1.25 fvdl * B_NEEDCOMMIT blocks will have to be written again. Since they are on the
2345 1.25 fvdl * dirty block list as B_DELWRI, all this takes is clearing the B_NEEDCOMMIT
2346 1.25 fvdl * flag. Once done the new write verifier can be set for the mount point.
2347 1.25 fvdl */
2348 1.25 fvdl void
2349 1.25 fvdl nfs_clearcommit(mp)
2350 1.25 fvdl struct mount *mp;
2351 1.25 fvdl {
2352 1.75 augustss struct vnode *vp, *nvp;
2353 1.75 augustss struct buf *bp, *nbp;
2354 1.25 fvdl int s;
2355 1.25 fvdl
2356 1.25 fvdl s = splbio();
2357 1.25 fvdl loop:
2358 1.25 fvdl for (vp = mp->mnt_vnodelist.lh_first; vp; vp = nvp) {
2359 1.25 fvdl if (vp->v_mount != mp) /* Paranoia */
2360 1.25 fvdl goto loop;
2361 1.25 fvdl nvp = vp->v_mntvnodes.le_next;
2362 1.25 fvdl for (bp = vp->v_dirtyblkhd.lh_first; bp; bp = nbp) {
2363 1.25 fvdl nbp = bp->b_vnbufs.le_next;
2364 1.25 fvdl if ((bp->b_flags & (B_BUSY | B_DELWRI | B_NEEDCOMMIT))
2365 1.25 fvdl == (B_DELWRI | B_NEEDCOMMIT))
2366 1.25 fvdl bp->b_flags &= ~B_NEEDCOMMIT;
2367 1.25 fvdl }
2368 1.25 fvdl }
2369 1.25 fvdl splx(s);
2370 1.25 fvdl }
2371 1.25 fvdl
2372 1.25 fvdl /*
2373 1.25 fvdl * Map errnos to NFS error numbers. For Version 3 also filter out error
2374 1.25 fvdl * numbers not specified for the associated procedure.
2375 1.25 fvdl */
2376 1.25 fvdl int
2377 1.25 fvdl nfsrv_errmap(nd, err)
2378 1.25 fvdl struct nfsrv_descript *nd;
2379 1.75 augustss int err;
2380 1.25 fvdl {
2381 1.75 augustss short *defaulterrp, *errp;
2382 1.25 fvdl
2383 1.25 fvdl if (nd->nd_flag & ND_NFSV3) {
2384 1.25 fvdl if (nd->nd_procnum <= NFSPROC_COMMIT) {
2385 1.25 fvdl errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum];
2386 1.25 fvdl while (*++errp) {
2387 1.25 fvdl if (*errp == err)
2388 1.25 fvdl return (err);
2389 1.25 fvdl else if (*errp > err)
2390 1.25 fvdl break;
2391 1.25 fvdl }
2392 1.25 fvdl return ((int)*defaulterrp);
2393 1.25 fvdl } else
2394 1.25 fvdl return (err & 0xffff);
2395 1.25 fvdl }
2396 1.25 fvdl if (err <= ELAST)
2397 1.25 fvdl return ((int)nfsrv_v2errmap[err - 1]);
2398 1.25 fvdl return (NFSERR_IO);
2399 1.25 fvdl }
2400 1.25 fvdl
2401 1.25 fvdl /*
2402 1.25 fvdl * Sort the group list in increasing numerical order.
2403 1.25 fvdl * (Insertion sort by Chris Torek, who was grossed out by the bubble sort
2404 1.25 fvdl * that used to be here.)
2405 1.25 fvdl */
2406 1.25 fvdl void
2407 1.25 fvdl nfsrvw_sort(list, num)
2408 1.75 augustss gid_t *list;
2409 1.75 augustss int num;
2410 1.25 fvdl {
2411 1.75 augustss int i, j;
2412 1.25 fvdl gid_t v;
2413 1.25 fvdl
2414 1.25 fvdl /* Insertion sort. */
2415 1.25 fvdl for (i = 1; i < num; i++) {
2416 1.25 fvdl v = list[i];
2417 1.25 fvdl /* find correct slot for value v, moving others up */
2418 1.25 fvdl for (j = i; --j >= 0 && v < list[j];)
2419 1.25 fvdl list[j + 1] = list[j];
2420 1.25 fvdl list[j + 1] = v;
2421 1.25 fvdl }
2422 1.25 fvdl }
2423 1.25 fvdl
2424 1.25 fvdl /*
2425 1.63 perry * copy credentials making sure that the result can be compared with memcmp().
2426 1.25 fvdl */
2427 1.25 fvdl void
2428 1.25 fvdl nfsrv_setcred(incred, outcred)
2429 1.75 augustss struct ucred *incred, *outcred;
2430 1.25 fvdl {
2431 1.75 augustss int i;
2432 1.25 fvdl
2433 1.63 perry memset((caddr_t)outcred, 0, sizeof (struct ucred));
2434 1.25 fvdl outcred->cr_ref = 1;
2435 1.25 fvdl outcred->cr_uid = incred->cr_uid;
2436 1.25 fvdl outcred->cr_gid = incred->cr_gid;
2437 1.25 fvdl outcred->cr_ngroups = incred->cr_ngroups;
2438 1.25 fvdl for (i = 0; i < incred->cr_ngroups; i++)
2439 1.25 fvdl outcred->cr_groups[i] = incred->cr_groups[i];
2440 1.25 fvdl nfsrvw_sort(outcred->cr_groups, outcred->cr_ngroups);
2441 1.1 cgd }
2442