/*-1* modified for Lites 1.12*3* Aug 1995, Godmar Back ([email protected])4* University of Utah, Department of Computer Science5*/6/*-7* SPDX-License-Identifier: BSD-3-Clause8*9* Copyright (c) 1982, 1986, 1989, 199310* The Regents of the University of California. All rights reserved.11*12* Redistribution and use in source and binary forms, with or without13* modification, are permitted provided that the following conditions14* are met:15* 1. Redistributions of source code must retain the above copyright16* notice, this list of conditions and the following disclaimer.17* 2. Redistributions in binary form must reproduce the above copyright18* notice, this list of conditions and the following disclaimer in the19* documentation and/or other materials provided with the distribution.20* 3. Neither the name of the University nor the names of its contributors21* may be used to endorse or promote products derived from this software22* without specific prior written permission.23*24* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND25* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE26* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE27* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE28* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL29* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS30* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)31* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT32* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY33* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF34* SUCH DAMAGE.35*/3637#include <sys/param.h>3839#include <sys/proc.h>40#include <sys/sdt.h>41#include <sys/systm.h>42#include <sys/bio.h>43#include <sys/buf.h>44#include <sys/lock.h>45#include <sys/ucred.h>46#include <sys/vnode.h>4748#include <fs/ext2fs/fs.h>49#include <fs/ext2fs/inode.h>50#include <fs/ext2fs/ext2fs.h>51#include <fs/ext2fs/ext2_extern.h>52#include <fs/ext2fs/fs.h>53#include <fs/ext2fs/ext2_extents.h>54#include <fs/ext2fs/ext2_mount.h>55#include <fs/ext2fs/ext2_dinode.h>5657/*58* Return buffer with the contents of block "offset" from the beginning of59* directory "ip". If "res" is non-zero, fill it in with a pointer to the60* remaining space in the directory.61*/62int63ext2_blkatoff(struct vnode *vp, off_t offset, char **res, struct buf **bpp)64{65struct inode *ip;66struct m_ext2fs *fs;67struct buf *bp;68e2fs_lbn_t lbn;69int error, bsize;7071ip = VTOI(vp);72fs = ip->i_e2fs;73lbn = lblkno(fs, offset);74bsize = blksize(fs, ip, lbn);7576if ((error = bread(vp, lbn, bsize, NOCRED, &bp)) != 0) {77brelse(bp);78return (error);79}80error = ext2_dir_blk_csum_verify(ip, bp);81if (error != 0) {82brelse(bp);83return (error);84}85if (res)86*res = (char *)bp->b_data + blkoff(fs, offset);8788*bpp = bp;8990return (0);91}9293/*94* Update the cluster map because of an allocation of free like ffs.95*96* Cnt == 1 means free; cnt == -1 means allocating.97*/98void99ext2_clusteracct(struct m_ext2fs *fs, char *bbp, int cg, e4fs_daddr_t bno, int cnt)100{101int32_t *sump = fs->e2fs_clustersum[cg].cs_sum;102int32_t *lp;103e4fs_daddr_t start, end, loc, forw, back;104int bit, i;105106/* Initialize the cluster summary array. */107if (fs->e2fs_clustersum[cg].cs_init == 0) {108int run = 0;109110bit = 1;111loc = 0;112113for (i = 0; i < fs->e2fs_fpg; i++) {114if ((bbp[loc] & bit) == 0)115run++;116else if (run != 0) {117if (run > fs->e2fs_contigsumsize)118run = fs->e2fs_contigsumsize;119sump[run]++;120run = 0;121}122if ((i & (NBBY - 1)) != (NBBY - 1))123bit <<= 1;124else {125loc++;126bit = 1;127}128}129if (run != 0) {130if (run > fs->e2fs_contigsumsize)131run = fs->e2fs_contigsumsize;132sump[run]++;133}134fs->e2fs_clustersum[cg].cs_init = 1;135}136137if (fs->e2fs_contigsumsize <= 0)138return;139140/* Find the size of the cluster going forward. */141start = bno + 1;142end = start + fs->e2fs_contigsumsize;143if (end > fs->e2fs_fpg)144end = fs->e2fs_fpg;145loc = start / NBBY;146bit = 1 << (start % NBBY);147for (i = start; i < end; i++) {148if ((bbp[loc] & bit) != 0)149break;150if ((i & (NBBY - 1)) != (NBBY - 1))151bit <<= 1;152else {153loc++;154bit = 1;155}156}157forw = i - start;158159/* Find the size of the cluster going backward. */160start = bno - 1;161end = start - fs->e2fs_contigsumsize;162if (end < 0)163end = -1;164loc = start / NBBY;165bit = 1 << (start % NBBY);166for (i = start; i > end; i--) {167if ((bbp[loc] & bit) != 0)168break;169if ((i & (NBBY - 1)) != 0)170bit >>= 1;171else {172loc--;173bit = 1 << (NBBY - 1);174}175}176back = start - i;177178/*179* Account for old cluster and the possibly new forward and180* back clusters.181*/182i = back + forw + 1;183if (i > fs->e2fs_contigsumsize)184i = fs->e2fs_contigsumsize;185sump[i] += cnt;186if (back > 0)187sump[back] -= cnt;188if (forw > 0)189sump[forw] -= cnt;190191/* Update cluster summary information. */192lp = &sump[fs->e2fs_contigsumsize];193for (i = fs->e2fs_contigsumsize; i > 0; i--)194if (*lp-- > 0)195break;196fs->e2fs_maxcluster[cg] = i;197}198199200