xfs: decouple log and transaction headers
[deliverable/linux.git] / fs / xfs / xfs_attr_list.c
1 /*
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * Copyright (c) 2013 Red Hat, Inc.
4 * All Rights Reserved.
5 *
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.
9 *
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.
14 *
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
18 */
19 #include "xfs.h"
20 #include "xfs_fs.h"
21 #include "xfs_log_format.h"
22 #include "xfs_trans_resv.h"
23 #include "xfs_bit.h"
24 #include "xfs_sb.h"
25 #include "xfs_ag.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"
40 #include "xfs_bmap.h"
41 #include "xfs_attr.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"
47
48 STATIC int
49 xfs_attr_shortform_compare(const void *a, const void *b)
50 {
51 xfs_attr_sf_sort_t *sa, *sb;
52
53 sa = (xfs_attr_sf_sort_t *)a;
54 sb = (xfs_attr_sf_sort_t *)b;
55 if (sa->hash < sb->hash) {
56 return(-1);
57 } else if (sa->hash > sb->hash) {
58 return(1);
59 } else {
60 return(sa->entno - sb->entno);
61 }
62 }
63
64 #define XFS_ISRESET_CURSOR(cursor) \
65 (!((cursor)->initted) && !((cursor)->hashval) && \
66 !((cursor)->blkno) && !((cursor)->offset))
67 /*
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.
73 */
74 int
75 xfs_attr_shortform_list(xfs_attr_list_context_t *context)
76 {
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;
81 xfs_inode_t *dp;
82 int sbsize, nsbuf, count, i;
83 int error;
84
85 ASSERT(context != NULL);
86 dp = context->dp;
87 ASSERT(dp != NULL);
88 ASSERT(dp->i_afp != NULL);
89 sf = (xfs_attr_shortform_t *)dp->i_afp->if_u1.if_data;
90 ASSERT(sf != NULL);
91 if (!sf->hdr.count)
92 return(0);
93 cursor = context->cursor;
94 ASSERT(cursor != NULL);
95
96 trace_xfs_attr_list_sf(context);
97
98 /*
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
102 * made.
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.
106 */
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,
112 sfe->flags,
113 sfe->nameval,
114 (int)sfe->namelen,
115 (int)sfe->valuelen,
116 &sfe->nameval[sfe->namelen]);
117
118 /*
119 * Either search callback finished early or
120 * didn't fit it all in the buffer after all.
121 */
122 if (context->seen_enough)
123 break;
124
125 if (error)
126 return error;
127 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
128 }
129 trace_xfs_attr_list_sf_all(context);
130 return(0);
131 }
132
133 /* do no more for a search callback */
134 if (context->bufsize == 0)
135 return 0;
136
137 /*
138 * It didn't all fit, so we have to sort everything on hashval.
139 */
140 sbsize = sf->hdr.count * sizeof(*sbuf);
141 sbp = sbuf = kmem_alloc(sbsize, KM_SLEEP | KM_NOFS);
142
143 /*
144 * Scan the attribute list for the rest of the entries, storing
145 * the relevant info from only those that match into a buffer.
146 */
147 nsbuf = 0;
148 for (i = 0, sfe = &sf->list[0]; i < sf->hdr.count; i++) {
149 if (unlikely(
150 ((char *)sfe < (char *)sf) ||
151 ((char *)sfe >= ((char *)sf + dp->i_afp->if_bytes)))) {
152 XFS_CORRUPTION_ERROR("xfs_attr_shortform_list",
153 XFS_ERRLEVEL_LOW,
154 context->dp->i_mount, sfe);
155 kmem_free(sbuf);
156 return XFS_ERROR(EFSCORRUPTED);
157 }
158
159 sbp->entno = i;
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);
167 sbp++;
168 nsbuf++;
169 }
170
171 /*
172 * Sort the entries on hash then entno.
173 */
174 xfs_sort(sbuf, nsbuf, sizeof(*sbuf), xfs_attr_shortform_compare);
175
176 /*
177 * Re-find our place IN THE SORTED LIST.
178 */
179 count = 0;
180 cursor->initted = 1;
181 cursor->blkno = 0;
182 for (sbp = sbuf, i = 0; i < nsbuf; i++, sbp++) {
183 if (sbp->hash == cursor->hashval) {
184 if (cursor->offset == count) {
185 break;
186 }
187 count++;
188 } else if (sbp->hash > cursor->hashval) {
189 break;
190 }
191 }
192 if (i == nsbuf) {
193 kmem_free(sbuf);
194 return(0);
195 }
196
197 /*
198 * Loop putting entries into the user buffer.
199 */
200 for ( ; i < nsbuf; i++, sbp++) {
201 if (cursor->hashval != sbp->hash) {
202 cursor->hashval = sbp->hash;
203 cursor->offset = 0;
204 }
205 error = context->put_listent(context,
206 sbp->flags,
207 sbp->name,
208 sbp->namelen,
209 sbp->valuelen,
210 &sbp->name[sbp->namelen]);
211 if (error)
212 return error;
213 if (context->seen_enough)
214 break;
215 cursor->offset++;
216 }
217
218 kmem_free(sbuf);
219 return(0);
220 }
221
222 STATIC int
223 xfs_attr_node_list(xfs_attr_list_context_t *context)
224 {
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;
231 int error, i;
232 struct xfs_buf *bp;
233
234 trace_xfs_attr_node_list(context);
235
236 cursor = context->cursor;
237 cursor->initted = 1;
238
239 /*
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.
243 */
244 bp = NULL;
245 if (cursor->blkno > 0) {
246 error = xfs_da3_node_read(NULL, context->dp, cursor->blkno, -1,
247 &bp, XFS_ATTR_FORK);
248 if ((error != 0) && (error != EFSCORRUPTED))
249 return(error);
250 if (bp) {
251 struct xfs_attr_leaf_entry *entries;
252
253 node = bp->b_addr;
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);
259 bp = NULL;
260 break;
261 case XFS_ATTR_LEAF_MAGIC:
262 case XFS_ATTR3_LEAF_MAGIC:
263 leaf = bp->b_addr;
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);
270 bp = NULL;
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);
275 bp = NULL;
276 }
277 break;
278 default:
279 trace_xfs_attr_list_wrong_blk(context);
280 xfs_trans_brelse(NULL, bp);
281 bp = NULL;
282 }
283 }
284 }
285
286 /*
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.
290 */
291 if (bp == NULL) {
292 cursor->blkno = 0;
293 for (;;) {
294 __uint16_t magic;
295
296 error = xfs_da3_node_read(NULL, context->dp,
297 cursor->blkno, -1, &bp,
298 XFS_ATTR_FORK);
299 if (error)
300 return(error);
301 node = bp->b_addr;
302 magic = be16_to_cpu(node->hdr.info.magic);
303 if (magic == XFS_ATTR_LEAF_MAGIC ||
304 magic == XFS_ATTR3_LEAF_MAGIC)
305 break;
306 if (magic != XFS_DA_NODE_MAGIC &&
307 magic != XFS_DA3_NODE_MAGIC) {
308 XFS_CORRUPTION_ERROR("xfs_attr_node_list(3)",
309 XFS_ERRLEVEL_LOW,
310 context->dp->i_mount,
311 node);
312 xfs_trans_brelse(NULL, bp);
313 return XFS_ERROR(EFSCORRUPTED);
314 }
315
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++) {
319 if (cursor->hashval
320 <= be32_to_cpu(btree->hashval)) {
321 cursor->blkno = be32_to_cpu(btree->before);
322 trace_xfs_attr_list_node_descend(context,
323 btree);
324 break;
325 }
326 }
327 if (i == nodehdr.count) {
328 xfs_trans_brelse(NULL, bp);
329 return 0;
330 }
331 xfs_trans_brelse(NULL, bp);
332 }
333 }
334 ASSERT(bp != NULL);
335
336 /*
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.
340 */
341 for (;;) {
342 leaf = bp->b_addr;
343 error = xfs_attr3_leaf_list_int(bp, context);
344 if (error) {
345 xfs_trans_brelse(NULL, bp);
346 return error;
347 }
348 xfs_attr3_leaf_hdr_from_disk(&leafhdr, leaf);
349 if (context->seen_enough || leafhdr.forw == 0)
350 break;
351 cursor->blkno = leafhdr.forw;
352 xfs_trans_brelse(NULL, bp);
353 error = xfs_attr3_leaf_read(NULL, context->dp, cursor->blkno, -1,
354 &bp);
355 if (error)
356 return error;
357 }
358 xfs_trans_brelse(NULL, bp);
359 return 0;
360 }
361
362 /*
363 * Copy out attribute list entries for attr_list(), for leaf attribute lists.
364 */
365 int
366 xfs_attr3_leaf_list_int(
367 struct xfs_buf *bp,
368 struct xfs_attr_list_context *context)
369 {
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;
375 int retval;
376 int i;
377
378 trace_xfs_attr_list_leaf(context);
379
380 leaf = bp->b_addr;
381 xfs_attr3_leaf_hdr_from_disk(&ichdr, leaf);
382 entries = xfs_attr3_leaf_entryp(leaf);
383
384 cursor = context->cursor;
385 cursor->initted = 1;
386
387 /*
388 * Re-find our place in the leaf block if this is a new syscall.
389 */
390 if (context->resynch) {
391 entry = &entries[0];
392 for (i = 0; i < ichdr.count; entry++, i++) {
393 if (be32_to_cpu(entry->hashval) == cursor->hashval) {
394 if (cursor->offset == context->dupcnt) {
395 context->dupcnt = 0;
396 break;
397 }
398 context->dupcnt++;
399 } else if (be32_to_cpu(entry->hashval) >
400 cursor->hashval) {
401 context->dupcnt = 0;
402 break;
403 }
404 }
405 if (i == ichdr.count) {
406 trace_xfs_attr_list_notfound(context);
407 return 0;
408 }
409 } else {
410 entry = &entries[0];
411 i = 0;
412 }
413 context->resynch = 0;
414
415 /*
416 * We have found our place, start copying out the new attributes.
417 */
418 retval = 0;
419 for (; i < ichdr.count; entry++, i++) {
420 if (be32_to_cpu(entry->hashval) != cursor->hashval) {
421 cursor->hashval = be32_to_cpu(entry->hashval);
422 cursor->offset = 0;
423 }
424
425 if (entry->flags & XFS_ATTR_INCOMPLETE)
426 continue; /* skip incomplete entries */
427
428 if (entry->flags & XFS_ATTR_LOCAL) {
429 xfs_attr_leaf_name_local_t *name_loc =
430 xfs_attr3_leaf_name_local(leaf, i);
431
432 retval = context->put_listent(context,
433 entry->flags,
434 name_loc->nameval,
435 (int)name_loc->namelen,
436 be16_to_cpu(name_loc->valuelen),
437 &name_loc->nameval[name_loc->namelen]);
438 if (retval)
439 return retval;
440 } else {
441 xfs_attr_leaf_name_remote_t *name_rmt =
442 xfs_attr3_leaf_name_remote(leaf, i);
443
444 int valuelen = be32_to_cpu(name_rmt->valuelen);
445
446 if (context->put_value) {
447 xfs_da_args_t args;
448
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);
458 if (retval)
459 return retval;
460 retval = context->put_listent(context,
461 entry->flags,
462 name_rmt->name,
463 (int)name_rmt->namelen,
464 valuelen,
465 args.value);
466 kmem_free(args.value);
467 } else {
468 retval = context->put_listent(context,
469 entry->flags,
470 name_rmt->name,
471 (int)name_rmt->namelen,
472 valuelen,
473 NULL);
474 }
475 if (retval)
476 return retval;
477 }
478 if (context->seen_enough)
479 break;
480 cursor->offset++;
481 }
482 trace_xfs_attr_list_leaf_end(context);
483 return retval;
484 }
485
486 /*
487 * Copy out attribute entries for attr_list(), for leaf attribute lists.
488 */
489 STATIC int
490 xfs_attr_leaf_list(xfs_attr_list_context_t *context)
491 {
492 int error;
493 struct xfs_buf *bp;
494
495 trace_xfs_attr_leaf_list(context);
496
497 context->cursor->blkno = 0;
498 error = xfs_attr3_leaf_read(NULL, context->dp, 0, -1, &bp);
499 if (error)
500 return XFS_ERROR(error);
501
502 error = xfs_attr3_leaf_list_int(bp, context);
503 xfs_trans_brelse(NULL, bp);
504 return XFS_ERROR(error);
505 }
506
507 int
508 xfs_attr_list_int(
509 xfs_attr_list_context_t *context)
510 {
511 int error;
512 xfs_inode_t *dp = context->dp;
513
514 XFS_STATS_INC(xs_attr_list);
515
516 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
517 return EIO;
518
519 xfs_ilock(dp, XFS_ILOCK_SHARED);
520
521 /*
522 * Decide on what work routines to call based on the inode size.
523 */
524 if (!xfs_inode_hasattr(dp)) {
525 error = 0;
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);
530 } else {
531 error = xfs_attr_node_list(context);
532 }
533
534 xfs_iunlock(dp, XFS_ILOCK_SHARED);
535
536 return error;
537 }
538
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))
544
545 /*
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.
549 */
550 STATIC int
551 xfs_attr_put_listent(
552 xfs_attr_list_context_t *context,
553 int flags,
554 unsigned char *name,
555 int namelen,
556 int valuelen,
557 unsigned char *value)
558 {
559 struct attrlist *alist = (struct attrlist *)context->alist;
560 attrlist_ent_t *aep;
561 int arraytop;
562
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);
568
569 /*
570 * Only list entries in the right namespace.
571 */
572 if (((context->flags & ATTR_SECURE) == 0) !=
573 ((flags & XFS_ATTR_SECURE) == 0))
574 return 0;
575 if (((context->flags & ATTR_ROOT) == 0) !=
576 ((flags & XFS_ATTR_ROOT) == 0))
577 return 0;
578
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);
584 alist->al_more = 1;
585 context->seen_enough = 1;
586 return 1;
587 }
588
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);
596 return 0;
597 }
598
599 /*
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
604 * success.
605 */
606 int
607 xfs_attr_list(
608 xfs_inode_t *dp,
609 char *buffer,
610 int bufsize,
611 int flags,
612 attrlist_cursor_kern_t *cursor)
613 {
614 xfs_attr_list_context_t context;
615 struct attrlist *alist;
616 int error;
617
618 /*
619 * Validate the cursor.
620 */
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);
626
627 /*
628 * Check for a properly aligned buffer.
629 */
630 if (((long)buffer) & (sizeof(int)-1))
631 return XFS_ERROR(EFAULT);
632 if (flags & ATTR_KERNOVAL)
633 bufsize = 0;
634
635 /*
636 * Initialize the output buffer.
637 */
638 memset(&context, 0, sizeof(context));
639 context.dp = dp;
640 context.cursor = cursor;
641 context.resynch = 1;
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;
647
648 alist = (struct attrlist *)context.alist;
649 alist->al_count = 0;
650 alist->al_more = 0;
651 alist->al_offset[0] = context.bufsize;
652
653 error = xfs_attr_list_int(&context);
654 ASSERT(error >= 0);
655 return error;
656 }
This page took 0.045514 seconds and 5 git commands to generate.