blob: edb079087a0cf9c4603b306df9591007a05ad432 [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0
Dave Chinnerfde22272013-08-12 20:49:39 +10002/*
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4 * Copyright (c) 2013 Red Hat, Inc.
5 * All Rights Reserved.
Dave Chinnerfde22272013-08-12 20:49:39 +10006 */
7#include "xfs.h"
8#include "xfs_fs.h"
Dave Chinner70a98832013-10-23 10:36:05 +11009#include "xfs_shared.h"
Dave Chinner239880e2013-10-23 10:50:10 +110010#include "xfs_format.h"
11#include "xfs_log_format.h"
12#include "xfs_trans_resv.h"
Dave Chinnerfde22272013-08-12 20:49:39 +100013#include "xfs_bit.h"
Dave Chinnerfde22272013-08-12 20:49:39 +100014#include "xfs_mount.h"
Dave Chinner57062782013-10-15 09:17:51 +110015#include "xfs_da_format.h"
Dave Chinnerfde22272013-08-12 20:49:39 +100016#include "xfs_da_btree.h"
Dave Chinnerfde22272013-08-12 20:49:39 +100017#include "xfs_inode.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110018#include "xfs_attr_remote.h"
Dave Chinner239880e2013-10-23 10:50:10 +110019#include "xfs_trans.h"
Dave Chinnerfde22272013-08-12 20:49:39 +100020#include "xfs_bmap.h"
21#include "xfs_attr.h"
22#include "xfs_attr_leaf.h"
Dave Chinnerfde22272013-08-12 20:49:39 +100023#include "xfs_quota.h"
Dave Chinner4bceb182013-10-29 22:11:51 +110024#include "xfs_dir2.h"
Darrick J. Wonga5155b82019-11-02 09:40:53 -070025#include "xfs_error.h"
Dave Chinnerfde22272013-08-12 20:49:39 +100026
27/*
Darrick J. Wonge8db2aa2020-01-07 16:11:45 -080028 * Invalidate any incore buffers associated with this remote attribute value
29 * extent. We never log remote attribute value buffers, which means that they
30 * won't be attached to a transaction and are therefore safe to mark stale.
31 * The actual bunmapi will be taken care of later.
Dave Chinnerfde22272013-08-12 20:49:39 +100032 */
33STATIC int
Darrick J. Wonge8db2aa2020-01-07 16:11:45 -080034xfs_attr3_rmt_stale(
Dave Chinnerfde22272013-08-12 20:49:39 +100035 struct xfs_inode *dp,
36 xfs_dablk_t blkno,
37 int blkcnt)
38{
39 struct xfs_bmbt_irec map;
Dave Chinnerfde22272013-08-12 20:49:39 +100040 xfs_dablk_t tblkno;
Dave Chinnerfde22272013-08-12 20:49:39 +100041 int tblkcnt;
Dave Chinnerfde22272013-08-12 20:49:39 +100042 int nmap;
43 int error;
44
45 /*
46 * Roll through the "value", invalidating the attribute value's
47 * blocks.
48 */
49 tblkno = blkno;
50 tblkcnt = blkcnt;
51 while (tblkcnt > 0) {
52 /*
53 * Try to remember where we decided to put the value.
54 */
55 nmap = 1;
56 error = xfs_bmapi_read(dp, (xfs_fileoff_t)tblkno, tblkcnt,
57 &map, &nmap, XFS_BMAPI_ATTRFORK);
Darrick J. Wonge8db2aa2020-01-07 16:11:45 -080058 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +100059 return error;
Darrick J. Wonge8db2aa2020-01-07 16:11:45 -080060 if (XFS_IS_CORRUPT(dp->i_mount, nmap != 1))
61 return -EFSCORRUPTED;
Dave Chinnerfde22272013-08-12 20:49:39 +100062
63 /*
Darrick J. Wonge8db2aa2020-01-07 16:11:45 -080064 * Mark any incore buffers for the remote value as stale. We
65 * never log remote attr value buffers, so the buffer should be
66 * easy to kill.
Dave Chinnerfde22272013-08-12 20:49:39 +100067 */
Darrick J. Wonge8db2aa2020-01-07 16:11:45 -080068 error = xfs_attr_rmtval_stale(dp, &map, 0);
69 if (error)
70 return error;
Dave Chinnerfde22272013-08-12 20:49:39 +100071
72 tblkno += map.br_blockcount;
73 tblkcnt -= map.br_blockcount;
74 }
75
Eric Sandeend99831f2014-06-22 15:03:54 +100076 return 0;
Dave Chinnerfde22272013-08-12 20:49:39 +100077}
78
79/*
80 * Invalidate all of the "remote" value regions pointed to by a particular
81 * leaf block.
82 * Note that we must release the lock on the buffer so that we are not
83 * caught holding something that the logging code wants to flush to disk.
84 */
85STATIC int
86xfs_attr3_leaf_inactive(
87 struct xfs_trans **trans,
88 struct xfs_inode *dp,
89 struct xfs_buf *bp)
90{
91 struct xfs_attr_leafblock *leaf;
92 struct xfs_attr3_icleaf_hdr ichdr;
93 struct xfs_attr_leaf_entry *entry;
94 struct xfs_attr_leaf_name_remote *name_rmt;
95 struct xfs_attr_inactive_list *list;
96 struct xfs_attr_inactive_list *lp;
97 int error;
98 int count;
99 int size;
100 int tmp;
101 int i;
Christoph Hellwigdbd329f12019-06-28 19:27:29 -0700102 struct xfs_mount *mp = bp->b_mount;
Dave Chinnerfde22272013-08-12 20:49:39 +1000103
104 leaf = bp->b_addr;
Brian Foster2f661242015-04-13 11:26:02 +1000105 xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr, leaf);
Dave Chinnerfde22272013-08-12 20:49:39 +1000106
107 /*
108 * Count the number of "remote" value extents.
109 */
110 count = 0;
111 entry = xfs_attr3_leaf_entryp(leaf);
112 for (i = 0; i < ichdr.count; entry++, i++) {
113 if (be16_to_cpu(entry->nameidx) &&
114 ((entry->flags & XFS_ATTR_LOCAL) == 0)) {
115 name_rmt = xfs_attr3_leaf_name_remote(leaf, i);
116 if (name_rmt->valueblk)
117 count++;
118 }
119 }
120
121 /*
122 * If there are no "remote" values, we're done.
123 */
124 if (count == 0) {
125 xfs_trans_brelse(*trans, bp);
126 return 0;
127 }
128
129 /*
130 * Allocate storage for a list of all the "remote" value extents.
131 */
132 size = count * sizeof(xfs_attr_inactive_list_t);
Tetsuo Handa707e0dd2019-08-26 12:06:22 -0700133 list = kmem_alloc(size, 0);
Dave Chinnerfde22272013-08-12 20:49:39 +1000134
135 /*
136 * Identify each of the "remote" value extents.
137 */
138 lp = list;
139 entry = xfs_attr3_leaf_entryp(leaf);
140 for (i = 0; i < ichdr.count; entry++, i++) {
141 if (be16_to_cpu(entry->nameidx) &&
142 ((entry->flags & XFS_ATTR_LOCAL) == 0)) {
143 name_rmt = xfs_attr3_leaf_name_remote(leaf, i);
144 if (name_rmt->valueblk) {
145 lp->valueblk = be32_to_cpu(name_rmt->valueblk);
146 lp->valuelen = xfs_attr3_rmt_blocks(dp->i_mount,
147 be32_to_cpu(name_rmt->valuelen));
148 lp++;
149 }
150 }
151 }
152 xfs_trans_brelse(*trans, bp); /* unlock for trans. in freextent() */
153
154 /*
155 * Invalidate each of the "remote" value extents.
156 */
157 error = 0;
158 for (lp = list, i = 0; i < count; i++, lp++) {
Darrick J. Wonge8db2aa2020-01-07 16:11:45 -0800159 tmp = xfs_attr3_rmt_stale(dp, lp->valueblk, lp->valuelen);
Dave Chinnerfde22272013-08-12 20:49:39 +1000160 if (error == 0)
161 error = tmp; /* save only the 1st errno */
162 }
163
164 kmem_free(list);
165 return error;
166}
167
168/*
169 * Recurse (gasp!) through the attribute nodes until we find leaves.
170 * We're doing a depth-first traversal in order to invalidate everything.
171 */
172STATIC int
173xfs_attr3_node_inactive(
Christoph Hellwig51908ca2019-11-08 14:57:48 -0800174 struct xfs_trans **trans,
175 struct xfs_inode *dp,
176 struct xfs_buf *bp,
177 int level)
Dave Chinnerfde22272013-08-12 20:49:39 +1000178{
Christoph Hellwig2911edb2019-11-20 09:46:05 -0800179 struct xfs_mount *mp = dp->i_mount;
Christoph Hellwig51908ca2019-11-08 14:57:48 -0800180 struct xfs_da_blkinfo *info;
181 xfs_dablk_t child_fsb;
182 xfs_daddr_t parent_blkno, child_blkno;
183 struct xfs_buf *child_bp;
Dave Chinnerfde22272013-08-12 20:49:39 +1000184 struct xfs_da3_icnode_hdr ichdr;
Christoph Hellwig51908ca2019-11-08 14:57:48 -0800185 int error, i;
Dave Chinnerfde22272013-08-12 20:49:39 +1000186
187 /*
188 * Since this code is recursive (gasp!) we must protect ourselves.
189 */
190 if (level > XFS_DA_NODE_MAXDEPTH) {
191 xfs_trans_brelse(*trans, bp); /* no locks for later trans */
Darrick J. Wonga5155b82019-11-02 09:40:53 -0700192 xfs_buf_corruption_error(bp);
Darrick J. Wongc2414ad2019-10-28 16:12:34 -0700193 return -EFSCORRUPTED;
Dave Chinnerfde22272013-08-12 20:49:39 +1000194 }
195
Christoph Hellwig51908ca2019-11-08 14:57:48 -0800196 xfs_da3_node_hdr_from_disk(dp->i_mount, &ichdr, bp->b_addr);
Dave Chinnerfde22272013-08-12 20:49:39 +1000197 parent_blkno = bp->b_bn;
198 if (!ichdr.count) {
199 xfs_trans_brelse(*trans, bp);
200 return 0;
201 }
Christoph Hellwig51908ca2019-11-08 14:57:48 -0800202 child_fsb = be32_to_cpu(ichdr.btree[0].before);
Dave Chinnerfde22272013-08-12 20:49:39 +1000203 xfs_trans_brelse(*trans, bp); /* no locks for later trans */
204
205 /*
206 * If this is the node level just above the leaves, simply loop
207 * over the leaves removing all of them. If this is higher up
208 * in the tree, recurse downward.
209 */
210 for (i = 0; i < ichdr.count; i++) {
211 /*
212 * Read the subsidiary block to see what we have to work with.
213 * Don't do this in a transaction. This is a depth-first
214 * traversal of the tree so we may deal with many blocks
215 * before we come back to this one.
216 */
Christoph Hellwig02c57f02019-11-20 09:46:04 -0800217 error = xfs_da3_node_read(*trans, dp, child_fsb, &child_bp,
Brian Fostera53efbd2017-10-17 14:16:28 -0700218 XFS_ATTR_FORK);
Dave Chinnerfde22272013-08-12 20:49:39 +1000219 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000220 return error;
Dave Chinnerfde22272013-08-12 20:49:39 +1000221
Brian Fostera53efbd2017-10-17 14:16:28 -0700222 /* save for re-read later */
223 child_blkno = XFS_BUF_ADDR(child_bp);
Dave Chinnerfde22272013-08-12 20:49:39 +1000224
Brian Fostera53efbd2017-10-17 14:16:28 -0700225 /*
226 * Invalidate the subtree, however we have to.
227 */
228 info = child_bp->b_addr;
229 switch (info->magic) {
230 case cpu_to_be16(XFS_DA_NODE_MAGIC):
231 case cpu_to_be16(XFS_DA3_NODE_MAGIC):
232 error = xfs_attr3_node_inactive(trans, dp, child_bp,
233 level + 1);
234 break;
235 case cpu_to_be16(XFS_ATTR_LEAF_MAGIC):
236 case cpu_to_be16(XFS_ATTR3_LEAF_MAGIC):
237 error = xfs_attr3_leaf_inactive(trans, dp, child_bp);
238 break;
239 default:
Darrick J. Wonga5155b82019-11-02 09:40:53 -0700240 xfs_buf_corruption_error(child_bp);
Brian Fostera53efbd2017-10-17 14:16:28 -0700241 xfs_trans_brelse(*trans, child_bp);
Darrick J. Wonga5155b82019-11-02 09:40:53 -0700242 error = -EFSCORRUPTED;
Brian Fostera53efbd2017-10-17 14:16:28 -0700243 break;
Dave Chinnerfde22272013-08-12 20:49:39 +1000244 }
Brian Fostera53efbd2017-10-17 14:16:28 -0700245 if (error)
246 return error;
247
248 /*
249 * Remove the subsidiary block from the cache and from the log.
250 */
Christoph Hellwig2911edb2019-11-20 09:46:05 -0800251 child_bp = xfs_trans_get_buf(*trans, mp->m_ddev_targp,
252 child_blkno,
253 XFS_FSB_TO_BB(mp, mp->m_attr_geo->fsbcount), 0);
254 if (!child_bp)
255 return -EIO;
256 error = bp->b_error;
257 if (error) {
258 xfs_trans_brelse(*trans, child_bp);
Brian Fostera53efbd2017-10-17 14:16:28 -0700259 return error;
Christoph Hellwig2911edb2019-11-20 09:46:05 -0800260 }
Brian Fostera53efbd2017-10-17 14:16:28 -0700261 xfs_trans_binval(*trans, child_bp);
Dave Chinnerfde22272013-08-12 20:49:39 +1000262
263 /*
264 * If we're not done, re-read the parent to get the next
265 * child block number.
266 */
267 if (i + 1 < ichdr.count) {
Christoph Hellwig51908ca2019-11-08 14:57:48 -0800268 struct xfs_da3_icnode_hdr phdr;
269
Christoph Hellwig02c57f02019-11-20 09:46:04 -0800270 error = xfs_da3_node_read_mapped(*trans, dp,
271 parent_blkno, &bp, XFS_ATTR_FORK);
Dave Chinnerfde22272013-08-12 20:49:39 +1000272 if (error)
273 return error;
Christoph Hellwig51908ca2019-11-08 14:57:48 -0800274 xfs_da3_node_hdr_from_disk(dp->i_mount, &phdr,
275 bp->b_addr);
276 child_fsb = be32_to_cpu(phdr.btree[i + 1].before);
Dave Chinnerfde22272013-08-12 20:49:39 +1000277 xfs_trans_brelse(*trans, bp);
278 }
279 /*
280 * Atomically commit the whole invalidate stuff.
281 */
Christoph Hellwig411350d2017-08-28 10:21:03 -0700282 error = xfs_trans_roll_inode(trans, dp);
Dave Chinnerfde22272013-08-12 20:49:39 +1000283 if (error)
284 return error;
285 }
286
287 return 0;
288}
289
290/*
291 * Indiscriminately delete the entire attribute fork
292 *
293 * Recurse (gasp!) through the attribute nodes until we find leaves.
294 * We're doing a depth-first traversal in order to invalidate everything.
295 */
Eric Sandeen0d5a75e2016-06-01 17:38:15 +1000296static int
Dave Chinnerfde22272013-08-12 20:49:39 +1000297xfs_attr3_root_inactive(
298 struct xfs_trans **trans,
299 struct xfs_inode *dp)
300{
Christoph Hellwig2911edb2019-11-20 09:46:05 -0800301 struct xfs_mount *mp = dp->i_mount;
Dave Chinnerfde22272013-08-12 20:49:39 +1000302 struct xfs_da_blkinfo *info;
303 struct xfs_buf *bp;
304 xfs_daddr_t blkno;
305 int error;
306
307 /*
308 * Read block 0 to see what we have to work with.
309 * We only get here if we have extents, since we remove
310 * the extents in reverse order the extent containing
311 * block 0 must still be there.
312 */
Christoph Hellwig02c57f02019-11-20 09:46:04 -0800313 error = xfs_da3_node_read(*trans, dp, 0, &bp, XFS_ATTR_FORK);
Dave Chinnerfde22272013-08-12 20:49:39 +1000314 if (error)
315 return error;
316 blkno = bp->b_bn;
317
318 /*
319 * Invalidate the tree, even if the "tree" is only a single leaf block.
320 * This is a depth-first traversal!
321 */
322 info = bp->b_addr;
323 switch (info->magic) {
324 case cpu_to_be16(XFS_DA_NODE_MAGIC):
325 case cpu_to_be16(XFS_DA3_NODE_MAGIC):
326 error = xfs_attr3_node_inactive(trans, dp, bp, 1);
327 break;
328 case cpu_to_be16(XFS_ATTR_LEAF_MAGIC):
329 case cpu_to_be16(XFS_ATTR3_LEAF_MAGIC):
330 error = xfs_attr3_leaf_inactive(trans, dp, bp);
331 break;
332 default:
Darrick J. Wongc2414ad2019-10-28 16:12:34 -0700333 error = -EFSCORRUPTED;
Darrick J. Wonga5155b82019-11-02 09:40:53 -0700334 xfs_buf_corruption_error(bp);
Dave Chinnerfde22272013-08-12 20:49:39 +1000335 xfs_trans_brelse(*trans, bp);
336 break;
337 }
338 if (error)
339 return error;
340
341 /*
342 * Invalidate the incore copy of the root block.
343 */
Christoph Hellwig2911edb2019-11-20 09:46:05 -0800344 bp = xfs_trans_get_buf(*trans, mp->m_ddev_targp, blkno,
345 XFS_FSB_TO_BB(mp, mp->m_attr_geo->fsbcount), 0);
346 if (!bp)
347 return -EIO;
348 error = bp->b_error;
349 if (error) {
350 xfs_trans_brelse(*trans, bp);
Dave Chinnerfde22272013-08-12 20:49:39 +1000351 return error;
Christoph Hellwig2911edb2019-11-20 09:46:05 -0800352 }
Dave Chinnerfde22272013-08-12 20:49:39 +1000353 xfs_trans_binval(*trans, bp); /* remove from cache */
354 /*
355 * Commit the invalidate and start the next transaction.
356 */
Christoph Hellwig411350d2017-08-28 10:21:03 -0700357 error = xfs_trans_roll_inode(trans, dp);
Dave Chinnerfde22272013-08-12 20:49:39 +1000358
359 return error;
360}
361
Dave Chinner6dfe5a02015-05-29 07:40:08 +1000362/*
363 * xfs_attr_inactive kills all traces of an attribute fork on an inode. It
364 * removes both the on-disk and in-memory inode fork. Note that this also has to
365 * handle the condition of inodes without attributes but with an attribute fork
366 * configured, so we can't use xfs_inode_hasattr() here.
367 *
368 * The in-memory attribute fork is removed even on error.
369 */
Dave Chinnerfde22272013-08-12 20:49:39 +1000370int
Dave Chinner6dfe5a02015-05-29 07:40:08 +1000371xfs_attr_inactive(
372 struct xfs_inode *dp)
Dave Chinnerfde22272013-08-12 20:49:39 +1000373{
Dave Chinner6dfe5a02015-05-29 07:40:08 +1000374 struct xfs_trans *trans;
375 struct xfs_mount *mp;
Dave Chinner6dfe5a02015-05-29 07:40:08 +1000376 int lock_mode = XFS_ILOCK_SHARED;
377 int error = 0;
Dave Chinnerfde22272013-08-12 20:49:39 +1000378
379 mp = dp->i_mount;
380 ASSERT(! XFS_NOT_DQATTACHED(mp, dp));
381
Dave Chinner6dfe5a02015-05-29 07:40:08 +1000382 xfs_ilock(dp, lock_mode);
383 if (!XFS_IFORK_Q(dp))
384 goto out_destroy_fork;
385 xfs_iunlock(dp, lock_mode);
Dave Chinnerfde22272013-08-12 20:49:39 +1000386
Dave Chinner6dfe5a02015-05-29 07:40:08 +1000387 lock_mode = 0;
Christoph Hellwig253f4912016-04-06 09:19:55 +1000388
389 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_attrinval, 0, 0, 0, &trans);
Dave Chinner6dfe5a02015-05-29 07:40:08 +1000390 if (error)
Christoph Hellwig253f4912016-04-06 09:19:55 +1000391 goto out_destroy_fork;
Dave Chinner6dfe5a02015-05-29 07:40:08 +1000392
393 lock_mode = XFS_ILOCK_EXCL;
Dave Chinner6dfe5a02015-05-29 07:40:08 +1000394 xfs_ilock(dp, lock_mode);
395
396 if (!XFS_IFORK_Q(dp))
397 goto out_cancel;
Dave Chinnerfde22272013-08-12 20:49:39 +1000398
399 /*
400 * No need to make quota reservations here. We expect to release some
401 * blocks, not allocate, in the common case.
402 */
403 xfs_trans_ijoin(trans, dp, 0);
404
Brian Fosterf66bf042015-06-23 08:47:20 +1000405 /*
406 * Invalidate and truncate the attribute fork extents. Make sure the
407 * fork actually has attributes as otherwise the invalidation has no
408 * blocks to read and returns an error. In this case, just do the fork
409 * removal below.
410 */
411 if (xfs_inode_hasattr(dp) &&
412 dp->i_d.di_aformat != XFS_DINODE_FMT_LOCAL) {
Dave Chinner6dfe5a02015-05-29 07:40:08 +1000413 error = xfs_attr3_root_inactive(&trans, dp);
414 if (error)
415 goto out_cancel;
Dave Chinnerfde22272013-08-12 20:49:39 +1000416
Dave Chinner6dfe5a02015-05-29 07:40:08 +1000417 error = xfs_itruncate_extents(&trans, dp, XFS_ATTR_FORK, 0);
418 if (error)
419 goto out_cancel;
420 }
421
422 /* Reset the attribute fork - this also destroys the in-core fork */
423 xfs_attr_fork_remove(dp, trans);
Dave Chinnerfde22272013-08-12 20:49:39 +1000424
Christoph Hellwig70393312015-06-04 13:48:08 +1000425 error = xfs_trans_commit(trans);
Dave Chinner6dfe5a02015-05-29 07:40:08 +1000426 xfs_iunlock(dp, lock_mode);
Eric Sandeend99831f2014-06-22 15:03:54 +1000427 return error;
Dave Chinnerfde22272013-08-12 20:49:39 +1000428
Dave Chinner6dfe5a02015-05-29 07:40:08 +1000429out_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +1000430 xfs_trans_cancel(trans);
Dave Chinner6dfe5a02015-05-29 07:40:08 +1000431out_destroy_fork:
432 /* kill the in-core attr fork before we drop the inode lock */
433 if (dp->i_afp)
434 xfs_idestroy_fork(dp, XFS_ATTR_FORK);
435 if (lock_mode)
436 xfs_iunlock(dp, lock_mode);
Eric Sandeend99831f2014-06-22 15:03:54 +1000437 return error;
Dave Chinnerfde22272013-08-12 20:49:39 +1000438}