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