blob: 4ece1906bd41c0d801f8b66a41efb48bd3bcad6d [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110020#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110022#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110024#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_trans.h"
26#include "xfs_sb.h"
27#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_dir2.h"
29#include "xfs_dmapi.h"
30#include "xfs_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110031#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_dir2_sf.h"
Nathan Scotta844f452005-11-02 14:38:42 +110036#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "xfs_dinode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110039#include "xfs_alloc.h"
40#include "xfs_btree.h"
41#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include "xfs_attr.h"
44#include "xfs_attr_leaf.h"
45#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include "xfs_quota.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include "xfs_trans_space.h"
Nathan Scotta844f452005-11-02 14:38:42 +110048#include "xfs_rw.h"
Christoph Hellwig739bfb22007-08-29 10:58:01 +100049#include "xfs_vnodeops.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51/*
52 * xfs_attr.c
53 *
54 * Provide the external interfaces to manage attribute lists.
55 */
56
57/*========================================================================
58 * Function prototypes for the kernel.
59 *========================================================================*/
60
61/*
62 * Internal routines when attribute list fits inside the inode.
63 */
64STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
65
66/*
67 * Internal routines when attribute list is one block.
68 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100069STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070STATIC int xfs_attr_leaf_addname(xfs_da_args_t *args);
71STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
72STATIC int xfs_attr_leaf_list(xfs_attr_list_context_t *context);
73
74/*
75 * Internal routines when attribute list is more than one block.
76 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100077STATIC int xfs_attr_node_get(xfs_da_args_t *args);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078STATIC int xfs_attr_node_addname(xfs_da_args_t *args);
79STATIC int xfs_attr_node_removename(xfs_da_args_t *args);
80STATIC int xfs_attr_node_list(xfs_attr_list_context_t *context);
81STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
82STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
83
84/*
85 * Routines to manipulate out-of-line attribute values.
86 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070087STATIC int xfs_attr_rmtval_set(xfs_da_args_t *args);
88STATIC int xfs_attr_rmtval_remove(xfs_da_args_t *args);
89
90#define ATTR_RMTVALUE_MAPSIZE 1 /* # of map entries at once */
91
92#if defined(XFS_ATTR_TRACE)
93ktrace_t *xfs_attr_trace_buf;
94#endif
95
Barry Naujoke8b0eba2008-04-22 17:34:31 +100096STATIC int
97xfs_attr_name_to_xname(
98 struct xfs_name *xname,
99 const char *aname)
100{
101 if (!aname)
102 return EINVAL;
103 xname->name = aname;
104 xname->len = strlen(aname);
105 if (xname->len >= MAXNAMELEN)
106 return EFAULT; /* match IRIX behaviour */
107
108 return 0;
109}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +1000111STATIC int
112xfs_inode_hasattr(
113 struct xfs_inode *ip)
114{
115 if (!XFS_IFORK_Q(ip) ||
116 (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
117 ip->i_d.di_anextents == 0))
118 return 0;
119 return 1;
120}
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122/*========================================================================
123 * Overall external interface routines.
124 *========================================================================*/
125
126int
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000127xfs_attr_fetch(xfs_inode_t *ip, struct xfs_name *name,
128 char *value, int *valuelenp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
130 xfs_da_args_t args;
131 int error;
132
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +1000133 if (!xfs_inode_hasattr(ip))
134 return ENOATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 /*
137 * Fill in the arg structure for this request.
138 */
139 memset((char *)&args, 0, sizeof(args));
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000140 args.name = name->name;
141 args.namelen = name->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 args.value = value;
143 args.valuelen = *valuelenp;
144 args.flags = flags;
145 args.hashval = xfs_da_hashname(args.name, args.namelen);
146 args.dp = ip;
147 args.whichfork = XFS_ATTR_FORK;
148
149 /*
150 * Decide on what work routines to call based on the inode size.
151 */
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +1000152 if (ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 error = xfs_attr_shortform_getvalue(&args);
154 } else if (xfs_bmap_one_block(ip, XFS_ATTR_FORK)) {
155 error = xfs_attr_leaf_get(&args);
156 } else {
157 error = xfs_attr_node_get(&args);
158 }
159
160 /*
161 * Return the number of bytes in the value to the caller.
162 */
163 *valuelenp = args.valuelen;
164
165 if (error == EEXIST)
166 error = 0;
167 return(error);
168}
169
170int
Christoph Hellwig993386c2007-08-28 16:12:30 +1000171xfs_attr_get(
172 xfs_inode_t *ip,
173 const char *name,
174 char *value,
175 int *valuelenp,
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000176 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000178 int error;
179 struct xfs_name xname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 XFS_STATS_INC(xs_attr_get);
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
184 return(EIO);
185
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000186 error = xfs_attr_name_to_xname(&xname, name);
187 if (error)
188 return error;
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 xfs_ilock(ip, XFS_ILOCK_SHARED);
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000191 error = xfs_attr_fetch(ip, &xname, value, valuelenp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 xfs_iunlock(ip, XFS_ILOCK_SHARED);
193 return(error);
194}
195
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000196/*
197 * Calculate how many blocks we need for the new attribute,
198 */
199int
200xfs_attr_calc_size(
201 struct xfs_inode *ip,
202 int namelen,
203 int valuelen,
204 int *local)
205{
206 struct xfs_mount *mp = ip->i_mount;
207 int size;
208 int nblks;
209
210 /*
211 * Determine space new attribute will use, and if it would be
212 * "local" or "remote" (note: local != inline).
213 */
214 size = xfs_attr_leaf_newentsize(namelen, valuelen,
215 mp->m_sb.sb_blocksize, local);
216
217 nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
218 if (*local) {
219 if (size > (mp->m_sb.sb_blocksize >> 1)) {
220 /* Double split possible */
221 nblks *= 2;
222 }
223 } else {
224 /*
225 * Out of line attribute, cannot double split, but
226 * make room for the attribute value itself.
227 */
228 uint dblocks = XFS_B_TO_FSB(mp, valuelen);
229 nblks += dblocks;
230 nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
231 }
232
233 return nblks;
234}
235
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000236STATIC int
237xfs_attr_set_int(xfs_inode_t *dp, struct xfs_name *name,
238 char *value, int valuelen, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
240 xfs_da_args_t args;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 xfs_fsblock_t firstblock;
242 xfs_bmap_free_t flist;
243 int error, err2, committed;
Nathan Scottaa82daa2005-11-02 10:33:33 +1100244 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 int rsvd = (flags & ATTR_ROOT) != 0;
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000246 int local;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 /*
249 * Attach the dquots to the inode.
250 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200251 error = xfs_qm_dqattach(dp, 0);
252 if (error)
253 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 /*
256 * If the inode doesn't have an attribute fork, add one.
257 * (inode must not be locked when we call this routine)
258 */
259 if (XFS_IFORK_Q(dp) == 0) {
Barry Naujoke5889e92007-02-10 18:35:58 +1100260 int sf_size = sizeof(xfs_attr_sf_hdr_t) +
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000261 XFS_ATTR_SF_ENTSIZE_BYNAME(name->len, valuelen);
Barry Naujoke5889e92007-02-10 18:35:58 +1100262
263 if ((error = xfs_bmap_add_attrfork(dp, sf_size, rsvd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 return(error);
265 }
266
267 /*
268 * Fill in the arg structure for this request.
269 */
270 memset((char *)&args, 0, sizeof(args));
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000271 args.name = name->name;
272 args.namelen = name->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 args.value = value;
274 args.valuelen = valuelen;
275 args.flags = flags;
276 args.hashval = xfs_da_hashname(args.name, args.namelen);
277 args.dp = dp;
278 args.firstblock = &firstblock;
279 args.flist = &flist;
280 args.whichfork = XFS_ATTR_FORK;
Barry Naujok6a178102008-05-21 16:42:05 +1000281 args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 /* Size is now blocks for attribute data */
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000284 args.total = xfs_attr_calc_size(dp, name->len, valuelen, &local);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 /*
287 * Start our first transaction of the day.
288 *
289 * All future transactions during this code must be "chained" off
290 * this one via the trans_dup() call. All transactions will contain
291 * the inode, and the inode will always be marked with trans_ihold().
292 * Since the inode will be locked in all transactions, we must log
293 * the inode in every transaction to let it float upward through
294 * the log.
295 */
296 args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_SET);
297
298 /*
299 * Root fork attributes can use reserved data blocks for this
300 * operation if necessary
301 */
302
303 if (rsvd)
304 args.trans->t_flags |= XFS_TRANS_RESERVE;
305
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000306 if ((error = xfs_trans_reserve(args.trans, args.total,
307 XFS_ATTRSET_LOG_RES(mp, args.total), 0,
308 XFS_TRANS_PERM_LOG_RES, XFS_ATTRSET_LOG_COUNT))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 xfs_trans_cancel(args.trans, 0);
310 return(error);
311 }
312 xfs_ilock(dp, XFS_ILOCK_EXCL);
313
Christoph Hellwig7d095252009-06-08 15:33:32 +0200314 error = xfs_trans_reserve_quota_nblks(args.trans, dp, args.total, 0,
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000315 rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
316 XFS_QMOPT_RES_REGBLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 if (error) {
318 xfs_iunlock(dp, XFS_ILOCK_EXCL);
319 xfs_trans_cancel(args.trans, XFS_TRANS_RELEASE_LOG_RES);
320 return (error);
321 }
322
323 xfs_trans_ijoin(args.trans, dp, XFS_ILOCK_EXCL);
324 xfs_trans_ihold(args.trans, dp);
325
326 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000327 * If the attribute list is non-existent or a shortform list,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 * upgrade it to a single-leaf-block attribute list.
329 */
330 if ((dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) ||
331 ((dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS) &&
332 (dp->i_d.di_anextents == 0))) {
333
334 /*
335 * Build initial attribute list (if required).
336 */
337 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
Nathan Scottd8cc8902005-11-02 10:34:53 +1100338 xfs_attr_shortform_create(&args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 /*
341 * Try to add the attr to the attribute list in
342 * the inode.
343 */
344 error = xfs_attr_shortform_addname(&args);
345 if (error != ENOSPC) {
346 /*
347 * Commit the shortform mods, and we're done.
348 * NOTE: this is also the error path (EEXIST, etc).
349 */
350 ASSERT(args.trans != NULL);
351
352 /*
353 * If this is a synchronous mount, make sure that
354 * the transaction goes to disk before returning
355 * to the user.
356 */
357 if (mp->m_flags & XFS_MOUNT_WSYNC) {
358 xfs_trans_set_sync(args.trans);
359 }
360 err2 = xfs_trans_commit(args.trans,
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000361 XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 xfs_iunlock(dp, XFS_ILOCK_EXCL);
363
364 /*
365 * Hit the inode change time.
366 */
367 if (!error && (flags & ATTR_KERNOTIME) == 0) {
368 xfs_ichgtime(dp, XFS_ICHGTIME_CHG);
369 }
370 return(error == 0 ? err2 : error);
371 }
372
373 /*
374 * It won't fit in the shortform, transform to a leaf block.
375 * GROT: another possible req'mt for a double-split btree op.
376 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600377 xfs_bmap_init(args.flist, args.firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 error = xfs_attr_shortform_to_leaf(&args);
379 if (!error) {
380 error = xfs_bmap_finish(&args.trans, args.flist,
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100381 &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 }
383 if (error) {
384 ASSERT(committed);
385 args.trans = NULL;
386 xfs_bmap_cancel(&flist);
387 goto out;
388 }
389
390 /*
391 * bmap_finish() may have committed the last trans and started
392 * a new one. We need the inode to be in all transactions.
393 */
394 if (committed) {
395 xfs_trans_ijoin(args.trans, dp, XFS_ILOCK_EXCL);
396 xfs_trans_ihold(args.trans, dp);
397 }
398
399 /*
400 * Commit the leaf transformation. We'll need another (linked)
401 * transaction to add the new attribute to the leaf.
402 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000403
404 error = xfs_trans_roll(&args.trans, dp);
405 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 goto out;
407
408 }
409
410 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
411 error = xfs_attr_leaf_addname(&args);
412 } else {
413 error = xfs_attr_node_addname(&args);
414 }
415 if (error) {
416 goto out;
417 }
418
419 /*
420 * If this is a synchronous mount, make sure that the
421 * transaction goes to disk before returning to the user.
422 */
423 if (mp->m_flags & XFS_MOUNT_WSYNC) {
424 xfs_trans_set_sync(args.trans);
425 }
426
427 /*
428 * Commit the last in the sequence of transactions.
429 */
430 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000431 error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 xfs_iunlock(dp, XFS_ILOCK_EXCL);
433
434 /*
435 * Hit the inode change time.
436 */
437 if (!error && (flags & ATTR_KERNOTIME) == 0) {
438 xfs_ichgtime(dp, XFS_ICHGTIME_CHG);
439 }
440
441 return(error);
442
443out:
444 if (args.trans)
445 xfs_trans_cancel(args.trans,
446 XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
447 xfs_iunlock(dp, XFS_ILOCK_EXCL);
448 return(error);
449}
450
Nathan Scottaa82daa2005-11-02 10:33:33 +1100451int
Christoph Hellwig993386c2007-08-28 16:12:30 +1000452xfs_attr_set(
453 xfs_inode_t *dp,
454 const char *name,
455 char *value,
456 int valuelen,
457 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000459 int error;
460 struct xfs_name xname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Nathan Scottaa82daa2005-11-02 10:33:33 +1100462 XFS_STATS_INC(xs_attr_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Nathan Scottaa82daa2005-11-02 10:33:33 +1100464 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 return (EIO);
466
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000467 error = xfs_attr_name_to_xname(&xname, name);
468 if (error)
469 return error;
470
471 return xfs_attr_set_int(dp, &xname, value, valuelen, flags);
Nathan Scottaa82daa2005-11-02 10:33:33 +1100472}
473
474/*
475 * Generic handler routine to remove a name from an attribute list.
476 * Transitions attribute list from Btree to shortform as necessary.
477 */
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000478STATIC int
479xfs_attr_remove_int(xfs_inode_t *dp, struct xfs_name *name, int flags)
Nathan Scottaa82daa2005-11-02 10:33:33 +1100480{
481 xfs_da_args_t args;
482 xfs_fsblock_t firstblock;
483 xfs_bmap_free_t flist;
484 int error;
485 xfs_mount_t *mp = dp->i_mount;
486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 /*
488 * Fill in the arg structure for this request.
489 */
490 memset((char *)&args, 0, sizeof(args));
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000491 args.name = name->name;
492 args.namelen = name->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 args.flags = flags;
494 args.hashval = xfs_da_hashname(args.name, args.namelen);
495 args.dp = dp;
496 args.firstblock = &firstblock;
497 args.flist = &flist;
498 args.total = 0;
499 args.whichfork = XFS_ATTR_FORK;
500
501 /*
502 * Attach the dquots to the inode.
503 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200504 error = xfs_qm_dqattach(dp, 0);
505 if (error)
506 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508 /*
509 * Start our first transaction of the day.
510 *
511 * All future transactions during this code must be "chained" off
512 * this one via the trans_dup() call. All transactions will contain
513 * the inode, and the inode will always be marked with trans_ihold().
514 * Since the inode will be locked in all transactions, we must log
515 * the inode in every transaction to let it float upward through
516 * the log.
517 */
518 args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_RM);
519
520 /*
521 * Root fork attributes can use reserved data blocks for this
522 * operation if necessary
523 */
524
525 if (flags & ATTR_ROOT)
526 args.trans->t_flags |= XFS_TRANS_RESERVE;
527
528 if ((error = xfs_trans_reserve(args.trans,
529 XFS_ATTRRM_SPACE_RES(mp),
530 XFS_ATTRRM_LOG_RES(mp),
531 0, XFS_TRANS_PERM_LOG_RES,
532 XFS_ATTRRM_LOG_COUNT))) {
533 xfs_trans_cancel(args.trans, 0);
534 return(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 }
536
537 xfs_ilock(dp, XFS_ILOCK_EXCL);
538 /*
539 * No need to make quota reservations here. We expect to release some
540 * blocks not allocate in the common case.
541 */
542 xfs_trans_ijoin(args.trans, dp, XFS_ILOCK_EXCL);
543 xfs_trans_ihold(args.trans, dp);
544
545 /*
546 * Decide on what work routines to call based on the inode size.
547 */
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +1000548 if (!xfs_inode_hasattr(dp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 error = XFS_ERROR(ENOATTR);
550 goto out;
551 }
552 if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
553 ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
554 error = xfs_attr_shortform_remove(&args);
555 if (error) {
556 goto out;
557 }
558 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
559 error = xfs_attr_leaf_removename(&args);
560 } else {
561 error = xfs_attr_node_removename(&args);
562 }
563 if (error) {
564 goto out;
565 }
566
567 /*
568 * If this is a synchronous mount, make sure that the
569 * transaction goes to disk before returning to the user.
570 */
571 if (mp->m_flags & XFS_MOUNT_WSYNC) {
572 xfs_trans_set_sync(args.trans);
573 }
574
575 /*
576 * Commit the last in the sequence of transactions.
577 */
578 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000579 error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 xfs_iunlock(dp, XFS_ILOCK_EXCL);
581
582 /*
583 * Hit the inode change time.
584 */
585 if (!error && (flags & ATTR_KERNOTIME) == 0) {
586 xfs_ichgtime(dp, XFS_ICHGTIME_CHG);
587 }
588
589 return(error);
590
591out:
592 if (args.trans)
593 xfs_trans_cancel(args.trans,
594 XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
595 xfs_iunlock(dp, XFS_ILOCK_EXCL);
596 return(error);
597}
598
Nathan Scottaa82daa2005-11-02 10:33:33 +1100599int
Christoph Hellwig993386c2007-08-28 16:12:30 +1000600xfs_attr_remove(
601 xfs_inode_t *dp,
602 const char *name,
603 int flags)
Nathan Scottaa82daa2005-11-02 10:33:33 +1100604{
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000605 int error;
606 struct xfs_name xname;
Nathan Scottaa82daa2005-11-02 10:33:33 +1100607
608 XFS_STATS_INC(xs_attr_remove);
609
Nathan Scottaa82daa2005-11-02 10:33:33 +1100610 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
611 return (EIO);
612
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000613 error = xfs_attr_name_to_xname(&xname, name);
614 if (error)
615 return error;
616
Nathan Scottaa82daa2005-11-02 10:33:33 +1100617 xfs_ilock(dp, XFS_ILOCK_SHARED);
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +1000618 if (!xfs_inode_hasattr(dp)) {
Nathan Scottaa82daa2005-11-02 10:33:33 +1100619 xfs_iunlock(dp, XFS_ILOCK_SHARED);
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +1000620 return XFS_ERROR(ENOATTR);
Nathan Scottaa82daa2005-11-02 10:33:33 +1100621 }
622 xfs_iunlock(dp, XFS_ILOCK_SHARED);
623
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000624 return xfs_attr_remove_int(dp, &xname, flags);
Nathan Scottaa82daa2005-11-02 10:33:33 +1100625}
626
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000627int
Tim Shimmin726801b2006-09-28 11:01:37 +1000628xfs_attr_list_int(xfs_attr_list_context_t *context)
629{
630 int error;
631 xfs_inode_t *dp = context->dp;
632
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000633 XFS_STATS_INC(xs_attr_list);
634
635 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
636 return EIO;
637
638 xfs_ilock(dp, XFS_ILOCK_SHARED);
639 xfs_attr_trace_l_c("syscall start", context);
640
Tim Shimmin726801b2006-09-28 11:01:37 +1000641 /*
642 * Decide on what work routines to call based on the inode size.
643 */
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +1000644 if (!xfs_inode_hasattr(dp)) {
Tim Shimmin726801b2006-09-28 11:01:37 +1000645 error = 0;
646 } else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
647 error = xfs_attr_shortform_list(context);
648 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
649 error = xfs_attr_leaf_list(context);
650 } else {
651 error = xfs_attr_node_list(context);
652 }
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000653
654 xfs_iunlock(dp, XFS_ILOCK_SHARED);
655 xfs_attr_trace_l_c("syscall end", context);
656
Tim Shimmin726801b2006-09-28 11:01:37 +1000657 return error;
658}
659
660#define ATTR_ENTBASESIZE /* minimum bytes used by an attr */ \
661 (((struct attrlist_ent *) 0)->a_name - (char *) 0)
662#define ATTR_ENTSIZE(namelen) /* actual bytes used by an attr */ \
663 ((ATTR_ENTBASESIZE + (namelen) + 1 + sizeof(u_int32_t)-1) \
664 & ~(sizeof(u_int32_t)-1))
665
666/*
667 * Format an attribute and copy it out to the user's buffer.
668 * Take care to check values and protect against them changing later,
669 * we may be reading them directly out of a user buffer.
670 */
671/*ARGSUSED*/
672STATIC int
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000673xfs_attr_put_listent(xfs_attr_list_context_t *context, int flags,
Tim Shimmin726801b2006-09-28 11:01:37 +1000674 char *name, int namelen,
675 int valuelen, char *value)
676{
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000677 struct attrlist *alist = (struct attrlist *)context->alist;
Tim Shimmin726801b2006-09-28 11:01:37 +1000678 attrlist_ent_t *aep;
679 int arraytop;
680
681 ASSERT(!(context->flags & ATTR_KERNOVAL));
682 ASSERT(context->count >= 0);
683 ASSERT(context->count < (ATTR_MAX_VALUELEN/8));
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000684 ASSERT(context->firstu >= sizeof(*alist));
Tim Shimmin726801b2006-09-28 11:01:37 +1000685 ASSERT(context->firstu <= context->bufsize);
686
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000687 /*
688 * Only list entries in the right namespace.
689 */
690 if (((context->flags & ATTR_SECURE) == 0) !=
691 ((flags & XFS_ATTR_SECURE) == 0))
692 return 0;
693 if (((context->flags & ATTR_ROOT) == 0) !=
694 ((flags & XFS_ATTR_ROOT) == 0))
695 return 0;
696
697 arraytop = sizeof(*alist) +
698 context->count * sizeof(alist->al_offset[0]);
Tim Shimmin726801b2006-09-28 11:01:37 +1000699 context->firstu -= ATTR_ENTSIZE(namelen);
700 if (context->firstu < arraytop) {
701 xfs_attr_trace_l_c("buffer full", context);
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000702 alist->al_more = 1;
Tim Shimmin726801b2006-09-28 11:01:37 +1000703 context->seen_enough = 1;
704 return 1;
705 }
706
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000707 aep = (attrlist_ent_t *)&context->alist[context->firstu];
Tim Shimmin726801b2006-09-28 11:01:37 +1000708 aep->a_valuelen = valuelen;
709 memcpy(aep->a_name, name, namelen);
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000710 aep->a_name[namelen] = 0;
711 alist->al_offset[context->count++] = context->firstu;
712 alist->al_count = context->count;
Tim Shimmin726801b2006-09-28 11:01:37 +1000713 xfs_attr_trace_l_c("add", context);
714 return 0;
715}
716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717/*
718 * Generate a list of extended attribute names and optionally
719 * also value lengths. Positive return value follows the XFS
720 * convention of being an error, zero or negative return code
721 * is the length of the buffer returned (negated), indicating
722 * success.
723 */
724int
Christoph Hellwig993386c2007-08-28 16:12:30 +1000725xfs_attr_list(
726 xfs_inode_t *dp,
727 char *buffer,
728 int bufsize,
729 int flags,
730 attrlist_cursor_kern_t *cursor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
732 xfs_attr_list_context_t context;
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000733 struct attrlist *alist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 int error;
735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 /*
737 * Validate the cursor.
738 */
739 if (cursor->pad1 || cursor->pad2)
740 return(XFS_ERROR(EINVAL));
741 if ((cursor->initted == 0) &&
742 (cursor->hashval || cursor->blkno || cursor->offset))
Tim Shimmin726801b2006-09-28 11:01:37 +1000743 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
745 /*
746 * Check for a properly aligned buffer.
747 */
748 if (((long)buffer) & (sizeof(int)-1))
Tim Shimmin726801b2006-09-28 11:01:37 +1000749 return XFS_ERROR(EFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 if (flags & ATTR_KERNOVAL)
751 bufsize = 0;
752
753 /*
754 * Initialize the output buffer.
755 */
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000756 memset(&context, 0, sizeof(context));
Christoph Hellwig993386c2007-08-28 16:12:30 +1000757 context.dp = dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 context.cursor = cursor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 context.resynch = 1;
760 context.flags = flags;
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000761 context.alist = buffer;
762 context.bufsize = (bufsize & ~(sizeof(int)-1)); /* align */
763 context.firstu = context.bufsize;
764 context.put_listent = xfs_attr_put_listent;
Tim Shimmin726801b2006-09-28 11:01:37 +1000765
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000766 alist = (struct attrlist *)context.alist;
767 alist->al_count = 0;
768 alist->al_more = 0;
769 alist->al_offset[0] = context.bufsize;
Tim Shimmin726801b2006-09-28 11:01:37 +1000770
771 error = xfs_attr_list_int(&context);
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000772 ASSERT(error >= 0);
Tim Shimmin726801b2006-09-28 11:01:37 +1000773 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774}
775
776int /* error */
777xfs_attr_inactive(xfs_inode_t *dp)
778{
779 xfs_trans_t *trans;
780 xfs_mount_t *mp;
781 int error;
782
783 mp = dp->i_mount;
784 ASSERT(! XFS_NOT_DQATTACHED(mp, dp));
785
786 xfs_ilock(dp, XFS_ILOCK_SHARED);
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +1000787 if (!xfs_inode_hasattr(dp) ||
788 dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 xfs_iunlock(dp, XFS_ILOCK_SHARED);
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +1000790 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 }
792 xfs_iunlock(dp, XFS_ILOCK_SHARED);
793
794 /*
795 * Start our first transaction of the day.
796 *
797 * All future transactions during this code must be "chained" off
798 * this one via the trans_dup() call. All transactions will contain
799 * the inode, and the inode will always be marked with trans_ihold().
800 * Since the inode will be locked in all transactions, we must log
801 * the inode in every transaction to let it float upward through
802 * the log.
803 */
804 trans = xfs_trans_alloc(mp, XFS_TRANS_ATTRINVAL);
805 if ((error = xfs_trans_reserve(trans, 0, XFS_ATTRINVAL_LOG_RES(mp), 0,
806 XFS_TRANS_PERM_LOG_RES,
807 XFS_ATTRINVAL_LOG_COUNT))) {
808 xfs_trans_cancel(trans, 0);
809 return(error);
810 }
811 xfs_ilock(dp, XFS_ILOCK_EXCL);
812
813 /*
814 * No need to make quota reservations here. We expect to release some
815 * blocks, not allocate, in the common case.
816 */
817 xfs_trans_ijoin(trans, dp, XFS_ILOCK_EXCL);
818 xfs_trans_ihold(trans, dp);
819
820 /*
821 * Decide on what work routines to call based on the inode size.
822 */
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +1000823 if (!xfs_inode_hasattr(dp) ||
824 dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 error = 0;
826 goto out;
827 }
828 error = xfs_attr_root_inactive(&trans, dp);
829 if (error)
830 goto out;
831 /*
832 * signal synchronous inactive transactions unless this
833 * is a synchronous mount filesystem in which case we
834 * know that we're here because we've been called out of
835 * xfs_inactive which means that the last reference is gone
836 * and the unlink transaction has already hit the disk so
837 * async inactive transactions are safe.
838 */
839 if ((error = xfs_itruncate_finish(&trans, dp, 0LL, XFS_ATTR_FORK,
840 (!(mp->m_flags & XFS_MOUNT_WSYNC)
841 ? 1 : 0))))
842 goto out;
843
844 /*
845 * Commit the last in the sequence of transactions.
846 */
847 xfs_trans_log_inode(trans, dp, XFS_ILOG_CORE);
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000848 error = xfs_trans_commit(trans, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 xfs_iunlock(dp, XFS_ILOCK_EXCL);
850
851 return(error);
852
853out:
854 xfs_trans_cancel(trans, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
855 xfs_iunlock(dp, XFS_ILOCK_EXCL);
856 return(error);
857}
858
859
860
861/*========================================================================
862 * External routines when attribute list is inside the inode
863 *========================================================================*/
864
865/*
866 * Add a name to the shortform attribute list structure
867 * This is the external routine.
868 */
869STATIC int
870xfs_attr_shortform_addname(xfs_da_args_t *args)
871{
Nathan Scottd8cc8902005-11-02 10:34:53 +1100872 int newsize, forkoff, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
874 retval = xfs_attr_shortform_lookup(args);
875 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
876 return(retval);
877 } else if (retval == EEXIST) {
878 if (args->flags & ATTR_CREATE)
879 return(retval);
880 retval = xfs_attr_shortform_remove(args);
881 ASSERT(retval == 0);
882 }
883
Nathan Scottd8cc8902005-11-02 10:34:53 +1100884 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
885 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
886 return(XFS_ERROR(ENOSPC));
887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
889 newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100890
891 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
892 if (!forkoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 return(XFS_ERROR(ENOSPC));
Nathan Scottd8cc8902005-11-02 10:34:53 +1100894
895 xfs_attr_shortform_add(args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 return(0);
897}
898
899
900/*========================================================================
901 * External routines when attribute list is one block
902 *========================================================================*/
903
904/*
905 * Add a name to the leaf attribute list structure
906 *
907 * This leaf block cannot have a "remote" value, we only call this routine
908 * if bmap_one_block() says there is only one block (ie: no remote blks).
909 */
David Chinnera8272ce2007-11-23 16:28:09 +1100910STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911xfs_attr_leaf_addname(xfs_da_args_t *args)
912{
913 xfs_inode_t *dp;
914 xfs_dabuf_t *bp;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100915 int retval, error, committed, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
917 /*
918 * Read the (only) block in the attribute list in.
919 */
920 dp = args->dp;
921 args->blkno = 0;
922 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
923 XFS_ATTR_FORK);
924 if (error)
925 return(error);
926 ASSERT(bp != NULL);
927
928 /*
929 * Look up the given attribute in the leaf block. Figure out if
930 * the given flags produce an error or call for an atomic rename.
931 */
932 retval = xfs_attr_leaf_lookup_int(bp, args);
933 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
934 xfs_da_brelse(args->trans, bp);
935 return(retval);
936 } else if (retval == EEXIST) {
937 if (args->flags & ATTR_CREATE) { /* pure create op */
938 xfs_da_brelse(args->trans, bp);
939 return(retval);
940 }
Barry Naujok6a178102008-05-21 16:42:05 +1000941 args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 args->blkno2 = args->blkno; /* set 2nd entry info*/
943 args->index2 = args->index;
944 args->rmtblkno2 = args->rmtblkno;
945 args->rmtblkcnt2 = args->rmtblkcnt;
946 }
947
948 /*
949 * Add the attribute to the leaf block, transitioning to a Btree
950 * if required.
951 */
952 retval = xfs_attr_leaf_add(bp, args);
953 xfs_da_buf_done(bp);
954 if (retval == ENOSPC) {
955 /*
956 * Promote the attribute list to the Btree format, then
957 * Commit that transaction so that the node_addname() call
958 * can manage its own transactions.
959 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600960 xfs_bmap_init(args->flist, args->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 error = xfs_attr_leaf_to_node(args);
962 if (!error) {
963 error = xfs_bmap_finish(&args->trans, args->flist,
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100964 &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 }
966 if (error) {
967 ASSERT(committed);
968 args->trans = NULL;
969 xfs_bmap_cancel(args->flist);
970 return(error);
971 }
972
973 /*
974 * bmap_finish() may have committed the last trans and started
975 * a new one. We need the inode to be in all transactions.
976 */
977 if (committed) {
978 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
979 xfs_trans_ihold(args->trans, dp);
980 }
981
982 /*
983 * Commit the current trans (including the inode) and start
984 * a new one.
985 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000986 error = xfs_trans_roll(&args->trans, dp);
987 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 return (error);
989
990 /*
991 * Fob the whole rest of the problem off on the Btree code.
992 */
993 error = xfs_attr_node_addname(args);
994 return(error);
995 }
996
997 /*
998 * Commit the transaction that added the attr name so that
999 * later routines can manage their own transactions.
1000 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10001001 error = xfs_trans_roll(&args->trans, dp);
1002 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 return (error);
1004
1005 /*
1006 * If there was an out-of-line value, allocate the blocks we
1007 * identified for its storage and copy the value. This is done
1008 * after we create the attribute so that we don't overflow the
1009 * maximum size of a transaction and/or hit a deadlock.
1010 */
1011 if (args->rmtblkno > 0) {
1012 error = xfs_attr_rmtval_set(args);
1013 if (error)
1014 return(error);
1015 }
1016
1017 /*
1018 * If this is an atomic rename operation, we must "flip" the
1019 * incomplete flags on the "new" and "old" attribute/value pairs
1020 * so that one disappears and one appears atomically. Then we
1021 * must remove the "old" attribute/value pair.
1022 */
Barry Naujok6a178102008-05-21 16:42:05 +10001023 if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 /*
1025 * In a separate transaction, set the incomplete flag on the
1026 * "old" attr and clear the incomplete flag on the "new" attr.
1027 */
1028 error = xfs_attr_leaf_flipflags(args);
1029 if (error)
1030 return(error);
1031
1032 /*
1033 * Dismantle the "old" attribute/value pair by removing
1034 * a "remote" value (if it exists).
1035 */
1036 args->index = args->index2;
1037 args->blkno = args->blkno2;
1038 args->rmtblkno = args->rmtblkno2;
1039 args->rmtblkcnt = args->rmtblkcnt2;
1040 if (args->rmtblkno) {
1041 error = xfs_attr_rmtval_remove(args);
1042 if (error)
1043 return(error);
1044 }
1045
1046 /*
1047 * Read in the block containing the "old" attr, then
1048 * remove the "old" attr from that block (neat, huh!)
1049 */
1050 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1,
1051 &bp, XFS_ATTR_FORK);
1052 if (error)
1053 return(error);
1054 ASSERT(bp != NULL);
1055 (void)xfs_attr_leaf_remove(bp, args);
1056
1057 /*
1058 * If the result is small enough, shrink it all into the inode.
1059 */
Nathan Scottd8cc8902005-11-02 10:34:53 +11001060 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001061 xfs_bmap_init(args->flist, args->firstblock);
Nathan Scottd8cc8902005-11-02 10:34:53 +11001062 error = xfs_attr_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 /* bp is gone due to xfs_da_shrink_inode */
1064 if (!error) {
1065 error = xfs_bmap_finish(&args->trans,
1066 args->flist,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 &committed);
1068 }
1069 if (error) {
1070 ASSERT(committed);
1071 args->trans = NULL;
1072 xfs_bmap_cancel(args->flist);
1073 return(error);
1074 }
1075
1076 /*
1077 * bmap_finish() may have committed the last trans
1078 * and started a new one. We need the inode to be
1079 * in all transactions.
1080 */
1081 if (committed) {
1082 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1083 xfs_trans_ihold(args->trans, dp);
1084 }
1085 } else
1086 xfs_da_buf_done(bp);
1087
1088 /*
1089 * Commit the remove and start the next trans in series.
1090 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10001091 error = xfs_trans_roll(&args->trans, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
1093 } else if (args->rmtblkno > 0) {
1094 /*
1095 * Added a "remote" value, just clear the incomplete flag.
1096 */
1097 error = xfs_attr_leaf_clearflag(args);
1098 }
1099 return(error);
1100}
1101
1102/*
1103 * Remove a name from the leaf attribute list structure
1104 *
1105 * This leaf block cannot have a "remote" value, we only call this routine
1106 * if bmap_one_block() says there is only one block (ie: no remote blks).
1107 */
1108STATIC int
1109xfs_attr_leaf_removename(xfs_da_args_t *args)
1110{
1111 xfs_inode_t *dp;
1112 xfs_dabuf_t *bp;
Nathan Scottd8cc8902005-11-02 10:34:53 +11001113 int error, committed, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
1115 /*
1116 * Remove the attribute.
1117 */
1118 dp = args->dp;
1119 args->blkno = 0;
1120 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
1121 XFS_ATTR_FORK);
1122 if (error) {
1123 return(error);
1124 }
1125
1126 ASSERT(bp != NULL);
1127 error = xfs_attr_leaf_lookup_int(bp, args);
1128 if (error == ENOATTR) {
1129 xfs_da_brelse(args->trans, bp);
1130 return(error);
1131 }
1132
1133 (void)xfs_attr_leaf_remove(bp, args);
1134
1135 /*
1136 * If the result is small enough, shrink it all into the inode.
1137 */
Nathan Scottd8cc8902005-11-02 10:34:53 +11001138 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001139 xfs_bmap_init(args->flist, args->firstblock);
Nathan Scottd8cc8902005-11-02 10:34:53 +11001140 error = xfs_attr_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 /* bp is gone due to xfs_da_shrink_inode */
1142 if (!error) {
1143 error = xfs_bmap_finish(&args->trans, args->flist,
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001144 &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 }
1146 if (error) {
1147 ASSERT(committed);
1148 args->trans = NULL;
1149 xfs_bmap_cancel(args->flist);
1150 return(error);
1151 }
1152
1153 /*
1154 * bmap_finish() may have committed the last trans and started
1155 * a new one. We need the inode to be in all transactions.
1156 */
1157 if (committed) {
1158 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1159 xfs_trans_ihold(args->trans, dp);
1160 }
1161 } else
1162 xfs_da_buf_done(bp);
1163 return(0);
1164}
1165
1166/*
1167 * Look up a name in a leaf attribute list structure.
1168 *
1169 * This leaf block cannot have a "remote" value, we only call this routine
1170 * if bmap_one_block() says there is only one block (ie: no remote blks).
1171 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001172STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173xfs_attr_leaf_get(xfs_da_args_t *args)
1174{
1175 xfs_dabuf_t *bp;
1176 int error;
1177
1178 args->blkno = 0;
1179 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
1180 XFS_ATTR_FORK);
1181 if (error)
1182 return(error);
1183 ASSERT(bp != NULL);
1184
1185 error = xfs_attr_leaf_lookup_int(bp, args);
1186 if (error != EEXIST) {
1187 xfs_da_brelse(args->trans, bp);
1188 return(error);
1189 }
1190 error = xfs_attr_leaf_getvalue(bp, args);
1191 xfs_da_brelse(args->trans, bp);
1192 if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
1193 error = xfs_attr_rmtval_get(args);
1194 }
1195 return(error);
1196}
1197
1198/*
1199 * Copy out attribute entries for attr_list(), for leaf attribute lists.
1200 */
1201STATIC int
1202xfs_attr_leaf_list(xfs_attr_list_context_t *context)
1203{
1204 xfs_attr_leafblock_t *leaf;
1205 int error;
1206 xfs_dabuf_t *bp;
1207
1208 context->cursor->blkno = 0;
1209 error = xfs_da_read_buf(NULL, context->dp, 0, -1, &bp, XFS_ATTR_FORK);
1210 if (error)
Tim Shimmin726801b2006-09-28 11:01:37 +10001211 return XFS_ERROR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 ASSERT(bp != NULL);
1213 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001214 if (unlikely(be16_to_cpu(leaf->hdr.info.magic) != XFS_ATTR_LEAF_MAGIC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 XFS_CORRUPTION_ERROR("xfs_attr_leaf_list", XFS_ERRLEVEL_LOW,
1216 context->dp->i_mount, leaf);
1217 xfs_da_brelse(NULL, bp);
Tim Shimmin726801b2006-09-28 11:01:37 +10001218 return XFS_ERROR(EFSCORRUPTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 }
1220
Tim Shimmin726801b2006-09-28 11:01:37 +10001221 error = xfs_attr_leaf_list_int(bp, context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 xfs_da_brelse(NULL, bp);
Tim Shimmin726801b2006-09-28 11:01:37 +10001223 return XFS_ERROR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224}
1225
1226
1227/*========================================================================
1228 * External routines when attribute list size > XFS_LBSIZE(mp).
1229 *========================================================================*/
1230
1231/*
1232 * Add a name to a Btree-format attribute list.
1233 *
1234 * This will involve walking down the Btree, and may involve splitting
1235 * leaf nodes and even splitting intermediate nodes up to and including
1236 * the root node (a special case of an intermediate node).
1237 *
1238 * "Remote" attribute values confuse the issue and atomic rename operations
1239 * add a whole extra layer of confusion on top of that.
1240 */
1241STATIC int
1242xfs_attr_node_addname(xfs_da_args_t *args)
1243{
1244 xfs_da_state_t *state;
1245 xfs_da_state_blk_t *blk;
1246 xfs_inode_t *dp;
1247 xfs_mount_t *mp;
1248 int committed, retval, error;
1249
1250 /*
1251 * Fill in bucket of arguments/results/context to carry around.
1252 */
1253 dp = args->dp;
1254 mp = dp->i_mount;
1255restart:
1256 state = xfs_da_state_alloc();
1257 state->args = args;
1258 state->mp = mp;
1259 state->blocksize = state->mp->m_sb.sb_blocksize;
1260 state->node_ents = state->mp->m_attr_node_ents;
1261
1262 /*
1263 * Search to see if name already exists, and get back a pointer
1264 * to where it should go.
1265 */
1266 error = xfs_da_node_lookup_int(state, &retval);
1267 if (error)
1268 goto out;
1269 blk = &state->path.blk[ state->path.active-1 ];
1270 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1271 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
1272 goto out;
1273 } else if (retval == EEXIST) {
1274 if (args->flags & ATTR_CREATE)
1275 goto out;
Barry Naujok6a178102008-05-21 16:42:05 +10001276 args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 args->blkno2 = args->blkno; /* set 2nd entry info*/
1278 args->index2 = args->index;
1279 args->rmtblkno2 = args->rmtblkno;
1280 args->rmtblkcnt2 = args->rmtblkcnt;
1281 args->rmtblkno = 0;
1282 args->rmtblkcnt = 0;
1283 }
1284
1285 retval = xfs_attr_leaf_add(blk->bp, state->args);
1286 if (retval == ENOSPC) {
1287 if (state->path.active == 1) {
1288 /*
1289 * Its really a single leaf node, but it had
1290 * out-of-line values so it looked like it *might*
1291 * have been a b-tree.
1292 */
1293 xfs_da_state_free(state);
Eric Sandeen9d87c312009-01-14 23:22:07 -06001294 xfs_bmap_init(args->flist, args->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 error = xfs_attr_leaf_to_node(args);
1296 if (!error) {
1297 error = xfs_bmap_finish(&args->trans,
1298 args->flist,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 &committed);
1300 }
1301 if (error) {
1302 ASSERT(committed);
1303 args->trans = NULL;
1304 xfs_bmap_cancel(args->flist);
1305 goto out;
1306 }
1307
1308 /*
1309 * bmap_finish() may have committed the last trans
1310 * and started a new one. We need the inode to be
1311 * in all transactions.
1312 */
1313 if (committed) {
1314 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1315 xfs_trans_ihold(args->trans, dp);
1316 }
1317
1318 /*
1319 * Commit the node conversion and start the next
1320 * trans in the chain.
1321 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10001322 error = xfs_trans_roll(&args->trans, dp);
1323 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 goto out;
1325
1326 goto restart;
1327 }
1328
1329 /*
1330 * Split as many Btree elements as required.
1331 * This code tracks the new and old attr's location
1332 * in the index/blkno/rmtblkno/rmtblkcnt fields and
1333 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
1334 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06001335 xfs_bmap_init(args->flist, args->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 error = xfs_da_split(state);
1337 if (!error) {
1338 error = xfs_bmap_finish(&args->trans, args->flist,
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001339 &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 }
1341 if (error) {
1342 ASSERT(committed);
1343 args->trans = NULL;
1344 xfs_bmap_cancel(args->flist);
1345 goto out;
1346 }
1347
1348 /*
1349 * bmap_finish() may have committed the last trans and started
1350 * a new one. We need the inode to be in all transactions.
1351 */
1352 if (committed) {
1353 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1354 xfs_trans_ihold(args->trans, dp);
1355 }
1356 } else {
1357 /*
1358 * Addition succeeded, update Btree hashvals.
1359 */
1360 xfs_da_fixhashpath(state, &state->path);
1361 }
1362
1363 /*
1364 * Kill the state structure, we're done with it and need to
1365 * allow the buffers to come back later.
1366 */
1367 xfs_da_state_free(state);
1368 state = NULL;
1369
1370 /*
1371 * Commit the leaf addition or btree split and start the next
1372 * trans in the chain.
1373 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10001374 error = xfs_trans_roll(&args->trans, dp);
1375 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 goto out;
1377
1378 /*
1379 * If there was an out-of-line value, allocate the blocks we
1380 * identified for its storage and copy the value. This is done
1381 * after we create the attribute so that we don't overflow the
1382 * maximum size of a transaction and/or hit a deadlock.
1383 */
1384 if (args->rmtblkno > 0) {
1385 error = xfs_attr_rmtval_set(args);
1386 if (error)
1387 return(error);
1388 }
1389
1390 /*
1391 * If this is an atomic rename operation, we must "flip" the
1392 * incomplete flags on the "new" and "old" attribute/value pairs
1393 * so that one disappears and one appears atomically. Then we
1394 * must remove the "old" attribute/value pair.
1395 */
Barry Naujok6a178102008-05-21 16:42:05 +10001396 if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 /*
1398 * In a separate transaction, set the incomplete flag on the
1399 * "old" attr and clear the incomplete flag on the "new" attr.
1400 */
1401 error = xfs_attr_leaf_flipflags(args);
1402 if (error)
1403 goto out;
1404
1405 /*
1406 * Dismantle the "old" attribute/value pair by removing
1407 * a "remote" value (if it exists).
1408 */
1409 args->index = args->index2;
1410 args->blkno = args->blkno2;
1411 args->rmtblkno = args->rmtblkno2;
1412 args->rmtblkcnt = args->rmtblkcnt2;
1413 if (args->rmtblkno) {
1414 error = xfs_attr_rmtval_remove(args);
1415 if (error)
1416 return(error);
1417 }
1418
1419 /*
1420 * Re-find the "old" attribute entry after any split ops.
1421 * The INCOMPLETE flag means that we will find the "old"
1422 * attr, not the "new" one.
1423 */
1424 args->flags |= XFS_ATTR_INCOMPLETE;
1425 state = xfs_da_state_alloc();
1426 state->args = args;
1427 state->mp = mp;
1428 state->blocksize = state->mp->m_sb.sb_blocksize;
1429 state->node_ents = state->mp->m_attr_node_ents;
1430 state->inleaf = 0;
1431 error = xfs_da_node_lookup_int(state, &retval);
1432 if (error)
1433 goto out;
1434
1435 /*
1436 * Remove the name and update the hashvals in the tree.
1437 */
1438 blk = &state->path.blk[ state->path.active-1 ];
1439 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1440 error = xfs_attr_leaf_remove(blk->bp, args);
1441 xfs_da_fixhashpath(state, &state->path);
1442
1443 /*
1444 * Check to see if the tree needs to be collapsed.
1445 */
1446 if (retval && (state->path.active > 1)) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001447 xfs_bmap_init(args->flist, args->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 error = xfs_da_join(state);
1449 if (!error) {
1450 error = xfs_bmap_finish(&args->trans,
1451 args->flist,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 &committed);
1453 }
1454 if (error) {
1455 ASSERT(committed);
1456 args->trans = NULL;
1457 xfs_bmap_cancel(args->flist);
1458 goto out;
1459 }
1460
1461 /*
1462 * bmap_finish() may have committed the last trans
1463 * and started a new one. We need the inode to be
1464 * in all transactions.
1465 */
1466 if (committed) {
1467 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1468 xfs_trans_ihold(args->trans, dp);
1469 }
1470 }
1471
1472 /*
1473 * Commit and start the next trans in the chain.
1474 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10001475 error = xfs_trans_roll(&args->trans, dp);
1476 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 goto out;
1478
1479 } else if (args->rmtblkno > 0) {
1480 /*
1481 * Added a "remote" value, just clear the incomplete flag.
1482 */
1483 error = xfs_attr_leaf_clearflag(args);
1484 if (error)
1485 goto out;
1486 }
1487 retval = error = 0;
1488
1489out:
1490 if (state)
1491 xfs_da_state_free(state);
1492 if (error)
1493 return(error);
1494 return(retval);
1495}
1496
1497/*
1498 * Remove a name from a B-tree attribute list.
1499 *
1500 * This will involve walking down the Btree, and may involve joining
1501 * leaf nodes and even joining intermediate nodes up to and including
1502 * the root node (a special case of an intermediate node).
1503 */
1504STATIC int
1505xfs_attr_node_removename(xfs_da_args_t *args)
1506{
1507 xfs_da_state_t *state;
1508 xfs_da_state_blk_t *blk;
1509 xfs_inode_t *dp;
1510 xfs_dabuf_t *bp;
Nathan Scottd8cc8902005-11-02 10:34:53 +11001511 int retval, error, committed, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
1513 /*
1514 * Tie a string around our finger to remind us where we are.
1515 */
1516 dp = args->dp;
1517 state = xfs_da_state_alloc();
1518 state->args = args;
1519 state->mp = dp->i_mount;
1520 state->blocksize = state->mp->m_sb.sb_blocksize;
1521 state->node_ents = state->mp->m_attr_node_ents;
1522
1523 /*
1524 * Search to see if name exists, and get back a pointer to it.
1525 */
1526 error = xfs_da_node_lookup_int(state, &retval);
1527 if (error || (retval != EEXIST)) {
1528 if (error == 0)
1529 error = retval;
1530 goto out;
1531 }
1532
1533 /*
1534 * If there is an out-of-line value, de-allocate the blocks.
1535 * This is done before we remove the attribute so that we don't
1536 * overflow the maximum size of a transaction and/or hit a deadlock.
1537 */
1538 blk = &state->path.blk[ state->path.active-1 ];
1539 ASSERT(blk->bp != NULL);
1540 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1541 if (args->rmtblkno > 0) {
1542 /*
1543 * Fill in disk block numbers in the state structure
1544 * so that we can get the buffers back after we commit
1545 * several transactions in the following calls.
1546 */
1547 error = xfs_attr_fillstate(state);
1548 if (error)
1549 goto out;
1550
1551 /*
1552 * Mark the attribute as INCOMPLETE, then bunmapi() the
1553 * remote value.
1554 */
1555 error = xfs_attr_leaf_setflag(args);
1556 if (error)
1557 goto out;
1558 error = xfs_attr_rmtval_remove(args);
1559 if (error)
1560 goto out;
1561
1562 /*
1563 * Refill the state structure with buffers, the prior calls
1564 * released our buffers.
1565 */
1566 error = xfs_attr_refillstate(state);
1567 if (error)
1568 goto out;
1569 }
1570
1571 /*
1572 * Remove the name and update the hashvals in the tree.
1573 */
1574 blk = &state->path.blk[ state->path.active-1 ];
1575 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1576 retval = xfs_attr_leaf_remove(blk->bp, args);
1577 xfs_da_fixhashpath(state, &state->path);
1578
1579 /*
1580 * Check to see if the tree needs to be collapsed.
1581 */
1582 if (retval && (state->path.active > 1)) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001583 xfs_bmap_init(args->flist, args->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 error = xfs_da_join(state);
1585 if (!error) {
1586 error = xfs_bmap_finish(&args->trans, args->flist,
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001587 &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 }
1589 if (error) {
1590 ASSERT(committed);
1591 args->trans = NULL;
1592 xfs_bmap_cancel(args->flist);
1593 goto out;
1594 }
1595
1596 /*
1597 * bmap_finish() may have committed the last trans and started
1598 * a new one. We need the inode to be in all transactions.
1599 */
1600 if (committed) {
1601 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1602 xfs_trans_ihold(args->trans, dp);
1603 }
1604
1605 /*
1606 * Commit the Btree join operation and start a new trans.
1607 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10001608 error = xfs_trans_roll(&args->trans, dp);
1609 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 goto out;
1611 }
1612
1613 /*
1614 * If the result is small enough, push it all into the inode.
1615 */
1616 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
1617 /*
1618 * Have to get rid of the copy of this dabuf in the state.
1619 */
1620 ASSERT(state->path.active == 1);
1621 ASSERT(state->path.blk[0].bp);
1622 xfs_da_buf_done(state->path.blk[0].bp);
1623 state->path.blk[0].bp = NULL;
1624
1625 error = xfs_da_read_buf(args->trans, args->dp, 0, -1, &bp,
1626 XFS_ATTR_FORK);
1627 if (error)
1628 goto out;
Nathan Scott89da0542006-03-17 17:28:40 +11001629 ASSERT(be16_to_cpu(((xfs_attr_leafblock_t *)
1630 bp->data)->hdr.info.magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 == XFS_ATTR_LEAF_MAGIC);
1632
Nathan Scottd8cc8902005-11-02 10:34:53 +11001633 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001634 xfs_bmap_init(args->flist, args->firstblock);
Nathan Scottd8cc8902005-11-02 10:34:53 +11001635 error = xfs_attr_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 /* bp is gone due to xfs_da_shrink_inode */
1637 if (!error) {
1638 error = xfs_bmap_finish(&args->trans,
1639 args->flist,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 &committed);
1641 }
1642 if (error) {
1643 ASSERT(committed);
1644 args->trans = NULL;
1645 xfs_bmap_cancel(args->flist);
1646 goto out;
1647 }
1648
1649 /*
1650 * bmap_finish() may have committed the last trans
1651 * and started a new one. We need the inode to be
1652 * in all transactions.
1653 */
1654 if (committed) {
1655 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1656 xfs_trans_ihold(args->trans, dp);
1657 }
1658 } else
1659 xfs_da_brelse(args->trans, bp);
1660 }
1661 error = 0;
1662
1663out:
1664 xfs_da_state_free(state);
1665 return(error);
1666}
1667
1668/*
1669 * Fill in the disk block numbers in the state structure for the buffers
1670 * that are attached to the state structure.
1671 * This is done so that we can quickly reattach ourselves to those buffers
Nathan Scottc41564b2006-03-29 08:55:14 +10001672 * after some set of transaction commits have released these buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 */
1674STATIC int
1675xfs_attr_fillstate(xfs_da_state_t *state)
1676{
1677 xfs_da_state_path_t *path;
1678 xfs_da_state_blk_t *blk;
1679 int level;
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->bp) {
1689 blk->disk_blkno = xfs_da_blkno(blk->bp);
1690 xfs_da_buf_done(blk->bp);
1691 blk->bp = NULL;
1692 } else {
1693 blk->disk_blkno = 0;
1694 }
1695 }
1696
1697 /*
1698 * Roll down the "altpath" in the state structure, storing the on-disk
1699 * block number for those buffers in the "altpath".
1700 */
1701 path = &state->altpath;
1702 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1703 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1704 if (blk->bp) {
1705 blk->disk_blkno = xfs_da_blkno(blk->bp);
1706 xfs_da_buf_done(blk->bp);
1707 blk->bp = NULL;
1708 } else {
1709 blk->disk_blkno = 0;
1710 }
1711 }
1712
1713 return(0);
1714}
1715
1716/*
1717 * Reattach the buffers to the state structure based on the disk block
1718 * numbers stored in the state structure.
Nathan Scottc41564b2006-03-29 08:55:14 +10001719 * This is done after some set of transaction commits have released those
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 * buffers from our grip.
1721 */
1722STATIC int
1723xfs_attr_refillstate(xfs_da_state_t *state)
1724{
1725 xfs_da_state_path_t *path;
1726 xfs_da_state_blk_t *blk;
1727 int level, error;
1728
1729 /*
1730 * Roll down the "path" in the state structure, storing the on-disk
1731 * block number for those buffers in the "path".
1732 */
1733 path = &state->path;
1734 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1735 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1736 if (blk->disk_blkno) {
1737 error = xfs_da_read_buf(state->args->trans,
1738 state->args->dp,
1739 blk->blkno, blk->disk_blkno,
1740 &blk->bp, XFS_ATTR_FORK);
1741 if (error)
1742 return(error);
1743 } else {
1744 blk->bp = NULL;
1745 }
1746 }
1747
1748 /*
1749 * Roll down the "altpath" in the state structure, storing the on-disk
1750 * block number for those buffers in the "altpath".
1751 */
1752 path = &state->altpath;
1753 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1754 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1755 if (blk->disk_blkno) {
1756 error = xfs_da_read_buf(state->args->trans,
1757 state->args->dp,
1758 blk->blkno, blk->disk_blkno,
1759 &blk->bp, XFS_ATTR_FORK);
1760 if (error)
1761 return(error);
1762 } else {
1763 blk->bp = NULL;
1764 }
1765 }
1766
1767 return(0);
1768}
1769
1770/*
1771 * Look up a filename in a node attribute list.
1772 *
1773 * This routine gets called for any attribute fork that has more than one
1774 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1775 * "remote" values taking up more blocks.
1776 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001777STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778xfs_attr_node_get(xfs_da_args_t *args)
1779{
1780 xfs_da_state_t *state;
1781 xfs_da_state_blk_t *blk;
1782 int error, retval;
1783 int i;
1784
1785 state = xfs_da_state_alloc();
1786 state->args = args;
1787 state->mp = args->dp->i_mount;
1788 state->blocksize = state->mp->m_sb.sb_blocksize;
1789 state->node_ents = state->mp->m_attr_node_ents;
1790
1791 /*
1792 * Search to see if name exists, and get back a pointer to it.
1793 */
1794 error = xfs_da_node_lookup_int(state, &retval);
1795 if (error) {
1796 retval = error;
1797 } else if (retval == EEXIST) {
1798 blk = &state->path.blk[ state->path.active-1 ];
1799 ASSERT(blk->bp != NULL);
1800 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1801
1802 /*
1803 * Get the value, local or "remote"
1804 */
1805 retval = xfs_attr_leaf_getvalue(blk->bp, args);
1806 if (!retval && (args->rmtblkno > 0)
1807 && !(args->flags & ATTR_KERNOVAL)) {
1808 retval = xfs_attr_rmtval_get(args);
1809 }
1810 }
1811
1812 /*
1813 * If not in a transaction, we have to release all the buffers.
1814 */
1815 for (i = 0; i < state->path.active; i++) {
1816 xfs_da_brelse(args->trans, state->path.blk[i].bp);
1817 state->path.blk[i].bp = NULL;
1818 }
1819
1820 xfs_da_state_free(state);
1821 return(retval);
1822}
1823
1824STATIC int /* error */
1825xfs_attr_node_list(xfs_attr_list_context_t *context)
1826{
1827 attrlist_cursor_kern_t *cursor;
1828 xfs_attr_leafblock_t *leaf;
1829 xfs_da_intnode_t *node;
1830 xfs_da_node_entry_t *btree;
1831 int error, i;
1832 xfs_dabuf_t *bp;
1833
1834 cursor = context->cursor;
1835 cursor->initted = 1;
1836
1837 /*
1838 * Do all sorts of validation on the passed-in cursor structure.
1839 * If anything is amiss, ignore the cursor and look up the hashval
1840 * starting from the btree root.
1841 */
1842 bp = NULL;
1843 if (cursor->blkno > 0) {
1844 error = xfs_da_read_buf(NULL, context->dp, cursor->blkno, -1,
1845 &bp, XFS_ATTR_FORK);
1846 if ((error != 0) && (error != EFSCORRUPTED))
1847 return(error);
1848 if (bp) {
1849 node = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001850 switch (be16_to_cpu(node->hdr.info.magic)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 case XFS_DA_NODE_MAGIC:
1852 xfs_attr_trace_l_cn("wrong blk", context, node);
1853 xfs_da_brelse(NULL, bp);
1854 bp = NULL;
1855 break;
1856 case XFS_ATTR_LEAF_MAGIC:
1857 leaf = bp->data;
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001858 if (cursor->hashval > be32_to_cpu(leaf->entries[
1859 be16_to_cpu(leaf->hdr.count)-1].hashval)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 xfs_attr_trace_l_cl("wrong blk",
1861 context, leaf);
1862 xfs_da_brelse(NULL, bp);
1863 bp = NULL;
1864 } else if (cursor->hashval <=
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001865 be32_to_cpu(leaf->entries[0].hashval)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 xfs_attr_trace_l_cl("maybe wrong blk",
1867 context, leaf);
1868 xfs_da_brelse(NULL, bp);
1869 bp = NULL;
1870 }
1871 break;
1872 default:
1873 xfs_attr_trace_l_c("wrong blk - ??", context);
1874 xfs_da_brelse(NULL, bp);
1875 bp = NULL;
1876 }
1877 }
1878 }
1879
1880 /*
1881 * We did not find what we expected given the cursor's contents,
1882 * so we start from the top and work down based on the hash value.
1883 * Note that start of node block is same as start of leaf block.
1884 */
1885 if (bp == NULL) {
1886 cursor->blkno = 0;
1887 for (;;) {
1888 error = xfs_da_read_buf(NULL, context->dp,
1889 cursor->blkno, -1, &bp,
1890 XFS_ATTR_FORK);
1891 if (error)
1892 return(error);
1893 if (unlikely(bp == NULL)) {
1894 XFS_ERROR_REPORT("xfs_attr_node_list(2)",
1895 XFS_ERRLEVEL_LOW,
1896 context->dp->i_mount);
1897 return(XFS_ERROR(EFSCORRUPTED));
1898 }
1899 node = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001900 if (be16_to_cpu(node->hdr.info.magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 == XFS_ATTR_LEAF_MAGIC)
1902 break;
Nathan Scott89da0542006-03-17 17:28:40 +11001903 if (unlikely(be16_to_cpu(node->hdr.info.magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 != XFS_DA_NODE_MAGIC)) {
1905 XFS_CORRUPTION_ERROR("xfs_attr_node_list(3)",
1906 XFS_ERRLEVEL_LOW,
1907 context->dp->i_mount,
1908 node);
1909 xfs_da_brelse(NULL, bp);
1910 return(XFS_ERROR(EFSCORRUPTED));
1911 }
1912 btree = node->btree;
Nathan Scottfac80cc2006-03-17 17:29:56 +11001913 for (i = 0; i < be16_to_cpu(node->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 btree++, i++) {
1915 if (cursor->hashval
Nathan Scott403432d2006-03-17 17:29:46 +11001916 <= be32_to_cpu(btree->hashval)) {
1917 cursor->blkno = be32_to_cpu(btree->before);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 xfs_attr_trace_l_cb("descending",
1919 context, btree);
1920 break;
1921 }
1922 }
Nathan Scottfac80cc2006-03-17 17:29:56 +11001923 if (i == be16_to_cpu(node->hdr.count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 xfs_da_brelse(NULL, bp);
1925 return(0);
1926 }
1927 xfs_da_brelse(NULL, bp);
1928 }
1929 }
1930 ASSERT(bp != NULL);
1931
1932 /*
1933 * Roll upward through the blocks, processing each leaf block in
1934 * order. As long as there is space in the result buffer, keep
1935 * adding the information.
1936 */
1937 for (;;) {
1938 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001939 if (unlikely(be16_to_cpu(leaf->hdr.info.magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 != XFS_ATTR_LEAF_MAGIC)) {
1941 XFS_CORRUPTION_ERROR("xfs_attr_node_list(4)",
1942 XFS_ERRLEVEL_LOW,
1943 context->dp->i_mount, leaf);
1944 xfs_da_brelse(NULL, bp);
1945 return(XFS_ERROR(EFSCORRUPTED));
1946 }
1947 error = xfs_attr_leaf_list_int(bp, context);
Tim Shimmin726801b2006-09-28 11:01:37 +10001948 if (error) {
1949 xfs_da_brelse(NULL, bp);
1950 return error;
1951 }
1952 if (context->seen_enough || leaf->hdr.info.forw == 0)
1953 break;
Nathan Scott89da0542006-03-17 17:28:40 +11001954 cursor->blkno = be32_to_cpu(leaf->hdr.info.forw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 xfs_da_brelse(NULL, bp);
1956 error = xfs_da_read_buf(NULL, context->dp, cursor->blkno, -1,
1957 &bp, XFS_ATTR_FORK);
1958 if (error)
1959 return(error);
1960 if (unlikely((bp == NULL))) {
1961 XFS_ERROR_REPORT("xfs_attr_node_list(5)",
1962 XFS_ERRLEVEL_LOW,
1963 context->dp->i_mount);
1964 return(XFS_ERROR(EFSCORRUPTED));
1965 }
1966 }
1967 xfs_da_brelse(NULL, bp);
1968 return(0);
1969}
1970
1971
1972/*========================================================================
1973 * External routines for manipulating out-of-line attribute values.
1974 *========================================================================*/
1975
1976/*
1977 * Read the value associated with an attribute from the out-of-line buffer
1978 * that we stored it in.
1979 */
Tim Shimmin726801b2006-09-28 11:01:37 +10001980int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981xfs_attr_rmtval_get(xfs_da_args_t *args)
1982{
1983 xfs_bmbt_irec_t map[ATTR_RMTVALUE_MAPSIZE];
1984 xfs_mount_t *mp;
1985 xfs_daddr_t dblkno;
1986 xfs_caddr_t dst;
1987 xfs_buf_t *bp;
1988 int nmap, error, tmp, valuelen, blkcnt, i;
1989 xfs_dablk_t lblkno;
1990
1991 ASSERT(!(args->flags & ATTR_KERNOVAL));
1992
1993 mp = args->dp->i_mount;
1994 dst = args->value;
1995 valuelen = args->valuelen;
1996 lblkno = args->rmtblkno;
1997 while (valuelen > 0) {
1998 nmap = ATTR_RMTVALUE_MAPSIZE;
1999 error = xfs_bmapi(args->trans, args->dp, (xfs_fileoff_t)lblkno,
2000 args->rmtblkcnt,
2001 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002002 NULL, 0, map, &nmap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 if (error)
2004 return(error);
2005 ASSERT(nmap >= 1);
2006
2007 for (i = 0; (i < nmap) && (valuelen > 0); i++) {
2008 ASSERT((map[i].br_startblock != DELAYSTARTBLOCK) &&
2009 (map[i].br_startblock != HOLESTARTBLOCK));
2010 dblkno = XFS_FSB_TO_DADDR(mp, map[i].br_startblock);
2011 blkcnt = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
2012 error = xfs_read_buf(mp, mp->m_ddev_targp, dblkno,
Christoph Hellwigddd3a142009-07-18 18:15:01 -04002013 blkcnt,
2014 XFS_BUF_LOCK | XBF_DONT_BLOCK,
2015 &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 if (error)
2017 return(error);
2018
2019 tmp = (valuelen < XFS_BUF_SIZE(bp))
2020 ? valuelen : XFS_BUF_SIZE(bp);
2021 xfs_biomove(bp, 0, tmp, dst, XFS_B_READ);
2022 xfs_buf_relse(bp);
2023 dst += tmp;
2024 valuelen -= tmp;
2025
2026 lblkno += map[i].br_blockcount;
2027 }
2028 }
2029 ASSERT(valuelen == 0);
2030 return(0);
2031}
2032
2033/*
2034 * Write the value associated with an attribute into the out-of-line buffer
2035 * that we have defined for it.
2036 */
2037STATIC int
2038xfs_attr_rmtval_set(xfs_da_args_t *args)
2039{
2040 xfs_mount_t *mp;
2041 xfs_fileoff_t lfileoff;
2042 xfs_inode_t *dp;
2043 xfs_bmbt_irec_t map;
2044 xfs_daddr_t dblkno;
2045 xfs_caddr_t src;
2046 xfs_buf_t *bp;
2047 xfs_dablk_t lblkno;
2048 int blkcnt, valuelen, nmap, error, tmp, committed;
2049
2050 dp = args->dp;
2051 mp = dp->i_mount;
2052 src = args->value;
2053
2054 /*
2055 * Find a "hole" in the attribute address space large enough for
2056 * us to drop the new attribute's value into.
2057 */
2058 blkcnt = XFS_B_TO_FSB(mp, args->valuelen);
2059 lfileoff = 0;
2060 error = xfs_bmap_first_unused(args->trans, args->dp, blkcnt, &lfileoff,
2061 XFS_ATTR_FORK);
2062 if (error) {
2063 return(error);
2064 }
2065 args->rmtblkno = lblkno = (xfs_dablk_t)lfileoff;
2066 args->rmtblkcnt = blkcnt;
2067
2068 /*
2069 * Roll through the "value", allocating blocks on disk as required.
2070 */
2071 while (blkcnt > 0) {
2072 /*
2073 * Allocate a single extent, up to the size of the value.
2074 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06002075 xfs_bmap_init(args->flist, args->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 nmap = 1;
2077 error = xfs_bmapi(args->trans, dp, (xfs_fileoff_t)lblkno,
2078 blkcnt,
2079 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA |
2080 XFS_BMAPI_WRITE,
2081 args->firstblock, args->total, &map, &nmap,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002082 args->flist, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 if (!error) {
2084 error = xfs_bmap_finish(&args->trans, args->flist,
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002085 &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 }
2087 if (error) {
2088 ASSERT(committed);
2089 args->trans = NULL;
2090 xfs_bmap_cancel(args->flist);
2091 return(error);
2092 }
2093
2094 /*
2095 * bmap_finish() may have committed the last trans and started
2096 * a new one. We need the inode to be in all transactions.
2097 */
2098 if (committed) {
2099 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
2100 xfs_trans_ihold(args->trans, dp);
2101 }
2102
2103 ASSERT(nmap == 1);
2104 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
2105 (map.br_startblock != HOLESTARTBLOCK));
2106 lblkno += map.br_blockcount;
2107 blkcnt -= map.br_blockcount;
2108
2109 /*
2110 * Start the next trans in the chain.
2111 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10002112 error = xfs_trans_roll(&args->trans, dp);
2113 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 return (error);
2115 }
2116
2117 /*
2118 * Roll through the "value", copying the attribute value to the
2119 * already-allocated blocks. Blocks are written synchronously
2120 * so that we can know they are all on disk before we turn off
2121 * the INCOMPLETE flag.
2122 */
2123 lblkno = args->rmtblkno;
2124 valuelen = args->valuelen;
2125 while (valuelen > 0) {
2126 /*
2127 * Try to remember where we decided to put the value.
2128 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06002129 xfs_bmap_init(args->flist, args->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 nmap = 1;
2131 error = xfs_bmapi(NULL, dp, (xfs_fileoff_t)lblkno,
2132 args->rmtblkcnt,
2133 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002134 args->firstblock, 0, &map, &nmap,
2135 NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 if (error) {
2137 return(error);
2138 }
2139 ASSERT(nmap == 1);
2140 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
2141 (map.br_startblock != HOLESTARTBLOCK));
2142
2143 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
2144 blkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
2145
Christoph Hellwig10746e42009-07-18 18:14:59 -04002146 bp = xfs_buf_get_flags(mp->m_ddev_targp, dblkno, blkcnt,
2147 XFS_BUF_LOCK | XBF_DONT_BLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 ASSERT(bp);
2149 ASSERT(!XFS_BUF_GETERROR(bp));
2150
2151 tmp = (valuelen < XFS_BUF_SIZE(bp)) ? valuelen :
2152 XFS_BUF_SIZE(bp);
2153 xfs_biomove(bp, 0, tmp, src, XFS_B_WRITE);
2154 if (tmp < XFS_BUF_SIZE(bp))
2155 xfs_biozero(bp, tmp, XFS_BUF_SIZE(bp) - tmp);
2156 if ((error = xfs_bwrite(mp, bp))) {/* GROT: NOTE: synchronous write */
2157 return (error);
2158 }
2159 src += tmp;
2160 valuelen -= tmp;
2161
2162 lblkno += map.br_blockcount;
2163 }
2164 ASSERT(valuelen == 0);
2165 return(0);
2166}
2167
2168/*
2169 * Remove the value associated with an attribute by deleting the
2170 * out-of-line buffer that it is stored on.
2171 */
2172STATIC int
2173xfs_attr_rmtval_remove(xfs_da_args_t *args)
2174{
2175 xfs_mount_t *mp;
2176 xfs_bmbt_irec_t map;
2177 xfs_buf_t *bp;
2178 xfs_daddr_t dblkno;
2179 xfs_dablk_t lblkno;
2180 int valuelen, blkcnt, nmap, error, done, committed;
2181
2182 mp = args->dp->i_mount;
2183
2184 /*
2185 * Roll through the "value", invalidating the attribute value's
2186 * blocks.
2187 */
2188 lblkno = args->rmtblkno;
2189 valuelen = args->rmtblkcnt;
2190 while (valuelen > 0) {
2191 /*
2192 * Try to remember where we decided to put the value.
2193 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06002194 xfs_bmap_init(args->flist, args->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 nmap = 1;
2196 error = xfs_bmapi(NULL, args->dp, (xfs_fileoff_t)lblkno,
2197 args->rmtblkcnt,
2198 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
2199 args->firstblock, 0, &map, &nmap,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002200 args->flist, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 if (error) {
2202 return(error);
2203 }
2204 ASSERT(nmap == 1);
2205 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
2206 (map.br_startblock != HOLESTARTBLOCK));
2207
2208 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
2209 blkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
2210
2211 /*
2212 * If the "remote" value is in the cache, remove it.
2213 */
2214 bp = xfs_incore(mp->m_ddev_targp, dblkno, blkcnt,
2215 XFS_INCORE_TRYLOCK);
2216 if (bp) {
2217 XFS_BUF_STALE(bp);
2218 XFS_BUF_UNDELAYWRITE(bp);
2219 xfs_buf_relse(bp);
2220 bp = NULL;
2221 }
2222
2223 valuelen -= map.br_blockcount;
2224
2225 lblkno += map.br_blockcount;
2226 }
2227
2228 /*
2229 * Keep de-allocating extents until the remote-value region is gone.
2230 */
2231 lblkno = args->rmtblkno;
2232 blkcnt = args->rmtblkcnt;
2233 done = 0;
2234 while (!done) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002235 xfs_bmap_init(args->flist, args->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 error = xfs_bunmapi(args->trans, args->dp, lblkno, blkcnt,
2237 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002238 1, args->firstblock, args->flist,
2239 NULL, &done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 if (!error) {
2241 error = xfs_bmap_finish(&args->trans, args->flist,
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002242 &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 }
2244 if (error) {
2245 ASSERT(committed);
2246 args->trans = NULL;
2247 xfs_bmap_cancel(args->flist);
2248 return(error);
2249 }
2250
2251 /*
2252 * bmap_finish() may have committed the last trans and started
2253 * a new one. We need the inode to be in all transactions.
2254 */
2255 if (committed) {
2256 xfs_trans_ijoin(args->trans, args->dp, XFS_ILOCK_EXCL);
2257 xfs_trans_ihold(args->trans, args->dp);
2258 }
2259
2260 /*
2261 * Close out trans and start the next one in the chain.
2262 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10002263 error = xfs_trans_roll(&args->trans, args->dp);
2264 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 return (error);
2266 }
2267 return(0);
2268}
2269
2270#if defined(XFS_ATTR_TRACE)
2271/*
2272 * Add a trace buffer entry for an attr_list context structure.
2273 */
2274void
2275xfs_attr_trace_l_c(char *where, struct xfs_attr_list_context *context)
2276{
Eric Sandeen68f34d52008-05-20 15:11:17 +10002277 xfs_attr_trace_enter(XFS_ATTR_KTRACE_L_C, where, context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 (__psunsigned_t)NULL,
2279 (__psunsigned_t)NULL,
2280 (__psunsigned_t)NULL);
2281}
2282
2283/*
2284 * Add a trace buffer entry for a context structure and a Btree node.
2285 */
2286void
2287xfs_attr_trace_l_cn(char *where, struct xfs_attr_list_context *context,
2288 struct xfs_da_intnode *node)
2289{
Eric Sandeen68f34d52008-05-20 15:11:17 +10002290 xfs_attr_trace_enter(XFS_ATTR_KTRACE_L_CN, where, context,
Nathan Scottfac80cc2006-03-17 17:29:56 +11002291 (__psunsigned_t)be16_to_cpu(node->hdr.count),
Nathan Scott403432d2006-03-17 17:29:46 +11002292 (__psunsigned_t)be32_to_cpu(node->btree[0].hashval),
Nathan Scottfac80cc2006-03-17 17:29:56 +11002293 (__psunsigned_t)be32_to_cpu(node->btree[
2294 be16_to_cpu(node->hdr.count)-1].hashval));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295}
2296
2297/*
2298 * Add a trace buffer entry for a context structure and a Btree element.
2299 */
2300void
2301xfs_attr_trace_l_cb(char *where, struct xfs_attr_list_context *context,
2302 struct xfs_da_node_entry *btree)
2303{
Eric Sandeen68f34d52008-05-20 15:11:17 +10002304 xfs_attr_trace_enter(XFS_ATTR_KTRACE_L_CB, where, context,
Nathan Scott403432d2006-03-17 17:29:46 +11002305 (__psunsigned_t)be32_to_cpu(btree->hashval),
2306 (__psunsigned_t)be32_to_cpu(btree->before),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 (__psunsigned_t)NULL);
2308}
2309
2310/*
2311 * Add a trace buffer entry for a context structure and a leaf block.
2312 */
2313void
2314xfs_attr_trace_l_cl(char *where, struct xfs_attr_list_context *context,
2315 struct xfs_attr_leafblock *leaf)
2316{
Eric Sandeen68f34d52008-05-20 15:11:17 +10002317 xfs_attr_trace_enter(XFS_ATTR_KTRACE_L_CL, where, context,
Nathan Scott918ae422006-03-17 17:28:54 +11002318 (__psunsigned_t)be16_to_cpu(leaf->hdr.count),
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002319 (__psunsigned_t)be32_to_cpu(leaf->entries[0].hashval),
2320 (__psunsigned_t)be32_to_cpu(leaf->entries[
2321 be16_to_cpu(leaf->hdr.count)-1].hashval));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322}
2323
2324/*
2325 * Add a trace buffer entry for the arguments given to the routine,
2326 * generic form.
2327 */
2328void
2329xfs_attr_trace_enter(int type, char *where,
Eric Sandeen68f34d52008-05-20 15:11:17 +10002330 struct xfs_attr_list_context *context,
2331 __psunsigned_t a13, __psunsigned_t a14,
2332 __psunsigned_t a15)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333{
2334 ASSERT(xfs_attr_trace_buf);
2335 ktrace_enter(xfs_attr_trace_buf, (void *)((__psunsigned_t)type),
Eric Sandeen68f34d52008-05-20 15:11:17 +10002336 (void *)((__psunsigned_t)where),
2337 (void *)((__psunsigned_t)context->dp),
2338 (void *)((__psunsigned_t)context->cursor->hashval),
2339 (void *)((__psunsigned_t)context->cursor->blkno),
2340 (void *)((__psunsigned_t)context->cursor->offset),
2341 (void *)((__psunsigned_t)context->alist),
2342 (void *)((__psunsigned_t)context->bufsize),
2343 (void *)((__psunsigned_t)context->count),
2344 (void *)((__psunsigned_t)context->firstu),
Christoph Hellwigad9b4632008-06-23 13:23:48 +10002345 NULL,
Eric Sandeen68f34d52008-05-20 15:11:17 +10002346 (void *)((__psunsigned_t)context->dupcnt),
2347 (void *)((__psunsigned_t)context->flags),
2348 (void *)a13, (void *)a14, (void *)a15);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349}
2350#endif /* XFS_ATTR_TRACE */