]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/nilfs2/mdt.c
nilfs2: extend nilfs_sustat ioctl struct
[mirror_ubuntu-artful-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 *
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:
47420c79
RK
126 if (likely(!err))
127 err = nilfs_transaction_commit(sb);
128 else
129 nilfs_transaction_abort(sb);
5eb563f5
RK
130 if (writer)
131 nilfs_put_writer(nilfs);
132 out:
133 return err;
134}
135
136static int
137nilfs_mdt_submit_block(struct inode *inode, unsigned long blkoff,
138 int mode, struct buffer_head **out_bh)
139{
140 struct buffer_head *bh;
141 unsigned long blknum = 0;
142 int ret = -ENOMEM;
143
144 bh = nilfs_grab_buffer(inode, inode->i_mapping, blkoff, 0);
145 if (unlikely(!bh))
146 goto failed;
147
148 ret = -EEXIST; /* internal code */
149 if (buffer_uptodate(bh))
150 goto out;
151
152 if (mode == READA) {
153 if (!trylock_buffer(bh)) {
154 ret = -EBUSY;
155 goto failed_bh;
156 }
157 } else {
158 BUG_ON(mode != READ);
159 lock_buffer(bh);
160 }
161
162 if (buffer_uptodate(bh)) {
163 unlock_buffer(bh);
164 goto out;
165 }
166 if (!buffer_mapped(bh)) { /* unused buffer */
167 ret = nilfs_bmap_lookup(NILFS_I(inode)->i_bmap, blkoff,
168 &blknum);
169 if (unlikely(ret)) {
170 unlock_buffer(bh);
171 goto failed_bh;
172 }
173 bh->b_bdev = NILFS_MDT(inode)->mi_nilfs->ns_bdev;
174 bh->b_blocknr = blknum;
175 set_buffer_mapped(bh);
176 }
177
178 bh->b_end_io = end_buffer_read_sync;
179 get_bh(bh);
180 submit_bh(mode, bh);
181 ret = 0;
182 out:
183 get_bh(bh);
184 *out_bh = bh;
185
186 failed_bh:
187 unlock_page(bh->b_page);
188 page_cache_release(bh->b_page);
189 brelse(bh);
190 failed:
191 return ret;
192}
193
194static int nilfs_mdt_read_block(struct inode *inode, unsigned long block,
195 struct buffer_head **out_bh)
196{
197 struct buffer_head *first_bh, *bh;
198 unsigned long blkoff;
199 int i, nr_ra_blocks = NILFS_MDT_MAX_RA_BLOCKS;
200 int err;
201
202 err = nilfs_mdt_submit_block(inode, block, READ, &first_bh);
203 if (err == -EEXIST) /* internal code */
204 goto out;
205
206 if (unlikely(err))
207 goto failed;
208
209 blkoff = block + 1;
210 for (i = 0; i < nr_ra_blocks; i++, blkoff++) {
211 err = nilfs_mdt_submit_block(inode, blkoff, READA, &bh);
212 if (likely(!err || err == -EEXIST))
213 brelse(bh);
214 else if (err != -EBUSY)
215 break; /* abort readahead if bmap lookup failed */
216
217 if (!buffer_locked(first_bh))
218 goto out_no_wait;
219 }
220
221 wait_on_buffer(first_bh);
222
223 out_no_wait:
224 err = -EIO;
225 if (!buffer_uptodate(first_bh))
226 goto failed_bh;
227 out:
228 *out_bh = first_bh;
229 return 0;
230
231 failed_bh:
232 brelse(first_bh);
233 failed:
234 return err;
235}
236
237/**
238 * nilfs_mdt_get_block - read or create a buffer on meta data file.
239 * @inode: inode of the meta data file
240 * @blkoff: block offset
241 * @create: create flag
242 * @init_block: initializer used for newly allocated block
243 * @out_bh: output of a pointer to the buffer_head
244 *
245 * nilfs_mdt_get_block() looks up the specified buffer and tries to create
246 * a new buffer if @create is not zero. On success, the returned buffer is
247 * assured to be either existing or formatted using a buffer lock on success.
248 * @out_bh is substituted only when zero is returned.
249 *
250 * Return Value: On success, it returns 0. On error, the following negative
251 * error code is returned.
252 *
253 * %-ENOMEM - Insufficient memory available.
254 *
255 * %-EIO - I/O error
256 *
257 * %-ENOENT - the specified block does not exist (hole block)
258 *
259 * %-EINVAL - bmap is broken. (the caller should call nilfs_error())
260 *
261 * %-EROFS - Read only filesystem (for create mode)
262 */
263int nilfs_mdt_get_block(struct inode *inode, unsigned long blkoff, int create,
264 void (*init_block)(struct inode *,
265 struct buffer_head *, void *),
266 struct buffer_head **out_bh)
267{
268 int ret;
269
270 /* Should be rewritten with merging nilfs_mdt_read_block() */
271 retry:
272 ret = nilfs_mdt_read_block(inode, blkoff, out_bh);
273 if (!create || ret != -ENOENT)
274 return ret;
275
276 ret = nilfs_mdt_create_block(inode, blkoff, out_bh, init_block);
277 if (unlikely(ret == -EEXIST)) {
278 /* create = 0; */ /* limit read-create loop retries */
279 goto retry;
280 }
281 return ret;
282}
283
284/**
285 * nilfs_mdt_delete_block - make a hole on the meta data file.
286 * @inode: inode of the meta data file
287 * @block: block offset
288 *
289 * Return Value: On success, zero is returned.
290 * On error, one of the following negative error code is returned.
291 *
292 * %-ENOMEM - Insufficient memory available.
293 *
294 * %-EIO - I/O error
295 *
296 * %-EINVAL - bmap is broken. (the caller should call nilfs_error())
297 */
298int nilfs_mdt_delete_block(struct inode *inode, unsigned long block)
299{
300 struct nilfs_inode_info *ii = NILFS_I(inode);
301 int err;
302
303 err = nilfs_bmap_delete(ii->i_bmap, block);
304 if (likely(!err)) {
305 nilfs_mdt_mark_dirty(inode);
306 nilfs_mdt_forget_block(inode, block);
307 }
308 return err;
309}
310
311/**
312 * nilfs_mdt_forget_block - discard dirty state and try to remove the page
313 * @inode: inode of the meta data file
314 * @block: block offset
315 *
316 * nilfs_mdt_forget_block() clears a dirty flag of the specified buffer, and
317 * tries to release the page including the buffer from a page cache.
318 *
319 * Return Value: On success, 0 is returned. On error, one of the following
320 * negative error code is returned.
321 *
322 * %-EBUSY - page has an active buffer.
323 *
324 * %-ENOENT - page cache has no page addressed by the offset.
325 */
326int nilfs_mdt_forget_block(struct inode *inode, unsigned long block)
327{
328 pgoff_t index = (pgoff_t)block >>
329 (PAGE_CACHE_SHIFT - inode->i_blkbits);
330 struct page *page;
331 unsigned long first_block;
332 int ret = 0;
333 int still_dirty;
334
335 page = find_lock_page(inode->i_mapping, index);
336 if (!page)
337 return -ENOENT;
338
339 wait_on_page_writeback(page);
340
341 first_block = (unsigned long)index <<
342 (PAGE_CACHE_SHIFT - inode->i_blkbits);
343 if (page_has_buffers(page)) {
344 struct buffer_head *bh;
345
346 bh = nilfs_page_get_nth_block(page, block - first_block);
347 nilfs_forget_buffer(bh);
348 }
349 still_dirty = PageDirty(page);
350 unlock_page(page);
351 page_cache_release(page);
352
353 if (still_dirty ||
354 invalidate_inode_pages2_range(inode->i_mapping, index, index) != 0)
355 ret = -EBUSY;
356 return ret;
357}
358
359/**
360 * nilfs_mdt_mark_block_dirty - mark a block on the meta data file dirty.
361 * @inode: inode of the meta data file
362 * @block: block offset
363 *
364 * Return Value: On success, it returns 0. On error, the following negative
365 * error code is returned.
366 *
367 * %-ENOMEM - Insufficient memory available.
368 *
369 * %-EIO - I/O error
370 *
371 * %-ENOENT - the specified block does not exist (hole block)
372 *
373 * %-EINVAL - bmap is broken. (the caller should call nilfs_error())
374 */
375int nilfs_mdt_mark_block_dirty(struct inode *inode, unsigned long block)
376{
377 struct buffer_head *bh;
378 int err;
379
380 err = nilfs_mdt_read_block(inode, block, &bh);
381 if (unlikely(err))
382 return err;
383 nilfs_mark_buffer_dirty(bh);
384 nilfs_mdt_mark_dirty(inode);
385 brelse(bh);
386 return 0;
387}
388
389int nilfs_mdt_fetch_dirty(struct inode *inode)
390{
391 struct nilfs_inode_info *ii = NILFS_I(inode);
392
393 if (nilfs_bmap_test_and_clear_dirty(ii->i_bmap)) {
394 set_bit(NILFS_I_DIRTY, &ii->i_state);
395 return 1;
396 }
397 return test_bit(NILFS_I_DIRTY, &ii->i_state);
398}
399
400static int
401nilfs_mdt_write_page(struct page *page, struct writeback_control *wbc)
402{
403 struct inode *inode = container_of(page->mapping,
404 struct inode, i_data);
405 struct super_block *sb = inode->i_sb;
406 struct nilfs_sb_info *writer = NULL;
407 int err = 0;
408
409 redirty_page_for_writepage(wbc, page);
410 unlock_page(page);
411
412 if (page->mapping->assoc_mapping)
413 return 0; /* Do not request flush for shadow page cache */
414 if (!sb) {
415 writer = nilfs_get_writer(NILFS_MDT(inode)->mi_nilfs);
416 if (!writer)
417 return -EROFS;
418 sb = writer->s_super;
419 }
420
421 if (wbc->sync_mode == WB_SYNC_ALL)
422 err = nilfs_construct_segment(sb);
423 else if (wbc->for_reclaim)
424 nilfs_flush_segment(sb, inode->i_ino);
425
426 if (writer)
427 nilfs_put_writer(NILFS_MDT(inode)->mi_nilfs);
428 return err;
429}
430
431
432static struct address_space_operations def_mdt_aops = {
433 .writepage = nilfs_mdt_write_page,
434};
435
436static struct inode_operations def_mdt_iops;
437static struct file_operations def_mdt_fops;
438
439/*
440 * NILFS2 uses pseudo inodes for meta data files such as DAT, cpfile, sufile,
441 * ifile, or gcinodes. This allows the B-tree code and segment constructor
442 * to treat them like regular files, and this helps to simplify the
443 * implementation.
444 * On the other hand, some of the pseudo inodes have an irregular point:
445 * They don't have valid inode->i_sb pointer because their lifetimes are
446 * longer than those of the super block structs; they may continue for
447 * several consecutive mounts/umounts. This would need discussions.
448 */
449struct inode *
450nilfs_mdt_new_common(struct the_nilfs *nilfs, struct super_block *sb,
451 ino_t ino, gfp_t gfp_mask)
452{
453 struct inode *inode = nilfs_alloc_inode(sb);
454
455 if (!inode)
456 return NULL;
457 else {
458 struct address_space * const mapping = &inode->i_data;
459 struct nilfs_mdt_info *mi = kzalloc(sizeof(*mi), GFP_NOFS);
460
461 if (!mi) {
462 nilfs_destroy_inode(inode);
463 return NULL;
464 }
465 mi->mi_nilfs = nilfs;
466 init_rwsem(&mi->mi_sem);
467
468 inode->i_sb = sb; /* sb may be NULL for some meta data files */
469 inode->i_blkbits = nilfs->ns_blocksize_bits;
470 inode->i_flags = 0;
471 atomic_set(&inode->i_count, 1);
472 inode->i_nlink = 1;
473 inode->i_ino = ino;
474 inode->i_mode = S_IFREG;
475 inode->i_private = mi;
476
477#ifdef INIT_UNUSED_INODE_FIELDS
478 atomic_set(&inode->i_writecount, 0);
479 inode->i_size = 0;
480 inode->i_blocks = 0;
481 inode->i_bytes = 0;
482 inode->i_generation = 0;
483#ifdef CONFIG_QUOTA
484 memset(&inode->i_dquot, 0, sizeof(inode->i_dquot));
485#endif
486 inode->i_pipe = NULL;
487 inode->i_bdev = NULL;
488 inode->i_cdev = NULL;
489 inode->i_rdev = 0;
490#ifdef CONFIG_SECURITY
491 inode->i_security = NULL;
492#endif
493 inode->dirtied_when = 0;
494
495 INIT_LIST_HEAD(&inode->i_list);
496 INIT_LIST_HEAD(&inode->i_sb_list);
497 inode->i_state = 0;
498#endif
499
500 spin_lock_init(&inode->i_lock);
501 mutex_init(&inode->i_mutex);
502 init_rwsem(&inode->i_alloc_sem);
503
504 mapping->host = NULL; /* instead of inode */
505 mapping->flags = 0;
506 mapping_set_gfp_mask(mapping, gfp_mask);
507 mapping->assoc_mapping = NULL;
508 mapping->backing_dev_info = nilfs->ns_bdi;
509
510 inode->i_mapping = mapping;
511 }
512
513 return inode;
514}
515
516struct inode *nilfs_mdt_new(struct the_nilfs *nilfs, struct super_block *sb,
517 ino_t ino, gfp_t gfp_mask)
518{
519 struct inode *inode = nilfs_mdt_new_common(nilfs, sb, ino, gfp_mask);
520
521 if (!inode)
522 return NULL;
523
524 inode->i_op = &def_mdt_iops;
525 inode->i_fop = &def_mdt_fops;
526 inode->i_mapping->a_ops = &def_mdt_aops;
527 return inode;
528}
529
530void nilfs_mdt_set_entry_size(struct inode *inode, unsigned entry_size,
531 unsigned header_size)
532{
533 struct nilfs_mdt_info *mi = NILFS_MDT(inode);
534
535 mi->mi_entry_size = entry_size;
536 mi->mi_entries_per_block = (1 << inode->i_blkbits) / entry_size;
537 mi->mi_first_entry_offset = DIV_ROUND_UP(header_size, entry_size);
538}
539
540void nilfs_mdt_set_shadow(struct inode *orig, struct inode *shadow)
541{
542 shadow->i_mapping->assoc_mapping = orig->i_mapping;
543 NILFS_I(shadow)->i_btnode_cache.assoc_mapping =
544 &NILFS_I(orig)->i_btnode_cache;
545}
546
547void nilfs_mdt_clear(struct inode *inode)
548{
549 struct nilfs_inode_info *ii = NILFS_I(inode);
550
551 invalidate_mapping_pages(inode->i_mapping, 0, -1);
552 truncate_inode_pages(inode->i_mapping, 0);
553
554 nilfs_bmap_clear(ii->i_bmap);
555 nilfs_btnode_cache_clear(&ii->i_btnode_cache);
556}
557
558void nilfs_mdt_destroy(struct inode *inode)
559{
560 struct nilfs_mdt_info *mdi = NILFS_MDT(inode);
561
562 kfree(mdi->mi_bgl); /* kfree(NULL) is safe */
563 kfree(mdi);
564 nilfs_destroy_inode(inode);
565}