]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - fs/xfs/xfs_attr.c
xfs: kill xfs_qmops
[mirror_ubuntu-zesty-kernel.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 */
16f7e0fe 18
1da177e4 19#include "xfs.h"
a844f451 20#include "xfs_fs.h"
1da177e4 21#include "xfs_types.h"
a844f451 22#include "xfs_bit.h"
1da177e4 23#include "xfs_log.h"
a844f451 24#include "xfs_inum.h"
1da177e4
LT
25#include "xfs_trans.h"
26#include "xfs_sb.h"
27#include "xfs_ag.h"
1da177e4
LT
28#include "xfs_dir2.h"
29#include "xfs_dmapi.h"
30#include "xfs_mount.h"
a844f451 31#include "xfs_da_btree.h"
1da177e4 32#include "xfs_bmap_btree.h"
a844f451 33#include "xfs_alloc_btree.h"
1da177e4 34#include "xfs_ialloc_btree.h"
1da177e4 35#include "xfs_dir2_sf.h"
a844f451 36#include "xfs_attr_sf.h"
1da177e4 37#include "xfs_dinode.h"
1da177e4 38#include "xfs_inode.h"
a844f451
NS
39#include "xfs_alloc.h"
40#include "xfs_btree.h"
41#include "xfs_inode_item.h"
1da177e4 42#include "xfs_bmap.h"
1da177e4
LT
43#include "xfs_attr.h"
44#include "xfs_attr_leaf.h"
45#include "xfs_error.h"
1da177e4 46#include "xfs_quota.h"
1da177e4
LT
47#include "xfs_trans_space.h"
48#include "xfs_acl.h"
a844f451 49#include "xfs_rw.h"
739bfb2a 50#include "xfs_vnodeops.h"
1da177e4
LT
51
52/*
53 * xfs_attr.c
54 *
55 * Provide the external interfaces to manage attribute lists.
56 */
57
58/*========================================================================
59 * Function prototypes for the kernel.
60 *========================================================================*/
61
62/*
63 * Internal routines when attribute list fits inside the inode.
64 */
65STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
66
67/*
68 * Internal routines when attribute list is one block.
69 */
ba0f32d4 70STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
1da177e4
LT
71STATIC int xfs_attr_leaf_addname(xfs_da_args_t *args);
72STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
73STATIC int xfs_attr_leaf_list(xfs_attr_list_context_t *context);
74
75/*
76 * Internal routines when attribute list is more than one block.
77 */
ba0f32d4 78STATIC int xfs_attr_node_get(xfs_da_args_t *args);
1da177e4
LT
79STATIC int xfs_attr_node_addname(xfs_da_args_t *args);
80STATIC int xfs_attr_node_removename(xfs_da_args_t *args);
81STATIC int xfs_attr_node_list(xfs_attr_list_context_t *context);
82STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
83STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
84
85/*
86 * Routines to manipulate out-of-line attribute values.
87 */
1da177e4
LT
88STATIC int xfs_attr_rmtval_set(xfs_da_args_t *args);
89STATIC int xfs_attr_rmtval_remove(xfs_da_args_t *args);
90
91#define ATTR_RMTVALUE_MAPSIZE 1 /* # of map entries at once */
92
93#if defined(XFS_ATTR_TRACE)
94ktrace_t *xfs_attr_trace_buf;
95#endif
96
e8b0ebaa
BN
97STATIC int
98xfs_attr_name_to_xname(
99 struct xfs_name *xname,
100 const char *aname)
101{
102 if (!aname)
103 return EINVAL;
104 xname->name = aname;
105 xname->len = strlen(aname);
106 if (xname->len >= MAXNAMELEN)
107 return EFAULT; /* match IRIX behaviour */
108
109 return 0;
110}
1da177e4 111
caf8aabd
CH
112STATIC int
113xfs_inode_hasattr(
114 struct xfs_inode *ip)
115{
116 if (!XFS_IFORK_Q(ip) ||
117 (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
118 ip->i_d.di_anextents == 0))
119 return 0;
120 return 1;
121}
122
1da177e4
LT
123/*========================================================================
124 * Overall external interface routines.
125 *========================================================================*/
126
127int
e8b0ebaa
BN
128xfs_attr_fetch(xfs_inode_t *ip, struct xfs_name *name,
129 char *value, int *valuelenp, int flags)
1da177e4
LT
130{
131 xfs_da_args_t args;
132 int error;
133
caf8aabd
CH
134 if (!xfs_inode_hasattr(ip))
135 return ENOATTR;
1da177e4 136
1da177e4
LT
137 /*
138 * Fill in the arg structure for this request.
139 */
140 memset((char *)&args, 0, sizeof(args));
e8b0ebaa
BN
141 args.name = name->name;
142 args.namelen = name->len;
1da177e4
LT
143 args.value = value;
144 args.valuelen = *valuelenp;
145 args.flags = flags;
146 args.hashval = xfs_da_hashname(args.name, args.namelen);
147 args.dp = ip;
148 args.whichfork = XFS_ATTR_FORK;
149
150 /*
151 * Decide on what work routines to call based on the inode size.
152 */
caf8aabd 153 if (ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
1da177e4
LT
154 error = xfs_attr_shortform_getvalue(&args);
155 } else if (xfs_bmap_one_block(ip, XFS_ATTR_FORK)) {
156 error = xfs_attr_leaf_get(&args);
157 } else {
158 error = xfs_attr_node_get(&args);
159 }
160
161 /*
162 * Return the number of bytes in the value to the caller.
163 */
164 *valuelenp = args.valuelen;
165
166 if (error == EEXIST)
167 error = 0;
168 return(error);
169}
170
171int
993386c1
CH
172xfs_attr_get(
173 xfs_inode_t *ip,
174 const char *name,
175 char *value,
176 int *valuelenp,
e8b0ebaa 177 int flags)
1da177e4 178{
e8b0ebaa
BN
179 int error;
180 struct xfs_name xname;
1da177e4
LT
181
182 XFS_STATS_INC(xs_attr_get);
183
1da177e4
LT
184 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
185 return(EIO);
186
e8b0ebaa
BN
187 error = xfs_attr_name_to_xname(&xname, name);
188 if (error)
189 return error;
190
1da177e4 191 xfs_ilock(ip, XFS_ILOCK_SHARED);
e8b0ebaa 192 error = xfs_attr_fetch(ip, &xname, value, valuelenp, flags);
1da177e4
LT
193 xfs_iunlock(ip, XFS_ILOCK_SHARED);
194 return(error);
195}
196
5e9da7b7
NS
197/*
198 * Calculate how many blocks we need for the new attribute,
199 */
200int
201xfs_attr_calc_size(
202 struct xfs_inode *ip,
203 int namelen,
204 int valuelen,
205 int *local)
206{
207 struct xfs_mount *mp = ip->i_mount;
208 int size;
209 int nblks;
210
211 /*
212 * Determine space new attribute will use, and if it would be
213 * "local" or "remote" (note: local != inline).
214 */
215 size = xfs_attr_leaf_newentsize(namelen, valuelen,
216 mp->m_sb.sb_blocksize, local);
217
218 nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
219 if (*local) {
220 if (size > (mp->m_sb.sb_blocksize >> 1)) {
221 /* Double split possible */
222 nblks *= 2;
223 }
224 } else {
225 /*
226 * Out of line attribute, cannot double split, but
227 * make room for the attribute value itself.
228 */
229 uint dblocks = XFS_B_TO_FSB(mp, valuelen);
230 nblks += dblocks;
231 nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
232 }
233
234 return nblks;
235}
236
e8b0ebaa
BN
237STATIC int
238xfs_attr_set_int(xfs_inode_t *dp, struct xfs_name *name,
239 char *value, int valuelen, int flags)
1da177e4
LT
240{
241 xfs_da_args_t args;
1da177e4
LT
242 xfs_fsblock_t firstblock;
243 xfs_bmap_free_t flist;
244 int error, err2, committed;
aa82daa0 245 xfs_mount_t *mp = dp->i_mount;
1da177e4 246 int rsvd = (flags & ATTR_ROOT) != 0;
5e9da7b7 247 int local;
1da177e4
LT
248
249 /*
250 * Attach the dquots to the inode.
251 */
7d095257
CH
252 error = xfs_qm_dqattach(dp, 0);
253 if (error)
254 return error;
1da177e4
LT
255
256 /*
257 * If the inode doesn't have an attribute fork, add one.
258 * (inode must not be locked when we call this routine)
259 */
260 if (XFS_IFORK_Q(dp) == 0) {
e5889e90 261 int sf_size = sizeof(xfs_attr_sf_hdr_t) +
e8b0ebaa 262 XFS_ATTR_SF_ENTSIZE_BYNAME(name->len, valuelen);
e5889e90
BN
263
264 if ((error = xfs_bmap_add_attrfork(dp, sf_size, rsvd)))
1da177e4
LT
265 return(error);
266 }
267
268 /*
269 * Fill in the arg structure for this request.
270 */
271 memset((char *)&args, 0, sizeof(args));
e8b0ebaa
BN
272 args.name = name->name;
273 args.namelen = name->len;
1da177e4
LT
274 args.value = value;
275 args.valuelen = valuelen;
276 args.flags = flags;
277 args.hashval = xfs_da_hashname(args.name, args.namelen);
278 args.dp = dp;
279 args.firstblock = &firstblock;
280 args.flist = &flist;
281 args.whichfork = XFS_ATTR_FORK;
6a178100 282 args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
1da177e4 283
1da177e4 284 /* Size is now blocks for attribute data */
5e9da7b7 285 args.total = xfs_attr_calc_size(dp, name->len, valuelen, &local);
1da177e4
LT
286
287 /*
288 * Start our first transaction of the day.
289 *
290 * All future transactions during this code must be "chained" off
291 * this one via the trans_dup() call. All transactions will contain
292 * the inode, and the inode will always be marked with trans_ihold().
293 * Since the inode will be locked in all transactions, we must log
294 * the inode in every transaction to let it float upward through
295 * the log.
296 */
297 args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_SET);
298
299 /*
300 * Root fork attributes can use reserved data blocks for this
301 * operation if necessary
302 */
303
304 if (rsvd)
305 args.trans->t_flags |= XFS_TRANS_RESERVE;
306
5e9da7b7
NS
307 if ((error = xfs_trans_reserve(args.trans, args.total,
308 XFS_ATTRSET_LOG_RES(mp, args.total), 0,
309 XFS_TRANS_PERM_LOG_RES, XFS_ATTRSET_LOG_COUNT))) {
1da177e4
LT
310 xfs_trans_cancel(args.trans, 0);
311 return(error);
312 }
313 xfs_ilock(dp, XFS_ILOCK_EXCL);
314
7d095257 315 error = xfs_trans_reserve_quota_nblks(args.trans, dp, args.total, 0,
5e9da7b7
NS
316 rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
317 XFS_QMOPT_RES_REGBLKS);
1da177e4
LT
318 if (error) {
319 xfs_iunlock(dp, XFS_ILOCK_EXCL);
320 xfs_trans_cancel(args.trans, XFS_TRANS_RELEASE_LOG_RES);
321 return (error);
322 }
323
324 xfs_trans_ijoin(args.trans, dp, XFS_ILOCK_EXCL);
325 xfs_trans_ihold(args.trans, dp);
326
327 /*
c41564b5 328 * If the attribute list is non-existent or a shortform list,
1da177e4
LT
329 * upgrade it to a single-leaf-block attribute list.
330 */
331 if ((dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) ||
332 ((dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS) &&
333 (dp->i_d.di_anextents == 0))) {
334
335 /*
336 * Build initial attribute list (if required).
337 */
338 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
d8cc890d 339 xfs_attr_shortform_create(&args);
1da177e4
LT
340
341 /*
342 * Try to add the attr to the attribute list in
343 * the inode.
344 */
345 error = xfs_attr_shortform_addname(&args);
346 if (error != ENOSPC) {
347 /*
348 * Commit the shortform mods, and we're done.
349 * NOTE: this is also the error path (EEXIST, etc).
350 */
351 ASSERT(args.trans != NULL);
352
353 /*
354 * If this is a synchronous mount, make sure that
355 * the transaction goes to disk before returning
356 * to the user.
357 */
358 if (mp->m_flags & XFS_MOUNT_WSYNC) {
359 xfs_trans_set_sync(args.trans);
360 }
361 err2 = xfs_trans_commit(args.trans,
1c72bf90 362 XFS_TRANS_RELEASE_LOG_RES);
1da177e4
LT
363 xfs_iunlock(dp, XFS_ILOCK_EXCL);
364
365 /*
366 * Hit the inode change time.
367 */
368 if (!error && (flags & ATTR_KERNOTIME) == 0) {
369 xfs_ichgtime(dp, XFS_ICHGTIME_CHG);
370 }
371 return(error == 0 ? err2 : error);
372 }
373
374 /*
375 * It won't fit in the shortform, transform to a leaf block.
376 * GROT: another possible req'mt for a double-split btree op.
377 */
9d87c319 378 xfs_bmap_init(args.flist, args.firstblock);
1da177e4
LT
379 error = xfs_attr_shortform_to_leaf(&args);
380 if (!error) {
381 error = xfs_bmap_finish(&args.trans, args.flist,
f7c99b6f 382 &committed);
1da177e4
LT
383 }
384 if (error) {
385 ASSERT(committed);
386 args.trans = NULL;
387 xfs_bmap_cancel(&flist);
388 goto out;
389 }
390
391 /*
392 * bmap_finish() may have committed the last trans and started
393 * a new one. We need the inode to be in all transactions.
394 */
395 if (committed) {
396 xfs_trans_ijoin(args.trans, dp, XFS_ILOCK_EXCL);
397 xfs_trans_ihold(args.trans, dp);
398 }
399
400 /*
401 * Commit the leaf transformation. We'll need another (linked)
402 * transaction to add the new attribute to the leaf.
403 */
322ff6b8
NS
404
405 error = xfs_trans_roll(&args.trans, dp);
406 if (error)
1da177e4
LT
407 goto out;
408
409 }
410
411 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
412 error = xfs_attr_leaf_addname(&args);
413 } else {
414 error = xfs_attr_node_addname(&args);
415 }
416 if (error) {
417 goto out;
418 }
419
420 /*
421 * If this is a synchronous mount, make sure that the
422 * transaction goes to disk before returning to the user.
423 */
424 if (mp->m_flags & XFS_MOUNT_WSYNC) {
425 xfs_trans_set_sync(args.trans);
426 }
427
428 /*
429 * Commit the last in the sequence of transactions.
430 */
431 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
1c72bf90 432 error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
1da177e4
LT
433 xfs_iunlock(dp, XFS_ILOCK_EXCL);
434
435 /*
436 * Hit the inode change time.
437 */
438 if (!error && (flags & ATTR_KERNOTIME) == 0) {
439 xfs_ichgtime(dp, XFS_ICHGTIME_CHG);
440 }
441
442 return(error);
443
444out:
445 if (args.trans)
446 xfs_trans_cancel(args.trans,
447 XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
448 xfs_iunlock(dp, XFS_ILOCK_EXCL);
449 return(error);
450}
451
aa82daa0 452int
993386c1
CH
453xfs_attr_set(
454 xfs_inode_t *dp,
455 const char *name,
456 char *value,
457 int valuelen,
458 int flags)
1da177e4 459{
e8b0ebaa
BN
460 int error;
461 struct xfs_name xname;
1da177e4 462
aa82daa0 463 XFS_STATS_INC(xs_attr_set);
1da177e4 464
aa82daa0 465 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
1da177e4
LT
466 return (EIO);
467
e8b0ebaa
BN
468 error = xfs_attr_name_to_xname(&xname, name);
469 if (error)
470 return error;
471
472 return xfs_attr_set_int(dp, &xname, value, valuelen, flags);
aa82daa0
NS
473}
474
475/*
476 * Generic handler routine to remove a name from an attribute list.
477 * Transitions attribute list from Btree to shortform as necessary.
478 */
e8b0ebaa
BN
479STATIC int
480xfs_attr_remove_int(xfs_inode_t *dp, struct xfs_name *name, int flags)
aa82daa0
NS
481{
482 xfs_da_args_t args;
483 xfs_fsblock_t firstblock;
484 xfs_bmap_free_t flist;
485 int error;
486 xfs_mount_t *mp = dp->i_mount;
487
1da177e4
LT
488 /*
489 * Fill in the arg structure for this request.
490 */
491 memset((char *)&args, 0, sizeof(args));
e8b0ebaa
BN
492 args.name = name->name;
493 args.namelen = name->len;
1da177e4
LT
494 args.flags = flags;
495 args.hashval = xfs_da_hashname(args.name, args.namelen);
496 args.dp = dp;
497 args.firstblock = &firstblock;
498 args.flist = &flist;
499 args.total = 0;
500 args.whichfork = XFS_ATTR_FORK;
501
502 /*
503 * Attach the dquots to the inode.
504 */
7d095257
CH
505 error = xfs_qm_dqattach(dp, 0);
506 if (error)
507 return error;
1da177e4
LT
508
509 /*
510 * Start our first transaction of the day.
511 *
512 * All future transactions during this code must be "chained" off
513 * this one via the trans_dup() call. All transactions will contain
514 * the inode, and the inode will always be marked with trans_ihold().
515 * Since the inode will be locked in all transactions, we must log
516 * the inode in every transaction to let it float upward through
517 * the log.
518 */
519 args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_RM);
520
521 /*
522 * Root fork attributes can use reserved data blocks for this
523 * operation if necessary
524 */
525
526 if (flags & ATTR_ROOT)
527 args.trans->t_flags |= XFS_TRANS_RESERVE;
528
529 if ((error = xfs_trans_reserve(args.trans,
530 XFS_ATTRRM_SPACE_RES(mp),
531 XFS_ATTRRM_LOG_RES(mp),
532 0, XFS_TRANS_PERM_LOG_RES,
533 XFS_ATTRRM_LOG_COUNT))) {
534 xfs_trans_cancel(args.trans, 0);
535 return(error);
1da177e4
LT
536 }
537
538 xfs_ilock(dp, XFS_ILOCK_EXCL);
539 /*
540 * No need to make quota reservations here. We expect to release some
541 * blocks not allocate in the common case.
542 */
543 xfs_trans_ijoin(args.trans, dp, XFS_ILOCK_EXCL);
544 xfs_trans_ihold(args.trans, dp);
545
546 /*
547 * Decide on what work routines to call based on the inode size.
548 */
caf8aabd 549 if (!xfs_inode_hasattr(dp)) {
1da177e4
LT
550 error = XFS_ERROR(ENOATTR);
551 goto out;
552 }
553 if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
554 ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
555 error = xfs_attr_shortform_remove(&args);
556 if (error) {
557 goto out;
558 }
559 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
560 error = xfs_attr_leaf_removename(&args);
561 } else {
562 error = xfs_attr_node_removename(&args);
563 }
564 if (error) {
565 goto out;
566 }
567
568 /*
569 * If this is a synchronous mount, make sure that the
570 * transaction goes to disk before returning to the user.
571 */
572 if (mp->m_flags & XFS_MOUNT_WSYNC) {
573 xfs_trans_set_sync(args.trans);
574 }
575
576 /*
577 * Commit the last in the sequence of transactions.
578 */
579 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
1c72bf90 580 error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
1da177e4
LT
581 xfs_iunlock(dp, XFS_ILOCK_EXCL);
582
583 /*
584 * Hit the inode change time.
585 */
586 if (!error && (flags & ATTR_KERNOTIME) == 0) {
587 xfs_ichgtime(dp, XFS_ICHGTIME_CHG);
588 }
589
590 return(error);
591
592out:
593 if (args.trans)
594 xfs_trans_cancel(args.trans,
595 XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
596 xfs_iunlock(dp, XFS_ILOCK_EXCL);
597 return(error);
598}
599
aa82daa0 600int
993386c1
CH
601xfs_attr_remove(
602 xfs_inode_t *dp,
603 const char *name,
604 int flags)
aa82daa0 605{
e8b0ebaa
BN
606 int error;
607 struct xfs_name xname;
aa82daa0
NS
608
609 XFS_STATS_INC(xs_attr_remove);
610
aa82daa0
NS
611 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
612 return (EIO);
613
e8b0ebaa
BN
614 error = xfs_attr_name_to_xname(&xname, name);
615 if (error)
616 return error;
617
aa82daa0 618 xfs_ilock(dp, XFS_ILOCK_SHARED);
caf8aabd 619 if (!xfs_inode_hasattr(dp)) {
aa82daa0 620 xfs_iunlock(dp, XFS_ILOCK_SHARED);
caf8aabd 621 return XFS_ERROR(ENOATTR);
aa82daa0
NS
622 }
623 xfs_iunlock(dp, XFS_ILOCK_SHARED);
624
e8b0ebaa 625 return xfs_attr_remove_int(dp, &xname, flags);
aa82daa0
NS
626}
627
ad9b463a 628int
726801ba
TS
629xfs_attr_list_int(xfs_attr_list_context_t *context)
630{
631 int error;
632 xfs_inode_t *dp = context->dp;
633
ad9b463a
CH
634 XFS_STATS_INC(xs_attr_list);
635
636 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
637 return EIO;
638
639 xfs_ilock(dp, XFS_ILOCK_SHARED);
640 xfs_attr_trace_l_c("syscall start", context);
641
726801ba
TS
642 /*
643 * Decide on what work routines to call based on the inode size.
644 */
caf8aabd 645 if (!xfs_inode_hasattr(dp)) {
726801ba
TS
646 error = 0;
647 } else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
648 error = xfs_attr_shortform_list(context);
649 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
650 error = xfs_attr_leaf_list(context);
651 } else {
652 error = xfs_attr_node_list(context);
653 }
ad9b463a
CH
654
655 xfs_iunlock(dp, XFS_ILOCK_SHARED);
656 xfs_attr_trace_l_c("syscall end", context);
657
726801ba
TS
658 return error;
659}
660
661#define ATTR_ENTBASESIZE /* minimum bytes used by an attr */ \
662 (((struct attrlist_ent *) 0)->a_name - (char *) 0)
663#define ATTR_ENTSIZE(namelen) /* actual bytes used by an attr */ \
664 ((ATTR_ENTBASESIZE + (namelen) + 1 + sizeof(u_int32_t)-1) \
665 & ~(sizeof(u_int32_t)-1))
666
667/*
668 * Format an attribute and copy it out to the user's buffer.
669 * Take care to check values and protect against them changing later,
670 * we may be reading them directly out of a user buffer.
671 */
672/*ARGSUSED*/
673STATIC int
ad9b463a 674xfs_attr_put_listent(xfs_attr_list_context_t *context, int flags,
726801ba
TS
675 char *name, int namelen,
676 int valuelen, char *value)
677{
ad9b463a 678 struct attrlist *alist = (struct attrlist *)context->alist;
726801ba
TS
679 attrlist_ent_t *aep;
680 int arraytop;
681
682 ASSERT(!(context->flags & ATTR_KERNOVAL));
683 ASSERT(context->count >= 0);
684 ASSERT(context->count < (ATTR_MAX_VALUELEN/8));
ad9b463a 685 ASSERT(context->firstu >= sizeof(*alist));
726801ba
TS
686 ASSERT(context->firstu <= context->bufsize);
687
ad9b463a
CH
688 /*
689 * Only list entries in the right namespace.
690 */
691 if (((context->flags & ATTR_SECURE) == 0) !=
692 ((flags & XFS_ATTR_SECURE) == 0))
693 return 0;
694 if (((context->flags & ATTR_ROOT) == 0) !=
695 ((flags & XFS_ATTR_ROOT) == 0))
696 return 0;
697
698 arraytop = sizeof(*alist) +
699 context->count * sizeof(alist->al_offset[0]);
726801ba
TS
700 context->firstu -= ATTR_ENTSIZE(namelen);
701 if (context->firstu < arraytop) {
702 xfs_attr_trace_l_c("buffer full", context);
ad9b463a 703 alist->al_more = 1;
726801ba
TS
704 context->seen_enough = 1;
705 return 1;
706 }
707
ad9b463a 708 aep = (attrlist_ent_t *)&context->alist[context->firstu];
726801ba
TS
709 aep->a_valuelen = valuelen;
710 memcpy(aep->a_name, name, namelen);
ad9b463a
CH
711 aep->a_name[namelen] = 0;
712 alist->al_offset[context->count++] = context->firstu;
713 alist->al_count = context->count;
726801ba
TS
714 xfs_attr_trace_l_c("add", context);
715 return 0;
716}
717
1da177e4
LT
718/*
719 * Generate a list of extended attribute names and optionally
720 * also value lengths. Positive return value follows the XFS
721 * convention of being an error, zero or negative return code
722 * is the length of the buffer returned (negated), indicating
723 * success.
724 */
725int
993386c1
CH
726xfs_attr_list(
727 xfs_inode_t *dp,
728 char *buffer,
729 int bufsize,
730 int flags,
731 attrlist_cursor_kern_t *cursor)
1da177e4
LT
732{
733 xfs_attr_list_context_t context;
ad9b463a 734 struct attrlist *alist;
1da177e4
LT
735 int error;
736
1da177e4
LT
737 /*
738 * Validate the cursor.
739 */
740 if (cursor->pad1 || cursor->pad2)
741 return(XFS_ERROR(EINVAL));
742 if ((cursor->initted == 0) &&
743 (cursor->hashval || cursor->blkno || cursor->offset))
726801ba 744 return XFS_ERROR(EINVAL);
1da177e4
LT
745
746 /*
747 * Check for a properly aligned buffer.
748 */
749 if (((long)buffer) & (sizeof(int)-1))
726801ba 750 return XFS_ERROR(EFAULT);
1da177e4
LT
751 if (flags & ATTR_KERNOVAL)
752 bufsize = 0;
753
754 /*
755 * Initialize the output buffer.
756 */
ad9b463a 757 memset(&context, 0, sizeof(context));
993386c1 758 context.dp = dp;
1da177e4 759 context.cursor = cursor;
1da177e4
LT
760 context.resynch = 1;
761 context.flags = flags;
ad9b463a
CH
762 context.alist = buffer;
763 context.bufsize = (bufsize & ~(sizeof(int)-1)); /* align */
764 context.firstu = context.bufsize;
765 context.put_listent = xfs_attr_put_listent;
1da177e4 766
ad9b463a
CH
767 alist = (struct attrlist *)context.alist;
768 alist->al_count = 0;
769 alist->al_more = 0;
770 alist->al_offset[0] = context.bufsize;
726801ba
TS
771
772 error = xfs_attr_list_int(&context);
ad9b463a 773 ASSERT(error >= 0);
726801ba 774 return error;
1da177e4
LT
775}
776
777int /* error */
778xfs_attr_inactive(xfs_inode_t *dp)
779{
780 xfs_trans_t *trans;
781 xfs_mount_t *mp;
782 int error;
783
784 mp = dp->i_mount;
785 ASSERT(! XFS_NOT_DQATTACHED(mp, dp));
786
787 xfs_ilock(dp, XFS_ILOCK_SHARED);
caf8aabd
CH
788 if (!xfs_inode_hasattr(dp) ||
789 dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
1da177e4 790 xfs_iunlock(dp, XFS_ILOCK_SHARED);
caf8aabd 791 return 0;
1da177e4
LT
792 }
793 xfs_iunlock(dp, XFS_ILOCK_SHARED);
794
795 /*
796 * Start our first transaction of the day.
797 *
798 * All future transactions during this code must be "chained" off
799 * this one via the trans_dup() call. All transactions will contain
800 * the inode, and the inode will always be marked with trans_ihold().
801 * Since the inode will be locked in all transactions, we must log
802 * the inode in every transaction to let it float upward through
803 * the log.
804 */
805 trans = xfs_trans_alloc(mp, XFS_TRANS_ATTRINVAL);
806 if ((error = xfs_trans_reserve(trans, 0, XFS_ATTRINVAL_LOG_RES(mp), 0,
807 XFS_TRANS_PERM_LOG_RES,
808 XFS_ATTRINVAL_LOG_COUNT))) {
809 xfs_trans_cancel(trans, 0);
810 return(error);
811 }
812 xfs_ilock(dp, XFS_ILOCK_EXCL);
813
814 /*
815 * No need to make quota reservations here. We expect to release some
816 * blocks, not allocate, in the common case.
817 */
818 xfs_trans_ijoin(trans, dp, XFS_ILOCK_EXCL);
819 xfs_trans_ihold(trans, dp);
820
821 /*
822 * Decide on what work routines to call based on the inode size.
823 */
caf8aabd
CH
824 if (!xfs_inode_hasattr(dp) ||
825 dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
1da177e4
LT
826 error = 0;
827 goto out;
828 }
829 error = xfs_attr_root_inactive(&trans, dp);
830 if (error)
831 goto out;
832 /*
833 * signal synchronous inactive transactions unless this
834 * is a synchronous mount filesystem in which case we
835 * know that we're here because we've been called out of
836 * xfs_inactive which means that the last reference is gone
837 * and the unlink transaction has already hit the disk so
838 * async inactive transactions are safe.
839 */
840 if ((error = xfs_itruncate_finish(&trans, dp, 0LL, XFS_ATTR_FORK,
841 (!(mp->m_flags & XFS_MOUNT_WSYNC)
842 ? 1 : 0))))
843 goto out;
844
845 /*
846 * Commit the last in the sequence of transactions.
847 */
848 xfs_trans_log_inode(trans, dp, XFS_ILOG_CORE);
1c72bf90 849 error = xfs_trans_commit(trans, XFS_TRANS_RELEASE_LOG_RES);
1da177e4
LT
850 xfs_iunlock(dp, XFS_ILOCK_EXCL);
851
852 return(error);
853
854out:
855 xfs_trans_cancel(trans, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
856 xfs_iunlock(dp, XFS_ILOCK_EXCL);
857 return(error);
858}
859
860
861
862/*========================================================================
863 * External routines when attribute list is inside the inode
864 *========================================================================*/
865
866/*
867 * Add a name to the shortform attribute list structure
868 * This is the external routine.
869 */
870STATIC int
871xfs_attr_shortform_addname(xfs_da_args_t *args)
872{
d8cc890d 873 int newsize, forkoff, retval;
1da177e4
LT
874
875 retval = xfs_attr_shortform_lookup(args);
876 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
877 return(retval);
878 } else if (retval == EEXIST) {
879 if (args->flags & ATTR_CREATE)
880 return(retval);
881 retval = xfs_attr_shortform_remove(args);
882 ASSERT(retval == 0);
883 }
884
d8cc890d
NS
885 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
886 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
887 return(XFS_ERROR(ENOSPC));
888
1da177e4
LT
889 newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
890 newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
d8cc890d
NS
891
892 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
893 if (!forkoff)
1da177e4 894 return(XFS_ERROR(ENOSPC));
d8cc890d
NS
895
896 xfs_attr_shortform_add(args, forkoff);
1da177e4
LT
897 return(0);
898}
899
900
901/*========================================================================
902 * External routines when attribute list is one block
903 *========================================================================*/
904
905/*
906 * Add a name to the leaf attribute list structure
907 *
908 * This leaf block cannot have a "remote" value, we only call this routine
909 * if bmap_one_block() says there is only one block (ie: no remote blks).
910 */
a8272ce0 911STATIC int
1da177e4
LT
912xfs_attr_leaf_addname(xfs_da_args_t *args)
913{
914 xfs_inode_t *dp;
915 xfs_dabuf_t *bp;
d8cc890d 916 int retval, error, committed, forkoff;
1da177e4
LT
917
918 /*
919 * Read the (only) block in the attribute list in.
920 */
921 dp = args->dp;
922 args->blkno = 0;
923 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
924 XFS_ATTR_FORK);
925 if (error)
926 return(error);
927 ASSERT(bp != NULL);
928
929 /*
930 * Look up the given attribute in the leaf block. Figure out if
931 * the given flags produce an error or call for an atomic rename.
932 */
933 retval = xfs_attr_leaf_lookup_int(bp, args);
934 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
935 xfs_da_brelse(args->trans, bp);
936 return(retval);
937 } else if (retval == EEXIST) {
938 if (args->flags & ATTR_CREATE) { /* pure create op */
939 xfs_da_brelse(args->trans, bp);
940 return(retval);
941 }
6a178100 942 args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
1da177e4
LT
943 args->blkno2 = args->blkno; /* set 2nd entry info*/
944 args->index2 = args->index;
945 args->rmtblkno2 = args->rmtblkno;
946 args->rmtblkcnt2 = args->rmtblkcnt;
947 }
948
949 /*
950 * Add the attribute to the leaf block, transitioning to a Btree
951 * if required.
952 */
953 retval = xfs_attr_leaf_add(bp, args);
954 xfs_da_buf_done(bp);
955 if (retval == ENOSPC) {
956 /*
957 * Promote the attribute list to the Btree format, then
958 * Commit that transaction so that the node_addname() call
959 * can manage its own transactions.
960 */
9d87c319 961 xfs_bmap_init(args->flist, args->firstblock);
1da177e4
LT
962 error = xfs_attr_leaf_to_node(args);
963 if (!error) {
964 error = xfs_bmap_finish(&args->trans, args->flist,
f7c99b6f 965 &committed);
1da177e4
LT
966 }
967 if (error) {
968 ASSERT(committed);
969 args->trans = NULL;
970 xfs_bmap_cancel(args->flist);
971 return(error);
972 }
973
974 /*
975 * bmap_finish() may have committed the last trans and started
976 * a new one. We need the inode to be in all transactions.
977 */
978 if (committed) {
979 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
980 xfs_trans_ihold(args->trans, dp);
981 }
982
983 /*
984 * Commit the current trans (including the inode) and start
985 * a new one.
986 */
322ff6b8
NS
987 error = xfs_trans_roll(&args->trans, dp);
988 if (error)
1da177e4
LT
989 return (error);
990
991 /*
992 * Fob the whole rest of the problem off on the Btree code.
993 */
994 error = xfs_attr_node_addname(args);
995 return(error);
996 }
997
998 /*
999 * Commit the transaction that added the attr name so that
1000 * later routines can manage their own transactions.
1001 */
322ff6b8
NS
1002 error = xfs_trans_roll(&args->trans, dp);
1003 if (error)
1da177e4
LT
1004 return (error);
1005
1006 /*
1007 * If there was an out-of-line value, allocate the blocks we
1008 * identified for its storage and copy the value. This is done
1009 * after we create the attribute so that we don't overflow the
1010 * maximum size of a transaction and/or hit a deadlock.
1011 */
1012 if (args->rmtblkno > 0) {
1013 error = xfs_attr_rmtval_set(args);
1014 if (error)
1015 return(error);
1016 }
1017
1018 /*
1019 * If this is an atomic rename operation, we must "flip" the
1020 * incomplete flags on the "new" and "old" attribute/value pairs
1021 * so that one disappears and one appears atomically. Then we
1022 * must remove the "old" attribute/value pair.
1023 */
6a178100 1024 if (args->op_flags & XFS_DA_OP_RENAME) {
1da177e4
LT
1025 /*
1026 * In a separate transaction, set the incomplete flag on the
1027 * "old" attr and clear the incomplete flag on the "new" attr.
1028 */
1029 error = xfs_attr_leaf_flipflags(args);
1030 if (error)
1031 return(error);
1032
1033 /*
1034 * Dismantle the "old" attribute/value pair by removing
1035 * a "remote" value (if it exists).
1036 */
1037 args->index = args->index2;
1038 args->blkno = args->blkno2;
1039 args->rmtblkno = args->rmtblkno2;
1040 args->rmtblkcnt = args->rmtblkcnt2;
1041 if (args->rmtblkno) {
1042 error = xfs_attr_rmtval_remove(args);
1043 if (error)
1044 return(error);
1045 }
1046
1047 /*
1048 * Read in the block containing the "old" attr, then
1049 * remove the "old" attr from that block (neat, huh!)
1050 */
1051 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1,
1052 &bp, XFS_ATTR_FORK);
1053 if (error)
1054 return(error);
1055 ASSERT(bp != NULL);
1056 (void)xfs_attr_leaf_remove(bp, args);
1057
1058 /*
1059 * If the result is small enough, shrink it all into the inode.
1060 */
d8cc890d 1061 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
9d87c319 1062 xfs_bmap_init(args->flist, args->firstblock);
d8cc890d 1063 error = xfs_attr_leaf_to_shortform(bp, args, forkoff);
1da177e4
LT
1064 /* bp is gone due to xfs_da_shrink_inode */
1065 if (!error) {
1066 error = xfs_bmap_finish(&args->trans,
1067 args->flist,
1da177e4
LT
1068 &committed);
1069 }
1070 if (error) {
1071 ASSERT(committed);
1072 args->trans = NULL;
1073 xfs_bmap_cancel(args->flist);
1074 return(error);
1075 }
1076
1077 /*
1078 * bmap_finish() may have committed the last trans
1079 * and started a new one. We need the inode to be
1080 * in all transactions.
1081 */
1082 if (committed) {
1083 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1084 xfs_trans_ihold(args->trans, dp);
1085 }
1086 } else
1087 xfs_da_buf_done(bp);
1088
1089 /*
1090 * Commit the remove and start the next trans in series.
1091 */
322ff6b8 1092 error = xfs_trans_roll(&args->trans, dp);
1da177e4
LT
1093
1094 } else if (args->rmtblkno > 0) {
1095 /*
1096 * Added a "remote" value, just clear the incomplete flag.
1097 */
1098 error = xfs_attr_leaf_clearflag(args);
1099 }
1100 return(error);
1101}
1102
1103/*
1104 * Remove a name from the leaf attribute list structure
1105 *
1106 * This leaf block cannot have a "remote" value, we only call this routine
1107 * if bmap_one_block() says there is only one block (ie: no remote blks).
1108 */
1109STATIC int
1110xfs_attr_leaf_removename(xfs_da_args_t *args)
1111{
1112 xfs_inode_t *dp;
1113 xfs_dabuf_t *bp;
d8cc890d 1114 int error, committed, forkoff;
1da177e4
LT
1115
1116 /*
1117 * Remove the attribute.
1118 */
1119 dp = args->dp;
1120 args->blkno = 0;
1121 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
1122 XFS_ATTR_FORK);
1123 if (error) {
1124 return(error);
1125 }
1126
1127 ASSERT(bp != NULL);
1128 error = xfs_attr_leaf_lookup_int(bp, args);
1129 if (error == ENOATTR) {
1130 xfs_da_brelse(args->trans, bp);
1131 return(error);
1132 }
1133
1134 (void)xfs_attr_leaf_remove(bp, args);
1135
1136 /*
1137 * If the result is small enough, shrink it all into the inode.
1138 */
d8cc890d 1139 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
9d87c319 1140 xfs_bmap_init(args->flist, args->firstblock);
d8cc890d 1141 error = xfs_attr_leaf_to_shortform(bp, args, forkoff);
1da177e4
LT
1142 /* bp is gone due to xfs_da_shrink_inode */
1143 if (!error) {
1144 error = xfs_bmap_finish(&args->trans, args->flist,
f7c99b6f 1145 &committed);
1da177e4
LT
1146 }
1147 if (error) {
1148 ASSERT(committed);
1149 args->trans = NULL;
1150 xfs_bmap_cancel(args->flist);
1151 return(error);
1152 }
1153
1154 /*
1155 * bmap_finish() may have committed the last trans and started
1156 * a new one. We need the inode to be in all transactions.
1157 */
1158 if (committed) {
1159 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1160 xfs_trans_ihold(args->trans, dp);
1161 }
1162 } else
1163 xfs_da_buf_done(bp);
1164 return(0);
1165}
1166
1167/*
1168 * Look up a name in a leaf attribute list structure.
1169 *
1170 * This leaf block cannot have a "remote" value, we only call this routine
1171 * if bmap_one_block() says there is only one block (ie: no remote blks).
1172 */
ba0f32d4 1173STATIC int
1da177e4
LT
1174xfs_attr_leaf_get(xfs_da_args_t *args)
1175{
1176 xfs_dabuf_t *bp;
1177 int error;
1178
1179 args->blkno = 0;
1180 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
1181 XFS_ATTR_FORK);
1182 if (error)
1183 return(error);
1184 ASSERT(bp != NULL);
1185
1186 error = xfs_attr_leaf_lookup_int(bp, args);
1187 if (error != EEXIST) {
1188 xfs_da_brelse(args->trans, bp);
1189 return(error);
1190 }
1191 error = xfs_attr_leaf_getvalue(bp, args);
1192 xfs_da_brelse(args->trans, bp);
1193 if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
1194 error = xfs_attr_rmtval_get(args);
1195 }
1196 return(error);
1197}
1198
1199/*
1200 * Copy out attribute entries for attr_list(), for leaf attribute lists.
1201 */
1202STATIC int
1203xfs_attr_leaf_list(xfs_attr_list_context_t *context)
1204{
1205 xfs_attr_leafblock_t *leaf;
1206 int error;
1207 xfs_dabuf_t *bp;
1208
1209 context->cursor->blkno = 0;
1210 error = xfs_da_read_buf(NULL, context->dp, 0, -1, &bp, XFS_ATTR_FORK);
1211 if (error)
726801ba 1212 return XFS_ERROR(error);
1da177e4
LT
1213 ASSERT(bp != NULL);
1214 leaf = bp->data;
89da0544 1215 if (unlikely(be16_to_cpu(leaf->hdr.info.magic) != XFS_ATTR_LEAF_MAGIC)) {
1da177e4
LT
1216 XFS_CORRUPTION_ERROR("xfs_attr_leaf_list", XFS_ERRLEVEL_LOW,
1217 context->dp->i_mount, leaf);
1218 xfs_da_brelse(NULL, bp);
726801ba 1219 return XFS_ERROR(EFSCORRUPTED);
1da177e4
LT
1220 }
1221
726801ba 1222 error = xfs_attr_leaf_list_int(bp, context);
1da177e4 1223 xfs_da_brelse(NULL, bp);
726801ba 1224 return XFS_ERROR(error);
1da177e4
LT
1225}
1226
1227
1228/*========================================================================
1229 * External routines when attribute list size > XFS_LBSIZE(mp).
1230 *========================================================================*/
1231
1232/*
1233 * Add a name to a Btree-format attribute list.
1234 *
1235 * This will involve walking down the Btree, and may involve splitting
1236 * leaf nodes and even splitting intermediate nodes up to and including
1237 * the root node (a special case of an intermediate node).
1238 *
1239 * "Remote" attribute values confuse the issue and atomic rename operations
1240 * add a whole extra layer of confusion on top of that.
1241 */
1242STATIC int
1243xfs_attr_node_addname(xfs_da_args_t *args)
1244{
1245 xfs_da_state_t *state;
1246 xfs_da_state_blk_t *blk;
1247 xfs_inode_t *dp;
1248 xfs_mount_t *mp;
1249 int committed, retval, error;
1250
1251 /*
1252 * Fill in bucket of arguments/results/context to carry around.
1253 */
1254 dp = args->dp;
1255 mp = dp->i_mount;
1256restart:
1257 state = xfs_da_state_alloc();
1258 state->args = args;
1259 state->mp = mp;
1260 state->blocksize = state->mp->m_sb.sb_blocksize;
1261 state->node_ents = state->mp->m_attr_node_ents;
1262
1263 /*
1264 * Search to see if name already exists, and get back a pointer
1265 * to where it should go.
1266 */
1267 error = xfs_da_node_lookup_int(state, &retval);
1268 if (error)
1269 goto out;
1270 blk = &state->path.blk[ state->path.active-1 ];
1271 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1272 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
1273 goto out;
1274 } else if (retval == EEXIST) {
1275 if (args->flags & ATTR_CREATE)
1276 goto out;
6a178100 1277 args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
1da177e4
LT
1278 args->blkno2 = args->blkno; /* set 2nd entry info*/
1279 args->index2 = args->index;
1280 args->rmtblkno2 = args->rmtblkno;
1281 args->rmtblkcnt2 = args->rmtblkcnt;
1282 args->rmtblkno = 0;
1283 args->rmtblkcnt = 0;
1284 }
1285
1286 retval = xfs_attr_leaf_add(blk->bp, state->args);
1287 if (retval == ENOSPC) {
1288 if (state->path.active == 1) {
1289 /*
1290 * Its really a single leaf node, but it had
1291 * out-of-line values so it looked like it *might*
1292 * have been a b-tree.
1293 */
1294 xfs_da_state_free(state);
9d87c319 1295 xfs_bmap_init(args->flist, args->firstblock);
1da177e4
LT
1296 error = xfs_attr_leaf_to_node(args);
1297 if (!error) {
1298 error = xfs_bmap_finish(&args->trans,
1299 args->flist,
1da177e4
LT
1300 &committed);
1301 }
1302 if (error) {
1303 ASSERT(committed);
1304 args->trans = NULL;
1305 xfs_bmap_cancel(args->flist);
1306 goto out;
1307 }
1308
1309 /*
1310 * bmap_finish() may have committed the last trans
1311 * and started a new one. We need the inode to be
1312 * in all transactions.
1313 */
1314 if (committed) {
1315 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1316 xfs_trans_ihold(args->trans, dp);
1317 }
1318
1319 /*
1320 * Commit the node conversion and start the next
1321 * trans in the chain.
1322 */
322ff6b8
NS
1323 error = xfs_trans_roll(&args->trans, dp);
1324 if (error)
1da177e4
LT
1325 goto out;
1326
1327 goto restart;
1328 }
1329
1330 /*
1331 * Split as many Btree elements as required.
1332 * This code tracks the new and old attr's location
1333 * in the index/blkno/rmtblkno/rmtblkcnt fields and
1334 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
1335 */
9d87c319 1336 xfs_bmap_init(args->flist, args->firstblock);
1da177e4
LT
1337 error = xfs_da_split(state);
1338 if (!error) {
1339 error = xfs_bmap_finish(&args->trans, args->flist,
f7c99b6f 1340 &committed);
1da177e4
LT
1341 }
1342 if (error) {
1343 ASSERT(committed);
1344 args->trans = NULL;
1345 xfs_bmap_cancel(args->flist);
1346 goto out;
1347 }
1348
1349 /*
1350 * bmap_finish() may have committed the last trans and started
1351 * a new one. We need the inode to be in all transactions.
1352 */
1353 if (committed) {
1354 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1355 xfs_trans_ihold(args->trans, dp);
1356 }
1357 } else {
1358 /*
1359 * Addition succeeded, update Btree hashvals.
1360 */
1361 xfs_da_fixhashpath(state, &state->path);
1362 }
1363
1364 /*
1365 * Kill the state structure, we're done with it and need to
1366 * allow the buffers to come back later.
1367 */
1368 xfs_da_state_free(state);
1369 state = NULL;
1370
1371 /*
1372 * Commit the leaf addition or btree split and start the next
1373 * trans in the chain.
1374 */
322ff6b8
NS
1375 error = xfs_trans_roll(&args->trans, dp);
1376 if (error)
1da177e4
LT
1377 goto out;
1378
1379 /*
1380 * If there was an out-of-line value, allocate the blocks we
1381 * identified for its storage and copy the value. This is done
1382 * after we create the attribute so that we don't overflow the
1383 * maximum size of a transaction and/or hit a deadlock.
1384 */
1385 if (args->rmtblkno > 0) {
1386 error = xfs_attr_rmtval_set(args);
1387 if (error)
1388 return(error);
1389 }
1390
1391 /*
1392 * If this is an atomic rename operation, we must "flip" the
1393 * incomplete flags on the "new" and "old" attribute/value pairs
1394 * so that one disappears and one appears atomically. Then we
1395 * must remove the "old" attribute/value pair.
1396 */
6a178100 1397 if (args->op_flags & XFS_DA_OP_RENAME) {
1da177e4
LT
1398 /*
1399 * In a separate transaction, set the incomplete flag on the
1400 * "old" attr and clear the incomplete flag on the "new" attr.
1401 */
1402 error = xfs_attr_leaf_flipflags(args);
1403 if (error)
1404 goto out;
1405
1406 /*
1407 * Dismantle the "old" attribute/value pair by removing
1408 * a "remote" value (if it exists).
1409 */
1410 args->index = args->index2;
1411 args->blkno = args->blkno2;
1412 args->rmtblkno = args->rmtblkno2;
1413 args->rmtblkcnt = args->rmtblkcnt2;
1414 if (args->rmtblkno) {
1415 error = xfs_attr_rmtval_remove(args);
1416 if (error)
1417 return(error);
1418 }
1419
1420 /*
1421 * Re-find the "old" attribute entry after any split ops.
1422 * The INCOMPLETE flag means that we will find the "old"
1423 * attr, not the "new" one.
1424 */
1425 args->flags |= XFS_ATTR_INCOMPLETE;
1426 state = xfs_da_state_alloc();
1427 state->args = args;
1428 state->mp = mp;
1429 state->blocksize = state->mp->m_sb.sb_blocksize;
1430 state->node_ents = state->mp->m_attr_node_ents;
1431 state->inleaf = 0;
1432 error = xfs_da_node_lookup_int(state, &retval);
1433 if (error)
1434 goto out;
1435
1436 /*
1437 * Remove the name and update the hashvals in the tree.
1438 */
1439 blk = &state->path.blk[ state->path.active-1 ];
1440 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1441 error = xfs_attr_leaf_remove(blk->bp, args);
1442 xfs_da_fixhashpath(state, &state->path);
1443
1444 /*
1445 * Check to see if the tree needs to be collapsed.
1446 */
1447 if (retval && (state->path.active > 1)) {
9d87c319 1448 xfs_bmap_init(args->flist, args->firstblock);
1da177e4
LT
1449 error = xfs_da_join(state);
1450 if (!error) {
1451 error = xfs_bmap_finish(&args->trans,
1452 args->flist,
1da177e4
LT
1453 &committed);
1454 }
1455 if (error) {
1456 ASSERT(committed);
1457 args->trans = NULL;
1458 xfs_bmap_cancel(args->flist);
1459 goto out;
1460 }
1461
1462 /*
1463 * bmap_finish() may have committed the last trans
1464 * and started a new one. We need the inode to be
1465 * in all transactions.
1466 */
1467 if (committed) {
1468 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1469 xfs_trans_ihold(args->trans, dp);
1470 }
1471 }
1472
1473 /*
1474 * Commit and start the next trans in the chain.
1475 */
322ff6b8
NS
1476 error = xfs_trans_roll(&args->trans, dp);
1477 if (error)
1da177e4
LT
1478 goto out;
1479
1480 } else if (args->rmtblkno > 0) {
1481 /*
1482 * Added a "remote" value, just clear the incomplete flag.
1483 */
1484 error = xfs_attr_leaf_clearflag(args);
1485 if (error)
1486 goto out;
1487 }
1488 retval = error = 0;
1489
1490out:
1491 if (state)
1492 xfs_da_state_free(state);
1493 if (error)
1494 return(error);
1495 return(retval);
1496}
1497
1498/*
1499 * Remove a name from a B-tree attribute list.
1500 *
1501 * This will involve walking down the Btree, and may involve joining
1502 * leaf nodes and even joining intermediate nodes up to and including
1503 * the root node (a special case of an intermediate node).
1504 */
1505STATIC int
1506xfs_attr_node_removename(xfs_da_args_t *args)
1507{
1508 xfs_da_state_t *state;
1509 xfs_da_state_blk_t *blk;
1510 xfs_inode_t *dp;
1511 xfs_dabuf_t *bp;
d8cc890d 1512 int retval, error, committed, forkoff;
1da177e4
LT
1513
1514 /*
1515 * Tie a string around our finger to remind us where we are.
1516 */
1517 dp = args->dp;
1518 state = xfs_da_state_alloc();
1519 state->args = args;
1520 state->mp = dp->i_mount;
1521 state->blocksize = state->mp->m_sb.sb_blocksize;
1522 state->node_ents = state->mp->m_attr_node_ents;
1523
1524 /*
1525 * Search to see if name exists, and get back a pointer to it.
1526 */
1527 error = xfs_da_node_lookup_int(state, &retval);
1528 if (error || (retval != EEXIST)) {
1529 if (error == 0)
1530 error = retval;
1531 goto out;
1532 }
1533
1534 /*
1535 * If there is an out-of-line value, de-allocate the blocks.
1536 * This is done before we remove the attribute so that we don't
1537 * overflow the maximum size of a transaction and/or hit a deadlock.
1538 */
1539 blk = &state->path.blk[ state->path.active-1 ];
1540 ASSERT(blk->bp != NULL);
1541 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1542 if (args->rmtblkno > 0) {
1543 /*
1544 * Fill in disk block numbers in the state structure
1545 * so that we can get the buffers back after we commit
1546 * several transactions in the following calls.
1547 */
1548 error = xfs_attr_fillstate(state);
1549 if (error)
1550 goto out;
1551
1552 /*
1553 * Mark the attribute as INCOMPLETE, then bunmapi() the
1554 * remote value.
1555 */
1556 error = xfs_attr_leaf_setflag(args);
1557 if (error)
1558 goto out;
1559 error = xfs_attr_rmtval_remove(args);
1560 if (error)
1561 goto out;
1562
1563 /*
1564 * Refill the state structure with buffers, the prior calls
1565 * released our buffers.
1566 */
1567 error = xfs_attr_refillstate(state);
1568 if (error)
1569 goto out;
1570 }
1571
1572 /*
1573 * Remove the name and update the hashvals in the tree.
1574 */
1575 blk = &state->path.blk[ state->path.active-1 ];
1576 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1577 retval = xfs_attr_leaf_remove(blk->bp, args);
1578 xfs_da_fixhashpath(state, &state->path);
1579
1580 /*
1581 * Check to see if the tree needs to be collapsed.
1582 */
1583 if (retval && (state->path.active > 1)) {
9d87c319 1584 xfs_bmap_init(args->flist, args->firstblock);
1da177e4
LT
1585 error = xfs_da_join(state);
1586 if (!error) {
1587 error = xfs_bmap_finish(&args->trans, args->flist,
f7c99b6f 1588 &committed);
1da177e4
LT
1589 }
1590 if (error) {
1591 ASSERT(committed);
1592 args->trans = NULL;
1593 xfs_bmap_cancel(args->flist);
1594 goto out;
1595 }
1596
1597 /*
1598 * bmap_finish() may have committed the last trans and started
1599 * a new one. We need the inode to be in all transactions.
1600 */
1601 if (committed) {
1602 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1603 xfs_trans_ihold(args->trans, dp);
1604 }
1605
1606 /*
1607 * Commit the Btree join operation and start a new trans.
1608 */
322ff6b8
NS
1609 error = xfs_trans_roll(&args->trans, dp);
1610 if (error)
1da177e4
LT
1611 goto out;
1612 }
1613
1614 /*
1615 * If the result is small enough, push it all into the inode.
1616 */
1617 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
1618 /*
1619 * Have to get rid of the copy of this dabuf in the state.
1620 */
1621 ASSERT(state->path.active == 1);
1622 ASSERT(state->path.blk[0].bp);
1623 xfs_da_buf_done(state->path.blk[0].bp);
1624 state->path.blk[0].bp = NULL;
1625
1626 error = xfs_da_read_buf(args->trans, args->dp, 0, -1, &bp,
1627 XFS_ATTR_FORK);
1628 if (error)
1629 goto out;
89da0544
NS
1630 ASSERT(be16_to_cpu(((xfs_attr_leafblock_t *)
1631 bp->data)->hdr.info.magic)
1da177e4
LT
1632 == XFS_ATTR_LEAF_MAGIC);
1633
d8cc890d 1634 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
9d87c319 1635 xfs_bmap_init(args->flist, args->firstblock);
d8cc890d 1636 error = xfs_attr_leaf_to_shortform(bp, args, forkoff);
1da177e4
LT
1637 /* bp is gone due to xfs_da_shrink_inode */
1638 if (!error) {
1639 error = xfs_bmap_finish(&args->trans,
1640 args->flist,
1da177e4
LT
1641 &committed);
1642 }
1643 if (error) {
1644 ASSERT(committed);
1645 args->trans = NULL;
1646 xfs_bmap_cancel(args->flist);
1647 goto out;
1648 }
1649
1650 /*
1651 * bmap_finish() may have committed the last trans
1652 * and started a new one. We need the inode to be
1653 * in all transactions.
1654 */
1655 if (committed) {
1656 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1657 xfs_trans_ihold(args->trans, dp);
1658 }
1659 } else
1660 xfs_da_brelse(args->trans, bp);
1661 }
1662 error = 0;
1663
1664out:
1665 xfs_da_state_free(state);
1666 return(error);
1667}
1668
1669/*
1670 * Fill in the disk block numbers in the state structure for the buffers
1671 * that are attached to the state structure.
1672 * This is done so that we can quickly reattach ourselves to those buffers
c41564b5 1673 * after some set of transaction commits have released these buffers.
1da177e4
LT
1674 */
1675STATIC int
1676xfs_attr_fillstate(xfs_da_state_t *state)
1677{
1678 xfs_da_state_path_t *path;
1679 xfs_da_state_blk_t *blk;
1680 int level;
1681
1682 /*
1683 * Roll down the "path" in the state structure, storing the on-disk
1684 * block number for those buffers in the "path".
1685 */
1686 path = &state->path;
1687 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1688 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1689 if (blk->bp) {
1690 blk->disk_blkno = xfs_da_blkno(blk->bp);
1691 xfs_da_buf_done(blk->bp);
1692 blk->bp = NULL;
1693 } else {
1694 blk->disk_blkno = 0;
1695 }
1696 }
1697
1698 /*
1699 * Roll down the "altpath" in the state structure, storing the on-disk
1700 * block number for those buffers in the "altpath".
1701 */
1702 path = &state->altpath;
1703 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1704 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1705 if (blk->bp) {
1706 blk->disk_blkno = xfs_da_blkno(blk->bp);
1707 xfs_da_buf_done(blk->bp);
1708 blk->bp = NULL;
1709 } else {
1710 blk->disk_blkno = 0;
1711 }
1712 }
1713
1714 return(0);
1715}
1716
1717/*
1718 * Reattach the buffers to the state structure based on the disk block
1719 * numbers stored in the state structure.
c41564b5 1720 * This is done after some set of transaction commits have released those
1da177e4
LT
1721 * buffers from our grip.
1722 */
1723STATIC int
1724xfs_attr_refillstate(xfs_da_state_t *state)
1725{
1726 xfs_da_state_path_t *path;
1727 xfs_da_state_blk_t *blk;
1728 int level, error;
1729
1730 /*
1731 * Roll down the "path" in the state structure, storing the on-disk
1732 * block number for those buffers in the "path".
1733 */
1734 path = &state->path;
1735 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1736 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1737 if (blk->disk_blkno) {
1738 error = xfs_da_read_buf(state->args->trans,
1739 state->args->dp,
1740 blk->blkno, blk->disk_blkno,
1741 &blk->bp, XFS_ATTR_FORK);
1742 if (error)
1743 return(error);
1744 } else {
1745 blk->bp = NULL;
1746 }
1747 }
1748
1749 /*
1750 * Roll down the "altpath" in the state structure, storing the on-disk
1751 * block number for those buffers in the "altpath".
1752 */
1753 path = &state->altpath;
1754 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1755 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1756 if (blk->disk_blkno) {
1757 error = xfs_da_read_buf(state->args->trans,
1758 state->args->dp,
1759 blk->blkno, blk->disk_blkno,
1760 &blk->bp, XFS_ATTR_FORK);
1761 if (error)
1762 return(error);
1763 } else {
1764 blk->bp = NULL;
1765 }
1766 }
1767
1768 return(0);
1769}
1770
1771/*
1772 * Look up a filename in a node attribute list.
1773 *
1774 * This routine gets called for any attribute fork that has more than one
1775 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1776 * "remote" values taking up more blocks.
1777 */
ba0f32d4 1778STATIC int
1da177e4
LT
1779xfs_attr_node_get(xfs_da_args_t *args)
1780{
1781 xfs_da_state_t *state;
1782 xfs_da_state_blk_t *blk;
1783 int error, retval;
1784 int i;
1785
1786 state = xfs_da_state_alloc();
1787 state->args = args;
1788 state->mp = args->dp->i_mount;
1789 state->blocksize = state->mp->m_sb.sb_blocksize;
1790 state->node_ents = state->mp->m_attr_node_ents;
1791
1792 /*
1793 * Search to see if name exists, and get back a pointer to it.
1794 */
1795 error = xfs_da_node_lookup_int(state, &retval);
1796 if (error) {
1797 retval = error;
1798 } else if (retval == EEXIST) {
1799 blk = &state->path.blk[ state->path.active-1 ];
1800 ASSERT(blk->bp != NULL);
1801 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1802
1803 /*
1804 * Get the value, local or "remote"
1805 */
1806 retval = xfs_attr_leaf_getvalue(blk->bp, args);
1807 if (!retval && (args->rmtblkno > 0)
1808 && !(args->flags & ATTR_KERNOVAL)) {
1809 retval = xfs_attr_rmtval_get(args);
1810 }
1811 }
1812
1813 /*
1814 * If not in a transaction, we have to release all the buffers.
1815 */
1816 for (i = 0; i < state->path.active; i++) {
1817 xfs_da_brelse(args->trans, state->path.blk[i].bp);
1818 state->path.blk[i].bp = NULL;
1819 }
1820
1821 xfs_da_state_free(state);
1822 return(retval);
1823}
1824
1825STATIC int /* error */
1826xfs_attr_node_list(xfs_attr_list_context_t *context)
1827{
1828 attrlist_cursor_kern_t *cursor;
1829 xfs_attr_leafblock_t *leaf;
1830 xfs_da_intnode_t *node;
1831 xfs_da_node_entry_t *btree;
1832 int error, i;
1833 xfs_dabuf_t *bp;
1834
1835 cursor = context->cursor;
1836 cursor->initted = 1;
1837
1838 /*
1839 * Do all sorts of validation on the passed-in cursor structure.
1840 * If anything is amiss, ignore the cursor and look up the hashval
1841 * starting from the btree root.
1842 */
1843 bp = NULL;
1844 if (cursor->blkno > 0) {
1845 error = xfs_da_read_buf(NULL, context->dp, cursor->blkno, -1,
1846 &bp, XFS_ATTR_FORK);
1847 if ((error != 0) && (error != EFSCORRUPTED))
1848 return(error);
1849 if (bp) {
1850 node = bp->data;
89da0544 1851 switch (be16_to_cpu(node->hdr.info.magic)) {
1da177e4
LT
1852 case XFS_DA_NODE_MAGIC:
1853 xfs_attr_trace_l_cn("wrong blk", context, node);
1854 xfs_da_brelse(NULL, bp);
1855 bp = NULL;
1856 break;
1857 case XFS_ATTR_LEAF_MAGIC:
1858 leaf = bp->data;
6b19f2d8
NS
1859 if (cursor->hashval > be32_to_cpu(leaf->entries[
1860 be16_to_cpu(leaf->hdr.count)-1].hashval)) {
1da177e4
LT
1861 xfs_attr_trace_l_cl("wrong blk",
1862 context, leaf);
1863 xfs_da_brelse(NULL, bp);
1864 bp = NULL;
1865 } else if (cursor->hashval <=
6b19f2d8 1866 be32_to_cpu(leaf->entries[0].hashval)) {
1da177e4
LT
1867 xfs_attr_trace_l_cl("maybe wrong blk",
1868 context, leaf);
1869 xfs_da_brelse(NULL, bp);
1870 bp = NULL;
1871 }
1872 break;
1873 default:
1874 xfs_attr_trace_l_c("wrong blk - ??", context);
1875 xfs_da_brelse(NULL, bp);
1876 bp = NULL;
1877 }
1878 }
1879 }
1880
1881 /*
1882 * We did not find what we expected given the cursor's contents,
1883 * so we start from the top and work down based on the hash value.
1884 * Note that start of node block is same as start of leaf block.
1885 */
1886 if (bp == NULL) {
1887 cursor->blkno = 0;
1888 for (;;) {
1889 error = xfs_da_read_buf(NULL, context->dp,
1890 cursor->blkno, -1, &bp,
1891 XFS_ATTR_FORK);
1892 if (error)
1893 return(error);
1894 if (unlikely(bp == NULL)) {
1895 XFS_ERROR_REPORT("xfs_attr_node_list(2)",
1896 XFS_ERRLEVEL_LOW,
1897 context->dp->i_mount);
1898 return(XFS_ERROR(EFSCORRUPTED));
1899 }
1900 node = bp->data;
89da0544 1901 if (be16_to_cpu(node->hdr.info.magic)
1da177e4
LT
1902 == XFS_ATTR_LEAF_MAGIC)
1903 break;
89da0544 1904 if (unlikely(be16_to_cpu(node->hdr.info.magic)
1da177e4
LT
1905 != XFS_DA_NODE_MAGIC)) {
1906 XFS_CORRUPTION_ERROR("xfs_attr_node_list(3)",
1907 XFS_ERRLEVEL_LOW,
1908 context->dp->i_mount,
1909 node);
1910 xfs_da_brelse(NULL, bp);
1911 return(XFS_ERROR(EFSCORRUPTED));
1912 }
1913 btree = node->btree;
fac80cce 1914 for (i = 0; i < be16_to_cpu(node->hdr.count);
1da177e4
LT
1915 btree++, i++) {
1916 if (cursor->hashval
403432dc
NS
1917 <= be32_to_cpu(btree->hashval)) {
1918 cursor->blkno = be32_to_cpu(btree->before);
1da177e4
LT
1919 xfs_attr_trace_l_cb("descending",
1920 context, btree);
1921 break;
1922 }
1923 }
fac80cce 1924 if (i == be16_to_cpu(node->hdr.count)) {
1da177e4
LT
1925 xfs_da_brelse(NULL, bp);
1926 return(0);
1927 }
1928 xfs_da_brelse(NULL, bp);
1929 }
1930 }
1931 ASSERT(bp != NULL);
1932
1933 /*
1934 * Roll upward through the blocks, processing each leaf block in
1935 * order. As long as there is space in the result buffer, keep
1936 * adding the information.
1937 */
1938 for (;;) {
1939 leaf = bp->data;
89da0544 1940 if (unlikely(be16_to_cpu(leaf->hdr.info.magic)
1da177e4
LT
1941 != XFS_ATTR_LEAF_MAGIC)) {
1942 XFS_CORRUPTION_ERROR("xfs_attr_node_list(4)",
1943 XFS_ERRLEVEL_LOW,
1944 context->dp->i_mount, leaf);
1945 xfs_da_brelse(NULL, bp);
1946 return(XFS_ERROR(EFSCORRUPTED));
1947 }
1948 error = xfs_attr_leaf_list_int(bp, context);
726801ba
TS
1949 if (error) {
1950 xfs_da_brelse(NULL, bp);
1951 return error;
1952 }
1953 if (context->seen_enough || leaf->hdr.info.forw == 0)
1954 break;
89da0544 1955 cursor->blkno = be32_to_cpu(leaf->hdr.info.forw);
1da177e4
LT
1956 xfs_da_brelse(NULL, bp);
1957 error = xfs_da_read_buf(NULL, context->dp, cursor->blkno, -1,
1958 &bp, XFS_ATTR_FORK);
1959 if (error)
1960 return(error);
1961 if (unlikely((bp == NULL))) {
1962 XFS_ERROR_REPORT("xfs_attr_node_list(5)",
1963 XFS_ERRLEVEL_LOW,
1964 context->dp->i_mount);
1965 return(XFS_ERROR(EFSCORRUPTED));
1966 }
1967 }
1968 xfs_da_brelse(NULL, bp);
1969 return(0);
1970}
1971
1972
1973/*========================================================================
1974 * External routines for manipulating out-of-line attribute values.
1975 *========================================================================*/
1976
1977/*
1978 * Read the value associated with an attribute from the out-of-line buffer
1979 * that we stored it in.
1980 */
726801ba 1981int
1da177e4
LT
1982xfs_attr_rmtval_get(xfs_da_args_t *args)
1983{
1984 xfs_bmbt_irec_t map[ATTR_RMTVALUE_MAPSIZE];
1985 xfs_mount_t *mp;
1986 xfs_daddr_t dblkno;
1987 xfs_caddr_t dst;
1988 xfs_buf_t *bp;
1989 int nmap, error, tmp, valuelen, blkcnt, i;
1990 xfs_dablk_t lblkno;
1991
1992 ASSERT(!(args->flags & ATTR_KERNOVAL));
1993
1994 mp = args->dp->i_mount;
1995 dst = args->value;
1996 valuelen = args->valuelen;
1997 lblkno = args->rmtblkno;
1998 while (valuelen > 0) {
1999 nmap = ATTR_RMTVALUE_MAPSIZE;
2000 error = xfs_bmapi(args->trans, args->dp, (xfs_fileoff_t)lblkno,
2001 args->rmtblkcnt,
2002 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
3e57ecf6 2003 NULL, 0, map, &nmap, NULL, NULL);
1da177e4
LT
2004 if (error)
2005 return(error);
2006 ASSERT(nmap >= 1);
2007
2008 for (i = 0; (i < nmap) && (valuelen > 0); i++) {
2009 ASSERT((map[i].br_startblock != DELAYSTARTBLOCK) &&
2010 (map[i].br_startblock != HOLESTARTBLOCK));
2011 dblkno = XFS_FSB_TO_DADDR(mp, map[i].br_startblock);
2012 blkcnt = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
2013 error = xfs_read_buf(mp, mp->m_ddev_targp, dblkno,
2014 blkcnt, XFS_BUF_LOCK, &bp);
2015 if (error)
2016 return(error);
2017
2018 tmp = (valuelen < XFS_BUF_SIZE(bp))
2019 ? valuelen : XFS_BUF_SIZE(bp);
2020 xfs_biomove(bp, 0, tmp, dst, XFS_B_READ);
2021 xfs_buf_relse(bp);
2022 dst += tmp;
2023 valuelen -= tmp;
2024
2025 lblkno += map[i].br_blockcount;
2026 }
2027 }
2028 ASSERT(valuelen == 0);
2029 return(0);
2030}
2031
2032/*
2033 * Write the value associated with an attribute into the out-of-line buffer
2034 * that we have defined for it.
2035 */
2036STATIC int
2037xfs_attr_rmtval_set(xfs_da_args_t *args)
2038{
2039 xfs_mount_t *mp;
2040 xfs_fileoff_t lfileoff;
2041 xfs_inode_t *dp;
2042 xfs_bmbt_irec_t map;
2043 xfs_daddr_t dblkno;
2044 xfs_caddr_t src;
2045 xfs_buf_t *bp;
2046 xfs_dablk_t lblkno;
2047 int blkcnt, valuelen, nmap, error, tmp, committed;
2048
2049 dp = args->dp;
2050 mp = dp->i_mount;
2051 src = args->value;
2052
2053 /*
2054 * Find a "hole" in the attribute address space large enough for
2055 * us to drop the new attribute's value into.
2056 */
2057 blkcnt = XFS_B_TO_FSB(mp, args->valuelen);
2058 lfileoff = 0;
2059 error = xfs_bmap_first_unused(args->trans, args->dp, blkcnt, &lfileoff,
2060 XFS_ATTR_FORK);
2061 if (error) {
2062 return(error);
2063 }
2064 args->rmtblkno = lblkno = (xfs_dablk_t)lfileoff;
2065 args->rmtblkcnt = blkcnt;
2066
2067 /*
2068 * Roll through the "value", allocating blocks on disk as required.
2069 */
2070 while (blkcnt > 0) {
2071 /*
2072 * Allocate a single extent, up to the size of the value.
2073 */
9d87c319 2074 xfs_bmap_init(args->flist, args->firstblock);
1da177e4
LT
2075 nmap = 1;
2076 error = xfs_bmapi(args->trans, dp, (xfs_fileoff_t)lblkno,
2077 blkcnt,
2078 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA |
2079 XFS_BMAPI_WRITE,
2080 args->firstblock, args->total, &map, &nmap,
3e57ecf6 2081 args->flist, NULL);
1da177e4
LT
2082 if (!error) {
2083 error = xfs_bmap_finish(&args->trans, args->flist,
f7c99b6f 2084 &committed);
1da177e4
LT
2085 }
2086 if (error) {
2087 ASSERT(committed);
2088 args->trans = NULL;
2089 xfs_bmap_cancel(args->flist);
2090 return(error);
2091 }
2092
2093 /*
2094 * bmap_finish() may have committed the last trans and started
2095 * a new one. We need the inode to be in all transactions.
2096 */
2097 if (committed) {
2098 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
2099 xfs_trans_ihold(args->trans, dp);
2100 }
2101
2102 ASSERT(nmap == 1);
2103 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
2104 (map.br_startblock != HOLESTARTBLOCK));
2105 lblkno += map.br_blockcount;
2106 blkcnt -= map.br_blockcount;
2107
2108 /*
2109 * Start the next trans in the chain.
2110 */
322ff6b8
NS
2111 error = xfs_trans_roll(&args->trans, dp);
2112 if (error)
1da177e4
LT
2113 return (error);
2114 }
2115
2116 /*
2117 * Roll through the "value", copying the attribute value to the
2118 * already-allocated blocks. Blocks are written synchronously
2119 * so that we can know they are all on disk before we turn off
2120 * the INCOMPLETE flag.
2121 */
2122 lblkno = args->rmtblkno;
2123 valuelen = args->valuelen;
2124 while (valuelen > 0) {
2125 /*
2126 * Try to remember where we decided to put the value.
2127 */
9d87c319 2128 xfs_bmap_init(args->flist, args->firstblock);
1da177e4
LT
2129 nmap = 1;
2130 error = xfs_bmapi(NULL, dp, (xfs_fileoff_t)lblkno,
2131 args->rmtblkcnt,
2132 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
3e57ecf6
OW
2133 args->firstblock, 0, &map, &nmap,
2134 NULL, NULL);
1da177e4
LT
2135 if (error) {
2136 return(error);
2137 }
2138 ASSERT(nmap == 1);
2139 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
2140 (map.br_startblock != HOLESTARTBLOCK));
2141
2142 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
2143 blkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
2144
2145 bp = xfs_buf_get_flags(mp->m_ddev_targp, dblkno,
2146 blkcnt, XFS_BUF_LOCK);
2147 ASSERT(bp);
2148 ASSERT(!XFS_BUF_GETERROR(bp));
2149
2150 tmp = (valuelen < XFS_BUF_SIZE(bp)) ? valuelen :
2151 XFS_BUF_SIZE(bp);
2152 xfs_biomove(bp, 0, tmp, src, XFS_B_WRITE);
2153 if (tmp < XFS_BUF_SIZE(bp))
2154 xfs_biozero(bp, tmp, XFS_BUF_SIZE(bp) - tmp);
2155 if ((error = xfs_bwrite(mp, bp))) {/* GROT: NOTE: synchronous write */
2156 return (error);
2157 }
2158 src += tmp;
2159 valuelen -= tmp;
2160
2161 lblkno += map.br_blockcount;
2162 }
2163 ASSERT(valuelen == 0);
2164 return(0);
2165}
2166
2167/*
2168 * Remove the value associated with an attribute by deleting the
2169 * out-of-line buffer that it is stored on.
2170 */
2171STATIC int
2172xfs_attr_rmtval_remove(xfs_da_args_t *args)
2173{
2174 xfs_mount_t *mp;
2175 xfs_bmbt_irec_t map;
2176 xfs_buf_t *bp;
2177 xfs_daddr_t dblkno;
2178 xfs_dablk_t lblkno;
2179 int valuelen, blkcnt, nmap, error, done, committed;
2180
2181 mp = args->dp->i_mount;
2182
2183 /*
2184 * Roll through the "value", invalidating the attribute value's
2185 * blocks.
2186 */
2187 lblkno = args->rmtblkno;
2188 valuelen = args->rmtblkcnt;
2189 while (valuelen > 0) {
2190 /*
2191 * Try to remember where we decided to put the value.
2192 */
9d87c319 2193 xfs_bmap_init(args->flist, args->firstblock);
1da177e4
LT
2194 nmap = 1;
2195 error = xfs_bmapi(NULL, args->dp, (xfs_fileoff_t)lblkno,
2196 args->rmtblkcnt,
2197 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
2198 args->firstblock, 0, &map, &nmap,
3e57ecf6 2199 args->flist, NULL);
1da177e4
LT
2200 if (error) {
2201 return(error);
2202 }
2203 ASSERT(nmap == 1);
2204 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
2205 (map.br_startblock != HOLESTARTBLOCK));
2206
2207 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
2208 blkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
2209
2210 /*
2211 * If the "remote" value is in the cache, remove it.
2212 */
2213 bp = xfs_incore(mp->m_ddev_targp, dblkno, blkcnt,
2214 XFS_INCORE_TRYLOCK);
2215 if (bp) {
2216 XFS_BUF_STALE(bp);
2217 XFS_BUF_UNDELAYWRITE(bp);
2218 xfs_buf_relse(bp);
2219 bp = NULL;
2220 }
2221
2222 valuelen -= map.br_blockcount;
2223
2224 lblkno += map.br_blockcount;
2225 }
2226
2227 /*
2228 * Keep de-allocating extents until the remote-value region is gone.
2229 */
2230 lblkno = args->rmtblkno;
2231 blkcnt = args->rmtblkcnt;
2232 done = 0;
2233 while (!done) {
9d87c319 2234 xfs_bmap_init(args->flist, args->firstblock);
1da177e4
LT
2235 error = xfs_bunmapi(args->trans, args->dp, lblkno, blkcnt,
2236 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
3e57ecf6
OW
2237 1, args->firstblock, args->flist,
2238 NULL, &done);
1da177e4
LT
2239 if (!error) {
2240 error = xfs_bmap_finish(&args->trans, args->flist,
f7c99b6f 2241 &committed);
1da177e4
LT
2242 }
2243 if (error) {
2244 ASSERT(committed);
2245 args->trans = NULL;
2246 xfs_bmap_cancel(args->flist);
2247 return(error);
2248 }
2249
2250 /*
2251 * bmap_finish() may have committed the last trans and started
2252 * a new one. We need the inode to be in all transactions.
2253 */
2254 if (committed) {
2255 xfs_trans_ijoin(args->trans, args->dp, XFS_ILOCK_EXCL);
2256 xfs_trans_ihold(args->trans, args->dp);
2257 }
2258
2259 /*
2260 * Close out trans and start the next one in the chain.
2261 */
322ff6b8
NS
2262 error = xfs_trans_roll(&args->trans, args->dp);
2263 if (error)
1da177e4
LT
2264 return (error);
2265 }
2266 return(0);
2267}
2268
2269#if defined(XFS_ATTR_TRACE)
2270/*
2271 * Add a trace buffer entry for an attr_list context structure.
2272 */
2273void
2274xfs_attr_trace_l_c(char *where, struct xfs_attr_list_context *context)
2275{
68f34d51 2276 xfs_attr_trace_enter(XFS_ATTR_KTRACE_L_C, where, context,
1da177e4
LT
2277 (__psunsigned_t)NULL,
2278 (__psunsigned_t)NULL,
2279 (__psunsigned_t)NULL);
2280}
2281
2282/*
2283 * Add a trace buffer entry for a context structure and a Btree node.
2284 */
2285void
2286xfs_attr_trace_l_cn(char *where, struct xfs_attr_list_context *context,
2287 struct xfs_da_intnode *node)
2288{
68f34d51 2289 xfs_attr_trace_enter(XFS_ATTR_KTRACE_L_CN, where, context,
fac80cce 2290 (__psunsigned_t)be16_to_cpu(node->hdr.count),
403432dc 2291 (__psunsigned_t)be32_to_cpu(node->btree[0].hashval),
fac80cce
NS
2292 (__psunsigned_t)be32_to_cpu(node->btree[
2293 be16_to_cpu(node->hdr.count)-1].hashval));
1da177e4
LT
2294}
2295
2296/*
2297 * Add a trace buffer entry for a context structure and a Btree element.
2298 */
2299void
2300xfs_attr_trace_l_cb(char *where, struct xfs_attr_list_context *context,
2301 struct xfs_da_node_entry *btree)
2302{
68f34d51 2303 xfs_attr_trace_enter(XFS_ATTR_KTRACE_L_CB, where, context,
403432dc
NS
2304 (__psunsigned_t)be32_to_cpu(btree->hashval),
2305 (__psunsigned_t)be32_to_cpu(btree->before),
1da177e4
LT
2306 (__psunsigned_t)NULL);
2307}
2308
2309/*
2310 * Add a trace buffer entry for a context structure and a leaf block.
2311 */
2312void
2313xfs_attr_trace_l_cl(char *where, struct xfs_attr_list_context *context,
2314 struct xfs_attr_leafblock *leaf)
2315{
68f34d51 2316 xfs_attr_trace_enter(XFS_ATTR_KTRACE_L_CL, where, context,
918ae424 2317 (__psunsigned_t)be16_to_cpu(leaf->hdr.count),
6b19f2d8
NS
2318 (__psunsigned_t)be32_to_cpu(leaf->entries[0].hashval),
2319 (__psunsigned_t)be32_to_cpu(leaf->entries[
2320 be16_to_cpu(leaf->hdr.count)-1].hashval));
1da177e4
LT
2321}
2322
2323/*
2324 * Add a trace buffer entry for the arguments given to the routine,
2325 * generic form.
2326 */
2327void
2328xfs_attr_trace_enter(int type, char *where,
68f34d51
ES
2329 struct xfs_attr_list_context *context,
2330 __psunsigned_t a13, __psunsigned_t a14,
2331 __psunsigned_t a15)
1da177e4
LT
2332{
2333 ASSERT(xfs_attr_trace_buf);
2334 ktrace_enter(xfs_attr_trace_buf, (void *)((__psunsigned_t)type),
68f34d51
ES
2335 (void *)((__psunsigned_t)where),
2336 (void *)((__psunsigned_t)context->dp),
2337 (void *)((__psunsigned_t)context->cursor->hashval),
2338 (void *)((__psunsigned_t)context->cursor->blkno),
2339 (void *)((__psunsigned_t)context->cursor->offset),
2340 (void *)((__psunsigned_t)context->alist),
2341 (void *)((__psunsigned_t)context->bufsize),
2342 (void *)((__psunsigned_t)context->count),
2343 (void *)((__psunsigned_t)context->firstu),
ad9b463a 2344 NULL,
68f34d51
ES
2345 (void *)((__psunsigned_t)context->dupcnt),
2346 (void *)((__psunsigned_t)context->flags),
2347 (void *)a13, (void *)a14, (void *)a15);
1da177e4
LT
2348}
2349#endif /* XFS_ATTR_TRACE */