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