]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libufs/inode.c
unbound: Vendor import 1.18.0
[FreeBSD/FreeBSD.git] / lib / libufs / inode.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2002 Juli Mallett.  All rights reserved.
5  *
6  * This software was written by Juli Mallett <jmallett@FreeBSD.org> for the
7  * FreeBSD project.  Redistribution and use in source and binary forms, with
8  * or without modification, are permitted provided that the following
9  * conditions are met:
10  *
11  * 1. Redistribution of source code must retain the above copyright notice,
12  *    this list of conditions and the following disclaimer.
13  * 2. Redistribution in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
21  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
26  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include <sys/cdefs.h>
31 #include <sys/param.h>
32 #include <sys/mount.h>
33 #include <sys/disklabel.h>
34 #include <sys/stat.h>
35
36 #include <ufs/ufs/ufsmount.h>
37 #include <ufs/ufs/dinode.h>
38 #include <ufs/ffs/fs.h>
39
40 #include <errno.h>
41 #include <fcntl.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46
47 #include <libufs.h>
48
49 int
50 getinode(struct uufsd *disk, union dinodep *dp, ino_t inum)
51 {
52         ino_t min, max;
53         caddr_t inoblock;
54         struct fs *fs;
55
56         ERROR(disk, NULL);
57
58         fs = &disk->d_fs;
59         if (inum >= (ino_t)fs->fs_ipg * fs->fs_ncg) {
60                 ERROR(disk, "inode number out of range");
61                 return (-1);
62         }
63         inoblock = disk->d_inoblock;
64         min = disk->d_inomin;
65         max = disk->d_inomax;
66
67         if (inoblock == NULL) {
68                 inoblock = malloc(fs->fs_bsize);
69                 if (inoblock == NULL) {
70                         ERROR(disk, "unable to allocate inode block");
71                         return (-1);
72                 }
73                 disk->d_inoblock = inoblock;
74         }
75         if (inum >= min && inum < max)
76                 goto gotit;
77         bread(disk, fsbtodb(fs, ino_to_fsba(fs, inum)), inoblock,
78             fs->fs_bsize);
79         disk->d_inomin = min = inum - (inum % INOPB(fs));
80         disk->d_inomax = max = min + INOPB(fs);
81 gotit:  switch (disk->d_ufs) {
82         case 1:
83                 disk->d_dp.dp1 = &((struct ufs1_dinode *)inoblock)[inum - min];
84                 if (dp != NULL)
85                         *dp = disk->d_dp;
86                 return (0);
87         case 2:
88                 disk->d_dp.dp2 = &((struct ufs2_dinode *)inoblock)[inum - min];
89                 if (dp != NULL)
90                         *dp = disk->d_dp;
91                 if (ffs_verify_dinode_ckhash(fs, disk->d_dp.dp2) == 0)
92                         return (0);
93                 ERROR(disk, "check-hash failed for inode read from disk");
94                 return (-1);
95         default:
96                 break;
97         }
98         ERROR(disk, "unknown UFS filesystem type");
99         return (-1);
100 }
101
102 int
103 putinode(struct uufsd *disk)
104 {
105         struct fs *fs;
106
107         fs = &disk->d_fs;
108         if (disk->d_inoblock == NULL) {
109                 ERROR(disk, "No inode block allocated");
110                 return (-1);
111         }
112         if (disk->d_ufs == 2)
113                 ffs_update_dinode_ckhash(fs, disk->d_dp.dp2);
114         if (bwrite(disk, fsbtodb(fs, ino_to_fsba(&disk->d_fs, disk->d_inomin)),
115             disk->d_inoblock, disk->d_fs.fs_bsize) <= 0)
116                 return (-1);
117         return (0);
118 }