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