GNU Linux-libre 4.9.337-gnu1
[releases.git] / fs / xfs / libxfs / xfs_attr.c
1 /*
2  * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3  * All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it would be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write the Free Software Foundation,
16  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_shared.h"
21 #include "xfs_format.h"
22 #include "xfs_log_format.h"
23 #include "xfs_trans_resv.h"
24 #include "xfs_bit.h"
25 #include "xfs_mount.h"
26 #include "xfs_defer.h"
27 #include "xfs_da_format.h"
28 #include "xfs_da_btree.h"
29 #include "xfs_attr_sf.h"
30 #include "xfs_inode.h"
31 #include "xfs_alloc.h"
32 #include "xfs_trans.h"
33 #include "xfs_inode_item.h"
34 #include "xfs_bmap.h"
35 #include "xfs_bmap_util.h"
36 #include "xfs_bmap_btree.h"
37 #include "xfs_attr.h"
38 #include "xfs_attr_leaf.h"
39 #include "xfs_attr_remote.h"
40 #include "xfs_error.h"
41 #include "xfs_quota.h"
42 #include "xfs_trans_space.h"
43 #include "xfs_trace.h"
44
45 /*
46  * xfs_attr.c
47  *
48  * Provide the external interfaces to manage attribute lists.
49  */
50
51 /*========================================================================
52  * Function prototypes for the kernel.
53  *========================================================================*/
54
55 /*
56  * Internal routines when attribute list fits inside the inode.
57  */
58 STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
59
60 /*
61  * Internal routines when attribute list is one block.
62  */
63 STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
64 STATIC int xfs_attr_leaf_addname(xfs_da_args_t *args);
65 STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
66
67 /*
68  * Internal routines when attribute list is more than one block.
69  */
70 STATIC int xfs_attr_node_get(xfs_da_args_t *args);
71 STATIC int xfs_attr_node_addname(xfs_da_args_t *args);
72 STATIC int xfs_attr_node_removename(xfs_da_args_t *args);
73 STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
74 STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
75
76
77 STATIC int
78 xfs_attr_args_init(
79         struct xfs_da_args      *args,
80         struct xfs_inode        *dp,
81         const unsigned char     *name,
82         int                     flags)
83 {
84
85         if (!name)
86                 return -EINVAL;
87
88         memset(args, 0, sizeof(*args));
89         args->geo = dp->i_mount->m_attr_geo;
90         args->whichfork = XFS_ATTR_FORK;
91         args->dp = dp;
92         args->flags = flags;
93         args->name = name;
94         args->namelen = strlen((const char *)name);
95         if (args->namelen >= MAXNAMELEN)
96                 return -EFAULT;         /* match IRIX behaviour */
97
98         args->hashval = xfs_da_hashname(args->name, args->namelen);
99         return 0;
100 }
101
102 int
103 xfs_inode_hasattr(
104         struct xfs_inode        *ip)
105 {
106         if (!XFS_IFORK_Q(ip) ||
107             (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
108              ip->i_d.di_anextents == 0))
109                 return 0;
110         return 1;
111 }
112
113 /*========================================================================
114  * Overall external interface routines.
115  *========================================================================*/
116
117 int
118 xfs_attr_get(
119         struct xfs_inode        *ip,
120         const unsigned char     *name,
121         unsigned char           *value,
122         int                     *valuelenp,
123         int                     flags)
124 {
125         struct xfs_da_args      args;
126         uint                    lock_mode;
127         int                     error;
128
129         XFS_STATS_INC(ip->i_mount, xs_attr_get);
130
131         if (XFS_FORCED_SHUTDOWN(ip->i_mount))
132                 return -EIO;
133
134         error = xfs_attr_args_init(&args, ip, name, flags);
135         if (error)
136                 return error;
137
138         args.value = value;
139         args.valuelen = *valuelenp;
140         /* Entirely possible to look up a name which doesn't exist */
141         args.op_flags = XFS_DA_OP_OKNOENT;
142
143         lock_mode = xfs_ilock_attr_map_shared(ip);
144         if (!xfs_inode_hasattr(ip))
145                 error = -ENOATTR;
146         else if (ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL)
147                 error = xfs_attr_shortform_getvalue(&args);
148         else if (xfs_bmap_one_block(ip, XFS_ATTR_FORK))
149                 error = xfs_attr_leaf_get(&args);
150         else
151                 error = xfs_attr_node_get(&args);
152         xfs_iunlock(ip, lock_mode);
153
154         *valuelenp = args.valuelen;
155         return error == -EEXIST ? 0 : error;
156 }
157
158 /*
159  * Calculate how many blocks we need for the new attribute,
160  */
161 STATIC int
162 xfs_attr_calc_size(
163         struct xfs_da_args      *args,
164         int                     *local)
165 {
166         struct xfs_mount        *mp = args->dp->i_mount;
167         int                     size;
168         int                     nblks;
169
170         /*
171          * Determine space new attribute will use, and if it would be
172          * "local" or "remote" (note: local != inline).
173          */
174         size = xfs_attr_leaf_newentsize(args, local);
175         nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
176         if (*local) {
177                 if (size > (args->geo->blksize / 2)) {
178                         /* Double split possible */
179                         nblks *= 2;
180                 }
181         } else {
182                 /*
183                  * Out of line attribute, cannot double split, but
184                  * make room for the attribute value itself.
185                  */
186                 uint    dblocks = xfs_attr3_rmt_blocks(mp, args->valuelen);
187                 nblks += dblocks;
188                 nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
189         }
190
191         return nblks;
192 }
193
194 int
195 xfs_attr_set(
196         struct xfs_inode        *dp,
197         const unsigned char     *name,
198         unsigned char           *value,
199         int                     valuelen,
200         int                     flags)
201 {
202         struct xfs_mount        *mp = dp->i_mount;
203         struct xfs_da_args      args;
204         struct xfs_defer_ops    dfops;
205         struct xfs_trans_res    tres;
206         xfs_fsblock_t           firstblock;
207         int                     rsvd = (flags & ATTR_ROOT) != 0;
208         int                     error, err2, local;
209
210         XFS_STATS_INC(mp, xs_attr_set);
211
212         if (XFS_FORCED_SHUTDOWN(dp->i_mount))
213                 return -EIO;
214
215         error = xfs_attr_args_init(&args, dp, name, flags);
216         if (error)
217                 return error;
218
219         args.value = value;
220         args.valuelen = valuelen;
221         args.firstblock = &firstblock;
222         args.dfops = &dfops;
223         args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
224         args.total = xfs_attr_calc_size(&args, &local);
225
226         error = xfs_qm_dqattach(dp, 0);
227         if (error)
228                 return error;
229
230         /*
231          * If the inode doesn't have an attribute fork, add one.
232          * (inode must not be locked when we call this routine)
233          */
234         if (XFS_IFORK_Q(dp) == 0) {
235                 int sf_size = sizeof(xfs_attr_sf_hdr_t) +
236                         XFS_ATTR_SF_ENTSIZE_BYNAME(args.namelen, valuelen);
237
238                 error = xfs_bmap_add_attrfork(dp, sf_size, rsvd);
239                 if (error)
240                         return error;
241         }
242
243         tres.tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
244                          M_RES(mp)->tr_attrsetrt.tr_logres * args.total;
245         tres.tr_logcount = XFS_ATTRSET_LOG_COUNT;
246         tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
247
248         /*
249          * Root fork attributes can use reserved data blocks for this
250          * operation if necessary
251          */
252         error = xfs_trans_alloc(mp, &tres, args.total, 0,
253                         rsvd ? XFS_TRANS_RESERVE : 0, &args.trans);
254         if (error)
255                 return error;
256
257         xfs_ilock(dp, XFS_ILOCK_EXCL);
258         error = xfs_trans_reserve_quota_nblks(args.trans, dp, args.total, 0,
259                                 rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
260                                        XFS_QMOPT_RES_REGBLKS);
261         if (error) {
262                 xfs_iunlock(dp, XFS_ILOCK_EXCL);
263                 xfs_trans_cancel(args.trans);
264                 return error;
265         }
266
267         xfs_trans_ijoin(args.trans, dp, 0);
268
269         /*
270          * If the attribute list is non-existent or a shortform list,
271          * upgrade it to a single-leaf-block attribute list.
272          */
273         if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL ||
274             (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
275              dp->i_d.di_anextents == 0)) {
276
277                 /*
278                  * Build initial attribute list (if required).
279                  */
280                 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
281                         xfs_attr_shortform_create(&args);
282
283                 /*
284                  * Try to add the attr to the attribute list in
285                  * the inode.
286                  */
287                 error = xfs_attr_shortform_addname(&args);
288                 if (error != -ENOSPC) {
289                         /*
290                          * Commit the shortform mods, and we're done.
291                          * NOTE: this is also the error path (EEXIST, etc).
292                          */
293                         ASSERT(args.trans != NULL);
294
295                         /*
296                          * If this is a synchronous mount, make sure that
297                          * the transaction goes to disk before returning
298                          * to the user.
299                          */
300                         if (mp->m_flags & XFS_MOUNT_WSYNC)
301                                 xfs_trans_set_sync(args.trans);
302
303                         if (!error && (flags & ATTR_KERNOTIME) == 0) {
304                                 xfs_trans_ichgtime(args.trans, dp,
305                                                         XFS_ICHGTIME_CHG);
306                         }
307                         err2 = xfs_trans_commit(args.trans);
308                         xfs_iunlock(dp, XFS_ILOCK_EXCL);
309
310                         return error ? error : err2;
311                 }
312
313                 /*
314                  * It won't fit in the shortform, transform to a leaf block.
315                  * GROT: another possible req'mt for a double-split btree op.
316                  */
317                 xfs_defer_init(args.dfops, args.firstblock);
318                 error = xfs_attr_shortform_to_leaf(&args);
319                 if (!error)
320                         error = xfs_defer_finish(&args.trans, args.dfops, dp);
321                 if (error) {
322                         args.trans = NULL;
323                         xfs_defer_cancel(&dfops);
324                         goto out;
325                 }
326
327                 /*
328                  * Commit the leaf transformation.  We'll need another (linked)
329                  * transaction to add the new attribute to the leaf.
330                  */
331
332                 error = xfs_trans_roll(&args.trans, dp);
333                 if (error)
334                         goto out;
335
336         }
337
338         if (xfs_bmap_one_block(dp, XFS_ATTR_FORK))
339                 error = xfs_attr_leaf_addname(&args);
340         else
341                 error = xfs_attr_node_addname(&args);
342         if (error)
343                 goto out;
344
345         /*
346          * If this is a synchronous mount, make sure that the
347          * transaction goes to disk before returning to the user.
348          */
349         if (mp->m_flags & XFS_MOUNT_WSYNC)
350                 xfs_trans_set_sync(args.trans);
351
352         if ((flags & ATTR_KERNOTIME) == 0)
353                 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
354
355         /*
356          * Commit the last in the sequence of transactions.
357          */
358         xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
359         error = xfs_trans_commit(args.trans);
360         xfs_iunlock(dp, XFS_ILOCK_EXCL);
361
362         return error;
363
364 out:
365         if (args.trans)
366                 xfs_trans_cancel(args.trans);
367         xfs_iunlock(dp, XFS_ILOCK_EXCL);
368         return error;
369 }
370
371 /*
372  * Generic handler routine to remove a name from an attribute list.
373  * Transitions attribute list from Btree to shortform as necessary.
374  */
375 int
376 xfs_attr_remove(
377         struct xfs_inode        *dp,
378         const unsigned char     *name,
379         int                     flags)
380 {
381         struct xfs_mount        *mp = dp->i_mount;
382         struct xfs_da_args      args;
383         struct xfs_defer_ops    dfops;
384         xfs_fsblock_t           firstblock;
385         int                     error;
386
387         XFS_STATS_INC(mp, xs_attr_remove);
388
389         if (XFS_FORCED_SHUTDOWN(dp->i_mount))
390                 return -EIO;
391
392         error = xfs_attr_args_init(&args, dp, name, flags);
393         if (error)
394                 return error;
395
396         args.firstblock = &firstblock;
397         args.dfops = &dfops;
398
399         /*
400          * we have no control over the attribute names that userspace passes us
401          * to remove, so we have to allow the name lookup prior to attribute
402          * removal to fail.
403          */
404         args.op_flags = XFS_DA_OP_OKNOENT;
405
406         error = xfs_qm_dqattach(dp, 0);
407         if (error)
408                 return error;
409
410         /*
411          * Root fork attributes can use reserved data blocks for this
412          * operation if necessary
413          */
414         error = xfs_trans_alloc(mp, &M_RES(mp)->tr_attrrm,
415                         XFS_ATTRRM_SPACE_RES(mp), 0,
416                         (flags & ATTR_ROOT) ? XFS_TRANS_RESERVE : 0,
417                         &args.trans);
418         if (error)
419                 return error;
420
421         xfs_ilock(dp, XFS_ILOCK_EXCL);
422         /*
423          * No need to make quota reservations here. We expect to release some
424          * blocks not allocate in the common case.
425          */
426         xfs_trans_ijoin(args.trans, dp, 0);
427
428         if (!xfs_inode_hasattr(dp)) {
429                 error = -ENOATTR;
430         } else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
431                 ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
432                 error = xfs_attr_shortform_remove(&args);
433         } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
434                 error = xfs_attr_leaf_removename(&args);
435         } else {
436                 error = xfs_attr_node_removename(&args);
437         }
438
439         if (error)
440                 goto out;
441
442         /*
443          * If this is a synchronous mount, make sure that the
444          * transaction goes to disk before returning to the user.
445          */
446         if (mp->m_flags & XFS_MOUNT_WSYNC)
447                 xfs_trans_set_sync(args.trans);
448
449         if ((flags & ATTR_KERNOTIME) == 0)
450                 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
451
452         /*
453          * Commit the last in the sequence of transactions.
454          */
455         xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
456         error = xfs_trans_commit(args.trans);
457         xfs_iunlock(dp, XFS_ILOCK_EXCL);
458
459         return error;
460
461 out:
462         if (args.trans)
463                 xfs_trans_cancel(args.trans);
464         xfs_iunlock(dp, XFS_ILOCK_EXCL);
465         return error;
466 }
467
468 /*========================================================================
469  * External routines when attribute list is inside the inode
470  *========================================================================*/
471
472 /*
473  * Add a name to the shortform attribute list structure
474  * This is the external routine.
475  */
476 STATIC int
477 xfs_attr_shortform_addname(xfs_da_args_t *args)
478 {
479         int newsize, forkoff, retval;
480
481         trace_xfs_attr_sf_addname(args);
482
483         retval = xfs_attr_shortform_lookup(args);
484         if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
485                 return retval;
486         } else if (retval == -EEXIST) {
487                 if (args->flags & ATTR_CREATE)
488                         return retval;
489                 retval = xfs_attr_shortform_remove(args);
490                 if (retval)
491                         return retval;
492                 /*
493                  * Since we have removed the old attr, clear ATTR_REPLACE so
494                  * that the leaf format add routine won't trip over the attr
495                  * not being around.
496                  */
497                 args->flags &= ~ATTR_REPLACE;
498         }
499
500         if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
501             args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
502                 return -ENOSPC;
503
504         newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
505         newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
506
507         forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
508         if (!forkoff)
509                 return -ENOSPC;
510
511         xfs_attr_shortform_add(args, forkoff);
512         return 0;
513 }
514
515
516 /*========================================================================
517  * External routines when attribute list is one block
518  *========================================================================*/
519
520 /*
521  * Add a name to the leaf attribute list structure
522  *
523  * This leaf block cannot have a "remote" value, we only call this routine
524  * if bmap_one_block() says there is only one block (ie: no remote blks).
525  */
526 STATIC int
527 xfs_attr_leaf_addname(xfs_da_args_t *args)
528 {
529         xfs_inode_t *dp;
530         struct xfs_buf *bp;
531         int retval, error, forkoff;
532
533         trace_xfs_attr_leaf_addname(args);
534
535         /*
536          * Read the (only) block in the attribute list in.
537          */
538         dp = args->dp;
539         args->blkno = 0;
540         error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
541         if (error)
542                 return error;
543
544         /*
545          * Look up the given attribute in the leaf block.  Figure out if
546          * the given flags produce an error or call for an atomic rename.
547          */
548         retval = xfs_attr3_leaf_lookup_int(bp, args);
549         if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
550                 xfs_trans_brelse(args->trans, bp);
551                 return retval;
552         } else if (retval == -EEXIST) {
553                 if (args->flags & ATTR_CREATE) {        /* pure create op */
554                         xfs_trans_brelse(args->trans, bp);
555                         return retval;
556                 }
557
558                 trace_xfs_attr_leaf_replace(args);
559
560                 /* save the attribute state for later removal*/
561                 args->op_flags |= XFS_DA_OP_RENAME;     /* an atomic rename */
562                 args->blkno2 = args->blkno;             /* set 2nd entry info*/
563                 args->index2 = args->index;
564                 args->rmtblkno2 = args->rmtblkno;
565                 args->rmtblkcnt2 = args->rmtblkcnt;
566                 args->rmtvaluelen2 = args->rmtvaluelen;
567
568                 /*
569                  * clear the remote attr state now that it is saved so that the
570                  * values reflect the state of the attribute we are about to
571                  * add, not the attribute we just found and will remove later.
572                  */
573                 args->rmtblkno = 0;
574                 args->rmtblkcnt = 0;
575                 args->rmtvaluelen = 0;
576         }
577
578         /*
579          * Add the attribute to the leaf block, transitioning to a Btree
580          * if required.
581          */
582         retval = xfs_attr3_leaf_add(bp, args);
583         if (retval == -ENOSPC) {
584                 /*
585                  * Promote the attribute list to the Btree format, then
586                  * Commit that transaction so that the node_addname() call
587                  * can manage its own transactions.
588                  */
589                 xfs_defer_init(args->dfops, args->firstblock);
590                 error = xfs_attr3_leaf_to_node(args);
591                 if (!error)
592                         error = xfs_defer_finish(&args->trans, args->dfops, dp);
593                 if (error) {
594                         args->trans = NULL;
595                         xfs_defer_cancel(args->dfops);
596                         return error;
597                 }
598
599                 /*
600                  * Commit the current trans (including the inode) and start
601                  * a new one.
602                  */
603                 error = xfs_trans_roll(&args->trans, dp);
604                 if (error)
605                         return error;
606
607                 /*
608                  * Fob the whole rest of the problem off on the Btree code.
609                  */
610                 error = xfs_attr_node_addname(args);
611                 return error;
612         }
613
614         /*
615          * Commit the transaction that added the attr name so that
616          * later routines can manage their own transactions.
617          */
618         error = xfs_trans_roll(&args->trans, dp);
619         if (error)
620                 return error;
621
622         /*
623          * If there was an out-of-line value, allocate the blocks we
624          * identified for its storage and copy the value.  This is done
625          * after we create the attribute so that we don't overflow the
626          * maximum size of a transaction and/or hit a deadlock.
627          */
628         if (args->rmtblkno > 0) {
629                 error = xfs_attr_rmtval_set(args);
630                 if (error)
631                         return error;
632         }
633
634         /*
635          * If this is an atomic rename operation, we must "flip" the
636          * incomplete flags on the "new" and "old" attribute/value pairs
637          * so that one disappears and one appears atomically.  Then we
638          * must remove the "old" attribute/value pair.
639          */
640         if (args->op_flags & XFS_DA_OP_RENAME) {
641                 /*
642                  * In a separate transaction, set the incomplete flag on the
643                  * "old" attr and clear the incomplete flag on the "new" attr.
644                  */
645                 error = xfs_attr3_leaf_flipflags(args);
646                 if (error)
647                         return error;
648
649                 /*
650                  * Dismantle the "old" attribute/value pair by removing
651                  * a "remote" value (if it exists).
652                  */
653                 args->index = args->index2;
654                 args->blkno = args->blkno2;
655                 args->rmtblkno = args->rmtblkno2;
656                 args->rmtblkcnt = args->rmtblkcnt2;
657                 args->rmtvaluelen = args->rmtvaluelen2;
658                 if (args->rmtblkno) {
659                         error = xfs_attr_rmtval_remove(args);
660                         if (error)
661                                 return error;
662                 }
663
664                 /*
665                  * Read in the block containing the "old" attr, then
666                  * remove the "old" attr from that block (neat, huh!)
667                  */
668                 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
669                                            -1, &bp);
670                 if (error)
671                         return error;
672
673                 xfs_attr3_leaf_remove(bp, args);
674
675                 /*
676                  * If the result is small enough, shrink it all into the inode.
677                  */
678                 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
679                         xfs_defer_init(args->dfops, args->firstblock);
680                         error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
681                         /* bp is gone due to xfs_da_shrink_inode */
682                         if (!error)
683                                 error = xfs_defer_finish(&args->trans,
684                                                         args->dfops, dp);
685                         if (error) {
686                                 args->trans = NULL;
687                                 xfs_defer_cancel(args->dfops);
688                                 return error;
689                         }
690                 }
691
692                 /*
693                  * Commit the remove and start the next trans in series.
694                  */
695                 error = xfs_trans_roll(&args->trans, dp);
696
697         } else if (args->rmtblkno > 0) {
698                 /*
699                  * Added a "remote" value, just clear the incomplete flag.
700                  */
701                 error = xfs_attr3_leaf_clearflag(args);
702         }
703         return error;
704 }
705
706 /*
707  * Remove a name from the leaf attribute list structure
708  *
709  * This leaf block cannot have a "remote" value, we only call this routine
710  * if bmap_one_block() says there is only one block (ie: no remote blks).
711  */
712 STATIC int
713 xfs_attr_leaf_removename(xfs_da_args_t *args)
714 {
715         xfs_inode_t *dp;
716         struct xfs_buf *bp;
717         int error, forkoff;
718
719         trace_xfs_attr_leaf_removename(args);
720
721         /*
722          * Remove the attribute.
723          */
724         dp = args->dp;
725         args->blkno = 0;
726         error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
727         if (error)
728                 return error;
729
730         error = xfs_attr3_leaf_lookup_int(bp, args);
731         if (error == -ENOATTR) {
732                 xfs_trans_brelse(args->trans, bp);
733                 return error;
734         }
735
736         xfs_attr3_leaf_remove(bp, args);
737
738         /*
739          * If the result is small enough, shrink it all into the inode.
740          */
741         if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
742                 xfs_defer_init(args->dfops, args->firstblock);
743                 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
744                 /* bp is gone due to xfs_da_shrink_inode */
745                 if (!error)
746                         error = xfs_defer_finish(&args->trans, args->dfops, dp);
747                 if (error) {
748                         args->trans = NULL;
749                         xfs_defer_cancel(args->dfops);
750                         return error;
751                 }
752         }
753         return 0;
754 }
755
756 /*
757  * Look up a name in a leaf attribute list structure.
758  *
759  * This leaf block cannot have a "remote" value, we only call this routine
760  * if bmap_one_block() says there is only one block (ie: no remote blks).
761  */
762 STATIC int
763 xfs_attr_leaf_get(xfs_da_args_t *args)
764 {
765         struct xfs_buf *bp;
766         int error;
767
768         trace_xfs_attr_leaf_get(args);
769
770         args->blkno = 0;
771         error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
772         if (error)
773                 return error;
774
775         error = xfs_attr3_leaf_lookup_int(bp, args);
776         if (error != -EEXIST)  {
777                 xfs_trans_brelse(args->trans, bp);
778                 return error;
779         }
780         error = xfs_attr3_leaf_getvalue(bp, args);
781         xfs_trans_brelse(args->trans, bp);
782         if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
783                 error = xfs_attr_rmtval_get(args);
784         }
785         return error;
786 }
787
788 /*========================================================================
789  * External routines when attribute list size > geo->blksize
790  *========================================================================*/
791
792 /*
793  * Add a name to a Btree-format attribute list.
794  *
795  * This will involve walking down the Btree, and may involve splitting
796  * leaf nodes and even splitting intermediate nodes up to and including
797  * the root node (a special case of an intermediate node).
798  *
799  * "Remote" attribute values confuse the issue and atomic rename operations
800  * add a whole extra layer of confusion on top of that.
801  */
802 STATIC int
803 xfs_attr_node_addname(xfs_da_args_t *args)
804 {
805         xfs_da_state_t *state;
806         xfs_da_state_blk_t *blk;
807         xfs_inode_t *dp;
808         xfs_mount_t *mp;
809         int retval, error;
810
811         trace_xfs_attr_node_addname(args);
812
813         /*
814          * Fill in bucket of arguments/results/context to carry around.
815          */
816         dp = args->dp;
817         mp = dp->i_mount;
818 restart:
819         state = xfs_da_state_alloc();
820         state->args = args;
821         state->mp = mp;
822
823         /*
824          * Search to see if name already exists, and get back a pointer
825          * to where it should go.
826          */
827         error = xfs_da3_node_lookup_int(state, &retval);
828         if (error)
829                 goto out;
830         blk = &state->path.blk[ state->path.active-1 ];
831         ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
832         if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
833                 goto out;
834         } else if (retval == -EEXIST) {
835                 if (args->flags & ATTR_CREATE)
836                         goto out;
837
838                 trace_xfs_attr_node_replace(args);
839
840                 /* save the attribute state for later removal*/
841                 args->op_flags |= XFS_DA_OP_RENAME;     /* atomic rename op */
842                 args->blkno2 = args->blkno;             /* set 2nd entry info*/
843                 args->index2 = args->index;
844                 args->rmtblkno2 = args->rmtblkno;
845                 args->rmtblkcnt2 = args->rmtblkcnt;
846                 args->rmtvaluelen2 = args->rmtvaluelen;
847
848                 /*
849                  * clear the remote attr state now that it is saved so that the
850                  * values reflect the state of the attribute we are about to
851                  * add, not the attribute we just found and will remove later.
852                  */
853                 args->rmtblkno = 0;
854                 args->rmtblkcnt = 0;
855                 args->rmtvaluelen = 0;
856         }
857
858         retval = xfs_attr3_leaf_add(blk->bp, state->args);
859         if (retval == -ENOSPC) {
860                 if (state->path.active == 1) {
861                         /*
862                          * Its really a single leaf node, but it had
863                          * out-of-line values so it looked like it *might*
864                          * have been a b-tree.
865                          */
866                         xfs_da_state_free(state);
867                         state = NULL;
868                         xfs_defer_init(args->dfops, args->firstblock);
869                         error = xfs_attr3_leaf_to_node(args);
870                         if (!error)
871                                 error = xfs_defer_finish(&args->trans,
872                                                         args->dfops, dp);
873                         if (error) {
874                                 args->trans = NULL;
875                                 xfs_defer_cancel(args->dfops);
876                                 goto out;
877                         }
878
879                         /*
880                          * Commit the node conversion and start the next
881                          * trans in the chain.
882                          */
883                         error = xfs_trans_roll(&args->trans, dp);
884                         if (error)
885                                 goto out;
886
887                         goto restart;
888                 }
889
890                 /*
891                  * Split as many Btree elements as required.
892                  * This code tracks the new and old attr's location
893                  * in the index/blkno/rmtblkno/rmtblkcnt fields and
894                  * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
895                  */
896                 xfs_defer_init(args->dfops, args->firstblock);
897                 error = xfs_da3_split(state);
898                 if (!error)
899                         error = xfs_defer_finish(&args->trans, args->dfops, dp);
900                 if (error) {
901                         args->trans = NULL;
902                         xfs_defer_cancel(args->dfops);
903                         goto out;
904                 }
905         } else {
906                 /*
907                  * Addition succeeded, update Btree hashvals.
908                  */
909                 xfs_da3_fixhashpath(state, &state->path);
910         }
911
912         /*
913          * Kill the state structure, we're done with it and need to
914          * allow the buffers to come back later.
915          */
916         xfs_da_state_free(state);
917         state = NULL;
918
919         /*
920          * Commit the leaf addition or btree split and start the next
921          * trans in the chain.
922          */
923         error = xfs_trans_roll(&args->trans, dp);
924         if (error)
925                 goto out;
926
927         /*
928          * If there was an out-of-line value, allocate the blocks we
929          * identified for its storage and copy the value.  This is done
930          * after we create the attribute so that we don't overflow the
931          * maximum size of a transaction and/or hit a deadlock.
932          */
933         if (args->rmtblkno > 0) {
934                 error = xfs_attr_rmtval_set(args);
935                 if (error)
936                         return error;
937         }
938
939         /*
940          * If this is an atomic rename operation, we must "flip" the
941          * incomplete flags on the "new" and "old" attribute/value pairs
942          * so that one disappears and one appears atomically.  Then we
943          * must remove the "old" attribute/value pair.
944          */
945         if (args->op_flags & XFS_DA_OP_RENAME) {
946                 /*
947                  * In a separate transaction, set the incomplete flag on the
948                  * "old" attr and clear the incomplete flag on the "new" attr.
949                  */
950                 error = xfs_attr3_leaf_flipflags(args);
951                 if (error)
952                         goto out;
953
954                 /*
955                  * Dismantle the "old" attribute/value pair by removing
956                  * a "remote" value (if it exists).
957                  */
958                 args->index = args->index2;
959                 args->blkno = args->blkno2;
960                 args->rmtblkno = args->rmtblkno2;
961                 args->rmtblkcnt = args->rmtblkcnt2;
962                 args->rmtvaluelen = args->rmtvaluelen2;
963                 if (args->rmtblkno) {
964                         error = xfs_attr_rmtval_remove(args);
965                         if (error)
966                                 return error;
967                 }
968
969                 /*
970                  * Re-find the "old" attribute entry after any split ops.
971                  * The INCOMPLETE flag means that we will find the "old"
972                  * attr, not the "new" one.
973                  */
974                 args->flags |= XFS_ATTR_INCOMPLETE;
975                 state = xfs_da_state_alloc();
976                 state->args = args;
977                 state->mp = mp;
978                 state->inleaf = 0;
979                 error = xfs_da3_node_lookup_int(state, &retval);
980                 if (error)
981                         goto out;
982
983                 /*
984                  * Remove the name and update the hashvals in the tree.
985                  */
986                 blk = &state->path.blk[ state->path.active-1 ];
987                 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
988                 error = xfs_attr3_leaf_remove(blk->bp, args);
989                 xfs_da3_fixhashpath(state, &state->path);
990
991                 /*
992                  * Check to see if the tree needs to be collapsed.
993                  */
994                 if (retval && (state->path.active > 1)) {
995                         xfs_defer_init(args->dfops, args->firstblock);
996                         error = xfs_da3_join(state);
997                         if (!error)
998                                 error = xfs_defer_finish(&args->trans,
999                                                         args->dfops, dp);
1000                         if (error) {
1001                                 args->trans = NULL;
1002                                 xfs_defer_cancel(args->dfops);
1003                                 goto out;
1004                         }
1005                 }
1006
1007                 /*
1008                  * Commit and start the next trans in the chain.
1009                  */
1010                 error = xfs_trans_roll(&args->trans, dp);
1011                 if (error)
1012                         goto out;
1013
1014         } else if (args->rmtblkno > 0) {
1015                 /*
1016                  * Added a "remote" value, just clear the incomplete flag.
1017                  */
1018                 error = xfs_attr3_leaf_clearflag(args);
1019                 if (error)
1020                         goto out;
1021         }
1022         retval = error = 0;
1023
1024 out:
1025         if (state)
1026                 xfs_da_state_free(state);
1027         if (error)
1028                 return error;
1029         return retval;
1030 }
1031
1032 /*
1033  * Remove a name from a B-tree attribute list.
1034  *
1035  * This will involve walking down the Btree, and may involve joining
1036  * leaf nodes and even joining intermediate nodes up to and including
1037  * the root node (a special case of an intermediate node).
1038  */
1039 STATIC int
1040 xfs_attr_node_removename(xfs_da_args_t *args)
1041 {
1042         xfs_da_state_t *state;
1043         xfs_da_state_blk_t *blk;
1044         xfs_inode_t *dp;
1045         struct xfs_buf *bp;
1046         int retval, error, forkoff;
1047
1048         trace_xfs_attr_node_removename(args);
1049
1050         /*
1051          * Tie a string around our finger to remind us where we are.
1052          */
1053         dp = args->dp;
1054         state = xfs_da_state_alloc();
1055         state->args = args;
1056         state->mp = dp->i_mount;
1057
1058         /*
1059          * Search to see if name exists, and get back a pointer to it.
1060          */
1061         error = xfs_da3_node_lookup_int(state, &retval);
1062         if (error || (retval != -EEXIST)) {
1063                 if (error == 0)
1064                         error = retval;
1065                 goto out;
1066         }
1067
1068         /*
1069          * If there is an out-of-line value, de-allocate the blocks.
1070          * This is done before we remove the attribute so that we don't
1071          * overflow the maximum size of a transaction and/or hit a deadlock.
1072          */
1073         blk = &state->path.blk[ state->path.active-1 ];
1074         ASSERT(blk->bp != NULL);
1075         ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1076         if (args->rmtblkno > 0) {
1077                 /*
1078                  * Fill in disk block numbers in the state structure
1079                  * so that we can get the buffers back after we commit
1080                  * several transactions in the following calls.
1081                  */
1082                 error = xfs_attr_fillstate(state);
1083                 if (error)
1084                         goto out;
1085
1086                 /*
1087                  * Mark the attribute as INCOMPLETE, then bunmapi() the
1088                  * remote value.
1089                  */
1090                 error = xfs_attr3_leaf_setflag(args);
1091                 if (error)
1092                         goto out;
1093                 error = xfs_attr_rmtval_remove(args);
1094                 if (error)
1095                         goto out;
1096
1097                 /*
1098                  * Refill the state structure with buffers, the prior calls
1099                  * released our buffers.
1100                  */
1101                 error = xfs_attr_refillstate(state);
1102                 if (error)
1103                         goto out;
1104         }
1105
1106         /*
1107          * Remove the name and update the hashvals in the tree.
1108          */
1109         blk = &state->path.blk[ state->path.active-1 ];
1110         ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1111         retval = xfs_attr3_leaf_remove(blk->bp, args);
1112         xfs_da3_fixhashpath(state, &state->path);
1113
1114         /*
1115          * Check to see if the tree needs to be collapsed.
1116          */
1117         if (retval && (state->path.active > 1)) {
1118                 xfs_defer_init(args->dfops, args->firstblock);
1119                 error = xfs_da3_join(state);
1120                 if (!error)
1121                         error = xfs_defer_finish(&args->trans, args->dfops, dp);
1122                 if (error) {
1123                         args->trans = NULL;
1124                         xfs_defer_cancel(args->dfops);
1125                         goto out;
1126                 }
1127                 /*
1128                  * Commit the Btree join operation and start a new trans.
1129                  */
1130                 error = xfs_trans_roll(&args->trans, dp);
1131                 if (error)
1132                         goto out;
1133         }
1134
1135         /*
1136          * If the result is small enough, push it all into the inode.
1137          */
1138         if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
1139                 /*
1140                  * Have to get rid of the copy of this dabuf in the state.
1141                  */
1142                 ASSERT(state->path.active == 1);
1143                 ASSERT(state->path.blk[0].bp);
1144                 state->path.blk[0].bp = NULL;
1145
1146                 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, -1, &bp);
1147                 if (error)
1148                         goto out;
1149
1150                 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
1151                         xfs_defer_init(args->dfops, args->firstblock);
1152                         error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
1153                         /* bp is gone due to xfs_da_shrink_inode */
1154                         if (!error)
1155                                 error = xfs_defer_finish(&args->trans,
1156                                                         args->dfops, dp);
1157                         if (error) {
1158                                 args->trans = NULL;
1159                                 xfs_defer_cancel(args->dfops);
1160                                 goto out;
1161                         }
1162                 } else
1163                         xfs_trans_brelse(args->trans, bp);
1164         }
1165         error = 0;
1166
1167 out:
1168         xfs_da_state_free(state);
1169         return error;
1170 }
1171
1172 /*
1173  * Fill in the disk block numbers in the state structure for the buffers
1174  * that are attached to the state structure.
1175  * This is done so that we can quickly reattach ourselves to those buffers
1176  * after some set of transaction commits have released these buffers.
1177  */
1178 STATIC int
1179 xfs_attr_fillstate(xfs_da_state_t *state)
1180 {
1181         xfs_da_state_path_t *path;
1182         xfs_da_state_blk_t *blk;
1183         int level;
1184
1185         trace_xfs_attr_fillstate(state->args);
1186
1187         /*
1188          * Roll down the "path" in the state structure, storing the on-disk
1189          * block number for those buffers in the "path".
1190          */
1191         path = &state->path;
1192         ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1193         for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1194                 if (blk->bp) {
1195                         blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
1196                         blk->bp = NULL;
1197                 } else {
1198                         blk->disk_blkno = 0;
1199                 }
1200         }
1201
1202         /*
1203          * Roll down the "altpath" in the state structure, storing the on-disk
1204          * block number for those buffers in the "altpath".
1205          */
1206         path = &state->altpath;
1207         ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1208         for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1209                 if (blk->bp) {
1210                         blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
1211                         blk->bp = NULL;
1212                 } else {
1213                         blk->disk_blkno = 0;
1214                 }
1215         }
1216
1217         return 0;
1218 }
1219
1220 /*
1221  * Reattach the buffers to the state structure based on the disk block
1222  * numbers stored in the state structure.
1223  * This is done after some set of transaction commits have released those
1224  * buffers from our grip.
1225  */
1226 STATIC int
1227 xfs_attr_refillstate(xfs_da_state_t *state)
1228 {
1229         xfs_da_state_path_t *path;
1230         xfs_da_state_blk_t *blk;
1231         int level, error;
1232
1233         trace_xfs_attr_refillstate(state->args);
1234
1235         /*
1236          * Roll down the "path" in the state structure, storing the on-disk
1237          * block number for those buffers in the "path".
1238          */
1239         path = &state->path;
1240         ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1241         for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1242                 if (blk->disk_blkno) {
1243                         error = xfs_da3_node_read(state->args->trans,
1244                                                 state->args->dp,
1245                                                 blk->blkno, blk->disk_blkno,
1246                                                 &blk->bp, XFS_ATTR_FORK);
1247                         if (error)
1248                                 return error;
1249                 } else {
1250                         blk->bp = NULL;
1251                 }
1252         }
1253
1254         /*
1255          * Roll down the "altpath" in the state structure, storing the on-disk
1256          * block number for those buffers in the "altpath".
1257          */
1258         path = &state->altpath;
1259         ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1260         for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1261                 if (blk->disk_blkno) {
1262                         error = xfs_da3_node_read(state->args->trans,
1263                                                 state->args->dp,
1264                                                 blk->blkno, blk->disk_blkno,
1265                                                 &blk->bp, XFS_ATTR_FORK);
1266                         if (error)
1267                                 return error;
1268                 } else {
1269                         blk->bp = NULL;
1270                 }
1271         }
1272
1273         return 0;
1274 }
1275
1276 /*
1277  * Look up a filename in a node attribute list.
1278  *
1279  * This routine gets called for any attribute fork that has more than one
1280  * block, ie: both true Btree attr lists and for single-leaf-blocks with
1281  * "remote" values taking up more blocks.
1282  */
1283 STATIC int
1284 xfs_attr_node_get(xfs_da_args_t *args)
1285 {
1286         xfs_da_state_t *state;
1287         xfs_da_state_blk_t *blk;
1288         int error, retval;
1289         int i;
1290
1291         trace_xfs_attr_node_get(args);
1292
1293         state = xfs_da_state_alloc();
1294         state->args = args;
1295         state->mp = args->dp->i_mount;
1296
1297         /*
1298          * Search to see if name exists, and get back a pointer to it.
1299          */
1300         error = xfs_da3_node_lookup_int(state, &retval);
1301         if (error) {
1302                 retval = error;
1303         } else if (retval == -EEXIST) {
1304                 blk = &state->path.blk[ state->path.active-1 ];
1305                 ASSERT(blk->bp != NULL);
1306                 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1307
1308                 /*
1309                  * Get the value, local or "remote"
1310                  */
1311                 retval = xfs_attr3_leaf_getvalue(blk->bp, args);
1312                 if (!retval && (args->rmtblkno > 0)
1313                     && !(args->flags & ATTR_KERNOVAL)) {
1314                         retval = xfs_attr_rmtval_get(args);
1315                 }
1316         }
1317
1318         /*
1319          * If not in a transaction, we have to release all the buffers.
1320          */
1321         for (i = 0; i < state->path.active; i++) {
1322                 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
1323                 state->path.blk[i].bp = NULL;
1324         }
1325
1326         xfs_da_state_free(state);
1327         return retval;
1328 }