nilfs2: do not call nilfs_mdt_bgl_lock() needlessly
[deliverable/linux.git] / fs / nilfs2 / alloc.c
CommitLineData
5442680f
RK
1/*
2 * alloc.c - NILFS dat/inode allocator
3 *
4 * Copyright (C) 2006-2008 Nippon Telegraph and Telephone Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 * Original code was written by Koji Sato <koji@osrg.net>.
21 * Two allocators were unified by Ryusuke Konishi <ryusuke@osrg.net>,
22 * Amagai Yoshiji <amagai@osrg.net>.
23 */
24
25#include <linux/types.h>
26#include <linux/buffer_head.h>
27#include <linux/fs.h>
28#include <linux/bitops.h>
5a0e3ad6 29#include <linux/slab.h>
5442680f
RK
30#include "mdt.h"
31#include "alloc.h"
32
33
db55d922
RK
34/**
35 * nilfs_palloc_groups_per_desc_block - get the number of groups that a group
36 * descriptor block can maintain
37 * @inode: inode of metadata file using this allocator
38 */
5442680f
RK
39static 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
db55d922
RK
46/**
47 * nilfs_palloc_groups_count - get maximum number of groups
48 * @inode: inode of metadata file using this allocator
49 */
5442680f
RK
50static 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
db55d922
RK
56/**
57 * nilfs_palloc_init_blockgroup - initialize private variables for allocator
58 * @inode: inode of metadata file using this allocator
59 * @entry_size: size of the persistent object
60 */
5442680f
RK
61int 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
db55d922
RK
86/**
87 * nilfs_palloc_group - get group number and offset from an entry number
88 * @inode: inode of metadata file using this allocator
89 * @nr: serial number of the entry (e.g. inode number)
90 * @offset: pointer to store offset number in the group
91 */
5442680f
RK
92static 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
db55d922
RK
101/**
102 * nilfs_palloc_desc_blkoff - get block offset of a group descriptor block
103 * @inode: inode of metadata file using this allocator
104 * @group: group number
105 *
106 * nilfs_palloc_desc_blkoff() returns block offset of the descriptor
107 * block which contains a descriptor of the specified group.
108 */
5442680f
RK
109static 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
db55d922
RK
117/**
118 * nilfs_palloc_bitmap_blkoff - get block offset of a bitmap block
119 * @inode: inode of metadata file using this allocator
120 * @group: group number
121 *
122 * nilfs_palloc_bitmap_blkoff() returns block offset of the bitmap
123 * block used to allocate/deallocate entries in the specified group.
124 */
5442680f
RK
125static 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
db55d922
RK
134/**
135 * nilfs_palloc_group_desc_nfrees - get the number of free entries in a group
db55d922 136 * @desc: pointer to descriptor structure for the group
4e9e63a6 137 * @lock: spin lock protecting @desc
db55d922 138 */
5442680f 139static unsigned long
4e9e63a6
RK
140nilfs_palloc_group_desc_nfrees(const struct nilfs_palloc_group_desc *desc,
141 spinlock_t *lock)
5442680f
RK
142{
143 unsigned long nfree;
144
4e9e63a6 145 spin_lock(lock);
5442680f 146 nfree = le32_to_cpu(desc->pg_nfrees);
4e9e63a6 147 spin_unlock(lock);
5442680f
RK
148 return nfree;
149}
150
db55d922
RK
151/**
152 * nilfs_palloc_group_desc_add_entries - adjust count of free entries
db55d922 153 * @desc: pointer to descriptor structure for the group
4e9e63a6 154 * @lock: spin lock protecting @desc
db55d922
RK
155 * @n: delta to be added
156 */
5442680f 157static void
4e9e63a6
RK
158nilfs_palloc_group_desc_add_entries(struct nilfs_palloc_group_desc *desc,
159 spinlock_t *lock, u32 n)
5442680f 160{
4e9e63a6 161 spin_lock(lock);
5442680f 162 le32_add_cpu(&desc->pg_nfrees, n);
4e9e63a6 163 spin_unlock(lock);
5442680f
RK
164}
165
db55d922
RK
166/**
167 * nilfs_palloc_entry_blkoff - get block offset of an entry block
168 * @inode: inode of metadata file using this allocator
169 * @nr: serial number of the entry (e.g. inode number)
170 */
5442680f
RK
171static 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
db55d922
RK
182/**
183 * nilfs_palloc_desc_block_init - initialize buffer of a group descriptor block
184 * @inode: inode of metadata file
185 * @bh: buffer head of the buffer to be initialized
186 * @kaddr: kernel address mapped for the page including the buffer
187 */
5442680f
RK
188static 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
70622a20
RK
202static 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
db55d922
RK
238/**
239 * nilfs_palloc_get_desc_block - get buffer head of a group descriptor block
240 * @inode: inode of metadata file using this allocator
241 * @group: group number
242 * @create: create flag
243 * @bhp: pointer to store the resultant buffer head
244 */
5442680f
RK
245static int nilfs_palloc_get_desc_block(struct inode *inode,
246 unsigned long group,
247 int create, struct buffer_head **bhp)
248{
70622a20
RK
249 struct nilfs_palloc_cache *cache = NILFS_MDT(inode)->mi_palloc_cache;
250
251 return nilfs_palloc_get_block(inode,
252 nilfs_palloc_desc_blkoff(inode, group),
253 create, nilfs_palloc_desc_block_init,
254 bhp, &cache->prev_desc, &cache->lock);
5442680f
RK
255}
256
db55d922
RK
257/**
258 * nilfs_palloc_get_bitmap_block - get buffer head of a bitmap block
259 * @inode: inode of metadata file using this allocator
260 * @group: group number
261 * @create: create flag
262 * @bhp: pointer to store the resultant buffer head
263 */
5442680f
RK
264static int nilfs_palloc_get_bitmap_block(struct inode *inode,
265 unsigned long group,
266 int create, struct buffer_head **bhp)
267{
70622a20
RK
268 struct nilfs_palloc_cache *cache = NILFS_MDT(inode)->mi_palloc_cache;
269
270 return nilfs_palloc_get_block(inode,
271 nilfs_palloc_bitmap_blkoff(inode, group),
272 create, NULL, bhp,
273 &cache->prev_bitmap, &cache->lock);
5442680f
RK
274}
275
db55d922
RK
276/**
277 * nilfs_palloc_get_entry_block - get buffer head of an entry block
278 * @inode: inode of metadata file using this allocator
279 * @nr: serial number of the entry (e.g. inode number)
280 * @create: create flag
281 * @bhp: pointer to store the resultant buffer head
282 */
5442680f
RK
283int nilfs_palloc_get_entry_block(struct inode *inode, __u64 nr,
284 int create, struct buffer_head **bhp)
285{
70622a20
RK
286 struct nilfs_palloc_cache *cache = NILFS_MDT(inode)->mi_palloc_cache;
287
288 return nilfs_palloc_get_block(inode,
289 nilfs_palloc_entry_blkoff(inode, nr),
290 create, NULL, bhp,
291 &cache->prev_entry, &cache->lock);
5442680f
RK
292}
293
db55d922
RK
294/**
295 * nilfs_palloc_block_get_group_desc - get kernel address of a group descriptor
296 * @inode: inode of metadata file using this allocator
297 * @group: group number
298 * @bh: buffer head of the buffer storing the group descriptor block
299 * @kaddr: kernel address mapped for the page including the buffer
300 */
5442680f
RK
301static 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
db55d922
RK
310/**
311 * nilfs_palloc_block_get_entry - get kernel address of an entry
312 * @inode: inode of metadata file using this allocator
313 * @nr: serial number of the entry (e.g. inode number)
314 * @bh: buffer head of the buffer storing the entry block
315 * @kaddr: kernel address mapped for the page including the buffer
316 */
5442680f
RK
317void *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
db55d922
RK
329/**
330 * nilfs_palloc_find_available_slot - find available slot in a group
db55d922 331 * @bitmap: bitmap of the group
4e9e63a6 332 * @target: offset number of an entry in the group (start point)
db55d922 333 * @bsize: size in bits
4e9e63a6 334 * @lock: spin lock protecting @bitmap
db55d922 335 */
4e9e63a6 336static int nilfs_palloc_find_available_slot(unsigned char *bitmap,
5442680f 337 unsigned long target,
4e9e63a6
RK
338 int bsize,
339 spinlock_t *lock)
5442680f
RK
340{
341 int curr, pos, end, i;
342
343 if (target > 0) {
344 end = (target + BITS_PER_LONG - 1) & ~(BITS_PER_LONG - 1);
345 if (end > bsize)
346 end = bsize;
347 pos = nilfs_find_next_zero_bit(bitmap, end, target);
4e9e63a6 348 if (pos < end && !nilfs_set_bit_atomic(lock, pos, bitmap))
5442680f 349 return pos;
4e9e63a6 350 } else {
5442680f 351 end = 0;
4e9e63a6 352 }
5442680f
RK
353
354 for (i = 0, curr = end;
355 i < bsize;
356 i += BITS_PER_LONG, curr += BITS_PER_LONG) {
357 /* wrap around */
358 if (curr >= bsize)
359 curr = 0;
360 while (*((unsigned long *)bitmap + curr / BITS_PER_LONG)
361 != ~0UL) {
362 end = curr + BITS_PER_LONG;
363 if (end > bsize)
364 end = bsize;
365 pos = nilfs_find_next_zero_bit(bitmap, end, curr);
4e9e63a6
RK
366 if (pos < end &&
367 !nilfs_set_bit_atomic(lock, pos, bitmap))
5442680f
RK
368 return pos;
369 }
370 }
371 return -ENOSPC;
372}
373
db55d922
RK
374/**
375 * nilfs_palloc_rest_groups_in_desc_block - get the remaining number of groups
376 * in a group descriptor block
377 * @inode: inode of metadata file using this allocator
378 * @curr: current group number
379 * @max: maximum number of groups
380 */
5442680f
RK
381static unsigned long
382nilfs_palloc_rest_groups_in_desc_block(const struct inode *inode,
383 unsigned long curr, unsigned long max)
384{
385 return min_t(unsigned long,
386 nilfs_palloc_groups_per_desc_block(inode) -
387 curr % nilfs_palloc_groups_per_desc_block(inode),
388 max - curr + 1);
389}
390
c7ef972c
VD
391/**
392 * nilfs_palloc_count_desc_blocks - count descriptor blocks number
393 * @inode: inode of metadata file using this allocator
394 * @desc_blocks: descriptor blocks number [out]
395 */
396static int nilfs_palloc_count_desc_blocks(struct inode *inode,
397 unsigned long *desc_blocks)
398{
3568a13f 399 __u64 blknum;
c7ef972c
VD
400 int ret;
401
402 ret = nilfs_bmap_last_key(NILFS_I(inode)->i_bmap, &blknum);
403 if (likely(!ret))
404 *desc_blocks = DIV_ROUND_UP(
3568a13f
RK
405 (unsigned long)blknum,
406 NILFS_MDT(inode)->mi_blocks_per_desc_block);
c7ef972c
VD
407 return ret;
408}
409
410/**
411 * nilfs_palloc_mdt_file_can_grow - check potential opportunity for
412 * MDT file growing
413 * @inode: inode of metadata file using this allocator
414 * @desc_blocks: known current descriptor blocks count
415 */
416static inline bool nilfs_palloc_mdt_file_can_grow(struct inode *inode,
417 unsigned long desc_blocks)
418{
419 return (nilfs_palloc_groups_per_desc_block(inode) * desc_blocks) <
420 nilfs_palloc_groups_count(inode);
421}
422
423/**
424 * nilfs_palloc_count_max_entries - count max number of entries that can be
425 * described by descriptor blocks count
426 * @inode: inode of metadata file using this allocator
427 * @nused: current number of used entries
428 * @nmaxp: max number of entries [out]
429 */
430int nilfs_palloc_count_max_entries(struct inode *inode, u64 nused, u64 *nmaxp)
431{
432 unsigned long desc_blocks = 0;
433 u64 entries_per_desc_block, nmax;
434 int err;
435
436 err = nilfs_palloc_count_desc_blocks(inode, &desc_blocks);
437 if (unlikely(err))
438 return err;
439
440 entries_per_desc_block = (u64)nilfs_palloc_entries_per_group(inode) *
441 nilfs_palloc_groups_per_desc_block(inode);
442 nmax = entries_per_desc_block * desc_blocks;
443
444 if (nused == nmax &&
445 nilfs_palloc_mdt_file_can_grow(inode, desc_blocks))
446 nmax += entries_per_desc_block;
447
448 if (nused > nmax)
449 return -ERANGE;
450
451 *nmaxp = nmax;
452 return 0;
453}
454
db55d922
RK
455/**
456 * nilfs_palloc_prepare_alloc_entry - prepare to allocate a persistent object
457 * @inode: inode of metadata file using this allocator
458 * @req: nilfs_palloc_req structure exchanged for the allocation
459 */
5442680f
RK
460int nilfs_palloc_prepare_alloc_entry(struct inode *inode,
461 struct nilfs_palloc_req *req)
462{
463 struct buffer_head *desc_bh, *bitmap_bh;
464 struct nilfs_palloc_group_desc *desc;
465 unsigned char *bitmap;
466 void *desc_kaddr, *bitmap_kaddr;
467 unsigned long group, maxgroup, ngroups;
468 unsigned long group_offset, maxgroup_offset;
469 unsigned long n, entries_per_group, groups_per_desc_block;
470 unsigned long i, j;
4e9e63a6 471 spinlock_t *lock;
5442680f
RK
472 int pos, ret;
473
474 ngroups = nilfs_palloc_groups_count(inode);
475 maxgroup = ngroups - 1;
476 group = nilfs_palloc_group(inode, req->pr_entry_nr, &group_offset);
477 entries_per_group = nilfs_palloc_entries_per_group(inode);
478 groups_per_desc_block = nilfs_palloc_groups_per_desc_block(inode);
479
480 for (i = 0; i < ngroups; i += n) {
481 if (group >= ngroups) {
482 /* wrap around */
483 group = 0;
484 maxgroup = nilfs_palloc_group(inode, req->pr_entry_nr,
485 &maxgroup_offset) - 1;
486 }
487 ret = nilfs_palloc_get_desc_block(inode, group, 1, &desc_bh);
488 if (ret < 0)
489 return ret;
490 desc_kaddr = kmap(desc_bh->b_page);
491 desc = nilfs_palloc_block_get_group_desc(
492 inode, group, desc_bh, desc_kaddr);
493 n = nilfs_palloc_rest_groups_in_desc_block(inode, group,
494 maxgroup);
495 for (j = 0; j < n; j++, desc++, group++) {
4e9e63a6
RK
496 lock = nilfs_mdt_bgl_lock(inode, group);
497 if (nilfs_palloc_group_desc_nfrees(desc, lock) > 0) {
5442680f
RK
498 ret = nilfs_palloc_get_bitmap_block(
499 inode, group, 1, &bitmap_bh);
500 if (ret < 0)
501 goto out_desc;
502 bitmap_kaddr = kmap(bitmap_bh->b_page);
141bbdba 503 bitmap = bitmap_kaddr + bh_offset(bitmap_bh);
5442680f 504 pos = nilfs_palloc_find_available_slot(
4e9e63a6
RK
505 bitmap, group_offset,
506 entries_per_group, lock);
5442680f
RK
507 if (pos >= 0) {
508 /* found a free entry */
509 nilfs_palloc_group_desc_add_entries(
4e9e63a6 510 desc, lock, -1);
5442680f
RK
511 req->pr_entry_nr =
512 entries_per_group * group + pos;
513 kunmap(desc_bh->b_page);
514 kunmap(bitmap_bh->b_page);
515
516 req->pr_desc_bh = desc_bh;
517 req->pr_bitmap_bh = bitmap_bh;
518 return 0;
519 }
520 kunmap(bitmap_bh->b_page);
521 brelse(bitmap_bh);
522 }
523
524 group_offset = 0;
525 }
526
527 kunmap(desc_bh->b_page);
528 brelse(desc_bh);
529 }
530
531 /* no entries left */
532 return -ENOSPC;
533
534 out_desc:
535 kunmap(desc_bh->b_page);
536 brelse(desc_bh);
537 return ret;
538}
539
db55d922
RK
540/**
541 * nilfs_palloc_commit_alloc_entry - finish allocation of a persistent object
542 * @inode: inode of metadata file using this allocator
543 * @req: nilfs_palloc_req structure exchanged for the allocation
544 */
5442680f
RK
545void nilfs_palloc_commit_alloc_entry(struct inode *inode,
546 struct nilfs_palloc_req *req)
547{
5fc7b141
RK
548 mark_buffer_dirty(req->pr_bitmap_bh);
549 mark_buffer_dirty(req->pr_desc_bh);
5442680f
RK
550 nilfs_mdt_mark_dirty(inode);
551
552 brelse(req->pr_bitmap_bh);
553 brelse(req->pr_desc_bh);
554}
555
db55d922
RK
556/**
557 * nilfs_palloc_commit_free_entry - finish deallocating a persistent object
558 * @inode: inode of metadata file using this allocator
559 * @req: nilfs_palloc_req structure exchanged for the removal
560 */
5442680f
RK
561void nilfs_palloc_commit_free_entry(struct inode *inode,
562 struct nilfs_palloc_req *req)
563{
564 struct nilfs_palloc_group_desc *desc;
565 unsigned long group, group_offset;
566 unsigned char *bitmap;
567 void *desc_kaddr, *bitmap_kaddr;
4e9e63a6 568 spinlock_t *lock;
5442680f
RK
569
570 group = nilfs_palloc_group(inode, req->pr_entry_nr, &group_offset);
571 desc_kaddr = kmap(req->pr_desc_bh->b_page);
572 desc = nilfs_palloc_block_get_group_desc(inode, group,
573 req->pr_desc_bh, desc_kaddr);
574 bitmap_kaddr = kmap(req->pr_bitmap_bh->b_page);
141bbdba 575 bitmap = bitmap_kaddr + bh_offset(req->pr_bitmap_bh);
4e9e63a6 576 lock = nilfs_mdt_bgl_lock(inode, group);
5442680f 577
4e9e63a6 578 if (!nilfs_clear_bit_atomic(lock, group_offset, bitmap))
b7bed712
RK
579 nilfs_warning(inode->i_sb, __func__,
580 "entry number %llu already freed: ino=%lu\n",
581 (unsigned long long)req->pr_entry_nr,
582 (unsigned long)inode->i_ino);
9954e7af 583 else
4e9e63a6 584 nilfs_palloc_group_desc_add_entries(desc, lock, 1);
5442680f
RK
585
586 kunmap(req->pr_bitmap_bh->b_page);
587 kunmap(req->pr_desc_bh->b_page);
588
5fc7b141
RK
589 mark_buffer_dirty(req->pr_desc_bh);
590 mark_buffer_dirty(req->pr_bitmap_bh);
5442680f
RK
591 nilfs_mdt_mark_dirty(inode);
592
593 brelse(req->pr_bitmap_bh);
594 brelse(req->pr_desc_bh);
595}
596
db55d922
RK
597/**
598 * nilfs_palloc_abort_alloc_entry - cancel allocation of a persistent object
599 * @inode: inode of metadata file using this allocator
600 * @req: nilfs_palloc_req structure exchanged for the allocation
601 */
5442680f
RK
602void nilfs_palloc_abort_alloc_entry(struct inode *inode,
603 struct nilfs_palloc_req *req)
604{
605 struct nilfs_palloc_group_desc *desc;
606 void *desc_kaddr, *bitmap_kaddr;
607 unsigned char *bitmap;
608 unsigned long group, group_offset;
4e9e63a6 609 spinlock_t *lock;
5442680f
RK
610
611 group = nilfs_palloc_group(inode, req->pr_entry_nr, &group_offset);
612 desc_kaddr = kmap(req->pr_desc_bh->b_page);
613 desc = nilfs_palloc_block_get_group_desc(inode, group,
614 req->pr_desc_bh, desc_kaddr);
615 bitmap_kaddr = kmap(req->pr_bitmap_bh->b_page);
141bbdba 616 bitmap = bitmap_kaddr + bh_offset(req->pr_bitmap_bh);
4e9e63a6
RK
617 lock = nilfs_mdt_bgl_lock(inode, group);
618
619 if (!nilfs_clear_bit_atomic(lock, group_offset, bitmap))
b7bed712
RK
620 nilfs_warning(inode->i_sb, __func__,
621 "entry number %llu already freed: ino=%lu\n",
622 (unsigned long long)req->pr_entry_nr,
623 (unsigned long)inode->i_ino);
9954e7af 624 else
4e9e63a6 625 nilfs_palloc_group_desc_add_entries(desc, lock, 1);
5442680f
RK
626
627 kunmap(req->pr_bitmap_bh->b_page);
628 kunmap(req->pr_desc_bh->b_page);
629
630 brelse(req->pr_bitmap_bh);
631 brelse(req->pr_desc_bh);
632
633 req->pr_entry_nr = 0;
634 req->pr_bitmap_bh = NULL;
635 req->pr_desc_bh = NULL;
636}
637
db55d922
RK
638/**
639 * nilfs_palloc_prepare_free_entry - prepare to deallocate a persistent object
640 * @inode: inode of metadata file using this allocator
641 * @req: nilfs_palloc_req structure exchanged for the removal
642 */
5442680f
RK
643int nilfs_palloc_prepare_free_entry(struct inode *inode,
644 struct nilfs_palloc_req *req)
645{
646 struct buffer_head *desc_bh, *bitmap_bh;
647 unsigned long group, group_offset;
648 int ret;
649
650 group = nilfs_palloc_group(inode, req->pr_entry_nr, &group_offset);
651 ret = nilfs_palloc_get_desc_block(inode, group, 1, &desc_bh);
652 if (ret < 0)
653 return ret;
654 ret = nilfs_palloc_get_bitmap_block(inode, group, 1, &bitmap_bh);
655 if (ret < 0) {
656 brelse(desc_bh);
657 return ret;
658 }
659
660 req->pr_desc_bh = desc_bh;
661 req->pr_bitmap_bh = bitmap_bh;
662 return 0;
663}
664
db55d922
RK
665/**
666 * nilfs_palloc_abort_free_entry - cancel deallocating a persistent object
667 * @inode: inode of metadata file using this allocator
668 * @req: nilfs_palloc_req structure exchanged for the removal
669 */
5442680f
RK
670void nilfs_palloc_abort_free_entry(struct inode *inode,
671 struct nilfs_palloc_req *req)
672{
673 brelse(req->pr_bitmap_bh);
674 brelse(req->pr_desc_bh);
675
676 req->pr_entry_nr = 0;
677 req->pr_bitmap_bh = NULL;
678 req->pr_desc_bh = NULL;
679}
680
db55d922
RK
681/**
682 * nilfs_palloc_group_is_in - judge if an entry is in a group
683 * @inode: inode of metadata file using this allocator
684 * @group: group number
685 * @nr: serial number of the entry (e.g. inode number)
686 */
5442680f
RK
687static int
688nilfs_palloc_group_is_in(struct inode *inode, unsigned long group, __u64 nr)
689{
690 __u64 first, last;
691
692 first = group * nilfs_palloc_entries_per_group(inode);
693 last = first + nilfs_palloc_entries_per_group(inode) - 1;
694 return (nr >= first) && (nr <= last);
695}
696
db55d922
RK
697/**
698 * nilfs_palloc_freev - deallocate a set of persistent objects
699 * @inode: inode of metadata file using this allocator
700 * @entry_nrs: array of entry numbers to be deallocated
701 * @nitems: number of entries stored in @entry_nrs
702 */
5442680f
RK
703int nilfs_palloc_freev(struct inode *inode, __u64 *entry_nrs, size_t nitems)
704{
705 struct buffer_head *desc_bh, *bitmap_bh;
706 struct nilfs_palloc_group_desc *desc;
707 unsigned char *bitmap;
708 void *desc_kaddr, *bitmap_kaddr;
709 unsigned long group, group_offset;
4e9e63a6 710 spinlock_t *lock;
5442680f
RK
711 int i, j, n, ret;
712
349dbc36 713 for (i = 0; i < nitems; i = j) {
5442680f
RK
714 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);
141bbdba 728 bitmap = bitmap_kaddr + bh_offset(bitmap_bh);
4e9e63a6 729 lock = nilfs_mdt_bgl_lock(inode, group);
5442680f
RK
730 for (j = i, n = 0;
731 (j < nitems) && nilfs_palloc_group_is_in(inode, group,
732 entry_nrs[j]);
9954e7af 733 j++) {
5442680f 734 nilfs_palloc_group(inode, entry_nrs[j], &group_offset);
4e9e63a6
RK
735 if (!nilfs_clear_bit_atomic(lock, group_offset,
736 bitmap)) {
b7bed712
RK
737 nilfs_warning(inode->i_sb, __func__,
738 "entry number %llu already freed: ino=%lu\n",
739 (unsigned long long)entry_nrs[j],
740 (unsigned long)inode->i_ino);
9954e7af
RK
741 } else {
742 n++;
5442680f
RK
743 }
744 }
4e9e63a6 745 nilfs_palloc_group_desc_add_entries(desc, lock, n);
5442680f
RK
746
747 kunmap(bitmap_bh->b_page);
748 kunmap(desc_bh->b_page);
749
5fc7b141
RK
750 mark_buffer_dirty(desc_bh);
751 mark_buffer_dirty(bitmap_bh);
5442680f
RK
752 nilfs_mdt_mark_dirty(inode);
753
754 brelse(bitmap_bh);
755 brelse(desc_bh);
756 }
757 return 0;
758}
db38d5ad
RK
759
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}
This page took 0.689612 seconds and 5 git commands to generate.