blob: b15daf871f996554f024b83be393bb09f1baafd4 [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/**
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 Konishi54426802009-04-06 19:01:29 -0700245static int nilfs_palloc_get_desc_block(struct inode *inode,
246 unsigned long group,
247 int create, struct buffer_head **bhp)
248{
Ryusuke Konishi70622a22009-11-14 18:40:27 +0900249 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 Konishi54426802009-04-06 19:01:29 -0700255}
256
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900257/**
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 Konishi54426802009-04-06 19:01:29 -0700264static int nilfs_palloc_get_bitmap_block(struct inode *inode,
265 unsigned long group,
266 int create, struct buffer_head **bhp)
267{
Ryusuke Konishi70622a22009-11-14 18:40:27 +0900268 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 Konishi54426802009-04-06 19:01:29 -0700274}
275
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900276/**
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 Konishi54426802009-04-06 19:01:29 -0700283int nilfs_palloc_get_entry_block(struct inode *inode, __u64 nr,
284 int create, struct buffer_head **bhp)
285{
Ryusuke Konishi70622a22009-11-14 18:40:27 +0900286 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 Konishi54426802009-04-06 19:01:29 -0700292}
293
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900294/**
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 Konishi54426802009-04-06 19:01:29 -0700301static struct nilfs_palloc_group_desc *
302nilfs_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 Konishidb55d922010-04-12 01:46:02 +0900310/**
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 Konishi54426802009-04-06 19:01:29 -0700317void *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 Konishidb55d922010-04-12 01:46:02 +0900329/**
330 * nilfs_palloc_find_available_slot - find available slot in a group
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900331 * @bitmap: bitmap of the group
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800332 * @target: offset number of an entry in the group (start point)
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900333 * @bsize: size in bits
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800334 * @lock: spin lock protecting @bitmap
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900335 */
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800336static int nilfs_palloc_find_available_slot(unsigned char *bitmap,
Ryusuke Konishi54426802009-04-06 19:01:29 -0700337 unsigned long target,
Ryusuke Konishi18c41b32015-11-06 16:31:48 -0800338 unsigned bsize,
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800339 spinlock_t *lock)
Ryusuke Konishi54426802009-04-06 19:01:29 -0700340{
Ryusuke Konishi18c41b32015-11-06 16:31:48 -0800341 int pos, end = bsize;
Ryusuke Konishi54426802009-04-06 19:01:29 -0700342
Ryusuke Konishi18c41b32015-11-06 16:31:48 -0800343 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 Konishi54426802009-04-06 19:01:29 -0700350 return pos;
Ryusuke Konishi18c41b32015-11-06 16:31:48 -0800351 } while (++pos < end);
352
353 end = target;
Ryusuke Konishi54426802009-04-06 19:01:29 -0700354 }
Ryusuke Konishi18c41b32015-11-06 16:31:48 -0800355
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 Konishi54426802009-04-06 19:01:29 -0700365 return -ENOSPC;
366}
367
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900368/**
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 Konishi54426802009-04-06 19:01:29 -0700375static unsigned long
376nilfs_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 Konishidb55d922010-04-12 01:46:02 +0900385/**
Vyacheslav Dubeykoc7ef9722013-07-03 15:08:05 -0700386 * 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 */
390static int nilfs_palloc_count_desc_blocks(struct inode *inode,
391 unsigned long *desc_blocks)
392{
Ryusuke Konishi3568a132015-04-16 12:46:34 -0700393 __u64 blknum;
Vyacheslav Dubeykoc7ef9722013-07-03 15:08:05 -0700394 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 Konishi3568a132015-04-16 12:46:34 -0700399 (unsigned long)blknum,
400 NILFS_MDT(inode)->mi_blocks_per_desc_block);
Vyacheslav Dubeykoc7ef9722013-07-03 15:08:05 -0700401 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 */
410static 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 */
424int 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 Konishidb55d922010-04-12 01:46:02 +0900450 * 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 Konishi54426802009-04-06 19:01:29 -0700454int 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 Konishi4e9e63a2015-11-06 16:31:45 -0800465 spinlock_t *lock;
Ryusuke Konishi54426802009-04-06 19:01:29 -0700466 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 Konishi4e9e63a2015-11-06 16:31:45 -0800490 lock = nilfs_mdt_bgl_lock(inode, group);
491 if (nilfs_palloc_group_desc_nfrees(desc, lock) > 0) {
Ryusuke Konishi54426802009-04-06 19:01:29 -0700492 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 Konishi141bbdb2009-11-14 13:48:06 +0900497 bitmap = bitmap_kaddr + bh_offset(bitmap_bh);
Ryusuke Konishi54426802009-04-06 19:01:29 -0700498 pos = nilfs_palloc_find_available_slot(
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800499 bitmap, group_offset,
500 entries_per_group, lock);
Ryusuke Konishi54426802009-04-06 19:01:29 -0700501 if (pos >= 0) {
502 /* found a free entry */
503 nilfs_palloc_group_desc_add_entries(
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800504 desc, lock, -1);
Ryusuke Konishi54426802009-04-06 19:01:29 -0700505 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 Konishidb55d922010-04-12 01:46:02 +0900534/**
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 Konishi54426802009-04-06 19:01:29 -0700539void nilfs_palloc_commit_alloc_entry(struct inode *inode,
540 struct nilfs_palloc_req *req)
541{
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900542 mark_buffer_dirty(req->pr_bitmap_bh);
543 mark_buffer_dirty(req->pr_desc_bh);
Ryusuke Konishi54426802009-04-06 19:01:29 -0700544 nilfs_mdt_mark_dirty(inode);
545
546 brelse(req->pr_bitmap_bh);
547 brelse(req->pr_desc_bh);
548}
549
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900550/**
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 Konishi54426802009-04-06 19:01:29 -0700555void 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 Konishi4e9e63a2015-11-06 16:31:45 -0800562 spinlock_t *lock;
Ryusuke Konishi54426802009-04-06 19:01:29 -0700563
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 Konishi141bbdb2009-11-14 13:48:06 +0900569 bitmap = bitmap_kaddr + bh_offset(req->pr_bitmap_bh);
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800570 lock = nilfs_mdt_bgl_lock(inode, group);
Ryusuke Konishi54426802009-04-06 19:01:29 -0700571
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800572 if (!nilfs_clear_bit_atomic(lock, group_offset, bitmap))
Ryusuke Konishib7bed712015-11-06 16:31:43 -0800573 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 Konishi9954e7a2011-02-23 02:26:17 +0900577 else
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800578 nilfs_palloc_group_desc_add_entries(desc, lock, 1);
Ryusuke Konishi54426802009-04-06 19:01:29 -0700579
580 kunmap(req->pr_bitmap_bh->b_page);
581 kunmap(req->pr_desc_bh->b_page);
582
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900583 mark_buffer_dirty(req->pr_desc_bh);
584 mark_buffer_dirty(req->pr_bitmap_bh);
Ryusuke Konishi54426802009-04-06 19:01:29 -0700585 nilfs_mdt_mark_dirty(inode);
586
587 brelse(req->pr_bitmap_bh);
588 brelse(req->pr_desc_bh);
589}
590
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900591/**
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 Konishi54426802009-04-06 19:01:29 -0700596void 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 Konishi4e9e63a2015-11-06 16:31:45 -0800603 spinlock_t *lock;
Ryusuke Konishi54426802009-04-06 19:01:29 -0700604
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 Konishi141bbdb2009-11-14 13:48:06 +0900610 bitmap = bitmap_kaddr + bh_offset(req->pr_bitmap_bh);
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800611 lock = nilfs_mdt_bgl_lock(inode, group);
612
613 if (!nilfs_clear_bit_atomic(lock, group_offset, bitmap))
Ryusuke Konishib7bed712015-11-06 16:31:43 -0800614 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 Konishi9954e7a2011-02-23 02:26:17 +0900618 else
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800619 nilfs_palloc_group_desc_add_entries(desc, lock, 1);
Ryusuke Konishi54426802009-04-06 19:01:29 -0700620
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 Konishidb55d922010-04-12 01:46:02 +0900632/**
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 Konishi54426802009-04-06 19:01:29 -0700637int 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 Konishidb55d922010-04-12 01:46:02 +0900659/**
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 Konishi54426802009-04-06 19:01:29 -0700664void 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 Konishidb55d922010-04-12 01:46:02 +0900675/**
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 Konishi54426802009-04-06 19:01:29 -0700681static int
682nilfs_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 Konishidb55d922010-04-12 01:46:02 +0900691/**
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 Konishi54426802009-04-06 19:01:29 -0700697int 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 Konishi4e9e63a2015-11-06 16:31:45 -0800704 spinlock_t *lock;
Ryusuke Konishi54426802009-04-06 19:01:29 -0700705 int i, j, n, ret;
706
Ryusuke Konishi349dbc32011-05-10 20:59:34 +0900707 for (i = 0; i < nitems; i = j) {
Ryusuke Konishi54426802009-04-06 19:01:29 -0700708 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 Konishi141bbdb2009-11-14 13:48:06 +0900722 bitmap = bitmap_kaddr + bh_offset(bitmap_bh);
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800723 lock = nilfs_mdt_bgl_lock(inode, group);
Ryusuke Konishi54426802009-04-06 19:01:29 -0700724 for (j = i, n = 0;
725 (j < nitems) && nilfs_palloc_group_is_in(inode, group,
726 entry_nrs[j]);
Ryusuke Konishi9954e7a2011-02-23 02:26:17 +0900727 j++) {
Ryusuke Konishi54426802009-04-06 19:01:29 -0700728 nilfs_palloc_group(inode, entry_nrs[j], &group_offset);
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800729 if (!nilfs_clear_bit_atomic(lock, group_offset,
730 bitmap)) {
Ryusuke Konishib7bed712015-11-06 16:31:43 -0800731 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 Konishi9954e7a2011-02-23 02:26:17 +0900735 } else {
736 n++;
Ryusuke Konishi54426802009-04-06 19:01:29 -0700737 }
738 }
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800739 nilfs_palloc_group_desc_add_entries(desc, lock, n);
Ryusuke Konishi54426802009-04-06 19:01:29 -0700740
741 kunmap(bitmap_bh->b_page);
742 kunmap(desc_bh->b_page);
743
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900744 mark_buffer_dirty(desc_bh);
745 mark_buffer_dirty(bitmap_bh);
Ryusuke Konishi54426802009-04-06 19:01:29 -0700746 nilfs_mdt_mark_dirty(inode);
747
748 brelse(bitmap_bh);
749 brelse(desc_bh);
750 }
751 return 0;
752}
Ryusuke Konishidb38d5a2009-11-14 15:54:27 +0900753
754void 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
761void 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
775void nilfs_palloc_destroy_cache(struct inode *inode)
776{
777 nilfs_palloc_clear_cache(inode);
778 NILFS_MDT(inode)->mi_palloc_cache = NULL;
779}