xref: /freebsd/lib/libufs/inode.c (revision 1111a44301da39d7b7459c784230e1405e8980f8)
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/param.h>
31 #include <sys/mount.h>
32 #include <sys/disklabel.h>
33 #include <sys/stat.h>
34 
35 #include <ufs/ufs/extattr.h>
36 #include <ufs/ufs/quota.h>
37 #include <ufs/ufs/ufsmount.h>
38 #include <ufs/ufs/dinode.h>
39 #include <ufs/ffs/fs.h>
40 
41 #include <errno.h>
42 #include <fcntl.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <time.h>
47 #include <unistd.h>
48 
49 #include <libufs.h>
50 
51 int
getinode(struct uufsd * disk,union dinodep * dp,ino_t inum)52 getinode(struct uufsd *disk, union dinodep *dp, ino_t inum)
53 {
54 	ino_t min, max;
55 	caddr_t inoblock;
56 	struct fs *fs;
57 	struct timespec now;
58 
59 	ERROR(disk, NULL);
60 
61 	fs = &disk->d_fs;
62 	if (inum >= (ino_t)fs->fs_ipg * fs->fs_ncg) {
63 		ERROR(disk, "inode number out of range");
64 		return (-1);
65 	}
66 	inoblock = (caddr_t)&disk->d_inos[0];
67 	min = disk->d_inomin;
68 	max = disk->d_inomax;
69 
70 	if (clock_gettime(CLOCK_REALTIME_FAST, &now) != 0) {
71 		ERROR(disk, "cannot get current time of day");
72 		return (-1);
73 	}
74 	if (inum >= min && inum < max)
75 		goto gotit;
76 	bread(disk, fsbtodb(fs, ino_to_fsba(fs, inum)), inoblock,
77 	    fs->fs_bsize);
78 	disk->d_inomin = min = inum - (inum % INOPB(fs));
79 	disk->d_inomax = max = min + INOPB(fs);
80 gotit:	switch (disk->d_ufs) {
81 	case 1:
82 		disk->d_dp.dp1 = &((struct ufs1_dinode *)inoblock)[inum - min];
83 		if (ffs_oldfscompat_inode_read(fs, disk->d_dp, now.tv_sec))
84 			putinode(disk);
85 		if (dp != NULL)
86 			*dp = disk->d_dp;
87 		return (0);
88 	case 2:
89 		disk->d_dp.dp2 = &((struct ufs2_dinode *)inoblock)[inum - min];
90 		if (dp != NULL)
91 			*dp = disk->d_dp;
92 		if (ffs_verify_dinode_ckhash(fs, disk->d_dp.dp2) == 0) {
93 			if (ffs_oldfscompat_inode_read(fs, disk->d_dp,
94 			    now.tv_sec))
95 				putinode(disk);
96 			return (0);
97 		}
98 		ERROR(disk, "check-hash failed for inode read from disk");
99 		return (-1);
100 	default:
101 		break;
102 	}
103 	ERROR(disk, "unknown UFS filesystem type");
104 	return (-1);
105 }
106 
107 int
putinode(struct uufsd * disk)108 putinode(struct uufsd *disk)
109 {
110 	struct fs *fs;
111 
112 	fs = &disk->d_fs;
113 	if (disk->d_ufs == 2)
114 		ffs_update_dinode_ckhash(fs, disk->d_dp.dp2);
115 	if (bwrite(disk, fsbtodb(fs, ino_to_fsba(&disk->d_fs, disk->d_inomin)),
116 	    (caddr_t)&disk->d_inos[0], disk->d_fs.fs_bsize) <= 0)
117 		return (-1);
118 	return (0);
119 }
120