2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * Copyright (c) 2013 Red Hat, Inc.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it would be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "xfs_log_format.h"
22 #include "xfs_trans_resv.h"
26 #include "xfs_mount.h"
27 #include "xfs_da_format.h"
28 #include "xfs_da_btree.h"
29 #include "xfs_bmap_btree.h"
30 #include "xfs_alloc_btree.h"
31 #include "xfs_ialloc_btree.h"
32 #include "xfs_alloc.h"
33 #include "xfs_btree.h"
34 #include "xfs_attr_sf.h"
35 #include "xfs_attr_remote.h"
36 #include "xfs_dinode.h"
37 #include "xfs_inode.h"
38 #include "xfs_trans.h"
39 #include "xfs_inode_item.h"
42 #include "xfs_attr_leaf.h"
43 #include "xfs_error.h"
44 #include "xfs_trace.h"
45 #include "xfs_buf_item.h"
46 #include "xfs_cksum.h"
49 xfs_attr_shortform_compare(const void *a
, const void *b
)
51 xfs_attr_sf_sort_t
*sa
, *sb
;
53 sa
= (xfs_attr_sf_sort_t
*)a
;
54 sb
= (xfs_attr_sf_sort_t
*)b
;
55 if (sa
->hash
< sb
->hash
) {
57 } else if (sa
->hash
> sb
->hash
) {
60 return(sa
->entno
- sb
->entno
);
64 #define XFS_ISRESET_CURSOR(cursor) \
65 (!((cursor)->initted) && !((cursor)->hashval) && \
66 !((cursor)->blkno) && !((cursor)->offset))
68 * Copy out entries of shortform attribute lists for attr_list().
69 * Shortform attribute lists are not stored in hashval sorted order.
70 * If the output buffer is not large enough to hold them all, then we
71 * we have to calculate each entries' hashvalue and sort them before
72 * we can begin returning them to the user.
75 xfs_attr_shortform_list(xfs_attr_list_context_t
*context
)
77 attrlist_cursor_kern_t
*cursor
;
78 xfs_attr_sf_sort_t
*sbuf
, *sbp
;
79 xfs_attr_shortform_t
*sf
;
80 xfs_attr_sf_entry_t
*sfe
;
82 int sbsize
, nsbuf
, count
, i
;
85 ASSERT(context
!= NULL
);
88 ASSERT(dp
->i_afp
!= NULL
);
89 sf
= (xfs_attr_shortform_t
*)dp
->i_afp
->if_u1
.if_data
;
93 cursor
= context
->cursor
;
94 ASSERT(cursor
!= NULL
);
96 trace_xfs_attr_list_sf(context
);
99 * If the buffer is large enough and the cursor is at the start,
100 * do not bother with sorting since we will return everything in
101 * one buffer and another call using the cursor won't need to be
103 * Note the generous fudge factor of 16 overhead bytes per entry.
104 * If bufsize is zero then put_listent must be a search function
105 * and can just scan through what we have.
107 if (context
->bufsize
== 0 ||
108 (XFS_ISRESET_CURSOR(cursor
) &&
109 (dp
->i_afp
->if_bytes
+ sf
->hdr
.count
* 16) < context
->bufsize
)) {
110 for (i
= 0, sfe
= &sf
->list
[0]; i
< sf
->hdr
.count
; i
++) {
111 error
= context
->put_listent(context
,
116 &sfe
->nameval
[sfe
->namelen
]);
119 * Either search callback finished early or
120 * didn't fit it all in the buffer after all.
122 if (context
->seen_enough
)
127 sfe
= XFS_ATTR_SF_NEXTENTRY(sfe
);
129 trace_xfs_attr_list_sf_all(context
);
133 /* do no more for a search callback */
134 if (context
->bufsize
== 0)
138 * It didn't all fit, so we have to sort everything on hashval.
140 sbsize
= sf
->hdr
.count
* sizeof(*sbuf
);
141 sbp
= sbuf
= kmem_alloc(sbsize
, KM_SLEEP
| KM_NOFS
);
144 * Scan the attribute list for the rest of the entries, storing
145 * the relevant info from only those that match into a buffer.
148 for (i
= 0, sfe
= &sf
->list
[0]; i
< sf
->hdr
.count
; i
++) {
150 ((char *)sfe
< (char *)sf
) ||
151 ((char *)sfe
>= ((char *)sf
+ dp
->i_afp
->if_bytes
)))) {
152 XFS_CORRUPTION_ERROR("xfs_attr_shortform_list",
154 context
->dp
->i_mount
, sfe
);
156 return XFS_ERROR(EFSCORRUPTED
);
160 sbp
->hash
= xfs_da_hashname(sfe
->nameval
, sfe
->namelen
);
161 sbp
->name
= sfe
->nameval
;
162 sbp
->namelen
= sfe
->namelen
;
163 /* These are bytes, and both on-disk, don't endian-flip */
164 sbp
->valuelen
= sfe
->valuelen
;
165 sbp
->flags
= sfe
->flags
;
166 sfe
= XFS_ATTR_SF_NEXTENTRY(sfe
);
172 * Sort the entries on hash then entno.
174 xfs_sort(sbuf
, nsbuf
, sizeof(*sbuf
), xfs_attr_shortform_compare
);
177 * Re-find our place IN THE SORTED LIST.
182 for (sbp
= sbuf
, i
= 0; i
< nsbuf
; i
++, sbp
++) {
183 if (sbp
->hash
== cursor
->hashval
) {
184 if (cursor
->offset
== count
) {
188 } else if (sbp
->hash
> cursor
->hashval
) {
198 * Loop putting entries into the user buffer.
200 for ( ; i
< nsbuf
; i
++, sbp
++) {
201 if (cursor
->hashval
!= sbp
->hash
) {
202 cursor
->hashval
= sbp
->hash
;
205 error
= context
->put_listent(context
,
210 &sbp
->name
[sbp
->namelen
]);
213 if (context
->seen_enough
)
223 xfs_attr_node_list(xfs_attr_list_context_t
*context
)
225 attrlist_cursor_kern_t
*cursor
;
226 xfs_attr_leafblock_t
*leaf
;
227 xfs_da_intnode_t
*node
;
228 struct xfs_attr3_icleaf_hdr leafhdr
;
229 struct xfs_da3_icnode_hdr nodehdr
;
230 struct xfs_da_node_entry
*btree
;
234 trace_xfs_attr_node_list(context
);
236 cursor
= context
->cursor
;
240 * Do all sorts of validation on the passed-in cursor structure.
241 * If anything is amiss, ignore the cursor and look up the hashval
242 * starting from the btree root.
245 if (cursor
->blkno
> 0) {
246 error
= xfs_da3_node_read(NULL
, context
->dp
, cursor
->blkno
, -1,
248 if ((error
!= 0) && (error
!= EFSCORRUPTED
))
251 struct xfs_attr_leaf_entry
*entries
;
254 switch (be16_to_cpu(node
->hdr
.info
.magic
)) {
255 case XFS_DA_NODE_MAGIC
:
256 case XFS_DA3_NODE_MAGIC
:
257 trace_xfs_attr_list_wrong_blk(context
);
258 xfs_trans_brelse(NULL
, bp
);
261 case XFS_ATTR_LEAF_MAGIC
:
262 case XFS_ATTR3_LEAF_MAGIC
:
264 xfs_attr3_leaf_hdr_from_disk(&leafhdr
, leaf
);
265 entries
= xfs_attr3_leaf_entryp(leaf
);
266 if (cursor
->hashval
> be32_to_cpu(
267 entries
[leafhdr
.count
- 1].hashval
)) {
268 trace_xfs_attr_list_wrong_blk(context
);
269 xfs_trans_brelse(NULL
, bp
);
271 } else if (cursor
->hashval
<= be32_to_cpu(
272 entries
[0].hashval
)) {
273 trace_xfs_attr_list_wrong_blk(context
);
274 xfs_trans_brelse(NULL
, bp
);
279 trace_xfs_attr_list_wrong_blk(context
);
280 xfs_trans_brelse(NULL
, bp
);
287 * We did not find what we expected given the cursor's contents,
288 * so we start from the top and work down based on the hash value.
289 * Note that start of node block is same as start of leaf block.
296 error
= xfs_da3_node_read(NULL
, context
->dp
,
297 cursor
->blkno
, -1, &bp
,
302 magic
= be16_to_cpu(node
->hdr
.info
.magic
);
303 if (magic
== XFS_ATTR_LEAF_MAGIC
||
304 magic
== XFS_ATTR3_LEAF_MAGIC
)
306 if (magic
!= XFS_DA_NODE_MAGIC
&&
307 magic
!= XFS_DA3_NODE_MAGIC
) {
308 XFS_CORRUPTION_ERROR("xfs_attr_node_list(3)",
310 context
->dp
->i_mount
,
312 xfs_trans_brelse(NULL
, bp
);
313 return XFS_ERROR(EFSCORRUPTED
);
316 xfs_da3_node_hdr_from_disk(&nodehdr
, node
);
317 btree
= xfs_da3_node_tree_p(node
);
318 for (i
= 0; i
< nodehdr
.count
; btree
++, i
++) {
320 <= be32_to_cpu(btree
->hashval
)) {
321 cursor
->blkno
= be32_to_cpu(btree
->before
);
322 trace_xfs_attr_list_node_descend(context
,
327 if (i
== nodehdr
.count
) {
328 xfs_trans_brelse(NULL
, bp
);
331 xfs_trans_brelse(NULL
, bp
);
337 * Roll upward through the blocks, processing each leaf block in
338 * order. As long as there is space in the result buffer, keep
339 * adding the information.
343 error
= xfs_attr3_leaf_list_int(bp
, context
);
345 xfs_trans_brelse(NULL
, bp
);
348 xfs_attr3_leaf_hdr_from_disk(&leafhdr
, leaf
);
349 if (context
->seen_enough
|| leafhdr
.forw
== 0)
351 cursor
->blkno
= leafhdr
.forw
;
352 xfs_trans_brelse(NULL
, bp
);
353 error
= xfs_attr3_leaf_read(NULL
, context
->dp
, cursor
->blkno
, -1,
358 xfs_trans_brelse(NULL
, bp
);
363 * Copy out attribute list entries for attr_list(), for leaf attribute lists.
366 xfs_attr3_leaf_list_int(
368 struct xfs_attr_list_context
*context
)
370 struct attrlist_cursor_kern
*cursor
;
371 struct xfs_attr_leafblock
*leaf
;
372 struct xfs_attr3_icleaf_hdr ichdr
;
373 struct xfs_attr_leaf_entry
*entries
;
374 struct xfs_attr_leaf_entry
*entry
;
378 trace_xfs_attr_list_leaf(context
);
381 xfs_attr3_leaf_hdr_from_disk(&ichdr
, leaf
);
382 entries
= xfs_attr3_leaf_entryp(leaf
);
384 cursor
= context
->cursor
;
388 * Re-find our place in the leaf block if this is a new syscall.
390 if (context
->resynch
) {
392 for (i
= 0; i
< ichdr
.count
; entry
++, i
++) {
393 if (be32_to_cpu(entry
->hashval
) == cursor
->hashval
) {
394 if (cursor
->offset
== context
->dupcnt
) {
399 } else if (be32_to_cpu(entry
->hashval
) >
405 if (i
== ichdr
.count
) {
406 trace_xfs_attr_list_notfound(context
);
413 context
->resynch
= 0;
416 * We have found our place, start copying out the new attributes.
419 for (; i
< ichdr
.count
; entry
++, i
++) {
420 if (be32_to_cpu(entry
->hashval
) != cursor
->hashval
) {
421 cursor
->hashval
= be32_to_cpu(entry
->hashval
);
425 if (entry
->flags
& XFS_ATTR_INCOMPLETE
)
426 continue; /* skip incomplete entries */
428 if (entry
->flags
& XFS_ATTR_LOCAL
) {
429 xfs_attr_leaf_name_local_t
*name_loc
=
430 xfs_attr3_leaf_name_local(leaf
, i
);
432 retval
= context
->put_listent(context
,
435 (int)name_loc
->namelen
,
436 be16_to_cpu(name_loc
->valuelen
),
437 &name_loc
->nameval
[name_loc
->namelen
]);
441 xfs_attr_leaf_name_remote_t
*name_rmt
=
442 xfs_attr3_leaf_name_remote(leaf
, i
);
444 int valuelen
= be32_to_cpu(name_rmt
->valuelen
);
446 if (context
->put_value
) {
449 memset((char *)&args
, 0, sizeof(args
));
450 args
.dp
= context
->dp
;
451 args
.whichfork
= XFS_ATTR_FORK
;
452 args
.valuelen
= valuelen
;
453 args
.value
= kmem_alloc(valuelen
, KM_SLEEP
| KM_NOFS
);
454 args
.rmtblkno
= be32_to_cpu(name_rmt
->valueblk
);
455 args
.rmtblkcnt
= xfs_attr3_rmt_blocks(
456 args
.dp
->i_mount
, valuelen
);
457 retval
= xfs_attr_rmtval_get(&args
);
460 retval
= context
->put_listent(context
,
463 (int)name_rmt
->namelen
,
466 kmem_free(args
.value
);
468 retval
= context
->put_listent(context
,
471 (int)name_rmt
->namelen
,
478 if (context
->seen_enough
)
482 trace_xfs_attr_list_leaf_end(context
);
487 * Copy out attribute entries for attr_list(), for leaf attribute lists.
490 xfs_attr_leaf_list(xfs_attr_list_context_t
*context
)
495 trace_xfs_attr_leaf_list(context
);
497 context
->cursor
->blkno
= 0;
498 error
= xfs_attr3_leaf_read(NULL
, context
->dp
, 0, -1, &bp
);
500 return XFS_ERROR(error
);
502 error
= xfs_attr3_leaf_list_int(bp
, context
);
503 xfs_trans_brelse(NULL
, bp
);
504 return XFS_ERROR(error
);
509 xfs_attr_list_context_t
*context
)
512 xfs_inode_t
*dp
= context
->dp
;
514 XFS_STATS_INC(xs_attr_list
);
516 if (XFS_FORCED_SHUTDOWN(dp
->i_mount
))
519 xfs_ilock(dp
, XFS_ILOCK_SHARED
);
522 * Decide on what work routines to call based on the inode size.
524 if (!xfs_inode_hasattr(dp
)) {
526 } else if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
) {
527 error
= xfs_attr_shortform_list(context
);
528 } else if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
)) {
529 error
= xfs_attr_leaf_list(context
);
531 error
= xfs_attr_node_list(context
);
534 xfs_iunlock(dp
, XFS_ILOCK_SHARED
);
539 #define ATTR_ENTBASESIZE /* minimum bytes used by an attr */ \
540 (((struct attrlist_ent *) 0)->a_name - (char *) 0)
541 #define ATTR_ENTSIZE(namelen) /* actual bytes used by an attr */ \
542 ((ATTR_ENTBASESIZE + (namelen) + 1 + sizeof(u_int32_t)-1) \
543 & ~(sizeof(u_int32_t)-1))
546 * Format an attribute and copy it out to the user's buffer.
547 * Take care to check values and protect against them changing later,
548 * we may be reading them directly out of a user buffer.
551 xfs_attr_put_listent(
552 xfs_attr_list_context_t
*context
,
557 unsigned char *value
)
559 struct attrlist
*alist
= (struct attrlist
*)context
->alist
;
563 ASSERT(!(context
->flags
& ATTR_KERNOVAL
));
564 ASSERT(context
->count
>= 0);
565 ASSERT(context
->count
< (ATTR_MAX_VALUELEN
/8));
566 ASSERT(context
->firstu
>= sizeof(*alist
));
567 ASSERT(context
->firstu
<= context
->bufsize
);
570 * Only list entries in the right namespace.
572 if (((context
->flags
& ATTR_SECURE
) == 0) !=
573 ((flags
& XFS_ATTR_SECURE
) == 0))
575 if (((context
->flags
& ATTR_ROOT
) == 0) !=
576 ((flags
& XFS_ATTR_ROOT
) == 0))
579 arraytop
= sizeof(*alist
) +
580 context
->count
* sizeof(alist
->al_offset
[0]);
581 context
->firstu
-= ATTR_ENTSIZE(namelen
);
582 if (context
->firstu
< arraytop
) {
583 trace_xfs_attr_list_full(context
);
585 context
->seen_enough
= 1;
589 aep
= (attrlist_ent_t
*)&context
->alist
[context
->firstu
];
590 aep
->a_valuelen
= valuelen
;
591 memcpy(aep
->a_name
, name
, namelen
);
592 aep
->a_name
[namelen
] = 0;
593 alist
->al_offset
[context
->count
++] = context
->firstu
;
594 alist
->al_count
= context
->count
;
595 trace_xfs_attr_list_add(context
);
600 * Generate a list of extended attribute names and optionally
601 * also value lengths. Positive return value follows the XFS
602 * convention of being an error, zero or negative return code
603 * is the length of the buffer returned (negated), indicating
612 attrlist_cursor_kern_t
*cursor
)
614 xfs_attr_list_context_t context
;
615 struct attrlist
*alist
;
619 * Validate the cursor.
621 if (cursor
->pad1
|| cursor
->pad2
)
622 return(XFS_ERROR(EINVAL
));
623 if ((cursor
->initted
== 0) &&
624 (cursor
->hashval
|| cursor
->blkno
|| cursor
->offset
))
625 return XFS_ERROR(EINVAL
);
628 * Check for a properly aligned buffer.
630 if (((long)buffer
) & (sizeof(int)-1))
631 return XFS_ERROR(EFAULT
);
632 if (flags
& ATTR_KERNOVAL
)
636 * Initialize the output buffer.
638 memset(&context
, 0, sizeof(context
));
640 context
.cursor
= cursor
;
642 context
.flags
= flags
;
643 context
.alist
= buffer
;
644 context
.bufsize
= (bufsize
& ~(sizeof(int)-1)); /* align */
645 context
.firstu
= context
.bufsize
;
646 context
.put_listent
= xfs_attr_put_listent
;
648 alist
= (struct attrlist
*)context
.alist
;
651 alist
->al_offset
[0] = context
.bufsize
;
653 error
= xfs_attr_list_int(&context
);