blob: 698f582d69afa28360aad32584e3fe87a03de2d5 [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 *
Ryusuke Konishi4b420ab2016-05-23 16:23:09 -070016 * Originally written by Koji Sato.
17 * Two allocators were unified by Ryusuke Konishi and Amagai Yoshiji.
Ryusuke Konishi54426802009-04-06 19:01:29 -070018 */
19
20#include <linux/types.h>
21#include <linux/buffer_head.h>
22#include <linux/fs.h>
23#include <linux/bitops.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Ryusuke Konishi54426802009-04-06 19:01:29 -070025#include "mdt.h"
26#include "alloc.h"
27
28
Ryusuke Konishidb55d922010-04-12 01:46:02 +090029/**
30 * nilfs_palloc_groups_per_desc_block - get the number of groups that a group
31 * descriptor block can maintain
32 * @inode: inode of metadata file using this allocator
33 */
Ryusuke Konishi54426802009-04-06 19:01:29 -070034static inline unsigned long
35nilfs_palloc_groups_per_desc_block(const struct inode *inode)
36{
37 return (1UL << inode->i_blkbits) /
38 sizeof(struct nilfs_palloc_group_desc);
39}
40
Ryusuke Konishidb55d922010-04-12 01:46:02 +090041/**
42 * nilfs_palloc_groups_count - get maximum number of groups
43 * @inode: inode of metadata file using this allocator
44 */
Ryusuke Konishi54426802009-04-06 19:01:29 -070045static inline unsigned long
46nilfs_palloc_groups_count(const struct inode *inode)
47{
48 return 1UL << (BITS_PER_LONG - (inode->i_blkbits + 3 /* log2(8) */));
49}
50
Ryusuke Konishidb55d922010-04-12 01:46:02 +090051/**
52 * nilfs_palloc_init_blockgroup - initialize private variables for allocator
53 * @inode: inode of metadata file using this allocator
54 * @entry_size: size of the persistent object
55 */
Ryusuke Konishi0c6c44c2016-05-23 16:23:39 -070056int nilfs_palloc_init_blockgroup(struct inode *inode, unsigned int entry_size)
Ryusuke Konishi54426802009-04-06 19:01:29 -070057{
58 struct nilfs_mdt_info *mi = NILFS_MDT(inode);
59
60 mi->mi_bgl = kmalloc(sizeof(*mi->mi_bgl), GFP_NOFS);
61 if (!mi->mi_bgl)
62 return -ENOMEM;
63
64 bgl_lock_init(mi->mi_bgl);
65
66 nilfs_mdt_set_entry_size(inode, entry_size, 0);
67
68 mi->mi_blocks_per_group =
69 DIV_ROUND_UP(nilfs_palloc_entries_per_group(inode),
70 mi->mi_entries_per_block) + 1;
71 /* Number of blocks in a group including entry blocks and
72 a bitmap block */
73 mi->mi_blocks_per_desc_block =
74 nilfs_palloc_groups_per_desc_block(inode) *
75 mi->mi_blocks_per_group + 1;
76 /* Number of blocks per descriptor including the
77 descriptor block */
78 return 0;
79}
80
Ryusuke Konishidb55d922010-04-12 01:46:02 +090081/**
82 * nilfs_palloc_group - get group number and offset from an entry number
83 * @inode: inode of metadata file using this allocator
84 * @nr: serial number of the entry (e.g. inode number)
85 * @offset: pointer to store offset number in the group
86 */
Ryusuke Konishi54426802009-04-06 19:01:29 -070087static unsigned long nilfs_palloc_group(const struct inode *inode, __u64 nr,
88 unsigned long *offset)
89{
90 __u64 group = nr;
91
92 *offset = do_div(group, nilfs_palloc_entries_per_group(inode));
93 return group;
94}
95
Ryusuke Konishidb55d922010-04-12 01:46:02 +090096/**
97 * nilfs_palloc_desc_blkoff - get block offset of a group descriptor block
98 * @inode: inode of metadata file using this allocator
99 * @group: group number
100 *
101 * nilfs_palloc_desc_blkoff() returns block offset of the descriptor
102 * block which contains a descriptor of the specified group.
103 */
Ryusuke Konishi54426802009-04-06 19:01:29 -0700104static unsigned long
105nilfs_palloc_desc_blkoff(const struct inode *inode, unsigned long group)
106{
107 unsigned long desc_block =
108 group / nilfs_palloc_groups_per_desc_block(inode);
109 return desc_block * NILFS_MDT(inode)->mi_blocks_per_desc_block;
110}
111
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900112/**
113 * nilfs_palloc_bitmap_blkoff - get block offset of a bitmap block
114 * @inode: inode of metadata file using this allocator
115 * @group: group number
116 *
117 * nilfs_palloc_bitmap_blkoff() returns block offset of the bitmap
118 * block used to allocate/deallocate entries in the specified group.
119 */
Ryusuke Konishi54426802009-04-06 19:01:29 -0700120static unsigned long
121nilfs_palloc_bitmap_blkoff(const struct inode *inode, unsigned long group)
122{
123 unsigned long desc_offset =
124 group % nilfs_palloc_groups_per_desc_block(inode);
125 return nilfs_palloc_desc_blkoff(inode, group) + 1 +
126 desc_offset * NILFS_MDT(inode)->mi_blocks_per_group;
127}
128
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900129/**
130 * nilfs_palloc_group_desc_nfrees - get the number of free entries in a group
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900131 * @desc: pointer to descriptor structure for the group
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800132 * @lock: spin lock protecting @desc
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900133 */
Ryusuke Konishi54426802009-04-06 19:01:29 -0700134static unsigned long
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800135nilfs_palloc_group_desc_nfrees(const struct nilfs_palloc_group_desc *desc,
136 spinlock_t *lock)
Ryusuke Konishi54426802009-04-06 19:01:29 -0700137{
138 unsigned long nfree;
139
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800140 spin_lock(lock);
Ryusuke Konishi54426802009-04-06 19:01:29 -0700141 nfree = le32_to_cpu(desc->pg_nfrees);
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800142 spin_unlock(lock);
Ryusuke Konishi54426802009-04-06 19:01:29 -0700143 return nfree;
144}
145
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900146/**
147 * nilfs_palloc_group_desc_add_entries - adjust count of free entries
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900148 * @desc: pointer to descriptor structure for the group
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800149 * @lock: spin lock protecting @desc
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900150 * @n: delta to be added
151 */
Ryusuke Konishid0c14a92015-11-06 16:31:56 -0800152static u32
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800153nilfs_palloc_group_desc_add_entries(struct nilfs_palloc_group_desc *desc,
154 spinlock_t *lock, u32 n)
Ryusuke Konishi54426802009-04-06 19:01:29 -0700155{
Ryusuke Konishid0c14a92015-11-06 16:31:56 -0800156 u32 nfree;
157
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800158 spin_lock(lock);
Ryusuke Konishi54426802009-04-06 19:01:29 -0700159 le32_add_cpu(&desc->pg_nfrees, n);
Ryusuke Konishid0c14a92015-11-06 16:31:56 -0800160 nfree = le32_to_cpu(desc->pg_nfrees);
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800161 spin_unlock(lock);
Ryusuke Konishid0c14a92015-11-06 16:31:56 -0800162 return nfree;
Ryusuke Konishi54426802009-04-06 19:01:29 -0700163}
164
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900165/**
166 * nilfs_palloc_entry_blkoff - get block offset of an entry block
167 * @inode: inode of metadata file using this allocator
168 * @nr: serial number of the entry (e.g. inode number)
169 */
Ryusuke Konishi54426802009-04-06 19:01:29 -0700170static unsigned long
171nilfs_palloc_entry_blkoff(const struct inode *inode, __u64 nr)
172{
173 unsigned long group, group_offset;
174
175 group = nilfs_palloc_group(inode, nr, &group_offset);
176
177 return nilfs_palloc_bitmap_blkoff(inode, group) + 1 +
178 group_offset / NILFS_MDT(inode)->mi_entries_per_block;
179}
180
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900181/**
182 * nilfs_palloc_desc_block_init - initialize buffer of a group descriptor block
183 * @inode: inode of metadata file
184 * @bh: buffer head of the buffer to be initialized
185 * @kaddr: kernel address mapped for the page including the buffer
186 */
Ryusuke Konishi54426802009-04-06 19:01:29 -0700187static void nilfs_palloc_desc_block_init(struct inode *inode,
188 struct buffer_head *bh, void *kaddr)
189{
190 struct nilfs_palloc_group_desc *desc = kaddr + bh_offset(bh);
191 unsigned long n = nilfs_palloc_groups_per_desc_block(inode);
192 __le32 nfrees;
193
194 nfrees = cpu_to_le32(nilfs_palloc_entries_per_group(inode));
195 while (n-- > 0) {
196 desc->pg_nfrees = nfrees;
197 desc++;
198 }
199}
200
Ryusuke Konishi70622a22009-11-14 18:40:27 +0900201static int nilfs_palloc_get_block(struct inode *inode, unsigned long blkoff,
202 int create,
203 void (*init_block)(struct inode *,
204 struct buffer_head *,
205 void *),
206 struct buffer_head **bhp,
207 struct nilfs_bh_assoc *prev,
208 spinlock_t *lock)
209{
210 int ret;
211
212 spin_lock(lock);
213 if (prev->bh && blkoff == prev->blkoff) {
214 get_bh(prev->bh);
215 *bhp = prev->bh;
216 spin_unlock(lock);
217 return 0;
218 }
219 spin_unlock(lock);
220
221 ret = nilfs_mdt_get_block(inode, blkoff, create, init_block, bhp);
222 if (!ret) {
223 spin_lock(lock);
224 /*
225 * The following code must be safe for change of the
226 * cache contents during the get block call.
227 */
228 brelse(prev->bh);
229 get_bh(*bhp);
230 prev->bh = *bhp;
231 prev->blkoff = blkoff;
232 spin_unlock(lock);
233 }
234 return ret;
235}
236
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900237/**
Ryusuke Konishida019952015-11-06 16:31:54 -0800238 * nilfs_palloc_delete_block - delete a block on the persistent allocator file
239 * @inode: inode of metadata file using this allocator
240 * @blkoff: block offset
241 * @prev: nilfs_bh_assoc struct of the last used buffer
242 * @lock: spin lock protecting @prev
243 */
244static int nilfs_palloc_delete_block(struct inode *inode, unsigned long blkoff,
245 struct nilfs_bh_assoc *prev,
246 spinlock_t *lock)
247{
248 spin_lock(lock);
249 if (prev->bh && blkoff == prev->blkoff) {
250 brelse(prev->bh);
251 prev->bh = NULL;
252 }
253 spin_unlock(lock);
254 return nilfs_mdt_delete_block(inode, blkoff);
255}
256
257/**
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900258 * nilfs_palloc_get_desc_block - get buffer head of a group descriptor block
259 * @inode: inode of metadata file using this allocator
260 * @group: group number
261 * @create: create flag
262 * @bhp: pointer to store the resultant buffer head
263 */
Ryusuke Konishi54426802009-04-06 19:01:29 -0700264static int nilfs_palloc_get_desc_block(struct inode *inode,
265 unsigned long group,
266 int create, struct buffer_head **bhp)
267{
Ryusuke Konishi70622a22009-11-14 18:40:27 +0900268 struct nilfs_palloc_cache *cache = NILFS_MDT(inode)->mi_palloc_cache;
269
270 return nilfs_palloc_get_block(inode,
271 nilfs_palloc_desc_blkoff(inode, group),
272 create, nilfs_palloc_desc_block_init,
273 bhp, &cache->prev_desc, &cache->lock);
Ryusuke Konishi54426802009-04-06 19:01:29 -0700274}
275
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900276/**
277 * nilfs_palloc_get_bitmap_block - get buffer head of a bitmap block
278 * @inode: inode of metadata file using this allocator
279 * @group: group number
280 * @create: create flag
281 * @bhp: pointer to store the resultant buffer head
282 */
Ryusuke Konishi54426802009-04-06 19:01:29 -0700283static int nilfs_palloc_get_bitmap_block(struct inode *inode,
284 unsigned long group,
285 int create, struct buffer_head **bhp)
286{
Ryusuke Konishi70622a22009-11-14 18:40:27 +0900287 struct nilfs_palloc_cache *cache = NILFS_MDT(inode)->mi_palloc_cache;
288
289 return nilfs_palloc_get_block(inode,
290 nilfs_palloc_bitmap_blkoff(inode, group),
291 create, NULL, bhp,
292 &cache->prev_bitmap, &cache->lock);
Ryusuke Konishi54426802009-04-06 19:01:29 -0700293}
294
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900295/**
Ryusuke Konishida019952015-11-06 16:31:54 -0800296 * nilfs_palloc_delete_bitmap_block - delete a bitmap block
297 * @inode: inode of metadata file using this allocator
298 * @group: group number
299 */
300static int nilfs_palloc_delete_bitmap_block(struct inode *inode,
301 unsigned long group)
302{
303 struct nilfs_palloc_cache *cache = NILFS_MDT(inode)->mi_palloc_cache;
304
305 return nilfs_palloc_delete_block(inode,
306 nilfs_palloc_bitmap_blkoff(inode,
307 group),
308 &cache->prev_bitmap, &cache->lock);
309}
310
311/**
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900312 * nilfs_palloc_get_entry_block - get buffer head of an entry block
313 * @inode: inode of metadata file using this allocator
314 * @nr: serial number of the entry (e.g. inode number)
315 * @create: create flag
316 * @bhp: pointer to store the resultant buffer head
317 */
Ryusuke Konishi54426802009-04-06 19:01:29 -0700318int nilfs_palloc_get_entry_block(struct inode *inode, __u64 nr,
319 int create, struct buffer_head **bhp)
320{
Ryusuke Konishi70622a22009-11-14 18:40:27 +0900321 struct nilfs_palloc_cache *cache = NILFS_MDT(inode)->mi_palloc_cache;
322
323 return nilfs_palloc_get_block(inode,
324 nilfs_palloc_entry_blkoff(inode, nr),
325 create, NULL, bhp,
326 &cache->prev_entry, &cache->lock);
Ryusuke Konishi54426802009-04-06 19:01:29 -0700327}
328
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900329/**
Ryusuke Konishida019952015-11-06 16:31:54 -0800330 * nilfs_palloc_delete_entry_block - delete an entry block
331 * @inode: inode of metadata file using this allocator
332 * @nr: serial number of the entry
333 */
334static int nilfs_palloc_delete_entry_block(struct inode *inode, __u64 nr)
335{
336 struct nilfs_palloc_cache *cache = NILFS_MDT(inode)->mi_palloc_cache;
337
338 return nilfs_palloc_delete_block(inode,
339 nilfs_palloc_entry_blkoff(inode, nr),
340 &cache->prev_entry, &cache->lock);
341}
342
343/**
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900344 * nilfs_palloc_block_get_group_desc - get kernel address of a group descriptor
345 * @inode: inode of metadata file using this allocator
346 * @group: group number
347 * @bh: buffer head of the buffer storing the group descriptor block
348 * @kaddr: kernel address mapped for the page including the buffer
349 */
Ryusuke Konishi54426802009-04-06 19:01:29 -0700350static struct nilfs_palloc_group_desc *
351nilfs_palloc_block_get_group_desc(const struct inode *inode,
352 unsigned long group,
353 const struct buffer_head *bh, void *kaddr)
354{
355 return (struct nilfs_palloc_group_desc *)(kaddr + bh_offset(bh)) +
356 group % nilfs_palloc_groups_per_desc_block(inode);
357}
358
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900359/**
360 * nilfs_palloc_block_get_entry - get kernel address of an entry
361 * @inode: inode of metadata file using this allocator
362 * @nr: serial number of the entry (e.g. inode number)
363 * @bh: buffer head of the buffer storing the entry block
364 * @kaddr: kernel address mapped for the page including the buffer
365 */
Ryusuke Konishi54426802009-04-06 19:01:29 -0700366void *nilfs_palloc_block_get_entry(const struct inode *inode, __u64 nr,
367 const struct buffer_head *bh, void *kaddr)
368{
369 unsigned long entry_offset, group_offset;
370
371 nilfs_palloc_group(inode, nr, &group_offset);
372 entry_offset = group_offset % NILFS_MDT(inode)->mi_entries_per_block;
373
374 return kaddr + bh_offset(bh) +
375 entry_offset * NILFS_MDT(inode)->mi_entry_size;
376}
377
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900378/**
379 * nilfs_palloc_find_available_slot - find available slot in a group
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900380 * @bitmap: bitmap of the group
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800381 * @target: offset number of an entry in the group (start point)
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900382 * @bsize: size in bits
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800383 * @lock: spin lock protecting @bitmap
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900384 */
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800385static int nilfs_palloc_find_available_slot(unsigned char *bitmap,
Ryusuke Konishi54426802009-04-06 19:01:29 -0700386 unsigned long target,
Ryusuke Konishi0c6c44c2016-05-23 16:23:39 -0700387 unsigned int bsize,
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800388 spinlock_t *lock)
Ryusuke Konishi54426802009-04-06 19:01:29 -0700389{
Ryusuke Konishi18c41b32015-11-06 16:31:48 -0800390 int pos, end = bsize;
Ryusuke Konishi54426802009-04-06 19:01:29 -0700391
Ryusuke Konishi18c41b32015-11-06 16:31:48 -0800392 if (likely(target < bsize)) {
393 pos = target;
394 do {
395 pos = nilfs_find_next_zero_bit(bitmap, end, pos);
396 if (pos >= end)
397 break;
398 if (!nilfs_set_bit_atomic(lock, pos, bitmap))
Ryusuke Konishi54426802009-04-06 19:01:29 -0700399 return pos;
Ryusuke Konishi18c41b32015-11-06 16:31:48 -0800400 } while (++pos < end);
401
402 end = target;
Ryusuke Konishi54426802009-04-06 19:01:29 -0700403 }
Ryusuke Konishi18c41b32015-11-06 16:31:48 -0800404
405 /* wrap around */
406 for (pos = 0; pos < end; pos++) {
407 pos = nilfs_find_next_zero_bit(bitmap, end, pos);
408 if (pos >= end)
409 break;
410 if (!nilfs_set_bit_atomic(lock, pos, bitmap))
411 return pos;
412 }
413
Ryusuke Konishi54426802009-04-06 19:01:29 -0700414 return -ENOSPC;
415}
416
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900417/**
418 * nilfs_palloc_rest_groups_in_desc_block - get the remaining number of groups
419 * in a group descriptor block
420 * @inode: inode of metadata file using this allocator
421 * @curr: current group number
422 * @max: maximum number of groups
423 */
Ryusuke Konishi54426802009-04-06 19:01:29 -0700424static unsigned long
425nilfs_palloc_rest_groups_in_desc_block(const struct inode *inode,
426 unsigned long curr, unsigned long max)
427{
428 return min_t(unsigned long,
429 nilfs_palloc_groups_per_desc_block(inode) -
430 curr % nilfs_palloc_groups_per_desc_block(inode),
431 max - curr + 1);
432}
433
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900434/**
Vyacheslav Dubeykoc7ef9722013-07-03 15:08:05 -0700435 * nilfs_palloc_count_desc_blocks - count descriptor blocks number
436 * @inode: inode of metadata file using this allocator
437 * @desc_blocks: descriptor blocks number [out]
438 */
439static int nilfs_palloc_count_desc_blocks(struct inode *inode,
440 unsigned long *desc_blocks)
441{
Ryusuke Konishi3568a132015-04-16 12:46:34 -0700442 __u64 blknum;
Vyacheslav Dubeykoc7ef9722013-07-03 15:08:05 -0700443 int ret;
444
445 ret = nilfs_bmap_last_key(NILFS_I(inode)->i_bmap, &blknum);
446 if (likely(!ret))
447 *desc_blocks = DIV_ROUND_UP(
Ryusuke Konishi3568a132015-04-16 12:46:34 -0700448 (unsigned long)blknum,
449 NILFS_MDT(inode)->mi_blocks_per_desc_block);
Vyacheslav Dubeykoc7ef9722013-07-03 15:08:05 -0700450 return ret;
451}
452
453/**
454 * nilfs_palloc_mdt_file_can_grow - check potential opportunity for
455 * MDT file growing
456 * @inode: inode of metadata file using this allocator
457 * @desc_blocks: known current descriptor blocks count
458 */
459static inline bool nilfs_palloc_mdt_file_can_grow(struct inode *inode,
460 unsigned long desc_blocks)
461{
462 return (nilfs_palloc_groups_per_desc_block(inode) * desc_blocks) <
463 nilfs_palloc_groups_count(inode);
464}
465
466/**
467 * nilfs_palloc_count_max_entries - count max number of entries that can be
468 * described by descriptor blocks count
469 * @inode: inode of metadata file using this allocator
470 * @nused: current number of used entries
471 * @nmaxp: max number of entries [out]
472 */
473int nilfs_palloc_count_max_entries(struct inode *inode, u64 nused, u64 *nmaxp)
474{
475 unsigned long desc_blocks = 0;
476 u64 entries_per_desc_block, nmax;
477 int err;
478
479 err = nilfs_palloc_count_desc_blocks(inode, &desc_blocks);
480 if (unlikely(err))
481 return err;
482
483 entries_per_desc_block = (u64)nilfs_palloc_entries_per_group(inode) *
484 nilfs_palloc_groups_per_desc_block(inode);
485 nmax = entries_per_desc_block * desc_blocks;
486
487 if (nused == nmax &&
488 nilfs_palloc_mdt_file_can_grow(inode, desc_blocks))
489 nmax += entries_per_desc_block;
490
491 if (nused > nmax)
492 return -ERANGE;
493
494 *nmaxp = nmax;
495 return 0;
496}
497
498/**
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900499 * nilfs_palloc_prepare_alloc_entry - prepare to allocate a persistent object
500 * @inode: inode of metadata file using this allocator
501 * @req: nilfs_palloc_req structure exchanged for the allocation
502 */
Ryusuke Konishi54426802009-04-06 19:01:29 -0700503int nilfs_palloc_prepare_alloc_entry(struct inode *inode,
504 struct nilfs_palloc_req *req)
505{
506 struct buffer_head *desc_bh, *bitmap_bh;
507 struct nilfs_palloc_group_desc *desc;
508 unsigned char *bitmap;
509 void *desc_kaddr, *bitmap_kaddr;
510 unsigned long group, maxgroup, ngroups;
511 unsigned long group_offset, maxgroup_offset;
Ryusuke Konishi09ef29e2015-11-06 16:32:14 -0800512 unsigned long n, entries_per_group;
Ryusuke Konishi54426802009-04-06 19:01:29 -0700513 unsigned long i, j;
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800514 spinlock_t *lock;
Ryusuke Konishi54426802009-04-06 19:01:29 -0700515 int pos, ret;
516
517 ngroups = nilfs_palloc_groups_count(inode);
518 maxgroup = ngroups - 1;
519 group = nilfs_palloc_group(inode, req->pr_entry_nr, &group_offset);
520 entries_per_group = nilfs_palloc_entries_per_group(inode);
Ryusuke Konishi54426802009-04-06 19:01:29 -0700521
522 for (i = 0; i < ngroups; i += n) {
523 if (group >= ngroups) {
524 /* wrap around */
525 group = 0;
526 maxgroup = nilfs_palloc_group(inode, req->pr_entry_nr,
527 &maxgroup_offset) - 1;
528 }
529 ret = nilfs_palloc_get_desc_block(inode, group, 1, &desc_bh);
530 if (ret < 0)
531 return ret;
532 desc_kaddr = kmap(desc_bh->b_page);
533 desc = nilfs_palloc_block_get_group_desc(
534 inode, group, desc_bh, desc_kaddr);
535 n = nilfs_palloc_rest_groups_in_desc_block(inode, group,
536 maxgroup);
537 for (j = 0; j < n; j++, desc++, group++) {
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800538 lock = nilfs_mdt_bgl_lock(inode, group);
539 if (nilfs_palloc_group_desc_nfrees(desc, lock) > 0) {
Ryusuke Konishi54426802009-04-06 19:01:29 -0700540 ret = nilfs_palloc_get_bitmap_block(
541 inode, group, 1, &bitmap_bh);
542 if (ret < 0)
543 goto out_desc;
544 bitmap_kaddr = kmap(bitmap_bh->b_page);
Ryusuke Konishi141bbdb2009-11-14 13:48:06 +0900545 bitmap = bitmap_kaddr + bh_offset(bitmap_bh);
Ryusuke Konishi54426802009-04-06 19:01:29 -0700546 pos = nilfs_palloc_find_available_slot(
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800547 bitmap, group_offset,
548 entries_per_group, lock);
Ryusuke Konishi54426802009-04-06 19:01:29 -0700549 if (pos >= 0) {
550 /* found a free entry */
551 nilfs_palloc_group_desc_add_entries(
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800552 desc, lock, -1);
Ryusuke Konishi54426802009-04-06 19:01:29 -0700553 req->pr_entry_nr =
554 entries_per_group * group + pos;
555 kunmap(desc_bh->b_page);
556 kunmap(bitmap_bh->b_page);
557
558 req->pr_desc_bh = desc_bh;
559 req->pr_bitmap_bh = bitmap_bh;
560 return 0;
561 }
562 kunmap(bitmap_bh->b_page);
563 brelse(bitmap_bh);
564 }
565
566 group_offset = 0;
567 }
568
569 kunmap(desc_bh->b_page);
570 brelse(desc_bh);
571 }
572
573 /* no entries left */
574 return -ENOSPC;
575
576 out_desc:
577 kunmap(desc_bh->b_page);
578 brelse(desc_bh);
579 return ret;
580}
581
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900582/**
583 * nilfs_palloc_commit_alloc_entry - finish allocation of a persistent object
584 * @inode: inode of metadata file using this allocator
585 * @req: nilfs_palloc_req structure exchanged for the allocation
586 */
Ryusuke Konishi54426802009-04-06 19:01:29 -0700587void nilfs_palloc_commit_alloc_entry(struct inode *inode,
588 struct nilfs_palloc_req *req)
589{
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900590 mark_buffer_dirty(req->pr_bitmap_bh);
591 mark_buffer_dirty(req->pr_desc_bh);
Ryusuke Konishi54426802009-04-06 19:01:29 -0700592 nilfs_mdt_mark_dirty(inode);
593
594 brelse(req->pr_bitmap_bh);
595 brelse(req->pr_desc_bh);
596}
597
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900598/**
599 * nilfs_palloc_commit_free_entry - finish deallocating a persistent object
600 * @inode: inode of metadata file using this allocator
601 * @req: nilfs_palloc_req structure exchanged for the removal
602 */
Ryusuke Konishi54426802009-04-06 19:01:29 -0700603void nilfs_palloc_commit_free_entry(struct inode *inode,
604 struct nilfs_palloc_req *req)
605{
606 struct nilfs_palloc_group_desc *desc;
607 unsigned long group, group_offset;
608 unsigned char *bitmap;
609 void *desc_kaddr, *bitmap_kaddr;
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800610 spinlock_t *lock;
Ryusuke Konishi54426802009-04-06 19:01:29 -0700611
612 group = nilfs_palloc_group(inode, req->pr_entry_nr, &group_offset);
613 desc_kaddr = kmap(req->pr_desc_bh->b_page);
614 desc = nilfs_palloc_block_get_group_desc(inode, group,
615 req->pr_desc_bh, desc_kaddr);
616 bitmap_kaddr = kmap(req->pr_bitmap_bh->b_page);
Ryusuke Konishi141bbdb2009-11-14 13:48:06 +0900617 bitmap = bitmap_kaddr + bh_offset(req->pr_bitmap_bh);
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800618 lock = nilfs_mdt_bgl_lock(inode, group);
Ryusuke Konishi54426802009-04-06 19:01:29 -0700619
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800620 if (!nilfs_clear_bit_atomic(lock, group_offset, bitmap))
Ryusuke Konishib7bed712015-11-06 16:31:43 -0800621 nilfs_warning(inode->i_sb, __func__,
Ryusuke Konishi06f4abf2016-05-23 16:23:31 -0700622 "entry number %llu already freed: ino=%lu",
Ryusuke Konishib7bed712015-11-06 16:31:43 -0800623 (unsigned long long)req->pr_entry_nr,
624 (unsigned long)inode->i_ino);
Ryusuke Konishi9954e7a2011-02-23 02:26:17 +0900625 else
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800626 nilfs_palloc_group_desc_add_entries(desc, lock, 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
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900631 mark_buffer_dirty(req->pr_desc_bh);
632 mark_buffer_dirty(req->pr_bitmap_bh);
Ryusuke Konishi54426802009-04-06 19:01:29 -0700633 nilfs_mdt_mark_dirty(inode);
634
635 brelse(req->pr_bitmap_bh);
636 brelse(req->pr_desc_bh);
637}
638
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900639/**
640 * nilfs_palloc_abort_alloc_entry - cancel allocation of a persistent object
641 * @inode: inode of metadata file using this allocator
642 * @req: nilfs_palloc_req structure exchanged for the allocation
643 */
Ryusuke Konishi54426802009-04-06 19:01:29 -0700644void nilfs_palloc_abort_alloc_entry(struct inode *inode,
645 struct nilfs_palloc_req *req)
646{
647 struct nilfs_palloc_group_desc *desc;
648 void *desc_kaddr, *bitmap_kaddr;
649 unsigned char *bitmap;
650 unsigned long group, group_offset;
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800651 spinlock_t *lock;
Ryusuke Konishi54426802009-04-06 19:01:29 -0700652
653 group = nilfs_palloc_group(inode, req->pr_entry_nr, &group_offset);
654 desc_kaddr = kmap(req->pr_desc_bh->b_page);
655 desc = nilfs_palloc_block_get_group_desc(inode, group,
656 req->pr_desc_bh, desc_kaddr);
657 bitmap_kaddr = kmap(req->pr_bitmap_bh->b_page);
Ryusuke Konishi141bbdb2009-11-14 13:48:06 +0900658 bitmap = bitmap_kaddr + bh_offset(req->pr_bitmap_bh);
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800659 lock = nilfs_mdt_bgl_lock(inode, group);
660
661 if (!nilfs_clear_bit_atomic(lock, group_offset, bitmap))
Ryusuke Konishib7bed712015-11-06 16:31:43 -0800662 nilfs_warning(inode->i_sb, __func__,
Ryusuke Konishi06f4abf2016-05-23 16:23:31 -0700663 "entry number %llu already freed: ino=%lu",
Ryusuke Konishib7bed712015-11-06 16:31:43 -0800664 (unsigned long long)req->pr_entry_nr,
665 (unsigned long)inode->i_ino);
Ryusuke Konishi9954e7a2011-02-23 02:26:17 +0900666 else
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800667 nilfs_palloc_group_desc_add_entries(desc, lock, 1);
Ryusuke Konishi54426802009-04-06 19:01:29 -0700668
669 kunmap(req->pr_bitmap_bh->b_page);
670 kunmap(req->pr_desc_bh->b_page);
671
672 brelse(req->pr_bitmap_bh);
673 brelse(req->pr_desc_bh);
674
675 req->pr_entry_nr = 0;
676 req->pr_bitmap_bh = NULL;
677 req->pr_desc_bh = NULL;
678}
679
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900680/**
681 * nilfs_palloc_prepare_free_entry - prepare to deallocate a persistent object
682 * @inode: inode of metadata file using this allocator
683 * @req: nilfs_palloc_req structure exchanged for the removal
684 */
Ryusuke Konishi54426802009-04-06 19:01:29 -0700685int nilfs_palloc_prepare_free_entry(struct inode *inode,
686 struct nilfs_palloc_req *req)
687{
688 struct buffer_head *desc_bh, *bitmap_bh;
689 unsigned long group, group_offset;
690 int ret;
691
692 group = nilfs_palloc_group(inode, req->pr_entry_nr, &group_offset);
693 ret = nilfs_palloc_get_desc_block(inode, group, 1, &desc_bh);
694 if (ret < 0)
695 return ret;
696 ret = nilfs_palloc_get_bitmap_block(inode, group, 1, &bitmap_bh);
697 if (ret < 0) {
698 brelse(desc_bh);
699 return ret;
700 }
701
702 req->pr_desc_bh = desc_bh;
703 req->pr_bitmap_bh = bitmap_bh;
704 return 0;
705}
706
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900707/**
708 * nilfs_palloc_abort_free_entry - cancel deallocating a persistent object
709 * @inode: inode of metadata file using this allocator
710 * @req: nilfs_palloc_req structure exchanged for the removal
711 */
Ryusuke Konishi54426802009-04-06 19:01:29 -0700712void nilfs_palloc_abort_free_entry(struct inode *inode,
713 struct nilfs_palloc_req *req)
714{
715 brelse(req->pr_bitmap_bh);
716 brelse(req->pr_desc_bh);
717
718 req->pr_entry_nr = 0;
719 req->pr_bitmap_bh = NULL;
720 req->pr_desc_bh = NULL;
721}
722
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900723/**
Ryusuke Konishidb55d922010-04-12 01:46:02 +0900724 * nilfs_palloc_freev - deallocate a set of persistent objects
725 * @inode: inode of metadata file using this allocator
726 * @entry_nrs: array of entry numbers to be deallocated
727 * @nitems: number of entries stored in @entry_nrs
728 */
Ryusuke Konishi54426802009-04-06 19:01:29 -0700729int nilfs_palloc_freev(struct inode *inode, __u64 *entry_nrs, size_t nitems)
730{
731 struct buffer_head *desc_bh, *bitmap_bh;
732 struct nilfs_palloc_group_desc *desc;
733 unsigned char *bitmap;
734 void *desc_kaddr, *bitmap_kaddr;
735 unsigned long group, group_offset;
Ryusuke Konishid0c14a92015-11-06 16:31:56 -0800736 __u64 group_min_nr, last_nrs[8];
Ryusuke Konishib2258092015-11-06 16:31:51 -0800737 const unsigned long epg = nilfs_palloc_entries_per_group(inode);
Ryusuke Konishi0c6c44c2016-05-23 16:23:39 -0700738 const unsigned int epb = NILFS_MDT(inode)->mi_entries_per_block;
739 unsigned int entry_start, end, pos;
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800740 spinlock_t *lock;
Ryusuke Konishid0c14a92015-11-06 16:31:56 -0800741 int i, j, k, ret;
742 u32 nfree;
Ryusuke Konishi54426802009-04-06 19:01:29 -0700743
Ryusuke Konishi349dbc32011-05-10 20:59:34 +0900744 for (i = 0; i < nitems; i = j) {
Ryusuke Konishid0c14a92015-11-06 16:31:56 -0800745 int change_group = false;
746 int nempties = 0, n = 0;
747
Ryusuke Konishi54426802009-04-06 19:01:29 -0700748 group = nilfs_palloc_group(inode, entry_nrs[i], &group_offset);
749 ret = nilfs_palloc_get_desc_block(inode, group, 0, &desc_bh);
750 if (ret < 0)
751 return ret;
752 ret = nilfs_palloc_get_bitmap_block(inode, group, 0,
753 &bitmap_bh);
754 if (ret < 0) {
755 brelse(desc_bh);
756 return ret;
757 }
Ryusuke Konishib2258092015-11-06 16:31:51 -0800758
759 /* Get the first entry number of the group */
760 group_min_nr = (__u64)group * epg;
761
Ryusuke Konishi54426802009-04-06 19:01:29 -0700762 bitmap_kaddr = kmap(bitmap_bh->b_page);
Ryusuke Konishi141bbdb2009-11-14 13:48:06 +0900763 bitmap = bitmap_kaddr + bh_offset(bitmap_bh);
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800764 lock = nilfs_mdt_bgl_lock(inode, group);
Ryusuke Konishid0c14a92015-11-06 16:31:56 -0800765
766 j = i;
767 entry_start = rounddown(group_offset, epb);
768 do {
Ryusuke Konishi4e9e63a2015-11-06 16:31:45 -0800769 if (!nilfs_clear_bit_atomic(lock, group_offset,
770 bitmap)) {
Ryusuke Konishib7bed712015-11-06 16:31:43 -0800771 nilfs_warning(inode->i_sb, __func__,
Ryusuke Konishi06f4abf2016-05-23 16:23:31 -0700772 "entry number %llu already freed: ino=%lu",
Ryusuke Konishib7bed712015-11-06 16:31:43 -0800773 (unsigned long long)entry_nrs[j],
774 (unsigned long)inode->i_ino);
Ryusuke Konishi9954e7a2011-02-23 02:26:17 +0900775 } else {
776 n++;
Ryusuke Konishi54426802009-04-06 19:01:29 -0700777 }
Ryusuke Konishid0c14a92015-11-06 16:31:56 -0800778
779 j++;
780 if (j >= nitems || entry_nrs[j] < group_min_nr ||
781 entry_nrs[j] >= group_min_nr + epg) {
782 change_group = true;
783 } else {
784 group_offset = entry_nrs[j] - group_min_nr;
785 if (group_offset >= entry_start &&
786 group_offset < entry_start + epb) {
787 /* This entry is in the same block */
788 continue;
789 }
790 }
791
792 /* Test if the entry block is empty or not */
793 end = entry_start + epb;
794 pos = nilfs_find_next_bit(bitmap, end, entry_start);
795 if (pos >= end) {
796 last_nrs[nempties++] = entry_nrs[j - 1];
797 if (nempties >= ARRAY_SIZE(last_nrs))
798 break;
799 }
800
801 if (change_group)
802 break;
803
804 /* Go on to the next entry block */
805 entry_start = rounddown(group_offset, epb);
806 } while (true);
Ryusuke Konishi54426802009-04-06 19:01:29 -0700807
808 kunmap(bitmap_bh->b_page);
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900809 mark_buffer_dirty(bitmap_bh);
Ryusuke Konishi54426802009-04-06 19:01:29 -0700810 brelse(bitmap_bh);
Ryusuke Konishid0c14a92015-11-06 16:31:56 -0800811
812 for (k = 0; k < nempties; k++) {
813 ret = nilfs_palloc_delete_entry_block(inode,
814 last_nrs[k]);
815 if (ret && ret != -ENOENT) {
816 nilfs_warning(inode->i_sb, __func__,
Ryusuke Konishi06f4abf2016-05-23 16:23:31 -0700817 "failed to delete block of entry %llu: ino=%lu, err=%d",
Ryusuke Konishid0c14a92015-11-06 16:31:56 -0800818 (unsigned long long)last_nrs[k],
819 (unsigned long)inode->i_ino, ret);
820 }
821 }
822
823 desc_kaddr = kmap_atomic(desc_bh->b_page);
824 desc = nilfs_palloc_block_get_group_desc(
825 inode, group, desc_bh, desc_kaddr);
826 nfree = nilfs_palloc_group_desc_add_entries(desc, lock, n);
827 kunmap_atomic(desc_kaddr);
828 mark_buffer_dirty(desc_bh);
829 nilfs_mdt_mark_dirty(inode);
Ryusuke Konishi54426802009-04-06 19:01:29 -0700830 brelse(desc_bh);
Ryusuke Konishid0c14a92015-11-06 16:31:56 -0800831
832 if (nfree == nilfs_palloc_entries_per_group(inode)) {
833 ret = nilfs_palloc_delete_bitmap_block(inode, group);
834 if (ret && ret != -ENOENT) {
835 nilfs_warning(inode->i_sb, __func__,
Ryusuke Konishi06f4abf2016-05-23 16:23:31 -0700836 "failed to delete bitmap block of group %lu: ino=%lu, err=%d",
Ryusuke Konishid0c14a92015-11-06 16:31:56 -0800837 group,
838 (unsigned long)inode->i_ino, ret);
839 }
840 }
Ryusuke Konishi54426802009-04-06 19:01:29 -0700841 }
842 return 0;
843}
Ryusuke Konishidb38d5a2009-11-14 15:54:27 +0900844
845void nilfs_palloc_setup_cache(struct inode *inode,
846 struct nilfs_palloc_cache *cache)
847{
848 NILFS_MDT(inode)->mi_palloc_cache = cache;
849 spin_lock_init(&cache->lock);
850}
851
852void nilfs_palloc_clear_cache(struct inode *inode)
853{
854 struct nilfs_palloc_cache *cache = NILFS_MDT(inode)->mi_palloc_cache;
855
856 spin_lock(&cache->lock);
857 brelse(cache->prev_desc.bh);
858 brelse(cache->prev_bitmap.bh);
859 brelse(cache->prev_entry.bh);
860 cache->prev_desc.bh = NULL;
861 cache->prev_bitmap.bh = NULL;
862 cache->prev_entry.bh = NULL;
863 spin_unlock(&cache->lock);
864}
865
866void nilfs_palloc_destroy_cache(struct inode *inode)
867{
868 nilfs_palloc_clear_cache(inode);
869 NILFS_MDT(inode)->mi_palloc_cache = NULL;
870}