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