]>
git.saurik.com Git - apple/xnu.git/blob - bsd/ufs/ffs/ffs_balloc.c
38cf2b72ac88f2c6ec71decaae85ea6c5963e557
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
24 * Copyright (c) 1982, 1986, 1989, 1993
25 * The Regents of the University of California. All rights reserved.
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 * 3. All advertising materials mentioning features or use of this software
36 * must display the following acknowledgement:
37 * This product includes software developed by the University of
38 * California, Berkeley and its contributors.
39 * 4. Neither the name of the University nor the names of its contributors
40 * may be used to endorse or promote products derived from this software
41 * without specific prior written permission.
43 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * @(#)ffs_balloc.c 8.8 (Berkeley) 6/16/95
58 #include <rev_endian_fs.h>
59 #include <sys/param.h>
60 #include <sys/systm.h>
64 #include <sys/vnode.h>
66 #include <sys/quota.h>
69 #include <sys/mount.h>
70 #endif /* REV_ENDIAN_FS */
74 #include <ufs/ufs/quota.h>
75 #include <ufs/ufs/inode.h>
76 #include <ufs/ufs/ufs_extern.h>
78 #include <ufs/ffs/fs.h>
79 #include <ufs/ffs/ffs_extern.h>
82 #include <ufs/ufs/ufs_byte_order.h>
83 #include <architecture/byte_order.h>
84 #endif /* REV_ENDIAN_FS */
87 * Balloc defines the structure of file system storage
88 * by allocating the physical blocks on a device given
89 * the inode and the logical block number in a file.
91 ffs_balloc(ip
, lbn
, size
, cred
, bpp
, flags
, blk_alloc
)
92 register struct inode
*ip
;
93 register ufs_daddr_t lbn
;
100 register struct fs
*fs
;
101 register ufs_daddr_t nb
;
102 struct buf
*bp
, *nbp
;
103 struct vnode
*vp
= ITOV(ip
);
104 struct indir indirs
[NIADDR
+ 2];
105 ufs_daddr_t newb
, *bap
, pref
;
106 int deallocated
, osize
, nsize
, num
, i
, error
;
107 ufs_daddr_t
*allocib
, *blkp
, *allocblk
, allociblk
[NIADDR
+ 1];
109 int alloc_buffer
= 1;
111 struct mount
*mp
=vp
->v_mount
;
112 int rev_endian
=(mp
->mnt_flag
& MNT_REVEND
);
113 #endif /* REV_ENDIAN_FS */
119 if (flags
& B_NOBUFF
)
126 * If the next write will extend the file into a new block,
127 * and the file is currently composed of a fragment
128 * this fragment has to be extended to be a full block.
130 nb
= lblkno(fs
, ip
->i_size
);
131 if (nb
< NDADDR
&& nb
< lbn
) {
132 /* the filesize prior to this write can fit in direct
133 * blocks (ie. fragmentaion is possibly done)
134 * we are now extending the file write beyond
135 * the block which has end of file prior to this write
137 osize
= blksize(fs
, ip
, nb
);
138 /* osize gives disk allocated size in the last block. It is
139 * either in fragments or a file system block size */
140 if (osize
< fs
->fs_bsize
&& osize
> 0) {
141 /* few fragments are already allocated,since the
142 * current extends beyond this block
143 * allocate the complete block as fragments are only
146 error
= ffs_realloccg(ip
, nb
,
147 ffs_blkpref(ip
, nb
, (int)nb
, &ip
->i_db
[0]),
148 osize
, (int)fs
->fs_bsize
, cred
, &bp
);
151 /* adjust the innode size we just grew */
152 /* it is in nb+1 as nb starts from 0 */
153 ip
->i_size
= (nb
+ 1) * fs
->fs_bsize
;
155 ubc_setsize(vp
, (off_t
)ip
->i_size
); /* XXX check error */
156 ip
->i_db
[nb
] = dbtofsb(fs
, bp
->b_blkno
);
157 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
158 if ((flags
& B_SYNC
) || (!alloc_buffer
)) {
160 SET(bp
->b_flags
, B_INVAL
);
164 /* note that bp is already released here */
168 * The first NDADDR blocks are direct blocks
172 if (nb
!= 0 && ip
->i_size
>= (lbn
+ 1) * fs
->fs_bsize
) {
174 error
= bread(vp
, lbn
, fs
->fs_bsize
, NOCRED
, &bp
);
185 * Consider need to reallocate a fragment.
187 osize
= fragroundup(fs
, blkoff(fs
, ip
->i_size
));
188 nsize
= fragroundup(fs
, size
);
189 if (nsize
<= osize
) {
191 error
= bread(vp
, lbn
, osize
, NOCRED
, &bp
);
196 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
201 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
205 error
= ffs_realloccg(ip
, lbn
,
206 ffs_blkpref(ip
, lbn
, (int)lbn
,
207 &ip
->i_db
[0]), osize
, nsize
, cred
, &bp
);
210 ip
->i_db
[lbn
] = dbtofsb(fs
, bp
->b_blkno
);
211 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
213 SET(bp
->b_flags
, B_INVAL
);
221 if (ip
->i_size
< (lbn
+ 1) * fs
->fs_bsize
)
222 nsize
= fragroundup(fs
, size
);
224 nsize
= fs
->fs_bsize
;
225 error
= ffs_alloc(ip
, lbn
,
226 ffs_blkpref(ip
, lbn
, (int)lbn
, &ip
->i_db
[0]),
231 bp
= getblk(vp
, lbn
, nsize
, 0, 0, BLK_WRITE
);
232 bp
->b_blkno
= fsbtodb(fs
, newb
);
233 if (flags
& B_CLRBUF
)
236 ip
->i_db
[lbn
] = newb
;
237 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
247 * Determine the number of levels of indirection.
250 if (error
= ufs_getlbns(vp
, lbn
, indirs
, &num
))
254 panic ("ffs_balloc: ufs_bmaparray returned indirect block\n");
257 * Fetch the first indirect block allocating if necessary.
260 nb
= ip
->i_ib
[indirs
[0].in_off
];
262 allocblk
= allociblk
;
264 pref
= ffs_blkpref(ip
, lbn
, 0, (ufs_daddr_t
*)0);
265 if (error
= ffs_alloc(ip
, lbn
, pref
, (int)fs
->fs_bsize
,
270 bp
= getblk(vp
, indirs
[1].in_lbn
, fs
->fs_bsize
, 0, 0, BLK_META
);
271 bp
->b_blkno
= fsbtodb(fs
, nb
);
274 * Write synchronously so that indirect blocks
275 * never point at garbage.
277 if (error
= bwrite(bp
))
279 allocib
= &ip
->i_ib
[indirs
[0].in_off
];
281 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
284 * Fetch through the indirect blocks, allocating as necessary.
287 error
= meta_bread(vp
,
288 indirs
[i
].in_lbn
, (int)fs
->fs_bsize
, NOCRED
, &bp
);
293 bap
= (ufs_daddr_t
*)bp
->b_data
;
296 nb
= NXSwapLong(bap
[indirs
[i
].in_off
]);
298 #endif /* REV_ENDIAN_FS */
299 nb
= bap
[indirs
[i
].in_off
];
302 #endif /* REV_ENDIAN_FS */
311 pref
= ffs_blkpref(ip
, lbn
, 0, (ufs_daddr_t
*)0);
313 ffs_alloc(ip
, lbn
, pref
, (int)fs
->fs_bsize
, cred
, &newb
)) {
319 nbp
= getblk(vp
, indirs
[i
].in_lbn
, fs
->fs_bsize
, 0, 0, BLK_META
);
320 nbp
->b_blkno
= fsbtodb(fs
, nb
);
323 * Write synchronously so that indirect blocks
324 * never point at garbage.
326 if (error
= bwrite(nbp
)) {
332 bap
[indirs
[i
- 1].in_off
] = NXSwapLong(nb
);
334 #endif /* REV_ENDIAN_FS */
335 bap
[indirs
[i
- 1].in_off
] = nb
;
338 #endif /* REV_ENDIAN_FS */
340 * If required, write synchronously, otherwise use
343 if (flags
& B_SYNC
) {
350 * Get the data block, allocating if necessary.
353 pref
= ffs_blkpref(ip
, lbn
, indirs
[i
].in_off
, &bap
[0]);
354 if (error
= ffs_alloc(ip
,
355 lbn
, pref
, (int)fs
->fs_bsize
, cred
, &newb
)) {
363 bap
[indirs
[i
].in_off
] = NXSwapLong(nb
);
365 #endif /* REV_ENDIAN_FS */
366 bap
[indirs
[i
].in_off
] = nb
;
369 #endif /* REV_ENDIAN_FS */
371 * If required, write synchronously, otherwise use
374 if ((flags
& B_SYNC
)) {
380 nbp
= getblk(vp
, lbn
, fs
->fs_bsize
, 0, 0, BLK_WRITE
);
381 nbp
->b_blkno
= fsbtodb(fs
, nb
);
382 if (flags
& B_CLRBUF
)
386 *blk_alloc
= fs
->fs_bsize
;
395 if (flags
& B_CLRBUF
) {
396 error
= bread(vp
, lbn
, (int)fs
->fs_bsize
, NOCRED
, &nbp
);
402 nbp
= getblk(vp
, lbn
, fs
->fs_bsize
, 0, 0, BLK_WRITE
);
403 nbp
->b_blkno
= fsbtodb(fs
, nb
);
410 * If we have failed part way through block allocation, we
411 * have to deallocate any indirect blocks that we have allocated.
413 for (deallocated
= 0, blkp
= allociblk
; blkp
< allocblk
; blkp
++) {
414 ffs_blkfree(ip
, *blkp
, fs
->fs_bsize
);
415 deallocated
+= fs
->fs_bsize
;
420 VOP_DEVBLOCKSIZE(ip
->i_devvp
,&devBlockSize
);
424 * Restore user's disk quota because allocation failed.
426 (void) chkdq(ip
, (int64_t)-deallocated
, cred
, FORCE
);
428 ip
->i_blocks
-= btodb(deallocated
, devBlockSize
);
429 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
435 * ffs_blkalloc allocates a disk block for ffs_pageout(), as a consequence
436 * it does no breads (that could lead to deadblock as the page may be already
437 * marked busy as it is being paged out. Also important to note that we are not
438 * growing the file in pageouts. So ip->i_size cannot increase by this call
439 * due to the way UBC works.
440 * This code is derived from ffs_balloc and many cases of that are dealt
441 * in ffs_balloc are not applicable here
442 * Do not call with B_CLRBUF flags as this should only be called only
445 ffs_blkalloc(ip
, lbn
, size
, cred
, flags
)
446 register struct inode
*ip
;
452 register struct fs
*fs
;
453 register ufs_daddr_t nb
;
454 struct buf
*bp
, *nbp
;
455 struct vnode
*vp
= ITOV(ip
);
456 struct indir indirs
[NIADDR
+ 2];
457 ufs_daddr_t newb
, *bap
, pref
;
458 int deallocated
, osize
, nsize
, num
, i
, error
;
459 ufs_daddr_t
*allocib
, *blkp
, *allocblk
, allociblk
[NIADDR
+ 1];
462 struct mount
*mp
=vp
->v_mount
;
463 int rev_endian
=(mp
->mnt_flag
& MNT_REVEND
);
464 #endif /* REV_ENDIAN_FS */
468 if(size
> fs
->fs_bsize
)
469 panic("ffs_blkalloc: too large for allocation\n");
472 * If the next write will extend the file into a new block,
473 * and the file is currently composed of a fragment
474 * this fragment has to be extended to be a full block.
476 nb
= lblkno(fs
, ip
->i_size
);
477 if (nb
< NDADDR
&& nb
< lbn
) {
478 panic("ffs_blkalloc():cannot extend file: i_size %d, lbn %d\n", ip
->i_size
, lbn
);
481 * The first NDADDR blocks are direct blocks
485 if (nb
!= 0 && ip
->i_size
>= (lbn
+ 1) * fs
->fs_bsize
) {
486 /* TBD: trivial case; the block is already allocated */
491 * Consider need to reallocate a fragment.
493 osize
= fragroundup(fs
, blkoff(fs
, ip
->i_size
));
494 nsize
= fragroundup(fs
, size
);
496 panic("ffs_allocblk: trying to extend
501 if (ip
->i_size
< (lbn
+ 1) * fs
->fs_bsize
)
502 nsize
= fragroundup(fs
, size
);
504 nsize
= fs
->fs_bsize
;
505 error
= ffs_alloc(ip
, lbn
,
506 ffs_blkpref(ip
, lbn
, (int)lbn
, &ip
->i_db
[0]),
510 ip
->i_db
[lbn
] = newb
;
511 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
516 * Determine the number of levels of indirection.
519 if (error
= ufs_getlbns(vp
, lbn
, indirs
, &num
))
523 panic("ffs_blkalloc: file with direct blocks only\n");
527 * Fetch the first indirect block allocating if necessary.
530 nb
= ip
->i_ib
[indirs
[0].in_off
];
532 allocblk
= allociblk
;
534 pref
= ffs_blkpref(ip
, lbn
, 0, (ufs_daddr_t
*)0);
535 if (error
= ffs_alloc(ip
, lbn
, pref
, (int)fs
->fs_bsize
,
540 bp
= getblk(vp
, indirs
[1].in_lbn
, fs
->fs_bsize
, 0, 0, BLK_META
);
541 bp
->b_blkno
= fsbtodb(fs
, nb
);
544 * Write synchronously so that indirect blocks
545 * never point at garbage.
547 if (error
= bwrite(bp
))
549 allocib
= &ip
->i_ib
[indirs
[0].in_off
];
551 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
554 * Fetch through the indirect blocks, allocating as necessary.
557 error
= meta_bread(vp
,
558 indirs
[i
].in_lbn
, (int)fs
->fs_bsize
, NOCRED
, &bp
);
563 bap
= (ufs_daddr_t
*)bp
->b_data
;
566 nb
= NXSwapLong(bap
[indirs
[i
].in_off
]);
568 #endif /* REV_ENDIAN_FS */
569 nb
= bap
[indirs
[i
].in_off
];
572 #endif /* REV_ENDIAN_FS */
581 pref
= ffs_blkpref(ip
, lbn
, 0, (ufs_daddr_t
*)0);
583 ffs_alloc(ip
, lbn
, pref
, (int)fs
->fs_bsize
, cred
, &newb
)) {
589 nbp
= getblk(vp
, indirs
[i
].in_lbn
, fs
->fs_bsize
, 0, 0, BLK_META
);
590 nbp
->b_blkno
= fsbtodb(fs
, nb
);
593 * Write synchronously so that indirect blocks
594 * never point at garbage.
596 if (error
= bwrite(nbp
)) {
602 bap
[indirs
[i
- 1].in_off
] = NXSwapLong(nb
);
604 #endif /* REV_ENDIAN_FS */
605 bap
[indirs
[i
- 1].in_off
] = nb
;
608 #endif /* REV_ENDIAN_FS */
610 * If required, write synchronously, otherwise use
613 if (flags
& B_SYNC
) {
620 * Get the data block, allocating if necessary.
623 pref
= ffs_blkpref(ip
, lbn
, indirs
[i
].in_off
, &bap
[0]);
624 if (error
= ffs_alloc(ip
,
625 lbn
, pref
, (int)fs
->fs_bsize
, cred
, &newb
)) {
633 bap
[indirs
[i
].in_off
] = NXSwapLong(nb
);
635 #endif /* REV_ENDIAN_FS */
636 bap
[indirs
[i
].in_off
] = nb
;
639 #endif /* REV_ENDIAN_FS */
641 * If required, write synchronously, otherwise use
644 if (flags
& B_SYNC
) {
655 * If we have failed part way through block allocation, we
656 * have to deallocate any indirect blocks that we have allocated.
658 for (deallocated
= 0, blkp
= allociblk
; blkp
< allocblk
; blkp
++) {
659 ffs_blkfree(ip
, *blkp
, fs
->fs_bsize
);
660 deallocated
+= fs
->fs_bsize
;
665 VOP_DEVBLOCKSIZE(ip
->i_devvp
,&devBlockSize
);
669 * Restore user's disk quota because allocation failed.
671 (void) chkdq(ip
, (int64_t)-deallocated
, cred
, FORCE
);
673 ip
->i_blocks
-= btodb(deallocated
, devBlockSize
);
674 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;