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