Spelling.
[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.10 2004/08/24 16:08:35 drhodus 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 filesystem 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                 /*
101                  * The filesize prior to this write can fit in direct
102                  * blocks (ex. fragmentation is possibly done)
103                  * we are now extending the file write beyond
104                  * the block which has end of the file prior to this write.
105                 osize = blksize(fs, ip, nb);
106                 /*
107                  * osize gives disk allocated size in the last block. It is
108                  * either in fragments or a file system block size.
109                  */
110                 if (osize < fs->fs_bsize && osize > 0) {
111                         /* A few fragments are already allocated, since the
112                          * current extends beyond this block allocated the
113                          * complete block as fragments are on in last block.
114                          */
115                         error = ffs_realloccg(ip, nb,
116                                 ffs_blkpref(ip, nb, (int)nb, &ip->i_db[0]),
117                                 osize, (int)fs->fs_bsize, cred, &bp);
118                         if (error)
119                                 return (error);
120                         if (DOINGSOFTDEP(vp))
121                                 softdep_setup_allocdirect(ip, nb,
122                                     dbtofsb(fs, bp->b_blkno), ip->i_db[nb],
123                                     fs->fs_bsize, osize, bp);
124                         /* adjust the inode size, we just grew */
125                         ip->i_size = smalllblktosize(fs, nb + 1);
126                         ip->i_db[nb] = dbtofsb(fs, bp->b_blkno);
127                         ip->i_flag |= IN_CHANGE | IN_UPDATE;
128                         if (flags & B_SYNC)
129                                 bwrite(bp);
130                         else
131                                 bawrite(bp);
132                         /* bp is already released here */
133                 }
134         }
135         /*
136          * The first NDADDR blocks are direct blocks
137          */
138         if (lbn < NDADDR) {
139                 nb = ip->i_db[lbn];
140                 if (nb != 0 && ip->i_size >= smalllblktosize(fs, lbn + 1)) {
141                         error = bread(vp, lbn, fs->fs_bsize, &bp);
142                         if (error) {
143                                 brelse(bp);
144                                 return (error);
145                         }
146                         bp->b_blkno = fsbtodb(fs, nb);
147                         *ap->a_bpp = bp;
148                         return (0);
149                 }
150                 if (nb != 0) {
151                         /*
152                          * Consider need to reallocate a fragment.
153                          */
154                         osize = fragroundup(fs, blkoff(fs, ip->i_size));
155                         nsize = fragroundup(fs, size);
156                         if (nsize <= osize) {
157                                 error = bread(vp, lbn, osize, &bp);
158                                 if (error) {
159                                         brelse(bp);
160                                         return (error);
161                                 }
162                                 bp->b_blkno = fsbtodb(fs, nb);
163                         } else {
164                                 error = ffs_realloccg(ip, lbn,
165                                     ffs_blkpref(ip, lbn, (int)lbn,
166                                         &ip->i_db[0]), osize, nsize, cred, &bp);
167                                 if (error)
168                                         return (error);
169                                 if (DOINGSOFTDEP(vp))
170                                         softdep_setup_allocdirect(ip, lbn,
171                                             dbtofsb(fs, bp->b_blkno), nb,
172                                             nsize, osize, bp);
173                         }
174                 } else {
175                         if (ip->i_size < smalllblktosize(fs, lbn + 1))
176                                 nsize = fragroundup(fs, size);
177                         else
178                                 nsize = fs->fs_bsize;
179                         error = ffs_alloc(ip, lbn,
180                             ffs_blkpref(ip, lbn, (int)lbn, &ip->i_db[0]),
181                             nsize, cred, &newb);
182                         if (error)
183                                 return (error);
184                         bp = getblk(vp, lbn, nsize, 0, 0);
185                         bp->b_blkno = fsbtodb(fs, newb);
186                         if (flags & B_CLRBUF)
187                                 vfs_bio_clrbuf(bp);
188                         if (DOINGSOFTDEP(vp))
189                                 softdep_setup_allocdirect(ip, lbn, newb, 0,
190                                     nsize, 0, bp);
191                 }
192                 ip->i_db[lbn] = dbtofsb(fs, bp->b_blkno);
193                 ip->i_flag |= IN_CHANGE | IN_UPDATE;
194                 *ap->a_bpp = bp;
195                 return (0);
196         }
197         /*
198          * Determine the number of levels of indirection.
199          */
200         pref = 0;
201         if ((error = ufs_getlbns(vp, lbn, indirs, &num)) != 0)
202                 return(error);
203 #ifdef DIAGNOSTIC
204         if (num < 1)
205                 panic ("ffs_balloc: ufs_bmaparray returned indirect block");
206 #endif
207         /*
208          * Fetch the first indirect block allocating if necessary.
209          */
210         --num;
211         nb = ip->i_ib[indirs[0].in_off];
212         allocib = NULL;
213         allocblk = allociblk;
214         if (nb == 0) {
215                 pref = ffs_blkpref(ip, lbn, 0, (ufs_daddr_t *)0);
216                 if ((error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
217                     cred, &newb)) != 0)
218                         return (error);
219                 nb = newb;
220                 *allocblk++ = nb;
221                 bp = getblk(vp, indirs[1].in_lbn, fs->fs_bsize, 0, 0);
222                 bp->b_blkno = fsbtodb(fs, nb);
223                 vfs_bio_clrbuf(bp);
224                 if (DOINGSOFTDEP(vp)) {
225                         softdep_setup_allocdirect(ip, NDADDR + indirs[0].in_off,
226                             newb, 0, fs->fs_bsize, 0, bp);
227                         bdwrite(bp);
228                 } else {
229                         /*
230                          * Write synchronously so that indirect blocks
231                          * never point at garbage.
232                          */
233                         if (DOINGASYNC(vp))
234                                 bdwrite(bp);
235                         else if ((error = bwrite(bp)) != 0)
236                                 goto fail;
237                 }
238                 allocib = &ip->i_ib[indirs[0].in_off];
239                 *allocib = nb;
240                 ip->i_flag |= IN_CHANGE | IN_UPDATE;
241         }
242         /*
243          * Fetch through the indirect blocks, allocating as necessary.
244          */
245         for (i = 1;;) {
246                 error = bread(vp, indirs[i].in_lbn, (int)fs->fs_bsize, &bp);
247                 if (error) {
248                         brelse(bp);
249                         goto fail;
250                 }
251                 bap = (ufs_daddr_t *)bp->b_data;
252                 nb = bap[indirs[i].in_off];
253                 if (i == num)
254                         break;
255                 i += 1;
256                 if (nb != 0) {
257                         bqrelse(bp);
258                         continue;
259                 }
260                 if (pref == 0)
261                         pref = ffs_blkpref(ip, lbn, 0, (ufs_daddr_t *)0);
262                 if ((error =
263                     ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred, &newb)) != 0) {
264                         brelse(bp);
265                         goto fail;
266                 }
267                 nb = newb;
268                 *allocblk++ = nb;
269                 nbp = getblk(vp, indirs[i].in_lbn, fs->fs_bsize, 0, 0);
270                 nbp->b_blkno = fsbtodb(fs, nb);
271                 vfs_bio_clrbuf(nbp);
272                 if (DOINGSOFTDEP(vp)) {
273                         softdep_setup_allocindir_meta(nbp, ip, bp,
274                             indirs[i - 1].in_off, nb);
275                         bdwrite(nbp);
276                 } else {
277                         /*
278                          * Write synchronously so that indirect blocks
279                          * never point at garbage.
280                          */
281                         if ((error = bwrite(nbp)) != 0) {
282                                 brelse(bp);
283                                 goto fail;
284                         }
285                 }
286                 bap[indirs[i - 1].in_off] = nb;
287                 if (allocib == NULL && unwindidx < 0)
288                         unwindidx = i - 1;
289                 /*
290                  * If required, write synchronously, otherwise use
291                  * delayed write.
292                  */
293                 if (flags & B_SYNC) {
294                         bwrite(bp);
295                 } else {
296                         if (bp->b_bufsize == fs->fs_bsize)
297                                 bp->b_flags |= B_CLUSTEROK;
298                         bdwrite(bp);
299                 }
300         }
301         /*
302          * Get the data block, allocating if necessary.
303          */
304         if (nb == 0) {
305                 pref = ffs_blkpref(ip, lbn, indirs[i].in_off, &bap[0]);
306                 error = ffs_alloc(ip,
307                     lbn, pref, (int)fs->fs_bsize, cred, &newb);
308                 if (error) {
309                         brelse(bp);
310                         goto fail;
311                 }
312                 nb = newb;
313                 *allocblk++ = nb;
314                 nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
315                 nbp->b_blkno = fsbtodb(fs, nb);
316                 if (flags & B_CLRBUF)
317                         vfs_bio_clrbuf(nbp);
318                 if (DOINGSOFTDEP(vp))
319                         softdep_setup_allocindir_page(ip, lbn, bp,
320                             indirs[i].in_off, nb, 0, nbp);
321                 bap[indirs[i].in_off] = nb;
322                 /*
323                  * If required, write synchronously, otherwise use
324                  * delayed write.
325                  */
326                 if (flags & B_SYNC) {
327                         bwrite(bp);
328                 } else {
329                         if (bp->b_bufsize == fs->fs_bsize)
330                                 bp->b_flags |= B_CLUSTEROK;
331                         bdwrite(bp);
332                 }
333                 *ap->a_bpp = nbp;
334                 return (0);
335         }
336         brelse(bp);
337         /*
338          * If requested clear invalid portions of the buffer.  If we 
339          * have to do a read-before-write (typical if B_CLRBUF is set),
340          * try to do some read-ahead in the sequential case to reduce
341          * the number of I/O transactions.
342          */
343         if (flags & B_CLRBUF) {
344                 int seqcount = (flags & B_SEQMASK) >> B_SEQSHIFT;
345                 if (seqcount &&
346                     (vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
347                         error = cluster_read(vp, ip->i_size, lbn,
348                                     (int)fs->fs_bsize, 
349                                     MAXBSIZE, seqcount, &nbp);
350                 } else {
351                         error = bread(vp, lbn, (int)fs->fs_bsize, &nbp);
352                 }
353                 if (error) {
354                         brelse(nbp);
355                         goto fail;
356                 }
357         } else {
358                 nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
359                 nbp->b_blkno = fsbtodb(fs, nb);
360         }
361         *ap->a_bpp = nbp;
362         return (0);
363 fail:
364         /*
365          * If we have failed part way through block allocation, we
366          * have to deallocate any indirect blocks that we have allocated.
367          * We have to fsync the file before we start to get rid of all
368          * of its dependencies so that we do not leave them dangling.
369          * We have to sync it at the end so that the soft updates code
370          * does not find any untracked changes. Although this is really
371          * slow, running out of disk space is not expected to be a common
372          * occurence. The error return from fsync is ignored as we already
373          * have an error to return to the user.
374          */
375         (void) VOP_FSYNC(vp, MNT_WAIT, td);
376         for (deallocated = 0, blkp = allociblk; blkp < allocblk; blkp++) {
377                 ffs_blkfree(ip, *blkp, fs->fs_bsize);
378                 deallocated += fs->fs_bsize;
379         }
380         if (allocib != NULL) {
381                 *allocib = 0;
382         } else if (unwindidx >= 0) {
383                 int r;
384
385                 r = bread(vp, indirs[unwindidx].in_lbn, (int)fs->fs_bsize, &bp);
386                 if (r) {
387                         panic("Could not unwind indirect block, error %d", r);
388                         brelse(bp);
389                 } else {
390                         bap = (ufs_daddr_t *)bp->b_data;
391                         bap[indirs[unwindidx].in_off] = 0;
392                         if (flags & B_SYNC) {
393                                 bwrite(bp);
394                         } else {
395                                 if (bp->b_bufsize == fs->fs_bsize)
396                                         bp->b_flags |= B_CLUSTEROK;
397                                 bdwrite(bp);
398                         }
399                 }
400         }
401         if (deallocated) {
402 #ifdef QUOTA
403                 /*
404                  * Restore user's disk quota because allocation failed.
405                  */
406                 (void) chkdq(ip, (long)-btodb(deallocated), cred, FORCE);
407 #endif
408                 ip->i_blocks -= btodb(deallocated);
409                 ip->i_flag |= IN_CHANGE | IN_UPDATE;
410         }
411         (void) VOP_FSYNC(vp, MNT_WAIT, td);
412         return (error);
413 }