Commit | Line | Data |
---|---|---|
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 |
39 | static inline unsigned long |
40 | nilfs_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 |
50 | static inline unsigned long |
51 | nilfs_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 |
61 | int 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 |
92 | static 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 |
109 | static unsigned long |
110 | nilfs_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 |
125 | static unsigned long |
126 | nilfs_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 | 139 | static unsigned long |
4e9e63a6 RK |
140 | nilfs_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 | 157 | static void |
4e9e63a6 RK |
158 | nilfs_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 |
171 | static unsigned long |
172 | nilfs_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 |
188 | static 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 |
202 | static 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 |
245 | static 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 |
264 | static 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 |
283 | int 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 |
301 | static struct nilfs_palloc_group_desc * |
302 | nilfs_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 |
317 | void *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 | 336 | static int nilfs_palloc_find_available_slot(unsigned char *bitmap, |
5442680f | 337 | unsigned long target, |
18c41b37 | 338 | unsigned bsize, |
4e9e63a6 | 339 | spinlock_t *lock) |
5442680f | 340 | { |
18c41b37 RK |
341 | int pos, end = bsize; |
342 | ||
343 | 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)) | |
350 | return pos; | |
351 | } while (++pos < end); | |
352 | ||
353 | end = target; | |
4e9e63a6 | 354 | } |
5442680f | 355 | |
18c41b37 RK |
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; | |
5442680f | 363 | } |
18c41b37 | 364 | |
5442680f RK |
365 | return -ENOSPC; |
366 | } | |
367 | ||
db55d922 RK |
368 | /** |
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 | */ | |
5442680f RK |
375 | static unsigned long |
376 | nilfs_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 | ||
c7ef972c VD |
385 | /** |
386 | * 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 | */ | |
390 | static int nilfs_palloc_count_desc_blocks(struct inode *inode, | |
391 | unsigned long *desc_blocks) | |
392 | { | |
3568a13f | 393 | __u64 blknum; |
c7ef972c VD |
394 | 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( | |
3568a13f RK |
399 | (unsigned long)blknum, |
400 | NILFS_MDT(inode)->mi_blocks_per_desc_block); | |
c7ef972c VD |
401 | 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 | */ | |
410 | static 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 | */ | |
424 | int 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 | ||
db55d922 RK |
449 | /** |
450 | * 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 | */ | |
5442680f RK |
454 | int 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; | |
4e9e63a6 | 465 | spinlock_t *lock; |
5442680f RK |
466 | 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++) { | |
4e9e63a6 RK |
490 | lock = nilfs_mdt_bgl_lock(inode, group); |
491 | if (nilfs_palloc_group_desc_nfrees(desc, lock) > 0) { | |
5442680f RK |
492 | 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); | |
141bbdba | 497 | bitmap = bitmap_kaddr + bh_offset(bitmap_bh); |
5442680f | 498 | pos = nilfs_palloc_find_available_slot( |
4e9e63a6 RK |
499 | bitmap, group_offset, |
500 | entries_per_group, lock); | |
5442680f RK |
501 | if (pos >= 0) { |
502 | /* found a free entry */ | |
503 | nilfs_palloc_group_desc_add_entries( | |
4e9e63a6 | 504 | desc, lock, -1); |
5442680f RK |
505 | 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 | ||
db55d922 RK |
534 | /** |
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 | */ | |
5442680f RK |
539 | void nilfs_palloc_commit_alloc_entry(struct inode *inode, |
540 | struct nilfs_palloc_req *req) | |
541 | { | |
5fc7b141 RK |
542 | mark_buffer_dirty(req->pr_bitmap_bh); |
543 | mark_buffer_dirty(req->pr_desc_bh); | |
5442680f RK |
544 | nilfs_mdt_mark_dirty(inode); |
545 | ||
546 | brelse(req->pr_bitmap_bh); | |
547 | brelse(req->pr_desc_bh); | |
548 | } | |
549 | ||
db55d922 RK |
550 | /** |
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 | */ | |
5442680f RK |
555 | void 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; | |
4e9e63a6 | 562 | spinlock_t *lock; |
5442680f RK |
563 | |
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); | |
141bbdba | 569 | bitmap = bitmap_kaddr + bh_offset(req->pr_bitmap_bh); |
4e9e63a6 | 570 | lock = nilfs_mdt_bgl_lock(inode, group); |
5442680f | 571 | |
4e9e63a6 | 572 | if (!nilfs_clear_bit_atomic(lock, group_offset, bitmap)) |
b7bed712 RK |
573 | 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); | |
9954e7af | 577 | else |
4e9e63a6 | 578 | nilfs_palloc_group_desc_add_entries(desc, lock, 1); |
5442680f RK |
579 | |
580 | kunmap(req->pr_bitmap_bh->b_page); | |
581 | kunmap(req->pr_desc_bh->b_page); | |
582 | ||
5fc7b141 RK |
583 | mark_buffer_dirty(req->pr_desc_bh); |
584 | mark_buffer_dirty(req->pr_bitmap_bh); | |
5442680f RK |
585 | nilfs_mdt_mark_dirty(inode); |
586 | ||
587 | brelse(req->pr_bitmap_bh); | |
588 | brelse(req->pr_desc_bh); | |
589 | } | |
590 | ||
db55d922 RK |
591 | /** |
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 | */ | |
5442680f RK |
596 | void 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; | |
4e9e63a6 | 603 | spinlock_t *lock; |
5442680f RK |
604 | |
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); | |
141bbdba | 610 | bitmap = bitmap_kaddr + bh_offset(req->pr_bitmap_bh); |
4e9e63a6 RK |
611 | lock = nilfs_mdt_bgl_lock(inode, group); |
612 | ||
613 | if (!nilfs_clear_bit_atomic(lock, group_offset, bitmap)) | |
b7bed712 RK |
614 | 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); | |
9954e7af | 618 | else |
4e9e63a6 | 619 | nilfs_palloc_group_desc_add_entries(desc, lock, 1); |
5442680f RK |
620 | |
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 | ||
db55d922 RK |
632 | /** |
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 | */ | |
5442680f RK |
637 | int 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 | ||
db55d922 RK |
659 | /** |
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 | */ | |
5442680f RK |
664 | void 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 | ||
db55d922 RK |
675 | /** |
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 | */ | |
5442680f RK |
681 | static int |
682 | nilfs_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 | ||
db55d922 RK |
691 | /** |
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 | */ | |
5442680f RK |
697 | int 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; | |
4e9e63a6 | 704 | spinlock_t *lock; |
5442680f RK |
705 | int i, j, n, ret; |
706 | ||
349dbc36 | 707 | for (i = 0; i < nitems; i = j) { |
5442680f RK |
708 | 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); | |
141bbdba | 722 | bitmap = bitmap_kaddr + bh_offset(bitmap_bh); |
4e9e63a6 | 723 | lock = nilfs_mdt_bgl_lock(inode, group); |
5442680f RK |
724 | for (j = i, n = 0; |
725 | (j < nitems) && nilfs_palloc_group_is_in(inode, group, | |
726 | entry_nrs[j]); | |
9954e7af | 727 | j++) { |
5442680f | 728 | nilfs_palloc_group(inode, entry_nrs[j], &group_offset); |
4e9e63a6 RK |
729 | if (!nilfs_clear_bit_atomic(lock, group_offset, |
730 | bitmap)) { | |
b7bed712 RK |
731 | 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); | |
9954e7af RK |
735 | } else { |
736 | n++; | |
5442680f RK |
737 | } |
738 | } | |
4e9e63a6 | 739 | nilfs_palloc_group_desc_add_entries(desc, lock, n); |
5442680f RK |
740 | |
741 | kunmap(bitmap_bh->b_page); | |
742 | kunmap(desc_bh->b_page); | |
743 | ||
5fc7b141 RK |
744 | mark_buffer_dirty(desc_bh); |
745 | mark_buffer_dirty(bitmap_bh); | |
5442680f RK |
746 | nilfs_mdt_mark_dirty(inode); |
747 | ||
748 | brelse(bitmap_bh); | |
749 | brelse(desc_bh); | |
750 | } | |
751 | return 0; | |
752 | } | |
db38d5ad RK |
753 | |
754 | void 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 | ||
761 | void 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 | ||
775 | void nilfs_palloc_destroy_cache(struct inode *inode) | |
776 | { | |
777 | nilfs_palloc_clear_cache(inode); | |
778 | NILFS_MDT(inode)->mi_palloc_cache = NULL; | |
779 | } |