OSDN Git Service

xfs: split xfs_da3_node_read
[tomoyo/tomoyo-test1.git] / fs / xfs / xfs_attr_inactive.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4  * Copyright (c) 2013 Red Hat, Inc.
5  * All Rights Reserved.
6  */
7 #include "xfs.h"
8 #include "xfs_fs.h"
9 #include "xfs_shared.h"
10 #include "xfs_format.h"
11 #include "xfs_log_format.h"
12 #include "xfs_trans_resv.h"
13 #include "xfs_bit.h"
14 #include "xfs_mount.h"
15 #include "xfs_da_format.h"
16 #include "xfs_da_btree.h"
17 #include "xfs_inode.h"
18 #include "xfs_attr_remote.h"
19 #include "xfs_trans.h"
20 #include "xfs_bmap.h"
21 #include "xfs_attr.h"
22 #include "xfs_attr_leaf.h"
23 #include "xfs_quota.h"
24 #include "xfs_dir2.h"
25 #include "xfs_error.h"
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) {
61                         return error;
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)
80                                 return -ENOMEM;
81                         xfs_trans_binval(*trans, bp);
82                         /*
83                          * Roll to next transaction.
84                          */
85                         error = xfs_trans_roll_inode(trans, dp);
86                         if (error)
87                                 return error;
88                 }
89
90                 tblkno += map.br_blockcount;
91                 tblkcnt -= map.br_blockcount;
92         }
93
94         return 0;
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;
120         struct xfs_mount        *mp = bp->b_mount;
121
122         leaf = bp->b_addr;
123         xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr, leaf);
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);
151         list = kmem_alloc(size, 0);
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(
194         struct xfs_trans        **trans,
195         struct xfs_inode        *dp,
196         struct xfs_buf          *bp,
197         int                     level)
198 {
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;
203         struct xfs_da3_icnode_hdr ichdr;
204         int                     error, i;
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 */
211                 xfs_buf_corruption_error(bp);
212                 return -EFSCORRUPTED;
213         }
214
215         xfs_da3_node_hdr_from_disk(dp->i_mount, &ichdr, bp->b_addr);
216         parent_blkno = bp->b_bn;
217         if (!ichdr.count) {
218                 xfs_trans_brelse(*trans, bp);
219                 return 0;
220         }
221         child_fsb = be32_to_cpu(ichdr.btree[0].before);
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                  */
236                 error = xfs_da3_node_read(*trans, dp, child_fsb, &child_bp,
237                                           XFS_ATTR_FORK);
238                 if (error)
239                         return error;
240
241                 /* save for re-read later */
242                 child_blkno = XFS_BUF_ADDR(child_bp);
243
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:
259                         xfs_buf_corruption_error(child_bp);
260                         xfs_trans_brelse(*trans, child_bp);
261                         error = -EFSCORRUPTED;
262                         break;
263                 }
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);
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) {
281                         struct xfs_da3_icnode_hdr phdr;
282
283                         error = xfs_da3_node_read_mapped(*trans, dp,
284                                         parent_blkno, &bp, XFS_ATTR_FORK);
285                         if (error)
286                                 return error;
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);
290                         xfs_trans_brelse(*trans, bp);
291                 }
292                 /*
293                  * Atomically commit the whole invalidate stuff.
294                  */
295                 error = xfs_trans_roll_inode(trans, dp);
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  */
309 static int
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          */
325         error = xfs_da3_node_read(*trans, dp, 0, &bp, XFS_ATTR_FORK);
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:
345                 error = -EFSCORRUPTED;
346                 xfs_buf_corruption_error(bp);
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          */
363         error = xfs_trans_roll_inode(trans, dp);
364
365         return error;
366 }
367
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  */
376 int
377 xfs_attr_inactive(
378         struct xfs_inode        *dp)
379 {
380         struct xfs_trans        *trans;
381         struct xfs_mount        *mp;
382         int                     lock_mode = XFS_ILOCK_SHARED;
383         int                     error = 0;
384
385         mp = dp->i_mount;
386         ASSERT(! XFS_NOT_DQATTACHED(mp, dp));
387
388         xfs_ilock(dp, lock_mode);
389         if (!XFS_IFORK_Q(dp))
390                 goto out_destroy_fork;
391         xfs_iunlock(dp, lock_mode);
392
393         lock_mode = 0;
394
395         error = xfs_trans_alloc(mp, &M_RES(mp)->tr_attrinval, 0, 0, 0, &trans);
396         if (error)
397                 goto out_destroy_fork;
398
399         lock_mode = XFS_ILOCK_EXCL;
400         xfs_ilock(dp, lock_mode);
401
402         if (!XFS_IFORK_Q(dp))
403                 goto out_cancel;
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
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) {
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;
426         }
427
428         /* Reset the attribute fork - this also destroys the in-core fork */
429         xfs_attr_fork_remove(dp, trans);
430
431         error = xfs_trans_commit(trans);
432         xfs_iunlock(dp, lock_mode);
433         return error;
434
435 out_cancel:
436         xfs_trans_cancel(trans);
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);
443         return error;
444 }