]>
git.saurik.com Git - apple/xnu.git/blob - bsd/ufs/ffs/ffs_balloc.c
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
27 * Copyright (c) 1982, 1986, 1989, 1993
28 * The Regents of the University of California. All rights reserved.
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by the University of
41 * California, Berkeley and its contributors.
42 * 4. Neither the name of the University nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * @(#)ffs_balloc.c 8.8 (Berkeley) 6/16/95
61 #include <rev_endian_fs.h>
62 #include <sys/param.h>
63 #include <sys/systm.h>
67 #include <sys/vnode.h>
69 #include <sys/quota.h>
72 #include <sys/mount.h>
73 #endif /* REV_ENDIAN_FS */
77 #include <ufs/ufs/quota.h>
78 #include <ufs/ufs/inode.h>
79 #include <ufs/ufs/ufs_extern.h>
81 #include <ufs/ffs/fs.h>
82 #include <ufs/ffs/ffs_extern.h>
85 #include <ufs/ufs/ufs_byte_order.h>
86 #include <architecture/byte_order.h>
87 #endif /* REV_ENDIAN_FS */
90 * Balloc defines the structure of file system storage
91 * by allocating the physical blocks on a device given
92 * the inode and the logical block number in a file.
94 ffs_balloc(ip
, lbn
, size
, cred
, bpp
, flags
, blk_alloc
)
95 register struct inode
*ip
;
96 register ufs_daddr_t lbn
;
103 register struct fs
*fs
;
104 register ufs_daddr_t nb
;
105 struct buf
*bp
, *nbp
;
106 struct vnode
*vp
= ITOV(ip
);
107 struct indir indirs
[NIADDR
+ 2];
108 ufs_daddr_t newb
, *bap
, pref
;
109 int deallocated
, osize
, nsize
, num
, i
, error
;
110 ufs_daddr_t
*allocib
, *blkp
, *allocblk
, allociblk
[NIADDR
+ 1];
112 int alloc_buffer
= 1;
114 struct mount
*mp
=vp
->v_mount
;
115 int rev_endian
=(mp
->mnt_flag
& MNT_REVEND
);
116 #endif /* REV_ENDIAN_FS */
122 if (flags
& B_NOBUFF
)
129 * If the next write will extend the file into a new block,
130 * and the file is currently composed of a fragment
131 * this fragment has to be extended to be a full block.
133 nb
= lblkno(fs
, ip
->i_size
);
134 if (nb
< NDADDR
&& nb
< lbn
) {
135 /* the filesize prior to this write can fit in direct
136 * blocks (ie. fragmentaion is possibly done)
137 * we are now extending the file write beyond
138 * the block which has end of file prior to this write
140 osize
= blksize(fs
, ip
, nb
);
141 /* osize gives disk allocated size in the last block. It is
142 * either in fragments or a file system block size */
143 if (osize
< fs
->fs_bsize
&& osize
> 0) {
144 /* few fragments are already allocated,since the
145 * current extends beyond this block
146 * allocate the complete block as fragments are only
149 error
= ffs_realloccg(ip
, nb
,
150 ffs_blkpref(ip
, nb
, (int)nb
, &ip
->i_db
[0]),
151 osize
, (int)fs
->fs_bsize
, cred
, &bp
);
154 /* adjust the innode size we just grew */
155 /* it is in nb+1 as nb starts from 0 */
156 ip
->i_size
= (nb
+ 1) * fs
->fs_bsize
;
158 ubc_setsize(vp
, (off_t
)ip
->i_size
); /* XXX check error */
159 ip
->i_db
[nb
] = dbtofsb(fs
, bp
->b_blkno
);
160 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
161 if ((flags
& B_SYNC
) || (!alloc_buffer
)) {
163 SET(bp
->b_flags
, B_INVAL
);
167 /* note that bp is already released here */
171 * The first NDADDR blocks are direct blocks
175 if (nb
!= 0 && ip
->i_size
>= (lbn
+ 1) * fs
->fs_bsize
) {
177 error
= bread(vp
, lbn
, fs
->fs_bsize
, NOCRED
, &bp
);
188 * Consider need to reallocate a fragment.
190 osize
= fragroundup(fs
, blkoff(fs
, ip
->i_size
));
191 nsize
= fragroundup(fs
, size
);
192 if (nsize
<= osize
) {
194 error
= bread(vp
, lbn
, osize
, NOCRED
, &bp
);
199 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
204 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
208 error
= ffs_realloccg(ip
, lbn
,
209 ffs_blkpref(ip
, lbn
, (int)lbn
,
210 &ip
->i_db
[0]), osize
, nsize
, cred
, &bp
);
213 ip
->i_db
[lbn
] = dbtofsb(fs
, bp
->b_blkno
);
214 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
216 SET(bp
->b_flags
, B_INVAL
);
224 if (ip
->i_size
< (lbn
+ 1) * fs
->fs_bsize
)
225 nsize
= fragroundup(fs
, size
);
227 nsize
= fs
->fs_bsize
;
228 error
= ffs_alloc(ip
, lbn
,
229 ffs_blkpref(ip
, lbn
, (int)lbn
, &ip
->i_db
[0]),
234 bp
= getblk(vp
, lbn
, nsize
, 0, 0, BLK_WRITE
);
235 bp
->b_blkno
= fsbtodb(fs
, newb
);
236 if (flags
& B_CLRBUF
)
239 ip
->i_db
[lbn
] = newb
;
240 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
250 * Determine the number of levels of indirection.
253 if (error
= ufs_getlbns(vp
, lbn
, indirs
, &num
))
257 panic ("ffs_balloc: ufs_bmaparray returned indirect block\n");
260 * Fetch the first indirect block allocating if necessary.
263 nb
= ip
->i_ib
[indirs
[0].in_off
];
265 allocblk
= allociblk
;
267 pref
= ffs_blkpref(ip
, lbn
, 0, (ufs_daddr_t
*)0);
268 if (error
= ffs_alloc(ip
, lbn
, pref
, (int)fs
->fs_bsize
,
273 bp
= getblk(vp
, indirs
[1].in_lbn
, fs
->fs_bsize
, 0, 0, BLK_META
);
274 bp
->b_blkno
= fsbtodb(fs
, nb
);
277 * Write synchronously so that indirect blocks
278 * never point at garbage.
280 if (error
= bwrite(bp
))
282 allocib
= &ip
->i_ib
[indirs
[0].in_off
];
284 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
287 * Fetch through the indirect blocks, allocating as necessary.
290 error
= meta_bread(vp
,
291 indirs
[i
].in_lbn
, (int)fs
->fs_bsize
, NOCRED
, &bp
);
296 bap
= (ufs_daddr_t
*)bp
->b_data
;
299 nb
= NXSwapLong(bap
[indirs
[i
].in_off
]);
301 #endif /* REV_ENDIAN_FS */
302 nb
= bap
[indirs
[i
].in_off
];
305 #endif /* REV_ENDIAN_FS */
314 pref
= ffs_blkpref(ip
, lbn
, 0, (ufs_daddr_t
*)0);
316 ffs_alloc(ip
, lbn
, pref
, (int)fs
->fs_bsize
, cred
, &newb
)) {
322 nbp
= getblk(vp
, indirs
[i
].in_lbn
, fs
->fs_bsize
, 0, 0, BLK_META
);
323 nbp
->b_blkno
= fsbtodb(fs
, nb
);
326 * Write synchronously so that indirect blocks
327 * never point at garbage.
329 if (error
= bwrite(nbp
)) {
335 bap
[indirs
[i
- 1].in_off
] = NXSwapLong(nb
);
337 #endif /* REV_ENDIAN_FS */
338 bap
[indirs
[i
- 1].in_off
] = nb
;
341 #endif /* REV_ENDIAN_FS */
343 * If required, write synchronously, otherwise use
346 if (flags
& B_SYNC
) {
353 * Get the data block, allocating if necessary.
356 pref
= ffs_blkpref(ip
, lbn
, indirs
[i
].in_off
, &bap
[0]);
357 if (error
= ffs_alloc(ip
,
358 lbn
, pref
, (int)fs
->fs_bsize
, cred
, &newb
)) {
366 bap
[indirs
[i
].in_off
] = NXSwapLong(nb
);
368 #endif /* REV_ENDIAN_FS */
369 bap
[indirs
[i
].in_off
] = nb
;
372 #endif /* REV_ENDIAN_FS */
374 * If required, write synchronously, otherwise use
377 if ((flags
& B_SYNC
)) {
383 nbp
= getblk(vp
, lbn
, fs
->fs_bsize
, 0, 0, BLK_WRITE
);
384 nbp
->b_blkno
= fsbtodb(fs
, nb
);
385 if (flags
& B_CLRBUF
)
389 *blk_alloc
= fs
->fs_bsize
;
398 if (flags
& B_CLRBUF
) {
399 error
= bread(vp
, lbn
, (int)fs
->fs_bsize
, NOCRED
, &nbp
);
405 nbp
= getblk(vp
, lbn
, fs
->fs_bsize
, 0, 0, BLK_WRITE
);
406 nbp
->b_blkno
= fsbtodb(fs
, nb
);
413 * If we have failed part way through block allocation, we
414 * have to deallocate any indirect blocks that we have allocated.
416 for (deallocated
= 0, blkp
= allociblk
; blkp
< allocblk
; blkp
++) {
417 ffs_blkfree(ip
, *blkp
, fs
->fs_bsize
);
418 deallocated
+= fs
->fs_bsize
;
423 VOP_DEVBLOCKSIZE(ip
->i_devvp
,&devBlockSize
);
427 * Restore user's disk quota because allocation failed.
429 (void) chkdq(ip
, (int64_t)-deallocated
, cred
, FORCE
);
431 ip
->i_blocks
-= btodb(deallocated
, devBlockSize
);
432 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
438 * ffs_blkalloc allocates a disk block for ffs_pageout(), as a consequence
439 * it does no breads (that could lead to deadblock as the page may be already
440 * marked busy as it is being paged out. Also important to note that we are not
441 * growing the file in pageouts. So ip->i_size cannot increase by this call
442 * due to the way UBC works.
443 * This code is derived from ffs_balloc and many cases of that are dealt
444 * in ffs_balloc are not applicable here
445 * Do not call with B_CLRBUF flags as this should only be called only
448 ffs_blkalloc(ip
, lbn
, size
, cred
, flags
)
449 register struct inode
*ip
;
455 register struct fs
*fs
;
456 register ufs_daddr_t nb
;
457 struct buf
*bp
, *nbp
;
458 struct vnode
*vp
= ITOV(ip
);
459 struct indir indirs
[NIADDR
+ 2];
460 ufs_daddr_t newb
, *bap
, pref
;
461 int deallocated
, osize
, nsize
, num
, i
, error
;
462 ufs_daddr_t
*allocib
, *blkp
, *allocblk
, allociblk
[NIADDR
+ 1];
465 struct mount
*mp
=vp
->v_mount
;
466 int rev_endian
=(mp
->mnt_flag
& MNT_REVEND
);
467 #endif /* REV_ENDIAN_FS */
471 if(size
> fs
->fs_bsize
)
472 panic("ffs_blkalloc: too large for allocation\n");
475 * If the next write will extend the file into a new block,
476 * and the file is currently composed of a fragment
477 * this fragment has to be extended to be a full block.
479 nb
= lblkno(fs
, ip
->i_size
);
480 if (nb
< NDADDR
&& nb
< lbn
) {
481 panic("ffs_blkalloc():cannot extend file: i_size %d, lbn %d\n", ip
->i_size
, lbn
);
484 * The first NDADDR blocks are direct blocks
488 if (nb
!= 0 && ip
->i_size
>= (lbn
+ 1) * fs
->fs_bsize
) {
489 /* TBD: trivial case; the block is already allocated */
494 * Consider need to reallocate a fragment.
496 osize
= fragroundup(fs
, blkoff(fs
, ip
->i_size
));
497 nsize
= fragroundup(fs
, size
);
499 panic("ffs_allocblk: trying to extend
504 if (ip
->i_size
< (lbn
+ 1) * fs
->fs_bsize
)
505 nsize
= fragroundup(fs
, size
);
507 nsize
= fs
->fs_bsize
;
508 error
= ffs_alloc(ip
, lbn
,
509 ffs_blkpref(ip
, lbn
, (int)lbn
, &ip
->i_db
[0]),
513 ip
->i_db
[lbn
] = newb
;
514 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
519 * Determine the number of levels of indirection.
522 if (error
= ufs_getlbns(vp
, lbn
, indirs
, &num
))
526 panic("ffs_blkalloc: file with direct blocks only\n");
530 * Fetch the first indirect block allocating if necessary.
533 nb
= ip
->i_ib
[indirs
[0].in_off
];
535 allocblk
= allociblk
;
537 pref
= ffs_blkpref(ip
, lbn
, 0, (ufs_daddr_t
*)0);
538 if (error
= ffs_alloc(ip
, lbn
, pref
, (int)fs
->fs_bsize
,
543 bp
= getblk(vp
, indirs
[1].in_lbn
, fs
->fs_bsize
, 0, 0, BLK_META
);
544 bp
->b_blkno
= fsbtodb(fs
, nb
);
547 * Write synchronously so that indirect blocks
548 * never point at garbage.
550 if (error
= bwrite(bp
))
552 allocib
= &ip
->i_ib
[indirs
[0].in_off
];
554 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
557 * Fetch through the indirect blocks, allocating as necessary.
560 error
= meta_bread(vp
,
561 indirs
[i
].in_lbn
, (int)fs
->fs_bsize
, NOCRED
, &bp
);
566 bap
= (ufs_daddr_t
*)bp
->b_data
;
569 nb
= NXSwapLong(bap
[indirs
[i
].in_off
]);
571 #endif /* REV_ENDIAN_FS */
572 nb
= bap
[indirs
[i
].in_off
];
575 #endif /* REV_ENDIAN_FS */
584 pref
= ffs_blkpref(ip
, lbn
, 0, (ufs_daddr_t
*)0);
586 ffs_alloc(ip
, lbn
, pref
, (int)fs
->fs_bsize
, cred
, &newb
)) {
592 nbp
= getblk(vp
, indirs
[i
].in_lbn
, fs
->fs_bsize
, 0, 0, BLK_META
);
593 nbp
->b_blkno
= fsbtodb(fs
, nb
);
596 * Write synchronously so that indirect blocks
597 * never point at garbage.
599 if (error
= bwrite(nbp
)) {
605 bap
[indirs
[i
- 1].in_off
] = NXSwapLong(nb
);
607 #endif /* REV_ENDIAN_FS */
608 bap
[indirs
[i
- 1].in_off
] = nb
;
611 #endif /* REV_ENDIAN_FS */
613 * If required, write synchronously, otherwise use
616 if (flags
& B_SYNC
) {
623 * Get the data block, allocating if necessary.
626 pref
= ffs_blkpref(ip
, lbn
, indirs
[i
].in_off
, &bap
[0]);
627 if (error
= ffs_alloc(ip
,
628 lbn
, pref
, (int)fs
->fs_bsize
, cred
, &newb
)) {
636 bap
[indirs
[i
].in_off
] = NXSwapLong(nb
);
638 #endif /* REV_ENDIAN_FS */
639 bap
[indirs
[i
].in_off
] = nb
;
642 #endif /* REV_ENDIAN_FS */
644 * If required, write synchronously, otherwise use
647 if (flags
& B_SYNC
) {
658 * If we have failed part way through block allocation, we
659 * have to deallocate any indirect blocks that we have allocated.
661 for (deallocated
= 0, blkp
= allociblk
; blkp
< allocblk
; blkp
++) {
662 ffs_blkfree(ip
, *blkp
, fs
->fs_bsize
);
663 deallocated
+= fs
->fs_bsize
;
668 VOP_DEVBLOCKSIZE(ip
->i_devvp
,&devBlockSize
);
672 * Restore user's disk quota because allocation failed.
674 (void) chkdq(ip
, (int64_t)-deallocated
, cred
, FORCE
);
676 ip
->i_blocks
-= btodb(deallocated
, devBlockSize
);
677 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;