xfs: rework deferred attribute operation setup
[linux-2.6-microblaze.git] / fs / xfs / libxfs / xfs_attr.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4  * All Rights Reserved.
5  */
6 #include "xfs.h"
7 #include "xfs_fs.h"
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
12 #include "xfs_mount.h"
13 #include "xfs_defer.h"
14 #include "xfs_da_format.h"
15 #include "xfs_da_btree.h"
16 #include "xfs_attr_sf.h"
17 #include "xfs_inode.h"
18 #include "xfs_trans.h"
19 #include "xfs_bmap.h"
20 #include "xfs_bmap_btree.h"
21 #include "xfs_attr.h"
22 #include "xfs_attr_leaf.h"
23 #include "xfs_attr_remote.h"
24 #include "xfs_quota.h"
25 #include "xfs_trans_space.h"
26 #include "xfs_trace.h"
27 #include "xfs_attr_item.h"
28 #include "xfs_log.h"
29
30 struct kmem_cache               *xfs_attri_cache;
31 struct kmem_cache               *xfs_attrd_cache;
32
33 /*
34  * xfs_attr.c
35  *
36  * Provide the external interfaces to manage attribute lists.
37  */
38
39 /*========================================================================
40  * Function prototypes for the kernel.
41  *========================================================================*/
42
43 /*
44  * Internal routines when attribute list fits inside the inode.
45  */
46 STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
47
48 /*
49  * Internal routines when attribute list is one block.
50  */
51 STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
52 STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
53 STATIC int xfs_attr_leaf_hasname(struct xfs_da_args *args, struct xfs_buf **bp);
54 STATIC int xfs_attr_leaf_try_add(struct xfs_da_args *args, struct xfs_buf *bp);
55
56 /*
57  * Internal routines when attribute list is more than one block.
58  */
59 STATIC int xfs_attr_node_get(xfs_da_args_t *args);
60 STATIC void xfs_attr_restore_rmt_blk(struct xfs_da_args *args);
61 STATIC int xfs_attr_node_addname(struct xfs_attr_item *attr);
62 STATIC int xfs_attr_node_addname_find_attr(struct xfs_attr_item *attr);
63 STATIC int xfs_attr_node_addname_clear_incomplete(struct xfs_attr_item *attr);
64 STATIC int xfs_attr_node_hasname(xfs_da_args_t *args,
65                                  struct xfs_da_state **state);
66 STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
67 STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
68 STATIC int xfs_attr_node_removename(struct xfs_da_args *args,
69                                     struct xfs_da_state *state);
70
71 int
72 xfs_inode_hasattr(
73         struct xfs_inode        *ip)
74 {
75         if (!XFS_IFORK_Q(ip) ||
76             (ip->i_afp->if_format == XFS_DINODE_FMT_EXTENTS &&
77              ip->i_afp->if_nextents == 0))
78                 return 0;
79         return 1;
80 }
81
82 /*
83  * Returns true if the there is exactly only block in the attr fork, in which
84  * case the attribute fork consists of a single leaf block entry.
85  */
86 bool
87 xfs_attr_is_leaf(
88         struct xfs_inode        *ip)
89 {
90         struct xfs_ifork        *ifp = ip->i_afp;
91         struct xfs_iext_cursor  icur;
92         struct xfs_bmbt_irec    imap;
93
94         if (ifp->if_nextents != 1 || ifp->if_format != XFS_DINODE_FMT_EXTENTS)
95                 return false;
96
97         xfs_iext_first(ifp, &icur);
98         xfs_iext_get_extent(ifp, &icur, &imap);
99         return imap.br_startoff == 0 && imap.br_blockcount == 1;
100 }
101
102 /*========================================================================
103  * Overall external interface routines.
104  *========================================================================*/
105
106 /*
107  * Retrieve an extended attribute and its value.  Must have ilock.
108  * Returns 0 on successful retrieval, otherwise an error.
109  */
110 int
111 xfs_attr_get_ilocked(
112         struct xfs_da_args      *args)
113 {
114         ASSERT(xfs_isilocked(args->dp, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
115
116         if (!xfs_inode_hasattr(args->dp))
117                 return -ENOATTR;
118
119         if (args->dp->i_afp->if_format == XFS_DINODE_FMT_LOCAL)
120                 return xfs_attr_shortform_getvalue(args);
121         if (xfs_attr_is_leaf(args->dp))
122                 return xfs_attr_leaf_get(args);
123         return xfs_attr_node_get(args);
124 }
125
126 /*
127  * Retrieve an extended attribute by name, and its value if requested.
128  *
129  * If args->valuelen is zero, then the caller does not want the value, just an
130  * indication whether the attribute exists and the size of the value if it
131  * exists. The size is returned in args.valuelen.
132  *
133  * If args->value is NULL but args->valuelen is non-zero, allocate the buffer
134  * for the value after existence of the attribute has been determined. The
135  * caller always has to free args->value if it is set, no matter if this
136  * function was successful or not.
137  *
138  * If the attribute is found, but exceeds the size limit set by the caller in
139  * args->valuelen, return -ERANGE with the size of the attribute that was found
140  * in args->valuelen.
141  */
142 int
143 xfs_attr_get(
144         struct xfs_da_args      *args)
145 {
146         uint                    lock_mode;
147         int                     error;
148
149         XFS_STATS_INC(args->dp->i_mount, xs_attr_get);
150
151         if (xfs_is_shutdown(args->dp->i_mount))
152                 return -EIO;
153
154         args->geo = args->dp->i_mount->m_attr_geo;
155         args->whichfork = XFS_ATTR_FORK;
156         args->hashval = xfs_da_hashname(args->name, args->namelen);
157
158         /* Entirely possible to look up a name which doesn't exist */
159         args->op_flags = XFS_DA_OP_OKNOENT;
160
161         lock_mode = xfs_ilock_attr_map_shared(args->dp);
162         error = xfs_attr_get_ilocked(args);
163         xfs_iunlock(args->dp, lock_mode);
164
165         return error;
166 }
167
168 /*
169  * Calculate how many blocks we need for the new attribute,
170  */
171 int
172 xfs_attr_calc_size(
173         struct xfs_da_args      *args,
174         int                     *local)
175 {
176         struct xfs_mount        *mp = args->dp->i_mount;
177         int                     size;
178         int                     nblks;
179
180         /*
181          * Determine space new attribute will use, and if it would be
182          * "local" or "remote" (note: local != inline).
183          */
184         size = xfs_attr_leaf_newentsize(args, local);
185         nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
186         if (*local) {
187                 if (size > (args->geo->blksize / 2)) {
188                         /* Double split possible */
189                         nblks *= 2;
190                 }
191         } else {
192                 /*
193                  * Out of line attribute, cannot double split, but
194                  * make room for the attribute value itself.
195                  */
196                 uint    dblocks = xfs_attr3_rmt_blocks(mp, args->valuelen);
197                 nblks += dblocks;
198                 nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
199         }
200
201         return nblks;
202 }
203
204 /* Initialize transaction reservation for attr operations */
205 void
206 xfs_init_attr_trans(
207         struct xfs_da_args      *args,
208         struct xfs_trans_res    *tres,
209         unsigned int            *total)
210 {
211         struct xfs_mount        *mp = args->dp->i_mount;
212
213         if (args->value) {
214                 tres->tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
215                                  M_RES(mp)->tr_attrsetrt.tr_logres *
216                                  args->total;
217                 tres->tr_logcount = XFS_ATTRSET_LOG_COUNT;
218                 tres->tr_logflags = XFS_TRANS_PERM_LOG_RES;
219                 *total = args->total;
220         } else {
221                 *tres = M_RES(mp)->tr_attrrm;
222                 *total = XFS_ATTRRM_SPACE_RES(mp);
223         }
224 }
225
226 STATIC int
227 xfs_attr_try_sf_addname(
228         struct xfs_inode        *dp,
229         struct xfs_da_args      *args)
230 {
231
232         int                     error;
233
234         /*
235          * Build initial attribute list (if required).
236          */
237         if (dp->i_afp->if_format == XFS_DINODE_FMT_EXTENTS)
238                 xfs_attr_shortform_create(args);
239
240         error = xfs_attr_shortform_addname(args);
241         if (error == -ENOSPC)
242                 return error;
243
244         /*
245          * Commit the shortform mods, and we're done.
246          * NOTE: this is also the error path (EEXIST, etc).
247          */
248         if (!error && !(args->op_flags & XFS_DA_OP_NOTIME))
249                 xfs_trans_ichgtime(args->trans, dp, XFS_ICHGTIME_CHG);
250
251         if (xfs_has_wsync(dp->i_mount))
252                 xfs_trans_set_sync(args->trans);
253
254         return error;
255 }
256
257 /*
258  * Check to see if the attr should be upgraded from non-existent or shortform to
259  * single-leaf-block attribute list.
260  */
261 static inline bool
262 xfs_attr_is_shortform(
263         struct xfs_inode    *ip)
264 {
265         return ip->i_afp->if_format == XFS_DINODE_FMT_LOCAL ||
266                (ip->i_afp->if_format == XFS_DINODE_FMT_EXTENTS &&
267                 ip->i_afp->if_nextents == 0);
268 }
269
270 STATIC int
271 xfs_attr_sf_addname(
272         struct xfs_attr_item            *attr)
273 {
274         struct xfs_da_args              *args = attr->xattri_da_args;
275         struct xfs_inode                *dp = args->dp;
276         int                             error = 0;
277
278         /*
279          * Try to add the attr to the attribute list in the inode.
280          */
281         error = xfs_attr_try_sf_addname(dp, args);
282
283         /* Should only be 0, -EEXIST or -ENOSPC */
284         if (error != -ENOSPC)
285                 return error;
286
287         /*
288          * It won't fit in the shortform, transform to a leaf block.  GROT:
289          * another possible req'mt for a double-split btree op.
290          */
291         error = xfs_attr_shortform_to_leaf(args, &attr->xattri_leaf_bp);
292         if (error)
293                 return error;
294
295         /*
296          * Prevent the leaf buffer from being unlocked so that a concurrent AIL
297          * push cannot grab the half-baked leaf buffer and run into problems
298          * with the write verifier.
299          */
300         xfs_trans_bhold(args->trans, attr->xattri_leaf_bp);
301
302         /*
303          * We're still in XFS_DAS_UNINIT state here.  We've converted
304          * the attr fork to leaf format and will restart with the leaf
305          * add.
306          */
307         trace_xfs_attr_sf_addname_return(XFS_DAS_UNINIT, args->dp);
308         return -EAGAIN;
309 }
310
311 STATIC int
312 xfs_attr_leaf_addname(
313         struct xfs_attr_item    *attr)
314 {
315         struct xfs_da_args      *args = attr->xattri_da_args;
316         struct xfs_inode        *dp = args->dp;
317         enum xfs_delattr_state  next_state = XFS_DAS_UNINIT;
318         int                     error;
319
320         if (xfs_attr_is_leaf(dp)) {
321
322                 /*
323                  * Use the leaf buffer we may already hold locked as a result of
324                  * a sf-to-leaf conversion. The held buffer is no longer valid
325                  * after this call, regardless of the result.
326                  */
327                 error = xfs_attr_leaf_try_add(args, attr->xattri_leaf_bp);
328                 attr->xattri_leaf_bp = NULL;
329
330                 if (error == -ENOSPC) {
331                         error = xfs_attr3_leaf_to_node(args);
332                         if (error)
333                                 return error;
334
335                         /*
336                          * Finish any deferred work items and roll the
337                          * transaction once more.  The goal here is to call
338                          * node_addname with the inode and transaction in the
339                          * same state (inode locked and joined, transaction
340                          * clean) no matter how we got to this step.
341                          *
342                          * At this point, we are still in XFS_DAS_UNINIT, but
343                          * when we come back, we'll be a node, so we'll fall
344                          * down into the node handling code below
345                          */
346                         error = -EAGAIN;
347                         goto out;
348                 }
349                 next_state = XFS_DAS_FOUND_LBLK;
350         } else {
351                 ASSERT(!attr->xattri_leaf_bp);
352
353                 error = xfs_attr_node_addname_find_attr(attr);
354                 if (error)
355                         return error;
356
357                 next_state = XFS_DAS_FOUND_NBLK;
358                 error = xfs_attr_node_addname(attr);
359         }
360         if (error)
361                 return error;
362
363         /*
364          * We need to commit and roll if we need to allocate remote xattr blocks
365          * or perform more xattr manipulations. Otherwise there is nothing more
366          * to do and we can return success.
367          */
368         if (args->rmtblkno ||
369             (args->op_flags & XFS_DA_OP_RENAME)) {
370                 attr->xattri_dela_state = next_state;
371                 error = -EAGAIN;
372         }
373
374 out:
375         trace_xfs_attr_leaf_addname_return(attr->xattri_dela_state, args->dp);
376         return error;
377 }
378
379 /*
380  * Set the attribute specified in @args.
381  * This routine is meant to function as a delayed operation, and may return
382  * -EAGAIN when the transaction needs to be rolled.  Calling functions will need
383  * to handle this, and recall the function until a successful error code is
384  * returned.
385  */
386 int
387 xfs_attr_set_iter(
388         struct xfs_attr_item            *attr)
389 {
390         struct xfs_da_args              *args = attr->xattri_da_args;
391         struct xfs_inode                *dp = args->dp;
392         struct xfs_buf                  *bp = NULL;
393         int                             forkoff, error = 0;
394         struct xfs_mount                *mp = args->dp->i_mount;
395
396         /* State machine switch */
397         switch (attr->xattri_dela_state) {
398         case XFS_DAS_UNINIT:
399                 /*
400                  * If the fork is shortform, attempt to add the attr. If there
401                  * is no space, this converts to leaf format and returns
402                  * -EAGAIN with the leaf buffer held across the roll. The caller
403                  * will deal with a transaction roll error, but otherwise
404                  * release the hold once we return with a clean transaction.
405                  */
406                 if (xfs_attr_is_shortform(dp))
407                         return xfs_attr_sf_addname(attr);
408                 return xfs_attr_leaf_addname(attr);
409
410         case XFS_DAS_FOUND_LBLK:
411                 /*
412                  * If there was an out-of-line value, allocate the blocks we
413                  * identified for its storage and copy the value.  This is done
414                  * after we create the attribute so that we don't overflow the
415                  * maximum size of a transaction and/or hit a deadlock.
416                  */
417
418                 /* Open coded xfs_attr_rmtval_set without trans handling */
419                 if ((attr->xattri_flags & XFS_DAC_LEAF_ADDNAME_INIT) == 0) {
420                         attr->xattri_flags |= XFS_DAC_LEAF_ADDNAME_INIT;
421                         if (args->rmtblkno > 0) {
422                                 error = xfs_attr_rmtval_find_space(attr);
423                                 if (error)
424                                         return error;
425                         }
426                 }
427
428                 /*
429                  * Repeat allocating remote blocks for the attr value until
430                  * blkcnt drops to zero.
431                  */
432                 if (attr->xattri_blkcnt > 0) {
433                         error = xfs_attr_rmtval_set_blk(attr);
434                         if (error)
435                                 return error;
436                         trace_xfs_attr_set_iter_return(attr->xattri_dela_state,
437                                                        args->dp);
438                         return -EAGAIN;
439                 }
440
441                 error = xfs_attr_rmtval_set_value(args);
442                 if (error)
443                         return error;
444
445                 /*
446                  * If this is not a rename, clear the incomplete flag and we're
447                  * done.
448                  */
449                 if (!(args->op_flags & XFS_DA_OP_RENAME)) {
450                         if (args->rmtblkno > 0)
451                                 error = xfs_attr3_leaf_clearflag(args);
452                         return error;
453                 }
454
455                 /*
456                  * If this is an atomic rename operation, we must "flip" the
457                  * incomplete flags on the "new" and "old" attribute/value pairs
458                  * so that one disappears and one appears atomically.  Then we
459                  * must remove the "old" attribute/value pair.
460                  *
461                  * In a separate transaction, set the incomplete flag on the
462                  * "old" attr and clear the incomplete flag on the "new" attr.
463                  */
464                 if (!xfs_has_larp(mp)) {
465                         error = xfs_attr3_leaf_flipflags(args);
466                         if (error)
467                                 return error;
468                         /*
469                          * Commit the flag value change and start the next trans
470                          * in series.
471                          */
472                         attr->xattri_dela_state = XFS_DAS_FLIP_LFLAG;
473                         trace_xfs_attr_set_iter_return(attr->xattri_dela_state,
474                                                        args->dp);
475                         return -EAGAIN;
476                 }
477
478                 fallthrough;
479         case XFS_DAS_FLIP_LFLAG:
480                 /*
481                  * Dismantle the "old" attribute/value pair by removing a
482                  * "remote" value (if it exists).
483                  */
484                 xfs_attr_restore_rmt_blk(args);
485                 error = xfs_attr_rmtval_invalidate(args);
486                 if (error)
487                         return error;
488
489                 fallthrough;
490         case XFS_DAS_RM_LBLK:
491                 /* Set state in case xfs_attr_rmtval_remove returns -EAGAIN */
492                 attr->xattri_dela_state = XFS_DAS_RM_LBLK;
493                 if (args->rmtblkno) {
494                         error = xfs_attr_rmtval_remove(attr);
495                         if (error == -EAGAIN)
496                                 trace_xfs_attr_set_iter_return(
497                                         attr->xattri_dela_state, args->dp);
498                         if (error)
499                                 return error;
500
501                         attr->xattri_dela_state = XFS_DAS_RD_LEAF;
502                         trace_xfs_attr_set_iter_return(attr->xattri_dela_state,
503                                                        args->dp);
504                         return -EAGAIN;
505                 }
506
507                 fallthrough;
508         case XFS_DAS_RD_LEAF:
509                 /*
510                  * This is the last step for leaf format. Read the block with
511                  * the old attr, remove the old attr, check for shortform
512                  * conversion and return.
513                  */
514                 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
515                                            &bp);
516                 if (error)
517                         return error;
518
519                 xfs_attr3_leaf_remove(bp, args);
520
521                 forkoff = xfs_attr_shortform_allfit(bp, dp);
522                 if (forkoff)
523                         error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
524                         /* bp is gone due to xfs_da_shrink_inode */
525
526                 return error;
527
528         case XFS_DAS_FOUND_NBLK:
529                 /*
530                  * Find space for remote blocks and fall into the allocation
531                  * state.
532                  */
533                 if (args->rmtblkno > 0) {
534                         error = xfs_attr_rmtval_find_space(attr);
535                         if (error)
536                                 return error;
537                 }
538
539                 fallthrough;
540         case XFS_DAS_ALLOC_NODE:
541                 /*
542                  * If there was an out-of-line value, allocate the blocks we
543                  * identified for its storage and copy the value.  This is done
544                  * after we create the attribute so that we don't overflow the
545                  * maximum size of a transaction and/or hit a deadlock.
546                  */
547                 attr->xattri_dela_state = XFS_DAS_ALLOC_NODE;
548                 if (args->rmtblkno > 0) {
549                         if (attr->xattri_blkcnt > 0) {
550                                 error = xfs_attr_rmtval_set_blk(attr);
551                                 if (error)
552                                         return error;
553                                 trace_xfs_attr_set_iter_return(
554                                         attr->xattri_dela_state, args->dp);
555                                 return -EAGAIN;
556                         }
557
558                         error = xfs_attr_rmtval_set_value(args);
559                         if (error)
560                                 return error;
561                 }
562
563                 /*
564                  * If this was not a rename, clear the incomplete flag and we're
565                  * done.
566                  */
567                 if (!(args->op_flags & XFS_DA_OP_RENAME)) {
568                         if (args->rmtblkno > 0)
569                                 error = xfs_attr3_leaf_clearflag(args);
570                         goto out;
571                 }
572
573                 /*
574                  * If this is an atomic rename operation, we must "flip" the
575                  * incomplete flags on the "new" and "old" attribute/value pairs
576                  * so that one disappears and one appears atomically.  Then we
577                  * must remove the "old" attribute/value pair.
578                  *
579                  * In a separate transaction, set the incomplete flag on the
580                  * "old" attr and clear the incomplete flag on the "new" attr.
581                  */
582                 if (!xfs_has_larp(mp)) {
583                         error = xfs_attr3_leaf_flipflags(args);
584                         if (error)
585                                 goto out;
586                         /*
587                          * Commit the flag value change and start the next trans
588                          * in series
589                          */
590                         attr->xattri_dela_state = XFS_DAS_FLIP_NFLAG;
591                         trace_xfs_attr_set_iter_return(attr->xattri_dela_state,
592                                                        args->dp);
593                         return -EAGAIN;
594                 }
595
596                 fallthrough;
597         case XFS_DAS_FLIP_NFLAG:
598                 /*
599                  * Dismantle the "old" attribute/value pair by removing a
600                  * "remote" value (if it exists).
601                  */
602                 xfs_attr_restore_rmt_blk(args);
603
604                 error = xfs_attr_rmtval_invalidate(args);
605                 if (error)
606                         return error;
607
608                 fallthrough;
609         case XFS_DAS_RM_NBLK:
610                 /* Set state in case xfs_attr_rmtval_remove returns -EAGAIN */
611                 attr->xattri_dela_state = XFS_DAS_RM_NBLK;
612                 if (args->rmtblkno) {
613                         error = xfs_attr_rmtval_remove(attr);
614                         if (error == -EAGAIN)
615                                 trace_xfs_attr_set_iter_return(
616                                         attr->xattri_dela_state, args->dp);
617
618                         if (error)
619                                 return error;
620
621                         attr->xattri_dela_state = XFS_DAS_CLR_FLAG;
622                         trace_xfs_attr_set_iter_return(attr->xattri_dela_state,
623                                                        args->dp);
624                         return -EAGAIN;
625                 }
626
627                 fallthrough;
628         case XFS_DAS_CLR_FLAG:
629                 /*
630                  * The last state for node format. Look up the old attr and
631                  * remove it.
632                  */
633                 error = xfs_attr_node_addname_clear_incomplete(attr);
634                 break;
635         default:
636                 ASSERT(0);
637                 break;
638         }
639 out:
640         return error;
641 }
642
643
644 /*
645  * Return EEXIST if attr is found, or ENOATTR if not
646  */
647 static int
648 xfs_attr_lookup(
649         struct xfs_da_args      *args)
650 {
651         struct xfs_inode        *dp = args->dp;
652         struct xfs_buf          *bp = NULL;
653         int                     error;
654
655         if (!xfs_inode_hasattr(dp))
656                 return -ENOATTR;
657
658         if (dp->i_afp->if_format == XFS_DINODE_FMT_LOCAL)
659                 return xfs_attr_sf_findname(args, NULL, NULL);
660
661         if (xfs_attr_is_leaf(dp)) {
662                 error = xfs_attr_leaf_hasname(args, &bp);
663
664                 if (bp)
665                         xfs_trans_brelse(args->trans, bp);
666
667                 return error;
668         }
669
670         return xfs_attr_node_hasname(args, NULL);
671 }
672
673 static int
674 xfs_attr_item_init(
675         struct xfs_da_args      *args,
676         unsigned int            op_flags,       /* op flag (set or remove) */
677         struct xfs_attr_item    **attr)         /* new xfs_attr_item */
678 {
679
680         struct xfs_attr_item    *new;
681
682         new = kmem_zalloc(sizeof(struct xfs_attr_item), KM_NOFS);
683         new->xattri_op_flags = op_flags;
684         new->xattri_da_args = args;
685
686         *attr = new;
687         return 0;
688 }
689
690 /* Sets an attribute for an inode as a deferred operation */
691 static int
692 xfs_attr_defer_add(
693         struct xfs_da_args      *args)
694 {
695         struct xfs_attr_item    *new;
696         int                     error = 0;
697
698         error = xfs_attr_item_init(args, XFS_ATTR_OP_FLAGS_SET, &new);
699         if (error)
700                 return error;
701
702         new->xattri_dela_state = XFS_DAS_UNINIT;
703         xfs_defer_add(args->trans, XFS_DEFER_OPS_TYPE_ATTR, &new->xattri_list);
704         trace_xfs_attr_defer_add(new->xattri_dela_state, args->dp);
705
706         return 0;
707 }
708
709 /* Sets an attribute for an inode as a deferred operation */
710 static int
711 xfs_attr_defer_replace(
712         struct xfs_da_args      *args)
713 {
714         struct xfs_attr_item    *new;
715         int                     error = 0;
716
717         error = xfs_attr_item_init(args, XFS_ATTR_OP_FLAGS_REPLACE, &new);
718         if (error)
719                 return error;
720
721         new->xattri_dela_state = XFS_DAS_UNINIT;
722         xfs_defer_add(args->trans, XFS_DEFER_OPS_TYPE_ATTR, &new->xattri_list);
723         trace_xfs_attr_defer_replace(new->xattri_dela_state, args->dp);
724
725         return 0;
726 }
727
728 /* Removes an attribute for an inode as a deferred operation */
729 static int
730 xfs_attr_defer_remove(
731         struct xfs_da_args      *args)
732 {
733
734         struct xfs_attr_item    *new;
735         int                     error;
736
737         error  = xfs_attr_item_init(args, XFS_ATTR_OP_FLAGS_REMOVE, &new);
738         if (error)
739                 return error;
740
741         new->xattri_dela_state = XFS_DAS_UNINIT;
742         xfs_defer_add(args->trans, XFS_DEFER_OPS_TYPE_ATTR, &new->xattri_list);
743         trace_xfs_attr_defer_remove(new->xattri_dela_state, args->dp);
744
745         return 0;
746 }
747
748 /*
749  * Note: If args->value is NULL the attribute will be removed, just like the
750  * Linux ->setattr API.
751  */
752 int
753 xfs_attr_set(
754         struct xfs_da_args      *args)
755 {
756         struct xfs_inode        *dp = args->dp;
757         struct xfs_mount        *mp = dp->i_mount;
758         struct xfs_trans_res    tres;
759         bool                    rsvd = (args->attr_filter & XFS_ATTR_ROOT);
760         int                     error, local;
761         int                     rmt_blks = 0;
762         unsigned int            total;
763         int                     delayed = xfs_has_larp(mp);
764
765         if (xfs_is_shutdown(dp->i_mount))
766                 return -EIO;
767
768         error = xfs_qm_dqattach(dp);
769         if (error)
770                 return error;
771
772         args->geo = mp->m_attr_geo;
773         args->whichfork = XFS_ATTR_FORK;
774         args->hashval = xfs_da_hashname(args->name, args->namelen);
775
776         /*
777          * We have no control over the attribute names that userspace passes us
778          * to remove, so we have to allow the name lookup prior to attribute
779          * removal to fail as well.
780          */
781         args->op_flags = XFS_DA_OP_OKNOENT;
782
783         if (args->value) {
784                 XFS_STATS_INC(mp, xs_attr_set);
785
786                 args->op_flags |= XFS_DA_OP_ADDNAME;
787                 args->total = xfs_attr_calc_size(args, &local);
788
789                 /*
790                  * If the inode doesn't have an attribute fork, add one.
791                  * (inode must not be locked when we call this routine)
792                  */
793                 if (XFS_IFORK_Q(dp) == 0) {
794                         int sf_size = sizeof(struct xfs_attr_sf_hdr) +
795                                 xfs_attr_sf_entsize_byname(args->namelen,
796                                                 args->valuelen);
797
798                         error = xfs_bmap_add_attrfork(dp, sf_size, rsvd);
799                         if (error)
800                                 return error;
801                 }
802
803                 if (!local)
804                         rmt_blks = xfs_attr3_rmt_blocks(mp, args->valuelen);
805         } else {
806                 XFS_STATS_INC(mp, xs_attr_remove);
807                 rmt_blks = xfs_attr3_rmt_blocks(mp, XFS_XATTR_SIZE_MAX);
808         }
809
810         if (delayed) {
811                 error = xfs_attr_use_log_assist(mp);
812                 if (error)
813                         return error;
814         }
815
816         /*
817          * Root fork attributes can use reserved data blocks for this
818          * operation if necessary
819          */
820         xfs_init_attr_trans(args, &tres, &total);
821         error = xfs_trans_alloc_inode(dp, &tres, total, 0, rsvd, &args->trans);
822         if (error)
823                 goto drop_incompat;
824
825         if (args->value || xfs_inode_hasattr(dp)) {
826                 error = xfs_iext_count_may_overflow(dp, XFS_ATTR_FORK,
827                                 XFS_IEXT_ATTR_MANIP_CNT(rmt_blks));
828                 if (error == -EFBIG)
829                         error = xfs_iext_count_upgrade(args->trans, dp,
830                                         XFS_IEXT_ATTR_MANIP_CNT(rmt_blks));
831                 if (error)
832                         goto out_trans_cancel;
833         }
834
835         error = xfs_attr_lookup(args);
836         switch (error) {
837         case -EEXIST:
838                 /* if no value, we are performing a remove operation */
839                 if (!args->value) {
840                         error = xfs_attr_defer_remove(args);
841                         break;
842                 }
843                 /* Pure create fails if the attr already exists */
844                 if (args->attr_flags & XATTR_CREATE)
845                         goto out_trans_cancel;
846
847                 error = xfs_attr_defer_replace(args);
848                 break;
849         case -ENOATTR:
850                 /* Can't remove what isn't there. */
851                 if (!args->value)
852                         goto out_trans_cancel;
853
854                 /* Pure replace fails if no existing attr to replace. */
855                 if (args->attr_flags & XATTR_REPLACE)
856                         goto out_trans_cancel;
857
858                 error = xfs_attr_defer_add(args);
859                 break;
860         default:
861                 goto out_trans_cancel;
862         }
863         if (error)
864                 goto out_trans_cancel;
865
866         /*
867          * If this is a synchronous mount, make sure that the
868          * transaction goes to disk before returning to the user.
869          */
870         if (xfs_has_wsync(mp))
871                 xfs_trans_set_sync(args->trans);
872
873         if (!(args->op_flags & XFS_DA_OP_NOTIME))
874                 xfs_trans_ichgtime(args->trans, dp, XFS_ICHGTIME_CHG);
875
876         /*
877          * Commit the last in the sequence of transactions.
878          */
879         xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
880         error = xfs_trans_commit(args->trans);
881 out_unlock:
882         xfs_iunlock(dp, XFS_ILOCK_EXCL);
883 drop_incompat:
884         if (delayed)
885                 xlog_drop_incompat_feat(mp->m_log);
886         return error;
887
888 out_trans_cancel:
889         if (args->trans)
890                 xfs_trans_cancel(args->trans);
891         goto out_unlock;
892 }
893
894 int __init
895 xfs_attri_init_cache(void)
896 {
897         xfs_attri_cache = kmem_cache_create("xfs_attri",
898                                             sizeof(struct xfs_attri_log_item),
899                                             0, 0, NULL);
900
901         return xfs_attri_cache != NULL ? 0 : -ENOMEM;
902 }
903
904 void
905 xfs_attri_destroy_cache(void)
906 {
907         kmem_cache_destroy(xfs_attri_cache);
908         xfs_attri_cache = NULL;
909 }
910
911 int __init
912 xfs_attrd_init_cache(void)
913 {
914         xfs_attrd_cache = kmem_cache_create("xfs_attrd",
915                                             sizeof(struct xfs_attrd_log_item),
916                                             0, 0, NULL);
917
918         return xfs_attrd_cache != NULL ? 0 : -ENOMEM;
919 }
920
921 void
922 xfs_attrd_destroy_cache(void)
923 {
924         kmem_cache_destroy(xfs_attrd_cache);
925         xfs_attrd_cache = NULL;
926 }
927
928 /*========================================================================
929  * External routines when attribute list is inside the inode
930  *========================================================================*/
931
932 static inline int xfs_attr_sf_totsize(struct xfs_inode *dp)
933 {
934         struct xfs_attr_shortform *sf;
935
936         sf = (struct xfs_attr_shortform *)dp->i_afp->if_u1.if_data;
937         return be16_to_cpu(sf->hdr.totsize);
938 }
939
940 /*
941  * Add a name to the shortform attribute list structure
942  * This is the external routine.
943  */
944 STATIC int
945 xfs_attr_shortform_addname(xfs_da_args_t *args)
946 {
947         int newsize, forkoff, retval;
948
949         trace_xfs_attr_sf_addname(args);
950
951         retval = xfs_attr_shortform_lookup(args);
952         if (retval == -ENOATTR && (args->attr_flags & XATTR_REPLACE))
953                 return retval;
954         if (retval == -EEXIST) {
955                 if (args->attr_flags & XATTR_CREATE)
956                         return retval;
957                 retval = xfs_attr_sf_removename(args);
958                 if (retval)
959                         return retval;
960                 /*
961                  * Since we have removed the old attr, clear ATTR_REPLACE so
962                  * that the leaf format add routine won't trip over the attr
963                  * not being around.
964                  */
965                 args->attr_flags &= ~XATTR_REPLACE;
966         }
967
968         if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
969             args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
970                 return -ENOSPC;
971
972         newsize = xfs_attr_sf_totsize(args->dp);
973         newsize += xfs_attr_sf_entsize_byname(args->namelen, args->valuelen);
974
975         forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
976         if (!forkoff)
977                 return -ENOSPC;
978
979         xfs_attr_shortform_add(args, forkoff);
980         return 0;
981 }
982
983
984 /*========================================================================
985  * External routines when attribute list is one block
986  *========================================================================*/
987
988 /* Store info about a remote block */
989 STATIC void
990 xfs_attr_save_rmt_blk(
991         struct xfs_da_args      *args)
992 {
993         args->blkno2 = args->blkno;
994         args->index2 = args->index;
995         args->rmtblkno2 = args->rmtblkno;
996         args->rmtblkcnt2 = args->rmtblkcnt;
997         args->rmtvaluelen2 = args->rmtvaluelen;
998 }
999
1000 /* Set stored info about a remote block */
1001 STATIC void
1002 xfs_attr_restore_rmt_blk(
1003         struct xfs_da_args      *args)
1004 {
1005         args->blkno = args->blkno2;
1006         args->index = args->index2;
1007         args->rmtblkno = args->rmtblkno2;
1008         args->rmtblkcnt = args->rmtblkcnt2;
1009         args->rmtvaluelen = args->rmtvaluelen2;
1010 }
1011
1012 /*
1013  * Tries to add an attribute to an inode in leaf form
1014  *
1015  * This function is meant to execute as part of a delayed operation and leaves
1016  * the transaction handling to the caller.  On success the attribute is added
1017  * and the inode and transaction are left dirty.  If there is not enough space,
1018  * the attr data is converted to node format and -ENOSPC is returned. Caller is
1019  * responsible for handling the dirty inode and transaction or adding the attr
1020  * in node format.
1021  */
1022 STATIC int
1023 xfs_attr_leaf_try_add(
1024         struct xfs_da_args      *args,
1025         struct xfs_buf          *bp)
1026 {
1027         int                     error;
1028
1029         /*
1030          * If the caller provided a buffer to us, it is locked and held in
1031          * the transaction because it just did a shortform to leaf conversion.
1032          * Hence we don't need to read it again. Otherwise read in the leaf
1033          * buffer.
1034          */
1035         if (bp) {
1036                 xfs_trans_bhold_release(args->trans, bp);
1037         } else {
1038                 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, &bp);
1039                 if (error)
1040                         return error;
1041         }
1042
1043         /*
1044          * Look up the xattr name to set the insertion point for the new xattr.
1045          */
1046         error = xfs_attr3_leaf_lookup_int(bp, args);
1047         if (error != -ENOATTR && error != -EEXIST)
1048                 goto out_brelse;
1049         if (error == -ENOATTR && (args->attr_flags & XATTR_REPLACE))
1050                 goto out_brelse;
1051         if (error == -EEXIST) {
1052                 if (args->attr_flags & XATTR_CREATE)
1053                         goto out_brelse;
1054
1055                 trace_xfs_attr_leaf_replace(args);
1056
1057                 /* save the attribute state for later removal*/
1058                 args->op_flags |= XFS_DA_OP_RENAME;     /* an atomic rename */
1059                 xfs_attr_save_rmt_blk(args);
1060
1061                 /*
1062                  * clear the remote attr state now that it is saved so that the
1063                  * values reflect the state of the attribute we are about to
1064                  * add, not the attribute we just found and will remove later.
1065                  */
1066                 args->rmtblkno = 0;
1067                 args->rmtblkcnt = 0;
1068                 args->rmtvaluelen = 0;
1069         }
1070
1071         return xfs_attr3_leaf_add(bp, args);
1072
1073 out_brelse:
1074         xfs_trans_brelse(args->trans, bp);
1075         return error;
1076 }
1077
1078 /*
1079  * Return EEXIST if attr is found, or ENOATTR if not
1080  */
1081 STATIC int
1082 xfs_attr_leaf_hasname(
1083         struct xfs_da_args      *args,
1084         struct xfs_buf          **bp)
1085 {
1086         int                     error = 0;
1087
1088         error = xfs_attr3_leaf_read(args->trans, args->dp, 0, bp);
1089         if (error)
1090                 return error;
1091
1092         error = xfs_attr3_leaf_lookup_int(*bp, args);
1093         if (error != -ENOATTR && error != -EEXIST)
1094                 xfs_trans_brelse(args->trans, *bp);
1095
1096         return error;
1097 }
1098
1099 /*
1100  * Remove a name from the leaf attribute list structure
1101  *
1102  * This leaf block cannot have a "remote" value, we only call this routine
1103  * if bmap_one_block() says there is only one block (ie: no remote blks).
1104  */
1105 STATIC int
1106 xfs_attr_leaf_removename(
1107         struct xfs_da_args      *args)
1108 {
1109         struct xfs_inode        *dp;
1110         struct xfs_buf          *bp;
1111         int                     error, forkoff;
1112
1113         trace_xfs_attr_leaf_removename(args);
1114
1115         /*
1116          * Remove the attribute.
1117          */
1118         dp = args->dp;
1119
1120         error = xfs_attr_leaf_hasname(args, &bp);
1121
1122         if (error == -ENOATTR) {
1123                 xfs_trans_brelse(args->trans, bp);
1124                 return error;
1125         } else if (error != -EEXIST)
1126                 return error;
1127
1128         xfs_attr3_leaf_remove(bp, args);
1129
1130         /*
1131          * If the result is small enough, shrink it all into the inode.
1132          */
1133         forkoff = xfs_attr_shortform_allfit(bp, dp);
1134         if (forkoff)
1135                 return xfs_attr3_leaf_to_shortform(bp, args, forkoff);
1136                 /* bp is gone due to xfs_da_shrink_inode */
1137
1138         return 0;
1139 }
1140
1141 /*
1142  * Look up a name in a leaf attribute list structure.
1143  *
1144  * This leaf block cannot have a "remote" value, we only call this routine
1145  * if bmap_one_block() says there is only one block (ie: no remote blks).
1146  *
1147  * Returns 0 on successful retrieval, otherwise an error.
1148  */
1149 STATIC int
1150 xfs_attr_leaf_get(xfs_da_args_t *args)
1151 {
1152         struct xfs_buf *bp;
1153         int error;
1154
1155         trace_xfs_attr_leaf_get(args);
1156
1157         error = xfs_attr_leaf_hasname(args, &bp);
1158
1159         if (error == -ENOATTR)  {
1160                 xfs_trans_brelse(args->trans, bp);
1161                 return error;
1162         } else if (error != -EEXIST)
1163                 return error;
1164
1165
1166         error = xfs_attr3_leaf_getvalue(bp, args);
1167         xfs_trans_brelse(args->trans, bp);
1168         return error;
1169 }
1170
1171 /*
1172  * Return EEXIST if attr is found, or ENOATTR if not
1173  * statep: If not null is set to point at the found state.  Caller will
1174  *         be responsible for freeing the state in this case.
1175  */
1176 STATIC int
1177 xfs_attr_node_hasname(
1178         struct xfs_da_args      *args,
1179         struct xfs_da_state     **statep)
1180 {
1181         struct xfs_da_state     *state;
1182         int                     retval, error;
1183
1184         state = xfs_da_state_alloc(args);
1185         if (statep != NULL)
1186                 *statep = state;
1187
1188         /*
1189          * Search to see if name exists, and get back a pointer to it.
1190          */
1191         error = xfs_da3_node_lookup_int(state, &retval);
1192         if (error)
1193                 retval = error;
1194
1195         if (!statep)
1196                 xfs_da_state_free(state);
1197
1198         return retval;
1199 }
1200
1201 /*========================================================================
1202  * External routines when attribute list size > geo->blksize
1203  *========================================================================*/
1204
1205 STATIC int
1206 xfs_attr_node_addname_find_attr(
1207          struct xfs_attr_item           *attr)
1208 {
1209         struct xfs_da_args              *args = attr->xattri_da_args;
1210         int                             retval;
1211
1212         /*
1213          * Search to see if name already exists, and get back a pointer
1214          * to where it should go.
1215          */
1216         retval = xfs_attr_node_hasname(args, &attr->xattri_da_state);
1217         if (retval != -ENOATTR && retval != -EEXIST)
1218                 goto error;
1219
1220         if (retval == -ENOATTR && (args->attr_flags & XATTR_REPLACE))
1221                 goto error;
1222         if (retval == -EEXIST) {
1223                 if (args->attr_flags & XATTR_CREATE)
1224                         goto error;
1225
1226                 trace_xfs_attr_node_replace(args);
1227
1228                 /* save the attribute state for later removal*/
1229                 args->op_flags |= XFS_DA_OP_RENAME;     /* atomic rename op */
1230                 xfs_attr_save_rmt_blk(args);
1231
1232                 /*
1233                  * clear the remote attr state now that it is saved so that the
1234                  * values reflect the state of the attribute we are about to
1235                  * add, not the attribute we just found and will remove later.
1236                  */
1237                 args->rmtblkno = 0;
1238                 args->rmtblkcnt = 0;
1239                 args->rmtvaluelen = 0;
1240         }
1241
1242         return 0;
1243 error:
1244         if (attr->xattri_da_state)
1245                 xfs_da_state_free(attr->xattri_da_state);
1246         return retval;
1247 }
1248
1249 /*
1250  * Add a name to a Btree-format attribute list.
1251  *
1252  * This will involve walking down the Btree, and may involve splitting
1253  * leaf nodes and even splitting intermediate nodes up to and including
1254  * the root node (a special case of an intermediate node).
1255  *
1256  * "Remote" attribute values confuse the issue and atomic rename operations
1257  * add a whole extra layer of confusion on top of that.
1258  *
1259  * This routine is meant to function as a delayed operation, and may return
1260  * -EAGAIN when the transaction needs to be rolled.  Calling functions will need
1261  * to handle this, and recall the function until a successful error code is
1262  *returned.
1263  */
1264 STATIC int
1265 xfs_attr_node_addname(
1266         struct xfs_attr_item            *attr)
1267 {
1268         struct xfs_da_args              *args = attr->xattri_da_args;
1269         struct xfs_da_state             *state = attr->xattri_da_state;
1270         struct xfs_da_state_blk         *blk;
1271         int                             error;
1272
1273         trace_xfs_attr_node_addname(args);
1274
1275         blk = &state->path.blk[state->path.active-1];
1276         ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1277
1278         error = xfs_attr3_leaf_add(blk->bp, state->args);
1279         if (error == -ENOSPC) {
1280                 if (state->path.active == 1) {
1281                         /*
1282                          * Its really a single leaf node, but it had
1283                          * out-of-line values so it looked like it *might*
1284                          * have been a b-tree.
1285                          */
1286                         xfs_da_state_free(state);
1287                         state = NULL;
1288                         error = xfs_attr3_leaf_to_node(args);
1289                         if (error)
1290                                 goto out;
1291
1292                         /*
1293                          * Now that we have converted the leaf to a node, we can
1294                          * roll the transaction, and try xfs_attr3_leaf_add
1295                          * again on re-entry.  No need to set dela_state to do
1296                          * this. dela_state is still unset by this function at
1297                          * this point.
1298                          */
1299                         trace_xfs_attr_node_addname_return(
1300                                         attr->xattri_dela_state, args->dp);
1301                         return -EAGAIN;
1302                 }
1303
1304                 /*
1305                  * Split as many Btree elements as required.
1306                  * This code tracks the new and old attr's location
1307                  * in the index/blkno/rmtblkno/rmtblkcnt fields and
1308                  * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
1309                  */
1310                 error = xfs_da3_split(state);
1311                 if (error)
1312                         goto out;
1313         } else {
1314                 /*
1315                  * Addition succeeded, update Btree hashvals.
1316                  */
1317                 xfs_da3_fixhashpath(state, &state->path);
1318         }
1319
1320 out:
1321         if (state)
1322                 xfs_da_state_free(state);
1323         return error;
1324 }
1325
1326
1327 STATIC int
1328 xfs_attr_node_addname_clear_incomplete(
1329         struct xfs_attr_item            *attr)
1330 {
1331         struct xfs_da_args              *args = attr->xattri_da_args;
1332         struct xfs_da_state             *state = NULL;
1333         struct xfs_mount                *mp = args->dp->i_mount;
1334         int                             retval = 0;
1335         int                             error = 0;
1336
1337         /*
1338          * Re-find the "old" attribute entry after any split ops. The INCOMPLETE
1339          * flag means that we will find the "old" attr, not the "new" one.
1340          */
1341         if (!xfs_has_larp(mp))
1342                 args->attr_filter |= XFS_ATTR_INCOMPLETE;
1343         state = xfs_da_state_alloc(args);
1344         state->inleaf = 0;
1345         error = xfs_da3_node_lookup_int(state, &retval);
1346         if (error)
1347                 goto out;
1348
1349         error = xfs_attr_node_removename(args, state);
1350
1351         /*
1352          * Check to see if the tree needs to be collapsed.
1353          */
1354         if (retval && (state->path.active > 1)) {
1355                 error = xfs_da3_join(state);
1356                 if (error)
1357                         goto out;
1358         }
1359         retval = error = 0;
1360
1361 out:
1362         if (state)
1363                 xfs_da_state_free(state);
1364         if (error)
1365                 return error;
1366         return retval;
1367 }
1368
1369 /*
1370  * Shrink an attribute from leaf to shortform
1371  */
1372 STATIC int
1373 xfs_attr_node_shrink(
1374         struct xfs_da_args      *args,
1375         struct xfs_da_state     *state)
1376 {
1377         struct xfs_inode        *dp = args->dp;
1378         int                     error, forkoff;
1379         struct xfs_buf          *bp;
1380
1381         /*
1382          * Have to get rid of the copy of this dabuf in the state.
1383          */
1384         ASSERT(state->path.active == 1);
1385         ASSERT(state->path.blk[0].bp);
1386         state->path.blk[0].bp = NULL;
1387
1388         error = xfs_attr3_leaf_read(args->trans, args->dp, 0, &bp);
1389         if (error)
1390                 return error;
1391
1392         forkoff = xfs_attr_shortform_allfit(bp, dp);
1393         if (forkoff) {
1394                 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
1395                 /* bp is gone due to xfs_da_shrink_inode */
1396         } else
1397                 xfs_trans_brelse(args->trans, bp);
1398
1399         return error;
1400 }
1401
1402 /*
1403  * Mark an attribute entry INCOMPLETE and save pointers to the relevant buffers
1404  * for later deletion of the entry.
1405  */
1406 STATIC int
1407 xfs_attr_leaf_mark_incomplete(
1408         struct xfs_da_args      *args,
1409         struct xfs_da_state     *state)
1410 {
1411         int                     error;
1412
1413         /*
1414          * Fill in disk block numbers in the state structure
1415          * so that we can get the buffers back after we commit
1416          * several transactions in the following calls.
1417          */
1418         error = xfs_attr_fillstate(state);
1419         if (error)
1420                 return error;
1421
1422         /*
1423          * Mark the attribute as INCOMPLETE
1424          */
1425         return xfs_attr3_leaf_setflag(args);
1426 }
1427
1428 /*
1429  * Initial setup for xfs_attr_node_removename.  Make sure the attr is there and
1430  * the blocks are valid.  Attr keys with remote blocks will be marked
1431  * incomplete.
1432  */
1433 STATIC
1434 int xfs_attr_node_removename_setup(
1435         struct xfs_attr_item            *attr)
1436 {
1437         struct xfs_da_args              *args = attr->xattri_da_args;
1438         struct xfs_da_state             **state = &attr->xattri_da_state;
1439         int                             error;
1440
1441         error = xfs_attr_node_hasname(args, state);
1442         if (error != -EEXIST)
1443                 goto out;
1444         error = 0;
1445
1446         ASSERT((*state)->path.blk[(*state)->path.active - 1].bp != NULL);
1447         ASSERT((*state)->path.blk[(*state)->path.active - 1].magic ==
1448                 XFS_ATTR_LEAF_MAGIC);
1449
1450         if (args->rmtblkno > 0) {
1451                 error = xfs_attr_leaf_mark_incomplete(args, *state);
1452                 if (error)
1453                         goto out;
1454
1455                 error = xfs_attr_rmtval_invalidate(args);
1456         }
1457 out:
1458         if (error)
1459                 xfs_da_state_free(*state);
1460
1461         return error;
1462 }
1463
1464 STATIC int
1465 xfs_attr_node_removename(
1466         struct xfs_da_args      *args,
1467         struct xfs_da_state     *state)
1468 {
1469         struct xfs_da_state_blk *blk;
1470         int                     retval;
1471
1472         /*
1473          * Remove the name and update the hashvals in the tree.
1474          */
1475         blk = &state->path.blk[state->path.active-1];
1476         ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1477         retval = xfs_attr3_leaf_remove(blk->bp, args);
1478         xfs_da3_fixhashpath(state, &state->path);
1479
1480         return retval;
1481 }
1482
1483 /*
1484  * Remove the attribute specified in @args.
1485  *
1486  * This will involve walking down the Btree, and may involve joining
1487  * leaf nodes and even joining intermediate nodes up to and including
1488  * the root node (a special case of an intermediate node).
1489  *
1490  * This routine is meant to function as either an in-line or delayed operation,
1491  * and may return -EAGAIN when the transaction needs to be rolled.  Calling
1492  * functions will need to handle this, and call the function until a
1493  * successful error code is returned.
1494  */
1495 int
1496 xfs_attr_remove_iter(
1497         struct xfs_attr_item            *attr)
1498 {
1499         struct xfs_da_args              *args = attr->xattri_da_args;
1500         struct xfs_da_state             *state = attr->xattri_da_state;
1501         int                             retval, error = 0;
1502         struct xfs_inode                *dp = args->dp;
1503
1504         trace_xfs_attr_node_removename(args);
1505
1506         switch (attr->xattri_dela_state) {
1507         case XFS_DAS_UNINIT:
1508                 if (!xfs_inode_hasattr(dp))
1509                         return -ENOATTR;
1510
1511                 /*
1512                  * Shortform or leaf formats don't require transaction rolls and
1513                  * thus state transitions. Call the right helper and return.
1514                  */
1515                 if (dp->i_afp->if_format == XFS_DINODE_FMT_LOCAL)
1516                         return xfs_attr_sf_removename(args);
1517
1518                 if (xfs_attr_is_leaf(dp))
1519                         return xfs_attr_leaf_removename(args);
1520
1521                 /*
1522                  * Node format may require transaction rolls. Set up the
1523                  * state context and fall into the state machine.
1524                  */
1525                 if (!attr->xattri_da_state) {
1526                         error = xfs_attr_node_removename_setup(attr);
1527                         if (error)
1528                                 return error;
1529                         state = attr->xattri_da_state;
1530                 }
1531
1532                 fallthrough;
1533         case XFS_DAS_RMTBLK:
1534                 attr->xattri_dela_state = XFS_DAS_RMTBLK;
1535
1536                 /*
1537                  * If there is an out-of-line value, de-allocate the blocks.
1538                  * This is done before we remove the attribute so that we don't
1539                  * overflow the maximum size of a transaction and/or hit a
1540                  * deadlock.
1541                  */
1542                 if (args->rmtblkno > 0) {
1543                         /*
1544                          * May return -EAGAIN. Roll and repeat until all remote
1545                          * blocks are removed.
1546                          */
1547                         error = xfs_attr_rmtval_remove(attr);
1548                         if (error == -EAGAIN) {
1549                                 trace_xfs_attr_remove_iter_return(
1550                                         attr->xattri_dela_state, args->dp);
1551                                 return error;
1552                         } else if (error) {
1553                                 goto out;
1554                         }
1555
1556                         /*
1557                          * Refill the state structure with buffers (the prior
1558                          * calls released our buffers) and close out this
1559                          * transaction before proceeding.
1560                          */
1561                         ASSERT(args->rmtblkno == 0);
1562                         error = xfs_attr_refillstate(state);
1563                         if (error)
1564                                 goto out;
1565
1566                         attr->xattri_dela_state = XFS_DAS_RM_NAME;
1567                         trace_xfs_attr_remove_iter_return(
1568                                         attr->xattri_dela_state, args->dp);
1569                         return -EAGAIN;
1570                 }
1571
1572                 fallthrough;
1573         case XFS_DAS_RM_NAME:
1574                 /*
1575                  * If we came here fresh from a transaction roll, reattach all
1576                  * the buffers to the current transaction.
1577                  */
1578                 if (attr->xattri_dela_state == XFS_DAS_RM_NAME) {
1579                         error = xfs_attr_refillstate(state);
1580                         if (error)
1581                                 goto out;
1582                 }
1583
1584                 retval = xfs_attr_node_removename(args, state);
1585
1586                 /*
1587                  * Check to see if the tree needs to be collapsed. If so, roll
1588                  * the transacton and fall into the shrink state.
1589                  */
1590                 if (retval && (state->path.active > 1)) {
1591                         error = xfs_da3_join(state);
1592                         if (error)
1593                                 goto out;
1594
1595                         attr->xattri_dela_state = XFS_DAS_RM_SHRINK;
1596                         trace_xfs_attr_remove_iter_return(
1597                                         attr->xattri_dela_state, args->dp);
1598                         return -EAGAIN;
1599                 }
1600
1601                 fallthrough;
1602         case XFS_DAS_RM_SHRINK:
1603                 /*
1604                  * If the result is small enough, push it all into the inode.
1605                  * This is our final state so it's safe to return a dirty
1606                  * transaction.
1607                  */
1608                 if (xfs_attr_is_leaf(dp))
1609                         error = xfs_attr_node_shrink(args, state);
1610                 ASSERT(error != -EAGAIN);
1611                 break;
1612         default:
1613                 ASSERT(0);
1614                 error = -EINVAL;
1615                 goto out;
1616         }
1617 out:
1618         if (state)
1619                 xfs_da_state_free(state);
1620         return error;
1621 }
1622
1623 /*
1624  * Fill in the disk block numbers in the state structure for the buffers
1625  * that are attached to the state structure.
1626  * This is done so that we can quickly reattach ourselves to those buffers
1627  * after some set of transaction commits have released these buffers.
1628  */
1629 STATIC int
1630 xfs_attr_fillstate(xfs_da_state_t *state)
1631 {
1632         xfs_da_state_path_t *path;
1633         xfs_da_state_blk_t *blk;
1634         int level;
1635
1636         trace_xfs_attr_fillstate(state->args);
1637
1638         /*
1639          * Roll down the "path" in the state structure, storing the on-disk
1640          * block number for those buffers in the "path".
1641          */
1642         path = &state->path;
1643         ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1644         for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1645                 if (blk->bp) {
1646                         blk->disk_blkno = xfs_buf_daddr(blk->bp);
1647                         blk->bp = NULL;
1648                 } else {
1649                         blk->disk_blkno = 0;
1650                 }
1651         }
1652
1653         /*
1654          * Roll down the "altpath" in the state structure, storing the on-disk
1655          * block number for those buffers in the "altpath".
1656          */
1657         path = &state->altpath;
1658         ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1659         for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1660                 if (blk->bp) {
1661                         blk->disk_blkno = xfs_buf_daddr(blk->bp);
1662                         blk->bp = NULL;
1663                 } else {
1664                         blk->disk_blkno = 0;
1665                 }
1666         }
1667
1668         return 0;
1669 }
1670
1671 /*
1672  * Reattach the buffers to the state structure based on the disk block
1673  * numbers stored in the state structure.
1674  * This is done after some set of transaction commits have released those
1675  * buffers from our grip.
1676  */
1677 STATIC int
1678 xfs_attr_refillstate(xfs_da_state_t *state)
1679 {
1680         xfs_da_state_path_t *path;
1681         xfs_da_state_blk_t *blk;
1682         int level, error;
1683
1684         trace_xfs_attr_refillstate(state->args);
1685
1686         /*
1687          * Roll down the "path" in the state structure, storing the on-disk
1688          * block number for those buffers in the "path".
1689          */
1690         path = &state->path;
1691         ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1692         for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1693                 if (blk->disk_blkno) {
1694                         error = xfs_da3_node_read_mapped(state->args->trans,
1695                                         state->args->dp, blk->disk_blkno,
1696                                         &blk->bp, XFS_ATTR_FORK);
1697                         if (error)
1698                                 return error;
1699                 } else {
1700                         blk->bp = NULL;
1701                 }
1702         }
1703
1704         /*
1705          * Roll down the "altpath" in the state structure, storing the on-disk
1706          * block number for those buffers in the "altpath".
1707          */
1708         path = &state->altpath;
1709         ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1710         for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1711                 if (blk->disk_blkno) {
1712                         error = xfs_da3_node_read_mapped(state->args->trans,
1713                                         state->args->dp, blk->disk_blkno,
1714                                         &blk->bp, XFS_ATTR_FORK);
1715                         if (error)
1716                                 return error;
1717                 } else {
1718                         blk->bp = NULL;
1719                 }
1720         }
1721
1722         return 0;
1723 }
1724
1725 /*
1726  * Retrieve the attribute data from a node attribute list.
1727  *
1728  * This routine gets called for any attribute fork that has more than one
1729  * block, ie: both true Btree attr lists and for single-leaf-blocks with
1730  * "remote" values taking up more blocks.
1731  *
1732  * Returns 0 on successful retrieval, otherwise an error.
1733  */
1734 STATIC int
1735 xfs_attr_node_get(
1736         struct xfs_da_args      *args)
1737 {
1738         struct xfs_da_state     *state;
1739         struct xfs_da_state_blk *blk;
1740         int                     i;
1741         int                     error;
1742
1743         trace_xfs_attr_node_get(args);
1744
1745         /*
1746          * Search to see if name exists, and get back a pointer to it.
1747          */
1748         error = xfs_attr_node_hasname(args, &state);
1749         if (error != -EEXIST)
1750                 goto out_release;
1751
1752         /*
1753          * Get the value, local or "remote"
1754          */
1755         blk = &state->path.blk[state->path.active - 1];
1756         error = xfs_attr3_leaf_getvalue(blk->bp, args);
1757
1758         /*
1759          * If not in a transaction, we have to release all the buffers.
1760          */
1761 out_release:
1762         for (i = 0; state != NULL && i < state->path.active; i++) {
1763                 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
1764                 state->path.blk[i].bp = NULL;
1765         }
1766
1767         if (state)
1768                 xfs_da_state_free(state);
1769         return error;
1770 }
1771
1772 /* Returns true if the attribute entry name is valid. */
1773 bool
1774 xfs_attr_namecheck(
1775         const void      *name,
1776         size_t          length)
1777 {
1778         /*
1779          * MAXNAMELEN includes the trailing null, but (name/length) leave it
1780          * out, so use >= for the length check.
1781          */
1782         if (length >= MAXNAMELEN)
1783                 return false;
1784
1785         /* There shouldn't be any nulls here */
1786         return !memchr(name, 0, length);
1787 }