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