]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/xfs/xfs_attr.c
xfs: simplify buffer pinning
[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 28#include "xfs_mount.h"
a844f451 29#include "xfs_da_btree.h"
1da177e4 30#include "xfs_bmap_btree.h"
a844f451 31#include "xfs_attr_sf.h"
1da177e4 32#include "xfs_dinode.h"
1da177e4 33#include "xfs_inode.h"
a844f451 34#include "xfs_alloc.h"
a844f451 35#include "xfs_inode_item.h"
1da177e4 36#include "xfs_bmap.h"
1da177e4
LT
37#include "xfs_attr.h"
38#include "xfs_attr_leaf.h"
39#include "xfs_error.h"
1da177e4 40#include "xfs_quota.h"
1da177e4 41#include "xfs_trans_space.h"
a844f451 42#include "xfs_rw.h"
739bfb2a 43#include "xfs_vnodeops.h"
0b1b213f 44#include "xfs_trace.h"
1da177e4
LT
45
46/*
47 * xfs_attr.c
48 *
49 * Provide the external interfaces to manage attribute lists.
50 */
51
52/*========================================================================
53 * Function prototypes for the kernel.
54 *========================================================================*/
55
56/*
57 * Internal routines when attribute list fits inside the inode.
58 */
59STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
60
61/*
62 * Internal routines when attribute list is one block.
63 */
ba0f32d4 64STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
1da177e4
LT
65STATIC int xfs_attr_leaf_addname(xfs_da_args_t *args);
66STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
67STATIC int xfs_attr_leaf_list(xfs_attr_list_context_t *context);
68
69/*
70 * Internal routines when attribute list is more than one block.
71 */
ba0f32d4 72STATIC int xfs_attr_node_get(xfs_da_args_t *args);
1da177e4
LT
73STATIC int xfs_attr_node_addname(xfs_da_args_t *args);
74STATIC int xfs_attr_node_removename(xfs_da_args_t *args);
75STATIC int xfs_attr_node_list(xfs_attr_list_context_t *context);
76STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
77STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
78
79/*
80 * Routines to manipulate out-of-line attribute values.
81 */
1da177e4
LT
82STATIC int xfs_attr_rmtval_set(xfs_da_args_t *args);
83STATIC int xfs_attr_rmtval_remove(xfs_da_args_t *args);
84
85#define ATTR_RMTVALUE_MAPSIZE 1 /* # of map entries at once */
86
e8b0ebaa
BN
87STATIC int
88xfs_attr_name_to_xname(
89 struct xfs_name *xname,
a9273ca5 90 const unsigned char *aname)
e8b0ebaa
BN
91{
92 if (!aname)
93 return EINVAL;
94 xname->name = aname;
a9273ca5 95 xname->len = strlen((char *)aname);
e8b0ebaa
BN
96 if (xname->len >= MAXNAMELEN)
97 return EFAULT; /* match IRIX behaviour */
98
99 return 0;
100}
1da177e4 101
caf8aabd
CH
102STATIC int
103xfs_inode_hasattr(
104 struct xfs_inode *ip)
105{
106 if (!XFS_IFORK_Q(ip) ||
107 (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
108 ip->i_d.di_anextents == 0))
109 return 0;
110 return 1;
111}
112
1da177e4
LT
113/*========================================================================
114 * Overall external interface routines.
115 *========================================================================*/
116
e82fa0c7
CH
117STATIC int
118xfs_attr_get_int(
119 struct xfs_inode *ip,
120 struct xfs_name *name,
a9273ca5 121 unsigned char *value,
e82fa0c7
CH
122 int *valuelenp,
123 int flags)
1da177e4
LT
124{
125 xfs_da_args_t args;
126 int error;
127
caf8aabd
CH
128 if (!xfs_inode_hasattr(ip))
129 return ENOATTR;
1da177e4 130
1da177e4
LT
131 /*
132 * Fill in the arg structure for this request.
133 */
134 memset((char *)&args, 0, sizeof(args));
e8b0ebaa
BN
135 args.name = name->name;
136 args.namelen = name->len;
1da177e4
LT
137 args.value = value;
138 args.valuelen = *valuelenp;
139 args.flags = flags;
140 args.hashval = xfs_da_hashname(args.name, args.namelen);
141 args.dp = ip;
142 args.whichfork = XFS_ATTR_FORK;
143
144 /*
145 * Decide on what work routines to call based on the inode size.
146 */
caf8aabd 147 if (ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
1da177e4
LT
148 error = xfs_attr_shortform_getvalue(&args);
149 } else if (xfs_bmap_one_block(ip, XFS_ATTR_FORK)) {
150 error = xfs_attr_leaf_get(&args);
151 } else {
152 error = xfs_attr_node_get(&args);
153 }
154
155 /*
156 * Return the number of bytes in the value to the caller.
157 */
158 *valuelenp = args.valuelen;
159
160 if (error == EEXIST)
161 error = 0;
162 return(error);
163}
164
165int
993386c1
CH
166xfs_attr_get(
167 xfs_inode_t *ip,
a9273ca5
DC
168 const unsigned char *name,
169 unsigned char *value,
993386c1 170 int *valuelenp,
e8b0ebaa 171 int flags)
1da177e4 172{
e8b0ebaa
BN
173 int error;
174 struct xfs_name xname;
1da177e4
LT
175
176 XFS_STATS_INC(xs_attr_get);
177
1da177e4
LT
178 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
179 return(EIO);
180
e8b0ebaa
BN
181 error = xfs_attr_name_to_xname(&xname, name);
182 if (error)
183 return error;
184
1da177e4 185 xfs_ilock(ip, XFS_ILOCK_SHARED);
e82fa0c7 186 error = xfs_attr_get_int(ip, &xname, value, valuelenp, flags);
1da177e4
LT
187 xfs_iunlock(ip, XFS_ILOCK_SHARED);
188 return(error);
189}
190
5e9da7b7
NS
191/*
192 * Calculate how many blocks we need for the new attribute,
193 */
5d77c0dc 194STATIC int
5e9da7b7
NS
195xfs_attr_calc_size(
196 struct xfs_inode *ip,
197 int namelen,
198 int valuelen,
199 int *local)
200{
201 struct xfs_mount *mp = ip->i_mount;
202 int size;
203 int nblks;
204
205 /*
206 * Determine space new attribute will use, and if it would be
207 * "local" or "remote" (note: local != inline).
208 */
209 size = xfs_attr_leaf_newentsize(namelen, valuelen,
210 mp->m_sb.sb_blocksize, local);
211
212 nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
213 if (*local) {
214 if (size > (mp->m_sb.sb_blocksize >> 1)) {
215 /* Double split possible */
216 nblks *= 2;
217 }
218 } else {
219 /*
220 * Out of line attribute, cannot double split, but
221 * make room for the attribute value itself.
222 */
223 uint dblocks = XFS_B_TO_FSB(mp, valuelen);
224 nblks += dblocks;
225 nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
226 }
227
228 return nblks;
229}
230
e8b0ebaa 231STATIC int
a9273ca5
DC
232xfs_attr_set_int(
233 struct xfs_inode *dp,
234 struct xfs_name *name,
235 unsigned char *value,
236 int valuelen,
237 int flags)
1da177e4
LT
238{
239 xfs_da_args_t args;
1da177e4
LT
240 xfs_fsblock_t firstblock;
241 xfs_bmap_free_t flist;
242 int error, err2, committed;
aa82daa0 243 xfs_mount_t *mp = dp->i_mount;
1da177e4 244 int rsvd = (flags & ATTR_ROOT) != 0;
5e9da7b7 245 int local;
1da177e4
LT
246
247 /*
248 * Attach the dquots to the inode.
249 */
7d095257
CH
250 error = xfs_qm_dqattach(dp, 0);
251 if (error)
252 return error;
1da177e4
LT
253
254 /*
255 * If the inode doesn't have an attribute fork, add one.
256 * (inode must not be locked when we call this routine)
257 */
258 if (XFS_IFORK_Q(dp) == 0) {
e5889e90 259 int sf_size = sizeof(xfs_attr_sf_hdr_t) +
e8b0ebaa 260 XFS_ATTR_SF_ENTSIZE_BYNAME(name->len, valuelen);
e5889e90
BN
261
262 if ((error = xfs_bmap_add_attrfork(dp, sf_size, rsvd)))
1da177e4
LT
263 return(error);
264 }
265
266 /*
267 * Fill in the arg structure for this request.
268 */
269 memset((char *)&args, 0, sizeof(args));
e8b0ebaa
BN
270 args.name = name->name;
271 args.namelen = name->len;
1da177e4
LT
272 args.value = value;
273 args.valuelen = valuelen;
274 args.flags = flags;
275 args.hashval = xfs_da_hashname(args.name, args.namelen);
276 args.dp = dp;
277 args.firstblock = &firstblock;
278 args.flist = &flist;
279 args.whichfork = XFS_ATTR_FORK;
6a178100 280 args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
1da177e4 281
1da177e4 282 /* Size is now blocks for attribute data */
5e9da7b7 283 args.total = xfs_attr_calc_size(dp, name->len, valuelen, &local);
1da177e4
LT
284
285 /*
286 * Start our first transaction of the day.
287 *
288 * All future transactions during this code must be "chained" off
289 * this one via the trans_dup() call. All transactions will contain
290 * the inode, and the inode will always be marked with trans_ihold().
291 * Since the inode will be locked in all transactions, we must log
292 * the inode in every transaction to let it float upward through
293 * the log.
294 */
295 args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_SET);
296
297 /*
298 * Root fork attributes can use reserved data blocks for this
299 * operation if necessary
300 */
301
302 if (rsvd)
303 args.trans->t_flags |= XFS_TRANS_RESERVE;
304
5e9da7b7
NS
305 if ((error = xfs_trans_reserve(args.trans, args.total,
306 XFS_ATTRSET_LOG_RES(mp, args.total), 0,
307 XFS_TRANS_PERM_LOG_RES, XFS_ATTRSET_LOG_COUNT))) {
1da177e4
LT
308 xfs_trans_cancel(args.trans, 0);
309 return(error);
310 }
311 xfs_ilock(dp, XFS_ILOCK_EXCL);
312
7d095257 313 error = xfs_trans_reserve_quota_nblks(args.trans, dp, args.total, 0,
5e9da7b7
NS
314 rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
315 XFS_QMOPT_RES_REGBLKS);
1da177e4
LT
316 if (error) {
317 xfs_iunlock(dp, XFS_ILOCK_EXCL);
318 xfs_trans_cancel(args.trans, XFS_TRANS_RELEASE_LOG_RES);
319 return (error);
320 }
321
322 xfs_trans_ijoin(args.trans, dp, XFS_ILOCK_EXCL);
323 xfs_trans_ihold(args.trans, dp);
324
325 /*
c41564b5 326 * If the attribute list is non-existent or a shortform list,
1da177e4
LT
327 * upgrade it to a single-leaf-block attribute list.
328 */
329 if ((dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) ||
330 ((dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS) &&
331 (dp->i_d.di_anextents == 0))) {
332
333 /*
334 * Build initial attribute list (if required).
335 */
336 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
d8cc890d 337 xfs_attr_shortform_create(&args);
1da177e4
LT
338
339 /*
340 * Try to add the attr to the attribute list in
341 * the inode.
342 */
343 error = xfs_attr_shortform_addname(&args);
344 if (error != ENOSPC) {
345 /*
346 * Commit the shortform mods, and we're done.
347 * NOTE: this is also the error path (EEXIST, etc).
348 */
349 ASSERT(args.trans != NULL);
350
351 /*
352 * If this is a synchronous mount, make sure that
353 * the transaction goes to disk before returning
354 * to the user.
355 */
356 if (mp->m_flags & XFS_MOUNT_WSYNC) {
357 xfs_trans_set_sync(args.trans);
358 }
359 err2 = xfs_trans_commit(args.trans,
1c72bf90 360 XFS_TRANS_RELEASE_LOG_RES);
1da177e4
LT
361 xfs_iunlock(dp, XFS_ILOCK_EXCL);
362
363 /*
364 * Hit the inode change time.
365 */
366 if (!error && (flags & ATTR_KERNOTIME) == 0) {
367 xfs_ichgtime(dp, XFS_ICHGTIME_CHG);
368 }
369 return(error == 0 ? err2 : error);
370 }
371
372 /*
373 * It won't fit in the shortform, transform to a leaf block.
374 * GROT: another possible req'mt for a double-split btree op.
375 */
9d87c319 376 xfs_bmap_init(args.flist, args.firstblock);
1da177e4
LT
377 error = xfs_attr_shortform_to_leaf(&args);
378 if (!error) {
379 error = xfs_bmap_finish(&args.trans, args.flist,
f7c99b6f 380 &committed);
1da177e4
LT
381 }
382 if (error) {
383 ASSERT(committed);
384 args.trans = NULL;
385 xfs_bmap_cancel(&flist);
386 goto out;
387 }
388
389 /*
390 * bmap_finish() may have committed the last trans and started
391 * a new one. We need the inode to be in all transactions.
392 */
393 if (committed) {
394 xfs_trans_ijoin(args.trans, dp, XFS_ILOCK_EXCL);
395 xfs_trans_ihold(args.trans, dp);
396 }
397
398 /*
399 * Commit the leaf transformation. We'll need another (linked)
400 * transaction to add the new attribute to the leaf.
401 */
322ff6b8
NS
402
403 error = xfs_trans_roll(&args.trans, dp);
404 if (error)
1da177e4
LT
405 goto out;
406
407 }
408
409 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
410 error = xfs_attr_leaf_addname(&args);
411 } else {
412 error = xfs_attr_node_addname(&args);
413 }
414 if (error) {
415 goto out;
416 }
417
418 /*
419 * If this is a synchronous mount, make sure that the
420 * transaction goes to disk before returning to the user.
421 */
422 if (mp->m_flags & XFS_MOUNT_WSYNC) {
423 xfs_trans_set_sync(args.trans);
424 }
425
426 /*
427 * Commit the last in the sequence of transactions.
428 */
429 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
1c72bf90 430 error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
1da177e4
LT
431 xfs_iunlock(dp, XFS_ILOCK_EXCL);
432
433 /*
434 * Hit the inode change time.
435 */
436 if (!error && (flags & ATTR_KERNOTIME) == 0) {
437 xfs_ichgtime(dp, XFS_ICHGTIME_CHG);
438 }
439
440 return(error);
441
442out:
443 if (args.trans)
444 xfs_trans_cancel(args.trans,
445 XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
446 xfs_iunlock(dp, XFS_ILOCK_EXCL);
447 return(error);
448}
449
aa82daa0 450int
993386c1
CH
451xfs_attr_set(
452 xfs_inode_t *dp,
a9273ca5
DC
453 const unsigned char *name,
454 unsigned char *value,
993386c1
CH
455 int valuelen,
456 int flags)
1da177e4 457{
e8b0ebaa
BN
458 int error;
459 struct xfs_name xname;
1da177e4 460
aa82daa0 461 XFS_STATS_INC(xs_attr_set);
1da177e4 462
aa82daa0 463 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
1da177e4
LT
464 return (EIO);
465
e8b0ebaa
BN
466 error = xfs_attr_name_to_xname(&xname, name);
467 if (error)
468 return error;
469
470 return xfs_attr_set_int(dp, &xname, value, valuelen, flags);
aa82daa0
NS
471}
472
473/*
474 * Generic handler routine to remove a name from an attribute list.
475 * Transitions attribute list from Btree to shortform as necessary.
476 */
e8b0ebaa
BN
477STATIC int
478xfs_attr_remove_int(xfs_inode_t *dp, struct xfs_name *name, int flags)
aa82daa0
NS
479{
480 xfs_da_args_t args;
481 xfs_fsblock_t firstblock;
482 xfs_bmap_free_t flist;
483 int error;
484 xfs_mount_t *mp = dp->i_mount;
485
1da177e4
LT
486 /*
487 * Fill in the arg structure for this request.
488 */
489 memset((char *)&args, 0, sizeof(args));
e8b0ebaa
BN
490 args.name = name->name;
491 args.namelen = name->len;
1da177e4
LT
492 args.flags = flags;
493 args.hashval = xfs_da_hashname(args.name, args.namelen);
494 args.dp = dp;
495 args.firstblock = &firstblock;
496 args.flist = &flist;
497 args.total = 0;
498 args.whichfork = XFS_ATTR_FORK;
499
500 /*
501 * Attach the dquots to the inode.
502 */
7d095257
CH
503 error = xfs_qm_dqattach(dp, 0);
504 if (error)
505 return error;
1da177e4
LT
506
507 /*
508 * Start our first transaction of the day.
509 *
510 * All future transactions during this code must be "chained" off
511 * this one via the trans_dup() call. All transactions will contain
512 * the inode, and the inode will always be marked with trans_ihold().
513 * Since the inode will be locked in all transactions, we must log
514 * the inode in every transaction to let it float upward through
515 * the log.
516 */
517 args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_RM);
518
519 /*
520 * Root fork attributes can use reserved data blocks for this
521 * operation if necessary
522 */
523
524 if (flags & ATTR_ROOT)
525 args.trans->t_flags |= XFS_TRANS_RESERVE;
526
527 if ((error = xfs_trans_reserve(args.trans,
528 XFS_ATTRRM_SPACE_RES(mp),
529 XFS_ATTRRM_LOG_RES(mp),
530 0, XFS_TRANS_PERM_LOG_RES,
531 XFS_ATTRRM_LOG_COUNT))) {
532 xfs_trans_cancel(args.trans, 0);
533 return(error);
1da177e4
LT
534 }
535
536 xfs_ilock(dp, XFS_ILOCK_EXCL);
537 /*
538 * No need to make quota reservations here. We expect to release some
539 * blocks not allocate in the common case.
540 */
541 xfs_trans_ijoin(args.trans, dp, XFS_ILOCK_EXCL);
542 xfs_trans_ihold(args.trans, dp);
543
544 /*
545 * Decide on what work routines to call based on the inode size.
546 */
caf8aabd 547 if (!xfs_inode_hasattr(dp)) {
1da177e4
LT
548 error = XFS_ERROR(ENOATTR);
549 goto out;
550 }
551 if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
552 ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
553 error = xfs_attr_shortform_remove(&args);
554 if (error) {
555 goto out;
556 }
557 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
558 error = xfs_attr_leaf_removename(&args);
559 } else {
560 error = xfs_attr_node_removename(&args);
561 }
562 if (error) {
563 goto out;
564 }
565
566 /*
567 * If this is a synchronous mount, make sure that the
568 * transaction goes to disk before returning to the user.
569 */
570 if (mp->m_flags & XFS_MOUNT_WSYNC) {
571 xfs_trans_set_sync(args.trans);
572 }
573
574 /*
575 * Commit the last in the sequence of transactions.
576 */
577 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
1c72bf90 578 error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
1da177e4
LT
579 xfs_iunlock(dp, XFS_ILOCK_EXCL);
580
581 /*
582 * Hit the inode change time.
583 */
584 if (!error && (flags & ATTR_KERNOTIME) == 0) {
585 xfs_ichgtime(dp, XFS_ICHGTIME_CHG);
586 }
587
588 return(error);
589
590out:
591 if (args.trans)
592 xfs_trans_cancel(args.trans,
593 XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
594 xfs_iunlock(dp, XFS_ILOCK_EXCL);
595 return(error);
596}
597
aa82daa0 598int
993386c1
CH
599xfs_attr_remove(
600 xfs_inode_t *dp,
a9273ca5 601 const unsigned char *name,
993386c1 602 int flags)
aa82daa0 603{
e8b0ebaa
BN
604 int error;
605 struct xfs_name xname;
aa82daa0
NS
606
607 XFS_STATS_INC(xs_attr_remove);
608
aa82daa0
NS
609 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
610 return (EIO);
611
e8b0ebaa
BN
612 error = xfs_attr_name_to_xname(&xname, name);
613 if (error)
614 return error;
615
aa82daa0 616 xfs_ilock(dp, XFS_ILOCK_SHARED);
caf8aabd 617 if (!xfs_inode_hasattr(dp)) {
aa82daa0 618 xfs_iunlock(dp, XFS_ILOCK_SHARED);
caf8aabd 619 return XFS_ERROR(ENOATTR);
aa82daa0
NS
620 }
621 xfs_iunlock(dp, XFS_ILOCK_SHARED);
622
e8b0ebaa 623 return xfs_attr_remove_int(dp, &xname, flags);
aa82daa0
NS
624}
625
ad9b463a 626int
726801ba
TS
627xfs_attr_list_int(xfs_attr_list_context_t *context)
628{
629 int error;
630 xfs_inode_t *dp = context->dp;
631
ad9b463a
CH
632 XFS_STATS_INC(xs_attr_list);
633
634 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
635 return EIO;
636
637 xfs_ilock(dp, XFS_ILOCK_SHARED);
ad9b463a 638
726801ba
TS
639 /*
640 * Decide on what work routines to call based on the inode size.
641 */
caf8aabd 642 if (!xfs_inode_hasattr(dp)) {
726801ba
TS
643 error = 0;
644 } else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
645 error = xfs_attr_shortform_list(context);
646 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
647 error = xfs_attr_leaf_list(context);
648 } else {
649 error = xfs_attr_node_list(context);
650 }
ad9b463a
CH
651
652 xfs_iunlock(dp, XFS_ILOCK_SHARED);
ad9b463a 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
a9273ca5
DC
670xfs_attr_put_listent(
671 xfs_attr_list_context_t *context,
672 int flags,
673 unsigned char *name,
674 int namelen,
675 int valuelen,
676 unsigned char *value)
726801ba 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) {
0b1b213f 702 trace_xfs_attr_list_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;
0b1b213f 714 trace_xfs_attr_list_add(context);
726801ba
TS
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 1852 case XFS_DA_NODE_MAGIC:
0b1b213f 1853 trace_xfs_attr_list_wrong_blk(context);
1da177e4
LT
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)) {
0b1b213f 1861 trace_xfs_attr_list_wrong_blk(context);
1da177e4
LT
1862 xfs_da_brelse(NULL, bp);
1863 bp = NULL;
1864 } else if (cursor->hashval <=
6b19f2d8 1865 be32_to_cpu(leaf->entries[0].hashval)) {
0b1b213f 1866 trace_xfs_attr_list_wrong_blk(context);
1da177e4
LT
1867 xfs_da_brelse(NULL, bp);
1868 bp = NULL;
1869 }
1870 break;
1871 default:
0b1b213f 1872 trace_xfs_attr_list_wrong_blk(context);
1da177e4
LT
1873 xfs_da_brelse(NULL, bp);
1874 bp = NULL;
1875 }
1876 }
1877 }
1878
1879 /*
1880 * We did not find what we expected given the cursor's contents,
1881 * so we start from the top and work down based on the hash value.
1882 * Note that start of node block is same as start of leaf block.
1883 */
1884 if (bp == NULL) {
1885 cursor->blkno = 0;
1886 for (;;) {
1887 error = xfs_da_read_buf(NULL, context->dp,
1888 cursor->blkno, -1, &bp,
1889 XFS_ATTR_FORK);
1890 if (error)
1891 return(error);
1892 if (unlikely(bp == NULL)) {
1893 XFS_ERROR_REPORT("xfs_attr_node_list(2)",
1894 XFS_ERRLEVEL_LOW,
1895 context->dp->i_mount);
1896 return(XFS_ERROR(EFSCORRUPTED));
1897 }
1898 node = bp->data;
89da0544 1899 if (be16_to_cpu(node->hdr.info.magic)
1da177e4
LT
1900 == XFS_ATTR_LEAF_MAGIC)
1901 break;
89da0544 1902 if (unlikely(be16_to_cpu(node->hdr.info.magic)
1da177e4
LT
1903 != XFS_DA_NODE_MAGIC)) {
1904 XFS_CORRUPTION_ERROR("xfs_attr_node_list(3)",
1905 XFS_ERRLEVEL_LOW,
1906 context->dp->i_mount,
1907 node);
1908 xfs_da_brelse(NULL, bp);
1909 return(XFS_ERROR(EFSCORRUPTED));
1910 }
1911 btree = node->btree;
fac80cce 1912 for (i = 0; i < be16_to_cpu(node->hdr.count);
1da177e4
LT
1913 btree++, i++) {
1914 if (cursor->hashval
403432dc
NS
1915 <= be32_to_cpu(btree->hashval)) {
1916 cursor->blkno = be32_to_cpu(btree->before);
0b1b213f
CH
1917 trace_xfs_attr_list_node_descend(context,
1918 btree);
1da177e4
LT
1919 break;
1920 }
1921 }
fac80cce 1922 if (i == be16_to_cpu(node->hdr.count)) {
1da177e4
LT
1923 xfs_da_brelse(NULL, bp);
1924 return(0);
1925 }
1926 xfs_da_brelse(NULL, bp);
1927 }
1928 }
1929 ASSERT(bp != NULL);
1930
1931 /*
1932 * Roll upward through the blocks, processing each leaf block in
1933 * order. As long as there is space in the result buffer, keep
1934 * adding the information.
1935 */
1936 for (;;) {
1937 leaf = bp->data;
89da0544 1938 if (unlikely(be16_to_cpu(leaf->hdr.info.magic)
1da177e4
LT
1939 != XFS_ATTR_LEAF_MAGIC)) {
1940 XFS_CORRUPTION_ERROR("xfs_attr_node_list(4)",
1941 XFS_ERRLEVEL_LOW,
1942 context->dp->i_mount, leaf);
1943 xfs_da_brelse(NULL, bp);
1944 return(XFS_ERROR(EFSCORRUPTED));
1945 }
1946 error = xfs_attr_leaf_list_int(bp, context);
726801ba
TS
1947 if (error) {
1948 xfs_da_brelse(NULL, bp);
1949 return error;
1950 }
1951 if (context->seen_enough || leaf->hdr.info.forw == 0)
1952 break;
89da0544 1953 cursor->blkno = be32_to_cpu(leaf->hdr.info.forw);
1da177e4
LT
1954 xfs_da_brelse(NULL, bp);
1955 error = xfs_da_read_buf(NULL, context->dp, cursor->blkno, -1,
1956 &bp, XFS_ATTR_FORK);
1957 if (error)
1958 return(error);
1959 if (unlikely((bp == NULL))) {
1960 XFS_ERROR_REPORT("xfs_attr_node_list(5)",
1961 XFS_ERRLEVEL_LOW,
1962 context->dp->i_mount);
1963 return(XFS_ERROR(EFSCORRUPTED));
1964 }
1965 }
1966 xfs_da_brelse(NULL, bp);
1967 return(0);
1968}
1969
1970
1971/*========================================================================
1972 * External routines for manipulating out-of-line attribute values.
1973 *========================================================================*/
1974
1975/*
1976 * Read the value associated with an attribute from the out-of-line buffer
1977 * that we stored it in.
1978 */
726801ba 1979int
1da177e4
LT
1980xfs_attr_rmtval_get(xfs_da_args_t *args)
1981{
1982 xfs_bmbt_irec_t map[ATTR_RMTVALUE_MAPSIZE];
1983 xfs_mount_t *mp;
1984 xfs_daddr_t dblkno;
a9273ca5 1985 void *dst;
1da177e4
LT
1986 xfs_buf_t *bp;
1987 int nmap, error, tmp, valuelen, blkcnt, i;
1988 xfs_dablk_t lblkno;
1989
1990 ASSERT(!(args->flags & ATTR_KERNOVAL));
1991
1992 mp = args->dp->i_mount;
1993 dst = args->value;
1994 valuelen = args->valuelen;
1995 lblkno = args->rmtblkno;
1996 while (valuelen > 0) {
1997 nmap = ATTR_RMTVALUE_MAPSIZE;
1998 error = xfs_bmapi(args->trans, args->dp, (xfs_fileoff_t)lblkno,
1999 args->rmtblkcnt,
2000 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
3e57ecf6 2001 NULL, 0, map, &nmap, NULL, NULL);
1da177e4
LT
2002 if (error)
2003 return(error);
2004 ASSERT(nmap >= 1);
2005
2006 for (i = 0; (i < nmap) && (valuelen > 0); i++) {
2007 ASSERT((map[i].br_startblock != DELAYSTARTBLOCK) &&
2008 (map[i].br_startblock != HOLESTARTBLOCK));
2009 dblkno = XFS_FSB_TO_DADDR(mp, map[i].br_startblock);
2010 blkcnt = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
2011 error = xfs_read_buf(mp, mp->m_ddev_targp, dblkno,
0cadda1c 2012 blkcnt, XBF_LOCK | XBF_DONT_BLOCK,
ddd3a14e 2013 &bp);
1da177e4
LT
2014 if (error)
2015 return(error);
2016
2017 tmp = (valuelen < XFS_BUF_SIZE(bp))
2018 ? valuelen : XFS_BUF_SIZE(bp);
0cadda1c 2019 xfs_biomove(bp, 0, tmp, dst, XBF_READ);
1da177e4
LT
2020 xfs_buf_relse(bp);
2021 dst += tmp;
2022 valuelen -= tmp;
2023
2024 lblkno += map[i].br_blockcount;
2025 }
2026 }
2027 ASSERT(valuelen == 0);
2028 return(0);
2029}
2030
2031/*
2032 * Write the value associated with an attribute into the out-of-line buffer
2033 * that we have defined for it.
2034 */
2035STATIC int
2036xfs_attr_rmtval_set(xfs_da_args_t *args)
2037{
2038 xfs_mount_t *mp;
2039 xfs_fileoff_t lfileoff;
2040 xfs_inode_t *dp;
2041 xfs_bmbt_irec_t map;
2042 xfs_daddr_t dblkno;
a9273ca5 2043 void *src;
1da177e4
LT
2044 xfs_buf_t *bp;
2045 xfs_dablk_t lblkno;
2046 int blkcnt, valuelen, nmap, error, tmp, committed;
2047
2048 dp = args->dp;
2049 mp = dp->i_mount;
2050 src = args->value;
2051
2052 /*
2053 * Find a "hole" in the attribute address space large enough for
2054 * us to drop the new attribute's value into.
2055 */
2056 blkcnt = XFS_B_TO_FSB(mp, args->valuelen);
2057 lfileoff = 0;
2058 error = xfs_bmap_first_unused(args->trans, args->dp, blkcnt, &lfileoff,
2059 XFS_ATTR_FORK);
2060 if (error) {
2061 return(error);
2062 }
2063 args->rmtblkno = lblkno = (xfs_dablk_t)lfileoff;
2064 args->rmtblkcnt = blkcnt;
2065
2066 /*
2067 * Roll through the "value", allocating blocks on disk as required.
2068 */
2069 while (blkcnt > 0) {
2070 /*
2071 * Allocate a single extent, up to the size of the value.
2072 */
9d87c319 2073 xfs_bmap_init(args->flist, args->firstblock);
1da177e4
LT
2074 nmap = 1;
2075 error = xfs_bmapi(args->trans, dp, (xfs_fileoff_t)lblkno,
2076 blkcnt,
2077 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA |
2078 XFS_BMAPI_WRITE,
2079 args->firstblock, args->total, &map, &nmap,
3e57ecf6 2080 args->flist, NULL);
1da177e4
LT
2081 if (!error) {
2082 error = xfs_bmap_finish(&args->trans, args->flist,
f7c99b6f 2083 &committed);
1da177e4
LT
2084 }
2085 if (error) {
2086 ASSERT(committed);
2087 args->trans = NULL;
2088 xfs_bmap_cancel(args->flist);
2089 return(error);
2090 }
2091
2092 /*
2093 * bmap_finish() may have committed the last trans and started
2094 * a new one. We need the inode to be in all transactions.
2095 */
2096 if (committed) {
2097 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
2098 xfs_trans_ihold(args->trans, dp);
2099 }
2100
2101 ASSERT(nmap == 1);
2102 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
2103 (map.br_startblock != HOLESTARTBLOCK));
2104 lblkno += map.br_blockcount;
2105 blkcnt -= map.br_blockcount;
2106
2107 /*
2108 * Start the next trans in the chain.
2109 */
322ff6b8
NS
2110 error = xfs_trans_roll(&args->trans, dp);
2111 if (error)
1da177e4
LT
2112 return (error);
2113 }
2114
2115 /*
2116 * Roll through the "value", copying the attribute value to the
2117 * already-allocated blocks. Blocks are written synchronously
2118 * so that we can know they are all on disk before we turn off
2119 * the INCOMPLETE flag.
2120 */
2121 lblkno = args->rmtblkno;
2122 valuelen = args->valuelen;
2123 while (valuelen > 0) {
2124 /*
2125 * Try to remember where we decided to put the value.
2126 */
9d87c319 2127 xfs_bmap_init(args->flist, args->firstblock);
1da177e4
LT
2128 nmap = 1;
2129 error = xfs_bmapi(NULL, dp, (xfs_fileoff_t)lblkno,
2130 args->rmtblkcnt,
2131 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
3e57ecf6
OW
2132 args->firstblock, 0, &map, &nmap,
2133 NULL, NULL);
1da177e4
LT
2134 if (error) {
2135 return(error);
2136 }
2137 ASSERT(nmap == 1);
2138 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
2139 (map.br_startblock != HOLESTARTBLOCK));
2140
2141 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
2142 blkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
2143
6ad112bf 2144 bp = xfs_buf_get(mp->m_ddev_targp, dblkno, blkcnt,
0cadda1c 2145 XBF_LOCK | XBF_DONT_BLOCK);
1da177e4
LT
2146 ASSERT(bp);
2147 ASSERT(!XFS_BUF_GETERROR(bp));
2148
2149 tmp = (valuelen < XFS_BUF_SIZE(bp)) ? valuelen :
2150 XFS_BUF_SIZE(bp);
0cadda1c 2151 xfs_biomove(bp, 0, tmp, src, XBF_WRITE);
1da177e4
LT
2152 if (tmp < XFS_BUF_SIZE(bp))
2153 xfs_biozero(bp, tmp, XFS_BUF_SIZE(bp) - tmp);
2154 if ((error = xfs_bwrite(mp, bp))) {/* GROT: NOTE: synchronous write */
2155 return (error);
2156 }
2157 src += tmp;
2158 valuelen -= tmp;
2159
2160 lblkno += map.br_blockcount;
2161 }
2162 ASSERT(valuelen == 0);
2163 return(0);
2164}
2165
2166/*
2167 * Remove the value associated with an attribute by deleting the
2168 * out-of-line buffer that it is stored on.
2169 */
2170STATIC int
2171xfs_attr_rmtval_remove(xfs_da_args_t *args)
2172{
2173 xfs_mount_t *mp;
2174 xfs_bmbt_irec_t map;
2175 xfs_buf_t *bp;
2176 xfs_daddr_t dblkno;
2177 xfs_dablk_t lblkno;
2178 int valuelen, blkcnt, nmap, error, done, committed;
2179
2180 mp = args->dp->i_mount;
2181
2182 /*
2183 * Roll through the "value", invalidating the attribute value's
2184 * blocks.
2185 */
2186 lblkno = args->rmtblkno;
2187 valuelen = args->rmtblkcnt;
2188 while (valuelen > 0) {
2189 /*
2190 * Try to remember where we decided to put the value.
2191 */
9d87c319 2192 xfs_bmap_init(args->flist, args->firstblock);
1da177e4
LT
2193 nmap = 1;
2194 error = xfs_bmapi(NULL, args->dp, (xfs_fileoff_t)lblkno,
2195 args->rmtblkcnt,
2196 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
2197 args->firstblock, 0, &map, &nmap,
3e57ecf6 2198 args->flist, NULL);
1da177e4
LT
2199 if (error) {
2200 return(error);
2201 }
2202 ASSERT(nmap == 1);
2203 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
2204 (map.br_startblock != HOLESTARTBLOCK));
2205
2206 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
2207 blkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
2208
2209 /*
2210 * If the "remote" value is in the cache, remove it.
2211 */
0cadda1c 2212 bp = xfs_incore(mp->m_ddev_targp, dblkno, blkcnt, XBF_TRYLOCK);
1da177e4
LT
2213 if (bp) {
2214 XFS_BUF_STALE(bp);
2215 XFS_BUF_UNDELAYWRITE(bp);
2216 xfs_buf_relse(bp);
2217 bp = NULL;
2218 }
2219
2220 valuelen -= map.br_blockcount;
2221
2222 lblkno += map.br_blockcount;
2223 }
2224
2225 /*
2226 * Keep de-allocating extents until the remote-value region is gone.
2227 */
2228 lblkno = args->rmtblkno;
2229 blkcnt = args->rmtblkcnt;
2230 done = 0;
2231 while (!done) {
9d87c319 2232 xfs_bmap_init(args->flist, args->firstblock);
1da177e4
LT
2233 error = xfs_bunmapi(args->trans, args->dp, lblkno, blkcnt,
2234 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
3e57ecf6
OW
2235 1, args->firstblock, args->flist,
2236 NULL, &done);
1da177e4
LT
2237 if (!error) {
2238 error = xfs_bmap_finish(&args->trans, args->flist,
f7c99b6f 2239 &committed);
1da177e4
LT
2240 }
2241 if (error) {
2242 ASSERT(committed);
2243 args->trans = NULL;
2244 xfs_bmap_cancel(args->flist);
2245 return(error);
2246 }
2247
2248 /*
2249 * bmap_finish() may have committed the last trans and started
2250 * a new one. We need the inode to be in all transactions.
2251 */
2252 if (committed) {
2253 xfs_trans_ijoin(args->trans, args->dp, XFS_ILOCK_EXCL);
2254 xfs_trans_ihold(args->trans, args->dp);
2255 }
2256
2257 /*
2258 * Close out trans and start the next one in the chain.
2259 */
322ff6b8
NS
2260 error = xfs_trans_roll(&args->trans, args->dp);
2261 if (error)
1da177e4
LT
2262 return (error);
2263 }
2264 return(0);
2265}