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"
45 * Look at all the extents for this logical region,
46 * invalidate any buffers that are incore/in transactions.
49 xfs_attr3_leaf_freextent(
50 struct xfs_trans **trans,
55 struct xfs_bmbt_irec map;
65 * Roll through the "value", invalidating the attribute value's
72 * Try to remember where we decided to put the value.
75 error = xfs_bmapi_read(dp, (xfs_fileoff_t)tblkno, tblkcnt,
76 &map, &nmap, XFS_BMAPI_ATTRFORK);
81 ASSERT(map.br_startblock != DELAYSTARTBLOCK);
84 * If it's a hole, these are already unmapped
85 * so there's nothing to invalidate.
87 if (map.br_startblock != HOLESTARTBLOCK) {
89 dblkno = XFS_FSB_TO_DADDR(dp->i_mount,
91 dblkcnt = XFS_FSB_TO_BB(dp->i_mount,
93 bp = xfs_trans_get_buf(*trans,
94 dp->i_mount->m_ddev_targp,
98 xfs_trans_binval(*trans, bp);
100 * Roll to next transaction.
102 error = xfs_trans_roll(trans, dp);
107 tblkno += map.br_blockcount;
108 tblkcnt -= map.br_blockcount;
115 * Invalidate all of the "remote" value regions pointed to by a particular
117 * Note that we must release the lock on the buffer so that we are not
118 * caught holding something that the logging code wants to flush to disk.
121 xfs_attr3_leaf_inactive(
122 struct xfs_trans **trans,
123 struct xfs_inode *dp,
126 struct xfs_attr_leafblock *leaf;
127 struct xfs_attr3_icleaf_hdr ichdr;
128 struct xfs_attr_leaf_entry *entry;
129 struct xfs_attr_leaf_name_remote *name_rmt;
130 struct xfs_attr_inactive_list *list;
131 struct xfs_attr_inactive_list *lp;
139 xfs_attr3_leaf_hdr_from_disk(&ichdr, leaf);
142 * Count the number of "remote" value extents.
145 entry = xfs_attr3_leaf_entryp(leaf);
146 for (i = 0; i < ichdr.count; entry++, i++) {
147 if (be16_to_cpu(entry->nameidx) &&
148 ((entry->flags & XFS_ATTR_LOCAL) == 0)) {
149 name_rmt = xfs_attr3_leaf_name_remote(leaf, i);
150 if (name_rmt->valueblk)
156 * If there are no "remote" values, we're done.
159 xfs_trans_brelse(*trans, bp);
164 * Allocate storage for a list of all the "remote" value extents.
166 size = count * sizeof(xfs_attr_inactive_list_t);
167 list = kmem_alloc(size, KM_SLEEP);
170 * Identify each of the "remote" value extents.
173 entry = xfs_attr3_leaf_entryp(leaf);
174 for (i = 0; i < ichdr.count; entry++, i++) {
175 if (be16_to_cpu(entry->nameidx) &&
176 ((entry->flags & XFS_ATTR_LOCAL) == 0)) {
177 name_rmt = xfs_attr3_leaf_name_remote(leaf, i);
178 if (name_rmt->valueblk) {
179 lp->valueblk = be32_to_cpu(name_rmt->valueblk);
180 lp->valuelen = xfs_attr3_rmt_blocks(dp->i_mount,
181 be32_to_cpu(name_rmt->valuelen));
186 xfs_trans_brelse(*trans, bp); /* unlock for trans. in freextent() */
189 * Invalidate each of the "remote" value extents.
192 for (lp = list, i = 0; i < count; i++, lp++) {
193 tmp = xfs_attr3_leaf_freextent(trans, dp,
194 lp->valueblk, lp->valuelen);
197 error = tmp; /* save only the 1st errno */
205 * Recurse (gasp!) through the attribute nodes until we find leaves.
206 * We're doing a depth-first traversal in order to invalidate everything.
209 xfs_attr3_node_inactive(
210 struct xfs_trans **trans,
211 struct xfs_inode *dp,
215 xfs_da_blkinfo_t *info;
216 xfs_da_intnode_t *node;
217 xfs_dablk_t child_fsb;
218 xfs_daddr_t parent_blkno, child_blkno;
220 struct xfs_buf *child_bp;
221 struct xfs_da_node_entry *btree;
222 struct xfs_da3_icnode_hdr ichdr;
225 * Since this code is recursive (gasp!) we must protect ourselves.
227 if (level > XFS_DA_NODE_MAXDEPTH) {
228 xfs_trans_brelse(*trans, bp); /* no locks for later trans */
233 dp->d_ops->node_hdr_from_disk(&ichdr, node);
234 parent_blkno = bp->b_bn;
236 xfs_trans_brelse(*trans, bp);
239 btree = dp->d_ops->node_tree_p(node);
240 child_fsb = be32_to_cpu(btree[0].before);
241 xfs_trans_brelse(*trans, bp); /* no locks for later trans */
244 * If this is the node level just above the leaves, simply loop
245 * over the leaves removing all of them. If this is higher up
246 * in the tree, recurse downward.
248 for (i = 0; i < ichdr.count; i++) {
250 * Read the subsidiary block to see what we have to work with.
251 * Don't do this in a transaction. This is a depth-first
252 * traversal of the tree so we may deal with many blocks
253 * before we come back to this one.
255 error = xfs_da3_node_read(*trans, dp, child_fsb, -2, &child_bp,
260 /* save for re-read later */
261 child_blkno = XFS_BUF_ADDR(child_bp);
264 * Invalidate the subtree, however we have to.
266 info = child_bp->b_addr;
267 switch (info->magic) {
268 case cpu_to_be16(XFS_DA_NODE_MAGIC):
269 case cpu_to_be16(XFS_DA3_NODE_MAGIC):
270 error = xfs_attr3_node_inactive(trans, dp,
271 child_bp, level + 1);
273 case cpu_to_be16(XFS_ATTR_LEAF_MAGIC):
274 case cpu_to_be16(XFS_ATTR3_LEAF_MAGIC):
275 error = xfs_attr3_leaf_inactive(trans, dp,
280 xfs_trans_brelse(*trans, child_bp);
287 * Remove the subsidiary block from the cache
290 error = xfs_da_get_buf(*trans, dp, 0, child_blkno,
291 &child_bp, XFS_ATTR_FORK);
294 xfs_trans_binval(*trans, child_bp);
298 * If we're not done, re-read the parent to get the next
299 * child block number.
301 if (i + 1 < ichdr.count) {
302 error = xfs_da3_node_read(*trans, dp, 0, parent_blkno,
306 child_fsb = be32_to_cpu(btree[i + 1].before);
307 xfs_trans_brelse(*trans, bp);
310 * Atomically commit the whole invalidate stuff.
312 error = xfs_trans_roll(trans, dp);
321 * Indiscriminately delete the entire attribute fork
323 * Recurse (gasp!) through the attribute nodes until we find leaves.
324 * We're doing a depth-first traversal in order to invalidate everything.
327 xfs_attr3_root_inactive(
328 struct xfs_trans **trans,
329 struct xfs_inode *dp)
331 struct xfs_da_blkinfo *info;
337 * Read block 0 to see what we have to work with.
338 * We only get here if we have extents, since we remove
339 * the extents in reverse order the extent containing
340 * block 0 must still be there.
342 error = xfs_da3_node_read(*trans, dp, 0, -1, &bp, XFS_ATTR_FORK);
348 * Invalidate the tree, even if the "tree" is only a single leaf block.
349 * This is a depth-first traversal!
352 switch (info->magic) {
353 case cpu_to_be16(XFS_DA_NODE_MAGIC):
354 case cpu_to_be16(XFS_DA3_NODE_MAGIC):
355 error = xfs_attr3_node_inactive(trans, dp, bp, 1);
357 case cpu_to_be16(XFS_ATTR_LEAF_MAGIC):
358 case cpu_to_be16(XFS_ATTR3_LEAF_MAGIC):
359 error = xfs_attr3_leaf_inactive(trans, dp, bp);
363 xfs_trans_brelse(*trans, bp);
370 * Invalidate the incore copy of the root block.
372 error = xfs_da_get_buf(*trans, dp, 0, blkno, &bp, XFS_ATTR_FORK);
375 xfs_trans_binval(*trans, bp); /* remove from cache */
377 * Commit the invalidate and start the next transaction.
379 error = xfs_trans_roll(trans, dp);
385 xfs_attr_inactive(xfs_inode_t *dp)
392 ASSERT(! XFS_NOT_DQATTACHED(mp, dp));
394 xfs_ilock(dp, XFS_ILOCK_SHARED);
395 if (!xfs_inode_hasattr(dp) ||
396 dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
397 xfs_iunlock(dp, XFS_ILOCK_SHARED);
400 xfs_iunlock(dp, XFS_ILOCK_SHARED);
403 * Start our first transaction of the day.
405 * All future transactions during this code must be "chained" off
406 * this one via the trans_dup() call. All transactions will contain
407 * the inode, and the inode will always be marked with trans_ihold().
408 * Since the inode will be locked in all transactions, we must log
409 * the inode in every transaction to let it float upward through
412 trans = xfs_trans_alloc(mp, XFS_TRANS_ATTRINVAL);
413 error = xfs_trans_reserve(trans, &M_RES(mp)->tr_attrinval, 0, 0);
415 xfs_trans_cancel(trans, 0);
418 xfs_ilock(dp, XFS_ILOCK_EXCL);
421 * No need to make quota reservations here. We expect to release some
422 * blocks, not allocate, in the common case.
424 xfs_trans_ijoin(trans, dp, 0);
427 * Decide on what work routines to call based on the inode size.
429 if (!xfs_inode_hasattr(dp) ||
430 dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
434 error = xfs_attr3_root_inactive(&trans, dp);
438 error = xfs_itruncate_extents(&trans, dp, XFS_ATTR_FORK, 0);
442 error = xfs_trans_commit(trans, XFS_TRANS_RELEASE_LOG_RES);
443 xfs_iunlock(dp, XFS_ILOCK_EXCL);
448 xfs_trans_cancel(trans, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
449 xfs_iunlock(dp, XFS_ILOCK_EXCL);