xref: /freebsd/lib/libufs/inode.c (revision 1111a44301da39d7b7459c784230e1405e8980f8)
15e53a4f9SPedro F. Giffuni /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
35e53a4f9SPedro F. Giffuni  *
420938dbfSJuli Mallett  * Copyright (c) 2002 Juli Mallett.  All rights reserved.
520938dbfSJuli Mallett  *
620938dbfSJuli Mallett  * This software was written by Juli Mallett <jmallett@FreeBSD.org> for the
720938dbfSJuli Mallett  * FreeBSD project.  Redistribution and use in source and binary forms, with
820938dbfSJuli Mallett  * or without modification, are permitted provided that the following
920938dbfSJuli Mallett  * conditions are met:
1020938dbfSJuli Mallett  *
1120938dbfSJuli Mallett  * 1. Redistribution of source code must retain the above copyright notice,
1220938dbfSJuli Mallett  *    this list of conditions and the following disclaimer.
1320938dbfSJuli Mallett  * 2. Redistribution in binary form must reproduce the above copyright
1420938dbfSJuli Mallett  *    notice, this list of conditions and the following disclaimer in the
1520938dbfSJuli Mallett  *    documentation and/or other materials provided with the distribution.
1620938dbfSJuli Mallett  *
1720938dbfSJuli Mallett  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1820938dbfSJuli Mallett  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1920938dbfSJuli Mallett  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
2020938dbfSJuli Mallett  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
2120938dbfSJuli Mallett  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
2220938dbfSJuli Mallett  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
2320938dbfSJuli Mallett  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2420938dbfSJuli Mallett  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
2520938dbfSJuli Mallett  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
2620938dbfSJuli Mallett  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2720938dbfSJuli Mallett  * POSSIBILITY OF SUCH DAMAGE.
2820938dbfSJuli Mallett  */
2920938dbfSJuli Mallett 
3020938dbfSJuli Mallett #include <sys/param.h>
3120938dbfSJuli Mallett #include <sys/mount.h>
3220938dbfSJuli Mallett #include <sys/disklabel.h>
3320938dbfSJuli Mallett #include <sys/stat.h>
3420938dbfSJuli Mallett 
35d485c77fSKonstantin Belousov #include <ufs/ufs/extattr.h>
36d485c77fSKonstantin Belousov #include <ufs/ufs/quota.h>
3720938dbfSJuli Mallett #include <ufs/ufs/ufsmount.h>
3820938dbfSJuli Mallett #include <ufs/ufs/dinode.h>
3920938dbfSJuli Mallett #include <ufs/ffs/fs.h>
4020938dbfSJuli Mallett 
4120938dbfSJuli Mallett #include <errno.h>
4220938dbfSJuli Mallett #include <fcntl.h>
4320938dbfSJuli Mallett #include <stdio.h>
4420938dbfSJuli Mallett #include <stdlib.h>
4520938dbfSJuli Mallett #include <string.h>
46*1111a443SKirk McKusick #include <time.h>
4720938dbfSJuli Mallett #include <unistd.h>
4820938dbfSJuli Mallett 
4920938dbfSJuli Mallett #include <libufs.h>
5020938dbfSJuli Mallett 
5120938dbfSJuli Mallett int
getinode(struct uufsd * disk,union dinodep * dp,ino_t inum)529fc5d538SKirk McKusick getinode(struct uufsd *disk, union dinodep *dp, ino_t inum)
5320938dbfSJuli Mallett {
5420938dbfSJuli Mallett 	ino_t min, max;
5520938dbfSJuli Mallett 	caddr_t inoblock;
5620938dbfSJuli Mallett 	struct fs *fs;
57*1111a443SKirk McKusick 	struct timespec now;
5820938dbfSJuli Mallett 
5949b2a686SJuli Mallett 	ERROR(disk, NULL);
6020938dbfSJuli Mallett 
6120938dbfSJuli Mallett 	fs = &disk->d_fs;
629fc5d538SKirk McKusick 	if (inum >= (ino_t)fs->fs_ipg * fs->fs_ncg) {
630c94b536SKirk McKusick 		ERROR(disk, "inode number out of range");
640c94b536SKirk McKusick 		return (-1);
650c94b536SKirk McKusick 	}
66772430ddSKirk McKusick 	inoblock = (caddr_t)&disk->d_inos[0];
6720938dbfSJuli Mallett 	min = disk->d_inomin;
6820938dbfSJuli Mallett 	max = disk->d_inomax;
6920938dbfSJuli Mallett 
70*1111a443SKirk McKusick 	if (clock_gettime(CLOCK_REALTIME_FAST, &now) != 0) {
71*1111a443SKirk McKusick 		ERROR(disk, "cannot get current time of day");
72*1111a443SKirk McKusick 		return (-1);
73*1111a443SKirk McKusick 	}
749fc5d538SKirk McKusick 	if (inum >= min && inum < max)
7520938dbfSJuli Mallett 		goto gotit;
769fc5d538SKirk McKusick 	bread(disk, fsbtodb(fs, ino_to_fsba(fs, inum)), inoblock,
7720938dbfSJuli Mallett 	    fs->fs_bsize);
789fc5d538SKirk McKusick 	disk->d_inomin = min = inum - (inum % INOPB(fs));
79cf6c0643SJuli Mallett 	disk->d_inomax = max = min + INOPB(fs);
8020938dbfSJuli Mallett gotit:	switch (disk->d_ufs) {
8120938dbfSJuli Mallett 	case 1:
829fc5d538SKirk McKusick 		disk->d_dp.dp1 = &((struct ufs1_dinode *)inoblock)[inum - min];
83*1111a443SKirk McKusick 		if (ffs_oldfscompat_inode_read(fs, disk->d_dp, now.tv_sec))
84*1111a443SKirk McKusick 			putinode(disk);
859fc5d538SKirk McKusick 		if (dp != NULL)
869fc5d538SKirk McKusick 			*dp = disk->d_dp;
871081253fSJuli Mallett 		return (0);
8820938dbfSJuli Mallett 	case 2:
899fc5d538SKirk McKusick 		disk->d_dp.dp2 = &((struct ufs2_dinode *)inoblock)[inum - min];
909fc5d538SKirk McKusick 		if (dp != NULL)
919fc5d538SKirk McKusick 			*dp = disk->d_dp;
92*1111a443SKirk McKusick 		if (ffs_verify_dinode_ckhash(fs, disk->d_dp.dp2) == 0) {
93*1111a443SKirk McKusick 			if (ffs_oldfscompat_inode_read(fs, disk->d_dp,
94*1111a443SKirk McKusick 			    now.tv_sec))
95*1111a443SKirk McKusick 				putinode(disk);
961081253fSJuli Mallett 			return (0);
97*1111a443SKirk McKusick 		}
988f829a5cSKirk McKusick 		ERROR(disk, "check-hash failed for inode read from disk");
998f829a5cSKirk McKusick 		return (-1);
10020938dbfSJuli Mallett 	default:
10120938dbfSJuli Mallett 		break;
10220938dbfSJuli Mallett 	}
10349b2a686SJuli Mallett 	ERROR(disk, "unknown UFS filesystem type");
1041081253fSJuli Mallett 	return (-1);
10520938dbfSJuli Mallett }
106113db2ddSJeff Roberson 
107113db2ddSJeff Roberson int
putinode(struct uufsd * disk)1089fc5d538SKirk McKusick putinode(struct uufsd *disk)
109113db2ddSJeff Roberson {
110113db2ddSJeff Roberson 	struct fs *fs;
111113db2ddSJeff Roberson 
112113db2ddSJeff Roberson 	fs = &disk->d_fs;
1138f829a5cSKirk McKusick 	if (disk->d_ufs == 2)
1148f829a5cSKirk McKusick 		ffs_update_dinode_ckhash(fs, disk->d_dp.dp2);
115113db2ddSJeff Roberson 	if (bwrite(disk, fsbtodb(fs, ino_to_fsba(&disk->d_fs, disk->d_inomin)),
116772430ddSKirk McKusick 	    (caddr_t)&disk->d_inos[0], disk->d_fs.fs_bsize) <= 0)
117113db2ddSJeff Roberson 		return (-1);
118113db2ddSJeff Roberson 	return (0);
119113db2ddSJeff Roberson }
120