]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - fs/nilfs2/mdt.c
nilfs2: remove FSF mailing address from GPL notices
[mirror_ubuntu-zesty-kernel.git] / fs / nilfs2 / mdt.c
CommitLineData
5eb563f5
RK
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 *
5eb563f5
RK
16 * Written by Ryusuke Konishi <ryusuke@osrg.net>
17 */
18
19#include <linux/buffer_head.h>
20#include <linux/mpage.h>
21#include <linux/mm.h>
22#include <linux/writeback.h>
23#include <linux/backing-dev.h>
24#include <linux/swap.h>
5a0e3ad6 25#include <linux/slab.h>
5eb563f5 26#include "nilfs.h"
05d0e94b 27#include "btnode.h"
5eb563f5
RK
28#include "segment.h"
29#include "page.h"
30#include "mdt.h"
31
a9cd207c 32#include <trace/events/nilfs2.h>
5eb563f5
RK
33
34#define NILFS_MDT_MAX_RA_BLOCKS (16 - 1)
35
5eb563f5
RK
36
37static int
38nilfs_mdt_insert_new_block(struct inode *inode, unsigned long block,
39 struct buffer_head *bh,
40 void (*init_block)(struct inode *,
41 struct buffer_head *, void *))
42{
43 struct nilfs_inode_info *ii = NILFS_I(inode);
44 void *kaddr;
45 int ret;
46
47 /* Caller exclude read accesses using page lock */
48
49 /* set_buffer_new(bh); */
50 bh->b_blocknr = 0;
51
52 ret = nilfs_bmap_insert(ii->i_bmap, block, (unsigned long)bh);
53 if (unlikely(ret))
54 return ret;
55
56 set_buffer_mapped(bh);
57
7b9c0976 58 kaddr = kmap_atomic(bh->b_page);
5eb563f5
RK
59 memset(kaddr + bh_offset(bh), 0, 1 << inode->i_blkbits);
60 if (init_block)
61 init_block(inode, bh, kaddr);
62 flush_dcache_page(bh->b_page);
7b9c0976 63 kunmap_atomic(kaddr);
5eb563f5
RK
64
65 set_buffer_uptodate(bh);
5fc7b141 66 mark_buffer_dirty(bh);
5eb563f5 67 nilfs_mdt_mark_dirty(inode);
a9cd207c
HM
68
69 trace_nilfs2_mdt_insert_new_block(inode, inode->i_ino, block);
70
5eb563f5
RK
71 return 0;
72}
73
74static int nilfs_mdt_create_block(struct inode *inode, unsigned long block,
75 struct buffer_head **out_bh,
76 void (*init_block)(struct inode *,
77 struct buffer_head *,
78 void *))
79{
5eb563f5
RK
80 struct super_block *sb = inode->i_sb;
81 struct nilfs_transaction_info ti;
82 struct buffer_head *bh;
83 int err;
84
5eb563f5
RK
85 nilfs_transaction_begin(sb, &ti, 0);
86
87 err = -ENOMEM;
88 bh = nilfs_grab_buffer(inode, inode->i_mapping, block, 0);
89 if (unlikely(!bh))
90 goto failed_unlock;
91
92 err = -EEXIST;
14351104 93 if (buffer_uptodate(bh))
5eb563f5 94 goto failed_bh;
14351104 95
5eb563f5
RK
96 wait_on_buffer(bh);
97 if (buffer_uptodate(bh))
98 goto failed_bh;
5eb563f5 99
090fd5b1 100 bh->b_bdev = sb->s_bdev;
5eb563f5
RK
101 err = nilfs_mdt_insert_new_block(inode, block, bh, init_block);
102 if (likely(!err)) {
103 get_bh(bh);
104 *out_bh = bh;
105 }
106
107 failed_bh:
108 unlock_page(bh->b_page);
09cbfeaf 109 put_page(bh->b_page);
5eb563f5
RK
110 brelse(bh);
111
112 failed_unlock:
47420c79
RK
113 if (likely(!err))
114 err = nilfs_transaction_commit(sb);
115 else
116 nilfs_transaction_abort(sb);
090fd5b1 117
5eb563f5
RK
118 return err;
119}
120
121static int
122nilfs_mdt_submit_block(struct inode *inode, unsigned long blkoff,
123 int mode, struct buffer_head **out_bh)
124{
125 struct buffer_head *bh;
0f3fe33b 126 __u64 blknum = 0;
5eb563f5
RK
127 int ret = -ENOMEM;
128
129 bh = nilfs_grab_buffer(inode, inode->i_mapping, blkoff, 0);
130 if (unlikely(!bh))
131 goto failed;
132
133 ret = -EEXIST; /* internal code */
134 if (buffer_uptodate(bh))
135 goto out;
136
137 if (mode == READA) {
138 if (!trylock_buffer(bh)) {
139 ret = -EBUSY;
140 goto failed_bh;
141 }
1f5abe7e 142 } else /* mode == READ */
5eb563f5 143 lock_buffer(bh);
5eb563f5
RK
144
145 if (buffer_uptodate(bh)) {
146 unlock_buffer(bh);
147 goto out;
148 }
14351104
RK
149
150 ret = nilfs_bmap_lookup(NILFS_I(inode)->i_bmap, blkoff, &blknum);
151 if (unlikely(ret)) {
152 unlock_buffer(bh);
153 goto failed_bh;
5eb563f5 154 }
c6e07188 155 map_bh(bh, inode->i_sb, (sector_t)blknum);
5eb563f5
RK
156
157 bh->b_end_io = end_buffer_read_sync;
158 get_bh(bh);
159 submit_bh(mode, bh);
160 ret = 0;
a9cd207c
HM
161
162 trace_nilfs2_mdt_submit_block(inode, inode->i_ino, blkoff, mode);
5eb563f5
RK
163 out:
164 get_bh(bh);
165 *out_bh = bh;
166
167 failed_bh:
168 unlock_page(bh->b_page);
09cbfeaf 169 put_page(bh->b_page);
5eb563f5
RK
170 brelse(bh);
171 failed:
172 return ret;
173}
174
175static int nilfs_mdt_read_block(struct inode *inode, unsigned long block,
b34a6506 176 int readahead, struct buffer_head **out_bh)
5eb563f5
RK
177{
178 struct buffer_head *first_bh, *bh;
179 unsigned long blkoff;
180 int i, nr_ra_blocks = NILFS_MDT_MAX_RA_BLOCKS;
181 int err;
182
183 err = nilfs_mdt_submit_block(inode, block, READ, &first_bh);
184 if (err == -EEXIST) /* internal code */
185 goto out;
186
187 if (unlikely(err))
188 goto failed;
189
b34a6506
RK
190 if (readahead) {
191 blkoff = block + 1;
192 for (i = 0; i < nr_ra_blocks; i++, blkoff++) {
193 err = nilfs_mdt_submit_block(inode, blkoff, READA, &bh);
194 if (likely(!err || err == -EEXIST))
195 brelse(bh);
196 else if (err != -EBUSY)
197 break;
198 /* abort readahead if bmap lookup failed */
199 if (!buffer_locked(first_bh))
200 goto out_no_wait;
201 }
5eb563f5
RK
202 }
203
204 wait_on_buffer(first_bh);
205
206 out_no_wait:
207 err = -EIO;
208 if (!buffer_uptodate(first_bh))
209 goto failed_bh;
210 out:
211 *out_bh = first_bh;
212 return 0;
213
214 failed_bh:
215 brelse(first_bh);
216 failed:
217 return err;
218}
219
220/**
221 * nilfs_mdt_get_block - read or create a buffer on meta data file.
222 * @inode: inode of the meta data file
223 * @blkoff: block offset
224 * @create: create flag
225 * @init_block: initializer used for newly allocated block
226 * @out_bh: output of a pointer to the buffer_head
227 *
228 * nilfs_mdt_get_block() looks up the specified buffer and tries to create
229 * a new buffer if @create is not zero. On success, the returned buffer is
230 * assured to be either existing or formatted using a buffer lock on success.
231 * @out_bh is substituted only when zero is returned.
232 *
233 * Return Value: On success, it returns 0. On error, the following negative
234 * error code is returned.
235 *
236 * %-ENOMEM - Insufficient memory available.
237 *
238 * %-EIO - I/O error
239 *
240 * %-ENOENT - the specified block does not exist (hole block)
241 *
5eb563f5
RK
242 * %-EROFS - Read only filesystem (for create mode)
243 */
244int nilfs_mdt_get_block(struct inode *inode, unsigned long blkoff, int create,
245 void (*init_block)(struct inode *,
246 struct buffer_head *, void *),
247 struct buffer_head **out_bh)
248{
249 int ret;
250
251 /* Should be rewritten with merging nilfs_mdt_read_block() */
252 retry:
b34a6506 253 ret = nilfs_mdt_read_block(inode, blkoff, !create, out_bh);
5eb563f5
RK
254 if (!create || ret != -ENOENT)
255 return ret;
256
257 ret = nilfs_mdt_create_block(inode, blkoff, out_bh, init_block);
258 if (unlikely(ret == -EEXIST)) {
259 /* create = 0; */ /* limit read-create loop retries */
260 goto retry;
261 }
262 return ret;
263}
264
fa33915c
RK
265/**
266 * nilfs_mdt_find_block - find and get a buffer on meta data file.
267 * @inode: inode of the meta data file
268 * @start: start block offset (inclusive)
269 * @end: end block offset (inclusive)
270 * @blkoff: block offset
271 * @out_bh: place to store a pointer to buffer_head struct
272 *
273 * nilfs_mdt_find_block() looks up an existing block in range of
274 * [@start, @end] and stores pointer to a buffer head of the block to
275 * @out_bh, and block offset to @blkoff, respectively. @out_bh and
276 * @blkoff are substituted only when zero is returned.
277 *
278 * Return Value: On success, it returns 0. On error, the following negative
279 * error code is returned.
280 *
281 * %-ENOMEM - Insufficient memory available.
282 *
283 * %-EIO - I/O error
284 *
285 * %-ENOENT - no block was found in the range
286 */
287int nilfs_mdt_find_block(struct inode *inode, unsigned long start,
288 unsigned long end, unsigned long *blkoff,
289 struct buffer_head **out_bh)
290{
291 __u64 next;
292 int ret;
293
294 if (unlikely(start > end))
295 return -ENOENT;
296
297 ret = nilfs_mdt_read_block(inode, start, true, out_bh);
298 if (!ret) {
299 *blkoff = start;
300 goto out;
301 }
302 if (unlikely(ret != -ENOENT || start == ULONG_MAX))
303 goto out;
304
305 ret = nilfs_bmap_seek_key(NILFS_I(inode)->i_bmap, start + 1, &next);
306 if (!ret) {
307 if (next <= end) {
308 ret = nilfs_mdt_read_block(inode, next, true, out_bh);
309 if (!ret)
310 *blkoff = next;
311 } else {
312 ret = -ENOENT;
313 }
314 }
315out:
316 return ret;
317}
318
5eb563f5
RK
319/**
320 * nilfs_mdt_delete_block - make a hole on the meta data file.
321 * @inode: inode of the meta data file
322 * @block: block offset
323 *
324 * Return Value: On success, zero is returned.
325 * On error, one of the following negative error code is returned.
326 *
327 * %-ENOMEM - Insufficient memory available.
328 *
329 * %-EIO - I/O error
5eb563f5
RK
330 */
331int nilfs_mdt_delete_block(struct inode *inode, unsigned long block)
332{
333 struct nilfs_inode_info *ii = NILFS_I(inode);
334 int err;
335
336 err = nilfs_bmap_delete(ii->i_bmap, block);
84338237 337 if (!err || err == -ENOENT) {
5eb563f5
RK
338 nilfs_mdt_mark_dirty(inode);
339 nilfs_mdt_forget_block(inode, block);
340 }
341 return err;
342}
343
344/**
345 * nilfs_mdt_forget_block - discard dirty state and try to remove the page
346 * @inode: inode of the meta data file
347 * @block: block offset
348 *
349 * nilfs_mdt_forget_block() clears a dirty flag of the specified buffer, and
350 * tries to release the page including the buffer from a page cache.
351 *
352 * Return Value: On success, 0 is returned. On error, one of the following
353 * negative error code is returned.
354 *
355 * %-EBUSY - page has an active buffer.
356 *
357 * %-ENOENT - page cache has no page addressed by the offset.
358 */
359int nilfs_mdt_forget_block(struct inode *inode, unsigned long block)
360{
361 pgoff_t index = (pgoff_t)block >>
09cbfeaf 362 (PAGE_SHIFT - inode->i_blkbits);
5eb563f5
RK
363 struct page *page;
364 unsigned long first_block;
365 int ret = 0;
366 int still_dirty;
367
368 page = find_lock_page(inode->i_mapping, index);
369 if (!page)
370 return -ENOENT;
371
372 wait_on_page_writeback(page);
373
374 first_block = (unsigned long)index <<
09cbfeaf 375 (PAGE_SHIFT - inode->i_blkbits);
5eb563f5
RK
376 if (page_has_buffers(page)) {
377 struct buffer_head *bh;
378
379 bh = nilfs_page_get_nth_block(page, block - first_block);
380 nilfs_forget_buffer(bh);
381 }
382 still_dirty = PageDirty(page);
383 unlock_page(page);
09cbfeaf 384 put_page(page);
5eb563f5
RK
385
386 if (still_dirty ||
387 invalidate_inode_pages2_range(inode->i_mapping, index, index) != 0)
388 ret = -EBUSY;
389 return ret;
390}
391
392/**
393 * nilfs_mdt_mark_block_dirty - mark a block on the meta data file dirty.
394 * @inode: inode of the meta data file
395 * @block: block offset
396 *
397 * Return Value: On success, it returns 0. On error, the following negative
398 * error code is returned.
399 *
400 * %-ENOMEM - Insufficient memory available.
401 *
402 * %-EIO - I/O error
403 *
404 * %-ENOENT - the specified block does not exist (hole block)
5eb563f5
RK
405 */
406int nilfs_mdt_mark_block_dirty(struct inode *inode, unsigned long block)
407{
408 struct buffer_head *bh;
409 int err;
410
b34a6506 411 err = nilfs_mdt_read_block(inode, block, 0, &bh);
5eb563f5
RK
412 if (unlikely(err))
413 return err;
5fc7b141 414 mark_buffer_dirty(bh);
5eb563f5
RK
415 nilfs_mdt_mark_dirty(inode);
416 brelse(bh);
417 return 0;
418}
419
420int nilfs_mdt_fetch_dirty(struct inode *inode)
421{
422 struct nilfs_inode_info *ii = NILFS_I(inode);
423
424 if (nilfs_bmap_test_and_clear_dirty(ii->i_bmap)) {
425 set_bit(NILFS_I_DIRTY, &ii->i_state);
426 return 1;
427 }
428 return test_bit(NILFS_I_DIRTY, &ii->i_state);
429}
430
431static int
432nilfs_mdt_write_page(struct page *page, struct writeback_control *wbc)
433{
8c26c4e2 434 struct inode *inode = page->mapping->host;
ebdfed4d 435 struct super_block *sb;
5eb563f5
RK
436 int err = 0;
437
8c26c4e2
VD
438 if (inode && (inode->i_sb->s_flags & MS_RDONLY)) {
439 /*
440 * It means that filesystem was remounted in read-only
441 * mode because of error or metadata corruption. But we
442 * have dirty pages that try to be flushed in background.
443 * So, here we simply discard this dirty page.
444 */
445 nilfs_clear_dirty_page(page, false);
446 unlock_page(page);
447 return -EROFS;
448 }
449
5eb563f5
RK
450 redirty_page_for_writepage(wbc, page);
451 unlock_page(page);
452
ebdfed4d
RK
453 if (!inode)
454 return 0;
455
456 sb = inode->i_sb;
5eb563f5
RK
457
458 if (wbc->sync_mode == WB_SYNC_ALL)
459 err = nilfs_construct_segment(sb);
460 else if (wbc->for_reclaim)
461 nilfs_flush_segment(sb, inode->i_ino);
462
5eb563f5
RK
463 return err;
464}
465
466
7f09410b 467static const struct address_space_operations def_mdt_aops = {
5eb563f5
RK
468 .writepage = nilfs_mdt_write_page,
469};
470
6e1d5dcc 471static const struct inode_operations def_mdt_iops;
828c0950 472static const struct file_operations def_mdt_fops;
5eb563f5 473
5e19a995 474
f1e89c86 475int nilfs_mdt_init(struct inode *inode, gfp_t gfp_mask, size_t objsz)
5e19a995
RK
476{
477 struct nilfs_mdt_info *mi;
478
479 mi = kzalloc(max(sizeof(*mi), objsz), GFP_NOFS);
480 if (!mi)
481 return -ENOMEM;
482
5e19a995
RK
483 init_rwsem(&mi->mi_sem);
484 inode->i_private = mi;
485
486 inode->i_mode = S_IFREG;
487 mapping_set_gfp_mask(inode->i_mapping, gfp_mask);
f1e89c86
RK
488
489 inode->i_op = &def_mdt_iops;
490 inode->i_fop = &def_mdt_fops;
491 inode->i_mapping->a_ops = &def_mdt_aops;
5e19a995
RK
492
493 return 0;
494}
495
5eb563f5
RK
496void nilfs_mdt_set_entry_size(struct inode *inode, unsigned entry_size,
497 unsigned header_size)
498{
499 struct nilfs_mdt_info *mi = NILFS_MDT(inode);
500
501 mi->mi_entry_size = entry_size;
502 mi->mi_entries_per_block = (1 << inode->i_blkbits) / entry_size;
503 mi->mi_first_entry_offset = DIV_ROUND_UP(header_size, entry_size);
504}
505
ebdfed4d
RK
506/**
507 * nilfs_mdt_setup_shadow_map - setup shadow map and bind it to metadata file
508 * @inode: inode of the metadata file
509 * @shadow: shadow mapping
510 */
511int nilfs_mdt_setup_shadow_map(struct inode *inode,
512 struct nilfs_shadow_map *shadow)
513{
514 struct nilfs_mdt_info *mi = NILFS_MDT(inode);
ebdfed4d
RK
515
516 INIT_LIST_HEAD(&shadow->frozen_buffers);
2aa15890 517 address_space_init_once(&shadow->frozen_data);
b83ae6d4 518 nilfs_mapping_init(&shadow->frozen_data, inode);
2aa15890 519 address_space_init_once(&shadow->frozen_btnodes);
b83ae6d4 520 nilfs_mapping_init(&shadow->frozen_btnodes, inode);
ebdfed4d
RK
521 mi->mi_shadow = shadow;
522 return 0;
523}
524
525/**
526 * nilfs_mdt_save_to_shadow_map - copy bmap and dirty pages to shadow map
527 * @inode: inode of the metadata file
528 */
529int nilfs_mdt_save_to_shadow_map(struct inode *inode)
530{
531 struct nilfs_mdt_info *mi = NILFS_MDT(inode);
532 struct nilfs_inode_info *ii = NILFS_I(inode);
533 struct nilfs_shadow_map *shadow = mi->mi_shadow;
534 int ret;
535
536 ret = nilfs_copy_dirty_pages(&shadow->frozen_data, inode->i_mapping);
537 if (ret)
538 goto out;
539
540 ret = nilfs_copy_dirty_pages(&shadow->frozen_btnodes,
541 &ii->i_btnode_cache);
542 if (ret)
543 goto out;
544
545 nilfs_bmap_save(ii->i_bmap, &shadow->bmap_store);
546 out:
547 return ret;
548}
549
b1f6a4f2
RK
550int nilfs_mdt_freeze_buffer(struct inode *inode, struct buffer_head *bh)
551{
552 struct nilfs_shadow_map *shadow = NILFS_MDT(inode)->mi_shadow;
553 struct buffer_head *bh_frozen;
554 struct page *page;
555 int blkbits = inode->i_blkbits;
b1f6a4f2
RK
556
557 page = grab_cache_page(&shadow->frozen_data, bh->b_page->index);
558 if (!page)
a7a8447e 559 return -ENOMEM;
b1f6a4f2
RK
560
561 if (!page_has_buffers(page))
562 create_empty_buffers(page, 1 << blkbits, 0);
563
564 bh_frozen = nilfs_page_get_nth_block(page, bh_offset(bh) >> blkbits);
a7a8447e
RK
565
566 if (!buffer_uptodate(bh_frozen))
567 nilfs_copy_buffer(bh_frozen, bh);
568 if (list_empty(&bh_frozen->b_assoc_buffers)) {
569 list_add_tail(&bh_frozen->b_assoc_buffers,
570 &shadow->frozen_buffers);
571 set_buffer_nilfs_redirected(bh);
572 } else {
573 brelse(bh_frozen); /* already frozen */
b1f6a4f2 574 }
a7a8447e 575
b1f6a4f2 576 unlock_page(page);
09cbfeaf 577 put_page(page);
a7a8447e 578 return 0;
b1f6a4f2
RK
579}
580
581struct buffer_head *
582nilfs_mdt_get_frozen_buffer(struct inode *inode, struct buffer_head *bh)
583{
584 struct nilfs_shadow_map *shadow = NILFS_MDT(inode)->mi_shadow;
585 struct buffer_head *bh_frozen = NULL;
586 struct page *page;
587 int n;
588
589 page = find_lock_page(&shadow->frozen_data, bh->b_page->index);
590 if (page) {
591 if (page_has_buffers(page)) {
592 n = bh_offset(bh) >> inode->i_blkbits;
593 bh_frozen = nilfs_page_get_nth_block(page, n);
594 }
595 unlock_page(page);
09cbfeaf 596 put_page(page);
b1f6a4f2
RK
597 }
598 return bh_frozen;
599}
600
601static void nilfs_release_frozen_buffers(struct nilfs_shadow_map *shadow)
602{
603 struct list_head *head = &shadow->frozen_buffers;
604 struct buffer_head *bh;
605
606 while (!list_empty(head)) {
607 bh = list_first_entry(head, struct buffer_head,
608 b_assoc_buffers);
609 list_del_init(&bh->b_assoc_buffers);
610 brelse(bh); /* drop ref-count to make it releasable */
611 }
612}
613
ebdfed4d
RK
614/**
615 * nilfs_mdt_restore_from_shadow_map - restore dirty pages and bmap state
616 * @inode: inode of the metadata file
617 */
618void nilfs_mdt_restore_from_shadow_map(struct inode *inode)
619{
620 struct nilfs_mdt_info *mi = NILFS_MDT(inode);
621 struct nilfs_inode_info *ii = NILFS_I(inode);
622 struct nilfs_shadow_map *shadow = mi->mi_shadow;
623
624 down_write(&mi->mi_sem);
625
626 if (mi->mi_palloc_cache)
627 nilfs_palloc_clear_cache(inode);
628
8c26c4e2 629 nilfs_clear_dirty_pages(inode->i_mapping, true);
ebdfed4d
RK
630 nilfs_copy_back_pages(inode->i_mapping, &shadow->frozen_data);
631
8c26c4e2 632 nilfs_clear_dirty_pages(&ii->i_btnode_cache, true);
ebdfed4d
RK
633 nilfs_copy_back_pages(&ii->i_btnode_cache, &shadow->frozen_btnodes);
634
635 nilfs_bmap_restore(ii->i_bmap, &shadow->bmap_store);
636
637 up_write(&mi->mi_sem);
638}
639
640/**
641 * nilfs_mdt_clear_shadow_map - truncate pages in shadow map caches
642 * @inode: inode of the metadata file
643 */
644void nilfs_mdt_clear_shadow_map(struct inode *inode)
645{
646 struct nilfs_mdt_info *mi = NILFS_MDT(inode);
647 struct nilfs_shadow_map *shadow = mi->mi_shadow;
648
649 down_write(&mi->mi_sem);
b1f6a4f2 650 nilfs_release_frozen_buffers(shadow);
ebdfed4d
RK
651 truncate_inode_pages(&shadow->frozen_data, 0);
652 truncate_inode_pages(&shadow->frozen_btnodes, 0);
653 up_write(&mi->mi_sem);
654}