The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/fs/ext2fs/ext2_balloc.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  *  modified for Lites 1.1
    3  *
    4  *  Aug 1995, Godmar Back (gback@cs.utah.edu)
    5  *  University of Utah, Department of Computer Science
    6  */
    7 /*-
    8  * Copyright (c) 1982, 1986, 1989, 1993
    9  *      The Regents of the University of California.  All rights reserved.
   10  *
   11  * Redistribution and use in source and binary forms, with or without
   12  * modification, are permitted provided that the following conditions
   13  * are met:
   14  * 1. Redistributions of source code must retain the above copyright
   15  *    notice, this list of conditions and the following disclaimer.
   16  * 2. Redistributions in binary form must reproduce the above copyright
   17  *    notice, this list of conditions and the following disclaimer in the
   18  *    documentation and/or other materials provided with the distribution.
   19  * 4. Neither the name of the University nor the names of its contributors
   20  *    may be used to endorse or promote products derived from this software
   21  *    without specific prior written permission.
   22  *
   23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   33  * SUCH DAMAGE.
   34  *
   35  *      @(#)ffs_balloc.c        8.4 (Berkeley) 9/23/93
   36  * $FreeBSD$
   37  */
   38 
   39 #include <sys/param.h>
   40 #include <sys/systm.h>
   41 #include <sys/bio.h>
   42 #include <sys/buf.h>
   43 #include <sys/lock.h>
   44 #include <sys/mount.h>
   45 #include <sys/vnode.h>
   46 
   47 #include <fs/ext2fs/fs.h>
   48 #include <fs/ext2fs/inode.h>
   49 #include <fs/ext2fs/ext2fs.h>
   50 #include <fs/ext2fs/ext2_dinode.h>
   51 #include <fs/ext2fs/ext2_extern.h>
   52 #include <fs/ext2fs/ext2_mount.h>
   53 
   54 /*
   55  * Balloc defines the structure of filesystem storage
   56  * by allocating the physical blocks on a device given
   57  * the inode and the logical block number in a file.
   58  */
   59 int
   60 ext2_balloc(struct inode *ip, e2fs_lbn_t lbn, int size, struct ucred *cred,
   61     struct buf **bpp, int flags)
   62 {
   63         struct m_ext2fs *fs;
   64         struct ext2mount *ump;
   65         struct buf *bp, *nbp;
   66         struct vnode *vp = ITOV(ip);
   67         struct indir indirs[NIADDR + 2];
   68         e4fs_daddr_t nb, newb;
   69         e2fs_daddr_t *bap, pref;
   70         int osize, nsize, num, i, error;
   71 
   72         *bpp = NULL;
   73         if (lbn < 0)
   74                 return (EFBIG);
   75         fs = ip->i_e2fs;
   76         ump = ip->i_ump;
   77 
   78         /*
   79          * check if this is a sequential block allocation. 
   80          * If so, increment next_alloc fields to allow ext2_blkpref 
   81          * to make a good guess
   82          */
   83         if (lbn == ip->i_next_alloc_block + 1) {
   84                 ip->i_next_alloc_block++;
   85                 ip->i_next_alloc_goal++;
   86         }
   87 
   88         /*
   89          * The first NDADDR blocks are direct blocks
   90          */
   91         if (lbn < NDADDR) {
   92                 nb = ip->i_db[lbn];
   93                 /* no new block is to be allocated, and no need to expand
   94                    the file */
   95                 if (nb != 0 && ip->i_size >= (lbn + 1) * fs->e2fs_bsize) {
   96                         error = bread(vp, lbn, fs->e2fs_bsize, NOCRED, &bp);
   97                         if (error) {
   98                                 brelse(bp);
   99                                 return (error);
  100                         }
  101                         bp->b_blkno = fsbtodb(fs, nb);
  102                         *bpp = bp;
  103                         return (0);
  104                 }
  105                 if (nb != 0) {
  106                         /*
  107                          * Consider need to reallocate a fragment.
  108                          */
  109                         osize = fragroundup(fs, blkoff(fs, ip->i_size));
  110                         nsize = fragroundup(fs, size);
  111                         if (nsize <= osize) {
  112                                 error = bread(vp, lbn, osize, NOCRED, &bp);
  113                                 if (error) {
  114                                         brelse(bp);
  115                                         return (error);
  116                                 }
  117                                 bp->b_blkno = fsbtodb(fs, nb);
  118                         } else {
  119                         /* Godmar thinks: this shouldn't happen w/o fragments */
  120                                 printf("nsize %d(%d) > osize %d(%d) nb %d\n", 
  121                                         (int)nsize, (int)size, (int)osize, 
  122                                         (int)ip->i_size, (int)nb);
  123                                 panic(
  124                                     "ext2_balloc: Something is terribly wrong");
  125 /*
  126  * please note there haven't been any changes from here on -
  127  * FFS seems to work.
  128  */
  129                         }
  130                 } else {
  131                         if (ip->i_size < (lbn + 1) * fs->e2fs_bsize)
  132                                 nsize = fragroundup(fs, size);
  133                         else
  134                                 nsize = fs->e2fs_bsize;
  135                         EXT2_LOCK(ump);
  136                         error = ext2_alloc(ip, lbn,
  137                             ext2_blkpref(ip, lbn, (int)lbn, &ip->i_db[0], 0),
  138                             nsize, cred, &newb);
  139                         if (error)
  140                                 return (error);
  141                         bp = getblk(vp, lbn, nsize, 0, 0, 0);
  142                         bp->b_blkno = fsbtodb(fs, newb);
  143                         if (flags & BA_CLRBUF)
  144                                 vfs_bio_clrbuf(bp);
  145                 }
  146                 ip->i_db[lbn] = dbtofsb(fs, bp->b_blkno);
  147                 ip->i_flag |= IN_CHANGE | IN_UPDATE;
  148                 *bpp = bp;
  149                 return (0);
  150         }
  151         /*
  152          * Determine the number of levels of indirection.
  153          */
  154         pref = 0;
  155         if ((error = ext2_getlbns(vp, lbn, indirs, &num)) != 0)
  156                 return (error);
  157 #ifdef INVARIANTS
  158         if (num < 1)
  159                 panic ("ext2_balloc: ext2_getlbns returned indirect block");
  160 #endif
  161         /*
  162          * Fetch the first indirect block allocating if necessary.
  163          */
  164         --num;
  165         nb = ip->i_ib[indirs[0].in_off];
  166         if (nb == 0) {
  167                 EXT2_LOCK(ump);
  168                 pref = ext2_blkpref(ip, lbn, indirs[0].in_off + 
  169                                              EXT2_NDIR_BLOCKS, &ip->i_db[0], 0);
  170                 if ((error = ext2_alloc(ip, lbn, pref, fs->e2fs_bsize, cred,
  171                         &newb)))
  172                         return (error);
  173                 nb = newb;
  174                 bp = getblk(vp, indirs[1].in_lbn, fs->e2fs_bsize, 0, 0, 0);
  175                 bp->b_blkno = fsbtodb(fs, newb);
  176                 vfs_bio_clrbuf(bp);
  177                 /*
  178                  * Write synchronously so that indirect blocks
  179                  * never point at garbage.
  180                  */
  181                 if ((error = bwrite(bp)) != 0) {
  182                         ext2_blkfree(ip, nb, fs->e2fs_bsize);
  183                         return (error);
  184                 }
  185                 ip->i_ib[indirs[0].in_off] = newb;
  186                 ip->i_flag |= IN_CHANGE | IN_UPDATE;
  187         }
  188         /*
  189          * Fetch through the indirect blocks, allocating as necessary.
  190          */
  191         for (i = 1;;) {
  192                 error = bread(vp,
  193                     indirs[i].in_lbn, (int)fs->e2fs_bsize, NOCRED, &bp);
  194                 if (error) {
  195                         brelse(bp);
  196                         return (error);
  197                 }
  198                 bap = (e2fs_daddr_t *)bp->b_data;
  199                 nb = bap[indirs[i].in_off];
  200                 if (i == num)
  201                         break;
  202                 i += 1;
  203                 if (nb != 0) {
  204                         bqrelse(bp);
  205                         continue;
  206                 }
  207                 EXT2_LOCK(ump);
  208                 if (pref == 0)
  209                         pref = ext2_blkpref(ip, lbn, indirs[i].in_off, bap,
  210                                                 bp->b_lblkno);
  211                 error =  ext2_alloc(ip, lbn, pref, (int)fs->e2fs_bsize, cred, &newb);
  212                 if (error) {
  213                         brelse(bp);
  214                         return (error);
  215                 }
  216                 nb = newb;
  217                 nbp = getblk(vp, indirs[i].in_lbn, fs->e2fs_bsize, 0, 0, 0);
  218                 nbp->b_blkno = fsbtodb(fs, nb);
  219                 vfs_bio_clrbuf(nbp);
  220                 /*
  221                  * Write synchronously so that indirect blocks
  222                  * never point at garbage.
  223                  */
  224                 if ((error = bwrite(nbp)) != 0) {
  225                         ext2_blkfree(ip, nb, fs->e2fs_bsize);
  226                         EXT2_UNLOCK(ump);
  227                         brelse(bp);
  228                         return (error);
  229                 }
  230                 bap[indirs[i - 1].in_off] = nb;
  231                 /*
  232                  * If required, write synchronously, otherwise use
  233                  * delayed write.
  234                  */
  235                 if (flags & IO_SYNC) {
  236                         bwrite(bp);
  237                 } else {
  238                         if (bp->b_bufsize == fs->e2fs_bsize)
  239                                 bp->b_flags |= B_CLUSTEROK;
  240                         bdwrite(bp);
  241                 }
  242         }
  243         /*
  244          * Get the data block, allocating if necessary.
  245          */
  246         if (nb == 0) {
  247                 EXT2_LOCK(ump);
  248                 pref = ext2_blkpref(ip, lbn, indirs[i].in_off, &bap[0], 
  249                                 bp->b_lblkno);
  250                 if ((error = ext2_alloc(ip,
  251                     lbn, pref, (int)fs->e2fs_bsize, cred, &newb)) != 0) {
  252                         brelse(bp);
  253                         return (error);
  254                 }
  255                 nb = newb;
  256                 nbp = getblk(vp, lbn, fs->e2fs_bsize, 0, 0, 0);
  257                 nbp->b_blkno = fsbtodb(fs, nb);
  258                 if (flags & BA_CLRBUF)
  259                         vfs_bio_clrbuf(nbp);
  260                 bap[indirs[i].in_off] = nb;
  261                 /*
  262                  * If required, write synchronously, otherwise use
  263                  * delayed write.
  264                  */
  265                 if (flags & IO_SYNC) {
  266                         bwrite(bp);
  267                 } else {
  268                 if (bp->b_bufsize == fs->e2fs_bsize)
  269                                 bp->b_flags |= B_CLUSTEROK;
  270                         bdwrite(bp);
  271                 }
  272                 *bpp = nbp;
  273                 return (0);
  274         }
  275         brelse(bp);
  276         if (flags & BA_CLRBUF) {
  277                 int seqcount = (flags & BA_SEQMASK) >> BA_SEQSHIFT;
  278                 if (seqcount && (vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
  279                         error = cluster_read(vp, ip->i_size, lbn,
  280                             (int)fs->e2fs_bsize, NOCRED,
  281                             MAXBSIZE, seqcount, &nbp);
  282                 } else {
  283                         error = bread(vp, lbn, (int)fs->e2fs_bsize, NOCRED, &nbp);
  284                 }
  285                 if (error) {
  286                         brelse(nbp);
  287                         return (error);
  288                 }
  289         } else {
  290                 nbp = getblk(vp, lbn, fs->e2fs_bsize, 0, 0, 0);
  291                 nbp->b_blkno = fsbtodb(fs, nb);
  292         }
  293         *bpp = nbp;
  294         return (0);
  295 }
  296 

Cache object: 0c83e84ef70648ccd39dad1e4905ad37


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.