xref: /dflybsd-src/sys/vfs/ext2fs/ext2_subr.c (revision e5b38eb588d517bc6924dd57a647f58246c5217b)
1cfe60390STomohiro Kusumi /*-
2cfe60390STomohiro Kusumi  *  modified for Lites 1.1
3cfe60390STomohiro Kusumi  *
4cfe60390STomohiro Kusumi  *  Aug 1995, Godmar Back (gback@cs.utah.edu)
5cfe60390STomohiro Kusumi  *  University of Utah, Department of Computer Science
6cfe60390STomohiro Kusumi  */
7cfe60390STomohiro Kusumi /*-
8cfe60390STomohiro Kusumi  * SPDX-License-Identifier: BSD-3-Clause
9cfe60390STomohiro Kusumi  *
10cfe60390STomohiro Kusumi  * Copyright (c) 1982, 1986, 1989, 1993
11cfe60390STomohiro Kusumi  *	The Regents of the University of California.  All rights reserved.
12cfe60390STomohiro Kusumi  *
13cfe60390STomohiro Kusumi  * Redistribution and use in source and binary forms, with or without
14cfe60390STomohiro Kusumi  * modification, are permitted provided that the following conditions
15cfe60390STomohiro Kusumi  * are met:
16cfe60390STomohiro Kusumi  * 1. Redistributions of source code must retain the above copyright
17cfe60390STomohiro Kusumi  *    notice, this list of conditions and the following disclaimer.
18cfe60390STomohiro Kusumi  * 2. Redistributions in binary form must reproduce the above copyright
19cfe60390STomohiro Kusumi  *    notice, this list of conditions and the following disclaimer in the
20cfe60390STomohiro Kusumi  *    documentation and/or other materials provided with the distribution.
21cfe60390STomohiro Kusumi  * 3. Neither the name of the University nor the names of its contributors
22cfe60390STomohiro Kusumi  *    may be used to endorse or promote products derived from this software
23cfe60390STomohiro Kusumi  *    without specific prior written permission.
24cfe60390STomohiro Kusumi  *
25cfe60390STomohiro Kusumi  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26cfe60390STomohiro Kusumi  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27cfe60390STomohiro Kusumi  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28cfe60390STomohiro Kusumi  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29cfe60390STomohiro Kusumi  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30cfe60390STomohiro Kusumi  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31cfe60390STomohiro Kusumi  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32cfe60390STomohiro Kusumi  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33cfe60390STomohiro Kusumi  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34cfe60390STomohiro Kusumi  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35cfe60390STomohiro Kusumi  * SUCH DAMAGE.
36cfe60390STomohiro Kusumi  *
37cfe60390STomohiro Kusumi  *	@(#)ffs_subr.c	8.2 (Berkeley) 9/21/93
38cfe60390STomohiro Kusumi  * $FreeBSD$
39cfe60390STomohiro Kusumi  */
40cfe60390STomohiro Kusumi 
41cfe60390STomohiro Kusumi #include <sys/param.h>
42cfe60390STomohiro Kusumi 
43cfe60390STomohiro Kusumi #include <sys/proc.h>
44cfe60390STomohiro Kusumi #include <sys/systm.h>
45cfe60390STomohiro Kusumi #include <sys/bio.h>
46cfe60390STomohiro Kusumi #include <sys/buf2.h>
47cfe60390STomohiro Kusumi #include <sys/lock.h>
48cfe60390STomohiro Kusumi #include <sys/ucred.h>
49cfe60390STomohiro Kusumi #include <sys/vnode.h>
50cfe60390STomohiro Kusumi #include <sys/mount.h>
51cfe60390STomohiro Kusumi #include <sys/malloc.h>
52cfe60390STomohiro Kusumi 
53cfe60390STomohiro Kusumi #include <vfs/ext2fs/fs.h>
54cfe60390STomohiro Kusumi #include <vfs/ext2fs/inode.h>
55cfe60390STomohiro Kusumi #include <vfs/ext2fs/ext2fs.h>
56cfe60390STomohiro Kusumi #include <vfs/ext2fs/ext2_extern.h>
57cfe60390STomohiro Kusumi #include <vfs/ext2fs/fs.h>
58cfe60390STomohiro Kusumi #include <vfs/ext2fs/ext2_extents.h>
59cfe60390STomohiro Kusumi #include <vfs/ext2fs/ext2_mount.h>
60cfe60390STomohiro Kusumi #include <vfs/ext2fs/ext2_dinode.h>
61cfe60390STomohiro Kusumi 
62cfe60390STomohiro Kusumi void
ext2_free(void * addr,struct malloc_type * type,const char * func)63cfe60390STomohiro Kusumi ext2_free(void *addr, struct malloc_type *type, const char *func)
64cfe60390STomohiro Kusumi {
65cfe60390STomohiro Kusumi 	if (addr == NULL) {
66cfe60390STomohiro Kusumi 		printf("%s: trying to free NULL pointer\n", func);
67cfe60390STomohiro Kusumi 		return;
68cfe60390STomohiro Kusumi 	}
69cfe60390STomohiro Kusumi 	kfree(addr, type);
70cfe60390STomohiro Kusumi }
71cfe60390STomohiro Kusumi 
72cfe60390STomohiro Kusumi /*
73cfe60390STomohiro Kusumi  * Return buffer with the contents of block "offset" from the beginning of
74cfe60390STomohiro Kusumi  * directory "ip".  If "res" is non-zero, fill it in with a pointer to the
75cfe60390STomohiro Kusumi  * remaining space in the directory.
76cfe60390STomohiro Kusumi  */
77cfe60390STomohiro Kusumi int
ext2_blkatoff(struct vnode * vp,off_t offset,char ** res,struct buf ** bpp)78cfe60390STomohiro Kusumi ext2_blkatoff(struct vnode *vp, off_t offset, char **res, struct buf **bpp)
79cfe60390STomohiro Kusumi {
80cfe60390STomohiro Kusumi 	struct inode *ip;
81cfe60390STomohiro Kusumi 	struct m_ext2fs *fs;
82cfe60390STomohiro Kusumi 	struct buf *bp;
83cfe60390STomohiro Kusumi 	e2fs_lbn_t lbn;
84cfe60390STomohiro Kusumi 	int error, bsize;
85cfe60390STomohiro Kusumi 
86cfe60390STomohiro Kusumi 	ip = VTOI(vp);
87cfe60390STomohiro Kusumi 	fs = ip->i_e2fs;
88cfe60390STomohiro Kusumi 	lbn = lblkno(fs, offset);
89cfe60390STomohiro Kusumi 	bsize = blksize(fs, ip, lbn);
90cfe60390STomohiro Kusumi 
91*e5b38eb5STomohiro Kusumi 	if ((error = bread(vp, lblktodoff(fs, lbn), bsize, &bp)) != 0) {
92*e5b38eb5STomohiro Kusumi 		brelse(bp);
93cfe60390STomohiro Kusumi 		return (error);
94cfe60390STomohiro Kusumi 	}
95cfe60390STomohiro Kusumi 	error = ext2_dir_blk_csum_verify(ip, bp);
96cfe60390STomohiro Kusumi 	if (error != 0) {
97*e5b38eb5STomohiro Kusumi 		brelse(bp);
98cfe60390STomohiro Kusumi 		return (error);
99cfe60390STomohiro Kusumi 	}
100cfe60390STomohiro Kusumi 	if (res)
101cfe60390STomohiro Kusumi 		*res = (char *)bp->b_data + blkoff(fs, offset);
102cfe60390STomohiro Kusumi 
103cfe60390STomohiro Kusumi 	*bpp = bp;
104cfe60390STomohiro Kusumi 
105cfe60390STomohiro Kusumi 	return (0);
106cfe60390STomohiro Kusumi }
107cfe60390STomohiro Kusumi 
108cfe60390STomohiro Kusumi /*
109cfe60390STomohiro Kusumi  * Update the cluster map because of an allocation of free like ffs.
110cfe60390STomohiro Kusumi  *
111cfe60390STomohiro Kusumi  * Cnt == 1 means free; cnt == -1 means allocating.
112cfe60390STomohiro Kusumi  */
113cfe60390STomohiro Kusumi void
ext2_clusteracct(struct m_ext2fs * fs,char * bbp,int cg,e4fs_daddr_t bno,int cnt)114cfe60390STomohiro Kusumi ext2_clusteracct(struct m_ext2fs *fs, char *bbp, int cg, e4fs_daddr_t bno, int cnt)
115cfe60390STomohiro Kusumi {
116cfe60390STomohiro Kusumi 	int32_t *sump = fs->e2fs_clustersum[cg].cs_sum;
117cfe60390STomohiro Kusumi 	int32_t *lp;
118cfe60390STomohiro Kusumi 	e4fs_daddr_t start, end, loc, forw, back;
119cfe60390STomohiro Kusumi 	int bit, i;
120cfe60390STomohiro Kusumi 
121cfe60390STomohiro Kusumi 	/* Initialize the cluster summary array. */
122cfe60390STomohiro Kusumi 	if (fs->e2fs_clustersum[cg].cs_init == 0) {
123cfe60390STomohiro Kusumi 		int run = 0;
124cfe60390STomohiro Kusumi 
125cfe60390STomohiro Kusumi 		bit = 1;
126cfe60390STomohiro Kusumi 		loc = 0;
127cfe60390STomohiro Kusumi 
128cfe60390STomohiro Kusumi 		for (i = 0; i < fs->e2fs_fpg; i++) {
129cfe60390STomohiro Kusumi 			if ((bbp[loc] & bit) == 0)
130cfe60390STomohiro Kusumi 				run++;
131cfe60390STomohiro Kusumi 			else if (run != 0) {
132cfe60390STomohiro Kusumi 				if (run > fs->e2fs_contigsumsize)
133cfe60390STomohiro Kusumi 					run = fs->e2fs_contigsumsize;
134cfe60390STomohiro Kusumi 				sump[run]++;
135cfe60390STomohiro Kusumi 				run = 0;
136cfe60390STomohiro Kusumi 			}
137cfe60390STomohiro Kusumi 			if ((i & (NBBY - 1)) != (NBBY - 1))
138cfe60390STomohiro Kusumi 				bit <<= 1;
139cfe60390STomohiro Kusumi 			else {
140cfe60390STomohiro Kusumi 				loc++;
141cfe60390STomohiro Kusumi 				bit = 1;
142cfe60390STomohiro Kusumi 			}
143cfe60390STomohiro Kusumi 		}
144cfe60390STomohiro Kusumi 		if (run != 0) {
145cfe60390STomohiro Kusumi 			if (run > fs->e2fs_contigsumsize)
146cfe60390STomohiro Kusumi 				run = fs->e2fs_contigsumsize;
147cfe60390STomohiro Kusumi 			sump[run]++;
148cfe60390STomohiro Kusumi 		}
149cfe60390STomohiro Kusumi 		fs->e2fs_clustersum[cg].cs_init = 1;
150cfe60390STomohiro Kusumi 	}
151cfe60390STomohiro Kusumi 
152cfe60390STomohiro Kusumi 	if (fs->e2fs_contigsumsize <= 0)
153cfe60390STomohiro Kusumi 		return;
154cfe60390STomohiro Kusumi 
155cfe60390STomohiro Kusumi 	/* Find the size of the cluster going forward. */
156cfe60390STomohiro Kusumi 	start = bno + 1;
157cfe60390STomohiro Kusumi 	end = start + fs->e2fs_contigsumsize;
158cfe60390STomohiro Kusumi 	if (end > fs->e2fs_fpg)
159cfe60390STomohiro Kusumi 		end = fs->e2fs_fpg;
160cfe60390STomohiro Kusumi 	loc = start / NBBY;
161cfe60390STomohiro Kusumi 	bit = 1 << (start % NBBY);
162cfe60390STomohiro Kusumi 	for (i = start; i < end; i++) {
163cfe60390STomohiro Kusumi 		if ((bbp[loc] & bit) != 0)
164cfe60390STomohiro Kusumi 			break;
165cfe60390STomohiro Kusumi 		if ((i & (NBBY - 1)) != (NBBY - 1))
166cfe60390STomohiro Kusumi 			bit <<= 1;
167cfe60390STomohiro Kusumi 		else {
168cfe60390STomohiro Kusumi 			loc++;
169cfe60390STomohiro Kusumi 			bit = 1;
170cfe60390STomohiro Kusumi 		}
171cfe60390STomohiro Kusumi 	}
172cfe60390STomohiro Kusumi 	forw = i - start;
173cfe60390STomohiro Kusumi 
174cfe60390STomohiro Kusumi 	/* Find the size of the cluster going backward. */
175cfe60390STomohiro Kusumi 	start = bno - 1;
176cfe60390STomohiro Kusumi 	end = start - fs->e2fs_contigsumsize;
177cfe60390STomohiro Kusumi 	if (end < 0)
178cfe60390STomohiro Kusumi 		end = -1;
179cfe60390STomohiro Kusumi 	loc = start / NBBY;
180cfe60390STomohiro Kusumi 	bit = 1 << (start % NBBY);
181cfe60390STomohiro Kusumi 	for (i = start; i > end; i--) {
182cfe60390STomohiro Kusumi 		if ((bbp[loc] & bit) != 0)
183cfe60390STomohiro Kusumi 			break;
184cfe60390STomohiro Kusumi 		if ((i & (NBBY - 1)) != 0)
185cfe60390STomohiro Kusumi 			bit >>= 1;
186cfe60390STomohiro Kusumi 		else {
187cfe60390STomohiro Kusumi 			loc--;
188cfe60390STomohiro Kusumi 			bit = 1 << (NBBY - 1);
189cfe60390STomohiro Kusumi 		}
190cfe60390STomohiro Kusumi 	}
191cfe60390STomohiro Kusumi 	back = start - i;
192cfe60390STomohiro Kusumi 
193cfe60390STomohiro Kusumi 	/*
194cfe60390STomohiro Kusumi 	 * Account for old cluster and the possibly new forward and
195cfe60390STomohiro Kusumi 	 * back clusters.
196cfe60390STomohiro Kusumi 	 */
197cfe60390STomohiro Kusumi 	i = back + forw + 1;
198cfe60390STomohiro Kusumi 	if (i > fs->e2fs_contigsumsize)
199cfe60390STomohiro Kusumi 		i = fs->e2fs_contigsumsize;
200cfe60390STomohiro Kusumi 	sump[i] += cnt;
201cfe60390STomohiro Kusumi 	if (back > 0)
202cfe60390STomohiro Kusumi 		sump[back] -= cnt;
203cfe60390STomohiro Kusumi 	if (forw > 0)
204cfe60390STomohiro Kusumi 		sump[forw] -= cnt;
205cfe60390STomohiro Kusumi 
206cfe60390STomohiro Kusumi 	/* Update cluster summary information. */
207cfe60390STomohiro Kusumi 	lp = &sump[fs->e2fs_contigsumsize];
208cfe60390STomohiro Kusumi 	for (i = fs->e2fs_contigsumsize; i > 0; i--)
209cfe60390STomohiro Kusumi 		if (*lp-- > 0)
210cfe60390STomohiro Kusumi 			break;
211cfe60390STomohiro Kusumi 	fs->e2fs_maxcluster[cg] = i;
212cfe60390STomohiro Kusumi }
213