Merge tag 'xfs-6.9-merge-8' of git://git.kernel.org/pub/scm/fs/xfs/xfs-linux
[linux-block.git] / fs / xfs / xfs_attr_list.c
CommitLineData
0b61f8a4 1// SPDX-License-Identifier: GPL-2.0
abec5f2b
DC
2/*
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4 * Copyright (c) 2013 Red Hat, Inc.
5 * All Rights Reserved.
abec5f2b
DC
6 */
7#include "xfs.h"
8#include "xfs_fs.h"
5467b34b 9#include "xfs_shared.h"
a4fbe6ab 10#include "xfs_format.h"
239880ef
DC
11#include "xfs_log_format.h"
12#include "xfs_trans_resv.h"
abec5f2b 13#include "xfs_mount.h"
57062787 14#include "xfs_da_format.h"
abec5f2b 15#include "xfs_inode.h"
239880ef 16#include "xfs_trans.h"
abec5f2b 17#include "xfs_bmap.h"
fd920008 18#include "xfs_da_btree.h"
abec5f2b 19#include "xfs_attr.h"
a4fbe6ab 20#include "xfs_attr_sf.h"
abec5f2b
DC
21#include "xfs_attr_leaf.h"
22#include "xfs_error.h"
23#include "xfs_trace.h"
4bceb18f 24#include "xfs_dir2.h"
ca14c096 25#include "xfs_health.h"
abec5f2b
DC
26
27STATIC int
28xfs_attr_shortform_compare(const void *a, const void *b)
29{
30 xfs_attr_sf_sort_t *sa, *sb;
31
32 sa = (xfs_attr_sf_sort_t *)a;
33 sb = (xfs_attr_sf_sort_t *)b;
34 if (sa->hash < sb->hash) {
d99831ff 35 return -1;
abec5f2b 36 } else if (sa->hash > sb->hash) {
d99831ff 37 return 1;
abec5f2b 38 } else {
d99831ff 39 return sa->entno - sb->entno;
abec5f2b
DC
40 }
41}
42
43#define XFS_ISRESET_CURSOR(cursor) \
44 (!((cursor)->initted) && !((cursor)->hashval) && \
45 !((cursor)->blkno) && !((cursor)->offset))
46/*
47 * Copy out entries of shortform attribute lists for attr_list().
48 * Shortform attribute lists are not stored in hashval sorted order.
b63da6c8 49 * If the output buffer is not large enough to hold them all, then
abec5f2b
DC
50 * we have to calculate each entries' hashvalue and sort them before
51 * we can begin returning them to the user.
52 */
0d5a75e9 53static int
16c6e92c
DW
54xfs_attr_shortform_list(
55 struct xfs_attr_list_context *context)
abec5f2b 56{
e3a19cde
CH
57 struct xfs_attrlist_cursor_kern *cursor = &context->cursor;
58 struct xfs_inode *dp = context->dp;
16c6e92c 59 struct xfs_attr_sf_sort *sbuf, *sbp;
41414722 60 struct xfs_attr_sf_hdr *sf = dp->i_af.if_data;
16c6e92c 61 struct xfs_attr_sf_entry *sfe;
16c6e92c
DW
62 int sbsize, nsbuf, count, i;
63 int error = 0;
abec5f2b 64
abec5f2b 65 ASSERT(sf != NULL);
41414722 66 if (!sf->count)
d99831ff 67 return 0;
abec5f2b
DC
68
69 trace_xfs_attr_list_sf(context);
70
71 /*
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
75 * made.
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.
79 */
80 if (context->bufsize == 0 ||
81 (XFS_ISRESET_CURSOR(cursor) &&
41414722
CH
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++) {
a71895c5
DW
84 if (XFS_IS_CORRUPT(context->dp->i_mount,
85 !xfs_attr_namecheck(sfe->nameval,
989d5ec3
DW
86 sfe->namelen))) {
87 xfs_dirattr_mark_sick(context->dp, XFS_ATTR_FORK);
16c6e92c 88 return -EFSCORRUPTED;
989d5ec3 89 }
f7a136ae
ES
90 context->put_listent(context,
91 sfe->flags,
92 sfe->nameval,
93 (int)sfe->namelen,
94 (int)sfe->valuelen);
abec5f2b
DC
95 /*
96 * Either search callback finished early or
97 * didn't fit it all in the buffer after all.
98 */
99 if (context->seen_enough)
100 break;
e01b7eed 101 sfe = xfs_attr_sf_nextentry(sfe);
abec5f2b
DC
102 }
103 trace_xfs_attr_list_sf_all(context);
d99831ff 104 return 0;
abec5f2b
DC
105 }
106
107 /* do no more for a search callback */
108 if (context->bufsize == 0)
109 return 0;
110
111 /*
112 * It didn't all fit, so we have to sort everything on hashval.
113 */
41414722 114 sbsize = sf->count * sizeof(*sbuf);
204fae32 115 sbp = sbuf = kmalloc(sbsize, GFP_KERNEL | __GFP_NOFAIL);
abec5f2b
DC
116
117 /*
118 * Scan the attribute list for the rest of the entries, storing
119 * the relevant info from only those that match into a buffer.
120 */
121 nsbuf = 0;
41414722 122 for (i = 0, sfe = xfs_attr_sf_firstentry(sf); i < sf->count; i++) {
abec5f2b
DC
123 if (unlikely(
124 ((char *)sfe < (char *)sf) ||
2ed5b09b 125 ((char *)sfe >= ((char *)sf + dp->i_af.if_bytes)))) {
abec5f2b
DC
126 XFS_CORRUPTION_ERROR("xfs_attr_shortform_list",
127 XFS_ERRLEVEL_LOW,
2551a530
DW
128 context->dp->i_mount, sfe,
129 sizeof(*sfe));
d4c75a1b 130 kfree(sbuf);
ca14c096 131 xfs_dirattr_mark_sick(dp, XFS_ATTR_FORK);
2451337d 132 return -EFSCORRUPTED;
abec5f2b
DC
133 }
134
135 sbp->entno = i;
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;
e01b7eed 142 sfe = xfs_attr_sf_nextentry(sfe);
abec5f2b
DC
143 sbp++;
144 nsbuf++;
145 }
146
147 /*
148 * Sort the entries on hash then entno.
149 */
150 xfs_sort(sbuf, nsbuf, sizeof(*sbuf), xfs_attr_shortform_compare);
151
152 /*
153 * Re-find our place IN THE SORTED LIST.
154 */
155 count = 0;
156 cursor->initted = 1;
157 cursor->blkno = 0;
158 for (sbp = sbuf, i = 0; i < nsbuf; i++, sbp++) {
159 if (sbp->hash == cursor->hashval) {
160 if (cursor->offset == count) {
161 break;
162 }
163 count++;
164 } else if (sbp->hash > cursor->hashval) {
165 break;
166 }
167 }
16c6e92c
DW
168 if (i == nsbuf)
169 goto out;
abec5f2b
DC
170
171 /*
172 * Loop putting entries into the user buffer.
173 */
174 for ( ; i < nsbuf; i++, sbp++) {
175 if (cursor->hashval != sbp->hash) {
176 cursor->hashval = sbp->hash;
177 cursor->offset = 0;
178 }
a71895c5
DW
179 if (XFS_IS_CORRUPT(context->dp->i_mount,
180 !xfs_attr_namecheck(sbp->name,
181 sbp->namelen))) {
989d5ec3 182 xfs_dirattr_mark_sick(context->dp, XFS_ATTR_FORK);
16c6e92c
DW
183 error = -EFSCORRUPTED;
184 goto out;
185 }
f7a136ae
ES
186 context->put_listent(context,
187 sbp->flags,
188 sbp->name,
189 sbp->namelen,
190 sbp->valuelen);
abec5f2b
DC
191 if (context->seen_enough)
192 break;
193 cursor->offset++;
194 }
16c6e92c 195out:
d4c75a1b 196 kfree(sbuf);
16c6e92c 197 return error;
abec5f2b
DC
198}
199
bdaac93f
DW
200/*
201 * We didn't find the block & hash mentioned in the cursor state, so
202 * walk down the attr btree looking for the hash.
203 */
abec5f2b 204STATIC int
bdaac93f
DW
205xfs_attr_node_list_lookup(
206 struct xfs_attr_list_context *context,
e3a19cde 207 struct xfs_attrlist_cursor_kern *cursor,
bdaac93f 208 struct xfs_buf **pbp)
abec5f2b 209{
bdaac93f
DW
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;
216 struct xfs_buf *bp;
217 int i;
218 int error = 0;
8210f4dd 219 unsigned int expected_level = 0;
bdaac93f
DW
220 uint16_t magic;
221
222 ASSERT(*pbp == NULL);
223 cursor->blkno = 0;
224 for (;;) {
02c57f0a 225 error = xfs_da3_node_read(tp, dp, cursor->blkno, &bp,
bdaac93f
DW
226 XFS_ATTR_FORK);
227 if (error)
228 return error;
229 node = bp->b_addr;
230 magic = be16_to_cpu(node->hdr.info.magic);
231 if (magic == XFS_ATTR_LEAF_MAGIC ||
232 magic == XFS_ATTR3_LEAF_MAGIC)
233 break;
234 if (magic != XFS_DA_NODE_MAGIC &&
235 magic != XFS_DA3_NODE_MAGIC) {
236 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
2551a530 237 node, sizeof(*node));
bdaac93f
DW
238 goto out_corruptbuf;
239 }
240
f475dc4d 241 xfs_da3_node_hdr_from_disk(mp, &nodehdr, node);
bdaac93f 242
8210f4dd
DW
243 /* Tree taller than we can handle; bail out! */
244 if (nodehdr.level >= XFS_DA_NODE_MAXDEPTH)
245 goto out_corruptbuf;
246
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)
251 goto out_corruptbuf;
252 else
253 expected_level--;
254
51908ca7 255 btree = nodehdr.btree;
bdaac93f
DW
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,
260 btree);
261 break;
262 }
263 }
264 xfs_trans_brelse(tp, bp);
265
266 if (i == nodehdr.count)
267 return 0;
8210f4dd
DW
268
269 /* We can't point back to the root. */
ca14c096
DW
270 if (XFS_IS_CORRUPT(mp, cursor->blkno == 0)) {
271 xfs_dirattr_mark_sick(dp, XFS_ATTR_FORK);
8210f4dd 272 return -EFSCORRUPTED;
ca14c096 273 }
bdaac93f
DW
274 }
275
8210f4dd
DW
276 if (expected_level != 0)
277 goto out_corruptbuf;
278
bdaac93f
DW
279 *pbp = bp;
280 return 0;
281
282out_corruptbuf:
8d57c216 283 xfs_buf_mark_corrupt(bp);
bdaac93f 284 xfs_trans_brelse(tp, bp);
ca14c096 285 xfs_dirattr_mark_sick(dp, XFS_ATTR_FORK);
bdaac93f
DW
286 return -EFSCORRUPTED;
287}
288
289STATIC int
290xfs_attr_node_list(
291 struct xfs_attr_list_context *context)
292{
e3a19cde 293 struct xfs_attrlist_cursor_kern *cursor = &context->cursor;
bdaac93f 294 struct xfs_attr3_icleaf_hdr leafhdr;
bdaac93f
DW
295 struct xfs_attr_leafblock *leaf;
296 struct xfs_da_intnode *node;
297 struct xfs_buf *bp;
298 struct xfs_inode *dp = context->dp;
299 struct xfs_mount *mp = dp->i_mount;
16c6e92c 300 int error = 0;
abec5f2b
DC
301
302 trace_xfs_attr_node_list(context);
303
abec5f2b
DC
304 cursor->initted = 1;
305
306 /*
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.
310 */
311 bp = NULL;
312 if (cursor->blkno > 0) {
02c57f0a
CH
313 error = xfs_da3_node_read(context->tp, dp, cursor->blkno, &bp,
314 XFS_ATTR_FORK);
ca14c096
DW
315 if (xfs_metadata_is_sick(error))
316 xfs_dirattr_mark_sick(dp, XFS_ATTR_FORK);
2451337d 317 if ((error != 0) && (error != -EFSCORRUPTED))
d99831ff 318 return error;
abec5f2b
DC
319 if (bp) {
320 struct xfs_attr_leaf_entry *entries;
321
322 node = bp->b_addr;
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);
ad017f65 327 xfs_trans_brelse(context->tp, bp);
abec5f2b
DC
328 bp = NULL;
329 break;
330 case XFS_ATTR_LEAF_MAGIC:
331 case XFS_ATTR3_LEAF_MAGIC:
332 leaf = bp->b_addr;
2f661241
BF
333 xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo,
334 &leafhdr, leaf);
abec5f2b
DC
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);
ad017f65 339 xfs_trans_brelse(context->tp, bp);
abec5f2b
DC
340 bp = NULL;
341 } else if (cursor->hashval <= be32_to_cpu(
342 entries[0].hashval)) {
343 trace_xfs_attr_list_wrong_blk(context);
ad017f65 344 xfs_trans_brelse(context->tp, bp);
abec5f2b
DC
345 bp = NULL;
346 }
347 break;
348 default:
349 trace_xfs_attr_list_wrong_blk(context);
ad017f65 350 xfs_trans_brelse(context->tp, bp);
abec5f2b
DC
351 bp = NULL;
352 }
353 }
354 }
355
356 /*
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.
360 */
361 if (bp == NULL) {
bdaac93f
DW
362 error = xfs_attr_node_list_lookup(context, cursor, &bp);
363 if (error || !bp)
364 return error;
abec5f2b
DC
365 }
366 ASSERT(bp != NULL);
367
368 /*
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.
372 */
373 for (;;) {
374 leaf = bp->b_addr;
16c6e92c
DW
375 error = xfs_attr3_leaf_list_int(bp, context);
376 if (error)
377 break;
2f661241 378 xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &leafhdr, leaf);
abec5f2b
DC
379 if (context->seen_enough || leafhdr.forw == 0)
380 break;
381 cursor->blkno = leafhdr.forw;
ad017f65 382 xfs_trans_brelse(context->tp, bp);
dfb87594
CH
383 error = xfs_attr3_leaf_read(context->tp, dp, cursor->blkno,
384 &bp);
abec5f2b
DC
385 if (error)
386 return error;
387 }
ad017f65 388 xfs_trans_brelse(context->tp, bp);
16c6e92c 389 return error;
abec5f2b
DC
390}
391
392/*
393 * Copy out attribute list entries for attr_list(), for leaf attribute lists.
394 */
16c6e92c 395int
abec5f2b
DC
396xfs_attr3_leaf_list_int(
397 struct xfs_buf *bp,
398 struct xfs_attr_list_context *context)
399{
e3a19cde 400 struct xfs_attrlist_cursor_kern *cursor = &context->cursor;
abec5f2b
DC
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;
abec5f2b 405 int i;
2f661241 406 struct xfs_mount *mp = context->dp->i_mount;
abec5f2b
DC
407
408 trace_xfs_attr_list_leaf(context);
409
410 leaf = bp->b_addr;
2f661241 411 xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr, leaf);
abec5f2b
DC
412 entries = xfs_attr3_leaf_entryp(leaf);
413
abec5f2b
DC
414 cursor->initted = 1;
415
416 /*
417 * Re-find our place in the leaf block if this is a new syscall.
418 */
419 if (context->resynch) {
420 entry = &entries[0];
421 for (i = 0; i < ichdr.count; entry++, i++) {
422 if (be32_to_cpu(entry->hashval) == cursor->hashval) {
423 if (cursor->offset == context->dupcnt) {
424 context->dupcnt = 0;
425 break;
426 }
427 context->dupcnt++;
428 } else if (be32_to_cpu(entry->hashval) >
429 cursor->hashval) {
430 context->dupcnt = 0;
431 break;
432 }
433 }
434 if (i == ichdr.count) {
435 trace_xfs_attr_list_notfound(context);
16c6e92c 436 return 0;
abec5f2b
DC
437 }
438 } else {
439 entry = &entries[0];
440 i = 0;
441 }
442 context->resynch = 0;
443
444 /*
445 * We have found our place, start copying out the new attributes.
446 */
abec5f2b 447 for (; i < ichdr.count; entry++, i++) {
3ab3ffca
ES
448 char *name;
449 int namelen, valuelen;
450
abec5f2b
DC
451 if (be32_to_cpu(entry->hashval) != cursor->hashval) {
452 cursor->hashval = be32_to_cpu(entry->hashval);
453 cursor->offset = 0;
454 }
455
eec0482e 456 if ((entry->flags & XFS_ATTR_INCOMPLETE) &&
5e813574
CH
457 !context->allow_incomplete)
458 continue;
abec5f2b
DC
459
460 if (entry->flags & XFS_ATTR_LOCAL) {
3ab3ffca 461 xfs_attr_leaf_name_local_t *name_loc;
abec5f2b 462
3ab3ffca
ES
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);
467 } else {
468 xfs_attr_leaf_name_remote_t *name_rmt;
abec5f2b 469
3ab3ffca
ES
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);
abec5f2b 474 }
3ab3ffca 475
a71895c5 476 if (XFS_IS_CORRUPT(context->dp->i_mount,
989d5ec3
DW
477 !xfs_attr_namecheck(name, namelen))) {
478 xfs_dirattr_mark_sick(context->dp, XFS_ATTR_FORK);
16c6e92c 479 return -EFSCORRUPTED;
989d5ec3 480 }
f7a136ae 481 context->put_listent(context, entry->flags,
3ab3ffca 482 name, namelen, valuelen);
abec5f2b
DC
483 if (context->seen_enough)
484 break;
485 cursor->offset++;
486 }
487 trace_xfs_attr_list_leaf_end(context);
16c6e92c 488 return 0;
abec5f2b
DC
489}
490
491/*
492 * Copy out attribute entries for attr_list(), for leaf attribute lists.
493 */
494STATIC int
a9c8c69b
CH
495xfs_attr_leaf_list(
496 struct xfs_attr_list_context *context)
abec5f2b 497{
a9c8c69b
CH
498 struct xfs_buf *bp;
499 int error;
abec5f2b
DC
500
501 trace_xfs_attr_leaf_list(context);
502
e3a19cde 503 context->cursor.blkno = 0;
dfb87594 504 error = xfs_attr3_leaf_read(context->tp, context->dp, 0, &bp);
abec5f2b 505 if (error)
b474c7ae 506 return error;
abec5f2b 507
16c6e92c 508 error = xfs_attr3_leaf_list_int(bp, context);
ad017f65 509 xfs_trans_brelse(context->tp, bp);
16c6e92c 510 return error;
abec5f2b
DC
511}
512
ad017f65 513int
17e1dd83 514xfs_attr_list_ilocked(
ad017f65
DW
515 struct xfs_attr_list_context *context)
516{
517 struct xfs_inode *dp = context->dp;
518
3fed24ff 519 xfs_assert_ilocked(dp, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL);
5af7777e 520
ad017f65
DW
521 /*
522 * Decide on what work routines to call based on the inode size.
523 */
524 if (!xfs_inode_hasattr(dp))
525 return 0;
2ed5b09b 526 if (dp->i_af.if_format == XFS_DINODE_FMT_LOCAL)
ad017f65 527 return xfs_attr_shortform_list(context);
2ac131df 528 if (xfs_attr_is_leaf(dp))
ad017f65
DW
529 return xfs_attr_leaf_list(context);
530 return xfs_attr_node_list(context);
531}
532
abec5f2b 533int
17e1dd83 534xfs_attr_list(
a9c8c69b 535 struct xfs_attr_list_context *context)
abec5f2b 536{
a9c8c69b
CH
537 struct xfs_inode *dp = context->dp;
538 uint lock_mode;
539 int error;
abec5f2b 540
ff6d6af2 541 XFS_STATS_INC(dp->i_mount, xs_attr_list);
abec5f2b 542
75c8c50f 543 if (xfs_is_shutdown(dp->i_mount))
2451337d 544 return -EIO;
abec5f2b 545
568d994e 546 lock_mode = xfs_ilock_attr_map_shared(dp);
17e1dd83 547 error = xfs_attr_list_ilocked(context);
568d994e 548 xfs_iunlock(dp, lock_mode);
abec5f2b
DC
549 return error;
550}