xfs_reflink.c (9b031c86506cef9acae45e61339fcf9deaabb793) xfs_reflink.c (b63da6c8dfa9b2ab3554e8c59ef294d1f28bb9bd)
1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2016 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
5 */
6#include "xfs.h"
7#include "xfs_fs.h"
8#include "xfs_shared.h"

--- 165 unchanged lines hidden (view full) ---

174 xfs_agnumber_t agno;
175 xfs_agblock_t agbno;
176 xfs_extlen_t aglen;
177 xfs_agblock_t fbno;
178 xfs_extlen_t flen;
179 int error = 0;
180
181 /* Holes, unwritten, and delalloc extents cannot be shared */
1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2016 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
5 */
6#include "xfs.h"
7#include "xfs_fs.h"
8#include "xfs_shared.h"

--- 165 unchanged lines hidden (view full) ---

174 xfs_agnumber_t agno;
175 xfs_agblock_t agbno;
176 xfs_extlen_t aglen;
177 xfs_agblock_t fbno;
178 xfs_extlen_t flen;
179 int error = 0;
180
181 /* Holes, unwritten, and delalloc extents cannot be shared */
182 if (!xfs_is_cow_inode(ip) || !xfs_bmap_is_real_extent(irec)) {
182 if (!xfs_is_cow_inode(ip) || !xfs_bmap_is_written_extent(irec)) {
183 *shared = false;
184 return 0;
185 }
186
187 trace_xfs_reflink_trim_around_shared(ip, irec);
188
189 agno = XFS_FSB_TO_AGNO(ip->i_mount, irec->br_startblock);
190 agbno = XFS_FSB_TO_AGBNO(ip->i_mount, irec->br_startblock);

--- 459 unchanged lines hidden (view full) ---

650
651 ASSERT(del.br_blockcount > 0);
652
653 /*
654 * Only remap real extents that contain data. With AIO, speculative
655 * preallocations can leak into the range we are called upon, and we
656 * need to skip them.
657 */
183 *shared = false;
184 return 0;
185 }
186
187 trace_xfs_reflink_trim_around_shared(ip, irec);
188
189 agno = XFS_FSB_TO_AGNO(ip->i_mount, irec->br_startblock);
190 agbno = XFS_FSB_TO_AGBNO(ip->i_mount, irec->br_startblock);

--- 459 unchanged lines hidden (view full) ---

650
651 ASSERT(del.br_blockcount > 0);
652
653 /*
654 * Only remap real extents that contain data. With AIO, speculative
655 * preallocations can leak into the range we are called upon, and we
656 * need to skip them.
657 */
658 if (!xfs_bmap_is_real_extent(&got)) {
658 if (!xfs_bmap_is_written_extent(&got)) {
659 *end_fsb = del.br_startoff;
660 goto out_cancel;
661 }
662
663 /* Unmap the old blocks in the data fork. */
664 rlen = del.br_blockcount;
665 error = __xfs_bunmapi(tp, ip, del.br_startoff, &rlen, 0, 1);
666 if (error)

--- 49 unchanged lines hidden (view full) ---

716 offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
717 end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
718
719 /*
720 * Walk backwards until we're out of the I/O range. The loop function
721 * repeatedly cycles the ILOCK to allocate one transaction per remapped
722 * extent.
723 *
659 *end_fsb = del.br_startoff;
660 goto out_cancel;
661 }
662
663 /* Unmap the old blocks in the data fork. */
664 rlen = del.br_blockcount;
665 error = __xfs_bunmapi(tp, ip, del.br_startoff, &rlen, 0, 1);
666 if (error)

--- 49 unchanged lines hidden (view full) ---

716 offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
717 end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
718
719 /*
720 * Walk backwards until we're out of the I/O range. The loop function
721 * repeatedly cycles the ILOCK to allocate one transaction per remapped
722 * extent.
723 *
724 * If we're being called by writeback then the the pages will still
724 * If we're being called by writeback then the pages will still
725 * have PageWriteback set, which prevents races with reflink remapping
726 * and truncate. Reflink remapping prevents races with writeback by
727 * taking the iolock and mmaplock before flushing the pages and
728 * remapping, which means there won't be any further writeback or page
729 * cache dirtying until the reflink completes.
730 *
731 * We should never have two threads issuing writeback for the same file
732 * region. There are also have post-eof checks in the writeback

--- 246 unchanged lines hidden (view full) ---

979 if (xfs_ag_resv_critical(pag, XFS_AG_RESV_RMAPBT) ||
980 xfs_ag_resv_critical(pag, XFS_AG_RESV_METADATA))
981 error = -ENOSPC;
982 xfs_perag_put(pag);
983 return error;
984}
985
986/*
725 * have PageWriteback set, which prevents races with reflink remapping
726 * and truncate. Reflink remapping prevents races with writeback by
727 * taking the iolock and mmaplock before flushing the pages and
728 * remapping, which means there won't be any further writeback or page
729 * cache dirtying until the reflink completes.
730 *
731 * We should never have two threads issuing writeback for the same file
732 * region. There are also have post-eof checks in the writeback

--- 246 unchanged lines hidden (view full) ---

979 if (xfs_ag_resv_critical(pag, XFS_AG_RESV_RMAPBT) ||
980 xfs_ag_resv_critical(pag, XFS_AG_RESV_METADATA))
981 error = -ENOSPC;
982 xfs_perag_put(pag);
983 return error;
984}
985
986/*
987 * Unmap a range of blocks from a file, then map other blocks into the hole.
988 * The range to unmap is (destoff : destoff + srcioff + irec->br_blockcount).
989 * The extent irec is mapped into dest at irec->br_startoff.
987 * Remap the given extent into the file. The dmap blockcount will be set to
988 * the number of blocks that were actually remapped.
990 */
991STATIC int
992xfs_reflink_remap_extent(
993 struct xfs_inode *ip,
989 */
990STATIC int
991xfs_reflink_remap_extent(
992 struct xfs_inode *ip,
994 struct xfs_bmbt_irec *irec,
995 xfs_fileoff_t destoff,
993 struct xfs_bmbt_irec *dmap,
996 xfs_off_t new_isize)
997{
994 xfs_off_t new_isize)
995{
996 struct xfs_bmbt_irec smap;
998 struct xfs_mount *mp = ip->i_mount;
997 struct xfs_mount *mp = ip->i_mount;
999 bool real_extent = xfs_bmap_is_real_extent(irec);
1000 struct xfs_trans *tp;
998 struct xfs_trans *tp;
1001 unsigned int resblks;
1002 struct xfs_bmbt_irec uirec;
1003 xfs_filblks_t rlen;
1004 xfs_filblks_t unmap_len;
1005 xfs_off_t newlen;
999 xfs_off_t newlen;
1000 int64_t qres, qdelta;
1001 unsigned int resblks;
1002 bool smap_real;
1003 bool dmap_written = xfs_bmap_is_written_extent(dmap);
1004 int nimaps;
1006 int error;
1007
1005 int error;
1006
1008 unmap_len = irec->br_startoff + irec->br_blockcount - destoff;
1009 trace_xfs_reflink_punch_range(ip, destoff, unmap_len);
1010
1011 /* No reflinking if we're low on space */
1012 if (real_extent) {
1013 error = xfs_reflink_ag_has_free_space(mp,
1014 XFS_FSB_TO_AGNO(mp, irec->br_startblock));
1015 if (error)
1016 goto out;
1017 }
1018
1019 /* Start a rolling transaction to switch the mappings */
1007 /* Start a rolling transaction to switch the mappings */
1020 resblks = XFS_EXTENTADD_SPACE_RES(ip->i_mount, XFS_DATA_FORK);
1008 resblks = XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK);
1021 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp);
1022 if (error)
1023 goto out;
1024
1025 xfs_ilock(ip, XFS_ILOCK_EXCL);
1026 xfs_trans_ijoin(tp, ip, 0);
1027
1009 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp);
1010 if (error)
1011 goto out;
1012
1013 xfs_ilock(ip, XFS_ILOCK_EXCL);
1014 xfs_trans_ijoin(tp, ip, 0);
1015
1028 /* If we're not just clearing space, then do we have enough quota? */
1029 if (real_extent) {
1030 error = xfs_trans_reserve_quota_nblks(tp, ip,
1031 irec->br_blockcount, 0, XFS_QMOPT_RES_REGBLKS);
1016 /*
1017 * Read what's currently mapped in the destination file into smap.
1018 * If smap isn't a hole, we will have to remove it before we can add
1019 * dmap to the destination file.
1020 */
1021 nimaps = 1;
1022 error = xfs_bmapi_read(ip, dmap->br_startoff, dmap->br_blockcount,
1023 &smap, &nimaps, 0);
1024 if (error)
1025 goto out_cancel;
1026 ASSERT(nimaps == 1 && smap.br_startoff == dmap->br_startoff);
1027 smap_real = xfs_bmap_is_real_extent(&smap);
1028
1029 /*
1030 * We can only remap as many blocks as the smaller of the two extent
1031 * maps, because we can only remap one extent at a time.
1032 */
1033 dmap->br_blockcount = min(dmap->br_blockcount, smap.br_blockcount);
1034 ASSERT(dmap->br_blockcount == smap.br_blockcount);
1035
1036 trace_xfs_reflink_remap_extent_dest(ip, &smap);
1037
1038 /*
1039 * Two extents mapped to the same physical block must not have
1040 * different states; that's filesystem corruption. Move on to the next
1041 * extent if they're both holes or both the same physical extent.
1042 */
1043 if (dmap->br_startblock == smap.br_startblock) {
1044 if (dmap->br_state != smap.br_state)
1045 error = -EFSCORRUPTED;
1046 goto out_cancel;
1047 }
1048
1049 /* If both extents are unwritten, leave them alone. */
1050 if (dmap->br_state == XFS_EXT_UNWRITTEN &&
1051 smap.br_state == XFS_EXT_UNWRITTEN)
1052 goto out_cancel;
1053
1054 /* No reflinking if the AG of the dest mapping is low on space. */
1055 if (dmap_written) {
1056 error = xfs_reflink_ag_has_free_space(mp,
1057 XFS_FSB_TO_AGNO(mp, dmap->br_startblock));
1032 if (error)
1033 goto out_cancel;
1034 }
1035
1058 if (error)
1059 goto out_cancel;
1060 }
1061
1036 trace_xfs_reflink_remap(ip, irec->br_startoff,
1037 irec->br_blockcount, irec->br_startblock);
1038
1039 /* Unmap the old blocks in the data fork. */
1040 rlen = unmap_len;
1041 while (rlen) {
1042 ASSERT(tp->t_firstblock == NULLFSBLOCK);
1043 error = __xfs_bunmapi(tp, ip, destoff, &rlen, 0, 1);
1062 /*
1063 * Compute quota reservation if we think the quota block counter for
1064 * this file could increase.
1065 *
1066 * Adding a written extent to the extent map can cause a bmbt split,
1067 * and removing a mapped extent from the extent can cause a bmbt split.
1068 * The two operations cannot both cause a split since they operate on
1069 * the same index in the bmap btree, so we only need a reservation for
1070 * one bmbt split if either thing is happening.
1071 *
1072 * If we are mapping a written extent into the file, we need to have
1073 * enough quota block count reservation to handle the blocks in that
1074 * extent. We log only the delta to the quota block counts, so if the
1075 * extent we're unmapping also has blocks allocated to it, we don't
1076 * need a quota reservation for the extent itself.
1077 *
1078 * Note that if we're replacing a delalloc reservation with a written
1079 * extent, we have to take the full quota reservation because removing
1080 * the delalloc reservation gives the block count back to the quota
1081 * count. This is suboptimal, but the VFS flushed the dest range
1082 * before we started. That should have removed all the delalloc
1083 * reservations, but we code defensively.
1084 */
1085 qres = qdelta = 0;
1086 if (smap_real || dmap_written)
1087 qres = XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK);
1088 if (!smap_real && dmap_written)
1089 qres += dmap->br_blockcount;
1090 if (qres > 0) {
1091 error = xfs_trans_reserve_quota_nblks(tp, ip, qres, 0,
1092 XFS_QMOPT_RES_REGBLKS);
1044 if (error)
1045 goto out_cancel;
1093 if (error)
1094 goto out_cancel;
1095 }
1046
1096
1097 if (smap_real) {
1047 /*
1098 /*
1048 * Trim the extent to whatever got unmapped.
1049 * Remember, bunmapi works backwards.
1099 * If the extent we're unmapping is backed by storage (written
1100 * or not), unmap the extent and drop its refcount.
1050 */
1101 */
1051 uirec.br_startblock = irec->br_startblock + rlen;
1052 uirec.br_startoff = irec->br_startoff + rlen;
1053 uirec.br_blockcount = unmap_len - rlen;
1054 uirec.br_state = irec->br_state;
1055 unmap_len = rlen;
1102 xfs_bmap_unmap_extent(tp, ip, &smap);
1103 xfs_refcount_decrease_extent(tp, &smap);
1104 qdelta -= smap.br_blockcount;
1105 } else if (smap.br_startblock == DELAYSTARTBLOCK) {
1106 xfs_filblks_t len = smap.br_blockcount;
1056
1107
1057 /* If this isn't a real mapping, we're done. */
1058 if (!real_extent || uirec.br_blockcount == 0)
1059 goto next_extent;
1108 /*
1109 * If the extent we're unmapping is a delalloc reservation,
1110 * we can use the regular bunmapi function to release the
1111 * incore state. Dropping the delalloc reservation takes care
1112 * of the quota reservation for us.
1113 */
1114 error = __xfs_bunmapi(NULL, ip, smap.br_startoff, &len, 0, 1);
1115 if (error)
1116 goto out_cancel;
1117 ASSERT(len == 0);
1118 }
1060
1119
1061 trace_xfs_reflink_remap(ip, uirec.br_startoff,
1062 uirec.br_blockcount, uirec.br_startblock);
1120 /*
1121 * If the extent we're sharing is backed by written storage, increase
1122 * its refcount and map it into the file.
1123 */
1124 if (dmap_written) {
1125 xfs_refcount_increase_extent(tp, dmap);
1126 xfs_bmap_map_extent(tp, ip, dmap);
1127 qdelta += dmap->br_blockcount;
1128 }
1063
1129
1064 /* Update the refcount tree */
1065 xfs_refcount_increase_extent(tp, &uirec);
1130 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, qdelta);
1066
1131
1067 /* Map the new blocks into the data fork. */
1068 xfs_bmap_map_extent(tp, ip, &uirec);
1069
1070 /* Update quota accounting. */
1071 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT,
1072 uirec.br_blockcount);
1073
1074 /* Update dest isize if needed. */
1075 newlen = XFS_FSB_TO_B(mp,
1076 uirec.br_startoff + uirec.br_blockcount);
1077 newlen = min_t(xfs_off_t, newlen, new_isize);
1078 if (newlen > i_size_read(VFS_I(ip))) {
1079 trace_xfs_reflink_update_inode_size(ip, newlen);
1080 i_size_write(VFS_I(ip), newlen);
1081 ip->i_d.di_size = newlen;
1082 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1083 }
1084
1085next_extent:
1086 /* Process all the deferred stuff. */
1087 error = xfs_defer_finish(&tp);
1088 if (error)
1089 goto out_cancel;
1132 /* Update dest isize if needed. */
1133 newlen = XFS_FSB_TO_B(mp, dmap->br_startoff + dmap->br_blockcount);
1134 newlen = min_t(xfs_off_t, newlen, new_isize);
1135 if (newlen > i_size_read(VFS_I(ip))) {
1136 trace_xfs_reflink_update_inode_size(ip, newlen);
1137 i_size_write(VFS_I(ip), newlen);
1138 ip->i_d.di_size = newlen;
1139 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1090 }
1091
1140 }
1141
1142 /* Commit everything and unlock. */
1092 error = xfs_trans_commit(tp);
1143 error = xfs_trans_commit(tp);
1093 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1094 if (error)
1095 goto out;
1096 return 0;
1144 goto out_unlock;
1097
1098out_cancel:
1099 xfs_trans_cancel(tp);
1145
1146out_cancel:
1147 xfs_trans_cancel(tp);
1148out_unlock:
1100 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1101out:
1149 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1150out:
1102 trace_xfs_reflink_remap_extent_error(ip, error, _RET_IP_);
1151 if (error)
1152 trace_xfs_reflink_remap_extent_error(ip, error, _RET_IP_);
1103 return error;
1104}
1105
1153 return error;
1154}
1155
1106/*
1107 * Iteratively remap one file's extents (and holes) to another's.
1108 */
1156/* Remap a range of one file to the other. */
1109int
1110xfs_reflink_remap_blocks(
1111 struct xfs_inode *src,
1112 loff_t pos_in,
1113 struct xfs_inode *dest,
1114 loff_t pos_out,
1115 loff_t remap_len,
1116 loff_t *remapped)
1117{
1118 struct xfs_bmbt_irec imap;
1157int
1158xfs_reflink_remap_blocks(
1159 struct xfs_inode *src,
1160 loff_t pos_in,
1161 struct xfs_inode *dest,
1162 loff_t pos_out,
1163 loff_t remap_len,
1164 loff_t *remapped)
1165{
1166 struct xfs_bmbt_irec imap;
1119 xfs_fileoff_t srcoff;
1120 xfs_fileoff_t destoff;
1167 struct xfs_mount *mp = src->i_mount;
1168 xfs_fileoff_t srcoff = XFS_B_TO_FSBT(mp, pos_in);
1169 xfs_fileoff_t destoff = XFS_B_TO_FSBT(mp, pos_out);
1121 xfs_filblks_t len;
1170 xfs_filblks_t len;
1122 xfs_filblks_t range_len;
1123 xfs_filblks_t remapped_len = 0;
1124 xfs_off_t new_isize = pos_out + remap_len;
1125 int nimaps;
1126 int error = 0;
1127
1171 xfs_filblks_t remapped_len = 0;
1172 xfs_off_t new_isize = pos_out + remap_len;
1173 int nimaps;
1174 int error = 0;
1175
1128 destoff = XFS_B_TO_FSBT(src->i_mount, pos_out);
1129 srcoff = XFS_B_TO_FSBT(src->i_mount, pos_in);
1130 len = XFS_B_TO_FSB(src->i_mount, remap_len);
1176 len = min_t(xfs_filblks_t, XFS_B_TO_FSB(mp, remap_len),
1177 XFS_MAX_FILEOFF);
1131
1178
1132 /* drange = (destoff, destoff + len); srange = (srcoff, srcoff + len) */
1133 while (len) {
1134 uint lock_mode;
1179 trace_xfs_reflink_remap_blocks(src, srcoff, len, dest, destoff);
1135
1180
1136 trace_xfs_reflink_remap_blocks_loop(src, srcoff, len,
1137 dest, destoff);
1181 while (len > 0) {
1182 unsigned int lock_mode;
1138
1139 /* Read extent from the source file */
1140 nimaps = 1;
1141 lock_mode = xfs_ilock_data_map_shared(src);
1142 error = xfs_bmapi_read(src, srcoff, len, &imap, &nimaps, 0);
1143 xfs_iunlock(src, lock_mode);
1144 if (error)
1145 break;
1183
1184 /* Read extent from the source file */
1185 nimaps = 1;
1186 lock_mode = xfs_ilock_data_map_shared(src);
1187 error = xfs_bmapi_read(src, srcoff, len, &imap, &nimaps, 0);
1188 xfs_iunlock(src, lock_mode);
1189 if (error)
1190 break;
1146 ASSERT(nimaps == 1);
1191 /*
1192 * The caller supposedly flushed all dirty pages in the source
1193 * file range, which means that writeback should have allocated
1194 * or deleted all delalloc reservations in that range. If we
1195 * find one, that's a good sign that something is seriously
1196 * wrong here.
1197 */
1198 ASSERT(nimaps == 1 && imap.br_startoff == srcoff);
1199 if (imap.br_startblock == DELAYSTARTBLOCK) {
1200 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
1201 error = -EFSCORRUPTED;
1202 break;
1203 }
1147
1204
1148 trace_xfs_reflink_remap_imap(src, srcoff, len, XFS_DATA_FORK,
1149 &imap);
1205 trace_xfs_reflink_remap_extent_src(src, &imap);
1150
1206
1151 /* Translate imap into the destination file. */
1152 range_len = imap.br_startoff + imap.br_blockcount - srcoff;
1153 imap.br_startoff += destoff - srcoff;
1154
1155 /* Clear dest from destoff to the end of imap and map it in. */
1156 error = xfs_reflink_remap_extent(dest, &imap, destoff,
1157 new_isize);
1207 /* Remap into the destination file at the given offset. */
1208 imap.br_startoff = destoff;
1209 error = xfs_reflink_remap_extent(dest, &imap, new_isize);
1158 if (error)
1159 break;
1160
1161 if (fatal_signal_pending(current)) {
1162 error = -EINTR;
1163 break;
1164 }
1165
1166 /* Advance drange/srange */
1210 if (error)
1211 break;
1212
1213 if (fatal_signal_pending(current)) {
1214 error = -EINTR;
1215 break;
1216 }
1217
1218 /* Advance drange/srange */
1167 srcoff += range_len;
1168 destoff += range_len;
1169 len -= range_len;
1170 remapped_len += range_len;
1219 srcoff += imap.br_blockcount;
1220 destoff += imap.br_blockcount;
1221 len -= imap.br_blockcount;
1222 remapped_len += imap.br_blockcount;
1171 }
1172
1173 if (error)
1174 trace_xfs_reflink_remap_blocks_error(dest, error, _RET_IP_);
1175 *remapped = min_t(loff_t, remap_len,
1176 XFS_FSB_TO_B(src->i_mount, remapped_len));
1177 return error;
1178}
1179
1180/*
1223 }
1224
1225 if (error)
1226 trace_xfs_reflink_remap_blocks_error(dest, error, _RET_IP_);
1227 *remapped = min_t(loff_t, remap_len,
1228 XFS_FSB_TO_B(src->i_mount, remapped_len));
1229 return error;
1230}
1231
1232/*
1181 * Grab the exclusive iolock for a data copy from src to dest, making sure to
1182 * abide vfs locking order (lowest pointer value goes first) and breaking the
1183 * layout leases before proceeding. The loop is needed because we cannot call
1184 * the blocking break_layout() with the iolocks held, and therefore have to
1185 * back out both locks.
1186 */
1187static int
1188xfs_iolock_two_inodes_and_break_layout(
1189 struct inode *src,
1190 struct inode *dest)
1191{
1192 int error;
1193
1194 if (src > dest)
1195 swap(src, dest);
1196
1197retry:
1198 /* Wait to break both inodes' layouts before we start locking. */
1199 error = break_layout(src, true);
1200 if (error)
1201 return error;
1202 if (src != dest) {
1203 error = break_layout(dest, true);
1204 if (error)
1205 return error;
1206 }
1207
1208 /* Lock one inode and make sure nobody got in and leased it. */
1209 inode_lock(src);
1210 error = break_layout(src, false);
1211 if (error) {
1212 inode_unlock(src);
1213 if (error == -EWOULDBLOCK)
1214 goto retry;
1215 return error;
1216 }
1217
1218 if (src == dest)
1219 return 0;
1220
1221 /* Lock the other inode and make sure nobody got in and leased it. */
1222 inode_lock_nested(dest, I_MUTEX_NONDIR2);
1223 error = break_layout(dest, false);
1224 if (error) {
1225 inode_unlock(src);
1226 inode_unlock(dest);
1227 if (error == -EWOULDBLOCK)
1228 goto retry;
1229 return error;
1230 }
1231
1232 return 0;
1233}
1234
1235/* Unlock both inodes after they've been prepped for a range clone. */
1236void
1237xfs_reflink_remap_unlock(
1238 struct file *file_in,
1239 struct file *file_out)
1240{
1241 struct inode *inode_in = file_inode(file_in);
1242 struct xfs_inode *src = XFS_I(inode_in);
1243 struct inode *inode_out = file_inode(file_out);
1244 struct xfs_inode *dest = XFS_I(inode_out);
1245 bool same_inode = (inode_in == inode_out);
1246
1247 xfs_iunlock(dest, XFS_MMAPLOCK_EXCL);
1248 if (!same_inode)
1249 xfs_iunlock(src, XFS_MMAPLOCK_EXCL);
1250 inode_unlock(inode_out);
1251 if (!same_inode)
1252 inode_unlock(inode_in);
1253}
1254
1255/*
1256 * If we're reflinking to a point past the destination file's EOF, we must
1257 * zero any speculative post-EOF preallocations that sit between the old EOF
1258 * and the destination file offset.
1259 */
1260static int
1261xfs_reflink_zero_posteof(
1262 struct xfs_inode *ip,
1263 loff_t pos)

--- 45 unchanged lines hidden (view full) ---

1309 loff_t pos_out,
1310 loff_t *len,
1311 unsigned int remap_flags)
1312{
1313 struct inode *inode_in = file_inode(file_in);
1314 struct xfs_inode *src = XFS_I(inode_in);
1315 struct inode *inode_out = file_inode(file_out);
1316 struct xfs_inode *dest = XFS_I(inode_out);
1233 * If we're reflinking to a point past the destination file's EOF, we must
1234 * zero any speculative post-EOF preallocations that sit between the old EOF
1235 * and the destination file offset.
1236 */
1237static int
1238xfs_reflink_zero_posteof(
1239 struct xfs_inode *ip,
1240 loff_t pos)

--- 45 unchanged lines hidden (view full) ---

1286 loff_t pos_out,
1287 loff_t *len,
1288 unsigned int remap_flags)
1289{
1290 struct inode *inode_in = file_inode(file_in);
1291 struct xfs_inode *src = XFS_I(inode_in);
1292 struct inode *inode_out = file_inode(file_out);
1293 struct xfs_inode *dest = XFS_I(inode_out);
1317 bool same_inode = (inode_in == inode_out);
1318 ssize_t ret;
1294 int ret;
1319
1320 /* Lock both files against IO */
1295
1296 /* Lock both files against IO */
1321 ret = xfs_iolock_two_inodes_and_break_layout(inode_in, inode_out);
1297 ret = xfs_ilock2_io_mmap(src, dest);
1322 if (ret)
1323 return ret;
1298 if (ret)
1299 return ret;
1324 if (same_inode)
1325 xfs_ilock(src, XFS_MMAPLOCK_EXCL);
1326 else
1327 xfs_lock_two_inodes(src, XFS_MMAPLOCK_EXCL, dest,
1328 XFS_MMAPLOCK_EXCL);
1329
1330 /* Check file eligibility and prepare for block sharing. */
1331 ret = -EINVAL;
1332 /* Don't reflink realtime inodes */
1333 if (XFS_IS_REALTIME_INODE(src) || XFS_IS_REALTIME_INODE(dest))
1334 goto out_unlock;
1335
1336 /* Don't share DAX file data for now. */
1337 if (IS_DAX(inode_in) || IS_DAX(inode_out))
1338 goto out_unlock;
1339
1340 ret = generic_remap_file_range_prep(file_in, pos_in, file_out, pos_out,
1341 len, remap_flags);
1300
1301 /* Check file eligibility and prepare for block sharing. */
1302 ret = -EINVAL;
1303 /* Don't reflink realtime inodes */
1304 if (XFS_IS_REALTIME_INODE(src) || XFS_IS_REALTIME_INODE(dest))
1305 goto out_unlock;
1306
1307 /* Don't share DAX file data for now. */
1308 if (IS_DAX(inode_in) || IS_DAX(inode_out))
1309 goto out_unlock;
1310
1311 ret = generic_remap_file_range_prep(file_in, pos_in, file_out, pos_out,
1312 len, remap_flags);
1342 if (ret < 0 || *len == 0)
1313 if (ret || *len == 0)
1343 goto out_unlock;
1344
1345 /* Attach dquots to dest inode before changing block map */
1346 ret = xfs_qm_dqattach(dest);
1347 if (ret)
1348 goto out_unlock;
1349
1350 /*

--- 18 unchanged lines hidden (view full) ---

1369 loff_t flen = *len + (pos_out - XFS_ISIZE(dest));
1370 ret = xfs_flush_unmap_range(dest, XFS_ISIZE(dest), flen);
1371 } else {
1372 ret = xfs_flush_unmap_range(dest, pos_out, *len);
1373 }
1374 if (ret)
1375 goto out_unlock;
1376
1314 goto out_unlock;
1315
1316 /* Attach dquots to dest inode before changing block map */
1317 ret = xfs_qm_dqattach(dest);
1318 if (ret)
1319 goto out_unlock;
1320
1321 /*

--- 18 unchanged lines hidden (view full) ---

1340 loff_t flen = *len + (pos_out - XFS_ISIZE(dest));
1341 ret = xfs_flush_unmap_range(dest, XFS_ISIZE(dest), flen);
1342 } else {
1343 ret = xfs_flush_unmap_range(dest, pos_out, *len);
1344 }
1345 if (ret)
1346 goto out_unlock;
1347
1377 return 1;
1348 return 0;
1378out_unlock:
1349out_unlock:
1379 xfs_reflink_remap_unlock(file_in, file_out);
1350 xfs_iunlock2_io_mmap(src, dest);
1380 return ret;
1381}
1382
1383/* Does this inode need the reflink flag? */
1384int
1385xfs_reflink_inode_has_shared_extents(
1386 struct xfs_trans *tp,
1387 struct xfs_inode *ip,

--- 160 unchanged lines hidden ---
1351 return ret;
1352}
1353
1354/* Does this inode need the reflink flag? */
1355int
1356xfs_reflink_inode_has_shared_extents(
1357 struct xfs_trans *tp,
1358 struct xfs_inode *ip,

--- 160 unchanged lines hidden ---