]>
Commit | Line | Data |
---|---|---|
fde2227c DC |
1 | /* |
2 | * Copyright (c) 2000-2005 Silicon Graphics, Inc. | |
3 | * Copyright (c) 2013 Red Hat, Inc. | |
4 | * All Rights Reserved. | |
5 | * | |
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. | |
9 | * | |
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. | |
14 | * | |
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 | |
18 | */ | |
19 | #include "xfs.h" | |
20 | #include "xfs_fs.h" | |
70a9883c | 21 | #include "xfs_shared.h" |
239880ef DC |
22 | #include "xfs_format.h" |
23 | #include "xfs_log_format.h" | |
24 | #include "xfs_trans_resv.h" | |
fde2227c | 25 | #include "xfs_bit.h" |
fde2227c DC |
26 | #include "xfs_sb.h" |
27 | #include "xfs_ag.h" | |
28 | #include "xfs_mount.h" | |
57062787 | 29 | #include "xfs_da_format.h" |
fde2227c DC |
30 | #include "xfs_da_btree.h" |
31 | #include "xfs_bmap_btree.h" | |
32 | #include "xfs_alloc_btree.h" | |
33 | #include "xfs_ialloc_btree.h" | |
34 | #include "xfs_alloc.h" | |
35 | #include "xfs_btree.h" | |
36 | #include "xfs_attr_remote.h" | |
37 | #include "xfs_dinode.h" | |
38 | #include "xfs_inode.h" | |
239880ef | 39 | #include "xfs_trans.h" |
fde2227c DC |
40 | #include "xfs_inode_item.h" |
41 | #include "xfs_bmap.h" | |
42 | #include "xfs_attr.h" | |
43 | #include "xfs_attr_leaf.h" | |
44 | #include "xfs_error.h" | |
45 | #include "xfs_quota.h" | |
46 | #include "xfs_trace.h" | |
fde2227c DC |
47 | |
48 | /* | |
49 | * Look at all the extents for this logical region, | |
50 | * invalidate any buffers that are incore/in transactions. | |
51 | */ | |
52 | STATIC int | |
53 | xfs_attr3_leaf_freextent( | |
54 | struct xfs_trans **trans, | |
55 | struct xfs_inode *dp, | |
56 | xfs_dablk_t blkno, | |
57 | int blkcnt) | |
58 | { | |
59 | struct xfs_bmbt_irec map; | |
60 | struct xfs_buf *bp; | |
61 | xfs_dablk_t tblkno; | |
62 | xfs_daddr_t dblkno; | |
63 | int tblkcnt; | |
64 | int dblkcnt; | |
65 | int nmap; | |
66 | int error; | |
67 | ||
68 | /* | |
69 | * Roll through the "value", invalidating the attribute value's | |
70 | * blocks. | |
71 | */ | |
72 | tblkno = blkno; | |
73 | tblkcnt = blkcnt; | |
74 | while (tblkcnt > 0) { | |
75 | /* | |
76 | * Try to remember where we decided to put the value. | |
77 | */ | |
78 | nmap = 1; | |
79 | error = xfs_bmapi_read(dp, (xfs_fileoff_t)tblkno, tblkcnt, | |
80 | &map, &nmap, XFS_BMAPI_ATTRFORK); | |
81 | if (error) { | |
82 | return(error); | |
83 | } | |
84 | ASSERT(nmap == 1); | |
85 | ASSERT(map.br_startblock != DELAYSTARTBLOCK); | |
86 | ||
87 | /* | |
88 | * If it's a hole, these are already unmapped | |
89 | * so there's nothing to invalidate. | |
90 | */ | |
91 | if (map.br_startblock != HOLESTARTBLOCK) { | |
92 | ||
93 | dblkno = XFS_FSB_TO_DADDR(dp->i_mount, | |
94 | map.br_startblock); | |
95 | dblkcnt = XFS_FSB_TO_BB(dp->i_mount, | |
96 | map.br_blockcount); | |
97 | bp = xfs_trans_get_buf(*trans, | |
98 | dp->i_mount->m_ddev_targp, | |
99 | dblkno, dblkcnt, 0); | |
100 | if (!bp) | |
101 | return ENOMEM; | |
102 | xfs_trans_binval(*trans, bp); | |
103 | /* | |
104 | * Roll to next transaction. | |
105 | */ | |
106 | error = xfs_trans_roll(trans, dp); | |
107 | if (error) | |
108 | return (error); | |
109 | } | |
110 | ||
111 | tblkno += map.br_blockcount; | |
112 | tblkcnt -= map.br_blockcount; | |
113 | } | |
114 | ||
115 | return(0); | |
116 | } | |
117 | ||
118 | /* | |
119 | * Invalidate all of the "remote" value regions pointed to by a particular | |
120 | * leaf block. | |
121 | * Note that we must release the lock on the buffer so that we are not | |
122 | * caught holding something that the logging code wants to flush to disk. | |
123 | */ | |
124 | STATIC int | |
125 | xfs_attr3_leaf_inactive( | |
126 | struct xfs_trans **trans, | |
127 | struct xfs_inode *dp, | |
128 | struct xfs_buf *bp) | |
129 | { | |
130 | struct xfs_attr_leafblock *leaf; | |
131 | struct xfs_attr3_icleaf_hdr ichdr; | |
132 | struct xfs_attr_leaf_entry *entry; | |
133 | struct xfs_attr_leaf_name_remote *name_rmt; | |
134 | struct xfs_attr_inactive_list *list; | |
135 | struct xfs_attr_inactive_list *lp; | |
136 | int error; | |
137 | int count; | |
138 | int size; | |
139 | int tmp; | |
140 | int i; | |
141 | ||
142 | leaf = bp->b_addr; | |
143 | xfs_attr3_leaf_hdr_from_disk(&ichdr, leaf); | |
144 | ||
145 | /* | |
146 | * Count the number of "remote" value extents. | |
147 | */ | |
148 | count = 0; | |
149 | entry = xfs_attr3_leaf_entryp(leaf); | |
150 | for (i = 0; i < ichdr.count; entry++, i++) { | |
151 | if (be16_to_cpu(entry->nameidx) && | |
152 | ((entry->flags & XFS_ATTR_LOCAL) == 0)) { | |
153 | name_rmt = xfs_attr3_leaf_name_remote(leaf, i); | |
154 | if (name_rmt->valueblk) | |
155 | count++; | |
156 | } | |
157 | } | |
158 | ||
159 | /* | |
160 | * If there are no "remote" values, we're done. | |
161 | */ | |
162 | if (count == 0) { | |
163 | xfs_trans_brelse(*trans, bp); | |
164 | return 0; | |
165 | } | |
166 | ||
167 | /* | |
168 | * Allocate storage for a list of all the "remote" value extents. | |
169 | */ | |
170 | size = count * sizeof(xfs_attr_inactive_list_t); | |
171 | list = kmem_alloc(size, KM_SLEEP); | |
172 | ||
173 | /* | |
174 | * Identify each of the "remote" value extents. | |
175 | */ | |
176 | lp = list; | |
177 | entry = xfs_attr3_leaf_entryp(leaf); | |
178 | for (i = 0; i < ichdr.count; entry++, i++) { | |
179 | if (be16_to_cpu(entry->nameidx) && | |
180 | ((entry->flags & XFS_ATTR_LOCAL) == 0)) { | |
181 | name_rmt = xfs_attr3_leaf_name_remote(leaf, i); | |
182 | if (name_rmt->valueblk) { | |
183 | lp->valueblk = be32_to_cpu(name_rmt->valueblk); | |
184 | lp->valuelen = xfs_attr3_rmt_blocks(dp->i_mount, | |
185 | be32_to_cpu(name_rmt->valuelen)); | |
186 | lp++; | |
187 | } | |
188 | } | |
189 | } | |
190 | xfs_trans_brelse(*trans, bp); /* unlock for trans. in freextent() */ | |
191 | ||
192 | /* | |
193 | * Invalidate each of the "remote" value extents. | |
194 | */ | |
195 | error = 0; | |
196 | for (lp = list, i = 0; i < count; i++, lp++) { | |
197 | tmp = xfs_attr3_leaf_freextent(trans, dp, | |
198 | lp->valueblk, lp->valuelen); | |
199 | ||
200 | if (error == 0) | |
201 | error = tmp; /* save only the 1st errno */ | |
202 | } | |
203 | ||
204 | kmem_free(list); | |
205 | return error; | |
206 | } | |
207 | ||
208 | /* | |
209 | * Recurse (gasp!) through the attribute nodes until we find leaves. | |
210 | * We're doing a depth-first traversal in order to invalidate everything. | |
211 | */ | |
212 | STATIC int | |
213 | xfs_attr3_node_inactive( | |
214 | struct xfs_trans **trans, | |
215 | struct xfs_inode *dp, | |
216 | struct xfs_buf *bp, | |
217 | int level) | |
218 | { | |
219 | xfs_da_blkinfo_t *info; | |
220 | xfs_da_intnode_t *node; | |
221 | xfs_dablk_t child_fsb; | |
222 | xfs_daddr_t parent_blkno, child_blkno; | |
223 | int error, i; | |
224 | struct xfs_buf *child_bp; | |
225 | struct xfs_da_node_entry *btree; | |
226 | struct xfs_da3_icnode_hdr ichdr; | |
227 | ||
228 | /* | |
229 | * Since this code is recursive (gasp!) we must protect ourselves. | |
230 | */ | |
231 | if (level > XFS_DA_NODE_MAXDEPTH) { | |
232 | xfs_trans_brelse(*trans, bp); /* no locks for later trans */ | |
233 | return XFS_ERROR(EIO); | |
234 | } | |
235 | ||
236 | node = bp->b_addr; | |
237 | xfs_da3_node_hdr_from_disk(&ichdr, node); | |
238 | parent_blkno = bp->b_bn; | |
239 | if (!ichdr.count) { | |
240 | xfs_trans_brelse(*trans, bp); | |
241 | return 0; | |
242 | } | |
243 | btree = xfs_da3_node_tree_p(node); | |
244 | child_fsb = be32_to_cpu(btree[0].before); | |
245 | xfs_trans_brelse(*trans, bp); /* no locks for later trans */ | |
246 | ||
247 | /* | |
248 | * If this is the node level just above the leaves, simply loop | |
249 | * over the leaves removing all of them. If this is higher up | |
250 | * in the tree, recurse downward. | |
251 | */ | |
252 | for (i = 0; i < ichdr.count; i++) { | |
253 | /* | |
254 | * Read the subsidiary block to see what we have to work with. | |
255 | * Don't do this in a transaction. This is a depth-first | |
256 | * traversal of the tree so we may deal with many blocks | |
257 | * before we come back to this one. | |
258 | */ | |
259 | error = xfs_da3_node_read(*trans, dp, child_fsb, -2, &child_bp, | |
260 | XFS_ATTR_FORK); | |
261 | if (error) | |
262 | return(error); | |
263 | if (child_bp) { | |
264 | /* save for re-read later */ | |
265 | child_blkno = XFS_BUF_ADDR(child_bp); | |
266 | ||
267 | /* | |
268 | * Invalidate the subtree, however we have to. | |
269 | */ | |
270 | info = child_bp->b_addr; | |
271 | switch (info->magic) { | |
272 | case cpu_to_be16(XFS_DA_NODE_MAGIC): | |
273 | case cpu_to_be16(XFS_DA3_NODE_MAGIC): | |
274 | error = xfs_attr3_node_inactive(trans, dp, | |
275 | child_bp, level + 1); | |
276 | break; | |
277 | case cpu_to_be16(XFS_ATTR_LEAF_MAGIC): | |
278 | case cpu_to_be16(XFS_ATTR3_LEAF_MAGIC): | |
279 | error = xfs_attr3_leaf_inactive(trans, dp, | |
280 | child_bp); | |
281 | break; | |
282 | default: | |
283 | error = XFS_ERROR(EIO); | |
284 | xfs_trans_brelse(*trans, child_bp); | |
285 | break; | |
286 | } | |
287 | if (error) | |
288 | return error; | |
289 | ||
290 | /* | |
291 | * Remove the subsidiary block from the cache | |
292 | * and from the log. | |
293 | */ | |
294 | error = xfs_da_get_buf(*trans, dp, 0, child_blkno, | |
295 | &child_bp, XFS_ATTR_FORK); | |
296 | if (error) | |
297 | return error; | |
298 | xfs_trans_binval(*trans, child_bp); | |
299 | } | |
300 | ||
301 | /* | |
302 | * If we're not done, re-read the parent to get the next | |
303 | * child block number. | |
304 | */ | |
305 | if (i + 1 < ichdr.count) { | |
306 | error = xfs_da3_node_read(*trans, dp, 0, parent_blkno, | |
307 | &bp, XFS_ATTR_FORK); | |
308 | if (error) | |
309 | return error; | |
310 | child_fsb = be32_to_cpu(btree[i + 1].before); | |
311 | xfs_trans_brelse(*trans, bp); | |
312 | } | |
313 | /* | |
314 | * Atomically commit the whole invalidate stuff. | |
315 | */ | |
316 | error = xfs_trans_roll(trans, dp); | |
317 | if (error) | |
318 | return error; | |
319 | } | |
320 | ||
321 | return 0; | |
322 | } | |
323 | ||
324 | /* | |
325 | * Indiscriminately delete the entire attribute fork | |
326 | * | |
327 | * Recurse (gasp!) through the attribute nodes until we find leaves. | |
328 | * We're doing a depth-first traversal in order to invalidate everything. | |
329 | */ | |
330 | int | |
331 | xfs_attr3_root_inactive( | |
332 | struct xfs_trans **trans, | |
333 | struct xfs_inode *dp) | |
334 | { | |
335 | struct xfs_da_blkinfo *info; | |
336 | struct xfs_buf *bp; | |
337 | xfs_daddr_t blkno; | |
338 | int error; | |
339 | ||
340 | /* | |
341 | * Read block 0 to see what we have to work with. | |
342 | * We only get here if we have extents, since we remove | |
343 | * the extents in reverse order the extent containing | |
344 | * block 0 must still be there. | |
345 | */ | |
346 | error = xfs_da3_node_read(*trans, dp, 0, -1, &bp, XFS_ATTR_FORK); | |
347 | if (error) | |
348 | return error; | |
349 | blkno = bp->b_bn; | |
350 | ||
351 | /* | |
352 | * Invalidate the tree, even if the "tree" is only a single leaf block. | |
353 | * This is a depth-first traversal! | |
354 | */ | |
355 | info = bp->b_addr; | |
356 | switch (info->magic) { | |
357 | case cpu_to_be16(XFS_DA_NODE_MAGIC): | |
358 | case cpu_to_be16(XFS_DA3_NODE_MAGIC): | |
359 | error = xfs_attr3_node_inactive(trans, dp, bp, 1); | |
360 | break; | |
361 | case cpu_to_be16(XFS_ATTR_LEAF_MAGIC): | |
362 | case cpu_to_be16(XFS_ATTR3_LEAF_MAGIC): | |
363 | error = xfs_attr3_leaf_inactive(trans, dp, bp); | |
364 | break; | |
365 | default: | |
366 | error = XFS_ERROR(EIO); | |
367 | xfs_trans_brelse(*trans, bp); | |
368 | break; | |
369 | } | |
370 | if (error) | |
371 | return error; | |
372 | ||
373 | /* | |
374 | * Invalidate the incore copy of the root block. | |
375 | */ | |
376 | error = xfs_da_get_buf(*trans, dp, 0, blkno, &bp, XFS_ATTR_FORK); | |
377 | if (error) | |
378 | return error; | |
379 | xfs_trans_binval(*trans, bp); /* remove from cache */ | |
380 | /* | |
381 | * Commit the invalidate and start the next transaction. | |
382 | */ | |
383 | error = xfs_trans_roll(trans, dp); | |
384 | ||
385 | return error; | |
386 | } | |
387 | ||
388 | int | |
389 | xfs_attr_inactive(xfs_inode_t *dp) | |
390 | { | |
391 | xfs_trans_t *trans; | |
392 | xfs_mount_t *mp; | |
393 | int error; | |
394 | ||
395 | mp = dp->i_mount; | |
396 | ASSERT(! XFS_NOT_DQATTACHED(mp, dp)); | |
397 | ||
398 | xfs_ilock(dp, XFS_ILOCK_SHARED); | |
399 | if (!xfs_inode_hasattr(dp) || | |
400 | dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) { | |
401 | xfs_iunlock(dp, XFS_ILOCK_SHARED); | |
402 | return 0; | |
403 | } | |
404 | xfs_iunlock(dp, XFS_ILOCK_SHARED); | |
405 | ||
406 | /* | |
407 | * Start our first transaction of the day. | |
408 | * | |
409 | * All future transactions during this code must be "chained" off | |
410 | * this one via the trans_dup() call. All transactions will contain | |
411 | * the inode, and the inode will always be marked with trans_ihold(). | |
412 | * Since the inode will be locked in all transactions, we must log | |
413 | * the inode in every transaction to let it float upward through | |
414 | * the log. | |
415 | */ | |
416 | trans = xfs_trans_alloc(mp, XFS_TRANS_ATTRINVAL); | |
3d3c8b52 JL |
417 | error = xfs_trans_reserve(trans, &M_RES(mp)->tr_attrinval, 0, 0); |
418 | if (error) { | |
fde2227c DC |
419 | xfs_trans_cancel(trans, 0); |
420 | return(error); | |
421 | } | |
422 | xfs_ilock(dp, XFS_ILOCK_EXCL); | |
423 | ||
424 | /* | |
425 | * No need to make quota reservations here. We expect to release some | |
426 | * blocks, not allocate, in the common case. | |
427 | */ | |
428 | xfs_trans_ijoin(trans, dp, 0); | |
429 | ||
430 | /* | |
431 | * Decide on what work routines to call based on the inode size. | |
432 | */ | |
433 | if (!xfs_inode_hasattr(dp) || | |
434 | dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) { | |
435 | error = 0; | |
436 | goto out; | |
437 | } | |
438 | error = xfs_attr3_root_inactive(&trans, dp); | |
439 | if (error) | |
440 | goto out; | |
441 | ||
442 | error = xfs_itruncate_extents(&trans, dp, XFS_ATTR_FORK, 0); | |
443 | if (error) | |
444 | goto out; | |
445 | ||
446 | error = xfs_trans_commit(trans, XFS_TRANS_RELEASE_LOG_RES); | |
447 | xfs_iunlock(dp, XFS_ILOCK_EXCL); | |
448 | ||
449 | return(error); | |
450 | ||
451 | out: | |
452 | xfs_trans_cancel(trans, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT); | |
453 | xfs_iunlock(dp, XFS_ILOCK_EXCL); | |
454 | return(error); | |
455 | } |