1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4 * Copyright (c) 2013 Red Hat, Inc.
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_mount.h"
14 #include "xfs_da_format.h"
15 #include "xfs_inode.h"
16 #include "xfs_trans.h"
18 #include "xfs_da_btree.h"
20 #include "xfs_attr_sf.h"
21 #include "xfs_attr_leaf.h"
22 #include "xfs_error.h"
23 #include "xfs_trace.h"
25 #include "xfs_health.h"
28 xfs_attr_shortform_compare(const void *a, const void *b)
30 xfs_attr_sf_sort_t *sa, *sb;
32 sa = (xfs_attr_sf_sort_t *)a;
33 sb = (xfs_attr_sf_sort_t *)b;
34 if (sa->hash < sb->hash) {
36 } else if (sa->hash > sb->hash) {
39 return sa->entno - sb->entno;
43 #define XFS_ISRESET_CURSOR(cursor) \
44 (!((cursor)->initted) && !((cursor)->hashval) && \
45 !((cursor)->blkno) && !((cursor)->offset))
47 * Copy out entries of shortform attribute lists for attr_list().
48 * Shortform attribute lists are not stored in hashval sorted order.
49 * If the output buffer is not large enough to hold them all, then
50 * we have to calculate each entries' hashvalue and sort them before
51 * we can begin returning them to the user.
54 xfs_attr_shortform_list(
55 struct xfs_attr_list_context *context)
57 struct xfs_attrlist_cursor_kern *cursor = &context->cursor;
58 struct xfs_inode *dp = context->dp;
59 struct xfs_attr_sf_sort *sbuf, *sbp;
60 struct xfs_attr_sf_hdr *sf = dp->i_af.if_data;
61 struct xfs_attr_sf_entry *sfe;
62 int sbsize, nsbuf, count, i;
69 trace_xfs_attr_list_sf(context);
72 * If the buffer is large enough and the cursor is at the start,
73 * do not bother with sorting since we will return everything in
74 * one buffer and another call using the cursor won't need to be
76 * Note the generous fudge factor of 16 overhead bytes per entry.
77 * If bufsize is zero then put_listent must be a search function
78 * and can just scan through what we have.
80 if (context->bufsize == 0 ||
81 (XFS_ISRESET_CURSOR(cursor) &&
82 (dp->i_af.if_bytes + sf->count * 16) < context->bufsize)) {
83 for (i = 0, sfe = xfs_attr_sf_firstentry(sf); i < sf->count; i++) {
84 if (XFS_IS_CORRUPT(context->dp->i_mount,
85 !xfs_attr_namecheck(sfe->nameval,
87 xfs_dirattr_mark_sick(context->dp, XFS_ATTR_FORK);
90 context->put_listent(context,
96 * Either search callback finished early or
97 * didn't fit it all in the buffer after all.
99 if (context->seen_enough)
101 sfe = xfs_attr_sf_nextentry(sfe);
103 trace_xfs_attr_list_sf_all(context);
107 /* do no more for a search callback */
108 if (context->bufsize == 0)
112 * It didn't all fit, so we have to sort everything on hashval.
114 sbsize = sf->count * sizeof(*sbuf);
115 sbp = sbuf = kmalloc(sbsize, GFP_KERNEL | __GFP_NOFAIL);
118 * Scan the attribute list for the rest of the entries, storing
119 * the relevant info from only those that match into a buffer.
122 for (i = 0, sfe = xfs_attr_sf_firstentry(sf); i < sf->count; i++) {
124 ((char *)sfe < (char *)sf) ||
125 ((char *)sfe >= ((char *)sf + dp->i_af.if_bytes)))) {
126 XFS_CORRUPTION_ERROR("xfs_attr_shortform_list",
128 context->dp->i_mount, sfe,
131 xfs_dirattr_mark_sick(dp, XFS_ATTR_FORK);
132 return -EFSCORRUPTED;
136 sbp->hash = xfs_da_hashname(sfe->nameval, sfe->namelen);
137 sbp->name = sfe->nameval;
138 sbp->namelen = sfe->namelen;
139 /* These are bytes, and both on-disk, don't endian-flip */
140 sbp->valuelen = sfe->valuelen;
141 sbp->flags = sfe->flags;
142 sfe = xfs_attr_sf_nextentry(sfe);
148 * Sort the entries on hash then entno.
150 xfs_sort(sbuf, nsbuf, sizeof(*sbuf), xfs_attr_shortform_compare);
153 * Re-find our place IN THE SORTED LIST.
158 for (sbp = sbuf, i = 0; i < nsbuf; i++, sbp++) {
159 if (sbp->hash == cursor->hashval) {
160 if (cursor->offset == count) {
164 } else if (sbp->hash > cursor->hashval) {
172 * Loop putting entries into the user buffer.
174 for ( ; i < nsbuf; i++, sbp++) {
175 if (cursor->hashval != sbp->hash) {
176 cursor->hashval = sbp->hash;
179 if (XFS_IS_CORRUPT(context->dp->i_mount,
180 !xfs_attr_namecheck(sbp->name,
182 xfs_dirattr_mark_sick(context->dp, XFS_ATTR_FORK);
183 error = -EFSCORRUPTED;
186 context->put_listent(context,
191 if (context->seen_enough)
201 * We didn't find the block & hash mentioned in the cursor state, so
202 * walk down the attr btree looking for the hash.
205 xfs_attr_node_list_lookup(
206 struct xfs_attr_list_context *context,
207 struct xfs_attrlist_cursor_kern *cursor,
208 struct xfs_buf **pbp)
210 struct xfs_da3_icnode_hdr nodehdr;
211 struct xfs_da_intnode *node;
212 struct xfs_da_node_entry *btree;
213 struct xfs_inode *dp = context->dp;
214 struct xfs_mount *mp = dp->i_mount;
215 struct xfs_trans *tp = context->tp;
219 unsigned int expected_level = 0;
222 ASSERT(*pbp == NULL);
225 error = xfs_da3_node_read(tp, dp, cursor->blkno, &bp,
230 magic = be16_to_cpu(node->hdr.info.magic);
231 if (magic == XFS_ATTR_LEAF_MAGIC ||
232 magic == XFS_ATTR3_LEAF_MAGIC)
234 if (magic != XFS_DA_NODE_MAGIC &&
235 magic != XFS_DA3_NODE_MAGIC) {
236 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
237 node, sizeof(*node));
241 xfs_da3_node_hdr_from_disk(mp, &nodehdr, node);
243 /* Tree taller than we can handle; bail out! */
244 if (nodehdr.level >= XFS_DA_NODE_MAXDEPTH)
247 /* Check the level from the root node. */
248 if (cursor->blkno == 0)
249 expected_level = nodehdr.level - 1;
250 else if (expected_level != nodehdr.level)
255 btree = nodehdr.btree;
256 for (i = 0; i < nodehdr.count; btree++, i++) {
257 if (cursor->hashval <= be32_to_cpu(btree->hashval)) {
258 cursor->blkno = be32_to_cpu(btree->before);
259 trace_xfs_attr_list_node_descend(context,
264 xfs_trans_brelse(tp, bp);
266 if (i == nodehdr.count)
269 /* We can't point back to the root. */
270 if (XFS_IS_CORRUPT(mp, cursor->blkno == 0)) {
271 xfs_dirattr_mark_sick(dp, XFS_ATTR_FORK);
272 return -EFSCORRUPTED;
276 if (expected_level != 0)
283 xfs_buf_mark_corrupt(bp);
284 xfs_trans_brelse(tp, bp);
285 xfs_dirattr_mark_sick(dp, XFS_ATTR_FORK);
286 return -EFSCORRUPTED;
291 struct xfs_attr_list_context *context)
293 struct xfs_attrlist_cursor_kern *cursor = &context->cursor;
294 struct xfs_attr3_icleaf_hdr leafhdr;
295 struct xfs_attr_leafblock *leaf;
296 struct xfs_da_intnode *node;
298 struct xfs_inode *dp = context->dp;
299 struct xfs_mount *mp = dp->i_mount;
302 trace_xfs_attr_node_list(context);
307 * Do all sorts of validation on the passed-in cursor structure.
308 * If anything is amiss, ignore the cursor and look up the hashval
309 * starting from the btree root.
312 if (cursor->blkno > 0) {
313 error = xfs_da3_node_read(context->tp, dp, cursor->blkno, &bp,
315 if (xfs_metadata_is_sick(error))
316 xfs_dirattr_mark_sick(dp, XFS_ATTR_FORK);
317 if ((error != 0) && (error != -EFSCORRUPTED))
320 struct xfs_attr_leaf_entry *entries;
323 switch (be16_to_cpu(node->hdr.info.magic)) {
324 case XFS_DA_NODE_MAGIC:
325 case XFS_DA3_NODE_MAGIC:
326 trace_xfs_attr_list_wrong_blk(context);
327 xfs_trans_brelse(context->tp, bp);
330 case XFS_ATTR_LEAF_MAGIC:
331 case XFS_ATTR3_LEAF_MAGIC:
333 xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo,
335 entries = xfs_attr3_leaf_entryp(leaf);
336 if (cursor->hashval > be32_to_cpu(
337 entries[leafhdr.count - 1].hashval)) {
338 trace_xfs_attr_list_wrong_blk(context);
339 xfs_trans_brelse(context->tp, bp);
341 } else if (cursor->hashval <= be32_to_cpu(
342 entries[0].hashval)) {
343 trace_xfs_attr_list_wrong_blk(context);
344 xfs_trans_brelse(context->tp, bp);
349 trace_xfs_attr_list_wrong_blk(context);
350 xfs_trans_brelse(context->tp, bp);
357 * We did not find what we expected given the cursor's contents,
358 * so we start from the top and work down based on the hash value.
359 * Note that start of node block is same as start of leaf block.
362 error = xfs_attr_node_list_lookup(context, cursor, &bp);
369 * Roll upward through the blocks, processing each leaf block in
370 * order. As long as there is space in the result buffer, keep
371 * adding the information.
375 error = xfs_attr3_leaf_list_int(bp, context);
378 xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &leafhdr, leaf);
379 if (context->seen_enough || leafhdr.forw == 0)
381 cursor->blkno = leafhdr.forw;
382 xfs_trans_brelse(context->tp, bp);
383 error = xfs_attr3_leaf_read(context->tp, dp, cursor->blkno,
388 xfs_trans_brelse(context->tp, bp);
393 * Copy out attribute list entries for attr_list(), for leaf attribute lists.
396 xfs_attr3_leaf_list_int(
398 struct xfs_attr_list_context *context)
400 struct xfs_attrlist_cursor_kern *cursor = &context->cursor;
401 struct xfs_attr_leafblock *leaf;
402 struct xfs_attr3_icleaf_hdr ichdr;
403 struct xfs_attr_leaf_entry *entries;
404 struct xfs_attr_leaf_entry *entry;
406 struct xfs_mount *mp = context->dp->i_mount;
408 trace_xfs_attr_list_leaf(context);
411 xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr, leaf);
412 entries = xfs_attr3_leaf_entryp(leaf);
417 * Re-find our place in the leaf block if this is a new syscall.
419 if (context->resynch) {
421 for (i = 0; i < ichdr.count; entry++, i++) {
422 if (be32_to_cpu(entry->hashval) == cursor->hashval) {
423 if (cursor->offset == context->dupcnt) {
428 } else if (be32_to_cpu(entry->hashval) >
434 if (i == ichdr.count) {
435 trace_xfs_attr_list_notfound(context);
442 context->resynch = 0;
445 * We have found our place, start copying out the new attributes.
447 for (; i < ichdr.count; entry++, i++) {
449 int namelen, valuelen;
451 if (be32_to_cpu(entry->hashval) != cursor->hashval) {
452 cursor->hashval = be32_to_cpu(entry->hashval);
456 if ((entry->flags & XFS_ATTR_INCOMPLETE) &&
457 !context->allow_incomplete)
460 if (entry->flags & XFS_ATTR_LOCAL) {
461 xfs_attr_leaf_name_local_t *name_loc;
463 name_loc = xfs_attr3_leaf_name_local(leaf, i);
464 name = name_loc->nameval;
465 namelen = name_loc->namelen;
466 valuelen = be16_to_cpu(name_loc->valuelen);
468 xfs_attr_leaf_name_remote_t *name_rmt;
470 name_rmt = xfs_attr3_leaf_name_remote(leaf, i);
471 name = name_rmt->name;
472 namelen = name_rmt->namelen;
473 valuelen = be32_to_cpu(name_rmt->valuelen);
476 if (XFS_IS_CORRUPT(context->dp->i_mount,
477 !xfs_attr_namecheck(name, namelen))) {
478 xfs_dirattr_mark_sick(context->dp, XFS_ATTR_FORK);
479 return -EFSCORRUPTED;
481 context->put_listent(context, entry->flags,
482 name, namelen, valuelen);
483 if (context->seen_enough)
487 trace_xfs_attr_list_leaf_end(context);
492 * Copy out attribute entries for attr_list(), for leaf attribute lists.
496 struct xfs_attr_list_context *context)
501 trace_xfs_attr_leaf_list(context);
503 context->cursor.blkno = 0;
504 error = xfs_attr3_leaf_read(context->tp, context->dp, 0, &bp);
508 error = xfs_attr3_leaf_list_int(bp, context);
509 xfs_trans_brelse(context->tp, bp);
514 xfs_attr_list_ilocked(
515 struct xfs_attr_list_context *context)
517 struct xfs_inode *dp = context->dp;
519 xfs_assert_ilocked(dp, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL);
522 * Decide on what work routines to call based on the inode size.
524 if (!xfs_inode_hasattr(dp))
526 if (dp->i_af.if_format == XFS_DINODE_FMT_LOCAL)
527 return xfs_attr_shortform_list(context);
528 if (xfs_attr_is_leaf(dp))
529 return xfs_attr_leaf_list(context);
530 return xfs_attr_node_list(context);
535 struct xfs_attr_list_context *context)
537 struct xfs_inode *dp = context->dp;
541 XFS_STATS_INC(dp->i_mount, xs_attr_list);
543 if (xfs_is_shutdown(dp->i_mount))
546 lock_mode = xfs_ilock_attr_map_shared(dp);
547 error = xfs_attr_list_ilocked(context);
548 xfs_iunlock(dp, lock_mode);