]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libxfs/xfs_attr.c
xfs: refactor xfs_trans_roll
[thirdparty/xfsprogs-dev.git] / 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 "libxfs_priv.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_bmap.h"
34 #include "xfs_bmap_btree.h"
35 #include "xfs_attr_leaf.h"
36 #include "xfs_attr_remote.h"
37 #include "xfs_trans_space.h"
38 #include "xfs_trace.h"
39
40 /*
41 * xfs_attr.c
42 *
43 * Provide the external interfaces to manage attribute lists.
44 */
45
46 /*========================================================================
47 * Function prototypes for the kernel.
48 *========================================================================*/
49
50 /*
51 * Internal routines when attribute list fits inside the inode.
52 */
53 STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
54
55 /*
56 * Internal routines when attribute list is one block.
57 */
58 STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
59 STATIC int xfs_attr_leaf_addname(xfs_da_args_t *args);
60 STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
61
62 /*
63 * Internal routines when attribute list is more than one block.
64 */
65 STATIC int xfs_attr_node_get(xfs_da_args_t *args);
66 STATIC int xfs_attr_node_addname(xfs_da_args_t *args);
67 STATIC int xfs_attr_node_removename(xfs_da_args_t *args);
68 STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
69 STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
70
71
72 STATIC int
73 xfs_attr_args_init(
74 struct xfs_da_args *args,
75 struct xfs_inode *dp,
76 const unsigned char *name,
77 int flags)
78 {
79
80 if (!name)
81 return -EINVAL;
82
83 memset(args, 0, sizeof(*args));
84 args->geo = dp->i_mount->m_attr_geo;
85 args->whichfork = XFS_ATTR_FORK;
86 args->dp = dp;
87 args->flags = flags;
88 args->name = name;
89 args->namelen = strlen((const char *)name);
90 if (args->namelen >= MAXNAMELEN)
91 return -EFAULT; /* match IRIX behaviour */
92
93 args->hashval = xfs_da_hashname(args->name, args->namelen);
94 return 0;
95 }
96
97 int
98 xfs_inode_hasattr(
99 struct xfs_inode *ip)
100 {
101 if (!XFS_IFORK_Q(ip) ||
102 (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
103 ip->i_d.di_anextents == 0))
104 return 0;
105 return 1;
106 }
107
108 /*========================================================================
109 * Overall external interface routines.
110 *========================================================================*/
111
112 /* Retrieve an extended attribute and its value. Must have ilock. */
113 int
114 xfs_attr_get_ilocked(
115 struct xfs_inode *ip,
116 struct xfs_da_args *args)
117 {
118 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
119
120 if (!xfs_inode_hasattr(ip))
121 return -ENOATTR;
122 else if (ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL)
123 return xfs_attr_shortform_getvalue(args);
124 else if (xfs_bmap_one_block(ip, XFS_ATTR_FORK))
125 return xfs_attr_leaf_get(args);
126 else
127 return xfs_attr_node_get(args);
128 }
129
130 /* Retrieve an extended attribute by name, and its value. */
131 int
132 xfs_attr_get(
133 struct xfs_inode *ip,
134 const unsigned char *name,
135 unsigned char *value,
136 int *valuelenp,
137 int flags)
138 {
139 struct xfs_da_args args;
140 uint lock_mode;
141 int error;
142
143 XFS_STATS_INC(ip->i_mount, xs_attr_get);
144
145 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
146 return -EIO;
147
148 error = xfs_attr_args_init(&args, ip, name, flags);
149 if (error)
150 return error;
151
152 args.value = value;
153 args.valuelen = *valuelenp;
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(ip);
158 error = xfs_attr_get_ilocked(ip, &args);
159 xfs_iunlock(ip, lock_mode);
160
161 *valuelenp = args.valuelen;
162 return error == -EEXIST ? 0 : error;
163 }
164
165 /*
166 * Calculate how many blocks we need for the new attribute,
167 */
168 STATIC int
169 xfs_attr_calc_size(
170 struct xfs_da_args *args,
171 int *local)
172 {
173 struct xfs_mount *mp = args->dp->i_mount;
174 int size;
175 int nblks;
176
177 /*
178 * Determine space new attribute will use, and if it would be
179 * "local" or "remote" (note: local != inline).
180 */
181 size = xfs_attr_leaf_newentsize(args, local);
182 nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
183 if (*local) {
184 if (size > (args->geo->blksize / 2)) {
185 /* Double split possible */
186 nblks *= 2;
187 }
188 } else {
189 /*
190 * Out of line attribute, cannot double split, but
191 * make room for the attribute value itself.
192 */
193 uint dblocks = xfs_attr3_rmt_blocks(mp, args->valuelen);
194 nblks += dblocks;
195 nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
196 }
197
198 return nblks;
199 }
200
201 int
202 xfs_attr_set(
203 struct xfs_inode *dp,
204 const unsigned char *name,
205 unsigned char *value,
206 int valuelen,
207 int flags)
208 {
209 struct xfs_mount *mp = dp->i_mount;
210 struct xfs_da_args args;
211 struct xfs_defer_ops dfops;
212 struct xfs_trans_res tres;
213 xfs_fsblock_t firstblock;
214 int rsvd = (flags & ATTR_ROOT) != 0;
215 int error, err2, local;
216
217 XFS_STATS_INC(mp, xs_attr_set);
218
219 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
220 return -EIO;
221
222 error = xfs_attr_args_init(&args, dp, name, flags);
223 if (error)
224 return error;
225
226 args.value = value;
227 args.valuelen = valuelen;
228 args.firstblock = &firstblock;
229 args.dfops = &dfops;
230 args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
231 args.total = xfs_attr_calc_size(&args, &local);
232
233 error = xfs_qm_dqattach(dp, 0);
234 if (error)
235 return error;
236
237 /*
238 * If the inode doesn't have an attribute fork, add one.
239 * (inode must not be locked when we call this routine)
240 */
241 if (XFS_IFORK_Q(dp) == 0) {
242 int sf_size = sizeof(xfs_attr_sf_hdr_t) +
243 XFS_ATTR_SF_ENTSIZE_BYNAME(args.namelen, valuelen);
244
245 error = xfs_bmap_add_attrfork(dp, sf_size, rsvd);
246 if (error)
247 return error;
248 }
249
250 tres.tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
251 M_RES(mp)->tr_attrsetrt.tr_logres * args.total;
252 tres.tr_logcount = XFS_ATTRSET_LOG_COUNT;
253 tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
254
255 /*
256 * Root fork attributes can use reserved data blocks for this
257 * operation if necessary
258 */
259 error = xfs_trans_alloc(mp, &tres, args.total, 0,
260 rsvd ? XFS_TRANS_RESERVE : 0, &args.trans);
261 if (error)
262 return error;
263
264 xfs_ilock(dp, XFS_ILOCK_EXCL);
265 error = xfs_trans_reserve_quota_nblks(args.trans, dp, args.total, 0,
266 rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
267 XFS_QMOPT_RES_REGBLKS);
268 if (error) {
269 xfs_iunlock(dp, XFS_ILOCK_EXCL);
270 xfs_trans_cancel(args.trans);
271 return error;
272 }
273
274 xfs_trans_ijoin(args.trans, dp, 0);
275
276 /*
277 * If the attribute list is non-existent or a shortform list,
278 * upgrade it to a single-leaf-block attribute list.
279 */
280 if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL ||
281 (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
282 dp->i_d.di_anextents == 0)) {
283
284 /*
285 * Build initial attribute list (if required).
286 */
287 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
288 xfs_attr_shortform_create(&args);
289
290 /*
291 * Try to add the attr to the attribute list in
292 * the inode.
293 */
294 error = xfs_attr_shortform_addname(&args);
295 if (error != -ENOSPC) {
296 /*
297 * Commit the shortform mods, and we're done.
298 * NOTE: this is also the error path (EEXIST, etc).
299 */
300 ASSERT(args.trans != NULL);
301
302 /*
303 * If this is a synchronous mount, make sure that
304 * the transaction goes to disk before returning
305 * to the user.
306 */
307 if (mp->m_flags & XFS_MOUNT_WSYNC)
308 xfs_trans_set_sync(args.trans);
309
310 if (!error && (flags & ATTR_KERNOTIME) == 0) {
311 xfs_trans_ichgtime(args.trans, dp,
312 XFS_ICHGTIME_CHG);
313 }
314 err2 = xfs_trans_commit(args.trans);
315 xfs_iunlock(dp, XFS_ILOCK_EXCL);
316
317 return error ? error : err2;
318 }
319
320 /*
321 * It won't fit in the shortform, transform to a leaf block.
322 * GROT: another possible req'mt for a double-split btree op.
323 */
324 xfs_defer_init(args.dfops, args.firstblock);
325 error = xfs_attr_shortform_to_leaf(&args);
326 if (!error)
327 error = xfs_defer_finish(&args.trans, args.dfops, dp);
328 if (error) {
329 args.trans = NULL;
330 xfs_defer_cancel(&dfops);
331 goto out;
332 }
333
334 /*
335 * Commit the leaf transformation. We'll need another (linked)
336 * transaction to add the new attribute to the leaf.
337 */
338
339 error = xfs_trans_roll_inode(&args.trans, dp);
340 if (error)
341 goto out;
342
343 }
344
345 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK))
346 error = xfs_attr_leaf_addname(&args);
347 else
348 error = xfs_attr_node_addname(&args);
349 if (error)
350 goto out;
351
352 /*
353 * If this is a synchronous mount, make sure that the
354 * transaction goes to disk before returning to the user.
355 */
356 if (mp->m_flags & XFS_MOUNT_WSYNC)
357 xfs_trans_set_sync(args.trans);
358
359 if ((flags & ATTR_KERNOTIME) == 0)
360 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
361
362 /*
363 * Commit the last in the sequence of transactions.
364 */
365 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
366 error = xfs_trans_commit(args.trans);
367 xfs_iunlock(dp, XFS_ILOCK_EXCL);
368
369 return error;
370
371 out:
372 if (args.trans)
373 xfs_trans_cancel(args.trans);
374 xfs_iunlock(dp, XFS_ILOCK_EXCL);
375 return error;
376 }
377
378 /*
379 * Generic handler routine to remove a name from an attribute list.
380 * Transitions attribute list from Btree to shortform as necessary.
381 */
382 int
383 xfs_attr_remove(
384 struct xfs_inode *dp,
385 const unsigned char *name,
386 int flags)
387 {
388 struct xfs_mount *mp = dp->i_mount;
389 struct xfs_da_args args;
390 struct xfs_defer_ops dfops;
391 xfs_fsblock_t firstblock;
392 int error;
393
394 XFS_STATS_INC(mp, xs_attr_remove);
395
396 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
397 return -EIO;
398
399 error = xfs_attr_args_init(&args, dp, name, flags);
400 if (error)
401 return error;
402
403 args.firstblock = &firstblock;
404 args.dfops = &dfops;
405
406 /*
407 * we have no control over the attribute names that userspace passes us
408 * to remove, so we have to allow the name lookup prior to attribute
409 * removal to fail.
410 */
411 args.op_flags = XFS_DA_OP_OKNOENT;
412
413 error = xfs_qm_dqattach(dp, 0);
414 if (error)
415 return error;
416
417 /*
418 * Root fork attributes can use reserved data blocks for this
419 * operation if necessary
420 */
421 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_attrrm,
422 XFS_ATTRRM_SPACE_RES(mp), 0,
423 (flags & ATTR_ROOT) ? XFS_TRANS_RESERVE : 0,
424 &args.trans);
425 if (error)
426 return error;
427
428 xfs_ilock(dp, XFS_ILOCK_EXCL);
429 /*
430 * No need to make quota reservations here. We expect to release some
431 * blocks not allocate in the common case.
432 */
433 xfs_trans_ijoin(args.trans, dp, 0);
434
435 if (!xfs_inode_hasattr(dp)) {
436 error = -ENOATTR;
437 } else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
438 ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
439 error = xfs_attr_shortform_remove(&args);
440 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
441 error = xfs_attr_leaf_removename(&args);
442 } else {
443 error = xfs_attr_node_removename(&args);
444 }
445
446 if (error)
447 goto out;
448
449 /*
450 * If this is a synchronous mount, make sure that the
451 * transaction goes to disk before returning to the user.
452 */
453 if (mp->m_flags & XFS_MOUNT_WSYNC)
454 xfs_trans_set_sync(args.trans);
455
456 if ((flags & ATTR_KERNOTIME) == 0)
457 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
458
459 /*
460 * Commit the last in the sequence of transactions.
461 */
462 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
463 error = xfs_trans_commit(args.trans);
464 xfs_iunlock(dp, XFS_ILOCK_EXCL);
465
466 return error;
467
468 out:
469 if (args.trans)
470 xfs_trans_cancel(args.trans);
471 xfs_iunlock(dp, XFS_ILOCK_EXCL);
472 return error;
473 }
474
475 /*========================================================================
476 * External routines when attribute list is inside the inode
477 *========================================================================*/
478
479 /*
480 * Add a name to the shortform attribute list structure
481 * This is the external routine.
482 */
483 STATIC int
484 xfs_attr_shortform_addname(xfs_da_args_t *args)
485 {
486 int newsize, forkoff, retval;
487
488 trace_xfs_attr_sf_addname(args);
489
490 retval = xfs_attr_shortform_lookup(args);
491 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
492 return retval;
493 } else if (retval == -EEXIST) {
494 if (args->flags & ATTR_CREATE)
495 return retval;
496 retval = xfs_attr_shortform_remove(args);
497 ASSERT(retval == 0);
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_inode(&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_inode(&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_inode(&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_inode(&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_inode(&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_inode(&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_inode(&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 }