xfs: Add helper xfs_attr_node_addname_find_attr
[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
28 /*
29  * xfs_attr.c
30  *
31  * Provide the external interfaces to manage attribute lists.
32  */
33
34 /*========================================================================
35  * Function prototypes for the kernel.
36  *========================================================================*/
37
38 /*
39  * Internal routines when attribute list fits inside the inode.
40  */
41 STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
42
43 /*
44  * Internal routines when attribute list is one block.
45  */
46 STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
47 STATIC int xfs_attr_leaf_addname(xfs_da_args_t *args);
48 STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
49 STATIC int xfs_attr_leaf_hasname(struct xfs_da_args *args, struct xfs_buf **bp);
50
51 /*
52  * Internal routines when attribute list is more than one block.
53  */
54 STATIC int xfs_attr_node_get(xfs_da_args_t *args);
55 STATIC int xfs_attr_node_addname(struct xfs_da_args *args,
56                                  struct xfs_da_state *state);
57 STATIC int xfs_attr_node_addname_find_attr(struct xfs_da_args *args,
58                                  struct xfs_da_state **state);
59 STATIC int xfs_attr_node_removename(xfs_da_args_t *args);
60 STATIC int xfs_attr_node_addname_clear_incomplete(struct xfs_da_args *args);
61 STATIC int xfs_attr_node_hasname(xfs_da_args_t *args,
62                                  struct xfs_da_state **state);
63 STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
64 STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
65
66 int
67 xfs_inode_hasattr(
68         struct xfs_inode        *ip)
69 {
70         if (!XFS_IFORK_Q(ip) ||
71             (ip->i_afp->if_format == XFS_DINODE_FMT_EXTENTS &&
72              ip->i_afp->if_nextents == 0))
73                 return 0;
74         return 1;
75 }
76
77 /*
78  * Returns true if the there is exactly only block in the attr fork, in which
79  * case the attribute fork consists of a single leaf block entry.
80  */
81 bool
82 xfs_attr_is_leaf(
83         struct xfs_inode        *ip)
84 {
85         struct xfs_ifork        *ifp = ip->i_afp;
86         struct xfs_iext_cursor  icur;
87         struct xfs_bmbt_irec    imap;
88
89         if (ifp->if_nextents != 1 || ifp->if_format != XFS_DINODE_FMT_EXTENTS)
90                 return false;
91
92         xfs_iext_first(ifp, &icur);
93         xfs_iext_get_extent(ifp, &icur, &imap);
94         return imap.br_startoff == 0 && imap.br_blockcount == 1;
95 }
96
97 /*========================================================================
98  * Overall external interface routines.
99  *========================================================================*/
100
101 /*
102  * Retrieve an extended attribute and its value.  Must have ilock.
103  * Returns 0 on successful retrieval, otherwise an error.
104  */
105 int
106 xfs_attr_get_ilocked(
107         struct xfs_da_args      *args)
108 {
109         ASSERT(xfs_isilocked(args->dp, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
110
111         if (!xfs_inode_hasattr(args->dp))
112                 return -ENOATTR;
113
114         if (args->dp->i_afp->if_format == XFS_DINODE_FMT_LOCAL)
115                 return xfs_attr_shortform_getvalue(args);
116         if (xfs_attr_is_leaf(args->dp))
117                 return xfs_attr_leaf_get(args);
118         return xfs_attr_node_get(args);
119 }
120
121 /*
122  * Retrieve an extended attribute by name, and its value if requested.
123  *
124  * If args->valuelen is zero, then the caller does not want the value, just an
125  * indication whether the attribute exists and the size of the value if it
126  * exists. The size is returned in args.valuelen.
127  *
128  * If args->value is NULL but args->valuelen is non-zero, allocate the buffer
129  * for the value after existence of the attribute has been determined. The
130  * caller always has to free args->value if it is set, no matter if this
131  * function was successful or not.
132  *
133  * If the attribute is found, but exceeds the size limit set by the caller in
134  * args->valuelen, return -ERANGE with the size of the attribute that was found
135  * in args->valuelen.
136  */
137 int
138 xfs_attr_get(
139         struct xfs_da_args      *args)
140 {
141         uint                    lock_mode;
142         int                     error;
143
144         XFS_STATS_INC(args->dp->i_mount, xs_attr_get);
145
146         if (XFS_FORCED_SHUTDOWN(args->dp->i_mount))
147                 return -EIO;
148
149         args->geo = args->dp->i_mount->m_attr_geo;
150         args->whichfork = XFS_ATTR_FORK;
151         args->hashval = xfs_da_hashname(args->name, args->namelen);
152
153         /* Entirely possible to look up a name which doesn't exist */
154         args->op_flags = XFS_DA_OP_OKNOENT;
155
156         lock_mode = xfs_ilock_attr_map_shared(args->dp);
157         error = xfs_attr_get_ilocked(args);
158         xfs_iunlock(args->dp, lock_mode);
159
160         return error;
161 }
162
163 /*
164  * Calculate how many blocks we need for the new attribute,
165  */
166 STATIC int
167 xfs_attr_calc_size(
168         struct xfs_da_args      *args,
169         int                     *local)
170 {
171         struct xfs_mount        *mp = args->dp->i_mount;
172         int                     size;
173         int                     nblks;
174
175         /*
176          * Determine space new attribute will use, and if it would be
177          * "local" or "remote" (note: local != inline).
178          */
179         size = xfs_attr_leaf_newentsize(args, local);
180         nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
181         if (*local) {
182                 if (size > (args->geo->blksize / 2)) {
183                         /* Double split possible */
184                         nblks *= 2;
185                 }
186         } else {
187                 /*
188                  * Out of line attribute, cannot double split, but
189                  * make room for the attribute value itself.
190                  */
191                 uint    dblocks = xfs_attr3_rmt_blocks(mp, args->valuelen);
192                 nblks += dblocks;
193                 nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
194         }
195
196         return nblks;
197 }
198
199 STATIC int
200 xfs_attr_try_sf_addname(
201         struct xfs_inode        *dp,
202         struct xfs_da_args      *args)
203 {
204
205         int                     error;
206
207         /*
208          * Build initial attribute list (if required).
209          */
210         if (dp->i_afp->if_format == XFS_DINODE_FMT_EXTENTS)
211                 xfs_attr_shortform_create(args);
212
213         error = xfs_attr_shortform_addname(args);
214         if (error == -ENOSPC)
215                 return error;
216
217         /*
218          * Commit the shortform mods, and we're done.
219          * NOTE: this is also the error path (EEXIST, etc).
220          */
221         if (!error && !(args->op_flags & XFS_DA_OP_NOTIME))
222                 xfs_trans_ichgtime(args->trans, dp, XFS_ICHGTIME_CHG);
223
224         if (dp->i_mount->m_flags & XFS_MOUNT_WSYNC)
225                 xfs_trans_set_sync(args->trans);
226
227         return error;
228 }
229
230 /*
231  * Check to see if the attr should be upgraded from non-existent or shortform to
232  * single-leaf-block attribute list.
233  */
234 static inline bool
235 xfs_attr_is_shortform(
236         struct xfs_inode    *ip)
237 {
238         return ip->i_afp->if_format == XFS_DINODE_FMT_LOCAL ||
239                (ip->i_afp->if_format == XFS_DINODE_FMT_EXTENTS &&
240                 ip->i_afp->if_nextents == 0);
241 }
242
243 STATIC int
244 xfs_attr_set_fmt(
245         struct xfs_da_args      *args)
246 {
247         struct xfs_buf          *leaf_bp = NULL;
248         struct xfs_inode        *dp = args->dp;
249         int                     error, error2 = 0;
250
251         /*
252          * Try to add the attr to the attribute list in the inode.
253          */
254         error = xfs_attr_try_sf_addname(dp, args);
255         if (error != -ENOSPC) {
256                 error2 = xfs_trans_commit(args->trans);
257                 args->trans = NULL;
258                 return error ? error : error2;
259         }
260
261         /*
262          * It won't fit in the shortform, transform to a leaf block.  GROT:
263          * another possible req'mt for a double-split btree op.
264          */
265         error = xfs_attr_shortform_to_leaf(args, &leaf_bp);
266         if (error)
267                 return error;
268
269         /*
270          * Prevent the leaf buffer from being unlocked so that a concurrent AIL
271          * push cannot grab the half-baked leaf buffer and run into problems
272          * with the write verifier.
273          */
274         xfs_trans_bhold(args->trans, leaf_bp);
275         error = xfs_defer_finish(&args->trans);
276         xfs_trans_bhold_release(args->trans, leaf_bp);
277         if (error) {
278                 xfs_trans_brelse(args->trans, leaf_bp);
279                 return error;
280         }
281
282         return -EAGAIN;
283 }
284
285 /*
286  * Set the attribute specified in @args.
287  */
288 int
289 xfs_attr_set_args(
290         struct xfs_da_args      *args)
291 {
292         struct xfs_inode        *dp = args->dp;
293         struct xfs_da_state     *state;
294         int                     error;
295
296         /*
297          * If the attribute list is already in leaf format, jump straight to
298          * leaf handling.  Otherwise, try to add the attribute to the shortform
299          * list; if there's no room then convert the list to leaf format and try
300          * again.
301          */
302         if (xfs_attr_is_shortform(dp)) {
303                 error = xfs_attr_set_fmt(args);
304                 if (error != -EAGAIN)
305                         return error;
306         }
307
308         if (xfs_attr_is_leaf(dp)) {
309                 error = xfs_attr_leaf_addname(args);
310                 if (error != -ENOSPC)
311                         return error;
312
313                 /*
314                  * Promote the attribute list to the Btree format.
315                  */
316                 error = xfs_attr3_leaf_to_node(args);
317                 if (error)
318                         return error;
319
320                 /*
321                  * Finish any deferred work items and roll the transaction once
322                  * more.  The goal here is to call node_addname with the inode
323                  * and transaction in the same state (inode locked and joined,
324                  * transaction clean) no matter how we got to this step.
325                  */
326                 error = xfs_defer_finish(&args->trans);
327                 if (error)
328                         return error;
329
330                 /*
331                  * Commit the current trans (including the inode) and
332                  * start a new one.
333                  */
334                 error = xfs_trans_roll_inode(&args->trans, dp);
335                 if (error)
336                         return error;
337         }
338
339         do {
340                 error = xfs_attr_node_addname_find_attr(args, &state);
341                 if (error)
342                         return error;
343                 error = xfs_attr_node_addname(args, state);
344         } while (error == -EAGAIN);
345
346         return error;
347 }
348
349 /*
350  * Return EEXIST if attr is found, or ENOATTR if not
351  */
352 int
353 xfs_has_attr(
354         struct xfs_da_args      *args)
355 {
356         struct xfs_inode        *dp = args->dp;
357         struct xfs_buf          *bp = NULL;
358         int                     error;
359
360         if (!xfs_inode_hasattr(dp))
361                 return -ENOATTR;
362
363         if (dp->i_afp->if_format == XFS_DINODE_FMT_LOCAL)
364                 return xfs_attr_sf_findname(args, NULL, NULL);
365
366         if (xfs_attr_is_leaf(dp)) {
367                 error = xfs_attr_leaf_hasname(args, &bp);
368
369                 if (bp)
370                         xfs_trans_brelse(args->trans, bp);
371
372                 return error;
373         }
374
375         return xfs_attr_node_hasname(args, NULL);
376 }
377
378 /*
379  * Remove the attribute specified in @args.
380  */
381 int
382 xfs_attr_remove_args(
383         struct xfs_da_args      *args)
384 {
385         if (!xfs_inode_hasattr(args->dp))
386                 return -ENOATTR;
387
388         if (args->dp->i_afp->if_format == XFS_DINODE_FMT_LOCAL)
389                 return xfs_attr_shortform_remove(args);
390         if (xfs_attr_is_leaf(args->dp))
391                 return xfs_attr_leaf_removename(args);
392         return xfs_attr_node_removename(args);
393 }
394
395 /*
396  * Note: If args->value is NULL the attribute will be removed, just like the
397  * Linux ->setattr API.
398  */
399 int
400 xfs_attr_set(
401         struct xfs_da_args      *args)
402 {
403         struct xfs_inode        *dp = args->dp;
404         struct xfs_mount        *mp = dp->i_mount;
405         struct xfs_trans_res    tres;
406         bool                    rsvd = (args->attr_filter & XFS_ATTR_ROOT);
407         int                     error, local;
408         int                     rmt_blks = 0;
409         unsigned int            total;
410
411         if (XFS_FORCED_SHUTDOWN(dp->i_mount))
412                 return -EIO;
413
414         error = xfs_qm_dqattach(dp);
415         if (error)
416                 return error;
417
418         args->geo = mp->m_attr_geo;
419         args->whichfork = XFS_ATTR_FORK;
420         args->hashval = xfs_da_hashname(args->name, args->namelen);
421
422         /*
423          * We have no control over the attribute names that userspace passes us
424          * to remove, so we have to allow the name lookup prior to attribute
425          * removal to fail as well.
426          */
427         args->op_flags = XFS_DA_OP_OKNOENT;
428
429         if (args->value) {
430                 XFS_STATS_INC(mp, xs_attr_set);
431
432                 args->op_flags |= XFS_DA_OP_ADDNAME;
433                 args->total = xfs_attr_calc_size(args, &local);
434
435                 /*
436                  * If the inode doesn't have an attribute fork, add one.
437                  * (inode must not be locked when we call this routine)
438                  */
439                 if (XFS_IFORK_Q(dp) == 0) {
440                         int sf_size = sizeof(struct xfs_attr_sf_hdr) +
441                                 xfs_attr_sf_entsize_byname(args->namelen,
442                                                 args->valuelen);
443
444                         error = xfs_bmap_add_attrfork(dp, sf_size, rsvd);
445                         if (error)
446                                 return error;
447                 }
448
449                 tres.tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
450                                  M_RES(mp)->tr_attrsetrt.tr_logres *
451                                         args->total;
452                 tres.tr_logcount = XFS_ATTRSET_LOG_COUNT;
453                 tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
454                 total = args->total;
455
456                 if (!local)
457                         rmt_blks = xfs_attr3_rmt_blocks(mp, args->valuelen);
458         } else {
459                 XFS_STATS_INC(mp, xs_attr_remove);
460
461                 tres = M_RES(mp)->tr_attrrm;
462                 total = XFS_ATTRRM_SPACE_RES(mp);
463                 rmt_blks = xfs_attr3_rmt_blocks(mp, XFS_XATTR_SIZE_MAX);
464         }
465
466         /*
467          * Root fork attributes can use reserved data blocks for this
468          * operation if necessary
469          */
470         error = xfs_trans_alloc_inode(dp, &tres, total, 0, rsvd, &args->trans);
471         if (error)
472                 return error;
473
474         if (args->value || xfs_inode_hasattr(dp)) {
475                 error = xfs_iext_count_may_overflow(dp, XFS_ATTR_FORK,
476                                 XFS_IEXT_ATTR_MANIP_CNT(rmt_blks));
477                 if (error)
478                         goto out_trans_cancel;
479         }
480
481         if (args->value) {
482                 error = xfs_has_attr(args);
483                 if (error == -EEXIST && (args->attr_flags & XATTR_CREATE))
484                         goto out_trans_cancel;
485                 if (error == -ENOATTR && (args->attr_flags & XATTR_REPLACE))
486                         goto out_trans_cancel;
487                 if (error != -ENOATTR && error != -EEXIST)
488                         goto out_trans_cancel;
489
490                 error = xfs_attr_set_args(args);
491                 if (error)
492                         goto out_trans_cancel;
493                 /* shortform attribute has already been committed */
494                 if (!args->trans)
495                         goto out_unlock;
496         } else {
497                 error = xfs_has_attr(args);
498                 if (error != -EEXIST)
499                         goto out_trans_cancel;
500
501                 error = xfs_attr_remove_args(args);
502                 if (error)
503                         goto out_trans_cancel;
504         }
505
506         /*
507          * If this is a synchronous mount, make sure that the
508          * transaction goes to disk before returning to the user.
509          */
510         if (mp->m_flags & XFS_MOUNT_WSYNC)
511                 xfs_trans_set_sync(args->trans);
512
513         if (!(args->op_flags & XFS_DA_OP_NOTIME))
514                 xfs_trans_ichgtime(args->trans, dp, XFS_ICHGTIME_CHG);
515
516         /*
517          * Commit the last in the sequence of transactions.
518          */
519         xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
520         error = xfs_trans_commit(args->trans);
521 out_unlock:
522         xfs_iunlock(dp, XFS_ILOCK_EXCL);
523         return error;
524
525 out_trans_cancel:
526         if (args->trans)
527                 xfs_trans_cancel(args->trans);
528         goto out_unlock;
529 }
530
531 /*========================================================================
532  * External routines when attribute list is inside the inode
533  *========================================================================*/
534
535 static inline int xfs_attr_sf_totsize(struct xfs_inode *dp)
536 {
537         struct xfs_attr_shortform *sf;
538
539         sf = (struct xfs_attr_shortform *)dp->i_afp->if_u1.if_data;
540         return be16_to_cpu(sf->hdr.totsize);
541 }
542
543 /*
544  * Add a name to the shortform attribute list structure
545  * This is the external routine.
546  */
547 STATIC int
548 xfs_attr_shortform_addname(xfs_da_args_t *args)
549 {
550         int newsize, forkoff, retval;
551
552         trace_xfs_attr_sf_addname(args);
553
554         retval = xfs_attr_shortform_lookup(args);
555         if (retval == -ENOATTR && (args->attr_flags & XATTR_REPLACE))
556                 return retval;
557         if (retval == -EEXIST) {
558                 if (args->attr_flags & XATTR_CREATE)
559                         return retval;
560                 retval = xfs_attr_shortform_remove(args);
561                 if (retval)
562                         return retval;
563                 /*
564                  * Since we have removed the old attr, clear ATTR_REPLACE so
565                  * that the leaf format add routine won't trip over the attr
566                  * not being around.
567                  */
568                 args->attr_flags &= ~XATTR_REPLACE;
569         }
570
571         if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
572             args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
573                 return -ENOSPC;
574
575         newsize = xfs_attr_sf_totsize(args->dp);
576         newsize += xfs_attr_sf_entsize_byname(args->namelen, args->valuelen);
577
578         forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
579         if (!forkoff)
580                 return -ENOSPC;
581
582         xfs_attr_shortform_add(args, forkoff);
583         return 0;
584 }
585
586
587 /*========================================================================
588  * External routines when attribute list is one block
589  *========================================================================*/
590
591 /* Store info about a remote block */
592 STATIC void
593 xfs_attr_save_rmt_blk(
594         struct xfs_da_args      *args)
595 {
596         args->blkno2 = args->blkno;
597         args->index2 = args->index;
598         args->rmtblkno2 = args->rmtblkno;
599         args->rmtblkcnt2 = args->rmtblkcnt;
600         args->rmtvaluelen2 = args->rmtvaluelen;
601 }
602
603 /* Set stored info about a remote block */
604 STATIC void
605 xfs_attr_restore_rmt_blk(
606         struct xfs_da_args      *args)
607 {
608         args->blkno = args->blkno2;
609         args->index = args->index2;
610         args->rmtblkno = args->rmtblkno2;
611         args->rmtblkcnt = args->rmtblkcnt2;
612         args->rmtvaluelen = args->rmtvaluelen2;
613 }
614
615 /*
616  * Tries to add an attribute to an inode in leaf form
617  *
618  * This function is meant to execute as part of a delayed operation and leaves
619  * the transaction handling to the caller.  On success the attribute is added
620  * and the inode and transaction are left dirty.  If there is not enough space,
621  * the attr data is converted to node format and -ENOSPC is returned. Caller is
622  * responsible for handling the dirty inode and transaction or adding the attr
623  * in node format.
624  */
625 STATIC int
626 xfs_attr_leaf_try_add(
627         struct xfs_da_args      *args,
628         struct xfs_buf          *bp)
629 {
630         int                     retval;
631
632         /*
633          * Look up the given attribute in the leaf block.  Figure out if
634          * the given flags produce an error or call for an atomic rename.
635          */
636         retval = xfs_attr_leaf_hasname(args, &bp);
637         if (retval != -ENOATTR && retval != -EEXIST)
638                 return retval;
639         if (retval == -ENOATTR && (args->attr_flags & XATTR_REPLACE))
640                 goto out_brelse;
641         if (retval == -EEXIST) {
642                 if (args->attr_flags & XATTR_CREATE)
643                         goto out_brelse;
644
645                 trace_xfs_attr_leaf_replace(args);
646
647                 /* save the attribute state for later removal*/
648                 args->op_flags |= XFS_DA_OP_RENAME;     /* an atomic rename */
649                 xfs_attr_save_rmt_blk(args);
650
651                 /*
652                  * clear the remote attr state now that it is saved so that the
653                  * values reflect the state of the attribute we are about to
654                  * add, not the attribute we just found and will remove later.
655                  */
656                 args->rmtblkno = 0;
657                 args->rmtblkcnt = 0;
658                 args->rmtvaluelen = 0;
659         }
660
661         /*
662          * Add the attribute to the leaf block
663          */
664         return xfs_attr3_leaf_add(bp, args);
665
666 out_brelse:
667         xfs_trans_brelse(args->trans, bp);
668         return retval;
669 }
670
671
672 /*
673  * Add a name to the leaf attribute list structure
674  *
675  * This leaf block cannot have a "remote" value, we only call this routine
676  * if bmap_one_block() says there is only one block (ie: no remote blks).
677  */
678 STATIC int
679 xfs_attr_leaf_addname(
680         struct xfs_da_args      *args)
681 {
682         int                     error, forkoff;
683         struct xfs_buf          *bp = NULL;
684         struct xfs_inode        *dp = args->dp;
685
686         trace_xfs_attr_leaf_addname(args);
687
688         error = xfs_attr_leaf_try_add(args, bp);
689         if (error)
690                 return error;
691
692         /*
693          * Commit the transaction that added the attr name so that
694          * later routines can manage their own transactions.
695          */
696         error = xfs_trans_roll_inode(&args->trans, dp);
697         if (error)
698                 return error;
699
700         /*
701          * If there was an out-of-line value, allocate the blocks we
702          * identified for its storage and copy the value.  This is done
703          * after we create the attribute so that we don't overflow the
704          * maximum size of a transaction and/or hit a deadlock.
705          */
706         if (args->rmtblkno > 0) {
707                 error = xfs_attr_rmtval_set(args);
708                 if (error)
709                         return error;
710         }
711
712         if (!(args->op_flags & XFS_DA_OP_RENAME)) {
713                 /*
714                  * Added a "remote" value, just clear the incomplete flag.
715                  */
716                 if (args->rmtblkno > 0)
717                         error = xfs_attr3_leaf_clearflag(args);
718
719                 return error;
720         }
721
722         /*
723          * If this is an atomic rename operation, we must "flip" the incomplete
724          * flags on the "new" and "old" attribute/value pairs so that one
725          * disappears and one appears atomically.  Then we must remove the "old"
726          * attribute/value pair.
727          *
728          * In a separate transaction, set the incomplete flag on the "old" attr
729          * and clear the incomplete flag on the "new" attr.
730          */
731
732         error = xfs_attr3_leaf_flipflags(args);
733         if (error)
734                 return error;
735         /*
736          * Commit the flag value change and start the next trans in series.
737          */
738         error = xfs_trans_roll_inode(&args->trans, args->dp);
739         if (error)
740                 return error;
741
742         /*
743          * Dismantle the "old" attribute/value pair by removing a "remote" value
744          * (if it exists).
745          */
746         xfs_attr_restore_rmt_blk(args);
747
748         if (args->rmtblkno) {
749                 error = xfs_attr_rmtval_invalidate(args);
750                 if (error)
751                         return error;
752
753                 error = xfs_attr_rmtval_remove(args);
754                 if (error)
755                         return error;
756         }
757
758         /*
759          * Read in the block containing the "old" attr, then remove the "old"
760          * attr from that block (neat, huh!)
761          */
762         error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
763                                    &bp);
764         if (error)
765                 return error;
766
767         xfs_attr3_leaf_remove(bp, args);
768
769         /*
770          * If the result is small enough, shrink it all into the inode.
771          */
772         forkoff = xfs_attr_shortform_allfit(bp, dp);
773         if (forkoff)
774                 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
775                 /* bp is gone due to xfs_da_shrink_inode */
776
777         return error;
778 }
779
780 /*
781  * Return EEXIST if attr is found, or ENOATTR if not
782  */
783 STATIC int
784 xfs_attr_leaf_hasname(
785         struct xfs_da_args      *args,
786         struct xfs_buf          **bp)
787 {
788         int                     error = 0;
789
790         error = xfs_attr3_leaf_read(args->trans, args->dp, 0, bp);
791         if (error)
792                 return error;
793
794         error = xfs_attr3_leaf_lookup_int(*bp, args);
795         if (error != -ENOATTR && error != -EEXIST)
796                 xfs_trans_brelse(args->trans, *bp);
797
798         return error;
799 }
800
801 /*
802  * Remove a name from the leaf attribute list structure
803  *
804  * This leaf block cannot have a "remote" value, we only call this routine
805  * if bmap_one_block() says there is only one block (ie: no remote blks).
806  */
807 STATIC int
808 xfs_attr_leaf_removename(
809         struct xfs_da_args      *args)
810 {
811         struct xfs_inode        *dp;
812         struct xfs_buf          *bp;
813         int                     error, forkoff;
814
815         trace_xfs_attr_leaf_removename(args);
816
817         /*
818          * Remove the attribute.
819          */
820         dp = args->dp;
821
822         error = xfs_attr_leaf_hasname(args, &bp);
823
824         if (error == -ENOATTR) {
825                 xfs_trans_brelse(args->trans, bp);
826                 return error;
827         } else if (error != -EEXIST)
828                 return error;
829
830         xfs_attr3_leaf_remove(bp, args);
831
832         /*
833          * If the result is small enough, shrink it all into the inode.
834          */
835         forkoff = xfs_attr_shortform_allfit(bp, dp);
836         if (forkoff)
837                 return xfs_attr3_leaf_to_shortform(bp, args, forkoff);
838                 /* bp is gone due to xfs_da_shrink_inode */
839
840         return 0;
841 }
842
843 /*
844  * Look up a name in a leaf attribute list structure.
845  *
846  * This leaf block cannot have a "remote" value, we only call this routine
847  * if bmap_one_block() says there is only one block (ie: no remote blks).
848  *
849  * Returns 0 on successful retrieval, otherwise an error.
850  */
851 STATIC int
852 xfs_attr_leaf_get(xfs_da_args_t *args)
853 {
854         struct xfs_buf *bp;
855         int error;
856
857         trace_xfs_attr_leaf_get(args);
858
859         error = xfs_attr_leaf_hasname(args, &bp);
860
861         if (error == -ENOATTR)  {
862                 xfs_trans_brelse(args->trans, bp);
863                 return error;
864         } else if (error != -EEXIST)
865                 return error;
866
867
868         error = xfs_attr3_leaf_getvalue(bp, args);
869         xfs_trans_brelse(args->trans, bp);
870         return error;
871 }
872
873 /*
874  * Return EEXIST if attr is found, or ENOATTR if not
875  * statep: If not null is set to point at the found state.  Caller will
876  *         be responsible for freeing the state in this case.
877  */
878 STATIC int
879 xfs_attr_node_hasname(
880         struct xfs_da_args      *args,
881         struct xfs_da_state     **statep)
882 {
883         struct xfs_da_state     *state;
884         int                     retval, error;
885
886         state = xfs_da_state_alloc(args);
887         if (statep != NULL)
888                 *statep = NULL;
889
890         /*
891          * Search to see if name exists, and get back a pointer to it.
892          */
893         error = xfs_da3_node_lookup_int(state, &retval);
894         if (error) {
895                 xfs_da_state_free(state);
896                 return error;
897         }
898
899         if (statep != NULL)
900                 *statep = state;
901         else
902                 xfs_da_state_free(state);
903         return retval;
904 }
905
906 /*========================================================================
907  * External routines when attribute list size > geo->blksize
908  *========================================================================*/
909
910 STATIC int
911 xfs_attr_node_addname_find_attr(
912         struct xfs_da_args      *args,
913         struct xfs_da_state     **state)
914 {
915         int                     retval;
916
917         /*
918          * Search to see if name already exists, and get back a pointer
919          * to where it should go.
920          */
921         retval = xfs_attr_node_hasname(args, state);
922         if (retval != -ENOATTR && retval != -EEXIST)
923                 goto error;
924
925         if (retval == -ENOATTR && (args->attr_flags & XATTR_REPLACE))
926                 goto error;
927         if (retval == -EEXIST) {
928                 if (args->attr_flags & XATTR_CREATE)
929                         goto error;
930
931                 trace_xfs_attr_node_replace(args);
932
933                 /* save the attribute state for later removal*/
934                 args->op_flags |= XFS_DA_OP_RENAME;     /* atomic rename op */
935                 xfs_attr_save_rmt_blk(args);
936
937                 /*
938                  * clear the remote attr state now that it is saved so that the
939                  * values reflect the state of the attribute we are about to
940                  * add, not the attribute we just found and will remove later.
941                  */
942                 args->rmtblkno = 0;
943                 args->rmtblkcnt = 0;
944                 args->rmtvaluelen = 0;
945         }
946
947         return 0;
948 error:
949         if (*state)
950                 xfs_da_state_free(*state);
951         return retval;
952 }
953
954 /*
955  * Add a name to a Btree-format attribute list.
956  *
957  * This will involve walking down the Btree, and may involve splitting
958  * leaf nodes and even splitting intermediate nodes up to and including
959  * the root node (a special case of an intermediate node).
960  *
961  * "Remote" attribute values confuse the issue and atomic rename operations
962  * add a whole extra layer of confusion on top of that.
963  */
964 STATIC int
965 xfs_attr_node_addname(
966         struct xfs_da_args      *args,
967         struct xfs_da_state     *state)
968 {
969         struct xfs_da_state_blk *blk;
970         struct xfs_inode        *dp;
971         int                     retval, error;
972
973         trace_xfs_attr_node_addname(args);
974
975         dp = args->dp;
976         blk = &state->path.blk[state->path.active-1];
977         ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
978
979         retval = xfs_attr3_leaf_add(blk->bp, state->args);
980         if (retval == -ENOSPC) {
981                 if (state->path.active == 1) {
982                         /*
983                          * Its really a single leaf node, but it had
984                          * out-of-line values so it looked like it *might*
985                          * have been a b-tree.
986                          */
987                         xfs_da_state_free(state);
988                         state = NULL;
989                         error = xfs_attr3_leaf_to_node(args);
990                         if (error)
991                                 goto out;
992                         error = xfs_defer_finish(&args->trans);
993                         if (error)
994                                 goto out;
995
996                         /*
997                          * Commit the node conversion and start the next
998                          * trans in the chain.
999                          */
1000                         error = xfs_trans_roll_inode(&args->trans, dp);
1001                         if (error)
1002                                 goto out;
1003
1004                         return -EAGAIN;
1005                 }
1006
1007                 /*
1008                  * Split as many Btree elements as required.
1009                  * This code tracks the new and old attr's location
1010                  * in the index/blkno/rmtblkno/rmtblkcnt fields and
1011                  * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
1012                  */
1013                 error = xfs_da3_split(state);
1014                 if (error)
1015                         goto out;
1016                 error = xfs_defer_finish(&args->trans);
1017                 if (error)
1018                         goto out;
1019         } else {
1020                 /*
1021                  * Addition succeeded, update Btree hashvals.
1022                  */
1023                 xfs_da3_fixhashpath(state, &state->path);
1024         }
1025
1026         /*
1027          * Kill the state structure, we're done with it and need to
1028          * allow the buffers to come back later.
1029          */
1030         xfs_da_state_free(state);
1031         state = NULL;
1032
1033         /*
1034          * Commit the leaf addition or btree split and start the next
1035          * trans in the chain.
1036          */
1037         error = xfs_trans_roll_inode(&args->trans, dp);
1038         if (error)
1039                 goto out;
1040
1041         /*
1042          * If there was an out-of-line value, allocate the blocks we
1043          * identified for its storage and copy the value.  This is done
1044          * after we create the attribute so that we don't overflow the
1045          * maximum size of a transaction and/or hit a deadlock.
1046          */
1047         if (args->rmtblkno > 0) {
1048                 error = xfs_attr_rmtval_set(args);
1049                 if (error)
1050                         return error;
1051         }
1052
1053         if (!(args->op_flags & XFS_DA_OP_RENAME)) {
1054                 /*
1055                  * Added a "remote" value, just clear the incomplete flag.
1056                  */
1057                 if (args->rmtblkno > 0)
1058                         error = xfs_attr3_leaf_clearflag(args);
1059                 retval = error;
1060                 goto out;
1061         }
1062
1063         /*
1064          * If this is an atomic rename operation, we must "flip" the incomplete
1065          * flags on the "new" and "old" attribute/value pairs so that one
1066          * disappears and one appears atomically.  Then we must remove the "old"
1067          * attribute/value pair.
1068          *
1069          * In a separate transaction, set the incomplete flag on the "old" attr
1070          * and clear the incomplete flag on the "new" attr.
1071          */
1072         error = xfs_attr3_leaf_flipflags(args);
1073         if (error)
1074                 goto out;
1075         /*
1076          * Commit the flag value change and start the next trans in series
1077          */
1078         error = xfs_trans_roll_inode(&args->trans, args->dp);
1079         if (error)
1080                 goto out;
1081
1082         /*
1083          * Dismantle the "old" attribute/value pair by removing a "remote" value
1084          * (if it exists).
1085          */
1086         xfs_attr_restore_rmt_blk(args);
1087
1088         if (args->rmtblkno) {
1089                 error = xfs_attr_rmtval_invalidate(args);
1090                 if (error)
1091                         return error;
1092
1093                 error = xfs_attr_rmtval_remove(args);
1094                 if (error)
1095                         return error;
1096         }
1097
1098         error = xfs_attr_node_addname_clear_incomplete(args);
1099         if (error)
1100                 goto out;
1101         retval = 0;
1102 out:
1103         if (state)
1104                 xfs_da_state_free(state);
1105         if (error)
1106                 return error;
1107         return retval;
1108 }
1109
1110
1111 STATIC int
1112 xfs_attr_node_addname_clear_incomplete(
1113         struct xfs_da_args              *args)
1114 {
1115         struct xfs_da_state             *state = NULL;
1116         struct xfs_da_state_blk         *blk;
1117         int                             retval = 0;
1118         int                             error = 0;
1119
1120         /*
1121          * Re-find the "old" attribute entry after any split ops. The INCOMPLETE
1122          * flag means that we will find the "old" attr, not the "new" one.
1123          */
1124         args->attr_filter |= XFS_ATTR_INCOMPLETE;
1125         state = xfs_da_state_alloc(args);
1126         state->inleaf = 0;
1127         error = xfs_da3_node_lookup_int(state, &retval);
1128         if (error)
1129                 goto out;
1130
1131         /*
1132          * Remove the name and update the hashvals in the tree.
1133          */
1134         blk = &state->path.blk[state->path.active-1];
1135         ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1136         error = xfs_attr3_leaf_remove(blk->bp, args);
1137         xfs_da3_fixhashpath(state, &state->path);
1138
1139         /*
1140          * Check to see if the tree needs to be collapsed.
1141          */
1142         if (retval && (state->path.active > 1)) {
1143                 error = xfs_da3_join(state);
1144                 if (error)
1145                         goto out;
1146         }
1147         retval = error = 0;
1148
1149 out:
1150         if (state)
1151                 xfs_da_state_free(state);
1152         if (error)
1153                 return error;
1154         return retval;
1155 }
1156
1157 /*
1158  * Shrink an attribute from leaf to shortform
1159  */
1160 STATIC int
1161 xfs_attr_node_shrink(
1162         struct xfs_da_args      *args,
1163         struct xfs_da_state     *state)
1164 {
1165         struct xfs_inode        *dp = args->dp;
1166         int                     error, forkoff;
1167         struct xfs_buf          *bp;
1168
1169         /*
1170          * Have to get rid of the copy of this dabuf in the state.
1171          */
1172         ASSERT(state->path.active == 1);
1173         ASSERT(state->path.blk[0].bp);
1174         state->path.blk[0].bp = NULL;
1175
1176         error = xfs_attr3_leaf_read(args->trans, args->dp, 0, &bp);
1177         if (error)
1178                 return error;
1179
1180         forkoff = xfs_attr_shortform_allfit(bp, dp);
1181         if (forkoff) {
1182                 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
1183                 /* bp is gone due to xfs_da_shrink_inode */
1184         } else
1185                 xfs_trans_brelse(args->trans, bp);
1186
1187         return error;
1188 }
1189
1190 /*
1191  * Mark an attribute entry INCOMPLETE and save pointers to the relevant buffers
1192  * for later deletion of the entry.
1193  */
1194 STATIC int
1195 xfs_attr_leaf_mark_incomplete(
1196         struct xfs_da_args      *args,
1197         struct xfs_da_state     *state)
1198 {
1199         int                     error;
1200
1201         /*
1202          * Fill in disk block numbers in the state structure
1203          * so that we can get the buffers back after we commit
1204          * several transactions in the following calls.
1205          */
1206         error = xfs_attr_fillstate(state);
1207         if (error)
1208                 return error;
1209
1210         /*
1211          * Mark the attribute as INCOMPLETE
1212          */
1213         return xfs_attr3_leaf_setflag(args);
1214 }
1215
1216 /*
1217  * Initial setup for xfs_attr_node_removename.  Make sure the attr is there and
1218  * the blocks are valid.  Attr keys with remote blocks will be marked
1219  * incomplete.
1220  */
1221 STATIC
1222 int xfs_attr_node_removename_setup(
1223         struct xfs_da_args      *args,
1224         struct xfs_da_state     **state)
1225 {
1226         int                     error;
1227
1228         error = xfs_attr_node_hasname(args, state);
1229         if (error != -EEXIST)
1230                 return error;
1231
1232         ASSERT((*state)->path.blk[(*state)->path.active - 1].bp != NULL);
1233         ASSERT((*state)->path.blk[(*state)->path.active - 1].magic ==
1234                 XFS_ATTR_LEAF_MAGIC);
1235
1236         if (args->rmtblkno > 0) {
1237                 error = xfs_attr_leaf_mark_incomplete(args, *state);
1238                 if (error)
1239                         return error;
1240
1241                 return xfs_attr_rmtval_invalidate(args);
1242         }
1243
1244         return 0;
1245 }
1246
1247 STATIC int
1248 xfs_attr_node_remove_name(
1249         struct xfs_da_args      *args,
1250         struct xfs_da_state     *state)
1251 {
1252         struct xfs_da_state_blk *blk;
1253         int                     retval;
1254
1255         /*
1256          * Remove the name and update the hashvals in the tree.
1257          */
1258         blk = &state->path.blk[state->path.active-1];
1259         ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1260         retval = xfs_attr3_leaf_remove(blk->bp, args);
1261         xfs_da3_fixhashpath(state, &state->path);
1262
1263         return retval;
1264 }
1265
1266 /*
1267  * Remove a name from a B-tree attribute list.
1268  *
1269  * This will involve walking down the Btree, and may involve joining
1270  * leaf nodes and even joining intermediate nodes up to and including
1271  * the root node (a special case of an intermediate node).
1272  */
1273 STATIC int
1274 xfs_attr_node_removename(
1275         struct xfs_da_args      *args)
1276 {
1277         struct xfs_da_state     *state;
1278         int                     retval, error;
1279         struct xfs_inode        *dp = args->dp;
1280
1281         trace_xfs_attr_node_removename(args);
1282
1283         error = xfs_attr_node_removename_setup(args, &state);
1284         if (error)
1285                 goto out;
1286
1287         /*
1288          * If there is an out-of-line value, de-allocate the blocks.
1289          * This is done before we remove the attribute so that we don't
1290          * overflow the maximum size of a transaction and/or hit a deadlock.
1291          */
1292         if (args->rmtblkno > 0) {
1293                 error = xfs_attr_rmtval_remove(args);
1294                 if (error)
1295                         goto out;
1296
1297                 /*
1298                  * Refill the state structure with buffers, the prior calls
1299                  * released our buffers.
1300                  */
1301                 error = xfs_attr_refillstate(state);
1302                 if (error)
1303                         goto out;
1304         }
1305         retval = xfs_attr_node_remove_name(args, state);
1306
1307         /*
1308          * Check to see if the tree needs to be collapsed.
1309          */
1310         if (retval && (state->path.active > 1)) {
1311                 error = xfs_da3_join(state);
1312                 if (error)
1313                         goto out;
1314                 error = xfs_defer_finish(&args->trans);
1315                 if (error)
1316                         goto out;
1317                 /*
1318                  * Commit the Btree join operation and start a new trans.
1319                  */
1320                 error = xfs_trans_roll_inode(&args->trans, dp);
1321                 if (error)
1322                         goto out;
1323         }
1324
1325         /*
1326          * If the result is small enough, push it all into the inode.
1327          */
1328         if (xfs_attr_is_leaf(dp))
1329                 error = xfs_attr_node_shrink(args, state);
1330
1331 out:
1332         if (state)
1333                 xfs_da_state_free(state);
1334         return error;
1335 }
1336
1337 /*
1338  * Fill in the disk block numbers in the state structure for the buffers
1339  * that are attached to the state structure.
1340  * This is done so that we can quickly reattach ourselves to those buffers
1341  * after some set of transaction commits have released these buffers.
1342  */
1343 STATIC int
1344 xfs_attr_fillstate(xfs_da_state_t *state)
1345 {
1346         xfs_da_state_path_t *path;
1347         xfs_da_state_blk_t *blk;
1348         int level;
1349
1350         trace_xfs_attr_fillstate(state->args);
1351
1352         /*
1353          * Roll down the "path" in the state structure, storing the on-disk
1354          * block number for those buffers in the "path".
1355          */
1356         path = &state->path;
1357         ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1358         for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1359                 if (blk->bp) {
1360                         blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
1361                         blk->bp = NULL;
1362                 } else {
1363                         blk->disk_blkno = 0;
1364                 }
1365         }
1366
1367         /*
1368          * Roll down the "altpath" in the state structure, storing the on-disk
1369          * block number for those buffers in the "altpath".
1370          */
1371         path = &state->altpath;
1372         ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1373         for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1374                 if (blk->bp) {
1375                         blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
1376                         blk->bp = NULL;
1377                 } else {
1378                         blk->disk_blkno = 0;
1379                 }
1380         }
1381
1382         return 0;
1383 }
1384
1385 /*
1386  * Reattach the buffers to the state structure based on the disk block
1387  * numbers stored in the state structure.
1388  * This is done after some set of transaction commits have released those
1389  * buffers from our grip.
1390  */
1391 STATIC int
1392 xfs_attr_refillstate(xfs_da_state_t *state)
1393 {
1394         xfs_da_state_path_t *path;
1395         xfs_da_state_blk_t *blk;
1396         int level, error;
1397
1398         trace_xfs_attr_refillstate(state->args);
1399
1400         /*
1401          * Roll down the "path" in the state structure, storing the on-disk
1402          * block number for those buffers in the "path".
1403          */
1404         path = &state->path;
1405         ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1406         for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1407                 if (blk->disk_blkno) {
1408                         error = xfs_da3_node_read_mapped(state->args->trans,
1409                                         state->args->dp, blk->disk_blkno,
1410                                         &blk->bp, XFS_ATTR_FORK);
1411                         if (error)
1412                                 return error;
1413                 } else {
1414                         blk->bp = NULL;
1415                 }
1416         }
1417
1418         /*
1419          * Roll down the "altpath" in the state structure, storing the on-disk
1420          * block number for those buffers in the "altpath".
1421          */
1422         path = &state->altpath;
1423         ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1424         for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1425                 if (blk->disk_blkno) {
1426                         error = xfs_da3_node_read_mapped(state->args->trans,
1427                                         state->args->dp, blk->disk_blkno,
1428                                         &blk->bp, XFS_ATTR_FORK);
1429                         if (error)
1430                                 return error;
1431                 } else {
1432                         blk->bp = NULL;
1433                 }
1434         }
1435
1436         return 0;
1437 }
1438
1439 /*
1440  * Retrieve the attribute data from a node attribute list.
1441  *
1442  * This routine gets called for any attribute fork that has more than one
1443  * block, ie: both true Btree attr lists and for single-leaf-blocks with
1444  * "remote" values taking up more blocks.
1445  *
1446  * Returns 0 on successful retrieval, otherwise an error.
1447  */
1448 STATIC int
1449 xfs_attr_node_get(
1450         struct xfs_da_args      *args)
1451 {
1452         struct xfs_da_state     *state;
1453         struct xfs_da_state_blk *blk;
1454         int                     i;
1455         int                     error;
1456
1457         trace_xfs_attr_node_get(args);
1458
1459         /*
1460          * Search to see if name exists, and get back a pointer to it.
1461          */
1462         error = xfs_attr_node_hasname(args, &state);
1463         if (error != -EEXIST)
1464                 goto out_release;
1465
1466         /*
1467          * Get the value, local or "remote"
1468          */
1469         blk = &state->path.blk[state->path.active - 1];
1470         error = xfs_attr3_leaf_getvalue(blk->bp, args);
1471
1472         /*
1473          * If not in a transaction, we have to release all the buffers.
1474          */
1475 out_release:
1476         for (i = 0; state != NULL && i < state->path.active; i++) {
1477                 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
1478                 state->path.blk[i].bp = NULL;
1479         }
1480
1481         if (state)
1482                 xfs_da_state_free(state);
1483         return error;
1484 }
1485
1486 /* Returns true if the attribute entry name is valid. */
1487 bool
1488 xfs_attr_namecheck(
1489         const void      *name,
1490         size_t          length)
1491 {
1492         /*
1493          * MAXNAMELEN includes the trailing null, but (name/length) leave it
1494          * out, so use >= for the length check.
1495          */
1496         if (length >= MAXNAMELEN)
1497                 return false;
1498
1499         /* There shouldn't be any nulls here */
1500         return !memchr(name, 0, length);
1501 }