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