Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * alloc.c - NILFS dat/inode allocator |
| 3 | * |
| 4 | * Copyright (C) 2006-2008 Nippon Telegraph and Telephone Corporation. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | * |
| 20 | * Original code was written by Koji Sato <koji@osrg.net>. |
| 21 | * Two allocators were unified by Ryusuke Konishi <ryusuke@osrg.net>, |
| 22 | * Amagai Yoshiji <amagai@osrg.net>. |
| 23 | */ |
| 24 | |
| 25 | #include <linux/types.h> |
| 26 | #include <linux/buffer_head.h> |
| 27 | #include <linux/fs.h> |
| 28 | #include <linux/bitops.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 29 | #include <linux/slab.h> |
Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 30 | #include "mdt.h" |
| 31 | #include "alloc.h" |
| 32 | |
| 33 | |
Ryusuke Konishi | db55d92 | 2010-04-12 01:46:02 +0900 | [diff] [blame] | 34 | /** |
| 35 | * nilfs_palloc_groups_per_desc_block - get the number of groups that a group |
| 36 | * descriptor block can maintain |
| 37 | * @inode: inode of metadata file using this allocator |
| 38 | */ |
Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 39 | static inline unsigned long |
| 40 | nilfs_palloc_groups_per_desc_block(const struct inode *inode) |
| 41 | { |
| 42 | return (1UL << inode->i_blkbits) / |
| 43 | sizeof(struct nilfs_palloc_group_desc); |
| 44 | } |
| 45 | |
Ryusuke Konishi | db55d92 | 2010-04-12 01:46:02 +0900 | [diff] [blame] | 46 | /** |
| 47 | * nilfs_palloc_groups_count - get maximum number of groups |
| 48 | * @inode: inode of metadata file using this allocator |
| 49 | */ |
Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 50 | static inline unsigned long |
| 51 | nilfs_palloc_groups_count(const struct inode *inode) |
| 52 | { |
| 53 | return 1UL << (BITS_PER_LONG - (inode->i_blkbits + 3 /* log2(8) */)); |
| 54 | } |
| 55 | |
Ryusuke Konishi | db55d92 | 2010-04-12 01:46:02 +0900 | [diff] [blame] | 56 | /** |
| 57 | * nilfs_palloc_init_blockgroup - initialize private variables for allocator |
| 58 | * @inode: inode of metadata file using this allocator |
| 59 | * @entry_size: size of the persistent object |
| 60 | */ |
Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 61 | int nilfs_palloc_init_blockgroup(struct inode *inode, unsigned entry_size) |
| 62 | { |
| 63 | struct nilfs_mdt_info *mi = NILFS_MDT(inode); |
| 64 | |
| 65 | mi->mi_bgl = kmalloc(sizeof(*mi->mi_bgl), GFP_NOFS); |
| 66 | if (!mi->mi_bgl) |
| 67 | return -ENOMEM; |
| 68 | |
| 69 | bgl_lock_init(mi->mi_bgl); |
| 70 | |
| 71 | nilfs_mdt_set_entry_size(inode, entry_size, 0); |
| 72 | |
| 73 | mi->mi_blocks_per_group = |
| 74 | DIV_ROUND_UP(nilfs_palloc_entries_per_group(inode), |
| 75 | mi->mi_entries_per_block) + 1; |
| 76 | /* Number of blocks in a group including entry blocks and |
| 77 | a bitmap block */ |
| 78 | mi->mi_blocks_per_desc_block = |
| 79 | nilfs_palloc_groups_per_desc_block(inode) * |
| 80 | mi->mi_blocks_per_group + 1; |
| 81 | /* Number of blocks per descriptor including the |
| 82 | descriptor block */ |
| 83 | return 0; |
| 84 | } |
| 85 | |
Ryusuke Konishi | db55d92 | 2010-04-12 01:46:02 +0900 | [diff] [blame] | 86 | /** |
| 87 | * nilfs_palloc_group - get group number and offset from an entry number |
| 88 | * @inode: inode of metadata file using this allocator |
| 89 | * @nr: serial number of the entry (e.g. inode number) |
| 90 | * @offset: pointer to store offset number in the group |
| 91 | */ |
Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 92 | static unsigned long nilfs_palloc_group(const struct inode *inode, __u64 nr, |
| 93 | unsigned long *offset) |
| 94 | { |
| 95 | __u64 group = nr; |
| 96 | |
| 97 | *offset = do_div(group, nilfs_palloc_entries_per_group(inode)); |
| 98 | return group; |
| 99 | } |
| 100 | |
Ryusuke Konishi | db55d92 | 2010-04-12 01:46:02 +0900 | [diff] [blame] | 101 | /** |
| 102 | * nilfs_palloc_desc_blkoff - get block offset of a group descriptor block |
| 103 | * @inode: inode of metadata file using this allocator |
| 104 | * @group: group number |
| 105 | * |
| 106 | * nilfs_palloc_desc_blkoff() returns block offset of the descriptor |
| 107 | * block which contains a descriptor of the specified group. |
| 108 | */ |
Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 109 | static unsigned long |
| 110 | nilfs_palloc_desc_blkoff(const struct inode *inode, unsigned long group) |
| 111 | { |
| 112 | unsigned long desc_block = |
| 113 | group / nilfs_palloc_groups_per_desc_block(inode); |
| 114 | return desc_block * NILFS_MDT(inode)->mi_blocks_per_desc_block; |
| 115 | } |
| 116 | |
Ryusuke Konishi | db55d92 | 2010-04-12 01:46:02 +0900 | [diff] [blame] | 117 | /** |
| 118 | * nilfs_palloc_bitmap_blkoff - get block offset of a bitmap block |
| 119 | * @inode: inode of metadata file using this allocator |
| 120 | * @group: group number |
| 121 | * |
| 122 | * nilfs_palloc_bitmap_blkoff() returns block offset of the bitmap |
| 123 | * block used to allocate/deallocate entries in the specified group. |
| 124 | */ |
Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 125 | static unsigned long |
| 126 | nilfs_palloc_bitmap_blkoff(const struct inode *inode, unsigned long group) |
| 127 | { |
| 128 | unsigned long desc_offset = |
| 129 | group % nilfs_palloc_groups_per_desc_block(inode); |
| 130 | return nilfs_palloc_desc_blkoff(inode, group) + 1 + |
| 131 | desc_offset * NILFS_MDT(inode)->mi_blocks_per_group; |
| 132 | } |
| 133 | |
Ryusuke Konishi | db55d92 | 2010-04-12 01:46:02 +0900 | [diff] [blame] | 134 | /** |
| 135 | * nilfs_palloc_group_desc_nfrees - get the number of free entries in a group |
Ryusuke Konishi | db55d92 | 2010-04-12 01:46:02 +0900 | [diff] [blame] | 136 | * @desc: pointer to descriptor structure for the group |
Ryusuke Konishi | 4e9e63a | 2015-11-06 16:31:45 -0800 | [diff] [blame] | 137 | * @lock: spin lock protecting @desc |
Ryusuke Konishi | db55d92 | 2010-04-12 01:46:02 +0900 | [diff] [blame] | 138 | */ |
Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 139 | static unsigned long |
Ryusuke Konishi | 4e9e63a | 2015-11-06 16:31:45 -0800 | [diff] [blame] | 140 | nilfs_palloc_group_desc_nfrees(const struct nilfs_palloc_group_desc *desc, |
| 141 | spinlock_t *lock) |
Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 142 | { |
| 143 | unsigned long nfree; |
| 144 | |
Ryusuke Konishi | 4e9e63a | 2015-11-06 16:31:45 -0800 | [diff] [blame] | 145 | spin_lock(lock); |
Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 146 | nfree = le32_to_cpu(desc->pg_nfrees); |
Ryusuke Konishi | 4e9e63a | 2015-11-06 16:31:45 -0800 | [diff] [blame] | 147 | spin_unlock(lock); |
Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 148 | return nfree; |
| 149 | } |
| 150 | |
Ryusuke Konishi | db55d92 | 2010-04-12 01:46:02 +0900 | [diff] [blame] | 151 | /** |
| 152 | * nilfs_palloc_group_desc_add_entries - adjust count of free entries |
Ryusuke Konishi | db55d92 | 2010-04-12 01:46:02 +0900 | [diff] [blame] | 153 | * @desc: pointer to descriptor structure for the group |
Ryusuke Konishi | 4e9e63a | 2015-11-06 16:31:45 -0800 | [diff] [blame] | 154 | * @lock: spin lock protecting @desc |
Ryusuke Konishi | db55d92 | 2010-04-12 01:46:02 +0900 | [diff] [blame] | 155 | * @n: delta to be added |
| 156 | */ |
Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 157 | static void |
Ryusuke Konishi | 4e9e63a | 2015-11-06 16:31:45 -0800 | [diff] [blame] | 158 | nilfs_palloc_group_desc_add_entries(struct nilfs_palloc_group_desc *desc, |
| 159 | spinlock_t *lock, u32 n) |
Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 160 | { |
Ryusuke Konishi | 4e9e63a | 2015-11-06 16:31:45 -0800 | [diff] [blame] | 161 | spin_lock(lock); |
Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 162 | le32_add_cpu(&desc->pg_nfrees, n); |
Ryusuke Konishi | 4e9e63a | 2015-11-06 16:31:45 -0800 | [diff] [blame] | 163 | spin_unlock(lock); |
Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Ryusuke Konishi | db55d92 | 2010-04-12 01:46:02 +0900 | [diff] [blame] | 166 | /** |
| 167 | * nilfs_palloc_entry_blkoff - get block offset of an entry block |
| 168 | * @inode: inode of metadata file using this allocator |
| 169 | * @nr: serial number of the entry (e.g. inode number) |
| 170 | */ |
Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 171 | static unsigned long |
| 172 | nilfs_palloc_entry_blkoff(const struct inode *inode, __u64 nr) |
| 173 | { |
| 174 | unsigned long group, group_offset; |
| 175 | |
| 176 | group = nilfs_palloc_group(inode, nr, &group_offset); |
| 177 | |
| 178 | return nilfs_palloc_bitmap_blkoff(inode, group) + 1 + |
| 179 | group_offset / NILFS_MDT(inode)->mi_entries_per_block; |
| 180 | } |
| 181 | |
Ryusuke Konishi | db55d92 | 2010-04-12 01:46:02 +0900 | [diff] [blame] | 182 | /** |
| 183 | * nilfs_palloc_desc_block_init - initialize buffer of a group descriptor block |
| 184 | * @inode: inode of metadata file |
| 185 | * @bh: buffer head of the buffer to be initialized |
| 186 | * @kaddr: kernel address mapped for the page including the buffer |
| 187 | */ |
Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 188 | static void nilfs_palloc_desc_block_init(struct inode *inode, |
| 189 | struct buffer_head *bh, void *kaddr) |
| 190 | { |
| 191 | struct nilfs_palloc_group_desc *desc = kaddr + bh_offset(bh); |
| 192 | unsigned long n = nilfs_palloc_groups_per_desc_block(inode); |
| 193 | __le32 nfrees; |
| 194 | |
| 195 | nfrees = cpu_to_le32(nilfs_palloc_entries_per_group(inode)); |
| 196 | while (n-- > 0) { |
| 197 | desc->pg_nfrees = nfrees; |
| 198 | desc++; |
| 199 | } |
| 200 | } |
| 201 | |
Ryusuke Konishi | 70622a2 | 2009-11-14 18:40:27 +0900 | [diff] [blame] | 202 | static int nilfs_palloc_get_block(struct inode *inode, unsigned long blkoff, |
| 203 | int create, |
| 204 | void (*init_block)(struct inode *, |
| 205 | struct buffer_head *, |
| 206 | void *), |
| 207 | struct buffer_head **bhp, |
| 208 | struct nilfs_bh_assoc *prev, |
| 209 | spinlock_t *lock) |
| 210 | { |
| 211 | int ret; |
| 212 | |
| 213 | spin_lock(lock); |
| 214 | if (prev->bh && blkoff == prev->blkoff) { |
| 215 | get_bh(prev->bh); |
| 216 | *bhp = prev->bh; |
| 217 | spin_unlock(lock); |
| 218 | return 0; |
| 219 | } |
| 220 | spin_unlock(lock); |
| 221 | |
| 222 | ret = nilfs_mdt_get_block(inode, blkoff, create, init_block, bhp); |
| 223 | if (!ret) { |
| 224 | spin_lock(lock); |
| 225 | /* |
| 226 | * The following code must be safe for change of the |
| 227 | * cache contents during the get block call. |
| 228 | */ |
| 229 | brelse(prev->bh); |
| 230 | get_bh(*bhp); |
| 231 | prev->bh = *bhp; |
| 232 | prev->blkoff = blkoff; |
| 233 | spin_unlock(lock); |
| 234 | } |
| 235 | return ret; |
| 236 | } |
| 237 | |
Ryusuke Konishi | db55d92 | 2010-04-12 01:46:02 +0900 | [diff] [blame] | 238 | /** |
| 239 | * nilfs_palloc_get_desc_block - get buffer head of a group descriptor block |
| 240 | * @inode: inode of metadata file using this allocator |
| 241 | * @group: group number |
| 242 | * @create: create flag |
| 243 | * @bhp: pointer to store the resultant buffer head |
| 244 | */ |
Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 245 | static int nilfs_palloc_get_desc_block(struct inode *inode, |
| 246 | unsigned long group, |
| 247 | int create, struct buffer_head **bhp) |
| 248 | { |
Ryusuke Konishi | 70622a2 | 2009-11-14 18:40:27 +0900 | [diff] [blame] | 249 | struct nilfs_palloc_cache *cache = NILFS_MDT(inode)->mi_palloc_cache; |
| 250 | |
| 251 | return nilfs_palloc_get_block(inode, |
| 252 | nilfs_palloc_desc_blkoff(inode, group), |
| 253 | create, nilfs_palloc_desc_block_init, |
| 254 | bhp, &cache->prev_desc, &cache->lock); |
Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 255 | } |
| 256 | |
Ryusuke Konishi | db55d92 | 2010-04-12 01:46:02 +0900 | [diff] [blame] | 257 | /** |
| 258 | * nilfs_palloc_get_bitmap_block - get buffer head of a bitmap block |
| 259 | * @inode: inode of metadata file using this allocator |
| 260 | * @group: group number |
| 261 | * @create: create flag |
| 262 | * @bhp: pointer to store the resultant buffer head |
| 263 | */ |
Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 264 | static int nilfs_palloc_get_bitmap_block(struct inode *inode, |
| 265 | unsigned long group, |
| 266 | int create, struct buffer_head **bhp) |
| 267 | { |
Ryusuke Konishi | 70622a2 | 2009-11-14 18:40:27 +0900 | [diff] [blame] | 268 | struct nilfs_palloc_cache *cache = NILFS_MDT(inode)->mi_palloc_cache; |
| 269 | |
| 270 | return nilfs_palloc_get_block(inode, |
| 271 | nilfs_palloc_bitmap_blkoff(inode, group), |
| 272 | create, NULL, bhp, |
| 273 | &cache->prev_bitmap, &cache->lock); |
Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Ryusuke Konishi | db55d92 | 2010-04-12 01:46:02 +0900 | [diff] [blame] | 276 | /** |
| 277 | * nilfs_palloc_get_entry_block - get buffer head of an entry block |
| 278 | * @inode: inode of metadata file using this allocator |
| 279 | * @nr: serial number of the entry (e.g. inode number) |
| 280 | * @create: create flag |
| 281 | * @bhp: pointer to store the resultant buffer head |
| 282 | */ |
Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 283 | int nilfs_palloc_get_entry_block(struct inode *inode, __u64 nr, |
| 284 | int create, struct buffer_head **bhp) |
| 285 | { |
Ryusuke Konishi | 70622a2 | 2009-11-14 18:40:27 +0900 | [diff] [blame] | 286 | struct nilfs_palloc_cache *cache = NILFS_MDT(inode)->mi_palloc_cache; |
| 287 | |
| 288 | return nilfs_palloc_get_block(inode, |
| 289 | nilfs_palloc_entry_blkoff(inode, nr), |
| 290 | create, NULL, bhp, |
| 291 | &cache->prev_entry, &cache->lock); |
Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 292 | } |
| 293 | |
Ryusuke Konishi | db55d92 | 2010-04-12 01:46:02 +0900 | [diff] [blame] | 294 | /** |
| 295 | * nilfs_palloc_block_get_group_desc - get kernel address of a group descriptor |
| 296 | * @inode: inode of metadata file using this allocator |
| 297 | * @group: group number |
| 298 | * @bh: buffer head of the buffer storing the group descriptor block |
| 299 | * @kaddr: kernel address mapped for the page including the buffer |
| 300 | */ |
Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 301 | static struct nilfs_palloc_group_desc * |
| 302 | nilfs_palloc_block_get_group_desc(const struct inode *inode, |
| 303 | unsigned long group, |
| 304 | const struct buffer_head *bh, void *kaddr) |
| 305 | { |
| 306 | return (struct nilfs_palloc_group_desc *)(kaddr + bh_offset(bh)) + |
| 307 | group % nilfs_palloc_groups_per_desc_block(inode); |
| 308 | } |
| 309 | |
Ryusuke Konishi | db55d92 | 2010-04-12 01:46:02 +0900 | [diff] [blame] | 310 | /** |
| 311 | * nilfs_palloc_block_get_entry - get kernel address of an entry |
| 312 | * @inode: inode of metadata file using this allocator |
| 313 | * @nr: serial number of the entry (e.g. inode number) |
| 314 | * @bh: buffer head of the buffer storing the entry block |
| 315 | * @kaddr: kernel address mapped for the page including the buffer |
| 316 | */ |
Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 317 | void *nilfs_palloc_block_get_entry(const struct inode *inode, __u64 nr, |
| 318 | const struct buffer_head *bh, void *kaddr) |
| 319 | { |
| 320 | unsigned long entry_offset, group_offset; |
| 321 | |
| 322 | nilfs_palloc_group(inode, nr, &group_offset); |
| 323 | entry_offset = group_offset % NILFS_MDT(inode)->mi_entries_per_block; |
| 324 | |
| 325 | return kaddr + bh_offset(bh) + |
| 326 | entry_offset * NILFS_MDT(inode)->mi_entry_size; |
| 327 | } |
| 328 | |
Ryusuke Konishi | db55d92 | 2010-04-12 01:46:02 +0900 | [diff] [blame] | 329 | /** |
| 330 | * nilfs_palloc_find_available_slot - find available slot in a group |
Ryusuke Konishi | db55d92 | 2010-04-12 01:46:02 +0900 | [diff] [blame] | 331 | * @bitmap: bitmap of the group |
Ryusuke Konishi | 4e9e63a | 2015-11-06 16:31:45 -0800 | [diff] [blame] | 332 | * @target: offset number of an entry in the group (start point) |
Ryusuke Konishi | db55d92 | 2010-04-12 01:46:02 +0900 | [diff] [blame] | 333 | * @bsize: size in bits |
Ryusuke Konishi | 4e9e63a | 2015-11-06 16:31:45 -0800 | [diff] [blame] | 334 | * @lock: spin lock protecting @bitmap |
Ryusuke Konishi | db55d92 | 2010-04-12 01:46:02 +0900 | [diff] [blame] | 335 | */ |
Ryusuke Konishi | 4e9e63a | 2015-11-06 16:31:45 -0800 | [diff] [blame] | 336 | static int nilfs_palloc_find_available_slot(unsigned char *bitmap, |
Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 337 | unsigned long target, |
Ryusuke Konishi | 18c41b3 | 2015-11-06 16:31:48 -0800 | [diff] [blame^] | 338 | unsigned bsize, |
Ryusuke Konishi | 4e9e63a | 2015-11-06 16:31:45 -0800 | [diff] [blame] | 339 | spinlock_t *lock) |
Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 340 | { |
Ryusuke Konishi | 18c41b3 | 2015-11-06 16:31:48 -0800 | [diff] [blame^] | 341 | int pos, end = bsize; |
Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 342 | |
Ryusuke Konishi | 18c41b3 | 2015-11-06 16:31:48 -0800 | [diff] [blame^] | 343 | if (likely(target < bsize)) { |
| 344 | pos = target; |
| 345 | do { |
| 346 | pos = nilfs_find_next_zero_bit(bitmap, end, pos); |
| 347 | if (pos >= end) |
| 348 | break; |
| 349 | if (!nilfs_set_bit_atomic(lock, pos, bitmap)) |
Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 350 | return pos; |
Ryusuke Konishi | 18c41b3 | 2015-11-06 16:31:48 -0800 | [diff] [blame^] | 351 | } while (++pos < end); |
| 352 | |
| 353 | end = target; |
Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 354 | } |
Ryusuke Konishi | 18c41b3 | 2015-11-06 16:31:48 -0800 | [diff] [blame^] | 355 | |
| 356 | /* wrap around */ |
| 357 | for (pos = 0; pos < end; pos++) { |
| 358 | pos = nilfs_find_next_zero_bit(bitmap, end, pos); |
| 359 | if (pos >= end) |
| 360 | break; |
| 361 | if (!nilfs_set_bit_atomic(lock, pos, bitmap)) |
| 362 | return pos; |
| 363 | } |
| 364 | |
Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 365 | return -ENOSPC; |
| 366 | } |
| 367 | |
Ryusuke Konishi | db55d92 | 2010-04-12 01:46:02 +0900 | [diff] [blame] | 368 | /** |
| 369 | * nilfs_palloc_rest_groups_in_desc_block - get the remaining number of groups |
| 370 | * in a group descriptor block |
| 371 | * @inode: inode of metadata file using this allocator |
| 372 | * @curr: current group number |
| 373 | * @max: maximum number of groups |
| 374 | */ |
Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 375 | static unsigned long |
| 376 | nilfs_palloc_rest_groups_in_desc_block(const struct inode *inode, |
| 377 | unsigned long curr, unsigned long max) |
| 378 | { |
| 379 | return min_t(unsigned long, |
| 380 | nilfs_palloc_groups_per_desc_block(inode) - |
| 381 | curr % nilfs_palloc_groups_per_desc_block(inode), |
| 382 | max - curr + 1); |
| 383 | } |
| 384 | |
Ryusuke Konishi | db55d92 | 2010-04-12 01:46:02 +0900 | [diff] [blame] | 385 | /** |
Vyacheslav Dubeyko | c7ef972 | 2013-07-03 15:08:05 -0700 | [diff] [blame] | 386 | * nilfs_palloc_count_desc_blocks - count descriptor blocks number |
| 387 | * @inode: inode of metadata file using this allocator |
| 388 | * @desc_blocks: descriptor blocks number [out] |
| 389 | */ |
| 390 | static int nilfs_palloc_count_desc_blocks(struct inode *inode, |
| 391 | unsigned long *desc_blocks) |
| 392 | { |
Ryusuke Konishi | 3568a13 | 2015-04-16 12:46:34 -0700 | [diff] [blame] | 393 | __u64 blknum; |
Vyacheslav Dubeyko | c7ef972 | 2013-07-03 15:08:05 -0700 | [diff] [blame] | 394 | int ret; |
| 395 | |
| 396 | ret = nilfs_bmap_last_key(NILFS_I(inode)->i_bmap, &blknum); |
| 397 | if (likely(!ret)) |
| 398 | *desc_blocks = DIV_ROUND_UP( |
Ryusuke Konishi | 3568a13 | 2015-04-16 12:46:34 -0700 | [diff] [blame] | 399 | (unsigned long)blknum, |
| 400 | NILFS_MDT(inode)->mi_blocks_per_desc_block); |
Vyacheslav Dubeyko | c7ef972 | 2013-07-03 15:08:05 -0700 | [diff] [blame] | 401 | return ret; |
| 402 | } |
| 403 | |
| 404 | /** |
| 405 | * nilfs_palloc_mdt_file_can_grow - check potential opportunity for |
| 406 | * MDT file growing |
| 407 | * @inode: inode of metadata file using this allocator |
| 408 | * @desc_blocks: known current descriptor blocks count |
| 409 | */ |
| 410 | static inline bool nilfs_palloc_mdt_file_can_grow(struct inode *inode, |
| 411 | unsigned long desc_blocks) |
| 412 | { |
| 413 | return (nilfs_palloc_groups_per_desc_block(inode) * desc_blocks) < |
| 414 | nilfs_palloc_groups_count(inode); |
| 415 | } |
| 416 | |
| 417 | /** |
| 418 | * nilfs_palloc_count_max_entries - count max number of entries that can be |
| 419 | * described by descriptor blocks count |
| 420 | * @inode: inode of metadata file using this allocator |
| 421 | * @nused: current number of used entries |
| 422 | * @nmaxp: max number of entries [out] |
| 423 | */ |
| 424 | int nilfs_palloc_count_max_entries(struct inode *inode, u64 nused, u64 *nmaxp) |
| 425 | { |
| 426 | unsigned long desc_blocks = 0; |
| 427 | u64 entries_per_desc_block, nmax; |
| 428 | int err; |
| 429 | |
| 430 | err = nilfs_palloc_count_desc_blocks(inode, &desc_blocks); |
| 431 | if (unlikely(err)) |
| 432 | return err; |
| 433 | |
| 434 | entries_per_desc_block = (u64)nilfs_palloc_entries_per_group(inode) * |
| 435 | nilfs_palloc_groups_per_desc_block(inode); |
| 436 | nmax = entries_per_desc_block * desc_blocks; |
| 437 | |
| 438 | if (nused == nmax && |
| 439 | nilfs_palloc_mdt_file_can_grow(inode, desc_blocks)) |
| 440 | nmax += entries_per_desc_block; |
| 441 | |
| 442 | if (nused > nmax) |
| 443 | return -ERANGE; |
| 444 | |
| 445 | *nmaxp = nmax; |
| 446 | return 0; |
| 447 | } |
| 448 | |
| 449 | /** |
Ryusuke Konishi | db55d92 | 2010-04-12 01:46:02 +0900 | [diff] [blame] | 450 | * nilfs_palloc_prepare_alloc_entry - prepare to allocate a persistent object |
| 451 | * @inode: inode of metadata file using this allocator |
| 452 | * @req: nilfs_palloc_req structure exchanged for the allocation |
| 453 | */ |
Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 454 | int nilfs_palloc_prepare_alloc_entry(struct inode *inode, |
| 455 | struct nilfs_palloc_req *req) |
| 456 | { |
| 457 | struct buffer_head *desc_bh, *bitmap_bh; |
| 458 | struct nilfs_palloc_group_desc *desc; |
| 459 | unsigned char *bitmap; |
| 460 | void *desc_kaddr, *bitmap_kaddr; |
| 461 | unsigned long group, maxgroup, ngroups; |
| 462 | unsigned long group_offset, maxgroup_offset; |
| 463 | unsigned long n, entries_per_group, groups_per_desc_block; |
| 464 | unsigned long i, j; |
Ryusuke Konishi | 4e9e63a | 2015-11-06 16:31:45 -0800 | [diff] [blame] | 465 | spinlock_t *lock; |
Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 466 | int pos, ret; |
| 467 | |
| 468 | ngroups = nilfs_palloc_groups_count(inode); |
| 469 | maxgroup = ngroups - 1; |
| 470 | group = nilfs_palloc_group(inode, req->pr_entry_nr, &group_offset); |
| 471 | entries_per_group = nilfs_palloc_entries_per_group(inode); |
| 472 | groups_per_desc_block = nilfs_palloc_groups_per_desc_block(inode); |
| 473 | |
| 474 | for (i = 0; i < ngroups; i += n) { |
| 475 | if (group >= ngroups) { |
| 476 | /* wrap around */ |
| 477 | group = 0; |
| 478 | maxgroup = nilfs_palloc_group(inode, req->pr_entry_nr, |
| 479 | &maxgroup_offset) - 1; |
| 480 | } |
| 481 | ret = nilfs_palloc_get_desc_block(inode, group, 1, &desc_bh); |
| 482 | if (ret < 0) |
| 483 | return ret; |
| 484 | desc_kaddr = kmap(desc_bh->b_page); |
| 485 | desc = nilfs_palloc_block_get_group_desc( |
| 486 | inode, group, desc_bh, desc_kaddr); |
| 487 | n = nilfs_palloc_rest_groups_in_desc_block(inode, group, |
| 488 | maxgroup); |
| 489 | for (j = 0; j < n; j++, desc++, group++) { |
Ryusuke Konishi | 4e9e63a | 2015-11-06 16:31:45 -0800 | [diff] [blame] | 490 | lock = nilfs_mdt_bgl_lock(inode, group); |
| 491 | if (nilfs_palloc_group_desc_nfrees(desc, lock) > 0) { |
Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 492 | ret = nilfs_palloc_get_bitmap_block( |
| 493 | inode, group, 1, &bitmap_bh); |
| 494 | if (ret < 0) |
| 495 | goto out_desc; |
| 496 | bitmap_kaddr = kmap(bitmap_bh->b_page); |
Ryusuke Konishi | 141bbdb | 2009-11-14 13:48:06 +0900 | [diff] [blame] | 497 | bitmap = bitmap_kaddr + bh_offset(bitmap_bh); |
Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 498 | pos = nilfs_palloc_find_available_slot( |
Ryusuke Konishi | 4e9e63a | 2015-11-06 16:31:45 -0800 | [diff] [blame] | 499 | bitmap, group_offset, |
| 500 | entries_per_group, lock); |
Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 501 | if (pos >= 0) { |
| 502 | /* found a free entry */ |
| 503 | nilfs_palloc_group_desc_add_entries( |
Ryusuke Konishi | 4e9e63a | 2015-11-06 16:31:45 -0800 | [diff] [blame] | 504 | desc, lock, -1); |
Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 505 | req->pr_entry_nr = |
| 506 | entries_per_group * group + pos; |
| 507 | kunmap(desc_bh->b_page); |
| 508 | kunmap(bitmap_bh->b_page); |
| 509 | |
| 510 | req->pr_desc_bh = desc_bh; |
| 511 | req->pr_bitmap_bh = bitmap_bh; |
| 512 | return 0; |
| 513 | } |
| 514 | kunmap(bitmap_bh->b_page); |
| 515 | brelse(bitmap_bh); |
| 516 | } |
| 517 | |
| 518 | group_offset = 0; |
| 519 | } |
| 520 | |
| 521 | kunmap(desc_bh->b_page); |
| 522 | brelse(desc_bh); |
| 523 | } |
| 524 | |
| 525 | /* no entries left */ |
| 526 | return -ENOSPC; |
| 527 | |
| 528 | out_desc: |
| 529 | kunmap(desc_bh->b_page); |
| 530 | brelse(desc_bh); |
| 531 | return ret; |
| 532 | } |
| 533 | |
Ryusuke Konishi | db55d92 | 2010-04-12 01:46:02 +0900 | [diff] [blame] | 534 | /** |
| 535 | * nilfs_palloc_commit_alloc_entry - finish allocation of a persistent object |
| 536 | * @inode: inode of metadata file using this allocator |
| 537 | * @req: nilfs_palloc_req structure exchanged for the allocation |
| 538 | */ |
Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 539 | void nilfs_palloc_commit_alloc_entry(struct inode *inode, |
| 540 | struct nilfs_palloc_req *req) |
| 541 | { |
Ryusuke Konishi | 5fc7b14 | 2011-05-05 12:56:51 +0900 | [diff] [blame] | 542 | mark_buffer_dirty(req->pr_bitmap_bh); |
| 543 | mark_buffer_dirty(req->pr_desc_bh); |
Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 544 | nilfs_mdt_mark_dirty(inode); |
| 545 | |
| 546 | brelse(req->pr_bitmap_bh); |
| 547 | brelse(req->pr_desc_bh); |
| 548 | } |
| 549 | |
Ryusuke Konishi | db55d92 | 2010-04-12 01:46:02 +0900 | [diff] [blame] | 550 | /** |
| 551 | * nilfs_palloc_commit_free_entry - finish deallocating a persistent object |
| 552 | * @inode: inode of metadata file using this allocator |
| 553 | * @req: nilfs_palloc_req structure exchanged for the removal |
| 554 | */ |
Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 555 | void nilfs_palloc_commit_free_entry(struct inode *inode, |
| 556 | struct nilfs_palloc_req *req) |
| 557 | { |
| 558 | struct nilfs_palloc_group_desc *desc; |
| 559 | unsigned long group, group_offset; |
| 560 | unsigned char *bitmap; |
| 561 | void *desc_kaddr, *bitmap_kaddr; |
Ryusuke Konishi | 4e9e63a | 2015-11-06 16:31:45 -0800 | [diff] [blame] | 562 | spinlock_t *lock; |
Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 563 | |
| 564 | group = nilfs_palloc_group(inode, req->pr_entry_nr, &group_offset); |
| 565 | desc_kaddr = kmap(req->pr_desc_bh->b_page); |
| 566 | desc = nilfs_palloc_block_get_group_desc(inode, group, |
| 567 | req->pr_desc_bh, desc_kaddr); |
| 568 | bitmap_kaddr = kmap(req->pr_bitmap_bh->b_page); |
Ryusuke Konishi | 141bbdb | 2009-11-14 13:48:06 +0900 | [diff] [blame] | 569 | bitmap = bitmap_kaddr + bh_offset(req->pr_bitmap_bh); |
Ryusuke Konishi | 4e9e63a | 2015-11-06 16:31:45 -0800 | [diff] [blame] | 570 | lock = nilfs_mdt_bgl_lock(inode, group); |
Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 571 | |
Ryusuke Konishi | 4e9e63a | 2015-11-06 16:31:45 -0800 | [diff] [blame] | 572 | if (!nilfs_clear_bit_atomic(lock, group_offset, bitmap)) |
Ryusuke Konishi | b7bed71 | 2015-11-06 16:31:43 -0800 | [diff] [blame] | 573 | nilfs_warning(inode->i_sb, __func__, |
| 574 | "entry number %llu already freed: ino=%lu\n", |
| 575 | (unsigned long long)req->pr_entry_nr, |
| 576 | (unsigned long)inode->i_ino); |
Ryusuke Konishi | 9954e7a | 2011-02-23 02:26:17 +0900 | [diff] [blame] | 577 | else |
Ryusuke Konishi | 4e9e63a | 2015-11-06 16:31:45 -0800 | [diff] [blame] | 578 | nilfs_palloc_group_desc_add_entries(desc, lock, 1); |
Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 579 | |
| 580 | kunmap(req->pr_bitmap_bh->b_page); |
| 581 | kunmap(req->pr_desc_bh->b_page); |
| 582 | |
Ryusuke Konishi | 5fc7b14 | 2011-05-05 12:56:51 +0900 | [diff] [blame] | 583 | mark_buffer_dirty(req->pr_desc_bh); |
| 584 | mark_buffer_dirty(req->pr_bitmap_bh); |
Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 585 | nilfs_mdt_mark_dirty(inode); |
| 586 | |
| 587 | brelse(req->pr_bitmap_bh); |
| 588 | brelse(req->pr_desc_bh); |
| 589 | } |
| 590 | |
Ryusuke Konishi | db55d92 | 2010-04-12 01:46:02 +0900 | [diff] [blame] | 591 | /** |
| 592 | * nilfs_palloc_abort_alloc_entry - cancel allocation of a persistent object |
| 593 | * @inode: inode of metadata file using this allocator |
| 594 | * @req: nilfs_palloc_req structure exchanged for the allocation |
| 595 | */ |
Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 596 | void nilfs_palloc_abort_alloc_entry(struct inode *inode, |
| 597 | struct nilfs_palloc_req *req) |
| 598 | { |
| 599 | struct nilfs_palloc_group_desc *desc; |
| 600 | void *desc_kaddr, *bitmap_kaddr; |
| 601 | unsigned char *bitmap; |
| 602 | unsigned long group, group_offset; |
Ryusuke Konishi | 4e9e63a | 2015-11-06 16:31:45 -0800 | [diff] [blame] | 603 | spinlock_t *lock; |
Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 604 | |
| 605 | group = nilfs_palloc_group(inode, req->pr_entry_nr, &group_offset); |
| 606 | desc_kaddr = kmap(req->pr_desc_bh->b_page); |
| 607 | desc = nilfs_palloc_block_get_group_desc(inode, group, |
| 608 | req->pr_desc_bh, desc_kaddr); |
| 609 | bitmap_kaddr = kmap(req->pr_bitmap_bh->b_page); |
Ryusuke Konishi | 141bbdb | 2009-11-14 13:48:06 +0900 | [diff] [blame] | 610 | bitmap = bitmap_kaddr + bh_offset(req->pr_bitmap_bh); |
Ryusuke Konishi | 4e9e63a | 2015-11-06 16:31:45 -0800 | [diff] [blame] | 611 | lock = nilfs_mdt_bgl_lock(inode, group); |
| 612 | |
| 613 | if (!nilfs_clear_bit_atomic(lock, group_offset, bitmap)) |
Ryusuke Konishi | b7bed71 | 2015-11-06 16:31:43 -0800 | [diff] [blame] | 614 | nilfs_warning(inode->i_sb, __func__, |
| 615 | "entry number %llu already freed: ino=%lu\n", |
| 616 | (unsigned long long)req->pr_entry_nr, |
| 617 | (unsigned long)inode->i_ino); |
Ryusuke Konishi | 9954e7a | 2011-02-23 02:26:17 +0900 | [diff] [blame] | 618 | else |
Ryusuke Konishi | 4e9e63a | 2015-11-06 16:31:45 -0800 | [diff] [blame] | 619 | nilfs_palloc_group_desc_add_entries(desc, lock, 1); |
Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 620 | |
| 621 | kunmap(req->pr_bitmap_bh->b_page); |
| 622 | kunmap(req->pr_desc_bh->b_page); |
| 623 | |
| 624 | brelse(req->pr_bitmap_bh); |
| 625 | brelse(req->pr_desc_bh); |
| 626 | |
| 627 | req->pr_entry_nr = 0; |
| 628 | req->pr_bitmap_bh = NULL; |
| 629 | req->pr_desc_bh = NULL; |
| 630 | } |
| 631 | |
Ryusuke Konishi | db55d92 | 2010-04-12 01:46:02 +0900 | [diff] [blame] | 632 | /** |
| 633 | * nilfs_palloc_prepare_free_entry - prepare to deallocate a persistent object |
| 634 | * @inode: inode of metadata file using this allocator |
| 635 | * @req: nilfs_palloc_req structure exchanged for the removal |
| 636 | */ |
Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 637 | int nilfs_palloc_prepare_free_entry(struct inode *inode, |
| 638 | struct nilfs_palloc_req *req) |
| 639 | { |
| 640 | struct buffer_head *desc_bh, *bitmap_bh; |
| 641 | unsigned long group, group_offset; |
| 642 | int ret; |
| 643 | |
| 644 | group = nilfs_palloc_group(inode, req->pr_entry_nr, &group_offset); |
| 645 | ret = nilfs_palloc_get_desc_block(inode, group, 1, &desc_bh); |
| 646 | if (ret < 0) |
| 647 | return ret; |
| 648 | ret = nilfs_palloc_get_bitmap_block(inode, group, 1, &bitmap_bh); |
| 649 | if (ret < 0) { |
| 650 | brelse(desc_bh); |
| 651 | return ret; |
| 652 | } |
| 653 | |
| 654 | req->pr_desc_bh = desc_bh; |
| 655 | req->pr_bitmap_bh = bitmap_bh; |
| 656 | return 0; |
| 657 | } |
| 658 | |
Ryusuke Konishi | db55d92 | 2010-04-12 01:46:02 +0900 | [diff] [blame] | 659 | /** |
| 660 | * nilfs_palloc_abort_free_entry - cancel deallocating a persistent object |
| 661 | * @inode: inode of metadata file using this allocator |
| 662 | * @req: nilfs_palloc_req structure exchanged for the removal |
| 663 | */ |
Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 664 | void nilfs_palloc_abort_free_entry(struct inode *inode, |
| 665 | struct nilfs_palloc_req *req) |
| 666 | { |
| 667 | brelse(req->pr_bitmap_bh); |
| 668 | brelse(req->pr_desc_bh); |
| 669 | |
| 670 | req->pr_entry_nr = 0; |
| 671 | req->pr_bitmap_bh = NULL; |
| 672 | req->pr_desc_bh = NULL; |
| 673 | } |
| 674 | |
Ryusuke Konishi | db55d92 | 2010-04-12 01:46:02 +0900 | [diff] [blame] | 675 | /** |
| 676 | * nilfs_palloc_group_is_in - judge if an entry is in a group |
| 677 | * @inode: inode of metadata file using this allocator |
| 678 | * @group: group number |
| 679 | * @nr: serial number of the entry (e.g. inode number) |
| 680 | */ |
Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 681 | static int |
| 682 | nilfs_palloc_group_is_in(struct inode *inode, unsigned long group, __u64 nr) |
| 683 | { |
| 684 | __u64 first, last; |
| 685 | |
| 686 | first = group * nilfs_palloc_entries_per_group(inode); |
| 687 | last = first + nilfs_palloc_entries_per_group(inode) - 1; |
| 688 | return (nr >= first) && (nr <= last); |
| 689 | } |
| 690 | |
Ryusuke Konishi | db55d92 | 2010-04-12 01:46:02 +0900 | [diff] [blame] | 691 | /** |
| 692 | * nilfs_palloc_freev - deallocate a set of persistent objects |
| 693 | * @inode: inode of metadata file using this allocator |
| 694 | * @entry_nrs: array of entry numbers to be deallocated |
| 695 | * @nitems: number of entries stored in @entry_nrs |
| 696 | */ |
Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 697 | int nilfs_palloc_freev(struct inode *inode, __u64 *entry_nrs, size_t nitems) |
| 698 | { |
| 699 | struct buffer_head *desc_bh, *bitmap_bh; |
| 700 | struct nilfs_palloc_group_desc *desc; |
| 701 | unsigned char *bitmap; |
| 702 | void *desc_kaddr, *bitmap_kaddr; |
| 703 | unsigned long group, group_offset; |
Ryusuke Konishi | 4e9e63a | 2015-11-06 16:31:45 -0800 | [diff] [blame] | 704 | spinlock_t *lock; |
Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 705 | int i, j, n, ret; |
| 706 | |
Ryusuke Konishi | 349dbc3 | 2011-05-10 20:59:34 +0900 | [diff] [blame] | 707 | for (i = 0; i < nitems; i = j) { |
Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 708 | group = nilfs_palloc_group(inode, entry_nrs[i], &group_offset); |
| 709 | ret = nilfs_palloc_get_desc_block(inode, group, 0, &desc_bh); |
| 710 | if (ret < 0) |
| 711 | return ret; |
| 712 | ret = nilfs_palloc_get_bitmap_block(inode, group, 0, |
| 713 | &bitmap_bh); |
| 714 | if (ret < 0) { |
| 715 | brelse(desc_bh); |
| 716 | return ret; |
| 717 | } |
| 718 | desc_kaddr = kmap(desc_bh->b_page); |
| 719 | desc = nilfs_palloc_block_get_group_desc( |
| 720 | inode, group, desc_bh, desc_kaddr); |
| 721 | bitmap_kaddr = kmap(bitmap_bh->b_page); |
Ryusuke Konishi | 141bbdb | 2009-11-14 13:48:06 +0900 | [diff] [blame] | 722 | bitmap = bitmap_kaddr + bh_offset(bitmap_bh); |
Ryusuke Konishi | 4e9e63a | 2015-11-06 16:31:45 -0800 | [diff] [blame] | 723 | lock = nilfs_mdt_bgl_lock(inode, group); |
Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 724 | for (j = i, n = 0; |
| 725 | (j < nitems) && nilfs_palloc_group_is_in(inode, group, |
| 726 | entry_nrs[j]); |
Ryusuke Konishi | 9954e7a | 2011-02-23 02:26:17 +0900 | [diff] [blame] | 727 | j++) { |
Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 728 | nilfs_palloc_group(inode, entry_nrs[j], &group_offset); |
Ryusuke Konishi | 4e9e63a | 2015-11-06 16:31:45 -0800 | [diff] [blame] | 729 | if (!nilfs_clear_bit_atomic(lock, group_offset, |
| 730 | bitmap)) { |
Ryusuke Konishi | b7bed71 | 2015-11-06 16:31:43 -0800 | [diff] [blame] | 731 | nilfs_warning(inode->i_sb, __func__, |
| 732 | "entry number %llu already freed: ino=%lu\n", |
| 733 | (unsigned long long)entry_nrs[j], |
| 734 | (unsigned long)inode->i_ino); |
Ryusuke Konishi | 9954e7a | 2011-02-23 02:26:17 +0900 | [diff] [blame] | 735 | } else { |
| 736 | n++; |
Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 737 | } |
| 738 | } |
Ryusuke Konishi | 4e9e63a | 2015-11-06 16:31:45 -0800 | [diff] [blame] | 739 | nilfs_palloc_group_desc_add_entries(desc, lock, n); |
Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 740 | |
| 741 | kunmap(bitmap_bh->b_page); |
| 742 | kunmap(desc_bh->b_page); |
| 743 | |
Ryusuke Konishi | 5fc7b14 | 2011-05-05 12:56:51 +0900 | [diff] [blame] | 744 | mark_buffer_dirty(desc_bh); |
| 745 | mark_buffer_dirty(bitmap_bh); |
Ryusuke Konishi | 5442680 | 2009-04-06 19:01:29 -0700 | [diff] [blame] | 746 | nilfs_mdt_mark_dirty(inode); |
| 747 | |
| 748 | brelse(bitmap_bh); |
| 749 | brelse(desc_bh); |
| 750 | } |
| 751 | return 0; |
| 752 | } |
Ryusuke Konishi | db38d5a | 2009-11-14 15:54:27 +0900 | [diff] [blame] | 753 | |
| 754 | void nilfs_palloc_setup_cache(struct inode *inode, |
| 755 | struct nilfs_palloc_cache *cache) |
| 756 | { |
| 757 | NILFS_MDT(inode)->mi_palloc_cache = cache; |
| 758 | spin_lock_init(&cache->lock); |
| 759 | } |
| 760 | |
| 761 | void nilfs_palloc_clear_cache(struct inode *inode) |
| 762 | { |
| 763 | struct nilfs_palloc_cache *cache = NILFS_MDT(inode)->mi_palloc_cache; |
| 764 | |
| 765 | spin_lock(&cache->lock); |
| 766 | brelse(cache->prev_desc.bh); |
| 767 | brelse(cache->prev_bitmap.bh); |
| 768 | brelse(cache->prev_entry.bh); |
| 769 | cache->prev_desc.bh = NULL; |
| 770 | cache->prev_bitmap.bh = NULL; |
| 771 | cache->prev_entry.bh = NULL; |
| 772 | spin_unlock(&cache->lock); |
| 773 | } |
| 774 | |
| 775 | void nilfs_palloc_destroy_cache(struct inode *inode) |
| 776 | { |
| 777 | nilfs_palloc_clear_cache(inode); |
| 778 | NILFS_MDT(inode)->mi_palloc_cache = NULL; |
| 779 | } |