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