2 * dat.c - NILFS disk address translation.
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 * Written by Koji Sato <koji@osrg.net>.
23 #include <linux/types.h>
24 #include <linux/buffer_head.h>
25 #include <linux/string.h>
26 #include <linux/errno.h>
33 #define NILFS_CNO_MIN ((__u64)1)
34 #define NILFS_CNO_MAX (~(__u64)0)
36 struct nilfs_dat_info
{
37 struct nilfs_mdt_info mi
;
38 struct nilfs_palloc_cache palloc_cache
;
41 static inline struct nilfs_dat_info
*NILFS_DAT_I(struct inode
*dat
)
43 return (struct nilfs_dat_info
*)NILFS_MDT(dat
);
46 static int nilfs_dat_prepare_entry(struct inode
*dat
,
47 struct nilfs_palloc_req
*req
, int create
)
49 return nilfs_palloc_get_entry_block(dat
, req
->pr_entry_nr
,
50 create
, &req
->pr_entry_bh
);
53 static void nilfs_dat_commit_entry(struct inode
*dat
,
54 struct nilfs_palloc_req
*req
)
56 nilfs_mdt_mark_buffer_dirty(req
->pr_entry_bh
);
57 nilfs_mdt_mark_dirty(dat
);
58 brelse(req
->pr_entry_bh
);
61 static void nilfs_dat_abort_entry(struct inode
*dat
,
62 struct nilfs_palloc_req
*req
)
64 brelse(req
->pr_entry_bh
);
67 int nilfs_dat_prepare_alloc(struct inode
*dat
, struct nilfs_palloc_req
*req
)
71 ret
= nilfs_palloc_prepare_alloc_entry(dat
, req
);
75 ret
= nilfs_dat_prepare_entry(dat
, req
, 1);
77 nilfs_palloc_abort_alloc_entry(dat
, req
);
82 void nilfs_dat_commit_alloc(struct inode
*dat
, struct nilfs_palloc_req
*req
)
84 struct nilfs_dat_entry
*entry
;
87 kaddr
= kmap_atomic(req
->pr_entry_bh
->b_page
, KM_USER0
);
88 entry
= nilfs_palloc_block_get_entry(dat
, req
->pr_entry_nr
,
89 req
->pr_entry_bh
, kaddr
);
90 entry
->de_start
= cpu_to_le64(NILFS_CNO_MIN
);
91 entry
->de_end
= cpu_to_le64(NILFS_CNO_MAX
);
92 entry
->de_blocknr
= cpu_to_le64(0);
93 kunmap_atomic(kaddr
, KM_USER0
);
95 nilfs_palloc_commit_alloc_entry(dat
, req
);
96 nilfs_dat_commit_entry(dat
, req
);
99 void nilfs_dat_abort_alloc(struct inode
*dat
, struct nilfs_palloc_req
*req
)
101 nilfs_dat_abort_entry(dat
, req
);
102 nilfs_palloc_abort_alloc_entry(dat
, req
);
105 void nilfs_dat_commit_free(struct inode
*dat
, struct nilfs_palloc_req
*req
)
107 struct nilfs_dat_entry
*entry
;
110 kaddr
= kmap_atomic(req
->pr_entry_bh
->b_page
, KM_USER0
);
111 entry
= nilfs_palloc_block_get_entry(dat
, req
->pr_entry_nr
,
112 req
->pr_entry_bh
, kaddr
);
113 entry
->de_start
= cpu_to_le64(NILFS_CNO_MIN
);
114 entry
->de_end
= cpu_to_le64(NILFS_CNO_MIN
);
115 entry
->de_blocknr
= cpu_to_le64(0);
116 kunmap_atomic(kaddr
, KM_USER0
);
118 nilfs_dat_commit_entry(dat
, req
);
119 nilfs_palloc_commit_free_entry(dat
, req
);
122 int nilfs_dat_prepare_start(struct inode
*dat
, struct nilfs_palloc_req
*req
)
126 ret
= nilfs_dat_prepare_entry(dat
, req
, 0);
127 WARN_ON(ret
== -ENOENT
);
131 void nilfs_dat_commit_start(struct inode
*dat
, struct nilfs_palloc_req
*req
,
134 struct nilfs_dat_entry
*entry
;
137 kaddr
= kmap_atomic(req
->pr_entry_bh
->b_page
, KM_USER0
);
138 entry
= nilfs_palloc_block_get_entry(dat
, req
->pr_entry_nr
,
139 req
->pr_entry_bh
, kaddr
);
140 entry
->de_start
= cpu_to_le64(nilfs_mdt_cno(dat
));
141 entry
->de_blocknr
= cpu_to_le64(blocknr
);
142 kunmap_atomic(kaddr
, KM_USER0
);
144 nilfs_dat_commit_entry(dat
, req
);
147 int nilfs_dat_prepare_end(struct inode
*dat
, struct nilfs_palloc_req
*req
)
149 struct nilfs_dat_entry
*entry
;
155 ret
= nilfs_dat_prepare_entry(dat
, req
, 0);
157 WARN_ON(ret
== -ENOENT
);
161 kaddr
= kmap_atomic(req
->pr_entry_bh
->b_page
, KM_USER0
);
162 entry
= nilfs_palloc_block_get_entry(dat
, req
->pr_entry_nr
,
163 req
->pr_entry_bh
, kaddr
);
164 start
= le64_to_cpu(entry
->de_start
);
165 blocknr
= le64_to_cpu(entry
->de_blocknr
);
166 kunmap_atomic(kaddr
, KM_USER0
);
169 ret
= nilfs_palloc_prepare_free_entry(dat
, req
);
171 nilfs_dat_abort_entry(dat
, req
);
179 void nilfs_dat_commit_end(struct inode
*dat
, struct nilfs_palloc_req
*req
,
182 struct nilfs_dat_entry
*entry
;
187 kaddr
= kmap_atomic(req
->pr_entry_bh
->b_page
, KM_USER0
);
188 entry
= nilfs_palloc_block_get_entry(dat
, req
->pr_entry_nr
,
189 req
->pr_entry_bh
, kaddr
);
190 end
= start
= le64_to_cpu(entry
->de_start
);
192 end
= nilfs_mdt_cno(dat
);
193 WARN_ON(start
> end
);
195 entry
->de_end
= cpu_to_le64(end
);
196 blocknr
= le64_to_cpu(entry
->de_blocknr
);
197 kunmap_atomic(kaddr
, KM_USER0
);
200 nilfs_dat_commit_free(dat
, req
);
202 nilfs_dat_commit_entry(dat
, req
);
205 void nilfs_dat_abort_end(struct inode
*dat
, struct nilfs_palloc_req
*req
)
207 struct nilfs_dat_entry
*entry
;
212 kaddr
= kmap_atomic(req
->pr_entry_bh
->b_page
, KM_USER0
);
213 entry
= nilfs_palloc_block_get_entry(dat
, req
->pr_entry_nr
,
214 req
->pr_entry_bh
, kaddr
);
215 start
= le64_to_cpu(entry
->de_start
);
216 blocknr
= le64_to_cpu(entry
->de_blocknr
);
217 kunmap_atomic(kaddr
, KM_USER0
);
219 if (start
== nilfs_mdt_cno(dat
) && blocknr
== 0)
220 nilfs_palloc_abort_free_entry(dat
, req
);
221 nilfs_dat_abort_entry(dat
, req
);
224 int nilfs_dat_prepare_update(struct inode
*dat
,
225 struct nilfs_palloc_req
*oldreq
,
226 struct nilfs_palloc_req
*newreq
)
230 ret
= nilfs_dat_prepare_end(dat
, oldreq
);
232 ret
= nilfs_dat_prepare_alloc(dat
, newreq
);
234 nilfs_dat_abort_end(dat
, oldreq
);
239 void nilfs_dat_commit_update(struct inode
*dat
,
240 struct nilfs_palloc_req
*oldreq
,
241 struct nilfs_palloc_req
*newreq
, int dead
)
243 nilfs_dat_commit_end(dat
, oldreq
, dead
);
244 nilfs_dat_commit_alloc(dat
, newreq
);
247 void nilfs_dat_abort_update(struct inode
*dat
,
248 struct nilfs_palloc_req
*oldreq
,
249 struct nilfs_palloc_req
*newreq
)
251 nilfs_dat_abort_end(dat
, oldreq
);
252 nilfs_dat_abort_alloc(dat
, newreq
);
256 * nilfs_dat_mark_dirty -
257 * @dat: DAT file inode
258 * @vblocknr: virtual block number
262 * Return Value: On success, 0 is returned. On error, one of the following
263 * negative error codes is returned.
267 * %-ENOMEM - Insufficient amount of memory available.
269 int nilfs_dat_mark_dirty(struct inode
*dat
, __u64 vblocknr
)
271 struct nilfs_palloc_req req
;
274 req
.pr_entry_nr
= vblocknr
;
275 ret
= nilfs_dat_prepare_entry(dat
, &req
, 0);
277 nilfs_dat_commit_entry(dat
, &req
);
282 * nilfs_dat_freev - free virtual block numbers
283 * @dat: DAT file inode
284 * @vblocknrs: array of virtual block numbers
285 * @nitems: number of virtual block numbers
287 * Description: nilfs_dat_freev() frees the virtual block numbers specified by
288 * @vblocknrs and @nitems.
290 * Return Value: On success, 0 is returned. On error, one of the following
291 * nagative error codes is returned.
295 * %-ENOMEM - Insufficient amount of memory available.
297 * %-ENOENT - The virtual block number have not been allocated.
299 int nilfs_dat_freev(struct inode
*dat
, __u64
*vblocknrs
, size_t nitems
)
301 return nilfs_palloc_freev(dat
, vblocknrs
, nitems
);
305 * nilfs_dat_move - change a block number
306 * @dat: DAT file inode
307 * @vblocknr: virtual block number
308 * @blocknr: block number
310 * Description: nilfs_dat_move() changes the block number associated with
311 * @vblocknr to @blocknr.
313 * Return Value: On success, 0 is returned. On error, one of the following
314 * negative error codes is returned.
318 * %-ENOMEM - Insufficient amount of memory available.
320 int nilfs_dat_move(struct inode
*dat
, __u64 vblocknr
, sector_t blocknr
)
322 struct buffer_head
*entry_bh
;
323 struct nilfs_dat_entry
*entry
;
327 ret
= nilfs_palloc_get_entry_block(dat
, vblocknr
, 0, &entry_bh
);
330 kaddr
= kmap_atomic(entry_bh
->b_page
, KM_USER0
);
331 entry
= nilfs_palloc_block_get_entry(dat
, vblocknr
, entry_bh
, kaddr
);
332 if (unlikely(entry
->de_blocknr
== cpu_to_le64(0))) {
333 printk(KERN_CRIT
"%s: vbn = %llu, [%llu, %llu)\n", __func__
,
334 (unsigned long long)vblocknr
,
335 (unsigned long long)le64_to_cpu(entry
->de_start
),
336 (unsigned long long)le64_to_cpu(entry
->de_end
));
337 kunmap_atomic(kaddr
, KM_USER0
);
341 WARN_ON(blocknr
== 0);
342 entry
->de_blocknr
= cpu_to_le64(blocknr
);
343 kunmap_atomic(kaddr
, KM_USER0
);
345 nilfs_mdt_mark_buffer_dirty(entry_bh
);
346 nilfs_mdt_mark_dirty(dat
);
354 * nilfs_dat_translate - translate a virtual block number to a block number
355 * @dat: DAT file inode
356 * @vblocknr: virtual block number
357 * @blocknrp: pointer to a block number
359 * Description: nilfs_dat_translate() maps the virtual block number @vblocknr
360 * to the corresponding block number.
362 * Return Value: On success, 0 is returned and the block number associated
363 * with @vblocknr is stored in the place pointed by @blocknrp. On error, one
364 * of the following negative error codes is returned.
368 * %-ENOMEM - Insufficient amount of memory available.
370 * %-ENOENT - A block number associated with @vblocknr does not exist.
372 int nilfs_dat_translate(struct inode
*dat
, __u64 vblocknr
, sector_t
*blocknrp
)
374 struct buffer_head
*entry_bh
;
375 struct nilfs_dat_entry
*entry
;
380 ret
= nilfs_palloc_get_entry_block(dat
, vblocknr
, 0, &entry_bh
);
384 kaddr
= kmap_atomic(entry_bh
->b_page
, KM_USER0
);
385 entry
= nilfs_palloc_block_get_entry(dat
, vblocknr
, entry_bh
, kaddr
);
386 blocknr
= le64_to_cpu(entry
->de_blocknr
);
391 if (blocknrp
!= NULL
)
395 kunmap_atomic(kaddr
, KM_USER0
);
400 ssize_t
nilfs_dat_get_vinfo(struct inode
*dat
, void *buf
, unsigned visz
,
403 struct buffer_head
*entry_bh
;
404 struct nilfs_dat_entry
*entry
;
405 struct nilfs_vinfo
*vinfo
= buf
;
408 unsigned long entries_per_block
= NILFS_MDT(dat
)->mi_entries_per_block
;
411 for (i
= 0; i
< nvi
; i
+= n
) {
412 ret
= nilfs_palloc_get_entry_block(dat
, vinfo
->vi_vblocknr
,
416 kaddr
= kmap_atomic(entry_bh
->b_page
, KM_USER0
);
417 /* last virtual block number in this block */
418 first
= vinfo
->vi_vblocknr
;
419 do_div(first
, entries_per_block
);
420 first
*= entries_per_block
;
421 last
= first
+ entries_per_block
- 1;
423 j
< nvi
&& vinfo
->vi_vblocknr
>= first
&&
424 vinfo
->vi_vblocknr
<= last
;
425 j
++, n
++, vinfo
= (void *)vinfo
+ visz
) {
426 entry
= nilfs_palloc_block_get_entry(
427 dat
, vinfo
->vi_vblocknr
, entry_bh
, kaddr
);
428 vinfo
->vi_start
= le64_to_cpu(entry
->de_start
);
429 vinfo
->vi_end
= le64_to_cpu(entry
->de_end
);
430 vinfo
->vi_blocknr
= le64_to_cpu(entry
->de_blocknr
);
432 kunmap_atomic(kaddr
, KM_USER0
);
440 * nilfs_dat_read - read dat inode
442 * @raw_inode: on-disk dat inode
444 int nilfs_dat_read(struct inode
*dat
, struct nilfs_inode
*raw_inode
)
446 return nilfs_read_inode_common(dat
, raw_inode
);
450 * nilfs_dat_new - create dat file
451 * @nilfs: nilfs object
452 * @entry_size: size of a dat entry
454 struct inode
*nilfs_dat_new(struct the_nilfs
*nilfs
, size_t entry_size
)
456 static struct lock_class_key dat_lock_key
;
458 struct nilfs_dat_info
*di
;
461 dat
= nilfs_mdt_new(nilfs
, NULL
, NILFS_DAT_INO
, sizeof(*di
));
463 err
= nilfs_palloc_init_blockgroup(dat
, entry_size
);
465 nilfs_mdt_destroy(dat
);
469 di
= NILFS_DAT_I(dat
);
470 lockdep_set_class(&di
->mi
.mi_sem
, &dat_lock_key
);
471 nilfs_palloc_setup_cache(dat
, &di
->palloc_cache
);