nfs.c revision 1.23 1 1.23 thorpej /* $NetBSD: nfs.c,v 1.23 1998/01/23 19:27:44 thorpej Exp $ */
2 1.4 cgd
3 1.1 brezak /*-
4 1.1 brezak * Copyright (c) 1993 John Brezak
5 1.1 brezak * All rights reserved.
6 1.1 brezak *
7 1.1 brezak * Redistribution and use in source and binary forms, with or without
8 1.1 brezak * modification, are permitted provided that the following conditions
9 1.1 brezak * are met:
10 1.1 brezak * 1. Redistributions of source code must retain the above copyright
11 1.1 brezak * notice, this list of conditions and the following disclaimer.
12 1.1 brezak * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 brezak * notice, this list of conditions and the following disclaimer in the
14 1.1 brezak * documentation and/or other materials provided with the distribution.
15 1.1 brezak * 3. The name of the author may not be used to endorse or promote products
16 1.1 brezak * derived from this software without specific prior written permission.
17 1.1 brezak *
18 1.1 brezak * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
19 1.1 brezak * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 1.1 brezak * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 1.1 brezak * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
22 1.1 brezak * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 1.1 brezak * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 1.1 brezak * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 1.1 brezak * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 1.1 brezak * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 1.1 brezak * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 1.1 brezak * POSSIBILITY OF SUCH DAMAGE.
29 1.1 brezak */
30 1.1 brezak
31 1.1 brezak #include <sys/param.h>
32 1.1 brezak #include <sys/time.h>
33 1.1 brezak #include <sys/socket.h>
34 1.1 brezak #include <sys/stat.h>
35 1.22 drochner #ifdef _STANDALONE
36 1.22 drochner #include <lib/libkern/libkern.h>
37 1.22 drochner #else
38 1.1 brezak #include <string.h>
39 1.22 drochner #endif
40 1.1 brezak
41 1.1 brezak #include <netinet/in.h>
42 1.1 brezak #include <netinet/in_systm.h>
43 1.1 brezak
44 1.14 gwr #include "rpcv2.h"
45 1.13 scottr #include "nfsv2.h"
46 1.1 brezak
47 1.1 brezak #include "stand.h"
48 1.1 brezak #include "net.h"
49 1.1 brezak #include "netif.h"
50 1.1 brezak #include "nfs.h"
51 1.1 brezak #include "rpc.h"
52 1.1 brezak
53 1.7 gwr /* Define our own NFS attributes without NQNFS stuff. */
54 1.7 gwr struct nfsv2_fattrs {
55 1.7 gwr n_long fa_type;
56 1.7 gwr n_long fa_mode;
57 1.7 gwr n_long fa_nlink;
58 1.7 gwr n_long fa_uid;
59 1.7 gwr n_long fa_gid;
60 1.7 gwr n_long fa_size;
61 1.7 gwr n_long fa_blocksize;
62 1.7 gwr n_long fa_rdev;
63 1.7 gwr n_long fa_blocks;
64 1.7 gwr n_long fa_fsid;
65 1.7 gwr n_long fa_fileid;
66 1.7 gwr struct nfsv2_time fa_atime;
67 1.7 gwr struct nfsv2_time fa_mtime;
68 1.7 gwr struct nfsv2_time fa_ctime;
69 1.7 gwr };
70 1.7 gwr
71 1.7 gwr
72 1.7 gwr struct nfs_read_args {
73 1.1 brezak u_char fh[NFS_FHSIZE];
74 1.7 gwr n_long off;
75 1.7 gwr n_long len;
76 1.7 gwr n_long xxx; /* XXX what's this for? */
77 1.1 brezak };
78 1.1 brezak
79 1.1 brezak /* Data part of nfs rpc reply (also the largest thing we receive) */
80 1.7 gwr #define NFSREAD_SIZE 1024
81 1.7 gwr struct nfs_read_repl {
82 1.7 gwr n_long errno;
83 1.7 gwr struct nfsv2_fattrs fa;
84 1.7 gwr n_long count;
85 1.7 gwr u_char data[NFSREAD_SIZE];
86 1.1 brezak };
87 1.1 brezak
88 1.21 drochner #ifndef NFS_NOSYMLINK
89 1.16 ws struct nfs_readlnk_repl {
90 1.16 ws n_long errno;
91 1.16 ws n_long len;
92 1.16 ws char path[NFS_MAXPATHLEN];
93 1.16 ws };
94 1.21 drochner #endif
95 1.16 ws
96 1.1 brezak struct nfs_iodesc {
97 1.7 gwr struct iodesc *iodesc;
98 1.1 brezak off_t off;
99 1.7 gwr u_char fh[NFS_FHSIZE];
100 1.7 gwr struct nfsv2_fattrs fa; /* all in network order */
101 1.7 gwr };
102 1.7 gwr
103 1.8 gwr struct nfs_iodesc nfs_root_node;
104 1.1 brezak
105 1.7 gwr
106 1.12 gwr /*
107 1.12 gwr * Fetch the root file handle (call mount daemon)
108 1.12 gwr * On error, return non-zero and set errno.
109 1.12 gwr */
110 1.7 gwr int
111 1.7 gwr nfs_getrootfh(d, path, fhp)
112 1.1 brezak register struct iodesc *d;
113 1.1 brezak char *path;
114 1.1 brezak u_char *fhp;
115 1.1 brezak {
116 1.1 brezak register int len;
117 1.7 gwr struct args {
118 1.7 gwr n_long len;
119 1.7 gwr char path[FNAME_SIZE];
120 1.7 gwr } *args;
121 1.7 gwr struct repl {
122 1.7 gwr n_long errno;
123 1.7 gwr u_char fh[NFS_FHSIZE];
124 1.7 gwr } *repl;
125 1.1 brezak struct {
126 1.7 gwr n_long h[RPC_HEADER_WORDS];
127 1.7 gwr struct args d;
128 1.7 gwr } sdata;
129 1.1 brezak struct {
130 1.7 gwr n_long h[RPC_HEADER_WORDS];
131 1.7 gwr struct repl d;
132 1.7 gwr } rdata;
133 1.6 mycroft size_t cc;
134 1.1 brezak
135 1.1 brezak #ifdef NFS_DEBUG
136 1.1 brezak if (debug)
137 1.19 christos printf("nfs_getrootfh: %s\n", path);
138 1.1 brezak #endif
139 1.6 mycroft
140 1.7 gwr args = &sdata.d;
141 1.7 gwr repl = &rdata.d;
142 1.7 gwr
143 1.7 gwr bzero(args, sizeof(*args));
144 1.1 brezak len = strlen(path);
145 1.7 gwr if (len > sizeof(args->path))
146 1.7 gwr len = sizeof(args->path);
147 1.7 gwr args->len = htonl(len);
148 1.7 gwr bcopy(path, args->path, len);
149 1.7 gwr len = 4 + roundup(len, 4);
150 1.7 gwr
151 1.7 gwr cc = rpc_call(d, RPCPROG_MNT, RPCMNT_VER1, RPCMNT_MOUNT,
152 1.7 gwr args, len, repl, sizeof(*repl));
153 1.12 gwr if (cc == -1) {
154 1.12 gwr /* errno was set by rpc_call */
155 1.12 gwr return (-1);
156 1.12 gwr }
157 1.12 gwr if (cc < 4) {
158 1.12 gwr errno = EBADRPC;
159 1.7 gwr return (-1);
160 1.12 gwr }
161 1.7 gwr if (repl->errno) {
162 1.7 gwr errno = ntohl(repl->errno);
163 1.6 mycroft return (-1);
164 1.1 brezak }
165 1.7 gwr bcopy(repl->fh, fhp, sizeof(repl->fh));
166 1.6 mycroft return (0);
167 1.1 brezak }
168 1.1 brezak
169 1.12 gwr /*
170 1.12 gwr * Lookup a file. Store handle and attributes.
171 1.12 gwr * Return zero or error number.
172 1.12 gwr */
173 1.7 gwr int
174 1.7 gwr nfs_lookupfh(d, name, newfd)
175 1.1 brezak struct nfs_iodesc *d;
176 1.1 brezak char *name;
177 1.7 gwr struct nfs_iodesc *newfd;
178 1.1 brezak {
179 1.1 brezak register int len, rlen;
180 1.7 gwr struct args {
181 1.1 brezak u_char fh[NFS_FHSIZE];
182 1.7 gwr n_long len;
183 1.1 brezak char name[FNAME_SIZE];
184 1.7 gwr } *args;
185 1.7 gwr struct repl {
186 1.7 gwr n_long errno;
187 1.7 gwr u_char fh[NFS_FHSIZE];
188 1.7 gwr struct nfsv2_fattrs fa;
189 1.7 gwr } *repl;
190 1.7 gwr struct {
191 1.7 gwr n_long h[RPC_HEADER_WORDS];
192 1.7 gwr struct args d;
193 1.7 gwr } sdata;
194 1.1 brezak struct {
195 1.7 gwr n_long h[RPC_HEADER_WORDS];
196 1.7 gwr struct repl d;
197 1.7 gwr } rdata;
198 1.9 pk ssize_t cc;
199 1.1 brezak
200 1.1 brezak #ifdef NFS_DEBUG
201 1.1 brezak if (debug)
202 1.19 christos printf("lookupfh: called\n");
203 1.1 brezak #endif
204 1.1 brezak
205 1.7 gwr args = &sdata.d;
206 1.7 gwr repl = &rdata.d;
207 1.7 gwr
208 1.7 gwr bzero(args, sizeof(*args));
209 1.7 gwr bcopy(d->fh, args->fh, sizeof(args->fh));
210 1.1 brezak len = strlen(name);
211 1.7 gwr if (len > sizeof(args->name))
212 1.7 gwr len = sizeof(args->name);
213 1.7 gwr bcopy(name, args->name, len);
214 1.7 gwr args->len = htonl(len);
215 1.7 gwr len = 4 + roundup(len, 4);
216 1.7 gwr len += NFS_FHSIZE;
217 1.7 gwr
218 1.7 gwr rlen = sizeof(*repl);
219 1.7 gwr
220 1.7 gwr cc = rpc_call(d->iodesc, NFS_PROG, NFS_VER2, NFSPROC_LOOKUP,
221 1.7 gwr args, len, repl, rlen);
222 1.12 gwr if (cc == -1)
223 1.12 gwr return (errno); /* XXX - from rpc_call */
224 1.7 gwr if (cc < 4)
225 1.7 gwr return (EIO);
226 1.7 gwr if (repl->errno) {
227 1.12 gwr /* saerrno.h now matches NFS error numbers. */
228 1.12 gwr return (ntohl(repl->errno));
229 1.1 brezak }
230 1.7 gwr bcopy( repl->fh, &newfd->fh, sizeof(newfd->fh));
231 1.7 gwr bcopy(&repl->fa, &newfd->fa, sizeof(newfd->fa));
232 1.1 brezak return (0);
233 1.1 brezak }
234 1.1 brezak
235 1.21 drochner #ifndef NFS_NOSYMLINK
236 1.12 gwr /*
237 1.16 ws * Get the destination of a symbolic link.
238 1.16 ws */
239 1.16 ws int
240 1.16 ws nfs_readlink(d, buf)
241 1.16 ws struct nfs_iodesc *d;
242 1.16 ws char *buf;
243 1.16 ws {
244 1.16 ws struct {
245 1.16 ws n_long h[RPC_HEADER_WORDS];
246 1.16 ws u_char fh[NFS_FHSIZE];
247 1.16 ws } sdata;
248 1.16 ws struct {
249 1.16 ws n_long h[RPC_HEADER_WORDS];
250 1.16 ws struct nfs_readlnk_repl d;
251 1.16 ws } rdata;
252 1.16 ws ssize_t cc;
253 1.16 ws
254 1.16 ws #ifdef NFS_DEBUG
255 1.16 ws if (debug)
256 1.19 christos printf("readlink: called\n");
257 1.16 ws #endif
258 1.16 ws
259 1.16 ws bcopy(d->fh, sdata.fh, NFS_FHSIZE);
260 1.16 ws cc = rpc_call(d->iodesc, NFS_PROG, NFS_VER2, NFSPROC_READLINK,
261 1.16 ws sdata.fh, NFS_FHSIZE,
262 1.16 ws &rdata.d, sizeof(rdata.d));
263 1.16 ws if (cc == -1)
264 1.16 ws return (errno);
265 1.16 ws
266 1.16 ws if (cc < 4)
267 1.16 ws return (EIO);
268 1.16 ws
269 1.16 ws if (rdata.d.errno)
270 1.16 ws return (ntohl(rdata.d.errno));
271 1.17 cgd
272 1.17 cgd rdata.d.len = ntohl(rdata.d.len);
273 1.16 ws if (rdata.d.len > NFS_MAXPATHLEN)
274 1.16 ws return (ENAMETOOLONG);
275 1.16 ws
276 1.16 ws bcopy(rdata.d.path, buf, rdata.d.len);
277 1.16 ws buf[rdata.d.len] = 0;
278 1.16 ws return (0);
279 1.16 ws }
280 1.21 drochner #endif
281 1.16 ws
282 1.16 ws /*
283 1.12 gwr * Read data from a file.
284 1.12 gwr * Return transfer count or -1 (and set errno)
285 1.12 gwr */
286 1.9 pk ssize_t
287 1.7 gwr nfs_readdata(d, off, addr, len)
288 1.7 gwr struct nfs_iodesc *d;
289 1.7 gwr off_t off;
290 1.7 gwr void *addr;
291 1.7 gwr size_t len;
292 1.1 brezak {
293 1.7 gwr struct nfs_read_args *args;
294 1.7 gwr struct nfs_read_repl *repl;
295 1.7 gwr struct {
296 1.7 gwr n_long h[RPC_HEADER_WORDS];
297 1.7 gwr struct nfs_read_args d;
298 1.7 gwr } sdata;
299 1.7 gwr struct {
300 1.7 gwr n_long h[RPC_HEADER_WORDS];
301 1.7 gwr struct nfs_read_repl d;
302 1.7 gwr } rdata;
303 1.6 mycroft size_t cc;
304 1.9 pk long x;
305 1.9 pk int hlen, rlen;
306 1.7 gwr
307 1.7 gwr args = &sdata.d;
308 1.7 gwr repl = &rdata.d;
309 1.6 mycroft
310 1.7 gwr bcopy(d->fh, args->fh, NFS_FHSIZE);
311 1.14 gwr args->off = htonl((n_long)off);
312 1.6 mycroft if (len > NFSREAD_SIZE)
313 1.6 mycroft len = NFSREAD_SIZE;
314 1.14 gwr args->len = htonl((n_long)len);
315 1.14 gwr args->xxx = htonl((n_long)0);
316 1.7 gwr hlen = sizeof(*repl) - NFSREAD_SIZE;
317 1.7 gwr
318 1.7 gwr cc = rpc_call(d->iodesc, NFS_PROG, NFS_VER2, NFSPROC_READ,
319 1.7 gwr args, sizeof(*args),
320 1.7 gwr repl, sizeof(*repl));
321 1.12 gwr if (cc == -1) {
322 1.12 gwr /* errno was already set by rpc_call */
323 1.12 gwr return (-1);
324 1.12 gwr }
325 1.9 pk if (cc < hlen) {
326 1.12 gwr errno = EBADRPC;
327 1.7 gwr return (-1);
328 1.9 pk }
329 1.7 gwr if (repl->errno) {
330 1.7 gwr errno = ntohl(repl->errno);
331 1.6 mycroft return (-1);
332 1.7 gwr }
333 1.7 gwr rlen = cc - hlen;
334 1.7 gwr x = ntohl(repl->count);
335 1.7 gwr if (rlen < x) {
336 1.19 christos printf("nfsread: short packet, %d < %ld\n", rlen, x);
337 1.9 pk errno = EBADRPC;
338 1.7 gwr return(-1);
339 1.7 gwr }
340 1.7 gwr bcopy(repl->data, addr, x);
341 1.7 gwr return (x);
342 1.1 brezak }
343 1.1 brezak
344 1.1 brezak /*
345 1.1 brezak * nfs_mount - mount this nfs filesystem to a host
346 1.12 gwr * On error, return non-zero and set errno.
347 1.1 brezak */
348 1.1 brezak int
349 1.1 brezak nfs_mount(sock, ip, path)
350 1.1 brezak int sock;
351 1.11 pk struct in_addr ip;
352 1.1 brezak char *path;
353 1.1 brezak {
354 1.1 brezak struct iodesc *desc;
355 1.8 gwr struct nfsv2_fattrs *fa;
356 1.7 gwr
357 1.1 brezak if (!(desc = socktodesc(sock))) {
358 1.1 brezak errno = EINVAL;
359 1.1 brezak return(-1);
360 1.1 brezak }
361 1.7 gwr
362 1.7 gwr /* Bind to a reserved port. */
363 1.7 gwr desc->myport = htons(--rpc_port);
364 1.1 brezak desc->destip = ip;
365 1.7 gwr if (nfs_getrootfh(desc, path, nfs_root_node.fh))
366 1.7 gwr return (-1);
367 1.7 gwr nfs_root_node.iodesc = desc;
368 1.8 gwr /* Fake up attributes for the root dir. */
369 1.8 gwr fa = &nfs_root_node.fa;
370 1.8 gwr fa->fa_type = htonl(NFDIR);
371 1.8 gwr fa->fa_mode = htonl(0755);
372 1.8 gwr fa->fa_nlink = htonl(2);
373 1.1 brezak
374 1.1 brezak #ifdef NFS_DEBUG
375 1.1 brezak if (debug)
376 1.19 christos printf("nfs_mount: got fh for %s\n", path);
377 1.1 brezak #endif
378 1.1 brezak
379 1.1 brezak return(0);
380 1.1 brezak }
381 1.1 brezak
382 1.1 brezak /*
383 1.1 brezak * Open a file.
384 1.12 gwr * return zero or error number
385 1.1 brezak */
386 1.1 brezak int
387 1.1 brezak nfs_open(path, f)
388 1.1 brezak char *path;
389 1.1 brezak struct open_file *f;
390 1.1 brezak {
391 1.16 ws struct nfs_iodesc *newfd, *currfd;
392 1.21 drochner #ifndef NFS_NOSYMLINK
393 1.16 ws register char *cp, *ncp;
394 1.16 ws register int c;
395 1.16 ws char namebuf[NFS_MAXPATHLEN + 1];
396 1.16 ws char linkbuf[NFS_MAXPATHLEN + 1];
397 1.16 ws int nlinks = 0;
398 1.21 drochner #endif
399 1.12 gwr int error = 0;
400 1.1 brezak
401 1.1 brezak #ifdef NFS_DEBUG
402 1.1 brezak if (debug)
403 1.19 christos printf("nfs_open: %s\n", path);
404 1.1 brezak #endif
405 1.7 gwr if (nfs_root_node.iodesc == NULL) {
406 1.19 christos printf("nfs_open: must mount first.\n");
407 1.12 gwr return (ENXIO);
408 1.1 brezak }
409 1.1 brezak
410 1.16 ws currfd = &nfs_root_node;
411 1.16 ws newfd = 0;
412 1.21 drochner
413 1.21 drochner #ifndef NFS_NOSYMLINK
414 1.16 ws cp = path;
415 1.16 ws while (*cp) {
416 1.16 ws /*
417 1.16 ws * Remove extra separators
418 1.16 ws */
419 1.16 ws while (*cp == '/')
420 1.16 ws cp++;
421 1.16 ws
422 1.16 ws if (*cp == '\0')
423 1.16 ws break;
424 1.16 ws /*
425 1.16 ws * Check that current node is a directory.
426 1.16 ws */
427 1.17 cgd if (currfd->fa.fa_type != htonl(NFDIR)) {
428 1.16 ws error = ENOTDIR;
429 1.16 ws goto out;
430 1.16 ws }
431 1.16 ws
432 1.16 ws /* allocate file system specific data structure */
433 1.16 ws newfd = alloc(sizeof(*newfd));
434 1.16 ws newfd->iodesc = currfd->iodesc;
435 1.16 ws newfd->off = 0;
436 1.16 ws
437 1.16 ws /*
438 1.16 ws * Get next component of path name.
439 1.16 ws */
440 1.16 ws {
441 1.16 ws register int len = 0;
442 1.16 ws
443 1.16 ws ncp = cp;
444 1.16 ws while ((c = *cp) != '\0' && c != '/') {
445 1.16 ws if (++len > NFS_MAXNAMLEN) {
446 1.16 ws error = ENOENT;
447 1.16 ws goto out;
448 1.16 ws }
449 1.16 ws cp++;
450 1.16 ws }
451 1.16 ws *cp = '\0';
452 1.16 ws }
453 1.16 ws
454 1.16 ws /* lookup a file handle */
455 1.16 ws error = nfs_lookupfh(currfd, ncp, newfd);
456 1.16 ws *cp = c;
457 1.16 ws if (error)
458 1.16 ws goto out;
459 1.16 ws
460 1.16 ws /*
461 1.16 ws * Check for symbolic link
462 1.16 ws */
463 1.17 cgd if (newfd->fa.fa_type == htonl(NFLNK)) {
464 1.16 ws int link_len, len;
465 1.16 ws
466 1.16 ws error = nfs_readlink(newfd, linkbuf);
467 1.16 ws if (error)
468 1.16 ws goto out;
469 1.16 ws
470 1.16 ws link_len = strlen(linkbuf);
471 1.16 ws len = strlen(cp);
472 1.16 ws
473 1.16 ws if (link_len + len > MAXPATHLEN
474 1.16 ws || ++nlinks > MAXSYMLINKS) {
475 1.16 ws error = ENOENT;
476 1.16 ws goto out;
477 1.16 ws }
478 1.16 ws
479 1.16 ws bcopy(cp, &namebuf[link_len], len + 1);
480 1.16 ws bcopy(linkbuf, namebuf, link_len);
481 1.16 ws
482 1.16 ws /*
483 1.16 ws * If absolute pathname, restart at root.
484 1.16 ws * If relative pathname, restart at parent directory.
485 1.16 ws */
486 1.16 ws cp = namebuf;
487 1.16 ws if (*cp == '/') {
488 1.16 ws if (currfd != &nfs_root_node)
489 1.16 ws free(currfd, sizeof(*currfd));
490 1.16 ws currfd = &nfs_root_node;
491 1.16 ws }
492 1.16 ws
493 1.16 ws free(newfd, sizeof(*newfd));
494 1.16 ws newfd = 0;
495 1.16 ws
496 1.16 ws continue;
497 1.16 ws }
498 1.16 ws
499 1.16 ws if (currfd != &nfs_root_node)
500 1.16 ws free(currfd, sizeof(*currfd));
501 1.16 ws currfd = newfd;
502 1.16 ws newfd = 0;
503 1.16 ws }
504 1.16 ws
505 1.16 ws error = 0;
506 1.1 brezak
507 1.16 ws out:
508 1.21 drochner #else
509 1.21 drochner /* allocate file system specific data structure */
510 1.21 drochner currfd = alloc(sizeof(*currfd));
511 1.21 drochner currfd->iodesc = nfs_root_node.iodesc;
512 1.21 drochner currfd->off = 0;
513 1.21 drochner
514 1.21 drochner error = nfs_lookupfh(&nfs_root_node, path, currfd);
515 1.21 drochner #endif
516 1.12 gwr if (!error) {
517 1.16 ws f->f_fsdata = (void *)currfd;
518 1.12 gwr return (0);
519 1.1 brezak }
520 1.16 ws
521 1.1 brezak #ifdef NFS_DEBUG
522 1.1 brezak if (debug)
523 1.19 christos printf("nfs_open: %s lookupfh failed: %s\n",
524 1.18 christos path, strerror(error));
525 1.1 brezak #endif
526 1.16 ws if (currfd != &nfs_root_node)
527 1.16 ws free(currfd, sizeof(*currfd));
528 1.16 ws if (newfd)
529 1.16 ws free(newfd, sizeof(*newfd));
530 1.16 ws
531 1.12 gwr return (error);
532 1.1 brezak }
533 1.1 brezak
534 1.1 brezak int
535 1.1 brezak nfs_close(f)
536 1.1 brezak struct open_file *f;
537 1.1 brezak {
538 1.1 brezak register struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
539 1.1 brezak
540 1.1 brezak #ifdef NFS_DEBUG
541 1.1 brezak if (debug)
542 1.23 thorpej printf("nfs_close: fp=0x%lx\n", (u_long)fp);
543 1.1 brezak #endif
544 1.7 gwr
545 1.7 gwr if (fp)
546 1.7 gwr free(fp, sizeof(struct nfs_iodesc));
547 1.1 brezak f->f_fsdata = (void *)0;
548 1.1 brezak
549 1.1 brezak return (0);
550 1.1 brezak }
551 1.1 brezak
552 1.1 brezak /*
553 1.1 brezak * read a portion of a file
554 1.1 brezak */
555 1.10 pk int
556 1.9 pk nfs_read(f, buf, size, resid)
557 1.1 brezak struct open_file *f;
558 1.9 pk void *buf;
559 1.9 pk size_t size;
560 1.9 pk size_t *resid; /* out */
561 1.1 brezak {
562 1.1 brezak register struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
563 1.9 pk register ssize_t cc;
564 1.9 pk register char *addr = buf;
565 1.1 brezak
566 1.1 brezak #ifdef NFS_DEBUG
567 1.1 brezak if (debug)
568 1.23 thorpej printf("nfs_read: size=%lu off=%d\n", (u_long)size,
569 1.23 thorpej (int)fp->off);
570 1.1 brezak #endif
571 1.9 pk while ((int)size > 0) {
572 1.7 gwr twiddle();
573 1.7 gwr cc = nfs_readdata(fp, fp->off, (void *)addr, size);
574 1.6 mycroft /* XXX maybe should retry on certain errors */
575 1.6 mycroft if (cc == -1) {
576 1.6 mycroft #ifdef NFS_DEBUG
577 1.6 mycroft if (debug)
578 1.19 christos printf("nfs_read: read: %s", strerror(errno));
579 1.1 brezak #endif
580 1.12 gwr return (errno); /* XXX - from nfs_readdata */
581 1.6 mycroft }
582 1.6 mycroft if (cc == 0) {
583 1.20 pk #ifdef NFS_DEBUG
584 1.1 brezak if (debug)
585 1.19 christos printf("nfs_read: hit EOF unexpectantly");
586 1.20 pk #endif
587 1.1 brezak goto ret;
588 1.1 brezak }
589 1.1 brezak fp->off += cc;
590 1.1 brezak addr += cc;
591 1.1 brezak size -= cc;
592 1.1 brezak }
593 1.1 brezak ret:
594 1.1 brezak if (resid)
595 1.1 brezak *resid = size;
596 1.1 brezak
597 1.1 brezak return (0);
598 1.1 brezak }
599 1.1 brezak
600 1.1 brezak /*
601 1.1 brezak * Not implemented.
602 1.1 brezak */
603 1.10 pk int
604 1.9 pk nfs_write(f, buf, size, resid)
605 1.1 brezak struct open_file *f;
606 1.9 pk void *buf;
607 1.9 pk size_t size;
608 1.9 pk size_t *resid; /* out */
609 1.1 brezak {
610 1.10 pk return (EROFS);
611 1.1 brezak }
612 1.1 brezak
613 1.1 brezak off_t
614 1.1 brezak nfs_seek(f, offset, where)
615 1.1 brezak struct open_file *f;
616 1.1 brezak off_t offset;
617 1.1 brezak int where;
618 1.1 brezak {
619 1.7 gwr register struct nfs_iodesc *d = (struct nfs_iodesc *)f->f_fsdata;
620 1.7 gwr n_long size = ntohl(d->fa.fa_size);
621 1.1 brezak
622 1.1 brezak switch (where) {
623 1.1 brezak case SEEK_SET:
624 1.7 gwr d->off = offset;
625 1.1 brezak break;
626 1.1 brezak case SEEK_CUR:
627 1.7 gwr d->off += offset;
628 1.1 brezak break;
629 1.1 brezak case SEEK_END:
630 1.7 gwr d->off = size - offset;
631 1.1 brezak break;
632 1.1 brezak default:
633 1.1 brezak return (-1);
634 1.1 brezak }
635 1.7 gwr
636 1.7 gwr return (d->off);
637 1.1 brezak }
638 1.1 brezak
639 1.7 gwr /* NFNON=0, NFREG=1, NFDIR=2, NFBLK=3, NFCHR=4, NFLNK=5 */
640 1.7 gwr int nfs_stat_types[8] = {
641 1.7 gwr 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, 0 };
642 1.7 gwr
643 1.1 brezak int
644 1.1 brezak nfs_stat(f, sb)
645 1.1 brezak struct open_file *f;
646 1.1 brezak struct stat *sb;
647 1.1 brezak {
648 1.7 gwr struct nfs_iodesc *fp = (struct nfs_iodesc *)f->f_fsdata;
649 1.7 gwr register n_long ftype, mode;
650 1.1 brezak
651 1.7 gwr ftype = ntohl(fp->fa.fa_type);
652 1.7 gwr mode = ntohl(fp->fa.fa_mode);
653 1.7 gwr mode |= nfs_stat_types[ftype & 7];
654 1.7 gwr
655 1.7 gwr sb->st_mode = mode;
656 1.7 gwr sb->st_nlink = ntohl(fp->fa.fa_nlink);
657 1.7 gwr sb->st_uid = ntohl(fp->fa.fa_uid);
658 1.7 gwr sb->st_gid = ntohl(fp->fa.fa_gid);
659 1.7 gwr sb->st_size = ntohl(fp->fa.fa_size);
660 1.1 brezak
661 1.1 brezak return (0);
662 1.1 brezak }
663