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