]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - fs/xfs/xfs_dfrag.c
jfs: fix error path in ialloc
[mirror_ubuntu-eoan-kernel.git] / fs / xfs / xfs_dfrag.c
CommitLineData
1da177e4 1/*
3e57ecf6 2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
7b718769 3 * All Rights Reserved.
1da177e4 4 *
7b718769
NS
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
1da177e4
LT
7 * published by the Free Software Foundation.
8 *
7b718769
NS
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
1da177e4 13 *
7b718769
NS
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1da177e4 17 */
1da177e4 18#include "xfs.h"
a844f451 19#include "xfs_fs.h"
1da177e4 20#include "xfs_types.h"
1da177e4
LT
21#include "xfs_log.h"
22#include "xfs_trans.h"
23#include "xfs_sb.h"
a844f451 24#include "xfs_ag.h"
1da177e4 25#include "xfs_mount.h"
1da177e4 26#include "xfs_bmap_btree.h"
427d9fe2
ES
27#include "xfs_alloc_btree.h"
28#include "xfs_ialloc_btree.h"
29#include "xfs_btree.h"
1da177e4 30#include "xfs_dinode.h"
1da177e4 31#include "xfs_inode.h"
a844f451 32#include "xfs_inode_item.h"
1da177e4 33#include "xfs_bmap.h"
1da177e4
LT
34#include "xfs_itable.h"
35#include "xfs_dfrag.h"
36#include "xfs_error.h"
739bfb2a 37#include "xfs_vnodeops.h"
0b1b213f 38#include "xfs_trace.h"
1da177e4 39
6bded0f3
DC
40
41static int xfs_swap_extents(
42 xfs_inode_t *ip, /* target inode */
43 xfs_inode_t *tip, /* tmp inode */
44 xfs_swapext_t *sxp);
45
1da177e4 46/*
6bded0f3 47 * ioctl interface for swapext
1da177e4
LT
48 */
49int
50xfs_swapext(
743bb465 51 xfs_swapext_t *sxp)
1da177e4 52{
35fec8df 53 xfs_inode_t *ip, *tip;
2903ff01
AV
54 struct fd f, tmp;
55 int error = 0;
1da177e4 56
1da177e4 57 /* Pull information for the target fd */
2903ff01
AV
58 f = fdget((int)sxp->sx_fdtarget);
59 if (!f.file) {
1da177e4 60 error = XFS_ERROR(EINVAL);
ac12b4e2 61 goto out;
1da177e4
LT
62 }
63
2903ff01
AV
64 if (!(f.file->f_mode & FMODE_WRITE) ||
65 !(f.file->f_mode & FMODE_READ) ||
66 (f.file->f_flags & O_APPEND)) {
f6aa7f21
CH
67 error = XFS_ERROR(EBADF);
68 goto out_put_file;
69 }
70
2903ff01
AV
71 tmp = fdget((int)sxp->sx_fdtmp);
72 if (!tmp.file) {
1da177e4 73 error = XFS_ERROR(EINVAL);
35fec8df 74 goto out_put_file;
1da177e4
LT
75 }
76
2903ff01
AV
77 if (!(tmp.file->f_mode & FMODE_WRITE) ||
78 !(tmp.file->f_mode & FMODE_READ) ||
79 (tmp.file->f_flags & O_APPEND)) {
f6aa7f21 80 error = XFS_ERROR(EBADF);
6bded0f3 81 goto out_put_tmp_file;
f6aa7f21
CH
82 }
83
496ad9aa
AV
84 if (IS_SWAPFILE(file_inode(f.file)) ||
85 IS_SWAPFILE(file_inode(tmp.file))) {
7c8f7af6 86 error = XFS_ERROR(EINVAL);
6bded0f3 87 goto out_put_tmp_file;
7c8f7af6
CH
88 }
89
496ad9aa
AV
90 ip = XFS_I(file_inode(f.file));
91 tip = XFS_I(file_inode(tmp.file));
1da177e4
LT
92
93 if (ip->i_mount != tip->i_mount) {
35fec8df 94 error = XFS_ERROR(EINVAL);
6bded0f3 95 goto out_put_tmp_file;
1da177e4
LT
96 }
97
98 if (ip->i_ino == tip->i_ino) {
35fec8df 99 error = XFS_ERROR(EINVAL);
6bded0f3 100 goto out_put_tmp_file;
1da177e4
LT
101 }
102
35fec8df
CH
103 if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
104 error = XFS_ERROR(EIO);
6bded0f3 105 goto out_put_tmp_file;
1da177e4
LT
106 }
107
541d7d3c 108 error = xfs_swap_extents(ip, tip, sxp);
3e57ecf6 109
6bded0f3 110 out_put_tmp_file:
2903ff01 111 fdput(tmp);
35fec8df 112 out_put_file:
2903ff01 113 fdput(f);
35fec8df 114 out:
3e57ecf6
OW
115 return error;
116}
117
e09f9860
DC
118/*
119 * We need to check that the format of the data fork in the temporary inode is
120 * valid for the target inode before doing the swap. This is not a problem with
121 * attr1 because of the fixed fork offset, but attr2 has a dynamically sized
122 * data fork depending on the space the attribute fork is taking so we can get
123 * invalid formats on the target inode.
124 *
125 * E.g. target has space for 7 extents in extent format, temp inode only has
126 * space for 6. If we defragment down to 7 extents, then the tmp format is a
127 * btree, but when swapped it needs to be in extent format. Hence we can't just
128 * blindly swap data forks on attr2 filesystems.
129 *
130 * Note that we check the swap in both directions so that we don't end up with
131 * a corrupt temporary inode, either.
132 *
133 * Note that fixing the way xfs_fsr sets up the attribute fork in the source
134 * inode will prevent this situation from occurring, so all we do here is
135 * reject and log the attempt. basically we are putting the responsibility on
136 * userspace to get this right.
137 */
138static int
139xfs_swap_extents_check_format(
140 xfs_inode_t *ip, /* target inode */
141 xfs_inode_t *tip) /* tmp inode */
142{
143
144 /* Should never get a local format */
145 if (ip->i_d.di_format == XFS_DINODE_FMT_LOCAL ||
146 tip->i_d.di_format == XFS_DINODE_FMT_LOCAL)
147 return EINVAL;
148
149 /*
150 * if the target inode has less extents that then temporary inode then
151 * why did userspace call us?
152 */
153 if (ip->i_d.di_nextents < tip->i_d.di_nextents)
154 return EINVAL;
155
156 /*
157 * if the target inode is in extent form and the temp inode is in btree
158 * form then we will end up with the target inode in the wrong format
159 * as we already know there are less extents in the temp inode.
160 */
161 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
162 tip->i_d.di_format == XFS_DINODE_FMT_BTREE)
163 return EINVAL;
164
165 /* Check temp in extent form to max in target */
166 if (tip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
8096b1eb
CH
167 XFS_IFORK_NEXTENTS(tip, XFS_DATA_FORK) >
168 XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK))
e09f9860
DC
169 return EINVAL;
170
171 /* Check target in extent form to max in temp */
172 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
8096b1eb
CH
173 XFS_IFORK_NEXTENTS(ip, XFS_DATA_FORK) >
174 XFS_IFORK_MAXEXT(tip, XFS_DATA_FORK))
e09f9860
DC
175 return EINVAL;
176
dd77ef92
DC
177 /*
178 * If we are in a btree format, check that the temp root block will fit
179 * in the target and that it has enough extents to be in btree format
180 * in the target.
181 *
182 * Note that we have to be careful to allow btree->extent conversions
183 * (a common defrag case) which will occur when the temp inode is in
184 * extent format...
185 */
8096b1eb
CH
186 if (tip->i_d.di_format == XFS_DINODE_FMT_BTREE) {
187 if (XFS_IFORK_BOFF(ip) &&
427d9fe2 188 XFS_BMAP_BMDR_SPACE(tip->i_df.if_broot) > XFS_IFORK_BOFF(ip))
8096b1eb
CH
189 return EINVAL;
190 if (XFS_IFORK_NEXTENTS(tip, XFS_DATA_FORK) <=
191 XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK))
192 return EINVAL;
193 }
e09f9860 194
dd77ef92 195 /* Reciprocal target->temp btree format checks */
8096b1eb
CH
196 if (ip->i_d.di_format == XFS_DINODE_FMT_BTREE) {
197 if (XFS_IFORK_BOFF(tip) &&
427d9fe2 198 XFS_BMAP_BMDR_SPACE(ip->i_df.if_broot) > XFS_IFORK_BOFF(tip))
8096b1eb 199 return EINVAL;
8096b1eb
CH
200 if (XFS_IFORK_NEXTENTS(ip, XFS_DATA_FORK) <=
201 XFS_IFORK_MAXEXT(tip, XFS_DATA_FORK))
202 return EINVAL;
203 }
e09f9860
DC
204
205 return 0;
206}
207
6bded0f3 208static int
3e57ecf6 209xfs_swap_extents(
e09f9860
DC
210 xfs_inode_t *ip, /* target inode */
211 xfs_inode_t *tip, /* tmp inode */
3e57ecf6
OW
212 xfs_swapext_t *sxp)
213{
45c51b99 214 xfs_mount_t *mp = ip->i_mount;
3e57ecf6
OW
215 xfs_trans_t *tp;
216 xfs_bstat_t *sbp = &sxp->sx_stat;
3e57ecf6 217 xfs_ifork_t *tempifp, *ifp, *tifp;
f5d8d5c4 218 int src_log_flags, target_log_flags;
3e57ecf6
OW
219 int error = 0;
220 int aforkblks = 0;
221 int taforkblks = 0;
222 __uint64_t tmp;
3e57ecf6 223
02f75405
DC
224 /*
225 * We have no way of updating owner information in the BMBT blocks for
226 * each inode on CRC enabled filesystems, so to avoid corrupting the
227 * this metadata we simply don't allow extent swaps to occur.
228 */
229 if (xfs_sb_version_hascrc(&mp->m_sb))
230 return XFS_ERROR(EINVAL);
231
3e57ecf6
OW
232 tempifp = kmem_alloc(sizeof(xfs_ifork_t), KM_MAYFAIL);
233 if (!tempifp) {
234 error = XFS_ERROR(ENOMEM);
ef8f7fc5 235 goto out;
3e57ecf6
OW
236 }
237
f9114eba
DC
238 /*
239 * we have to do two separate lock calls here to keep lockdep
240 * happy. If we try to get all the locks in one call, lock will
241 * report false positives when we drop the ILOCK and regain them
242 * below.
243 */
244 xfs_lock_two_inodes(ip, tip, XFS_IOLOCK_EXCL);
245 xfs_lock_two_inodes(ip, tip, XFS_ILOCK_EXCL);
1da177e4 246
1da177e4
LT
247 /* Verify that both files have the same format */
248 if ((ip->i_d.di_mode & S_IFMT) != (tip->i_d.di_mode & S_IFMT)) {
249 error = XFS_ERROR(EINVAL);
ef8f7fc5 250 goto out_unlock;
1da177e4
LT
251 }
252
253 /* Verify both files are either real-time or non-realtime */
71ddabb9 254 if (XFS_IS_REALTIME_INODE(ip) != XFS_IS_REALTIME_INODE(tip)) {
1da177e4 255 error = XFS_ERROR(EINVAL);
ef8f7fc5 256 goto out_unlock;
1da177e4
LT
257 }
258
65e3aa77 259 error = -filemap_write_and_wait(VFS_I(tip)->i_mapping);
fb595814
DC
260 if (error)
261 goto out_unlock;
65e3aa77 262 truncate_pagecache_range(VFS_I(tip), 0, -1);
1da177e4
LT
263
264 /* Verify O_DIRECT for ftmp */
df80c933 265 if (VN_CACHED(VFS_I(tip)) != 0) {
1da177e4 266 error = XFS_ERROR(EINVAL);
ef8f7fc5 267 goto out_unlock;
1da177e4
LT
268 }
269
270 /* Verify all data are being swapped */
d0cfb373
ES
271 if (sxp->sx_offset != 0 ||
272 sxp->sx_length != ip->i_d.di_size ||
273 sxp->sx_length != tip->i_d.di_size) {
1da177e4 274 error = XFS_ERROR(EFAULT);
ef8f7fc5 275 goto out_unlock;
1da177e4
LT
276 }
277
3a85cd96
DC
278 trace_xfs_swap_extent_before(ip, 0);
279 trace_xfs_swap_extent_before(tip, 1);
280
e09f9860
DC
281 /* check inode formats now that data is flushed */
282 error = xfs_swap_extents_check_format(ip, tip);
283 if (error) {
53487786 284 xfs_notice(mp,
e09f9860 285 "%s: inode 0x%llx format is incompatible for exchanging.",
53487786 286 __func__, ip->i_ino);
ef8f7fc5 287 goto out_unlock;
1da177e4
LT
288 }
289
290 /*
291 * Compare the current change & modify times with that
292 * passed in. If they differ, we abort this swap.
293 * This is the mechanism used to ensure the calling
294 * process that the file was not changed out from
295 * under it.
296 */
f9581b14
CH
297 if ((sbp->bs_ctime.tv_sec != VFS_I(ip)->i_ctime.tv_sec) ||
298 (sbp->bs_ctime.tv_nsec != VFS_I(ip)->i_ctime.tv_nsec) ||
299 (sbp->bs_mtime.tv_sec != VFS_I(ip)->i_mtime.tv_sec) ||
300 (sbp->bs_mtime.tv_nsec != VFS_I(ip)->i_mtime.tv_nsec)) {
1da177e4 301 error = XFS_ERROR(EBUSY);
ef8f7fc5 302 goto out_unlock;
1da177e4
LT
303 }
304
305 /* We need to fail if the file is memory mapped. Once we have tossed
306 * all existing pages, the page fault will have no option
307 * but to go to the filesystem for pages. By making the page fault call
67fcaa73 308 * vop_read (or write in the case of autogrow) they block on the iolock
1da177e4
LT
309 * until we have switched the extents.
310 */
df80c933 311 if (VN_MAPPED(VFS_I(ip))) {
1da177e4 312 error = XFS_ERROR(EBUSY);
ef8f7fc5 313 goto out_unlock;
1da177e4
LT
314 }
315
316 xfs_iunlock(ip, XFS_ILOCK_EXCL);
317 xfs_iunlock(tip, XFS_ILOCK_EXCL);
318
319 /*
320 * There is a race condition here since we gave up the
321 * ilock. However, the data fork will not change since
322 * we have the iolock (locked for truncation too) so we
323 * are safe. We don't really care if non-io related
324 * fields change.
325 */
f5b8911b 326 truncate_pagecache_range(VFS_I(ip), 0, -1);
1da177e4
LT
327
328 tp = xfs_trans_alloc(mp, XFS_TRANS_SWAPEXT);
329 if ((error = xfs_trans_reserve(tp, 0,
330 XFS_ICHANGE_LOG_RES(mp), 0,
331 0, 0))) {
332 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
333 xfs_iunlock(tip, XFS_IOLOCK_EXCL);
334 xfs_trans_cancel(tp, 0);
ef8f7fc5 335 goto out;
1da177e4 336 }
e1cccd91 337 xfs_lock_two_inodes(ip, tip, XFS_ILOCK_EXCL);
1da177e4
LT
338
339 /*
340 * Count the number of extended attribute blocks
341 */
342 if ( ((XFS_IFORK_Q(ip) != 0) && (ip->i_d.di_anextents > 0)) &&
343 (ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)) {
344 error = xfs_bmap_count_blocks(tp, ip, XFS_ATTR_FORK, &aforkblks);
ef8f7fc5
JJS
345 if (error)
346 goto out_trans_cancel;
1da177e4
LT
347 }
348 if ( ((XFS_IFORK_Q(tip) != 0) && (tip->i_d.di_anextents > 0)) &&
349 (tip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)) {
350 error = xfs_bmap_count_blocks(tp, tip, XFS_ATTR_FORK,
351 &taforkblks);
ef8f7fc5
JJS
352 if (error)
353 goto out_trans_cancel;
1da177e4
LT
354 }
355
356 /*
357 * Swap the data forks of the inodes
358 */
359 ifp = &ip->i_df;
360 tifp = &tip->i_df;
d0cfb373
ES
361 *tempifp = *ifp; /* struct copy */
362 *ifp = *tifp; /* struct copy */
363 *tifp = *tempifp; /* struct copy */
1da177e4
LT
364
365 /*
366 * Fix the on-disk inode values
367 */
368 tmp = (__uint64_t)ip->i_d.di_nblocks;
369 ip->i_d.di_nblocks = tip->i_d.di_nblocks - taforkblks + aforkblks;
370 tip->i_d.di_nblocks = tmp + taforkblks - aforkblks;
371
372 tmp = (__uint64_t) ip->i_d.di_nextents;
373 ip->i_d.di_nextents = tip->i_d.di_nextents;
374 tip->i_d.di_nextents = tmp;
375
376 tmp = (__uint64_t) ip->i_d.di_format;
377 ip->i_d.di_format = tip->i_d.di_format;
378 tip->i_d.di_format = tmp;
379
309c8480
DC
380 /*
381 * The extents in the source inode could still contain speculative
382 * preallocation beyond EOF (e.g. the file is open but not modified
383 * while defrag is in progress). In that case, we need to copy over the
384 * number of delalloc blocks the data fork in the source inode is
385 * tracking beyond EOF so that when the fork is truncated away when the
386 * temporary inode is unlinked we don't underrun the i_delayed_blks
387 * counter on that inode.
388 */
389 ASSERT(tip->i_delayed_blks == 0);
390 tip->i_delayed_blks = ip->i_delayed_blks;
391 ip->i_delayed_blks = 0;
392
f5d8d5c4
CH
393 src_log_flags = XFS_ILOG_CORE;
394 switch (ip->i_d.di_format) {
1da177e4
LT
395 case XFS_DINODE_FMT_EXTENTS:
396 /* If the extents fit in the inode, fix the
397 * pointer. Otherwise it's already NULL or
398 * pointing to the extent.
399 */
400 if (ip->i_d.di_nextents <= XFS_INLINE_EXTS) {
401 ifp->if_u1.if_extents =
402 ifp->if_u2.if_inline_ext;
403 }
f5d8d5c4 404 src_log_flags |= XFS_ILOG_DEXT;
1da177e4
LT
405 break;
406 case XFS_DINODE_FMT_BTREE:
f5d8d5c4 407 src_log_flags |= XFS_ILOG_DBROOT;
1da177e4
LT
408 break;
409 }
410
f5d8d5c4
CH
411 target_log_flags = XFS_ILOG_CORE;
412 switch (tip->i_d.di_format) {
1da177e4
LT
413 case XFS_DINODE_FMT_EXTENTS:
414 /* If the extents fit in the inode, fix the
415 * pointer. Otherwise it's already NULL or
416 * pointing to the extent.
417 */
418 if (tip->i_d.di_nextents <= XFS_INLINE_EXTS) {
419 tifp->if_u1.if_extents =
420 tifp->if_u2.if_inline_ext;
421 }
f5d8d5c4 422 target_log_flags |= XFS_ILOG_DEXT;
1da177e4
LT
423 break;
424 case XFS_DINODE_FMT_BTREE:
f5d8d5c4 425 target_log_flags |= XFS_ILOG_DBROOT;
1da177e4
LT
426 break;
427 }
428
1da177e4 429
ddc3415a
CH
430 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
431 xfs_trans_ijoin(tp, tip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1da177e4 432
f5d8d5c4
CH
433 xfs_trans_log_inode(tp, ip, src_log_flags);
434 xfs_trans_log_inode(tp, tip, target_log_flags);
1da177e4
LT
435
436 /*
437 * If this is a synchronous mount, make sure that the
438 * transaction goes to disk before returning to the user.
439 */
ef8f7fc5 440 if (mp->m_flags & XFS_MOUNT_WSYNC)
1da177e4 441 xfs_trans_set_sync(tp);
1da177e4 442
815cb216 443 error = xfs_trans_commit(tp, 0);
1da177e4 444
3a85cd96
DC
445 trace_xfs_swap_extent_after(ip, 0);
446 trace_xfs_swap_extent_after(tip, 1);
ef8f7fc5
JJS
447out:
448 kmem_free(tempifp);
1da177e4 449 return error;
ef8f7fc5 450
1f23920d
FB
451out_unlock:
452 xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
453 xfs_iunlock(tip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
454 goto out;
455
ef8f7fc5
JJS
456out_trans_cancel:
457 xfs_trans_cancel(tp, 0);
458 goto out_unlock;
1da177e4 459}