]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - lib/libstand/ufs.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / lib / libstand / ufs.c
1 /*      $NetBSD: ufs.c,v 1.20 1998/03/01 07:15:39 ross Exp $    */
2
3 /*-
4  * Copyright (c) 2002 Networks Associates Technology, Inc.
5  * All rights reserved.
6  *
7  * This software was developed for the FreeBSD Project by Marshall
8  * Kirk McKusick and Network Associates Laboratories, the Security
9  * Research Division of Network Associates, Inc. under DARPA/SPAWAR
10  * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
11  * research program
12  *
13  * Copyright (c) 1982, 1989, 1993
14  *      The Regents of the University of California.  All rights reserved.
15  *
16  * This code is derived from software contributed to Berkeley by
17  * The Mach Operating System project at Carnegie-Mellon University.
18  *
19  * Redistribution and use in source and binary forms, with or without
20  * modification, are permitted provided that the following conditions
21  * are met:
22  * 1. Redistributions of source code must retain the above copyright
23  *    notice, this list of conditions and the following disclaimer.
24  * 2. Redistributions in binary form must reproduce the above copyright
25  *    notice, this list of conditions and the following disclaimer in the
26  *    documentation and/or other materials provided with the distribution.
27  * 4. Neither the name of the University nor the names of its contributors
28  *    may be used to endorse or promote products derived from this software
29  *    without specific prior written permission.
30  *
31  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
32  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
35  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41  * SUCH DAMAGE.
42  *  
43  *
44  * Copyright (c) 1990, 1991 Carnegie Mellon University
45  * All Rights Reserved.
46  *
47  * Author: David Golub
48  * 
49  * Permission to use, copy, modify and distribute this software and its
50  * documentation is hereby granted, provided that both the copyright
51  * notice and this permission notice appear in all copies of the
52  * software, derivative works or modified versions, and any portions
53  * thereof, and that both notices appear in supporting documentation.
54  * 
55  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
56  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
57  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
58  * 
59  * Carnegie Mellon requests users of this software to return to
60  * 
61  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
62  *  School of Computer Science
63  *  Carnegie Mellon University
64  *  Pittsburgh PA 15213-3890
65  * 
66  * any improvements or extensions that they make and grant Carnegie the
67  * rights to redistribute these changes.
68  */
69
70 #include <sys/cdefs.h>
71 __FBSDID("$FreeBSD$");
72
73 /*
74  *      Stand-alone file reading package.
75  */
76
77 #include <sys/param.h>
78 #include <sys/disklabel.h>
79 #include <sys/time.h>
80 #include <ufs/ufs/dinode.h>
81 #include <ufs/ufs/dir.h>
82 #include <ufs/ffs/fs.h>
83 #include "stand.h"
84 #include "string.h"
85
86 #ifdef __alpha__
87 #define COMPAT_UFS              /* DUX has old format file systems */
88 #endif
89
90 static int      ufs_open(const char *path, struct open_file *f);
91 static int      ufs_write(struct open_file *f, void *buf, size_t size, size_t *resid);
92 static int      ufs_close(struct open_file *f);
93 static int      ufs_read(struct open_file *f, void *buf, size_t size, size_t *resid);
94 static off_t    ufs_seek(struct open_file *f, off_t offset, int where);
95 static int      ufs_stat(struct open_file *f, struct stat *sb);
96 static int      ufs_readdir(struct open_file *f, struct dirent *d);
97
98 struct fs_ops ufs_fsops = {
99         "ufs",
100         ufs_open,
101         ufs_close,
102         ufs_read,
103         ufs_write,
104         ufs_seek,
105         ufs_stat,
106         ufs_readdir
107 };
108
109 /*
110  * In-core open file.
111  */
112 struct file {
113         off_t           f_seekp;        /* seek pointer */
114         struct fs       *f_fs;          /* pointer to super-block */
115         union dinode {
116                 struct ufs1_dinode di1;
117                 struct ufs2_dinode di2;
118         }               f_di;           /* copy of on-disk inode */
119         int             f_nindir[NIADDR];
120                                         /* number of blocks mapped by
121                                            indirect block at level i */
122         char            *f_blk[NIADDR]; /* buffer for indirect block at
123                                            level i */
124         size_t          f_blksize[NIADDR];
125                                         /* size of buffer */
126         ufs2_daddr_t    f_blkno[NIADDR];/* disk address of block in buffer */
127         ufs2_daddr_t    f_buf_blkno;    /* block number of data block */
128         char            *f_buf;         /* buffer for data block */
129         size_t          f_buf_size;     /* size of data block */
130 };
131 #define DIP(fp, field) \
132         ((fp)->f_fs->fs_magic == FS_UFS1_MAGIC ? \
133         (fp)->f_di.di1.field : (fp)->f_di.di2.field)
134
135 static int      read_inode(ino_t, struct open_file *);
136 static int      block_map(struct open_file *, ufs2_daddr_t, ufs2_daddr_t *);
137 static int      buf_read_file(struct open_file *, char **, size_t *);
138 static int      buf_write_file(struct open_file *, char *, size_t *);
139 static int      search_directory(char *, struct open_file *, ino_t *);
140
141 /*
142  * Read a new inode into a file structure.
143  */
144 static int
145 read_inode(inumber, f)
146         ino_t inumber;
147         struct open_file *f;
148 {
149         struct file *fp = (struct file *)f->f_fsdata;
150         struct fs *fs = fp->f_fs;
151         char *buf;
152         size_t rsize;
153         int rc;
154
155         if (fs == NULL)
156             panic("fs == NULL");
157
158         /*
159          * Read inode and save it.
160          */
161         buf = malloc(fs->fs_bsize);
162         twiddle();
163         rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
164                 fsbtodb(fs, ino_to_fsba(fs, inumber)), fs->fs_bsize,
165                 buf, &rsize);
166         if (rc)
167                 goto out;
168         if (rsize != fs->fs_bsize) {
169                 rc = EIO;
170                 goto out;
171         }
172
173         if (fp->f_fs->fs_magic == FS_UFS1_MAGIC)
174                 fp->f_di.di1 = ((struct ufs1_dinode *)buf)
175                     [ino_to_fsbo(fs, inumber)];
176         else
177                 fp->f_di.di2 = ((struct ufs2_dinode *)buf)
178                     [ino_to_fsbo(fs, inumber)];
179
180         /*
181          * Clear out the old buffers
182          */
183         {
184                 int level;
185
186                 for (level = 0; level < NIADDR; level++)
187                         fp->f_blkno[level] = -1;
188                 fp->f_buf_blkno = -1;
189         }
190         fp->f_seekp = 0;
191 out:
192         free(buf);
193         return (rc);     
194 }
195
196 /*
197  * Given an offset in a file, find the disk block number that
198  * contains that block.
199  */
200 static int
201 block_map(f, file_block, disk_block_p)
202         struct open_file *f;
203         ufs2_daddr_t file_block;
204         ufs2_daddr_t *disk_block_p;     /* out */
205 {
206         struct file *fp = (struct file *)f->f_fsdata;
207         struct fs *fs = fp->f_fs;
208         int level;
209         int idx;
210         ufs2_daddr_t ind_block_num;
211         int rc;
212
213         /*
214          * Index structure of an inode:
215          *
216          * di_db[0..NDADDR-1]   hold block numbers for blocks
217          *                      0..NDADDR-1
218          *
219          * di_ib[0]             index block 0 is the single indirect block
220          *                      holds block numbers for blocks
221          *                      NDADDR .. NDADDR + NINDIR(fs)-1
222          *
223          * di_ib[1]             index block 1 is the double indirect block
224          *                      holds block numbers for INDEX blocks for blocks
225          *                      NDADDR + NINDIR(fs) ..
226          *                      NDADDR + NINDIR(fs) + NINDIR(fs)**2 - 1
227          *
228          * di_ib[2]             index block 2 is the triple indirect block
229          *                      holds block numbers for double-indirect
230          *                      blocks for blocks
231          *                      NDADDR + NINDIR(fs) + NINDIR(fs)**2 ..
232          *                      NDADDR + NINDIR(fs) + NINDIR(fs)**2
233          *                              + NINDIR(fs)**3 - 1
234          */
235
236         if (file_block < NDADDR) {
237                 /* Direct block. */
238                 *disk_block_p = DIP(fp, di_db[file_block]);
239                 return (0);
240         }
241
242         file_block -= NDADDR;
243
244         /*
245          * nindir[0] = NINDIR
246          * nindir[1] = NINDIR**2
247          * nindir[2] = NINDIR**3
248          *      etc
249          */
250         for (level = 0; level < NIADDR; level++) {
251                 if (file_block < fp->f_nindir[level])
252                         break;
253                 file_block -= fp->f_nindir[level];
254         }
255         if (level == NIADDR) {
256                 /* Block number too high */
257                 return (EFBIG);
258         }
259
260         ind_block_num = DIP(fp, di_ib[level]);
261
262         for (; level >= 0; level--) {
263                 if (ind_block_num == 0) {
264                         *disk_block_p = 0;      /* missing */
265                         return (0);
266                 }
267
268                 if (fp->f_blkno[level] != ind_block_num) {
269                         if (fp->f_blk[level] == (char *)0)
270                                 fp->f_blk[level] =
271                                         malloc(fs->fs_bsize);
272                         twiddle();
273                         rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
274                                 fsbtodb(fp->f_fs, ind_block_num),
275                                 fs->fs_bsize,
276                                 fp->f_blk[level],
277                                 &fp->f_blksize[level]);
278                         if (rc)
279                                 return (rc);
280                         if (fp->f_blksize[level] != fs->fs_bsize)
281                                 return (EIO);
282                         fp->f_blkno[level] = ind_block_num;
283                 }
284
285                 if (level > 0) {
286                         idx = file_block / fp->f_nindir[level - 1];
287                         file_block %= fp->f_nindir[level - 1];
288                 } else
289                         idx = file_block;
290
291                 if (fp->f_fs->fs_magic == FS_UFS1_MAGIC)
292                         ind_block_num = ((ufs1_daddr_t *)fp->f_blk[level])[idx];
293                 else
294                         ind_block_num = ((ufs2_daddr_t *)fp->f_blk[level])[idx];
295         }
296
297         *disk_block_p = ind_block_num;
298
299         return (0);
300 }
301
302 /*
303  * Write a portion of a file from an internal buffer.
304  */
305 static int
306 buf_write_file(f, buf_p, size_p)
307         struct open_file *f;
308         char *buf_p;
309         size_t *size_p;         /* out */
310 {
311         struct file *fp = (struct file *)f->f_fsdata;
312         struct fs *fs = fp->f_fs;
313         long off;
314         ufs_lbn_t file_block;
315         ufs2_daddr_t disk_block;
316         size_t block_size;
317         int rc;
318
319         /*
320          * Calculate the starting block address and offset.
321          */
322         off = blkoff(fs, fp->f_seekp);
323         file_block = lblkno(fs, fp->f_seekp);
324         block_size = sblksize(fs, DIP(fp, di_size), file_block);
325
326         rc = block_map(f, file_block, &disk_block);
327         if (rc)
328                 return (rc);
329
330         if (disk_block == 0)
331                 /* Because we can't allocate space on the drive */
332                 return (EFBIG);
333
334         /*
335          * Truncate buffer at end of file, and at the end of
336          * this block.
337          */
338         if (*size_p > DIP(fp, di_size) - fp->f_seekp)
339                 *size_p = DIP(fp, di_size) - fp->f_seekp;
340         if (*size_p > block_size - off) 
341                 *size_p = block_size - off;
342
343         /*
344          * If we don't entirely occlude the block and it's not
345          * in memory already, read it in first.
346          */
347         if (((off > 0) || (*size_p + off < block_size)) &&
348             (file_block != fp->f_buf_blkno)) {
349
350                 if (fp->f_buf == (char *)0)
351                         fp->f_buf = malloc(fs->fs_bsize);
352
353                 twiddle();
354                 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
355                         fsbtodb(fs, disk_block),
356                         block_size, fp->f_buf, &fp->f_buf_size);
357                 if (rc)
358                         return (rc);
359
360                 fp->f_buf_blkno = file_block;
361         }
362
363         /*
364          *      Copy the user data into the cached block.
365          */
366         bcopy(buf_p, fp->f_buf + off, *size_p);
367
368         /*
369          *      Write the block out to storage.
370          */
371
372         twiddle();
373         rc = (f->f_dev->dv_strategy)(f->f_devdata, F_WRITE,
374                 fsbtodb(fs, disk_block),
375                 block_size, fp->f_buf, &fp->f_buf_size);
376         return (rc);
377 }
378
379 /*
380  * Read a portion of a file into an internal buffer.  Return
381  * the location in the buffer and the amount in the buffer.
382  */
383 static int
384 buf_read_file(f, buf_p, size_p)
385         struct open_file *f;
386         char **buf_p;           /* out */
387         size_t *size_p;         /* out */
388 {
389         struct file *fp = (struct file *)f->f_fsdata;
390         struct fs *fs = fp->f_fs;
391         long off;
392         ufs_lbn_t file_block;
393         ufs2_daddr_t disk_block;
394         size_t block_size;
395         int rc;
396
397         off = blkoff(fs, fp->f_seekp);
398         file_block = lblkno(fs, fp->f_seekp);
399         block_size = sblksize(fs, DIP(fp, di_size), file_block);
400
401         if (file_block != fp->f_buf_blkno) {
402                 if (fp->f_buf == (char *)0)
403                         fp->f_buf = malloc(fs->fs_bsize);
404
405                 rc = block_map(f, file_block, &disk_block);
406                 if (rc)
407                         return (rc);
408
409                 if (disk_block == 0) {
410                         bzero(fp->f_buf, block_size);
411                         fp->f_buf_size = block_size;
412                 } else {
413                         twiddle();
414                         rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
415                                 fsbtodb(fs, disk_block),
416                                 block_size, fp->f_buf, &fp->f_buf_size);
417                         if (rc)
418                                 return (rc);
419                 }
420
421                 fp->f_buf_blkno = file_block;
422         }
423
424         /*
425          * Return address of byte in buffer corresponding to
426          * offset, and size of remainder of buffer after that
427          * byte.
428          */
429         *buf_p = fp->f_buf + off;
430         *size_p = block_size - off;
431
432         /*
433          * But truncate buffer at end of file.
434          */
435         if (*size_p > DIP(fp, di_size) - fp->f_seekp)
436                 *size_p = DIP(fp, di_size) - fp->f_seekp;
437
438         return (0);
439 }
440
441 /*
442  * Search a directory for a name and return its
443  * i_number.
444  */
445 static int
446 search_directory(name, f, inumber_p)
447         char *name;
448         struct open_file *f;
449         ino_t *inumber_p;               /* out */
450 {
451         struct file *fp = (struct file *)f->f_fsdata;
452         struct direct *dp;
453         struct direct *edp;
454         char *buf;
455         size_t buf_size;
456         int namlen, length;
457         int rc;
458
459         length = strlen(name);
460
461         fp->f_seekp = 0;
462         while (fp->f_seekp < DIP(fp, di_size)) {
463                 rc = buf_read_file(f, &buf, &buf_size);
464                 if (rc)
465                         return (rc);
466
467                 dp = (struct direct *)buf;
468                 edp = (struct direct *)(buf + buf_size);
469                 while (dp < edp) {
470                         if (dp->d_ino == (ino_t)0)
471                                 goto next;
472 #if BYTE_ORDER == LITTLE_ENDIAN
473                         if (fp->f_fs->fs_maxsymlinklen <= 0)
474                                 namlen = dp->d_type;
475                         else
476 #endif
477                                 namlen = dp->d_namlen;
478                         if (namlen == length &&
479                             !strcmp(name, dp->d_name)) {
480                                 /* found entry */
481                                 *inumber_p = dp->d_ino;
482                                 return (0);
483                         }
484                 next:
485                         dp = (struct direct *)((char *)dp + dp->d_reclen);
486                 }
487                 fp->f_seekp += buf_size;
488         }
489         return (ENOENT);
490 }
491
492 static int sblock_try[] = SBLOCKSEARCH;
493
494 /*
495  * Open a file.
496  */
497 static int
498 ufs_open(upath, f)
499         const char *upath;
500         struct open_file *f;
501 {
502         char *cp, *ncp;
503         int c;
504         ino_t inumber, parent_inumber;
505         struct file *fp;
506         struct fs *fs;
507         int i, rc;
508         size_t buf_size;
509         int nlinks = 0;
510         char namebuf[MAXPATHLEN+1];
511         char *buf = NULL;
512         char *path = NULL;
513
514         /* allocate file system specific data structure */
515         fp = malloc(sizeof(struct file));
516         bzero(fp, sizeof(struct file));
517         f->f_fsdata = (void *)fp;
518
519         /* allocate space and read super block */
520         fs = malloc(SBLOCKSIZE);
521         fp->f_fs = fs;
522         twiddle();
523         /*
524          * Try reading the superblock in each of its possible locations.
525          */
526         for (i = 0; sblock_try[i] != -1; i++) {
527                 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
528                     sblock_try[i] / DEV_BSIZE, SBLOCKSIZE,
529                     (char *)fs, &buf_size);
530                 if (rc)
531                         goto out;
532                 if ((fs->fs_magic == FS_UFS1_MAGIC ||
533                      (fs->fs_magic == FS_UFS2_MAGIC &&
534                       fs->fs_sblockloc == sblock_try[i])) &&
535                     buf_size == SBLOCKSIZE &&
536                     fs->fs_bsize <= MAXBSIZE &&
537                     fs->fs_bsize >= sizeof(struct fs))
538                         break;
539         }
540         if (sblock_try[i] == -1) {
541                 rc = EINVAL;
542                 goto out;
543         }
544         /*
545          * Calculate indirect block levels.
546          */
547         {
548                 ufs2_daddr_t mult;
549                 int level;
550
551                 mult = 1;
552                 for (level = 0; level < NIADDR; level++) {
553                         mult *= NINDIR(fs);
554                         fp->f_nindir[level] = mult;
555                 }
556         }
557
558         inumber = ROOTINO;
559         if ((rc = read_inode(inumber, f)) != 0)
560                 goto out;
561
562         cp = path = strdup(upath);
563         if (path == NULL) {
564             rc = ENOMEM;
565             goto out;
566         }
567         while (*cp) {
568
569                 /*
570                  * Remove extra separators
571                  */
572                 while (*cp == '/')
573                         cp++;
574                 if (*cp == '\0')
575                         break;
576
577                 /*
578                  * Check that current node is a directory.
579                  */
580                 if ((DIP(fp, di_mode) & IFMT) != IFDIR) {
581                         rc = ENOTDIR;
582                         goto out;
583                 }
584
585                 /*
586                  * Get next component of path name.
587                  */
588                 {
589                         int len = 0;
590
591                         ncp = cp;
592                         while ((c = *cp) != '\0' && c != '/') {
593                                 if (++len > MAXNAMLEN) {
594                                         rc = ENOENT;
595                                         goto out;
596                                 }
597                                 cp++;
598                         }
599                         *cp = '\0';
600                 }
601
602                 /*
603                  * Look up component in current directory.
604                  * Save directory inumber in case we find a
605                  * symbolic link.
606                  */
607                 parent_inumber = inumber;
608                 rc = search_directory(ncp, f, &inumber);
609                 *cp = c;
610                 if (rc)
611                         goto out;
612
613                 /*
614                  * Open next component.
615                  */
616                 if ((rc = read_inode(inumber, f)) != 0)
617                         goto out;
618
619                 /*
620                  * Check for symbolic link.
621                  */
622                 if ((DIP(fp, di_mode) & IFMT) == IFLNK) {
623                         int link_len = DIP(fp, di_size);
624                         int len;
625
626                         len = strlen(cp);
627
628                         if (link_len + len > MAXPATHLEN ||
629                             ++nlinks > MAXSYMLINKS) {
630                                 rc = ENOENT;
631                                 goto out;
632                         }
633
634                         bcopy(cp, &namebuf[link_len], len + 1);
635
636                         if (link_len < fs->fs_maxsymlinklen) {
637                                 if (fp->f_fs->fs_magic == FS_UFS1_MAGIC)
638                                         cp = (caddr_t)(fp->f_di.di1.di_db);
639                                 else
640                                         cp = (caddr_t)(fp->f_di.di2.di_db);
641                                 bcopy(cp, namebuf, (unsigned) link_len);
642                         } else {
643                                 /*
644                                  * Read file for symbolic link
645                                  */
646                                 size_t buf_size;
647                                 ufs2_daddr_t disk_block;
648                                 struct fs *fs = fp->f_fs;
649
650                                 if (!buf)
651                                         buf = malloc(fs->fs_bsize);
652                                 rc = block_map(f, (ufs2_daddr_t)0, &disk_block);
653                                 if (rc)
654                                         goto out;
655                                 
656                                 twiddle();
657                                 rc = (f->f_dev->dv_strategy)(f->f_devdata,
658                                         F_READ, fsbtodb(fs, disk_block),
659                                         fs->fs_bsize, buf, &buf_size);
660                                 if (rc)
661                                         goto out;
662
663                                 bcopy((char *)buf, namebuf, (unsigned)link_len);
664                         }
665
666                         /*
667                          * If relative pathname, restart at parent directory.
668                          * If absolute pathname, restart at root.
669                          */
670                         cp = namebuf;
671                         if (*cp != '/')
672                                 inumber = parent_inumber;
673                         else
674                                 inumber = (ino_t)ROOTINO;
675
676                         if ((rc = read_inode(inumber, f)) != 0)
677                                 goto out;
678                 }
679         }
680
681         /*
682          * Found terminal component.
683          */
684         rc = 0;
685         fp->f_seekp = 0;
686 out:
687         if (buf)
688                 free(buf);
689         if (path)
690                 free(path);
691         if (rc) {
692                 if (fp->f_buf)
693                         free(fp->f_buf);
694                 free(fp->f_fs);
695                 free(fp);
696         }
697         return (rc);
698 }
699
700 static int
701 ufs_close(f)
702         struct open_file *f;
703 {
704         struct file *fp = (struct file *)f->f_fsdata;
705         int level;
706
707         f->f_fsdata = (void *)0;
708         if (fp == (struct file *)0)
709                 return (0);
710
711         for (level = 0; level < NIADDR; level++) {
712                 if (fp->f_blk[level])
713                         free(fp->f_blk[level]);
714         }
715         if (fp->f_buf)
716                 free(fp->f_buf);
717         free(fp->f_fs);
718         free(fp);
719         return (0);
720 }
721
722 /*
723  * Copy a portion of a file into kernel memory.
724  * Cross block boundaries when necessary.
725  */
726 static int
727 ufs_read(f, start, size, resid)
728         struct open_file *f;
729         void *start;
730         size_t size;
731         size_t *resid;  /* out */
732 {
733         struct file *fp = (struct file *)f->f_fsdata;
734         size_t csize;
735         char *buf;
736         size_t buf_size;
737         int rc = 0;
738         char *addr = start;
739
740         while (size != 0) {
741                 if (fp->f_seekp >= DIP(fp, di_size))
742                         break;
743
744                 rc = buf_read_file(f, &buf, &buf_size);
745                 if (rc)
746                         break;
747
748                 csize = size;
749                 if (csize > buf_size)
750                         csize = buf_size;
751
752                 bcopy(buf, addr, csize);
753
754                 fp->f_seekp += csize;
755                 addr += csize;
756                 size -= csize;
757         }
758         if (resid)
759                 *resid = size;
760         return (rc);
761 }
762
763 /*
764  * Write to a portion of an already allocated file.
765  * Cross block boundaries when necessary. Can not
766  * extend the file.
767  */
768 static int
769 ufs_write(f, start, size, resid)
770         struct open_file *f;
771         void *start;
772         size_t size;
773         size_t *resid;  /* out */
774 {
775         struct file *fp = (struct file *)f->f_fsdata;
776         size_t csize;
777         int rc = 0;
778         char *addr = start;
779
780         csize = size;
781         while ((size != 0) && (csize != 0)) {
782                 if (fp->f_seekp >= DIP(fp, di_size))
783                         break;
784
785                 if (csize >= 512) csize = 512; /* XXX */
786
787                 rc = buf_write_file(f, addr, &csize);
788                 if (rc)
789                         break;
790
791                 fp->f_seekp += csize;
792                 addr += csize;
793                 size -= csize;
794         }
795         if (resid)
796                 *resid = size;
797         return (rc);
798 }
799
800 static off_t
801 ufs_seek(f, offset, where)
802         struct open_file *f;
803         off_t offset;
804         int where;
805 {
806         struct file *fp = (struct file *)f->f_fsdata;
807
808         switch (where) {
809         case SEEK_SET:
810                 fp->f_seekp = offset;
811                 break;
812         case SEEK_CUR:
813                 fp->f_seekp += offset;
814                 break;
815         case SEEK_END:
816                 fp->f_seekp = DIP(fp, di_size) - offset;
817                 break;
818         default:
819                 errno = EINVAL;
820                 return (-1);
821         }
822         return (fp->f_seekp);
823 }
824
825 static int
826 ufs_stat(f, sb)
827         struct open_file *f;
828         struct stat *sb;
829 {
830         struct file *fp = (struct file *)f->f_fsdata;
831
832         /* only important stuff */
833         sb->st_mode = DIP(fp, di_mode);
834         sb->st_uid = DIP(fp, di_uid);
835         sb->st_gid = DIP(fp, di_gid);
836         sb->st_size = DIP(fp, di_size);
837         return (0);
838 }
839
840 static int
841 ufs_readdir(struct open_file *f, struct dirent *d)
842 {
843         struct file *fp = (struct file *)f->f_fsdata;
844         struct direct *dp;
845         char *buf;
846         size_t buf_size;
847         int error;
848
849         /*
850          * assume that a directory entry will not be split across blocks
851          */
852 again:
853         if (fp->f_seekp >= DIP(fp, di_size))
854                 return (ENOENT);
855         error = buf_read_file(f, &buf, &buf_size);
856         if (error)
857                 return (error);
858         dp = (struct direct *)buf;
859         fp->f_seekp += dp->d_reclen;
860         if (dp->d_ino == (ino_t)0)
861                 goto again;
862         d->d_type = dp->d_type;
863         strcpy(d->d_name, dp->d_name);
864         return (0);
865 }