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