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