1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4 * Copyright (c) 2013 Red Hat, Inc.
9 #include "xfs_shared.h"
10 #include "xfs_format.h"
11 #include "xfs_log_format.h"
12 #include "xfs_trans_resv.h"
14 #include "xfs_mount.h"
15 #include "xfs_da_format.h"
16 #include "xfs_da_btree.h"
17 #include "xfs_inode.h"
18 #include "xfs_attr_remote.h"
19 #include "xfs_trans.h"
22 #include "xfs_attr_leaf.h"
23 #include "xfs_quota.h"
25 #include "xfs_error.h"
28 * Look at all the extents for this logical region,
29 * invalidate any buffers that are incore/in transactions.
32 xfs_attr3_leaf_freextent(
33 struct xfs_trans **trans,
38 struct xfs_bmbt_irec map;
48 * Roll through the "value", invalidating the attribute value's
55 * Try to remember where we decided to put the value.
58 error = xfs_bmapi_read(dp, (xfs_fileoff_t)tblkno, tblkcnt,
59 &map, &nmap, XFS_BMAPI_ATTRFORK);
64 ASSERT(map.br_startblock != DELAYSTARTBLOCK);
67 * If it's a hole, these are already unmapped
68 * so there's nothing to invalidate.
70 if (map.br_startblock != HOLESTARTBLOCK) {
72 dblkno = XFS_FSB_TO_DADDR(dp->i_mount,
74 dblkcnt = XFS_FSB_TO_BB(dp->i_mount,
76 bp = xfs_trans_get_buf(*trans,
77 dp->i_mount->m_ddev_targp,
81 xfs_trans_binval(*trans, bp);
83 * Roll to next transaction.
85 error = xfs_trans_roll_inode(trans, dp);
90 tblkno += map.br_blockcount;
91 tblkcnt -= map.br_blockcount;
98 * Invalidate all of the "remote" value regions pointed to by a particular
100 * Note that we must release the lock on the buffer so that we are not
101 * caught holding something that the logging code wants to flush to disk.
104 xfs_attr3_leaf_inactive(
105 struct xfs_trans **trans,
106 struct xfs_inode *dp,
109 struct xfs_attr_leafblock *leaf;
110 struct xfs_attr3_icleaf_hdr ichdr;
111 struct xfs_attr_leaf_entry *entry;
112 struct xfs_attr_leaf_name_remote *name_rmt;
113 struct xfs_attr_inactive_list *list;
114 struct xfs_attr_inactive_list *lp;
120 struct xfs_mount *mp = bp->b_mount;
123 xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr, leaf);
126 * Count the number of "remote" value extents.
129 entry = xfs_attr3_leaf_entryp(leaf);
130 for (i = 0; i < ichdr.count; entry++, i++) {
131 if (be16_to_cpu(entry->nameidx) &&
132 ((entry->flags & XFS_ATTR_LOCAL) == 0)) {
133 name_rmt = xfs_attr3_leaf_name_remote(leaf, i);
134 if (name_rmt->valueblk)
140 * If there are no "remote" values, we're done.
143 xfs_trans_brelse(*trans, bp);
148 * Allocate storage for a list of all the "remote" value extents.
150 size = count * sizeof(xfs_attr_inactive_list_t);
151 list = kmem_alloc(size, 0);
154 * Identify each of the "remote" value extents.
157 entry = xfs_attr3_leaf_entryp(leaf);
158 for (i = 0; i < ichdr.count; entry++, i++) {
159 if (be16_to_cpu(entry->nameidx) &&
160 ((entry->flags & XFS_ATTR_LOCAL) == 0)) {
161 name_rmt = xfs_attr3_leaf_name_remote(leaf, i);
162 if (name_rmt->valueblk) {
163 lp->valueblk = be32_to_cpu(name_rmt->valueblk);
164 lp->valuelen = xfs_attr3_rmt_blocks(dp->i_mount,
165 be32_to_cpu(name_rmt->valuelen));
170 xfs_trans_brelse(*trans, bp); /* unlock for trans. in freextent() */
173 * Invalidate each of the "remote" value extents.
176 for (lp = list, i = 0; i < count; i++, lp++) {
177 tmp = xfs_attr3_leaf_freextent(trans, dp,
178 lp->valueblk, lp->valuelen);
181 error = tmp; /* save only the 1st errno */
189 * Recurse (gasp!) through the attribute nodes until we find leaves.
190 * We're doing a depth-first traversal in order to invalidate everything.
193 xfs_attr3_node_inactive(
194 struct xfs_trans **trans,
195 struct xfs_inode *dp,
199 struct xfs_da_blkinfo *info;
200 xfs_dablk_t child_fsb;
201 xfs_daddr_t parent_blkno, child_blkno;
202 struct xfs_buf *child_bp;
203 struct xfs_da3_icnode_hdr ichdr;
207 * Since this code is recursive (gasp!) we must protect ourselves.
209 if (level > XFS_DA_NODE_MAXDEPTH) {
210 xfs_trans_brelse(*trans, bp); /* no locks for later trans */
211 xfs_buf_corruption_error(bp);
212 return -EFSCORRUPTED;
215 xfs_da3_node_hdr_from_disk(dp->i_mount, &ichdr, bp->b_addr);
216 parent_blkno = bp->b_bn;
218 xfs_trans_brelse(*trans, bp);
221 child_fsb = be32_to_cpu(ichdr.btree[0].before);
222 xfs_trans_brelse(*trans, bp); /* no locks for later trans */
225 * If this is the node level just above the leaves, simply loop
226 * over the leaves removing all of them. If this is higher up
227 * in the tree, recurse downward.
229 for (i = 0; i < ichdr.count; i++) {
231 * Read the subsidiary block to see what we have to work with.
232 * Don't do this in a transaction. This is a depth-first
233 * traversal of the tree so we may deal with many blocks
234 * before we come back to this one.
236 error = xfs_da3_node_read(*trans, dp, child_fsb, -1, &child_bp,
241 /* save for re-read later */
242 child_blkno = XFS_BUF_ADDR(child_bp);
245 * Invalidate the subtree, however we have to.
247 info = child_bp->b_addr;
248 switch (info->magic) {
249 case cpu_to_be16(XFS_DA_NODE_MAGIC):
250 case cpu_to_be16(XFS_DA3_NODE_MAGIC):
251 error = xfs_attr3_node_inactive(trans, dp, child_bp,
254 case cpu_to_be16(XFS_ATTR_LEAF_MAGIC):
255 case cpu_to_be16(XFS_ATTR3_LEAF_MAGIC):
256 error = xfs_attr3_leaf_inactive(trans, dp, child_bp);
259 xfs_buf_corruption_error(child_bp);
260 xfs_trans_brelse(*trans, child_bp);
261 error = -EFSCORRUPTED;
268 * Remove the subsidiary block from the cache and from the log.
270 error = xfs_da_get_buf(*trans, dp, 0, child_blkno, &child_bp,
274 xfs_trans_binval(*trans, child_bp);
277 * If we're not done, re-read the parent to get the next
278 * child block number.
280 if (i + 1 < ichdr.count) {
281 struct xfs_da3_icnode_hdr phdr;
283 error = xfs_da3_node_read(*trans, dp, 0, parent_blkno,
287 xfs_da3_node_hdr_from_disk(dp->i_mount, &phdr,
289 child_fsb = be32_to_cpu(phdr.btree[i + 1].before);
290 xfs_trans_brelse(*trans, bp);
293 * Atomically commit the whole invalidate stuff.
295 error = xfs_trans_roll_inode(trans, dp);
304 * Indiscriminately delete the entire attribute fork
306 * Recurse (gasp!) through the attribute nodes until we find leaves.
307 * We're doing a depth-first traversal in order to invalidate everything.
310 xfs_attr3_root_inactive(
311 struct xfs_trans **trans,
312 struct xfs_inode *dp)
314 struct xfs_da_blkinfo *info;
320 * Read block 0 to see what we have to work with.
321 * We only get here if we have extents, since we remove
322 * the extents in reverse order the extent containing
323 * block 0 must still be there.
325 error = xfs_da3_node_read(*trans, dp, 0, -1, &bp, XFS_ATTR_FORK);
331 * Invalidate the tree, even if the "tree" is only a single leaf block.
332 * This is a depth-first traversal!
335 switch (info->magic) {
336 case cpu_to_be16(XFS_DA_NODE_MAGIC):
337 case cpu_to_be16(XFS_DA3_NODE_MAGIC):
338 error = xfs_attr3_node_inactive(trans, dp, bp, 1);
340 case cpu_to_be16(XFS_ATTR_LEAF_MAGIC):
341 case cpu_to_be16(XFS_ATTR3_LEAF_MAGIC):
342 error = xfs_attr3_leaf_inactive(trans, dp, bp);
345 error = -EFSCORRUPTED;
346 xfs_buf_corruption_error(bp);
347 xfs_trans_brelse(*trans, bp);
354 * Invalidate the incore copy of the root block.
356 error = xfs_da_get_buf(*trans, dp, 0, blkno, &bp, XFS_ATTR_FORK);
359 xfs_trans_binval(*trans, bp); /* remove from cache */
361 * Commit the invalidate and start the next transaction.
363 error = xfs_trans_roll_inode(trans, dp);
369 * xfs_attr_inactive kills all traces of an attribute fork on an inode. It
370 * removes both the on-disk and in-memory inode fork. Note that this also has to
371 * handle the condition of inodes without attributes but with an attribute fork
372 * configured, so we can't use xfs_inode_hasattr() here.
374 * The in-memory attribute fork is removed even on error.
378 struct xfs_inode *dp)
380 struct xfs_trans *trans;
381 struct xfs_mount *mp;
382 int lock_mode = XFS_ILOCK_SHARED;
386 ASSERT(! XFS_NOT_DQATTACHED(mp, dp));
388 xfs_ilock(dp, lock_mode);
389 if (!XFS_IFORK_Q(dp))
390 goto out_destroy_fork;
391 xfs_iunlock(dp, lock_mode);
395 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_attrinval, 0, 0, 0, &trans);
397 goto out_destroy_fork;
399 lock_mode = XFS_ILOCK_EXCL;
400 xfs_ilock(dp, lock_mode);
402 if (!XFS_IFORK_Q(dp))
406 * No need to make quota reservations here. We expect to release some
407 * blocks, not allocate, in the common case.
409 xfs_trans_ijoin(trans, dp, 0);
412 * Invalidate and truncate the attribute fork extents. Make sure the
413 * fork actually has attributes as otherwise the invalidation has no
414 * blocks to read and returns an error. In this case, just do the fork
417 if (xfs_inode_hasattr(dp) &&
418 dp->i_d.di_aformat != XFS_DINODE_FMT_LOCAL) {
419 error = xfs_attr3_root_inactive(&trans, dp);
423 error = xfs_itruncate_extents(&trans, dp, XFS_ATTR_FORK, 0);
428 /* Reset the attribute fork - this also destroys the in-core fork */
429 xfs_attr_fork_remove(dp, trans);
431 error = xfs_trans_commit(trans);
432 xfs_iunlock(dp, lock_mode);
436 xfs_trans_cancel(trans);
438 /* kill the in-core attr fork before we drop the inode lock */
440 xfs_idestroy_fork(dp, XFS_ATTR_FORK);
442 xfs_iunlock(dp, lock_mode);