AFS: Documentation updates
[linux-2.6/mini2440.git] / fs / nilfs2 / dat.c
blob8927ca27e6f79cbd82db5028d5dfd002baa99bb0
1 /*
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>
27 #include "nilfs.h"
28 #include "mdt.h"
29 #include "alloc.h"
30 #include "dat.h"
33 #define NILFS_CNO_MIN ((__u64)1)
34 #define NILFS_CNO_MAX (~(__u64)0)
36 static int nilfs_dat_prepare_entry(struct inode *dat,
37 struct nilfs_palloc_req *req, int create)
39 return nilfs_palloc_get_entry_block(dat, req->pr_entry_nr,
40 create, &req->pr_entry_bh);
43 static void nilfs_dat_commit_entry(struct inode *dat,
44 struct nilfs_palloc_req *req)
46 nilfs_mdt_mark_buffer_dirty(req->pr_entry_bh);
47 nilfs_mdt_mark_dirty(dat);
48 brelse(req->pr_entry_bh);
51 static void nilfs_dat_abort_entry(struct inode *dat,
52 struct nilfs_palloc_req *req)
54 brelse(req->pr_entry_bh);
57 int nilfs_dat_prepare_alloc(struct inode *dat, struct nilfs_palloc_req *req)
59 int ret;
61 ret = nilfs_palloc_prepare_alloc_entry(dat, req);
62 if (ret < 0)
63 return ret;
65 ret = nilfs_dat_prepare_entry(dat, req, 1);
66 if (ret < 0)
67 nilfs_palloc_abort_alloc_entry(dat, req);
69 return ret;
72 void nilfs_dat_commit_alloc(struct inode *dat, struct nilfs_palloc_req *req)
74 struct nilfs_dat_entry *entry;
75 void *kaddr;
77 kaddr = kmap_atomic(req->pr_entry_bh->b_page, KM_USER0);
78 entry = nilfs_palloc_block_get_entry(dat, req->pr_entry_nr,
79 req->pr_entry_bh, kaddr);
80 entry->de_start = cpu_to_le64(NILFS_CNO_MIN);
81 entry->de_end = cpu_to_le64(NILFS_CNO_MAX);
82 entry->de_blocknr = cpu_to_le64(0);
83 kunmap_atomic(kaddr, KM_USER0);
85 nilfs_palloc_commit_alloc_entry(dat, req);
86 nilfs_dat_commit_entry(dat, req);
89 void nilfs_dat_abort_alloc(struct inode *dat, struct nilfs_palloc_req *req)
91 nilfs_dat_abort_entry(dat, req);
92 nilfs_palloc_abort_alloc_entry(dat, req);
95 void nilfs_dat_commit_free(struct inode *dat, struct nilfs_palloc_req *req)
97 struct nilfs_dat_entry *entry;
98 void *kaddr;
100 kaddr = kmap_atomic(req->pr_entry_bh->b_page, KM_USER0);
101 entry = nilfs_palloc_block_get_entry(dat, req->pr_entry_nr,
102 req->pr_entry_bh, kaddr);
103 entry->de_start = cpu_to_le64(NILFS_CNO_MIN);
104 entry->de_end = cpu_to_le64(NILFS_CNO_MIN);
105 entry->de_blocknr = cpu_to_le64(0);
106 kunmap_atomic(kaddr, KM_USER0);
108 nilfs_dat_commit_entry(dat, req);
109 nilfs_palloc_commit_free_entry(dat, req);
112 void nilfs_dat_abort_free(struct inode *dat, struct nilfs_palloc_req *req)
114 nilfs_dat_abort_entry(dat, req);
115 nilfs_palloc_abort_free_entry(dat, req);
118 int nilfs_dat_prepare_start(struct inode *dat, struct nilfs_palloc_req *req)
120 int ret;
122 ret = nilfs_dat_prepare_entry(dat, req, 0);
123 WARN_ON(ret == -ENOENT);
124 return ret;
127 void nilfs_dat_commit_start(struct inode *dat, struct nilfs_palloc_req *req,
128 sector_t blocknr)
130 struct nilfs_dat_entry *entry;
131 void *kaddr;
133 kaddr = kmap_atomic(req->pr_entry_bh->b_page, KM_USER0);
134 entry = nilfs_palloc_block_get_entry(dat, req->pr_entry_nr,
135 req->pr_entry_bh, kaddr);
136 entry->de_start = cpu_to_le64(nilfs_mdt_cno(dat));
137 entry->de_blocknr = cpu_to_le64(blocknr);
138 kunmap_atomic(kaddr, KM_USER0);
140 nilfs_dat_commit_entry(dat, req);
143 void nilfs_dat_abort_start(struct inode *dat, struct nilfs_palloc_req *req)
145 nilfs_dat_abort_entry(dat, req);
148 int nilfs_dat_prepare_end(struct inode *dat, struct nilfs_palloc_req *req)
150 struct nilfs_dat_entry *entry;
151 __u64 start;
152 sector_t blocknr;
153 void *kaddr;
154 int ret;
156 ret = nilfs_dat_prepare_entry(dat, req, 0);
157 if (ret < 0) {
158 WARN_ON(ret == -ENOENT);
159 return ret;
162 kaddr = kmap_atomic(req->pr_entry_bh->b_page, KM_USER0);
163 entry = nilfs_palloc_block_get_entry(dat, req->pr_entry_nr,
164 req->pr_entry_bh, kaddr);
165 start = le64_to_cpu(entry->de_start);
166 blocknr = le64_to_cpu(entry->de_blocknr);
167 kunmap_atomic(kaddr, KM_USER0);
169 if (blocknr == 0) {
170 ret = nilfs_palloc_prepare_free_entry(dat, req);
171 if (ret < 0) {
172 nilfs_dat_abort_entry(dat, req);
173 return ret;
177 return 0;
180 void nilfs_dat_commit_end(struct inode *dat, struct nilfs_palloc_req *req,
181 int dead)
183 struct nilfs_dat_entry *entry;
184 __u64 start, end;
185 sector_t blocknr;
186 void *kaddr;
188 kaddr = kmap_atomic(req->pr_entry_bh->b_page, KM_USER0);
189 entry = nilfs_palloc_block_get_entry(dat, req->pr_entry_nr,
190 req->pr_entry_bh, kaddr);
191 end = start = le64_to_cpu(entry->de_start);
192 if (!dead) {
193 end = nilfs_mdt_cno(dat);
194 WARN_ON(start > end);
196 entry->de_end = cpu_to_le64(end);
197 blocknr = le64_to_cpu(entry->de_blocknr);
198 kunmap_atomic(kaddr, KM_USER0);
200 if (blocknr == 0)
201 nilfs_dat_commit_free(dat, req);
202 else
203 nilfs_dat_commit_entry(dat, req);
206 void nilfs_dat_abort_end(struct inode *dat, struct nilfs_palloc_req *req)
208 struct nilfs_dat_entry *entry;
209 __u64 start;
210 sector_t blocknr;
211 void *kaddr;
213 kaddr = kmap_atomic(req->pr_entry_bh->b_page, KM_USER0);
214 entry = nilfs_palloc_block_get_entry(dat, req->pr_entry_nr,
215 req->pr_entry_bh, kaddr);
216 start = le64_to_cpu(entry->de_start);
217 blocknr = le64_to_cpu(entry->de_blocknr);
218 kunmap_atomic(kaddr, KM_USER0);
220 if (start == nilfs_mdt_cno(dat) && blocknr == 0)
221 nilfs_palloc_abort_free_entry(dat, req);
222 nilfs_dat_abort_entry(dat, req);
226 * nilfs_dat_mark_dirty -
227 * @dat: DAT file inode
228 * @vblocknr: virtual block number
230 * Description:
232 * Return Value: On success, 0 is returned. On error, one of the following
233 * negative error codes is returned.
235 * %-EIO - I/O error.
237 * %-ENOMEM - Insufficient amount of memory available.
239 int nilfs_dat_mark_dirty(struct inode *dat, __u64 vblocknr)
241 struct nilfs_palloc_req req;
242 int ret;
244 req.pr_entry_nr = vblocknr;
245 ret = nilfs_dat_prepare_entry(dat, &req, 0);
246 if (ret == 0)
247 nilfs_dat_commit_entry(dat, &req);
248 return ret;
252 * nilfs_dat_freev - free virtual block numbers
253 * @dat: DAT file inode
254 * @vblocknrs: array of virtual block numbers
255 * @nitems: number of virtual block numbers
257 * Description: nilfs_dat_freev() frees the virtual block numbers specified by
258 * @vblocknrs and @nitems.
260 * Return Value: On success, 0 is returned. On error, one of the following
261 * nagative error codes is returned.
263 * %-EIO - I/O error.
265 * %-ENOMEM - Insufficient amount of memory available.
267 * %-ENOENT - The virtual block number have not been allocated.
269 int nilfs_dat_freev(struct inode *dat, __u64 *vblocknrs, size_t nitems)
271 return nilfs_palloc_freev(dat, vblocknrs, nitems);
275 * nilfs_dat_move - change a block number
276 * @dat: DAT file inode
277 * @vblocknr: virtual block number
278 * @blocknr: block number
280 * Description: nilfs_dat_move() changes the block number associated with
281 * @vblocknr to @blocknr.
283 * Return Value: On success, 0 is returned. On error, one of the following
284 * negative error codes is returned.
286 * %-EIO - I/O error.
288 * %-ENOMEM - Insufficient amount of memory available.
290 int nilfs_dat_move(struct inode *dat, __u64 vblocknr, sector_t blocknr)
292 struct buffer_head *entry_bh;
293 struct nilfs_dat_entry *entry;
294 void *kaddr;
295 int ret;
297 ret = nilfs_palloc_get_entry_block(dat, vblocknr, 0, &entry_bh);
298 if (ret < 0)
299 return ret;
300 kaddr = kmap_atomic(entry_bh->b_page, KM_USER0);
301 entry = nilfs_palloc_block_get_entry(dat, vblocknr, entry_bh, kaddr);
302 if (unlikely(entry->de_blocknr == cpu_to_le64(0))) {
303 printk(KERN_CRIT "%s: vbn = %llu, [%llu, %llu)\n", __func__,
304 (unsigned long long)vblocknr,
305 (unsigned long long)le64_to_cpu(entry->de_start),
306 (unsigned long long)le64_to_cpu(entry->de_end));
307 kunmap_atomic(kaddr, KM_USER0);
308 brelse(entry_bh);
309 return -EINVAL;
311 WARN_ON(blocknr == 0);
312 entry->de_blocknr = cpu_to_le64(blocknr);
313 kunmap_atomic(kaddr, KM_USER0);
315 nilfs_mdt_mark_buffer_dirty(entry_bh);
316 nilfs_mdt_mark_dirty(dat);
318 brelse(entry_bh);
320 return 0;
324 * nilfs_dat_translate - translate a virtual block number to a block number
325 * @dat: DAT file inode
326 * @vblocknr: virtual block number
327 * @blocknrp: pointer to a block number
329 * Description: nilfs_dat_translate() maps the virtual block number @vblocknr
330 * to the corresponding block number.
332 * Return Value: On success, 0 is returned and the block number associated
333 * with @vblocknr is stored in the place pointed by @blocknrp. On error, one
334 * of the following negative error codes is returned.
336 * %-EIO - I/O error.
338 * %-ENOMEM - Insufficient amount of memory available.
340 * %-ENOENT - A block number associated with @vblocknr does not exist.
342 int nilfs_dat_translate(struct inode *dat, __u64 vblocknr, sector_t *blocknrp)
344 struct buffer_head *entry_bh;
345 struct nilfs_dat_entry *entry;
346 sector_t blocknr;
347 void *kaddr;
348 int ret;
350 ret = nilfs_palloc_get_entry_block(dat, vblocknr, 0, &entry_bh);
351 if (ret < 0)
352 return ret;
354 kaddr = kmap_atomic(entry_bh->b_page, KM_USER0);
355 entry = nilfs_palloc_block_get_entry(dat, vblocknr, entry_bh, kaddr);
356 blocknr = le64_to_cpu(entry->de_blocknr);
357 if (blocknr == 0) {
358 ret = -ENOENT;
359 goto out;
361 if (blocknrp != NULL)
362 *blocknrp = blocknr;
364 out:
365 kunmap_atomic(kaddr, KM_USER0);
366 brelse(entry_bh);
367 return ret;
370 ssize_t nilfs_dat_get_vinfo(struct inode *dat, void *buf, unsigned visz,
371 size_t nvi)
373 struct buffer_head *entry_bh;
374 struct nilfs_dat_entry *entry;
375 struct nilfs_vinfo *vinfo = buf;
376 __u64 first, last;
377 void *kaddr;
378 unsigned long entries_per_block = NILFS_MDT(dat)->mi_entries_per_block;
379 int i, j, n, ret;
381 for (i = 0; i < nvi; i += n) {
382 ret = nilfs_palloc_get_entry_block(dat, vinfo->vi_vblocknr,
383 0, &entry_bh);
384 if (ret < 0)
385 return ret;
386 kaddr = kmap_atomic(entry_bh->b_page, KM_USER0);
387 /* last virtual block number in this block */
388 first = vinfo->vi_vblocknr;
389 do_div(first, entries_per_block);
390 first *= entries_per_block;
391 last = first + entries_per_block - 1;
392 for (j = i, n = 0;
393 j < nvi && vinfo->vi_vblocknr >= first &&
394 vinfo->vi_vblocknr <= last;
395 j++, n++, vinfo = (void *)vinfo + visz) {
396 entry = nilfs_palloc_block_get_entry(
397 dat, vinfo->vi_vblocknr, entry_bh, kaddr);
398 vinfo->vi_start = le64_to_cpu(entry->de_start);
399 vinfo->vi_end = le64_to_cpu(entry->de_end);
400 vinfo->vi_blocknr = le64_to_cpu(entry->de_blocknr);
402 kunmap_atomic(kaddr, KM_USER0);
403 brelse(entry_bh);
406 return nvi;