Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/ufs/balloc.c |
| 3 | * |
| 4 | * Copyright (C) 1998 |
| 5 | * Daniel Pirkl <daniel.pirkl@email.cz> |
| 6 | * Charles University, Faculty of Mathematics and Physics |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 7 | * |
| 8 | * UFS2 write support Evgeniy Dushistov <dushistov@mail.ru>, 2007 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <linux/fs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/stat.h> |
| 13 | #include <linux/time.h> |
| 14 | #include <linux/string.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/buffer_head.h> |
Randy Dunlap | 16f7e0f | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 16 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/bitops.h> |
| 18 | #include <asm/byteorder.h> |
| 19 | |
Mike Frysinger | e542059 | 2008-02-08 04:21:31 -0800 | [diff] [blame] | 20 | #include "ufs_fs.h" |
Christoph Hellwig | bcd6d4e | 2007-10-16 23:26:51 -0700 | [diff] [blame] | 21 | #include "ufs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include "swab.h" |
| 23 | #include "util.h" |
| 24 | |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 25 | #define INVBLOCK ((u64)-1L) |
| 26 | |
Fabian Frederick | 45641c8 | 2014-06-06 14:36:33 -0700 | [diff] [blame] | 27 | static u64 ufs_add_fragments(struct inode *, u64, unsigned, unsigned); |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 28 | static u64 ufs_alloc_fragments(struct inode *, unsigned, u64, unsigned, int *); |
| 29 | static u64 ufs_alloccg_block(struct inode *, struct ufs_cg_private_info *, u64, int *); |
| 30 | static u64 ufs_bitmap_search (struct super_block *, struct ufs_cg_private_info *, u64, unsigned); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | static unsigned char ufs_fragtable_8fpb[], ufs_fragtable_other[]; |
| 32 | static void ufs_clusteracct(struct super_block *, struct ufs_cg_private_info *, unsigned, int); |
| 33 | |
| 34 | /* |
| 35 | * Free 'count' fragments from fragment number 'fragment' |
| 36 | */ |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 37 | void ufs_free_fragments(struct inode *inode, u64 fragment, unsigned count) |
Evgeniy Dushistov | 6ef4d6b | 2006-06-25 05:47:20 -0700 | [diff] [blame] | 38 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | struct super_block * sb; |
| 40 | struct ufs_sb_private_info * uspi; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | struct ufs_cg_private_info * ucpi; |
| 42 | struct ufs_cylinder_group * ucg; |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 43 | unsigned cgno, bit, end_bit, bbase, blkmap, i; |
| 44 | u64 blkno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
| 46 | sb = inode->i_sb; |
| 47 | uspi = UFS_SB(sb)->s_uspi; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 49 | UFSD("ENTER, fragment %llu, count %u\n", |
| 50 | (unsigned long long)fragment, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
| 52 | if (ufs_fragnum(fragment) + count > uspi->s_fpg) |
| 53 | ufs_error (sb, "ufs_free_fragments", "internal error"); |
| 54 | |
Fabian Frederick | 0244756 | 2014-06-06 14:38:34 -0700 | [diff] [blame] | 55 | lock_ufs(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 57 | cgno = ufs_dtog(uspi, fragment); |
| 58 | bit = ufs_dtogd(uspi, fragment); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | if (cgno >= uspi->s_ncg) { |
| 60 | ufs_panic (sb, "ufs_free_fragments", "freeing blocks are outside device"); |
| 61 | goto failed; |
| 62 | } |
| 63 | |
| 64 | ucpi = ufs_load_cylinder (sb, cgno); |
| 65 | if (!ucpi) |
| 66 | goto failed; |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 67 | ucg = ubh_get_ucg (UCPI_UBH(ucpi)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | if (!ufs_cg_chkmagic(sb, ucg)) { |
| 69 | ufs_panic (sb, "ufs_free_fragments", "internal error, bad magic number on cg %u", cgno); |
| 70 | goto failed; |
| 71 | } |
| 72 | |
| 73 | end_bit = bit + count; |
| 74 | bbase = ufs_blknum (bit); |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 75 | blkmap = ubh_blkmap (UCPI_UBH(ucpi), ucpi->c_freeoff, bbase); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | ufs_fragacct (sb, blkmap, ucg->cg_frsum, -1); |
| 77 | for (i = bit; i < end_bit; i++) { |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 78 | if (ubh_isclr (UCPI_UBH(ucpi), ucpi->c_freeoff, i)) |
| 79 | ubh_setbit (UCPI_UBH(ucpi), ucpi->c_freeoff, i); |
Evgeniy | 7b4ee73 | 2006-01-14 11:42:06 +0300 | [diff] [blame] | 80 | else |
| 81 | ufs_error (sb, "ufs_free_fragments", |
| 82 | "bit already cleared for fragment %u", i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | fs32_add(sb, &ucg->cg_cs.cs_nffree, count); |
Evgeniy Dushistov | ee3ffd6 | 2006-06-25 05:47:30 -0700 | [diff] [blame] | 86 | uspi->cs_total.cs_nffree += count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | fs32_add(sb, &UFS_SB(sb)->fs_cs(cgno).cs_nffree, count); |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 88 | blkmap = ubh_blkmap (UCPI_UBH(ucpi), ucpi->c_freeoff, bbase); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | ufs_fragacct(sb, blkmap, ucg->cg_frsum, 1); |
| 90 | |
| 91 | /* |
| 92 | * Trying to reassemble free fragments into block |
| 93 | */ |
| 94 | blkno = ufs_fragstoblks (bbase); |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 95 | if (ubh_isblockset(UCPI_UBH(ucpi), ucpi->c_freeoff, blkno)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | fs32_sub(sb, &ucg->cg_cs.cs_nffree, uspi->s_fpb); |
Evgeniy Dushistov | ee3ffd6 | 2006-06-25 05:47:30 -0700 | [diff] [blame] | 97 | uspi->cs_total.cs_nffree -= uspi->s_fpb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | fs32_sub(sb, &UFS_SB(sb)->fs_cs(cgno).cs_nffree, uspi->s_fpb); |
| 99 | if ((UFS_SB(sb)->s_flags & UFS_CG_MASK) == UFS_CG_44BSD) |
| 100 | ufs_clusteracct (sb, ucpi, blkno, 1); |
| 101 | fs32_add(sb, &ucg->cg_cs.cs_nbfree, 1); |
Evgeniy Dushistov | ee3ffd6 | 2006-06-25 05:47:30 -0700 | [diff] [blame] | 102 | uspi->cs_total.cs_nbfree++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | fs32_add(sb, &UFS_SB(sb)->fs_cs(cgno).cs_nbfree, 1); |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 104 | if (uspi->fs_magic != UFS2_MAGIC) { |
| 105 | unsigned cylno = ufs_cbtocylno (bbase); |
| 106 | |
| 107 | fs16_add(sb, &ubh_cg_blks(ucpi, cylno, |
| 108 | ufs_cbtorpos(bbase)), 1); |
| 109 | fs32_add(sb, &ubh_cg_blktot(ucpi, cylno), 1); |
| 110 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 113 | ubh_mark_buffer_dirty (USPI_UBH(uspi)); |
| 114 | ubh_mark_buffer_dirty (UCPI_UBH(ucpi)); |
Christoph Hellwig | 9cb569d | 2010-08-11 17:06:24 +0200 | [diff] [blame] | 115 | if (sb->s_flags & MS_SYNCHRONOUS) |
| 116 | ubh_sync_block(UCPI_UBH(ucpi)); |
Artem Bityutskiy | 9e9ad5f | 2012-07-12 16:28:08 +0300 | [diff] [blame] | 117 | ufs_mark_sb_dirty(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | |
Fabian Frederick | 0244756 | 2014-06-06 14:38:34 -0700 | [diff] [blame] | 119 | unlock_ufs(sb); |
Evgeniy Dushistov | abf5d15 | 2006-06-25 05:47:24 -0700 | [diff] [blame] | 120 | UFSD("EXIT\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | return; |
| 122 | |
| 123 | failed: |
Fabian Frederick | 0244756 | 2014-06-06 14:38:34 -0700 | [diff] [blame] | 124 | unlock_ufs(sb); |
Evgeniy Dushistov | abf5d15 | 2006-06-25 05:47:24 -0700 | [diff] [blame] | 125 | UFSD("EXIT (FAILED)\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | return; |
| 127 | } |
| 128 | |
| 129 | /* |
| 130 | * Free 'count' fragments from fragment number 'fragment' (free whole blocks) |
| 131 | */ |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 132 | void ufs_free_blocks(struct inode *inode, u64 fragment, unsigned count) |
Evgeniy Dushistov | 6ef4d6b | 2006-06-25 05:47:20 -0700 | [diff] [blame] | 133 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | struct super_block * sb; |
| 135 | struct ufs_sb_private_info * uspi; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | struct ufs_cg_private_info * ucpi; |
| 137 | struct ufs_cylinder_group * ucg; |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 138 | unsigned overflow, cgno, bit, end_bit, i; |
| 139 | u64 blkno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | |
| 141 | sb = inode->i_sb; |
| 142 | uspi = UFS_SB(sb)->s_uspi; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 144 | UFSD("ENTER, fragment %llu, count %u\n", |
| 145 | (unsigned long long)fragment, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
| 147 | if ((fragment & uspi->s_fpbmask) || (count & uspi->s_fpbmask)) { |
| 148 | ufs_error (sb, "ufs_free_blocks", "internal error, " |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 149 | "fragment %llu, count %u\n", |
| 150 | (unsigned long long)fragment, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | goto failed; |
| 152 | } |
| 153 | |
Fabian Frederick | 0244756 | 2014-06-06 14:38:34 -0700 | [diff] [blame] | 154 | lock_ufs(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | |
| 156 | do_more: |
| 157 | overflow = 0; |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 158 | cgno = ufs_dtog(uspi, fragment); |
| 159 | bit = ufs_dtogd(uspi, fragment); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | if (cgno >= uspi->s_ncg) { |
| 161 | ufs_panic (sb, "ufs_free_blocks", "freeing blocks are outside device"); |
Evgeniy Dushistov | 2e00639 | 2006-06-25 05:47:26 -0700 | [diff] [blame] | 162 | goto failed_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | } |
| 164 | end_bit = bit + count; |
| 165 | if (end_bit > uspi->s_fpg) { |
| 166 | overflow = bit + count - uspi->s_fpg; |
| 167 | count -= overflow; |
| 168 | end_bit -= overflow; |
| 169 | } |
| 170 | |
| 171 | ucpi = ufs_load_cylinder (sb, cgno); |
| 172 | if (!ucpi) |
Evgeniy Dushistov | 2e00639 | 2006-06-25 05:47:26 -0700 | [diff] [blame] | 173 | goto failed_unlock; |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 174 | ucg = ubh_get_ucg (UCPI_UBH(ucpi)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | if (!ufs_cg_chkmagic(sb, ucg)) { |
| 176 | ufs_panic (sb, "ufs_free_blocks", "internal error, bad magic number on cg %u", cgno); |
Evgeniy Dushistov | 2e00639 | 2006-06-25 05:47:26 -0700 | [diff] [blame] | 177 | goto failed_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | for (i = bit; i < end_bit; i += uspi->s_fpb) { |
| 181 | blkno = ufs_fragstoblks(i); |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 182 | if (ubh_isblockset(UCPI_UBH(ucpi), ucpi->c_freeoff, blkno)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | ufs_error(sb, "ufs_free_blocks", "freeing free fragment"); |
| 184 | } |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 185 | ubh_setblock(UCPI_UBH(ucpi), ucpi->c_freeoff, blkno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | if ((UFS_SB(sb)->s_flags & UFS_CG_MASK) == UFS_CG_44BSD) |
| 187 | ufs_clusteracct (sb, ucpi, blkno, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | |
| 189 | fs32_add(sb, &ucg->cg_cs.cs_nbfree, 1); |
Evgeniy Dushistov | ee3ffd6 | 2006-06-25 05:47:30 -0700 | [diff] [blame] | 190 | uspi->cs_total.cs_nbfree++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | fs32_add(sb, &UFS_SB(sb)->fs_cs(cgno).cs_nbfree, 1); |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 192 | |
| 193 | if (uspi->fs_magic != UFS2_MAGIC) { |
| 194 | unsigned cylno = ufs_cbtocylno(i); |
| 195 | |
| 196 | fs16_add(sb, &ubh_cg_blks(ucpi, cylno, |
| 197 | ufs_cbtorpos(i)), 1); |
| 198 | fs32_add(sb, &ubh_cg_blktot(ucpi, cylno), 1); |
| 199 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 202 | ubh_mark_buffer_dirty (USPI_UBH(uspi)); |
| 203 | ubh_mark_buffer_dirty (UCPI_UBH(ucpi)); |
Christoph Hellwig | 9cb569d | 2010-08-11 17:06:24 +0200 | [diff] [blame] | 204 | if (sb->s_flags & MS_SYNCHRONOUS) |
| 205 | ubh_sync_block(UCPI_UBH(ucpi)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | |
| 207 | if (overflow) { |
| 208 | fragment += count; |
| 209 | count = overflow; |
| 210 | goto do_more; |
| 211 | } |
| 212 | |
Artem Bityutskiy | 9e9ad5f | 2012-07-12 16:28:08 +0300 | [diff] [blame] | 213 | ufs_mark_sb_dirty(sb); |
Fabian Frederick | 0244756 | 2014-06-06 14:38:34 -0700 | [diff] [blame] | 214 | unlock_ufs(sb); |
Evgeniy Dushistov | abf5d15 | 2006-06-25 05:47:24 -0700 | [diff] [blame] | 215 | UFSD("EXIT\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | return; |
| 217 | |
Evgeniy Dushistov | 2e00639 | 2006-06-25 05:47:26 -0700 | [diff] [blame] | 218 | failed_unlock: |
Fabian Frederick | 0244756 | 2014-06-06 14:38:34 -0700 | [diff] [blame] | 219 | unlock_ufs(sb); |
Evgeniy Dushistov | 2e00639 | 2006-06-25 05:47:26 -0700 | [diff] [blame] | 220 | failed: |
Evgeniy Dushistov | abf5d15 | 2006-06-25 05:47:24 -0700 | [diff] [blame] | 221 | UFSD("EXIT (FAILED)\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | return; |
| 223 | } |
| 224 | |
Evgeniy Dushistov | 6ef4d6b | 2006-06-25 05:47:20 -0700 | [diff] [blame] | 225 | /* |
| 226 | * Modify inode page cache in such way: |
| 227 | * have - blocks with b_blocknr equal to oldb...oldb+count-1 |
| 228 | * get - blocks with b_blocknr equal to newb...newb+count-1 |
| 229 | * also we suppose that oldb...oldb+count-1 blocks |
| 230 | * situated at the end of file. |
| 231 | * |
| 232 | * We can come here from ufs_writepage or ufs_prepare_write, |
| 233 | * locked_page is argument of these functions, so we already lock it. |
| 234 | */ |
Evgeniy Dushistov | 5431bf9 | 2007-03-16 13:38:08 -0800 | [diff] [blame] | 235 | static void ufs_change_blocknr(struct inode *inode, sector_t beg, |
| 236 | unsigned int count, sector_t oldb, |
| 237 | sector_t newb, struct page *locked_page) |
Evgeniy Dushistov | 6ef4d6b | 2006-06-25 05:47:20 -0700 | [diff] [blame] | 238 | { |
Evgeniy Dushistov | 5431bf9 | 2007-03-16 13:38:08 -0800 | [diff] [blame] | 239 | const unsigned blks_per_page = |
| 240 | 1 << (PAGE_CACHE_SHIFT - inode->i_blkbits); |
| 241 | const unsigned mask = blks_per_page - 1; |
Evgeniy Dushistov | efee2b8 | 2007-01-29 13:19:56 -0800 | [diff] [blame] | 242 | struct address_space * const mapping = inode->i_mapping; |
Evgeniy Dushistov | 5431bf9 | 2007-03-16 13:38:08 -0800 | [diff] [blame] | 243 | pgoff_t index, cur_index, last_index; |
| 244 | unsigned pos, j, lblock; |
| 245 | sector_t end, i; |
Evgeniy Dushistov | 6ef4d6b | 2006-06-25 05:47:20 -0700 | [diff] [blame] | 246 | struct page *page; |
| 247 | struct buffer_head *head, *bh; |
| 248 | |
Evgeniy Dushistov | 5431bf9 | 2007-03-16 13:38:08 -0800 | [diff] [blame] | 249 | UFSD("ENTER, ino %lu, count %u, oldb %llu, newb %llu\n", |
| 250 | inode->i_ino, count, |
| 251 | (unsigned long long)oldb, (unsigned long long)newb); |
Evgeniy Dushistov | 6ef4d6b | 2006-06-25 05:47:20 -0700 | [diff] [blame] | 252 | |
Evgeniy Dushistov | a685e26 | 2007-01-29 13:19:54 -0800 | [diff] [blame] | 253 | BUG_ON(!locked_page); |
Evgeniy Dushistov | 6ef4d6b | 2006-06-25 05:47:20 -0700 | [diff] [blame] | 254 | BUG_ON(!PageLocked(locked_page)); |
| 255 | |
Evgeniy Dushistov | a685e26 | 2007-01-29 13:19:54 -0800 | [diff] [blame] | 256 | cur_index = locked_page->index; |
Evgeniy Dushistov | 5431bf9 | 2007-03-16 13:38:08 -0800 | [diff] [blame] | 257 | end = count + beg; |
| 258 | last_index = end >> (PAGE_CACHE_SHIFT - inode->i_blkbits); |
| 259 | for (i = beg; i < end; i = (i | mask) + 1) { |
| 260 | index = i >> (PAGE_CACHE_SHIFT - inode->i_blkbits); |
Evgeniy Dushistov | 6ef4d6b | 2006-06-25 05:47:20 -0700 | [diff] [blame] | 261 | |
| 262 | if (likely(cur_index != index)) { |
| 263 | page = ufs_get_locked_page(mapping, index); |
Evgeniy Dushistov | 5431bf9 | 2007-03-16 13:38:08 -0800 | [diff] [blame] | 264 | if (!page)/* it was truncated */ |
Evgeniy Dushistov | 6ef4d6b | 2006-06-25 05:47:20 -0700 | [diff] [blame] | 265 | continue; |
Evgeniy Dushistov | 5431bf9 | 2007-03-16 13:38:08 -0800 | [diff] [blame] | 266 | if (IS_ERR(page)) {/* or EIO */ |
Harvey Harrison | 9746077a7 | 2008-04-28 02:16:17 -0700 | [diff] [blame] | 267 | ufs_error(inode->i_sb, __func__, |
Evgeniy Dushistov | 5431bf9 | 2007-03-16 13:38:08 -0800 | [diff] [blame] | 268 | "read of page %llu failed\n", |
| 269 | (unsigned long long)index); |
| 270 | continue; |
| 271 | } |
Evgeniy Dushistov | 6ef4d6b | 2006-06-25 05:47:20 -0700 | [diff] [blame] | 272 | } else |
| 273 | page = locked_page; |
| 274 | |
Evgeniy Dushistov | 6ef4d6b | 2006-06-25 05:47:20 -0700 | [diff] [blame] | 275 | head = page_buffers(page); |
| 276 | bh = head; |
Evgeniy Dushistov | 5431bf9 | 2007-03-16 13:38:08 -0800 | [diff] [blame] | 277 | pos = i & mask; |
Evgeniy Dushistov | efee2b8 | 2007-01-29 13:19:56 -0800 | [diff] [blame] | 278 | for (j = 0; j < pos; ++j) |
| 279 | bh = bh->b_this_page; |
Evgeniy Dushistov | 5431bf9 | 2007-03-16 13:38:08 -0800 | [diff] [blame] | 280 | |
| 281 | |
| 282 | if (unlikely(index == last_index)) |
| 283 | lblock = end & mask; |
| 284 | else |
| 285 | lblock = blks_per_page; |
| 286 | |
Evgeniy Dushistov | 6ef4d6b | 2006-06-25 05:47:20 -0700 | [diff] [blame] | 287 | do { |
Evgeniy Dushistov | 5431bf9 | 2007-03-16 13:38:08 -0800 | [diff] [blame] | 288 | if (j >= lblock) |
| 289 | break; |
| 290 | pos = (i - beg) + j; |
| 291 | |
| 292 | if (!buffer_mapped(bh)) |
| 293 | map_bh(bh, inode->i_sb, oldb + pos); |
| 294 | if (!buffer_uptodate(bh)) { |
| 295 | ll_rw_block(READ, 1, &bh); |
| 296 | wait_on_buffer(bh); |
| 297 | if (!buffer_uptodate(bh)) { |
Harvey Harrison | 9746077a7 | 2008-04-28 02:16:17 -0700 | [diff] [blame] | 298 | ufs_error(inode->i_sb, __func__, |
Evgeniy Dushistov | 5431bf9 | 2007-03-16 13:38:08 -0800 | [diff] [blame] | 299 | "read of block failed\n"); |
| 300 | break; |
Evgeniy Dushistov | efee2b8 | 2007-01-29 13:19:56 -0800 | [diff] [blame] | 301 | } |
Evgeniy Dushistov | 6ef4d6b | 2006-06-25 05:47:20 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Evgeniy Dushistov | 5431bf9 | 2007-03-16 13:38:08 -0800 | [diff] [blame] | 304 | UFSD(" change from %llu to %llu, pos %u\n", |
Andrew Morton | 9df1303 | 2008-03-19 17:01:05 -0700 | [diff] [blame] | 305 | (unsigned long long)(pos + oldb), |
| 306 | (unsigned long long)(pos + newb), pos); |
Evgeniy Dushistov | 5431bf9 | 2007-03-16 13:38:08 -0800 | [diff] [blame] | 307 | |
| 308 | bh->b_blocknr = newb + pos; |
| 309 | unmap_underlying_metadata(bh->b_bdev, |
| 310 | bh->b_blocknr); |
| 311 | mark_buffer_dirty(bh); |
| 312 | ++j; |
Evgeniy Dushistov | 6ef4d6b | 2006-06-25 05:47:20 -0700 | [diff] [blame] | 313 | bh = bh->b_this_page; |
| 314 | } while (bh != head); |
| 315 | |
Evgeniy Dushistov | 10e5dce | 2006-07-01 04:36:24 -0700 | [diff] [blame] | 316 | if (likely(cur_index != index)) |
| 317 | ufs_put_locked_page(page); |
Evgeniy Dushistov | 6ef4d6b | 2006-06-25 05:47:20 -0700 | [diff] [blame] | 318 | } |
Evgeniy Dushistov | abf5d15 | 2006-06-25 05:47:24 -0700 | [diff] [blame] | 319 | UFSD("EXIT\n"); |
Evgeniy Dushistov | 6ef4d6b | 2006-06-25 05:47:20 -0700 | [diff] [blame] | 320 | } |
| 321 | |
Evgeniy Dushistov | d63b709 | 2007-01-05 16:37:04 -0800 | [diff] [blame] | 322 | static void ufs_clear_frags(struct inode *inode, sector_t beg, unsigned int n, |
| 323 | int sync) |
| 324 | { |
| 325 | struct buffer_head *bh; |
| 326 | sector_t end = beg + n; |
| 327 | |
| 328 | for (; beg < end; ++beg) { |
| 329 | bh = sb_getblk(inode->i_sb, beg); |
| 330 | lock_buffer(bh); |
| 331 | memset(bh->b_data, 0, inode->i_sb->s_blocksize); |
| 332 | set_buffer_uptodate(bh); |
| 333 | mark_buffer_dirty(bh); |
| 334 | unlock_buffer(bh); |
| 335 | if (IS_SYNC(inode) || sync) |
| 336 | sync_dirty_buffer(bh); |
| 337 | brelse(bh); |
| 338 | } |
| 339 | } |
| 340 | |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 341 | u64 ufs_new_fragments(struct inode *inode, void *p, u64 fragment, |
| 342 | u64 goal, unsigned count, int *err, |
| 343 | struct page *locked_page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | { |
| 345 | struct super_block * sb; |
| 346 | struct ufs_sb_private_info * uspi; |
| 347 | struct ufs_super_block_first * usb1; |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 348 | unsigned cgno, oldcount, newcount; |
| 349 | u64 tmp, request, result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 351 | UFSD("ENTER, ino %lu, fragment %llu, goal %llu, count %u\n", |
| 352 | inode->i_ino, (unsigned long long)fragment, |
| 353 | (unsigned long long)goal, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | |
| 355 | sb = inode->i_sb; |
| 356 | uspi = UFS_SB(sb)->s_uspi; |
Evgeniy | 7b4ee73 | 2006-01-14 11:42:06 +0300 | [diff] [blame] | 357 | usb1 = ubh_get_usb_first(uspi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | *err = -ENOSPC; |
| 359 | |
Fabian Frederick | 0244756 | 2014-06-06 14:38:34 -0700 | [diff] [blame] | 360 | lock_ufs(sb); |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 361 | tmp = ufs_data_ptr_to_cpu(sb, p); |
| 362 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | if (count + ufs_fragnum(fragment) > uspi->s_fpb) { |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 364 | ufs_warning(sb, "ufs_new_fragments", "internal warning" |
| 365 | " fragment %llu, count %u", |
| 366 | (unsigned long long)fragment, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | count = uspi->s_fpb - ufs_fragnum(fragment); |
| 368 | } |
| 369 | oldcount = ufs_fragnum (fragment); |
| 370 | newcount = oldcount + count; |
| 371 | |
| 372 | /* |
| 373 | * Somebody else has just allocated our fragments |
| 374 | */ |
| 375 | if (oldcount) { |
| 376 | if (!tmp) { |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 377 | ufs_error(sb, "ufs_new_fragments", "internal error, " |
| 378 | "fragment %llu, tmp %llu\n", |
| 379 | (unsigned long long)fragment, |
| 380 | (unsigned long long)tmp); |
Fabian Frederick | 0244756 | 2014-06-06 14:38:34 -0700 | [diff] [blame] | 381 | unlock_ufs(sb); |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 382 | return INVBLOCK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | } |
| 384 | if (fragment < UFS_I(inode)->i_lastfrag) { |
Evgeniy Dushistov | abf5d15 | 2006-06-25 05:47:24 -0700 | [diff] [blame] | 385 | UFSD("EXIT (ALREADY ALLOCATED)\n"); |
Fabian Frederick | 0244756 | 2014-06-06 14:38:34 -0700 | [diff] [blame] | 386 | unlock_ufs(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | return 0; |
| 388 | } |
| 389 | } |
| 390 | else { |
| 391 | if (tmp) { |
Evgeniy Dushistov | abf5d15 | 2006-06-25 05:47:24 -0700 | [diff] [blame] | 392 | UFSD("EXIT (ALREADY ALLOCATED)\n"); |
Fabian Frederick | 0244756 | 2014-06-06 14:38:34 -0700 | [diff] [blame] | 393 | unlock_ufs(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | return 0; |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | /* |
| 399 | * There is not enough space for user on the device |
| 400 | */ |
Evgeniy Dushistov | ee3ffd6 | 2006-06-25 05:47:30 -0700 | [diff] [blame] | 401 | if (!capable(CAP_SYS_RESOURCE) && ufs_freespace(uspi, UFS_MINFREE) <= 0) { |
Fabian Frederick | 0244756 | 2014-06-06 14:38:34 -0700 | [diff] [blame] | 402 | unlock_ufs(sb); |
Evgeniy Dushistov | abf5d15 | 2006-06-25 05:47:24 -0700 | [diff] [blame] | 403 | UFSD("EXIT (FAILED)\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | return 0; |
| 405 | } |
| 406 | |
| 407 | if (goal >= uspi->s_size) |
| 408 | goal = 0; |
| 409 | if (goal == 0) |
| 410 | cgno = ufs_inotocg (inode->i_ino); |
| 411 | else |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 412 | cgno = ufs_dtog(uspi, goal); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | |
| 414 | /* |
| 415 | * allocate new fragment |
| 416 | */ |
| 417 | if (oldcount == 0) { |
| 418 | result = ufs_alloc_fragments (inode, cgno, goal, count, err); |
| 419 | if (result) { |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 420 | ufs_cpu_to_data_ptr(sb, p, result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | *err = 0; |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 422 | UFS_I(inode)->i_lastfrag = |
Dan Carpenter | 1d58272 | 2011-05-26 16:25:12 -0700 | [diff] [blame] | 423 | max(UFS_I(inode)->i_lastfrag, fragment + count); |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 424 | ufs_clear_frags(inode, result + oldcount, |
| 425 | newcount - oldcount, locked_page != NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | } |
Fabian Frederick | 0244756 | 2014-06-06 14:38:34 -0700 | [diff] [blame] | 427 | unlock_ufs(sb); |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 428 | UFSD("EXIT, result %llu\n", (unsigned long long)result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | return result; |
| 430 | } |
| 431 | |
| 432 | /* |
| 433 | * resize block |
| 434 | */ |
Fabian Frederick | 45641c8 | 2014-06-06 14:36:33 -0700 | [diff] [blame] | 435 | result = ufs_add_fragments(inode, tmp, oldcount, newcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | if (result) { |
| 437 | *err = 0; |
Dan Carpenter | 1d58272 | 2011-05-26 16:25:12 -0700 | [diff] [blame] | 438 | UFS_I(inode)->i_lastfrag = max(UFS_I(inode)->i_lastfrag, |
| 439 | fragment + count); |
Evgeniy Dushistov | d63b709 | 2007-01-05 16:37:04 -0800 | [diff] [blame] | 440 | ufs_clear_frags(inode, result + oldcount, newcount - oldcount, |
| 441 | locked_page != NULL); |
Fabian Frederick | 0244756 | 2014-06-06 14:38:34 -0700 | [diff] [blame] | 442 | unlock_ufs(sb); |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 443 | UFSD("EXIT, result %llu\n", (unsigned long long)result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | return result; |
| 445 | } |
| 446 | |
| 447 | /* |
| 448 | * allocate new block and move data |
| 449 | */ |
| 450 | switch (fs32_to_cpu(sb, usb1->fs_optim)) { |
| 451 | case UFS_OPTSPACE: |
| 452 | request = newcount; |
Evgeniy Dushistov | ee3ffd6 | 2006-06-25 05:47:30 -0700 | [diff] [blame] | 453 | if (uspi->s_minfree < 5 || uspi->cs_total.cs_nffree |
| 454 | > uspi->s_dsize * uspi->s_minfree / (2 * 100)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | break; |
| 456 | usb1->fs_optim = cpu_to_fs32(sb, UFS_OPTTIME); |
| 457 | break; |
| 458 | default: |
| 459 | usb1->fs_optim = cpu_to_fs32(sb, UFS_OPTTIME); |
| 460 | |
| 461 | case UFS_OPTTIME: |
| 462 | request = uspi->s_fpb; |
Evgeniy Dushistov | ee3ffd6 | 2006-06-25 05:47:30 -0700 | [diff] [blame] | 463 | if (uspi->cs_total.cs_nffree < uspi->s_dsize * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | (uspi->s_minfree - 2) / 100) |
| 465 | break; |
| 466 | usb1->fs_optim = cpu_to_fs32(sb, UFS_OPTTIME); |
| 467 | break; |
| 468 | } |
| 469 | result = ufs_alloc_fragments (inode, cgno, goal, request, err); |
| 470 | if (result) { |
Evgeniy Dushistov | efee2b8 | 2007-01-29 13:19:56 -0800 | [diff] [blame] | 471 | ufs_clear_frags(inode, result + oldcount, newcount - oldcount, |
| 472 | locked_page != NULL); |
Evgeniy Dushistov | 4b25a37 | 2007-03-16 13:38:09 -0800 | [diff] [blame] | 473 | ufs_change_blocknr(inode, fragment - oldcount, oldcount, |
| 474 | uspi->s_sbbase + tmp, |
| 475 | uspi->s_sbbase + result, locked_page); |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 476 | ufs_cpu_to_data_ptr(sb, p, result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | *err = 0; |
Dan Carpenter | 1d58272 | 2011-05-26 16:25:12 -0700 | [diff] [blame] | 478 | UFS_I(inode)->i_lastfrag = max(UFS_I(inode)->i_lastfrag, |
| 479 | fragment + count); |
Fabian Frederick | 0244756 | 2014-06-06 14:38:34 -0700 | [diff] [blame] | 480 | unlock_ufs(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | if (newcount < request) |
| 482 | ufs_free_fragments (inode, result + newcount, request - newcount); |
| 483 | ufs_free_fragments (inode, tmp, oldcount); |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 484 | UFSD("EXIT, result %llu\n", (unsigned long long)result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | return result; |
| 486 | } |
| 487 | |
Fabian Frederick | 0244756 | 2014-06-06 14:38:34 -0700 | [diff] [blame] | 488 | unlock_ufs(sb); |
Evgeniy Dushistov | abf5d15 | 2006-06-25 05:47:24 -0700 | [diff] [blame] | 489 | UFSD("EXIT (FAILED)\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | return 0; |
| 491 | } |
| 492 | |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 493 | static u64 ufs_add_fragments(struct inode *inode, u64 fragment, |
Fabian Frederick | 45641c8 | 2014-06-06 14:36:33 -0700 | [diff] [blame] | 494 | unsigned oldcount, unsigned newcount) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | { |
| 496 | struct super_block * sb; |
| 497 | struct ufs_sb_private_info * uspi; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | struct ufs_cg_private_info * ucpi; |
| 499 | struct ufs_cylinder_group * ucg; |
| 500 | unsigned cgno, fragno, fragoff, count, fragsize, i; |
| 501 | |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 502 | UFSD("ENTER, fragment %llu, oldcount %u, newcount %u\n", |
| 503 | (unsigned long long)fragment, oldcount, newcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | |
| 505 | sb = inode->i_sb; |
| 506 | uspi = UFS_SB(sb)->s_uspi; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | count = newcount - oldcount; |
| 508 | |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 509 | cgno = ufs_dtog(uspi, fragment); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | if (fs32_to_cpu(sb, UFS_SB(sb)->fs_cs(cgno).cs_nffree) < count) |
| 511 | return 0; |
| 512 | if ((ufs_fragnum (fragment) + newcount) > uspi->s_fpb) |
| 513 | return 0; |
| 514 | ucpi = ufs_load_cylinder (sb, cgno); |
| 515 | if (!ucpi) |
| 516 | return 0; |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 517 | ucg = ubh_get_ucg (UCPI_UBH(ucpi)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | if (!ufs_cg_chkmagic(sb, ucg)) { |
| 519 | ufs_panic (sb, "ufs_add_fragments", |
| 520 | "internal error, bad magic number on cg %u", cgno); |
| 521 | return 0; |
| 522 | } |
| 523 | |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 524 | fragno = ufs_dtogd(uspi, fragment); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | fragoff = ufs_fragnum (fragno); |
| 526 | for (i = oldcount; i < newcount; i++) |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 527 | if (ubh_isclr (UCPI_UBH(ucpi), ucpi->c_freeoff, fragno + i)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | return 0; |
| 529 | /* |
| 530 | * Block can be extended |
| 531 | */ |
| 532 | ucg->cg_time = cpu_to_fs32(sb, get_seconds()); |
| 533 | for (i = newcount; i < (uspi->s_fpb - fragoff); i++) |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 534 | if (ubh_isclr (UCPI_UBH(ucpi), ucpi->c_freeoff, fragno + i)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | break; |
| 536 | fragsize = i - oldcount; |
| 537 | if (!fs32_to_cpu(sb, ucg->cg_frsum[fragsize])) |
| 538 | ufs_panic (sb, "ufs_add_fragments", |
| 539 | "internal error or corrupted bitmap on cg %u", cgno); |
| 540 | fs32_sub(sb, &ucg->cg_frsum[fragsize], 1); |
| 541 | if (fragsize != count) |
| 542 | fs32_add(sb, &ucg->cg_frsum[fragsize - count], 1); |
| 543 | for (i = oldcount; i < newcount; i++) |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 544 | ubh_clrbit (UCPI_UBH(ucpi), ucpi->c_freeoff, fragno + i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | |
| 546 | fs32_sub(sb, &ucg->cg_cs.cs_nffree, count); |
| 547 | fs32_sub(sb, &UFS_SB(sb)->fs_cs(cgno).cs_nffree, count); |
Evgeniy Dushistov | ee3ffd6 | 2006-06-25 05:47:30 -0700 | [diff] [blame] | 548 | uspi->cs_total.cs_nffree -= count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 550 | ubh_mark_buffer_dirty (USPI_UBH(uspi)); |
| 551 | ubh_mark_buffer_dirty (UCPI_UBH(ucpi)); |
Christoph Hellwig | 9cb569d | 2010-08-11 17:06:24 +0200 | [diff] [blame] | 552 | if (sb->s_flags & MS_SYNCHRONOUS) |
| 553 | ubh_sync_block(UCPI_UBH(ucpi)); |
Artem Bityutskiy | 9e9ad5f | 2012-07-12 16:28:08 +0300 | [diff] [blame] | 554 | ufs_mark_sb_dirty(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 556 | UFSD("EXIT, fragment %llu\n", (unsigned long long)fragment); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | |
| 558 | return fragment; |
| 559 | } |
| 560 | |
| 561 | #define UFS_TEST_FREE_SPACE_CG \ |
| 562 | ucg = (struct ufs_cylinder_group *) UFS_SB(sb)->s_ucg[cgno]->b_data; \ |
| 563 | if (fs32_to_cpu(sb, ucg->cg_cs.cs_nbfree)) \ |
| 564 | goto cg_found; \ |
| 565 | for (k = count; k < uspi->s_fpb; k++) \ |
| 566 | if (fs32_to_cpu(sb, ucg->cg_frsum[k])) \ |
| 567 | goto cg_found; |
| 568 | |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 569 | static u64 ufs_alloc_fragments(struct inode *inode, unsigned cgno, |
| 570 | u64 goal, unsigned count, int *err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | { |
| 572 | struct super_block * sb; |
| 573 | struct ufs_sb_private_info * uspi; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | struct ufs_cg_private_info * ucpi; |
| 575 | struct ufs_cylinder_group * ucg; |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 576 | unsigned oldcg, i, j, k, allocsize; |
| 577 | u64 result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 579 | UFSD("ENTER, ino %lu, cgno %u, goal %llu, count %u\n", |
| 580 | inode->i_ino, cgno, (unsigned long long)goal, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | |
| 582 | sb = inode->i_sb; |
| 583 | uspi = UFS_SB(sb)->s_uspi; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | oldcg = cgno; |
| 585 | |
| 586 | /* |
| 587 | * 1. searching on preferred cylinder group |
| 588 | */ |
| 589 | UFS_TEST_FREE_SPACE_CG |
| 590 | |
| 591 | /* |
| 592 | * 2. quadratic rehash |
| 593 | */ |
| 594 | for (j = 1; j < uspi->s_ncg; j *= 2) { |
| 595 | cgno += j; |
| 596 | if (cgno >= uspi->s_ncg) |
| 597 | cgno -= uspi->s_ncg; |
| 598 | UFS_TEST_FREE_SPACE_CG |
| 599 | } |
| 600 | |
| 601 | /* |
| 602 | * 3. brute force search |
| 603 | * We start at i = 2 ( 0 is checked at 1.step, 1 at 2.step ) |
| 604 | */ |
| 605 | cgno = (oldcg + 1) % uspi->s_ncg; |
| 606 | for (j = 2; j < uspi->s_ncg; j++) { |
| 607 | cgno++; |
| 608 | if (cgno >= uspi->s_ncg) |
| 609 | cgno = 0; |
| 610 | UFS_TEST_FREE_SPACE_CG |
| 611 | } |
| 612 | |
Evgeniy Dushistov | abf5d15 | 2006-06-25 05:47:24 -0700 | [diff] [blame] | 613 | UFSD("EXIT (FAILED)\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | return 0; |
| 615 | |
| 616 | cg_found: |
| 617 | ucpi = ufs_load_cylinder (sb, cgno); |
| 618 | if (!ucpi) |
| 619 | return 0; |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 620 | ucg = ubh_get_ucg (UCPI_UBH(ucpi)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | if (!ufs_cg_chkmagic(sb, ucg)) |
| 622 | ufs_panic (sb, "ufs_alloc_fragments", |
| 623 | "internal error, bad magic number on cg %u", cgno); |
| 624 | ucg->cg_time = cpu_to_fs32(sb, get_seconds()); |
| 625 | |
| 626 | if (count == uspi->s_fpb) { |
| 627 | result = ufs_alloccg_block (inode, ucpi, goal, err); |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 628 | if (result == INVBLOCK) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | return 0; |
| 630 | goto succed; |
| 631 | } |
| 632 | |
| 633 | for (allocsize = count; allocsize < uspi->s_fpb; allocsize++) |
| 634 | if (fs32_to_cpu(sb, ucg->cg_frsum[allocsize]) != 0) |
| 635 | break; |
| 636 | |
| 637 | if (allocsize == uspi->s_fpb) { |
| 638 | result = ufs_alloccg_block (inode, ucpi, goal, err); |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 639 | if (result == INVBLOCK) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | return 0; |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 641 | goal = ufs_dtogd(uspi, result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | for (i = count; i < uspi->s_fpb; i++) |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 643 | ubh_setbit (UCPI_UBH(ucpi), ucpi->c_freeoff, goal + i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | i = uspi->s_fpb - count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | |
| 646 | fs32_add(sb, &ucg->cg_cs.cs_nffree, i); |
Evgeniy Dushistov | ee3ffd6 | 2006-06-25 05:47:30 -0700 | [diff] [blame] | 647 | uspi->cs_total.cs_nffree += i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | fs32_add(sb, &UFS_SB(sb)->fs_cs(cgno).cs_nffree, i); |
| 649 | fs32_add(sb, &ucg->cg_frsum[i], 1); |
| 650 | goto succed; |
| 651 | } |
| 652 | |
| 653 | result = ufs_bitmap_search (sb, ucpi, goal, allocsize); |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 654 | if (result == INVBLOCK) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | for (i = 0; i < count; i++) |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 657 | ubh_clrbit (UCPI_UBH(ucpi), ucpi->c_freeoff, result + i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | |
| 659 | fs32_sub(sb, &ucg->cg_cs.cs_nffree, count); |
Evgeniy Dushistov | ee3ffd6 | 2006-06-25 05:47:30 -0700 | [diff] [blame] | 660 | uspi->cs_total.cs_nffree -= count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | fs32_sub(sb, &UFS_SB(sb)->fs_cs(cgno).cs_nffree, count); |
| 662 | fs32_sub(sb, &ucg->cg_frsum[allocsize], 1); |
| 663 | |
| 664 | if (count != allocsize) |
| 665 | fs32_add(sb, &ucg->cg_frsum[allocsize - count], 1); |
| 666 | |
| 667 | succed: |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 668 | ubh_mark_buffer_dirty (USPI_UBH(uspi)); |
| 669 | ubh_mark_buffer_dirty (UCPI_UBH(ucpi)); |
Christoph Hellwig | 9cb569d | 2010-08-11 17:06:24 +0200 | [diff] [blame] | 670 | if (sb->s_flags & MS_SYNCHRONOUS) |
| 671 | ubh_sync_block(UCPI_UBH(ucpi)); |
Artem Bityutskiy | 9e9ad5f | 2012-07-12 16:28:08 +0300 | [diff] [blame] | 672 | ufs_mark_sb_dirty(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | |
| 674 | result += cgno * uspi->s_fpg; |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 675 | UFSD("EXIT3, result %llu\n", (unsigned long long)result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | return result; |
| 677 | } |
| 678 | |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 679 | static u64 ufs_alloccg_block(struct inode *inode, |
| 680 | struct ufs_cg_private_info *ucpi, |
| 681 | u64 goal, int *err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | { |
| 683 | struct super_block * sb; |
| 684 | struct ufs_sb_private_info * uspi; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | struct ufs_cylinder_group * ucg; |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 686 | u64 result, blkno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 688 | UFSD("ENTER, goal %llu\n", (unsigned long long)goal); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | |
| 690 | sb = inode->i_sb; |
| 691 | uspi = UFS_SB(sb)->s_uspi; |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 692 | ucg = ubh_get_ucg(UCPI_UBH(ucpi)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | |
| 694 | if (goal == 0) { |
| 695 | goal = ucpi->c_rotor; |
| 696 | goto norot; |
| 697 | } |
| 698 | goal = ufs_blknum (goal); |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 699 | goal = ufs_dtogd(uspi, goal); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | |
| 701 | /* |
| 702 | * If the requested block is available, use it. |
| 703 | */ |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 704 | if (ubh_isblockset(UCPI_UBH(ucpi), ucpi->c_freeoff, ufs_fragstoblks(goal))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | result = goal; |
| 706 | goto gotit; |
| 707 | } |
| 708 | |
| 709 | norot: |
| 710 | result = ufs_bitmap_search (sb, ucpi, goal, uspi->s_fpb); |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 711 | if (result == INVBLOCK) |
| 712 | return INVBLOCK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | ucpi->c_rotor = result; |
| 714 | gotit: |
| 715 | blkno = ufs_fragstoblks(result); |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 716 | ubh_clrblock (UCPI_UBH(ucpi), ucpi->c_freeoff, blkno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | if ((UFS_SB(sb)->s_flags & UFS_CG_MASK) == UFS_CG_44BSD) |
| 718 | ufs_clusteracct (sb, ucpi, blkno, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | |
| 720 | fs32_sub(sb, &ucg->cg_cs.cs_nbfree, 1); |
Evgeniy Dushistov | ee3ffd6 | 2006-06-25 05:47:30 -0700 | [diff] [blame] | 721 | uspi->cs_total.cs_nbfree--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | fs32_sub(sb, &UFS_SB(sb)->fs_cs(ucpi->c_cgx).cs_nbfree, 1); |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 723 | |
| 724 | if (uspi->fs_magic != UFS2_MAGIC) { |
| 725 | unsigned cylno = ufs_cbtocylno((unsigned)result); |
| 726 | |
| 727 | fs16_sub(sb, &ubh_cg_blks(ucpi, cylno, |
| 728 | ufs_cbtorpos((unsigned)result)), 1); |
| 729 | fs32_sub(sb, &ubh_cg_blktot(ucpi, cylno), 1); |
| 730 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 732 | UFSD("EXIT, result %llu\n", (unsigned long long)result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | |
| 734 | return result; |
| 735 | } |
| 736 | |
Evgeniy Dushistov | 3e41f59 | 2006-06-25 05:47:23 -0700 | [diff] [blame] | 737 | static unsigned ubh_scanc(struct ufs_sb_private_info *uspi, |
| 738 | struct ufs_buffer_head *ubh, |
| 739 | unsigned begin, unsigned size, |
| 740 | unsigned char *table, unsigned char mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | { |
Evgeniy Dushistov | 3e41f59 | 2006-06-25 05:47:23 -0700 | [diff] [blame] | 742 | unsigned rest, offset; |
| 743 | unsigned char *cp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | |
Evgeniy Dushistov | 3e41f59 | 2006-06-25 05:47:23 -0700 | [diff] [blame] | 746 | offset = begin & ~uspi->s_fmask; |
| 747 | begin >>= uspi->s_fshift; |
| 748 | for (;;) { |
| 749 | if ((offset + size) < uspi->s_fsize) |
| 750 | rest = size; |
| 751 | else |
| 752 | rest = uspi->s_fsize - offset; |
| 753 | size -= rest; |
| 754 | cp = ubh->bh[begin]->b_data + offset; |
| 755 | while ((table[*cp++] & mask) == 0 && --rest) |
| 756 | ; |
| 757 | if (rest || !size) |
| 758 | break; |
| 759 | begin++; |
| 760 | offset = 0; |
| 761 | } |
| 762 | return (size + rest); |
| 763 | } |
| 764 | |
| 765 | /* |
| 766 | * Find a block of the specified size in the specified cylinder group. |
| 767 | * @sp: pointer to super block |
| 768 | * @ucpi: pointer to cylinder group info |
| 769 | * @goal: near which block we want find new one |
| 770 | * @count: specified size |
| 771 | */ |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 772 | static u64 ufs_bitmap_search(struct super_block *sb, |
| 773 | struct ufs_cg_private_info *ucpi, |
| 774 | u64 goal, unsigned count) |
Evgeniy Dushistov | 3e41f59 | 2006-06-25 05:47:23 -0700 | [diff] [blame] | 775 | { |
| 776 | /* |
| 777 | * Bit patterns for identifying fragments in the block map |
| 778 | * used as ((map & mask_arr) == want_arr) |
| 779 | */ |
| 780 | static const int mask_arr[9] = { |
| 781 | 0x3, 0x7, 0xf, 0x1f, 0x3f, 0x7f, 0xff, 0x1ff, 0x3ff |
| 782 | }; |
| 783 | static const int want_arr[9] = { |
| 784 | 0x0, 0x2, 0x6, 0xe, 0x1e, 0x3e, 0x7e, 0xfe, 0x1fe |
| 785 | }; |
| 786 | struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi; |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 787 | unsigned start, length, loc; |
Evgeniy Dushistov | 3e41f59 | 2006-06-25 05:47:23 -0700 | [diff] [blame] | 788 | unsigned pos, want, blockmap, mask, end; |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 789 | u64 result; |
Evgeniy Dushistov | 3e41f59 | 2006-06-25 05:47:23 -0700 | [diff] [blame] | 790 | |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 791 | UFSD("ENTER, cg %u, goal %llu, count %u\n", ucpi->c_cgx, |
| 792 | (unsigned long long)goal, count); |
Evgeniy Dushistov | 3e41f59 | 2006-06-25 05:47:23 -0700 | [diff] [blame] | 793 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | if (goal) |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 795 | start = ufs_dtogd(uspi, goal) >> 3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | else |
| 797 | start = ucpi->c_frotor >> 3; |
| 798 | |
| 799 | length = ((uspi->s_fpg + 7) >> 3) - start; |
Evgeniy Dushistov | 3e41f59 | 2006-06-25 05:47:23 -0700 | [diff] [blame] | 800 | loc = ubh_scanc(uspi, UCPI_UBH(ucpi), ucpi->c_freeoff + start, length, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | (uspi->s_fpb == 8) ? ufs_fragtable_8fpb : ufs_fragtable_other, |
| 802 | 1 << (count - 1 + (uspi->s_fpb & 7))); |
Evgeniy Dushistov | 3e41f59 | 2006-06-25 05:47:23 -0700 | [diff] [blame] | 803 | if (loc == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | length = start + 1; |
Evgeniy Dushistov | 3e41f59 | 2006-06-25 05:47:23 -0700 | [diff] [blame] | 805 | loc = ubh_scanc(uspi, UCPI_UBH(ucpi), ucpi->c_freeoff, length, |
| 806 | (uspi->s_fpb == 8) ? ufs_fragtable_8fpb : |
| 807 | ufs_fragtable_other, |
| 808 | 1 << (count - 1 + (uspi->s_fpb & 7))); |
| 809 | if (loc == 0) { |
| 810 | ufs_error(sb, "ufs_bitmap_search", |
| 811 | "bitmap corrupted on cg %u, start %u," |
| 812 | " length %u, count %u, freeoff %u\n", |
| 813 | ucpi->c_cgx, start, length, count, |
| 814 | ucpi->c_freeoff); |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 815 | return INVBLOCK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | } |
| 817 | start = 0; |
| 818 | } |
Evgeniy Dushistov | 3e41f59 | 2006-06-25 05:47:23 -0700 | [diff] [blame] | 819 | result = (start + length - loc) << 3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | ucpi->c_frotor = result; |
| 821 | |
| 822 | /* |
| 823 | * found the byte in the map |
| 824 | */ |
Evgeniy Dushistov | 3e41f59 | 2006-06-25 05:47:23 -0700 | [diff] [blame] | 825 | |
| 826 | for (end = result + 8; result < end; result += uspi->s_fpb) { |
| 827 | blockmap = ubh_blkmap(UCPI_UBH(ucpi), ucpi->c_freeoff, result); |
| 828 | blockmap <<= 1; |
| 829 | mask = mask_arr[count]; |
| 830 | want = want_arr[count]; |
| 831 | for (pos = 0; pos <= uspi->s_fpb - count; pos++) { |
| 832 | if ((blockmap & mask) == want) { |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 833 | UFSD("EXIT, result %llu\n", |
| 834 | (unsigned long long)result); |
Evgeniy Dushistov | 3e41f59 | 2006-06-25 05:47:23 -0700 | [diff] [blame] | 835 | return result + pos; |
| 836 | } |
| 837 | mask <<= 1; |
| 838 | want <<= 1; |
| 839 | } |
| 840 | } |
| 841 | |
| 842 | ufs_error(sb, "ufs_bitmap_search", "block not in map on cg %u\n", |
| 843 | ucpi->c_cgx); |
Evgeniy Dushistov | abf5d15 | 2006-06-25 05:47:24 -0700 | [diff] [blame] | 844 | UFSD("EXIT (FAILED)\n"); |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 845 | return INVBLOCK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | static void ufs_clusteracct(struct super_block * sb, |
| 849 | struct ufs_cg_private_info * ucpi, unsigned blkno, int cnt) |
| 850 | { |
| 851 | struct ufs_sb_private_info * uspi; |
| 852 | int i, start, end, forw, back; |
| 853 | |
| 854 | uspi = UFS_SB(sb)->s_uspi; |
| 855 | if (uspi->s_contigsumsize <= 0) |
| 856 | return; |
| 857 | |
| 858 | if (cnt > 0) |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 859 | ubh_setbit(UCPI_UBH(ucpi), ucpi->c_clusteroff, blkno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | else |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 861 | ubh_clrbit(UCPI_UBH(ucpi), ucpi->c_clusteroff, blkno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | |
| 863 | /* |
| 864 | * Find the size of the cluster going forward. |
| 865 | */ |
| 866 | start = blkno + 1; |
| 867 | end = start + uspi->s_contigsumsize; |
| 868 | if ( end >= ucpi->c_nclusterblks) |
| 869 | end = ucpi->c_nclusterblks; |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 870 | i = ubh_find_next_zero_bit (UCPI_UBH(ucpi), ucpi->c_clusteroff, end, start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | if (i > end) |
| 872 | i = end; |
| 873 | forw = i - start; |
| 874 | |
| 875 | /* |
| 876 | * Find the size of the cluster going backward. |
| 877 | */ |
| 878 | start = blkno - 1; |
| 879 | end = start - uspi->s_contigsumsize; |
| 880 | if (end < 0 ) |
| 881 | end = -1; |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 882 | i = ubh_find_last_zero_bit (UCPI_UBH(ucpi), ucpi->c_clusteroff, start, end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | if ( i < end) |
| 884 | i = end; |
| 885 | back = start - i; |
| 886 | |
| 887 | /* |
| 888 | * Account for old cluster and the possibly new forward and |
| 889 | * back clusters. |
| 890 | */ |
| 891 | i = back + forw + 1; |
| 892 | if (i > uspi->s_contigsumsize) |
| 893 | i = uspi->s_contigsumsize; |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 894 | fs32_add(sb, (__fs32*)ubh_get_addr(UCPI_UBH(ucpi), ucpi->c_clustersumoff + (i << 2)), cnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | if (back > 0) |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 896 | fs32_sub(sb, (__fs32*)ubh_get_addr(UCPI_UBH(ucpi), ucpi->c_clustersumoff + (back << 2)), cnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | if (forw > 0) |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 898 | fs32_sub(sb, (__fs32*)ubh_get_addr(UCPI_UBH(ucpi), ucpi->c_clustersumoff + (forw << 2)), cnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | |
| 902 | static unsigned char ufs_fragtable_8fpb[] = { |
| 903 | 0x00, 0x01, 0x01, 0x02, 0x01, 0x01, 0x02, 0x04, 0x01, 0x01, 0x01, 0x03, 0x02, 0x03, 0x04, 0x08, |
| 904 | 0x01, 0x01, 0x01, 0x03, 0x01, 0x01, 0x03, 0x05, 0x02, 0x03, 0x03, 0x02, 0x04, 0x05, 0x08, 0x10, |
| 905 | 0x01, 0x01, 0x01, 0x03, 0x01, 0x01, 0x03, 0x05, 0x01, 0x01, 0x01, 0x03, 0x03, 0x03, 0x05, 0x09, |
| 906 | 0x02, 0x03, 0x03, 0x02, 0x03, 0x03, 0x02, 0x06, 0x04, 0x05, 0x05, 0x06, 0x08, 0x09, 0x10, 0x20, |
| 907 | 0x01, 0x01, 0x01, 0x03, 0x01, 0x01, 0x03, 0x05, 0x01, 0x01, 0x01, 0x03, 0x03, 0x03, 0x05, 0x09, |
| 908 | 0x01, 0x01, 0x01, 0x03, 0x01, 0x01, 0x03, 0x05, 0x03, 0x03, 0x03, 0x03, 0x05, 0x05, 0x09, 0x11, |
| 909 | 0x02, 0x03, 0x03, 0x02, 0x03, 0x03, 0x02, 0x06, 0x03, 0x03, 0x03, 0x03, 0x02, 0x03, 0x06, 0x0A, |
| 910 | 0x04, 0x05, 0x05, 0x06, 0x05, 0x05, 0x06, 0x04, 0x08, 0x09, 0x09, 0x0A, 0x10, 0x11, 0x20, 0x40, |
| 911 | 0x01, 0x01, 0x01, 0x03, 0x01, 0x01, 0x03, 0x05, 0x01, 0x01, 0x01, 0x03, 0x03, 0x03, 0x05, 0x09, |
| 912 | 0x01, 0x01, 0x01, 0x03, 0x01, 0x01, 0x03, 0x05, 0x03, 0x03, 0x03, 0x03, 0x05, 0x05, 0x09, 0x11, |
| 913 | 0x01, 0x01, 0x01, 0x03, 0x01, 0x01, 0x03, 0x05, 0x01, 0x01, 0x01, 0x03, 0x03, 0x03, 0x05, 0x09, |
| 914 | 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x07, 0x05, 0x05, 0x05, 0x07, 0x09, 0x09, 0x11, 0x21, |
| 915 | 0x02, 0x03, 0x03, 0x02, 0x03, 0x03, 0x02, 0x06, 0x03, 0x03, 0x03, 0x03, 0x02, 0x03, 0x06, 0x0A, |
| 916 | 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x07, 0x02, 0x03, 0x03, 0x02, 0x06, 0x07, 0x0A, 0x12, |
| 917 | 0x04, 0x05, 0x05, 0x06, 0x05, 0x05, 0x06, 0x04, 0x05, 0x05, 0x05, 0x07, 0x06, 0x07, 0x04, 0x0C, |
| 918 | 0x08, 0x09, 0x09, 0x0A, 0x09, 0x09, 0x0A, 0x0C, 0x10, 0x11, 0x11, 0x12, 0x20, 0x21, 0x40, 0x80, |
| 919 | }; |
| 920 | |
| 921 | static unsigned char ufs_fragtable_other[] = { |
| 922 | 0x00, 0x16, 0x16, 0x2A, 0x16, 0x16, 0x26, 0x4E, 0x16, 0x16, 0x16, 0x3E, 0x2A, 0x3E, 0x4E, 0x8A, |
| 923 | 0x16, 0x16, 0x16, 0x3E, 0x16, 0x16, 0x36, 0x5E, 0x16, 0x16, 0x16, 0x3E, 0x3E, 0x3E, 0x5E, 0x9E, |
| 924 | 0x16, 0x16, 0x16, 0x3E, 0x16, 0x16, 0x36, 0x5E, 0x16, 0x16, 0x16, 0x3E, 0x3E, 0x3E, 0x5E, 0x9E, |
| 925 | 0x2A, 0x3E, 0x3E, 0x2A, 0x3E, 0x3E, 0x2E, 0x6E, 0x3E, 0x3E, 0x3E, 0x3E, 0x2A, 0x3E, 0x6E, 0xAA, |
| 926 | 0x16, 0x16, 0x16, 0x3E, 0x16, 0x16, 0x36, 0x5E, 0x16, 0x16, 0x16, 0x3E, 0x3E, 0x3E, 0x5E, 0x9E, |
| 927 | 0x16, 0x16, 0x16, 0x3E, 0x16, 0x16, 0x36, 0x5E, 0x16, 0x16, 0x16, 0x3E, 0x3E, 0x3E, 0x5E, 0x9E, |
| 928 | 0x26, 0x36, 0x36, 0x2E, 0x36, 0x36, 0x26, 0x6E, 0x36, 0x36, 0x36, 0x3E, 0x2E, 0x3E, 0x6E, 0xAE, |
| 929 | 0x4E, 0x5E, 0x5E, 0x6E, 0x5E, 0x5E, 0x6E, 0x4E, 0x5E, 0x5E, 0x5E, 0x7E, 0x6E, 0x7E, 0x4E, 0xCE, |
| 930 | 0x16, 0x16, 0x16, 0x3E, 0x16, 0x16, 0x36, 0x5E, 0x16, 0x16, 0x16, 0x3E, 0x3E, 0x3E, 0x5E, 0x9E, |
| 931 | 0x16, 0x16, 0x16, 0x3E, 0x16, 0x16, 0x36, 0x5E, 0x16, 0x16, 0x16, 0x3E, 0x3E, 0x3E, 0x5E, 0x9E, |
| 932 | 0x16, 0x16, 0x16, 0x3E, 0x16, 0x16, 0x36, 0x5E, 0x16, 0x16, 0x16, 0x3E, 0x3E, 0x3E, 0x5E, 0x9E, |
| 933 | 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x7E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x7E, 0xBE, |
| 934 | 0x2A, 0x3E, 0x3E, 0x2A, 0x3E, 0x3E, 0x2E, 0x6E, 0x3E, 0x3E, 0x3E, 0x3E, 0x2A, 0x3E, 0x6E, 0xAA, |
| 935 | 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x7E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x7E, 0xBE, |
| 936 | 0x4E, 0x5E, 0x5E, 0x6E, 0x5E, 0x5E, 0x6E, 0x4E, 0x5E, 0x5E, 0x5E, 0x7E, 0x6E, 0x7E, 0x4E, 0xCE, |
| 937 | 0x8A, 0x9E, 0x9E, 0xAA, 0x9E, 0x9E, 0xAE, 0xCE, 0x9E, 0x9E, 0x9E, 0xBE, 0xAA, 0xBE, 0xCE, 0x8A, |
| 938 | }; |