2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * Copyright (c) 2013 Red Hat, Inc.
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.
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.
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
21 #include "xfs_shared.h"
22 #include "xfs_format.h"
23 #include "xfs_log_format.h"
24 #include "xfs_trans_resv.h"
28 #include "xfs_mount.h"
29 #include "xfs_da_format.h"
30 #include "xfs_da_btree.h"
31 #include "xfs_inode.h"
32 #include "xfs_alloc.h"
33 #include "xfs_attr_remote.h"
34 #include "xfs_trans.h"
35 #include "xfs_inode_item.h"
38 #include "xfs_attr_leaf.h"
39 #include "xfs_error.h"
40 #include "xfs_quota.h"
41 #include "xfs_trace.h"
42 #include "xfs_dinode.h"
46 * Look at all the extents for this logical region,
47 * invalidate any buffers that are incore/in transactions.
50 xfs_attr3_leaf_freextent(
51 struct xfs_trans
**trans
,
56 struct xfs_bmbt_irec map
;
66 * Roll through the "value", invalidating the attribute value's
73 * Try to remember where we decided to put the value.
76 error
= xfs_bmapi_read(dp
, (xfs_fileoff_t
)tblkno
, tblkcnt
,
77 &map
, &nmap
, XFS_BMAPI_ATTRFORK
);
82 ASSERT(map
.br_startblock
!= DELAYSTARTBLOCK
);
85 * If it's a hole, these are already unmapped
86 * so there's nothing to invalidate.
88 if (map
.br_startblock
!= HOLESTARTBLOCK
) {
90 dblkno
= XFS_FSB_TO_DADDR(dp
->i_mount
,
92 dblkcnt
= XFS_FSB_TO_BB(dp
->i_mount
,
94 bp
= xfs_trans_get_buf(*trans
,
95 dp
->i_mount
->m_ddev_targp
,
99 xfs_trans_binval(*trans
, bp
);
101 * Roll to next transaction.
103 error
= xfs_trans_roll(trans
, dp
);
108 tblkno
+= map
.br_blockcount
;
109 tblkcnt
-= map
.br_blockcount
;
116 * Invalidate all of the "remote" value regions pointed to by a particular
118 * Note that we must release the lock on the buffer so that we are not
119 * caught holding something that the logging code wants to flush to disk.
122 xfs_attr3_leaf_inactive(
123 struct xfs_trans
**trans
,
124 struct xfs_inode
*dp
,
127 struct xfs_attr_leafblock
*leaf
;
128 struct xfs_attr3_icleaf_hdr ichdr
;
129 struct xfs_attr_leaf_entry
*entry
;
130 struct xfs_attr_leaf_name_remote
*name_rmt
;
131 struct xfs_attr_inactive_list
*list
;
132 struct xfs_attr_inactive_list
*lp
;
140 xfs_attr3_leaf_hdr_from_disk(&ichdr
, leaf
);
143 * Count the number of "remote" value extents.
146 entry
= xfs_attr3_leaf_entryp(leaf
);
147 for (i
= 0; i
< ichdr
.count
; entry
++, i
++) {
148 if (be16_to_cpu(entry
->nameidx
) &&
149 ((entry
->flags
& XFS_ATTR_LOCAL
) == 0)) {
150 name_rmt
= xfs_attr3_leaf_name_remote(leaf
, i
);
151 if (name_rmt
->valueblk
)
157 * If there are no "remote" values, we're done.
160 xfs_trans_brelse(*trans
, bp
);
165 * Allocate storage for a list of all the "remote" value extents.
167 size
= count
* sizeof(xfs_attr_inactive_list_t
);
168 list
= kmem_alloc(size
, KM_SLEEP
);
171 * Identify each of the "remote" value extents.
174 entry
= xfs_attr3_leaf_entryp(leaf
);
175 for (i
= 0; i
< ichdr
.count
; entry
++, i
++) {
176 if (be16_to_cpu(entry
->nameidx
) &&
177 ((entry
->flags
& XFS_ATTR_LOCAL
) == 0)) {
178 name_rmt
= xfs_attr3_leaf_name_remote(leaf
, i
);
179 if (name_rmt
->valueblk
) {
180 lp
->valueblk
= be32_to_cpu(name_rmt
->valueblk
);
181 lp
->valuelen
= xfs_attr3_rmt_blocks(dp
->i_mount
,
182 be32_to_cpu(name_rmt
->valuelen
));
187 xfs_trans_brelse(*trans
, bp
); /* unlock for trans. in freextent() */
190 * Invalidate each of the "remote" value extents.
193 for (lp
= list
, i
= 0; i
< count
; i
++, lp
++) {
194 tmp
= xfs_attr3_leaf_freextent(trans
, dp
,
195 lp
->valueblk
, lp
->valuelen
);
198 error
= tmp
; /* save only the 1st errno */
206 * Recurse (gasp!) through the attribute nodes until we find leaves.
207 * We're doing a depth-first traversal in order to invalidate everything.
210 xfs_attr3_node_inactive(
211 struct xfs_trans
**trans
,
212 struct xfs_inode
*dp
,
216 xfs_da_blkinfo_t
*info
;
217 xfs_da_intnode_t
*node
;
218 xfs_dablk_t child_fsb
;
219 xfs_daddr_t parent_blkno
, child_blkno
;
221 struct xfs_buf
*child_bp
;
222 struct xfs_da_node_entry
*btree
;
223 struct xfs_da3_icnode_hdr ichdr
;
226 * Since this code is recursive (gasp!) we must protect ourselves.
228 if (level
> XFS_DA_NODE_MAXDEPTH
) {
229 xfs_trans_brelse(*trans
, bp
); /* no locks for later trans */
230 return XFS_ERROR(EIO
);
234 dp
->d_ops
->node_hdr_from_disk(&ichdr
, node
);
235 parent_blkno
= bp
->b_bn
;
237 xfs_trans_brelse(*trans
, bp
);
240 btree
= dp
->d_ops
->node_tree_p(node
);
241 child_fsb
= be32_to_cpu(btree
[0].before
);
242 xfs_trans_brelse(*trans
, bp
); /* no locks for later trans */
245 * If this is the node level just above the leaves, simply loop
246 * over the leaves removing all of them. If this is higher up
247 * in the tree, recurse downward.
249 for (i
= 0; i
< ichdr
.count
; i
++) {
251 * Read the subsidiary block to see what we have to work with.
252 * Don't do this in a transaction. This is a depth-first
253 * traversal of the tree so we may deal with many blocks
254 * before we come back to this one.
256 error
= xfs_da3_node_read(*trans
, dp
, child_fsb
, -2, &child_bp
,
261 /* save for re-read later */
262 child_blkno
= XFS_BUF_ADDR(child_bp
);
265 * Invalidate the subtree, however we have to.
267 info
= child_bp
->b_addr
;
268 switch (info
->magic
) {
269 case cpu_to_be16(XFS_DA_NODE_MAGIC
):
270 case cpu_to_be16(XFS_DA3_NODE_MAGIC
):
271 error
= xfs_attr3_node_inactive(trans
, dp
,
272 child_bp
, level
+ 1);
274 case cpu_to_be16(XFS_ATTR_LEAF_MAGIC
):
275 case cpu_to_be16(XFS_ATTR3_LEAF_MAGIC
):
276 error
= xfs_attr3_leaf_inactive(trans
, dp
,
280 error
= XFS_ERROR(EIO
);
281 xfs_trans_brelse(*trans
, child_bp
);
288 * Remove the subsidiary block from the cache
291 error
= xfs_da_get_buf(*trans
, dp
, 0, child_blkno
,
292 &child_bp
, XFS_ATTR_FORK
);
295 xfs_trans_binval(*trans
, child_bp
);
299 * If we're not done, re-read the parent to get the next
300 * child block number.
302 if (i
+ 1 < ichdr
.count
) {
303 error
= xfs_da3_node_read(*trans
, dp
, 0, parent_blkno
,
307 child_fsb
= be32_to_cpu(btree
[i
+ 1].before
);
308 xfs_trans_brelse(*trans
, bp
);
311 * Atomically commit the whole invalidate stuff.
313 error
= xfs_trans_roll(trans
, dp
);
322 * Indiscriminately delete the entire attribute fork
324 * Recurse (gasp!) through the attribute nodes until we find leaves.
325 * We're doing a depth-first traversal in order to invalidate everything.
328 xfs_attr3_root_inactive(
329 struct xfs_trans
**trans
,
330 struct xfs_inode
*dp
)
332 struct xfs_da_blkinfo
*info
;
338 * Read block 0 to see what we have to work with.
339 * We only get here if we have extents, since we remove
340 * the extents in reverse order the extent containing
341 * block 0 must still be there.
343 error
= xfs_da3_node_read(*trans
, dp
, 0, -1, &bp
, XFS_ATTR_FORK
);
349 * Invalidate the tree, even if the "tree" is only a single leaf block.
350 * This is a depth-first traversal!
353 switch (info
->magic
) {
354 case cpu_to_be16(XFS_DA_NODE_MAGIC
):
355 case cpu_to_be16(XFS_DA3_NODE_MAGIC
):
356 error
= xfs_attr3_node_inactive(trans
, dp
, bp
, 1);
358 case cpu_to_be16(XFS_ATTR_LEAF_MAGIC
):
359 case cpu_to_be16(XFS_ATTR3_LEAF_MAGIC
):
360 error
= xfs_attr3_leaf_inactive(trans
, dp
, bp
);
363 error
= XFS_ERROR(EIO
);
364 xfs_trans_brelse(*trans
, bp
);
371 * Invalidate the incore copy of the root block.
373 error
= xfs_da_get_buf(*trans
, dp
, 0, blkno
, &bp
, XFS_ATTR_FORK
);
376 xfs_trans_binval(*trans
, bp
); /* remove from cache */
378 * Commit the invalidate and start the next transaction.
380 error
= xfs_trans_roll(trans
, dp
);
386 * xfs_attr_inactive kills all traces of an attribute fork on an inode. It
387 * removes both the on-disk and in-memory inode fork. Note that this also has to
388 * handle the condition of inodes without attributes but with an attribute fork
389 * configured, so we can't use xfs_inode_hasattr() here.
391 * The in-memory attribute fork is removed even on error.
395 struct xfs_inode
*dp
)
397 struct xfs_trans
*trans
;
398 struct xfs_mount
*mp
;
399 int cancel_flags
= 0;
400 int lock_mode
= XFS_ILOCK_SHARED
;
404 ASSERT(! XFS_NOT_DQATTACHED(mp
, dp
));
406 xfs_ilock(dp
, lock_mode
);
407 if (!XFS_IFORK_Q(dp
))
408 goto out_destroy_fork
;
409 xfs_iunlock(dp
, lock_mode
);
412 * Start our first transaction of the day.
414 * All future transactions during this code must be "chained" off
415 * this one via the trans_dup() call. All transactions will contain
416 * the inode, and the inode will always be marked with trans_ihold().
417 * Since the inode will be locked in all transactions, we must log
418 * the inode in every transaction to let it float upward through
422 trans
= xfs_trans_alloc(mp
, XFS_TRANS_ATTRINVAL
);
423 error
= xfs_trans_reserve(trans
, &M_RES(mp
)->tr_attrinval
, 0, 0);
427 lock_mode
= XFS_ILOCK_EXCL
;
428 cancel_flags
= XFS_TRANS_RELEASE_LOG_RES
| XFS_TRANS_ABORT
;
429 xfs_ilock(dp
, lock_mode
);
431 if (!XFS_IFORK_Q(dp
))
435 * No need to make quota reservations here. We expect to release some
436 * blocks, not allocate, in the common case.
438 xfs_trans_ijoin(trans
, dp
, 0);
441 * Invalidate and truncate the attribute fork extents. Make sure the
442 * fork actually has attributes as otherwise the invalidation has no
443 * blocks to read and returns an error. In this case, just do the fork
446 if (xfs_inode_hasattr(dp
) &&
447 dp
->i_d
.di_aformat
!= XFS_DINODE_FMT_LOCAL
) {
448 error
= xfs_attr3_root_inactive(&trans
, dp
);
452 error
= xfs_itruncate_extents(&trans
, dp
, XFS_ATTR_FORK
, 0);
457 /* Reset the attribute fork - this also destroys the in-core fork */
458 xfs_attr_fork_remove(dp
, trans
);
460 error
= xfs_trans_commit(trans
, XFS_TRANS_RELEASE_LOG_RES
);
461 xfs_iunlock(dp
, lock_mode
);
465 xfs_trans_cancel(trans
, cancel_flags
);
467 /* kill the in-core attr fork before we drop the inode lock */
469 xfs_idestroy_fork(dp
, XFS_ATTR_FORK
);
471 xfs_iunlock(dp
, lock_mode
);