blob: 8df0f3b7839b5ee979ee48d63323dde21b637ce2 [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
136 * @inode: inode of metadata file using this allocator
137 * @group: group number
138 * @desc: pointer to descriptor structure for the group
139 */
Ryusuke Konishi54426802009-04-06 19:01:29 -0700140static unsigned long
141nilfs_palloc_group_desc_nfrees(struct inode *inode, unsigned long group,
142 const struct nilfs_palloc_group_desc *desc)
143{
144 unsigned long nfree;
145
146 spin_lock(nilfs_mdt_bgl_lock(inode, group));
147 nfree = le32_to_cpu(desc->pg_nfrees);
148 spin_unlock(nilfs_mdt_bgl_lock(inode, group));
149 return nfree;
150}
151
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900152/**
153 * nilfs_palloc_group_desc_add_entries - adjust count of free entries
154 * @inode: inode of metadata file using this allocator
155 * @group: group number
156 * @desc: pointer to descriptor structure for the group
157 * @n: delta to be added
158 */
Ryusuke Konishi54426802009-04-06 19:01:29 -0700159static void
160nilfs_palloc_group_desc_add_entries(struct inode *inode,
161 unsigned long group,
162 struct nilfs_palloc_group_desc *desc,
163 u32 n)
164{
165 spin_lock(nilfs_mdt_bgl_lock(inode, group));
166 le32_add_cpu(&desc->pg_nfrees, n);
167 spin_unlock(nilfs_mdt_bgl_lock(inode, group));
168}
169
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900170/**
171 * nilfs_palloc_entry_blkoff - get block offset of an entry block
172 * @inode: inode of metadata file using this allocator
173 * @nr: serial number of the entry (e.g. inode number)
174 */
Ryusuke Konishi54426802009-04-06 19:01:29 -0700175static unsigned long
176nilfs_palloc_entry_blkoff(const struct inode *inode, __u64 nr)
177{
178 unsigned long group, group_offset;
179
180 group = nilfs_palloc_group(inode, nr, &group_offset);
181
182 return nilfs_palloc_bitmap_blkoff(inode, group) + 1 +
183 group_offset / NILFS_MDT(inode)->mi_entries_per_block;
184}
185
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900186/**
187 * nilfs_palloc_desc_block_init - initialize buffer of a group descriptor block
188 * @inode: inode of metadata file
189 * @bh: buffer head of the buffer to be initialized
190 * @kaddr: kernel address mapped for the page including the buffer
191 */
Ryusuke Konishi54426802009-04-06 19:01:29 -0700192static void nilfs_palloc_desc_block_init(struct inode *inode,
193 struct buffer_head *bh, void *kaddr)
194{
195 struct nilfs_palloc_group_desc *desc = kaddr + bh_offset(bh);
196 unsigned long n = nilfs_palloc_groups_per_desc_block(inode);
197 __le32 nfrees;
198
199 nfrees = cpu_to_le32(nilfs_palloc_entries_per_group(inode));
200 while (n-- > 0) {
201 desc->pg_nfrees = nfrees;
202 desc++;
203 }
204}
205
Ryusuke Konishi70622a22009-11-14 18:40:27 +0900206static int nilfs_palloc_get_block(struct inode *inode, unsigned long blkoff,
207 int create,
208 void (*init_block)(struct inode *,
209 struct buffer_head *,
210 void *),
211 struct buffer_head **bhp,
212 struct nilfs_bh_assoc *prev,
213 spinlock_t *lock)
214{
215 int ret;
216
217 spin_lock(lock);
218 if (prev->bh && blkoff == prev->blkoff) {
219 get_bh(prev->bh);
220 *bhp = prev->bh;
221 spin_unlock(lock);
222 return 0;
223 }
224 spin_unlock(lock);
225
226 ret = nilfs_mdt_get_block(inode, blkoff, create, init_block, bhp);
227 if (!ret) {
228 spin_lock(lock);
229 /*
230 * The following code must be safe for change of the
231 * cache contents during the get block call.
232 */
233 brelse(prev->bh);
234 get_bh(*bhp);
235 prev->bh = *bhp;
236 prev->blkoff = blkoff;
237 spin_unlock(lock);
238 }
239 return ret;
240}
241
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900242/**
243 * nilfs_palloc_get_desc_block - get buffer head of a group descriptor block
244 * @inode: inode of metadata file using this allocator
245 * @group: group number
246 * @create: create flag
247 * @bhp: pointer to store the resultant buffer head
248 */
Ryusuke Konishi54426802009-04-06 19:01:29 -0700249static int nilfs_palloc_get_desc_block(struct inode *inode,
250 unsigned long group,
251 int create, struct buffer_head **bhp)
252{
Ryusuke Konishi70622a22009-11-14 18:40:27 +0900253 struct nilfs_palloc_cache *cache = NILFS_MDT(inode)->mi_palloc_cache;
254
255 return nilfs_palloc_get_block(inode,
256 nilfs_palloc_desc_blkoff(inode, group),
257 create, nilfs_palloc_desc_block_init,
258 bhp, &cache->prev_desc, &cache->lock);
Ryusuke Konishi54426802009-04-06 19:01:29 -0700259}
260
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900261/**
262 * nilfs_palloc_get_bitmap_block - get buffer head of a bitmap block
263 * @inode: inode of metadata file using this allocator
264 * @group: group number
265 * @create: create flag
266 * @bhp: pointer to store the resultant buffer head
267 */
Ryusuke Konishi54426802009-04-06 19:01:29 -0700268static int nilfs_palloc_get_bitmap_block(struct inode *inode,
269 unsigned long group,
270 int create, struct buffer_head **bhp)
271{
Ryusuke Konishi70622a22009-11-14 18:40:27 +0900272 struct nilfs_palloc_cache *cache = NILFS_MDT(inode)->mi_palloc_cache;
273
274 return nilfs_palloc_get_block(inode,
275 nilfs_palloc_bitmap_blkoff(inode, group),
276 create, NULL, bhp,
277 &cache->prev_bitmap, &cache->lock);
Ryusuke Konishi54426802009-04-06 19:01:29 -0700278}
279
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900280/**
281 * nilfs_palloc_get_entry_block - get buffer head of an entry block
282 * @inode: inode of metadata file using this allocator
283 * @nr: serial number of the entry (e.g. inode number)
284 * @create: create flag
285 * @bhp: pointer to store the resultant buffer head
286 */
Ryusuke Konishi54426802009-04-06 19:01:29 -0700287int nilfs_palloc_get_entry_block(struct inode *inode, __u64 nr,
288 int create, struct buffer_head **bhp)
289{
Ryusuke Konishi70622a22009-11-14 18:40:27 +0900290 struct nilfs_palloc_cache *cache = NILFS_MDT(inode)->mi_palloc_cache;
291
292 return nilfs_palloc_get_block(inode,
293 nilfs_palloc_entry_blkoff(inode, nr),
294 create, NULL, bhp,
295 &cache->prev_entry, &cache->lock);
Ryusuke Konishi54426802009-04-06 19:01:29 -0700296}
297
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900298/**
299 * nilfs_palloc_block_get_group_desc - get kernel address of a group descriptor
300 * @inode: inode of metadata file using this allocator
301 * @group: group number
302 * @bh: buffer head of the buffer storing the group descriptor block
303 * @kaddr: kernel address mapped for the page including the buffer
304 */
Ryusuke Konishi54426802009-04-06 19:01:29 -0700305static struct nilfs_palloc_group_desc *
306nilfs_palloc_block_get_group_desc(const struct inode *inode,
307 unsigned long group,
308 const struct buffer_head *bh, void *kaddr)
309{
310 return (struct nilfs_palloc_group_desc *)(kaddr + bh_offset(bh)) +
311 group % nilfs_palloc_groups_per_desc_block(inode);
312}
313
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900314/**
315 * nilfs_palloc_block_get_entry - get kernel address of an entry
316 * @inode: inode of metadata file using this allocator
317 * @nr: serial number of the entry (e.g. inode number)
318 * @bh: buffer head of the buffer storing the entry block
319 * @kaddr: kernel address mapped for the page including the buffer
320 */
Ryusuke Konishi54426802009-04-06 19:01:29 -0700321void *nilfs_palloc_block_get_entry(const struct inode *inode, __u64 nr,
322 const struct buffer_head *bh, void *kaddr)
323{
324 unsigned long entry_offset, group_offset;
325
326 nilfs_palloc_group(inode, nr, &group_offset);
327 entry_offset = group_offset % NILFS_MDT(inode)->mi_entries_per_block;
328
329 return kaddr + bh_offset(bh) +
330 entry_offset * NILFS_MDT(inode)->mi_entry_size;
331}
332
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900333/**
334 * nilfs_palloc_find_available_slot - find available slot in a group
335 * @inode: inode of metadata file using this allocator
336 * @group: group number
337 * @target: offset number of an entry in the group (start point)
338 * @bitmap: bitmap of the group
339 * @bsize: size in bits
340 */
Ryusuke Konishi54426802009-04-06 19:01:29 -0700341static int nilfs_palloc_find_available_slot(struct inode *inode,
342 unsigned long group,
343 unsigned long target,
344 unsigned char *bitmap,
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900345 int bsize)
Ryusuke Konishi54426802009-04-06 19:01:29 -0700346{
347 int curr, pos, end, i;
348
349 if (target > 0) {
350 end = (target + BITS_PER_LONG - 1) & ~(BITS_PER_LONG - 1);
351 if (end > bsize)
352 end = bsize;
353 pos = nilfs_find_next_zero_bit(bitmap, end, target);
354 if (pos < end &&
355 !nilfs_set_bit_atomic(
356 nilfs_mdt_bgl_lock(inode, group), pos, bitmap))
357 return pos;
358 } else
359 end = 0;
360
361 for (i = 0, curr = end;
362 i < bsize;
363 i += BITS_PER_LONG, curr += BITS_PER_LONG) {
364 /* wrap around */
365 if (curr >= bsize)
366 curr = 0;
367 while (*((unsigned long *)bitmap + curr / BITS_PER_LONG)
368 != ~0UL) {
369 end = curr + BITS_PER_LONG;
370 if (end > bsize)
371 end = bsize;
372 pos = nilfs_find_next_zero_bit(bitmap, end, curr);
373 if ((pos < end) &&
374 !nilfs_set_bit_atomic(
375 nilfs_mdt_bgl_lock(inode, group), pos,
376 bitmap))
377 return pos;
378 }
379 }
380 return -ENOSPC;
381}
382
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900383/**
384 * nilfs_palloc_rest_groups_in_desc_block - get the remaining number of groups
385 * in a group descriptor block
386 * @inode: inode of metadata file using this allocator
387 * @curr: current group number
388 * @max: maximum number of groups
389 */
Ryusuke Konishi54426802009-04-06 19:01:29 -0700390static unsigned long
391nilfs_palloc_rest_groups_in_desc_block(const struct inode *inode,
392 unsigned long curr, unsigned long max)
393{
394 return min_t(unsigned long,
395 nilfs_palloc_groups_per_desc_block(inode) -
396 curr % nilfs_palloc_groups_per_desc_block(inode),
397 max - curr + 1);
398}
399
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900400/**
Vyacheslav Dubeykoc7ef9722013-07-03 15:08:05 -0700401 * nilfs_palloc_count_desc_blocks - count descriptor blocks number
402 * @inode: inode of metadata file using this allocator
403 * @desc_blocks: descriptor blocks number [out]
404 */
405static int nilfs_palloc_count_desc_blocks(struct inode *inode,
406 unsigned long *desc_blocks)
407{
Ryusuke Konishi3568a132015-04-16 12:46:34 -0700408 __u64 blknum;
Vyacheslav Dubeykoc7ef9722013-07-03 15:08:05 -0700409 int ret;
410
411 ret = nilfs_bmap_last_key(NILFS_I(inode)->i_bmap, &blknum);
412 if (likely(!ret))
413 *desc_blocks = DIV_ROUND_UP(
Ryusuke Konishi3568a132015-04-16 12:46:34 -0700414 (unsigned long)blknum,
415 NILFS_MDT(inode)->mi_blocks_per_desc_block);
Vyacheslav Dubeykoc7ef9722013-07-03 15:08:05 -0700416 return ret;
417}
418
419/**
420 * nilfs_palloc_mdt_file_can_grow - check potential opportunity for
421 * MDT file growing
422 * @inode: inode of metadata file using this allocator
423 * @desc_blocks: known current descriptor blocks count
424 */
425static inline bool nilfs_palloc_mdt_file_can_grow(struct inode *inode,
426 unsigned long desc_blocks)
427{
428 return (nilfs_palloc_groups_per_desc_block(inode) * desc_blocks) <
429 nilfs_palloc_groups_count(inode);
430}
431
432/**
433 * nilfs_palloc_count_max_entries - count max number of entries that can be
434 * described by descriptor blocks count
435 * @inode: inode of metadata file using this allocator
436 * @nused: current number of used entries
437 * @nmaxp: max number of entries [out]
438 */
439int nilfs_palloc_count_max_entries(struct inode *inode, u64 nused, u64 *nmaxp)
440{
441 unsigned long desc_blocks = 0;
442 u64 entries_per_desc_block, nmax;
443 int err;
444
445 err = nilfs_palloc_count_desc_blocks(inode, &desc_blocks);
446 if (unlikely(err))
447 return err;
448
449 entries_per_desc_block = (u64)nilfs_palloc_entries_per_group(inode) *
450 nilfs_palloc_groups_per_desc_block(inode);
451 nmax = entries_per_desc_block * desc_blocks;
452
453 if (nused == nmax &&
454 nilfs_palloc_mdt_file_can_grow(inode, desc_blocks))
455 nmax += entries_per_desc_block;
456
457 if (nused > nmax)
458 return -ERANGE;
459
460 *nmaxp = nmax;
461 return 0;
462}
463
464/**
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900465 * nilfs_palloc_prepare_alloc_entry - prepare to allocate a persistent object
466 * @inode: inode of metadata file using this allocator
467 * @req: nilfs_palloc_req structure exchanged for the allocation
468 */
Ryusuke Konishi54426802009-04-06 19:01:29 -0700469int nilfs_palloc_prepare_alloc_entry(struct inode *inode,
470 struct nilfs_palloc_req *req)
471{
472 struct buffer_head *desc_bh, *bitmap_bh;
473 struct nilfs_palloc_group_desc *desc;
474 unsigned char *bitmap;
475 void *desc_kaddr, *bitmap_kaddr;
476 unsigned long group, maxgroup, ngroups;
477 unsigned long group_offset, maxgroup_offset;
478 unsigned long n, entries_per_group, groups_per_desc_block;
479 unsigned long i, j;
480 int pos, ret;
481
482 ngroups = nilfs_palloc_groups_count(inode);
483 maxgroup = ngroups - 1;
484 group = nilfs_palloc_group(inode, req->pr_entry_nr, &group_offset);
485 entries_per_group = nilfs_palloc_entries_per_group(inode);
486 groups_per_desc_block = nilfs_palloc_groups_per_desc_block(inode);
487
488 for (i = 0; i < ngroups; i += n) {
489 if (group >= ngroups) {
490 /* wrap around */
491 group = 0;
492 maxgroup = nilfs_palloc_group(inode, req->pr_entry_nr,
493 &maxgroup_offset) - 1;
494 }
495 ret = nilfs_palloc_get_desc_block(inode, group, 1, &desc_bh);
496 if (ret < 0)
497 return ret;
498 desc_kaddr = kmap(desc_bh->b_page);
499 desc = nilfs_palloc_block_get_group_desc(
500 inode, group, desc_bh, desc_kaddr);
501 n = nilfs_palloc_rest_groups_in_desc_block(inode, group,
502 maxgroup);
503 for (j = 0; j < n; j++, desc++, group++) {
504 if (nilfs_palloc_group_desc_nfrees(inode, group, desc)
505 > 0) {
506 ret = nilfs_palloc_get_bitmap_block(
507 inode, group, 1, &bitmap_bh);
508 if (ret < 0)
509 goto out_desc;
510 bitmap_kaddr = kmap(bitmap_bh->b_page);
Ryusuke Konishi141bbdb2009-11-14 13:48:06 +0900511 bitmap = bitmap_kaddr + bh_offset(bitmap_bh);
Ryusuke Konishi54426802009-04-06 19:01:29 -0700512 pos = nilfs_palloc_find_available_slot(
513 inode, group, group_offset, bitmap,
514 entries_per_group);
515 if (pos >= 0) {
516 /* found a free entry */
517 nilfs_palloc_group_desc_add_entries(
518 inode, group, desc, -1);
519 req->pr_entry_nr =
520 entries_per_group * group + pos;
521 kunmap(desc_bh->b_page);
522 kunmap(bitmap_bh->b_page);
523
524 req->pr_desc_bh = desc_bh;
525 req->pr_bitmap_bh = bitmap_bh;
526 return 0;
527 }
528 kunmap(bitmap_bh->b_page);
529 brelse(bitmap_bh);
530 }
531
532 group_offset = 0;
533 }
534
535 kunmap(desc_bh->b_page);
536 brelse(desc_bh);
537 }
538
539 /* no entries left */
540 return -ENOSPC;
541
542 out_desc:
543 kunmap(desc_bh->b_page);
544 brelse(desc_bh);
545 return ret;
546}
547
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900548/**
549 * nilfs_palloc_commit_alloc_entry - finish allocation of a persistent object
550 * @inode: inode of metadata file using this allocator
551 * @req: nilfs_palloc_req structure exchanged for the allocation
552 */
Ryusuke Konishi54426802009-04-06 19:01:29 -0700553void nilfs_palloc_commit_alloc_entry(struct inode *inode,
554 struct nilfs_palloc_req *req)
555{
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900556 mark_buffer_dirty(req->pr_bitmap_bh);
557 mark_buffer_dirty(req->pr_desc_bh);
Ryusuke Konishi54426802009-04-06 19:01:29 -0700558 nilfs_mdt_mark_dirty(inode);
559
560 brelse(req->pr_bitmap_bh);
561 brelse(req->pr_desc_bh);
562}
563
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900564/**
565 * nilfs_palloc_commit_free_entry - finish deallocating a persistent object
566 * @inode: inode of metadata file using this allocator
567 * @req: nilfs_palloc_req structure exchanged for the removal
568 */
Ryusuke Konishi54426802009-04-06 19:01:29 -0700569void nilfs_palloc_commit_free_entry(struct inode *inode,
570 struct nilfs_palloc_req *req)
571{
572 struct nilfs_palloc_group_desc *desc;
573 unsigned long group, group_offset;
574 unsigned char *bitmap;
575 void *desc_kaddr, *bitmap_kaddr;
576
577 group = nilfs_palloc_group(inode, req->pr_entry_nr, &group_offset);
578 desc_kaddr = kmap(req->pr_desc_bh->b_page);
579 desc = nilfs_palloc_block_get_group_desc(inode, group,
580 req->pr_desc_bh, desc_kaddr);
581 bitmap_kaddr = kmap(req->pr_bitmap_bh->b_page);
Ryusuke Konishi141bbdb2009-11-14 13:48:06 +0900582 bitmap = bitmap_kaddr + bh_offset(req->pr_bitmap_bh);
Ryusuke Konishi54426802009-04-06 19:01:29 -0700583
584 if (!nilfs_clear_bit_atomic(nilfs_mdt_bgl_lock(inode, group),
585 group_offset, bitmap))
586 printk(KERN_WARNING "%s: entry number %llu already freed\n",
587 __func__, (unsigned long long)req->pr_entry_nr);
Ryusuke Konishi9954e7a2011-02-23 02:26:17 +0900588 else
589 nilfs_palloc_group_desc_add_entries(inode, group, desc, 1);
Ryusuke Konishi54426802009-04-06 19:01:29 -0700590
591 kunmap(req->pr_bitmap_bh->b_page);
592 kunmap(req->pr_desc_bh->b_page);
593
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900594 mark_buffer_dirty(req->pr_desc_bh);
595 mark_buffer_dirty(req->pr_bitmap_bh);
Ryusuke Konishi54426802009-04-06 19:01:29 -0700596 nilfs_mdt_mark_dirty(inode);
597
598 brelse(req->pr_bitmap_bh);
599 brelse(req->pr_desc_bh);
600}
601
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900602/**
603 * nilfs_palloc_abort_alloc_entry - cancel allocation of a persistent object
604 * @inode: inode of metadata file using this allocator
605 * @req: nilfs_palloc_req structure exchanged for the allocation
606 */
Ryusuke Konishi54426802009-04-06 19:01:29 -0700607void nilfs_palloc_abort_alloc_entry(struct inode *inode,
608 struct nilfs_palloc_req *req)
609{
610 struct nilfs_palloc_group_desc *desc;
611 void *desc_kaddr, *bitmap_kaddr;
612 unsigned char *bitmap;
613 unsigned long group, group_offset;
614
615 group = nilfs_palloc_group(inode, req->pr_entry_nr, &group_offset);
616 desc_kaddr = kmap(req->pr_desc_bh->b_page);
617 desc = nilfs_palloc_block_get_group_desc(inode, group,
618 req->pr_desc_bh, desc_kaddr);
619 bitmap_kaddr = kmap(req->pr_bitmap_bh->b_page);
Ryusuke Konishi141bbdb2009-11-14 13:48:06 +0900620 bitmap = bitmap_kaddr + bh_offset(req->pr_bitmap_bh);
Ryusuke Konishi54426802009-04-06 19:01:29 -0700621 if (!nilfs_clear_bit_atomic(nilfs_mdt_bgl_lock(inode, group),
622 group_offset, bitmap))
Ryusuke Konishibe3bd222010-04-12 01:51:03 +0900623 printk(KERN_WARNING "%s: entry number %llu already freed\n",
Ryusuke Konishi54426802009-04-06 19:01:29 -0700624 __func__, (unsigned long long)req->pr_entry_nr);
Ryusuke Konishi9954e7a2011-02-23 02:26:17 +0900625 else
626 nilfs_palloc_group_desc_add_entries(inode, group, desc, 1);
Ryusuke Konishi54426802009-04-06 19:01:29 -0700627
628 kunmap(req->pr_bitmap_bh->b_page);
629 kunmap(req->pr_desc_bh->b_page);
630
631 brelse(req->pr_bitmap_bh);
632 brelse(req->pr_desc_bh);
633
634 req->pr_entry_nr = 0;
635 req->pr_bitmap_bh = NULL;
636 req->pr_desc_bh = NULL;
637}
638
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900639/**
640 * nilfs_palloc_prepare_free_entry - prepare to deallocate a persistent object
641 * @inode: inode of metadata file using this allocator
642 * @req: nilfs_palloc_req structure exchanged for the removal
643 */
Ryusuke Konishi54426802009-04-06 19:01:29 -0700644int nilfs_palloc_prepare_free_entry(struct inode *inode,
645 struct nilfs_palloc_req *req)
646{
647 struct buffer_head *desc_bh, *bitmap_bh;
648 unsigned long group, group_offset;
649 int ret;
650
651 group = nilfs_palloc_group(inode, req->pr_entry_nr, &group_offset);
652 ret = nilfs_palloc_get_desc_block(inode, group, 1, &desc_bh);
653 if (ret < 0)
654 return ret;
655 ret = nilfs_palloc_get_bitmap_block(inode, group, 1, &bitmap_bh);
656 if (ret < 0) {
657 brelse(desc_bh);
658 return ret;
659 }
660
661 req->pr_desc_bh = desc_bh;
662 req->pr_bitmap_bh = bitmap_bh;
663 return 0;
664}
665
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900666/**
667 * nilfs_palloc_abort_free_entry - cancel deallocating a persistent object
668 * @inode: inode of metadata file using this allocator
669 * @req: nilfs_palloc_req structure exchanged for the removal
670 */
Ryusuke Konishi54426802009-04-06 19:01:29 -0700671void nilfs_palloc_abort_free_entry(struct inode *inode,
672 struct nilfs_palloc_req *req)
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_group_is_in - judge if an entry is in a group
684 * @inode: inode of metadata file using this allocator
685 * @group: group number
686 * @nr: serial number of the entry (e.g. inode number)
687 */
Ryusuke Konishi54426802009-04-06 19:01:29 -0700688static int
689nilfs_palloc_group_is_in(struct inode *inode, unsigned long group, __u64 nr)
690{
691 __u64 first, last;
692
693 first = group * nilfs_palloc_entries_per_group(inode);
694 last = first + nilfs_palloc_entries_per_group(inode) - 1;
695 return (nr >= first) && (nr <= last);
696}
697
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900698/**
699 * nilfs_palloc_freev - deallocate a set of persistent objects
700 * @inode: inode of metadata file using this allocator
701 * @entry_nrs: array of entry numbers to be deallocated
702 * @nitems: number of entries stored in @entry_nrs
703 */
Ryusuke Konishi54426802009-04-06 19:01:29 -0700704int nilfs_palloc_freev(struct inode *inode, __u64 *entry_nrs, size_t nitems)
705{
706 struct buffer_head *desc_bh, *bitmap_bh;
707 struct nilfs_palloc_group_desc *desc;
708 unsigned char *bitmap;
709 void *desc_kaddr, *bitmap_kaddr;
710 unsigned long group, group_offset;
711 int i, j, n, ret;
712
Ryusuke Konishi349dbc32011-05-10 20:59:34 +0900713 for (i = 0; i < nitems; i = j) {
Ryusuke Konishi54426802009-04-06 19:01:29 -0700714 group = nilfs_palloc_group(inode, entry_nrs[i], &group_offset);
715 ret = nilfs_palloc_get_desc_block(inode, group, 0, &desc_bh);
716 if (ret < 0)
717 return ret;
718 ret = nilfs_palloc_get_bitmap_block(inode, group, 0,
719 &bitmap_bh);
720 if (ret < 0) {
721 brelse(desc_bh);
722 return ret;
723 }
724 desc_kaddr = kmap(desc_bh->b_page);
725 desc = nilfs_palloc_block_get_group_desc(
726 inode, group, desc_bh, desc_kaddr);
727 bitmap_kaddr = kmap(bitmap_bh->b_page);
Ryusuke Konishi141bbdb2009-11-14 13:48:06 +0900728 bitmap = bitmap_kaddr + bh_offset(bitmap_bh);
Ryusuke Konishi54426802009-04-06 19:01:29 -0700729 for (j = i, n = 0;
730 (j < nitems) && nilfs_palloc_group_is_in(inode, group,
731 entry_nrs[j]);
Ryusuke Konishi9954e7a2011-02-23 02:26:17 +0900732 j++) {
Ryusuke Konishi54426802009-04-06 19:01:29 -0700733 nilfs_palloc_group(inode, entry_nrs[j], &group_offset);
734 if (!nilfs_clear_bit_atomic(
735 nilfs_mdt_bgl_lock(inode, group),
736 group_offset, bitmap)) {
737 printk(KERN_WARNING
738 "%s: entry number %llu already freed\n",
739 __func__,
740 (unsigned long long)entry_nrs[j]);
Ryusuke Konishi9954e7a2011-02-23 02:26:17 +0900741 } else {
742 n++;
Ryusuke Konishi54426802009-04-06 19:01:29 -0700743 }
744 }
745 nilfs_palloc_group_desc_add_entries(inode, group, desc, n);
746
747 kunmap(bitmap_bh->b_page);
748 kunmap(desc_bh->b_page);
749
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900750 mark_buffer_dirty(desc_bh);
751 mark_buffer_dirty(bitmap_bh);
Ryusuke Konishi54426802009-04-06 19:01:29 -0700752 nilfs_mdt_mark_dirty(inode);
753
754 brelse(bitmap_bh);
755 brelse(desc_bh);
756 }
757 return 0;
758}
Ryusuke Konishidb38d5a2009-11-14 15:54:27 +0900759
760void nilfs_palloc_setup_cache(struct inode *inode,
761 struct nilfs_palloc_cache *cache)
762{
763 NILFS_MDT(inode)->mi_palloc_cache = cache;
764 spin_lock_init(&cache->lock);
765}
766
767void nilfs_palloc_clear_cache(struct inode *inode)
768{
769 struct nilfs_palloc_cache *cache = NILFS_MDT(inode)->mi_palloc_cache;
770
771 spin_lock(&cache->lock);
772 brelse(cache->prev_desc.bh);
773 brelse(cache->prev_bitmap.bh);
774 brelse(cache->prev_entry.bh);
775 cache->prev_desc.bh = NULL;
776 cache->prev_bitmap.bh = NULL;
777 cache->prev_entry.bh = NULL;
778 spin_unlock(&cache->lock);
779}
780
781void nilfs_palloc_destroy_cache(struct inode *inode)
782{
783 nilfs_palloc_clear_cache(inode);
784 NILFS_MDT(inode)->mi_palloc_cache = NULL;
785}