aboutsummaryrefslogtreecommitdiffstats
path: root/fs/nilfs2/mdt.c
diff options
context:
space:
mode:
authorRyusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>2009-04-06 22:01:28 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2009-04-07 11:31:13 -0400
commit5eb563f5f299a2ed488d9eb52acede45ccb14c7b (patch)
tree965243b138a7beb5f8e2700160903a051e0510de /fs/nilfs2/mdt.c
parent0bd49f9446130a6a3914eb07b54db489e3222b34 (diff)
nilfs2: meta data file
This adds the meta data file, which serves common buffer functions to the DAT, sufile, cpfile, ifile, and so forth. Signed-off-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'fs/nilfs2/mdt.c')
-rw-r--r--fs/nilfs2/mdt.c562
1 files changed, 562 insertions, 0 deletions
diff --git a/fs/nilfs2/mdt.c b/fs/nilfs2/mdt.c
new file mode 100644
index 000000000000..6ab847578615
--- /dev/null
+++ b/fs/nilfs2/mdt.c
@@ -0,0 +1,562 @@
1/*
2 * mdt.c - meta data file for NILFS
3 *
4 * Copyright (C) 2005-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 Ryusuke Konishi <ryusuke@osrg.net>
21 */
22
23#include <linux/buffer_head.h>
24#include <linux/mpage.h>
25#include <linux/mm.h>
26#include <linux/writeback.h>
27#include <linux/backing-dev.h>
28#include <linux/swap.h>
29#include "nilfs.h"
30#include "segment.h"
31#include "page.h"
32#include "mdt.h"
33
34
35#define NILFS_MDT_MAX_RA_BLOCKS (16 - 1)
36
37#define INIT_UNUSED_INODE_FIELDS
38
39static int
40nilfs_mdt_insert_new_block(struct inode *inode, unsigned long block,
41 struct buffer_head *bh,
42 void (*init_block)(struct inode *,
43 struct buffer_head *, void *))
44{
45 struct nilfs_inode_info *ii = NILFS_I(inode);
46 void *kaddr;
47 int ret;
48
49 /* Caller exclude read accesses using page lock */
50
51 /* set_buffer_new(bh); */
52 bh->b_blocknr = 0;
53
54 ret = nilfs_bmap_insert(ii->i_bmap, block, (unsigned long)bh);
55 if (unlikely(ret))
56 return ret;
57
58 set_buffer_mapped(bh);
59
60 kaddr = kmap_atomic(bh->b_page, KM_USER0);
61 memset(kaddr + bh_offset(bh), 0, 1 << inode->i_blkbits);
62 if (init_block)
63 init_block(inode, bh, kaddr);
64 flush_dcache_page(bh->b_page);
65 kunmap_atomic(kaddr, KM_USER0);
66
67 set_buffer_uptodate(bh);
68 nilfs_mark_buffer_dirty(bh);
69 nilfs_mdt_mark_dirty(inode);
70 return 0;
71}
72
73static int nilfs_mdt_create_block(struct inode *inode, unsigned long block,
74 struct buffer_head **out_bh,
75 void (*init_block)(struct inode *,
76 struct buffer_head *,
77 void *))
78{
79 struct the_nilfs *nilfs = NILFS_MDT(inode)->mi_nilfs;
80 struct nilfs_sb_info *writer = NULL;
81 struct super_block *sb = inode->i_sb;
82 struct nilfs_transaction_info ti;
83 struct buffer_head *bh;
84 int err;
85
86 if (!sb) {
87 writer = nilfs_get_writer(nilfs);
88 if (!writer) {
89 err = -EROFS;
90 goto out;
91 }
92 sb = writer->s_super;
93 }
94
95 nilfs_transaction_begin(sb, &ti, 0);
96
97 err = -ENOMEM;
98 bh = nilfs_grab_buffer(inode, inode->i_mapping, block, 0);
99 if (unlikely(!bh))
100 goto failed_unlock;
101
102 err = -EEXIST;
103 if (buffer_uptodate(bh) || buffer_mapped(bh))
104 goto failed_bh;
105#if 0
106 /* The uptodate flag is not protected by the page lock, but
107 the mapped flag is. Thus, we don't have to wait the buffer. */
108 wait_on_buffer(bh);
109 if (buffer_uptodate(bh))
110 goto failed_bh;
111#endif
112
113 bh->b_bdev = nilfs->ns_bdev;
114 err = nilfs_mdt_insert_new_block(inode, block, bh, init_block);
115 if (likely(!err)) {
116 get_bh(bh);
117 *out_bh = bh;
118 }
119
120 failed_bh:
121 unlock_page(bh->b_page);
122 page_cache_release(bh->b_page);
123 brelse(bh);
124
125 failed_unlock:
126 nilfs_transaction_end(sb, !err);
127 if (writer)
128 nilfs_put_writer(nilfs);
129 out:
130 return err;
131}
132
133static int
134nilfs_mdt_submit_block(struct inode *inode, unsigned long blkoff,
135 int mode, struct buffer_head **out_bh)
136{
137 struct buffer_head *bh;
138 unsigned long blknum = 0;
139 int ret = -ENOMEM;
140
141 bh = nilfs_grab_buffer(inode, inode->i_mapping, blkoff, 0);
142 if (unlikely(!bh))
143 goto failed;
144
145 ret = -EEXIST; /* internal code */
146 if (buffer_uptodate(bh))
147 goto out;
148
149 if (mode == READA) {
150 if (!trylock_buffer(bh)) {
151 ret = -EBUSY;
152 goto failed_bh;
153 }
154 } else {
155 BUG_ON(mode != READ);
156 lock_buffer(bh);
157 }
158
159 if (buffer_uptodate(bh)) {
160 unlock_buffer(bh);
161 goto out;
162 }
163 if (!buffer_mapped(bh)) { /* unused buffer */
164 ret = nilfs_bmap_lookup(NILFS_I(inode)->i_bmap, blkoff,
165 &blknum);
166 if (unlikely(ret)) {
167 unlock_buffer(bh);
168 goto failed_bh;
169 }
170 bh->b_bdev = NILFS_MDT(inode)->mi_nilfs->ns_bdev;
171 bh->b_blocknr = blknum;
172 set_buffer_mapped(bh);
173 }
174
175 bh->b_end_io = end_buffer_read_sync;
176 get_bh(bh);
177 submit_bh(mode, bh);
178 ret = 0;
179 out:
180 get_bh(bh);
181 *out_bh = bh;
182
183 failed_bh:
184 unlock_page(bh->b_page);
185 page_cache_release(bh->b_page);
186 brelse(bh);
187 failed:
188 return ret;
189}
190
191static int nilfs_mdt_read_block(struct inode *inode, unsigned long block,
192 struct buffer_head **out_bh)
193{
194 struct buffer_head *first_bh, *bh;
195 unsigned long blkoff;
196 int i, nr_ra_blocks = NILFS_MDT_MAX_RA_BLOCKS;
197 int err;
198
199 err = nilfs_mdt_submit_block(inode, block, READ, &first_bh);
200 if (err == -EEXIST) /* internal code */
201 goto out;
202
203 if (unlikely(err))
204 goto failed;
205
206 blkoff = block + 1;
207 for (i = 0; i < nr_ra_blocks; i++, blkoff++) {
208 err = nilfs_mdt_submit_block(inode, blkoff, READA, &bh);
209 if (likely(!err || err == -EEXIST))
210 brelse(bh);
211 else if (err != -EBUSY)
212 break; /* abort readahead if bmap lookup failed */
213
214 if (!buffer_locked(first_bh))
215 goto out_no_wait;
216 }
217
218 wait_on_buffer(first_bh);
219
220 out_no_wait:
221 err = -EIO;
222 if (!buffer_uptodate(first_bh))
223 goto failed_bh;
224 out:
225 *out_bh = first_bh;
226 return 0;
227
228 failed_bh:
229 brelse(first_bh);
230 failed:
231 return err;
232}
233
234/**
235 * nilfs_mdt_get_block - read or create a buffer on meta data file.
236 * @inode: inode of the meta data file
237 * @blkoff: block offset
238 * @create: create flag
239 * @init_block: initializer used for newly allocated block
240 * @out_bh: output of a pointer to the buffer_head
241 *
242 * nilfs_mdt_get_block() looks up the specified buffer and tries to create
243 * a new buffer if @create is not zero. On success, the returned buffer is
244 * assured to be either existing or formatted using a buffer lock on success.
245 * @out_bh is substituted only when zero is returned.
246 *
247 * Return Value: On success, it returns 0. On error, the following negative
248 * error code is returned.
249 *
250 * %-ENOMEM - Insufficient memory available.
251 *
252 * %-EIO - I/O error
253 *
254 * %-ENOENT - the specified block does not exist (hole block)
255 *
256 * %-EINVAL - bmap is broken. (the caller should call nilfs_error())
257 *
258 * %-EROFS - Read only filesystem (for create mode)
259 */
260int nilfs_mdt_get_block(struct inode *inode, unsigned long blkoff, int create,
261 void (*init_block)(struct inode *,
262 struct buffer_head *, void *),
263 struct buffer_head **out_bh)
264{
265 int ret;
266
267 /* Should be rewritten with merging nilfs_mdt_read_block() */
268 retry:
269 ret = nilfs_mdt_read_block(inode, blkoff, out_bh);
270 if (!create || ret != -ENOENT)
271 return ret;
272
273 ret = nilfs_mdt_create_block(inode, blkoff, out_bh, init_block);
274 if (unlikely(ret == -EEXIST)) {
275 /* create = 0; */ /* limit read-create loop retries */
276 goto retry;
277 }
278 return ret;
279}
280
281/**
282 * nilfs_mdt_delete_block - make a hole on the meta data file.
283 * @inode: inode of the meta data file
284 * @block: block offset
285 *
286 * Return Value: On success, zero is returned.
287 * On error, one of the following negative error code is returned.
288 *
289 * %-ENOMEM - Insufficient memory available.
290 *
291 * %-EIO - I/O error
292 *
293 * %-EINVAL - bmap is broken. (the caller should call nilfs_error())
294 */
295int nilfs_mdt_delete_block(struct inode *inode, unsigned long block)
296{
297 struct nilfs_inode_info *ii = NILFS_I(inode);
298 int err;
299
300 err = nilfs_bmap_delete(ii->i_bmap, block);
301 if (likely(!err)) {
302 nilfs_mdt_mark_dirty(inode);
303 nilfs_mdt_forget_block(inode, block);
304 }
305 return err;
306}
307
308/**
309 * nilfs_mdt_forget_block - discard dirty state and try to remove the page
310 * @inode: inode of the meta data file
311 * @block: block offset
312 *
313 * nilfs_mdt_forget_block() clears a dirty flag of the specified buffer, and
314 * tries to release the page including the buffer from a page cache.
315 *
316 * Return Value: On success, 0 is returned. On error, one of the following
317 * negative error code is returned.
318 *
319 * %-EBUSY - page has an active buffer.
320 *
321 * %-ENOENT - page cache has no page addressed by the offset.
322 */
323int nilfs_mdt_forget_block(struct inode *inode, unsigned long block)
324{
325 pgoff_t index = (pgoff_t)block >>
326 (PAGE_CACHE_SHIFT - inode->i_blkbits);
327 struct page *page;
328 unsigned long first_block;
329 int ret = 0;
330 int still_dirty;
331
332 page = find_lock_page(inode->i_mapping, index);
333 if (!page)
334 return -ENOENT;
335
336 wait_on_page_writeback(page);
337
338 first_block = (unsigned long)index <<
339 (PAGE_CACHE_SHIFT - inode->i_blkbits);
340 if (page_has_buffers(page)) {
341 struct buffer_head *bh;
342
343 bh = nilfs_page_get_nth_block(page, block - first_block);
344 nilfs_forget_buffer(bh);
345 }
346 still_dirty = PageDirty(page);
347 unlock_page(page);
348 page_cache_release(page);
349
350 if (still_dirty ||
351 invalidate_inode_pages2_range(inode->i_mapping, index, index) != 0)
352 ret = -EBUSY;
353 return ret;
354}
355
356/**
357 * nilfs_mdt_mark_block_dirty - mark a block on the meta data file dirty.
358 * @inode: inode of the meta data file
359 * @block: block offset
360 *
361 * Return Value: On success, it returns 0. On error, the following negative
362 * error code is returned.
363 *
364 * %-ENOMEM - Insufficient memory available.
365 *
366 * %-EIO - I/O error
367 *
368 * %-ENOENT - the specified block does not exist (hole block)
369 *
370 * %-EINVAL - bmap is broken. (the caller should call nilfs_error())
371 */
372int nilfs_mdt_mark_block_dirty(struct inode *inode, unsigned long block)
373{
374 struct buffer_head *bh;
375 int err;
376
377 err = nilfs_mdt_read_block(inode, block, &bh);
378 if (unlikely(err))
379 return err;
380 nilfs_mark_buffer_dirty(bh);
381 nilfs_mdt_mark_dirty(inode);
382 brelse(bh);
383 return 0;
384}
385
386int nilfs_mdt_fetch_dirty(struct inode *inode)
387{
388 struct nilfs_inode_info *ii = NILFS_I(inode);
389
390 if (nilfs_bmap_test_and_clear_dirty(ii->i_bmap)) {
391 set_bit(NILFS_I_DIRTY, &ii->i_state);
392 return 1;
393 }
394 return test_bit(NILFS_I_DIRTY, &ii->i_state);
395}
396
397static int
398nilfs_mdt_write_page(struct page *page, struct writeback_control *wbc)
399{
400 struct inode *inode = container_of(page->mapping,
401 struct inode, i_data);
402 struct super_block *sb = inode->i_sb;
403 struct nilfs_sb_info *writer = NULL;
404 int err = 0;
405
406 redirty_page_for_writepage(wbc, page);
407 unlock_page(page);
408
409 if (page->mapping->assoc_mapping)
410 return 0; /* Do not request flush for shadow page cache */
411 if (!sb) {
412 writer = nilfs_get_writer(NILFS_MDT(inode)->mi_nilfs);
413 if (!writer)
414 return -EROFS;
415 sb = writer->s_super;
416 }
417
418 if (wbc->sync_mode == WB_SYNC_ALL)
419 err = nilfs_construct_segment(sb);
420 else if (wbc->for_reclaim)
421 nilfs_flush_segment(sb, inode->i_ino);
422
423 if (writer)
424 nilfs_put_writer(NILFS_MDT(inode)->mi_nilfs);
425 return err;
426}
427
428
429static struct address_space_operations def_mdt_aops = {
430 .writepage = nilfs_mdt_write_page,
431};
432
433static struct inode_operations def_mdt_iops;
434static struct file_operations def_mdt_fops;
435
436/*
437 * NILFS2 uses pseudo inodes for meta data files such as DAT, cpfile, sufile,
438 * ifile, or gcinodes. This allows the B-tree code and segment constructor
439 * to treat them like regular files, and this helps to simplify the
440 * implementation.
441 * On the other hand, some of the pseudo inodes have an irregular point:
442 * They don't have valid inode->i_sb pointer because their lifetimes are
443 * longer than those of the super block structs; they may continue for
444 * several consecutive mounts/umounts. This would need discussions.
445 */
446struct inode *
447nilfs_mdt_new_common(struct the_nilfs *nilfs, struct super_block *sb,
448 ino_t ino, gfp_t gfp_mask)
449{
450 struct inode *inode = nilfs_alloc_inode(sb);
451
452 if (!inode)
453 return NULL;
454 else {
455 struct address_space * const mapping = &inode->i_data;
456 struct nilfs_mdt_info *mi = kzalloc(sizeof(*mi), GFP_NOFS);
457
458 if (!mi) {
459 nilfs_destroy_inode(inode);
460 return NULL;
461 }
462 mi->mi_nilfs = nilfs;
463 init_rwsem(&mi->mi_sem);
464
465 inode->i_sb = sb; /* sb may be NULL for some meta data files */
466 inode->i_blkbits = nilfs->ns_blocksize_bits;
467 inode->i_flags = 0;
468 atomic_set(&inode->i_count, 1);
469 inode->i_nlink = 1;
470 inode->i_ino = ino;
471 inode->i_mode = S_IFREG;
472 inode->i_private = mi;
473
474#ifdef INIT_UNUSED_INODE_FIELDS
475 atomic_set(&inode->i_writecount, 0);
476 inode->i_size = 0;
477 inode->i_blocks = 0;
478 inode->i_bytes = 0;
479 inode->i_generation = 0;
480#ifdef CONFIG_QUOTA
481 memset(&inode->i_dquot, 0, sizeof(inode->i_dquot));
482#endif
483 inode->i_pipe = NULL;
484 inode->i_bdev = NULL;
485 inode->i_cdev = NULL;
486 inode->i_rdev = 0;
487#ifdef CONFIG_SECURITY
488 inode->i_security = NULL;
489#endif
490 inode->dirtied_when = 0;
491
492 INIT_LIST_HEAD(&inode->i_list);
493 INIT_LIST_HEAD(&inode->i_sb_list);
494 inode->i_state = 0;
495#endif
496
497 spin_lock_init(&inode->i_lock);
498 mutex_init(&inode->i_mutex);
499 init_rwsem(&inode->i_alloc_sem);
500
501 mapping->host = NULL; /* instead of inode */
502 mapping->flags = 0;
503 mapping_set_gfp_mask(mapping, gfp_mask);
504 mapping->assoc_mapping = NULL;
505 mapping->backing_dev_info = nilfs->ns_bdi;
506
507 inode->i_mapping = mapping;
508 }
509
510 return inode;
511}
512
513struct inode *nilfs_mdt_new(struct the_nilfs *nilfs, struct super_block *sb,
514 ino_t ino, gfp_t gfp_mask)
515{
516 struct inode *inode = nilfs_mdt_new_common(nilfs, sb, ino, gfp_mask);
517
518 if (!inode)
519 return NULL;
520
521 inode->i_op = &def_mdt_iops;
522 inode->i_fop = &def_mdt_fops;
523 inode->i_mapping->a_ops = &def_mdt_aops;
524 return inode;
525}
526
527void nilfs_mdt_set_entry_size(struct inode *inode, unsigned entry_size,
528 unsigned header_size)
529{
530 struct nilfs_mdt_info *mi = NILFS_MDT(inode);
531
532 mi->mi_entry_size = entry_size;
533 mi->mi_entries_per_block = (1 << inode->i_blkbits) / entry_size;
534 mi->mi_first_entry_offset = DIV_ROUND_UP(header_size, entry_size);
535}
536
537void nilfs_mdt_set_shadow(struct inode *orig, struct inode *shadow)
538{
539 shadow->i_mapping->assoc_mapping = orig->i_mapping;
540 NILFS_I(shadow)->i_btnode_cache.assoc_mapping =
541 &NILFS_I(orig)->i_btnode_cache;
542}
543
544void nilfs_mdt_clear(struct inode *inode)
545{
546 struct nilfs_inode_info *ii = NILFS_I(inode);
547
548 invalidate_mapping_pages(inode->i_mapping, 0, -1);
549 truncate_inode_pages(inode->i_mapping, 0);
550
551 nilfs_bmap_clear(ii->i_bmap);
552 nilfs_btnode_cache_clear(&ii->i_btnode_cache);
553}
554
555void nilfs_mdt_destroy(struct inode *inode)
556{
557 struct nilfs_mdt_info *mdi = NILFS_MDT(inode);
558
559 kfree(mdi->mi_bgl); /* kfree(NULL) is safe */
560 kfree(mdi);
561 nilfs_destroy_inode(inode);
562}