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