xref: /csrg-svn/sys/ufs/lfs/lfs_subr.c (revision 51503)
1 /*
2  * Copyright (c) 1991 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)lfs_subr.c	7.2 (Berkeley) 11/01/91
8  */
9 
10 #include <sys/param.h>
11 #include <sys/namei.h>
12 #include <sys/vnode.h>
13 #include <sys/buf.h>
14 
15 #include <ufs/ufs/quota.h>
16 #include <ufs/ufs/inode.h>
17 
18 #include <ufs/lfs/lfs.h>
19 #include <ufs/lfs/lfs_extern.h>
20 
21 /*
22  * Return buffer with the contents of block "offset" from the beginning of
23  * directory "ip".  If "res" is non-zero, fill it in with a pointer to the
24  * remaining space in the directory.
25  */
26 int
27 lfs_blkatoff(ip, offset, res, bpp)
28 	struct inode *ip;
29 	off_t offset;
30 	char **res;
31 	struct buf **bpp;
32 {
33 	register struct lfs *fs;
34 	struct buf *bp;
35 	daddr_t lbn;
36 	int bsize, error;
37 
38 	fs = ip->i_lfs;
39 	lbn = lblkno(fs, offset);
40 	bsize = blksize(fs);
41 
42 	*bpp = NULL;
43 	if (error = bread(ITOV(ip), lbn, bsize, NOCRED, &bp)) {
44 		brelse(bp);
45 		return (error);
46 	}
47 	if (res)
48 		*res = bp->b_un.b_addr + blkoff(fs, offset);
49 	*bpp = bp;
50 	return (0);
51 }
52 
53 /* Return the current version number for a specific inode. */
54 u_long
55 lfs_getversion(fs, ino)
56 	struct lfs *fs;
57 	ino_t ino;
58 {
59 	BUF *bp;
60 	IFILE *ifp;
61 	u_long version;
62 
63 	/*
64 	 * Read the appropriate block from the ifile.  Return the
65 	 * version number.
66 	 */
67 	LFS_IENTRY(ifp, fs, ino, bp);
68 	version = ifp->if_version;
69 	brelse(bp);
70 	return (version);
71 }
72 
73 /* Search a block for a specific dinode. */
74 DINODE *
75 lfs_ifind(fs, ino, page)
76 	struct lfs *fs;
77 	ino_t ino;
78 	void *page;
79 {
80 	register DINODE *dip;
81 	register int cnt;
82 
83 #ifdef ALLOCPRINT
84 	printf("lfs_ifind: inode %d\n", ino);
85 #endif
86 	dip = page;
87 	for (cnt = INOPB(fs); cnt--; ++dip)
88 		if (dip->di_inum == ino)
89 			return (dip);
90 
91 	panic("lfs_ifind: dinode %u not found", ino);
92 	/* NOTREACHED */
93 }
94 
95 /* Set values in the ifile for the inode. */
96 void
97 lfs_iset(ip, daddr, atime)
98 	INODE *ip;
99 	daddr_t daddr;
100 	time_t atime;
101 {
102 	BUF *bp;
103 	IFILE *ifp;
104 	struct lfs *fs;
105 	ino_t ino;
106 
107 #ifdef ALLOCPRINT
108 	printf("lfs_iset: setting ino %d daddr %lx time %lx\n",
109 	    ip->i_number, daddr, atime);
110 #endif
111 
112 	fs = ip->i_lfs;
113 	ino = ip->i_number;
114 	LFS_IENTRY(ifp, fs, ino, bp);
115 
116 	ifp->if_daddr = daddr;
117 	ifp->if_st_atime = atime;
118 	lfs_bwrite(bp);
119 }
120 
121 /* Translate an inode number to a disk address. */
122 daddr_t
123 lfs_itod(fs, ino)
124 	struct lfs *fs;
125 	ino_t ino;
126 {
127 	BUF *bp;
128 	IFILE *ifp;
129 	daddr_t iaddr;
130 
131 	/* Read the appropriate block from the ifile. */
132 	LFS_IENTRY(ifp, fs, ino, bp);
133 
134 	if (ifp->if_daddr == LFS_UNUSED_DADDR)
135 		panic("itod: unused disk address");
136 	iaddr = ifp->if_daddr;
137 	brelse(bp);
138 	return (iaddr);
139 }
140