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