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