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 * Written by Koji Sato.
19 #include <linux/types.h>
20 #include <linux/buffer_head.h>
21 #include <linux/string.h>
22 #include <linux/errno.h>
29 #define NILFS_CNO_MIN ((__u64)1)
30 #define NILFS_CNO_MAX (~(__u64)0)
33 * struct nilfs_dat_info - on-memory private data of DAT file
34 * @mi: on-memory private data of metadata file
35 * @palloc_cache: persistent object allocator cache of DAT file
36 * @shadow: shadow map of DAT file
38 struct nilfs_dat_info {
39 struct nilfs_mdt_info mi;
40 struct nilfs_palloc_cache palloc_cache;
41 struct nilfs_shadow_map shadow;
44 static inline struct nilfs_dat_info *NILFS_DAT_I(struct inode *dat)
46 return (struct nilfs_dat_info *)NILFS_MDT(dat);
49 static int nilfs_dat_prepare_entry(struct inode *dat,
50 struct nilfs_palloc_req *req, int create)
52 return nilfs_palloc_get_entry_block(dat, req->pr_entry_nr,
53 create, &req->pr_entry_bh);
56 static void nilfs_dat_commit_entry(struct inode *dat,
57 struct nilfs_palloc_req *req)
59 mark_buffer_dirty(req->pr_entry_bh);
60 nilfs_mdt_mark_dirty(dat);
61 brelse(req->pr_entry_bh);
64 static void nilfs_dat_abort_entry(struct inode *dat,
65 struct nilfs_palloc_req *req)
67 brelse(req->pr_entry_bh);
70 int nilfs_dat_prepare_alloc(struct inode *dat, struct nilfs_palloc_req *req)
74 ret = nilfs_palloc_prepare_alloc_entry(dat, req);
78 ret = nilfs_dat_prepare_entry(dat, req, 1);
80 nilfs_palloc_abort_alloc_entry(dat, req);
85 void nilfs_dat_commit_alloc(struct inode *dat, struct nilfs_palloc_req *req)
87 struct nilfs_dat_entry *entry;
90 kaddr = kmap_atomic(req->pr_entry_bh->b_page);
91 entry = nilfs_palloc_block_get_entry(dat, req->pr_entry_nr,
92 req->pr_entry_bh, kaddr);
93 entry->de_start = cpu_to_le64(NILFS_CNO_MIN);
94 entry->de_end = cpu_to_le64(NILFS_CNO_MAX);
95 entry->de_blocknr = cpu_to_le64(0);
98 nilfs_palloc_commit_alloc_entry(dat, req);
99 nilfs_dat_commit_entry(dat, req);
102 void nilfs_dat_abort_alloc(struct inode *dat, struct nilfs_palloc_req *req)
104 nilfs_dat_abort_entry(dat, req);
105 nilfs_palloc_abort_alloc_entry(dat, req);
108 static void nilfs_dat_commit_free(struct inode *dat,
109 struct nilfs_palloc_req *req)
111 struct nilfs_dat_entry *entry;
114 kaddr = kmap_atomic(req->pr_entry_bh->b_page);
115 entry = nilfs_palloc_block_get_entry(dat, req->pr_entry_nr,
116 req->pr_entry_bh, kaddr);
117 entry->de_start = cpu_to_le64(NILFS_CNO_MIN);
118 entry->de_end = cpu_to_le64(NILFS_CNO_MIN);
119 entry->de_blocknr = cpu_to_le64(0);
120 kunmap_atomic(kaddr);
122 nilfs_dat_commit_entry(dat, req);
124 if (unlikely(req->pr_desc_bh == NULL || req->pr_bitmap_bh == NULL)) {
125 nilfs_error(dat->i_sb,
126 "state inconsistency probably due to duplicate use of vblocknr = %llu",
127 (unsigned long long)req->pr_entry_nr);
130 nilfs_palloc_commit_free_entry(dat, req);
133 int nilfs_dat_prepare_start(struct inode *dat, struct nilfs_palloc_req *req)
137 ret = nilfs_dat_prepare_entry(dat, req, 0);
138 WARN_ON(ret == -ENOENT);
142 void nilfs_dat_commit_start(struct inode *dat, struct nilfs_palloc_req *req,
145 struct nilfs_dat_entry *entry;
148 kaddr = kmap_atomic(req->pr_entry_bh->b_page);
149 entry = nilfs_palloc_block_get_entry(dat, req->pr_entry_nr,
150 req->pr_entry_bh, kaddr);
151 entry->de_start = cpu_to_le64(nilfs_mdt_cno(dat));
152 entry->de_blocknr = cpu_to_le64(blocknr);
153 kunmap_atomic(kaddr);
155 nilfs_dat_commit_entry(dat, req);
158 int nilfs_dat_prepare_end(struct inode *dat, struct nilfs_palloc_req *req)
160 struct nilfs_dat_entry *entry;
165 ret = nilfs_dat_prepare_entry(dat, req, 0);
167 WARN_ON(ret == -ENOENT);
171 kaddr = kmap_atomic(req->pr_entry_bh->b_page);
172 entry = nilfs_palloc_block_get_entry(dat, req->pr_entry_nr,
173 req->pr_entry_bh, kaddr);
174 blocknr = le64_to_cpu(entry->de_blocknr);
175 kunmap_atomic(kaddr);
178 ret = nilfs_palloc_prepare_free_entry(dat, req);
180 nilfs_dat_abort_entry(dat, req);
188 void nilfs_dat_commit_end(struct inode *dat, struct nilfs_palloc_req *req,
191 struct nilfs_dat_entry *entry;
196 kaddr = kmap_atomic(req->pr_entry_bh->b_page);
197 entry = nilfs_palloc_block_get_entry(dat, req->pr_entry_nr,
198 req->pr_entry_bh, kaddr);
199 end = start = le64_to_cpu(entry->de_start);
201 end = nilfs_mdt_cno(dat);
202 WARN_ON(start > end);
204 entry->de_end = cpu_to_le64(end);
205 blocknr = le64_to_cpu(entry->de_blocknr);
206 kunmap_atomic(kaddr);
209 nilfs_dat_commit_free(dat, req);
211 nilfs_dat_commit_entry(dat, req);
214 void nilfs_dat_abort_end(struct inode *dat, struct nilfs_palloc_req *req)
216 struct nilfs_dat_entry *entry;
221 kaddr = kmap_atomic(req->pr_entry_bh->b_page);
222 entry = nilfs_palloc_block_get_entry(dat, req->pr_entry_nr,
223 req->pr_entry_bh, kaddr);
224 start = le64_to_cpu(entry->de_start);
225 blocknr = le64_to_cpu(entry->de_blocknr);
226 kunmap_atomic(kaddr);
228 if (start == nilfs_mdt_cno(dat) && blocknr == 0)
229 nilfs_palloc_abort_free_entry(dat, req);
230 nilfs_dat_abort_entry(dat, req);
233 int nilfs_dat_prepare_update(struct inode *dat,
234 struct nilfs_palloc_req *oldreq,
235 struct nilfs_palloc_req *newreq)
239 ret = nilfs_dat_prepare_end(dat, oldreq);
241 ret = nilfs_dat_prepare_alloc(dat, newreq);
243 nilfs_dat_abort_end(dat, oldreq);
248 void nilfs_dat_commit_update(struct inode *dat,
249 struct nilfs_palloc_req *oldreq,
250 struct nilfs_palloc_req *newreq, int dead)
252 nilfs_dat_commit_end(dat, oldreq, dead);
253 nilfs_dat_commit_alloc(dat, newreq);
256 void nilfs_dat_abort_update(struct inode *dat,
257 struct nilfs_palloc_req *oldreq,
258 struct nilfs_palloc_req *newreq)
260 nilfs_dat_abort_end(dat, oldreq);
261 nilfs_dat_abort_alloc(dat, newreq);
265 * nilfs_dat_mark_dirty -
266 * @dat: DAT file inode
267 * @vblocknr: virtual block number
271 * Return Value: On success, 0 is returned. On error, one of the following
272 * negative error codes is returned.
276 * %-ENOMEM - Insufficient amount of memory available.
278 int nilfs_dat_mark_dirty(struct inode *dat, __u64 vblocknr)
280 struct nilfs_palloc_req req;
283 req.pr_entry_nr = vblocknr;
284 ret = nilfs_dat_prepare_entry(dat, &req, 0);
286 nilfs_dat_commit_entry(dat, &req);
291 * nilfs_dat_freev - free virtual block numbers
292 * @dat: DAT file inode
293 * @vblocknrs: array of virtual block numbers
294 * @nitems: number of virtual block numbers
296 * Description: nilfs_dat_freev() frees the virtual block numbers specified by
297 * @vblocknrs and @nitems.
299 * Return Value: On success, 0 is returned. On error, one of the following
300 * negative error codes is returned.
304 * %-ENOMEM - Insufficient amount of memory available.
306 * %-ENOENT - The virtual block number have not been allocated.
308 int nilfs_dat_freev(struct inode *dat, __u64 *vblocknrs, size_t nitems)
310 return nilfs_palloc_freev(dat, vblocknrs, nitems);
314 * nilfs_dat_move - change a block number
315 * @dat: DAT file inode
316 * @vblocknr: virtual block number
317 * @blocknr: block number
319 * Description: nilfs_dat_move() changes the block number associated with
320 * @vblocknr to @blocknr.
322 * Return Value: On success, 0 is returned. On error, one of the following
323 * negative error codes is returned.
327 * %-ENOMEM - Insufficient amount of memory available.
329 int nilfs_dat_move(struct inode *dat, __u64 vblocknr, sector_t blocknr)
331 struct buffer_head *entry_bh;
332 struct nilfs_dat_entry *entry;
336 ret = nilfs_palloc_get_entry_block(dat, vblocknr, 0, &entry_bh);
341 * The given disk block number (blocknr) is not yet written to
342 * the device at this point.
344 * To prevent nilfs_dat_translate() from returning the
345 * uncommitted block number, this makes a copy of the entry
346 * buffer and redirects nilfs_dat_translate() to the copy.
348 if (!buffer_nilfs_redirected(entry_bh)) {
349 ret = nilfs_mdt_freeze_buffer(dat, entry_bh);
356 kaddr = kmap_atomic(entry_bh->b_page);
357 entry = nilfs_palloc_block_get_entry(dat, vblocknr, entry_bh, kaddr);
358 if (unlikely(entry->de_blocknr == cpu_to_le64(0))) {
359 nilfs_msg(dat->i_sb, KERN_CRIT,
360 "%s: invalid vblocknr = %llu, [%llu, %llu)",
361 __func__, (unsigned long long)vblocknr,
362 (unsigned long long)le64_to_cpu(entry->de_start),
363 (unsigned long long)le64_to_cpu(entry->de_end));
364 kunmap_atomic(kaddr);
368 WARN_ON(blocknr == 0);
369 entry->de_blocknr = cpu_to_le64(blocknr);
370 kunmap_atomic(kaddr);
372 mark_buffer_dirty(entry_bh);
373 nilfs_mdt_mark_dirty(dat);
381 * nilfs_dat_translate - translate a virtual block number to a block number
382 * @dat: DAT file inode
383 * @vblocknr: virtual block number
384 * @blocknrp: pointer to a block number
386 * Description: nilfs_dat_translate() maps the virtual block number @vblocknr
387 * to the corresponding block number.
389 * Return Value: On success, 0 is returned and the block number associated
390 * with @vblocknr is stored in the place pointed by @blocknrp. On error, one
391 * of the following negative error codes is returned.
395 * %-ENOMEM - Insufficient amount of memory available.
397 * %-ENOENT - A block number associated with @vblocknr does not exist.
399 int nilfs_dat_translate(struct inode *dat, __u64 vblocknr, sector_t *blocknrp)
401 struct buffer_head *entry_bh, *bh;
402 struct nilfs_dat_entry *entry;
407 ret = nilfs_palloc_get_entry_block(dat, vblocknr, 0, &entry_bh);
411 if (!nilfs_doing_gc() && buffer_nilfs_redirected(entry_bh)) {
412 bh = nilfs_mdt_get_frozen_buffer(dat, entry_bh);
414 WARN_ON(!buffer_uptodate(bh));
420 kaddr = kmap_atomic(entry_bh->b_page);
421 entry = nilfs_palloc_block_get_entry(dat, vblocknr, entry_bh, kaddr);
422 blocknr = le64_to_cpu(entry->de_blocknr);
430 kunmap_atomic(kaddr);
435 ssize_t nilfs_dat_get_vinfo(struct inode *dat, void *buf, unsigned int visz,
438 struct buffer_head *entry_bh;
439 struct nilfs_dat_entry *entry;
440 struct nilfs_vinfo *vinfo = buf;
443 unsigned long entries_per_block = NILFS_MDT(dat)->mi_entries_per_block;
446 for (i = 0; i < nvi; i += n) {
447 ret = nilfs_palloc_get_entry_block(dat, vinfo->vi_vblocknr,
451 kaddr = kmap_atomic(entry_bh->b_page);
452 /* last virtual block number in this block */
453 first = vinfo->vi_vblocknr;
454 do_div(first, entries_per_block);
455 first *= entries_per_block;
456 last = first + entries_per_block - 1;
458 j < nvi && vinfo->vi_vblocknr >= first &&
459 vinfo->vi_vblocknr <= last;
460 j++, n++, vinfo = (void *)vinfo + visz) {
461 entry = nilfs_palloc_block_get_entry(
462 dat, vinfo->vi_vblocknr, entry_bh, kaddr);
463 vinfo->vi_start = le64_to_cpu(entry->de_start);
464 vinfo->vi_end = le64_to_cpu(entry->de_end);
465 vinfo->vi_blocknr = le64_to_cpu(entry->de_blocknr);
467 kunmap_atomic(kaddr);
475 * nilfs_dat_read - read or get dat inode
476 * @sb: super block instance
477 * @entry_size: size of a dat entry
478 * @raw_inode: on-disk dat inode
479 * @inodep: buffer to store the inode
481 int nilfs_dat_read(struct super_block *sb, size_t entry_size,
482 struct nilfs_inode *raw_inode, struct inode **inodep)
484 static struct lock_class_key dat_lock_key;
486 struct nilfs_dat_info *di;
489 if (entry_size > sb->s_blocksize) {
490 nilfs_msg(sb, KERN_ERR, "too large DAT entry size: %zu bytes",
493 } else if (entry_size < NILFS_MIN_DAT_ENTRY_SIZE) {
494 nilfs_msg(sb, KERN_ERR, "too small DAT entry size: %zu bytes",
499 dat = nilfs_iget_locked(sb, NULL, NILFS_DAT_INO);
502 if (!(dat->i_state & I_NEW))
505 err = nilfs_mdt_init(dat, NILFS_MDT_GFP, sizeof(*di));
509 err = nilfs_palloc_init_blockgroup(dat, entry_size);
513 di = NILFS_DAT_I(dat);
514 lockdep_set_class(&di->mi.mi_sem, &dat_lock_key);
515 nilfs_palloc_setup_cache(dat, &di->palloc_cache);
516 err = nilfs_mdt_setup_shadow_map(dat, &di->shadow);
520 err = nilfs_read_inode_common(dat, raw_inode);
524 unlock_new_inode(dat);