blob: 6b2f580c9672c86b2835f0db1feec8e961e17e0c [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * inode.c - NILFS inode operations.
4 *
5 * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
6 *
7 * Written by Ryusuke Konishi.
8 *
9 */
10
11#include <linux/buffer_head.h>
12#include <linux/gfp.h>
13#include <linux/mpage.h>
14#include <linux/pagemap.h>
15#include <linux/writeback.h>
16#include <linux/uio.h>
17#include "nilfs.h"
18#include "btnode.h"
19#include "segment.h"
20#include "page.h"
21#include "mdt.h"
22#include "cpfile.h"
23#include "ifile.h"
24
25/**
26 * struct nilfs_iget_args - arguments used during comparison between inodes
27 * @ino: inode number
28 * @cno: checkpoint number
29 * @root: pointer on NILFS root object (mounted checkpoint)
30 * @for_gc: inode for GC flag
31 * @for_btnc: inode for B-tree node cache flag
32 * @for_shadow: inode for shadowed page cache flag
33 */
34struct nilfs_iget_args {
35 u64 ino;
36 __u64 cno;
37 struct nilfs_root *root;
38 bool for_gc;
39 bool for_btnc;
40 bool for_shadow;
41};
42
43static int nilfs_iget_test(struct inode *inode, void *opaque);
44
45void nilfs_inode_add_blocks(struct inode *inode, int n)
46{
47 struct nilfs_root *root = NILFS_I(inode)->i_root;
48
49 inode_add_bytes(inode, i_blocksize(inode) * n);
50 if (root)
51 atomic64_add(n, &root->blocks_count);
52}
53
54void nilfs_inode_sub_blocks(struct inode *inode, int n)
55{
56 struct nilfs_root *root = NILFS_I(inode)->i_root;
57
58 inode_sub_bytes(inode, i_blocksize(inode) * n);
59 if (root)
60 atomic64_sub(n, &root->blocks_count);
61}
62
63/**
64 * nilfs_get_block() - get a file block on the filesystem (callback function)
65 * @inode - inode struct of the target file
66 * @blkoff - file block number
67 * @bh_result - buffer head to be mapped on
68 * @create - indicate whether allocating the block or not when it has not
69 * been allocated yet.
70 *
71 * This function does not issue actual read request of the specified data
72 * block. It is done by VFS.
73 */
74int nilfs_get_block(struct inode *inode, sector_t blkoff,
75 struct buffer_head *bh_result, int create)
76{
77 struct nilfs_inode_info *ii = NILFS_I(inode);
78 struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
79 __u64 blknum = 0;
80 int err = 0, ret;
81 unsigned int maxblocks = bh_result->b_size >> inode->i_blkbits;
82
83 down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
84 ret = nilfs_bmap_lookup_contig(ii->i_bmap, blkoff, &blknum, maxblocks);
85 up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
86 if (ret >= 0) { /* found */
87 map_bh(bh_result, inode->i_sb, blknum);
88 if (ret > 0)
89 bh_result->b_size = (ret << inode->i_blkbits);
90 goto out;
91 }
92 /* data block was not found */
93 if (ret == -ENOENT && create) {
94 struct nilfs_transaction_info ti;
95
96 bh_result->b_blocknr = 0;
97 err = nilfs_transaction_begin(inode->i_sb, &ti, 1);
98 if (unlikely(err))
99 goto out;
100 err = nilfs_bmap_insert(ii->i_bmap, blkoff,
101 (unsigned long)bh_result);
102 if (unlikely(err != 0)) {
103 if (err == -EEXIST) {
104 /*
105 * The get_block() function could be called
106 * from multiple callers for an inode.
107 * However, the page having this block must
108 * be locked in this case.
109 */
110 nilfs_warn(inode->i_sb,
111 "%s (ino=%lu): a race condition while inserting a data block at offset=%llu",
112 __func__, inode->i_ino,
113 (unsigned long long)blkoff);
114 err = -EAGAIN;
115 }
116 nilfs_transaction_abort(inode->i_sb);
117 goto out;
118 }
119 nilfs_mark_inode_dirty_sync(inode);
120 nilfs_transaction_commit(inode->i_sb); /* never fails */
121 /* Error handling should be detailed */
122 set_buffer_new(bh_result);
123 set_buffer_delay(bh_result);
124 map_bh(bh_result, inode->i_sb, 0);
125 /* Disk block number must be changed to proper value */
126
127 } else if (ret == -ENOENT) {
128 /*
129 * not found is not error (e.g. hole); must return without
130 * the mapped state flag.
131 */
132 ;
133 } else {
134 err = ret;
135 }
136
137 out:
138 return err;
139}
140
141/**
142 * nilfs_readpage() - implement readpage() method of nilfs_aops {}
143 * address_space_operations.
144 * @file - file struct of the file to be read
145 * @page - the page to be read
146 */
147static int nilfs_readpage(struct file *file, struct page *page)
148{
149 return mpage_readpage(page, nilfs_get_block);
150}
151
152/**
153 * nilfs_readpages() - implement readpages() method of nilfs_aops {}
154 * address_space_operations.
155 * @file - file struct of the file to be read
156 * @mapping - address_space struct used for reading multiple pages
157 * @pages - the pages to be read
158 * @nr_pages - number of pages to be read
159 */
160static int nilfs_readpages(struct file *file, struct address_space *mapping,
161 struct list_head *pages, unsigned int nr_pages)
162{
163 return mpage_readpages(mapping, pages, nr_pages, nilfs_get_block);
164}
165
166static int nilfs_writepages(struct address_space *mapping,
167 struct writeback_control *wbc)
168{
169 struct inode *inode = mapping->host;
170 int err = 0;
171
172 if (sb_rdonly(inode->i_sb)) {
173 nilfs_clear_dirty_pages(mapping, false);
174 return -EROFS;
175 }
176
177 if (wbc->sync_mode == WB_SYNC_ALL)
178 err = nilfs_construct_dsync_segment(inode->i_sb, inode,
179 wbc->range_start,
180 wbc->range_end);
181 return err;
182}
183
184static int nilfs_writepage(struct page *page, struct writeback_control *wbc)
185{
186 struct inode *inode = page->mapping->host;
187 int err;
188
189 if (sb_rdonly(inode->i_sb)) {
190 /*
191 * It means that filesystem was remounted in read-only
192 * mode because of error or metadata corruption. But we
193 * have dirty pages that try to be flushed in background.
194 * So, here we simply discard this dirty page.
195 */
196 nilfs_clear_dirty_page(page, false);
197 unlock_page(page);
198 return -EROFS;
199 }
200
201 redirty_page_for_writepage(wbc, page);
202 unlock_page(page);
203
204 if (wbc->sync_mode == WB_SYNC_ALL) {
205 err = nilfs_construct_segment(inode->i_sb);
206 if (unlikely(err))
207 return err;
208 } else if (wbc->for_reclaim)
209 nilfs_flush_segment(inode->i_sb, inode->i_ino);
210
211 return 0;
212}
213
214static int nilfs_set_page_dirty(struct page *page)
215{
216 struct inode *inode = page->mapping->host;
217 int ret = __set_page_dirty_nobuffers(page);
218
219 if (page_has_buffers(page)) {
220 unsigned int nr_dirty = 0;
221 struct buffer_head *bh, *head;
222
223 /*
224 * This page is locked by callers, and no other thread
225 * concurrently marks its buffers dirty since they are
226 * only dirtied through routines in fs/buffer.c in
227 * which call sites of mark_buffer_dirty are protected
228 * by page lock.
229 */
230 bh = head = page_buffers(page);
231 do {
232 /* Do not mark hole blocks dirty */
233 if (buffer_dirty(bh) || !buffer_mapped(bh))
234 continue;
235
236 set_buffer_dirty(bh);
237 nr_dirty++;
238 } while (bh = bh->b_this_page, bh != head);
239
240 if (nr_dirty)
241 nilfs_set_file_dirty(inode, nr_dirty);
242 } else if (ret) {
243 unsigned int nr_dirty = 1 << (PAGE_SHIFT - inode->i_blkbits);
244
245 nilfs_set_file_dirty(inode, nr_dirty);
246 }
247 return ret;
248}
249
250void nilfs_write_failed(struct address_space *mapping, loff_t to)
251{
252 struct inode *inode = mapping->host;
253
254 if (to > inode->i_size) {
255 truncate_pagecache(inode, inode->i_size);
256 nilfs_truncate(inode);
257 }
258}
259
260static int nilfs_write_begin(struct file *file, struct address_space *mapping,
261 loff_t pos, unsigned len, unsigned flags,
262 struct page **pagep, void **fsdata)
263
264{
265 struct inode *inode = mapping->host;
266 int err = nilfs_transaction_begin(inode->i_sb, NULL, 1);
267
268 if (unlikely(err))
269 return err;
270
271 err = block_write_begin(mapping, pos, len, flags, pagep,
272 nilfs_get_block);
273 if (unlikely(err)) {
274 nilfs_write_failed(mapping, pos + len);
275 nilfs_transaction_abort(inode->i_sb);
276 }
277 return err;
278}
279
280static int nilfs_write_end(struct file *file, struct address_space *mapping,
281 loff_t pos, unsigned len, unsigned copied,
282 struct page *page, void *fsdata)
283{
284 struct inode *inode = mapping->host;
285 unsigned int start = pos & (PAGE_SIZE - 1);
286 unsigned int nr_dirty;
287 int err;
288
289 nr_dirty = nilfs_page_count_clean_buffers(page, start,
290 start + copied);
291 copied = generic_write_end(file, mapping, pos, len, copied, page,
292 fsdata);
293 nilfs_set_file_dirty(inode, nr_dirty);
294 err = nilfs_transaction_commit(inode->i_sb);
295 return err ? : copied;
296}
297
298static ssize_t
299nilfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
300{
301 struct inode *inode = file_inode(iocb->ki_filp);
302
303 if (iov_iter_rw(iter) == WRITE)
304 return 0;
305
306 /* Needs synchronization with the cleaner */
307 return blockdev_direct_IO(iocb, inode, iter, nilfs_get_block);
308}
309
310const struct address_space_operations nilfs_aops = {
311 .writepage = nilfs_writepage,
312 .readpage = nilfs_readpage,
313 .writepages = nilfs_writepages,
314 .set_page_dirty = nilfs_set_page_dirty,
315 .readpages = nilfs_readpages,
316 .write_begin = nilfs_write_begin,
317 .write_end = nilfs_write_end,
318 /* .releasepage = nilfs_releasepage, */
319 .invalidatepage = block_invalidatepage,
320 .direct_IO = nilfs_direct_IO,
321 .is_partially_uptodate = block_is_partially_uptodate,
322};
323
324static int nilfs_insert_inode_locked(struct inode *inode,
325 struct nilfs_root *root,
326 unsigned long ino)
327{
328 struct nilfs_iget_args args = {
329 .ino = ino, .root = root, .cno = 0, .for_gc = false,
330 .for_btnc = false, .for_shadow = false
331 };
332
333 return insert_inode_locked4(inode, ino, nilfs_iget_test, &args);
334}
335
336struct inode *nilfs_new_inode(struct inode *dir, umode_t mode)
337{
338 struct super_block *sb = dir->i_sb;
339 struct the_nilfs *nilfs = sb->s_fs_info;
340 struct inode *inode;
341 struct nilfs_inode_info *ii;
342 struct nilfs_root *root;
343 struct buffer_head *bh;
344 int err = -ENOMEM;
345 ino_t ino;
346
347 inode = new_inode(sb);
348 if (unlikely(!inode))
349 goto failed;
350
351 mapping_set_gfp_mask(inode->i_mapping,
352 mapping_gfp_constraint(inode->i_mapping, ~__GFP_FS));
353
354 root = NILFS_I(dir)->i_root;
355 ii = NILFS_I(inode);
356 ii->i_state = BIT(NILFS_I_NEW);
357 ii->i_root = root;
358
359 err = nilfs_ifile_create_inode(root->ifile, &ino, &bh);
360 if (unlikely(err))
361 goto failed_ifile_create_inode;
362 /* reference count of i_bh inherits from nilfs_mdt_read_block() */
363
364 if (unlikely(ino < NILFS_USER_INO)) {
365 nilfs_msg(sb, KERN_WARNING,
366 "inode bitmap is inconsistent for reserved inodes");
367 do {
368 brelse(bh);
369 err = nilfs_ifile_create_inode(root->ifile, &ino, &bh);
370 if (unlikely(err))
371 goto failed_ifile_create_inode;
372 } while (ino < NILFS_USER_INO);
373
374 nilfs_msg(sb, KERN_INFO,
375 "repaired inode bitmap for reserved inodes");
376 }
377 ii->i_bh = bh;
378
379 atomic64_inc(&root->inodes_count);
380 inode_init_owner(inode, dir, mode);
381 inode->i_ino = ino;
382 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
383
384 if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) {
385 err = nilfs_bmap_read(ii->i_bmap, NULL);
386 if (err < 0)
387 goto failed_after_creation;
388
389 set_bit(NILFS_I_BMAP, &ii->i_state);
390 /* No lock is needed; iget() ensures it. */
391 }
392
393 ii->i_flags = nilfs_mask_flags(
394 mode, NILFS_I(dir)->i_flags & NILFS_FL_INHERITED);
395
396 /* ii->i_file_acl = 0; */
397 /* ii->i_dir_acl = 0; */
398 ii->i_dir_start_lookup = 0;
399 nilfs_set_inode_flags(inode);
400 spin_lock(&nilfs->ns_next_gen_lock);
401 inode->i_generation = nilfs->ns_next_generation++;
402 spin_unlock(&nilfs->ns_next_gen_lock);
403 if (nilfs_insert_inode_locked(inode, root, ino) < 0) {
404 err = -EIO;
405 goto failed_after_creation;
406 }
407
408 err = nilfs_init_acl(inode, dir);
409 if (unlikely(err))
410 /*
411 * Never occur. When supporting nilfs_init_acl(),
412 * proper cancellation of above jobs should be considered.
413 */
414 goto failed_after_creation;
415
416 return inode;
417
418 failed_after_creation:
419 clear_nlink(inode);
420 unlock_new_inode(inode);
421 iput(inode); /*
422 * raw_inode will be deleted through
423 * nilfs_evict_inode().
424 */
425 goto failed;
426
427 failed_ifile_create_inode:
428 make_bad_inode(inode);
429 iput(inode);
430 failed:
431 return ERR_PTR(err);
432}
433
434void nilfs_set_inode_flags(struct inode *inode)
435{
436 unsigned int flags = NILFS_I(inode)->i_flags;
437 unsigned int new_fl = 0;
438
439 if (flags & FS_SYNC_FL)
440 new_fl |= S_SYNC;
441 if (flags & FS_APPEND_FL)
442 new_fl |= S_APPEND;
443 if (flags & FS_IMMUTABLE_FL)
444 new_fl |= S_IMMUTABLE;
445 if (flags & FS_NOATIME_FL)
446 new_fl |= S_NOATIME;
447 if (flags & FS_DIRSYNC_FL)
448 new_fl |= S_DIRSYNC;
449 inode_set_flags(inode, new_fl, S_SYNC | S_APPEND | S_IMMUTABLE |
450 S_NOATIME | S_DIRSYNC);
451}
452
453int nilfs_read_inode_common(struct inode *inode,
454 struct nilfs_inode *raw_inode)
455{
456 struct nilfs_inode_info *ii = NILFS_I(inode);
457 int err;
458
459 inode->i_mode = le16_to_cpu(raw_inode->i_mode);
460 i_uid_write(inode, le32_to_cpu(raw_inode->i_uid));
461 i_gid_write(inode, le32_to_cpu(raw_inode->i_gid));
462 set_nlink(inode, le16_to_cpu(raw_inode->i_links_count));
463 inode->i_size = le64_to_cpu(raw_inode->i_size);
464 inode->i_atime.tv_sec = le64_to_cpu(raw_inode->i_mtime);
465 inode->i_ctime.tv_sec = le64_to_cpu(raw_inode->i_ctime);
466 inode->i_mtime.tv_sec = le64_to_cpu(raw_inode->i_mtime);
467 inode->i_atime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec);
468 inode->i_ctime.tv_nsec = le32_to_cpu(raw_inode->i_ctime_nsec);
469 inode->i_mtime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec);
470 if (nilfs_is_metadata_file_inode(inode) && !S_ISREG(inode->i_mode))
471 return -EIO; /* this inode is for metadata and corrupted */
472 if (inode->i_nlink == 0)
473 return -ESTALE; /* this inode is deleted */
474
475 inode->i_blocks = le64_to_cpu(raw_inode->i_blocks);
476 ii->i_flags = le32_to_cpu(raw_inode->i_flags);
477#if 0
478 ii->i_file_acl = le32_to_cpu(raw_inode->i_file_acl);
479 ii->i_dir_acl = S_ISREG(inode->i_mode) ?
480 0 : le32_to_cpu(raw_inode->i_dir_acl);
481#endif
482 ii->i_dir_start_lookup = 0;
483 inode->i_generation = le32_to_cpu(raw_inode->i_generation);
484
485 if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
486 S_ISLNK(inode->i_mode)) {
487 err = nilfs_bmap_read(ii->i_bmap, raw_inode);
488 if (err < 0)
489 return err;
490 set_bit(NILFS_I_BMAP, &ii->i_state);
491 /* No lock is needed; iget() ensures it. */
492 }
493 return 0;
494}
495
496static int __nilfs_read_inode(struct super_block *sb,
497 struct nilfs_root *root, unsigned long ino,
498 struct inode *inode)
499{
500 struct the_nilfs *nilfs = sb->s_fs_info;
501 struct buffer_head *bh;
502 struct nilfs_inode *raw_inode;
503 int err;
504
505 down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
506 err = nilfs_ifile_get_inode_block(root->ifile, ino, &bh);
507 if (unlikely(err))
508 goto bad_inode;
509
510 raw_inode = nilfs_ifile_map_inode(root->ifile, ino, bh);
511
512 err = nilfs_read_inode_common(inode, raw_inode);
513 if (err)
514 goto failed_unmap;
515
516 if (S_ISREG(inode->i_mode)) {
517 inode->i_op = &nilfs_file_inode_operations;
518 inode->i_fop = &nilfs_file_operations;
519 inode->i_mapping->a_ops = &nilfs_aops;
520 } else if (S_ISDIR(inode->i_mode)) {
521 inode->i_op = &nilfs_dir_inode_operations;
522 inode->i_fop = &nilfs_dir_operations;
523 inode->i_mapping->a_ops = &nilfs_aops;
524 } else if (S_ISLNK(inode->i_mode)) {
525 inode->i_op = &nilfs_symlink_inode_operations;
526 inode_nohighmem(inode);
527 inode->i_mapping->a_ops = &nilfs_aops;
528 } else {
529 inode->i_op = &nilfs_special_inode_operations;
530 init_special_inode(
531 inode, inode->i_mode,
532 huge_decode_dev(le64_to_cpu(raw_inode->i_device_code)));
533 }
534 nilfs_ifile_unmap_inode(root->ifile, ino, bh);
535 brelse(bh);
536 up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
537 nilfs_set_inode_flags(inode);
538 mapping_set_gfp_mask(inode->i_mapping,
539 mapping_gfp_constraint(inode->i_mapping, ~__GFP_FS));
540 return 0;
541
542 failed_unmap:
543 nilfs_ifile_unmap_inode(root->ifile, ino, bh);
544 brelse(bh);
545
546 bad_inode:
547 up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
548 return err;
549}
550
551static int nilfs_iget_test(struct inode *inode, void *opaque)
552{
553 struct nilfs_iget_args *args = opaque;
554 struct nilfs_inode_info *ii;
555
556 if (args->ino != inode->i_ino || args->root != NILFS_I(inode)->i_root)
557 return 0;
558
559 ii = NILFS_I(inode);
560 if (test_bit(NILFS_I_BTNC, &ii->i_state)) {
561 if (!args->for_btnc)
562 return 0;
563 } else if (args->for_btnc) {
564 return 0;
565 }
566 if (test_bit(NILFS_I_SHADOW, &ii->i_state)) {
567 if (!args->for_shadow)
568 return 0;
569 } else if (args->for_shadow) {
570 return 0;
571 }
572
573 if (!test_bit(NILFS_I_GCINODE, &ii->i_state))
574 return !args->for_gc;
575
576 return args->for_gc && args->cno == ii->i_cno;
577}
578
579static int nilfs_iget_set(struct inode *inode, void *opaque)
580{
581 struct nilfs_iget_args *args = opaque;
582
583 inode->i_ino = args->ino;
584 NILFS_I(inode)->i_cno = args->cno;
585 NILFS_I(inode)->i_root = args->root;
586 if (args->root && args->ino == NILFS_ROOT_INO)
587 nilfs_get_root(args->root);
588
589 if (args->for_gc)
590 NILFS_I(inode)->i_state = BIT(NILFS_I_GCINODE);
591 if (args->for_btnc)
592 NILFS_I(inode)->i_state |= BIT(NILFS_I_BTNC);
593 if (args->for_shadow)
594 NILFS_I(inode)->i_state |= BIT(NILFS_I_SHADOW);
595 return 0;
596}
597
598struct inode *nilfs_ilookup(struct super_block *sb, struct nilfs_root *root,
599 unsigned long ino)
600{
601 struct nilfs_iget_args args = {
602 .ino = ino, .root = root, .cno = 0, .for_gc = false,
603 .for_btnc = false, .for_shadow = false
604 };
605
606 return ilookup5(sb, ino, nilfs_iget_test, &args);
607}
608
609struct inode *nilfs_iget_locked(struct super_block *sb, struct nilfs_root *root,
610 unsigned long ino)
611{
612 struct nilfs_iget_args args = {
613 .ino = ino, .root = root, .cno = 0, .for_gc = false,
614 .for_btnc = false, .for_shadow = false
615 };
616
617 return iget5_locked(sb, ino, nilfs_iget_test, nilfs_iget_set, &args);
618}
619
620struct inode *nilfs_iget(struct super_block *sb, struct nilfs_root *root,
621 unsigned long ino)
622{
623 struct inode *inode;
624 int err;
625
626 inode = nilfs_iget_locked(sb, root, ino);
627 if (unlikely(!inode))
628 return ERR_PTR(-ENOMEM);
629
630 if (!(inode->i_state & I_NEW)) {
631 if (!inode->i_nlink) {
632 iput(inode);
633 return ERR_PTR(-ESTALE);
634 }
635 return inode;
636 }
637
638 err = __nilfs_read_inode(sb, root, ino, inode);
639 if (unlikely(err)) {
640 iget_failed(inode);
641 return ERR_PTR(err);
642 }
643 unlock_new_inode(inode);
644 return inode;
645}
646
647struct inode *nilfs_iget_for_gc(struct super_block *sb, unsigned long ino,
648 __u64 cno)
649{
650 struct nilfs_iget_args args = {
651 .ino = ino, .root = NULL, .cno = cno, .for_gc = true,
652 .for_btnc = false, .for_shadow = false
653 };
654 struct inode *inode;
655 int err;
656
657 inode = iget5_locked(sb, ino, nilfs_iget_test, nilfs_iget_set, &args);
658 if (unlikely(!inode))
659 return ERR_PTR(-ENOMEM);
660 if (!(inode->i_state & I_NEW))
661 return inode;
662
663 err = nilfs_init_gcinode(inode);
664 if (unlikely(err)) {
665 iget_failed(inode);
666 return ERR_PTR(err);
667 }
668 unlock_new_inode(inode);
669 return inode;
670}
671
672/**
673 * nilfs_attach_btree_node_cache - attach a B-tree node cache to the inode
674 * @inode: inode object
675 *
676 * nilfs_attach_btree_node_cache() attaches a B-tree node cache to @inode,
677 * or does nothing if the inode already has it. This function allocates
678 * an additional inode to maintain page cache of B-tree nodes one-on-one.
679 *
680 * Return Value: On success, 0 is returned. On errors, one of the following
681 * negative error code is returned.
682 *
683 * %-ENOMEM - Insufficient memory available.
684 */
685int nilfs_attach_btree_node_cache(struct inode *inode)
686{
687 struct nilfs_inode_info *ii = NILFS_I(inode);
688 struct inode *btnc_inode;
689 struct nilfs_iget_args args;
690
691 if (ii->i_assoc_inode)
692 return 0;
693
694 args.ino = inode->i_ino;
695 args.root = ii->i_root;
696 args.cno = ii->i_cno;
697 args.for_gc = test_bit(NILFS_I_GCINODE, &ii->i_state) != 0;
698 args.for_btnc = true;
699 args.for_shadow = test_bit(NILFS_I_SHADOW, &ii->i_state) != 0;
700
701 btnc_inode = iget5_locked(inode->i_sb, inode->i_ino, nilfs_iget_test,
702 nilfs_iget_set, &args);
703 if (unlikely(!btnc_inode))
704 return -ENOMEM;
705 if (btnc_inode->i_state & I_NEW) {
706 nilfs_init_btnc_inode(btnc_inode);
707 unlock_new_inode(btnc_inode);
708 }
709 NILFS_I(btnc_inode)->i_assoc_inode = inode;
710 NILFS_I(btnc_inode)->i_bmap = ii->i_bmap;
711 ii->i_assoc_inode = btnc_inode;
712
713 return 0;
714}
715
716/**
717 * nilfs_detach_btree_node_cache - detach the B-tree node cache from the inode
718 * @inode: inode object
719 *
720 * nilfs_detach_btree_node_cache() detaches the B-tree node cache and its
721 * holder inode bound to @inode, or does nothing if @inode doesn't have it.
722 */
723void nilfs_detach_btree_node_cache(struct inode *inode)
724{
725 struct nilfs_inode_info *ii = NILFS_I(inode);
726 struct inode *btnc_inode = ii->i_assoc_inode;
727
728 if (btnc_inode) {
729 NILFS_I(btnc_inode)->i_assoc_inode = NULL;
730 ii->i_assoc_inode = NULL;
731 iput(btnc_inode);
732 }
733}
734
735/**
736 * nilfs_iget_for_shadow - obtain inode for shadow mapping
737 * @inode: inode object that uses shadow mapping
738 *
739 * nilfs_iget_for_shadow() allocates a pair of inodes that holds page
740 * caches for shadow mapping. The page cache for data pages is set up
741 * in one inode and the one for b-tree node pages is set up in the
742 * other inode, which is attached to the former inode.
743 *
744 * Return Value: On success, a pointer to the inode for data pages is
745 * returned. On errors, one of the following negative error code is returned
746 * in a pointer type.
747 *
748 * %-ENOMEM - Insufficient memory available.
749 */
750struct inode *nilfs_iget_for_shadow(struct inode *inode)
751{
752 struct nilfs_iget_args args = {
753 .ino = inode->i_ino, .root = NULL, .cno = 0, .for_gc = false,
754 .for_btnc = false, .for_shadow = true
755 };
756 struct inode *s_inode;
757 int err;
758
759 s_inode = iget5_locked(inode->i_sb, inode->i_ino, nilfs_iget_test,
760 nilfs_iget_set, &args);
761 if (unlikely(!s_inode))
762 return ERR_PTR(-ENOMEM);
763 if (!(s_inode->i_state & I_NEW))
764 return inode;
765
766 NILFS_I(s_inode)->i_flags = 0;
767 memset(NILFS_I(s_inode)->i_bmap, 0, sizeof(struct nilfs_bmap));
768 mapping_set_gfp_mask(s_inode->i_mapping, GFP_NOFS);
769
770 err = nilfs_attach_btree_node_cache(s_inode);
771 if (unlikely(err)) {
772 iget_failed(s_inode);
773 return ERR_PTR(err);
774 }
775 unlock_new_inode(s_inode);
776 return s_inode;
777}
778
779void nilfs_write_inode_common(struct inode *inode,
780 struct nilfs_inode *raw_inode, int has_bmap)
781{
782 struct nilfs_inode_info *ii = NILFS_I(inode);
783
784 raw_inode->i_mode = cpu_to_le16(inode->i_mode);
785 raw_inode->i_uid = cpu_to_le32(i_uid_read(inode));
786 raw_inode->i_gid = cpu_to_le32(i_gid_read(inode));
787 raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
788 raw_inode->i_size = cpu_to_le64(inode->i_size);
789 raw_inode->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
790 raw_inode->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec);
791 raw_inode->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
792 raw_inode->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
793 raw_inode->i_blocks = cpu_to_le64(inode->i_blocks);
794
795 raw_inode->i_flags = cpu_to_le32(ii->i_flags);
796 raw_inode->i_generation = cpu_to_le32(inode->i_generation);
797
798 if (NILFS_ROOT_METADATA_FILE(inode->i_ino)) {
799 struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
800
801 /* zero-fill unused portion in the case of super root block */
802 raw_inode->i_xattr = 0;
803 raw_inode->i_pad = 0;
804 memset((void *)raw_inode + sizeof(*raw_inode), 0,
805 nilfs->ns_inode_size - sizeof(*raw_inode));
806 }
807
808 if (has_bmap)
809 nilfs_bmap_write(ii->i_bmap, raw_inode);
810 else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
811 raw_inode->i_device_code =
812 cpu_to_le64(huge_encode_dev(inode->i_rdev));
813 /*
814 * When extending inode, nilfs->ns_inode_size should be checked
815 * for substitutions of appended fields.
816 */
817}
818
819void nilfs_update_inode(struct inode *inode, struct buffer_head *ibh, int flags)
820{
821 ino_t ino = inode->i_ino;
822 struct nilfs_inode_info *ii = NILFS_I(inode);
823 struct inode *ifile = ii->i_root->ifile;
824 struct nilfs_inode *raw_inode;
825
826 raw_inode = nilfs_ifile_map_inode(ifile, ino, ibh);
827
828 if (test_and_clear_bit(NILFS_I_NEW, &ii->i_state))
829 memset(raw_inode, 0, NILFS_MDT(ifile)->mi_entry_size);
830 if (flags & I_DIRTY_DATASYNC)
831 set_bit(NILFS_I_INODE_SYNC, &ii->i_state);
832
833 nilfs_write_inode_common(inode, raw_inode, 0);
834 /*
835 * XXX: call with has_bmap = 0 is a workaround to avoid
836 * deadlock of bmap. This delays update of i_bmap to just
837 * before writing.
838 */
839
840 nilfs_ifile_unmap_inode(ifile, ino, ibh);
841}
842
843#define NILFS_MAX_TRUNCATE_BLOCKS 16384 /* 64MB for 4KB block */
844
845static void nilfs_truncate_bmap(struct nilfs_inode_info *ii,
846 unsigned long from)
847{
848 __u64 b;
849 int ret;
850
851 if (!test_bit(NILFS_I_BMAP, &ii->i_state))
852 return;
853repeat:
854 ret = nilfs_bmap_last_key(ii->i_bmap, &b);
855 if (ret == -ENOENT)
856 return;
857 else if (ret < 0)
858 goto failed;
859
860 if (b < from)
861 return;
862
863 b -= min_t(__u64, NILFS_MAX_TRUNCATE_BLOCKS, b - from);
864 ret = nilfs_bmap_truncate(ii->i_bmap, b);
865 nilfs_relax_pressure_in_lock(ii->vfs_inode.i_sb);
866 if (!ret || (ret == -ENOMEM &&
867 nilfs_bmap_truncate(ii->i_bmap, b) == 0))
868 goto repeat;
869
870failed:
871 nilfs_warn(ii->vfs_inode.i_sb, "error %d truncating bmap (ino=%lu)",
872 ret, ii->vfs_inode.i_ino);
873}
874
875void nilfs_truncate(struct inode *inode)
876{
877 unsigned long blkoff;
878 unsigned int blocksize;
879 struct nilfs_transaction_info ti;
880 struct super_block *sb = inode->i_sb;
881 struct nilfs_inode_info *ii = NILFS_I(inode);
882
883 if (!test_bit(NILFS_I_BMAP, &ii->i_state))
884 return;
885 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
886 return;
887
888 blocksize = sb->s_blocksize;
889 blkoff = (inode->i_size + blocksize - 1) >> sb->s_blocksize_bits;
890 nilfs_transaction_begin(sb, &ti, 0); /* never fails */
891
892 block_truncate_page(inode->i_mapping, inode->i_size, nilfs_get_block);
893
894 nilfs_truncate_bmap(ii, blkoff);
895
896 inode->i_mtime = inode->i_ctime = current_time(inode);
897 if (IS_SYNC(inode))
898 nilfs_set_transaction_flag(NILFS_TI_SYNC);
899
900 nilfs_mark_inode_dirty(inode);
901 nilfs_set_file_dirty(inode, 0);
902 nilfs_transaction_commit(sb);
903 /*
904 * May construct a logical segment and may fail in sync mode.
905 * But truncate has no return value.
906 */
907}
908
909static void nilfs_clear_inode(struct inode *inode)
910{
911 struct nilfs_inode_info *ii = NILFS_I(inode);
912
913 /*
914 * Free resources allocated in nilfs_read_inode(), here.
915 */
916 BUG_ON(!list_empty(&ii->i_dirty));
917 brelse(ii->i_bh);
918 ii->i_bh = NULL;
919
920 if (nilfs_is_metadata_file_inode(inode))
921 nilfs_mdt_clear(inode);
922
923 if (test_bit(NILFS_I_BMAP, &ii->i_state))
924 nilfs_bmap_clear(ii->i_bmap);
925
926 if (!test_bit(NILFS_I_BTNC, &ii->i_state))
927 nilfs_detach_btree_node_cache(inode);
928
929 if (ii->i_root && inode->i_ino == NILFS_ROOT_INO)
930 nilfs_put_root(ii->i_root);
931}
932
933void nilfs_evict_inode(struct inode *inode)
934{
935 struct nilfs_transaction_info ti;
936 struct super_block *sb = inode->i_sb;
937 struct nilfs_inode_info *ii = NILFS_I(inode);
938 struct the_nilfs *nilfs;
939 int ret;
940
941 if (inode->i_nlink || !ii->i_root || unlikely(is_bad_inode(inode))) {
942 truncate_inode_pages_final(&inode->i_data);
943 clear_inode(inode);
944 nilfs_clear_inode(inode);
945 return;
946 }
947 nilfs_transaction_begin(sb, &ti, 0); /* never fails */
948
949 truncate_inode_pages_final(&inode->i_data);
950
951 nilfs = sb->s_fs_info;
952 if (unlikely(sb_rdonly(sb) || !nilfs->ns_writer)) {
953 /*
954 * If this inode is about to be disposed after the file system
955 * has been degraded to read-only due to file system corruption
956 * or after the writer has been detached, do not make any
957 * changes that cause writes, just clear it.
958 * Do this check after read-locking ns_segctor_sem by
959 * nilfs_transaction_begin() in order to avoid a race with
960 * the writer detach operation.
961 */
962 clear_inode(inode);
963 nilfs_clear_inode(inode);
964 nilfs_transaction_abort(sb);
965 return;
966 }
967
968 /* TODO: some of the following operations may fail. */
969 nilfs_truncate_bmap(ii, 0);
970 nilfs_mark_inode_dirty(inode);
971 clear_inode(inode);
972
973 ret = nilfs_ifile_delete_inode(ii->i_root->ifile, inode->i_ino);
974 if (!ret)
975 atomic64_dec(&ii->i_root->inodes_count);
976
977 nilfs_clear_inode(inode);
978
979 if (IS_SYNC(inode))
980 nilfs_set_transaction_flag(NILFS_TI_SYNC);
981 nilfs_transaction_commit(sb);
982 /*
983 * May construct a logical segment and may fail in sync mode.
984 * But delete_inode has no return value.
985 */
986}
987
988int nilfs_setattr(struct dentry *dentry, struct iattr *iattr)
989{
990 struct nilfs_transaction_info ti;
991 struct inode *inode = d_inode(dentry);
992 struct super_block *sb = inode->i_sb;
993 int err;
994
995 err = setattr_prepare(dentry, iattr);
996 if (err)
997 return err;
998
999 err = nilfs_transaction_begin(sb, &ti, 0);
1000 if (unlikely(err))
1001 return err;
1002
1003 if ((iattr->ia_valid & ATTR_SIZE) &&
1004 iattr->ia_size != i_size_read(inode)) {
1005 inode_dio_wait(inode);
1006 truncate_setsize(inode, iattr->ia_size);
1007 nilfs_truncate(inode);
1008 }
1009
1010 setattr_copy(inode, iattr);
1011 mark_inode_dirty(inode);
1012
1013 if (iattr->ia_valid & ATTR_MODE) {
1014 err = nilfs_acl_chmod(inode);
1015 if (unlikely(err))
1016 goto out_err;
1017 }
1018
1019 return nilfs_transaction_commit(sb);
1020
1021out_err:
1022 nilfs_transaction_abort(sb);
1023 return err;
1024}
1025
1026int nilfs_permission(struct inode *inode, int mask)
1027{
1028 struct nilfs_root *root = NILFS_I(inode)->i_root;
1029
1030 if ((mask & MAY_WRITE) && root &&
1031 root->cno != NILFS_CPTREE_CURRENT_CNO)
1032 return -EROFS; /* snapshot is not writable */
1033
1034 return generic_permission(inode, mask);
1035}
1036
1037int nilfs_load_inode_block(struct inode *inode, struct buffer_head **pbh)
1038{
1039 struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
1040 struct nilfs_inode_info *ii = NILFS_I(inode);
1041 int err;
1042
1043 spin_lock(&nilfs->ns_inode_lock);
1044 if (ii->i_bh == NULL || unlikely(!buffer_uptodate(ii->i_bh))) {
1045 spin_unlock(&nilfs->ns_inode_lock);
1046 err = nilfs_ifile_get_inode_block(ii->i_root->ifile,
1047 inode->i_ino, pbh);
1048 if (unlikely(err))
1049 return err;
1050 spin_lock(&nilfs->ns_inode_lock);
1051 if (ii->i_bh == NULL)
1052 ii->i_bh = *pbh;
1053 else if (unlikely(!buffer_uptodate(ii->i_bh))) {
1054 __brelse(ii->i_bh);
1055 ii->i_bh = *pbh;
1056 } else {
1057 brelse(*pbh);
1058 *pbh = ii->i_bh;
1059 }
1060 } else
1061 *pbh = ii->i_bh;
1062
1063 get_bh(*pbh);
1064 spin_unlock(&nilfs->ns_inode_lock);
1065 return 0;
1066}
1067
1068int nilfs_inode_dirty(struct inode *inode)
1069{
1070 struct nilfs_inode_info *ii = NILFS_I(inode);
1071 struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
1072 int ret = 0;
1073
1074 if (!list_empty(&ii->i_dirty)) {
1075 spin_lock(&nilfs->ns_inode_lock);
1076 ret = test_bit(NILFS_I_DIRTY, &ii->i_state) ||
1077 test_bit(NILFS_I_BUSY, &ii->i_state);
1078 spin_unlock(&nilfs->ns_inode_lock);
1079 }
1080 return ret;
1081}
1082
1083int nilfs_set_file_dirty(struct inode *inode, unsigned int nr_dirty)
1084{
1085 struct nilfs_inode_info *ii = NILFS_I(inode);
1086 struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
1087
1088 atomic_add(nr_dirty, &nilfs->ns_ndirtyblks);
1089
1090 if (test_and_set_bit(NILFS_I_DIRTY, &ii->i_state))
1091 return 0;
1092
1093 spin_lock(&nilfs->ns_inode_lock);
1094 if (!test_bit(NILFS_I_QUEUED, &ii->i_state) &&
1095 !test_bit(NILFS_I_BUSY, &ii->i_state)) {
1096 /*
1097 * Because this routine may race with nilfs_dispose_list(),
1098 * we have to check NILFS_I_QUEUED here, too.
1099 */
1100 if (list_empty(&ii->i_dirty) && igrab(inode) == NULL) {
1101 /*
1102 * This will happen when somebody is freeing
1103 * this inode.
1104 */
1105 nilfs_warn(inode->i_sb,
1106 "cannot set file dirty (ino=%lu): the file is being freed",
1107 inode->i_ino);
1108 spin_unlock(&nilfs->ns_inode_lock);
1109 return -EINVAL; /*
1110 * NILFS_I_DIRTY may remain for
1111 * freeing inode.
1112 */
1113 }
1114 list_move_tail(&ii->i_dirty, &nilfs->ns_dirty_files);
1115 set_bit(NILFS_I_QUEUED, &ii->i_state);
1116 }
1117 spin_unlock(&nilfs->ns_inode_lock);
1118 return 0;
1119}
1120
1121int __nilfs_mark_inode_dirty(struct inode *inode, int flags)
1122{
1123 struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
1124 struct buffer_head *ibh;
1125 int err;
1126
1127 /*
1128 * Do not dirty inodes after the log writer has been detached
1129 * and its nilfs_root struct has been freed.
1130 */
1131 if (unlikely(nilfs_purging(nilfs)))
1132 return 0;
1133
1134 err = nilfs_load_inode_block(inode, &ibh);
1135 if (unlikely(err)) {
1136 nilfs_warn(inode->i_sb,
1137 "cannot mark inode dirty (ino=%lu): error %d loading inode block",
1138 inode->i_ino, err);
1139 return err;
1140 }
1141 nilfs_update_inode(inode, ibh, flags);
1142 mark_buffer_dirty(ibh);
1143 nilfs_mdt_mark_dirty(NILFS_I(inode)->i_root->ifile);
1144 brelse(ibh);
1145 return 0;
1146}
1147
1148/**
1149 * nilfs_dirty_inode - reflect changes on given inode to an inode block.
1150 * @inode: inode of the file to be registered.
1151 *
1152 * nilfs_dirty_inode() loads a inode block containing the specified
1153 * @inode and copies data from a nilfs_inode to a corresponding inode
1154 * entry in the inode block. This operation is excluded from the segment
1155 * construction. This function can be called both as a single operation
1156 * and as a part of indivisible file operations.
1157 */
1158void nilfs_dirty_inode(struct inode *inode, int flags)
1159{
1160 struct nilfs_transaction_info ti;
1161 struct nilfs_mdt_info *mdi = NILFS_MDT(inode);
1162
1163 if (is_bad_inode(inode)) {
1164 nilfs_warn(inode->i_sb,
1165 "tried to mark bad_inode dirty. ignored.");
1166 dump_stack();
1167 return;
1168 }
1169 if (mdi) {
1170 nilfs_mdt_mark_dirty(inode);
1171 return;
1172 }
1173 nilfs_transaction_begin(inode->i_sb, &ti, 0);
1174 __nilfs_mark_inode_dirty(inode, flags);
1175 nilfs_transaction_commit(inode->i_sb); /* never fails */
1176}
1177
1178int nilfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1179 __u64 start, __u64 len)
1180{
1181 struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
1182 __u64 logical = 0, phys = 0, size = 0;
1183 __u32 flags = 0;
1184 loff_t isize;
1185 sector_t blkoff, end_blkoff;
1186 sector_t delalloc_blkoff;
1187 unsigned long delalloc_blklen;
1188 unsigned int blkbits = inode->i_blkbits;
1189 int ret, n;
1190
1191 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
1192 if (ret)
1193 return ret;
1194
1195 inode_lock(inode);
1196
1197 isize = i_size_read(inode);
1198
1199 blkoff = start >> blkbits;
1200 end_blkoff = (start + len - 1) >> blkbits;
1201
1202 delalloc_blklen = nilfs_find_uncommitted_extent(inode, blkoff,
1203 &delalloc_blkoff);
1204
1205 do {
1206 __u64 blkphy;
1207 unsigned int maxblocks;
1208
1209 if (delalloc_blklen && blkoff == delalloc_blkoff) {
1210 if (size) {
1211 /* End of the current extent */
1212 ret = fiemap_fill_next_extent(
1213 fieinfo, logical, phys, size, flags);
1214 if (ret)
1215 break;
1216 }
1217 if (blkoff > end_blkoff)
1218 break;
1219
1220 flags = FIEMAP_EXTENT_MERGED | FIEMAP_EXTENT_DELALLOC;
1221 logical = blkoff << blkbits;
1222 phys = 0;
1223 size = delalloc_blklen << blkbits;
1224
1225 blkoff = delalloc_blkoff + delalloc_blklen;
1226 delalloc_blklen = nilfs_find_uncommitted_extent(
1227 inode, blkoff, &delalloc_blkoff);
1228 continue;
1229 }
1230
1231 /*
1232 * Limit the number of blocks that we look up so as
1233 * not to get into the next delayed allocation extent.
1234 */
1235 maxblocks = INT_MAX;
1236 if (delalloc_blklen)
1237 maxblocks = min_t(sector_t, delalloc_blkoff - blkoff,
1238 maxblocks);
1239 blkphy = 0;
1240
1241 down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
1242 n = nilfs_bmap_lookup_contig(
1243 NILFS_I(inode)->i_bmap, blkoff, &blkphy, maxblocks);
1244 up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
1245
1246 if (n < 0) {
1247 int past_eof;
1248
1249 if (unlikely(n != -ENOENT))
1250 break; /* error */
1251
1252 /* HOLE */
1253 blkoff++;
1254 past_eof = ((blkoff << blkbits) >= isize);
1255
1256 if (size) {
1257 /* End of the current extent */
1258
1259 if (past_eof)
1260 flags |= FIEMAP_EXTENT_LAST;
1261
1262 ret = fiemap_fill_next_extent(
1263 fieinfo, logical, phys, size, flags);
1264 if (ret)
1265 break;
1266 size = 0;
1267 }
1268 if (blkoff > end_blkoff || past_eof)
1269 break;
1270 } else {
1271 if (size) {
1272 if (phys && blkphy << blkbits == phys + size) {
1273 /* The current extent goes on */
1274 size += n << blkbits;
1275 } else {
1276 /* Terminate the current extent */
1277 ret = fiemap_fill_next_extent(
1278 fieinfo, logical, phys, size,
1279 flags);
1280 if (ret || blkoff > end_blkoff)
1281 break;
1282
1283 /* Start another extent */
1284 flags = FIEMAP_EXTENT_MERGED;
1285 logical = blkoff << blkbits;
1286 phys = blkphy << blkbits;
1287 size = n << blkbits;
1288 }
1289 } else {
1290 /* Start a new extent */
1291 flags = FIEMAP_EXTENT_MERGED;
1292 logical = blkoff << blkbits;
1293 phys = blkphy << blkbits;
1294 size = n << blkbits;
1295 }
1296 blkoff += n;
1297 }
1298 cond_resched();
1299 } while (true);
1300
1301 /* If ret is 1 then we just hit the end of the extent array */
1302 if (ret == 1)
1303 ret = 0;
1304
1305 inode_unlock(inode);
1306 return ret;
1307}