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"
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_trans.h"
19 #include "xfs_inode_item.h"
22 #include "xfs_attr_sf.h"
23 #include "xfs_attr_remote.h"
24 #include "xfs_attr_leaf.h"
25 #include "xfs_error.h"
26 #include "xfs_trace.h"
27 #include "xfs_buf_item.h"
28 #include "xfs_cksum.h"
32 xfs_attr_shortform_compare(const void *a, const void *b)
34 xfs_attr_sf_sort_t *sa, *sb;
36 sa = (xfs_attr_sf_sort_t *)a;
37 sb = (xfs_attr_sf_sort_t *)b;
38 if (sa->hash < sb->hash) {
40 } else if (sa->hash > sb->hash) {
43 return sa->entno - sb->entno;
47 #define XFS_ISRESET_CURSOR(cursor) \
48 (!((cursor)->initted) && !((cursor)->hashval) && \
49 !((cursor)->blkno) && !((cursor)->offset))
51 * Copy out entries of shortform attribute lists for attr_list().
52 * Shortform attribute lists are not stored in hashval sorted order.
53 * If the output buffer is not large enough to hold them all, then we
54 * we have to calculate each entries' hashvalue and sort them before
55 * we can begin returning them to the user.
58 xfs_attr_shortform_list(xfs_attr_list_context_t *context)
60 attrlist_cursor_kern_t *cursor;
61 xfs_attr_sf_sort_t *sbuf, *sbp;
62 xfs_attr_shortform_t *sf;
63 xfs_attr_sf_entry_t *sfe;
65 int sbsize, nsbuf, count, i;
67 ASSERT(context != NULL);
70 ASSERT(dp->i_afp != NULL);
71 sf = (xfs_attr_shortform_t *)dp->i_afp->if_u1.if_data;
75 cursor = context->cursor;
76 ASSERT(cursor != NULL);
78 trace_xfs_attr_list_sf(context);
81 * If the buffer is large enough and the cursor is at the start,
82 * do not bother with sorting since we will return everything in
83 * one buffer and another call using the cursor won't need to be
85 * Note the generous fudge factor of 16 overhead bytes per entry.
86 * If bufsize is zero then put_listent must be a search function
87 * and can just scan through what we have.
89 if (context->bufsize == 0 ||
90 (XFS_ISRESET_CURSOR(cursor) &&
91 (dp->i_afp->if_bytes + sf->hdr.count * 16) < context->bufsize)) {
92 for (i = 0, sfe = &sf->list[0]; i < sf->hdr.count; i++) {
93 context->put_listent(context,
99 * Either search callback finished early or
100 * didn't fit it all in the buffer after all.
102 if (context->seen_enough)
104 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
106 trace_xfs_attr_list_sf_all(context);
110 /* do no more for a search callback */
111 if (context->bufsize == 0)
115 * It didn't all fit, so we have to sort everything on hashval.
117 sbsize = sf->hdr.count * sizeof(*sbuf);
118 sbp = sbuf = kmem_alloc(sbsize, KM_SLEEP | KM_NOFS);
121 * Scan the attribute list for the rest of the entries, storing
122 * the relevant info from only those that match into a buffer.
125 for (i = 0, sfe = &sf->list[0]; i < sf->hdr.count; i++) {
127 ((char *)sfe < (char *)sf) ||
128 ((char *)sfe >= ((char *)sf + dp->i_afp->if_bytes)))) {
129 XFS_CORRUPTION_ERROR("xfs_attr_shortform_list",
131 context->dp->i_mount, sfe,
134 return -EFSCORRUPTED;
138 sbp->hash = xfs_da_hashname(sfe->nameval, sfe->namelen);
139 sbp->name = sfe->nameval;
140 sbp->namelen = sfe->namelen;
141 /* These are bytes, and both on-disk, don't endian-flip */
142 sbp->valuelen = sfe->valuelen;
143 sbp->flags = sfe->flags;
144 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
150 * Sort the entries on hash then entno.
152 xfs_sort(sbuf, nsbuf, sizeof(*sbuf), xfs_attr_shortform_compare);
155 * Re-find our place IN THE SORTED LIST.
160 for (sbp = sbuf, i = 0; i < nsbuf; i++, sbp++) {
161 if (sbp->hash == cursor->hashval) {
162 if (cursor->offset == count) {
166 } else if (sbp->hash > cursor->hashval) {
176 * Loop putting entries into the user buffer.
178 for ( ; i < nsbuf; i++, sbp++) {
179 if (cursor->hashval != sbp->hash) {
180 cursor->hashval = sbp->hash;
183 context->put_listent(context,
188 if (context->seen_enough)
198 * We didn't find the block & hash mentioned in the cursor state, so
199 * walk down the attr btree looking for the hash.
202 xfs_attr_node_list_lookup(
203 struct xfs_attr_list_context *context,
204 struct attrlist_cursor_kern *cursor,
205 struct xfs_buf **pbp)
207 struct xfs_da3_icnode_hdr nodehdr;
208 struct xfs_da_intnode *node;
209 struct xfs_da_node_entry *btree;
210 struct xfs_inode *dp = context->dp;
211 struct xfs_mount *mp = dp->i_mount;
212 struct xfs_trans *tp = context->tp;
216 unsigned int expected_level = 0;
219 ASSERT(*pbp == NULL);
222 error = xfs_da3_node_read(tp, dp, cursor->blkno, -1, &bp,
227 magic = be16_to_cpu(node->hdr.info.magic);
228 if (magic == XFS_ATTR_LEAF_MAGIC ||
229 magic == XFS_ATTR3_LEAF_MAGIC)
231 if (magic != XFS_DA_NODE_MAGIC &&
232 magic != XFS_DA3_NODE_MAGIC) {
233 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
234 node, sizeof(*node));
238 dp->d_ops->node_hdr_from_disk(&nodehdr, node);
240 /* Tree taller than we can handle; bail out! */
241 if (nodehdr.level >= XFS_DA_NODE_MAXDEPTH)
244 /* Check the level from the root node. */
245 if (cursor->blkno == 0)
246 expected_level = nodehdr.level - 1;
247 else if (expected_level != nodehdr.level)
252 btree = dp->d_ops->node_tree_p(node);
253 for (i = 0; i < nodehdr.count; btree++, i++) {
254 if (cursor->hashval <= be32_to_cpu(btree->hashval)) {
255 cursor->blkno = be32_to_cpu(btree->before);
256 trace_xfs_attr_list_node_descend(context,
261 xfs_trans_brelse(tp, bp);
263 if (i == nodehdr.count)
266 /* We can't point back to the root. */
267 if (cursor->blkno == 0)
268 return -EFSCORRUPTED;
271 if (expected_level != 0)
278 xfs_trans_brelse(tp, bp);
279 return -EFSCORRUPTED;
284 struct xfs_attr_list_context *context)
286 struct xfs_attr3_icleaf_hdr leafhdr;
287 struct attrlist_cursor_kern *cursor;
288 struct xfs_attr_leafblock *leaf;
289 struct xfs_da_intnode *node;
291 struct xfs_inode *dp = context->dp;
292 struct xfs_mount *mp = dp->i_mount;
295 trace_xfs_attr_node_list(context);
297 cursor = context->cursor;
301 * Do all sorts of validation on the passed-in cursor structure.
302 * If anything is amiss, ignore the cursor and look up the hashval
303 * starting from the btree root.
306 if (cursor->blkno > 0) {
307 error = xfs_da3_node_read(context->tp, dp, cursor->blkno, -1,
309 if ((error != 0) && (error != -EFSCORRUPTED))
312 struct xfs_attr_leaf_entry *entries;
315 switch (be16_to_cpu(node->hdr.info.magic)) {
316 case XFS_DA_NODE_MAGIC:
317 case XFS_DA3_NODE_MAGIC:
318 trace_xfs_attr_list_wrong_blk(context);
319 xfs_trans_brelse(context->tp, bp);
322 case XFS_ATTR_LEAF_MAGIC:
323 case XFS_ATTR3_LEAF_MAGIC:
325 xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo,
327 entries = xfs_attr3_leaf_entryp(leaf);
328 if (cursor->hashval > be32_to_cpu(
329 entries[leafhdr.count - 1].hashval)) {
330 trace_xfs_attr_list_wrong_blk(context);
331 xfs_trans_brelse(context->tp, bp);
333 } else if (cursor->hashval <= be32_to_cpu(
334 entries[0].hashval)) {
335 trace_xfs_attr_list_wrong_blk(context);
336 xfs_trans_brelse(context->tp, bp);
341 trace_xfs_attr_list_wrong_blk(context);
342 xfs_trans_brelse(context->tp, bp);
349 * We did not find what we expected given the cursor's contents,
350 * so we start from the top and work down based on the hash value.
351 * Note that start of node block is same as start of leaf block.
354 error = xfs_attr_node_list_lookup(context, cursor, &bp);
361 * Roll upward through the blocks, processing each leaf block in
362 * order. As long as there is space in the result buffer, keep
363 * adding the information.
367 xfs_attr3_leaf_list_int(bp, context);
368 xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &leafhdr, leaf);
369 if (context->seen_enough || leafhdr.forw == 0)
371 cursor->blkno = leafhdr.forw;
372 xfs_trans_brelse(context->tp, bp);
373 error = xfs_attr3_leaf_read(context->tp, dp, cursor->blkno, -1, &bp);
377 xfs_trans_brelse(context->tp, bp);
382 * Copy out attribute list entries for attr_list(), for leaf attribute lists.
385 xfs_attr3_leaf_list_int(
387 struct xfs_attr_list_context *context)
389 struct attrlist_cursor_kern *cursor;
390 struct xfs_attr_leafblock *leaf;
391 struct xfs_attr3_icleaf_hdr ichdr;
392 struct xfs_attr_leaf_entry *entries;
393 struct xfs_attr_leaf_entry *entry;
395 struct xfs_mount *mp = context->dp->i_mount;
397 trace_xfs_attr_list_leaf(context);
400 xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr, leaf);
401 entries = xfs_attr3_leaf_entryp(leaf);
403 cursor = context->cursor;
407 * Re-find our place in the leaf block if this is a new syscall.
409 if (context->resynch) {
411 for (i = 0; i < ichdr.count; entry++, i++) {
412 if (be32_to_cpu(entry->hashval) == cursor->hashval) {
413 if (cursor->offset == context->dupcnt) {
418 } else if (be32_to_cpu(entry->hashval) >
424 if (i == ichdr.count) {
425 trace_xfs_attr_list_notfound(context);
432 context->resynch = 0;
435 * We have found our place, start copying out the new attributes.
437 for (; i < ichdr.count; entry++, i++) {
439 int namelen, valuelen;
441 if (be32_to_cpu(entry->hashval) != cursor->hashval) {
442 cursor->hashval = be32_to_cpu(entry->hashval);
446 if ((entry->flags & XFS_ATTR_INCOMPLETE) &&
447 !(context->flags & ATTR_INCOMPLETE))
448 continue; /* skip incomplete entries */
450 if (entry->flags & XFS_ATTR_LOCAL) {
451 xfs_attr_leaf_name_local_t *name_loc;
453 name_loc = xfs_attr3_leaf_name_local(leaf, i);
454 name = name_loc->nameval;
455 namelen = name_loc->namelen;
456 valuelen = be16_to_cpu(name_loc->valuelen);
458 xfs_attr_leaf_name_remote_t *name_rmt;
460 name_rmt = xfs_attr3_leaf_name_remote(leaf, i);
461 name = name_rmt->name;
462 namelen = name_rmt->namelen;
463 valuelen = be32_to_cpu(name_rmt->valuelen);
466 context->put_listent(context, entry->flags,
467 name, namelen, valuelen);
468 if (context->seen_enough)
472 trace_xfs_attr_list_leaf_end(context);
477 * Copy out attribute entries for attr_list(), for leaf attribute lists.
480 xfs_attr_leaf_list(xfs_attr_list_context_t *context)
485 trace_xfs_attr_leaf_list(context);
487 context->cursor->blkno = 0;
488 error = xfs_attr3_leaf_read(context->tp, context->dp, 0, -1, &bp);
492 xfs_attr3_leaf_list_int(bp, context);
493 xfs_trans_brelse(context->tp, bp);
498 xfs_attr_list_int_ilocked(
499 struct xfs_attr_list_context *context)
501 struct xfs_inode *dp = context->dp;
503 ASSERT(xfs_isilocked(dp, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
506 * Decide on what work routines to call based on the inode size.
508 if (!xfs_inode_hasattr(dp))
510 else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL)
511 return xfs_attr_shortform_list(context);
512 else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK))
513 return xfs_attr_leaf_list(context);
514 return xfs_attr_node_list(context);
519 xfs_attr_list_context_t *context)
522 xfs_inode_t *dp = context->dp;
525 XFS_STATS_INC(dp->i_mount, xs_attr_list);
527 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
530 lock_mode = xfs_ilock_attr_map_shared(dp);
531 error = xfs_attr_list_int_ilocked(context);
532 xfs_iunlock(dp, lock_mode);
536 #define ATTR_ENTBASESIZE /* minimum bytes used by an attr */ \
537 (((struct attrlist_ent *) 0)->a_name - (char *) 0)
538 #define ATTR_ENTSIZE(namelen) /* actual bytes used by an attr */ \
539 ((ATTR_ENTBASESIZE + (namelen) + 1 + sizeof(uint32_t)-1) \
540 & ~(sizeof(uint32_t)-1))
543 * Format an attribute and copy it out to the user's buffer.
544 * Take care to check values and protect against them changing later,
545 * we may be reading them directly out of a user buffer.
548 xfs_attr_put_listent(
549 xfs_attr_list_context_t *context,
555 struct attrlist *alist = (struct attrlist *)context->alist;
559 ASSERT(!context->seen_enough);
560 ASSERT(!(context->flags & ATTR_KERNOVAL));
561 ASSERT(context->count >= 0);
562 ASSERT(context->count < (ATTR_MAX_VALUELEN/8));
563 ASSERT(context->firstu >= sizeof(*alist));
564 ASSERT(context->firstu <= context->bufsize);
567 * Only list entries in the right namespace.
569 if (((context->flags & ATTR_SECURE) == 0) !=
570 ((flags & XFS_ATTR_SECURE) == 0))
572 if (((context->flags & ATTR_ROOT) == 0) !=
573 ((flags & XFS_ATTR_ROOT) == 0))
576 arraytop = sizeof(*alist) +
577 context->count * sizeof(alist->al_offset[0]);
578 context->firstu -= ATTR_ENTSIZE(namelen);
579 if (context->firstu < arraytop) {
580 trace_xfs_attr_list_full(context);
582 context->seen_enough = 1;
586 aep = (attrlist_ent_t *)&context->alist[context->firstu];
587 aep->a_valuelen = valuelen;
588 memcpy(aep->a_name, name, namelen);
589 aep->a_name[namelen] = 0;
590 alist->al_offset[context->count++] = context->firstu;
591 alist->al_count = context->count;
592 trace_xfs_attr_list_add(context);
597 * Generate a list of extended attribute names and optionally
598 * also value lengths. Positive return value follows the XFS
599 * convention of being an error, zero or negative return code
600 * is the length of the buffer returned (negated), indicating
609 attrlist_cursor_kern_t *cursor)
611 xfs_attr_list_context_t context;
612 struct attrlist *alist;
616 * Validate the cursor.
618 if (cursor->pad1 || cursor->pad2)
620 if ((cursor->initted == 0) &&
621 (cursor->hashval || cursor->blkno || cursor->offset))
624 /* Only internal consumers can retrieve incomplete attrs. */
625 if (flags & ATTR_INCOMPLETE)
629 * Check for a properly aligned buffer.
631 if (((long)buffer) & (sizeof(int)-1))
633 if (flags & ATTR_KERNOVAL)
637 * Initialize the output buffer.
639 memset(&context, 0, sizeof(context));
641 context.cursor = cursor;
643 context.flags = flags;
644 context.alist = buffer;
645 context.bufsize = (bufsize & ~(sizeof(int)-1)); /* align */
646 context.firstu = context.bufsize;
647 context.put_listent = xfs_attr_put_listent;
649 alist = (struct attrlist *)context.alist;
652 alist->al_offset[0] = context.bufsize;
654 error = xfs_attr_list_int(&context);