nilfs2: clean up sketch file
[deliverable/linux.git] / fs / nilfs2 / inode.c
CommitLineData
05fe58fd
RK
1/*
2 * inode.c - NILFS inode operations.
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
24#include <linux/buffer_head.h>
25#include <linux/mpage.h>
26#include <linux/writeback.h>
f30bf3e4 27#include <linux/uio.h>
05fe58fd
RK
28#include "nilfs.h"
29#include "segment.h"
30#include "page.h"
31#include "mdt.h"
32#include "cpfile.h"
33#include "ifile.h"
34
35
36/**
37 * nilfs_get_block() - get a file block on the filesystem (callback function)
38 * @inode - inode struct of the target file
39 * @blkoff - file block number
40 * @bh_result - buffer head to be mapped on
41 * @create - indicate whether allocating the block or not when it has not
42 * been allocated yet.
43 *
44 * This function does not issue actual read request of the specified data
45 * block. It is done by VFS.
46 * Bulk read for direct-io is not supported yet. (should be supported)
47 */
48int nilfs_get_block(struct inode *inode, sector_t blkoff,
49 struct buffer_head *bh_result, int create)
50{
51 struct nilfs_inode_info *ii = NILFS_I(inode);
52 unsigned long blknum = 0;
53 int err = 0, ret;
54 struct inode *dat = nilfs_dat_inode(NILFS_I_NILFS(inode));
55
56 /* This exclusion control is a workaround; should be revised */
57 down_read(&NILFS_MDT(dat)->mi_sem); /* XXX */
58 ret = nilfs_bmap_lookup(ii->i_bmap, (unsigned long)blkoff, &blknum);
59 up_read(&NILFS_MDT(dat)->mi_sem); /* XXX */
60 if (ret == 0) { /* found */
61 map_bh(bh_result, inode->i_sb, blknum);
62 goto out;
63 }
05fe58fd
RK
64 /* data block was not found */
65 if (ret == -ENOENT && create) {
66 struct nilfs_transaction_info ti;
67
68 bh_result->b_blocknr = 0;
69 err = nilfs_transaction_begin(inode->i_sb, &ti, 1);
70 if (unlikely(err))
71 goto out;
72 err = nilfs_bmap_insert(ii->i_bmap, (unsigned long)blkoff,
73 (unsigned long)bh_result);
05fe58fd
RK
74 if (unlikely(err != 0)) {
75 if (err == -EEXIST) {
76 /*
77 * The get_block() function could be called
78 * from multiple callers for an inode.
79 * However, the page having this block must
80 * be locked in this case.
81 */
1f5abe7e 82 printk(KERN_WARNING
05fe58fd
RK
83 "nilfs_get_block: a race condition "
84 "while inserting a data block. "
85 "(inode number=%lu, file block "
86 "offset=%llu)\n",
87 inode->i_ino,
88 (unsigned long long)blkoff);
1f5abe7e 89 err = 0;
05fe58fd
RK
90 } else if (err == -EINVAL) {
91 nilfs_error(inode->i_sb, __func__,
92 "broken bmap (inode=%lu)\n",
93 inode->i_ino);
94 err = -EIO;
95 }
47420c79 96 nilfs_transaction_abort(inode->i_sb);
05fe58fd
RK
97 goto out;
98 }
47420c79 99 nilfs_transaction_commit(inode->i_sb); /* never fails */
05fe58fd
RK
100 /* Error handling should be detailed */
101 set_buffer_new(bh_result);
102 map_bh(bh_result, inode->i_sb, 0); /* dbn must be changed
103 to proper value */
104 } else if (ret == -ENOENT) {
105 /* not found is not error (e.g. hole); must return without
106 the mapped state flag. */
107 ;
108 } else {
109 err = ret;
110 }
111
112 out:
113 return err;
114}
115
116/**
117 * nilfs_readpage() - implement readpage() method of nilfs_aops {}
118 * address_space_operations.
119 * @file - file struct of the file to be read
120 * @page - the page to be read
121 */
122static int nilfs_readpage(struct file *file, struct page *page)
123{
124 return mpage_readpage(page, nilfs_get_block);
125}
126
127/**
128 * nilfs_readpages() - implement readpages() method of nilfs_aops {}
129 * address_space_operations.
130 * @file - file struct of the file to be read
131 * @mapping - address_space struct used for reading multiple pages
132 * @pages - the pages to be read
133 * @nr_pages - number of pages to be read
134 */
135static int nilfs_readpages(struct file *file, struct address_space *mapping,
136 struct list_head *pages, unsigned nr_pages)
137{
138 return mpage_readpages(mapping, pages, nr_pages, nilfs_get_block);
139}
140
141static int nilfs_writepages(struct address_space *mapping,
142 struct writeback_control *wbc)
143{
f30bf3e4
RK
144 struct inode *inode = mapping->host;
145 int err = 0;
146
147 if (wbc->sync_mode == WB_SYNC_ALL)
148 err = nilfs_construct_dsync_segment(inode->i_sb, inode,
149 wbc->range_start,
150 wbc->range_end);
151 return err;
05fe58fd
RK
152}
153
154static int nilfs_writepage(struct page *page, struct writeback_control *wbc)
155{
156 struct inode *inode = page->mapping->host;
157 int err;
158
159 redirty_page_for_writepage(wbc, page);
160 unlock_page(page);
161
162 if (wbc->sync_mode == WB_SYNC_ALL) {
163 err = nilfs_construct_segment(inode->i_sb);
164 if (unlikely(err))
165 return err;
166 } else if (wbc->for_reclaim)
167 nilfs_flush_segment(inode->i_sb, inode->i_ino);
168
169 return 0;
170}
171
172static int nilfs_set_page_dirty(struct page *page)
173{
174 int ret = __set_page_dirty_buffers(page);
175
176 if (ret) {
177 struct inode *inode = page->mapping->host;
178 struct nilfs_sb_info *sbi = NILFS_SB(inode->i_sb);
179 unsigned nr_dirty = 1 << (PAGE_SHIFT - inode->i_blkbits);
180
181 nilfs_set_file_dirty(sbi, inode, nr_dirty);
182 }
183 return ret;
184}
185
186static int nilfs_write_begin(struct file *file, struct address_space *mapping,
187 loff_t pos, unsigned len, unsigned flags,
188 struct page **pagep, void **fsdata)
189
190{
191 struct inode *inode = mapping->host;
192 int err = nilfs_transaction_begin(inode->i_sb, NULL, 1);
193
194 if (unlikely(err))
195 return err;
196
197 *pagep = NULL;
198 err = block_write_begin(file, mapping, pos, len, flags, pagep,
199 fsdata, nilfs_get_block);
200 if (unlikely(err))
47420c79 201 nilfs_transaction_abort(inode->i_sb);
05fe58fd
RK
202 return err;
203}
204
205static int nilfs_write_end(struct file *file, struct address_space *mapping,
206 loff_t pos, unsigned len, unsigned copied,
207 struct page *page, void *fsdata)
208{
209 struct inode *inode = mapping->host;
210 unsigned start = pos & (PAGE_CACHE_SIZE - 1);
211 unsigned nr_dirty;
212 int err;
213
214 nr_dirty = nilfs_page_count_clean_buffers(page, start,
215 start + copied);
216 copied = generic_write_end(file, mapping, pos, len, copied, page,
217 fsdata);
218 nilfs_set_file_dirty(NILFS_SB(inode->i_sb), inode, nr_dirty);
47420c79 219 err = nilfs_transaction_commit(inode->i_sb);
05fe58fd
RK
220 return err ? : copied;
221}
222
223static ssize_t
224nilfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
225 loff_t offset, unsigned long nr_segs)
226{
227 struct file *file = iocb->ki_filp;
228 struct inode *inode = file->f_mapping->host;
229 ssize_t size;
05fe58fd
RK
230
231 if (rw == WRITE)
232 return 0;
233
234 /* Needs synchronization with the cleaner */
235 size = blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
236 offset, nr_segs, nilfs_get_block, NULL);
237 return size;
238}
239
240struct address_space_operations nilfs_aops = {
241 .writepage = nilfs_writepage,
242 .readpage = nilfs_readpage,
243 /* .sync_page = nilfs_sync_page, */
244 .writepages = nilfs_writepages,
245 .set_page_dirty = nilfs_set_page_dirty,
246 .readpages = nilfs_readpages,
247 .write_begin = nilfs_write_begin,
248 .write_end = nilfs_write_end,
249 /* .releasepage = nilfs_releasepage, */
250 .invalidatepage = block_invalidatepage,
251 .direct_IO = nilfs_direct_IO,
252};
253
254struct inode *nilfs_new_inode(struct inode *dir, int mode)
255{
256 struct super_block *sb = dir->i_sb;
257 struct nilfs_sb_info *sbi = NILFS_SB(sb);
258 struct inode *inode;
259 struct nilfs_inode_info *ii;
260 int err = -ENOMEM;
261 ino_t ino;
262
263 inode = new_inode(sb);
264 if (unlikely(!inode))
265 goto failed;
266
267 mapping_set_gfp_mask(inode->i_mapping,
268 mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS);
269
270 ii = NILFS_I(inode);
271 ii->i_state = 1 << NILFS_I_NEW;
272
273 err = nilfs_ifile_create_inode(sbi->s_ifile, &ino, &ii->i_bh);
274 if (unlikely(err))
275 goto failed_ifile_create_inode;
276 /* reference count of i_bh inherits from nilfs_mdt_read_block() */
277
278 atomic_inc(&sbi->s_inodes_count);
279
280 inode->i_uid = current_fsuid();
281 if (dir->i_mode & S_ISGID) {
282 inode->i_gid = dir->i_gid;
283 if (S_ISDIR(mode))
284 mode |= S_ISGID;
285 } else
286 inode->i_gid = current_fsgid();
287
288 inode->i_mode = mode;
289 inode->i_ino = ino;
290 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
291
292 if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) {
293 err = nilfs_bmap_read(ii->i_bmap, NULL);
294 if (err < 0)
295 goto failed_bmap;
296
297 set_bit(NILFS_I_BMAP, &ii->i_state);
298 /* No lock is needed; iget() ensures it. */
299 }
300
301 ii->i_flags = NILFS_I(dir)->i_flags;
302 if (S_ISLNK(mode))
303 ii->i_flags &= ~(NILFS_IMMUTABLE_FL | NILFS_APPEND_FL);
304 if (!S_ISDIR(mode))
305 ii->i_flags &= ~NILFS_DIRSYNC_FL;
306
307 /* ii->i_file_acl = 0; */
308 /* ii->i_dir_acl = 0; */
309 ii->i_dtime = 0;
310 ii->i_dir_start_lookup = 0;
311#ifdef CONFIG_NILFS_FS_POSIX_ACL
312 ii->i_acl = NULL;
313 ii->i_default_acl = NULL;
314#endif
315 ii->i_cno = 0;
316 nilfs_set_inode_flags(inode);
317 spin_lock(&sbi->s_next_gen_lock);
318 inode->i_generation = sbi->s_next_generation++;
319 spin_unlock(&sbi->s_next_gen_lock);
320 insert_inode_hash(inode);
321
322 err = nilfs_init_acl(inode, dir);
323 if (unlikely(err))
324 goto failed_acl; /* never occur. When supporting
325 nilfs_init_acl(), proper cancellation of
326 above jobs should be considered */
327
328 mark_inode_dirty(inode);
329 return inode;
330
331 failed_acl:
332 failed_bmap:
333 inode->i_nlink = 0;
334 iput(inode); /* raw_inode will be deleted through
335 generic_delete_inode() */
336 goto failed;
337
338 failed_ifile_create_inode:
339 make_bad_inode(inode);
340 iput(inode); /* if i_nlink == 1, generic_forget_inode() will be
341 called */
342 failed:
343 return ERR_PTR(err);
344}
345
346void nilfs_free_inode(struct inode *inode)
347{
348 struct super_block *sb = inode->i_sb;
349 struct nilfs_sb_info *sbi = NILFS_SB(sb);
350
351 clear_inode(inode);
352 /* XXX: check error code? Is there any thing I can do? */
353 (void) nilfs_ifile_delete_inode(sbi->s_ifile, inode->i_ino);
354 atomic_dec(&sbi->s_inodes_count);
355}
356
357void nilfs_set_inode_flags(struct inode *inode)
358{
359 unsigned int flags = NILFS_I(inode)->i_flags;
360
361 inode->i_flags &= ~(S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME |
362 S_DIRSYNC);
363 if (flags & NILFS_SYNC_FL)
364 inode->i_flags |= S_SYNC;
365 if (flags & NILFS_APPEND_FL)
366 inode->i_flags |= S_APPEND;
367 if (flags & NILFS_IMMUTABLE_FL)
368 inode->i_flags |= S_IMMUTABLE;
369#ifndef NILFS_ATIME_DISABLE
370 if (flags & NILFS_NOATIME_FL)
371#endif
372 inode->i_flags |= S_NOATIME;
373 if (flags & NILFS_DIRSYNC_FL)
374 inode->i_flags |= S_DIRSYNC;
375 mapping_set_gfp_mask(inode->i_mapping,
376 mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS);
377}
378
379int nilfs_read_inode_common(struct inode *inode,
380 struct nilfs_inode *raw_inode)
381{
382 struct nilfs_inode_info *ii = NILFS_I(inode);
383 int err;
384
385 inode->i_mode = le16_to_cpu(raw_inode->i_mode);
386 inode->i_uid = (uid_t)le32_to_cpu(raw_inode->i_uid);
387 inode->i_gid = (gid_t)le32_to_cpu(raw_inode->i_gid);
388 inode->i_nlink = le16_to_cpu(raw_inode->i_links_count);
389 inode->i_size = le64_to_cpu(raw_inode->i_size);
390 inode->i_atime.tv_sec = le64_to_cpu(raw_inode->i_mtime);
391 inode->i_ctime.tv_sec = le64_to_cpu(raw_inode->i_ctime);
392 inode->i_mtime.tv_sec = le64_to_cpu(raw_inode->i_mtime);
393 inode->i_atime.tv_nsec = 0;
394 inode->i_ctime.tv_nsec = 0;
395 inode->i_mtime.tv_nsec = 0;
396 ii->i_dtime = le64_to_cpu(raw_inode->i_dtime);
397 if (inode->i_nlink == 0 && (inode->i_mode == 0 || ii->i_dtime))
398 return -EINVAL; /* this inode is deleted */
399
400 inode->i_blocks = le64_to_cpu(raw_inode->i_blocks);
401 ii->i_flags = le32_to_cpu(raw_inode->i_flags);
402#if 0
403 ii->i_file_acl = le32_to_cpu(raw_inode->i_file_acl);
404 ii->i_dir_acl = S_ISREG(inode->i_mode) ?
405 0 : le32_to_cpu(raw_inode->i_dir_acl);
406#endif
407 ii->i_cno = 0;
408 inode->i_generation = le32_to_cpu(raw_inode->i_generation);
409
410 if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
411 S_ISLNK(inode->i_mode)) {
412 err = nilfs_bmap_read(ii->i_bmap, raw_inode);
413 if (err < 0)
414 return err;
415 set_bit(NILFS_I_BMAP, &ii->i_state);
416 /* No lock is needed; iget() ensures it. */
417 }
418 return 0;
419}
420
05fe58fd
RK
421static int __nilfs_read_inode(struct super_block *sb, unsigned long ino,
422 struct inode *inode)
423{
424 struct nilfs_sb_info *sbi = NILFS_SB(sb);
425 struct inode *dat = nilfs_dat_inode(sbi->s_nilfs);
426 struct buffer_head *bh;
427 struct nilfs_inode *raw_inode;
428 int err;
429
430 down_read(&NILFS_MDT(dat)->mi_sem); /* XXX */
431 err = nilfs_ifile_get_inode_block(sbi->s_ifile, ino, &bh);
432 if (unlikely(err))
433 goto bad_inode;
434
435 raw_inode = nilfs_ifile_map_inode(sbi->s_ifile, ino, bh);
436
437#ifdef CONFIG_NILFS_FS_POSIX_ACL
438 ii->i_acl = NILFS_ACL_NOT_CACHED;
439 ii->i_default_acl = NILFS_ACL_NOT_CACHED;
440#endif
441 if (nilfs_read_inode_common(inode, raw_inode))
442 goto failed_unmap;
443
444 if (S_ISREG(inode->i_mode)) {
445 inode->i_op = &nilfs_file_inode_operations;
446 inode->i_fop = &nilfs_file_operations;
447 inode->i_mapping->a_ops = &nilfs_aops;
05fe58fd
RK
448 } else if (S_ISDIR(inode->i_mode)) {
449 inode->i_op = &nilfs_dir_inode_operations;
450 inode->i_fop = &nilfs_dir_operations;
451 inode->i_mapping->a_ops = &nilfs_aops;
452 } else if (S_ISLNK(inode->i_mode)) {
453 inode->i_op = &nilfs_symlink_inode_operations;
454 inode->i_mapping->a_ops = &nilfs_aops;
455 } else {
456 inode->i_op = &nilfs_special_inode_operations;
457 init_special_inode(
458 inode, inode->i_mode,
459 new_decode_dev(le64_to_cpu(raw_inode->i_device_code)));
460 }
461 nilfs_ifile_unmap_inode(sbi->s_ifile, ino, bh);
462 brelse(bh);
463 up_read(&NILFS_MDT(dat)->mi_sem); /* XXX */
464 nilfs_set_inode_flags(inode);
465 return 0;
466
467 failed_unmap:
468 nilfs_ifile_unmap_inode(sbi->s_ifile, ino, bh);
469 brelse(bh);
470
471 bad_inode:
472 up_read(&NILFS_MDT(dat)->mi_sem); /* XXX */
473 return err;
474}
475
476struct inode *nilfs_iget(struct super_block *sb, unsigned long ino)
477{
478 struct inode *inode;
479 int err;
480
481 inode = iget_locked(sb, ino);
482 if (unlikely(!inode))
483 return ERR_PTR(-ENOMEM);
484 if (!(inode->i_state & I_NEW))
485 return inode;
486
487 err = __nilfs_read_inode(sb, ino, inode);
488 if (unlikely(err)) {
489 iget_failed(inode);
490 return ERR_PTR(err);
491 }
492 unlock_new_inode(inode);
493 return inode;
494}
495
496void nilfs_write_inode_common(struct inode *inode,
497 struct nilfs_inode *raw_inode, int has_bmap)
498{
499 struct nilfs_inode_info *ii = NILFS_I(inode);
500
501 raw_inode->i_mode = cpu_to_le16(inode->i_mode);
502 raw_inode->i_uid = cpu_to_le32(inode->i_uid);
503 raw_inode->i_gid = cpu_to_le32(inode->i_gid);
504 raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
505 raw_inode->i_size = cpu_to_le64(inode->i_size);
506 raw_inode->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
507 raw_inode->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec);
508 raw_inode->i_blocks = cpu_to_le64(inode->i_blocks);
509
510 raw_inode->i_dtime = cpu_to_le64(ii->i_dtime);
511 raw_inode->i_flags = cpu_to_le32(ii->i_flags);
512 raw_inode->i_generation = cpu_to_le32(inode->i_generation);
513
514 if (has_bmap)
515 nilfs_bmap_write(ii->i_bmap, raw_inode);
516 else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
517 raw_inode->i_device_code =
518 cpu_to_le64(new_encode_dev(inode->i_rdev));
519 /* When extending inode, nilfs->ns_inode_size should be checked
520 for substitutions of appended fields */
521}
522
523void nilfs_update_inode(struct inode *inode, struct buffer_head *ibh)
524{
525 ino_t ino = inode->i_ino;
526 struct nilfs_inode_info *ii = NILFS_I(inode);
527 struct super_block *sb = inode->i_sb;
528 struct nilfs_sb_info *sbi = NILFS_SB(sb);
529 struct nilfs_inode *raw_inode;
530
531 raw_inode = nilfs_ifile_map_inode(sbi->s_ifile, ino, ibh);
532
533 /* The buffer is guarded with lock_buffer() by the caller */
534 if (test_and_clear_bit(NILFS_I_NEW, &ii->i_state))
535 memset(raw_inode, 0, NILFS_MDT(sbi->s_ifile)->mi_entry_size);
536 set_bit(NILFS_I_INODE_DIRTY, &ii->i_state);
537
538 nilfs_write_inode_common(inode, raw_inode, 0);
539 /* XXX: call with has_bmap = 0 is a workaround to avoid
540 deadlock of bmap. This delays update of i_bmap to just
541 before writing */
542 nilfs_ifile_unmap_inode(sbi->s_ifile, ino, ibh);
543}
544
545#define NILFS_MAX_TRUNCATE_BLOCKS 16384 /* 64MB for 4KB block */
546
547static void nilfs_truncate_bmap(struct nilfs_inode_info *ii,
548 unsigned long from)
549{
550 unsigned long b;
551 int ret;
552
553 if (!test_bit(NILFS_I_BMAP, &ii->i_state))
554 return;
555 repeat:
556 ret = nilfs_bmap_last_key(ii->i_bmap, &b);
557 if (ret == -ENOENT)
558 return;
559 else if (ret < 0)
560 goto failed;
561
562 if (b < from)
563 return;
564
565 b -= min_t(unsigned long, NILFS_MAX_TRUNCATE_BLOCKS, b - from);
566 ret = nilfs_bmap_truncate(ii->i_bmap, b);
567 nilfs_relax_pressure_in_lock(ii->vfs_inode.i_sb);
568 if (!ret || (ret == -ENOMEM &&
569 nilfs_bmap_truncate(ii->i_bmap, b) == 0))
570 goto repeat;
571
572 failed:
573 if (ret == -EINVAL)
574 nilfs_error(ii->vfs_inode.i_sb, __func__,
575 "bmap is broken (ino=%lu)", ii->vfs_inode.i_ino);
576 else
577 nilfs_warning(ii->vfs_inode.i_sb, __func__,
578 "failed to truncate bmap (ino=%lu, err=%d)",
579 ii->vfs_inode.i_ino, ret);
580}
581
582void nilfs_truncate(struct inode *inode)
583{
584 unsigned long blkoff;
585 unsigned int blocksize;
586 struct nilfs_transaction_info ti;
587 struct super_block *sb = inode->i_sb;
588 struct nilfs_inode_info *ii = NILFS_I(inode);
05fe58fd
RK
589
590 if (!test_bit(NILFS_I_BMAP, &ii->i_state))
591 return;
592 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
593 return;
594
595 blocksize = sb->s_blocksize;
596 blkoff = (inode->i_size + blocksize - 1) >> sb->s_blocksize_bits;
1f5abe7e 597 nilfs_transaction_begin(sb, &ti, 0); /* never fails */
05fe58fd
RK
598
599 block_truncate_page(inode->i_mapping, inode->i_size, nilfs_get_block);
600
601 nilfs_truncate_bmap(ii, blkoff);
602
603 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
604 if (IS_SYNC(inode))
605 nilfs_set_transaction_flag(NILFS_TI_SYNC);
606
607 nilfs_set_file_dirty(NILFS_SB(sb), inode, 0);
47420c79 608 nilfs_transaction_commit(sb);
05fe58fd
RK
609 /* May construct a logical segment and may fail in sync mode.
610 But truncate has no return value. */
611}
612
613void nilfs_delete_inode(struct inode *inode)
614{
615 struct nilfs_transaction_info ti;
616 struct super_block *sb = inode->i_sb;
617 struct nilfs_inode_info *ii = NILFS_I(inode);
05fe58fd
RK
618
619 if (unlikely(is_bad_inode(inode))) {
620 if (inode->i_data.nrpages)
621 truncate_inode_pages(&inode->i_data, 0);
622 clear_inode(inode);
623 return;
624 }
1f5abe7e
RK
625 nilfs_transaction_begin(sb, &ti, 0); /* never fails */
626
05fe58fd
RK
627 if (inode->i_data.nrpages)
628 truncate_inode_pages(&inode->i_data, 0);
629
630 nilfs_truncate_bmap(ii, 0);
631 nilfs_free_inode(inode);
632 /* nilfs_free_inode() marks inode buffer dirty */
633 if (IS_SYNC(inode))
634 nilfs_set_transaction_flag(NILFS_TI_SYNC);
47420c79 635 nilfs_transaction_commit(sb);
05fe58fd
RK
636 /* May construct a logical segment and may fail in sync mode.
637 But delete_inode has no return value. */
638}
639
640int nilfs_setattr(struct dentry *dentry, struct iattr *iattr)
641{
642 struct nilfs_transaction_info ti;
643 struct inode *inode = dentry->d_inode;
644 struct super_block *sb = inode->i_sb;
47420c79 645 int err;
05fe58fd
RK
646
647 err = inode_change_ok(inode, iattr);
648 if (err)
649 return err;
650
651 err = nilfs_transaction_begin(sb, &ti, 0);
652 if (unlikely(err))
653 return err;
654 err = inode_setattr(inode, iattr);
655 if (!err && (iattr->ia_valid & ATTR_MODE))
656 err = nilfs_acl_chmod(inode);
47420c79
RK
657 if (likely(!err))
658 err = nilfs_transaction_commit(sb);
659 else
660 nilfs_transaction_abort(sb);
661
662 return err;
05fe58fd
RK
663}
664
665int nilfs_load_inode_block(struct nilfs_sb_info *sbi, struct inode *inode,
666 struct buffer_head **pbh)
667{
668 struct nilfs_inode_info *ii = NILFS_I(inode);
669 int err;
670
671 spin_lock(&sbi->s_inode_lock);
672 /* Caller of this function MUST lock s_inode_lock */
673 if (ii->i_bh == NULL) {
674 spin_unlock(&sbi->s_inode_lock);
675 err = nilfs_ifile_get_inode_block(sbi->s_ifile, inode->i_ino,
676 pbh);
677 if (unlikely(err))
678 return err;
679 spin_lock(&sbi->s_inode_lock);
680 if (ii->i_bh == NULL)
681 ii->i_bh = *pbh;
682 else {
683 brelse(*pbh);
684 *pbh = ii->i_bh;
685 }
686 } else
687 *pbh = ii->i_bh;
688
689 get_bh(*pbh);
690 spin_unlock(&sbi->s_inode_lock);
691 return 0;
692}
693
694int nilfs_inode_dirty(struct inode *inode)
695{
696 struct nilfs_inode_info *ii = NILFS_I(inode);
697 struct nilfs_sb_info *sbi = NILFS_SB(inode->i_sb);
698 int ret = 0;
699
700 if (!list_empty(&ii->i_dirty)) {
701 spin_lock(&sbi->s_inode_lock);
702 ret = test_bit(NILFS_I_DIRTY, &ii->i_state) ||
703 test_bit(NILFS_I_BUSY, &ii->i_state);
704 spin_unlock(&sbi->s_inode_lock);
705 }
706 return ret;
707}
708
709int nilfs_set_file_dirty(struct nilfs_sb_info *sbi, struct inode *inode,
710 unsigned nr_dirty)
711{
712 struct nilfs_inode_info *ii = NILFS_I(inode);
713
714 atomic_add(nr_dirty, &sbi->s_nilfs->ns_ndirtyblks);
715
458c5b08 716 if (test_and_set_bit(NILFS_I_DIRTY, &ii->i_state))
05fe58fd
RK
717 return 0;
718
719 spin_lock(&sbi->s_inode_lock);
720 if (!test_bit(NILFS_I_QUEUED, &ii->i_state) &&
721 !test_bit(NILFS_I_BUSY, &ii->i_state)) {
722 /* Because this routine may race with nilfs_dispose_list(),
723 we have to check NILFS_I_QUEUED here, too. */
724 if (list_empty(&ii->i_dirty) && igrab(inode) == NULL) {
725 /* This will happen when somebody is freeing
726 this inode. */
727 nilfs_warning(sbi->s_super, __func__,
728 "cannot get inode (ino=%lu)\n",
729 inode->i_ino);
730 spin_unlock(&sbi->s_inode_lock);
731 return -EINVAL; /* NILFS_I_DIRTY may remain for
732 freeing inode */
733 }
734 list_del(&ii->i_dirty);
735 list_add_tail(&ii->i_dirty, &sbi->s_dirty_files);
736 set_bit(NILFS_I_QUEUED, &ii->i_state);
737 }
738 spin_unlock(&sbi->s_inode_lock);
739 return 0;
740}
741
742int nilfs_mark_inode_dirty(struct inode *inode)
743{
744 struct nilfs_sb_info *sbi = NILFS_SB(inode->i_sb);
745 struct buffer_head *ibh;
746 int err;
747
748 err = nilfs_load_inode_block(sbi, inode, &ibh);
749 if (unlikely(err)) {
750 nilfs_warning(inode->i_sb, __func__,
751 "failed to reget inode block.\n");
752 return err;
753 }
754 lock_buffer(ibh);
755 nilfs_update_inode(inode, ibh);
756 unlock_buffer(ibh);
757 nilfs_mdt_mark_buffer_dirty(ibh);
758 nilfs_mdt_mark_dirty(sbi->s_ifile);
759 brelse(ibh);
760 return 0;
761}
762
763/**
764 * nilfs_dirty_inode - reflect changes on given inode to an inode block.
765 * @inode: inode of the file to be registered.
766 *
767 * nilfs_dirty_inode() loads a inode block containing the specified
768 * @inode and copies data from a nilfs_inode to a corresponding inode
769 * entry in the inode block. This operation is excluded from the segment
770 * construction. This function can be called both as a single operation
771 * and as a part of indivisible file operations.
772 */
773void nilfs_dirty_inode(struct inode *inode)
774{
775 struct nilfs_transaction_info ti;
776
777 if (is_bad_inode(inode)) {
778 nilfs_warning(inode->i_sb, __func__,
779 "tried to mark bad_inode dirty. ignored.\n");
780 dump_stack();
781 return;
782 }
783 nilfs_transaction_begin(inode->i_sb, &ti, 0);
458c5b08 784 nilfs_mark_inode_dirty(inode);
47420c79 785 nilfs_transaction_commit(inode->i_sb); /* never fails */
05fe58fd 786}
This page took 0.058636 seconds and 5 git commands to generate.