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