]>
Commit | Line | Data |
---|---|---|
0b61f8a4 | 1 | // SPDX-License-Identifier: GPL-2.0 |
fde2227c DC |
2 | /* |
3 | * Copyright (c) 2000-2005 Silicon Graphics, Inc. | |
4 | * Copyright (c) 2013 Red Hat, Inc. | |
5 | * All Rights Reserved. | |
fde2227c DC |
6 | */ |
7 | #include "xfs.h" | |
8 | #include "xfs_fs.h" | |
70a9883c | 9 | #include "xfs_shared.h" |
239880ef DC |
10 | #include "xfs_format.h" |
11 | #include "xfs_log_format.h" | |
12 | #include "xfs_trans_resv.h" | |
fde2227c | 13 | #include "xfs_bit.h" |
fde2227c | 14 | #include "xfs_mount.h" |
57062787 | 15 | #include "xfs_da_format.h" |
fde2227c | 16 | #include "xfs_da_btree.h" |
a4fbe6ab | 17 | #include "xfs_inode.h" |
fde2227c | 18 | #include "xfs_attr_remote.h" |
239880ef | 19 | #include "xfs_trans.h" |
fde2227c DC |
20 | #include "xfs_bmap.h" |
21 | #include "xfs_attr.h" | |
22 | #include "xfs_attr_leaf.h" | |
fde2227c | 23 | #include "xfs_quota.h" |
4bceb18f | 24 | #include "xfs_dir2.h" |
a5155b87 | 25 | #include "xfs_error.h" |
fde2227c DC |
26 | |
27 | /* | |
28 | * Look at all the extents for this logical region, | |
29 | * invalidate any buffers that are incore/in transactions. | |
30 | */ | |
31 | STATIC int | |
32 | xfs_attr3_leaf_freextent( | |
33 | struct xfs_trans **trans, | |
34 | struct xfs_inode *dp, | |
35 | xfs_dablk_t blkno, | |
36 | int blkcnt) | |
37 | { | |
38 | struct xfs_bmbt_irec map; | |
39 | struct xfs_buf *bp; | |
40 | xfs_dablk_t tblkno; | |
41 | xfs_daddr_t dblkno; | |
42 | int tblkcnt; | |
43 | int dblkcnt; | |
44 | int nmap; | |
45 | int error; | |
46 | ||
47 | /* | |
48 | * Roll through the "value", invalidating the attribute value's | |
49 | * blocks. | |
50 | */ | |
51 | tblkno = blkno; | |
52 | tblkcnt = blkcnt; | |
53 | while (tblkcnt > 0) { | |
54 | /* | |
55 | * Try to remember where we decided to put the value. | |
56 | */ | |
57 | nmap = 1; | |
58 | error = xfs_bmapi_read(dp, (xfs_fileoff_t)tblkno, tblkcnt, | |
59 | &map, &nmap, XFS_BMAPI_ATTRFORK); | |
60 | if (error) { | |
d99831ff | 61 | return error; |
fde2227c DC |
62 | } |
63 | ASSERT(nmap == 1); | |
64 | ASSERT(map.br_startblock != DELAYSTARTBLOCK); | |
65 | ||
66 | /* | |
67 | * If it's a hole, these are already unmapped | |
68 | * so there's nothing to invalidate. | |
69 | */ | |
70 | if (map.br_startblock != HOLESTARTBLOCK) { | |
71 | ||
72 | dblkno = XFS_FSB_TO_DADDR(dp->i_mount, | |
73 | map.br_startblock); | |
74 | dblkcnt = XFS_FSB_TO_BB(dp->i_mount, | |
75 | map.br_blockcount); | |
76 | bp = xfs_trans_get_buf(*trans, | |
77 | dp->i_mount->m_ddev_targp, | |
78 | dblkno, dblkcnt, 0); | |
79 | if (!bp) | |
2451337d | 80 | return -ENOMEM; |
fde2227c DC |
81 | xfs_trans_binval(*trans, bp); |
82 | /* | |
83 | * Roll to next transaction. | |
84 | */ | |
411350df | 85 | error = xfs_trans_roll_inode(trans, dp); |
fde2227c | 86 | if (error) |
d99831ff | 87 | return error; |
fde2227c DC |
88 | } |
89 | ||
90 | tblkno += map.br_blockcount; | |
91 | tblkcnt -= map.br_blockcount; | |
92 | } | |
93 | ||
d99831ff | 94 | return 0; |
fde2227c DC |
95 | } |
96 | ||
97 | /* | |
98 | * Invalidate all of the "remote" value regions pointed to by a particular | |
99 | * leaf block. | |
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. | |
102 | */ | |
103 | STATIC int | |
104 | xfs_attr3_leaf_inactive( | |
105 | struct xfs_trans **trans, | |
106 | struct xfs_inode *dp, | |
107 | struct xfs_buf *bp) | |
108 | { | |
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; | |
115 | int error; | |
116 | int count; | |
117 | int size; | |
118 | int tmp; | |
119 | int i; | |
dbd329f1 | 120 | struct xfs_mount *mp = bp->b_mount; |
fde2227c DC |
121 | |
122 | leaf = bp->b_addr; | |
2f661241 | 123 | xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr, leaf); |
fde2227c DC |
124 | |
125 | /* | |
126 | * Count the number of "remote" value extents. | |
127 | */ | |
128 | count = 0; | |
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) | |
135 | count++; | |
136 | } | |
137 | } | |
138 | ||
139 | /* | |
140 | * If there are no "remote" values, we're done. | |
141 | */ | |
142 | if (count == 0) { | |
143 | xfs_trans_brelse(*trans, bp); | |
144 | return 0; | |
145 | } | |
146 | ||
147 | /* | |
148 | * Allocate storage for a list of all the "remote" value extents. | |
149 | */ | |
150 | size = count * sizeof(xfs_attr_inactive_list_t); | |
707e0dda | 151 | list = kmem_alloc(size, 0); |
fde2227c DC |
152 | |
153 | /* | |
154 | * Identify each of the "remote" value extents. | |
155 | */ | |
156 | lp = list; | |
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)); | |
166 | lp++; | |
167 | } | |
168 | } | |
169 | } | |
170 | xfs_trans_brelse(*trans, bp); /* unlock for trans. in freextent() */ | |
171 | ||
172 | /* | |
173 | * Invalidate each of the "remote" value extents. | |
174 | */ | |
175 | error = 0; | |
176 | for (lp = list, i = 0; i < count; i++, lp++) { | |
177 | tmp = xfs_attr3_leaf_freextent(trans, dp, | |
178 | lp->valueblk, lp->valuelen); | |
179 | ||
180 | if (error == 0) | |
181 | error = tmp; /* save only the 1st errno */ | |
182 | } | |
183 | ||
184 | kmem_free(list); | |
185 | return error; | |
186 | } | |
187 | ||
188 | /* | |
189 | * Recurse (gasp!) through the attribute nodes until we find leaves. | |
190 | * We're doing a depth-first traversal in order to invalidate everything. | |
191 | */ | |
192 | STATIC int | |
193 | xfs_attr3_node_inactive( | |
51908ca7 CH |
194 | struct xfs_trans **trans, |
195 | struct xfs_inode *dp, | |
196 | struct xfs_buf *bp, | |
197 | int level) | |
fde2227c | 198 | { |
51908ca7 CH |
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; | |
fde2227c | 203 | struct xfs_da3_icnode_hdr ichdr; |
51908ca7 | 204 | int error, i; |
fde2227c DC |
205 | |
206 | /* | |
207 | * Since this code is recursive (gasp!) we must protect ourselves. | |
208 | */ | |
209 | if (level > XFS_DA_NODE_MAXDEPTH) { | |
210 | xfs_trans_brelse(*trans, bp); /* no locks for later trans */ | |
a5155b87 | 211 | xfs_buf_corruption_error(bp); |
c2414ad6 | 212 | return -EFSCORRUPTED; |
fde2227c DC |
213 | } |
214 | ||
51908ca7 | 215 | xfs_da3_node_hdr_from_disk(dp->i_mount, &ichdr, bp->b_addr); |
fde2227c DC |
216 | parent_blkno = bp->b_bn; |
217 | if (!ichdr.count) { | |
218 | xfs_trans_brelse(*trans, bp); | |
219 | return 0; | |
220 | } | |
51908ca7 | 221 | child_fsb = be32_to_cpu(ichdr.btree[0].before); |
fde2227c DC |
222 | xfs_trans_brelse(*trans, bp); /* no locks for later trans */ |
223 | ||
224 | /* | |
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. | |
228 | */ | |
229 | for (i = 0; i < ichdr.count; i++) { | |
230 | /* | |
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. | |
235 | */ | |
02c57f0a | 236 | error = xfs_da3_node_read(*trans, dp, child_fsb, &child_bp, |
a53efbd5 | 237 | XFS_ATTR_FORK); |
fde2227c | 238 | if (error) |
d99831ff | 239 | return error; |
fde2227c | 240 | |
a53efbd5 BF |
241 | /* save for re-read later */ |
242 | child_blkno = XFS_BUF_ADDR(child_bp); | |
fde2227c | 243 | |
a53efbd5 BF |
244 | /* |
245 | * Invalidate the subtree, however we have to. | |
246 | */ | |
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, | |
252 | level + 1); | |
253 | break; | |
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); | |
257 | break; | |
258 | default: | |
a5155b87 | 259 | xfs_buf_corruption_error(child_bp); |
a53efbd5 | 260 | xfs_trans_brelse(*trans, child_bp); |
a5155b87 | 261 | error = -EFSCORRUPTED; |
a53efbd5 | 262 | break; |
fde2227c | 263 | } |
a53efbd5 BF |
264 | if (error) |
265 | return error; | |
266 | ||
267 | /* | |
268 | * Remove the subsidiary block from the cache and from the log. | |
269 | */ | |
270 | error = xfs_da_get_buf(*trans, dp, 0, child_blkno, &child_bp, | |
271 | XFS_ATTR_FORK); | |
272 | if (error) | |
273 | return error; | |
274 | xfs_trans_binval(*trans, child_bp); | |
fde2227c DC |
275 | |
276 | /* | |
277 | * If we're not done, re-read the parent to get the next | |
278 | * child block number. | |
279 | */ | |
280 | if (i + 1 < ichdr.count) { | |
51908ca7 CH |
281 | struct xfs_da3_icnode_hdr phdr; |
282 | ||
02c57f0a CH |
283 | error = xfs_da3_node_read_mapped(*trans, dp, |
284 | parent_blkno, &bp, XFS_ATTR_FORK); | |
fde2227c DC |
285 | if (error) |
286 | return error; | |
51908ca7 CH |
287 | xfs_da3_node_hdr_from_disk(dp->i_mount, &phdr, |
288 | bp->b_addr); | |
289 | child_fsb = be32_to_cpu(phdr.btree[i + 1].before); | |
fde2227c DC |
290 | xfs_trans_brelse(*trans, bp); |
291 | } | |
292 | /* | |
293 | * Atomically commit the whole invalidate stuff. | |
294 | */ | |
411350df | 295 | error = xfs_trans_roll_inode(trans, dp); |
fde2227c DC |
296 | if (error) |
297 | return error; | |
298 | } | |
299 | ||
300 | return 0; | |
301 | } | |
302 | ||
303 | /* | |
304 | * Indiscriminately delete the entire attribute fork | |
305 | * | |
306 | * Recurse (gasp!) through the attribute nodes until we find leaves. | |
307 | * We're doing a depth-first traversal in order to invalidate everything. | |
308 | */ | |
0d5a75e9 | 309 | static int |
fde2227c DC |
310 | xfs_attr3_root_inactive( |
311 | struct xfs_trans **trans, | |
312 | struct xfs_inode *dp) | |
313 | { | |
314 | struct xfs_da_blkinfo *info; | |
315 | struct xfs_buf *bp; | |
316 | xfs_daddr_t blkno; | |
317 | int error; | |
318 | ||
319 | /* | |
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. | |
324 | */ | |
02c57f0a | 325 | error = xfs_da3_node_read(*trans, dp, 0, &bp, XFS_ATTR_FORK); |
fde2227c DC |
326 | if (error) |
327 | return error; | |
328 | blkno = bp->b_bn; | |
329 | ||
330 | /* | |
331 | * Invalidate the tree, even if the "tree" is only a single leaf block. | |
332 | * This is a depth-first traversal! | |
333 | */ | |
334 | info = bp->b_addr; | |
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); | |
339 | break; | |
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); | |
343 | break; | |
344 | default: | |
c2414ad6 | 345 | error = -EFSCORRUPTED; |
a5155b87 | 346 | xfs_buf_corruption_error(bp); |
fde2227c DC |
347 | xfs_trans_brelse(*trans, bp); |
348 | break; | |
349 | } | |
350 | if (error) | |
351 | return error; | |
352 | ||
353 | /* | |
354 | * Invalidate the incore copy of the root block. | |
355 | */ | |
356 | error = xfs_da_get_buf(*trans, dp, 0, blkno, &bp, XFS_ATTR_FORK); | |
357 | if (error) | |
358 | return error; | |
359 | xfs_trans_binval(*trans, bp); /* remove from cache */ | |
360 | /* | |
361 | * Commit the invalidate and start the next transaction. | |
362 | */ | |
411350df | 363 | error = xfs_trans_roll_inode(trans, dp); |
fde2227c DC |
364 | |
365 | return error; | |
366 | } | |
367 | ||
6dfe5a04 DC |
368 | /* |
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. | |
373 | * | |
374 | * The in-memory attribute fork is removed even on error. | |
375 | */ | |
fde2227c | 376 | int |
6dfe5a04 DC |
377 | xfs_attr_inactive( |
378 | struct xfs_inode *dp) | |
fde2227c | 379 | { |
6dfe5a04 DC |
380 | struct xfs_trans *trans; |
381 | struct xfs_mount *mp; | |
6dfe5a04 DC |
382 | int lock_mode = XFS_ILOCK_SHARED; |
383 | int error = 0; | |
fde2227c DC |
384 | |
385 | mp = dp->i_mount; | |
386 | ASSERT(! XFS_NOT_DQATTACHED(mp, dp)); | |
387 | ||
6dfe5a04 DC |
388 | xfs_ilock(dp, lock_mode); |
389 | if (!XFS_IFORK_Q(dp)) | |
390 | goto out_destroy_fork; | |
391 | xfs_iunlock(dp, lock_mode); | |
fde2227c | 392 | |
6dfe5a04 | 393 | lock_mode = 0; |
253f4911 CH |
394 | |
395 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_attrinval, 0, 0, 0, &trans); | |
6dfe5a04 | 396 | if (error) |
253f4911 | 397 | goto out_destroy_fork; |
6dfe5a04 DC |
398 | |
399 | lock_mode = XFS_ILOCK_EXCL; | |
6dfe5a04 DC |
400 | xfs_ilock(dp, lock_mode); |
401 | ||
402 | if (!XFS_IFORK_Q(dp)) | |
403 | goto out_cancel; | |
fde2227c DC |
404 | |
405 | /* | |
406 | * No need to make quota reservations here. We expect to release some | |
407 | * blocks, not allocate, in the common case. | |
408 | */ | |
409 | xfs_trans_ijoin(trans, dp, 0); | |
410 | ||
f66bf042 BF |
411 | /* |
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 | |
415 | * removal below. | |
416 | */ | |
417 | if (xfs_inode_hasattr(dp) && | |
418 | dp->i_d.di_aformat != XFS_DINODE_FMT_LOCAL) { | |
6dfe5a04 DC |
419 | error = xfs_attr3_root_inactive(&trans, dp); |
420 | if (error) | |
421 | goto out_cancel; | |
422 | ||
423 | error = xfs_itruncate_extents(&trans, dp, XFS_ATTR_FORK, 0); | |
424 | if (error) | |
425 | goto out_cancel; | |
fde2227c | 426 | } |
fde2227c | 427 | |
6dfe5a04 DC |
428 | /* Reset the attribute fork - this also destroys the in-core fork */ |
429 | xfs_attr_fork_remove(dp, trans); | |
fde2227c | 430 | |
70393313 | 431 | error = xfs_trans_commit(trans); |
6dfe5a04 | 432 | xfs_iunlock(dp, lock_mode); |
d99831ff | 433 | return error; |
fde2227c | 434 | |
6dfe5a04 | 435 | out_cancel: |
4906e215 | 436 | xfs_trans_cancel(trans); |
6dfe5a04 DC |
437 | out_destroy_fork: |
438 | /* kill the in-core attr fork before we drop the inode lock */ | |
439 | if (dp->i_afp) | |
440 | xfs_idestroy_fork(dp, XFS_ATTR_FORK); | |
441 | if (lock_mode) | |
442 | xfs_iunlock(dp, lock_mode); | |
d99831ff | 443 | return error; |
fde2227c | 444 | } |