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