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