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