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