linux/fs/ufs/ialloc.c
<<
>>
Prefs
   1/*
   2 *  linux/fs/ufs/ialloc.c
   3 *
   4 * Copyright (c) 1998
   5 * Daniel Pirkl <daniel.pirkl@email.cz>
   6 * Charles University, Faculty of Mathematics and Physics
   7 *
   8 *  from
   9 *
  10 *  linux/fs/ext2/ialloc.c
  11 *
  12 * Copyright (C) 1992, 1993, 1994, 1995
  13 * Remy Card (card@masi.ibp.fr)
  14 * Laboratoire MASI - Institut Blaise Pascal
  15 * Universite Pierre et Marie Curie (Paris VI)
  16 *
  17 *  BSD ufs-inspired inode and directory allocation by 
  18 *  Stephen Tweedie (sct@dcs.ed.ac.uk), 1993
  19 *  Big-endian to little-endian byte-swapping/bitmaps by
  20 *        David S. Miller (davem@caip.rutgers.edu), 1995
  21 *
  22 * UFS2 write support added by
  23 * Evgeniy Dushistov <dushistov@mail.ru>, 2007
  24 */
  25
  26#include <linux/fs.h>
  27#include <linux/time.h>
  28#include <linux/stat.h>
  29#include <linux/string.h>
  30#include <linux/quotaops.h>
  31#include <linux/buffer_head.h>
  32#include <linux/sched.h>
  33#include <linux/bitops.h>
  34#include <asm/byteorder.h>
  35
  36#include "ufs_fs.h"
  37#include "ufs.h"
  38#include "swab.h"
  39#include "util.h"
  40
  41/*
  42 * NOTE! When we get the inode, we're the only people
  43 * that have access to it, and as such there are no
  44 * race conditions we have to worry about. The inode
  45 * is not on the hash-lists, and it cannot be reached
  46 * through the filesystem because the directory entry
  47 * has been deleted earlier.
  48 *
  49 * HOWEVER: we must make sure that we get no aliases,
  50 * which means that we have to call "clear_inode()"
  51 * _before_ we mark the inode not in use in the inode
  52 * bitmaps. Otherwise a newly created file might use
  53 * the same inode number (not actually the same pointer
  54 * though), and then we'd have two inodes sharing the
  55 * same inode number and space on the harddisk.
  56 */
  57void ufs_free_inode (struct inode * inode)
  58{
  59        struct super_block * sb;
  60        struct ufs_sb_private_info * uspi;
  61        struct ufs_super_block_first * usb1;
  62        struct ufs_cg_private_info * ucpi;
  63        struct ufs_cylinder_group * ucg;
  64        int is_directory;
  65        unsigned ino, cg, bit;
  66        
  67        UFSD("ENTER, ino %lu\n", inode->i_ino);
  68
  69        sb = inode->i_sb;
  70        uspi = UFS_SB(sb)->s_uspi;
  71        usb1 = ubh_get_usb_first(uspi);
  72        
  73        ino = inode->i_ino;
  74
  75        lock_super (sb);
  76
  77        if (!((ino > 1) && (ino < (uspi->s_ncg * uspi->s_ipg )))) {
  78                ufs_warning(sb, "ufs_free_inode", "reserved inode or nonexistent inode %u\n", ino);
  79                unlock_super (sb);
  80                return;
  81        }
  82        
  83        cg = ufs_inotocg (ino);
  84        bit = ufs_inotocgoff (ino);
  85        ucpi = ufs_load_cylinder (sb, cg);
  86        if (!ucpi) {
  87                unlock_super (sb);
  88                return;
  89        }
  90        ucg = ubh_get_ucg(UCPI_UBH(ucpi));
  91        if (!ufs_cg_chkmagic(sb, ucg))
  92                ufs_panic (sb, "ufs_free_fragments", "internal error, bad cg magic number");
  93
  94        ucg->cg_time = cpu_to_fs32(sb, get_seconds());
  95
  96        is_directory = S_ISDIR(inode->i_mode);
  97
  98        vfs_dq_free_inode(inode);
  99        vfs_dq_drop(inode);
 100
 101        clear_inode (inode);
 102
 103        if (ubh_isclr (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit))
 104                ufs_error(sb, "ufs_free_inode", "bit already cleared for inode %u", ino);
 105        else {
 106                ubh_clrbit (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit);
 107                if (ino < ucpi->c_irotor)
 108                        ucpi->c_irotor = ino;
 109                fs32_add(sb, &ucg->cg_cs.cs_nifree, 1);
 110                uspi->cs_total.cs_nifree++;
 111                fs32_add(sb, &UFS_SB(sb)->fs_cs(cg).cs_nifree, 1);
 112
 113                if (is_directory) {
 114                        fs32_sub(sb, &ucg->cg_cs.cs_ndir, 1);
 115                        uspi->cs_total.cs_ndir--;
 116                        fs32_sub(sb, &UFS_SB(sb)->fs_cs(cg).cs_ndir, 1);
 117                }
 118        }
 119
 120        ubh_mark_buffer_dirty (USPI_UBH(uspi));
 121        ubh_mark_buffer_dirty (UCPI_UBH(ucpi));
 122        if (sb->s_flags & MS_SYNCHRONOUS) {
 123                ubh_ll_rw_block(SWRITE, UCPI_UBH(ucpi));
 124                ubh_wait_on_buffer (UCPI_UBH(ucpi));
 125        }
 126        
 127        sb->s_dirt = 1;
 128        unlock_super (sb);
 129        UFSD("EXIT\n");
 130}
 131
 132/*
 133 * Nullify new chunk of inodes,
 134 * BSD people also set ui_gen field of inode
 135 * during nullification, but we not care about
 136 * that because of linux ufs do not support NFS
 137 */
 138static void ufs2_init_inodes_chunk(struct super_block *sb,
 139                                   struct ufs_cg_private_info *ucpi,
 140                                   struct ufs_cylinder_group *ucg)
 141{
 142        struct buffer_head *bh;
 143        struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
 144        sector_t beg = uspi->s_sbbase +
 145                ufs_inotofsba(ucpi->c_cgx * uspi->s_ipg +
 146                              fs32_to_cpu(sb, ucg->cg_u.cg_u2.cg_initediblk));
 147        sector_t end = beg + uspi->s_fpb;
 148
 149        UFSD("ENTER cgno %d\n", ucpi->c_cgx);
 150
 151        for (; beg < end; ++beg) {
 152                bh = sb_getblk(sb, beg);
 153                lock_buffer(bh);
 154                memset(bh->b_data, 0, sb->s_blocksize);
 155                set_buffer_uptodate(bh);
 156                mark_buffer_dirty(bh);
 157                unlock_buffer(bh);
 158                if (sb->s_flags & MS_SYNCHRONOUS)
 159                        sync_dirty_buffer(bh);
 160                brelse(bh);
 161        }
 162
 163        fs32_add(sb, &ucg->cg_u.cg_u2.cg_initediblk, uspi->s_inopb);
 164        ubh_mark_buffer_dirty(UCPI_UBH(ucpi));
 165        if (sb->s_flags & MS_SYNCHRONOUS) {
 166                ubh_ll_rw_block(SWRITE, UCPI_UBH(ucpi));
 167                ubh_wait_on_buffer(UCPI_UBH(ucpi));
 168        }
 169
 170        UFSD("EXIT\n");
 171}
 172
 173/*
 174 * There are two policies for allocating an inode.  If the new inode is
 175 * a directory, then a forward search is made for a block group with both
 176 * free space and a low directory-to-inode ratio; if that fails, then of
 177 * the groups with above-average free space, that group with the fewest
 178 * directories already is chosen.
 179 *
 180 * For other inodes, search forward from the parent directory's block
 181 * group to find a free inode.
 182 */
 183struct inode * ufs_new_inode(struct inode * dir, int mode)
 184{
 185        struct super_block * sb;
 186        struct ufs_sb_info * sbi;
 187        struct ufs_sb_private_info * uspi;
 188        struct ufs_super_block_first * usb1;
 189        struct ufs_cg_private_info * ucpi;
 190        struct ufs_cylinder_group * ucg;
 191        struct inode * inode;
 192        unsigned cg, bit, i, j, start;
 193        struct ufs_inode_info *ufsi;
 194        int err = -ENOSPC;
 195
 196        UFSD("ENTER\n");
 197        
 198        /* Cannot create files in a deleted directory */
 199        if (!dir || !dir->i_nlink)
 200                return ERR_PTR(-EPERM);
 201        sb = dir->i_sb;
 202        inode = new_inode(sb);
 203        if (!inode)
 204                return ERR_PTR(-ENOMEM);
 205        ufsi = UFS_I(inode);
 206        sbi = UFS_SB(sb);
 207        uspi = sbi->s_uspi;
 208        usb1 = ubh_get_usb_first(uspi);
 209
 210        lock_super (sb);
 211
 212        /*
 213         * Try to place the inode in its parent directory
 214         */
 215        i = ufs_inotocg(dir->i_ino);
 216        if (sbi->fs_cs(i).cs_nifree) {
 217                cg = i;
 218                goto cg_found;
 219        }
 220
 221        /*
 222         * Use a quadratic hash to find a group with a free inode
 223         */
 224        for ( j = 1; j < uspi->s_ncg; j <<= 1 ) {
 225                i += j;
 226                if (i >= uspi->s_ncg)
 227                        i -= uspi->s_ncg;
 228                if (sbi->fs_cs(i).cs_nifree) {
 229                        cg = i;
 230                        goto cg_found;
 231                }
 232        }
 233
 234        /*
 235         * That failed: try linear search for a free inode
 236         */
 237        i = ufs_inotocg(dir->i_ino) + 1;
 238        for (j = 2; j < uspi->s_ncg; j++) {
 239                i++;
 240                if (i >= uspi->s_ncg)
 241                        i = 0;
 242                if (sbi->fs_cs(i).cs_nifree) {
 243                        cg = i;
 244                        goto cg_found;
 245                }
 246        }
 247
 248        goto failed;
 249
 250cg_found:
 251        ucpi = ufs_load_cylinder (sb, cg);
 252        if (!ucpi) {
 253                err = -EIO;
 254                goto failed;
 255        }
 256        ucg = ubh_get_ucg(UCPI_UBH(ucpi));
 257        if (!ufs_cg_chkmagic(sb, ucg)) 
 258                ufs_panic (sb, "ufs_new_inode", "internal error, bad cg magic number");
 259
 260        start = ucpi->c_irotor;
 261        bit = ubh_find_next_zero_bit (UCPI_UBH(ucpi), ucpi->c_iusedoff, uspi->s_ipg, start);
 262        if (!(bit < uspi->s_ipg)) {
 263                bit = ubh_find_first_zero_bit (UCPI_UBH(ucpi), ucpi->c_iusedoff, start);
 264                if (!(bit < start)) {
 265                        ufs_error (sb, "ufs_new_inode",
 266                            "cylinder group %u corrupted - error in inode bitmap\n", cg);
 267                        err = -EIO;
 268                        goto failed;
 269                }
 270        }
 271        UFSD("start = %u, bit = %u, ipg = %u\n", start, bit, uspi->s_ipg);
 272        if (ubh_isclr (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit))
 273                ubh_setbit (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit);
 274        else {
 275                ufs_panic (sb, "ufs_new_inode", "internal error");
 276                err = -EIO;
 277                goto failed;
 278        }
 279
 280        if (uspi->fs_magic == UFS2_MAGIC) {
 281                u32 initediblk = fs32_to_cpu(sb, ucg->cg_u.cg_u2.cg_initediblk);
 282
 283                if (bit + uspi->s_inopb > initediblk &&
 284                    initediblk < fs32_to_cpu(sb, ucg->cg_u.cg_u2.cg_niblk))
 285                        ufs2_init_inodes_chunk(sb, ucpi, ucg);
 286        }
 287
 288        fs32_sub(sb, &ucg->cg_cs.cs_nifree, 1);
 289        uspi->cs_total.cs_nifree--;
 290        fs32_sub(sb, &sbi->fs_cs(cg).cs_nifree, 1);
 291        
 292        if (S_ISDIR(mode)) {
 293                fs32_add(sb, &ucg->cg_cs.cs_ndir, 1);
 294                uspi->cs_total.cs_ndir++;
 295                fs32_add(sb, &sbi->fs_cs(cg).cs_ndir, 1);
 296        }
 297        ubh_mark_buffer_dirty (USPI_UBH(uspi));
 298        ubh_mark_buffer_dirty (UCPI_UBH(ucpi));
 299        if (sb->s_flags & MS_SYNCHRONOUS) {
 300                ubh_ll_rw_block(SWRITE, UCPI_UBH(ucpi));
 301                ubh_wait_on_buffer (UCPI_UBH(ucpi));
 302        }
 303        sb->s_dirt = 1;
 304
 305        inode->i_ino = cg * uspi->s_ipg + bit;
 306        inode->i_mode = mode;
 307        inode->i_uid = current_fsuid();
 308        if (dir->i_mode & S_ISGID) {
 309                inode->i_gid = dir->i_gid;
 310                if (S_ISDIR(mode))
 311                        inode->i_mode |= S_ISGID;
 312        } else
 313                inode->i_gid = current_fsgid();
 314
 315        inode->i_blocks = 0;
 316        inode->i_generation = 0;
 317        inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
 318        ufsi->i_flags = UFS_I(dir)->i_flags;
 319        ufsi->i_lastfrag = 0;
 320        ufsi->i_shadow = 0;
 321        ufsi->i_osync = 0;
 322        ufsi->i_oeftflag = 0;
 323        ufsi->i_dir_start_lookup = 0;
 324        memset(&ufsi->i_u1, 0, sizeof(ufsi->i_u1));
 325        insert_inode_hash(inode);
 326        mark_inode_dirty(inode);
 327
 328        if (uspi->fs_magic == UFS2_MAGIC) {
 329                struct buffer_head *bh;
 330                struct ufs2_inode *ufs2_inode;
 331
 332                /*
 333                 * setup birth date, we do it here because of there is no sense
 334                 * to hold it in struct ufs_inode_info, and lose 64 bit
 335                 */
 336                bh = sb_bread(sb, uspi->s_sbbase + ufs_inotofsba(inode->i_ino));
 337                if (!bh) {
 338                        ufs_warning(sb, "ufs_read_inode",
 339                                    "unable to read inode %lu\n",
 340                                    inode->i_ino);
 341                        err = -EIO;
 342                        goto fail_remove_inode;
 343                }
 344                lock_buffer(bh);
 345                ufs2_inode = (struct ufs2_inode *)bh->b_data;
 346                ufs2_inode += ufs_inotofsbo(inode->i_ino);
 347                ufs2_inode->ui_birthtime = cpu_to_fs64(sb, CURRENT_TIME.tv_sec);
 348                ufs2_inode->ui_birthnsec = cpu_to_fs32(sb, CURRENT_TIME.tv_nsec);
 349                mark_buffer_dirty(bh);
 350                unlock_buffer(bh);
 351                if (sb->s_flags & MS_SYNCHRONOUS)
 352                        sync_dirty_buffer(bh);
 353                brelse(bh);
 354        }
 355
 356        unlock_super (sb);
 357
 358        if (vfs_dq_alloc_inode(inode)) {
 359                vfs_dq_drop(inode);
 360                err = -EDQUOT;
 361                goto fail_without_unlock;
 362        }
 363
 364        UFSD("allocating inode %lu\n", inode->i_ino);
 365        UFSD("EXIT\n");
 366        return inode;
 367
 368fail_remove_inode:
 369        unlock_super(sb);
 370fail_without_unlock:
 371        inode->i_flags |= S_NOQUOTA;
 372        inode->i_nlink = 0;
 373        iput(inode);
 374        UFSD("EXIT (FAILED): err %d\n", err);
 375        return ERR_PTR(err);
 376failed:
 377        unlock_super (sb);
 378        make_bad_inode(inode);
 379        iput (inode);
 380        UFSD("EXIT (FAILED): err %d\n", err);
 381        return ERR_PTR(err);
 382}
 383