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