]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libstand/ufs.c
This commit was generated by cvs2svn to compensate for changes in r99160,
[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 out:
195         free(buf);
196         return (rc);     
197 }
198
199 /*
200  * Given an offset in a file, find the disk block number that
201  * contains that block.
202  */
203 static int
204 block_map(f, file_block, disk_block_p)
205         struct open_file *f;
206         ufs2_daddr_t file_block;
207         ufs2_daddr_t *disk_block_p;     /* out */
208 {
209         struct file *fp = (struct file *)f->f_fsdata;
210         struct fs *fs = fp->f_fs;
211         int level;
212         int idx;
213         ufs2_daddr_t ind_block_num;
214         int rc;
215
216         /*
217          * Index structure of an inode:
218          *
219          * di_db[0..NDADDR-1]   hold block numbers for blocks
220          *                      0..NDADDR-1
221          *
222          * di_ib[0]             index block 0 is the single indirect block
223          *                      holds block numbers for blocks
224          *                      NDADDR .. NDADDR + NINDIR(fs)-1
225          *
226          * di_ib[1]             index block 1 is the double indirect block
227          *                      holds block numbers for INDEX blocks for blocks
228          *                      NDADDR + NINDIR(fs) ..
229          *                      NDADDR + NINDIR(fs) + NINDIR(fs)**2 - 1
230          *
231          * di_ib[2]             index block 2 is the triple indirect block
232          *                      holds block numbers for double-indirect
233          *                      blocks for blocks
234          *                      NDADDR + NINDIR(fs) + NINDIR(fs)**2 ..
235          *                      NDADDR + NINDIR(fs) + NINDIR(fs)**2
236          *                              + NINDIR(fs)**3 - 1
237          */
238
239         if (file_block < NDADDR) {
240                 /* Direct block. */
241                 *disk_block_p = DIP(fp, di_db[file_block]);
242                 return (0);
243         }
244
245         file_block -= NDADDR;
246
247         /*
248          * nindir[0] = NINDIR
249          * nindir[1] = NINDIR**2
250          * nindir[2] = NINDIR**3
251          *      etc
252          */
253         for (level = 0; level < NIADDR; level++) {
254                 if (file_block < fp->f_nindir[level])
255                         break;
256                 file_block -= fp->f_nindir[level];
257         }
258         if (level == NIADDR) {
259                 /* Block number too high */
260                 return (EFBIG);
261         }
262
263         ind_block_num = DIP(fp, di_ib[level]);
264
265         for (; level >= 0; level--) {
266                 if (ind_block_num == 0) {
267                         *disk_block_p = 0;      /* missing */
268                         return (0);
269                 }
270
271                 if (fp->f_blkno[level] != ind_block_num) {
272                         if (fp->f_blk[level] == (char *)0)
273                                 fp->f_blk[level] =
274                                         malloc(fs->fs_bsize);
275                         twiddle();
276                         rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
277                                 fsbtodb(fp->f_fs, ind_block_num),
278                                 fs->fs_bsize,
279                                 fp->f_blk[level],
280                                 &fp->f_blksize[level]);
281                         if (rc)
282                                 return (rc);
283                         if (fp->f_blksize[level] != fs->fs_bsize)
284                                 return (EIO);
285                         fp->f_blkno[level] = ind_block_num;
286                 }
287
288                 if (level > 0) {
289                         idx = file_block / fp->f_nindir[level - 1];
290                         file_block %= fp->f_nindir[level - 1];
291                 } else
292                         idx = file_block;
293
294                 if (fp->f_fs->fs_magic == FS_UFS1_MAGIC)
295                         ind_block_num = ((ufs1_daddr_t *)fp->f_blk[level])[idx];
296                 else
297                         ind_block_num = ((ufs2_daddr_t *)fp->f_blk[level])[idx];
298         }
299
300         *disk_block_p = ind_block_num;
301
302         return (0);
303 }
304
305 /*
306  * Write a portion of a file from an internal buffer.
307  */
308 static int
309 buf_write_file(f, buf_p, size_p)
310         struct open_file *f;
311         char *buf_p;
312         size_t *size_p;         /* out */
313 {
314         struct file *fp = (struct file *)f->f_fsdata;
315         struct fs *fs = fp->f_fs;
316         long off;
317         ufs_lbn_t file_block;
318         ufs2_daddr_t disk_block;
319         size_t block_size;
320         int rc;
321
322         /*
323          * Calculate the starting block address and offset.
324          */
325         off = blkoff(fs, fp->f_seekp);
326         file_block = lblkno(fs, fp->f_seekp);
327         block_size = sblksize(fs, DIP(fp, di_size), file_block);
328
329         rc = block_map(f, file_block, &disk_block);
330         if (rc)
331                 return (rc);
332
333         if (disk_block == 0)
334                 /* Because we can't allocate space on the drive */
335                 return (EFBIG);
336
337         /*
338          * Truncate buffer at end of file, and at the end of
339          * this block.
340          */
341         if (*size_p > DIP(fp, di_size) - fp->f_seekp)
342                 *size_p = DIP(fp, di_size) - fp->f_seekp;
343         if (*size_p > block_size - off) 
344                 *size_p = block_size - off;
345
346         /*
347          * If we don't entirely occlude the block and it's not
348          * in memory already, read it in first.
349          */
350         if (((off > 0) || (*size_p + off < block_size)) &&
351             (file_block != fp->f_buf_blkno)) {
352
353                 if (fp->f_buf == (char *)0)
354                         fp->f_buf = malloc(fs->fs_bsize);
355
356                 twiddle();
357                 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
358                         fsbtodb(fs, disk_block),
359                         block_size, fp->f_buf, &fp->f_buf_size);
360                 if (rc)
361                         return (rc);
362
363                 fp->f_buf_blkno = file_block;
364         }
365
366         /*
367          *      Copy the user data into the cached block.
368          */
369         bcopy(buf_p, fp->f_buf + off, *size_p);
370
371         /*
372          *      Write the block out to storage.
373          */
374
375         twiddle();
376         rc = (f->f_dev->dv_strategy)(f->f_devdata, F_WRITE,
377                 fsbtodb(fs, disk_block),
378                 block_size, fp->f_buf, &fp->f_buf_size);
379         return (rc);
380 }
381
382 /*
383  * Read a portion of a file into an internal buffer.  Return
384  * the location in the buffer and the amount in the buffer.
385  */
386 static int
387 buf_read_file(f, buf_p, size_p)
388         struct open_file *f;
389         char **buf_p;           /* out */
390         size_t *size_p;         /* out */
391 {
392         struct file *fp = (struct file *)f->f_fsdata;
393         struct fs *fs = fp->f_fs;
394         long off;
395         ufs_lbn_t file_block;
396         ufs2_daddr_t disk_block;
397         size_t block_size;
398         int rc;
399
400         off = blkoff(fs, fp->f_seekp);
401         file_block = lblkno(fs, fp->f_seekp);
402         block_size = sblksize(fs, DIP(fp, di_size), file_block);
403
404         if (file_block != fp->f_buf_blkno) {
405                 if (fp->f_buf == (char *)0)
406                         fp->f_buf = malloc(fs->fs_bsize);
407
408                 rc = block_map(f, file_block, &disk_block);
409                 if (rc)
410                         return (rc);
411
412                 if (disk_block == 0) {
413                         bzero(fp->f_buf, block_size);
414                         fp->f_buf_size = block_size;
415                 } else {
416                         twiddle();
417                         rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
418                                 fsbtodb(fs, disk_block),
419                                 block_size, fp->f_buf, &fp->f_buf_size);
420                         if (rc)
421                                 return (rc);
422                 }
423
424                 fp->f_buf_blkno = file_block;
425         }
426
427         /*
428          * Return address of byte in buffer corresponding to
429          * offset, and size of remainder of buffer after that
430          * byte.
431          */
432         *buf_p = fp->f_buf + off;
433         *size_p = block_size - off;
434
435         /*
436          * But truncate buffer at end of file.
437          */
438         if (*size_p > DIP(fp, di_size) - fp->f_seekp)
439                 *size_p = DIP(fp, di_size) - fp->f_seekp;
440
441         return (0);
442 }
443
444 /*
445  * Search a directory for a name and return its
446  * i_number.
447  */
448 static int
449 search_directory(name, f, inumber_p)
450         char *name;
451         struct open_file *f;
452         ino_t *inumber_p;               /* out */
453 {
454         struct file *fp = (struct file *)f->f_fsdata;
455         struct direct *dp;
456         struct direct *edp;
457         char *buf;
458         size_t buf_size;
459         int namlen, length;
460         int rc;
461
462         length = strlen(name);
463
464         fp->f_seekp = 0;
465         while (fp->f_seekp < DIP(fp, di_size)) {
466                 rc = buf_read_file(f, &buf, &buf_size);
467                 if (rc)
468                         return (rc);
469
470                 dp = (struct direct *)buf;
471                 edp = (struct direct *)(buf + buf_size);
472                 while (dp < edp) {
473                         if (dp->d_ino == (ino_t)0)
474                                 goto next;
475 #if BYTE_ORDER == LITTLE_ENDIAN
476                         if (fp->f_fs->fs_maxsymlinklen <= 0)
477                                 namlen = dp->d_type;
478                         else
479 #endif
480                                 namlen = dp->d_namlen;
481                         if (namlen == length &&
482                             !strcmp(name, dp->d_name)) {
483                                 /* found entry */
484                                 *inumber_p = dp->d_ino;
485                                 return (0);
486                         }
487                 next:
488                         dp = (struct direct *)((char *)dp + dp->d_reclen);
489                 }
490                 fp->f_seekp += buf_size;
491         }
492         return (ENOENT);
493 }
494
495 static int sblock_try[] = SBLOCKSEARCH;
496
497 /*
498  * Open a file.
499  */
500 static int
501 ufs_open(upath, f)
502         const char *upath;
503         struct open_file *f;
504 {
505         char *cp, *ncp;
506         int c;
507         ino_t inumber, parent_inumber;
508         struct file *fp;
509         struct fs *fs;
510         int i, rc;
511         size_t buf_size;
512         int nlinks = 0;
513         char namebuf[MAXPATHLEN+1];
514         char *buf = NULL;
515         char *path = NULL;
516
517         /* allocate file system specific data structure */
518         fp = malloc(sizeof(struct file));
519         bzero(fp, sizeof(struct file));
520         f->f_fsdata = (void *)fp;
521
522         /* allocate space and read super block */
523         fs = malloc(SBLOCKSIZE);
524         fp->f_fs = fs;
525         twiddle();
526         /*
527          * Try reading the superblock in each of its possible locations.
528          */
529         for (i = 0; sblock_try[i] != -1; i++) {
530                 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
531                     sblock_try[i] / DEV_BSIZE, SBLOCKSIZE,
532                     (char *)fs, &buf_size);
533                 if (rc)
534                         goto out;
535                 if ((fs->fs_magic == FS_UFS1_MAGIC ||
536                      (fs->fs_magic == FS_UFS2_MAGIC &&
537                       fs->fs_sblockloc == numfrags(fs, sblock_try[i]))) &&
538                     buf_size == SBLOCKSIZE &&
539                     fs->fs_bsize <= MAXBSIZE &&
540                     fs->fs_bsize >= sizeof(struct fs))
541                         break;
542         }
543         if (sblock_try[i] == -1) {
544                 rc = EINVAL;
545                 goto out;
546         }
547         /*
548          * Calculate indirect block levels.
549          */
550         {
551                 ufs2_daddr_t mult;
552                 int level;
553
554                 mult = 1;
555                 for (level = 0; level < NIADDR; level++) {
556                         mult *= NINDIR(fs);
557                         fp->f_nindir[level] = mult;
558                 }
559         }
560
561         inumber = ROOTINO;
562         if ((rc = read_inode(inumber, f)) != 0)
563                 goto out;
564
565         cp = path = strdup(upath);
566         if (path == NULL) {
567             rc = ENOMEM;
568             goto out;
569         }
570         while (*cp) {
571
572                 /*
573                  * Remove extra separators
574                  */
575                 while (*cp == '/')
576                         cp++;
577                 if (*cp == '\0')
578                         break;
579
580                 /*
581                  * Check that current node is a directory.
582                  */
583                 if ((DIP(fp, di_mode) & IFMT) != IFDIR) {
584                         rc = ENOTDIR;
585                         goto out;
586                 }
587
588                 /*
589                  * Get next component of path name.
590                  */
591                 {
592                         int len = 0;
593
594                         ncp = cp;
595                         while ((c = *cp) != '\0' && c != '/') {
596                                 if (++len > MAXNAMLEN) {
597                                         rc = ENOENT;
598                                         goto out;
599                                 }
600                                 cp++;
601                         }
602                         *cp = '\0';
603                 }
604
605                 /*
606                  * Look up component in current directory.
607                  * Save directory inumber in case we find a
608                  * symbolic link.
609                  */
610                 parent_inumber = inumber;
611                 rc = search_directory(ncp, f, &inumber);
612                 *cp = c;
613                 if (rc)
614                         goto out;
615
616                 /*
617                  * Open next component.
618                  */
619                 if ((rc = read_inode(inumber, f)) != 0)
620                         goto out;
621
622                 /*
623                  * Check for symbolic link.
624                  */
625                 if ((DIP(fp, di_mode) & IFMT) == IFLNK) {
626                         int link_len = DIP(fp, di_size);
627                         int len;
628
629                         len = strlen(cp);
630
631                         if (link_len + len > MAXPATHLEN ||
632                             ++nlinks > MAXSYMLINKS) {
633                                 rc = ENOENT;
634                                 goto out;
635                         }
636
637                         bcopy(cp, &namebuf[link_len], len + 1);
638
639                         if (link_len < fs->fs_maxsymlinklen) {
640                                 if (fp->f_fs->fs_magic == FS_UFS1_MAGIC)
641                                         cp = (caddr_t)(fp->f_di.di1.di_db);
642                                 else
643                                         cp = (caddr_t)(fp->f_di.di2.di_db);
644                                 bcopy(cp, namebuf, (unsigned) link_len);
645                         } else {
646                                 /*
647                                  * Read file for symbolic link
648                                  */
649                                 size_t buf_size;
650                                 ufs2_daddr_t disk_block;
651                                 struct fs *fs = fp->f_fs;
652
653                                 if (!buf)
654                                         buf = malloc(fs->fs_bsize);
655                                 rc = block_map(f, (ufs2_daddr_t)0, &disk_block);
656                                 if (rc)
657                                         goto out;
658                                 
659                                 twiddle();
660                                 rc = (f->f_dev->dv_strategy)(f->f_devdata,
661                                         F_READ, fsbtodb(fs, disk_block),
662                                         fs->fs_bsize, buf, &buf_size);
663                                 if (rc)
664                                         goto out;
665
666                                 bcopy((char *)buf, namebuf, (unsigned)link_len);
667                         }
668
669                         /*
670                          * If relative pathname, restart at parent directory.
671                          * If absolute pathname, restart at root.
672                          */
673                         cp = namebuf;
674                         if (*cp != '/')
675                                 inumber = parent_inumber;
676                         else
677                                 inumber = (ino_t)ROOTINO;
678
679                         if ((rc = read_inode(inumber, f)) != 0)
680                                 goto out;
681                 }
682         }
683
684         /*
685          * Found terminal component.
686          */
687         rc = 0;
688 out:
689         if (buf)
690                 free(buf);
691         if (path)
692                 free(path);
693         if (rc) {
694                 if (fp->f_buf)
695                         free(fp->f_buf);
696                 free(fp->f_fs);
697                 free(fp);
698         }
699         return (rc);
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 < 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         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         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                 return (-1);
822         }
823         return (fp->f_seekp);
824 }
825
826 static int
827 ufs_stat(f, sb)
828         struct open_file *f;
829         struct stat *sb;
830 {
831         struct file *fp = (struct file *)f->f_fsdata;
832
833         /* only important stuff */
834         sb->st_mode = DIP(fp, di_mode);
835         sb->st_uid = DIP(fp, di_uid);
836         sb->st_gid = DIP(fp, di_gid);
837         sb->st_size = DIP(fp, di_size);
838         return (0);
839 }
840
841 static int
842 ufs_readdir(struct open_file *f, struct dirent *d)
843 {
844         struct file *fp = (struct file *)f->f_fsdata;
845         struct direct *dp;
846         char *buf;
847         size_t buf_size;
848         int error;
849
850         /*
851          * assume that a directory entry will not be split across blocks
852          */
853 again:
854         if (fp->f_seekp >= DIP(fp, di_size))
855                 return (ENOENT);
856         error = buf_read_file(f, &buf, &buf_size);
857         if (error)
858                 return (error);
859         dp = (struct direct *)buf;
860         fp->f_seekp += dp->d_reclen;
861         if (dp->d_ino == (ino_t)0)
862                 goto again;
863         d->d_type = dp->d_type;
864         strcpy(d->d_name, dp->d_name);
865         return (0);
866 }