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