nilfs2: get rid of GCDAT inode
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / fs / nilfs2 / dat.c
CommitLineData
a17564f5
KS
1/*
2 * dat.c - NILFS disk address translation.
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 *
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
19 *
20 * Written by Koji Sato <koji@osrg.net>.
21 */
22
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"
31
32
33#define NILFS_CNO_MIN ((__u64)1)
34#define NILFS_CNO_MAX (~(__u64)0)
35
8908b2f7
RK
36struct nilfs_dat_info {
37 struct nilfs_mdt_info mi;
38 struct nilfs_palloc_cache palloc_cache;
c1c1d709 39 struct nilfs_shadow_map shadow;
8908b2f7
RK
40};
41
42static inline struct nilfs_dat_info *NILFS_DAT_I(struct inode *dat)
43{
44 return (struct nilfs_dat_info *)NILFS_MDT(dat);
45}
46
a17564f5
KS
47static int nilfs_dat_prepare_entry(struct inode *dat,
48 struct nilfs_palloc_req *req, int create)
49{
50 return nilfs_palloc_get_entry_block(dat, req->pr_entry_nr,
51 create, &req->pr_entry_bh);
52}
53
54static void nilfs_dat_commit_entry(struct inode *dat,
55 struct nilfs_palloc_req *req)
56{
57 nilfs_mdt_mark_buffer_dirty(req->pr_entry_bh);
58 nilfs_mdt_mark_dirty(dat);
59 brelse(req->pr_entry_bh);
60}
61
62static void nilfs_dat_abort_entry(struct inode *dat,
63 struct nilfs_palloc_req *req)
64{
65 brelse(req->pr_entry_bh);
66}
67
68int nilfs_dat_prepare_alloc(struct inode *dat, struct nilfs_palloc_req *req)
69{
70 int ret;
71
72 ret = nilfs_palloc_prepare_alloc_entry(dat, req);
73 if (ret < 0)
74 return ret;
75
76 ret = nilfs_dat_prepare_entry(dat, req, 1);
77 if (ret < 0)
78 nilfs_palloc_abort_alloc_entry(dat, req);
79
80 return ret;
81}
82
83void nilfs_dat_commit_alloc(struct inode *dat, struct nilfs_palloc_req *req)
84{
85 struct nilfs_dat_entry *entry;
86 void *kaddr;
87
88 kaddr = kmap_atomic(req->pr_entry_bh->b_page, KM_USER0);
89 entry = nilfs_palloc_block_get_entry(dat, req->pr_entry_nr,
90 req->pr_entry_bh, kaddr);
91 entry->de_start = cpu_to_le64(NILFS_CNO_MIN);
92 entry->de_end = cpu_to_le64(NILFS_CNO_MAX);
93 entry->de_blocknr = cpu_to_le64(0);
94 kunmap_atomic(kaddr, KM_USER0);
95
96 nilfs_palloc_commit_alloc_entry(dat, req);
97 nilfs_dat_commit_entry(dat, req);
98}
99
100void nilfs_dat_abort_alloc(struct inode *dat, struct nilfs_palloc_req *req)
101{
102 nilfs_dat_abort_entry(dat, req);
103 nilfs_palloc_abort_alloc_entry(dat, req);
104}
105
a17564f5
KS
106void nilfs_dat_commit_free(struct inode *dat, struct nilfs_palloc_req *req)
107{
108 struct nilfs_dat_entry *entry;
109 void *kaddr;
110
111 kaddr = kmap_atomic(req->pr_entry_bh->b_page, KM_USER0);
112 entry = nilfs_palloc_block_get_entry(dat, req->pr_entry_nr,
113 req->pr_entry_bh, kaddr);
114 entry->de_start = cpu_to_le64(NILFS_CNO_MIN);
115 entry->de_end = cpu_to_le64(NILFS_CNO_MIN);
116 entry->de_blocknr = cpu_to_le64(0);
117 kunmap_atomic(kaddr, KM_USER0);
118
119 nilfs_dat_commit_entry(dat, req);
120 nilfs_palloc_commit_free_entry(dat, req);
121}
122
a17564f5
KS
123int nilfs_dat_prepare_start(struct inode *dat, struct nilfs_palloc_req *req)
124{
125 int ret;
126
127 ret = nilfs_dat_prepare_entry(dat, req, 0);
1f5abe7e 128 WARN_ON(ret == -ENOENT);
a17564f5
KS
129 return ret;
130}
131
132void nilfs_dat_commit_start(struct inode *dat, struct nilfs_palloc_req *req,
133 sector_t blocknr)
134{
135 struct nilfs_dat_entry *entry;
136 void *kaddr;
137
138 kaddr = kmap_atomic(req->pr_entry_bh->b_page, KM_USER0);
139 entry = nilfs_palloc_block_get_entry(dat, req->pr_entry_nr,
140 req->pr_entry_bh, kaddr);
141 entry->de_start = cpu_to_le64(nilfs_mdt_cno(dat));
a17564f5
KS
142 entry->de_blocknr = cpu_to_le64(blocknr);
143 kunmap_atomic(kaddr, KM_USER0);
144
145 nilfs_dat_commit_entry(dat, req);
146}
147
a17564f5
KS
148int nilfs_dat_prepare_end(struct inode *dat, struct nilfs_palloc_req *req)
149{
150 struct nilfs_dat_entry *entry;
151 __u64 start;
152 sector_t blocknr;
153 void *kaddr;
154 int ret;
155
156 ret = nilfs_dat_prepare_entry(dat, req, 0);
157 if (ret < 0) {
1f5abe7e 158 WARN_ON(ret == -ENOENT);
a17564f5
KS
159 return ret;
160 }
161
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);
168
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;
174 }
175 }
176
177 return 0;
178}
179
180void nilfs_dat_commit_end(struct inode *dat, struct nilfs_palloc_req *req,
181 int dead)
182{
183 struct nilfs_dat_entry *entry;
184 __u64 start, end;
185 sector_t blocknr;
186 void *kaddr;
187
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);
1f5abe7e 194 WARN_ON(start > end);
a17564f5
KS
195 }
196 entry->de_end = cpu_to_le64(end);
197 blocknr = le64_to_cpu(entry->de_blocknr);
198 kunmap_atomic(kaddr, KM_USER0);
199
200 if (blocknr == 0)
201 nilfs_dat_commit_free(dat, req);
202 else
203 nilfs_dat_commit_entry(dat, req);
204}
205
206void nilfs_dat_abort_end(struct inode *dat, struct nilfs_palloc_req *req)
207{
208 struct nilfs_dat_entry *entry;
209 __u64 start;
210 sector_t blocknr;
211 void *kaddr;
212
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);
219
220 if (start == nilfs_mdt_cno(dat) && blocknr == 0)
221 nilfs_palloc_abort_free_entry(dat, req);
222 nilfs_dat_abort_entry(dat, req);
223}
224
bd8169ef
RK
225int nilfs_dat_prepare_update(struct inode *dat,
226 struct nilfs_palloc_req *oldreq,
227 struct nilfs_palloc_req *newreq)
228{
229 int ret;
230
231 ret = nilfs_dat_prepare_end(dat, oldreq);
232 if (!ret) {
233 ret = nilfs_dat_prepare_alloc(dat, newreq);
234 if (ret < 0)
235 nilfs_dat_abort_end(dat, oldreq);
236 }
237 return ret;
238}
239
240void nilfs_dat_commit_update(struct inode *dat,
241 struct nilfs_palloc_req *oldreq,
242 struct nilfs_palloc_req *newreq, int dead)
243{
244 nilfs_dat_commit_end(dat, oldreq, dead);
245 nilfs_dat_commit_alloc(dat, newreq);
246}
247
248void nilfs_dat_abort_update(struct inode *dat,
249 struct nilfs_palloc_req *oldreq,
250 struct nilfs_palloc_req *newreq)
251{
252 nilfs_dat_abort_end(dat, oldreq);
253 nilfs_dat_abort_alloc(dat, newreq);
254}
255
a17564f5
KS
256/**
257 * nilfs_dat_mark_dirty -
258 * @dat: DAT file inode
259 * @vblocknr: virtual block number
260 *
261 * Description:
262 *
263 * Return Value: On success, 0 is returned. On error, one of the following
264 * negative error codes is returned.
265 *
266 * %-EIO - I/O error.
267 *
268 * %-ENOMEM - Insufficient amount of memory available.
269 */
270int nilfs_dat_mark_dirty(struct inode *dat, __u64 vblocknr)
271{
272 struct nilfs_palloc_req req;
273 int ret;
274
275 req.pr_entry_nr = vblocknr;
276 ret = nilfs_dat_prepare_entry(dat, &req, 0);
277 if (ret == 0)
278 nilfs_dat_commit_entry(dat, &req);
279 return ret;
280}
281
282/**
283 * nilfs_dat_freev - free virtual block numbers
284 * @dat: DAT file inode
285 * @vblocknrs: array of virtual block numbers
286 * @nitems: number of virtual block numbers
287 *
288 * Description: nilfs_dat_freev() frees the virtual block numbers specified by
289 * @vblocknrs and @nitems.
290 *
291 * Return Value: On success, 0 is returned. On error, one of the following
7a65004b 292 * negative error codes is returned.
a17564f5
KS
293 *
294 * %-EIO - I/O error.
295 *
296 * %-ENOMEM - Insufficient amount of memory available.
297 *
298 * %-ENOENT - The virtual block number have not been allocated.
299 */
300int nilfs_dat_freev(struct inode *dat, __u64 *vblocknrs, size_t nitems)
301{
302 return nilfs_palloc_freev(dat, vblocknrs, nitems);
303}
304
305/**
306 * nilfs_dat_move - change a block number
307 * @dat: DAT file inode
308 * @vblocknr: virtual block number
309 * @blocknr: block number
310 *
311 * Description: nilfs_dat_move() changes the block number associated with
312 * @vblocknr to @blocknr.
313 *
314 * Return Value: On success, 0 is returned. On error, one of the following
315 * negative error codes is returned.
316 *
317 * %-EIO - I/O error.
318 *
319 * %-ENOMEM - Insufficient amount of memory available.
320 */
321int nilfs_dat_move(struct inode *dat, __u64 vblocknr, sector_t blocknr)
322{
323 struct buffer_head *entry_bh;
324 struct nilfs_dat_entry *entry;
325 void *kaddr;
326 int ret;
327
328 ret = nilfs_palloc_get_entry_block(dat, vblocknr, 0, &entry_bh);
329 if (ret < 0)
330 return ret;
c1c1d709
RK
331
332 /*
333 * The given disk block number (blocknr) is not yet written to
334 * the device at this point.
335 *
336 * To prevent nilfs_dat_translate() from returning the
337 * uncommited block number, this makes a copy of the entry
338 * buffer and redirects nilfs_dat_translate() to the copy.
339 */
340 if (!buffer_nilfs_redirected(entry_bh)) {
341 ret = nilfs_mdt_freeze_buffer(dat, entry_bh);
342 if (ret) {
343 brelse(entry_bh);
344 return ret;
345 }
346 }
347
a17564f5
KS
348 kaddr = kmap_atomic(entry_bh->b_page, KM_USER0);
349 entry = nilfs_palloc_block_get_entry(dat, vblocknr, entry_bh, kaddr);
1f5abe7e 350 if (unlikely(entry->de_blocknr == cpu_to_le64(0))) {
a17564f5
KS
351 printk(KERN_CRIT "%s: vbn = %llu, [%llu, %llu)\n", __func__,
352 (unsigned long long)vblocknr,
353 (unsigned long long)le64_to_cpu(entry->de_start),
354 (unsigned long long)le64_to_cpu(entry->de_end));
1f5abe7e
RK
355 kunmap_atomic(kaddr, KM_USER0);
356 brelse(entry_bh);
357 return -EINVAL;
a17564f5 358 }
1f5abe7e 359 WARN_ON(blocknr == 0);
a17564f5
KS
360 entry->de_blocknr = cpu_to_le64(blocknr);
361 kunmap_atomic(kaddr, KM_USER0);
362
363 nilfs_mdt_mark_buffer_dirty(entry_bh);
364 nilfs_mdt_mark_dirty(dat);
365
366 brelse(entry_bh);
367
368 return 0;
369}
370
371/**
372 * nilfs_dat_translate - translate a virtual block number to a block number
373 * @dat: DAT file inode
374 * @vblocknr: virtual block number
375 * @blocknrp: pointer to a block number
376 *
377 * Description: nilfs_dat_translate() maps the virtual block number @vblocknr
378 * to the corresponding block number.
379 *
380 * Return Value: On success, 0 is returned and the block number associated
381 * with @vblocknr is stored in the place pointed by @blocknrp. On error, one
382 * of the following negative error codes is returned.
383 *
384 * %-EIO - I/O error.
385 *
386 * %-ENOMEM - Insufficient amount of memory available.
387 *
388 * %-ENOENT - A block number associated with @vblocknr does not exist.
389 */
390int nilfs_dat_translate(struct inode *dat, __u64 vblocknr, sector_t *blocknrp)
391{
c1c1d709 392 struct buffer_head *entry_bh, *bh;
a17564f5
KS
393 struct nilfs_dat_entry *entry;
394 sector_t blocknr;
395 void *kaddr;
396 int ret;
397
398 ret = nilfs_palloc_get_entry_block(dat, vblocknr, 0, &entry_bh);
399 if (ret < 0)
400 return ret;
401
c1c1d709
RK
402 if (!nilfs_doing_gc() && buffer_nilfs_redirected(entry_bh)) {
403 bh = nilfs_mdt_get_frozen_buffer(dat, entry_bh);
404 if (bh) {
405 WARN_ON(!buffer_uptodate(bh));
406 brelse(entry_bh);
407 entry_bh = bh;
408 }
409 }
410
a17564f5
KS
411 kaddr = kmap_atomic(entry_bh->b_page, KM_USER0);
412 entry = nilfs_palloc_block_get_entry(dat, vblocknr, entry_bh, kaddr);
413 blocknr = le64_to_cpu(entry->de_blocknr);
414 if (blocknr == 0) {
415 ret = -ENOENT;
416 goto out;
417 }
086d1764 418 *blocknrp = blocknr;
a17564f5
KS
419
420 out:
421 kunmap_atomic(kaddr, KM_USER0);
422 brelse(entry_bh);
423 return ret;
424}
425
003ff182 426ssize_t nilfs_dat_get_vinfo(struct inode *dat, void *buf, unsigned visz,
a17564f5
KS
427 size_t nvi)
428{
429 struct buffer_head *entry_bh;
430 struct nilfs_dat_entry *entry;
003ff182 431 struct nilfs_vinfo *vinfo = buf;
a17564f5
KS
432 __u64 first, last;
433 void *kaddr;
434 unsigned long entries_per_block = NILFS_MDT(dat)->mi_entries_per_block;
435 int i, j, n, ret;
436
437 for (i = 0; i < nvi; i += n) {
003ff182 438 ret = nilfs_palloc_get_entry_block(dat, vinfo->vi_vblocknr,
a17564f5
KS
439 0, &entry_bh);
440 if (ret < 0)
441 return ret;
442 kaddr = kmap_atomic(entry_bh->b_page, KM_USER0);
443 /* last virtual block number in this block */
003ff182 444 first = vinfo->vi_vblocknr;
a17564f5
KS
445 do_div(first, entries_per_block);
446 first *= entries_per_block;
447 last = first + entries_per_block - 1;
448 for (j = i, n = 0;
003ff182
RK
449 j < nvi && vinfo->vi_vblocknr >= first &&
450 vinfo->vi_vblocknr <= last;
451 j++, n++, vinfo = (void *)vinfo + visz) {
a17564f5 452 entry = nilfs_palloc_block_get_entry(
003ff182
RK
453 dat, vinfo->vi_vblocknr, entry_bh, kaddr);
454 vinfo->vi_start = le64_to_cpu(entry->de_start);
455 vinfo->vi_end = le64_to_cpu(entry->de_end);
456 vinfo->vi_blocknr = le64_to_cpu(entry->de_blocknr);
a17564f5
KS
457 }
458 kunmap_atomic(kaddr, KM_USER0);
459 brelse(entry_bh);
460 }
461
462 return nvi;
463}
79739565 464
8707df38
RK
465/**
466 * nilfs_dat_read - read dat inode
467 * @dat: dat inode
468 * @raw_inode: on-disk dat inode
469 */
470int nilfs_dat_read(struct inode *dat, struct nilfs_inode *raw_inode)
471{
472 return nilfs_read_inode_common(dat, raw_inode);
473}
474
79739565
RK
475/**
476 * nilfs_dat_new - create dat file
477 * @nilfs: nilfs object
478 * @entry_size: size of a dat entry
479 */
480struct inode *nilfs_dat_new(struct the_nilfs *nilfs, size_t entry_size)
481{
482 static struct lock_class_key dat_lock_key;
483 struct inode *dat;
8908b2f7 484 struct nilfs_dat_info *di;
79739565
RK
485 int err;
486
8908b2f7 487 dat = nilfs_mdt_new(nilfs, NULL, NILFS_DAT_INO, sizeof(*di));
79739565
RK
488 if (dat) {
489 err = nilfs_palloc_init_blockgroup(dat, entry_size);
490 if (unlikely(err)) {
491 nilfs_mdt_destroy(dat);
492 return NULL;
493 }
8908b2f7
RK
494
495 di = NILFS_DAT_I(dat);
496 lockdep_set_class(&di->mi.mi_sem, &dat_lock_key);
497 nilfs_palloc_setup_cache(dat, &di->palloc_cache);
c1c1d709 498 nilfs_mdt_setup_shadow_map(dat, &di->shadow);
79739565
RK
499 }
500 return dat;
501}