Remove the thread argument from ext2_quotaoff(), ext2_flushfiles(),
[dragonfly.git] / sys / vfs / ufs / ffs_subr.c
1 /*
2  * Copyright (c) 1982, 1986, 1989, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *      @(#)ffs_subr.c  8.5 (Berkeley) 3/21/95
34  * $FreeBSD: src/sys/ufs/ffs/ffs_subr.c,v 1.25 1999/12/29 04:55:04 peter Exp $
35  * $DragonFly: src/sys/vfs/ufs/ffs_subr.c,v 1.12 2006/04/27 23:28:37 dillon Exp $
36  */
37
38 #include <sys/param.h>
39
40 #ifndef _KERNEL
41 #include "dinode.h"
42 #include "fs.h"
43 #else
44 #include "opt_ddb.h"
45
46 #include <sys/systm.h>
47 #include <sys/lock.h>
48 #include <sys/vnode.h>
49 #include <sys/buf.h>
50 #include <sys/ucred.h>
51
52 #include "quota.h"
53 #include "inode.h"
54 #include "fs.h"
55 #include "ffs_extern.h"
56
57 /*
58  * Return buffer with the contents of block "offset" from the beginning of
59  * directory "ip".  If "res" is non-zero, fill it in with a pointer to the
60  * remaining space in the directory.
61  */
62 int
63 ffs_blkatoff(struct vnode *vp, off_t offset, char **res, struct buf **bpp)
64 {
65         struct inode *ip;
66         struct fs *fs;
67         struct buf *bp;
68         ufs_daddr_t lbn;
69         int bsize, error;
70
71         ip = VTOI(vp);
72         fs = ip->i_fs;
73         lbn = lblkno(fs, offset);
74         bsize = blksize(fs, ip, lbn);
75
76         *bpp = NULL;
77         error = bread(vp, lblktodoff(fs, lbn), bsize, &bp);
78         if (error) {
79                 brelse(bp);
80                 return (error);
81         }
82         if (res)
83                 *res = (char *)bp->b_data + blkoff(fs, offset);
84         *bpp = bp;
85         return (0);
86 }
87 #endif
88
89 /*
90  * Update the frsum fields to reflect addition or deletion
91  * of some frags.
92  */
93 void
94 ffs_fragacct(struct fs *fs, int fragmap, int32_t fraglist[], int cnt)
95 {
96         int inblk;
97         int field, subfield;
98         int siz, pos;
99
100         /*
101          * inblk represents a bitmap of fragment sizes which may be
102          * contained in the data 'fragmap'.  e.g. if a fragment of size
103          * 1 is available, bit 0 would be set.  inblk is shifted left
104          * by one so we do not have to calculate (1 << (siz - 1)).
105          *
106          * fragment represents the data pattern we are trying to decipher,
107          * we shift it left by one to align it with the 'around' and 'inside'
108          * masks.
109          *
110          * around represents the bits around the subfield and is a mask.
111          * inside represents what we must match within the mask, it is
112          * basically the mask with the first and last bit set to 0, allowing
113          * us to represent a whole fragment.
114          *
115          * When we find a match we bump our position by the size of the
116          * matching fragment, then bump the position again:
117          *
118          * 010101010 fragmap (shifted left by 1)
119          *       111 around mask
120          *       010 inside mask
121          *      111     (shifted by siz)
122          *      010
123          *     111      (shifted again)
124          *     010
125          */
126         inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1;
127         fragmap <<= 1;
128         for (siz = 1; siz < fs->fs_frag; siz++) {
129                 if ((inblk & (1 << (siz + (fs->fs_frag % NBBY)))) == 0)
130                         continue;
131                 field = around[siz];
132                 subfield = inside[siz];
133                 for (pos = siz; pos <= fs->fs_frag; pos++) {
134                         if ((fragmap & field) == subfield) {
135                                 fraglist[siz] += cnt;
136                                 pos += siz;
137                                 field <<= siz;
138                                 subfield <<= siz;
139                         }
140                         field <<= 1;
141                         subfield <<= 1;
142                 }
143         }
144 }
145
146 /*
147  * block operations
148  *
149  * check if a block is available
150  */
151 int
152 ffs_isblock(struct fs *fs, unsigned char *cp, ufs_daddr_t h)
153 {
154         unsigned char mask;
155
156         switch ((int)fs->fs_frag) {
157         case 8:
158                 return (cp[h] == 0xff);
159         case 4:
160                 mask = 0x0f << ((h & 0x1) << 2);
161                 return ((cp[h >> 1] & mask) == mask);
162         case 2:
163                 mask = 0x03 << ((h & 0x3) << 1);
164                 return ((cp[h >> 2] & mask) == mask);
165         case 1:
166                 mask = 0x01 << (h & 0x7);
167                 return ((cp[h >> 3] & mask) == mask);
168         default:
169                 panic("ffs_isblock");
170         }
171 }
172
173 /*
174  * check if a block is free
175  */
176 int
177 ffs_isfreeblock(struct fs *fs, unsigned char *cp, ufs_daddr_t h)
178 {
179         switch ((int)fs->fs_frag) {
180         case 8:
181                 return (cp[h] == 0);
182         case 4:
183                 return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0);
184         case 2:
185                 return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0);
186         case 1:
187                 return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0);
188         default:
189                 panic("ffs_isfreeblock");
190         }
191 }
192
193 /*
194  * take a block out of the map
195  */
196 void
197 ffs_clrblock(struct fs *fs, u_char *cp, ufs_daddr_t h)
198 {
199         switch ((int)fs->fs_frag) {
200         case 8:
201                 cp[h] = 0;
202                 return;
203         case 4:
204                 cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
205                 return;
206         case 2:
207                 cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
208                 return;
209         case 1:
210                 cp[h >> 3] &= ~(0x01 << (h & 0x7));
211                 return;
212         default:
213                 panic("ffs_clrblock");
214         }
215 }
216
217 /*
218  * put a block into the map
219  */
220 void
221 ffs_setblock(struct fs *fs, unsigned char *cp, ufs_daddr_t h)
222 {
223         switch ((int)fs->fs_frag) {
224         case 8:
225                 cp[h] = 0xff;
226                 return;
227         case 4:
228                 cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
229                 return;
230         case 2:
231                 cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
232                 return;
233         case 1:
234                 cp[h >> 3] |= (0x01 << (h & 0x7));
235                 return;
236         default:
237                 panic("ffs_setblock");
238         }
239 }