]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/ufs/ffs/ffs_subr.c
MFV r302649: 7016 arc_available_memory is not 32-bit safe
[FreeBSD/FreeBSD.git] / sys / ufs / ffs / ffs_subr.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
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 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 #include <sys/param.h>
38
39 #ifndef _KERNEL
40 #include <stdio.h>
41 #include <string.h>
42 #include <stdlib.h>
43 #include <time.h>
44 #include <sys/errno.h>
45 #include <ufs/ufs/dinode.h>
46 #include <ufs/ffs/fs.h>
47
48 struct malloc_type;
49 #define UFS_MALLOC(size, type, flags) malloc(size)
50 #define UFS_FREE(ptr, type) free(ptr)
51 #define UFS_TIME time(NULL)
52
53 #else /* _KERNEL */
54 #include <sys/systm.h>
55 #include <sys/lock.h>
56 #include <sys/malloc.h>
57 #include <sys/mount.h>
58 #include <sys/vnode.h>
59 #include <sys/bio.h>
60 #include <sys/buf.h>
61 #include <sys/ucred.h>
62
63 #include <ufs/ufs/quota.h>
64 #include <ufs/ufs/inode.h>
65 #include <ufs/ufs/extattr.h>
66 #include <ufs/ufs/ufsmount.h>
67 #include <ufs/ufs/ufs_extern.h>
68 #include <ufs/ffs/ffs_extern.h>
69 #include <ufs/ffs/fs.h>
70
71 #define UFS_MALLOC(size, type, flags) malloc(size, type, flags)
72 #define UFS_FREE(ptr, type) free(ptr, type)
73 #define UFS_TIME time_second
74
75 /*
76  * Return buffer with the contents of block "offset" from the beginning of
77  * directory "ip".  If "res" is non-zero, fill it in with a pointer to the
78  * remaining space in the directory.
79  */
80 int
81 ffs_blkatoff(struct vnode *vp, off_t offset, char **res, struct buf **bpp)
82 {
83         struct inode *ip;
84         struct fs *fs;
85         struct buf *bp;
86         ufs_lbn_t lbn;
87         int bsize, error;
88
89         ip = VTOI(vp);
90         fs = ITOFS(ip);
91         lbn = lblkno(fs, offset);
92         bsize = blksize(fs, ip, lbn);
93
94         *bpp = NULL;
95         error = bread(vp, lbn, bsize, NOCRED, &bp);
96         if (error) {
97                 brelse(bp);
98                 return (error);
99         }
100         if (res)
101                 *res = (char *)bp->b_data + blkoff(fs, offset);
102         *bpp = bp;
103         return (0);
104 }
105
106 /*
107  * Load up the contents of an inode and copy the appropriate pieces
108  * to the incore copy.
109  */
110 void
111 ffs_load_inode(struct buf *bp, struct inode *ip, struct fs *fs, ino_t ino)
112 {
113
114         if (I_IS_UFS1(ip)) {
115                 *ip->i_din1 =
116                     *((struct ufs1_dinode *)bp->b_data + ino_to_fsbo(fs, ino));
117                 ip->i_mode = ip->i_din1->di_mode;
118                 ip->i_nlink = ip->i_din1->di_nlink;
119                 ip->i_size = ip->i_din1->di_size;
120                 ip->i_flags = ip->i_din1->di_flags;
121                 ip->i_gen = ip->i_din1->di_gen;
122                 ip->i_uid = ip->i_din1->di_uid;
123                 ip->i_gid = ip->i_din1->di_gid;
124         } else {
125                 *ip->i_din2 =
126                     *((struct ufs2_dinode *)bp->b_data + ino_to_fsbo(fs, ino));
127                 ip->i_mode = ip->i_din2->di_mode;
128                 ip->i_nlink = ip->i_din2->di_nlink;
129                 ip->i_size = ip->i_din2->di_size;
130                 ip->i_flags = ip->i_din2->di_flags;
131                 ip->i_gen = ip->i_din2->di_gen;
132                 ip->i_uid = ip->i_din2->di_uid;
133                 ip->i_gid = ip->i_din2->di_gid;
134         }
135 }
136 #endif /* KERNEL */
137
138 /*
139  * These are the low-level functions that actually read and write
140  * the superblock and its associated data.
141  */
142 static off_t sblock_try[] = SBLOCKSEARCH;
143 static int readsuper(void *, struct fs **, off_t,
144         int (*)(void *, off_t, void **, int));
145
146 /*
147  * Read a superblock from the devfd device.
148  *
149  * If an alternate superblock is specified, it is read. Otherwise the
150  * set of locations given in the SBLOCKSEARCH list is searched for a
151  * superblock. Memory is allocated for the superblock by the readfunc and
152  * is returned. If filltype is non-NULL, additional memory is allocated
153  * of type filltype and filled in with the superblock summary information.
154  *
155  * If a superblock is found, zero is returned. Otherwise one of the
156  * following error values is returned:
157  *     EIO: non-existent or truncated superblock.
158  *     EIO: error reading summary information.
159  *     ENOENT: no usable known superblock found.
160  *     ENOSPC: failed to allocate space for the superblock.
161  *     EINVAL: The previous newfs operation on this volume did not complete.
162  *         The administrator must complete newfs before using this volume.
163  */
164 int
165 ffs_sbget(void *devfd, struct fs **fsp, off_t altsuperblock,
166     struct malloc_type *filltype,
167     int (*readfunc)(void *devfd, off_t loc, void **bufp, int size))
168 {
169         struct fs *fs;
170         int i, ret, size, blks;
171         uint8_t *space;
172         int32_t *lp;
173         char *buf;
174
175         *fsp = NULL;
176         if (altsuperblock != -1) {
177                 ret = readsuper(devfd, fsp, altsuperblock, readfunc);
178                 if (*fsp != NULL)
179                         (*fsp)->fs_csp = NULL;
180                 if (ret != 0)
181                         return (ret);
182         } else {
183                 for (i = 0; sblock_try[i] != -1; i++) {
184                         ret = readsuper(devfd, fsp, sblock_try[i], readfunc);
185                         if (*fsp != NULL)
186                                 (*fsp)->fs_csp = NULL;
187                         if (ret == 0)
188                                 break;
189                         if (ret == ENOENT)
190                                 continue;
191                         return (ret);
192                 }
193                 if (sblock_try[i] == -1)
194                         return (ENOENT);
195         }
196
197         /*
198          * Not filling in summary information, return.
199          */
200         if (filltype == NULL)
201                 return (0);
202
203         /*
204          * Read in the superblock summary information.
205          */
206         fs = *fsp;
207         size = fs->fs_cssize;
208         blks = howmany(size, fs->fs_fsize);
209         if (fs->fs_contigsumsize > 0)
210                 size += fs->fs_ncg * sizeof(int32_t);
211         size += fs->fs_ncg * sizeof(u_int8_t);
212         space = UFS_MALLOC(size, filltype, M_WAITOK);
213         fs->fs_csp = (struct csum *)space;
214         for (i = 0; i < blks; i += fs->fs_frag) {
215                 size = fs->fs_bsize;
216                 if (i + fs->fs_frag > blks)
217                         size = (blks - i) * fs->fs_fsize;
218                 buf = NULL;
219                 ret = (*readfunc)(devfd,
220                     dbtob(fsbtodb(fs, fs->fs_csaddr + i)), (void **)&buf, size);
221                 if (ret) {
222                         UFS_FREE(buf, filltype);
223                         UFS_FREE(fs->fs_csp, filltype);
224                         fs->fs_csp = NULL;
225                         return (ret);
226                 }
227                 memcpy(space, buf, size);
228                 UFS_FREE(buf, filltype);
229                 space += size;
230         }
231         if (fs->fs_contigsumsize > 0) {
232                 fs->fs_maxcluster = lp = (int32_t *)space;
233                 for (i = 0; i < fs->fs_ncg; i++)
234                         *lp++ = fs->fs_contigsumsize;
235                 space = (uint8_t *)lp;
236         }
237         size = fs->fs_ncg * sizeof(u_int8_t);
238         fs->fs_contigdirs = (u_int8_t *)space;
239         bzero(fs->fs_contigdirs, size);
240         return (0);
241 }
242
243 /*
244  * Try to read a superblock from the location specified by sblockloc.
245  * Return zero on success or an errno on failure.
246  */
247 static int
248 readsuper(void *devfd, struct fs **fsp, off_t sblockloc,
249     int (*readfunc)(void *devfd, off_t loc, void **bufp, int size))
250 {
251         struct fs *fs;
252         int error;
253
254         error = (*readfunc)(devfd, sblockloc, (void **)fsp, SBLOCKSIZE);
255         if (error != 0)
256                 return (error);
257         fs = *fsp;
258         if (fs->fs_magic == FS_BAD_MAGIC)
259                 return (EINVAL);
260         if (((fs->fs_magic == FS_UFS1_MAGIC && sblockloc <= SBLOCK_UFS1) ||
261              (fs->fs_magic == FS_UFS2_MAGIC &&
262               sblockloc == fs->fs_sblockloc)) &&
263             fs->fs_ncg >= 1 &&
264             fs->fs_bsize >= MINBSIZE &&
265             fs->fs_bsize <= MAXBSIZE &&
266             fs->fs_bsize >= roundup(sizeof(struct fs), DEV_BSIZE)) {
267                 /* Have to set for old filesystems that predate this field */
268                 fs->fs_sblockactualloc = sblockloc;
269                 return (0);
270         }
271         return (ENOENT);
272 }
273
274 /*
275  * Write a superblock to the devfd device from the memory pointed to by fs.
276  * Write out the superblock summary information if it is present.
277  *
278  * If the write is successful, zero is returned. Otherwise one of the
279  * following error values is returned:
280  *     EIO: failed to write superblock.
281  *     EIO: failed to write superblock summary information.
282  */
283 int
284 ffs_sbput(void *devfd, struct fs *fs, off_t loc,
285     int (*writefunc)(void *devfd, off_t loc, void *buf, int size))
286 {
287         int i, error, blks, size;
288         uint8_t *space;
289
290         /*
291          * If there is summary information, write it first, so if there
292          * is an error, the superblock will not be marked as clean.
293          */
294         if (fs->fs_csp != NULL) {
295                 blks = howmany(fs->fs_cssize, fs->fs_fsize);
296                 space = (uint8_t *)fs->fs_csp;
297                 for (i = 0; i < blks; i += fs->fs_frag) {
298                         size = fs->fs_bsize;
299                         if (i + fs->fs_frag > blks)
300                                 size = (blks - i) * fs->fs_fsize;
301                         if ((error = (*writefunc)(devfd,
302                              dbtob(fsbtodb(fs, fs->fs_csaddr + i)),
303                              space, size)) != 0)
304                                 return (error);
305                         space += size;
306                 }
307         }
308         fs->fs_fmod = 0;
309         fs->fs_time = UFS_TIME;
310         if ((error = (*writefunc)(devfd, loc, fs, fs->fs_sbsize)) != 0)
311                 return (error);
312         return (0);
313 }
314
315 /*
316  * Update the frsum fields to reflect addition or deletion
317  * of some frags.
318  */
319 void
320 ffs_fragacct(struct fs *fs, int fragmap, int32_t fraglist[], int cnt)
321 {
322         int inblk;
323         int field, subfield;
324         int siz, pos;
325
326         inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1;
327         fragmap <<= 1;
328         for (siz = 1; siz < fs->fs_frag; siz++) {
329                 if ((inblk & (1 << (siz + (fs->fs_frag % NBBY)))) == 0)
330                         continue;
331                 field = around[siz];
332                 subfield = inside[siz];
333                 for (pos = siz; pos <= fs->fs_frag; pos++) {
334                         if ((fragmap & field) == subfield) {
335                                 fraglist[siz] += cnt;
336                                 pos += siz;
337                                 field <<= siz;
338                                 subfield <<= siz;
339                         }
340                         field <<= 1;
341                         subfield <<= 1;
342                 }
343         }
344 }
345
346 /*
347  * block operations
348  *
349  * check if a block is available
350  */
351 int
352 ffs_isblock(struct fs *fs, unsigned char *cp, ufs1_daddr_t h)
353 {
354         unsigned char mask;
355
356         switch ((int)fs->fs_frag) {
357         case 8:
358                 return (cp[h] == 0xff);
359         case 4:
360                 mask = 0x0f << ((h & 0x1) << 2);
361                 return ((cp[h >> 1] & mask) == mask);
362         case 2:
363                 mask = 0x03 << ((h & 0x3) << 1);
364                 return ((cp[h >> 2] & mask) == mask);
365         case 1:
366                 mask = 0x01 << (h & 0x7);
367                 return ((cp[h >> 3] & mask) == mask);
368         default:
369 #ifdef _KERNEL
370                 panic("ffs_isblock");
371 #endif
372                 break;
373         }
374         return (0);
375 }
376
377 /*
378  * check if a block is free
379  */
380 int
381 ffs_isfreeblock(struct fs *fs, u_char *cp, ufs1_daddr_t h)
382 {
383  
384         switch ((int)fs->fs_frag) {
385         case 8:
386                 return (cp[h] == 0);
387         case 4:
388                 return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0);
389         case 2:
390                 return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0);
391         case 1:
392                 return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0);
393         default:
394 #ifdef _KERNEL
395                 panic("ffs_isfreeblock");
396 #endif
397                 break;
398         }
399         return (0);
400 }
401
402 /*
403  * take a block out of the map
404  */
405 void
406 ffs_clrblock(struct fs *fs, u_char *cp, ufs1_daddr_t h)
407 {
408
409         switch ((int)fs->fs_frag) {
410         case 8:
411                 cp[h] = 0;
412                 return;
413         case 4:
414                 cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
415                 return;
416         case 2:
417                 cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
418                 return;
419         case 1:
420                 cp[h >> 3] &= ~(0x01 << (h & 0x7));
421                 return;
422         default:
423 #ifdef _KERNEL
424                 panic("ffs_clrblock");
425 #endif
426                 break;
427         }
428 }
429
430 /*
431  * put a block into the map
432  */
433 void
434 ffs_setblock(struct fs *fs, unsigned char *cp, ufs1_daddr_t h)
435 {
436
437         switch ((int)fs->fs_frag) {
438
439         case 8:
440                 cp[h] = 0xff;
441                 return;
442         case 4:
443                 cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
444                 return;
445         case 2:
446                 cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
447                 return;
448         case 1:
449                 cp[h >> 3] |= (0x01 << (h & 0x7));
450                 return;
451         default:
452 #ifdef _KERNEL
453                 panic("ffs_setblock");
454 #endif
455                 break;
456         }
457 }
458
459 /*
460  * Update the cluster map because of an allocation or free.
461  *
462  * Cnt == 1 means free; cnt == -1 means allocating.
463  */
464 void
465 ffs_clusteracct(struct fs *fs, struct cg *cgp, ufs1_daddr_t blkno, int cnt)
466 {
467         int32_t *sump;
468         int32_t *lp;
469         u_char *freemapp, *mapp;
470         int i, start, end, forw, back, map, bit;
471
472         if (fs->fs_contigsumsize <= 0)
473                 return;
474         freemapp = cg_clustersfree(cgp);
475         sump = cg_clustersum(cgp);
476         /*
477          * Allocate or clear the actual block.
478          */
479         if (cnt > 0)
480                 setbit(freemapp, blkno);
481         else
482                 clrbit(freemapp, blkno);
483         /*
484          * Find the size of the cluster going forward.
485          */
486         start = blkno + 1;
487         end = start + fs->fs_contigsumsize;
488         if (end >= cgp->cg_nclusterblks)
489                 end = cgp->cg_nclusterblks;
490         mapp = &freemapp[start / NBBY];
491         map = *mapp++;
492         bit = 1 << (start % NBBY);
493         for (i = start; i < end; i++) {
494                 if ((map & bit) == 0)
495                         break;
496                 if ((i & (NBBY - 1)) != (NBBY - 1)) {
497                         bit <<= 1;
498                 } else {
499                         map = *mapp++;
500                         bit = 1;
501                 }
502         }
503         forw = i - start;
504         /*
505          * Find the size of the cluster going backward.
506          */
507         start = blkno - 1;
508         end = start - fs->fs_contigsumsize;
509         if (end < 0)
510                 end = -1;
511         mapp = &freemapp[start / NBBY];
512         map = *mapp--;
513         bit = 1 << (start % NBBY);
514         for (i = start; i > end; i--) {
515                 if ((map & bit) == 0)
516                         break;
517                 if ((i & (NBBY - 1)) != 0) {
518                         bit >>= 1;
519                 } else {
520                         map = *mapp--;
521                         bit = 1 << (NBBY - 1);
522                 }
523         }
524         back = start - i;
525         /*
526          * Account for old cluster and the possibly new forward and
527          * back clusters.
528          */
529         i = back + forw + 1;
530         if (i > fs->fs_contigsumsize)
531                 i = fs->fs_contigsumsize;
532         sump[i] += cnt;
533         if (back > 0)
534                 sump[back] -= cnt;
535         if (forw > 0)
536                 sump[forw] -= cnt;
537         /*
538          * Update cluster summary information.
539          */
540         lp = &sump[fs->fs_contigsumsize];
541         for (i = fs->fs_contigsumsize; i > 0; i--)
542                 if (*lp-- > 0)
543                         break;
544         fs->fs_maxcluster[cgp->cg_cgx] = i;
545 }