2 * alloc.c - NILFS dat/inode allocator
4 * Copyright (C) 2006-2008 Nippon Telegraph and Telephone Corporation.
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.
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.
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
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>.
25 #include <linux/types.h>
26 #include <linux/buffer_head.h>
28 #include <linux/bitops.h>
33 static inline unsigned long
34 nilfs_palloc_groups_per_desc_block(const struct inode
*inode
)
36 return (1UL << inode
->i_blkbits
) /
37 sizeof(struct nilfs_palloc_group_desc
);
40 static inline unsigned long
41 nilfs_palloc_groups_count(const struct inode
*inode
)
43 return 1UL << (BITS_PER_LONG
- (inode
->i_blkbits
+ 3 /* log2(8) */));
46 int nilfs_palloc_init_blockgroup(struct inode
*inode
, unsigned entry_size
)
48 struct nilfs_mdt_info
*mi
= NILFS_MDT(inode
);
50 mi
->mi_bgl
= kmalloc(sizeof(*mi
->mi_bgl
), GFP_NOFS
);
54 bgl_lock_init(mi
->mi_bgl
);
56 nilfs_mdt_set_entry_size(inode
, entry_size
, 0);
58 mi
->mi_blocks_per_group
=
59 DIV_ROUND_UP(nilfs_palloc_entries_per_group(inode
),
60 mi
->mi_entries_per_block
) + 1;
61 /* Number of blocks in a group including entry blocks and
63 mi
->mi_blocks_per_desc_block
=
64 nilfs_palloc_groups_per_desc_block(inode
) *
65 mi
->mi_blocks_per_group
+ 1;
66 /* Number of blocks per descriptor including the
71 static unsigned long nilfs_palloc_group(const struct inode
*inode
, __u64 nr
,
72 unsigned long *offset
)
76 *offset
= do_div(group
, nilfs_palloc_entries_per_group(inode
));
81 nilfs_palloc_desc_blkoff(const struct inode
*inode
, unsigned long group
)
83 unsigned long desc_block
=
84 group
/ nilfs_palloc_groups_per_desc_block(inode
);
85 return desc_block
* NILFS_MDT(inode
)->mi_blocks_per_desc_block
;
89 nilfs_palloc_bitmap_blkoff(const struct inode
*inode
, unsigned long group
)
91 unsigned long desc_offset
=
92 group
% nilfs_palloc_groups_per_desc_block(inode
);
93 return nilfs_palloc_desc_blkoff(inode
, group
) + 1 +
94 desc_offset
* NILFS_MDT(inode
)->mi_blocks_per_group
;
98 nilfs_palloc_group_desc_nfrees(struct inode
*inode
, unsigned long group
,
99 const struct nilfs_palloc_group_desc
*desc
)
103 spin_lock(nilfs_mdt_bgl_lock(inode
, group
));
104 nfree
= le32_to_cpu(desc
->pg_nfrees
);
105 spin_unlock(nilfs_mdt_bgl_lock(inode
, group
));
110 nilfs_palloc_group_desc_add_entries(struct inode
*inode
,
112 struct nilfs_palloc_group_desc
*desc
,
115 spin_lock(nilfs_mdt_bgl_lock(inode
, group
));
116 le32_add_cpu(&desc
->pg_nfrees
, n
);
117 spin_unlock(nilfs_mdt_bgl_lock(inode
, group
));
121 nilfs_palloc_entry_blkoff(const struct inode
*inode
, __u64 nr
)
123 unsigned long group
, group_offset
;
125 group
= nilfs_palloc_group(inode
, nr
, &group_offset
);
127 return nilfs_palloc_bitmap_blkoff(inode
, group
) + 1 +
128 group_offset
/ NILFS_MDT(inode
)->mi_entries_per_block
;
131 static void nilfs_palloc_desc_block_init(struct inode
*inode
,
132 struct buffer_head
*bh
, void *kaddr
)
134 struct nilfs_palloc_group_desc
*desc
= kaddr
+ bh_offset(bh
);
135 unsigned long n
= nilfs_palloc_groups_per_desc_block(inode
);
138 nfrees
= cpu_to_le32(nilfs_palloc_entries_per_group(inode
));
140 desc
->pg_nfrees
= nfrees
;
145 static int nilfs_palloc_get_desc_block(struct inode
*inode
,
147 int create
, struct buffer_head
**bhp
)
149 return nilfs_mdt_get_block(inode
,
150 nilfs_palloc_desc_blkoff(inode
, group
),
151 create
, nilfs_palloc_desc_block_init
, bhp
);
154 static int nilfs_palloc_get_bitmap_block(struct inode
*inode
,
156 int create
, struct buffer_head
**bhp
)
158 return nilfs_mdt_get_block(inode
,
159 nilfs_palloc_bitmap_blkoff(inode
, group
),
163 int nilfs_palloc_get_entry_block(struct inode
*inode
, __u64 nr
,
164 int create
, struct buffer_head
**bhp
)
166 return nilfs_mdt_get_block(inode
, nilfs_palloc_entry_blkoff(inode
, nr
),
170 static struct nilfs_palloc_group_desc
*
171 nilfs_palloc_block_get_group_desc(const struct inode
*inode
,
173 const struct buffer_head
*bh
, void *kaddr
)
175 return (struct nilfs_palloc_group_desc
*)(kaddr
+ bh_offset(bh
)) +
176 group
% nilfs_palloc_groups_per_desc_block(inode
);
179 void *nilfs_palloc_block_get_entry(const struct inode
*inode
, __u64 nr
,
180 const struct buffer_head
*bh
, void *kaddr
)
182 unsigned long entry_offset
, group_offset
;
184 nilfs_palloc_group(inode
, nr
, &group_offset
);
185 entry_offset
= group_offset
% NILFS_MDT(inode
)->mi_entries_per_block
;
187 return kaddr
+ bh_offset(bh
) +
188 entry_offset
* NILFS_MDT(inode
)->mi_entry_size
;
191 static int nilfs_palloc_find_available_slot(struct inode
*inode
,
193 unsigned long target
,
194 unsigned char *bitmap
,
195 int bsize
) /* size in bits */
197 int curr
, pos
, end
, i
;
200 end
= (target
+ BITS_PER_LONG
- 1) & ~(BITS_PER_LONG
- 1);
203 pos
= nilfs_find_next_zero_bit(bitmap
, end
, target
);
205 !nilfs_set_bit_atomic(
206 nilfs_mdt_bgl_lock(inode
, group
), pos
, bitmap
))
211 for (i
= 0, curr
= end
;
213 i
+= BITS_PER_LONG
, curr
+= BITS_PER_LONG
) {
217 while (*((unsigned long *)bitmap
+ curr
/ BITS_PER_LONG
)
219 end
= curr
+ BITS_PER_LONG
;
222 pos
= nilfs_find_next_zero_bit(bitmap
, end
, curr
);
224 !nilfs_set_bit_atomic(
225 nilfs_mdt_bgl_lock(inode
, group
), pos
,
234 nilfs_palloc_rest_groups_in_desc_block(const struct inode
*inode
,
235 unsigned long curr
, unsigned long max
)
237 return min_t(unsigned long,
238 nilfs_palloc_groups_per_desc_block(inode
) -
239 curr
% nilfs_palloc_groups_per_desc_block(inode
),
243 int nilfs_palloc_prepare_alloc_entry(struct inode
*inode
,
244 struct nilfs_palloc_req
*req
)
246 struct buffer_head
*desc_bh
, *bitmap_bh
;
247 struct nilfs_palloc_group_desc
*desc
;
248 unsigned char *bitmap
;
249 void *desc_kaddr
, *bitmap_kaddr
;
250 unsigned long group
, maxgroup
, ngroups
;
251 unsigned long group_offset
, maxgroup_offset
;
252 unsigned long n
, entries_per_group
, groups_per_desc_block
;
256 ngroups
= nilfs_palloc_groups_count(inode
);
257 maxgroup
= ngroups
- 1;
258 group
= nilfs_palloc_group(inode
, req
->pr_entry_nr
, &group_offset
);
259 entries_per_group
= nilfs_palloc_entries_per_group(inode
);
260 groups_per_desc_block
= nilfs_palloc_groups_per_desc_block(inode
);
262 for (i
= 0; i
< ngroups
; i
+= n
) {
263 if (group
>= ngroups
) {
266 maxgroup
= nilfs_palloc_group(inode
, req
->pr_entry_nr
,
267 &maxgroup_offset
) - 1;
269 ret
= nilfs_palloc_get_desc_block(inode
, group
, 1, &desc_bh
);
272 desc_kaddr
= kmap(desc_bh
->b_page
);
273 desc
= nilfs_palloc_block_get_group_desc(
274 inode
, group
, desc_bh
, desc_kaddr
);
275 n
= nilfs_palloc_rest_groups_in_desc_block(inode
, group
,
277 for (j
= 0; j
< n
; j
++, desc
++, group
++) {
278 if (nilfs_palloc_group_desc_nfrees(inode
, group
, desc
)
280 ret
= nilfs_palloc_get_bitmap_block(
281 inode
, group
, 1, &bitmap_bh
);
284 bitmap_kaddr
= kmap(bitmap_bh
->b_page
);
285 bitmap
= bitmap_kaddr
+ bh_offset(bitmap_bh
);
286 pos
= nilfs_palloc_find_available_slot(
287 inode
, group
, group_offset
, bitmap
,
290 /* found a free entry */
291 nilfs_palloc_group_desc_add_entries(
292 inode
, group
, desc
, -1);
294 entries_per_group
* group
+ pos
;
295 kunmap(desc_bh
->b_page
);
296 kunmap(bitmap_bh
->b_page
);
298 req
->pr_desc_bh
= desc_bh
;
299 req
->pr_bitmap_bh
= bitmap_bh
;
302 kunmap(bitmap_bh
->b_page
);
309 kunmap(desc_bh
->b_page
);
313 /* no entries left */
317 kunmap(desc_bh
->b_page
);
322 void nilfs_palloc_commit_alloc_entry(struct inode
*inode
,
323 struct nilfs_palloc_req
*req
)
325 nilfs_mdt_mark_buffer_dirty(req
->pr_bitmap_bh
);
326 nilfs_mdt_mark_buffer_dirty(req
->pr_desc_bh
);
327 nilfs_mdt_mark_dirty(inode
);
329 brelse(req
->pr_bitmap_bh
);
330 brelse(req
->pr_desc_bh
);
333 void nilfs_palloc_commit_free_entry(struct inode
*inode
,
334 struct nilfs_palloc_req
*req
)
336 struct nilfs_palloc_group_desc
*desc
;
337 unsigned long group
, group_offset
;
338 unsigned char *bitmap
;
339 void *desc_kaddr
, *bitmap_kaddr
;
341 group
= nilfs_palloc_group(inode
, req
->pr_entry_nr
, &group_offset
);
342 desc_kaddr
= kmap(req
->pr_desc_bh
->b_page
);
343 desc
= nilfs_palloc_block_get_group_desc(inode
, group
,
344 req
->pr_desc_bh
, desc_kaddr
);
345 bitmap_kaddr
= kmap(req
->pr_bitmap_bh
->b_page
);
346 bitmap
= bitmap_kaddr
+ bh_offset(req
->pr_bitmap_bh
);
348 if (!nilfs_clear_bit_atomic(nilfs_mdt_bgl_lock(inode
, group
),
349 group_offset
, bitmap
))
350 printk(KERN_WARNING
"%s: entry number %llu already freed\n",
351 __func__
, (unsigned long long)req
->pr_entry_nr
);
353 nilfs_palloc_group_desc_add_entries(inode
, group
, desc
, 1);
355 kunmap(req
->pr_bitmap_bh
->b_page
);
356 kunmap(req
->pr_desc_bh
->b_page
);
358 nilfs_mdt_mark_buffer_dirty(req
->pr_desc_bh
);
359 nilfs_mdt_mark_buffer_dirty(req
->pr_bitmap_bh
);
360 nilfs_mdt_mark_dirty(inode
);
362 brelse(req
->pr_bitmap_bh
);
363 brelse(req
->pr_desc_bh
);
366 void nilfs_palloc_abort_alloc_entry(struct inode
*inode
,
367 struct nilfs_palloc_req
*req
)
369 struct nilfs_palloc_group_desc
*desc
;
370 void *desc_kaddr
, *bitmap_kaddr
;
371 unsigned char *bitmap
;
372 unsigned long group
, group_offset
;
374 group
= nilfs_palloc_group(inode
, req
->pr_entry_nr
, &group_offset
);
375 desc_kaddr
= kmap(req
->pr_desc_bh
->b_page
);
376 desc
= nilfs_palloc_block_get_group_desc(inode
, group
,
377 req
->pr_desc_bh
, desc_kaddr
);
378 bitmap_kaddr
= kmap(req
->pr_bitmap_bh
->b_page
);
379 bitmap
= bitmap_kaddr
+ bh_offset(req
->pr_bitmap_bh
);
380 if (!nilfs_clear_bit_atomic(nilfs_mdt_bgl_lock(inode
, group
),
381 group_offset
, bitmap
))
382 printk(KERN_WARNING
"%s: entry numer %llu already freed\n",
383 __func__
, (unsigned long long)req
->pr_entry_nr
);
385 nilfs_palloc_group_desc_add_entries(inode
, group
, desc
, 1);
387 kunmap(req
->pr_bitmap_bh
->b_page
);
388 kunmap(req
->pr_desc_bh
->b_page
);
390 brelse(req
->pr_bitmap_bh
);
391 brelse(req
->pr_desc_bh
);
393 req
->pr_entry_nr
= 0;
394 req
->pr_bitmap_bh
= NULL
;
395 req
->pr_desc_bh
= NULL
;
398 int nilfs_palloc_prepare_free_entry(struct inode
*inode
,
399 struct nilfs_palloc_req
*req
)
401 struct buffer_head
*desc_bh
, *bitmap_bh
;
402 unsigned long group
, group_offset
;
405 group
= nilfs_palloc_group(inode
, req
->pr_entry_nr
, &group_offset
);
406 ret
= nilfs_palloc_get_desc_block(inode
, group
, 1, &desc_bh
);
409 ret
= nilfs_palloc_get_bitmap_block(inode
, group
, 1, &bitmap_bh
);
415 req
->pr_desc_bh
= desc_bh
;
416 req
->pr_bitmap_bh
= bitmap_bh
;
420 void nilfs_palloc_abort_free_entry(struct inode
*inode
,
421 struct nilfs_palloc_req
*req
)
423 brelse(req
->pr_bitmap_bh
);
424 brelse(req
->pr_desc_bh
);
426 req
->pr_entry_nr
= 0;
427 req
->pr_bitmap_bh
= NULL
;
428 req
->pr_desc_bh
= NULL
;
432 nilfs_palloc_group_is_in(struct inode
*inode
, unsigned long group
, __u64 nr
)
436 first
= group
* nilfs_palloc_entries_per_group(inode
);
437 last
= first
+ nilfs_palloc_entries_per_group(inode
) - 1;
438 return (nr
>= first
) && (nr
<= last
);
441 int nilfs_palloc_freev(struct inode
*inode
, __u64
*entry_nrs
, size_t nitems
)
443 struct buffer_head
*desc_bh
, *bitmap_bh
;
444 struct nilfs_palloc_group_desc
*desc
;
445 unsigned char *bitmap
;
446 void *desc_kaddr
, *bitmap_kaddr
;
447 unsigned long group
, group_offset
;
450 for (i
= 0; i
< nitems
; i
+= n
) {
451 group
= nilfs_palloc_group(inode
, entry_nrs
[i
], &group_offset
);
452 ret
= nilfs_palloc_get_desc_block(inode
, group
, 0, &desc_bh
);
455 ret
= nilfs_palloc_get_bitmap_block(inode
, group
, 0,
461 desc_kaddr
= kmap(desc_bh
->b_page
);
462 desc
= nilfs_palloc_block_get_group_desc(
463 inode
, group
, desc_bh
, desc_kaddr
);
464 bitmap_kaddr
= kmap(bitmap_bh
->b_page
);
465 bitmap
= bitmap_kaddr
+ bh_offset(bitmap_bh
);
467 (j
< nitems
) && nilfs_palloc_group_is_in(inode
, group
,
470 nilfs_palloc_group(inode
, entry_nrs
[j
], &group_offset
);
471 if (!nilfs_clear_bit_atomic(
472 nilfs_mdt_bgl_lock(inode
, group
),
473 group_offset
, bitmap
)) {
475 "%s: entry number %llu already freed\n",
477 (unsigned long long)entry_nrs
[j
]);
480 nilfs_palloc_group_desc_add_entries(inode
, group
, desc
, n
);
482 kunmap(bitmap_bh
->b_page
);
483 kunmap(desc_bh
->b_page
);
485 nilfs_mdt_mark_buffer_dirty(desc_bh
);
486 nilfs_mdt_mark_buffer_dirty(bitmap_bh
);
487 nilfs_mdt_mark_dirty(inode
);