OpenCores
URL https://opencores.org/ocsvn/eco32/eco32/trunk

Subversion Repositories eco32

[/] [eco32/] [tags/] [eco32-0.22/] [disk/] [tools/] [fs-NetBSD/] [makefs/] [ffs_subr.c] - Blame information for rev 148

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 17 hellwig
/*      $NetBSD: ffs_subr.c,v 1.47 2011/08/14 12:37:09 christos Exp $   */
2
 
3
/*
4
 * Copyright (c) 1982, 1986, 1989, 1993
5
 *      The Regents of the University of California.  All rights reserved.
6
 *
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions
9
 * are met:
10
 * 1. Redistributions of source code must retain the above copyright
11
 *    notice, this list of conditions and the following disclaimer.
12
 * 2. Redistributions in binary form must reproduce the above copyright
13
 *    notice, this list of conditions and the following disclaimer in the
14
 *    documentation and/or other materials provided with the distribution.
15
 * 3. Neither the name of the University nor the names of its contributors
16
 *    may be used to endorse or promote products derived from this software
17
 *    without specific prior written permission.
18
 *
19
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29
 * SUCH DAMAGE.
30
 *
31
 *      @(#)ffs_subr.c  8.5 (Berkeley) 3/21/95
32
 */
33
 
34
#if HAVE_NBTOOL_CONFIG_H
35
#include "nbtool_config.h"
36
#endif
37
 
38
#include <sys/cdefs.h>
39
#if defined(__KERNEL_RCSID) && !defined(__lint)
40
__KERNEL_RCSID(0, "$NetBSD: ffs_subr.c,v 1.47 2011/08/14 12:37:09 christos Exp $");
41
#endif
42
 
43
#include <sys/param.h>
44
 
45
#include "common.h"
46
 
47
/* in ffs_tables.c */
48
extern const int inside[], around[];
49
extern const u_char * const fragtbl[];
50
 
51
#ifndef _KERNEL
52
#define FFS_EI /* always include byteswapped filesystems support */
53
#endif
54
#include "fs.h"
55
#include "dinode.h"
56
#include "ufs_inode.h"
57
#include "ffs_extern.h"
58
#include "ufs_bswap.h"
59
 
60
#ifndef _KERNEL
61
#include "dinode.h"
62
void    panic(const char *, ...)
63
    __attribute__((__noreturn__,__format__(__printf__,1,2)));
64
 
65
#else   /* _KERNEL */
66
#include <sys/systm.h>
67
#include <sys/vnode.h>
68
#include <sys/mount.h>
69
#include <sys/buf.h>
70
#include <sys/inttypes.h>
71
#include <sys/pool.h>
72
#include <sys/fstrans.h>
73
#include <ufs/ufs/inode.h>
74
#include <ufs/ufs/ufsmount.h>
75
#include <ufs/ufs/ufs_extern.h>
76
 
77
/*
78
 * Load up the contents of an inode and copy the appropriate pieces
79
 * to the incore copy.
80
 */
81
void
82
ffs_load_inode(struct buf *bp, struct inode *ip, struct fs *fs, ino_t ino)
83
{
84
        struct ufs1_dinode *dp1;
85
        struct ufs2_dinode *dp2;
86
 
87
        if (ip->i_ump->um_fstype == UFS1) {
88
                dp1 = (struct ufs1_dinode *)bp->b_data + ino_to_fsbo(fs, ino);
89
#ifdef FFS_EI
90
                if (UFS_FSNEEDSWAP(fs))
91
                        ffs_dinode1_swap(dp1, ip->i_din.ffs1_din);
92
                else
93
#endif
94
                *ip->i_din.ffs1_din = *dp1;
95
 
96
                ip->i_mode = ip->i_ffs1_mode;
97
                ip->i_nlink = ip->i_ffs1_nlink;
98
                ip->i_size = ip->i_ffs1_size;
99
                ip->i_flags = ip->i_ffs1_flags;
100
                ip->i_gen = ip->i_ffs1_gen;
101
                ip->i_uid = ip->i_ffs1_uid;
102
                ip->i_gid = ip->i_ffs1_gid;
103
        } else {
104
                dp2 = (struct ufs2_dinode *)bp->b_data + ino_to_fsbo(fs, ino);
105
#ifdef FFS_EI
106
                if (UFS_FSNEEDSWAP(fs))
107
                        ffs_dinode2_swap(dp2, ip->i_din.ffs2_din);
108
                else
109
#endif
110
                *ip->i_din.ffs2_din = *dp2;
111
 
112
                ip->i_mode = ip->i_ffs2_mode;
113
                ip->i_nlink = ip->i_ffs2_nlink;
114
                ip->i_size = ip->i_ffs2_size;
115
                ip->i_flags = ip->i_ffs2_flags;
116
                ip->i_gen = ip->i_ffs2_gen;
117
                ip->i_uid = ip->i_ffs2_uid;
118
                ip->i_gid = ip->i_ffs2_gid;
119
        }
120
}
121
 
122
int
123
ffs_getblk(struct vnode *vp, daddr_t lblkno, daddr_t blkno, int size,
124
    bool clearbuf, buf_t **bpp)
125
{
126
        int error = 0;
127
 
128
        KASSERT(blkno >= 0 || blkno == FFS_NOBLK);
129
 
130
        if ((*bpp = getblk(vp, lblkno, size, 0, 0)) == NULL)
131
                return ENOMEM;
132
        if (blkno != FFS_NOBLK)
133
                (*bpp)->b_blkno = blkno;
134
        if (clearbuf)
135
                clrbuf(*bpp);
136
        if ((*bpp)->b_blkno >= 0 && (error = fscow_run(*bpp, false)) != 0)
137
                brelse(*bpp, BC_INVAL);
138
        return error;
139
}
140
 
141
#endif  /* _KERNEL */
142
 
143
/*
144
 * Update the frsum fields to reflect addition or deletion
145
 * of some frags.
146
 */
147
void
148
ffs_fragacct(struct fs *fs, int fragmap, int32_t fraglist[], int cnt,
149
    int needswap)
150
{
151
        int inblk;
152
        int field, subfield;
153
        int siz, pos;
154
 
155
        inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1;
156
        fragmap <<= 1;
157
        for (siz = 1; siz < fs->fs_frag; siz++) {
158
                if ((inblk & (1 << (siz + (fs->fs_frag & (NBBY - 1))))) == 0)
159
                        continue;
160
                field = around[siz];
161
                subfield = inside[siz];
162
                for (pos = siz; pos <= fs->fs_frag; pos++) {
163
                        if ((fragmap & field) == subfield) {
164
                                fraglist[siz] = ufs_rw32(
165
                                    ufs_rw32(fraglist[siz], needswap) + cnt,
166
                                    needswap);
167
                                pos += siz;
168
                                field <<= siz;
169
                                subfield <<= siz;
170
                        }
171
                        field <<= 1;
172
                        subfield <<= 1;
173
                }
174
        }
175
}
176
 
177
/*
178
 * block operations
179
 *
180
 * check if a block is available
181
 *  returns true if all the correponding bits in the free map are 1
182
 *  returns false if any corresponding bit in the free map is 0
183
 */
184
int
185
ffs_isblock(struct fs *fs, u_char *cp, int32_t h)
186
{
187
        u_char mask;
188
 
189
        switch ((int)fs->fs_fragshift) {
190
        case 3:
191
                return (cp[h] == 0xff);
192
        case 2:
193
                mask = 0x0f << ((h & 0x1) << 2);
194
                return ((cp[h >> 1] & mask) == mask);
195
        case 1:
196
                mask = 0x03 << ((h & 0x3) << 1);
197
                return ((cp[h >> 2] & mask) == mask);
198
        case 0:
199
                mask = 0x01 << (h & 0x7);
200
                return ((cp[h >> 3] & mask) == mask);
201
        default:
202
                panic("ffs_isblock: unknown fs_fragshift %d",
203
                    (int)fs->fs_fragshift);
204
        }
205
}
206
 
207
/*
208
 * check if a block is completely allocated
209
 *  returns true if all the corresponding bits in the free map are 0
210
 *  returns false if any corresponding bit in the free map is 1
211
 */
212
int
213
ffs_isfreeblock(struct fs *fs, u_char *cp, int32_t h)
214
{
215
 
216
        switch ((int)fs->fs_fragshift) {
217
        case 3:
218
                return (cp[h] == 0);
219
        case 2:
220
                return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0);
221
        case 1:
222
                return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0);
223
        case 0:
224
                return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0);
225
        default:
226
                panic("ffs_isfreeblock: unknown fs_fragshift %d",
227
                    (int)fs->fs_fragshift);
228
        }
229
}
230
 
231
/*
232
 * take a block out of the map
233
 */
234
void
235
ffs_clrblock(struct fs *fs, u_char *cp, int32_t h)
236
{
237
 
238
        switch ((int)fs->fs_fragshift) {
239
        case 3:
240
                cp[h] = 0;
241
                return;
242
        case 2:
243
                cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
244
                return;
245
        case 1:
246
                cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
247
                return;
248
        case 0:
249
                cp[h >> 3] &= ~(0x01 << (h & 0x7));
250
                return;
251
        default:
252
                panic("ffs_clrblock: unknown fs_fragshift %d",
253
                    (int)fs->fs_fragshift);
254
        }
255
}
256
 
257
/*
258
 * put a block into the map
259
 */
260
void
261
ffs_setblock(struct fs *fs, u_char *cp, int32_t h)
262
{
263
 
264
        switch ((int)fs->fs_fragshift) {
265
        case 3:
266
                cp[h] = 0xff;
267
                return;
268
        case 2:
269
                cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
270
                return;
271
        case 1:
272
                cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
273
                return;
274
        case 0:
275
                cp[h >> 3] |= (0x01 << (h & 0x7));
276
                return;
277
        default:
278
                panic("ffs_setblock: unknown fs_fragshift %d",
279
                    (int)fs->fs_fragshift);
280
        }
281
}
282
 
283
/*
284
 * Update the cluster map because of an allocation or free.
285
 *
286
 * Cnt == 1 means free; cnt == -1 means allocating.
287
 */
288
void
289
ffs_clusteracct(struct fs *fs, struct cg *cgp, int32_t blkno, int cnt)
290
{
291
        int32_t *sump;
292
        int32_t *lp;
293
        u_char *freemapp, *mapp;
294
        int i, start, end, forw, back, map, bit;
295
#ifdef FFS_EI
296
        const int needswap = UFS_FSNEEDSWAP(fs);
297
#endif
298
 
299
        /* KASSERT(mutex_owned(&ump->um_lock)); */
300
 
301
        if (fs->fs_contigsumsize <= 0)
302
                return;
303
        freemapp = cg_clustersfree(cgp, needswap);
304
        sump = cg_clustersum(cgp, needswap);
305
        /*
306
         * Allocate or clear the actual block.
307
         */
308
        if (cnt > 0)
309
                setbit(freemapp, blkno);
310
        else
311
                clrbit(freemapp, blkno);
312
        /*
313
         * Find the size of the cluster going forward.
314
         */
315
        start = blkno + 1;
316
        end = start + fs->fs_contigsumsize;
317
        if ((uint32_t)end >= ufs_rw32(cgp->cg_nclusterblks, needswap))
318
                end = ufs_rw32(cgp->cg_nclusterblks, needswap);
319
        mapp = &freemapp[start / NBBY];
320
        map = *mapp++;
321
        bit = 1 << (start % NBBY);
322
        for (i = start; i < end; i++) {
323
                if ((map & bit) == 0)
324
                        break;
325
                if ((i & (NBBY - 1)) != (NBBY - 1)) {
326
                        bit <<= 1;
327
                } else {
328
                        map = *mapp++;
329
                        bit = 1;
330
                }
331
        }
332
        forw = i - start;
333
        /*
334
         * Find the size of the cluster going backward.
335
         */
336
        start = blkno - 1;
337
        end = start - fs->fs_contigsumsize;
338
        if (end < 0)
339
                end = -1;
340
        mapp = &freemapp[start / NBBY];
341
        map = *mapp--;
342
        bit = 1 << (start % NBBY);
343
        for (i = start; i > end; i--) {
344
                if ((map & bit) == 0)
345
                        break;
346
                if ((i & (NBBY - 1)) != 0) {
347
                        bit >>= 1;
348
                } else {
349
                        map = *mapp--;
350
                        bit = 1 << (NBBY - 1);
351
                }
352
        }
353
        back = start - i;
354
        /*
355
         * Account for old cluster and the possibly new forward and
356
         * back clusters.
357
         */
358
        i = back + forw + 1;
359
        if (i > fs->fs_contigsumsize)
360
                i = fs->fs_contigsumsize;
361
        ufs_add32(sump[i], cnt, needswap);
362
        if (back > 0)
363
                ufs_add32(sump[back], -cnt, needswap);
364
        if (forw > 0)
365
                ufs_add32(sump[forw], -cnt, needswap);
366
 
367
        /*
368
         * Update cluster summary information.
369
         */
370
        lp = &sump[fs->fs_contigsumsize];
371
        for (i = fs->fs_contigsumsize; i > 0; i--)
372
                if (ufs_rw32(*lp--, needswap) > 0)
373
                        break;
374
#if defined(_KERNEL)
375
        fs->fs_maxcluster[ufs_rw32(cgp->cg_cgx, needswap)] = i;
376
#endif
377
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.