30
static inline __le64 *nilfs_direct_dptrs(const struct nilfs_direct *direct)
30
static inline __le64 *nilfs_direct_dptrs(const struct nilfs_bmap *direct)
33
((struct nilfs_direct_node *)direct->d_bmap.b_u.u_data + 1);
33
((struct nilfs_direct_node *)direct->b_u.u_data + 1);
36
36
static inline __u64
37
nilfs_direct_get_ptr(const struct nilfs_direct *direct, __u64 key)
37
nilfs_direct_get_ptr(const struct nilfs_bmap *direct, __u64 key)
39
return nilfs_bmap_dptr_to_ptr(*(nilfs_direct_dptrs(direct) + key));
39
return le64_to_cpu(*(nilfs_direct_dptrs(direct) + key));
42
static inline void nilfs_direct_set_ptr(struct nilfs_direct *direct,
42
static inline void nilfs_direct_set_ptr(struct nilfs_bmap *direct,
43
43
__u64 key, __u64 ptr)
45
*(nilfs_direct_dptrs(direct) + key) = nilfs_bmap_ptr_to_dptr(ptr);
45
*(nilfs_direct_dptrs(direct) + key) = cpu_to_le64(ptr);
48
static int nilfs_direct_lookup(const struct nilfs_bmap *bmap,
48
static int nilfs_direct_lookup(const struct nilfs_bmap *direct,
49
49
__u64 key, int level, __u64 *ptrp)
51
struct nilfs_direct *direct;
54
direct = (struct nilfs_direct *)bmap;
55
if ((key > NILFS_DIRECT_KEY_MAX) ||
56
(level != 1) || /* XXX: use macro for level 1 */
57
((ptr = nilfs_direct_get_ptr(direct, key)) ==
58
NILFS_BMAP_INVALID_PTR))
53
if (key > NILFS_DIRECT_KEY_MAX || level != 1)
55
ptr = nilfs_direct_get_ptr(direct, key);
56
if (ptr == NILFS_BMAP_INVALID_PTR)
66
static int nilfs_direct_lookup_contig(const struct nilfs_bmap *bmap,
63
static int nilfs_direct_lookup_contig(const struct nilfs_bmap *direct,
67
64
__u64 key, __u64 *ptrp,
68
65
unsigned maxblocks)
70
struct nilfs_direct *direct = (struct nilfs_direct *)bmap;
71
67
struct inode *dat = NULL;
76
if (key > NILFS_DIRECT_KEY_MAX ||
77
(ptr = nilfs_direct_get_ptr(direct, key)) ==
78
NILFS_BMAP_INVALID_PTR)
72
if (key > NILFS_DIRECT_KEY_MAX)
74
ptr = nilfs_direct_get_ptr(direct, key);
75
if (ptr == NILFS_BMAP_INVALID_PTR)
81
if (NILFS_BMAP_USE_VBN(bmap)) {
82
dat = nilfs_bmap_get_dat(bmap);
78
if (NILFS_BMAP_USE_VBN(direct)) {
79
dat = nilfs_bmap_get_dat(direct);
83
80
ret = nilfs_dat_translate(dat, ptr, &blocknr);
108
nilfs_direct_find_target_v(const struct nilfs_direct *direct, __u64 key)
105
nilfs_direct_find_target_v(const struct nilfs_bmap *direct, __u64 key)
112
ptr = nilfs_bmap_find_target_seq(&direct->d_bmap, key);
109
ptr = nilfs_bmap_find_target_seq(direct, key);
113
110
if (ptr != NILFS_BMAP_INVALID_PTR)
114
111
/* sequential access */
117
114
/* block group */
118
return nilfs_bmap_find_target_in_group(&direct->d_bmap);
121
static void nilfs_direct_set_target_v(struct nilfs_direct *direct,
122
__u64 key, __u64 ptr)
124
direct->d_bmap.b_last_allocated_key = key;
125
direct->d_bmap.b_last_allocated_ptr = ptr;
115
return nilfs_bmap_find_target_in_group(direct);
128
118
static int nilfs_direct_insert(struct nilfs_bmap *bmap, __u64 key, __u64 ptr)
130
struct nilfs_direct *direct = (struct nilfs_direct *)bmap;
131
120
union nilfs_bmap_ptr_req req;
132
121
struct inode *dat = NULL;
133
122
struct buffer_head *bh;
136
125
if (key > NILFS_DIRECT_KEY_MAX)
138
if (nilfs_direct_get_ptr(direct, key) != NILFS_BMAP_INVALID_PTR)
127
if (nilfs_direct_get_ptr(bmap, key) != NILFS_BMAP_INVALID_PTR)
141
130
if (NILFS_BMAP_USE_VBN(bmap)) {
142
req.bpr_ptr = nilfs_direct_find_target_v(direct, key);
131
req.bpr_ptr = nilfs_direct_find_target_v(bmap, key);
143
132
dat = nilfs_bmap_get_dat(bmap);
145
134
ret = nilfs_bmap_prepare_alloc_ptr(bmap, &req, dat);
149
138
set_buffer_nilfs_volatile(bh);
151
140
nilfs_bmap_commit_alloc_ptr(bmap, &req, dat);
152
nilfs_direct_set_ptr(direct, key, req.bpr_ptr);
141
nilfs_direct_set_ptr(bmap, key, req.bpr_ptr);
154
143
if (!nilfs_bmap_dirty(bmap))
155
144
nilfs_bmap_set_dirty(bmap);
157
146
if (NILFS_BMAP_USE_VBN(bmap))
158
nilfs_direct_set_target_v(direct, key, req.bpr_ptr);
147
nilfs_bmap_set_target_v(bmap, key, req.bpr_ptr);
160
149
nilfs_bmap_add_blocks(bmap, 1);
165
154
static int nilfs_direct_delete(struct nilfs_bmap *bmap, __u64 key)
167
struct nilfs_direct *direct = (struct nilfs_direct *)bmap;
168
156
union nilfs_bmap_ptr_req req;
169
157
struct inode *dat;
172
160
if (key > NILFS_DIRECT_KEY_MAX ||
173
nilfs_direct_get_ptr(direct, key) == NILFS_BMAP_INVALID_PTR)
161
nilfs_direct_get_ptr(bmap, key) == NILFS_BMAP_INVALID_PTR)
176
164
dat = NILFS_BMAP_USE_VBN(bmap) ? nilfs_bmap_get_dat(bmap) : NULL;
177
req.bpr_ptr = nilfs_direct_get_ptr(direct, key);
165
req.bpr_ptr = nilfs_direct_get_ptr(bmap, key);
179
167
ret = nilfs_bmap_prepare_end_ptr(bmap, &req, dat);
181
169
nilfs_bmap_commit_end_ptr(bmap, &req, dat);
182
nilfs_direct_set_ptr(direct, key, NILFS_BMAP_INVALID_PTR);
170
nilfs_direct_set_ptr(bmap, key, NILFS_BMAP_INVALID_PTR);
183
171
nilfs_bmap_sub_blocks(bmap, 1);
188
static int nilfs_direct_last_key(const struct nilfs_bmap *bmap, __u64 *keyp)
176
static int nilfs_direct_last_key(const struct nilfs_bmap *direct, __u64 *keyp)
190
struct nilfs_direct *direct;
191
178
__u64 key, lastkey;
193
direct = (struct nilfs_direct *)bmap;
194
180
lastkey = NILFS_DIRECT_KEY_MAX + 1;
195
181
for (key = NILFS_DIRECT_KEY_MIN; key <= NILFS_DIRECT_KEY_MAX; key++)
196
182
if (nilfs_direct_get_ptr(direct, key) !=
210
196
return key > NILFS_DIRECT_KEY_MAX;
213
static int nilfs_direct_gather_data(struct nilfs_bmap *bmap,
199
static int nilfs_direct_gather_data(struct nilfs_bmap *direct,
214
200
__u64 *keys, __u64 *ptrs, int nitems)
216
struct nilfs_direct *direct;
221
direct = (struct nilfs_direct *)bmap;
222
206
if (nitems > NILFS_DIRECT_NBLOCKS)
223
207
nitems = NILFS_DIRECT_NBLOCKS;
252
235
bmap->b_ops->bop_clear(bmap);
255
direct = (struct nilfs_direct *)bmap;
256
dptrs = nilfs_direct_dptrs(direct);
238
dptrs = nilfs_direct_dptrs(bmap);
257
239
for (i = 0, j = 0; i < NILFS_DIRECT_NBLOCKS; i++) {
258
240
if ((j < n) && (i == keys[j])) {
259
241
dptrs[i] = (i != key) ?
260
nilfs_bmap_ptr_to_dptr(ptrs[j]) :
242
cpu_to_le64(ptrs[j]) :
261
243
NILFS_BMAP_INVALID_PTR;
284
265
dat = nilfs_bmap_get_dat(bmap);
285
266
key = nilfs_bmap_data_get_key(bmap, bh);
286
ptr = nilfs_direct_get_ptr(direct, key);
267
ptr = nilfs_direct_get_ptr(bmap, key);
287
268
if (!buffer_nilfs_volatile(bh)) {
288
269
oldreq.pr_entry_nr = ptr;
289
270
newreq.pr_entry_nr = ptr;
293
274
nilfs_dat_commit_update(dat, &oldreq, &newreq,
294
275
bmap->b_ptr_type == NILFS_BMAP_PTR_VS);
295
276
set_buffer_nilfs_volatile(bh);
296
nilfs_direct_set_ptr(direct, key, newreq.pr_entry_nr);
277
nilfs_direct_set_ptr(bmap, key, newreq.pr_entry_nr);
298
279
ret = nilfs_dat_mark_dirty(dat, ptr);
303
static int nilfs_direct_assign_v(struct nilfs_direct *direct,
284
static int nilfs_direct_assign_v(struct nilfs_bmap *direct,
304
285
__u64 key, __u64 ptr,
305
286
struct buffer_head **bh,
306
287
sector_t blocknr,
307
288
union nilfs_binfo *binfo)
309
struct inode *dat = nilfs_bmap_get_dat(&direct->d_bmap);
290
struct inode *dat = nilfs_bmap_get_dat(direct);
310
291
union nilfs_bmap_ptr_req req;
314
295
ret = nilfs_dat_prepare_start(dat, &req.bpr_req);
316
297
nilfs_dat_commit_start(dat, &req.bpr_req, blocknr);
317
binfo->bi_v.bi_vblocknr = nilfs_bmap_ptr_to_dptr(ptr);
318
binfo->bi_v.bi_blkoff = nilfs_bmap_key_to_dkey(key);
298
binfo->bi_v.bi_vblocknr = cpu_to_le64(ptr);
299
binfo->bi_v.bi_blkoff = cpu_to_le64(key);
323
static int nilfs_direct_assign_p(struct nilfs_direct *direct,
304
static int nilfs_direct_assign_p(struct nilfs_bmap *direct,
324
305
__u64 key, __u64 ptr,
325
306
struct buffer_head **bh,
326
307
sector_t blocknr,
339
320
sector_t blocknr,
340
321
union nilfs_binfo *binfo)
342
struct nilfs_direct *direct;
346
direct = (struct nilfs_direct *)bmap;
347
326
key = nilfs_bmap_data_get_key(bmap, *bh);
348
327
if (unlikely(key > NILFS_DIRECT_KEY_MAX)) {
349
328
printk(KERN_CRIT "%s: invalid key: %llu\n", __func__,
350
329
(unsigned long long)key);
353
ptr = nilfs_direct_get_ptr(direct, key);
332
ptr = nilfs_direct_get_ptr(bmap, key);
354
333
if (unlikely(ptr == NILFS_BMAP_INVALID_PTR)) {
355
334
printk(KERN_CRIT "%s: invalid pointer: %llu\n", __func__,
356
335
(unsigned long long)ptr);
360
339
return NILFS_BMAP_USE_VBN(bmap) ?
361
nilfs_direct_assign_v(direct, key, ptr, bh, blocknr, binfo) :
362
nilfs_direct_assign_p(direct, key, ptr, bh, blocknr, binfo);
340
nilfs_direct_assign_v(bmap, key, ptr, bh, blocknr, binfo) :
341
nilfs_direct_assign_p(bmap, key, ptr, bh, blocknr, binfo);
365
344
static const struct nilfs_bmap_operations nilfs_direct_ops = {