blob: f33fb62b7f17d36c3cba87eac0cedaae8475fa49 [file] [log] [blame]
Dave Chinnerfde22272013-08-12 20:49:39 +10001/*
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"
Dave Chinner70a98832013-10-23 10:36:05 +110021#include "xfs_shared.h"
Dave Chinner239880e2013-10-23 10:50:10 +110022#include "xfs_format.h"
23#include "xfs_log_format.h"
24#include "xfs_trans_resv.h"
Dave Chinnerfde22272013-08-12 20:49:39 +100025#include "xfs_bit.h"
Dave Chinnerfde22272013-08-12 20:49:39 +100026#include "xfs_sb.h"
27#include "xfs_ag.h"
28#include "xfs_mount.h"
Dave Chinner57062782013-10-15 09:17:51 +110029#include "xfs_da_format.h"
Dave Chinnerfde22272013-08-12 20:49:39 +100030#include "xfs_da_btree.h"
Dave Chinnerfde22272013-08-12 20:49:39 +100031#include "xfs_inode.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110032#include "xfs_alloc.h"
33#include "xfs_attr_remote.h"
Dave Chinner239880e2013-10-23 10:50:10 +110034#include "xfs_trans.h"
Dave Chinnerfde22272013-08-12 20:49:39 +100035#include "xfs_inode_item.h"
36#include "xfs_bmap.h"
37#include "xfs_attr.h"
38#include "xfs_attr_leaf.h"
39#include "xfs_error.h"
40#include "xfs_quota.h"
41#include "xfs_trace.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110042#include "xfs_dinode.h"
Dave Chinnerfde22272013-08-12 20:49:39 +100043
44/*
45 * Look at all the extents for this logical region,
46 * invalidate any buffers that are incore/in transactions.
47 */
48STATIC int
49xfs_attr3_leaf_freextent(
50 struct xfs_trans **trans,
51 struct xfs_inode *dp,
52 xfs_dablk_t blkno,
53 int blkcnt)
54{
55 struct xfs_bmbt_irec map;
56 struct xfs_buf *bp;
57 xfs_dablk_t tblkno;
58 xfs_daddr_t dblkno;
59 int tblkcnt;
60 int dblkcnt;
61 int nmap;
62 int error;
63
64 /*
65 * Roll through the "value", invalidating the attribute value's
66 * blocks.
67 */
68 tblkno = blkno;
69 tblkcnt = blkcnt;
70 while (tblkcnt > 0) {
71 /*
72 * Try to remember where we decided to put the value.
73 */
74 nmap = 1;
75 error = xfs_bmapi_read(dp, (xfs_fileoff_t)tblkno, tblkcnt,
76 &map, &nmap, XFS_BMAPI_ATTRFORK);
77 if (error) {
78 return(error);
79 }
80 ASSERT(nmap == 1);
81 ASSERT(map.br_startblock != DELAYSTARTBLOCK);
82
83 /*
84 * If it's a hole, these are already unmapped
85 * so there's nothing to invalidate.
86 */
87 if (map.br_startblock != HOLESTARTBLOCK) {
88
89 dblkno = XFS_FSB_TO_DADDR(dp->i_mount,
90 map.br_startblock);
91 dblkcnt = XFS_FSB_TO_BB(dp->i_mount,
92 map.br_blockcount);
93 bp = xfs_trans_get_buf(*trans,
94 dp->i_mount->m_ddev_targp,
95 dblkno, dblkcnt, 0);
96 if (!bp)
97 return ENOMEM;
98 xfs_trans_binval(*trans, bp);
99 /*
100 * Roll to next transaction.
101 */
102 error = xfs_trans_roll(trans, dp);
103 if (error)
104 return (error);
105 }
106
107 tblkno += map.br_blockcount;
108 tblkcnt -= map.br_blockcount;
109 }
110
111 return(0);
112}
113
114/*
115 * Invalidate all of the "remote" value regions pointed to by a particular
116 * leaf block.
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.
119 */
120STATIC int
121xfs_attr3_leaf_inactive(
122 struct xfs_trans **trans,
123 struct xfs_inode *dp,
124 struct xfs_buf *bp)
125{
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;
132 int error;
133 int count;
134 int size;
135 int tmp;
136 int i;
137
138 leaf = bp->b_addr;
139 xfs_attr3_leaf_hdr_from_disk(&ichdr, leaf);
140
141 /*
142 * Count the number of "remote" value extents.
143 */
144 count = 0;
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)
151 count++;
152 }
153 }
154
155 /*
156 * If there are no "remote" values, we're done.
157 */
158 if (count == 0) {
159 xfs_trans_brelse(*trans, bp);
160 return 0;
161 }
162
163 /*
164 * Allocate storage for a list of all the "remote" value extents.
165 */
166 size = count * sizeof(xfs_attr_inactive_list_t);
167 list = kmem_alloc(size, KM_SLEEP);
168
169 /*
170 * Identify each of the "remote" value extents.
171 */
172 lp = list;
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));
182 lp++;
183 }
184 }
185 }
186 xfs_trans_brelse(*trans, bp); /* unlock for trans. in freextent() */
187
188 /*
189 * Invalidate each of the "remote" value extents.
190 */
191 error = 0;
192 for (lp = list, i = 0; i < count; i++, lp++) {
193 tmp = xfs_attr3_leaf_freextent(trans, dp,
194 lp->valueblk, lp->valuelen);
195
196 if (error == 0)
197 error = tmp; /* save only the 1st errno */
198 }
199
200 kmem_free(list);
201 return error;
202}
203
204/*
205 * Recurse (gasp!) through the attribute nodes until we find leaves.
206 * We're doing a depth-first traversal in order to invalidate everything.
207 */
208STATIC int
209xfs_attr3_node_inactive(
210 struct xfs_trans **trans,
211 struct xfs_inode *dp,
212 struct xfs_buf *bp,
213 int level)
214{
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;
219 int error, i;
220 struct xfs_buf *child_bp;
221 struct xfs_da_node_entry *btree;
222 struct xfs_da3_icnode_hdr ichdr;
223
224 /*
225 * Since this code is recursive (gasp!) we must protect ourselves.
226 */
227 if (level > XFS_DA_NODE_MAXDEPTH) {
228 xfs_trans_brelse(*trans, bp); /* no locks for later trans */
229 return XFS_ERROR(EIO);
230 }
231
232 node = bp->b_addr;
233 xfs_da3_node_hdr_from_disk(&ichdr, node);
234 parent_blkno = bp->b_bn;
235 if (!ichdr.count) {
236 xfs_trans_brelse(*trans, bp);
237 return 0;
238 }
239 btree = xfs_da3_node_tree_p(node);
240 child_fsb = be32_to_cpu(btree[0].before);
241 xfs_trans_brelse(*trans, bp); /* no locks for later trans */
242
243 /*
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.
247 */
248 for (i = 0; i < ichdr.count; i++) {
249 /*
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.
254 */
255 error = xfs_da3_node_read(*trans, dp, child_fsb, -2, &child_bp,
256 XFS_ATTR_FORK);
257 if (error)
258 return(error);
259 if (child_bp) {
260 /* save for re-read later */
261 child_blkno = XFS_BUF_ADDR(child_bp);
262
263 /*
264 * Invalidate the subtree, however we have to.
265 */
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);
272 break;
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,
276 child_bp);
277 break;
278 default:
279 error = XFS_ERROR(EIO);
280 xfs_trans_brelse(*trans, child_bp);
281 break;
282 }
283 if (error)
284 return error;
285
286 /*
287 * Remove the subsidiary block from the cache
288 * and from the log.
289 */
290 error = xfs_da_get_buf(*trans, dp, 0, child_blkno,
291 &child_bp, XFS_ATTR_FORK);
292 if (error)
293 return error;
294 xfs_trans_binval(*trans, child_bp);
295 }
296
297 /*
298 * If we're not done, re-read the parent to get the next
299 * child block number.
300 */
301 if (i + 1 < ichdr.count) {
302 error = xfs_da3_node_read(*trans, dp, 0, parent_blkno,
303 &bp, XFS_ATTR_FORK);
304 if (error)
305 return error;
306 child_fsb = be32_to_cpu(btree[i + 1].before);
307 xfs_trans_brelse(*trans, bp);
308 }
309 /*
310 * Atomically commit the whole invalidate stuff.
311 */
312 error = xfs_trans_roll(trans, dp);
313 if (error)
314 return error;
315 }
316
317 return 0;
318}
319
320/*
321 * Indiscriminately delete the entire attribute fork
322 *
323 * Recurse (gasp!) through the attribute nodes until we find leaves.
324 * We're doing a depth-first traversal in order to invalidate everything.
325 */
326int
327xfs_attr3_root_inactive(
328 struct xfs_trans **trans,
329 struct xfs_inode *dp)
330{
331 struct xfs_da_blkinfo *info;
332 struct xfs_buf *bp;
333 xfs_daddr_t blkno;
334 int error;
335
336 /*
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.
341 */
342 error = xfs_da3_node_read(*trans, dp, 0, -1, &bp, XFS_ATTR_FORK);
343 if (error)
344 return error;
345 blkno = bp->b_bn;
346
347 /*
348 * Invalidate the tree, even if the "tree" is only a single leaf block.
349 * This is a depth-first traversal!
350 */
351 info = bp->b_addr;
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);
356 break;
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);
360 break;
361 default:
362 error = XFS_ERROR(EIO);
363 xfs_trans_brelse(*trans, bp);
364 break;
365 }
366 if (error)
367 return error;
368
369 /*
370 * Invalidate the incore copy of the root block.
371 */
372 error = xfs_da_get_buf(*trans, dp, 0, blkno, &bp, XFS_ATTR_FORK);
373 if (error)
374 return error;
375 xfs_trans_binval(*trans, bp); /* remove from cache */
376 /*
377 * Commit the invalidate and start the next transaction.
378 */
379 error = xfs_trans_roll(trans, dp);
380
381 return error;
382}
383
384int
385xfs_attr_inactive(xfs_inode_t *dp)
386{
387 xfs_trans_t *trans;
388 xfs_mount_t *mp;
389 int error;
390
391 mp = dp->i_mount;
392 ASSERT(! XFS_NOT_DQATTACHED(mp, dp));
393
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);
398 return 0;
399 }
400 xfs_iunlock(dp, XFS_ILOCK_SHARED);
401
402 /*
403 * Start our first transaction of the day.
404 *
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
410 * the log.
411 */
412 trans = xfs_trans_alloc(mp, XFS_TRANS_ATTRINVAL);
Jie Liu3d3c8b52013-08-12 20:49:59 +1000413 error = xfs_trans_reserve(trans, &M_RES(mp)->tr_attrinval, 0, 0);
414 if (error) {
Dave Chinnerfde22272013-08-12 20:49:39 +1000415 xfs_trans_cancel(trans, 0);
416 return(error);
417 }
418 xfs_ilock(dp, XFS_ILOCK_EXCL);
419
420 /*
421 * No need to make quota reservations here. We expect to release some
422 * blocks, not allocate, in the common case.
423 */
424 xfs_trans_ijoin(trans, dp, 0);
425
426 /*
427 * Decide on what work routines to call based on the inode size.
428 */
429 if (!xfs_inode_hasattr(dp) ||
430 dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
431 error = 0;
432 goto out;
433 }
434 error = xfs_attr3_root_inactive(&trans, dp);
435 if (error)
436 goto out;
437
438 error = xfs_itruncate_extents(&trans, dp, XFS_ATTR_FORK, 0);
439 if (error)
440 goto out;
441
442 error = xfs_trans_commit(trans, XFS_TRANS_RELEASE_LOG_RES);
443 xfs_iunlock(dp, XFS_ILOCK_EXCL);
444
445 return(error);
446
447out:
448 xfs_trans_cancel(trans, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
449 xfs_iunlock(dp, XFS_ILOCK_EXCL);
450 return(error);
451}