[S390] qdio: Sanitize do_QDIO sanity checks
[linux-2.6/kvm.git] / fs / nilfs2 / dat.c
blob0b2710e2d56547ac7aa36b6367e5ed6f36685c49
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 if (entry->de_blocknr != cpu_to_le64(0) ||
138 entry->de_end != cpu_to_le64(NILFS_CNO_MAX)) {
139 printk(KERN_CRIT
140 "%s: vbn = %llu, start = %llu, end = %llu, pbn = %llu\n",
141 __func__, (unsigned long long)req->pr_entry_nr,
142 (unsigned long long)le64_to_cpu(entry->de_start),
143 (unsigned long long)le64_to_cpu(entry->de_end),
144 (unsigned long long)le64_to_cpu(entry->de_blocknr));
146 entry->de_blocknr = cpu_to_le64(blocknr);
147 kunmap_atomic(kaddr, KM_USER0);
149 nilfs_dat_commit_entry(dat, req);
152 void nilfs_dat_abort_start(struct inode *dat, struct nilfs_palloc_req *req)
154 nilfs_dat_abort_entry(dat, req);
157 int nilfs_dat_prepare_end(struct inode *dat, struct nilfs_palloc_req *req)
159 struct nilfs_dat_entry *entry;
160 __u64 start;
161 sector_t blocknr;
162 void *kaddr;
163 int ret;
165 ret = nilfs_dat_prepare_entry(dat, req, 0);
166 if (ret < 0) {
167 WARN_ON(ret == -ENOENT);
168 return ret;
171 kaddr = kmap_atomic(req->pr_entry_bh->b_page, KM_USER0);
172 entry = nilfs_palloc_block_get_entry(dat, req->pr_entry_nr,
173 req->pr_entry_bh, kaddr);
174 start = le64_to_cpu(entry->de_start);
175 blocknr = le64_to_cpu(entry->de_blocknr);
176 kunmap_atomic(kaddr, KM_USER0);
178 if (blocknr == 0) {
179 ret = nilfs_palloc_prepare_free_entry(dat, req);
180 if (ret < 0) {
181 nilfs_dat_abort_entry(dat, req);
182 return ret;
186 return 0;
189 void nilfs_dat_commit_end(struct inode *dat, struct nilfs_palloc_req *req,
190 int dead)
192 struct nilfs_dat_entry *entry;
193 __u64 start, end;
194 sector_t blocknr;
195 void *kaddr;
197 kaddr = kmap_atomic(req->pr_entry_bh->b_page, KM_USER0);
198 entry = nilfs_palloc_block_get_entry(dat, req->pr_entry_nr,
199 req->pr_entry_bh, kaddr);
200 end = start = le64_to_cpu(entry->de_start);
201 if (!dead) {
202 end = nilfs_mdt_cno(dat);
203 WARN_ON(start > end);
205 entry->de_end = cpu_to_le64(end);
206 blocknr = le64_to_cpu(entry->de_blocknr);
207 kunmap_atomic(kaddr, KM_USER0);
209 if (blocknr == 0)
210 nilfs_dat_commit_free(dat, req);
211 else
212 nilfs_dat_commit_entry(dat, req);
215 void nilfs_dat_abort_end(struct inode *dat, struct nilfs_palloc_req *req)
217 struct nilfs_dat_entry *entry;
218 __u64 start;
219 sector_t blocknr;
220 void *kaddr;
222 kaddr = kmap_atomic(req->pr_entry_bh->b_page, KM_USER0);
223 entry = nilfs_palloc_block_get_entry(dat, req->pr_entry_nr,
224 req->pr_entry_bh, kaddr);
225 start = le64_to_cpu(entry->de_start);
226 blocknr = le64_to_cpu(entry->de_blocknr);
227 kunmap_atomic(kaddr, KM_USER0);
229 if (start == nilfs_mdt_cno(dat) && blocknr == 0)
230 nilfs_palloc_abort_free_entry(dat, req);
231 nilfs_dat_abort_entry(dat, req);
235 * nilfs_dat_mark_dirty -
236 * @dat: DAT file inode
237 * @vblocknr: virtual block number
239 * Description:
241 * Return Value: On success, 0 is returned. On error, one of the following
242 * negative error codes is returned.
244 * %-EIO - I/O error.
246 * %-ENOMEM - Insufficient amount of memory available.
248 int nilfs_dat_mark_dirty(struct inode *dat, __u64 vblocknr)
250 struct nilfs_palloc_req req;
251 int ret;
253 req.pr_entry_nr = vblocknr;
254 ret = nilfs_dat_prepare_entry(dat, &req, 0);
255 if (ret == 0)
256 nilfs_dat_commit_entry(dat, &req);
257 return ret;
261 * nilfs_dat_freev - free virtual block numbers
262 * @dat: DAT file inode
263 * @vblocknrs: array of virtual block numbers
264 * @nitems: number of virtual block numbers
266 * Description: nilfs_dat_freev() frees the virtual block numbers specified by
267 * @vblocknrs and @nitems.
269 * Return Value: On success, 0 is returned. On error, one of the following
270 * nagative error codes is returned.
272 * %-EIO - I/O error.
274 * %-ENOMEM - Insufficient amount of memory available.
276 * %-ENOENT - The virtual block number have not been allocated.
278 int nilfs_dat_freev(struct inode *dat, __u64 *vblocknrs, size_t nitems)
280 return nilfs_palloc_freev(dat, vblocknrs, nitems);
284 * nilfs_dat_move - change a block number
285 * @dat: DAT file inode
286 * @vblocknr: virtual block number
287 * @blocknr: block number
289 * Description: nilfs_dat_move() changes the block number associated with
290 * @vblocknr to @blocknr.
292 * Return Value: On success, 0 is returned. On error, one of the following
293 * negative error codes is returned.
295 * %-EIO - I/O error.
297 * %-ENOMEM - Insufficient amount of memory available.
299 int nilfs_dat_move(struct inode *dat, __u64 vblocknr, sector_t blocknr)
301 struct buffer_head *entry_bh;
302 struct nilfs_dat_entry *entry;
303 void *kaddr;
304 int ret;
306 ret = nilfs_palloc_get_entry_block(dat, vblocknr, 0, &entry_bh);
307 if (ret < 0)
308 return ret;
309 kaddr = kmap_atomic(entry_bh->b_page, KM_USER0);
310 entry = nilfs_palloc_block_get_entry(dat, vblocknr, entry_bh, kaddr);
311 if (unlikely(entry->de_blocknr == cpu_to_le64(0))) {
312 printk(KERN_CRIT "%s: vbn = %llu, [%llu, %llu)\n", __func__,
313 (unsigned long long)vblocknr,
314 (unsigned long long)le64_to_cpu(entry->de_start),
315 (unsigned long long)le64_to_cpu(entry->de_end));
316 kunmap_atomic(kaddr, KM_USER0);
317 brelse(entry_bh);
318 return -EINVAL;
320 WARN_ON(blocknr == 0);
321 entry->de_blocknr = cpu_to_le64(blocknr);
322 kunmap_atomic(kaddr, KM_USER0);
324 nilfs_mdt_mark_buffer_dirty(entry_bh);
325 nilfs_mdt_mark_dirty(dat);
327 brelse(entry_bh);
329 return 0;
333 * nilfs_dat_translate - translate a virtual block number to a block number
334 * @dat: DAT file inode
335 * @vblocknr: virtual block number
336 * @blocknrp: pointer to a block number
338 * Description: nilfs_dat_translate() maps the virtual block number @vblocknr
339 * to the corresponding block number.
341 * Return Value: On success, 0 is returned and the block number associated
342 * with @vblocknr is stored in the place pointed by @blocknrp. On error, one
343 * of the following negative error codes is returned.
345 * %-EIO - I/O error.
347 * %-ENOMEM - Insufficient amount of memory available.
349 * %-ENOENT - A block number associated with @vblocknr does not exist.
351 int nilfs_dat_translate(struct inode *dat, __u64 vblocknr, sector_t *blocknrp)
353 struct buffer_head *entry_bh;
354 struct nilfs_dat_entry *entry;
355 sector_t blocknr;
356 void *kaddr;
357 int ret;
359 ret = nilfs_palloc_get_entry_block(dat, vblocknr, 0, &entry_bh);
360 if (ret < 0)
361 return ret;
363 kaddr = kmap_atomic(entry_bh->b_page, KM_USER0);
364 entry = nilfs_palloc_block_get_entry(dat, vblocknr, entry_bh, kaddr);
365 blocknr = le64_to_cpu(entry->de_blocknr);
366 if (blocknr == 0) {
367 ret = -ENOENT;
368 goto out;
370 if (blocknrp != NULL)
371 *blocknrp = blocknr;
373 out:
374 kunmap_atomic(kaddr, KM_USER0);
375 brelse(entry_bh);
376 return ret;
379 ssize_t nilfs_dat_get_vinfo(struct inode *dat, void *buf, unsigned visz,
380 size_t nvi)
382 struct buffer_head *entry_bh;
383 struct nilfs_dat_entry *entry;
384 struct nilfs_vinfo *vinfo = buf;
385 __u64 first, last;
386 void *kaddr;
387 unsigned long entries_per_block = NILFS_MDT(dat)->mi_entries_per_block;
388 int i, j, n, ret;
390 for (i = 0; i < nvi; i += n) {
391 ret = nilfs_palloc_get_entry_block(dat, vinfo->vi_vblocknr,
392 0, &entry_bh);
393 if (ret < 0)
394 return ret;
395 kaddr = kmap_atomic(entry_bh->b_page, KM_USER0);
396 /* last virtual block number in this block */
397 first = vinfo->vi_vblocknr;
398 do_div(first, entries_per_block);
399 first *= entries_per_block;
400 last = first + entries_per_block - 1;
401 for (j = i, n = 0;
402 j < nvi && vinfo->vi_vblocknr >= first &&
403 vinfo->vi_vblocknr <= last;
404 j++, n++, vinfo = (void *)vinfo + visz) {
405 entry = nilfs_palloc_block_get_entry(
406 dat, vinfo->vi_vblocknr, entry_bh, kaddr);
407 vinfo->vi_start = le64_to_cpu(entry->de_start);
408 vinfo->vi_end = le64_to_cpu(entry->de_end);
409 vinfo->vi_blocknr = le64_to_cpu(entry->de_blocknr);
411 kunmap_atomic(kaddr, KM_USER0);
412 brelse(entry_bh);
415 return nvi;