]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libxfs/xfs_attr.c
xfs: convert to SPDX license tags
[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.dfops = &dfops;
219 args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
220 args.total = xfs_attr_calc_size(&args, &local);
221
222 error = xfs_qm_dqattach(dp);
223 if (error)
224 return error;
225
226 /*
227 * If the inode doesn't have an attribute fork, add one.
228 * (inode must not be locked when we call this routine)
229 */
230 if (XFS_IFORK_Q(dp) == 0) {
231 int sf_size = sizeof(xfs_attr_sf_hdr_t) +
232 XFS_ATTR_SF_ENTSIZE_BYNAME(args.namelen, valuelen);
233
234 error = xfs_bmap_add_attrfork(dp, sf_size, rsvd);
235 if (error)
236 return error;
237 }
238
239 tres.tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
240 M_RES(mp)->tr_attrsetrt.tr_logres * args.total;
241 tres.tr_logcount = XFS_ATTRSET_LOG_COUNT;
242 tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
243
244 /*
245 * Root fork attributes can use reserved data blocks for this
246 * operation if necessary
247 */
248 error = xfs_trans_alloc(mp, &tres, args.total, 0,
249 rsvd ? XFS_TRANS_RESERVE : 0, &args.trans);
250 if (error)
251 return error;
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 xfs_defer_init(args.dfops, args.firstblock);
314 error = xfs_attr_shortform_to_leaf(&args, &leaf_bp);
315 if (error)
316 goto out_defer_cancel;
317 /*
318 * Prevent the leaf buffer from being unlocked so that a
319 * concurrent AIL push cannot grab the half-baked leaf
320 * buffer and run into problems with the write verifier.
321 */
322 xfs_trans_bhold(args.trans, leaf_bp);
323 xfs_defer_bjoin(args.dfops, leaf_bp);
324 xfs_defer_ijoin(args.dfops, dp);
325 error = xfs_defer_finish(&args.trans, args.dfops);
326 if (error)
327 goto out_defer_cancel;
328
329 /*
330 * Commit the leaf transformation. We'll need another (linked)
331 * transaction to add the new attribute to the leaf, which
332 * means that we have to hold & join the leaf buffer here too.
333 */
334 error = xfs_trans_roll_inode(&args.trans, dp);
335 if (error)
336 goto out;
337 xfs_trans_bjoin(args.trans, leaf_bp);
338 leaf_bp = NULL;
339 }
340
341 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK))
342 error = xfs_attr_leaf_addname(&args);
343 else
344 error = xfs_attr_node_addname(&args);
345 if (error)
346 goto out;
347
348 /*
349 * If this is a synchronous mount, make sure that the
350 * transaction goes to disk before returning to the user.
351 */
352 if (mp->m_flags & XFS_MOUNT_WSYNC)
353 xfs_trans_set_sync(args.trans);
354
355 if ((flags & ATTR_KERNOTIME) == 0)
356 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
357
358 /*
359 * Commit the last in the sequence of transactions.
360 */
361 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
362 error = xfs_trans_commit(args.trans);
363 xfs_iunlock(dp, XFS_ILOCK_EXCL);
364
365 return error;
366
367 out_defer_cancel:
368 xfs_defer_cancel(&dfops);
369 out:
370 if (leaf_bp)
371 xfs_trans_brelse(args.trans, leaf_bp);
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);
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 if (retval)
498 return retval;
499 /*
500 * Since we have removed the old attr, clear ATTR_REPLACE so
501 * that the leaf format add routine won't trip over the attr
502 * not being around.
503 */
504 args->flags &= ~ATTR_REPLACE;
505 }
506
507 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
508 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
509 return -ENOSPC;
510
511 newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
512 newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
513
514 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
515 if (!forkoff)
516 return -ENOSPC;
517
518 xfs_attr_shortform_add(args, forkoff);
519 return 0;
520 }
521
522
523 /*========================================================================
524 * External routines when attribute list is one block
525 *========================================================================*/
526
527 /*
528 * Add a name to the leaf attribute list structure
529 *
530 * This leaf block cannot have a "remote" value, we only call this routine
531 * if bmap_one_block() says there is only one block (ie: no remote blks).
532 */
533 STATIC int
534 xfs_attr_leaf_addname(xfs_da_args_t *args)
535 {
536 xfs_inode_t *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(args->dfops, args->firstblock);
597 error = xfs_attr3_leaf_to_node(args);
598 if (error)
599 goto out_defer_cancel;
600 xfs_defer_ijoin(args->dfops, dp);
601 error = xfs_defer_finish(&args->trans, args->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(args->dfops, args->firstblock);
686 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
687 /* bp is gone due to xfs_da_shrink_inode */
688 if (error)
689 goto out_defer_cancel;
690 xfs_defer_ijoin(args->dfops, dp);
691 error = xfs_defer_finish(&args->trans, args->dfops);
692 if (error)
693 goto out_defer_cancel;
694 }
695
696 /*
697 * Commit the remove and start the next trans in series.
698 */
699 error = xfs_trans_roll_inode(&args->trans, dp);
700
701 } else if (args->rmtblkno > 0) {
702 /*
703 * Added a "remote" value, just clear the incomplete flag.
704 */
705 error = xfs_attr3_leaf_clearflag(args);
706 }
707 return error;
708 out_defer_cancel:
709 xfs_defer_cancel(args->dfops);
710 return error;
711 }
712
713 /*
714 * Remove a name from the leaf attribute list structure
715 *
716 * This leaf block cannot have a "remote" value, we only call this routine
717 * if bmap_one_block() says there is only one block (ie: no remote blks).
718 */
719 STATIC int
720 xfs_attr_leaf_removename(xfs_da_args_t *args)
721 {
722 xfs_inode_t *dp;
723 struct xfs_buf *bp;
724 int error, forkoff;
725
726 trace_xfs_attr_leaf_removename(args);
727
728 /*
729 * Remove the attribute.
730 */
731 dp = args->dp;
732 args->blkno = 0;
733 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
734 if (error)
735 return error;
736
737 error = xfs_attr3_leaf_lookup_int(bp, args);
738 if (error == -ENOATTR) {
739 xfs_trans_brelse(args->trans, bp);
740 return error;
741 }
742
743 xfs_attr3_leaf_remove(bp, args);
744
745 /*
746 * If the result is small enough, shrink it all into the inode.
747 */
748 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
749 xfs_defer_init(args->dfops, args->firstblock);
750 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
751 /* bp is gone due to xfs_da_shrink_inode */
752 if (error)
753 goto out_defer_cancel;
754 xfs_defer_ijoin(args->dfops, dp);
755 error = xfs_defer_finish(&args->trans, args->dfops);
756 if (error)
757 goto out_defer_cancel;
758 }
759 return 0;
760 out_defer_cancel:
761 xfs_defer_cancel(args->dfops);
762 return error;
763 }
764
765 /*
766 * Look up a name in a leaf attribute list structure.
767 *
768 * This leaf block cannot have a "remote" value, we only call this routine
769 * if bmap_one_block() says there is only one block (ie: no remote blks).
770 */
771 STATIC int
772 xfs_attr_leaf_get(xfs_da_args_t *args)
773 {
774 struct xfs_buf *bp;
775 int error;
776
777 trace_xfs_attr_leaf_get(args);
778
779 args->blkno = 0;
780 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
781 if (error)
782 return error;
783
784 error = xfs_attr3_leaf_lookup_int(bp, args);
785 if (error != -EEXIST) {
786 xfs_trans_brelse(args->trans, bp);
787 return error;
788 }
789 error = xfs_attr3_leaf_getvalue(bp, args);
790 xfs_trans_brelse(args->trans, bp);
791 if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
792 error = xfs_attr_rmtval_get(args);
793 }
794 return error;
795 }
796
797 /*========================================================================
798 * External routines when attribute list size > geo->blksize
799 *========================================================================*/
800
801 /*
802 * Add a name to a Btree-format attribute list.
803 *
804 * This will involve walking down the Btree, and may involve splitting
805 * leaf nodes and even splitting intermediate nodes up to and including
806 * the root node (a special case of an intermediate node).
807 *
808 * "Remote" attribute values confuse the issue and atomic rename operations
809 * add a whole extra layer of confusion on top of that.
810 */
811 STATIC int
812 xfs_attr_node_addname(xfs_da_args_t *args)
813 {
814 xfs_da_state_t *state;
815 xfs_da_state_blk_t *blk;
816 xfs_inode_t *dp;
817 xfs_mount_t *mp;
818 int retval, error;
819
820 trace_xfs_attr_node_addname(args);
821
822 /*
823 * Fill in bucket of arguments/results/context to carry around.
824 */
825 dp = args->dp;
826 mp = dp->i_mount;
827 restart:
828 state = xfs_da_state_alloc();
829 state->args = args;
830 state->mp = mp;
831
832 /*
833 * Search to see if name already exists, and get back a pointer
834 * to where it should go.
835 */
836 error = xfs_da3_node_lookup_int(state, &retval);
837 if (error)
838 goto out;
839 blk = &state->path.blk[ state->path.active-1 ];
840 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
841 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
842 goto out;
843 } else if (retval == -EEXIST) {
844 if (args->flags & ATTR_CREATE)
845 goto out;
846
847 trace_xfs_attr_node_replace(args);
848
849 /* save the attribute state for later removal*/
850 args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
851 args->blkno2 = args->blkno; /* set 2nd entry info*/
852 args->index2 = args->index;
853 args->rmtblkno2 = args->rmtblkno;
854 args->rmtblkcnt2 = args->rmtblkcnt;
855 args->rmtvaluelen2 = args->rmtvaluelen;
856
857 /*
858 * clear the remote attr state now that it is saved so that the
859 * values reflect the state of the attribute we are about to
860 * add, not the attribute we just found and will remove later.
861 */
862 args->rmtblkno = 0;
863 args->rmtblkcnt = 0;
864 args->rmtvaluelen = 0;
865 }
866
867 retval = xfs_attr3_leaf_add(blk->bp, state->args);
868 if (retval == -ENOSPC) {
869 if (state->path.active == 1) {
870 /*
871 * Its really a single leaf node, but it had
872 * out-of-line values so it looked like it *might*
873 * have been a b-tree.
874 */
875 xfs_da_state_free(state);
876 state = NULL;
877 xfs_defer_init(args->dfops, args->firstblock);
878 error = xfs_attr3_leaf_to_node(args);
879 if (error)
880 goto out_defer_cancel;
881 xfs_defer_ijoin(args->dfops, dp);
882 error = xfs_defer_finish(&args->trans, args->dfops);
883 if (error)
884 goto out_defer_cancel;
885
886 /*
887 * Commit the node conversion and start the next
888 * trans in the chain.
889 */
890 error = xfs_trans_roll_inode(&args->trans, dp);
891 if (error)
892 goto out;
893
894 goto restart;
895 }
896
897 /*
898 * Split as many Btree elements as required.
899 * This code tracks the new and old attr's location
900 * in the index/blkno/rmtblkno/rmtblkcnt fields and
901 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
902 */
903 xfs_defer_init(args->dfops, args->firstblock);
904 error = xfs_da3_split(state);
905 if (error)
906 goto out_defer_cancel;
907 xfs_defer_ijoin(args->dfops, dp);
908 error = xfs_defer_finish(&args->trans, args->dfops);
909 if (error)
910 goto out_defer_cancel;
911 } else {
912 /*
913 * Addition succeeded, update Btree hashvals.
914 */
915 xfs_da3_fixhashpath(state, &state->path);
916 }
917
918 /*
919 * Kill the state structure, we're done with it and need to
920 * allow the buffers to come back later.
921 */
922 xfs_da_state_free(state);
923 state = NULL;
924
925 /*
926 * Commit the leaf addition or btree split and start the next
927 * trans in the chain.
928 */
929 error = xfs_trans_roll_inode(&args->trans, dp);
930 if (error)
931 goto out;
932
933 /*
934 * If there was an out-of-line value, allocate the blocks we
935 * identified for its storage and copy the value. This is done
936 * after we create the attribute so that we don't overflow the
937 * maximum size of a transaction and/or hit a deadlock.
938 */
939 if (args->rmtblkno > 0) {
940 error = xfs_attr_rmtval_set(args);
941 if (error)
942 return error;
943 }
944
945 /*
946 * If this is an atomic rename operation, we must "flip" the
947 * incomplete flags on the "new" and "old" attribute/value pairs
948 * so that one disappears and one appears atomically. Then we
949 * must remove the "old" attribute/value pair.
950 */
951 if (args->op_flags & XFS_DA_OP_RENAME) {
952 /*
953 * In a separate transaction, set the incomplete flag on the
954 * "old" attr and clear the incomplete flag on the "new" attr.
955 */
956 error = xfs_attr3_leaf_flipflags(args);
957 if (error)
958 goto out;
959
960 /*
961 * Dismantle the "old" attribute/value pair by removing
962 * a "remote" value (if it exists).
963 */
964 args->index = args->index2;
965 args->blkno = args->blkno2;
966 args->rmtblkno = args->rmtblkno2;
967 args->rmtblkcnt = args->rmtblkcnt2;
968 args->rmtvaluelen = args->rmtvaluelen2;
969 if (args->rmtblkno) {
970 error = xfs_attr_rmtval_remove(args);
971 if (error)
972 return error;
973 }
974
975 /*
976 * Re-find the "old" attribute entry after any split ops.
977 * The INCOMPLETE flag means that we will find the "old"
978 * attr, not the "new" one.
979 */
980 args->flags |= XFS_ATTR_INCOMPLETE;
981 state = xfs_da_state_alloc();
982 state->args = args;
983 state->mp = mp;
984 state->inleaf = 0;
985 error = xfs_da3_node_lookup_int(state, &retval);
986 if (error)
987 goto out;
988
989 /*
990 * Remove the name and update the hashvals in the tree.
991 */
992 blk = &state->path.blk[ state->path.active-1 ];
993 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
994 error = xfs_attr3_leaf_remove(blk->bp, args);
995 xfs_da3_fixhashpath(state, &state->path);
996
997 /*
998 * Check to see if the tree needs to be collapsed.
999 */
1000 if (retval && (state->path.active > 1)) {
1001 xfs_defer_init(args->dfops, args->firstblock);
1002 error = xfs_da3_join(state);
1003 if (error)
1004 goto out_defer_cancel;
1005 xfs_defer_ijoin(args->dfops, dp);
1006 error = xfs_defer_finish(&args->trans, args->dfops);
1007 if (error)
1008 goto out_defer_cancel;
1009 }
1010
1011 /*
1012 * Commit and start the next trans in the chain.
1013 */
1014 error = xfs_trans_roll_inode(&args->trans, dp);
1015 if (error)
1016 goto out;
1017
1018 } else if (args->rmtblkno > 0) {
1019 /*
1020 * Added a "remote" value, just clear the incomplete flag.
1021 */
1022 error = xfs_attr3_leaf_clearflag(args);
1023 if (error)
1024 goto out;
1025 }
1026 retval = error = 0;
1027
1028 out:
1029 if (state)
1030 xfs_da_state_free(state);
1031 if (error)
1032 return error;
1033 return retval;
1034 out_defer_cancel:
1035 xfs_defer_cancel(args->dfops);
1036 goto out;
1037 }
1038
1039 /*
1040 * Remove a name from a B-tree attribute list.
1041 *
1042 * This will involve walking down the Btree, and may involve joining
1043 * leaf nodes and even joining intermediate nodes up to and including
1044 * the root node (a special case of an intermediate node).
1045 */
1046 STATIC int
1047 xfs_attr_node_removename(xfs_da_args_t *args)
1048 {
1049 xfs_da_state_t *state;
1050 xfs_da_state_blk_t *blk;
1051 xfs_inode_t *dp;
1052 struct xfs_buf *bp;
1053 int retval, error, forkoff;
1054
1055 trace_xfs_attr_node_removename(args);
1056
1057 /*
1058 * Tie a string around our finger to remind us where we are.
1059 */
1060 dp = args->dp;
1061 state = xfs_da_state_alloc();
1062 state->args = args;
1063 state->mp = dp->i_mount;
1064
1065 /*
1066 * Search to see if name exists, and get back a pointer to it.
1067 */
1068 error = xfs_da3_node_lookup_int(state, &retval);
1069 if (error || (retval != -EEXIST)) {
1070 if (error == 0)
1071 error = retval;
1072 goto out;
1073 }
1074
1075 /*
1076 * If there is an out-of-line value, de-allocate the blocks.
1077 * This is done before we remove the attribute so that we don't
1078 * overflow the maximum size of a transaction and/or hit a deadlock.
1079 */
1080 blk = &state->path.blk[ state->path.active-1 ];
1081 ASSERT(blk->bp != NULL);
1082 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1083 if (args->rmtblkno > 0) {
1084 /*
1085 * Fill in disk block numbers in the state structure
1086 * so that we can get the buffers back after we commit
1087 * several transactions in the following calls.
1088 */
1089 error = xfs_attr_fillstate(state);
1090 if (error)
1091 goto out;
1092
1093 /*
1094 * Mark the attribute as INCOMPLETE, then bunmapi() the
1095 * remote value.
1096 */
1097 error = xfs_attr3_leaf_setflag(args);
1098 if (error)
1099 goto out;
1100 error = xfs_attr_rmtval_remove(args);
1101 if (error)
1102 goto out;
1103
1104 /*
1105 * Refill the state structure with buffers, the prior calls
1106 * released our buffers.
1107 */
1108 error = xfs_attr_refillstate(state);
1109 if (error)
1110 goto out;
1111 }
1112
1113 /*
1114 * Remove the name and update the hashvals in the tree.
1115 */
1116 blk = &state->path.blk[ state->path.active-1 ];
1117 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1118 retval = xfs_attr3_leaf_remove(blk->bp, args);
1119 xfs_da3_fixhashpath(state, &state->path);
1120
1121 /*
1122 * Check to see if the tree needs to be collapsed.
1123 */
1124 if (retval && (state->path.active > 1)) {
1125 xfs_defer_init(args->dfops, args->firstblock);
1126 error = xfs_da3_join(state);
1127 if (error)
1128 goto out_defer_cancel;
1129 xfs_defer_ijoin(args->dfops, dp);
1130 error = xfs_defer_finish(&args->trans, args->dfops);
1131 if (error)
1132 goto out_defer_cancel;
1133 /*
1134 * Commit the Btree join operation and start a new trans.
1135 */
1136 error = xfs_trans_roll_inode(&args->trans, dp);
1137 if (error)
1138 goto out;
1139 }
1140
1141 /*
1142 * If the result is small enough, push it all into the inode.
1143 */
1144 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
1145 /*
1146 * Have to get rid of the copy of this dabuf in the state.
1147 */
1148 ASSERT(state->path.active == 1);
1149 ASSERT(state->path.blk[0].bp);
1150 state->path.blk[0].bp = NULL;
1151
1152 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, -1, &bp);
1153 if (error)
1154 goto out;
1155
1156 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
1157 xfs_defer_init(args->dfops, args->firstblock);
1158 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
1159 /* bp is gone due to xfs_da_shrink_inode */
1160 if (error)
1161 goto out_defer_cancel;
1162 xfs_defer_ijoin(args->dfops, dp);
1163 error = xfs_defer_finish(&args->trans, args->dfops);
1164 if (error)
1165 goto out_defer_cancel;
1166 } else
1167 xfs_trans_brelse(args->trans, bp);
1168 }
1169 error = 0;
1170
1171 out:
1172 xfs_da_state_free(state);
1173 return error;
1174 out_defer_cancel:
1175 xfs_defer_cancel(args->dfops);
1176 goto out;
1177 }
1178
1179 /*
1180 * Fill in the disk block numbers in the state structure for the buffers
1181 * that are attached to the state structure.
1182 * This is done so that we can quickly reattach ourselves to those buffers
1183 * after some set of transaction commits have released these buffers.
1184 */
1185 STATIC int
1186 xfs_attr_fillstate(xfs_da_state_t *state)
1187 {
1188 xfs_da_state_path_t *path;
1189 xfs_da_state_blk_t *blk;
1190 int level;
1191
1192 trace_xfs_attr_fillstate(state->args);
1193
1194 /*
1195 * Roll down the "path" in the state structure, storing the on-disk
1196 * block number for those buffers in the "path".
1197 */
1198 path = &state->path;
1199 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1200 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1201 if (blk->bp) {
1202 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
1203 blk->bp = NULL;
1204 } else {
1205 blk->disk_blkno = 0;
1206 }
1207 }
1208
1209 /*
1210 * Roll down the "altpath" in the state structure, storing the on-disk
1211 * block number for those buffers in the "altpath".
1212 */
1213 path = &state->altpath;
1214 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1215 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1216 if (blk->bp) {
1217 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
1218 blk->bp = NULL;
1219 } else {
1220 blk->disk_blkno = 0;
1221 }
1222 }
1223
1224 return 0;
1225 }
1226
1227 /*
1228 * Reattach the buffers to the state structure based on the disk block
1229 * numbers stored in the state structure.
1230 * This is done after some set of transaction commits have released those
1231 * buffers from our grip.
1232 */
1233 STATIC int
1234 xfs_attr_refillstate(xfs_da_state_t *state)
1235 {
1236 xfs_da_state_path_t *path;
1237 xfs_da_state_blk_t *blk;
1238 int level, error;
1239
1240 trace_xfs_attr_refillstate(state->args);
1241
1242 /*
1243 * Roll down the "path" in the state structure, storing the on-disk
1244 * block number for those buffers in the "path".
1245 */
1246 path = &state->path;
1247 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1248 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1249 if (blk->disk_blkno) {
1250 error = xfs_da3_node_read(state->args->trans,
1251 state->args->dp,
1252 blk->blkno, blk->disk_blkno,
1253 &blk->bp, XFS_ATTR_FORK);
1254 if (error)
1255 return error;
1256 } else {
1257 blk->bp = NULL;
1258 }
1259 }
1260
1261 /*
1262 * Roll down the "altpath" in the state structure, storing the on-disk
1263 * block number for those buffers in the "altpath".
1264 */
1265 path = &state->altpath;
1266 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1267 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1268 if (blk->disk_blkno) {
1269 error = xfs_da3_node_read(state->args->trans,
1270 state->args->dp,
1271 blk->blkno, blk->disk_blkno,
1272 &blk->bp, XFS_ATTR_FORK);
1273 if (error)
1274 return error;
1275 } else {
1276 blk->bp = NULL;
1277 }
1278 }
1279
1280 return 0;
1281 }
1282
1283 /*
1284 * Look up a filename in a node attribute list.
1285 *
1286 * This routine gets called for any attribute fork that has more than one
1287 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1288 * "remote" values taking up more blocks.
1289 */
1290 STATIC int
1291 xfs_attr_node_get(xfs_da_args_t *args)
1292 {
1293 xfs_da_state_t *state;
1294 xfs_da_state_blk_t *blk;
1295 int error, retval;
1296 int i;
1297
1298 trace_xfs_attr_node_get(args);
1299
1300 state = xfs_da_state_alloc();
1301 state->args = args;
1302 state->mp = args->dp->i_mount;
1303
1304 /*
1305 * Search to see if name exists, and get back a pointer to it.
1306 */
1307 error = xfs_da3_node_lookup_int(state, &retval);
1308 if (error) {
1309 retval = error;
1310 } else if (retval == -EEXIST) {
1311 blk = &state->path.blk[ state->path.active-1 ];
1312 ASSERT(blk->bp != NULL);
1313 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1314
1315 /*
1316 * Get the value, local or "remote"
1317 */
1318 retval = xfs_attr3_leaf_getvalue(blk->bp, args);
1319 if (!retval && (args->rmtblkno > 0)
1320 && !(args->flags & ATTR_KERNOVAL)) {
1321 retval = xfs_attr_rmtval_get(args);
1322 }
1323 }
1324
1325 /*
1326 * If not in a transaction, we have to release all the buffers.
1327 */
1328 for (i = 0; i < state->path.active; i++) {
1329 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
1330 state->path.blk[i].bp = NULL;
1331 }
1332
1333 xfs_da_state_free(state);
1334 return retval;
1335 }