blob: 225b797688654d5c1b802a9472d800f2a8e48e7f [file] [log] [blame]
Ryusuke Konishi54426802009-04-06 19:01:29 -07001/*
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 Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Ryusuke Konishi54426802009-04-06 19:01:29 -070030#include "mdt.h"
31#include "alloc.h"
32
33
Ryusuke Konishidb55d922010-04-12 01:46:02 +090034/**
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 Konishi54426802009-04-06 19:01:29 -070039static inline unsigned long
40nilfs_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 Konishidb55d922010-04-12 01:46:02 +090046/**
47 * nilfs_palloc_groups_count - get maximum number of groups
48 * @inode: inode of metadata file using this allocator
49 */
Ryusuke Konishi54426802009-04-06 19:01:29 -070050static inline unsigned long
51nilfs_palloc_groups_count(const struct inode *inode)
52{
53 return 1UL << (BITS_PER_LONG - (inode->i_blkbits + 3 /* log2(8) */));
54}
55
Ryusuke Konishidb55d922010-04-12 01:46:02 +090056/**
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 Konishi54426802009-04-06 19:01:29 -070061int 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 Konishidb55d922010-04-12 01:46:02 +090086/**
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 Konishi54426802009-04-06 19:01:29 -070092static 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 Konishidb55d922010-04-12 01:46:02 +0900101/**
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 Konishi54426802009-04-06 19:01:29 -0700109static unsigned long
110nilfs_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 Konishidb55d922010-04-12 01:46:02 +0900117/**
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 Konishi54426802009-04-06 19:01:29 -0700125static unsigned long
126nilfs_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 Konishidb55d922010-04-12 01:46:02 +0900134/**
135 * nilfs_palloc_group_desc_nfrees - get the number of free entries in a group
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900136 * @desc: pointer to descriptor structure for the group
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800137 * @lock: spin lock protecting @desc
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900138 */
Ryusuke Konishi54426802009-04-06 19:01:29 -0700139static unsigned long
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800140nilfs_palloc_group_desc_nfrees(const struct nilfs_palloc_group_desc *desc,
141 spinlock_t *lock)
Ryusuke Konishi54426802009-04-06 19:01:29 -0700142{
143 unsigned long nfree;
144
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800145 spin_lock(lock);
Ryusuke Konishi54426802009-04-06 19:01:29 -0700146 nfree = le32_to_cpu(desc->pg_nfrees);
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800147 spin_unlock(lock);
Ryusuke Konishi54426802009-04-06 19:01:29 -0700148 return nfree;
149}
150
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900151/**
152 * nilfs_palloc_group_desc_add_entries - adjust count of free entries
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900153 * @desc: pointer to descriptor structure for the group
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800154 * @lock: spin lock protecting @desc
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900155 * @n: delta to be added
156 */
Ryusuke Konishi54426802009-04-06 19:01:29 -0700157static void
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800158nilfs_palloc_group_desc_add_entries(struct nilfs_palloc_group_desc *desc,
159 spinlock_t *lock, u32 n)
Ryusuke Konishi54426802009-04-06 19:01:29 -0700160{
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800161 spin_lock(lock);
Ryusuke Konishi54426802009-04-06 19:01:29 -0700162 le32_add_cpu(&desc->pg_nfrees, n);
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800163 spin_unlock(lock);
Ryusuke Konishi54426802009-04-06 19:01:29 -0700164}
165
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900166/**
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 Konishi54426802009-04-06 19:01:29 -0700171static unsigned long
172nilfs_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 Konishidb55d922010-04-12 01:46:02 +0900182/**
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 Konishi54426802009-04-06 19:01:29 -0700188static 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 Konishi70622a22009-11-14 18:40:27 +0900202static 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 Konishidb55d922010-04-12 01:46:02 +0900238/**
Ryusuke Konishida019952015-11-06 16:31:54 -0800239 * nilfs_palloc_delete_block - delete a block on the persistent allocator file
240 * @inode: inode of metadata file using this allocator
241 * @blkoff: block offset
242 * @prev: nilfs_bh_assoc struct of the last used buffer
243 * @lock: spin lock protecting @prev
244 */
245static int nilfs_palloc_delete_block(struct inode *inode, unsigned long blkoff,
246 struct nilfs_bh_assoc *prev,
247 spinlock_t *lock)
248{
249 spin_lock(lock);
250 if (prev->bh && blkoff == prev->blkoff) {
251 brelse(prev->bh);
252 prev->bh = NULL;
253 }
254 spin_unlock(lock);
255 return nilfs_mdt_delete_block(inode, blkoff);
256}
257
258/**
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900259 * nilfs_palloc_get_desc_block - get buffer head of a group descriptor block
260 * @inode: inode of metadata file using this allocator
261 * @group: group number
262 * @create: create flag
263 * @bhp: pointer to store the resultant buffer head
264 */
Ryusuke Konishi54426802009-04-06 19:01:29 -0700265static int nilfs_palloc_get_desc_block(struct inode *inode,
266 unsigned long group,
267 int create, struct buffer_head **bhp)
268{
Ryusuke Konishi70622a22009-11-14 18:40:27 +0900269 struct nilfs_palloc_cache *cache = NILFS_MDT(inode)->mi_palloc_cache;
270
271 return nilfs_palloc_get_block(inode,
272 nilfs_palloc_desc_blkoff(inode, group),
273 create, nilfs_palloc_desc_block_init,
274 bhp, &cache->prev_desc, &cache->lock);
Ryusuke Konishi54426802009-04-06 19:01:29 -0700275}
276
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900277/**
278 * nilfs_palloc_get_bitmap_block - get buffer head of a bitmap block
279 * @inode: inode of metadata file using this allocator
280 * @group: group number
281 * @create: create flag
282 * @bhp: pointer to store the resultant buffer head
283 */
Ryusuke Konishi54426802009-04-06 19:01:29 -0700284static int nilfs_palloc_get_bitmap_block(struct inode *inode,
285 unsigned long group,
286 int create, struct buffer_head **bhp)
287{
Ryusuke Konishi70622a22009-11-14 18:40:27 +0900288 struct nilfs_palloc_cache *cache = NILFS_MDT(inode)->mi_palloc_cache;
289
290 return nilfs_palloc_get_block(inode,
291 nilfs_palloc_bitmap_blkoff(inode, group),
292 create, NULL, bhp,
293 &cache->prev_bitmap, &cache->lock);
Ryusuke Konishi54426802009-04-06 19:01:29 -0700294}
295
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900296/**
Ryusuke Konishida019952015-11-06 16:31:54 -0800297 * nilfs_palloc_delete_bitmap_block - delete a bitmap block
298 * @inode: inode of metadata file using this allocator
299 * @group: group number
300 */
301static int nilfs_palloc_delete_bitmap_block(struct inode *inode,
302 unsigned long group)
303{
304 struct nilfs_palloc_cache *cache = NILFS_MDT(inode)->mi_palloc_cache;
305
306 return nilfs_palloc_delete_block(inode,
307 nilfs_palloc_bitmap_blkoff(inode,
308 group),
309 &cache->prev_bitmap, &cache->lock);
310}
311
312/**
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900313 * nilfs_palloc_get_entry_block - get buffer head of an entry block
314 * @inode: inode of metadata file using this allocator
315 * @nr: serial number of the entry (e.g. inode number)
316 * @create: create flag
317 * @bhp: pointer to store the resultant buffer head
318 */
Ryusuke Konishi54426802009-04-06 19:01:29 -0700319int nilfs_palloc_get_entry_block(struct inode *inode, __u64 nr,
320 int create, struct buffer_head **bhp)
321{
Ryusuke Konishi70622a22009-11-14 18:40:27 +0900322 struct nilfs_palloc_cache *cache = NILFS_MDT(inode)->mi_palloc_cache;
323
324 return nilfs_palloc_get_block(inode,
325 nilfs_palloc_entry_blkoff(inode, nr),
326 create, NULL, bhp,
327 &cache->prev_entry, &cache->lock);
Ryusuke Konishi54426802009-04-06 19:01:29 -0700328}
329
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900330/**
Ryusuke Konishida019952015-11-06 16:31:54 -0800331 * nilfs_palloc_delete_entry_block - delete an entry block
332 * @inode: inode of metadata file using this allocator
333 * @nr: serial number of the entry
334 */
335static int nilfs_palloc_delete_entry_block(struct inode *inode, __u64 nr)
336{
337 struct nilfs_palloc_cache *cache = NILFS_MDT(inode)->mi_palloc_cache;
338
339 return nilfs_palloc_delete_block(inode,
340 nilfs_palloc_entry_blkoff(inode, nr),
341 &cache->prev_entry, &cache->lock);
342}
343
344/**
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900345 * nilfs_palloc_block_get_group_desc - get kernel address of a group descriptor
346 * @inode: inode of metadata file using this allocator
347 * @group: group number
348 * @bh: buffer head of the buffer storing the group descriptor block
349 * @kaddr: kernel address mapped for the page including the buffer
350 */
Ryusuke Konishi54426802009-04-06 19:01:29 -0700351static struct nilfs_palloc_group_desc *
352nilfs_palloc_block_get_group_desc(const struct inode *inode,
353 unsigned long group,
354 const struct buffer_head *bh, void *kaddr)
355{
356 return (struct nilfs_palloc_group_desc *)(kaddr + bh_offset(bh)) +
357 group % nilfs_palloc_groups_per_desc_block(inode);
358}
359
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900360/**
361 * nilfs_palloc_block_get_entry - get kernel address of an entry
362 * @inode: inode of metadata file using this allocator
363 * @nr: serial number of the entry (e.g. inode number)
364 * @bh: buffer head of the buffer storing the entry block
365 * @kaddr: kernel address mapped for the page including the buffer
366 */
Ryusuke Konishi54426802009-04-06 19:01:29 -0700367void *nilfs_palloc_block_get_entry(const struct inode *inode, __u64 nr,
368 const struct buffer_head *bh, void *kaddr)
369{
370 unsigned long entry_offset, group_offset;
371
372 nilfs_palloc_group(inode, nr, &group_offset);
373 entry_offset = group_offset % NILFS_MDT(inode)->mi_entries_per_block;
374
375 return kaddr + bh_offset(bh) +
376 entry_offset * NILFS_MDT(inode)->mi_entry_size;
377}
378
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900379/**
380 * nilfs_palloc_find_available_slot - find available slot in a group
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900381 * @bitmap: bitmap of the group
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800382 * @target: offset number of an entry in the group (start point)
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900383 * @bsize: size in bits
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800384 * @lock: spin lock protecting @bitmap
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900385 */
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800386static int nilfs_palloc_find_available_slot(unsigned char *bitmap,
Ryusuke Konishi54426802009-04-06 19:01:29 -0700387 unsigned long target,
Ryusuke Konishi18c41b32015-11-06 16:31:48 -0800388 unsigned bsize,
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800389 spinlock_t *lock)
Ryusuke Konishi54426802009-04-06 19:01:29 -0700390{
Ryusuke Konishi18c41b32015-11-06 16:31:48 -0800391 int pos, end = bsize;
Ryusuke Konishi54426802009-04-06 19:01:29 -0700392
Ryusuke Konishi18c41b32015-11-06 16:31:48 -0800393 if (likely(target < bsize)) {
394 pos = target;
395 do {
396 pos = nilfs_find_next_zero_bit(bitmap, end, pos);
397 if (pos >= end)
398 break;
399 if (!nilfs_set_bit_atomic(lock, pos, bitmap))
Ryusuke Konishi54426802009-04-06 19:01:29 -0700400 return pos;
Ryusuke Konishi18c41b32015-11-06 16:31:48 -0800401 } while (++pos < end);
402
403 end = target;
Ryusuke Konishi54426802009-04-06 19:01:29 -0700404 }
Ryusuke Konishi18c41b32015-11-06 16:31:48 -0800405
406 /* wrap around */
407 for (pos = 0; pos < end; pos++) {
408 pos = nilfs_find_next_zero_bit(bitmap, end, pos);
409 if (pos >= end)
410 break;
411 if (!nilfs_set_bit_atomic(lock, pos, bitmap))
412 return pos;
413 }
414
Ryusuke Konishi54426802009-04-06 19:01:29 -0700415 return -ENOSPC;
416}
417
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900418/**
419 * nilfs_palloc_rest_groups_in_desc_block - get the remaining number of groups
420 * in a group descriptor block
421 * @inode: inode of metadata file using this allocator
422 * @curr: current group number
423 * @max: maximum number of groups
424 */
Ryusuke Konishi54426802009-04-06 19:01:29 -0700425static unsigned long
426nilfs_palloc_rest_groups_in_desc_block(const struct inode *inode,
427 unsigned long curr, unsigned long max)
428{
429 return min_t(unsigned long,
430 nilfs_palloc_groups_per_desc_block(inode) -
431 curr % nilfs_palloc_groups_per_desc_block(inode),
432 max - curr + 1);
433}
434
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900435/**
Vyacheslav Dubeykoc7ef9722013-07-03 15:08:05 -0700436 * nilfs_palloc_count_desc_blocks - count descriptor blocks number
437 * @inode: inode of metadata file using this allocator
438 * @desc_blocks: descriptor blocks number [out]
439 */
440static int nilfs_palloc_count_desc_blocks(struct inode *inode,
441 unsigned long *desc_blocks)
442{
Ryusuke Konishi3568a132015-04-16 12:46:34 -0700443 __u64 blknum;
Vyacheslav Dubeykoc7ef9722013-07-03 15:08:05 -0700444 int ret;
445
446 ret = nilfs_bmap_last_key(NILFS_I(inode)->i_bmap, &blknum);
447 if (likely(!ret))
448 *desc_blocks = DIV_ROUND_UP(
Ryusuke Konishi3568a132015-04-16 12:46:34 -0700449 (unsigned long)blknum,
450 NILFS_MDT(inode)->mi_blocks_per_desc_block);
Vyacheslav Dubeykoc7ef9722013-07-03 15:08:05 -0700451 return ret;
452}
453
454/**
455 * nilfs_palloc_mdt_file_can_grow - check potential opportunity for
456 * MDT file growing
457 * @inode: inode of metadata file using this allocator
458 * @desc_blocks: known current descriptor blocks count
459 */
460static inline bool nilfs_palloc_mdt_file_can_grow(struct inode *inode,
461 unsigned long desc_blocks)
462{
463 return (nilfs_palloc_groups_per_desc_block(inode) * desc_blocks) <
464 nilfs_palloc_groups_count(inode);
465}
466
467/**
468 * nilfs_palloc_count_max_entries - count max number of entries that can be
469 * described by descriptor blocks count
470 * @inode: inode of metadata file using this allocator
471 * @nused: current number of used entries
472 * @nmaxp: max number of entries [out]
473 */
474int nilfs_palloc_count_max_entries(struct inode *inode, u64 nused, u64 *nmaxp)
475{
476 unsigned long desc_blocks = 0;
477 u64 entries_per_desc_block, nmax;
478 int err;
479
480 err = nilfs_palloc_count_desc_blocks(inode, &desc_blocks);
481 if (unlikely(err))
482 return err;
483
484 entries_per_desc_block = (u64)nilfs_palloc_entries_per_group(inode) *
485 nilfs_palloc_groups_per_desc_block(inode);
486 nmax = entries_per_desc_block * desc_blocks;
487
488 if (nused == nmax &&
489 nilfs_palloc_mdt_file_can_grow(inode, desc_blocks))
490 nmax += entries_per_desc_block;
491
492 if (nused > nmax)
493 return -ERANGE;
494
495 *nmaxp = nmax;
496 return 0;
497}
498
499/**
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900500 * nilfs_palloc_prepare_alloc_entry - prepare to allocate a persistent object
501 * @inode: inode of metadata file using this allocator
502 * @req: nilfs_palloc_req structure exchanged for the allocation
503 */
Ryusuke Konishi54426802009-04-06 19:01:29 -0700504int nilfs_palloc_prepare_alloc_entry(struct inode *inode,
505 struct nilfs_palloc_req *req)
506{
507 struct buffer_head *desc_bh, *bitmap_bh;
508 struct nilfs_palloc_group_desc *desc;
509 unsigned char *bitmap;
510 void *desc_kaddr, *bitmap_kaddr;
511 unsigned long group, maxgroup, ngroups;
512 unsigned long group_offset, maxgroup_offset;
513 unsigned long n, entries_per_group, groups_per_desc_block;
514 unsigned long i, j;
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800515 spinlock_t *lock;
Ryusuke Konishi54426802009-04-06 19:01:29 -0700516 int pos, ret;
517
518 ngroups = nilfs_palloc_groups_count(inode);
519 maxgroup = ngroups - 1;
520 group = nilfs_palloc_group(inode, req->pr_entry_nr, &group_offset);
521 entries_per_group = nilfs_palloc_entries_per_group(inode);
522 groups_per_desc_block = nilfs_palloc_groups_per_desc_block(inode);
523
524 for (i = 0; i < ngroups; i += n) {
525 if (group >= ngroups) {
526 /* wrap around */
527 group = 0;
528 maxgroup = nilfs_palloc_group(inode, req->pr_entry_nr,
529 &maxgroup_offset) - 1;
530 }
531 ret = nilfs_palloc_get_desc_block(inode, group, 1, &desc_bh);
532 if (ret < 0)
533 return ret;
534 desc_kaddr = kmap(desc_bh->b_page);
535 desc = nilfs_palloc_block_get_group_desc(
536 inode, group, desc_bh, desc_kaddr);
537 n = nilfs_palloc_rest_groups_in_desc_block(inode, group,
538 maxgroup);
539 for (j = 0; j < n; j++, desc++, group++) {
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800540 lock = nilfs_mdt_bgl_lock(inode, group);
541 if (nilfs_palloc_group_desc_nfrees(desc, lock) > 0) {
Ryusuke Konishi54426802009-04-06 19:01:29 -0700542 ret = nilfs_palloc_get_bitmap_block(
543 inode, group, 1, &bitmap_bh);
544 if (ret < 0)
545 goto out_desc;
546 bitmap_kaddr = kmap(bitmap_bh->b_page);
Ryusuke Konishi141bbdb2009-11-14 13:48:06 +0900547 bitmap = bitmap_kaddr + bh_offset(bitmap_bh);
Ryusuke Konishi54426802009-04-06 19:01:29 -0700548 pos = nilfs_palloc_find_available_slot(
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800549 bitmap, group_offset,
550 entries_per_group, lock);
Ryusuke Konishi54426802009-04-06 19:01:29 -0700551 if (pos >= 0) {
552 /* found a free entry */
553 nilfs_palloc_group_desc_add_entries(
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800554 desc, lock, -1);
Ryusuke Konishi54426802009-04-06 19:01:29 -0700555 req->pr_entry_nr =
556 entries_per_group * group + pos;
557 kunmap(desc_bh->b_page);
558 kunmap(bitmap_bh->b_page);
559
560 req->pr_desc_bh = desc_bh;
561 req->pr_bitmap_bh = bitmap_bh;
562 return 0;
563 }
564 kunmap(bitmap_bh->b_page);
565 brelse(bitmap_bh);
566 }
567
568 group_offset = 0;
569 }
570
571 kunmap(desc_bh->b_page);
572 brelse(desc_bh);
573 }
574
575 /* no entries left */
576 return -ENOSPC;
577
578 out_desc:
579 kunmap(desc_bh->b_page);
580 brelse(desc_bh);
581 return ret;
582}
583
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900584/**
585 * nilfs_palloc_commit_alloc_entry - finish allocation of a persistent object
586 * @inode: inode of metadata file using this allocator
587 * @req: nilfs_palloc_req structure exchanged for the allocation
588 */
Ryusuke Konishi54426802009-04-06 19:01:29 -0700589void nilfs_palloc_commit_alloc_entry(struct inode *inode,
590 struct nilfs_palloc_req *req)
591{
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900592 mark_buffer_dirty(req->pr_bitmap_bh);
593 mark_buffer_dirty(req->pr_desc_bh);
Ryusuke Konishi54426802009-04-06 19:01:29 -0700594 nilfs_mdt_mark_dirty(inode);
595
596 brelse(req->pr_bitmap_bh);
597 brelse(req->pr_desc_bh);
598}
599
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900600/**
601 * nilfs_palloc_commit_free_entry - finish deallocating a persistent object
602 * @inode: inode of metadata file using this allocator
603 * @req: nilfs_palloc_req structure exchanged for the removal
604 */
Ryusuke Konishi54426802009-04-06 19:01:29 -0700605void nilfs_palloc_commit_free_entry(struct inode *inode,
606 struct nilfs_palloc_req *req)
607{
608 struct nilfs_palloc_group_desc *desc;
609 unsigned long group, group_offset;
610 unsigned char *bitmap;
611 void *desc_kaddr, *bitmap_kaddr;
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800612 spinlock_t *lock;
Ryusuke Konishi54426802009-04-06 19:01:29 -0700613
614 group = nilfs_palloc_group(inode, req->pr_entry_nr, &group_offset);
615 desc_kaddr = kmap(req->pr_desc_bh->b_page);
616 desc = nilfs_palloc_block_get_group_desc(inode, group,
617 req->pr_desc_bh, desc_kaddr);
618 bitmap_kaddr = kmap(req->pr_bitmap_bh->b_page);
Ryusuke Konishi141bbdb2009-11-14 13:48:06 +0900619 bitmap = bitmap_kaddr + bh_offset(req->pr_bitmap_bh);
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800620 lock = nilfs_mdt_bgl_lock(inode, group);
Ryusuke Konishi54426802009-04-06 19:01:29 -0700621
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800622 if (!nilfs_clear_bit_atomic(lock, group_offset, bitmap))
Ryusuke Konishib7bed712015-11-06 16:31:43 -0800623 nilfs_warning(inode->i_sb, __func__,
624 "entry number %llu already freed: ino=%lu\n",
625 (unsigned long long)req->pr_entry_nr,
626 (unsigned long)inode->i_ino);
Ryusuke Konishi9954e7a2011-02-23 02:26:17 +0900627 else
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800628 nilfs_palloc_group_desc_add_entries(desc, lock, 1);
Ryusuke Konishi54426802009-04-06 19:01:29 -0700629
630 kunmap(req->pr_bitmap_bh->b_page);
631 kunmap(req->pr_desc_bh->b_page);
632
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900633 mark_buffer_dirty(req->pr_desc_bh);
634 mark_buffer_dirty(req->pr_bitmap_bh);
Ryusuke Konishi54426802009-04-06 19:01:29 -0700635 nilfs_mdt_mark_dirty(inode);
636
637 brelse(req->pr_bitmap_bh);
638 brelse(req->pr_desc_bh);
639}
640
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900641/**
642 * nilfs_palloc_abort_alloc_entry - cancel allocation of a persistent object
643 * @inode: inode of metadata file using this allocator
644 * @req: nilfs_palloc_req structure exchanged for the allocation
645 */
Ryusuke Konishi54426802009-04-06 19:01:29 -0700646void nilfs_palloc_abort_alloc_entry(struct inode *inode,
647 struct nilfs_palloc_req *req)
648{
649 struct nilfs_palloc_group_desc *desc;
650 void *desc_kaddr, *bitmap_kaddr;
651 unsigned char *bitmap;
652 unsigned long group, group_offset;
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800653 spinlock_t *lock;
Ryusuke Konishi54426802009-04-06 19:01:29 -0700654
655 group = nilfs_palloc_group(inode, req->pr_entry_nr, &group_offset);
656 desc_kaddr = kmap(req->pr_desc_bh->b_page);
657 desc = nilfs_palloc_block_get_group_desc(inode, group,
658 req->pr_desc_bh, desc_kaddr);
659 bitmap_kaddr = kmap(req->pr_bitmap_bh->b_page);
Ryusuke Konishi141bbdb2009-11-14 13:48:06 +0900660 bitmap = bitmap_kaddr + bh_offset(req->pr_bitmap_bh);
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800661 lock = nilfs_mdt_bgl_lock(inode, group);
662
663 if (!nilfs_clear_bit_atomic(lock, group_offset, bitmap))
Ryusuke Konishib7bed712015-11-06 16:31:43 -0800664 nilfs_warning(inode->i_sb, __func__,
665 "entry number %llu already freed: ino=%lu\n",
666 (unsigned long long)req->pr_entry_nr,
667 (unsigned long)inode->i_ino);
Ryusuke Konishi9954e7a2011-02-23 02:26:17 +0900668 else
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800669 nilfs_palloc_group_desc_add_entries(desc, lock, 1);
Ryusuke Konishi54426802009-04-06 19:01:29 -0700670
671 kunmap(req->pr_bitmap_bh->b_page);
672 kunmap(req->pr_desc_bh->b_page);
673
674 brelse(req->pr_bitmap_bh);
675 brelse(req->pr_desc_bh);
676
677 req->pr_entry_nr = 0;
678 req->pr_bitmap_bh = NULL;
679 req->pr_desc_bh = NULL;
680}
681
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900682/**
683 * nilfs_palloc_prepare_free_entry - prepare to deallocate a persistent object
684 * @inode: inode of metadata file using this allocator
685 * @req: nilfs_palloc_req structure exchanged for the removal
686 */
Ryusuke Konishi54426802009-04-06 19:01:29 -0700687int nilfs_palloc_prepare_free_entry(struct inode *inode,
688 struct nilfs_palloc_req *req)
689{
690 struct buffer_head *desc_bh, *bitmap_bh;
691 unsigned long group, group_offset;
692 int ret;
693
694 group = nilfs_palloc_group(inode, req->pr_entry_nr, &group_offset);
695 ret = nilfs_palloc_get_desc_block(inode, group, 1, &desc_bh);
696 if (ret < 0)
697 return ret;
698 ret = nilfs_palloc_get_bitmap_block(inode, group, 1, &bitmap_bh);
699 if (ret < 0) {
700 brelse(desc_bh);
701 return ret;
702 }
703
704 req->pr_desc_bh = desc_bh;
705 req->pr_bitmap_bh = bitmap_bh;
706 return 0;
707}
708
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900709/**
710 * nilfs_palloc_abort_free_entry - cancel deallocating a persistent object
711 * @inode: inode of metadata file using this allocator
712 * @req: nilfs_palloc_req structure exchanged for the removal
713 */
Ryusuke Konishi54426802009-04-06 19:01:29 -0700714void nilfs_palloc_abort_free_entry(struct inode *inode,
715 struct nilfs_palloc_req *req)
716{
717 brelse(req->pr_bitmap_bh);
718 brelse(req->pr_desc_bh);
719
720 req->pr_entry_nr = 0;
721 req->pr_bitmap_bh = NULL;
722 req->pr_desc_bh = NULL;
723}
724
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900725/**
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900726 * nilfs_palloc_freev - deallocate a set of persistent objects
727 * @inode: inode of metadata file using this allocator
728 * @entry_nrs: array of entry numbers to be deallocated
729 * @nitems: number of entries stored in @entry_nrs
730 */
Ryusuke Konishi54426802009-04-06 19:01:29 -0700731int nilfs_palloc_freev(struct inode *inode, __u64 *entry_nrs, size_t nitems)
732{
733 struct buffer_head *desc_bh, *bitmap_bh;
734 struct nilfs_palloc_group_desc *desc;
735 unsigned char *bitmap;
736 void *desc_kaddr, *bitmap_kaddr;
737 unsigned long group, group_offset;
Ryusuke Konishib2258092015-11-06 16:31:51 -0800738 __u64 group_min_nr;
739 const unsigned long epg = nilfs_palloc_entries_per_group(inode);
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800740 spinlock_t *lock;
Ryusuke Konishi54426802009-04-06 19:01:29 -0700741 int i, j, n, ret;
742
Ryusuke Konishi349dbc32011-05-10 20:59:34 +0900743 for (i = 0; i < nitems; i = j) {
Ryusuke Konishi54426802009-04-06 19:01:29 -0700744 group = nilfs_palloc_group(inode, entry_nrs[i], &group_offset);
745 ret = nilfs_palloc_get_desc_block(inode, group, 0, &desc_bh);
746 if (ret < 0)
747 return ret;
748 ret = nilfs_palloc_get_bitmap_block(inode, group, 0,
749 &bitmap_bh);
750 if (ret < 0) {
751 brelse(desc_bh);
752 return ret;
753 }
Ryusuke Konishib2258092015-11-06 16:31:51 -0800754
755 /* Get the first entry number of the group */
756 group_min_nr = (__u64)group * epg;
757
Ryusuke Konishi54426802009-04-06 19:01:29 -0700758 desc_kaddr = kmap(desc_bh->b_page);
759 desc = nilfs_palloc_block_get_group_desc(
760 inode, group, desc_bh, desc_kaddr);
761 bitmap_kaddr = kmap(bitmap_bh->b_page);
Ryusuke Konishi141bbdb2009-11-14 13:48:06 +0900762 bitmap = bitmap_kaddr + bh_offset(bitmap_bh);
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800763 lock = nilfs_mdt_bgl_lock(inode, group);
Ryusuke Konishi54426802009-04-06 19:01:29 -0700764 for (j = i, n = 0;
Ryusuke Konishib2258092015-11-06 16:31:51 -0800765 j < nitems && entry_nrs[j] >= group_min_nr &&
766 entry_nrs[j] < group_min_nr + epg;
Ryusuke Konishi9954e7a2011-02-23 02:26:17 +0900767 j++) {
Ryusuke Konishib2258092015-11-06 16:31:51 -0800768 group_offset = entry_nrs[j] - group_min_nr;
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800769 if (!nilfs_clear_bit_atomic(lock, group_offset,
770 bitmap)) {
Ryusuke Konishib7bed712015-11-06 16:31:43 -0800771 nilfs_warning(inode->i_sb, __func__,
772 "entry number %llu already freed: ino=%lu\n",
773 (unsigned long long)entry_nrs[j],
774 (unsigned long)inode->i_ino);
Ryusuke Konishi9954e7a2011-02-23 02:26:17 +0900775 } else {
776 n++;
Ryusuke Konishi54426802009-04-06 19:01:29 -0700777 }
778 }
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800779 nilfs_palloc_group_desc_add_entries(desc, lock, n);
Ryusuke Konishi54426802009-04-06 19:01:29 -0700780
781 kunmap(bitmap_bh->b_page);
782 kunmap(desc_bh->b_page);
783
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900784 mark_buffer_dirty(desc_bh);
785 mark_buffer_dirty(bitmap_bh);
Ryusuke Konishi54426802009-04-06 19:01:29 -0700786 nilfs_mdt_mark_dirty(inode);
787
788 brelse(bitmap_bh);
789 brelse(desc_bh);
790 }
791 return 0;
792}
Ryusuke Konishidb38d5a2009-11-14 15:54:27 +0900793
794void nilfs_palloc_setup_cache(struct inode *inode,
795 struct nilfs_palloc_cache *cache)
796{
797 NILFS_MDT(inode)->mi_palloc_cache = cache;
798 spin_lock_init(&cache->lock);
799}
800
801void nilfs_palloc_clear_cache(struct inode *inode)
802{
803 struct nilfs_palloc_cache *cache = NILFS_MDT(inode)->mi_palloc_cache;
804
805 spin_lock(&cache->lock);
806 brelse(cache->prev_desc.bh);
807 brelse(cache->prev_bitmap.bh);
808 brelse(cache->prev_entry.bh);
809 cache->prev_desc.bh = NULL;
810 cache->prev_bitmap.bh = NULL;
811 cache->prev_entry.bh = NULL;
812 spin_unlock(&cache->lock);
813}
814
815void nilfs_palloc_destroy_cache(struct inode *inode)
816{
817 nilfs_palloc_clear_cache(inode);
818 NILFS_MDT(inode)->mi_palloc_cache = NULL;
819}