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