7ffee91a9fdb31d89286359911606eaf418a758f
[linux-2.6-microblaze.git] / fs / xfs / xfs_attr_list.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4  * Copyright (c) 2013 Red Hat, Inc.
5  * All Rights Reserved.
6  */
7 #include "xfs.h"
8 #include "xfs_fs.h"
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_bit.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"
20 #include "xfs_bmap.h"
21 #include "xfs_attr.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"
29 #include "xfs_dir2.h"
30
31 STATIC int
32 xfs_attr_shortform_compare(const void *a, const void *b)
33 {
34         xfs_attr_sf_sort_t *sa, *sb;
35
36         sa = (xfs_attr_sf_sort_t *)a;
37         sb = (xfs_attr_sf_sort_t *)b;
38         if (sa->hash < sb->hash) {
39                 return -1;
40         } else if (sa->hash > sb->hash) {
41                 return 1;
42         } else {
43                 return sa->entno - sb->entno;
44         }
45 }
46
47 #define XFS_ISRESET_CURSOR(cursor) \
48         (!((cursor)->initted) && !((cursor)->hashval) && \
49          !((cursor)->blkno) && !((cursor)->offset))
50 /*
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.
56  */
57 static int
58 xfs_attr_shortform_list(xfs_attr_list_context_t *context)
59 {
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;
64         xfs_inode_t *dp;
65         int sbsize, nsbuf, count, i;
66
67         ASSERT(context != NULL);
68         dp = context->dp;
69         ASSERT(dp != NULL);
70         ASSERT(dp->i_afp != NULL);
71         sf = (xfs_attr_shortform_t *)dp->i_afp->if_u1.if_data;
72         ASSERT(sf != NULL);
73         if (!sf->hdr.count)
74                 return 0;
75         cursor = context->cursor;
76         ASSERT(cursor != NULL);
77
78         trace_xfs_attr_list_sf(context);
79
80         /*
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
84          * made.
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.
88          */
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,
94                                              sfe->flags,
95                                              sfe->nameval,
96                                              (int)sfe->namelen,
97                                              (int)sfe->valuelen);
98                         /*
99                          * Either search callback finished early or
100                          * didn't fit it all in the buffer after all.
101                          */
102                         if (context->seen_enough)
103                                 break;
104                         sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
105                 }
106                 trace_xfs_attr_list_sf_all(context);
107                 return 0;
108         }
109
110         /* do no more for a search callback */
111         if (context->bufsize == 0)
112                 return 0;
113
114         /*
115          * It didn't all fit, so we have to sort everything on hashval.
116          */
117         sbsize = sf->hdr.count * sizeof(*sbuf);
118         sbp = sbuf = kmem_alloc(sbsize, KM_SLEEP | KM_NOFS);
119
120         /*
121          * Scan the attribute list for the rest of the entries, storing
122          * the relevant info from only those that match into a buffer.
123          */
124         nsbuf = 0;
125         for (i = 0, sfe = &sf->list[0]; i < sf->hdr.count; i++) {
126                 if (unlikely(
127                     ((char *)sfe < (char *)sf) ||
128                     ((char *)sfe >= ((char *)sf + dp->i_afp->if_bytes)))) {
129                         XFS_CORRUPTION_ERROR("xfs_attr_shortform_list",
130                                              XFS_ERRLEVEL_LOW,
131                                              context->dp->i_mount, sfe,
132                                              sizeof(*sfe));
133                         kmem_free(sbuf);
134                         return -EFSCORRUPTED;
135                 }
136
137                 sbp->entno = i;
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);
145                 sbp++;
146                 nsbuf++;
147         }
148
149         /*
150          * Sort the entries on hash then entno.
151          */
152         xfs_sort(sbuf, nsbuf, sizeof(*sbuf), xfs_attr_shortform_compare);
153
154         /*
155          * Re-find our place IN THE SORTED LIST.
156          */
157         count = 0;
158         cursor->initted = 1;
159         cursor->blkno = 0;
160         for (sbp = sbuf, i = 0; i < nsbuf; i++, sbp++) {
161                 if (sbp->hash == cursor->hashval) {
162                         if (cursor->offset == count) {
163                                 break;
164                         }
165                         count++;
166                 } else if (sbp->hash > cursor->hashval) {
167                         break;
168                 }
169         }
170         if (i == nsbuf) {
171                 kmem_free(sbuf);
172                 return 0;
173         }
174
175         /*
176          * Loop putting entries into the user buffer.
177          */
178         for ( ; i < nsbuf; i++, sbp++) {
179                 if (cursor->hashval != sbp->hash) {
180                         cursor->hashval = sbp->hash;
181                         cursor->offset = 0;
182                 }
183                 context->put_listent(context,
184                                      sbp->flags,
185                                      sbp->name,
186                                      sbp->namelen,
187                                      sbp->valuelen);
188                 if (context->seen_enough)
189                         break;
190                 cursor->offset++;
191         }
192
193         kmem_free(sbuf);
194         return 0;
195 }
196
197 /*
198  * We didn't find the block & hash mentioned in the cursor state, so
199  * walk down the attr btree looking for the hash.
200  */
201 STATIC int
202 xfs_attr_node_list_lookup(
203         struct xfs_attr_list_context    *context,
204         struct attrlist_cursor_kern     *cursor,
205         struct xfs_buf                  **pbp)
206 {
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;
213         struct xfs_buf                  *bp;
214         int                             i;
215         int                             error = 0;
216         unsigned int                    expected_level = 0;
217         uint16_t                        magic;
218
219         ASSERT(*pbp == NULL);
220         cursor->blkno = 0;
221         for (;;) {
222                 error = xfs_da3_node_read(tp, dp, cursor->blkno, -1, &bp,
223                                 XFS_ATTR_FORK);
224                 if (error)
225                         return error;
226                 node = bp->b_addr;
227                 magic = be16_to_cpu(node->hdr.info.magic);
228                 if (magic == XFS_ATTR_LEAF_MAGIC ||
229                     magic == XFS_ATTR3_LEAF_MAGIC)
230                         break;
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));
235                         goto out_corruptbuf;
236                 }
237
238                 dp->d_ops->node_hdr_from_disk(&nodehdr, node);
239
240                 /* Tree taller than we can handle; bail out! */
241                 if (nodehdr.level >= XFS_DA_NODE_MAXDEPTH)
242                         goto out_corruptbuf;
243
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)
248                         goto out_corruptbuf;
249                 else
250                         expected_level--;
251
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,
257                                                 btree);
258                                 break;
259                         }
260                 }
261                 xfs_trans_brelse(tp, bp);
262
263                 if (i == nodehdr.count)
264                         return 0;
265
266                 /* We can't point back to the root. */
267                 if (cursor->blkno == 0)
268                         return -EFSCORRUPTED;
269         }
270
271         if (expected_level != 0)
272                 goto out_corruptbuf;
273
274         *pbp = bp;
275         return 0;
276
277 out_corruptbuf:
278         xfs_trans_brelse(tp, bp);
279         return -EFSCORRUPTED;
280 }
281
282 STATIC int
283 xfs_attr_node_list(
284         struct xfs_attr_list_context    *context)
285 {
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;
290         struct xfs_buf                  *bp;
291         struct xfs_inode                *dp = context->dp;
292         struct xfs_mount                *mp = dp->i_mount;
293         int                             error;
294
295         trace_xfs_attr_node_list(context);
296
297         cursor = context->cursor;
298         cursor->initted = 1;
299
300         /*
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.
304          */
305         bp = NULL;
306         if (cursor->blkno > 0) {
307                 error = xfs_da3_node_read(context->tp, dp, cursor->blkno, -1,
308                                               &bp, XFS_ATTR_FORK);
309                 if ((error != 0) && (error != -EFSCORRUPTED))
310                         return error;
311                 if (bp) {
312                         struct xfs_attr_leaf_entry *entries;
313
314                         node = bp->b_addr;
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);
320                                 bp = NULL;
321                                 break;
322                         case XFS_ATTR_LEAF_MAGIC:
323                         case XFS_ATTR3_LEAF_MAGIC:
324                                 leaf = bp->b_addr;
325                                 xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo,
326                                                              &leafhdr, leaf);
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);
332                                         bp = NULL;
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);
337                                         bp = NULL;
338                                 }
339                                 break;
340                         default:
341                                 trace_xfs_attr_list_wrong_blk(context);
342                                 xfs_trans_brelse(context->tp, bp);
343                                 bp = NULL;
344                         }
345                 }
346         }
347
348         /*
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.
352          */
353         if (bp == NULL) {
354                 error = xfs_attr_node_list_lookup(context, cursor, &bp);
355                 if (error || !bp)
356                         return error;
357         }
358         ASSERT(bp != NULL);
359
360         /*
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.
364          */
365         for (;;) {
366                 leaf = bp->b_addr;
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)
370                         break;
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);
374                 if (error)
375                         return error;
376         }
377         xfs_trans_brelse(context->tp, bp);
378         return 0;
379 }
380
381 /*
382  * Copy out attribute list entries for attr_list(), for leaf attribute lists.
383  */
384 void
385 xfs_attr3_leaf_list_int(
386         struct xfs_buf                  *bp,
387         struct xfs_attr_list_context    *context)
388 {
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;
394         int                             i;
395         struct xfs_mount                *mp = context->dp->i_mount;
396
397         trace_xfs_attr_list_leaf(context);
398
399         leaf = bp->b_addr;
400         xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr, leaf);
401         entries = xfs_attr3_leaf_entryp(leaf);
402
403         cursor = context->cursor;
404         cursor->initted = 1;
405
406         /*
407          * Re-find our place in the leaf block if this is a new syscall.
408          */
409         if (context->resynch) {
410                 entry = &entries[0];
411                 for (i = 0; i < ichdr.count; entry++, i++) {
412                         if (be32_to_cpu(entry->hashval) == cursor->hashval) {
413                                 if (cursor->offset == context->dupcnt) {
414                                         context->dupcnt = 0;
415                                         break;
416                                 }
417                                 context->dupcnt++;
418                         } else if (be32_to_cpu(entry->hashval) >
419                                         cursor->hashval) {
420                                 context->dupcnt = 0;
421                                 break;
422                         }
423                 }
424                 if (i == ichdr.count) {
425                         trace_xfs_attr_list_notfound(context);
426                         return;
427                 }
428         } else {
429                 entry = &entries[0];
430                 i = 0;
431         }
432         context->resynch = 0;
433
434         /*
435          * We have found our place, start copying out the new attributes.
436          */
437         for (; i < ichdr.count; entry++, i++) {
438                 char *name;
439                 int namelen, valuelen;
440
441                 if (be32_to_cpu(entry->hashval) != cursor->hashval) {
442                         cursor->hashval = be32_to_cpu(entry->hashval);
443                         cursor->offset = 0;
444                 }
445
446                 if ((entry->flags & XFS_ATTR_INCOMPLETE) &&
447                     !(context->flags & ATTR_INCOMPLETE))
448                         continue;               /* skip incomplete entries */
449
450                 if (entry->flags & XFS_ATTR_LOCAL) {
451                         xfs_attr_leaf_name_local_t *name_loc;
452
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);
457                 } else {
458                         xfs_attr_leaf_name_remote_t *name_rmt;
459
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);
464                 }
465
466                 context->put_listent(context, entry->flags,
467                                               name, namelen, valuelen);
468                 if (context->seen_enough)
469                         break;
470                 cursor->offset++;
471         }
472         trace_xfs_attr_list_leaf_end(context);
473         return;
474 }
475
476 /*
477  * Copy out attribute entries for attr_list(), for leaf attribute lists.
478  */
479 STATIC int
480 xfs_attr_leaf_list(xfs_attr_list_context_t *context)
481 {
482         int error;
483         struct xfs_buf *bp;
484
485         trace_xfs_attr_leaf_list(context);
486
487         context->cursor->blkno = 0;
488         error = xfs_attr3_leaf_read(context->tp, context->dp, 0, -1, &bp);
489         if (error)
490                 return error;
491
492         xfs_attr3_leaf_list_int(bp, context);
493         xfs_trans_brelse(context->tp, bp);
494         return 0;
495 }
496
497 int
498 xfs_attr_list_int_ilocked(
499         struct xfs_attr_list_context    *context)
500 {
501         struct xfs_inode                *dp = context->dp;
502
503         ASSERT(xfs_isilocked(dp, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
504
505         /*
506          * Decide on what work routines to call based on the inode size.
507          */
508         if (!xfs_inode_hasattr(dp))
509                 return 0;
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);
515 }
516
517 int
518 xfs_attr_list_int(
519         xfs_attr_list_context_t *context)
520 {
521         int error;
522         xfs_inode_t *dp = context->dp;
523         uint            lock_mode;
524
525         XFS_STATS_INC(dp->i_mount, xs_attr_list);
526
527         if (XFS_FORCED_SHUTDOWN(dp->i_mount))
528                 return -EIO;
529
530         lock_mode = xfs_ilock_attr_map_shared(dp);
531         error = xfs_attr_list_int_ilocked(context);
532         xfs_iunlock(dp, lock_mode);
533         return error;
534 }
535
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))
541
542 /*
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.
546  */
547 STATIC void
548 xfs_attr_put_listent(
549         xfs_attr_list_context_t *context,
550         int             flags,
551         unsigned char   *name,
552         int             namelen,
553         int             valuelen)
554 {
555         struct attrlist *alist = (struct attrlist *)context->alist;
556         attrlist_ent_t *aep;
557         int arraytop;
558
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);
565
566         /*
567          * Only list entries in the right namespace.
568          */
569         if (((context->flags & ATTR_SECURE) == 0) !=
570             ((flags & XFS_ATTR_SECURE) == 0))
571                 return;
572         if (((context->flags & ATTR_ROOT) == 0) !=
573             ((flags & XFS_ATTR_ROOT) == 0))
574                 return;
575
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);
581                 alist->al_more = 1;
582                 context->seen_enough = 1;
583                 return;
584         }
585
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);
593         return;
594 }
595
596 /*
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
601  * success.
602  */
603 int
604 xfs_attr_list(
605         xfs_inode_t     *dp,
606         char            *buffer,
607         int             bufsize,
608         int             flags,
609         attrlist_cursor_kern_t *cursor)
610 {
611         xfs_attr_list_context_t context;
612         struct attrlist *alist;
613         int error;
614
615         /*
616          * Validate the cursor.
617          */
618         if (cursor->pad1 || cursor->pad2)
619                 return -EINVAL;
620         if ((cursor->initted == 0) &&
621             (cursor->hashval || cursor->blkno || cursor->offset))
622                 return -EINVAL;
623
624         /* Only internal consumers can retrieve incomplete attrs. */
625         if (flags & ATTR_INCOMPLETE)
626                 return -EINVAL;
627
628         /*
629          * Check for a properly aligned buffer.
630          */
631         if (((long)buffer) & (sizeof(int)-1))
632                 return -EFAULT;
633         if (flags & ATTR_KERNOVAL)
634                 bufsize = 0;
635
636         /*
637          * Initialize the output buffer.
638          */
639         memset(&context, 0, sizeof(context));
640         context.dp = dp;
641         context.cursor = cursor;
642         context.resynch = 1;
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;
648
649         alist = (struct attrlist *)context.alist;
650         alist->al_count = 0;
651         alist->al_more = 0;
652         alist->al_offset[0] = context.bufsize;
653
654         error = xfs_attr_list_int(&context);
655         ASSERT(error <= 0);
656         return error;
657 }