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