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_mount
*mp
= dp
->i_mount
;
200 struct xfs_da_blkinfo
*info
;
201 xfs_dablk_t child_fsb
;
202 xfs_daddr_t parent_blkno
, child_blkno
;
203 struct xfs_buf
*child_bp
;
204 struct xfs_da3_icnode_hdr ichdr
;
208 * Since this code is recursive (gasp!) we must protect ourselves.
210 if (level
> XFS_DA_NODE_MAXDEPTH
) {
211 xfs_trans_brelse(*trans
, bp
); /* no locks for later trans */
212 xfs_buf_corruption_error(bp
);
213 return -EFSCORRUPTED
;
216 xfs_da3_node_hdr_from_disk(dp
->i_mount
, &ichdr
, bp
->b_addr
);
217 parent_blkno
= bp
->b_bn
;
219 xfs_trans_brelse(*trans
, bp
);
222 child_fsb
= be32_to_cpu(ichdr
.btree
[0].before
);
223 xfs_trans_brelse(*trans
, bp
); /* no locks for later trans */
226 * If this is the node level just above the leaves, simply loop
227 * over the leaves removing all of them. If this is higher up
228 * in the tree, recurse downward.
230 for (i
= 0; i
< ichdr
.count
; i
++) {
232 * Read the subsidiary block to see what we have to work with.
233 * Don't do this in a transaction. This is a depth-first
234 * traversal of the tree so we may deal with many blocks
235 * before we come back to this one.
237 error
= xfs_da3_node_read(*trans
, dp
, child_fsb
, &child_bp
,
242 /* save for re-read later */
243 child_blkno
= XFS_BUF_ADDR(child_bp
);
246 * Invalidate the subtree, however we have to.
248 info
= child_bp
->b_addr
;
249 switch (info
->magic
) {
250 case cpu_to_be16(XFS_DA_NODE_MAGIC
):
251 case cpu_to_be16(XFS_DA3_NODE_MAGIC
):
252 error
= xfs_attr3_node_inactive(trans
, dp
, child_bp
,
255 case cpu_to_be16(XFS_ATTR_LEAF_MAGIC
):
256 case cpu_to_be16(XFS_ATTR3_LEAF_MAGIC
):
257 error
= xfs_attr3_leaf_inactive(trans
, dp
, child_bp
);
260 xfs_buf_corruption_error(child_bp
);
261 xfs_trans_brelse(*trans
, child_bp
);
262 error
= -EFSCORRUPTED
;
269 * Remove the subsidiary block from the cache and from the log.
271 child_bp
= xfs_trans_get_buf(*trans
, mp
->m_ddev_targp
,
273 XFS_FSB_TO_BB(mp
, mp
->m_attr_geo
->fsbcount
), 0);
278 xfs_trans_brelse(*trans
, child_bp
);
281 xfs_trans_binval(*trans
, child_bp
);
284 * If we're not done, re-read the parent to get the next
285 * child block number.
287 if (i
+ 1 < ichdr
.count
) {
288 struct xfs_da3_icnode_hdr phdr
;
290 error
= xfs_da3_node_read_mapped(*trans
, dp
,
291 parent_blkno
, &bp
, XFS_ATTR_FORK
);
294 xfs_da3_node_hdr_from_disk(dp
->i_mount
, &phdr
,
296 child_fsb
= be32_to_cpu(phdr
.btree
[i
+ 1].before
);
297 xfs_trans_brelse(*trans
, bp
);
300 * Atomically commit the whole invalidate stuff.
302 error
= xfs_trans_roll_inode(trans
, dp
);
311 * Indiscriminately delete the entire attribute fork
313 * Recurse (gasp!) through the attribute nodes until we find leaves.
314 * We're doing a depth-first traversal in order to invalidate everything.
317 xfs_attr3_root_inactive(
318 struct xfs_trans
**trans
,
319 struct xfs_inode
*dp
)
321 struct xfs_mount
*mp
= dp
->i_mount
;
322 struct xfs_da_blkinfo
*info
;
328 * Read block 0 to see what we have to work with.
329 * We only get here if we have extents, since we remove
330 * the extents in reverse order the extent containing
331 * block 0 must still be there.
333 error
= xfs_da3_node_read(*trans
, dp
, 0, &bp
, XFS_ATTR_FORK
);
339 * Invalidate the tree, even if the "tree" is only a single leaf block.
340 * This is a depth-first traversal!
343 switch (info
->magic
) {
344 case cpu_to_be16(XFS_DA_NODE_MAGIC
):
345 case cpu_to_be16(XFS_DA3_NODE_MAGIC
):
346 error
= xfs_attr3_node_inactive(trans
, dp
, bp
, 1);
348 case cpu_to_be16(XFS_ATTR_LEAF_MAGIC
):
349 case cpu_to_be16(XFS_ATTR3_LEAF_MAGIC
):
350 error
= xfs_attr3_leaf_inactive(trans
, dp
, bp
);
353 error
= -EFSCORRUPTED
;
354 xfs_buf_corruption_error(bp
);
355 xfs_trans_brelse(*trans
, bp
);
362 * Invalidate the incore copy of the root block.
364 bp
= xfs_trans_get_buf(*trans
, mp
->m_ddev_targp
, blkno
,
365 XFS_FSB_TO_BB(mp
, mp
->m_attr_geo
->fsbcount
), 0);
370 xfs_trans_brelse(*trans
, bp
);
373 xfs_trans_binval(*trans
, bp
); /* remove from cache */
375 * Commit the invalidate and start the next transaction.
377 error
= xfs_trans_roll_inode(trans
, dp
);
383 * xfs_attr_inactive kills all traces of an attribute fork on an inode. It
384 * removes both the on-disk and in-memory inode fork. Note that this also has to
385 * handle the condition of inodes without attributes but with an attribute fork
386 * configured, so we can't use xfs_inode_hasattr() here.
388 * The in-memory attribute fork is removed even on error.
392 struct xfs_inode
*dp
)
394 struct xfs_trans
*trans
;
395 struct xfs_mount
*mp
;
396 int lock_mode
= XFS_ILOCK_SHARED
;
400 ASSERT(! XFS_NOT_DQATTACHED(mp
, dp
));
402 xfs_ilock(dp
, lock_mode
);
403 if (!XFS_IFORK_Q(dp
))
404 goto out_destroy_fork
;
405 xfs_iunlock(dp
, lock_mode
);
409 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_attrinval
, 0, 0, 0, &trans
);
411 goto out_destroy_fork
;
413 lock_mode
= XFS_ILOCK_EXCL
;
414 xfs_ilock(dp
, lock_mode
);
416 if (!XFS_IFORK_Q(dp
))
420 * No need to make quota reservations here. We expect to release some
421 * blocks, not allocate, in the common case.
423 xfs_trans_ijoin(trans
, dp
, 0);
426 * Invalidate and truncate the attribute fork extents. Make sure the
427 * fork actually has attributes as otherwise the invalidation has no
428 * blocks to read and returns an error. In this case, just do the fork
431 if (xfs_inode_hasattr(dp
) &&
432 dp
->i_d
.di_aformat
!= XFS_DINODE_FMT_LOCAL
) {
433 error
= xfs_attr3_root_inactive(&trans
, dp
);
437 error
= xfs_itruncate_extents(&trans
, dp
, XFS_ATTR_FORK
, 0);
442 /* Reset the attribute fork - this also destroys the in-core fork */
443 xfs_attr_fork_remove(dp
, trans
);
445 error
= xfs_trans_commit(trans
);
446 xfs_iunlock(dp
, lock_mode
);
450 xfs_trans_cancel(trans
);
452 /* kill the in-core attr fork before we drop the inode lock */
454 xfs_idestroy_fork(dp
, XFS_ATTR_FORK
);
456 xfs_iunlock(dp
, lock_mode
);