]> Git Repo - J-linux.git/blob - fs/xfs/libxfs/xfs_attr.c
Merge patch series "riscv: Extension parsing fixes"
[J-linux.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_xattr.h"
29 #include "xfs_parent.h"
30
31 struct kmem_cache               *xfs_attr_intent_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);
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_try_addname(struct xfs_attr_intent *attr);
62 STATIC int xfs_attr_node_addname_find_attr(struct xfs_attr_intent *attr);
63 STATIC int xfs_attr_node_remove_attr(struct xfs_attr_intent *attr);
64 STATIC int xfs_attr_node_lookup(struct xfs_da_args *args,
65                 struct xfs_da_state *state);
66
67 int
68 xfs_inode_hasattr(
69         struct xfs_inode        *ip)
70 {
71         if (!xfs_inode_has_attr_fork(ip))
72                 return 0;
73         if (ip->i_af.if_format == XFS_DINODE_FMT_EXTENTS &&
74             ip->i_af.if_nextents == 0)
75                 return 0;
76         return 1;
77 }
78
79 /*
80  * Returns true if the there is exactly only block in the attr fork, in which
81  * case the attribute fork consists of a single leaf block entry.
82  */
83 bool
84 xfs_attr_is_leaf(
85         struct xfs_inode        *ip)
86 {
87         struct xfs_ifork        *ifp = &ip->i_af;
88         struct xfs_iext_cursor  icur;
89         struct xfs_bmbt_irec    imap;
90
91         ASSERT(!xfs_need_iread_extents(ifp));
92
93         if (ifp->if_nextents != 1 || ifp->if_format != XFS_DINODE_FMT_EXTENTS)
94                 return false;
95
96         xfs_iext_first(ifp, &icur);
97         xfs_iext_get_extent(ifp, &icur, &imap);
98         return imap.br_startoff == 0 && imap.br_blockcount == 1;
99 }
100
101 /*
102  * XXX (dchinner): name path state saving and refilling is an optimisation to
103  * avoid needing to look up name entries after rolling transactions removing
104  * remote xattr blocks between the name entry lookup and name entry removal.
105  * This optimisation got sidelined when combining the set and remove state
106  * machines, but the code has been left in place because it is worthwhile to
107  * restore the optimisation once the combined state machine paths have settled.
108  *
109  * This comment is a public service announcement to remind Future Dave that he
110  * still needs to restore this code to working order.
111  */
112 #if 0
113 /*
114  * Fill in the disk block numbers in the state structure for the buffers
115  * that are attached to the state structure.
116  * This is done so that we can quickly reattach ourselves to those buffers
117  * after some set of transaction commits have released these buffers.
118  */
119 static int
120 xfs_attr_fillstate(xfs_da_state_t *state)
121 {
122         xfs_da_state_path_t *path;
123         xfs_da_state_blk_t *blk;
124         int level;
125
126         trace_xfs_attr_fillstate(state->args);
127
128         /*
129          * Roll down the "path" in the state structure, storing the on-disk
130          * block number for those buffers in the "path".
131          */
132         path = &state->path;
133         ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
134         for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
135                 if (blk->bp) {
136                         blk->disk_blkno = xfs_buf_daddr(blk->bp);
137                         blk->bp = NULL;
138                 } else {
139                         blk->disk_blkno = 0;
140                 }
141         }
142
143         /*
144          * Roll down the "altpath" in the state structure, storing the on-disk
145          * block number for those buffers in the "altpath".
146          */
147         path = &state->altpath;
148         ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
149         for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
150                 if (blk->bp) {
151                         blk->disk_blkno = xfs_buf_daddr(blk->bp);
152                         blk->bp = NULL;
153                 } else {
154                         blk->disk_blkno = 0;
155                 }
156         }
157
158         return 0;
159 }
160
161 /*
162  * Reattach the buffers to the state structure based on the disk block
163  * numbers stored in the state structure.
164  * This is done after some set of transaction commits have released those
165  * buffers from our grip.
166  */
167 static int
168 xfs_attr_refillstate(xfs_da_state_t *state)
169 {
170         xfs_da_state_path_t *path;
171         xfs_da_state_blk_t *blk;
172         int level, error;
173
174         trace_xfs_attr_refillstate(state->args);
175
176         /*
177          * Roll down the "path" in the state structure, storing the on-disk
178          * block number for those buffers in the "path".
179          */
180         path = &state->path;
181         ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
182         for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
183                 if (blk->disk_blkno) {
184                         error = xfs_da3_node_read_mapped(state->args->trans,
185                                         state->args->dp, blk->disk_blkno,
186                                         &blk->bp, XFS_ATTR_FORK);
187                         if (error)
188                                 return error;
189                 } else {
190                         blk->bp = NULL;
191                 }
192         }
193
194         /*
195          * Roll down the "altpath" in the state structure, storing the on-disk
196          * block number for those buffers in the "altpath".
197          */
198         path = &state->altpath;
199         ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
200         for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
201                 if (blk->disk_blkno) {
202                         error = xfs_da3_node_read_mapped(state->args->trans,
203                                         state->args->dp, blk->disk_blkno,
204                                         &blk->bp, XFS_ATTR_FORK);
205                         if (error)
206                                 return error;
207                 } else {
208                         blk->bp = NULL;
209                 }
210         }
211
212         return 0;
213 }
214 #else
215 static int xfs_attr_fillstate(xfs_da_state_t *state) { return 0; }
216 #endif
217
218 /*========================================================================
219  * Overall external interface routines.
220  *========================================================================*/
221
222 /*
223  * Retrieve an extended attribute and its value.  Must have ilock.
224  * Returns 0 on successful retrieval, otherwise an error.
225  */
226 int
227 xfs_attr_get_ilocked(
228         struct xfs_da_args      *args)
229 {
230         int                     error;
231
232         xfs_assert_ilocked(args->dp, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL);
233
234         if (!xfs_inode_hasattr(args->dp))
235                 return -ENOATTR;
236
237         /*
238          * The incore attr fork iext tree must be loaded for xfs_attr_is_leaf
239          * to work correctly.
240          */
241         error = xfs_iread_extents(args->trans, args->dp, XFS_ATTR_FORK);
242         if (error)
243                 return error;
244
245         if (args->dp->i_af.if_format == XFS_DINODE_FMT_LOCAL)
246                 return xfs_attr_shortform_getvalue(args);
247         if (xfs_attr_is_leaf(args->dp))
248                 return xfs_attr_leaf_get(args);
249         return xfs_attr_node_get(args);
250 }
251
252 /*
253  * Retrieve an extended attribute by name, and its value if requested.
254  *
255  * If args->valuelen is zero, then the caller does not want the value, just an
256  * indication whether the attribute exists and the size of the value if it
257  * exists. The size is returned in args.valuelen.
258  *
259  * If args->value is NULL but args->valuelen is non-zero, allocate the buffer
260  * for the value after existence of the attribute has been determined. The
261  * caller always has to free args->value if it is set, no matter if this
262  * function was successful or not.
263  *
264  * If the attribute is found, but exceeds the size limit set by the caller in
265  * args->valuelen, return -ERANGE with the size of the attribute that was found
266  * in args->valuelen.
267  */
268 int
269 xfs_attr_get(
270         struct xfs_da_args      *args)
271 {
272         uint                    lock_mode;
273         int                     error;
274
275         XFS_STATS_INC(args->dp->i_mount, xs_attr_get);
276
277         if (xfs_is_shutdown(args->dp->i_mount))
278                 return -EIO;
279
280         if (!args->owner)
281                 args->owner = args->dp->i_ino;
282         args->geo = args->dp->i_mount->m_attr_geo;
283         args->whichfork = XFS_ATTR_FORK;
284         xfs_attr_sethash(args);
285
286         /* Entirely possible to look up a name which doesn't exist */
287         args->op_flags = XFS_DA_OP_OKNOENT;
288
289         lock_mode = xfs_ilock_attr_map_shared(args->dp);
290         error = xfs_attr_get_ilocked(args);
291         xfs_iunlock(args->dp, lock_mode);
292
293         return error;
294 }
295
296 /*
297  * Calculate how many blocks we need for the new attribute,
298  */
299 int
300 xfs_attr_calc_size(
301         struct xfs_da_args      *args,
302         int                     *local)
303 {
304         struct xfs_mount        *mp = args->dp->i_mount;
305         int                     size;
306         int                     nblks;
307
308         /*
309          * Determine space new attribute will use, and if it would be
310          * "local" or "remote" (note: local != inline).
311          */
312         size = xfs_attr_leaf_newentsize(args, local);
313         nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
314         if (*local) {
315                 if (size > (args->geo->blksize / 2)) {
316                         /* Double split possible */
317                         nblks *= 2;
318                 }
319         } else {
320                 /*
321                  * Out of line attribute, cannot double split, but
322                  * make room for the attribute value itself.
323                  */
324                 uint    dblocks = xfs_attr3_rmt_blocks(mp, args->valuelen);
325                 nblks += dblocks;
326                 nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
327         }
328
329         return nblks;
330 }
331
332 /* Initialize transaction reservation for attr operations */
333 void
334 xfs_init_attr_trans(
335         struct xfs_da_args      *args,
336         struct xfs_trans_res    *tres,
337         unsigned int            *total)
338 {
339         struct xfs_mount        *mp = args->dp->i_mount;
340
341         if (args->value) {
342                 tres->tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
343                                  M_RES(mp)->tr_attrsetrt.tr_logres *
344                                  args->total;
345                 tres->tr_logcount = XFS_ATTRSET_LOG_COUNT;
346                 tres->tr_logflags = XFS_TRANS_PERM_LOG_RES;
347                 *total = args->total;
348         } else {
349                 *tres = M_RES(mp)->tr_attrrm;
350                 *total = XFS_ATTRRM_SPACE_RES(mp);
351         }
352 }
353
354 /*
355  * Add an attr to a shortform fork. If there is no space,
356  * xfs_attr_shortform_addname() will convert to leaf format and return -ENOSPC.
357  * to use.
358  */
359 STATIC int
360 xfs_attr_try_sf_addname(
361         struct xfs_inode        *dp,
362         struct xfs_da_args      *args)
363 {
364
365         int                     error;
366
367         /*
368          * Build initial attribute list (if required).
369          */
370         if (dp->i_af.if_format == XFS_DINODE_FMT_EXTENTS)
371                 xfs_attr_shortform_create(args);
372
373         error = xfs_attr_shortform_addname(args);
374         if (error == -ENOSPC)
375                 return error;
376
377         /*
378          * Commit the shortform mods, and we're done.
379          * NOTE: this is also the error path (EEXIST, etc).
380          */
381         if (!error)
382                 xfs_trans_ichgtime(args->trans, dp, XFS_ICHGTIME_CHG);
383
384         if (xfs_has_wsync(dp->i_mount))
385                 xfs_trans_set_sync(args->trans);
386
387         return error;
388 }
389
390 static int
391 xfs_attr_sf_addname(
392         struct xfs_attr_intent          *attr)
393 {
394         struct xfs_da_args              *args = attr->xattri_da_args;
395         struct xfs_inode                *dp = args->dp;
396         int                             error = 0;
397
398         error = xfs_attr_try_sf_addname(dp, args);
399         if (error != -ENOSPC) {
400                 ASSERT(!error || error == -EEXIST);
401                 attr->xattri_dela_state = XFS_DAS_DONE;
402                 goto out;
403         }
404
405         /*
406          * It won't fit in the shortform, transform to a leaf block.  GROT:
407          * another possible req'mt for a double-split btree op.
408          */
409         error = xfs_attr_shortform_to_leaf(args);
410         if (error)
411                 return error;
412
413         attr->xattri_dela_state = XFS_DAS_LEAF_ADD;
414 out:
415         trace_xfs_attr_sf_addname_return(attr->xattri_dela_state, args->dp);
416         return error;
417 }
418
419 /* Compute the hash value for a user/root/secure extended attribute */
420 xfs_dahash_t
421 xfs_attr_hashname(
422         const uint8_t           *name,
423         int                     namelen)
424 {
425         return xfs_da_hashname(name, namelen);
426 }
427
428 /* Compute the hash value for any extended attribute from any namespace. */
429 xfs_dahash_t
430 xfs_attr_hashval(
431         struct xfs_mount        *mp,
432         unsigned int            attr_flags,
433         const uint8_t           *name,
434         int                     namelen,
435         const void              *value,
436         int                     valuelen)
437 {
438         ASSERT(xfs_attr_check_namespace(attr_flags));
439
440         if (attr_flags & XFS_ATTR_PARENT)
441                 return xfs_parent_hashattr(mp, name, namelen, value, valuelen);
442
443         return xfs_attr_hashname(name, namelen);
444 }
445
446 /*
447  * PPTR_REPLACE operations require the caller to set the old and new names and
448  * values explicitly.  Update the canonical fields to the new name and value
449  * here now that the removal phase has finished.
450  */
451 static void
452 xfs_attr_update_pptr_replace_args(
453         struct xfs_da_args      *args)
454 {
455         ASSERT(args->new_namelen > 0);
456         args->name = args->new_name;
457         args->namelen = args->new_namelen;
458         args->value = args->new_value;
459         args->valuelen = args->new_valuelen;
460         xfs_attr_sethash(args);
461 }
462
463 /*
464  * Handle the state change on completion of a multi-state attr operation.
465  *
466  * If the XFS_DA_OP_REPLACE flag is set, this means the operation was the first
467  * modification in a attr replace operation and we still have to do the second
468  * state, indicated by @replace_state.
469  *
470  * We consume the XFS_DA_OP_REPLACE flag so that when we are called again on
471  * completion of the second half of the attr replace operation we correctly
472  * signal that it is done.
473  */
474 static enum xfs_delattr_state
475 xfs_attr_complete_op(
476         struct xfs_attr_intent  *attr,
477         enum xfs_delattr_state  replace_state)
478 {
479         struct xfs_da_args      *args = attr->xattri_da_args;
480
481         if (!(args->op_flags & XFS_DA_OP_REPLACE))
482                 replace_state = XFS_DAS_DONE;
483         else if (xfs_attr_intent_op(attr) == XFS_ATTRI_OP_FLAGS_PPTR_REPLACE)
484                 xfs_attr_update_pptr_replace_args(args);
485
486         args->op_flags &= ~XFS_DA_OP_REPLACE;
487         args->attr_filter &= ~XFS_ATTR_INCOMPLETE;
488         return replace_state;
489 }
490
491 static int
492 xfs_attr_leaf_addname(
493         struct xfs_attr_intent  *attr)
494 {
495         struct xfs_da_args      *args = attr->xattri_da_args;
496         int                     error;
497
498         ASSERT(xfs_attr_is_leaf(args->dp));
499
500         /*
501          * Use the leaf buffer we may already hold locked as a result of
502          * a sf-to-leaf conversion.
503          */
504         error = xfs_attr_leaf_try_add(args);
505
506         if (error == -ENOSPC) {
507                 error = xfs_attr3_leaf_to_node(args);
508                 if (error)
509                         return error;
510
511                 /*
512                  * We're not in leaf format anymore, so roll the transaction and
513                  * retry the add to the newly allocated node block.
514                  */
515                 attr->xattri_dela_state = XFS_DAS_NODE_ADD;
516                 goto out;
517         }
518         if (error)
519                 return error;
520
521         /*
522          * We need to commit and roll if we need to allocate remote xattr blocks
523          * or perform more xattr manipulations. Otherwise there is nothing more
524          * to do and we can return success.
525          */
526         if (args->rmtblkno)
527                 attr->xattri_dela_state = XFS_DAS_LEAF_SET_RMT;
528         else
529                 attr->xattri_dela_state = xfs_attr_complete_op(attr,
530                                                         XFS_DAS_LEAF_REPLACE);
531 out:
532         trace_xfs_attr_leaf_addname_return(attr->xattri_dela_state, args->dp);
533         return error;
534 }
535
536 /*
537  * Add an entry to a node format attr tree.
538  *
539  * Note that we might still have a leaf here - xfs_attr_is_leaf() cannot tell
540  * the difference between leaf + remote attr blocks and a node format tree,
541  * so we may still end up having to convert from leaf to node format here.
542  */
543 static int
544 xfs_attr_node_addname(
545         struct xfs_attr_intent  *attr)
546 {
547         struct xfs_da_args      *args = attr->xattri_da_args;
548         int                     error;
549
550         error = xfs_attr_node_addname_find_attr(attr);
551         if (error)
552                 return error;
553
554         error = xfs_attr_node_try_addname(attr);
555         if (error == -ENOSPC) {
556                 error = xfs_attr3_leaf_to_node(args);
557                 if (error)
558                         return error;
559                 /*
560                  * No state change, we really are in node form now
561                  * but we need the transaction rolled to continue.
562                  */
563                 goto out;
564         }
565         if (error)
566                 return error;
567
568         if (args->rmtblkno)
569                 attr->xattri_dela_state = XFS_DAS_NODE_SET_RMT;
570         else
571                 attr->xattri_dela_state = xfs_attr_complete_op(attr,
572                                                         XFS_DAS_NODE_REPLACE);
573 out:
574         trace_xfs_attr_node_addname_return(attr->xattri_dela_state, args->dp);
575         return error;
576 }
577
578 static int
579 xfs_attr_rmtval_alloc(
580         struct xfs_attr_intent          *attr)
581 {
582         struct xfs_da_args              *args = attr->xattri_da_args;
583         int                             error = 0;
584
585         /*
586          * If there was an out-of-line value, allocate the blocks we
587          * identified for its storage and copy the value.  This is done
588          * after we create the attribute so that we don't overflow the
589          * maximum size of a transaction and/or hit a deadlock.
590          */
591         if (attr->xattri_blkcnt > 0) {
592                 error = xfs_attr_rmtval_set_blk(attr);
593                 if (error)
594                         return error;
595                 /* Roll the transaction only if there is more to allocate. */
596                 if (attr->xattri_blkcnt > 0)
597                         goto out;
598         }
599
600         error = xfs_attr_rmtval_set_value(args);
601         if (error)
602                 return error;
603
604         attr->xattri_dela_state = xfs_attr_complete_op(attr,
605                                                 ++attr->xattri_dela_state);
606         /*
607          * If we are not doing a rename, we've finished the operation but still
608          * have to clear the incomplete flag protecting the new attr from
609          * exposing partially initialised state if we crash during creation.
610          */
611         if (attr->xattri_dela_state == XFS_DAS_DONE)
612                 error = xfs_attr3_leaf_clearflag(args);
613 out:
614         trace_xfs_attr_rmtval_alloc(attr->xattri_dela_state, args->dp);
615         return error;
616 }
617
618 /*
619  * Mark an attribute entry INCOMPLETE and save pointers to the relevant buffers
620  * for later deletion of the entry.
621  */
622 static int
623 xfs_attr_leaf_mark_incomplete(
624         struct xfs_da_args      *args,
625         struct xfs_da_state     *state)
626 {
627         int                     error;
628
629         /*
630          * Fill in disk block numbers in the state structure
631          * so that we can get the buffers back after we commit
632          * several transactions in the following calls.
633          */
634         error = xfs_attr_fillstate(state);
635         if (error)
636                 return error;
637
638         /*
639          * Mark the attribute as INCOMPLETE
640          */
641         return xfs_attr3_leaf_setflag(args);
642 }
643
644 /* Ensure the da state of an xattr deferred work item is ready to go. */
645 static inline void
646 xfs_attr_item_init_da_state(
647         struct xfs_attr_intent  *attr)
648 {
649         struct xfs_da_args      *args = attr->xattri_da_args;
650
651         if (!attr->xattri_da_state)
652                 attr->xattri_da_state = xfs_da_state_alloc(args);
653         else
654                 xfs_da_state_reset(attr->xattri_da_state, args);
655 }
656
657 /*
658  * Initial setup for xfs_attr_node_removename.  Make sure the attr is there and
659  * the blocks are valid.  Attr keys with remote blocks will be marked
660  * incomplete.
661  */
662 static
663 int xfs_attr_node_removename_setup(
664         struct xfs_attr_intent          *attr)
665 {
666         struct xfs_da_args              *args = attr->xattri_da_args;
667         struct xfs_da_state             *state;
668         int                             error;
669
670         xfs_attr_item_init_da_state(attr);
671         error = xfs_attr_node_lookup(args, attr->xattri_da_state);
672         if (error != -EEXIST)
673                 goto out;
674         error = 0;
675
676         state = attr->xattri_da_state;
677         ASSERT(state->path.blk[state->path.active - 1].bp != NULL);
678         ASSERT(state->path.blk[state->path.active - 1].magic ==
679                 XFS_ATTR_LEAF_MAGIC);
680
681         error = xfs_attr_leaf_mark_incomplete(args, state);
682         if (error)
683                 goto out;
684         if (args->rmtblkno > 0)
685                 error = xfs_attr_rmtval_invalidate(args);
686 out:
687         if (error) {
688                 xfs_da_state_free(attr->xattri_da_state);
689                 attr->xattri_da_state = NULL;
690         }
691
692         return error;
693 }
694
695 /*
696  * Remove the original attr we have just replaced. This is dependent on the
697  * original lookup and insert placing the old attr in args->blkno/args->index
698  * and the new attr in args->blkno2/args->index2.
699  */
700 static int
701 xfs_attr_leaf_remove_attr(
702         struct xfs_attr_intent          *attr)
703 {
704         struct xfs_da_args              *args = attr->xattri_da_args;
705         struct xfs_inode                *dp = args->dp;
706         struct xfs_buf                  *bp = NULL;
707         int                             forkoff;
708         int                             error;
709
710         error = xfs_attr3_leaf_read(args->trans, args->dp, args->owner,
711                         args->blkno, &bp);
712         if (error)
713                 return error;
714
715         xfs_attr3_leaf_remove(bp, args);
716
717         forkoff = xfs_attr_shortform_allfit(bp, dp);
718         if (forkoff)
719                 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
720                 /* bp is gone due to xfs_da_shrink_inode */
721
722         return error;
723 }
724
725 /*
726  * Shrink an attribute from leaf to shortform. Used by the node format remove
727  * path when the node format collapses to a single block and so we have to check
728  * if it can be collapsed further.
729  */
730 static int
731 xfs_attr_leaf_shrink(
732         struct xfs_da_args      *args)
733 {
734         struct xfs_inode        *dp = args->dp;
735         struct xfs_buf          *bp;
736         int                     forkoff;
737         int                     error;
738
739         if (!xfs_attr_is_leaf(dp))
740                 return 0;
741
742         error = xfs_attr3_leaf_read(args->trans, args->dp, args->owner, 0, &bp);
743         if (error)
744                 return error;
745
746         forkoff = xfs_attr_shortform_allfit(bp, dp);
747         if (forkoff) {
748                 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
749                 /* bp is gone due to xfs_da_shrink_inode */
750         } else {
751                 xfs_trans_brelse(args->trans, bp);
752         }
753
754         return error;
755 }
756
757 /*
758  * Run the attribute operation specified in @attr.
759  *
760  * This routine is meant to function as a delayed operation and will set the
761  * state to XFS_DAS_DONE when the operation is complete.  Calling functions will
762  * need to handle this, and recall the function until either an error or
763  * XFS_DAS_DONE is detected.
764  */
765 int
766 xfs_attr_set_iter(
767         struct xfs_attr_intent          *attr)
768 {
769         struct xfs_da_args              *args = attr->xattri_da_args;
770         int                             error = 0;
771
772         /* State machine switch */
773 next_state:
774         switch (attr->xattri_dela_state) {
775         case XFS_DAS_UNINIT:
776                 ASSERT(0);
777                 return -EFSCORRUPTED;
778         case XFS_DAS_SF_ADD:
779                 return xfs_attr_sf_addname(attr);
780         case XFS_DAS_LEAF_ADD:
781                 return xfs_attr_leaf_addname(attr);
782         case XFS_DAS_NODE_ADD:
783                 return xfs_attr_node_addname(attr);
784
785         case XFS_DAS_SF_REMOVE:
786                 error = xfs_attr_sf_removename(args);
787                 attr->xattri_dela_state = xfs_attr_complete_op(attr,
788                                                 xfs_attr_init_add_state(args));
789                 break;
790         case XFS_DAS_LEAF_REMOVE:
791                 error = xfs_attr_leaf_removename(args);
792                 attr->xattri_dela_state = xfs_attr_complete_op(attr,
793                                                 xfs_attr_init_add_state(args));
794                 break;
795         case XFS_DAS_NODE_REMOVE:
796                 error = xfs_attr_node_removename_setup(attr);
797                 if (error == -ENOATTR &&
798                     (args->op_flags & XFS_DA_OP_RECOVERY)) {
799                         attr->xattri_dela_state = xfs_attr_complete_op(attr,
800                                                 xfs_attr_init_add_state(args));
801                         error = 0;
802                         break;
803                 }
804                 if (error)
805                         return error;
806                 attr->xattri_dela_state = XFS_DAS_NODE_REMOVE_RMT;
807                 if (args->rmtblkno == 0)
808                         attr->xattri_dela_state++;
809                 break;
810
811         case XFS_DAS_LEAF_SET_RMT:
812         case XFS_DAS_NODE_SET_RMT:
813                 error = xfs_attr_rmtval_find_space(attr);
814                 if (error)
815                         return error;
816                 attr->xattri_dela_state++;
817                 fallthrough;
818
819         case XFS_DAS_LEAF_ALLOC_RMT:
820         case XFS_DAS_NODE_ALLOC_RMT:
821                 error = xfs_attr_rmtval_alloc(attr);
822                 if (error)
823                         return error;
824                 if (attr->xattri_dela_state == XFS_DAS_DONE)
825                         break;
826                 goto next_state;
827
828         case XFS_DAS_LEAF_REPLACE:
829         case XFS_DAS_NODE_REPLACE:
830                 /*
831                  * We must "flip" the incomplete flags on the "new" and "old"
832                  * attribute/value pairs so that one disappears and one appears
833                  * atomically.
834                  */
835                 error = xfs_attr3_leaf_flipflags(args);
836                 if (error)
837                         return error;
838                 /*
839                  * We must commit the flag value change now to make it atomic
840                  * and then we can start the next trans in series at REMOVE_OLD.
841                  */
842                 attr->xattri_dela_state++;
843                 break;
844
845         case XFS_DAS_LEAF_REMOVE_OLD:
846         case XFS_DAS_NODE_REMOVE_OLD:
847                 /*
848                  * If we have a remote attr, start the process of removing it
849                  * by invalidating any cached buffers.
850                  *
851                  * If we don't have a remote attr, we skip the remote block
852                  * removal state altogether with a second state increment.
853                  */
854                 xfs_attr_restore_rmt_blk(args);
855                 if (args->rmtblkno) {
856                         error = xfs_attr_rmtval_invalidate(args);
857                         if (error)
858                                 return error;
859                 } else {
860                         attr->xattri_dela_state++;
861                 }
862
863                 attr->xattri_dela_state++;
864                 goto next_state;
865
866         case XFS_DAS_LEAF_REMOVE_RMT:
867         case XFS_DAS_NODE_REMOVE_RMT:
868                 error = xfs_attr_rmtval_remove(attr);
869                 if (error == -EAGAIN) {
870                         error = 0;
871                         break;
872                 }
873                 if (error)
874                         return error;
875
876                 /*
877                  * We've finished removing the remote attr blocks, so commit the
878                  * transaction and move on to removing the attr name from the
879                  * leaf/node block. Removing the attr might require a full
880                  * transaction reservation for btree block freeing, so we
881                  * can't do that in the same transaction where we removed the
882                  * remote attr blocks.
883                  */
884                 attr->xattri_dela_state++;
885                 break;
886
887         case XFS_DAS_LEAF_REMOVE_ATTR:
888                 error = xfs_attr_leaf_remove_attr(attr);
889                 attr->xattri_dela_state = xfs_attr_complete_op(attr,
890                                                 xfs_attr_init_add_state(args));
891                 break;
892
893         case XFS_DAS_NODE_REMOVE_ATTR:
894                 error = xfs_attr_node_remove_attr(attr);
895                 if (!error)
896                         error = xfs_attr_leaf_shrink(args);
897                 attr->xattri_dela_state = xfs_attr_complete_op(attr,
898                                                 xfs_attr_init_add_state(args));
899                 break;
900         default:
901                 ASSERT(0);
902                 break;
903         }
904
905         trace_xfs_attr_set_iter_return(attr->xattri_dela_state, args->dp);
906         return error;
907 }
908
909
910 /*
911  * Return EEXIST if attr is found, or ENOATTR if not
912  */
913 static int
914 xfs_attr_lookup(
915         struct xfs_da_args      *args)
916 {
917         struct xfs_inode        *dp = args->dp;
918         struct xfs_buf          *bp = NULL;
919         struct xfs_da_state     *state;
920         int                     error;
921
922         if (!xfs_inode_hasattr(dp))
923                 return -ENOATTR;
924
925         if (dp->i_af.if_format == XFS_DINODE_FMT_LOCAL) {
926                 if (xfs_attr_sf_findname(args))
927                         return -EEXIST;
928                 return -ENOATTR;
929         }
930
931         /* Prerequisite for xfs_attr_is_leaf */
932         error = xfs_iread_extents(args->trans, args->dp, XFS_ATTR_FORK);
933         if (error)
934                 return error;
935
936         if (xfs_attr_is_leaf(dp)) {
937                 error = xfs_attr_leaf_hasname(args, &bp);
938
939                 if (bp)
940                         xfs_trans_brelse(args->trans, bp);
941
942                 return error;
943         }
944
945         state = xfs_da_state_alloc(args);
946         error = xfs_attr_node_lookup(args, state);
947         xfs_da_state_free(state);
948         return error;
949 }
950
951 int
952 xfs_attr_add_fork(
953         struct xfs_inode        *ip,            /* incore inode pointer */
954         int                     size,           /* space new attribute needs */
955         int                     rsvd)           /* xact may use reserved blks */
956 {
957         struct xfs_mount        *mp = ip->i_mount;
958         struct xfs_trans        *tp;            /* transaction pointer */
959         unsigned int            blks;           /* space reservation */
960         int                     error;          /* error return value */
961
962         ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
963
964         blks = XFS_ADDAFORK_SPACE_RES(mp);
965
966         error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_addafork, blks, 0,
967                         rsvd, &tp);
968         if (error)
969                 return error;
970
971         if (xfs_inode_has_attr_fork(ip))
972                 goto trans_cancel;
973
974         error = xfs_bmap_add_attrfork(tp, ip, size, rsvd);
975         if (error)
976                 goto trans_cancel;
977
978         error = xfs_trans_commit(tp);
979         xfs_iunlock(ip, XFS_ILOCK_EXCL);
980         return error;
981
982 trans_cancel:
983         xfs_trans_cancel(tp);
984         xfs_iunlock(ip, XFS_ILOCK_EXCL);
985         return error;
986 }
987
988 /*
989  * Make a change to the xattr structure.
990  *
991  * The caller must have initialized @args, attached dquots, and must not hold
992  * any ILOCKs.  Reserved data blocks may be used if @rsvd is set.
993  *
994  * Returns -EEXIST for XFS_ATTRUPDATE_CREATE if the name already exists.
995  * Returns -ENOATTR for XFS_ATTRUPDATE_REMOVE if the name does not exist.
996  * Returns 0 on success, or a negative errno if something else went wrong.
997  */
998 int
999 xfs_attr_set(
1000         struct xfs_da_args      *args,
1001         enum xfs_attr_update    op,
1002         bool                    rsvd)
1003 {
1004         struct xfs_inode        *dp = args->dp;
1005         struct xfs_mount        *mp = dp->i_mount;
1006         struct xfs_trans_res    tres;
1007         int                     error, local;
1008         int                     rmt_blks = 0;
1009         unsigned int            total;
1010
1011         ASSERT(!args->trans);
1012
1013         switch (op) {
1014         case XFS_ATTRUPDATE_UPSERT:
1015         case XFS_ATTRUPDATE_CREATE:
1016         case XFS_ATTRUPDATE_REPLACE:
1017                 XFS_STATS_INC(mp, xs_attr_set);
1018                 args->total = xfs_attr_calc_size(args, &local);
1019
1020                 /*
1021                  * If the inode doesn't have an attribute fork, add one.
1022                  * (inode must not be locked when we call this routine)
1023                  */
1024                 if (xfs_inode_has_attr_fork(dp) == 0) {
1025                         int sf_size = sizeof(struct xfs_attr_sf_hdr) +
1026                                 xfs_attr_sf_entsize_byname(args->namelen,
1027                                                 args->valuelen);
1028
1029                         error = xfs_attr_add_fork(dp, sf_size, rsvd);
1030                         if (error)
1031                                 return error;
1032                 }
1033
1034                 if (!local)
1035                         rmt_blks = xfs_attr3_rmt_blocks(mp, args->valuelen);
1036                 break;
1037         case XFS_ATTRUPDATE_REMOVE:
1038                 XFS_STATS_INC(mp, xs_attr_remove);
1039                 rmt_blks = xfs_attr3_max_rmt_blocks(mp);
1040                 break;
1041         }
1042
1043         /*
1044          * Root fork attributes can use reserved data blocks for this
1045          * operation if necessary
1046          */
1047         xfs_init_attr_trans(args, &tres, &total);
1048         error = xfs_trans_alloc_inode(dp, &tres, total, 0, rsvd, &args->trans);
1049         if (error)
1050                 return error;
1051
1052         if (op != XFS_ATTRUPDATE_REMOVE || xfs_inode_hasattr(dp)) {
1053                 error = xfs_iext_count_extend(args->trans, dp, XFS_ATTR_FORK,
1054                                 XFS_IEXT_ATTR_MANIP_CNT(rmt_blks));
1055                 if (error)
1056                         goto out_trans_cancel;
1057         }
1058
1059         error = xfs_attr_lookup(args);
1060         switch (error) {
1061         case -EEXIST:
1062                 if (op == XFS_ATTRUPDATE_REMOVE) {
1063                         /* if no value, we are performing a remove operation */
1064                         xfs_attr_defer_add(args, XFS_ATTR_DEFER_REMOVE);
1065                         break;
1066                 }
1067
1068                 /* Pure create fails if the attr already exists */
1069                 if (op == XFS_ATTRUPDATE_CREATE)
1070                         goto out_trans_cancel;
1071                 xfs_attr_defer_add(args, XFS_ATTR_DEFER_REPLACE);
1072                 break;
1073         case -ENOATTR:
1074                 /* Can't remove what isn't there. */
1075                 if (op == XFS_ATTRUPDATE_REMOVE)
1076                         goto out_trans_cancel;
1077
1078                 /* Pure replace fails if no existing attr to replace. */
1079                 if (op == XFS_ATTRUPDATE_REPLACE)
1080                         goto out_trans_cancel;
1081                 xfs_attr_defer_add(args, XFS_ATTR_DEFER_SET);
1082                 break;
1083         default:
1084                 goto out_trans_cancel;
1085         }
1086
1087         /*
1088          * If this is a synchronous mount, make sure that the
1089          * transaction goes to disk before returning to the user.
1090          */
1091         if (xfs_has_wsync(mp))
1092                 xfs_trans_set_sync(args->trans);
1093
1094         xfs_trans_ichgtime(args->trans, dp, XFS_ICHGTIME_CHG);
1095
1096         /*
1097          * Commit the last in the sequence of transactions.
1098          */
1099         xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
1100         error = xfs_trans_commit(args->trans);
1101 out_unlock:
1102         xfs_iunlock(dp, XFS_ILOCK_EXCL);
1103         args->trans = NULL;
1104         return error;
1105
1106 out_trans_cancel:
1107         if (args->trans)
1108                 xfs_trans_cancel(args->trans);
1109         goto out_unlock;
1110 }
1111
1112 /*========================================================================
1113  * External routines when attribute list is inside the inode
1114  *========================================================================*/
1115
1116 int xfs_attr_sf_totsize(struct xfs_inode *dp)
1117 {
1118         struct xfs_attr_sf_hdr *sf = dp->i_af.if_data;
1119
1120         return be16_to_cpu(sf->totsize);
1121 }
1122
1123 /*
1124  * Add a name to the shortform attribute list structure
1125  * This is the external routine.
1126  */
1127 static int
1128 xfs_attr_shortform_addname(
1129         struct xfs_da_args      *args)
1130 {
1131         int                     newsize, forkoff;
1132
1133         trace_xfs_attr_sf_addname(args);
1134
1135         if (xfs_attr_sf_findname(args)) {
1136                 int             error;
1137
1138                 ASSERT(args->op_flags & XFS_DA_OP_REPLACE);
1139
1140                 error = xfs_attr_sf_removename(args);
1141                 if (error)
1142                         return error;
1143
1144                 /*
1145                  * Since we have removed the old attr, clear XFS_DA_OP_REPLACE
1146                  * so that the new attr doesn't fit in shortform format, the
1147                  * leaf format add routine won't trip over the attr not being
1148                  * around.
1149                  */
1150                 args->op_flags &= ~XFS_DA_OP_REPLACE;
1151         } else {
1152                 ASSERT(!(args->op_flags & XFS_DA_OP_REPLACE));
1153         }
1154
1155         if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
1156             args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
1157                 return -ENOSPC;
1158
1159         newsize = xfs_attr_sf_totsize(args->dp);
1160         newsize += xfs_attr_sf_entsize_byname(args->namelen, args->valuelen);
1161
1162         forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
1163         if (!forkoff)
1164                 return -ENOSPC;
1165
1166         xfs_attr_shortform_add(args, forkoff);
1167         return 0;
1168 }
1169
1170
1171 /*========================================================================
1172  * External routines when attribute list is one block
1173  *========================================================================*/
1174
1175 /* Save the current remote block info and clear the current pointers. */
1176 static void
1177 xfs_attr_save_rmt_blk(
1178         struct xfs_da_args      *args)
1179 {
1180         args->blkno2 = args->blkno;
1181         args->index2 = args->index;
1182         args->rmtblkno2 = args->rmtblkno;
1183         args->rmtblkcnt2 = args->rmtblkcnt;
1184         args->rmtvaluelen2 = args->rmtvaluelen;
1185         args->rmtblkno = 0;
1186         args->rmtblkcnt = 0;
1187         args->rmtvaluelen = 0;
1188 }
1189
1190 /* Set stored info about a remote block */
1191 static void
1192 xfs_attr_restore_rmt_blk(
1193         struct xfs_da_args      *args)
1194 {
1195         args->blkno = args->blkno2;
1196         args->index = args->index2;
1197         args->rmtblkno = args->rmtblkno2;
1198         args->rmtblkcnt = args->rmtblkcnt2;
1199         args->rmtvaluelen = args->rmtvaluelen2;
1200 }
1201
1202 /*
1203  * Tries to add an attribute to an inode in leaf form
1204  *
1205  * This function is meant to execute as part of a delayed operation and leaves
1206  * the transaction handling to the caller.  On success the attribute is added
1207  * and the inode and transaction are left dirty.  If there is not enough space,
1208  * the attr data is converted to node format and -ENOSPC is returned. Caller is
1209  * responsible for handling the dirty inode and transaction or adding the attr
1210  * in node format.
1211  */
1212 STATIC int
1213 xfs_attr_leaf_try_add(
1214         struct xfs_da_args      *args)
1215 {
1216         struct xfs_buf          *bp;
1217         int                     error;
1218
1219         error = xfs_attr3_leaf_read(args->trans, args->dp, args->owner, 0, &bp);
1220         if (error)
1221                 return error;
1222
1223         /*
1224          * Look up the xattr name to set the insertion point for the new xattr.
1225          */
1226         error = xfs_attr3_leaf_lookup_int(bp, args);
1227         switch (error) {
1228         case -ENOATTR:
1229                 if (args->op_flags & XFS_DA_OP_REPLACE)
1230                         goto out_brelse;
1231                 break;
1232         case -EEXIST:
1233                 if (!(args->op_flags & XFS_DA_OP_REPLACE))
1234                         goto out_brelse;
1235
1236                 trace_xfs_attr_leaf_replace(args);
1237                 /*
1238                  * Save the existing remote attr state so that the current
1239                  * values reflect the state of the new attribute we are about to
1240                  * add, not the attribute we just found and will remove later.
1241                  */
1242                 xfs_attr_save_rmt_blk(args);
1243                 break;
1244         case 0:
1245                 break;
1246         default:
1247                 goto out_brelse;
1248         }
1249
1250         return xfs_attr3_leaf_add(bp, args);
1251
1252 out_brelse:
1253         xfs_trans_brelse(args->trans, bp);
1254         return error;
1255 }
1256
1257 /*
1258  * Return EEXIST if attr is found, or ENOATTR if not
1259  */
1260 STATIC int
1261 xfs_attr_leaf_hasname(
1262         struct xfs_da_args      *args,
1263         struct xfs_buf          **bp)
1264 {
1265         int                     error = 0;
1266
1267         error = xfs_attr3_leaf_read(args->trans, args->dp, args->owner, 0, bp);
1268         if (error)
1269                 return error;
1270
1271         error = xfs_attr3_leaf_lookup_int(*bp, args);
1272         if (error != -ENOATTR && error != -EEXIST)
1273                 xfs_trans_brelse(args->trans, *bp);
1274
1275         return error;
1276 }
1277
1278 /*
1279  * Remove a name from the leaf attribute list structure
1280  *
1281  * This leaf block cannot have a "remote" value, we only call this routine
1282  * if bmap_one_block() says there is only one block (ie: no remote blks).
1283  */
1284 STATIC int
1285 xfs_attr_leaf_removename(
1286         struct xfs_da_args      *args)
1287 {
1288         struct xfs_inode        *dp;
1289         struct xfs_buf          *bp;
1290         int                     error, forkoff;
1291
1292         trace_xfs_attr_leaf_removename(args);
1293
1294         /*
1295          * Remove the attribute.
1296          */
1297         dp = args->dp;
1298
1299         error = xfs_attr_leaf_hasname(args, &bp);
1300         if (error == -ENOATTR) {
1301                 xfs_trans_brelse(args->trans, bp);
1302                 if (args->op_flags & XFS_DA_OP_RECOVERY)
1303                         return 0;
1304                 return error;
1305         } else if (error != -EEXIST)
1306                 return error;
1307
1308         xfs_attr3_leaf_remove(bp, args);
1309
1310         /*
1311          * If the result is small enough, shrink it all into the inode.
1312          */
1313         forkoff = xfs_attr_shortform_allfit(bp, dp);
1314         if (forkoff)
1315                 return xfs_attr3_leaf_to_shortform(bp, args, forkoff);
1316                 /* bp is gone due to xfs_da_shrink_inode */
1317
1318         return 0;
1319 }
1320
1321 /*
1322  * Look up a name in a leaf attribute list structure.
1323  *
1324  * This leaf block cannot have a "remote" value, we only call this routine
1325  * if bmap_one_block() says there is only one block (ie: no remote blks).
1326  *
1327  * Returns 0 on successful retrieval, otherwise an error.
1328  */
1329 STATIC int
1330 xfs_attr_leaf_get(xfs_da_args_t *args)
1331 {
1332         struct xfs_buf *bp;
1333         int error;
1334
1335         trace_xfs_attr_leaf_get(args);
1336
1337         error = xfs_attr_leaf_hasname(args, &bp);
1338
1339         if (error == -ENOATTR)  {
1340                 xfs_trans_brelse(args->trans, bp);
1341                 return error;
1342         } else if (error != -EEXIST)
1343                 return error;
1344
1345
1346         error = xfs_attr3_leaf_getvalue(bp, args);
1347         xfs_trans_brelse(args->trans, bp);
1348         return error;
1349 }
1350
1351 /* Return EEXIST if attr is found, or ENOATTR if not. */
1352 STATIC int
1353 xfs_attr_node_lookup(
1354         struct xfs_da_args      *args,
1355         struct xfs_da_state     *state)
1356 {
1357         int                     retval, error;
1358
1359         /*
1360          * Search to see if name exists, and get back a pointer to it.
1361          */
1362         error = xfs_da3_node_lookup_int(state, &retval);
1363         if (error)
1364                 return error;
1365
1366         return retval;
1367 }
1368
1369 /*========================================================================
1370  * External routines when attribute list size > geo->blksize
1371  *========================================================================*/
1372
1373 STATIC int
1374 xfs_attr_node_addname_find_attr(
1375          struct xfs_attr_intent *attr)
1376 {
1377         struct xfs_da_args      *args = attr->xattri_da_args;
1378         int                     error;
1379
1380         /*
1381          * Search to see if name already exists, and get back a pointer
1382          * to where it should go.
1383          */
1384         xfs_attr_item_init_da_state(attr);
1385         error = xfs_attr_node_lookup(args, attr->xattri_da_state);
1386         switch (error) {
1387         case -ENOATTR:
1388                 if (args->op_flags & XFS_DA_OP_REPLACE)
1389                         goto error;
1390                 break;
1391         case -EEXIST:
1392                 if (!(args->op_flags & XFS_DA_OP_REPLACE))
1393                         goto error;
1394
1395
1396                 trace_xfs_attr_node_replace(args);
1397                 /*
1398                  * Save the existing remote attr state so that the current
1399                  * values reflect the state of the new attribute we are about to
1400                  * add, not the attribute we just found and will remove later.
1401                  */
1402                 xfs_attr_save_rmt_blk(args);
1403                 break;
1404         case 0:
1405                 break;
1406         default:
1407                 goto error;
1408         }
1409
1410         return 0;
1411 error:
1412         if (attr->xattri_da_state) {
1413                 xfs_da_state_free(attr->xattri_da_state);
1414                 attr->xattri_da_state = NULL;
1415         }
1416         return error;
1417 }
1418
1419 /*
1420  * Add a name to a Btree-format attribute list.
1421  *
1422  * This will involve walking down the Btree, and may involve splitting
1423  * leaf nodes and even splitting intermediate nodes up to and including
1424  * the root node (a special case of an intermediate node).
1425  */
1426 static int
1427 xfs_attr_node_try_addname(
1428         struct xfs_attr_intent          *attr)
1429 {
1430         struct xfs_da_state             *state = attr->xattri_da_state;
1431         struct xfs_da_state_blk         *blk;
1432         int                             error;
1433
1434         trace_xfs_attr_node_addname(state->args);
1435
1436         blk = &state->path.blk[state->path.active-1];
1437         ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1438
1439         error = xfs_attr3_leaf_add(blk->bp, state->args);
1440         if (error == -ENOSPC) {
1441                 if (state->path.active == 1) {
1442                         /*
1443                          * Its really a single leaf node, but it had
1444                          * out-of-line values so it looked like it *might*
1445                          * have been a b-tree. Let the caller deal with this.
1446                          */
1447                         goto out;
1448                 }
1449
1450                 /*
1451                  * Split as many Btree elements as required.
1452                  * This code tracks the new and old attr's location
1453                  * in the index/blkno/rmtblkno/rmtblkcnt fields and
1454                  * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
1455                  */
1456                 error = xfs_da3_split(state);
1457                 if (error)
1458                         goto out;
1459         } else {
1460                 /*
1461                  * Addition succeeded, update Btree hashvals.
1462                  */
1463                 xfs_da3_fixhashpath(state, &state->path);
1464         }
1465
1466 out:
1467         xfs_da_state_free(state);
1468         attr->xattri_da_state = NULL;
1469         return error;
1470 }
1471
1472 static int
1473 xfs_attr_node_removename(
1474         struct xfs_da_args      *args,
1475         struct xfs_da_state     *state)
1476 {
1477         struct xfs_da_state_blk *blk;
1478         int                     retval;
1479
1480         /*
1481          * Remove the name and update the hashvals in the tree.
1482          */
1483         blk = &state->path.blk[state->path.active-1];
1484         ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1485         retval = xfs_attr3_leaf_remove(blk->bp, args);
1486         xfs_da3_fixhashpath(state, &state->path);
1487
1488         return retval;
1489 }
1490
1491 static int
1492 xfs_attr_node_remove_attr(
1493         struct xfs_attr_intent          *attr)
1494 {
1495         struct xfs_da_args              *args = attr->xattri_da_args;
1496         struct xfs_da_state             *state = xfs_da_state_alloc(args);
1497         int                             retval = 0;
1498         int                             error = 0;
1499
1500         /*
1501          * The attr we are removing has already been marked incomplete, so
1502          * we need to set the filter appropriately to re-find the "old"
1503          * attribute entry after any split ops.
1504          */
1505         args->attr_filter |= XFS_ATTR_INCOMPLETE;
1506         error = xfs_da3_node_lookup_int(state, &retval);
1507         if (error)
1508                 goto out;
1509
1510         error = xfs_attr_node_removename(args, state);
1511
1512         /*
1513          * Check to see if the tree needs to be collapsed.
1514          */
1515         if (retval && (state->path.active > 1)) {
1516                 error = xfs_da3_join(state);
1517                 if (error)
1518                         goto out;
1519         }
1520         retval = error = 0;
1521
1522 out:
1523         xfs_da_state_free(state);
1524         if (error)
1525                 return error;
1526         return retval;
1527 }
1528
1529 /*
1530  * Retrieve the attribute data from a node attribute list.
1531  *
1532  * This routine gets called for any attribute fork that has more than one
1533  * block, ie: both true Btree attr lists and for single-leaf-blocks with
1534  * "remote" values taking up more blocks.
1535  *
1536  * Returns 0 on successful retrieval, otherwise an error.
1537  */
1538 STATIC int
1539 xfs_attr_node_get(
1540         struct xfs_da_args      *args)
1541 {
1542         struct xfs_da_state     *state;
1543         struct xfs_da_state_blk *blk;
1544         int                     i;
1545         int                     error;
1546
1547         trace_xfs_attr_node_get(args);
1548
1549         /*
1550          * Search to see if name exists, and get back a pointer to it.
1551          */
1552         state = xfs_da_state_alloc(args);
1553         error = xfs_attr_node_lookup(args, state);
1554         if (error != -EEXIST)
1555                 goto out_release;
1556
1557         /*
1558          * Get the value, local or "remote"
1559          */
1560         blk = &state->path.blk[state->path.active - 1];
1561         error = xfs_attr3_leaf_getvalue(blk->bp, args);
1562
1563         /*
1564          * If not in a transaction, we have to release all the buffers.
1565          */
1566 out_release:
1567         for (i = 0; i < state->path.active; i++) {
1568                 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
1569                 state->path.blk[i].bp = NULL;
1570         }
1571
1572         xfs_da_state_free(state);
1573         return error;
1574 }
1575
1576 /* Enforce that there is at most one namespace bit per attr. */
1577 inline bool xfs_attr_check_namespace(unsigned int attr_flags)
1578 {
1579         return hweight32(attr_flags & XFS_ATTR_NSP_ONDISK_MASK) < 2;
1580 }
1581
1582 /* Returns true if the attribute entry name is valid. */
1583 bool
1584 xfs_attr_namecheck(
1585         unsigned int    attr_flags,
1586         const void      *name,
1587         size_t          length)
1588 {
1589         /* Only one namespace bit allowed. */
1590         if (!xfs_attr_check_namespace(attr_flags))
1591                 return false;
1592
1593         /*
1594          * MAXNAMELEN includes the trailing null, but (name/length) leave it
1595          * out, so use >= for the length check.
1596          */
1597         if (length >= MAXNAMELEN)
1598                 return false;
1599
1600         /* Parent pointers have their own validation. */
1601         if (attr_flags & XFS_ATTR_PARENT)
1602                 return xfs_parent_namecheck(attr_flags, name, length);
1603
1604         /* There shouldn't be any nulls here */
1605         return !memchr(name, 0, length);
1606 }
1607
1608 int __init
1609 xfs_attr_intent_init_cache(void)
1610 {
1611         xfs_attr_intent_cache = kmem_cache_create("xfs_attr_intent",
1612                         sizeof(struct xfs_attr_intent),
1613                         0, 0, NULL);
1614
1615         return xfs_attr_intent_cache != NULL ? 0 : -ENOMEM;
1616 }
1617
1618 void
1619 xfs_attr_intent_destroy_cache(void)
1620 {
1621         kmem_cache_destroy(xfs_attr_intent_cache);
1622         xfs_attr_intent_cache = NULL;
1623 }
This page took 0.124321 seconds and 4 git commands to generate.