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