GNU Linux-libre 4.19.314-gnu1
[releases.git] / fs / nilfs2 / inode.c
1 // 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  */
34 struct 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
43 static int nilfs_iget_test(struct inode *inode, void *opaque);
44
45 void 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
54 void 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  */
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_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  */
147 static 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  */
160 static 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
166 static 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
184 static 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
214 static 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
250 void 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
260 static 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
280 static 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
298 static ssize_t
299 nilfs_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
310 const 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
324 static 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
336 struct 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
434 void 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
453 int 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
496 static 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
551 static 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
579 static 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
598 struct 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
609 struct 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
620 struct 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         if (!(inode->i_state & I_NEW))
630                 return inode;
631
632         err = __nilfs_read_inode(sb, root, ino, inode);
633         if (unlikely(err)) {
634                 iget_failed(inode);
635                 return ERR_PTR(err);
636         }
637         unlock_new_inode(inode);
638         return inode;
639 }
640
641 struct inode *nilfs_iget_for_gc(struct super_block *sb, unsigned long ino,
642                                 __u64 cno)
643 {
644         struct nilfs_iget_args args = {
645                 .ino = ino, .root = NULL, .cno = cno, .for_gc = true,
646                 .for_btnc = false, .for_shadow = false
647         };
648         struct inode *inode;
649         int err;
650
651         inode = iget5_locked(sb, ino, nilfs_iget_test, nilfs_iget_set, &args);
652         if (unlikely(!inode))
653                 return ERR_PTR(-ENOMEM);
654         if (!(inode->i_state & I_NEW))
655                 return inode;
656
657         err = nilfs_init_gcinode(inode);
658         if (unlikely(err)) {
659                 iget_failed(inode);
660                 return ERR_PTR(err);
661         }
662         unlock_new_inode(inode);
663         return inode;
664 }
665
666 /**
667  * nilfs_attach_btree_node_cache - attach a B-tree node cache to the inode
668  * @inode: inode object
669  *
670  * nilfs_attach_btree_node_cache() attaches a B-tree node cache to @inode,
671  * or does nothing if the inode already has it.  This function allocates
672  * an additional inode to maintain page cache of B-tree nodes one-on-one.
673  *
674  * Return Value: On success, 0 is returned. On errors, one of the following
675  * negative error code is returned.
676  *
677  * %-ENOMEM - Insufficient memory available.
678  */
679 int nilfs_attach_btree_node_cache(struct inode *inode)
680 {
681         struct nilfs_inode_info *ii = NILFS_I(inode);
682         struct inode *btnc_inode;
683         struct nilfs_iget_args args;
684
685         if (ii->i_assoc_inode)
686                 return 0;
687
688         args.ino = inode->i_ino;
689         args.root = ii->i_root;
690         args.cno = ii->i_cno;
691         args.for_gc = test_bit(NILFS_I_GCINODE, &ii->i_state) != 0;
692         args.for_btnc = true;
693         args.for_shadow = test_bit(NILFS_I_SHADOW, &ii->i_state) != 0;
694
695         btnc_inode = iget5_locked(inode->i_sb, inode->i_ino, nilfs_iget_test,
696                                   nilfs_iget_set, &args);
697         if (unlikely(!btnc_inode))
698                 return -ENOMEM;
699         if (btnc_inode->i_state & I_NEW) {
700                 nilfs_init_btnc_inode(btnc_inode);
701                 unlock_new_inode(btnc_inode);
702         }
703         NILFS_I(btnc_inode)->i_assoc_inode = inode;
704         NILFS_I(btnc_inode)->i_bmap = ii->i_bmap;
705         ii->i_assoc_inode = btnc_inode;
706
707         return 0;
708 }
709
710 /**
711  * nilfs_detach_btree_node_cache - detach the B-tree node cache from the inode
712  * @inode: inode object
713  *
714  * nilfs_detach_btree_node_cache() detaches the B-tree node cache and its
715  * holder inode bound to @inode, or does nothing if @inode doesn't have it.
716  */
717 void nilfs_detach_btree_node_cache(struct inode *inode)
718 {
719         struct nilfs_inode_info *ii = NILFS_I(inode);
720         struct inode *btnc_inode = ii->i_assoc_inode;
721
722         if (btnc_inode) {
723                 NILFS_I(btnc_inode)->i_assoc_inode = NULL;
724                 ii->i_assoc_inode = NULL;
725                 iput(btnc_inode);
726         }
727 }
728
729 /**
730  * nilfs_iget_for_shadow - obtain inode for shadow mapping
731  * @inode: inode object that uses shadow mapping
732  *
733  * nilfs_iget_for_shadow() allocates a pair of inodes that holds page
734  * caches for shadow mapping.  The page cache for data pages is set up
735  * in one inode and the one for b-tree node pages is set up in the
736  * other inode, which is attached to the former inode.
737  *
738  * Return Value: On success, a pointer to the inode for data pages is
739  * returned. On errors, one of the following negative error code is returned
740  * in a pointer type.
741  *
742  * %-ENOMEM - Insufficient memory available.
743  */
744 struct inode *nilfs_iget_for_shadow(struct inode *inode)
745 {
746         struct nilfs_iget_args args = {
747                 .ino = inode->i_ino, .root = NULL, .cno = 0, .for_gc = false,
748                 .for_btnc = false, .for_shadow = true
749         };
750         struct inode *s_inode;
751         int err;
752
753         s_inode = iget5_locked(inode->i_sb, inode->i_ino, nilfs_iget_test,
754                                nilfs_iget_set, &args);
755         if (unlikely(!s_inode))
756                 return ERR_PTR(-ENOMEM);
757         if (!(s_inode->i_state & I_NEW))
758                 return inode;
759
760         NILFS_I(s_inode)->i_flags = 0;
761         memset(NILFS_I(s_inode)->i_bmap, 0, sizeof(struct nilfs_bmap));
762         mapping_set_gfp_mask(s_inode->i_mapping, GFP_NOFS);
763
764         err = nilfs_attach_btree_node_cache(s_inode);
765         if (unlikely(err)) {
766                 iget_failed(s_inode);
767                 return ERR_PTR(err);
768         }
769         unlock_new_inode(s_inode);
770         return s_inode;
771 }
772
773 void nilfs_write_inode_common(struct inode *inode,
774                               struct nilfs_inode *raw_inode, int has_bmap)
775 {
776         struct nilfs_inode_info *ii = NILFS_I(inode);
777
778         raw_inode->i_mode = cpu_to_le16(inode->i_mode);
779         raw_inode->i_uid = cpu_to_le32(i_uid_read(inode));
780         raw_inode->i_gid = cpu_to_le32(i_gid_read(inode));
781         raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
782         raw_inode->i_size = cpu_to_le64(inode->i_size);
783         raw_inode->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
784         raw_inode->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec);
785         raw_inode->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
786         raw_inode->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
787         raw_inode->i_blocks = cpu_to_le64(inode->i_blocks);
788
789         raw_inode->i_flags = cpu_to_le32(ii->i_flags);
790         raw_inode->i_generation = cpu_to_le32(inode->i_generation);
791
792         if (NILFS_ROOT_METADATA_FILE(inode->i_ino)) {
793                 struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
794
795                 /* zero-fill unused portion in the case of super root block */
796                 raw_inode->i_xattr = 0;
797                 raw_inode->i_pad = 0;
798                 memset((void *)raw_inode + sizeof(*raw_inode), 0,
799                        nilfs->ns_inode_size - sizeof(*raw_inode));
800         }
801
802         if (has_bmap)
803                 nilfs_bmap_write(ii->i_bmap, raw_inode);
804         else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
805                 raw_inode->i_device_code =
806                         cpu_to_le64(huge_encode_dev(inode->i_rdev));
807         /*
808          * When extending inode, nilfs->ns_inode_size should be checked
809          * for substitutions of appended fields.
810          */
811 }
812
813 void nilfs_update_inode(struct inode *inode, struct buffer_head *ibh, int flags)
814 {
815         ino_t ino = inode->i_ino;
816         struct nilfs_inode_info *ii = NILFS_I(inode);
817         struct inode *ifile = ii->i_root->ifile;
818         struct nilfs_inode *raw_inode;
819
820         raw_inode = nilfs_ifile_map_inode(ifile, ino, ibh);
821
822         if (test_and_clear_bit(NILFS_I_NEW, &ii->i_state))
823                 memset(raw_inode, 0, NILFS_MDT(ifile)->mi_entry_size);
824         if (flags & I_DIRTY_DATASYNC)
825                 set_bit(NILFS_I_INODE_SYNC, &ii->i_state);
826
827         nilfs_write_inode_common(inode, raw_inode, 0);
828                 /*
829                  * XXX: call with has_bmap = 0 is a workaround to avoid
830                  * deadlock of bmap.  This delays update of i_bmap to just
831                  * before writing.
832                  */
833
834         nilfs_ifile_unmap_inode(ifile, ino, ibh);
835 }
836
837 #define NILFS_MAX_TRUNCATE_BLOCKS       16384  /* 64MB for 4KB block */
838
839 static void nilfs_truncate_bmap(struct nilfs_inode_info *ii,
840                                 unsigned long from)
841 {
842         __u64 b;
843         int ret;
844
845         if (!test_bit(NILFS_I_BMAP, &ii->i_state))
846                 return;
847 repeat:
848         ret = nilfs_bmap_last_key(ii->i_bmap, &b);
849         if (ret == -ENOENT)
850                 return;
851         else if (ret < 0)
852                 goto failed;
853
854         if (b < from)
855                 return;
856
857         b -= min_t(__u64, NILFS_MAX_TRUNCATE_BLOCKS, b - from);
858         ret = nilfs_bmap_truncate(ii->i_bmap, b);
859         nilfs_relax_pressure_in_lock(ii->vfs_inode.i_sb);
860         if (!ret || (ret == -ENOMEM &&
861                      nilfs_bmap_truncate(ii->i_bmap, b) == 0))
862                 goto repeat;
863
864 failed:
865         nilfs_warn(ii->vfs_inode.i_sb, "error %d truncating bmap (ino=%lu)",
866                    ret, ii->vfs_inode.i_ino);
867 }
868
869 void nilfs_truncate(struct inode *inode)
870 {
871         unsigned long blkoff;
872         unsigned int blocksize;
873         struct nilfs_transaction_info ti;
874         struct super_block *sb = inode->i_sb;
875         struct nilfs_inode_info *ii = NILFS_I(inode);
876
877         if (!test_bit(NILFS_I_BMAP, &ii->i_state))
878                 return;
879         if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
880                 return;
881
882         blocksize = sb->s_blocksize;
883         blkoff = (inode->i_size + blocksize - 1) >> sb->s_blocksize_bits;
884         nilfs_transaction_begin(sb, &ti, 0); /* never fails */
885
886         block_truncate_page(inode->i_mapping, inode->i_size, nilfs_get_block);
887
888         nilfs_truncate_bmap(ii, blkoff);
889
890         inode->i_mtime = inode->i_ctime = current_time(inode);
891         if (IS_SYNC(inode))
892                 nilfs_set_transaction_flag(NILFS_TI_SYNC);
893
894         nilfs_mark_inode_dirty(inode);
895         nilfs_set_file_dirty(inode, 0);
896         nilfs_transaction_commit(sb);
897         /*
898          * May construct a logical segment and may fail in sync mode.
899          * But truncate has no return value.
900          */
901 }
902
903 static void nilfs_clear_inode(struct inode *inode)
904 {
905         struct nilfs_inode_info *ii = NILFS_I(inode);
906
907         /*
908          * Free resources allocated in nilfs_read_inode(), here.
909          */
910         BUG_ON(!list_empty(&ii->i_dirty));
911         brelse(ii->i_bh);
912         ii->i_bh = NULL;
913
914         if (nilfs_is_metadata_file_inode(inode))
915                 nilfs_mdt_clear(inode);
916
917         if (test_bit(NILFS_I_BMAP, &ii->i_state))
918                 nilfs_bmap_clear(ii->i_bmap);
919
920         if (!test_bit(NILFS_I_BTNC, &ii->i_state))
921                 nilfs_detach_btree_node_cache(inode);
922
923         if (ii->i_root && inode->i_ino == NILFS_ROOT_INO)
924                 nilfs_put_root(ii->i_root);
925 }
926
927 void nilfs_evict_inode(struct inode *inode)
928 {
929         struct nilfs_transaction_info ti;
930         struct super_block *sb = inode->i_sb;
931         struct nilfs_inode_info *ii = NILFS_I(inode);
932         struct the_nilfs *nilfs;
933         int ret;
934
935         if (inode->i_nlink || !ii->i_root || unlikely(is_bad_inode(inode))) {
936                 truncate_inode_pages_final(&inode->i_data);
937                 clear_inode(inode);
938                 nilfs_clear_inode(inode);
939                 return;
940         }
941         nilfs_transaction_begin(sb, &ti, 0); /* never fails */
942
943         truncate_inode_pages_final(&inode->i_data);
944
945         nilfs = sb->s_fs_info;
946         if (unlikely(sb_rdonly(sb) || !nilfs->ns_writer)) {
947                 /*
948                  * If this inode is about to be disposed after the file system
949                  * has been degraded to read-only due to file system corruption
950                  * or after the writer has been detached, do not make any
951                  * changes that cause writes, just clear it.
952                  * Do this check after read-locking ns_segctor_sem by
953                  * nilfs_transaction_begin() in order to avoid a race with
954                  * the writer detach operation.
955                  */
956                 clear_inode(inode);
957                 nilfs_clear_inode(inode);
958                 nilfs_transaction_abort(sb);
959                 return;
960         }
961
962         /* TODO: some of the following operations may fail.  */
963         nilfs_truncate_bmap(ii, 0);
964         nilfs_mark_inode_dirty(inode);
965         clear_inode(inode);
966
967         ret = nilfs_ifile_delete_inode(ii->i_root->ifile, inode->i_ino);
968         if (!ret)
969                 atomic64_dec(&ii->i_root->inodes_count);
970
971         nilfs_clear_inode(inode);
972
973         if (IS_SYNC(inode))
974                 nilfs_set_transaction_flag(NILFS_TI_SYNC);
975         nilfs_transaction_commit(sb);
976         /*
977          * May construct a logical segment and may fail in sync mode.
978          * But delete_inode has no return value.
979          */
980 }
981
982 int nilfs_setattr(struct dentry *dentry, struct iattr *iattr)
983 {
984         struct nilfs_transaction_info ti;
985         struct inode *inode = d_inode(dentry);
986         struct super_block *sb = inode->i_sb;
987         int err;
988
989         err = setattr_prepare(dentry, iattr);
990         if (err)
991                 return err;
992
993         err = nilfs_transaction_begin(sb, &ti, 0);
994         if (unlikely(err))
995                 return err;
996
997         if ((iattr->ia_valid & ATTR_SIZE) &&
998             iattr->ia_size != i_size_read(inode)) {
999                 inode_dio_wait(inode);
1000                 truncate_setsize(inode, iattr->ia_size);
1001                 nilfs_truncate(inode);
1002         }
1003
1004         setattr_copy(inode, iattr);
1005         mark_inode_dirty(inode);
1006
1007         if (iattr->ia_valid & ATTR_MODE) {
1008                 err = nilfs_acl_chmod(inode);
1009                 if (unlikely(err))
1010                         goto out_err;
1011         }
1012
1013         return nilfs_transaction_commit(sb);
1014
1015 out_err:
1016         nilfs_transaction_abort(sb);
1017         return err;
1018 }
1019
1020 int nilfs_permission(struct inode *inode, int mask)
1021 {
1022         struct nilfs_root *root = NILFS_I(inode)->i_root;
1023
1024         if ((mask & MAY_WRITE) && root &&
1025             root->cno != NILFS_CPTREE_CURRENT_CNO)
1026                 return -EROFS; /* snapshot is not writable */
1027
1028         return generic_permission(inode, mask);
1029 }
1030
1031 int nilfs_load_inode_block(struct inode *inode, struct buffer_head **pbh)
1032 {
1033         struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
1034         struct nilfs_inode_info *ii = NILFS_I(inode);
1035         int err;
1036
1037         spin_lock(&nilfs->ns_inode_lock);
1038         if (ii->i_bh == NULL || unlikely(!buffer_uptodate(ii->i_bh))) {
1039                 spin_unlock(&nilfs->ns_inode_lock);
1040                 err = nilfs_ifile_get_inode_block(ii->i_root->ifile,
1041                                                   inode->i_ino, pbh);
1042                 if (unlikely(err))
1043                         return err;
1044                 spin_lock(&nilfs->ns_inode_lock);
1045                 if (ii->i_bh == NULL)
1046                         ii->i_bh = *pbh;
1047                 else if (unlikely(!buffer_uptodate(ii->i_bh))) {
1048                         __brelse(ii->i_bh);
1049                         ii->i_bh = *pbh;
1050                 } else {
1051                         brelse(*pbh);
1052                         *pbh = ii->i_bh;
1053                 }
1054         } else
1055                 *pbh = ii->i_bh;
1056
1057         get_bh(*pbh);
1058         spin_unlock(&nilfs->ns_inode_lock);
1059         return 0;
1060 }
1061
1062 int nilfs_inode_dirty(struct inode *inode)
1063 {
1064         struct nilfs_inode_info *ii = NILFS_I(inode);
1065         struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
1066         int ret = 0;
1067
1068         if (!list_empty(&ii->i_dirty)) {
1069                 spin_lock(&nilfs->ns_inode_lock);
1070                 ret = test_bit(NILFS_I_DIRTY, &ii->i_state) ||
1071                         test_bit(NILFS_I_BUSY, &ii->i_state);
1072                 spin_unlock(&nilfs->ns_inode_lock);
1073         }
1074         return ret;
1075 }
1076
1077 int nilfs_set_file_dirty(struct inode *inode, unsigned int nr_dirty)
1078 {
1079         struct nilfs_inode_info *ii = NILFS_I(inode);
1080         struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
1081
1082         atomic_add(nr_dirty, &nilfs->ns_ndirtyblks);
1083
1084         if (test_and_set_bit(NILFS_I_DIRTY, &ii->i_state))
1085                 return 0;
1086
1087         spin_lock(&nilfs->ns_inode_lock);
1088         if (!test_bit(NILFS_I_QUEUED, &ii->i_state) &&
1089             !test_bit(NILFS_I_BUSY, &ii->i_state)) {
1090                 /*
1091                  * Because this routine may race with nilfs_dispose_list(),
1092                  * we have to check NILFS_I_QUEUED here, too.
1093                  */
1094                 if (list_empty(&ii->i_dirty) && igrab(inode) == NULL) {
1095                         /*
1096                          * This will happen when somebody is freeing
1097                          * this inode.
1098                          */
1099                         nilfs_warn(inode->i_sb,
1100                                    "cannot set file dirty (ino=%lu): the file is being freed",
1101                                    inode->i_ino);
1102                         spin_unlock(&nilfs->ns_inode_lock);
1103                         return -EINVAL; /*
1104                                          * NILFS_I_DIRTY may remain for
1105                                          * freeing inode.
1106                                          */
1107                 }
1108                 list_move_tail(&ii->i_dirty, &nilfs->ns_dirty_files);
1109                 set_bit(NILFS_I_QUEUED, &ii->i_state);
1110         }
1111         spin_unlock(&nilfs->ns_inode_lock);
1112         return 0;
1113 }
1114
1115 int __nilfs_mark_inode_dirty(struct inode *inode, int flags)
1116 {
1117         struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
1118         struct buffer_head *ibh;
1119         int err;
1120
1121         /*
1122          * Do not dirty inodes after the log writer has been detached
1123          * and its nilfs_root struct has been freed.
1124          */
1125         if (unlikely(nilfs_purging(nilfs)))
1126                 return 0;
1127
1128         err = nilfs_load_inode_block(inode, &ibh);
1129         if (unlikely(err)) {
1130                 nilfs_warn(inode->i_sb,
1131                            "cannot mark inode dirty (ino=%lu): error %d loading inode block",
1132                            inode->i_ino, err);
1133                 return err;
1134         }
1135         nilfs_update_inode(inode, ibh, flags);
1136         mark_buffer_dirty(ibh);
1137         nilfs_mdt_mark_dirty(NILFS_I(inode)->i_root->ifile);
1138         brelse(ibh);
1139         return 0;
1140 }
1141
1142 /**
1143  * nilfs_dirty_inode - reflect changes on given inode to an inode block.
1144  * @inode: inode of the file to be registered.
1145  *
1146  * nilfs_dirty_inode() loads a inode block containing the specified
1147  * @inode and copies data from a nilfs_inode to a corresponding inode
1148  * entry in the inode block. This operation is excluded from the segment
1149  * construction. This function can be called both as a single operation
1150  * and as a part of indivisible file operations.
1151  */
1152 void nilfs_dirty_inode(struct inode *inode, int flags)
1153 {
1154         struct nilfs_transaction_info ti;
1155         struct nilfs_mdt_info *mdi = NILFS_MDT(inode);
1156
1157         if (is_bad_inode(inode)) {
1158                 nilfs_warn(inode->i_sb,
1159                            "tried to mark bad_inode dirty. ignored.");
1160                 dump_stack();
1161                 return;
1162         }
1163         if (mdi) {
1164                 nilfs_mdt_mark_dirty(inode);
1165                 return;
1166         }
1167         nilfs_transaction_begin(inode->i_sb, &ti, 0);
1168         __nilfs_mark_inode_dirty(inode, flags);
1169         nilfs_transaction_commit(inode->i_sb); /* never fails */
1170 }
1171
1172 int nilfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1173                  __u64 start, __u64 len)
1174 {
1175         struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
1176         __u64 logical = 0, phys = 0, size = 0;
1177         __u32 flags = 0;
1178         loff_t isize;
1179         sector_t blkoff, end_blkoff;
1180         sector_t delalloc_blkoff;
1181         unsigned long delalloc_blklen;
1182         unsigned int blkbits = inode->i_blkbits;
1183         int ret, n;
1184
1185         ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
1186         if (ret)
1187                 return ret;
1188
1189         inode_lock(inode);
1190
1191         isize = i_size_read(inode);
1192
1193         blkoff = start >> blkbits;
1194         end_blkoff = (start + len - 1) >> blkbits;
1195
1196         delalloc_blklen = nilfs_find_uncommitted_extent(inode, blkoff,
1197                                                         &delalloc_blkoff);
1198
1199         do {
1200                 __u64 blkphy;
1201                 unsigned int maxblocks;
1202
1203                 if (delalloc_blklen && blkoff == delalloc_blkoff) {
1204                         if (size) {
1205                                 /* End of the current extent */
1206                                 ret = fiemap_fill_next_extent(
1207                                         fieinfo, logical, phys, size, flags);
1208                                 if (ret)
1209                                         break;
1210                         }
1211                         if (blkoff > end_blkoff)
1212                                 break;
1213
1214                         flags = FIEMAP_EXTENT_MERGED | FIEMAP_EXTENT_DELALLOC;
1215                         logical = blkoff << blkbits;
1216                         phys = 0;
1217                         size = delalloc_blklen << blkbits;
1218
1219                         blkoff = delalloc_blkoff + delalloc_blklen;
1220                         delalloc_blklen = nilfs_find_uncommitted_extent(
1221                                 inode, blkoff, &delalloc_blkoff);
1222                         continue;
1223                 }
1224
1225                 /*
1226                  * Limit the number of blocks that we look up so as
1227                  * not to get into the next delayed allocation extent.
1228                  */
1229                 maxblocks = INT_MAX;
1230                 if (delalloc_blklen)
1231                         maxblocks = min_t(sector_t, delalloc_blkoff - blkoff,
1232                                           maxblocks);
1233                 blkphy = 0;
1234
1235                 down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
1236                 n = nilfs_bmap_lookup_contig(
1237                         NILFS_I(inode)->i_bmap, blkoff, &blkphy, maxblocks);
1238                 up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
1239
1240                 if (n < 0) {
1241                         int past_eof;
1242
1243                         if (unlikely(n != -ENOENT))
1244                                 break; /* error */
1245
1246                         /* HOLE */
1247                         blkoff++;
1248                         past_eof = ((blkoff << blkbits) >= isize);
1249
1250                         if (size) {
1251                                 /* End of the current extent */
1252
1253                                 if (past_eof)
1254                                         flags |= FIEMAP_EXTENT_LAST;
1255
1256                                 ret = fiemap_fill_next_extent(
1257                                         fieinfo, logical, phys, size, flags);
1258                                 if (ret)
1259                                         break;
1260                                 size = 0;
1261                         }
1262                         if (blkoff > end_blkoff || past_eof)
1263                                 break;
1264                 } else {
1265                         if (size) {
1266                                 if (phys && blkphy << blkbits == phys + size) {
1267                                         /* The current extent goes on */
1268                                         size += n << blkbits;
1269                                 } else {
1270                                         /* Terminate the current extent */
1271                                         ret = fiemap_fill_next_extent(
1272                                                 fieinfo, logical, phys, size,
1273                                                 flags);
1274                                         if (ret || blkoff > end_blkoff)
1275                                                 break;
1276
1277                                         /* Start another extent */
1278                                         flags = FIEMAP_EXTENT_MERGED;
1279                                         logical = blkoff << blkbits;
1280                                         phys = blkphy << blkbits;
1281                                         size = n << blkbits;
1282                                 }
1283                         } else {
1284                                 /* Start a new extent */
1285                                 flags = FIEMAP_EXTENT_MERGED;
1286                                 logical = blkoff << blkbits;
1287                                 phys = blkphy << blkbits;
1288                                 size = n << blkbits;
1289                         }
1290                         blkoff += n;
1291                 }
1292                 cond_resched();
1293         } while (true);
1294
1295         /* If ret is 1 then we just hit the end of the extent array */
1296         if (ret == 1)
1297                 ret = 0;
1298
1299         inode_unlock(inode);
1300         return ret;
1301 }