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"
27 xfs_attr_shortform_compare(const void *a, const void *b)
29 xfs_attr_sf_sort_t *sa, *sb;
31 sa = (xfs_attr_sf_sort_t *)a;
32 sb = (xfs_attr_sf_sort_t *)b;
33 if (sa->hash < sb->hash) {
35 } else if (sa->hash > sb->hash) {
38 return sa->entno - sb->entno;
42 #define XFS_ISRESET_CURSOR(cursor) \
43 (!((cursor)->initted) && !((cursor)->hashval) && \
44 !((cursor)->blkno) && !((cursor)->offset))
46 * Copy out entries of shortform attribute lists for attr_list().
47 * Shortform attribute lists are not stored in hashval sorted order.
48 * If the output buffer is not large enough to hold them all, then
49 * we have to calculate each entries' hashvalue and sort them before
50 * we can begin returning them to the user.
53 xfs_attr_shortform_list(
54 struct xfs_attr_list_context *context)
56 struct xfs_attrlist_cursor_kern *cursor = &context->cursor;
57 struct xfs_inode *dp = context->dp;
58 struct xfs_attr_sf_sort *sbuf, *sbp;
59 struct xfs_attr_shortform *sf;
60 struct xfs_attr_sf_entry *sfe;
61 int sbsize, nsbuf, count, i;
64 ASSERT(dp->i_afp != NULL);
65 sf = (struct xfs_attr_shortform *)dp->i_afp->if_u1.if_data;
70 trace_xfs_attr_list_sf(context);
73 * If the buffer is large enough and the cursor is at the start,
74 * do not bother with sorting since we will return everything in
75 * one buffer and another call using the cursor won't need to be
77 * Note the generous fudge factor of 16 overhead bytes per entry.
78 * If bufsize is zero then put_listent must be a search function
79 * and can just scan through what we have.
81 if (context->bufsize == 0 ||
82 (XFS_ISRESET_CURSOR(cursor) &&
83 (dp->i_afp->if_bytes + sf->hdr.count * 16) < context->bufsize)) {
84 for (i = 0, sfe = &sf->list[0]; i < sf->hdr.count; i++) {
85 if (XFS_IS_CORRUPT(context->dp->i_mount,
86 !xfs_attr_namecheck(sfe->nameval,
89 context->put_listent(context,
95 * Either search callback finished early or
96 * didn't fit it all in the buffer after all.
98 if (context->seen_enough)
100 sfe = xfs_attr_sf_nextentry(sfe);
102 trace_xfs_attr_list_sf_all(context);
106 /* do no more for a search callback */
107 if (context->bufsize == 0)
111 * It didn't all fit, so we have to sort everything on hashval.
113 sbsize = sf->hdr.count * sizeof(*sbuf);
114 sbp = sbuf = kmem_alloc(sbsize, KM_NOFS);
117 * Scan the attribute list for the rest of the entries, storing
118 * the relevant info from only those that match into a buffer.
121 for (i = 0, sfe = &sf->list[0]; i < sf->hdr.count; i++) {
123 ((char *)sfe < (char *)sf) ||
124 ((char *)sfe >= ((char *)sf + dp->i_afp->if_bytes)))) {
125 XFS_CORRUPTION_ERROR("xfs_attr_shortform_list",
127 context->dp->i_mount, sfe,
130 return -EFSCORRUPTED;
134 sbp->hash = xfs_da_hashname(sfe->nameval, sfe->namelen);
135 sbp->name = sfe->nameval;
136 sbp->namelen = sfe->namelen;
137 /* These are bytes, and both on-disk, don't endian-flip */
138 sbp->valuelen = sfe->valuelen;
139 sbp->flags = sfe->flags;
140 sfe = xfs_attr_sf_nextentry(sfe);
146 * Sort the entries on hash then entno.
148 xfs_sort(sbuf, nsbuf, sizeof(*sbuf), xfs_attr_shortform_compare);
151 * Re-find our place IN THE SORTED LIST.
156 for (sbp = sbuf, i = 0; i < nsbuf; i++, sbp++) {
157 if (sbp->hash == cursor->hashval) {
158 if (cursor->offset == count) {
162 } else if (sbp->hash > cursor->hashval) {
170 * Loop putting entries into the user buffer.
172 for ( ; i < nsbuf; i++, sbp++) {
173 if (cursor->hashval != sbp->hash) {
174 cursor->hashval = sbp->hash;
177 if (XFS_IS_CORRUPT(context->dp->i_mount,
178 !xfs_attr_namecheck(sbp->name,
180 error = -EFSCORRUPTED;
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 xfs_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, &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 xfs_da3_node_hdr_from_disk(mp, &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 = nodehdr.btree;
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 (XFS_IS_CORRUPT(mp, cursor->blkno == 0))
268 return -EFSCORRUPTED;
271 if (expected_level != 0)
278 xfs_buf_mark_corrupt(bp);
279 xfs_trans_brelse(tp, bp);
280 return -EFSCORRUPTED;
285 struct xfs_attr_list_context *context)
287 struct xfs_attrlist_cursor_kern *cursor = &context->cursor;
288 struct xfs_attr3_icleaf_hdr leafhdr;
289 struct xfs_attr_leafblock *leaf;
290 struct xfs_da_intnode *node;
292 struct xfs_inode *dp = context->dp;
293 struct xfs_mount *mp = dp->i_mount;
296 trace_xfs_attr_node_list(context);
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, &bp,
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 error = xfs_attr3_leaf_list_int(bp, context);
370 xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &leafhdr, leaf);
371 if (context->seen_enough || leafhdr.forw == 0)
373 cursor->blkno = leafhdr.forw;
374 xfs_trans_brelse(context->tp, bp);
375 error = xfs_attr3_leaf_read(context->tp, dp, cursor->blkno,
380 xfs_trans_brelse(context->tp, bp);
385 * Copy out attribute list entries for attr_list(), for leaf attribute lists.
388 xfs_attr3_leaf_list_int(
390 struct xfs_attr_list_context *context)
392 struct xfs_attrlist_cursor_kern *cursor = &context->cursor;
393 struct xfs_attr_leafblock *leaf;
394 struct xfs_attr3_icleaf_hdr ichdr;
395 struct xfs_attr_leaf_entry *entries;
396 struct xfs_attr_leaf_entry *entry;
398 struct xfs_mount *mp = context->dp->i_mount;
400 trace_xfs_attr_list_leaf(context);
403 xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr, leaf);
404 entries = xfs_attr3_leaf_entryp(leaf);
409 * Re-find our place in the leaf block if this is a new syscall.
411 if (context->resynch) {
413 for (i = 0; i < ichdr.count; entry++, i++) {
414 if (be32_to_cpu(entry->hashval) == cursor->hashval) {
415 if (cursor->offset == context->dupcnt) {
420 } else if (be32_to_cpu(entry->hashval) >
426 if (i == ichdr.count) {
427 trace_xfs_attr_list_notfound(context);
434 context->resynch = 0;
437 * We have found our place, start copying out the new attributes.
439 for (; i < ichdr.count; entry++, i++) {
441 int namelen, valuelen;
443 if (be32_to_cpu(entry->hashval) != cursor->hashval) {
444 cursor->hashval = be32_to_cpu(entry->hashval);
448 if ((entry->flags & XFS_ATTR_INCOMPLETE) &&
449 !context->allow_incomplete)
452 if (entry->flags & XFS_ATTR_LOCAL) {
453 xfs_attr_leaf_name_local_t *name_loc;
455 name_loc = xfs_attr3_leaf_name_local(leaf, i);
456 name = name_loc->nameval;
457 namelen = name_loc->namelen;
458 valuelen = be16_to_cpu(name_loc->valuelen);
460 xfs_attr_leaf_name_remote_t *name_rmt;
462 name_rmt = xfs_attr3_leaf_name_remote(leaf, i);
463 name = name_rmt->name;
464 namelen = name_rmt->namelen;
465 valuelen = be32_to_cpu(name_rmt->valuelen);
468 if (XFS_IS_CORRUPT(context->dp->i_mount,
469 !xfs_attr_namecheck(name, namelen)))
470 return -EFSCORRUPTED;
471 context->put_listent(context, entry->flags,
472 name, namelen, valuelen);
473 if (context->seen_enough)
477 trace_xfs_attr_list_leaf_end(context);
482 * Copy out attribute entries for attr_list(), for leaf attribute lists.
486 struct xfs_attr_list_context *context)
491 trace_xfs_attr_leaf_list(context);
493 context->cursor.blkno = 0;
494 error = xfs_attr3_leaf_read(context->tp, context->dp, 0, &bp);
498 error = xfs_attr3_leaf_list_int(bp, context);
499 xfs_trans_brelse(context->tp, bp);
504 xfs_attr_list_ilocked(
505 struct xfs_attr_list_context *context)
507 struct xfs_inode *dp = context->dp;
509 ASSERT(xfs_isilocked(dp, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
512 * Decide on what work routines to call based on the inode size.
514 if (!xfs_inode_hasattr(dp))
516 if (dp->i_afp->if_format == XFS_DINODE_FMT_LOCAL)
517 return xfs_attr_shortform_list(context);
518 if (xfs_attr_is_leaf(dp))
519 return xfs_attr_leaf_list(context);
520 return xfs_attr_node_list(context);
525 struct xfs_attr_list_context *context)
527 struct xfs_inode *dp = context->dp;
531 XFS_STATS_INC(dp->i_mount, xs_attr_list);
533 if (xfs_is_shutdown(dp->i_mount))
536 lock_mode = xfs_ilock_attr_map_shared(dp);
537 error = xfs_attr_list_ilocked(context);
538 xfs_iunlock(dp, lock_mode);