Fix compile warning.
[dragonfly.git] / sys / vfs / ufs / ffs_balloc.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_balloc.c        8.8 (Berkeley) 6/16/95
34  * $FreeBSD: src/sys/ufs/ffs/ffs_balloc.c,v 1.26.2.1 2002/10/10 19:48:20 dillon Exp $
35  * $DragonFly: src/sys/vfs/ufs/ffs_balloc.c,v 1.7 2004/05/18 00:16:46 cpressey Exp $
36  */
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/proc.h>
41 #include <sys/buf.h>
42 #include <sys/lock.h>
43 #include <sys/mount.h>
44 #include <sys/vnode.h>
45
46 #include "quota.h"
47 #include "inode.h"
48 #include "ufs_extern.h"
49
50 #include "fs.h"
51 #include "ffs_extern.h"
52
53 /*
54  * Balloc defines the structure of file system storage
55  * by allocating the physical blocks on a device given
56  * the inode and the logical block number in a file.
57  *
58  * ffs_balloc(struct vnode *a_vp, ufs_daddr_t a_lbn, int a_size,
59  *            struct ucred *a_cred, int a_flags, struct buf *a_bpp)
60  */
61 int
62 ffs_balloc(struct vop_balloc_args *ap)
63 {
64         struct inode *ip;
65         ufs_daddr_t lbn;
66         int size;
67         struct ucred *cred;
68         int flags;
69         struct fs *fs;
70         ufs_daddr_t nb;
71         struct buf *bp, *nbp;
72         struct vnode *vp;
73         struct indir indirs[NIADDR + 2];
74         ufs_daddr_t newb, *bap, pref;
75         int deallocated, osize, nsize, num, i, error;
76         ufs_daddr_t *allocib, *blkp, *allocblk, allociblk[NIADDR + 1];
77         int unwindidx = -1;
78         struct thread *td = curthread;  /* XXX */
79
80         vp = ap->a_vp;
81         ip = VTOI(vp);
82         fs = ip->i_fs;
83         lbn = lblkno(fs, ap->a_startoffset);
84         size = blkoff(fs, ap->a_startoffset) + ap->a_size;
85         if (size > fs->fs_bsize)
86                 panic("ffs_balloc: blk too big");
87         *ap->a_bpp = NULL;
88         if (lbn < 0)
89                 return (EFBIG);
90         cred = ap->a_cred;
91         flags = ap->a_flags;
92
93         /*
94          * If the next write will extend the file into a new block,
95          * and the file is currently composed of a fragment
96          * this fragment has to be extended to be a full block.
97          */
98         nb = lblkno(fs, ip->i_size);
99         if (nb < NDADDR && nb < lbn) {
100                 osize = blksize(fs, ip, nb);
101                 if (osize < fs->fs_bsize && osize > 0) {
102                         error = ffs_realloccg(ip, nb,
103                                 ffs_blkpref(ip, nb, (int)nb, &ip->i_db[0]),
104                                 osize, (int)fs->fs_bsize, cred, &bp);
105                         if (error)
106                                 return (error);
107                         if (DOINGSOFTDEP(vp))
108                                 softdep_setup_allocdirect(ip, nb,
109                                     dbtofsb(fs, bp->b_blkno), ip->i_db[nb],
110                                     fs->fs_bsize, osize, bp);
111                         ip->i_size = smalllblktosize(fs, nb + 1);
112                         ip->i_db[nb] = dbtofsb(fs, bp->b_blkno);
113                         ip->i_flag |= IN_CHANGE | IN_UPDATE;
114                         if (flags & B_SYNC)
115                                 bwrite(bp);
116                         else
117                                 bawrite(bp);
118                 }
119         }
120         /*
121          * The first NDADDR blocks are direct blocks
122          */
123         if (lbn < NDADDR) {
124                 nb = ip->i_db[lbn];
125                 if (nb != 0 && ip->i_size >= smalllblktosize(fs, lbn + 1)) {
126                         error = bread(vp, lbn, fs->fs_bsize, &bp);
127                         if (error) {
128                                 brelse(bp);
129                                 return (error);
130                         }
131                         bp->b_blkno = fsbtodb(fs, nb);
132                         *ap->a_bpp = bp;
133                         return (0);
134                 }
135                 if (nb != 0) {
136                         /*
137                          * Consider need to reallocate a fragment.
138                          */
139                         osize = fragroundup(fs, blkoff(fs, ip->i_size));
140                         nsize = fragroundup(fs, size);
141                         if (nsize <= osize) {
142                                 error = bread(vp, lbn, osize, &bp);
143                                 if (error) {
144                                         brelse(bp);
145                                         return (error);
146                                 }
147                                 bp->b_blkno = fsbtodb(fs, nb);
148                         } else {
149                                 error = ffs_realloccg(ip, lbn,
150                                     ffs_blkpref(ip, lbn, (int)lbn,
151                                         &ip->i_db[0]), osize, nsize, cred, &bp);
152                                 if (error)
153                                         return (error);
154                                 if (DOINGSOFTDEP(vp))
155                                         softdep_setup_allocdirect(ip, lbn,
156                                             dbtofsb(fs, bp->b_blkno), nb,
157                                             nsize, osize, bp);
158                         }
159                 } else {
160                         if (ip->i_size < smalllblktosize(fs, lbn + 1))
161                                 nsize = fragroundup(fs, size);
162                         else
163                                 nsize = fs->fs_bsize;
164                         error = ffs_alloc(ip, lbn,
165                             ffs_blkpref(ip, lbn, (int)lbn, &ip->i_db[0]),
166                             nsize, cred, &newb);
167                         if (error)
168                                 return (error);
169                         bp = getblk(vp, lbn, nsize, 0, 0);
170                         bp->b_blkno = fsbtodb(fs, newb);
171                         if (flags & B_CLRBUF)
172                                 vfs_bio_clrbuf(bp);
173                         if (DOINGSOFTDEP(vp))
174                                 softdep_setup_allocdirect(ip, lbn, newb, 0,
175                                     nsize, 0, bp);
176                 }
177                 ip->i_db[lbn] = dbtofsb(fs, bp->b_blkno);
178                 ip->i_flag |= IN_CHANGE | IN_UPDATE;
179                 *ap->a_bpp = bp;
180                 return (0);
181         }
182         /*
183          * Determine the number of levels of indirection.
184          */
185         pref = 0;
186         if ((error = ufs_getlbns(vp, lbn, indirs, &num)) != 0)
187                 return(error);
188 #ifdef DIAGNOSTIC
189         if (num < 1)
190                 panic ("ffs_balloc: ufs_bmaparray returned indirect block");
191 #endif
192         /*
193          * Fetch the first indirect block allocating if necessary.
194          */
195         --num;
196         nb = ip->i_ib[indirs[0].in_off];
197         allocib = NULL;
198         allocblk = allociblk;
199         if (nb == 0) {
200                 pref = ffs_blkpref(ip, lbn, 0, (ufs_daddr_t *)0);
201                 if ((error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
202                     cred, &newb)) != 0)
203                         return (error);
204                 nb = newb;
205                 *allocblk++ = nb;
206                 bp = getblk(vp, indirs[1].in_lbn, fs->fs_bsize, 0, 0);
207                 bp->b_blkno = fsbtodb(fs, nb);
208                 vfs_bio_clrbuf(bp);
209                 if (DOINGSOFTDEP(vp)) {
210                         softdep_setup_allocdirect(ip, NDADDR + indirs[0].in_off,
211                             newb, 0, fs->fs_bsize, 0, bp);
212                         bdwrite(bp);
213                 } else {
214                         /*
215                          * Write synchronously so that indirect blocks
216                          * never point at garbage.
217                          */
218                         if (DOINGASYNC(vp))
219                                 bdwrite(bp);
220                         else if ((error = bwrite(bp)) != 0)
221                                 goto fail;
222                 }
223                 allocib = &ip->i_ib[indirs[0].in_off];
224                 *allocib = nb;
225                 ip->i_flag |= IN_CHANGE | IN_UPDATE;
226         }
227         /*
228          * Fetch through the indirect blocks, allocating as necessary.
229          */
230         for (i = 1;;) {
231                 error = bread(vp, indirs[i].in_lbn, (int)fs->fs_bsize, &bp);
232                 if (error) {
233                         brelse(bp);
234                         goto fail;
235                 }
236                 bap = (ufs_daddr_t *)bp->b_data;
237                 nb = bap[indirs[i].in_off];
238                 if (i == num)
239                         break;
240                 i += 1;
241                 if (nb != 0) {
242                         bqrelse(bp);
243                         continue;
244                 }
245                 if (pref == 0)
246                         pref = ffs_blkpref(ip, lbn, 0, (ufs_daddr_t *)0);
247                 if ((error =
248                     ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred, &newb)) != 0) {
249                         brelse(bp);
250                         goto fail;
251                 }
252                 nb = newb;
253                 *allocblk++ = nb;
254                 nbp = getblk(vp, indirs[i].in_lbn, fs->fs_bsize, 0, 0);
255                 nbp->b_blkno = fsbtodb(fs, nb);
256                 vfs_bio_clrbuf(nbp);
257                 if (DOINGSOFTDEP(vp)) {
258                         softdep_setup_allocindir_meta(nbp, ip, bp,
259                             indirs[i - 1].in_off, nb);
260                         bdwrite(nbp);
261                 } else {
262                         /*
263                          * Write synchronously so that indirect blocks
264                          * never point at garbage.
265                          */
266                         if ((error = bwrite(nbp)) != 0) {
267                                 brelse(bp);
268                                 goto fail;
269                         }
270                 }
271                 bap[indirs[i - 1].in_off] = nb;
272                 if (allocib == NULL && unwindidx < 0)
273                         unwindidx = i - 1;
274                 /*
275                  * If required, write synchronously, otherwise use
276                  * delayed write.
277                  */
278                 if (flags & B_SYNC) {
279                         bwrite(bp);
280                 } else {
281                         if (bp->b_bufsize == fs->fs_bsize)
282                                 bp->b_flags |= B_CLUSTEROK;
283                         bdwrite(bp);
284                 }
285         }
286         /*
287          * Get the data block, allocating if necessary.
288          */
289         if (nb == 0) {
290                 pref = ffs_blkpref(ip, lbn, indirs[i].in_off, &bap[0]);
291                 error = ffs_alloc(ip,
292                     lbn, pref, (int)fs->fs_bsize, cred, &newb);
293                 if (error) {
294                         brelse(bp);
295                         goto fail;
296                 }
297                 nb = newb;
298                 *allocblk++ = nb;
299                 nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
300                 nbp->b_blkno = fsbtodb(fs, nb);
301                 if (flags & B_CLRBUF)
302                         vfs_bio_clrbuf(nbp);
303                 if (DOINGSOFTDEP(vp))
304                         softdep_setup_allocindir_page(ip, lbn, bp,
305                             indirs[i].in_off, nb, 0, nbp);
306                 bap[indirs[i].in_off] = nb;
307                 /*
308                  * If required, write synchronously, otherwise use
309                  * delayed write.
310                  */
311                 if (flags & B_SYNC) {
312                         bwrite(bp);
313                 } else {
314                         if (bp->b_bufsize == fs->fs_bsize)
315                                 bp->b_flags |= B_CLUSTEROK;
316                         bdwrite(bp);
317                 }
318                 *ap->a_bpp = nbp;
319                 return (0);
320         }
321         brelse(bp);
322         /*
323          * If requested clear invalid portions of the buffer.  If we 
324          * have to do a read-before-write (typical if B_CLRBUF is set),
325          * try to do some read-ahead in the sequential case to reduce
326          * the number of I/O transactions.
327          */
328         if (flags & B_CLRBUF) {
329                 int seqcount = (flags & B_SEQMASK) >> B_SEQSHIFT;
330                 if (seqcount &&
331                     (vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
332                         error = cluster_read(vp, ip->i_size, lbn,
333                                     (int)fs->fs_bsize, 
334                                     MAXBSIZE, seqcount, &nbp);
335                 } else {
336                         error = bread(vp, lbn, (int)fs->fs_bsize, &nbp);
337                 }
338                 if (error) {
339                         brelse(nbp);
340                         goto fail;
341                 }
342         } else {
343                 nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
344                 nbp->b_blkno = fsbtodb(fs, nb);
345         }
346         *ap->a_bpp = nbp;
347         return (0);
348 fail:
349         /*
350          * If we have failed part way through block allocation, we
351          * have to deallocate any indirect blocks that we have allocated.
352          * We have to fsync the file before we start to get rid of all
353          * of its dependencies so that we do not leave them dangling.
354          * We have to sync it at the end so that the soft updates code
355          * does not find any untracked changes. Although this is really
356          * slow, running out of disk space is not expected to be a common
357          * occurence. The error return from fsync is ignored as we already
358          * have an error to return to the user.
359          */
360         (void) VOP_FSYNC(vp, MNT_WAIT, td);
361         for (deallocated = 0, blkp = allociblk; blkp < allocblk; blkp++) {
362                 ffs_blkfree(ip, *blkp, fs->fs_bsize);
363                 deallocated += fs->fs_bsize;
364         }
365         if (allocib != NULL) {
366                 *allocib = 0;
367         } else if (unwindidx >= 0) {
368                 int r;
369
370                 r = bread(vp, indirs[unwindidx].in_lbn, (int)fs->fs_bsize, &bp);
371                 if (r) {
372                         panic("Could not unwind indirect block, error %d", r);
373                         brelse(bp);
374                 } else {
375                         bap = (ufs_daddr_t *)bp->b_data;
376                         bap[indirs[unwindidx].in_off] = 0;
377                         if (flags & B_SYNC) {
378                                 bwrite(bp);
379                         } else {
380                                 if (bp->b_bufsize == fs->fs_bsize)
381                                         bp->b_flags |= B_CLUSTEROK;
382                                 bdwrite(bp);
383                         }
384                 }
385         }
386         if (deallocated) {
387 #ifdef QUOTA
388                 /*
389                  * Restore user's disk quota because allocation failed.
390                  */
391                 (void) chkdq(ip, (long)-btodb(deallocated), cred, FORCE);
392 #endif
393                 ip->i_blocks -= btodb(deallocated);
394                 ip->i_flag |= IN_CHANGE | IN_UPDATE;
395         }
396         (void) VOP_FSYNC(vp, MNT_WAIT, td);
397         return (error);
398 }