blob: 0a2ccfc0d6f9a60de9affe30e64501d74c7de83c [file] [log] [blame]
Ryusuke Konishi5eb563f2009-04-06 19:01:28 -07001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Ryusuke Konishi5eb563f2009-04-06 19:01:28 -070030#include "nilfs.h"
Ryusuke Konishi05d0e942010-07-10 20:52:09 +090031#include "btnode.h"
Ryusuke Konishi5eb563f2009-04-06 19:01:28 -070032#include "segment.h"
33#include "page.h"
34#include "mdt.h"
35
36
37#define NILFS_MDT_MAX_RA_BLOCKS (16 - 1)
38
39#define INIT_UNUSED_INODE_FIELDS
40
41static int
42nilfs_mdt_insert_new_block(struct inode *inode, unsigned long block,
43 struct buffer_head *bh,
44 void (*init_block)(struct inode *,
45 struct buffer_head *, void *))
46{
47 struct nilfs_inode_info *ii = NILFS_I(inode);
48 void *kaddr;
49 int ret;
50
51 /* Caller exclude read accesses using page lock */
52
53 /* set_buffer_new(bh); */
54 bh->b_blocknr = 0;
55
56 ret = nilfs_bmap_insert(ii->i_bmap, block, (unsigned long)bh);
57 if (unlikely(ret))
58 return ret;
59
60 set_buffer_mapped(bh);
61
62 kaddr = kmap_atomic(bh->b_page, KM_USER0);
63 memset(kaddr + bh_offset(bh), 0, 1 << inode->i_blkbits);
64 if (init_block)
65 init_block(inode, bh, kaddr);
66 flush_dcache_page(bh->b_page);
67 kunmap_atomic(kaddr, KM_USER0);
68
69 set_buffer_uptodate(bh);
70 nilfs_mark_buffer_dirty(bh);
71 nilfs_mdt_mark_dirty(inode);
72 return 0;
73}
74
75static int nilfs_mdt_create_block(struct inode *inode, unsigned long block,
76 struct buffer_head **out_bh,
77 void (*init_block)(struct inode *,
78 struct buffer_head *,
79 void *))
80{
Ryusuke Konishi5eb563f2009-04-06 19:01:28 -070081 struct super_block *sb = inode->i_sb;
82 struct nilfs_transaction_info ti;
83 struct buffer_head *bh;
84 int err;
85
Ryusuke Konishi5eb563f2009-04-06 19:01:28 -070086 nilfs_transaction_begin(sb, &ti, 0);
87
88 err = -ENOMEM;
89 bh = nilfs_grab_buffer(inode, inode->i_mapping, block, 0);
90 if (unlikely(!bh))
91 goto failed_unlock;
92
93 err = -EEXIST;
Ryusuke Konishi14351102009-09-06 17:49:49 +090094 if (buffer_uptodate(bh))
Ryusuke Konishi5eb563f2009-04-06 19:01:28 -070095 goto failed_bh;
Ryusuke Konishi14351102009-09-06 17:49:49 +090096
Ryusuke Konishi5eb563f2009-04-06 19:01:28 -070097 wait_on_buffer(bh);
98 if (buffer_uptodate(bh))
99 goto failed_bh;
Ryusuke Konishi5eb563f2009-04-06 19:01:28 -0700100
Ryusuke Konishi090fd5b2010-09-05 16:17:35 +0900101 bh->b_bdev = sb->s_bdev;
Ryusuke Konishi5eb563f2009-04-06 19:01:28 -0700102 err = nilfs_mdt_insert_new_block(inode, block, bh, init_block);
103 if (likely(!err)) {
104 get_bh(bh);
105 *out_bh = bh;
106 }
107
108 failed_bh:
109 unlock_page(bh->b_page);
110 page_cache_release(bh->b_page);
111 brelse(bh);
112
113 failed_unlock:
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700114 if (likely(!err))
115 err = nilfs_transaction_commit(sb);
116 else
117 nilfs_transaction_abort(sb);
Ryusuke Konishi090fd5b2010-09-05 16:17:35 +0900118
Ryusuke Konishi5eb563f2009-04-06 19:01:28 -0700119 return err;
120}
121
122static int
123nilfs_mdt_submit_block(struct inode *inode, unsigned long blkoff,
124 int mode, struct buffer_head **out_bh)
125{
126 struct buffer_head *bh;
Ryusuke Konishi0f3fe332009-08-15 02:29:28 +0900127 __u64 blknum = 0;
Ryusuke Konishi5eb563f2009-04-06 19:01:28 -0700128 int ret = -ENOMEM;
129
130 bh = nilfs_grab_buffer(inode, inode->i_mapping, blkoff, 0);
131 if (unlikely(!bh))
132 goto failed;
133
134 ret = -EEXIST; /* internal code */
135 if (buffer_uptodate(bh))
136 goto out;
137
138 if (mode == READA) {
139 if (!trylock_buffer(bh)) {
140 ret = -EBUSY;
141 goto failed_bh;
142 }
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700143 } else /* mode == READ */
Ryusuke Konishi5eb563f2009-04-06 19:01:28 -0700144 lock_buffer(bh);
Ryusuke Konishi5eb563f2009-04-06 19:01:28 -0700145
146 if (buffer_uptodate(bh)) {
147 unlock_buffer(bh);
148 goto out;
149 }
Ryusuke Konishi14351102009-09-06 17:49:49 +0900150
151 ret = nilfs_bmap_lookup(NILFS_I(inode)->i_bmap, blkoff, &blknum);
152 if (unlikely(ret)) {
153 unlock_buffer(bh);
154 goto failed_bh;
Ryusuke Konishi5eb563f2009-04-06 19:01:28 -0700155 }
Ryusuke Konishic6e07182010-09-05 00:23:50 +0900156 map_bh(bh, inode->i_sb, (sector_t)blknum);
Ryusuke Konishi5eb563f2009-04-06 19:01:28 -0700157
158 bh->b_end_io = end_buffer_read_sync;
159 get_bh(bh);
160 submit_bh(mode, bh);
161 ret = 0;
162 out:
163 get_bh(bh);
164 *out_bh = bh;
165
166 failed_bh:
167 unlock_page(bh->b_page);
168 page_cache_release(bh->b_page);
169 brelse(bh);
170 failed:
171 return ret;
172}
173
174static int nilfs_mdt_read_block(struct inode *inode, unsigned long block,
Ryusuke Konishib34a6502009-11-14 00:09:47 +0900175 int readahead, struct buffer_head **out_bh)
Ryusuke Konishi5eb563f2009-04-06 19:01:28 -0700176{
177 struct buffer_head *first_bh, *bh;
178 unsigned long blkoff;
179 int i, nr_ra_blocks = NILFS_MDT_MAX_RA_BLOCKS;
180 int err;
181
182 err = nilfs_mdt_submit_block(inode, block, READ, &first_bh);
183 if (err == -EEXIST) /* internal code */
184 goto out;
185
186 if (unlikely(err))
187 goto failed;
188
Ryusuke Konishib34a6502009-11-14 00:09:47 +0900189 if (readahead) {
190 blkoff = block + 1;
191 for (i = 0; i < nr_ra_blocks; i++, blkoff++) {
192 err = nilfs_mdt_submit_block(inode, blkoff, READA, &bh);
193 if (likely(!err || err == -EEXIST))
194 brelse(bh);
195 else if (err != -EBUSY)
196 break;
197 /* abort readahead if bmap lookup failed */
198 if (!buffer_locked(first_bh))
199 goto out_no_wait;
200 }
Ryusuke Konishi5eb563f2009-04-06 19:01:28 -0700201 }
202
203 wait_on_buffer(first_bh);
204
205 out_no_wait:
206 err = -EIO;
207 if (!buffer_uptodate(first_bh))
208 goto failed_bh;
209 out:
210 *out_bh = first_bh;
211 return 0;
212
213 failed_bh:
214 brelse(first_bh);
215 failed:
216 return err;
217}
218
219/**
220 * nilfs_mdt_get_block - read or create a buffer on meta data file.
221 * @inode: inode of the meta data file
222 * @blkoff: block offset
223 * @create: create flag
224 * @init_block: initializer used for newly allocated block
225 * @out_bh: output of a pointer to the buffer_head
226 *
227 * nilfs_mdt_get_block() looks up the specified buffer and tries to create
228 * a new buffer if @create is not zero. On success, the returned buffer is
229 * assured to be either existing or formatted using a buffer lock on success.
230 * @out_bh is substituted only when zero is returned.
231 *
232 * Return Value: On success, it returns 0. On error, the following negative
233 * error code is returned.
234 *
235 * %-ENOMEM - Insufficient memory available.
236 *
237 * %-EIO - I/O error
238 *
239 * %-ENOENT - the specified block does not exist (hole block)
240 *
241 * %-EINVAL - bmap is broken. (the caller should call nilfs_error())
242 *
243 * %-EROFS - Read only filesystem (for create mode)
244 */
245int nilfs_mdt_get_block(struct inode *inode, unsigned long blkoff, int create,
246 void (*init_block)(struct inode *,
247 struct buffer_head *, void *),
248 struct buffer_head **out_bh)
249{
250 int ret;
251
252 /* Should be rewritten with merging nilfs_mdt_read_block() */
253 retry:
Ryusuke Konishib34a6502009-11-14 00:09:47 +0900254 ret = nilfs_mdt_read_block(inode, blkoff, !create, out_bh);
Ryusuke Konishi5eb563f2009-04-06 19:01:28 -0700255 if (!create || ret != -ENOENT)
256 return ret;
257
258 ret = nilfs_mdt_create_block(inode, blkoff, out_bh, init_block);
259 if (unlikely(ret == -EEXIST)) {
260 /* create = 0; */ /* limit read-create loop retries */
261 goto retry;
262 }
263 return ret;
264}
265
266/**
267 * nilfs_mdt_delete_block - make a hole on the meta data file.
268 * @inode: inode of the meta data file
269 * @block: block offset
270 *
271 * Return Value: On success, zero is returned.
272 * On error, one of the following negative error code is returned.
273 *
274 * %-ENOMEM - Insufficient memory available.
275 *
276 * %-EIO - I/O error
277 *
278 * %-EINVAL - bmap is broken. (the caller should call nilfs_error())
279 */
280int nilfs_mdt_delete_block(struct inode *inode, unsigned long block)
281{
282 struct nilfs_inode_info *ii = NILFS_I(inode);
283 int err;
284
285 err = nilfs_bmap_delete(ii->i_bmap, block);
Ryusuke Konishi84338232009-05-05 21:52:06 +0900286 if (!err || err == -ENOENT) {
Ryusuke Konishi5eb563f2009-04-06 19:01:28 -0700287 nilfs_mdt_mark_dirty(inode);
288 nilfs_mdt_forget_block(inode, block);
289 }
290 return err;
291}
292
293/**
294 * nilfs_mdt_forget_block - discard dirty state and try to remove the page
295 * @inode: inode of the meta data file
296 * @block: block offset
297 *
298 * nilfs_mdt_forget_block() clears a dirty flag of the specified buffer, and
299 * tries to release the page including the buffer from a page cache.
300 *
301 * Return Value: On success, 0 is returned. On error, one of the following
302 * negative error code is returned.
303 *
304 * %-EBUSY - page has an active buffer.
305 *
306 * %-ENOENT - page cache has no page addressed by the offset.
307 */
308int nilfs_mdt_forget_block(struct inode *inode, unsigned long block)
309{
310 pgoff_t index = (pgoff_t)block >>
311 (PAGE_CACHE_SHIFT - inode->i_blkbits);
312 struct page *page;
313 unsigned long first_block;
314 int ret = 0;
315 int still_dirty;
316
317 page = find_lock_page(inode->i_mapping, index);
318 if (!page)
319 return -ENOENT;
320
321 wait_on_page_writeback(page);
322
323 first_block = (unsigned long)index <<
324 (PAGE_CACHE_SHIFT - inode->i_blkbits);
325 if (page_has_buffers(page)) {
326 struct buffer_head *bh;
327
328 bh = nilfs_page_get_nth_block(page, block - first_block);
329 nilfs_forget_buffer(bh);
330 }
331 still_dirty = PageDirty(page);
332 unlock_page(page);
333 page_cache_release(page);
334
335 if (still_dirty ||
336 invalidate_inode_pages2_range(inode->i_mapping, index, index) != 0)
337 ret = -EBUSY;
338 return ret;
339}
340
341/**
342 * nilfs_mdt_mark_block_dirty - mark a block on the meta data file dirty.
343 * @inode: inode of the meta data file
344 * @block: block offset
345 *
346 * Return Value: On success, it returns 0. On error, the following negative
347 * error code is returned.
348 *
349 * %-ENOMEM - Insufficient memory available.
350 *
351 * %-EIO - I/O error
352 *
353 * %-ENOENT - the specified block does not exist (hole block)
354 *
355 * %-EINVAL - bmap is broken. (the caller should call nilfs_error())
356 */
357int nilfs_mdt_mark_block_dirty(struct inode *inode, unsigned long block)
358{
359 struct buffer_head *bh;
360 int err;
361
Ryusuke Konishib34a6502009-11-14 00:09:47 +0900362 err = nilfs_mdt_read_block(inode, block, 0, &bh);
Ryusuke Konishi5eb563f2009-04-06 19:01:28 -0700363 if (unlikely(err))
364 return err;
365 nilfs_mark_buffer_dirty(bh);
366 nilfs_mdt_mark_dirty(inode);
367 brelse(bh);
368 return 0;
369}
370
371int nilfs_mdt_fetch_dirty(struct inode *inode)
372{
373 struct nilfs_inode_info *ii = NILFS_I(inode);
374
375 if (nilfs_bmap_test_and_clear_dirty(ii->i_bmap)) {
376 set_bit(NILFS_I_DIRTY, &ii->i_state);
377 return 1;
378 }
379 return test_bit(NILFS_I_DIRTY, &ii->i_state);
380}
381
382static int
383nilfs_mdt_write_page(struct page *page, struct writeback_control *wbc)
384{
Ryusuke Konishiebdfed42010-09-06 12:05:43 +0900385 struct inode *inode;
386 struct super_block *sb;
Ryusuke Konishi5eb563f2009-04-06 19:01:28 -0700387 int err = 0;
388
389 redirty_page_for_writepage(wbc, page);
390 unlock_page(page);
391
Ryusuke Konishiebdfed42010-09-06 12:05:43 +0900392 inode = page->mapping->host;
393 if (!inode)
394 return 0;
395
396 sb = inode->i_sb;
Ryusuke Konishi5eb563f2009-04-06 19:01:28 -0700397
398 if (wbc->sync_mode == WB_SYNC_ALL)
399 err = nilfs_construct_segment(sb);
400 else if (wbc->for_reclaim)
401 nilfs_flush_segment(sb, inode->i_ino);
402
Ryusuke Konishi5eb563f2009-04-06 19:01:28 -0700403 return err;
404}
405
406
Alexey Dobriyan7f094102009-09-21 17:01:10 -0700407static const struct address_space_operations def_mdt_aops = {
Ryusuke Konishi5eb563f2009-04-06 19:01:28 -0700408 .writepage = nilfs_mdt_write_page,
Ryusuke Konishifa032742009-05-27 22:44:34 +0900409 .sync_page = block_sync_page,
Ryusuke Konishi5eb563f2009-04-06 19:01:28 -0700410};
411
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700412static const struct inode_operations def_mdt_iops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700413static const struct file_operations def_mdt_fops;
Ryusuke Konishi5eb563f2009-04-06 19:01:28 -0700414
Ryusuke Konishi5e19a992010-08-21 22:01:51 +0900415
Ryusuke Konishif1e89c82010-09-05 12:20:59 +0900416int nilfs_mdt_init(struct inode *inode, gfp_t gfp_mask, size_t objsz)
Ryusuke Konishi5e19a992010-08-21 22:01:51 +0900417{
418 struct nilfs_mdt_info *mi;
419
420 mi = kzalloc(max(sizeof(*mi), objsz), GFP_NOFS);
421 if (!mi)
422 return -ENOMEM;
423
Ryusuke Konishi5e19a992010-08-21 22:01:51 +0900424 init_rwsem(&mi->mi_sem);
425 inode->i_private = mi;
426
427 inode->i_mode = S_IFREG;
428 mapping_set_gfp_mask(inode->i_mapping, gfp_mask);
Ryusuke Konishif1e89c82010-09-05 12:20:59 +0900429 inode->i_mapping->backing_dev_info = inode->i_sb->s_bdi;
430
431 inode->i_op = &def_mdt_iops;
432 inode->i_fop = &def_mdt_fops;
433 inode->i_mapping->a_ops = &def_mdt_aops;
Ryusuke Konishi5e19a992010-08-21 22:01:51 +0900434
435 return 0;
436}
437
Ryusuke Konishi5eb563f2009-04-06 19:01:28 -0700438/*
439 * NILFS2 uses pseudo inodes for meta data files such as DAT, cpfile, sufile,
440 * ifile, or gcinodes. This allows the B-tree code and segment constructor
441 * to treat them like regular files, and this helps to simplify the
442 * implementation.
443 * On the other hand, some of the pseudo inodes have an irregular point:
444 * They don't have valid inode->i_sb pointer because their lifetimes are
445 * longer than those of the super block structs; they may continue for
446 * several consecutive mounts/umounts. This would need discussions.
447 */
Ryusuke Konishi5731e192009-11-12 22:42:04 +0900448/**
449 * nilfs_mdt_new_common - allocate a pseudo inode for metadata file
450 * @nilfs: nilfs object
451 * @sb: super block instance the metadata file belongs to
452 * @ino: inode number
Ryusuke Konishi5731e192009-11-12 22:42:04 +0900453 */
Ryusuke Konishi5eb563f2009-04-06 19:01:28 -0700454struct inode *
455nilfs_mdt_new_common(struct the_nilfs *nilfs, struct super_block *sb,
Ryusuke Konishi5e19a992010-08-21 22:01:51 +0900456 ino_t ino)
Ryusuke Konishi5eb563f2009-04-06 19:01:28 -0700457{
Ryusuke Konishia53b4752009-05-27 22:11:46 +0900458 struct inode *inode = nilfs_alloc_inode_common(nilfs);
Ryusuke Konishi5eb563f2009-04-06 19:01:28 -0700459
460 if (!inode)
461 return NULL;
462 else {
463 struct address_space * const mapping = &inode->i_data;
Ryusuke Konishi5eb563f2009-04-06 19:01:28 -0700464
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;
Ryusuke Konishi5eb563f2009-04-06 19:01:28 -0700471
472#ifdef INIT_UNUSED_INODE_FIELDS
473 atomic_set(&inode->i_writecount, 0);
474 inode->i_size = 0;
475 inode->i_blocks = 0;
476 inode->i_bytes = 0;
477 inode->i_generation = 0;
478#ifdef CONFIG_QUOTA
479 memset(&inode->i_dquot, 0, sizeof(inode->i_dquot));
480#endif
481 inode->i_pipe = NULL;
482 inode->i_bdev = NULL;
483 inode->i_cdev = NULL;
484 inode->i_rdev = 0;
485#ifdef CONFIG_SECURITY
486 inode->i_security = NULL;
487#endif
488 inode->dirtied_when = 0;
489
490 INIT_LIST_HEAD(&inode->i_list);
491 INIT_LIST_HEAD(&inode->i_sb_list);
492 inode->i_state = 0;
493#endif
494
495 spin_lock_init(&inode->i_lock);
496 mutex_init(&inode->i_mutex);
497 init_rwsem(&inode->i_alloc_sem);
498
499 mapping->host = NULL; /* instead of inode */
500 mapping->flags = 0;
Ryusuke Konishi5eb563f2009-04-06 19:01:28 -0700501 mapping->assoc_mapping = NULL;
Ryusuke Konishi5eb563f2009-04-06 19:01:28 -0700502
503 inode->i_mapping = mapping;
504 }
505
506 return inode;
507}
508
509struct inode *nilfs_mdt_new(struct the_nilfs *nilfs, struct super_block *sb,
Ryusuke Konishi5731e192009-11-12 22:42:04 +0900510 ino_t ino, size_t objsz)
Ryusuke Konishi5eb563f2009-04-06 19:01:28 -0700511{
Ryusuke Konishi5731e192009-11-12 22:42:04 +0900512 struct inode *inode;
Ryusuke Konishi5eb563f2009-04-06 19:01:28 -0700513
Ryusuke Konishi5e19a992010-08-21 22:01:51 +0900514 inode = nilfs_mdt_new_common(nilfs, sb, ino);
Ryusuke Konishi5eb563f2009-04-06 19:01:28 -0700515 if (!inode)
516 return NULL;
517
Ryusuke Konishif1e89c82010-09-05 12:20:59 +0900518 if (nilfs_mdt_init(inode, NILFS_MDT_GFP, objsz) < 0) {
Ryusuke Konishi5e19a992010-08-21 22:01:51 +0900519 nilfs_destroy_inode(inode);
520 return NULL;
521 }
Ryusuke Konishi5eb563f2009-04-06 19:01:28 -0700522 return inode;
523}
524
525void nilfs_mdt_set_entry_size(struct inode *inode, unsigned entry_size,
526 unsigned header_size)
527{
528 struct nilfs_mdt_info *mi = NILFS_MDT(inode);
529
530 mi->mi_entry_size = entry_size;
531 mi->mi_entries_per_block = (1 << inode->i_blkbits) / entry_size;
532 mi->mi_first_entry_offset = DIV_ROUND_UP(header_size, entry_size);
533}
534
Ryusuke Konishiebdfed42010-09-06 12:05:43 +0900535static const struct address_space_operations shadow_map_aops = {
536 .sync_page = block_sync_page,
537};
538
539/**
540 * nilfs_mdt_setup_shadow_map - setup shadow map and bind it to metadata file
541 * @inode: inode of the metadata file
542 * @shadow: shadow mapping
543 */
544int nilfs_mdt_setup_shadow_map(struct inode *inode,
545 struct nilfs_shadow_map *shadow)
546{
547 struct nilfs_mdt_info *mi = NILFS_MDT(inode);
548 struct backing_dev_info *bdi = NILFS_I_NILFS(inode)->ns_bdi;
549
550 INIT_LIST_HEAD(&shadow->frozen_buffers);
551 nilfs_mapping_init_once(&shadow->frozen_data);
552 nilfs_mapping_init(&shadow->frozen_data, bdi, &shadow_map_aops);
553 nilfs_mapping_init_once(&shadow->frozen_btnodes);
554 nilfs_mapping_init(&shadow->frozen_btnodes, bdi, &shadow_map_aops);
555 mi->mi_shadow = shadow;
556 return 0;
557}
558
559/**
560 * nilfs_mdt_save_to_shadow_map - copy bmap and dirty pages to shadow map
561 * @inode: inode of the metadata file
562 */
563int nilfs_mdt_save_to_shadow_map(struct inode *inode)
564{
565 struct nilfs_mdt_info *mi = NILFS_MDT(inode);
566 struct nilfs_inode_info *ii = NILFS_I(inode);
567 struct nilfs_shadow_map *shadow = mi->mi_shadow;
568 int ret;
569
570 ret = nilfs_copy_dirty_pages(&shadow->frozen_data, inode->i_mapping);
571 if (ret)
572 goto out;
573
574 ret = nilfs_copy_dirty_pages(&shadow->frozen_btnodes,
575 &ii->i_btnode_cache);
576 if (ret)
577 goto out;
578
579 nilfs_bmap_save(ii->i_bmap, &shadow->bmap_store);
580 out:
581 return ret;
582}
583
Ryusuke Konishib1f6a4f2010-08-31 11:40:34 +0900584int nilfs_mdt_freeze_buffer(struct inode *inode, struct buffer_head *bh)
585{
586 struct nilfs_shadow_map *shadow = NILFS_MDT(inode)->mi_shadow;
587 struct buffer_head *bh_frozen;
588 struct page *page;
589 int blkbits = inode->i_blkbits;
590 int ret = -ENOMEM;
591
592 page = grab_cache_page(&shadow->frozen_data, bh->b_page->index);
593 if (!page)
594 return ret;
595
596 if (!page_has_buffers(page))
597 create_empty_buffers(page, 1 << blkbits, 0);
598
599 bh_frozen = nilfs_page_get_nth_block(page, bh_offset(bh) >> blkbits);
600 if (bh_frozen) {
601 if (!buffer_uptodate(bh_frozen))
602 nilfs_copy_buffer(bh_frozen, bh);
603 if (list_empty(&bh_frozen->b_assoc_buffers)) {
604 list_add_tail(&bh_frozen->b_assoc_buffers,
605 &shadow->frozen_buffers);
606 set_buffer_nilfs_redirected(bh);
607 } else {
608 brelse(bh_frozen); /* already frozen */
609 }
610 ret = 0;
611 }
612 unlock_page(page);
613 page_cache_release(page);
614 return ret;
615}
616
617struct buffer_head *
618nilfs_mdt_get_frozen_buffer(struct inode *inode, struct buffer_head *bh)
619{
620 struct nilfs_shadow_map *shadow = NILFS_MDT(inode)->mi_shadow;
621 struct buffer_head *bh_frozen = NULL;
622 struct page *page;
623 int n;
624
625 page = find_lock_page(&shadow->frozen_data, bh->b_page->index);
626 if (page) {
627 if (page_has_buffers(page)) {
628 n = bh_offset(bh) >> inode->i_blkbits;
629 bh_frozen = nilfs_page_get_nth_block(page, n);
630 }
631 unlock_page(page);
632 page_cache_release(page);
633 }
634 return bh_frozen;
635}
636
637static void nilfs_release_frozen_buffers(struct nilfs_shadow_map *shadow)
638{
639 struct list_head *head = &shadow->frozen_buffers;
640 struct buffer_head *bh;
641
642 while (!list_empty(head)) {
643 bh = list_first_entry(head, struct buffer_head,
644 b_assoc_buffers);
645 list_del_init(&bh->b_assoc_buffers);
646 brelse(bh); /* drop ref-count to make it releasable */
647 }
648}
649
Ryusuke Konishiebdfed42010-09-06 12:05:43 +0900650/**
651 * nilfs_mdt_restore_from_shadow_map - restore dirty pages and bmap state
652 * @inode: inode of the metadata file
653 */
654void nilfs_mdt_restore_from_shadow_map(struct inode *inode)
655{
656 struct nilfs_mdt_info *mi = NILFS_MDT(inode);
657 struct nilfs_inode_info *ii = NILFS_I(inode);
658 struct nilfs_shadow_map *shadow = mi->mi_shadow;
659
660 down_write(&mi->mi_sem);
661
662 if (mi->mi_palloc_cache)
663 nilfs_palloc_clear_cache(inode);
664
665 nilfs_clear_dirty_pages(inode->i_mapping);
666 nilfs_copy_back_pages(inode->i_mapping, &shadow->frozen_data);
667
668 nilfs_clear_dirty_pages(&ii->i_btnode_cache);
669 nilfs_copy_back_pages(&ii->i_btnode_cache, &shadow->frozen_btnodes);
670
671 nilfs_bmap_restore(ii->i_bmap, &shadow->bmap_store);
672
673 up_write(&mi->mi_sem);
674}
675
676/**
677 * nilfs_mdt_clear_shadow_map - truncate pages in shadow map caches
678 * @inode: inode of the metadata file
679 */
680void nilfs_mdt_clear_shadow_map(struct inode *inode)
681{
682 struct nilfs_mdt_info *mi = NILFS_MDT(inode);
683 struct nilfs_shadow_map *shadow = mi->mi_shadow;
684
685 down_write(&mi->mi_sem);
Ryusuke Konishib1f6a4f2010-08-31 11:40:34 +0900686 nilfs_release_frozen_buffers(shadow);
Ryusuke Konishiebdfed42010-09-06 12:05:43 +0900687 truncate_inode_pages(&shadow->frozen_data, 0);
688 truncate_inode_pages(&shadow->frozen_btnodes, 0);
689 up_write(&mi->mi_sem);
690}
691
Ryusuke Konishifd66c0d2009-11-13 02:25:41 +0900692static void nilfs_mdt_clear(struct inode *inode)
Ryusuke Konishi5eb563f2009-04-06 19:01:28 -0700693{
694 struct nilfs_inode_info *ii = NILFS_I(inode);
695
696 invalidate_mapping_pages(inode->i_mapping, 0, -1);
697 truncate_inode_pages(inode->i_mapping, 0);
698
Ryusuke Konishifd66c0d2009-11-13 02:25:41 +0900699 if (test_bit(NILFS_I_BMAP, &ii->i_state))
700 nilfs_bmap_clear(ii->i_bmap);
Ryusuke Konishi5eb563f2009-04-06 19:01:28 -0700701 nilfs_btnode_cache_clear(&ii->i_btnode_cache);
702}
703
704void nilfs_mdt_destroy(struct inode *inode)
705{
706 struct nilfs_mdt_info *mdi = NILFS_MDT(inode);
707
Ryusuke Konishidb38d5a2009-11-14 15:54:27 +0900708 if (mdi->mi_palloc_cache)
709 nilfs_palloc_destroy_cache(inode);
Ryusuke Konishifd66c0d2009-11-13 02:25:41 +0900710 nilfs_mdt_clear(inode);
711
Ryusuke Konishi5eb563f2009-04-06 19:01:28 -0700712 nilfs_destroy_inode(inode);
713}