Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 
0003 #include <linux/blkdev.h>
0004 #include <linux/iversion.h>
0005 #include "compression.h"
0006 #include "ctree.h"
0007 #include "delalloc-space.h"
0008 #include "disk-io.h"
0009 #include "reflink.h"
0010 #include "transaction.h"
0011 #include "subpage.h"
0012 
0013 #define BTRFS_MAX_DEDUPE_LEN    SZ_16M
0014 
0015 static int clone_finish_inode_update(struct btrfs_trans_handle *trans,
0016                      struct inode *inode,
0017                      u64 endoff,
0018                      const u64 destoff,
0019                      const u64 olen,
0020                      int no_time_update)
0021 {
0022     struct btrfs_root *root = BTRFS_I(inode)->root;
0023     int ret;
0024 
0025     inode_inc_iversion(inode);
0026     if (!no_time_update) {
0027         inode->i_mtime = current_time(inode);
0028         inode->i_ctime = inode->i_mtime;
0029     }
0030     /*
0031      * We round up to the block size at eof when determining which
0032      * extents to clone above, but shouldn't round up the file size.
0033      */
0034     if (endoff > destoff + olen)
0035         endoff = destoff + olen;
0036     if (endoff > inode->i_size) {
0037         i_size_write(inode, endoff);
0038         btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
0039     }
0040 
0041     ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
0042     if (ret) {
0043         btrfs_abort_transaction(trans, ret);
0044         btrfs_end_transaction(trans);
0045         goto out;
0046     }
0047     ret = btrfs_end_transaction(trans);
0048 out:
0049     return ret;
0050 }
0051 
0052 static int copy_inline_to_page(struct btrfs_inode *inode,
0053                    const u64 file_offset,
0054                    char *inline_data,
0055                    const u64 size,
0056                    const u64 datal,
0057                    const u8 comp_type)
0058 {
0059     struct btrfs_fs_info *fs_info = inode->root->fs_info;
0060     const u32 block_size = fs_info->sectorsize;
0061     const u64 range_end = file_offset + block_size - 1;
0062     const size_t inline_size = size - btrfs_file_extent_calc_inline_size(0);
0063     char *data_start = inline_data + btrfs_file_extent_calc_inline_size(0);
0064     struct extent_changeset *data_reserved = NULL;
0065     struct page *page = NULL;
0066     struct address_space *mapping = inode->vfs_inode.i_mapping;
0067     int ret;
0068 
0069     ASSERT(IS_ALIGNED(file_offset, block_size));
0070 
0071     /*
0072      * We have flushed and locked the ranges of the source and destination
0073      * inodes, we also have locked the inodes, so we are safe to do a
0074      * reservation here. Also we must not do the reservation while holding
0075      * a transaction open, otherwise we would deadlock.
0076      */
0077     ret = btrfs_delalloc_reserve_space(inode, &data_reserved, file_offset,
0078                        block_size);
0079     if (ret)
0080         goto out;
0081 
0082     page = find_or_create_page(mapping, file_offset >> PAGE_SHIFT,
0083                    btrfs_alloc_write_mask(mapping));
0084     if (!page) {
0085         ret = -ENOMEM;
0086         goto out_unlock;
0087     }
0088 
0089     ret = set_page_extent_mapped(page);
0090     if (ret < 0)
0091         goto out_unlock;
0092 
0093     clear_extent_bit(&inode->io_tree, file_offset, range_end,
0094              EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
0095              0, 0, NULL);
0096     ret = btrfs_set_extent_delalloc(inode, file_offset, range_end, 0, NULL);
0097     if (ret)
0098         goto out_unlock;
0099 
0100     /*
0101      * After dirtying the page our caller will need to start a transaction,
0102      * and if we are low on metadata free space, that can cause flushing of
0103      * delalloc for all inodes in order to get metadata space released.
0104      * However we are holding the range locked for the whole duration of
0105      * the clone/dedupe operation, so we may deadlock if that happens and no
0106      * other task releases enough space. So mark this inode as not being
0107      * possible to flush to avoid such deadlock. We will clear that flag
0108      * when we finish cloning all extents, since a transaction is started
0109      * after finding each extent to clone.
0110      */
0111     set_bit(BTRFS_INODE_NO_DELALLOC_FLUSH, &inode->runtime_flags);
0112 
0113     if (comp_type == BTRFS_COMPRESS_NONE) {
0114         memcpy_to_page(page, offset_in_page(file_offset), data_start,
0115                    datal);
0116     } else {
0117         ret = btrfs_decompress(comp_type, data_start, page,
0118                        offset_in_page(file_offset),
0119                        inline_size, datal);
0120         if (ret)
0121             goto out_unlock;
0122         flush_dcache_page(page);
0123     }
0124 
0125     /*
0126      * If our inline data is smaller then the block/page size, then the
0127      * remaining of the block/page is equivalent to zeroes. We had something
0128      * like the following done:
0129      *
0130      * $ xfs_io -f -c "pwrite -S 0xab 0 500" file
0131      * $ sync  # (or fsync)
0132      * $ xfs_io -c "falloc 0 4K" file
0133      * $ xfs_io -c "pwrite -S 0xcd 4K 4K"
0134      *
0135      * So what's in the range [500, 4095] corresponds to zeroes.
0136      */
0137     if (datal < block_size)
0138         memzero_page(page, datal, block_size - datal);
0139 
0140     btrfs_page_set_uptodate(fs_info, page, file_offset, block_size);
0141     btrfs_page_clear_checked(fs_info, page, file_offset, block_size);
0142     btrfs_page_set_dirty(fs_info, page, file_offset, block_size);
0143 out_unlock:
0144     if (page) {
0145         unlock_page(page);
0146         put_page(page);
0147     }
0148     if (ret)
0149         btrfs_delalloc_release_space(inode, data_reserved, file_offset,
0150                          block_size, true);
0151     btrfs_delalloc_release_extents(inode, block_size);
0152 out:
0153     extent_changeset_free(data_reserved);
0154 
0155     return ret;
0156 }
0157 
0158 /*
0159  * Deal with cloning of inline extents. We try to copy the inline extent from
0160  * the source inode to destination inode when possible. When not possible we
0161  * copy the inline extent's data into the respective page of the inode.
0162  */
0163 static int clone_copy_inline_extent(struct inode *dst,
0164                     struct btrfs_path *path,
0165                     struct btrfs_key *new_key,
0166                     const u64 drop_start,
0167                     const u64 datal,
0168                     const u64 size,
0169                     const u8 comp_type,
0170                     char *inline_data,
0171                     struct btrfs_trans_handle **trans_out)
0172 {
0173     struct btrfs_fs_info *fs_info = btrfs_sb(dst->i_sb);
0174     struct btrfs_root *root = BTRFS_I(dst)->root;
0175     const u64 aligned_end = ALIGN(new_key->offset + datal,
0176                       fs_info->sectorsize);
0177     struct btrfs_trans_handle *trans = NULL;
0178     struct btrfs_drop_extents_args drop_args = { 0 };
0179     int ret;
0180     struct btrfs_key key;
0181 
0182     if (new_key->offset > 0) {
0183         ret = copy_inline_to_page(BTRFS_I(dst), new_key->offset,
0184                       inline_data, size, datal, comp_type);
0185         goto out;
0186     }
0187 
0188     key.objectid = btrfs_ino(BTRFS_I(dst));
0189     key.type = BTRFS_EXTENT_DATA_KEY;
0190     key.offset = 0;
0191     ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
0192     if (ret < 0) {
0193         return ret;
0194     } else if (ret > 0) {
0195         if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
0196             ret = btrfs_next_leaf(root, path);
0197             if (ret < 0)
0198                 return ret;
0199             else if (ret > 0)
0200                 goto copy_inline_extent;
0201         }
0202         btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
0203         if (key.objectid == btrfs_ino(BTRFS_I(dst)) &&
0204             key.type == BTRFS_EXTENT_DATA_KEY) {
0205             /*
0206              * There's an implicit hole at file offset 0, copy the
0207              * inline extent's data to the page.
0208              */
0209             ASSERT(key.offset > 0);
0210             goto copy_to_page;
0211         }
0212     } else if (i_size_read(dst) <= datal) {
0213         struct btrfs_file_extent_item *ei;
0214 
0215         ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
0216                     struct btrfs_file_extent_item);
0217         /*
0218          * If it's an inline extent replace it with the source inline
0219          * extent, otherwise copy the source inline extent data into
0220          * the respective page at the destination inode.
0221          */
0222         if (btrfs_file_extent_type(path->nodes[0], ei) ==
0223             BTRFS_FILE_EXTENT_INLINE)
0224             goto copy_inline_extent;
0225 
0226         goto copy_to_page;
0227     }
0228 
0229 copy_inline_extent:
0230     /*
0231      * We have no extent items, or we have an extent at offset 0 which may
0232      * or may not be inlined. All these cases are dealt the same way.
0233      */
0234     if (i_size_read(dst) > datal) {
0235         /*
0236          * At the destination offset 0 we have either a hole, a regular
0237          * extent or an inline extent larger then the one we want to
0238          * clone. Deal with all these cases by copying the inline extent
0239          * data into the respective page at the destination inode.
0240          */
0241         goto copy_to_page;
0242     }
0243 
0244     /*
0245      * Release path before starting a new transaction so we don't hold locks
0246      * that would confuse lockdep.
0247      */
0248     btrfs_release_path(path);
0249     /*
0250      * If we end up here it means were copy the inline extent into a leaf
0251      * of the destination inode. We know we will drop or adjust at most one
0252      * extent item in the destination root.
0253      *
0254      * 1 unit - adjusting old extent (we may have to split it)
0255      * 1 unit - add new extent
0256      * 1 unit - inode update
0257      */
0258     trans = btrfs_start_transaction(root, 3);
0259     if (IS_ERR(trans)) {
0260         ret = PTR_ERR(trans);
0261         trans = NULL;
0262         goto out;
0263     }
0264     drop_args.path = path;
0265     drop_args.start = drop_start;
0266     drop_args.end = aligned_end;
0267     drop_args.drop_cache = true;
0268     ret = btrfs_drop_extents(trans, root, BTRFS_I(dst), &drop_args);
0269     if (ret)
0270         goto out;
0271     ret = btrfs_insert_empty_item(trans, root, path, new_key, size);
0272     if (ret)
0273         goto out;
0274 
0275     write_extent_buffer(path->nodes[0], inline_data,
0276                 btrfs_item_ptr_offset(path->nodes[0],
0277                           path->slots[0]),
0278                 size);
0279     btrfs_update_inode_bytes(BTRFS_I(dst), datal, drop_args.bytes_found);
0280     btrfs_set_inode_full_sync(BTRFS_I(dst));
0281     ret = btrfs_inode_set_file_extent_range(BTRFS_I(dst), 0, aligned_end);
0282 out:
0283     if (!ret && !trans) {
0284         /*
0285          * No transaction here means we copied the inline extent into a
0286          * page of the destination inode.
0287          *
0288          * 1 unit to update inode item
0289          */
0290         trans = btrfs_start_transaction(root, 1);
0291         if (IS_ERR(trans)) {
0292             ret = PTR_ERR(trans);
0293             trans = NULL;
0294         }
0295     }
0296     if (ret && trans) {
0297         btrfs_abort_transaction(trans, ret);
0298         btrfs_end_transaction(trans);
0299     }
0300     if (!ret)
0301         *trans_out = trans;
0302 
0303     return ret;
0304 
0305 copy_to_page:
0306     /*
0307      * Release our path because we don't need it anymore and also because
0308      * copy_inline_to_page() needs to reserve data and metadata, which may
0309      * need to flush delalloc when we are low on available space and
0310      * therefore cause a deadlock if writeback of an inline extent needs to
0311      * write to the same leaf or an ordered extent completion needs to write
0312      * to the same leaf.
0313      */
0314     btrfs_release_path(path);
0315 
0316     ret = copy_inline_to_page(BTRFS_I(dst), new_key->offset,
0317                   inline_data, size, datal, comp_type);
0318     goto out;
0319 }
0320 
0321 /**
0322  * btrfs_clone() - clone a range from inode file to another
0323  *
0324  * @src: Inode to clone from
0325  * @inode: Inode to clone to
0326  * @off: Offset within source to start clone from
0327  * @olen: Original length, passed by user, of range to clone
0328  * @olen_aligned: Block-aligned value of olen
0329  * @destoff: Offset within @inode to start clone
0330  * @no_time_update: Whether to update mtime/ctime on the target inode
0331  */
0332 static int btrfs_clone(struct inode *src, struct inode *inode,
0333                const u64 off, const u64 olen, const u64 olen_aligned,
0334                const u64 destoff, int no_time_update)
0335 {
0336     struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
0337     struct btrfs_path *path = NULL;
0338     struct extent_buffer *leaf;
0339     struct btrfs_trans_handle *trans;
0340     char *buf = NULL;
0341     struct btrfs_key key;
0342     u32 nritems;
0343     int slot;
0344     int ret;
0345     const u64 len = olen_aligned;
0346     u64 last_dest_end = destoff;
0347     u64 prev_extent_end = off;
0348 
0349     ret = -ENOMEM;
0350     buf = kvmalloc(fs_info->nodesize, GFP_KERNEL);
0351     if (!buf)
0352         return ret;
0353 
0354     path = btrfs_alloc_path();
0355     if (!path) {
0356         kvfree(buf);
0357         return ret;
0358     }
0359 
0360     path->reada = READA_FORWARD;
0361     /* Clone data */
0362     key.objectid = btrfs_ino(BTRFS_I(src));
0363     key.type = BTRFS_EXTENT_DATA_KEY;
0364     key.offset = off;
0365 
0366     while (1) {
0367         struct btrfs_file_extent_item *extent;
0368         u64 extent_gen;
0369         int type;
0370         u32 size;
0371         struct btrfs_key new_key;
0372         u64 disko = 0, diskl = 0;
0373         u64 datao = 0, datal = 0;
0374         u8 comp;
0375         u64 drop_start;
0376 
0377         /* Note the key will change type as we walk through the tree */
0378         ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
0379                 0, 0);
0380         if (ret < 0)
0381             goto out;
0382         /*
0383          * First search, if no extent item that starts at offset off was
0384          * found but the previous item is an extent item, it's possible
0385          * it might overlap our target range, therefore process it.
0386          */
0387         if (key.offset == off && ret > 0 && path->slots[0] > 0) {
0388             btrfs_item_key_to_cpu(path->nodes[0], &key,
0389                           path->slots[0] - 1);
0390             if (key.type == BTRFS_EXTENT_DATA_KEY)
0391                 path->slots[0]--;
0392         }
0393 
0394         nritems = btrfs_header_nritems(path->nodes[0]);
0395 process_slot:
0396         if (path->slots[0] >= nritems) {
0397             ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
0398             if (ret < 0)
0399                 goto out;
0400             if (ret > 0)
0401                 break;
0402             nritems = btrfs_header_nritems(path->nodes[0]);
0403         }
0404         leaf = path->nodes[0];
0405         slot = path->slots[0];
0406 
0407         btrfs_item_key_to_cpu(leaf, &key, slot);
0408         if (key.type > BTRFS_EXTENT_DATA_KEY ||
0409             key.objectid != btrfs_ino(BTRFS_I(src)))
0410             break;
0411 
0412         ASSERT(key.type == BTRFS_EXTENT_DATA_KEY);
0413 
0414         extent = btrfs_item_ptr(leaf, slot,
0415                     struct btrfs_file_extent_item);
0416         extent_gen = btrfs_file_extent_generation(leaf, extent);
0417         comp = btrfs_file_extent_compression(leaf, extent);
0418         type = btrfs_file_extent_type(leaf, extent);
0419         if (type == BTRFS_FILE_EXTENT_REG ||
0420             type == BTRFS_FILE_EXTENT_PREALLOC) {
0421             disko = btrfs_file_extent_disk_bytenr(leaf, extent);
0422             diskl = btrfs_file_extent_disk_num_bytes(leaf, extent);
0423             datao = btrfs_file_extent_offset(leaf, extent);
0424             datal = btrfs_file_extent_num_bytes(leaf, extent);
0425         } else if (type == BTRFS_FILE_EXTENT_INLINE) {
0426             /* Take upper bound, may be compressed */
0427             datal = btrfs_file_extent_ram_bytes(leaf, extent);
0428         }
0429 
0430         /*
0431          * The first search might have left us at an extent item that
0432          * ends before our target range's start, can happen if we have
0433          * holes and NO_HOLES feature enabled.
0434          *
0435          * Subsequent searches may leave us on a file range we have
0436          * processed before - this happens due to a race with ordered
0437          * extent completion for a file range that is outside our source
0438          * range, but that range was part of a file extent item that
0439          * also covered a leading part of our source range.
0440          */
0441         if (key.offset + datal <= prev_extent_end) {
0442             path->slots[0]++;
0443             goto process_slot;
0444         } else if (key.offset >= off + len) {
0445             break;
0446         }
0447 
0448         prev_extent_end = key.offset + datal;
0449         size = btrfs_item_size(leaf, slot);
0450         read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf, slot),
0451                    size);
0452 
0453         btrfs_release_path(path);
0454 
0455         memcpy(&new_key, &key, sizeof(new_key));
0456         new_key.objectid = btrfs_ino(BTRFS_I(inode));
0457         if (off <= key.offset)
0458             new_key.offset = key.offset + destoff - off;
0459         else
0460             new_key.offset = destoff;
0461 
0462         /*
0463          * Deal with a hole that doesn't have an extent item that
0464          * represents it (NO_HOLES feature enabled).
0465          * This hole is either in the middle of the cloning range or at
0466          * the beginning (fully overlaps it or partially overlaps it).
0467          */
0468         if (new_key.offset != last_dest_end)
0469             drop_start = last_dest_end;
0470         else
0471             drop_start = new_key.offset;
0472 
0473         if (type == BTRFS_FILE_EXTENT_REG ||
0474             type == BTRFS_FILE_EXTENT_PREALLOC) {
0475             struct btrfs_replace_extent_info clone_info;
0476 
0477             /*
0478              *    a  | --- range to clone ---|  b
0479              * | ------------- extent ------------- |
0480              */
0481 
0482             /* Subtract range b */
0483             if (key.offset + datal > off + len)
0484                 datal = off + len - key.offset;
0485 
0486             /* Subtract range a */
0487             if (off > key.offset) {
0488                 datao += off - key.offset;
0489                 datal -= off - key.offset;
0490             }
0491 
0492             clone_info.disk_offset = disko;
0493             clone_info.disk_len = diskl;
0494             clone_info.data_offset = datao;
0495             clone_info.data_len = datal;
0496             clone_info.file_offset = new_key.offset;
0497             clone_info.extent_buf = buf;
0498             clone_info.is_new_extent = false;
0499             clone_info.update_times = !no_time_update;
0500             ret = btrfs_replace_file_extents(BTRFS_I(inode), path,
0501                     drop_start, new_key.offset + datal - 1,
0502                     &clone_info, &trans);
0503             if (ret)
0504                 goto out;
0505         } else {
0506             ASSERT(type == BTRFS_FILE_EXTENT_INLINE);
0507             /*
0508              * Inline extents always have to start at file offset 0
0509              * and can never be bigger then the sector size. We can
0510              * never clone only parts of an inline extent, since all
0511              * reflink operations must start at a sector size aligned
0512              * offset, and the length must be aligned too or end at
0513              * the i_size (which implies the whole inlined data).
0514              */
0515             ASSERT(key.offset == 0);
0516             ASSERT(datal <= fs_info->sectorsize);
0517             if (WARN_ON(type != BTRFS_FILE_EXTENT_INLINE) ||
0518                 WARN_ON(key.offset != 0) ||
0519                 WARN_ON(datal > fs_info->sectorsize)) {
0520                 ret = -EUCLEAN;
0521                 goto out;
0522             }
0523 
0524             ret = clone_copy_inline_extent(inode, path, &new_key,
0525                                drop_start, datal, size,
0526                                comp, buf, &trans);
0527             if (ret)
0528                 goto out;
0529         }
0530 
0531         btrfs_release_path(path);
0532 
0533         /*
0534          * Whenever we share an extent we update the last_reflink_trans
0535          * of each inode to the current transaction. This is needed to
0536          * make sure fsync does not log multiple checksum items with
0537          * overlapping ranges (because some extent items might refer
0538          * only to sections of the original extent). For the destination
0539          * inode we do this regardless of the generation of the extents
0540          * or even if they are inline extents or explicit holes, to make
0541          * sure a full fsync does not skip them. For the source inode,
0542          * we only need to update last_reflink_trans in case it's a new
0543          * extent that is not a hole or an inline extent, to deal with
0544          * the checksums problem on fsync.
0545          */
0546         if (extent_gen == trans->transid && disko > 0)
0547             BTRFS_I(src)->last_reflink_trans = trans->transid;
0548 
0549         BTRFS_I(inode)->last_reflink_trans = trans->transid;
0550 
0551         last_dest_end = ALIGN(new_key.offset + datal,
0552                       fs_info->sectorsize);
0553         ret = clone_finish_inode_update(trans, inode, last_dest_end,
0554                         destoff, olen, no_time_update);
0555         if (ret)
0556             goto out;
0557         if (new_key.offset + datal >= destoff + len)
0558             break;
0559 
0560         btrfs_release_path(path);
0561         key.offset = prev_extent_end;
0562 
0563         if (fatal_signal_pending(current)) {
0564             ret = -EINTR;
0565             goto out;
0566         }
0567 
0568         cond_resched();
0569     }
0570     ret = 0;
0571 
0572     if (last_dest_end < destoff + len) {
0573         /*
0574          * We have an implicit hole that fully or partially overlaps our
0575          * cloning range at its end. This means that we either have the
0576          * NO_HOLES feature enabled or the implicit hole happened due to
0577          * mixing buffered and direct IO writes against this file.
0578          */
0579         btrfs_release_path(path);
0580 
0581         /*
0582          * When using NO_HOLES and we are cloning a range that covers
0583          * only a hole (no extents) into a range beyond the current
0584          * i_size, punching a hole in the target range will not create
0585          * an extent map defining a hole, because the range starts at or
0586          * beyond current i_size. If the file previously had an i_size
0587          * greater than the new i_size set by this clone operation, we
0588          * need to make sure the next fsync is a full fsync, so that it
0589          * detects and logs a hole covering a range from the current
0590          * i_size to the new i_size. If the clone range covers extents,
0591          * besides a hole, then we know the full sync flag was already
0592          * set by previous calls to btrfs_replace_file_extents() that
0593          * replaced file extent items.
0594          */
0595         if (last_dest_end >= i_size_read(inode))
0596             btrfs_set_inode_full_sync(BTRFS_I(inode));
0597 
0598         ret = btrfs_replace_file_extents(BTRFS_I(inode), path,
0599                 last_dest_end, destoff + len - 1, NULL, &trans);
0600         if (ret)
0601             goto out;
0602 
0603         ret = clone_finish_inode_update(trans, inode, destoff + len,
0604                         destoff, olen, no_time_update);
0605     }
0606 
0607 out:
0608     btrfs_free_path(path);
0609     kvfree(buf);
0610     clear_bit(BTRFS_INODE_NO_DELALLOC_FLUSH, &BTRFS_I(inode)->runtime_flags);
0611 
0612     return ret;
0613 }
0614 
0615 static void btrfs_double_extent_unlock(struct inode *inode1, u64 loff1,
0616                        struct inode *inode2, u64 loff2, u64 len)
0617 {
0618     unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
0619     unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
0620 }
0621 
0622 static void btrfs_double_extent_lock(struct inode *inode1, u64 loff1,
0623                      struct inode *inode2, u64 loff2, u64 len)
0624 {
0625     u64 range1_end = loff1 + len - 1;
0626     u64 range2_end = loff2 + len - 1;
0627 
0628     if (inode1 < inode2) {
0629         swap(inode1, inode2);
0630         swap(loff1, loff2);
0631         swap(range1_end, range2_end);
0632     } else if (inode1 == inode2 && loff2 < loff1) {
0633         swap(loff1, loff2);
0634         swap(range1_end, range2_end);
0635     }
0636 
0637     lock_extent(&BTRFS_I(inode1)->io_tree, loff1, range1_end);
0638     lock_extent(&BTRFS_I(inode2)->io_tree, loff2, range2_end);
0639 
0640     btrfs_assert_inode_range_clean(BTRFS_I(inode1), loff1, range1_end);
0641     btrfs_assert_inode_range_clean(BTRFS_I(inode2), loff2, range2_end);
0642 }
0643 
0644 static void btrfs_double_mmap_lock(struct inode *inode1, struct inode *inode2)
0645 {
0646     if (inode1 < inode2)
0647         swap(inode1, inode2);
0648     down_write(&BTRFS_I(inode1)->i_mmap_lock);
0649     down_write_nested(&BTRFS_I(inode2)->i_mmap_lock, SINGLE_DEPTH_NESTING);
0650 }
0651 
0652 static void btrfs_double_mmap_unlock(struct inode *inode1, struct inode *inode2)
0653 {
0654     up_write(&BTRFS_I(inode1)->i_mmap_lock);
0655     up_write(&BTRFS_I(inode2)->i_mmap_lock);
0656 }
0657 
0658 static int btrfs_extent_same_range(struct inode *src, u64 loff, u64 len,
0659                    struct inode *dst, u64 dst_loff)
0660 {
0661     struct btrfs_fs_info *fs_info = BTRFS_I(src)->root->fs_info;
0662     const u64 bs = fs_info->sb->s_blocksize;
0663     int ret;
0664 
0665     /*
0666      * Lock destination range to serialize with concurrent readahead() and
0667      * source range to serialize with relocation.
0668      */
0669     btrfs_double_extent_lock(src, loff, dst, dst_loff, len);
0670     ret = btrfs_clone(src, dst, loff, len, ALIGN(len, bs), dst_loff, 1);
0671     btrfs_double_extent_unlock(src, loff, dst, dst_loff, len);
0672 
0673     btrfs_btree_balance_dirty(fs_info);
0674 
0675     return ret;
0676 }
0677 
0678 static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen,
0679                  struct inode *dst, u64 dst_loff)
0680 {
0681     int ret = 0;
0682     u64 i, tail_len, chunk_count;
0683     struct btrfs_root *root_dst = BTRFS_I(dst)->root;
0684 
0685     spin_lock(&root_dst->root_item_lock);
0686     if (root_dst->send_in_progress) {
0687         btrfs_warn_rl(root_dst->fs_info,
0688 "cannot deduplicate to root %llu while send operations are using it (%d in progress)",
0689                   root_dst->root_key.objectid,
0690                   root_dst->send_in_progress);
0691         spin_unlock(&root_dst->root_item_lock);
0692         return -EAGAIN;
0693     }
0694     root_dst->dedupe_in_progress++;
0695     spin_unlock(&root_dst->root_item_lock);
0696 
0697     tail_len = olen % BTRFS_MAX_DEDUPE_LEN;
0698     chunk_count = div_u64(olen, BTRFS_MAX_DEDUPE_LEN);
0699 
0700     for (i = 0; i < chunk_count; i++) {
0701         ret = btrfs_extent_same_range(src, loff, BTRFS_MAX_DEDUPE_LEN,
0702                           dst, dst_loff);
0703         if (ret)
0704             goto out;
0705 
0706         loff += BTRFS_MAX_DEDUPE_LEN;
0707         dst_loff += BTRFS_MAX_DEDUPE_LEN;
0708     }
0709 
0710     if (tail_len > 0)
0711         ret = btrfs_extent_same_range(src, loff, tail_len, dst, dst_loff);
0712 out:
0713     spin_lock(&root_dst->root_item_lock);
0714     root_dst->dedupe_in_progress--;
0715     spin_unlock(&root_dst->root_item_lock);
0716 
0717     return ret;
0718 }
0719 
0720 static noinline int btrfs_clone_files(struct file *file, struct file *file_src,
0721                     u64 off, u64 olen, u64 destoff)
0722 {
0723     struct inode *inode = file_inode(file);
0724     struct inode *src = file_inode(file_src);
0725     struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
0726     int ret;
0727     int wb_ret;
0728     u64 len = olen;
0729     u64 bs = fs_info->sb->s_blocksize;
0730 
0731     /*
0732      * VFS's generic_remap_file_range_prep() protects us from cloning the
0733      * eof block into the middle of a file, which would result in corruption
0734      * if the file size is not blocksize aligned. So we don't need to check
0735      * for that case here.
0736      */
0737     if (off + len == src->i_size)
0738         len = ALIGN(src->i_size, bs) - off;
0739 
0740     if (destoff > inode->i_size) {
0741         const u64 wb_start = ALIGN_DOWN(inode->i_size, bs);
0742 
0743         ret = btrfs_cont_expand(BTRFS_I(inode), inode->i_size, destoff);
0744         if (ret)
0745             return ret;
0746         /*
0747          * We may have truncated the last block if the inode's size is
0748          * not sector size aligned, so we need to wait for writeback to
0749          * complete before proceeding further, otherwise we can race
0750          * with cloning and attempt to increment a reference to an
0751          * extent that no longer exists (writeback completed right after
0752          * we found the previous extent covering eof and before we
0753          * attempted to increment its reference count).
0754          */
0755         ret = btrfs_wait_ordered_range(inode, wb_start,
0756                            destoff - wb_start);
0757         if (ret)
0758             return ret;
0759     }
0760 
0761     /*
0762      * Lock destination range to serialize with concurrent readahead() and
0763      * source range to serialize with relocation.
0764      */
0765     btrfs_double_extent_lock(src, off, inode, destoff, len);
0766     ret = btrfs_clone(src, inode, off, olen, len, destoff, 0);
0767     btrfs_double_extent_unlock(src, off, inode, destoff, len);
0768 
0769     /*
0770      * We may have copied an inline extent into a page of the destination
0771      * range, so wait for writeback to complete before truncating pages
0772      * from the page cache. This is a rare case.
0773      */
0774     wb_ret = btrfs_wait_ordered_range(inode, destoff, len);
0775     ret = ret ? ret : wb_ret;
0776     /*
0777      * Truncate page cache pages so that future reads will see the cloned
0778      * data immediately and not the previous data.
0779      */
0780     truncate_inode_pages_range(&inode->i_data,
0781                 round_down(destoff, PAGE_SIZE),
0782                 round_up(destoff + len, PAGE_SIZE) - 1);
0783 
0784     btrfs_btree_balance_dirty(fs_info);
0785 
0786     return ret;
0787 }
0788 
0789 static int btrfs_remap_file_range_prep(struct file *file_in, loff_t pos_in,
0790                        struct file *file_out, loff_t pos_out,
0791                        loff_t *len, unsigned int remap_flags)
0792 {
0793     struct inode *inode_in = file_inode(file_in);
0794     struct inode *inode_out = file_inode(file_out);
0795     u64 bs = BTRFS_I(inode_out)->root->fs_info->sb->s_blocksize;
0796     u64 wb_len;
0797     int ret;
0798 
0799     if (!(remap_flags & REMAP_FILE_DEDUP)) {
0800         struct btrfs_root *root_out = BTRFS_I(inode_out)->root;
0801 
0802         if (btrfs_root_readonly(root_out))
0803             return -EROFS;
0804 
0805         ASSERT(inode_in->i_sb == inode_out->i_sb);
0806     }
0807 
0808     /* Don't make the dst file partly checksummed */
0809     if ((BTRFS_I(inode_in)->flags & BTRFS_INODE_NODATASUM) !=
0810         (BTRFS_I(inode_out)->flags & BTRFS_INODE_NODATASUM)) {
0811         return -EINVAL;
0812     }
0813 
0814     /*
0815      * Now that the inodes are locked, we need to start writeback ourselves
0816      * and can not rely on the writeback from the VFS's generic helper
0817      * generic_remap_file_range_prep() because:
0818      *
0819      * 1) For compression we must call filemap_fdatawrite_range() range
0820      *    twice (btrfs_fdatawrite_range() does it for us), and the generic
0821      *    helper only calls it once;
0822      *
0823      * 2) filemap_fdatawrite_range(), called by the generic helper only
0824      *    waits for the writeback to complete, i.e. for IO to be done, and
0825      *    not for the ordered extents to complete. We need to wait for them
0826      *    to complete so that new file extent items are in the fs tree.
0827      */
0828     if (*len == 0 && !(remap_flags & REMAP_FILE_DEDUP))
0829         wb_len = ALIGN(inode_in->i_size, bs) - ALIGN_DOWN(pos_in, bs);
0830     else
0831         wb_len = ALIGN(*len, bs);
0832 
0833     /*
0834      * Workaround to make sure NOCOW buffered write reach disk as NOCOW.
0835      *
0836      * Btrfs' back references do not have a block level granularity, they
0837      * work at the whole extent level.
0838      * NOCOW buffered write without data space reserved may not be able
0839      * to fall back to CoW due to lack of data space, thus could cause
0840      * data loss.
0841      *
0842      * Here we take a shortcut by flushing the whole inode, so that all
0843      * nocow write should reach disk as nocow before we increase the
0844      * reference of the extent. We could do better by only flushing NOCOW
0845      * data, but that needs extra accounting.
0846      *
0847      * Also we don't need to check ASYNC_EXTENT, as async extent will be
0848      * CoWed anyway, not affecting nocow part.
0849      */
0850     ret = filemap_flush(inode_in->i_mapping);
0851     if (ret < 0)
0852         return ret;
0853 
0854     ret = btrfs_wait_ordered_range(inode_in, ALIGN_DOWN(pos_in, bs),
0855                        wb_len);
0856     if (ret < 0)
0857         return ret;
0858     ret = btrfs_wait_ordered_range(inode_out, ALIGN_DOWN(pos_out, bs),
0859                        wb_len);
0860     if (ret < 0)
0861         return ret;
0862 
0863     return generic_remap_file_range_prep(file_in, pos_in, file_out, pos_out,
0864                         len, remap_flags);
0865 }
0866 
0867 static bool file_sync_write(const struct file *file)
0868 {
0869     if (file->f_flags & (__O_SYNC | O_DSYNC))
0870         return true;
0871     if (IS_SYNC(file_inode(file)))
0872         return true;
0873 
0874     return false;
0875 }
0876 
0877 loff_t btrfs_remap_file_range(struct file *src_file, loff_t off,
0878         struct file *dst_file, loff_t destoff, loff_t len,
0879         unsigned int remap_flags)
0880 {
0881     struct inode *src_inode = file_inode(src_file);
0882     struct inode *dst_inode = file_inode(dst_file);
0883     bool same_inode = dst_inode == src_inode;
0884     int ret;
0885 
0886     if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY))
0887         return -EINVAL;
0888 
0889     if (same_inode) {
0890         btrfs_inode_lock(src_inode, BTRFS_ILOCK_MMAP);
0891     } else {
0892         lock_two_nondirectories(src_inode, dst_inode);
0893         btrfs_double_mmap_lock(src_inode, dst_inode);
0894     }
0895 
0896     ret = btrfs_remap_file_range_prep(src_file, off, dst_file, destoff,
0897                       &len, remap_flags);
0898     if (ret < 0 || len == 0)
0899         goto out_unlock;
0900 
0901     if (remap_flags & REMAP_FILE_DEDUP)
0902         ret = btrfs_extent_same(src_inode, off, len, dst_inode, destoff);
0903     else
0904         ret = btrfs_clone_files(dst_file, src_file, off, len, destoff);
0905 
0906 out_unlock:
0907     if (same_inode) {
0908         btrfs_inode_unlock(src_inode, BTRFS_ILOCK_MMAP);
0909     } else {
0910         btrfs_double_mmap_unlock(src_inode, dst_inode);
0911         unlock_two_nondirectories(src_inode, dst_inode);
0912     }
0913 
0914     /*
0915      * If either the source or the destination file was opened with O_SYNC,
0916      * O_DSYNC or has the S_SYNC attribute, fsync both the destination and
0917      * source files/ranges, so that after a successful return (0) followed
0918      * by a power failure results in the reflinked data to be readable from
0919      * both files/ranges.
0920      */
0921     if (ret == 0 && len > 0 &&
0922         (file_sync_write(src_file) || file_sync_write(dst_file))) {
0923         ret = btrfs_sync_file(src_file, off, off + len - 1, 0);
0924         if (ret == 0)
0925             ret = btrfs_sync_file(dst_file, destoff,
0926                           destoff + len - 1, 0);
0927     }
0928 
0929     return ret < 0 ? ret : len;
0930 }