blob: d3c3d46245b296b822faa7a267f2d43ddaf785ba [file] [log] [blame]
Dave Chinner68988112013-08-12 20:49:42 +10001/*
2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Dave Chinnerc24b5df2013-08-12 20:49:45 +10003 * Copyright (c) 2012 Red Hat, Inc.
Dave Chinner68988112013-08-12 20:49:42 +10004 * All Rights Reserved.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it would be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19#include "xfs.h"
20#include "xfs_fs.h"
Dave Chinner70a9883c2013-10-23 10:36:05 +110021#include "xfs_shared.h"
Dave Chinner239880e2013-10-23 10:50:10 +110022#include "xfs_format.h"
23#include "xfs_log_format.h"
24#include "xfs_trans_resv.h"
Dave Chinner68988112013-08-12 20:49:42 +100025#include "xfs_bit.h"
Dave Chinner68988112013-08-12 20:49:42 +100026#include "xfs_mount.h"
Dave Chinner57062782013-10-15 09:17:51 +110027#include "xfs_da_format.h"
Darrick J. Wong3ab78df2016-08-03 11:15:38 +100028#include "xfs_defer.h"
Dave Chinner68988112013-08-12 20:49:42 +100029#include "xfs_inode.h"
30#include "xfs_btree.h"
Dave Chinner239880e2013-10-23 10:50:10 +110031#include "xfs_trans.h"
Dave Chinner68988112013-08-12 20:49:42 +100032#include "xfs_extfree_item.h"
33#include "xfs_alloc.h"
34#include "xfs_bmap.h"
35#include "xfs_bmap_util.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110036#include "xfs_bmap_btree.h"
Dave Chinner68988112013-08-12 20:49:42 +100037#include "xfs_rtalloc.h"
38#include "xfs_error.h"
39#include "xfs_quota.h"
40#include "xfs_trans_space.h"
41#include "xfs_trace.h"
Dave Chinnerc24b5df2013-08-12 20:49:45 +100042#include "xfs_icache.h"
Dave Chinner239880e2013-10-23 10:50:10 +110043#include "xfs_log.h"
Dave Chinner68988112013-08-12 20:49:42 +100044
45/* Kernel only BMAP related definitions and functions */
46
47/*
48 * Convert the given file system block to a disk block. We have to treat it
49 * differently based on whether the file is a real time file or not, because the
50 * bmap code does.
51 */
52xfs_daddr_t
53xfs_fsb_to_db(struct xfs_inode *ip, xfs_fsblock_t fsb)
54{
55 return (XFS_IS_REALTIME_INODE(ip) ? \
56 (xfs_daddr_t)XFS_FSB_TO_BB((ip)->i_mount, (fsb)) : \
57 XFS_FSB_TO_DADDR((ip)->i_mount, (fsb)));
58}
59
60/*
Dave Chinner3fbbbea2015-11-03 12:27:22 +110061 * Routine to zero an extent on disk allocated to the specific inode.
62 *
63 * The VFS functions take a linearised filesystem block offset, so we have to
64 * convert the sparse xfs fsb to the right format first.
65 * VFS types are real funky, too.
66 */
67int
68xfs_zero_extent(
69 struct xfs_inode *ip,
70 xfs_fsblock_t start_fsb,
71 xfs_off_t count_fsb)
72{
73 struct xfs_mount *mp = ip->i_mount;
74 xfs_daddr_t sector = xfs_fsb_to_db(ip, start_fsb);
75 sector_t block = XFS_BB_TO_FSBT(mp, sector);
Dave Chinner3fbbbea2015-11-03 12:27:22 +110076
Matthew Wilcox3dc29162016-03-15 11:20:41 -060077 return blkdev_issue_zeroout(xfs_find_bdev_for_inode(VFS_I(ip)),
78 block << (mp->m_super->s_blocksize_bits - 9),
79 count_fsb << (mp->m_super->s_blocksize_bits - 9),
80 GFP_NOFS, true);
Dave Chinner3fbbbea2015-11-03 12:27:22 +110081}
82
Dave Chinner68988112013-08-12 20:49:42 +100083int
84xfs_bmap_rtalloc(
85 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
86{
87 xfs_alloctype_t atype = 0; /* type for allocation routines */
88 int error; /* error return value */
89 xfs_mount_t *mp; /* mount point structure */
90 xfs_extlen_t prod = 0; /* product factor for allocators */
91 xfs_extlen_t ralen = 0; /* realtime allocation length */
92 xfs_extlen_t align; /* minimum allocation alignment */
93 xfs_rtblock_t rtb;
94
95 mp = ap->ip->i_mount;
96 align = xfs_get_extsz_hint(ap->ip);
97 prod = align / mp->m_sb.sb_rextsize;
98 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
99 align, 1, ap->eof, 0,
100 ap->conv, &ap->offset, &ap->length);
101 if (error)
102 return error;
103 ASSERT(ap->length);
104 ASSERT(ap->length % mp->m_sb.sb_rextsize == 0);
105
106 /*
107 * If the offset & length are not perfectly aligned
108 * then kill prod, it will just get us in trouble.
109 */
110 if (do_mod(ap->offset, align) || ap->length % align)
111 prod = 1;
112 /*
113 * Set ralen to be the actual requested length in rtextents.
114 */
115 ralen = ap->length / mp->m_sb.sb_rextsize;
116 /*
117 * If the old value was close enough to MAXEXTLEN that
118 * we rounded up to it, cut it back so it's valid again.
119 * Note that if it's a really large request (bigger than
120 * MAXEXTLEN), we don't hear about that number, and can't
121 * adjust the starting point to match it.
122 */
123 if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
124 ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
125
126 /*
Dave Chinner4b680af2016-02-08 10:46:51 +1100127 * Lock out modifications to both the RT bitmap and summary inodes
Dave Chinner68988112013-08-12 20:49:42 +1000128 */
Darrick J. Wongf4a06602016-08-03 11:00:42 +1000129 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
Dave Chinner68988112013-08-12 20:49:42 +1000130 xfs_trans_ijoin(ap->tp, mp->m_rbmip, XFS_ILOCK_EXCL);
Darrick J. Wongf4a06602016-08-03 11:00:42 +1000131 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
Dave Chinner4b680af2016-02-08 10:46:51 +1100132 xfs_trans_ijoin(ap->tp, mp->m_rsumip, XFS_ILOCK_EXCL);
Dave Chinner68988112013-08-12 20:49:42 +1000133
134 /*
135 * If it's an allocation to an empty file at offset 0,
136 * pick an extent that will space things out in the rt area.
137 */
138 if (ap->eof && ap->offset == 0) {
139 xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */
140
141 error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
142 if (error)
143 return error;
144 ap->blkno = rtx * mp->m_sb.sb_rextsize;
145 } else {
146 ap->blkno = 0;
147 }
148
149 xfs_bmap_adjacent(ap);
150
151 /*
152 * Realtime allocation, done through xfs_rtallocate_extent.
153 */
154 atype = ap->blkno == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO;
155 do_div(ap->blkno, mp->m_sb.sb_rextsize);
156 rtb = ap->blkno;
157 ap->length = ralen;
158 if ((error = xfs_rtallocate_extent(ap->tp, ap->blkno, 1, ap->length,
159 &ralen, atype, ap->wasdel, prod, &rtb)))
160 return error;
161 if (rtb == NULLFSBLOCK && prod > 1 &&
162 (error = xfs_rtallocate_extent(ap->tp, ap->blkno, 1,
163 ap->length, &ralen, atype,
164 ap->wasdel, 1, &rtb)))
165 return error;
166 ap->blkno = rtb;
167 if (ap->blkno != NULLFSBLOCK) {
168 ap->blkno *= mp->m_sb.sb_rextsize;
169 ralen *= mp->m_sb.sb_rextsize;
170 ap->length = ralen;
171 ap->ip->i_d.di_nblocks += ralen;
172 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
173 if (ap->wasdel)
174 ap->ip->i_delayed_blks -= ralen;
175 /*
176 * Adjust the disk quota also. This was reserved
177 * earlier.
178 */
179 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
180 ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
181 XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
Dave Chinner3fbbbea2015-11-03 12:27:22 +1100182
183 /* Zero the extent if we were asked to do so */
184 if (ap->userdata & XFS_ALLOC_USERDATA_ZERO) {
185 error = xfs_zero_extent(ap->ip, ap->blkno, ap->length);
186 if (error)
187 return error;
188 }
Dave Chinner68988112013-08-12 20:49:42 +1000189 } else {
190 ap->length = 0;
191 }
192 return 0;
193}
194
195/*
Dave Chinner68988112013-08-12 20:49:42 +1000196 * Check if the endoff is outside the last extent. If so the caller will grow
197 * the allocation to a stripe unit boundary. All offsets are considered outside
198 * the end of file for an empty fork, so 1 is returned in *eof in that case.
199 */
200int
201xfs_bmap_eof(
202 struct xfs_inode *ip,
203 xfs_fileoff_t endoff,
204 int whichfork,
205 int *eof)
206{
207 struct xfs_bmbt_irec rec;
208 int error;
209
210 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, eof);
211 if (error || *eof)
212 return error;
213
214 *eof = endoff >= rec.br_startoff + rec.br_blockcount;
215 return 0;
216}
217
218/*
219 * Extent tree block counting routines.
220 */
221
222/*
223 * Count leaf blocks given a range of extent records.
224 */
225STATIC void
226xfs_bmap_count_leaves(
227 xfs_ifork_t *ifp,
228 xfs_extnum_t idx,
229 int numrecs,
230 int *count)
231{
232 int b;
233
234 for (b = 0; b < numrecs; b++) {
235 xfs_bmbt_rec_host_t *frp = xfs_iext_get_ext(ifp, idx + b);
236 *count += xfs_bmbt_get_blockcount(frp);
237 }
238}
239
240/*
241 * Count leaf blocks given a range of extent records originally
242 * in btree format.
243 */
244STATIC void
245xfs_bmap_disk_count_leaves(
246 struct xfs_mount *mp,
247 struct xfs_btree_block *block,
248 int numrecs,
249 int *count)
250{
251 int b;
252 xfs_bmbt_rec_t *frp;
253
254 for (b = 1; b <= numrecs; b++) {
255 frp = XFS_BMBT_REC_ADDR(mp, block, b);
256 *count += xfs_bmbt_disk_get_blockcount(frp);
257 }
258}
259
260/*
261 * Recursively walks each level of a btree
Zhi Yong Wu8be11e92013-08-12 03:14:52 +0000262 * to count total fsblocks in use.
Dave Chinner68988112013-08-12 20:49:42 +1000263 */
264STATIC int /* error */
265xfs_bmap_count_tree(
266 xfs_mount_t *mp, /* file system mount point */
267 xfs_trans_t *tp, /* transaction pointer */
268 xfs_ifork_t *ifp, /* inode fork pointer */
269 xfs_fsblock_t blockno, /* file system block number */
270 int levelin, /* level in btree */
271 int *count) /* Count of blocks */
272{
273 int error;
274 xfs_buf_t *bp, *nbp;
275 int level = levelin;
276 __be64 *pp;
277 xfs_fsblock_t bno = blockno;
278 xfs_fsblock_t nextbno;
279 struct xfs_btree_block *block, *nextblock;
280 int numrecs;
281
282 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF,
283 &xfs_bmbt_buf_ops);
284 if (error)
285 return error;
286 *count += 1;
287 block = XFS_BUF_TO_BLOCK(bp);
288
289 if (--level) {
290 /* Not at node above leaves, count this level of nodes */
291 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
292 while (nextbno != NULLFSBLOCK) {
293 error = xfs_btree_read_bufl(mp, tp, nextbno, 0, &nbp,
294 XFS_BMAP_BTREE_REF,
295 &xfs_bmbt_buf_ops);
296 if (error)
297 return error;
298 *count += 1;
299 nextblock = XFS_BUF_TO_BLOCK(nbp);
300 nextbno = be64_to_cpu(nextblock->bb_u.l.bb_rightsib);
301 xfs_trans_brelse(tp, nbp);
302 }
303
304 /* Dive to the next level */
305 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
306 bno = be64_to_cpu(*pp);
307 if (unlikely((error =
308 xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) {
309 xfs_trans_brelse(tp, bp);
310 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
311 XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +1000312 return -EFSCORRUPTED;
Dave Chinner68988112013-08-12 20:49:42 +1000313 }
314 xfs_trans_brelse(tp, bp);
315 } else {
316 /* count all level 1 nodes and their leaves */
317 for (;;) {
318 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
319 numrecs = be16_to_cpu(block->bb_numrecs);
320 xfs_bmap_disk_count_leaves(mp, block, numrecs, count);
321 xfs_trans_brelse(tp, bp);
322 if (nextbno == NULLFSBLOCK)
323 break;
324 bno = nextbno;
325 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
326 XFS_BMAP_BTREE_REF,
327 &xfs_bmbt_buf_ops);
328 if (error)
329 return error;
330 *count += 1;
331 block = XFS_BUF_TO_BLOCK(bp);
332 }
333 }
334 return 0;
335}
336
337/*
338 * Count fsblocks of the given fork.
339 */
Eric Sandeen0d5a75e2016-06-01 17:38:15 +1000340static int /* error */
Dave Chinner68988112013-08-12 20:49:42 +1000341xfs_bmap_count_blocks(
342 xfs_trans_t *tp, /* transaction pointer */
343 xfs_inode_t *ip, /* incore inode */
344 int whichfork, /* data or attr fork */
345 int *count) /* out: count of blocks */
346{
347 struct xfs_btree_block *block; /* current btree block */
348 xfs_fsblock_t bno; /* block # of "block" */
349 xfs_ifork_t *ifp; /* fork structure */
350 int level; /* btree level, for checking */
351 xfs_mount_t *mp; /* file system mount structure */
352 __be64 *pp; /* pointer to block address */
353
354 bno = NULLFSBLOCK;
355 mp = ip->i_mount;
356 ifp = XFS_IFORK_PTR(ip, whichfork);
357 if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) {
358 xfs_bmap_count_leaves(ifp, 0,
359 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t),
360 count);
361 return 0;
362 }
363
364 /*
365 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
366 */
367 block = ifp->if_broot;
368 level = be16_to_cpu(block->bb_level);
369 ASSERT(level > 0);
370 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
371 bno = be64_to_cpu(*pp);
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000372 ASSERT(bno != NULLFSBLOCK);
Dave Chinner68988112013-08-12 20:49:42 +1000373 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
374 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
375
376 if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) {
377 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW,
378 mp);
Dave Chinner24513372014-06-25 14:58:08 +1000379 return -EFSCORRUPTED;
Dave Chinner68988112013-08-12 20:49:42 +1000380 }
381
382 return 0;
383}
384
385/*
386 * returns 1 for success, 0 if we failed to map the extent.
387 */
388STATIC int
389xfs_getbmapx_fix_eof_hole(
390 xfs_inode_t *ip, /* xfs incore inode pointer */
391 struct getbmapx *out, /* output structure */
392 int prealloced, /* this is a file with
393 * preallocated data space */
394 __int64_t end, /* last block requested */
395 xfs_fsblock_t startblock)
396{
397 __int64_t fixlen;
398 xfs_mount_t *mp; /* file system mount point */
399 xfs_ifork_t *ifp; /* inode fork pointer */
400 xfs_extnum_t lastx; /* last extent pointer */
401 xfs_fileoff_t fileblock;
402
403 if (startblock == HOLESTARTBLOCK) {
404 mp = ip->i_mount;
405 out->bmv_block = -1;
406 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, XFS_ISIZE(ip)));
407 fixlen -= out->bmv_offset;
408 if (prealloced && out->bmv_offset + out->bmv_length == end) {
409 /* Came to hole at EOF. Trim it. */
410 if (fixlen <= 0)
411 return 0;
412 out->bmv_length = fixlen;
413 }
414 } else {
415 if (startblock == DELAYSTARTBLOCK)
416 out->bmv_block = -2;
417 else
418 out->bmv_block = xfs_fsb_to_db(ip, startblock);
419 fileblock = XFS_BB_TO_FSB(ip->i_mount, out->bmv_offset);
420 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
421 if (xfs_iext_bno_to_ext(ifp, fileblock, &lastx) &&
422 (lastx == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))-1))
423 out->bmv_oflags |= BMV_OF_LAST;
424 }
425
426 return 1;
427}
428
429/*
430 * Get inode's extents as described in bmv, and format for output.
431 * Calls formatter to fill the user's buffer until all extents
432 * are mapped, until the passed-in bmv->bmv_count slots have
433 * been filled, or until the formatter short-circuits the loop,
434 * if it is tracking filled-in extents on its own.
435 */
436int /* error code */
437xfs_getbmap(
438 xfs_inode_t *ip,
439 struct getbmapx *bmv, /* user bmap structure */
440 xfs_bmap_format_t formatter, /* format to user */
441 void *arg) /* formatter arg */
442{
443 __int64_t bmvend; /* last block requested */
444 int error = 0; /* return value */
445 __int64_t fixlen; /* length for -1 case */
446 int i; /* extent number */
447 int lock; /* lock state */
448 xfs_bmbt_irec_t *map; /* buffer for user's data */
449 xfs_mount_t *mp; /* file system mount point */
450 int nex; /* # of user extents can do */
451 int nexleft; /* # of user extents left */
452 int subnex; /* # of bmapi's can do */
453 int nmap; /* number of map entries */
454 struct getbmapx *out; /* output structure */
455 int whichfork; /* data or attr fork */
456 int prealloced; /* this is a file with
457 * preallocated data space */
458 int iflags; /* interface flags */
459 int bmapi_flags; /* flags for xfs_bmapi */
460 int cur_ext = 0;
461
462 mp = ip->i_mount;
463 iflags = bmv->bmv_iflags;
464 whichfork = iflags & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK;
465
466 if (whichfork == XFS_ATTR_FORK) {
467 if (XFS_IFORK_Q(ip)) {
468 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
469 ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
470 ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
Dave Chinner24513372014-06-25 14:58:08 +1000471 return -EINVAL;
Dave Chinner68988112013-08-12 20:49:42 +1000472 } else if (unlikely(
473 ip->i_d.di_aformat != 0 &&
474 ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
475 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
476 ip->i_mount);
Dave Chinner24513372014-06-25 14:58:08 +1000477 return -EFSCORRUPTED;
Dave Chinner68988112013-08-12 20:49:42 +1000478 }
479
480 prealloced = 0;
481 fixlen = 1LL << 32;
482 } else {
483 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
484 ip->i_d.di_format != XFS_DINODE_FMT_BTREE &&
485 ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
Dave Chinner24513372014-06-25 14:58:08 +1000486 return -EINVAL;
Dave Chinner68988112013-08-12 20:49:42 +1000487
488 if (xfs_get_extsz_hint(ip) ||
489 ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
490 prealloced = 1;
491 fixlen = mp->m_super->s_maxbytes;
492 } else {
493 prealloced = 0;
494 fixlen = XFS_ISIZE(ip);
495 }
496 }
497
498 if (bmv->bmv_length == -1) {
499 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
500 bmv->bmv_length =
501 max_t(__int64_t, fixlen - bmv->bmv_offset, 0);
502 } else if (bmv->bmv_length == 0) {
503 bmv->bmv_entries = 0;
504 return 0;
505 } else if (bmv->bmv_length < 0) {
Dave Chinner24513372014-06-25 14:58:08 +1000506 return -EINVAL;
Dave Chinner68988112013-08-12 20:49:42 +1000507 }
508
509 nex = bmv->bmv_count - 1;
510 if (nex <= 0)
Dave Chinner24513372014-06-25 14:58:08 +1000511 return -EINVAL;
Dave Chinner68988112013-08-12 20:49:42 +1000512 bmvend = bmv->bmv_offset + bmv->bmv_length;
513
514
515 if (bmv->bmv_count > ULONG_MAX / sizeof(struct getbmapx))
Dave Chinner24513372014-06-25 14:58:08 +1000516 return -ENOMEM;
Dave Chinnerfdd3cce2013-09-02 20:53:00 +1000517 out = kmem_zalloc_large(bmv->bmv_count * sizeof(struct getbmapx), 0);
518 if (!out)
Dave Chinner24513372014-06-25 14:58:08 +1000519 return -ENOMEM;
Dave Chinner68988112013-08-12 20:49:42 +1000520
521 xfs_ilock(ip, XFS_IOLOCK_SHARED);
Christoph Hellwigefa70be2013-12-18 02:14:39 -0800522 if (whichfork == XFS_DATA_FORK) {
523 if (!(iflags & BMV_IF_DELALLOC) &&
524 (ip->i_delayed_blks || XFS_ISIZE(ip) > ip->i_d.di_size)) {
Dave Chinner24513372014-06-25 14:58:08 +1000525 error = filemap_write_and_wait(VFS_I(ip)->i_mapping);
Dave Chinner68988112013-08-12 20:49:42 +1000526 if (error)
527 goto out_unlock_iolock;
Dave Chinner68988112013-08-12 20:49:42 +1000528
Christoph Hellwigefa70be2013-12-18 02:14:39 -0800529 /*
530 * Even after flushing the inode, there can still be
531 * delalloc blocks on the inode beyond EOF due to
532 * speculative preallocation. These are not removed
533 * until the release function is called or the inode
534 * is inactivated. Hence we cannot assert here that
535 * ip->i_delayed_blks == 0.
536 */
537 }
538
539 lock = xfs_ilock_data_map_shared(ip);
540 } else {
541 lock = xfs_ilock_attr_map_shared(ip);
542 }
Dave Chinner68988112013-08-12 20:49:42 +1000543
544 /*
545 * Don't let nex be bigger than the number of extents
546 * we can have assuming alternating holes and real extents.
547 */
548 if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
549 nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
550
551 bmapi_flags = xfs_bmapi_aflag(whichfork);
552 if (!(iflags & BMV_IF_PREALLOC))
553 bmapi_flags |= XFS_BMAPI_IGSTATE;
554
555 /*
556 * Allocate enough space to handle "subnex" maps at a time.
557 */
Dave Chinner24513372014-06-25 14:58:08 +1000558 error = -ENOMEM;
Dave Chinner68988112013-08-12 20:49:42 +1000559 subnex = 16;
560 map = kmem_alloc(subnex * sizeof(*map), KM_MAYFAIL | KM_NOFS);
561 if (!map)
562 goto out_unlock_ilock;
563
564 bmv->bmv_entries = 0;
565
566 if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0 &&
567 (whichfork == XFS_ATTR_FORK || !(iflags & BMV_IF_DELALLOC))) {
568 error = 0;
569 goto out_free_map;
570 }
571
572 nexleft = nex;
573
574 do {
575 nmap = (nexleft > subnex) ? subnex : nexleft;
576 error = xfs_bmapi_read(ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
577 XFS_BB_TO_FSB(mp, bmv->bmv_length),
578 map, &nmap, bmapi_flags);
579 if (error)
580 goto out_free_map;
581 ASSERT(nmap <= subnex);
582
583 for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) {
584 out[cur_ext].bmv_oflags = 0;
585 if (map[i].br_state == XFS_EXT_UNWRITTEN)
586 out[cur_ext].bmv_oflags |= BMV_OF_PREALLOC;
587 else if (map[i].br_startblock == DELAYSTARTBLOCK)
588 out[cur_ext].bmv_oflags |= BMV_OF_DELALLOC;
589 out[cur_ext].bmv_offset =
590 XFS_FSB_TO_BB(mp, map[i].br_startoff);
591 out[cur_ext].bmv_length =
592 XFS_FSB_TO_BB(mp, map[i].br_blockcount);
593 out[cur_ext].bmv_unused1 = 0;
594 out[cur_ext].bmv_unused2 = 0;
595
596 /*
597 * delayed allocation extents that start beyond EOF can
598 * occur due to speculative EOF allocation when the
599 * delalloc extent is larger than the largest freespace
600 * extent at conversion time. These extents cannot be
601 * converted by data writeback, so can exist here even
602 * if we are not supposed to be finding delalloc
603 * extents.
604 */
605 if (map[i].br_startblock == DELAYSTARTBLOCK &&
606 map[i].br_startoff <= XFS_B_TO_FSB(mp, XFS_ISIZE(ip)))
607 ASSERT((iflags & BMV_IF_DELALLOC) != 0);
608
609 if (map[i].br_startblock == HOLESTARTBLOCK &&
610 whichfork == XFS_ATTR_FORK) {
611 /* came to the end of attribute fork */
612 out[cur_ext].bmv_oflags |= BMV_OF_LAST;
613 goto out_free_map;
614 }
615
616 if (!xfs_getbmapx_fix_eof_hole(ip, &out[cur_ext],
617 prealloced, bmvend,
618 map[i].br_startblock))
619 goto out_free_map;
620
621 bmv->bmv_offset =
622 out[cur_ext].bmv_offset +
623 out[cur_ext].bmv_length;
624 bmv->bmv_length =
625 max_t(__int64_t, 0, bmvend - bmv->bmv_offset);
626
627 /*
628 * In case we don't want to return the hole,
629 * don't increase cur_ext so that we can reuse
630 * it in the next loop.
631 */
632 if ((iflags & BMV_IF_NO_HOLES) &&
633 map[i].br_startblock == HOLESTARTBLOCK) {
634 memset(&out[cur_ext], 0, sizeof(out[cur_ext]));
635 continue;
636 }
637
638 nexleft--;
639 bmv->bmv_entries++;
640 cur_ext++;
641 }
642 } while (nmap && nexleft && bmv->bmv_length);
643
644 out_free_map:
645 kmem_free(map);
646 out_unlock_ilock:
Christoph Hellwig01f4f322013-12-06 12:30:08 -0800647 xfs_iunlock(ip, lock);
Dave Chinner68988112013-08-12 20:49:42 +1000648 out_unlock_iolock:
649 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
650
651 for (i = 0; i < cur_ext; i++) {
652 int full = 0; /* user array is full */
653
654 /* format results & advance arg */
655 error = formatter(&arg, &out[i], &full);
656 if (error || full)
657 break;
658 }
659
Dave Chinnerfdd3cce2013-09-02 20:53:00 +1000660 kmem_free(out);
Dave Chinner68988112013-08-12 20:49:42 +1000661 return error;
662}
663
664/*
665 * dead simple method of punching delalyed allocation blocks from a range in
666 * the inode. Walks a block at a time so will be slow, but is only executed in
Zhi Yong Wuad4809b2013-08-12 03:14:55 +0000667 * rare error cases so the overhead is not critical. This will always punch out
Dave Chinner68988112013-08-12 20:49:42 +1000668 * both the start and end blocks, even if the ranges only partially overlap
669 * them, so it is up to the caller to ensure that partial blocks are not
670 * passed in.
671 */
672int
673xfs_bmap_punch_delalloc_range(
674 struct xfs_inode *ip,
675 xfs_fileoff_t start_fsb,
676 xfs_fileoff_t length)
677{
678 xfs_fileoff_t remaining = length;
679 int error = 0;
680
681 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
682
683 do {
684 int done;
685 xfs_bmbt_irec_t imap;
686 int nimaps = 1;
687 xfs_fsblock_t firstblock;
688 xfs_bmap_free_t flist;
689
690 /*
691 * Map the range first and check that it is a delalloc extent
692 * before trying to unmap the range. Otherwise we will be
693 * trying to remove a real extent (which requires a
694 * transaction) or a hole, which is probably a bad idea...
695 */
696 error = xfs_bmapi_read(ip, start_fsb, 1, &imap, &nimaps,
697 XFS_BMAPI_ENTIRE);
698
699 if (error) {
700 /* something screwed, just bail */
701 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
702 xfs_alert(ip->i_mount,
703 "Failed delalloc mapping lookup ino %lld fsb %lld.",
704 ip->i_ino, start_fsb);
705 }
706 break;
707 }
708 if (!nimaps) {
709 /* nothing there */
710 goto next_block;
711 }
712 if (imap.br_startblock != DELAYSTARTBLOCK) {
713 /* been converted, ignore */
714 goto next_block;
715 }
716 WARN_ON(imap.br_blockcount == 0);
717
718 /*
719 * Note: while we initialise the firstblock/flist pair, they
720 * should never be used because blocks should never be
721 * allocated or freed for a delalloc extent and hence we need
722 * don't cancel or finish them after the xfs_bunmapi() call.
723 */
724 xfs_bmap_init(&flist, &firstblock);
725 error = xfs_bunmapi(NULL, ip, start_fsb, 1, 0, 1, &firstblock,
726 &flist, &done);
727 if (error)
728 break;
729
Darrick J. Wong3ab78df2016-08-03 11:15:38 +1000730 ASSERT(!xfs_defer_has_unfinished_work(&flist));
Dave Chinner68988112013-08-12 20:49:42 +1000731next_block:
732 start_fsb++;
733 remaining--;
734 } while(remaining > 0);
735
736 return error;
737}
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000738
739/*
740 * Test whether it is appropriate to check an inode for and free post EOF
741 * blocks. The 'force' parameter determines whether we should also consider
742 * regular files that are marked preallocated or append-only.
743 */
744bool
745xfs_can_free_eofblocks(struct xfs_inode *ip, bool force)
746{
747 /* prealloc/delalloc exists only on regular files */
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100748 if (!S_ISREG(VFS_I(ip)->i_mode))
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000749 return false;
750
751 /*
752 * Zero sized files with no cached pages and delalloc blocks will not
753 * have speculative prealloc/delalloc blocks to remove.
754 */
755 if (VFS_I(ip)->i_size == 0 &&
Dave Chinner2667c6f2014-08-04 13:23:15 +1000756 VFS_I(ip)->i_mapping->nrpages == 0 &&
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000757 ip->i_delayed_blks == 0)
758 return false;
759
760 /* If we haven't read in the extent list, then don't do it now. */
761 if (!(ip->i_df.if_flags & XFS_IFEXTENTS))
762 return false;
763
764 /*
765 * Do not free real preallocated or append-only files unless the file
766 * has delalloc blocks and we are forced to remove them.
767 */
768 if (ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND))
769 if (!force || ip->i_delayed_blks == 0)
770 return false;
771
772 return true;
773}
774
775/*
776 * This is called by xfs_inactive to free any blocks beyond eof
777 * when the link count isn't zero and by xfs_dm_punch_hole() when
778 * punching a hole to EOF.
779 */
780int
781xfs_free_eofblocks(
782 xfs_mount_t *mp,
783 xfs_inode_t *ip,
784 bool need_iolock)
785{
786 xfs_trans_t *tp;
787 int error;
788 xfs_fileoff_t end_fsb;
789 xfs_fileoff_t last_fsb;
790 xfs_filblks_t map_len;
791 int nimaps;
792 xfs_bmbt_irec_t imap;
793
794 /*
795 * Figure out if there are any blocks beyond the end
796 * of the file. If not, then there is nothing to do.
797 */
798 end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_ISIZE(ip));
799 last_fsb = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
800 if (last_fsb <= end_fsb)
801 return 0;
802 map_len = last_fsb - end_fsb;
803
804 nimaps = 1;
805 xfs_ilock(ip, XFS_ILOCK_SHARED);
806 error = xfs_bmapi_read(ip, end_fsb, map_len, &imap, &nimaps, 0);
807 xfs_iunlock(ip, XFS_ILOCK_SHARED);
808
809 if (!error && (nimaps != 0) &&
810 (imap.br_startblock != HOLESTARTBLOCK ||
811 ip->i_delayed_blks)) {
812 /*
813 * Attach the dquots to the inode up front.
814 */
815 error = xfs_qm_dqattach(ip, 0);
816 if (error)
817 return error;
818
819 /*
820 * There are blocks after the end of file.
821 * Free them up now by truncating the file to
822 * its current size.
823 */
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000824 if (need_iolock) {
Christoph Hellwig253f4912016-04-06 09:19:55 +1000825 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL))
Dave Chinner24513372014-06-25 14:58:08 +1000826 return -EAGAIN;
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000827 }
828
Christoph Hellwig253f4912016-04-06 09:19:55 +1000829 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0,
830 &tp);
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000831 if (error) {
832 ASSERT(XFS_FORCED_SHUTDOWN(mp));
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000833 if (need_iolock)
834 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
835 return error;
836 }
837
838 xfs_ilock(ip, XFS_ILOCK_EXCL);
839 xfs_trans_ijoin(tp, ip, 0);
840
841 /*
842 * Do not update the on-disk file size. If we update the
843 * on-disk file size and then the system crashes before the
844 * contents of the file are flushed to disk then the files
845 * may be full of holes (ie NULL files bug).
846 */
847 error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK,
848 XFS_ISIZE(ip));
849 if (error) {
850 /*
851 * If we get an error at this point we simply don't
852 * bother truncating the file.
853 */
Christoph Hellwig4906e212015-06-04 13:47:56 +1000854 xfs_trans_cancel(tp);
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000855 } else {
Christoph Hellwig70393312015-06-04 13:48:08 +1000856 error = xfs_trans_commit(tp);
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000857 if (!error)
858 xfs_inode_clear_eofblocks_tag(ip);
859 }
860
861 xfs_iunlock(ip, XFS_ILOCK_EXCL);
862 if (need_iolock)
863 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
864 }
865 return error;
866}
867
Christoph Hellwig83aee9e2013-10-12 00:55:07 -0700868int
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000869xfs_alloc_file_space(
Christoph Hellwig83aee9e2013-10-12 00:55:07 -0700870 struct xfs_inode *ip,
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000871 xfs_off_t offset,
872 xfs_off_t len,
Christoph Hellwig5f8aca82013-10-12 00:55:06 -0700873 int alloc_type)
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000874{
875 xfs_mount_t *mp = ip->i_mount;
876 xfs_off_t count;
877 xfs_filblks_t allocated_fsb;
878 xfs_filblks_t allocatesize_fsb;
879 xfs_extlen_t extsz, temp;
880 xfs_fileoff_t startoffset_fsb;
881 xfs_fsblock_t firstfsb;
882 int nimaps;
883 int quota_flag;
884 int rt;
885 xfs_trans_t *tp;
886 xfs_bmbt_irec_t imaps[1], *imapp;
887 xfs_bmap_free_t free_list;
888 uint qblocks, resblks, resrtextents;
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000889 int error;
890
891 trace_xfs_alloc_file_space(ip);
892
893 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +1000894 return -EIO;
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000895
896 error = xfs_qm_dqattach(ip, 0);
897 if (error)
898 return error;
899
900 if (len <= 0)
Dave Chinner24513372014-06-25 14:58:08 +1000901 return -EINVAL;
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000902
903 rt = XFS_IS_REALTIME_INODE(ip);
904 extsz = xfs_get_extsz_hint(ip);
905
906 count = len;
907 imapp = &imaps[0];
908 nimaps = 1;
909 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
910 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
911
912 /*
913 * Allocate file space until done or until there is an error
914 */
915 while (allocatesize_fsb && !error) {
916 xfs_fileoff_t s, e;
917
918 /*
919 * Determine space reservations for data/realtime.
920 */
921 if (unlikely(extsz)) {
922 s = startoffset_fsb;
923 do_div(s, extsz);
924 s *= extsz;
925 e = startoffset_fsb + allocatesize_fsb;
926 if ((temp = do_mod(startoffset_fsb, extsz)))
927 e += temp;
928 if ((temp = do_mod(e, extsz)))
929 e += extsz - temp;
930 } else {
931 s = 0;
932 e = allocatesize_fsb;
933 }
934
935 /*
936 * The transaction reservation is limited to a 32-bit block
937 * count, hence we need to limit the number of blocks we are
938 * trying to reserve to avoid an overflow. We can't allocate
939 * more than @nimaps extents, and an extent is limited on disk
940 * to MAXEXTLEN (21 bits), so use that to enforce the limit.
941 */
942 resblks = min_t(xfs_fileoff_t, (e - s), (MAXEXTLEN * nimaps));
943 if (unlikely(rt)) {
944 resrtextents = qblocks = resblks;
945 resrtextents /= mp->m_sb.sb_rextsize;
946 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
947 quota_flag = XFS_QMOPT_RES_RTBLKS;
948 } else {
949 resrtextents = 0;
950 resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resblks);
951 quota_flag = XFS_QMOPT_RES_REGBLKS;
952 }
953
954 /*
955 * Allocate and setup the transaction.
956 */
Christoph Hellwig253f4912016-04-06 09:19:55 +1000957 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks,
958 resrtextents, 0, &tp);
959
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000960 /*
961 * Check for running out of space
962 */
963 if (error) {
964 /*
965 * Free the transaction structure.
966 */
Dave Chinner24513372014-06-25 14:58:08 +1000967 ASSERT(error == -ENOSPC || XFS_FORCED_SHUTDOWN(mp));
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000968 break;
969 }
970 xfs_ilock(ip, XFS_ILOCK_EXCL);
971 error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks,
972 0, quota_flag);
973 if (error)
974 goto error1;
975
976 xfs_trans_ijoin(tp, ip, 0);
977
978 xfs_bmap_init(&free_list, &firstfsb);
979 error = xfs_bmapi_write(tp, ip, startoffset_fsb,
980 allocatesize_fsb, alloc_type, &firstfsb,
Brian Fosterdbd5c8c2015-10-12 16:04:13 +1100981 resblks, imapp, &nimaps, &free_list);
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100982 if (error)
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000983 goto error0;
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000984
985 /*
986 * Complete the transaction
987 */
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100988 error = xfs_bmap_finish(&tp, &free_list, NULL);
989 if (error)
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000990 goto error0;
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000991
Christoph Hellwig70393312015-06-04 13:48:08 +1000992 error = xfs_trans_commit(tp);
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000993 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100994 if (error)
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000995 break;
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000996
997 allocated_fsb = imapp->br_blockcount;
998
999 if (nimaps == 0) {
Dave Chinner24513372014-06-25 14:58:08 +10001000 error = -ENOSPC;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001001 break;
1002 }
1003
1004 startoffset_fsb += allocated_fsb;
1005 allocatesize_fsb -= allocated_fsb;
1006 }
1007
1008 return error;
1009
1010error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
1011 xfs_bmap_cancel(&free_list);
1012 xfs_trans_unreserve_quota_nblks(tp, ip, (long)qblocks, 0, quota_flag);
1013
1014error1: /* Just cancel transaction */
Christoph Hellwig4906e212015-06-04 13:47:56 +10001015 xfs_trans_cancel(tp);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001016 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1017 return error;
1018}
1019
Christoph Hellwigbdb0d042016-06-21 10:00:55 +10001020static int
1021xfs_unmap_extent(
1022 struct xfs_inode *ip,
1023 xfs_fileoff_t startoffset_fsb,
1024 xfs_filblks_t len_fsb,
1025 int *done)
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001026{
Christoph Hellwigbdb0d042016-06-21 10:00:55 +10001027 struct xfs_mount *mp = ip->i_mount;
1028 struct xfs_trans *tp;
1029 struct xfs_bmap_free free_list;
1030 xfs_fsblock_t firstfsb;
1031 uint resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
1032 int error;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001033
Christoph Hellwigbdb0d042016-06-21 10:00:55 +10001034 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp);
1035 if (error) {
1036 ASSERT(error == -ENOSPC || XFS_FORCED_SHUTDOWN(mp));
1037 return error;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001038 }
Christoph Hellwigbdb0d042016-06-21 10:00:55 +10001039
1040 xfs_ilock(ip, XFS_ILOCK_EXCL);
1041 error = xfs_trans_reserve_quota(tp, mp, ip->i_udquot, ip->i_gdquot,
1042 ip->i_pdquot, resblks, 0, XFS_QMOPT_RES_REGBLKS);
1043 if (error)
1044 goto out_trans_cancel;
1045
1046 xfs_trans_ijoin(tp, ip, 0);
1047
1048 xfs_bmap_init(&free_list, &firstfsb);
1049 error = xfs_bunmapi(tp, ip, startoffset_fsb, len_fsb, 0, 2, &firstfsb,
1050 &free_list, done);
1051 if (error)
1052 goto out_bmap_cancel;
1053
1054 error = xfs_bmap_finish(&tp, &free_list, NULL);
1055 if (error)
1056 goto out_bmap_cancel;
1057
1058 error = xfs_trans_commit(tp);
1059out_unlock:
1060 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001061 return error;
Christoph Hellwigbdb0d042016-06-21 10:00:55 +10001062
1063out_bmap_cancel:
1064 xfs_bmap_cancel(&free_list);
1065out_trans_cancel:
1066 xfs_trans_cancel(tp);
1067 goto out_unlock;
1068}
1069
1070static int
1071xfs_adjust_extent_unmap_boundaries(
1072 struct xfs_inode *ip,
1073 xfs_fileoff_t *startoffset_fsb,
1074 xfs_fileoff_t *endoffset_fsb)
1075{
1076 struct xfs_mount *mp = ip->i_mount;
1077 struct xfs_bmbt_irec imap;
1078 int nimap, error;
1079 xfs_extlen_t mod = 0;
1080
1081 nimap = 1;
1082 error = xfs_bmapi_read(ip, *startoffset_fsb, 1, &imap, &nimap, 0);
1083 if (error)
1084 return error;
1085
1086 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
1087 xfs_daddr_t block;
1088
1089 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
1090 block = imap.br_startblock;
1091 mod = do_div(block, mp->m_sb.sb_rextsize);
1092 if (mod)
1093 *startoffset_fsb += mp->m_sb.sb_rextsize - mod;
1094 }
1095
1096 nimap = 1;
1097 error = xfs_bmapi_read(ip, *endoffset_fsb - 1, 1, &imap, &nimap, 0);
1098 if (error)
1099 return error;
1100
1101 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
1102 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
1103 mod++;
1104 if (mod && mod != mp->m_sb.sb_rextsize)
1105 *endoffset_fsb -= mod;
1106 }
1107
1108 return 0;
1109}
1110
1111static int
1112xfs_flush_unmap_range(
1113 struct xfs_inode *ip,
1114 xfs_off_t offset,
1115 xfs_off_t len)
1116{
1117 struct xfs_mount *mp = ip->i_mount;
1118 struct inode *inode = VFS_I(ip);
1119 xfs_off_t rounding, start, end;
1120 int error;
1121
1122 /* wait for the completion of any pending DIOs */
1123 inode_dio_wait(inode);
1124
1125 rounding = max_t(xfs_off_t, 1 << mp->m_sb.sb_blocklog, PAGE_SIZE);
1126 start = round_down(offset, rounding);
1127 end = round_up(offset + len, rounding) - 1;
1128
1129 error = filemap_write_and_wait_range(inode->i_mapping, start, end);
1130 if (error)
1131 return error;
1132 truncate_pagecache_range(inode, start, end);
1133 return 0;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001134}
1135
Christoph Hellwig83aee9e2013-10-12 00:55:07 -07001136int
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001137xfs_free_file_space(
Christoph Hellwig83aee9e2013-10-12 00:55:07 -07001138 struct xfs_inode *ip,
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001139 xfs_off_t offset,
Christoph Hellwig5f8aca82013-10-12 00:55:06 -07001140 xfs_off_t len)
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001141{
Christoph Hellwigbdb0d042016-06-21 10:00:55 +10001142 struct xfs_mount *mp = ip->i_mount;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001143 xfs_fileoff_t startoffset_fsb;
Christoph Hellwigbdb0d042016-06-21 10:00:55 +10001144 xfs_fileoff_t endoffset_fsb;
Christoph Hellwig3c2bdc92016-06-21 10:02:23 +10001145 int done = 0, error;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001146
1147 trace_xfs_free_file_space(ip);
1148
1149 error = xfs_qm_dqattach(ip, 0);
1150 if (error)
1151 return error;
1152
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001153 if (len <= 0) /* if nothing being freed */
Christoph Hellwigbdb0d042016-06-21 10:00:55 +10001154 return 0;
1155
1156 error = xfs_flush_unmap_range(ip, offset, len);
1157 if (error)
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001158 return error;
Christoph Hellwigbdb0d042016-06-21 10:00:55 +10001159
1160 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001161 endoffset_fsb = XFS_B_TO_FSBT(mp, offset + len);
1162
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001163 /*
Christoph Hellwigbdb0d042016-06-21 10:00:55 +10001164 * Need to zero the stuff we're not freeing, on disk. If it's a RT file
1165 * and we can't use unwritten extents then we actually need to ensure
1166 * to zero the whole extent, otherwise we just need to take of block
1167 * boundaries, and xfs_bunmapi will handle the rest.
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001168 */
Christoph Hellwigbdb0d042016-06-21 10:00:55 +10001169 if (XFS_IS_REALTIME_INODE(ip) &&
1170 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
1171 error = xfs_adjust_extent_unmap_boundaries(ip, &startoffset_fsb,
1172 &endoffset_fsb);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001173 if (error)
Christoph Hellwigbdb0d042016-06-21 10:00:55 +10001174 return error;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001175 }
Christoph Hellwigbdb0d042016-06-21 10:00:55 +10001176
Christoph Hellwig3c2bdc92016-06-21 10:02:23 +10001177 if (endoffset_fsb > startoffset_fsb) {
1178 while (!done) {
1179 error = xfs_unmap_extent(ip, startoffset_fsb,
1180 endoffset_fsb - startoffset_fsb, &done);
1181 if (error)
1182 return error;
1183 }
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001184 }
1185
1186 /*
Christoph Hellwig3c2bdc92016-06-21 10:02:23 +10001187 * Now that we've unmap all full blocks we'll have to zero out any
1188 * partial block at the beginning and/or end. xfs_zero_range is
1189 * smart enough to skip any holes, including those we just created.
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001190 */
Christoph Hellwig3c2bdc92016-06-21 10:02:23 +10001191 return xfs_zero_range(ip, offset, len, NULL);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001192}
1193
Brian Foster5d11fb42014-10-30 10:35:11 +11001194/*
1195 * Preallocate and zero a range of a file. This mechanism has the allocation
1196 * semantics of fallocate and in addition converts data in the range to zeroes.
1197 */
Christoph Hellwig865e9442013-10-12 00:55:08 -07001198int
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001199xfs_zero_file_space(
1200 struct xfs_inode *ip,
1201 xfs_off_t offset,
Christoph Hellwig5f8aca82013-10-12 00:55:06 -07001202 xfs_off_t len)
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001203{
1204 struct xfs_mount *mp = ip->i_mount;
Brian Foster5d11fb42014-10-30 10:35:11 +11001205 uint blksize;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001206 int error;
1207
Dave Chinner897b73b2014-04-14 18:15:11 +10001208 trace_xfs_zero_file_space(ip);
1209
Brian Foster5d11fb42014-10-30 10:35:11 +11001210 blksize = 1 << mp->m_sb.sb_blocklog;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001211
1212 /*
Brian Foster5d11fb42014-10-30 10:35:11 +11001213 * Punch a hole and prealloc the range. We use hole punch rather than
1214 * unwritten extent conversion for two reasons:
1215 *
1216 * 1.) Hole punch handles partial block zeroing for us.
1217 *
1218 * 2.) If prealloc returns ENOSPC, the file range is still zero-valued
1219 * by virtue of the hole punch.
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001220 */
Brian Foster5d11fb42014-10-30 10:35:11 +11001221 error = xfs_free_file_space(ip, offset, len);
1222 if (error)
1223 goto out;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001224
Brian Foster5d11fb42014-10-30 10:35:11 +11001225 error = xfs_alloc_file_space(ip, round_down(offset, blksize),
1226 round_up(offset + len, blksize) -
1227 round_down(offset, blksize),
1228 XFS_BMAPI_PREALLOC);
Christoph Hellwig5f8aca82013-10-12 00:55:06 -07001229out:
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001230 return error;
1231
1232}
1233
1234/*
Namjae Jeona904b1c2015-03-25 15:08:56 +11001235 * @next_fsb will keep track of the extent currently undergoing shift.
1236 * @stop_fsb will keep track of the extent at which we have to stop.
1237 * If we are shifting left, we will start with block (offset + len) and
1238 * shift each extent till last extent.
1239 * If we are shifting right, we will start with last extent inside file space
1240 * and continue until we reach the block corresponding to offset.
Namjae Jeone1d8fb82014-02-24 10:58:19 +11001241 */
kbuild test robot72c1a732015-04-13 11:25:04 +10001242static int
Namjae Jeona904b1c2015-03-25 15:08:56 +11001243xfs_shift_file_space(
1244 struct xfs_inode *ip,
1245 xfs_off_t offset,
1246 xfs_off_t len,
1247 enum shift_direction direction)
Namjae Jeone1d8fb82014-02-24 10:58:19 +11001248{
1249 int done = 0;
1250 struct xfs_mount *mp = ip->i_mount;
1251 struct xfs_trans *tp;
1252 int error;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11001253 struct xfs_bmap_free free_list;
1254 xfs_fsblock_t first_block;
Namjae Jeona904b1c2015-03-25 15:08:56 +11001255 xfs_fileoff_t stop_fsb;
Brian Foster2c845f52014-09-23 15:37:09 +10001256 xfs_fileoff_t next_fsb;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11001257 xfs_fileoff_t shift_fsb;
1258
Namjae Jeona904b1c2015-03-25 15:08:56 +11001259 ASSERT(direction == SHIFT_LEFT || direction == SHIFT_RIGHT);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11001260
Namjae Jeona904b1c2015-03-25 15:08:56 +11001261 if (direction == SHIFT_LEFT) {
1262 next_fsb = XFS_B_TO_FSB(mp, offset + len);
1263 stop_fsb = XFS_B_TO_FSB(mp, VFS_I(ip)->i_size);
1264 } else {
1265 /*
1266 * If right shift, delegate the work of initialization of
1267 * next_fsb to xfs_bmap_shift_extent as it has ilock held.
1268 */
1269 next_fsb = NULLFSBLOCK;
1270 stop_fsb = XFS_B_TO_FSB(mp, offset);
1271 }
Namjae Jeone1d8fb82014-02-24 10:58:19 +11001272
Namjae Jeone1d8fb82014-02-24 10:58:19 +11001273 shift_fsb = XFS_B_TO_FSB(mp, len);
1274
Brian Fosterf71721d2014-09-23 15:39:05 +10001275 /*
1276 * Trim eofblocks to avoid shifting uninitialized post-eof preallocation
1277 * into the accessible region of the file.
1278 */
Brian Foster41b9d722014-09-02 12:12:53 +10001279 if (xfs_can_free_eofblocks(ip, true)) {
1280 error = xfs_free_eofblocks(mp, ip, false);
1281 if (error)
1282 return error;
1283 }
Dave Chinner1669a8c2014-09-02 12:12:53 +10001284
Brian Fosterf71721d2014-09-23 15:39:05 +10001285 /*
1286 * Writeback and invalidate cache for the remainder of the file as we're
Namjae Jeona904b1c2015-03-25 15:08:56 +11001287 * about to shift down every extent from offset to EOF.
Brian Fosterf71721d2014-09-23 15:39:05 +10001288 */
1289 error = filemap_write_and_wait_range(VFS_I(ip)->i_mapping,
Namjae Jeona904b1c2015-03-25 15:08:56 +11001290 offset, -1);
Brian Fosterf71721d2014-09-23 15:39:05 +10001291 if (error)
1292 return error;
1293 error = invalidate_inode_pages2_range(VFS_I(ip)->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001294 offset >> PAGE_SHIFT, -1);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11001295 if (error)
1296 return error;
1297
Namjae Jeona904b1c2015-03-25 15:08:56 +11001298 /*
1299 * The extent shiting code works on extent granularity. So, if
1300 * stop_fsb is not the starting block of extent, we need to split
1301 * the extent at stop_fsb.
1302 */
1303 if (direction == SHIFT_RIGHT) {
1304 error = xfs_bmap_split_extent(ip, stop_fsb);
1305 if (error)
1306 return error;
1307 }
1308
Namjae Jeone1d8fb82014-02-24 10:58:19 +11001309 while (!error && !done) {
Namjae Jeone1d8fb82014-02-24 10:58:19 +11001310 /*
1311 * We would need to reserve permanent block for transaction.
1312 * This will come into picture when after shifting extent into
1313 * hole we found that adjacent extents can be merged which
1314 * may lead to freeing of a block during record update.
1315 */
Christoph Hellwig253f4912016-04-06 09:19:55 +10001316 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write,
1317 XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp);
1318 if (error)
Namjae Jeone1d8fb82014-02-24 10:58:19 +11001319 break;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11001320
1321 xfs_ilock(ip, XFS_ILOCK_EXCL);
1322 error = xfs_trans_reserve_quota(tp, mp, ip->i_udquot,
1323 ip->i_gdquot, ip->i_pdquot,
1324 XFS_DIOSTRAT_SPACE_RES(mp, 0), 0,
1325 XFS_QMOPT_RES_REGBLKS);
1326 if (error)
Brian Fosterd4a97a02015-08-19 10:01:40 +10001327 goto out_trans_cancel;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11001328
Namjae Jeona904b1c2015-03-25 15:08:56 +11001329 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11001330
1331 xfs_bmap_init(&free_list, &first_block);
1332
1333 /*
1334 * We are using the write transaction in which max 2 bmbt
1335 * updates are allowed
1336 */
Namjae Jeona904b1c2015-03-25 15:08:56 +11001337 error = xfs_bmap_shift_extents(tp, ip, &next_fsb, shift_fsb,
1338 &done, stop_fsb, &first_block, &free_list,
1339 direction, XFS_BMAP_MAX_SHIFT_EXTENTS);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11001340 if (error)
Brian Fosterd4a97a02015-08-19 10:01:40 +10001341 goto out_bmap_cancel;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11001342
Eric Sandeenf6106ef2016-01-11 11:34:01 +11001343 error = xfs_bmap_finish(&tp, &free_list, NULL);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11001344 if (error)
Brian Fosterd4a97a02015-08-19 10:01:40 +10001345 goto out_bmap_cancel;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11001346
Christoph Hellwig70393312015-06-04 13:48:08 +10001347 error = xfs_trans_commit(tp);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11001348 }
1349
1350 return error;
1351
Brian Fosterd4a97a02015-08-19 10:01:40 +10001352out_bmap_cancel:
1353 xfs_bmap_cancel(&free_list);
1354out_trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001355 xfs_trans_cancel(tp);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11001356 return error;
1357}
1358
1359/*
Namjae Jeona904b1c2015-03-25 15:08:56 +11001360 * xfs_collapse_file_space()
1361 * This routine frees disk space and shift extent for the given file.
1362 * The first thing we do is to free data blocks in the specified range
1363 * by calling xfs_free_file_space(). It would also sync dirty data
1364 * and invalidate page cache over the region on which collapse range
1365 * is working. And Shift extent records to the left to cover a hole.
1366 * RETURNS:
1367 * 0 on success
1368 * errno on error
1369 *
1370 */
1371int
1372xfs_collapse_file_space(
1373 struct xfs_inode *ip,
1374 xfs_off_t offset,
1375 xfs_off_t len)
1376{
1377 int error;
1378
1379 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
1380 trace_xfs_collapse_file_space(ip);
1381
1382 error = xfs_free_file_space(ip, offset, len);
1383 if (error)
1384 return error;
1385
1386 return xfs_shift_file_space(ip, offset, len, SHIFT_LEFT);
1387}
1388
1389/*
1390 * xfs_insert_file_space()
1391 * This routine create hole space by shifting extents for the given file.
1392 * The first thing we do is to sync dirty data and invalidate page cache
1393 * over the region on which insert range is working. And split an extent
1394 * to two extents at given offset by calling xfs_bmap_split_extent.
1395 * And shift all extent records which are laying between [offset,
1396 * last allocated extent] to the right to reserve hole range.
1397 * RETURNS:
1398 * 0 on success
1399 * errno on error
1400 */
1401int
1402xfs_insert_file_space(
1403 struct xfs_inode *ip,
1404 loff_t offset,
1405 loff_t len)
1406{
1407 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
1408 trace_xfs_insert_file_space(ip);
1409
1410 return xfs_shift_file_space(ip, offset, len, SHIFT_RIGHT);
1411}
1412
1413/*
Dave Chinnera133d952013-08-12 20:49:48 +10001414 * We need to check that the format of the data fork in the temporary inode is
1415 * valid for the target inode before doing the swap. This is not a problem with
1416 * attr1 because of the fixed fork offset, but attr2 has a dynamically sized
1417 * data fork depending on the space the attribute fork is taking so we can get
1418 * invalid formats on the target inode.
1419 *
1420 * E.g. target has space for 7 extents in extent format, temp inode only has
1421 * space for 6. If we defragment down to 7 extents, then the tmp format is a
1422 * btree, but when swapped it needs to be in extent format. Hence we can't just
1423 * blindly swap data forks on attr2 filesystems.
1424 *
1425 * Note that we check the swap in both directions so that we don't end up with
1426 * a corrupt temporary inode, either.
1427 *
1428 * Note that fixing the way xfs_fsr sets up the attribute fork in the source
1429 * inode will prevent this situation from occurring, so all we do here is
1430 * reject and log the attempt. basically we are putting the responsibility on
1431 * userspace to get this right.
1432 */
1433static int
1434xfs_swap_extents_check_format(
1435 xfs_inode_t *ip, /* target inode */
1436 xfs_inode_t *tip) /* tmp inode */
1437{
1438
1439 /* Should never get a local format */
1440 if (ip->i_d.di_format == XFS_DINODE_FMT_LOCAL ||
1441 tip->i_d.di_format == XFS_DINODE_FMT_LOCAL)
Dave Chinner24513372014-06-25 14:58:08 +10001442 return -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001443
1444 /*
1445 * if the target inode has less extents that then temporary inode then
1446 * why did userspace call us?
1447 */
1448 if (ip->i_d.di_nextents < tip->i_d.di_nextents)
Dave Chinner24513372014-06-25 14:58:08 +10001449 return -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001450
1451 /*
1452 * if the target inode is in extent form and the temp inode is in btree
1453 * form then we will end up with the target inode in the wrong format
1454 * as we already know there are less extents in the temp inode.
1455 */
1456 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1457 tip->i_d.di_format == XFS_DINODE_FMT_BTREE)
Dave Chinner24513372014-06-25 14:58:08 +10001458 return -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001459
1460 /* Check temp in extent form to max in target */
1461 if (tip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1462 XFS_IFORK_NEXTENTS(tip, XFS_DATA_FORK) >
1463 XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK))
Dave Chinner24513372014-06-25 14:58:08 +10001464 return -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001465
1466 /* Check target in extent form to max in temp */
1467 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1468 XFS_IFORK_NEXTENTS(ip, XFS_DATA_FORK) >
1469 XFS_IFORK_MAXEXT(tip, XFS_DATA_FORK))
Dave Chinner24513372014-06-25 14:58:08 +10001470 return -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001471
1472 /*
1473 * If we are in a btree format, check that the temp root block will fit
1474 * in the target and that it has enough extents to be in btree format
1475 * in the target.
1476 *
1477 * Note that we have to be careful to allow btree->extent conversions
1478 * (a common defrag case) which will occur when the temp inode is in
1479 * extent format...
1480 */
1481 if (tip->i_d.di_format == XFS_DINODE_FMT_BTREE) {
1482 if (XFS_IFORK_BOFF(ip) &&
1483 XFS_BMAP_BMDR_SPACE(tip->i_df.if_broot) > XFS_IFORK_BOFF(ip))
Dave Chinner24513372014-06-25 14:58:08 +10001484 return -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001485 if (XFS_IFORK_NEXTENTS(tip, XFS_DATA_FORK) <=
1486 XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK))
Dave Chinner24513372014-06-25 14:58:08 +10001487 return -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001488 }
1489
1490 /* Reciprocal target->temp btree format checks */
1491 if (ip->i_d.di_format == XFS_DINODE_FMT_BTREE) {
1492 if (XFS_IFORK_BOFF(tip) &&
1493 XFS_BMAP_BMDR_SPACE(ip->i_df.if_broot) > XFS_IFORK_BOFF(tip))
Dave Chinner24513372014-06-25 14:58:08 +10001494 return -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001495 if (XFS_IFORK_NEXTENTS(ip, XFS_DATA_FORK) <=
1496 XFS_IFORK_MAXEXT(tip, XFS_DATA_FORK))
Dave Chinner24513372014-06-25 14:58:08 +10001497 return -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001498 }
1499
1500 return 0;
1501}
1502
Dave Chinner7abbb8f2014-09-23 16:20:11 +10001503static int
Dave Chinner4ef897a2014-08-04 13:44:08 +10001504xfs_swap_extent_flush(
1505 struct xfs_inode *ip)
1506{
1507 int error;
1508
1509 error = filemap_write_and_wait(VFS_I(ip)->i_mapping);
1510 if (error)
1511 return error;
1512 truncate_pagecache_range(VFS_I(ip), 0, -1);
1513
1514 /* Verify O_DIRECT for ftmp */
1515 if (VFS_I(ip)->i_mapping->nrpages)
1516 return -EINVAL;
Dave Chinner4ef897a2014-08-04 13:44:08 +10001517 return 0;
1518}
1519
1520int
Dave Chinnera133d952013-08-12 20:49:48 +10001521xfs_swap_extents(
1522 xfs_inode_t *ip, /* target inode */
1523 xfs_inode_t *tip, /* tmp inode */
1524 xfs_swapext_t *sxp)
1525{
1526 xfs_mount_t *mp = ip->i_mount;
1527 xfs_trans_t *tp;
1528 xfs_bstat_t *sbp = &sxp->sx_stat;
1529 xfs_ifork_t *tempifp, *ifp, *tifp;
1530 int src_log_flags, target_log_flags;
1531 int error = 0;
1532 int aforkblks = 0;
1533 int taforkblks = 0;
1534 __uint64_t tmp;
Dave Chinner81217682014-08-04 13:29:32 +10001535 int lock_flags;
Dave Chinnera133d952013-08-12 20:49:48 +10001536
Dave Chinnera133d952013-08-12 20:49:48 +10001537 tempifp = kmem_alloc(sizeof(xfs_ifork_t), KM_MAYFAIL);
1538 if (!tempifp) {
Dave Chinner24513372014-06-25 14:58:08 +10001539 error = -ENOMEM;
Dave Chinnera133d952013-08-12 20:49:48 +10001540 goto out;
1541 }
1542
1543 /*
Dave Chinner723cac42015-02-23 21:47:29 +11001544 * Lock the inodes against other IO, page faults and truncate to
1545 * begin with. Then we can ensure the inodes are flushed and have no
1546 * page cache safely. Once we have done this we can take the ilocks and
1547 * do the rest of the checks.
Dave Chinnera133d952013-08-12 20:49:48 +10001548 */
Dave Chinner723cac42015-02-23 21:47:29 +11001549 lock_flags = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL;
Dave Chinnera133d952013-08-12 20:49:48 +10001550 xfs_lock_two_inodes(ip, tip, XFS_IOLOCK_EXCL);
Dave Chinner723cac42015-02-23 21:47:29 +11001551 xfs_lock_two_inodes(ip, tip, XFS_MMAPLOCK_EXCL);
Dave Chinnera133d952013-08-12 20:49:48 +10001552
1553 /* Verify that both files have the same format */
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001554 if ((VFS_I(ip)->i_mode & S_IFMT) != (VFS_I(tip)->i_mode & S_IFMT)) {
Dave Chinner24513372014-06-25 14:58:08 +10001555 error = -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001556 goto out_unlock;
1557 }
1558
1559 /* Verify both files are either real-time or non-realtime */
1560 if (XFS_IS_REALTIME_INODE(ip) != XFS_IS_REALTIME_INODE(tip)) {
Dave Chinner24513372014-06-25 14:58:08 +10001561 error = -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001562 goto out_unlock;
1563 }
1564
Dave Chinner4ef897a2014-08-04 13:44:08 +10001565 error = xfs_swap_extent_flush(ip);
Dave Chinnera133d952013-08-12 20:49:48 +10001566 if (error)
1567 goto out_unlock;
Dave Chinner4ef897a2014-08-04 13:44:08 +10001568 error = xfs_swap_extent_flush(tip);
1569 if (error)
1570 goto out_unlock;
Dave Chinnera133d952013-08-12 20:49:48 +10001571
Christoph Hellwig253f4912016-04-06 09:19:55 +10001572 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
1573 if (error)
Dave Chinnera133d952013-08-12 20:49:48 +10001574 goto out_unlock;
Dave Chinner723cac42015-02-23 21:47:29 +11001575
1576 /*
1577 * Lock and join the inodes to the tansaction so that transaction commit
1578 * or cancel will unlock the inodes from this point onwards.
1579 */
Dave Chinner4ef897a2014-08-04 13:44:08 +10001580 xfs_lock_two_inodes(ip, tip, XFS_ILOCK_EXCL);
1581 lock_flags |= XFS_ILOCK_EXCL;
Dave Chinner723cac42015-02-23 21:47:29 +11001582 xfs_trans_ijoin(tp, ip, lock_flags);
1583 xfs_trans_ijoin(tp, tip, lock_flags);
1584
Dave Chinnera133d952013-08-12 20:49:48 +10001585
1586 /* Verify all data are being swapped */
1587 if (sxp->sx_offset != 0 ||
1588 sxp->sx_length != ip->i_d.di_size ||
1589 sxp->sx_length != tip->i_d.di_size) {
Dave Chinner24513372014-06-25 14:58:08 +10001590 error = -EFAULT;
Dave Chinner4ef897a2014-08-04 13:44:08 +10001591 goto out_trans_cancel;
Dave Chinnera133d952013-08-12 20:49:48 +10001592 }
1593
1594 trace_xfs_swap_extent_before(ip, 0);
1595 trace_xfs_swap_extent_before(tip, 1);
1596
1597 /* check inode formats now that data is flushed */
1598 error = xfs_swap_extents_check_format(ip, tip);
1599 if (error) {
1600 xfs_notice(mp,
1601 "%s: inode 0x%llx format is incompatible for exchanging.",
1602 __func__, ip->i_ino);
Dave Chinner4ef897a2014-08-04 13:44:08 +10001603 goto out_trans_cancel;
Dave Chinnera133d952013-08-12 20:49:48 +10001604 }
1605
1606 /*
1607 * Compare the current change & modify times with that
1608 * passed in. If they differ, we abort this swap.
1609 * This is the mechanism used to ensure the calling
1610 * process that the file was not changed out from
1611 * under it.
1612 */
1613 if ((sbp->bs_ctime.tv_sec != VFS_I(ip)->i_ctime.tv_sec) ||
1614 (sbp->bs_ctime.tv_nsec != VFS_I(ip)->i_ctime.tv_nsec) ||
1615 (sbp->bs_mtime.tv_sec != VFS_I(ip)->i_mtime.tv_sec) ||
1616 (sbp->bs_mtime.tv_nsec != VFS_I(ip)->i_mtime.tv_nsec)) {
Dave Chinner24513372014-06-25 14:58:08 +10001617 error = -EBUSY;
Dave Chinner81217682014-08-04 13:29:32 +10001618 goto out_trans_cancel;
Dave Chinnera133d952013-08-12 20:49:48 +10001619 }
Dave Chinnera133d952013-08-12 20:49:48 +10001620 /*
1621 * Count the number of extended attribute blocks
1622 */
1623 if ( ((XFS_IFORK_Q(ip) != 0) && (ip->i_d.di_anextents > 0)) &&
1624 (ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)) {
1625 error = xfs_bmap_count_blocks(tp, ip, XFS_ATTR_FORK, &aforkblks);
1626 if (error)
1627 goto out_trans_cancel;
1628 }
1629 if ( ((XFS_IFORK_Q(tip) != 0) && (tip->i_d.di_anextents > 0)) &&
1630 (tip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)) {
1631 error = xfs_bmap_count_blocks(tp, tip, XFS_ATTR_FORK,
1632 &taforkblks);
1633 if (error)
1634 goto out_trans_cancel;
1635 }
1636
Dave Chinner21b5c972013-08-30 10:23:44 +10001637 /*
1638 * Before we've swapped the forks, lets set the owners of the forks
1639 * appropriately. We have to do this as we are demand paging the btree
1640 * buffers, and so the validation done on read will expect the owner
1641 * field to be correctly set. Once we change the owners, we can swap the
1642 * inode forks.
1643 *
1644 * Note the trickiness in setting the log flags - we set the owner log
1645 * flag on the opposite inode (i.e. the inode we are setting the new
1646 * owner to be) because once we swap the forks and log that, log
1647 * recovery is going to see the fork as owned by the swapped inode,
1648 * not the pre-swapped inodes.
1649 */
1650 src_log_flags = XFS_ILOG_CORE;
1651 target_log_flags = XFS_ILOG_CORE;
1652 if (ip->i_d.di_version == 3 &&
1653 ip->i_d.di_format == XFS_DINODE_FMT_BTREE) {
Dave Chinner638f44162013-08-30 10:23:45 +10001654 target_log_flags |= XFS_ILOG_DOWNER;
1655 error = xfs_bmbt_change_owner(tp, ip, XFS_DATA_FORK,
1656 tip->i_ino, NULL);
Dave Chinner21b5c972013-08-30 10:23:44 +10001657 if (error)
1658 goto out_trans_cancel;
1659 }
1660
1661 if (tip->i_d.di_version == 3 &&
1662 tip->i_d.di_format == XFS_DINODE_FMT_BTREE) {
Dave Chinner638f44162013-08-30 10:23:45 +10001663 src_log_flags |= XFS_ILOG_DOWNER;
1664 error = xfs_bmbt_change_owner(tp, tip, XFS_DATA_FORK,
1665 ip->i_ino, NULL);
Dave Chinner21b5c972013-08-30 10:23:44 +10001666 if (error)
1667 goto out_trans_cancel;
1668 }
1669
Dave Chinnera133d952013-08-12 20:49:48 +10001670 /*
1671 * Swap the data forks of the inodes
1672 */
1673 ifp = &ip->i_df;
1674 tifp = &tip->i_df;
1675 *tempifp = *ifp; /* struct copy */
1676 *ifp = *tifp; /* struct copy */
1677 *tifp = *tempifp; /* struct copy */
1678
1679 /*
1680 * Fix the on-disk inode values
1681 */
1682 tmp = (__uint64_t)ip->i_d.di_nblocks;
1683 ip->i_d.di_nblocks = tip->i_d.di_nblocks - taforkblks + aforkblks;
1684 tip->i_d.di_nblocks = tmp + taforkblks - aforkblks;
1685
1686 tmp = (__uint64_t) ip->i_d.di_nextents;
1687 ip->i_d.di_nextents = tip->i_d.di_nextents;
1688 tip->i_d.di_nextents = tmp;
1689
1690 tmp = (__uint64_t) ip->i_d.di_format;
1691 ip->i_d.di_format = tip->i_d.di_format;
1692 tip->i_d.di_format = tmp;
1693
1694 /*
1695 * The extents in the source inode could still contain speculative
1696 * preallocation beyond EOF (e.g. the file is open but not modified
1697 * while defrag is in progress). In that case, we need to copy over the
1698 * number of delalloc blocks the data fork in the source inode is
1699 * tracking beyond EOF so that when the fork is truncated away when the
1700 * temporary inode is unlinked we don't underrun the i_delayed_blks
1701 * counter on that inode.
1702 */
1703 ASSERT(tip->i_delayed_blks == 0);
1704 tip->i_delayed_blks = ip->i_delayed_blks;
1705 ip->i_delayed_blks = 0;
1706
Dave Chinnera133d952013-08-12 20:49:48 +10001707 switch (ip->i_d.di_format) {
1708 case XFS_DINODE_FMT_EXTENTS:
1709 /* If the extents fit in the inode, fix the
1710 * pointer. Otherwise it's already NULL or
1711 * pointing to the extent.
1712 */
1713 if (ip->i_d.di_nextents <= XFS_INLINE_EXTS) {
1714 ifp->if_u1.if_extents =
1715 ifp->if_u2.if_inline_ext;
1716 }
1717 src_log_flags |= XFS_ILOG_DEXT;
1718 break;
1719 case XFS_DINODE_FMT_BTREE:
Dave Chinner21b5c972013-08-30 10:23:44 +10001720 ASSERT(ip->i_d.di_version < 3 ||
Dave Chinner638f44162013-08-30 10:23:45 +10001721 (src_log_flags & XFS_ILOG_DOWNER));
Dave Chinnera133d952013-08-12 20:49:48 +10001722 src_log_flags |= XFS_ILOG_DBROOT;
1723 break;
1724 }
1725
Dave Chinnera133d952013-08-12 20:49:48 +10001726 switch (tip->i_d.di_format) {
1727 case XFS_DINODE_FMT_EXTENTS:
1728 /* If the extents fit in the inode, fix the
1729 * pointer. Otherwise it's already NULL or
1730 * pointing to the extent.
1731 */
1732 if (tip->i_d.di_nextents <= XFS_INLINE_EXTS) {
1733 tifp->if_u1.if_extents =
1734 tifp->if_u2.if_inline_ext;
1735 }
1736 target_log_flags |= XFS_ILOG_DEXT;
1737 break;
1738 case XFS_DINODE_FMT_BTREE:
1739 target_log_flags |= XFS_ILOG_DBROOT;
Dave Chinner21b5c972013-08-30 10:23:44 +10001740 ASSERT(tip->i_d.di_version < 3 ||
Dave Chinner638f44162013-08-30 10:23:45 +10001741 (target_log_flags & XFS_ILOG_DOWNER));
Dave Chinnera133d952013-08-12 20:49:48 +10001742 break;
1743 }
1744
Dave Chinnera133d952013-08-12 20:49:48 +10001745 xfs_trans_log_inode(tp, ip, src_log_flags);
1746 xfs_trans_log_inode(tp, tip, target_log_flags);
1747
1748 /*
1749 * If this is a synchronous mount, make sure that the
1750 * transaction goes to disk before returning to the user.
1751 */
1752 if (mp->m_flags & XFS_MOUNT_WSYNC)
1753 xfs_trans_set_sync(tp);
1754
Christoph Hellwig70393312015-06-04 13:48:08 +10001755 error = xfs_trans_commit(tp);
Dave Chinnera133d952013-08-12 20:49:48 +10001756
1757 trace_xfs_swap_extent_after(ip, 0);
1758 trace_xfs_swap_extent_after(tip, 1);
1759out:
1760 kmem_free(tempifp);
1761 return error;
1762
1763out_unlock:
Dave Chinner81217682014-08-04 13:29:32 +10001764 xfs_iunlock(ip, lock_flags);
1765 xfs_iunlock(tip, lock_flags);
Dave Chinnera133d952013-08-12 20:49:48 +10001766 goto out;
1767
1768out_trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001769 xfs_trans_cancel(tp);
Dave Chinner723cac42015-02-23 21:47:29 +11001770 goto out;
Dave Chinnera133d952013-08-12 20:49:48 +10001771}