]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - stand/libsa/ufs.c
Upgrade Unbound to 1.6.0. More to follow.
[FreeBSD/FreeBSD.git] / stand / libsa / 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  * 3. 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 static int      ufs_open(const char *path, struct open_file *f);
87 static int      ufs_write(struct open_file *f, const void *buf, size_t size,
88                 size_t *resid);
89 static int      ufs_close(struct open_file *f);
90 static int      ufs_read(struct open_file *f, void *buf, size_t size, size_t *resid);
91 static off_t    ufs_seek(struct open_file *f, off_t offset, int where);
92 static int      ufs_stat(struct open_file *f, struct stat *sb);
93 static int      ufs_readdir(struct open_file *f, struct dirent *d);
94
95 struct fs_ops ufs_fsops = {
96         "ufs",
97         ufs_open,
98         ufs_close,
99         ufs_read,
100         ufs_write,
101         ufs_seek,
102         ufs_stat,
103         ufs_readdir
104 };
105
106 /*
107  * In-core open file.
108  */
109 struct file {
110         off_t           f_seekp;        /* seek pointer */
111         struct fs       *f_fs;          /* pointer to super-block */
112         union dinode {
113                 struct ufs1_dinode di1;
114                 struct ufs2_dinode di2;
115         }               f_di;           /* copy of on-disk inode */
116         int             f_nindir[UFS_NIADDR];
117                                         /* number of blocks mapped by
118                                            indirect block at level i */
119         char            *f_blk[UFS_NIADDR];     /* buffer for indirect block at
120                                            level i */
121         size_t          f_blksize[UFS_NIADDR];
122                                         /* size of buffer */
123         ufs2_daddr_t    f_blkno[UFS_NIADDR];/* disk address of block in buffer */
124         ufs2_daddr_t    f_buf_blkno;    /* block number of data block */
125         char            *f_buf;         /* buffer for data block */
126         size_t          f_buf_size;     /* size of data block */
127 };
128 #define DIP(fp, field) \
129         ((fp)->f_fs->fs_magic == FS_UFS1_MAGIC ? \
130         (fp)->f_di.di1.field : (fp)->f_di.di2.field)
131
132 static int      read_inode(ino_t, struct open_file *);
133 static int      block_map(struct open_file *, ufs2_daddr_t, ufs2_daddr_t *);
134 static int      buf_read_file(struct open_file *, char **, size_t *);
135 static int      buf_write_file(struct open_file *, const char *, size_t *);
136 static int      search_directory(char *, struct open_file *, ino_t *);
137 static int      ufs_use_sa_read(void *, off_t, void **, int);
138
139 /* from ffs_subr.c */
140 int     ffs_sbget(void *, struct fs **, off_t, char *,
141             int (*)(void *, off_t, void **, int));
142
143 /*
144  * Read a new inode into a file structure.
145  */
146 static int
147 read_inode(inumber, f)
148         ino_t inumber;
149         struct open_file *f;
150 {
151         struct file *fp = (struct file *)f->f_fsdata;
152         struct fs *fs = fp->f_fs;
153         char *buf;
154         size_t rsize;
155         int rc;
156
157         if (fs == NULL)
158             panic("fs == NULL");
159
160         /*
161          * Read inode and save it.
162          */
163         buf = malloc(fs->fs_bsize);
164         twiddle(1);
165         rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
166                 fsbtodb(fs, ino_to_fsba(fs, inumber)), fs->fs_bsize,
167                 buf, &rsize);
168         if (rc)
169                 goto out;
170         if (rsize != fs->fs_bsize) {
171                 rc = EIO;
172                 goto out;
173         }
174
175         if (fp->f_fs->fs_magic == FS_UFS1_MAGIC)
176                 fp->f_di.di1 = ((struct ufs1_dinode *)buf)
177                     [ino_to_fsbo(fs, inumber)];
178         else
179                 fp->f_di.di2 = ((struct ufs2_dinode *)buf)
180                     [ino_to_fsbo(fs, inumber)];
181
182         /*
183          * Clear out the old buffers
184          */
185         {
186                 int level;
187
188                 for (level = 0; level < UFS_NIADDR; level++)
189                         fp->f_blkno[level] = -1;
190                 fp->f_buf_blkno = -1;
191         }
192         fp->f_seekp = 0;
193 out:
194         free(buf);
195         return (rc);     
196 }
197
198 /*
199  * Given an offset in a file, find the disk block number that
200  * contains that block.
201  */
202 static int
203 block_map(f, file_block, disk_block_p)
204         struct open_file *f;
205         ufs2_daddr_t file_block;
206         ufs2_daddr_t *disk_block_p;     /* out */
207 {
208         struct file *fp = (struct file *)f->f_fsdata;
209         struct fs *fs = fp->f_fs;
210         int level;
211         int idx;
212         ufs2_daddr_t ind_block_num;
213         int rc;
214
215         /*
216          * Index structure of an inode:
217          *
218          * di_db[0..UFS_NDADDR-1] hold block numbers for blocks
219          *                      0..UFS_NDADDR-1
220          *
221          * di_ib[0]             index block 0 is the single indirect block
222          *                      holds block numbers for blocks
223          *                      UFS_NDADDR .. UFS_NDADDR + NINDIR(fs)-1
224          *
225          * di_ib[1]             index block 1 is the double indirect block
226          *                      holds block numbers for INDEX blocks for blocks
227          *                      UFS_NDADDR + NINDIR(fs) ..
228          *                      UFS_NDADDR + NINDIR(fs) + NINDIR(fs)**2 - 1
229          *
230          * di_ib[2]             index block 2 is the triple indirect block
231          *                      holds block numbers for double-indirect
232          *                      blocks for blocks
233          *                      UFS_NDADDR + NINDIR(fs) + NINDIR(fs)**2 ..
234          *                      UFS_NDADDR + NINDIR(fs) + NINDIR(fs)**2
235          *                              + NINDIR(fs)**3 - 1
236          */
237
238         if (file_block < UFS_NDADDR) {
239                 /* Direct block. */
240                 *disk_block_p = DIP(fp, di_db[file_block]);
241                 return (0);
242         }
243
244         file_block -= UFS_NDADDR;
245
246         /*
247          * nindir[0] = NINDIR
248          * nindir[1] = NINDIR**2
249          * nindir[2] = NINDIR**3
250          *      etc
251          */
252         for (level = 0; level < UFS_NIADDR; level++) {
253                 if (file_block < fp->f_nindir[level])
254                         break;
255                 file_block -= fp->f_nindir[level];
256         }
257         if (level == UFS_NIADDR) {
258                 /* Block number too high */
259                 return (EFBIG);
260         }
261
262         ind_block_num = DIP(fp, di_ib[level]);
263
264         for (; level >= 0; level--) {
265                 if (ind_block_num == 0) {
266                         *disk_block_p = 0;      /* missing */
267                         return (0);
268                 }
269
270                 if (fp->f_blkno[level] != ind_block_num) {
271                         if (fp->f_blk[level] == (char *)0)
272                                 fp->f_blk[level] =
273                                         malloc(fs->fs_bsize);
274                         twiddle(1);
275                         rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
276                                 fsbtodb(fp->f_fs, ind_block_num),
277                                 fs->fs_bsize,
278                                 fp->f_blk[level],
279                                 &fp->f_blksize[level]);
280                         if (rc)
281                                 return (rc);
282                         if (fp->f_blksize[level] != fs->fs_bsize)
283                                 return (EIO);
284                         fp->f_blkno[level] = ind_block_num;
285                 }
286
287                 if (level > 0) {
288                         idx = file_block / fp->f_nindir[level - 1];
289                         file_block %= fp->f_nindir[level - 1];
290                 } else
291                         idx = file_block;
292
293                 if (fp->f_fs->fs_magic == FS_UFS1_MAGIC)
294                         ind_block_num = ((ufs1_daddr_t *)fp->f_blk[level])[idx];
295                 else
296                         ind_block_num = ((ufs2_daddr_t *)fp->f_blk[level])[idx];
297         }
298
299         *disk_block_p = ind_block_num;
300
301         return (0);
302 }
303
304 /*
305  * Write a portion of a file from an internal buffer.
306  */
307 static int
308 buf_write_file(f, buf_p, size_p)
309         struct open_file *f;
310         const char *buf_p;
311         size_t *size_p;         /* out */
312 {
313         struct file *fp = (struct file *)f->f_fsdata;
314         struct fs *fs = fp->f_fs;
315         long off;
316         ufs_lbn_t file_block;
317         ufs2_daddr_t disk_block;
318         size_t block_size;
319         int rc;
320
321         /*
322          * Calculate the starting block address and offset.
323          */
324         off = blkoff(fs, fp->f_seekp);
325         file_block = lblkno(fs, fp->f_seekp);
326         block_size = sblksize(fs, DIP(fp, di_size), file_block);
327
328         rc = block_map(f, file_block, &disk_block);
329         if (rc)
330                 return (rc);
331
332         if (disk_block == 0)
333                 /* Because we can't allocate space on the drive */
334                 return (EFBIG);
335
336         /*
337          * Truncate buffer at end of file, and at the end of
338          * this block.
339          */
340         if (*size_p > DIP(fp, di_size) - fp->f_seekp)
341                 *size_p = DIP(fp, di_size) - fp->f_seekp;
342         if (*size_p > block_size - off) 
343                 *size_p = block_size - off;
344
345         /*
346          * If we don't entirely occlude the block and it's not
347          * in memory already, read it in first.
348          */
349         if (((off > 0) || (*size_p + off < block_size)) &&
350             (file_block != fp->f_buf_blkno)) {
351
352                 if (fp->f_buf == (char *)0)
353                         fp->f_buf = malloc(fs->fs_bsize);
354
355                 twiddle(4);
356                 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
357                         fsbtodb(fs, disk_block),
358                         block_size, fp->f_buf, &fp->f_buf_size);
359                 if (rc)
360                         return (rc);
361
362                 fp->f_buf_blkno = file_block;
363         }
364
365         /*
366          *      Copy the user data into the cached block.
367          */
368         bcopy(buf_p, fp->f_buf + off, *size_p);
369
370         /*
371          *      Write the block out to storage.
372          */
373
374         twiddle(4);
375         rc = (f->f_dev->dv_strategy)(f->f_devdata, F_WRITE,
376                 fsbtodb(fs, disk_block),
377                 block_size, fp->f_buf, &fp->f_buf_size);
378         return (rc);
379 }
380
381 /*
382  * Read a portion of a file into an internal buffer.  Return
383  * the location in the buffer and the amount in the buffer.
384  */
385 static int
386 buf_read_file(f, buf_p, size_p)
387         struct open_file *f;
388         char **buf_p;           /* out */
389         size_t *size_p;         /* out */
390 {
391         struct file *fp = (struct file *)f->f_fsdata;
392         struct fs *fs = fp->f_fs;
393         long off;
394         ufs_lbn_t file_block;
395         ufs2_daddr_t disk_block;
396         size_t block_size;
397         int rc;
398
399         off = blkoff(fs, fp->f_seekp);
400         file_block = lblkno(fs, fp->f_seekp);
401         block_size = sblksize(fs, DIP(fp, di_size), file_block);
402
403         if (file_block != fp->f_buf_blkno) {
404                 if (fp->f_buf == (char *)0)
405                         fp->f_buf = malloc(fs->fs_bsize);
406
407                 rc = block_map(f, file_block, &disk_block);
408                 if (rc)
409                         return (rc);
410
411                 if (disk_block == 0) {
412                         bzero(fp->f_buf, block_size);
413                         fp->f_buf_size = block_size;
414                 } else {
415                         twiddle(4);
416                         rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
417                                 fsbtodb(fs, disk_block),
418                                 block_size, fp->f_buf, &fp->f_buf_size);
419                         if (rc)
420                                 return (rc);
421                 }
422
423                 fp->f_buf_blkno = file_block;
424         }
425
426         /*
427          * Return address of byte in buffer corresponding to
428          * offset, and size of remainder of buffer after that
429          * byte.
430          */
431         *buf_p = fp->f_buf + off;
432         *size_p = block_size - off;
433
434         /*
435          * But truncate buffer at end of file.
436          */
437         if (*size_p > DIP(fp, di_size) - fp->f_seekp)
438                 *size_p = DIP(fp, di_size) - fp->f_seekp;
439
440         return (0);
441 }
442
443 /*
444  * Search a directory for a name and return its
445  * i_number.
446  */
447 static int
448 search_directory(name, f, inumber_p)
449         char *name;
450         struct open_file *f;
451         ino_t *inumber_p;               /* out */
452 {
453         struct file *fp = (struct file *)f->f_fsdata;
454         struct direct *dp;
455         struct direct *edp;
456         char *buf;
457         size_t buf_size;
458         int namlen, length;
459         int rc;
460
461         length = strlen(name);
462
463         fp->f_seekp = 0;
464         while (fp->f_seekp < DIP(fp, di_size)) {
465                 rc = buf_read_file(f, &buf, &buf_size);
466                 if (rc)
467                         return (rc);
468
469                 dp = (struct direct *)buf;
470                 edp = (struct direct *)(buf + buf_size);
471                 while (dp < edp) {
472                         if (dp->d_ino == (ino_t)0)
473                                 goto next;
474 #if BYTE_ORDER == LITTLE_ENDIAN
475                         if (fp->f_fs->fs_maxsymlinklen <= 0)
476                                 namlen = dp->d_type;
477                         else
478 #endif
479                                 namlen = dp->d_namlen;
480                         if (namlen == length &&
481                             !strcmp(name, dp->d_name)) {
482                                 /* found entry */
483                                 *inumber_p = dp->d_ino;
484                                 return (0);
485                         }
486                 next:
487                         dp = (struct direct *)((char *)dp + dp->d_reclen);
488                 }
489                 fp->f_seekp += buf_size;
490         }
491         return (ENOENT);
492 }
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         /* read super block */
520         twiddle(1);
521         if ((rc = ffs_sbget(f, &fs, -1, "stand", ufs_use_sa_read)) != 0)
522                 goto out;
523         fp->f_fs = fs;
524         /*
525          * Calculate indirect block levels.
526          */
527         {
528                 ufs2_daddr_t mult;
529                 int level;
530
531                 mult = 1;
532                 for (level = 0; level < UFS_NIADDR; level++) {
533                         mult *= NINDIR(fs);
534                         fp->f_nindir[level] = mult;
535                 }
536         }
537
538         inumber = UFS_ROOTINO;
539         if ((rc = read_inode(inumber, f)) != 0)
540                 goto out;
541
542         cp = path = strdup(upath);
543         if (path == NULL) {
544             rc = ENOMEM;
545             goto out;
546         }
547         while (*cp) {
548
549                 /*
550                  * Remove extra separators
551                  */
552                 while (*cp == '/')
553                         cp++;
554                 if (*cp == '\0')
555                         break;
556
557                 /*
558                  * Check that current node is a directory.
559                  */
560                 if ((DIP(fp, di_mode) & IFMT) != IFDIR) {
561                         rc = ENOTDIR;
562                         goto out;
563                 }
564
565                 /*
566                  * Get next component of path name.
567                  */
568                 {
569                         int len = 0;
570
571                         ncp = cp;
572                         while ((c = *cp) != '\0' && c != '/') {
573                                 if (++len > UFS_MAXNAMLEN) {
574                                         rc = ENOENT;
575                                         goto out;
576                                 }
577                                 cp++;
578                         }
579                         *cp = '\0';
580                 }
581
582                 /*
583                  * Look up component in current directory.
584                  * Save directory inumber in case we find a
585                  * symbolic link.
586                  */
587                 parent_inumber = inumber;
588                 rc = search_directory(ncp, f, &inumber);
589                 *cp = c;
590                 if (rc)
591                         goto out;
592
593                 /*
594                  * Open next component.
595                  */
596                 if ((rc = read_inode(inumber, f)) != 0)
597                         goto out;
598
599                 /*
600                  * Check for symbolic link.
601                  */
602                 if ((DIP(fp, di_mode) & IFMT) == IFLNK) {
603                         int link_len = DIP(fp, di_size);
604                         int len;
605
606                         len = strlen(cp);
607
608                         if (link_len + len > MAXPATHLEN ||
609                             ++nlinks > MAXSYMLINKS) {
610                                 rc = ENOENT;
611                                 goto out;
612                         }
613
614                         bcopy(cp, &namebuf[link_len], len + 1);
615
616                         if (link_len < fs->fs_maxsymlinklen) {
617                                 if (fp->f_fs->fs_magic == FS_UFS1_MAGIC)
618                                         cp = (caddr_t)(fp->f_di.di1.di_db);
619                                 else
620                                         cp = (caddr_t)(fp->f_di.di2.di_db);
621                                 bcopy(cp, namebuf, (unsigned) link_len);
622                         } else {
623                                 /*
624                                  * Read file for symbolic link
625                                  */
626                                 size_t buf_size;
627                                 ufs2_daddr_t disk_block;
628                                 struct fs *fs = fp->f_fs;
629
630                                 if (!buf)
631                                         buf = malloc(fs->fs_bsize);
632                                 rc = block_map(f, (ufs2_daddr_t)0, &disk_block);
633                                 if (rc)
634                                         goto out;
635                                 
636                                 twiddle(1);
637                                 rc = (f->f_dev->dv_strategy)(f->f_devdata,
638                                         F_READ, fsbtodb(fs, disk_block),
639                                         fs->fs_bsize, buf, &buf_size);
640                                 if (rc)
641                                         goto out;
642
643                                 bcopy((char *)buf, namebuf, (unsigned)link_len);
644                         }
645
646                         /*
647                          * If relative pathname, restart at parent directory.
648                          * If absolute pathname, restart at root.
649                          */
650                         cp = namebuf;
651                         if (*cp != '/')
652                                 inumber = parent_inumber;
653                         else
654                                 inumber = (ino_t)UFS_ROOTINO;
655
656                         if ((rc = read_inode(inumber, f)) != 0)
657                                 goto out;
658                 }
659         }
660
661         /*
662          * Found terminal component.
663          */
664         rc = 0;
665         fp->f_seekp = 0;
666 out:
667         if (buf)
668                 free(buf);
669         if (path)
670                 free(path);
671         if (rc) {
672                 if (fp->f_buf)
673                         free(fp->f_buf);
674                 free(fp->f_fs);
675                 free(fp);
676         }
677         return (rc);
678 }
679
680 /*
681  * A read function for use by standalone-layer routines.
682  */
683 static int
684 ufs_use_sa_read(void *devfd, off_t loc, void **bufp, int size)
685 {
686         struct open_file *f;
687         size_t buf_size;
688         int error;
689
690         f = (struct open_file *)devfd;
691         if ((*bufp = malloc(size)) == NULL)
692                 return (ENOSPC);
693         error = (f->f_dev->dv_strategy)(f->f_devdata, F_READ, loc / DEV_BSIZE,
694             size, *bufp, &buf_size);
695         if (error != 0)
696                 return (error);
697         if (buf_size != size)
698                 return (EIO);
699         return (0);
700 }
701
702 static int
703 ufs_close(f)
704         struct open_file *f;
705 {
706         struct file *fp = (struct file *)f->f_fsdata;
707         int level;
708
709         f->f_fsdata = (void *)0;
710         if (fp == (struct file *)0)
711                 return (0);
712
713         for (level = 0; level < UFS_NIADDR; level++) {
714                 if (fp->f_blk[level])
715                         free(fp->f_blk[level]);
716         }
717         if (fp->f_buf)
718                 free(fp->f_buf);
719         free(fp->f_fs);
720         free(fp);
721         return (0);
722 }
723
724 /*
725  * Copy a portion of a file into kernel memory.
726  * Cross block boundaries when necessary.
727  */
728 static int
729 ufs_read(f, start, size, resid)
730         struct open_file *f;
731         void *start;
732         size_t size;
733         size_t *resid;  /* out */
734 {
735         struct file *fp = (struct file *)f->f_fsdata;
736         size_t csize;
737         char *buf;
738         size_t buf_size;
739         int rc = 0;
740         char *addr = start;
741
742         while (size != 0) {
743                 if (fp->f_seekp >= DIP(fp, di_size))
744                         break;
745
746                 rc = buf_read_file(f, &buf, &buf_size);
747                 if (rc)
748                         break;
749
750                 csize = size;
751                 if (csize > buf_size)
752                         csize = buf_size;
753
754                 bcopy(buf, addr, csize);
755
756                 fp->f_seekp += csize;
757                 addr += csize;
758                 size -= csize;
759         }
760         if (resid)
761                 *resid = size;
762         return (rc);
763 }
764
765 /*
766  * Write to a portion of an already allocated file.
767  * Cross block boundaries when necessary. Can not
768  * extend the file.
769  */
770 static int
771 ufs_write(f, start, size, resid)
772         struct open_file *f;
773         const void *start;
774         size_t size;
775         size_t *resid;  /* out */
776 {
777         struct file *fp = (struct file *)f->f_fsdata;
778         size_t csize;
779         int rc = 0;
780         const char *addr = start;
781
782         csize = size;
783         while ((size != 0) && (csize != 0)) {
784                 if (fp->f_seekp >= DIP(fp, di_size))
785                         break;
786
787                 if (csize >= 512) csize = 512; /* XXX */
788
789                 rc = buf_write_file(f, addr, &csize);
790                 if (rc)
791                         break;
792
793                 fp->f_seekp += csize;
794                 addr += csize;
795                 size -= csize;
796         }
797         if (resid)
798                 *resid = size;
799         return (rc);
800 }
801
802 static off_t
803 ufs_seek(f, offset, where)
804         struct open_file *f;
805         off_t offset;
806         int where;
807 {
808         struct file *fp = (struct file *)f->f_fsdata;
809
810         switch (where) {
811         case SEEK_SET:
812                 fp->f_seekp = offset;
813                 break;
814         case SEEK_CUR:
815                 fp->f_seekp += offset;
816                 break;
817         case SEEK_END:
818                 fp->f_seekp = DIP(fp, di_size) - offset;
819                 break;
820         default:
821                 errno = EINVAL;
822                 return (-1);
823         }
824         return (fp->f_seekp);
825 }
826
827 static int
828 ufs_stat(f, sb)
829         struct open_file *f;
830         struct stat *sb;
831 {
832         struct file *fp = (struct file *)f->f_fsdata;
833
834         /* only important stuff */
835         sb->st_mode = DIP(fp, di_mode);
836         sb->st_uid = DIP(fp, di_uid);
837         sb->st_gid = DIP(fp, di_gid);
838         sb->st_size = DIP(fp, di_size);
839         return (0);
840 }
841
842 static int
843 ufs_readdir(struct open_file *f, struct dirent *d)
844 {
845         struct file *fp = (struct file *)f->f_fsdata;
846         struct direct *dp;
847         char *buf;
848         size_t buf_size;
849         int error;
850
851         /*
852          * assume that a directory entry will not be split across blocks
853          */
854 again:
855         if (fp->f_seekp >= DIP(fp, di_size))
856                 return (ENOENT);
857         error = buf_read_file(f, &buf, &buf_size);
858         if (error)
859                 return (error);
860         dp = (struct direct *)buf;
861         fp->f_seekp += dp->d_reclen;
862         if (dp->d_ino == (ino_t)0)
863                 goto again;
864         d->d_type = dp->d_type;
865         strcpy(d->d_name, dp->d_name);
866         return (0);
867 }