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