blob: 0d92e21ca33c77a106d3335c2ca347fe6147e55d [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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Dave Chinner6ca1c902013-08-12 20:49:26 +100020#include "xfs_format.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_log.h"
23#include "xfs_trans.h"
Dave Chinnerd386b322013-08-12 20:49:31 +100024#include "xfs_trans_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_sb.h"
26#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110028#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110030#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_dinode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_alloc.h"
Nathan Scotta844f452005-11-02 14:38:42 +110034#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_attr.h"
37#include "xfs_attr_leaf.h"
Dave Chinner95920cd2013-04-03 16:11:27 +110038#include "xfs_attr_remote.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include "xfs_quota.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include "xfs_trans_space.h"
Christoph Hellwig739bfb22007-08-29 10:58:01 +100042#include "xfs_vnodeops.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000043#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45/*
46 * xfs_attr.c
47 *
48 * Provide the external interfaces to manage attribute lists.
49 */
50
51/*========================================================================
52 * Function prototypes for the kernel.
53 *========================================================================*/
54
55/*
56 * Internal routines when attribute list fits inside the inode.
57 */
58STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
59
60/*
61 * Internal routines when attribute list is one block.
62 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100063STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064STATIC int xfs_attr_leaf_addname(xfs_da_args_t *args);
65STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
66STATIC int xfs_attr_leaf_list(xfs_attr_list_context_t *context);
67
68/*
69 * Internal routines when attribute list is more than one block.
70 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100071STATIC int xfs_attr_node_get(xfs_da_args_t *args);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072STATIC int xfs_attr_node_addname(xfs_da_args_t *args);
73STATIC int xfs_attr_node_removename(xfs_da_args_t *args);
74STATIC int xfs_attr_node_list(xfs_attr_list_context_t *context);
75STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
76STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Barry Naujoke8b0eba2008-04-22 17:34:31 +100079STATIC int
80xfs_attr_name_to_xname(
81 struct xfs_name *xname,
Dave Chinnera9273ca2010-01-20 10:47:48 +110082 const unsigned char *aname)
Barry Naujoke8b0eba2008-04-22 17:34:31 +100083{
84 if (!aname)
85 return EINVAL;
86 xname->name = aname;
Dave Chinnera9273ca2010-01-20 10:47:48 +110087 xname->len = strlen((char *)aname);
Barry Naujoke8b0eba2008-04-22 17:34:31 +100088 if (xname->len >= MAXNAMELEN)
89 return EFAULT; /* match IRIX behaviour */
90
91 return 0;
92}
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +100094STATIC int
95xfs_inode_hasattr(
96 struct xfs_inode *ip)
97{
98 if (!XFS_IFORK_Q(ip) ||
99 (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
100 ip->i_d.di_anextents == 0))
101 return 0;
102 return 1;
103}
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105/*========================================================================
106 * Overall external interface routines.
107 *========================================================================*/
108
Christoph Hellwige82fa0c2009-11-14 16:17:20 +0000109STATIC int
110xfs_attr_get_int(
111 struct xfs_inode *ip,
112 struct xfs_name *name,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100113 unsigned char *value,
Christoph Hellwige82fa0c2009-11-14 16:17:20 +0000114 int *valuelenp,
115 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
117 xfs_da_args_t args;
118 int error;
119
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +1000120 if (!xfs_inode_hasattr(ip))
121 return ENOATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 /*
124 * Fill in the arg structure for this request.
125 */
126 memset((char *)&args, 0, sizeof(args));
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000127 args.name = name->name;
128 args.namelen = name->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 args.value = value;
130 args.valuelen = *valuelenp;
131 args.flags = flags;
132 args.hashval = xfs_da_hashname(args.name, args.namelen);
133 args.dp = ip;
134 args.whichfork = XFS_ATTR_FORK;
135
136 /*
137 * Decide on what work routines to call based on the inode size.
138 */
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +1000139 if (ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 error = xfs_attr_shortform_getvalue(&args);
141 } else if (xfs_bmap_one_block(ip, XFS_ATTR_FORK)) {
142 error = xfs_attr_leaf_get(&args);
143 } else {
144 error = xfs_attr_node_get(&args);
145 }
146
147 /*
148 * Return the number of bytes in the value to the caller.
149 */
150 *valuelenp = args.valuelen;
151
152 if (error == EEXIST)
153 error = 0;
154 return(error);
155}
156
157int
Christoph Hellwig993386c2007-08-28 16:12:30 +1000158xfs_attr_get(
159 xfs_inode_t *ip,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100160 const unsigned char *name,
161 unsigned char *value,
Christoph Hellwig993386c2007-08-28 16:12:30 +1000162 int *valuelenp,
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000163 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000165 int error;
166 struct xfs_name xname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 XFS_STATS_INC(xs_attr_get);
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
171 return(EIO);
172
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000173 error = xfs_attr_name_to_xname(&xname, name);
174 if (error)
175 return error;
176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 xfs_ilock(ip, XFS_ILOCK_SHARED);
Christoph Hellwige82fa0c2009-11-14 16:17:20 +0000178 error = xfs_attr_get_int(ip, &xname, value, valuelenp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 xfs_iunlock(ip, XFS_ILOCK_SHARED);
180 return(error);
181}
182
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000183/*
184 * Calculate how many blocks we need for the new attribute,
185 */
Eric Sandeen5d77c0d2009-11-19 15:52:00 +0000186STATIC int
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000187xfs_attr_calc_size(
188 struct xfs_inode *ip,
189 int namelen,
190 int valuelen,
191 int *local)
192{
193 struct xfs_mount *mp = ip->i_mount;
194 int size;
195 int nblks;
196
197 /*
198 * Determine space new attribute will use, and if it would be
199 * "local" or "remote" (note: local != inline).
200 */
201 size = xfs_attr_leaf_newentsize(namelen, valuelen,
202 mp->m_sb.sb_blocksize, local);
203
204 nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
205 if (*local) {
206 if (size > (mp->m_sb.sb_blocksize >> 1)) {
207 /* Double split possible */
208 nblks *= 2;
209 }
210 } else {
211 /*
212 * Out of line attribute, cannot double split, but
213 * make room for the attribute value itself.
214 */
215 uint dblocks = XFS_B_TO_FSB(mp, valuelen);
216 nblks += dblocks;
217 nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
218 }
219
220 return nblks;
221}
222
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000223STATIC int
Dave Chinnera9273ca2010-01-20 10:47:48 +1100224xfs_attr_set_int(
225 struct xfs_inode *dp,
226 struct xfs_name *name,
227 unsigned char *value,
228 int valuelen,
229 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
231 xfs_da_args_t args;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 xfs_fsblock_t firstblock;
233 xfs_bmap_free_t flist;
234 int error, err2, committed;
Nathan Scottaa82daa2005-11-02 10:33:33 +1100235 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 int rsvd = (flags & ATTR_ROOT) != 0;
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000237 int local;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 /*
240 * Attach the dquots to the inode.
241 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200242 error = xfs_qm_dqattach(dp, 0);
243 if (error)
244 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246 /*
247 * If the inode doesn't have an attribute fork, add one.
248 * (inode must not be locked when we call this routine)
249 */
250 if (XFS_IFORK_Q(dp) == 0) {
Barry Naujoke5889e92007-02-10 18:35:58 +1100251 int sf_size = sizeof(xfs_attr_sf_hdr_t) +
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000252 XFS_ATTR_SF_ENTSIZE_BYNAME(name->len, valuelen);
Barry Naujoke5889e92007-02-10 18:35:58 +1100253
254 if ((error = xfs_bmap_add_attrfork(dp, sf_size, rsvd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return(error);
256 }
257
258 /*
259 * Fill in the arg structure for this request.
260 */
261 memset((char *)&args, 0, sizeof(args));
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000262 args.name = name->name;
263 args.namelen = name->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 args.value = value;
265 args.valuelen = valuelen;
266 args.flags = flags;
267 args.hashval = xfs_da_hashname(args.name, args.namelen);
268 args.dp = dp;
269 args.firstblock = &firstblock;
270 args.flist = &flist;
271 args.whichfork = XFS_ATTR_FORK;
Barry Naujok6a178102008-05-21 16:42:05 +1000272 args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 /* Size is now blocks for attribute data */
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000275 args.total = xfs_attr_calc_size(dp, name->len, valuelen, &local);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277 /*
278 * Start our first transaction of the day.
279 *
280 * All future transactions during this code must be "chained" off
281 * this one via the trans_dup() call. All transactions will contain
282 * the inode, and the inode will always be marked with trans_ihold().
283 * Since the inode will be locked in all transactions, we must log
284 * the inode in every transaction to let it float upward through
285 * the log.
286 */
287 args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_SET);
288
289 /*
290 * Root fork attributes can use reserved data blocks for this
291 * operation if necessary
292 */
293
294 if (rsvd)
295 args.trans->t_flags |= XFS_TRANS_RESERVE;
296
Jeff Liua21cd502013-01-28 21:27:53 +0800297 error = xfs_trans_reserve(args.trans, args.total,
298 XFS_ATTRSETM_LOG_RES(mp) +
299 XFS_ATTRSETRT_LOG_RES(mp) * args.total,
300 0, XFS_TRANS_PERM_LOG_RES,
301 XFS_ATTRSET_LOG_COUNT);
302 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 xfs_trans_cancel(args.trans, 0);
304 return(error);
305 }
306 xfs_ilock(dp, XFS_ILOCK_EXCL);
307
Christoph Hellwig7d095252009-06-08 15:33:32 +0200308 error = xfs_trans_reserve_quota_nblks(args.trans, dp, args.total, 0,
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000309 rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
310 XFS_QMOPT_RES_REGBLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 if (error) {
312 xfs_iunlock(dp, XFS_ILOCK_EXCL);
313 xfs_trans_cancel(args.trans, XFS_TRANS_RELEASE_LOG_RES);
314 return (error);
315 }
316
Christoph Hellwigddc34152011-09-19 15:00:54 +0000317 xfs_trans_ijoin(args.trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000320 * If the attribute list is non-existent or a shortform list,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 * upgrade it to a single-leaf-block attribute list.
322 */
323 if ((dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) ||
324 ((dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS) &&
325 (dp->i_d.di_anextents == 0))) {
326
327 /*
328 * Build initial attribute list (if required).
329 */
330 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
Nathan Scottd8cc8902005-11-02 10:34:53 +1100331 xfs_attr_shortform_create(&args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 /*
334 * Try to add the attr to the attribute list in
335 * the inode.
336 */
337 error = xfs_attr_shortform_addname(&args);
338 if (error != ENOSPC) {
339 /*
340 * Commit the shortform mods, and we're done.
341 * NOTE: this is also the error path (EEXIST, etc).
342 */
343 ASSERT(args.trans != NULL);
344
345 /*
346 * If this is a synchronous mount, make sure that
347 * the transaction goes to disk before returning
348 * to the user.
349 */
350 if (mp->m_flags & XFS_MOUNT_WSYNC) {
351 xfs_trans_set_sync(args.trans);
352 }
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000353
354 if (!error && (flags & ATTR_KERNOTIME) == 0) {
355 xfs_trans_ichgtime(args.trans, dp,
356 XFS_ICHGTIME_CHG);
357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 err2 = xfs_trans_commit(args.trans,
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000359 XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 xfs_iunlock(dp, XFS_ILOCK_EXCL);
361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 return(error == 0 ? err2 : error);
363 }
364
365 /*
366 * It won't fit in the shortform, transform to a leaf block.
367 * GROT: another possible req'mt for a double-split btree op.
368 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600369 xfs_bmap_init(args.flist, args.firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 error = xfs_attr_shortform_to_leaf(&args);
371 if (!error) {
372 error = xfs_bmap_finish(&args.trans, args.flist,
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100373 &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
375 if (error) {
376 ASSERT(committed);
377 args.trans = NULL;
378 xfs_bmap_cancel(&flist);
379 goto out;
380 }
381
382 /*
383 * bmap_finish() may have committed the last trans and started
384 * a new one. We need the inode to be in all transactions.
385 */
Christoph Hellwig898621d2010-06-24 11:36:58 +1000386 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +0000387 xfs_trans_ijoin(args.trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 /*
390 * Commit the leaf transformation. We'll need another (linked)
391 * transaction to add the new attribute to the leaf.
392 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000393
394 error = xfs_trans_roll(&args.trans, dp);
395 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 goto out;
397
398 }
399
400 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
401 error = xfs_attr_leaf_addname(&args);
402 } else {
403 error = xfs_attr_node_addname(&args);
404 }
405 if (error) {
406 goto out;
407 }
408
409 /*
410 * If this is a synchronous mount, make sure that the
411 * transaction goes to disk before returning to the user.
412 */
413 if (mp->m_flags & XFS_MOUNT_WSYNC) {
414 xfs_trans_set_sync(args.trans);
415 }
416
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000417 if ((flags & ATTR_KERNOTIME) == 0)
418 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 /*
421 * Commit the last in the sequence of transactions.
422 */
423 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000424 error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 xfs_iunlock(dp, XFS_ILOCK_EXCL);
426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 return(error);
428
429out:
430 if (args.trans)
431 xfs_trans_cancel(args.trans,
432 XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
433 xfs_iunlock(dp, XFS_ILOCK_EXCL);
434 return(error);
435}
436
Nathan Scottaa82daa2005-11-02 10:33:33 +1100437int
Christoph Hellwig993386c2007-08-28 16:12:30 +1000438xfs_attr_set(
439 xfs_inode_t *dp,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100440 const unsigned char *name,
441 unsigned char *value,
Christoph Hellwig993386c2007-08-28 16:12:30 +1000442 int valuelen,
443 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000445 int error;
446 struct xfs_name xname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Nathan Scottaa82daa2005-11-02 10:33:33 +1100448 XFS_STATS_INC(xs_attr_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Nathan Scottaa82daa2005-11-02 10:33:33 +1100450 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 return (EIO);
452
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000453 error = xfs_attr_name_to_xname(&xname, name);
454 if (error)
455 return error;
456
457 return xfs_attr_set_int(dp, &xname, value, valuelen, flags);
Nathan Scottaa82daa2005-11-02 10:33:33 +1100458}
459
460/*
461 * Generic handler routine to remove a name from an attribute list.
462 * Transitions attribute list from Btree to shortform as necessary.
463 */
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000464STATIC int
465xfs_attr_remove_int(xfs_inode_t *dp, struct xfs_name *name, int flags)
Nathan Scottaa82daa2005-11-02 10:33:33 +1100466{
467 xfs_da_args_t args;
468 xfs_fsblock_t firstblock;
469 xfs_bmap_free_t flist;
470 int error;
471 xfs_mount_t *mp = dp->i_mount;
472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 /*
474 * Fill in the arg structure for this request.
475 */
476 memset((char *)&args, 0, sizeof(args));
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000477 args.name = name->name;
478 args.namelen = name->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 args.flags = flags;
480 args.hashval = xfs_da_hashname(args.name, args.namelen);
481 args.dp = dp;
482 args.firstblock = &firstblock;
483 args.flist = &flist;
484 args.total = 0;
485 args.whichfork = XFS_ATTR_FORK;
486
487 /*
Dave Chinner4a338212011-06-23 01:35:01 +0000488 * we have no control over the attribute names that userspace passes us
489 * to remove, so we have to allow the name lookup prior to attribute
490 * removal to fail.
491 */
492 args.op_flags = XFS_DA_OP_OKNOENT;
493
494 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 * Attach the dquots to the inode.
496 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200497 error = xfs_qm_dqattach(dp, 0);
498 if (error)
499 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 /*
502 * Start our first transaction of the day.
503 *
504 * All future transactions during this code must be "chained" off
505 * this one via the trans_dup() call. All transactions will contain
506 * the inode, and the inode will always be marked with trans_ihold().
507 * Since the inode will be locked in all transactions, we must log
508 * the inode in every transaction to let it float upward through
509 * the log.
510 */
511 args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_RM);
512
513 /*
514 * Root fork attributes can use reserved data blocks for this
515 * operation if necessary
516 */
517
518 if (flags & ATTR_ROOT)
519 args.trans->t_flags |= XFS_TRANS_RESERVE;
520
521 if ((error = xfs_trans_reserve(args.trans,
522 XFS_ATTRRM_SPACE_RES(mp),
523 XFS_ATTRRM_LOG_RES(mp),
524 0, XFS_TRANS_PERM_LOG_RES,
525 XFS_ATTRRM_LOG_COUNT))) {
526 xfs_trans_cancel(args.trans, 0);
527 return(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 }
529
530 xfs_ilock(dp, XFS_ILOCK_EXCL);
531 /*
532 * No need to make quota reservations here. We expect to release some
533 * blocks not allocate in the common case.
534 */
Christoph Hellwigddc34152011-09-19 15:00:54 +0000535 xfs_trans_ijoin(args.trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 /*
538 * Decide on what work routines to call based on the inode size.
539 */
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +1000540 if (!xfs_inode_hasattr(dp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 error = XFS_ERROR(ENOATTR);
542 goto out;
543 }
544 if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
545 ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
546 error = xfs_attr_shortform_remove(&args);
547 if (error) {
548 goto out;
549 }
550 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
551 error = xfs_attr_leaf_removename(&args);
552 } else {
553 error = xfs_attr_node_removename(&args);
554 }
555 if (error) {
556 goto out;
557 }
558
559 /*
560 * If this is a synchronous mount, make sure that the
561 * transaction goes to disk before returning to the user.
562 */
563 if (mp->m_flags & XFS_MOUNT_WSYNC) {
564 xfs_trans_set_sync(args.trans);
565 }
566
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000567 if ((flags & ATTR_KERNOTIME) == 0)
568 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 /*
571 * Commit the last in the sequence of transactions.
572 */
573 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000574 error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 xfs_iunlock(dp, XFS_ILOCK_EXCL);
576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 return(error);
578
579out:
580 if (args.trans)
581 xfs_trans_cancel(args.trans,
582 XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
583 xfs_iunlock(dp, XFS_ILOCK_EXCL);
584 return(error);
585}
586
Nathan Scottaa82daa2005-11-02 10:33:33 +1100587int
Christoph Hellwig993386c2007-08-28 16:12:30 +1000588xfs_attr_remove(
589 xfs_inode_t *dp,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100590 const unsigned char *name,
Christoph Hellwig993386c2007-08-28 16:12:30 +1000591 int flags)
Nathan Scottaa82daa2005-11-02 10:33:33 +1100592{
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000593 int error;
594 struct xfs_name xname;
Nathan Scottaa82daa2005-11-02 10:33:33 +1100595
596 XFS_STATS_INC(xs_attr_remove);
597
Nathan Scottaa82daa2005-11-02 10:33:33 +1100598 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
599 return (EIO);
600
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000601 error = xfs_attr_name_to_xname(&xname, name);
602 if (error)
603 return error;
604
Nathan Scottaa82daa2005-11-02 10:33:33 +1100605 xfs_ilock(dp, XFS_ILOCK_SHARED);
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +1000606 if (!xfs_inode_hasattr(dp)) {
Nathan Scottaa82daa2005-11-02 10:33:33 +1100607 xfs_iunlock(dp, XFS_ILOCK_SHARED);
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +1000608 return XFS_ERROR(ENOATTR);
Nathan Scottaa82daa2005-11-02 10:33:33 +1100609 }
610 xfs_iunlock(dp, XFS_ILOCK_SHARED);
611
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000612 return xfs_attr_remove_int(dp, &xname, flags);
Nathan Scottaa82daa2005-11-02 10:33:33 +1100613}
614
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000615int
Tim Shimmin726801b2006-09-28 11:01:37 +1000616xfs_attr_list_int(xfs_attr_list_context_t *context)
617{
618 int error;
619 xfs_inode_t *dp = context->dp;
620
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000621 XFS_STATS_INC(xs_attr_list);
622
623 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
624 return EIO;
625
626 xfs_ilock(dp, XFS_ILOCK_SHARED);
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000627
Tim Shimmin726801b2006-09-28 11:01:37 +1000628 /*
629 * Decide on what work routines to call based on the inode size.
630 */
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +1000631 if (!xfs_inode_hasattr(dp)) {
Tim Shimmin726801b2006-09-28 11:01:37 +1000632 error = 0;
633 } else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
634 error = xfs_attr_shortform_list(context);
635 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
636 error = xfs_attr_leaf_list(context);
637 } else {
638 error = xfs_attr_node_list(context);
639 }
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000640
641 xfs_iunlock(dp, XFS_ILOCK_SHARED);
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000642
Tim Shimmin726801b2006-09-28 11:01:37 +1000643 return error;
644}
645
646#define ATTR_ENTBASESIZE /* minimum bytes used by an attr */ \
647 (((struct attrlist_ent *) 0)->a_name - (char *) 0)
648#define ATTR_ENTSIZE(namelen) /* actual bytes used by an attr */ \
649 ((ATTR_ENTBASESIZE + (namelen) + 1 + sizeof(u_int32_t)-1) \
650 & ~(sizeof(u_int32_t)-1))
651
652/*
653 * Format an attribute and copy it out to the user's buffer.
654 * Take care to check values and protect against them changing later,
655 * we may be reading them directly out of a user buffer.
656 */
657/*ARGSUSED*/
658STATIC int
Dave Chinnera9273ca2010-01-20 10:47:48 +1100659xfs_attr_put_listent(
660 xfs_attr_list_context_t *context,
661 int flags,
662 unsigned char *name,
663 int namelen,
664 int valuelen,
665 unsigned char *value)
Tim Shimmin726801b2006-09-28 11:01:37 +1000666{
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000667 struct attrlist *alist = (struct attrlist *)context->alist;
Tim Shimmin726801b2006-09-28 11:01:37 +1000668 attrlist_ent_t *aep;
669 int arraytop;
670
671 ASSERT(!(context->flags & ATTR_KERNOVAL));
672 ASSERT(context->count >= 0);
673 ASSERT(context->count < (ATTR_MAX_VALUELEN/8));
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000674 ASSERT(context->firstu >= sizeof(*alist));
Tim Shimmin726801b2006-09-28 11:01:37 +1000675 ASSERT(context->firstu <= context->bufsize);
676
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000677 /*
678 * Only list entries in the right namespace.
679 */
680 if (((context->flags & ATTR_SECURE) == 0) !=
681 ((flags & XFS_ATTR_SECURE) == 0))
682 return 0;
683 if (((context->flags & ATTR_ROOT) == 0) !=
684 ((flags & XFS_ATTR_ROOT) == 0))
685 return 0;
686
687 arraytop = sizeof(*alist) +
688 context->count * sizeof(alist->al_offset[0]);
Tim Shimmin726801b2006-09-28 11:01:37 +1000689 context->firstu -= ATTR_ENTSIZE(namelen);
690 if (context->firstu < arraytop) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000691 trace_xfs_attr_list_full(context);
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000692 alist->al_more = 1;
Tim Shimmin726801b2006-09-28 11:01:37 +1000693 context->seen_enough = 1;
694 return 1;
695 }
696
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000697 aep = (attrlist_ent_t *)&context->alist[context->firstu];
Tim Shimmin726801b2006-09-28 11:01:37 +1000698 aep->a_valuelen = valuelen;
699 memcpy(aep->a_name, name, namelen);
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000700 aep->a_name[namelen] = 0;
701 alist->al_offset[context->count++] = context->firstu;
702 alist->al_count = context->count;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000703 trace_xfs_attr_list_add(context);
Tim Shimmin726801b2006-09-28 11:01:37 +1000704 return 0;
705}
706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707/*
708 * Generate a list of extended attribute names and optionally
709 * also value lengths. Positive return value follows the XFS
710 * convention of being an error, zero or negative return code
711 * is the length of the buffer returned (negated), indicating
712 * success.
713 */
714int
Christoph Hellwig993386c2007-08-28 16:12:30 +1000715xfs_attr_list(
716 xfs_inode_t *dp,
717 char *buffer,
718 int bufsize,
719 int flags,
720 attrlist_cursor_kern_t *cursor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
722 xfs_attr_list_context_t context;
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000723 struct attrlist *alist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 int error;
725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 /*
727 * Validate the cursor.
728 */
729 if (cursor->pad1 || cursor->pad2)
730 return(XFS_ERROR(EINVAL));
731 if ((cursor->initted == 0) &&
732 (cursor->hashval || cursor->blkno || cursor->offset))
Tim Shimmin726801b2006-09-28 11:01:37 +1000733 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
735 /*
736 * Check for a properly aligned buffer.
737 */
738 if (((long)buffer) & (sizeof(int)-1))
Tim Shimmin726801b2006-09-28 11:01:37 +1000739 return XFS_ERROR(EFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 if (flags & ATTR_KERNOVAL)
741 bufsize = 0;
742
743 /*
744 * Initialize the output buffer.
745 */
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000746 memset(&context, 0, sizeof(context));
Christoph Hellwig993386c2007-08-28 16:12:30 +1000747 context.dp = dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 context.cursor = cursor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 context.resynch = 1;
750 context.flags = flags;
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000751 context.alist = buffer;
752 context.bufsize = (bufsize & ~(sizeof(int)-1)); /* align */
753 context.firstu = context.bufsize;
754 context.put_listent = xfs_attr_put_listent;
Tim Shimmin726801b2006-09-28 11:01:37 +1000755
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000756 alist = (struct attrlist *)context.alist;
757 alist->al_count = 0;
758 alist->al_more = 0;
759 alist->al_offset[0] = context.bufsize;
Tim Shimmin726801b2006-09-28 11:01:37 +1000760
761 error = xfs_attr_list_int(&context);
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000762 ASSERT(error >= 0);
Tim Shimmin726801b2006-09-28 11:01:37 +1000763 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
765
766int /* error */
767xfs_attr_inactive(xfs_inode_t *dp)
768{
769 xfs_trans_t *trans;
770 xfs_mount_t *mp;
771 int error;
772
773 mp = dp->i_mount;
774 ASSERT(! XFS_NOT_DQATTACHED(mp, dp));
775
776 xfs_ilock(dp, XFS_ILOCK_SHARED);
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +1000777 if (!xfs_inode_hasattr(dp) ||
778 dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 xfs_iunlock(dp, XFS_ILOCK_SHARED);
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +1000780 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 }
782 xfs_iunlock(dp, XFS_ILOCK_SHARED);
783
784 /*
785 * Start our first transaction of the day.
786 *
787 * All future transactions during this code must be "chained" off
788 * this one via the trans_dup() call. All transactions will contain
789 * the inode, and the inode will always be marked with trans_ihold().
790 * Since the inode will be locked in all transactions, we must log
791 * the inode in every transaction to let it float upward through
792 * the log.
793 */
794 trans = xfs_trans_alloc(mp, XFS_TRANS_ATTRINVAL);
795 if ((error = xfs_trans_reserve(trans, 0, XFS_ATTRINVAL_LOG_RES(mp), 0,
796 XFS_TRANS_PERM_LOG_RES,
797 XFS_ATTRINVAL_LOG_COUNT))) {
798 xfs_trans_cancel(trans, 0);
799 return(error);
800 }
801 xfs_ilock(dp, XFS_ILOCK_EXCL);
802
803 /*
804 * No need to make quota reservations here. We expect to release some
805 * blocks, not allocate, in the common case.
806 */
Christoph Hellwigddc34152011-09-19 15:00:54 +0000807 xfs_trans_ijoin(trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809 /*
810 * Decide on what work routines to call based on the inode size.
811 */
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +1000812 if (!xfs_inode_hasattr(dp) ||
813 dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 error = 0;
815 goto out;
816 }
Dave Chinner517c2222013-04-24 18:58:55 +1000817 error = xfs_attr3_root_inactive(&trans, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 if (error)
819 goto out;
Christoph Hellwig8f04c472011-07-08 14:34:34 +0200820
Christoph Hellwig8f04c472011-07-08 14:34:34 +0200821 error = xfs_itruncate_extents(&trans, dp, XFS_ATTR_FORK, 0);
822 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 goto out;
824
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000825 error = xfs_trans_commit(trans, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 xfs_iunlock(dp, XFS_ILOCK_EXCL);
827
828 return(error);
829
830out:
831 xfs_trans_cancel(trans, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
832 xfs_iunlock(dp, XFS_ILOCK_EXCL);
833 return(error);
834}
835
836
837
838/*========================================================================
839 * External routines when attribute list is inside the inode
840 *========================================================================*/
841
842/*
843 * Add a name to the shortform attribute list structure
844 * This is the external routine.
845 */
846STATIC int
847xfs_attr_shortform_addname(xfs_da_args_t *args)
848{
Nathan Scottd8cc8902005-11-02 10:34:53 +1100849 int newsize, forkoff, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Dave Chinner5a5881c2012-03-22 05:15:13 +0000851 trace_xfs_attr_sf_addname(args);
852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 retval = xfs_attr_shortform_lookup(args);
854 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
855 return(retval);
856 } else if (retval == EEXIST) {
857 if (args->flags & ATTR_CREATE)
858 return(retval);
859 retval = xfs_attr_shortform_remove(args);
860 ASSERT(retval == 0);
861 }
862
Nathan Scottd8cc8902005-11-02 10:34:53 +1100863 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
864 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
865 return(XFS_ERROR(ENOSPC));
866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
868 newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100869
870 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
871 if (!forkoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 return(XFS_ERROR(ENOSPC));
Nathan Scottd8cc8902005-11-02 10:34:53 +1100873
874 xfs_attr_shortform_add(args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 return(0);
876}
877
878
879/*========================================================================
880 * External routines when attribute list is one block
881 *========================================================================*/
882
883/*
884 * Add a name to the leaf attribute list structure
885 *
886 * This leaf block cannot have a "remote" value, we only call this routine
887 * if bmap_one_block() says there is only one block (ie: no remote blks).
888 */
David Chinnera8272ce2007-11-23 16:28:09 +1100889STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890xfs_attr_leaf_addname(xfs_da_args_t *args)
891{
892 xfs_inode_t *dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000893 struct xfs_buf *bp;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100894 int retval, error, committed, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Dave Chinner5a5881c2012-03-22 05:15:13 +0000896 trace_xfs_attr_leaf_addname(args);
897
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 /*
899 * Read the (only) block in the attribute list in.
900 */
901 dp = args->dp;
902 args->blkno = 0;
Dave Chinner517c2222013-04-24 18:58:55 +1000903 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100905 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
907 /*
908 * Look up the given attribute in the leaf block. Figure out if
909 * the given flags produce an error or call for an atomic rename.
910 */
Dave Chinner517c2222013-04-24 18:58:55 +1000911 retval = xfs_attr3_leaf_lookup_int(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000913 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000914 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 } else if (retval == EEXIST) {
916 if (args->flags & ATTR_CREATE) { /* pure create op */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000917 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000918 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 }
Dave Chinner5a5881c2012-03-22 05:15:13 +0000920
921 trace_xfs_attr_leaf_replace(args);
922
Barry Naujok6a178102008-05-21 16:42:05 +1000923 args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 args->blkno2 = args->blkno; /* set 2nd entry info*/
925 args->index2 = args->index;
926 args->rmtblkno2 = args->rmtblkno;
927 args->rmtblkcnt2 = args->rmtblkcnt;
928 }
929
930 /*
931 * Add the attribute to the leaf block, transitioning to a Btree
932 * if required.
933 */
Dave Chinner517c2222013-04-24 18:58:55 +1000934 retval = xfs_attr3_leaf_add(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 if (retval == ENOSPC) {
936 /*
937 * Promote the attribute list to the Btree format, then
938 * Commit that transaction so that the node_addname() call
939 * can manage its own transactions.
940 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600941 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000942 error = xfs_attr3_leaf_to_node(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 if (!error) {
944 error = xfs_bmap_finish(&args->trans, args->flist,
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100945 &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 }
947 if (error) {
948 ASSERT(committed);
949 args->trans = NULL;
950 xfs_bmap_cancel(args->flist);
951 return(error);
952 }
953
954 /*
955 * bmap_finish() may have committed the last trans and started
956 * a new one. We need the inode to be in all transactions.
957 */
Christoph Hellwig898621d2010-06-24 11:36:58 +1000958 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +0000959 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961 /*
962 * Commit the current trans (including the inode) and start
963 * a new one.
964 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000965 error = xfs_trans_roll(&args->trans, dp);
966 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 return (error);
968
969 /*
970 * Fob the whole rest of the problem off on the Btree code.
971 */
972 error = xfs_attr_node_addname(args);
973 return(error);
974 }
975
976 /*
977 * Commit the transaction that added the attr name so that
978 * later routines can manage their own transactions.
979 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000980 error = xfs_trans_roll(&args->trans, dp);
981 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 return (error);
983
984 /*
985 * If there was an out-of-line value, allocate the blocks we
986 * identified for its storage and copy the value. This is done
987 * after we create the attribute so that we don't overflow the
988 * maximum size of a transaction and/or hit a deadlock.
989 */
990 if (args->rmtblkno > 0) {
991 error = xfs_attr_rmtval_set(args);
992 if (error)
993 return(error);
994 }
995
996 /*
997 * If this is an atomic rename operation, we must "flip" the
998 * incomplete flags on the "new" and "old" attribute/value pairs
999 * so that one disappears and one appears atomically. Then we
1000 * must remove the "old" attribute/value pair.
1001 */
Barry Naujok6a178102008-05-21 16:42:05 +10001002 if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 /*
1004 * In a separate transaction, set the incomplete flag on the
1005 * "old" attr and clear the incomplete flag on the "new" attr.
1006 */
Dave Chinner517c2222013-04-24 18:58:55 +10001007 error = xfs_attr3_leaf_flipflags(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 if (error)
1009 return(error);
1010
1011 /*
1012 * Dismantle the "old" attribute/value pair by removing
1013 * a "remote" value (if it exists).
1014 */
1015 args->index = args->index2;
1016 args->blkno = args->blkno2;
1017 args->rmtblkno = args->rmtblkno2;
1018 args->rmtblkcnt = args->rmtblkcnt2;
1019 if (args->rmtblkno) {
1020 error = xfs_attr_rmtval_remove(args);
1021 if (error)
1022 return(error);
1023 }
1024
1025 /*
1026 * Read in the block containing the "old" attr, then
1027 * remove the "old" attr from that block (neat, huh!)
1028 */
Dave Chinner517c2222013-04-24 18:58:55 +10001029 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
Dave Chinnerad14c332012-11-12 22:54:16 +11001030 -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +11001032 return error;
1033
Dave Chinner517c2222013-04-24 18:58:55 +10001034 xfs_attr3_leaf_remove(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
1036 /*
1037 * If the result is small enough, shrink it all into the inode.
1038 */
Nathan Scottd8cc8902005-11-02 10:34:53 +11001039 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001040 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +10001041 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 /* bp is gone due to xfs_da_shrink_inode */
1043 if (!error) {
1044 error = xfs_bmap_finish(&args->trans,
1045 args->flist,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 &committed);
1047 }
1048 if (error) {
1049 ASSERT(committed);
1050 args->trans = NULL;
1051 xfs_bmap_cancel(args->flist);
1052 return(error);
1053 }
1054
1055 /*
1056 * bmap_finish() may have committed the last trans
1057 * and started a new one. We need the inode to be
1058 * in all transactions.
1059 */
Christoph Hellwig898621d2010-06-24 11:36:58 +10001060 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +00001061 xfs_trans_ijoin(args->trans, dp, 0);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
1064 /*
1065 * Commit the remove and start the next trans in series.
1066 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10001067 error = xfs_trans_roll(&args->trans, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
1069 } else if (args->rmtblkno > 0) {
1070 /*
1071 * Added a "remote" value, just clear the incomplete flag.
1072 */
Dave Chinner517c2222013-04-24 18:58:55 +10001073 error = xfs_attr3_leaf_clearflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 }
Dave Chinner517c2222013-04-24 18:58:55 +10001075 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076}
1077
1078/*
1079 * Remove a name from the leaf attribute list structure
1080 *
1081 * This leaf block cannot have a "remote" value, we only call this routine
1082 * if bmap_one_block() says there is only one block (ie: no remote blks).
1083 */
1084STATIC int
1085xfs_attr_leaf_removename(xfs_da_args_t *args)
1086{
1087 xfs_inode_t *dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001088 struct xfs_buf *bp;
Nathan Scottd8cc8902005-11-02 10:34:53 +11001089 int error, committed, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Dave Chinner5a5881c2012-03-22 05:15:13 +00001091 trace_xfs_attr_leaf_removename(args);
1092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 /*
1094 * Remove the attribute.
1095 */
1096 dp = args->dp;
1097 args->blkno = 0;
Dave Chinner517c2222013-04-24 18:58:55 +10001098 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Dave Chinnerad14c332012-11-12 22:54:16 +11001099 if (error)
1100 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
Dave Chinner517c2222013-04-24 18:58:55 +10001102 error = xfs_attr3_leaf_lookup_int(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 if (error == ENOATTR) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001104 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +10001105 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 }
1107
Dave Chinner517c2222013-04-24 18:58:55 +10001108 xfs_attr3_leaf_remove(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
1110 /*
1111 * If the result is small enough, shrink it all into the inode.
1112 */
Nathan Scottd8cc8902005-11-02 10:34:53 +11001113 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001114 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +10001115 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 /* bp is gone due to xfs_da_shrink_inode */
1117 if (!error) {
1118 error = xfs_bmap_finish(&args->trans, args->flist,
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001119 &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 }
1121 if (error) {
1122 ASSERT(committed);
1123 args->trans = NULL;
1124 xfs_bmap_cancel(args->flist);
Dave Chinner517c2222013-04-24 18:58:55 +10001125 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 }
1127
1128 /*
1129 * bmap_finish() may have committed the last trans and started
1130 * a new one. We need the inode to be in all transactions.
1131 */
Christoph Hellwig898621d2010-06-24 11:36:58 +10001132 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +00001133 xfs_trans_ijoin(args->trans, dp, 0);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001134 }
Dave Chinner517c2222013-04-24 18:58:55 +10001135 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136}
1137
1138/*
1139 * Look up a name in a leaf attribute list structure.
1140 *
1141 * This leaf block cannot have a "remote" value, we only call this routine
1142 * if bmap_one_block() says there is only one block (ie: no remote blks).
1143 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001144STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145xfs_attr_leaf_get(xfs_da_args_t *args)
1146{
Dave Chinner1d9025e2012-06-22 18:50:14 +10001147 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 int error;
1149
Dave Chinneree732592012-11-12 22:53:53 +11001150 trace_xfs_attr_leaf_get(args);
1151
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 args->blkno = 0;
Dave Chinner517c2222013-04-24 18:58:55 +10001153 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +11001155 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
Dave Chinner517c2222013-04-24 18:58:55 +10001157 error = xfs_attr3_leaf_lookup_int(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 if (error != EEXIST) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001159 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +10001160 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 }
Dave Chinner517c2222013-04-24 18:58:55 +10001162 error = xfs_attr3_leaf_getvalue(bp, args);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001163 xfs_trans_brelse(args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
1165 error = xfs_attr_rmtval_get(args);
1166 }
Dave Chinner517c2222013-04-24 18:58:55 +10001167 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168}
1169
1170/*
1171 * Copy out attribute entries for attr_list(), for leaf attribute lists.
1172 */
1173STATIC int
1174xfs_attr_leaf_list(xfs_attr_list_context_t *context)
1175{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 int error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001177 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Dave Chinneree732592012-11-12 22:53:53 +11001179 trace_xfs_attr_leaf_list(context);
1180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 context->cursor->blkno = 0;
Dave Chinner517c2222013-04-24 18:58:55 +10001182 error = xfs_attr3_leaf_read(NULL, context->dp, 0, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 if (error)
Tim Shimmin726801b2006-09-28 11:01:37 +10001184 return XFS_ERROR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Dave Chinner517c2222013-04-24 18:58:55 +10001186 error = xfs_attr3_leaf_list_int(bp, context);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001187 xfs_trans_brelse(NULL, bp);
Tim Shimmin726801b2006-09-28 11:01:37 +10001188 return XFS_ERROR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189}
1190
1191
1192/*========================================================================
1193 * External routines when attribute list size > XFS_LBSIZE(mp).
1194 *========================================================================*/
1195
1196/*
1197 * Add a name to a Btree-format attribute list.
1198 *
1199 * This will involve walking down the Btree, and may involve splitting
1200 * leaf nodes and even splitting intermediate nodes up to and including
1201 * the root node (a special case of an intermediate node).
1202 *
1203 * "Remote" attribute values confuse the issue and atomic rename operations
1204 * add a whole extra layer of confusion on top of that.
1205 */
1206STATIC int
1207xfs_attr_node_addname(xfs_da_args_t *args)
1208{
1209 xfs_da_state_t *state;
1210 xfs_da_state_blk_t *blk;
1211 xfs_inode_t *dp;
1212 xfs_mount_t *mp;
1213 int committed, retval, error;
1214
Dave Chinner5a5881c2012-03-22 05:15:13 +00001215 trace_xfs_attr_node_addname(args);
1216
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 /*
1218 * Fill in bucket of arguments/results/context to carry around.
1219 */
1220 dp = args->dp;
1221 mp = dp->i_mount;
1222restart:
1223 state = xfs_da_state_alloc();
1224 state->args = args;
1225 state->mp = mp;
1226 state->blocksize = state->mp->m_sb.sb_blocksize;
1227 state->node_ents = state->mp->m_attr_node_ents;
1228
1229 /*
1230 * Search to see if name already exists, and get back a pointer
1231 * to where it should go.
1232 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001233 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 if (error)
1235 goto out;
1236 blk = &state->path.blk[ state->path.active-1 ];
1237 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1238 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
1239 goto out;
1240 } else if (retval == EEXIST) {
1241 if (args->flags & ATTR_CREATE)
1242 goto out;
Dave Chinner5a5881c2012-03-22 05:15:13 +00001243
1244 trace_xfs_attr_node_replace(args);
1245
Barry Naujok6a178102008-05-21 16:42:05 +10001246 args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 args->blkno2 = args->blkno; /* set 2nd entry info*/
1248 args->index2 = args->index;
1249 args->rmtblkno2 = args->rmtblkno;
1250 args->rmtblkcnt2 = args->rmtblkcnt;
1251 args->rmtblkno = 0;
1252 args->rmtblkcnt = 0;
1253 }
1254
Dave Chinner517c2222013-04-24 18:58:55 +10001255 retval = xfs_attr3_leaf_add(blk->bp, state->args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 if (retval == ENOSPC) {
1257 if (state->path.active == 1) {
1258 /*
1259 * Its really a single leaf node, but it had
1260 * out-of-line values so it looked like it *might*
1261 * have been a b-tree.
1262 */
1263 xfs_da_state_free(state);
Eric Sandeen9d87c312009-01-14 23:22:07 -06001264 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +10001265 error = xfs_attr3_leaf_to_node(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 if (!error) {
1267 error = xfs_bmap_finish(&args->trans,
1268 args->flist,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 &committed);
1270 }
1271 if (error) {
1272 ASSERT(committed);
1273 args->trans = NULL;
1274 xfs_bmap_cancel(args->flist);
1275 goto out;
1276 }
1277
1278 /*
1279 * bmap_finish() may have committed the last trans
1280 * and started a new one. We need the inode to be
1281 * in all transactions.
1282 */
Christoph Hellwig898621d2010-06-24 11:36:58 +10001283 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +00001284 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286 /*
1287 * Commit the node conversion and start the next
1288 * trans in the chain.
1289 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10001290 error = xfs_trans_roll(&args->trans, dp);
1291 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 goto out;
1293
1294 goto restart;
1295 }
1296
1297 /*
1298 * Split as many Btree elements as required.
1299 * This code tracks the new and old attr's location
1300 * in the index/blkno/rmtblkno/rmtblkcnt fields and
1301 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
1302 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06001303 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001304 error = xfs_da3_split(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 if (!error) {
1306 error = xfs_bmap_finish(&args->trans, args->flist,
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001307 &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 }
1309 if (error) {
1310 ASSERT(committed);
1311 args->trans = NULL;
1312 xfs_bmap_cancel(args->flist);
1313 goto out;
1314 }
1315
1316 /*
1317 * bmap_finish() may have committed the last trans and started
1318 * a new one. We need the inode to be in all transactions.
1319 */
Christoph Hellwig898621d2010-06-24 11:36:58 +10001320 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +00001321 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 } else {
1323 /*
1324 * Addition succeeded, update Btree hashvals.
1325 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001326 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 }
1328
1329 /*
1330 * Kill the state structure, we're done with it and need to
1331 * allow the buffers to come back later.
1332 */
1333 xfs_da_state_free(state);
1334 state = NULL;
1335
1336 /*
1337 * Commit the leaf addition or btree split and start the next
1338 * trans in the chain.
1339 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10001340 error = xfs_trans_roll(&args->trans, dp);
1341 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 goto out;
1343
1344 /*
1345 * If there was an out-of-line value, allocate the blocks we
1346 * identified for its storage and copy the value. This is done
1347 * after we create the attribute so that we don't overflow the
1348 * maximum size of a transaction and/or hit a deadlock.
1349 */
1350 if (args->rmtblkno > 0) {
1351 error = xfs_attr_rmtval_set(args);
1352 if (error)
1353 return(error);
1354 }
1355
1356 /*
1357 * If this is an atomic rename operation, we must "flip" the
1358 * incomplete flags on the "new" and "old" attribute/value pairs
1359 * so that one disappears and one appears atomically. Then we
1360 * must remove the "old" attribute/value pair.
1361 */
Barry Naujok6a178102008-05-21 16:42:05 +10001362 if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 /*
1364 * In a separate transaction, set the incomplete flag on the
1365 * "old" attr and clear the incomplete flag on the "new" attr.
1366 */
Dave Chinner517c2222013-04-24 18:58:55 +10001367 error = xfs_attr3_leaf_flipflags(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 if (error)
1369 goto out;
1370
1371 /*
1372 * Dismantle the "old" attribute/value pair by removing
1373 * a "remote" value (if it exists).
1374 */
1375 args->index = args->index2;
1376 args->blkno = args->blkno2;
1377 args->rmtblkno = args->rmtblkno2;
1378 args->rmtblkcnt = args->rmtblkcnt2;
1379 if (args->rmtblkno) {
1380 error = xfs_attr_rmtval_remove(args);
1381 if (error)
1382 return(error);
1383 }
1384
1385 /*
1386 * Re-find the "old" attribute entry after any split ops.
1387 * The INCOMPLETE flag means that we will find the "old"
1388 * attr, not the "new" one.
1389 */
1390 args->flags |= XFS_ATTR_INCOMPLETE;
1391 state = xfs_da_state_alloc();
1392 state->args = args;
1393 state->mp = mp;
1394 state->blocksize = state->mp->m_sb.sb_blocksize;
1395 state->node_ents = state->mp->m_attr_node_ents;
1396 state->inleaf = 0;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001397 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 if (error)
1399 goto out;
1400
1401 /*
1402 * Remove the name and update the hashvals in the tree.
1403 */
1404 blk = &state->path.blk[ state->path.active-1 ];
1405 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner517c2222013-04-24 18:58:55 +10001406 error = xfs_attr3_leaf_remove(blk->bp, args);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001407 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
1409 /*
1410 * Check to see if the tree needs to be collapsed.
1411 */
1412 if (retval && (state->path.active > 1)) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001413 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001414 error = xfs_da3_join(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 if (!error) {
1416 error = xfs_bmap_finish(&args->trans,
1417 args->flist,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 &committed);
1419 }
1420 if (error) {
1421 ASSERT(committed);
1422 args->trans = NULL;
1423 xfs_bmap_cancel(args->flist);
1424 goto out;
1425 }
1426
1427 /*
1428 * bmap_finish() may have committed the last trans
1429 * and started a new one. We need the inode to be
1430 * in all transactions.
1431 */
Christoph Hellwig898621d2010-06-24 11:36:58 +10001432 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +00001433 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 }
1435
1436 /*
1437 * Commit and start the next trans in the chain.
1438 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10001439 error = xfs_trans_roll(&args->trans, dp);
1440 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 goto out;
1442
1443 } else if (args->rmtblkno > 0) {
1444 /*
1445 * Added a "remote" value, just clear the incomplete flag.
1446 */
Dave Chinner517c2222013-04-24 18:58:55 +10001447 error = xfs_attr3_leaf_clearflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 if (error)
1449 goto out;
1450 }
1451 retval = error = 0;
1452
1453out:
1454 if (state)
1455 xfs_da_state_free(state);
1456 if (error)
1457 return(error);
1458 return(retval);
1459}
1460
1461/*
1462 * Remove a name from a B-tree attribute list.
1463 *
1464 * This will involve walking down the Btree, and may involve joining
1465 * leaf nodes and even joining intermediate nodes up to and including
1466 * the root node (a special case of an intermediate node).
1467 */
1468STATIC int
1469xfs_attr_node_removename(xfs_da_args_t *args)
1470{
1471 xfs_da_state_t *state;
1472 xfs_da_state_blk_t *blk;
1473 xfs_inode_t *dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001474 struct xfs_buf *bp;
Nathan Scottd8cc8902005-11-02 10:34:53 +11001475 int retval, error, committed, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
Dave Chinner5a5881c2012-03-22 05:15:13 +00001477 trace_xfs_attr_node_removename(args);
1478
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 /*
1480 * Tie a string around our finger to remind us where we are.
1481 */
1482 dp = args->dp;
1483 state = xfs_da_state_alloc();
1484 state->args = args;
1485 state->mp = dp->i_mount;
1486 state->blocksize = state->mp->m_sb.sb_blocksize;
1487 state->node_ents = state->mp->m_attr_node_ents;
1488
1489 /*
1490 * Search to see if name exists, and get back a pointer to it.
1491 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001492 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 if (error || (retval != EEXIST)) {
1494 if (error == 0)
1495 error = retval;
1496 goto out;
1497 }
1498
1499 /*
1500 * If there is an out-of-line value, de-allocate the blocks.
1501 * This is done before we remove the attribute so that we don't
1502 * overflow the maximum size of a transaction and/or hit a deadlock.
1503 */
1504 blk = &state->path.blk[ state->path.active-1 ];
1505 ASSERT(blk->bp != NULL);
1506 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1507 if (args->rmtblkno > 0) {
1508 /*
1509 * Fill in disk block numbers in the state structure
1510 * so that we can get the buffers back after we commit
1511 * several transactions in the following calls.
1512 */
1513 error = xfs_attr_fillstate(state);
1514 if (error)
1515 goto out;
1516
1517 /*
1518 * Mark the attribute as INCOMPLETE, then bunmapi() the
1519 * remote value.
1520 */
Dave Chinner517c2222013-04-24 18:58:55 +10001521 error = xfs_attr3_leaf_setflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 if (error)
1523 goto out;
1524 error = xfs_attr_rmtval_remove(args);
1525 if (error)
1526 goto out;
1527
1528 /*
1529 * Refill the state structure with buffers, the prior calls
1530 * released our buffers.
1531 */
1532 error = xfs_attr_refillstate(state);
1533 if (error)
1534 goto out;
1535 }
1536
1537 /*
1538 * Remove the name and update the hashvals in the tree.
1539 */
1540 blk = &state->path.blk[ state->path.active-1 ];
1541 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner517c2222013-04-24 18:58:55 +10001542 retval = xfs_attr3_leaf_remove(blk->bp, args);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001543 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
1545 /*
1546 * Check to see if the tree needs to be collapsed.
1547 */
1548 if (retval && (state->path.active > 1)) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001549 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001550 error = xfs_da3_join(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 if (!error) {
1552 error = xfs_bmap_finish(&args->trans, args->flist,
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001553 &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 }
1555 if (error) {
1556 ASSERT(committed);
1557 args->trans = NULL;
1558 xfs_bmap_cancel(args->flist);
1559 goto out;
1560 }
1561
1562 /*
1563 * bmap_finish() may have committed the last trans and started
1564 * a new one. We need the inode to be in all transactions.
1565 */
Christoph Hellwig898621d2010-06-24 11:36:58 +10001566 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +00001567 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
1569 /*
1570 * Commit the Btree join operation and start a new trans.
1571 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10001572 error = xfs_trans_roll(&args->trans, dp);
1573 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 goto out;
1575 }
1576
1577 /*
1578 * If the result is small enough, push it all into the inode.
1579 */
1580 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
1581 /*
1582 * Have to get rid of the copy of this dabuf in the state.
1583 */
1584 ASSERT(state->path.active == 1);
1585 ASSERT(state->path.blk[0].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 state->path.blk[0].bp = NULL;
1587
Dave Chinner517c2222013-04-24 18:58:55 +10001588 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 if (error)
1590 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
Nathan Scottd8cc8902005-11-02 10:34:53 +11001592 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001593 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +10001594 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 /* bp is gone due to xfs_da_shrink_inode */
1596 if (!error) {
1597 error = xfs_bmap_finish(&args->trans,
1598 args->flist,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 &committed);
1600 }
1601 if (error) {
1602 ASSERT(committed);
1603 args->trans = NULL;
1604 xfs_bmap_cancel(args->flist);
1605 goto out;
1606 }
1607
1608 /*
1609 * bmap_finish() may have committed the last trans
1610 * and started a new one. We need the inode to be
1611 * in all transactions.
1612 */
Christoph Hellwig898621d2010-06-24 11:36:58 +10001613 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +00001614 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 } else
Dave Chinner1d9025e2012-06-22 18:50:14 +10001616 xfs_trans_brelse(args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 }
1618 error = 0;
1619
1620out:
1621 xfs_da_state_free(state);
1622 return(error);
1623}
1624
1625/*
1626 * Fill in the disk block numbers in the state structure for the buffers
1627 * that are attached to the state structure.
1628 * This is done so that we can quickly reattach ourselves to those buffers
Nathan Scottc41564b2006-03-29 08:55:14 +10001629 * after some set of transaction commits have released these buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 */
1631STATIC int
1632xfs_attr_fillstate(xfs_da_state_t *state)
1633{
1634 xfs_da_state_path_t *path;
1635 xfs_da_state_blk_t *blk;
1636 int level;
1637
Dave Chinneree732592012-11-12 22:53:53 +11001638 trace_xfs_attr_fillstate(state->args);
1639
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 /*
1641 * Roll down the "path" in the state structure, storing the on-disk
1642 * block number for those buffers in the "path".
1643 */
1644 path = &state->path;
1645 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1646 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1647 if (blk->bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001648 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 blk->bp = NULL;
1650 } else {
1651 blk->disk_blkno = 0;
1652 }
1653 }
1654
1655 /*
1656 * Roll down the "altpath" in the state structure, storing the on-disk
1657 * block number for those buffers in the "altpath".
1658 */
1659 path = &state->altpath;
1660 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1661 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1662 if (blk->bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001663 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 blk->bp = NULL;
1665 } else {
1666 blk->disk_blkno = 0;
1667 }
1668 }
1669
1670 return(0);
1671}
1672
1673/*
1674 * Reattach the buffers to the state structure based on the disk block
1675 * numbers stored in the state structure.
Nathan Scottc41564b2006-03-29 08:55:14 +10001676 * This is done after some set of transaction commits have released those
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 * buffers from our grip.
1678 */
1679STATIC int
1680xfs_attr_refillstate(xfs_da_state_t *state)
1681{
1682 xfs_da_state_path_t *path;
1683 xfs_da_state_blk_t *blk;
1684 int level, error;
1685
Dave Chinneree732592012-11-12 22:53:53 +11001686 trace_xfs_attr_refillstate(state->args);
1687
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 /*
1689 * Roll down the "path" in the state structure, storing the on-disk
1690 * block number for those buffers in the "path".
1691 */
1692 path = &state->path;
1693 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1694 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1695 if (blk->disk_blkno) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001696 error = xfs_da3_node_read(state->args->trans,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 state->args->dp,
1698 blk->blkno, blk->disk_blkno,
Dave Chinnerd9392a42012-11-12 22:54:17 +11001699 &blk->bp, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 if (error)
1701 return(error);
1702 } else {
1703 blk->bp = NULL;
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->disk_blkno) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001715 error = xfs_da3_node_read(state->args->trans,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 state->args->dp,
1717 blk->blkno, blk->disk_blkno,
Dave Chinnerd9392a42012-11-12 22:54:17 +11001718 &blk->bp, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 if (error)
1720 return(error);
1721 } else {
1722 blk->bp = NULL;
1723 }
1724 }
1725
1726 return(0);
1727}
1728
1729/*
1730 * Look up a filename in a node attribute list.
1731 *
1732 * This routine gets called for any attribute fork that has more than one
1733 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1734 * "remote" values taking up more blocks.
1735 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001736STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737xfs_attr_node_get(xfs_da_args_t *args)
1738{
1739 xfs_da_state_t *state;
1740 xfs_da_state_blk_t *blk;
1741 int error, retval;
1742 int i;
1743
Dave Chinneree732592012-11-12 22:53:53 +11001744 trace_xfs_attr_node_get(args);
1745
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 state = xfs_da_state_alloc();
1747 state->args = args;
1748 state->mp = args->dp->i_mount;
1749 state->blocksize = state->mp->m_sb.sb_blocksize;
1750 state->node_ents = state->mp->m_attr_node_ents;
1751
1752 /*
1753 * Search to see if name exists, and get back a pointer to it.
1754 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001755 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 if (error) {
1757 retval = error;
1758 } else if (retval == EEXIST) {
1759 blk = &state->path.blk[ state->path.active-1 ];
1760 ASSERT(blk->bp != NULL);
1761 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1762
1763 /*
1764 * Get the value, local or "remote"
1765 */
Dave Chinner517c2222013-04-24 18:58:55 +10001766 retval = xfs_attr3_leaf_getvalue(blk->bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 if (!retval && (args->rmtblkno > 0)
1768 && !(args->flags & ATTR_KERNOVAL)) {
1769 retval = xfs_attr_rmtval_get(args);
1770 }
1771 }
1772
1773 /*
1774 * If not in a transaction, we have to release all the buffers.
1775 */
1776 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001777 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 state->path.blk[i].bp = NULL;
1779 }
1780
1781 xfs_da_state_free(state);
1782 return(retval);
1783}
1784
1785STATIC int /* error */
1786xfs_attr_node_list(xfs_attr_list_context_t *context)
1787{
1788 attrlist_cursor_kern_t *cursor;
1789 xfs_attr_leafblock_t *leaf;
1790 xfs_da_intnode_t *node;
Dave Chinner517c2222013-04-24 18:58:55 +10001791 struct xfs_attr3_icleaf_hdr leafhdr;
1792 struct xfs_da3_icnode_hdr nodehdr;
1793 struct xfs_da_node_entry *btree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 int error, i;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001795 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
Dave Chinneree732592012-11-12 22:53:53 +11001797 trace_xfs_attr_node_list(context);
1798
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 cursor = context->cursor;
1800 cursor->initted = 1;
1801
1802 /*
1803 * Do all sorts of validation on the passed-in cursor structure.
1804 * If anything is amiss, ignore the cursor and look up the hashval
1805 * starting from the btree root.
1806 */
1807 bp = NULL;
1808 if (cursor->blkno > 0) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001809 error = xfs_da3_node_read(NULL, context->dp, cursor->blkno, -1,
Dave Chinnerd9392a42012-11-12 22:54:17 +11001810 &bp, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 if ((error != 0) && (error != EFSCORRUPTED))
1812 return(error);
1813 if (bp) {
Dave Chinner517c2222013-04-24 18:58:55 +10001814 struct xfs_attr_leaf_entry *entries;
1815
Dave Chinner1d9025e2012-06-22 18:50:14 +10001816 node = bp->b_addr;
Nathan Scott89da0542006-03-17 17:28:40 +11001817 switch (be16_to_cpu(node->hdr.info.magic)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 case XFS_DA_NODE_MAGIC:
Dave Chinner517c2222013-04-24 18:58:55 +10001819 case XFS_DA3_NODE_MAGIC:
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001820 trace_xfs_attr_list_wrong_blk(context);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001821 xfs_trans_brelse(NULL, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 bp = NULL;
1823 break;
1824 case XFS_ATTR_LEAF_MAGIC:
Dave Chinner517c2222013-04-24 18:58:55 +10001825 case XFS_ATTR3_LEAF_MAGIC:
Dave Chinner1d9025e2012-06-22 18:50:14 +10001826 leaf = bp->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10001827 xfs_attr3_leaf_hdr_from_disk(&leafhdr, leaf);
1828 entries = xfs_attr3_leaf_entryp(leaf);
1829 if (cursor->hashval > be32_to_cpu(
1830 entries[leafhdr.count - 1].hashval)) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001831 trace_xfs_attr_list_wrong_blk(context);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001832 xfs_trans_brelse(NULL, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 bp = NULL;
Dave Chinner517c2222013-04-24 18:58:55 +10001834 } else if (cursor->hashval <= be32_to_cpu(
1835 entries[0].hashval)) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001836 trace_xfs_attr_list_wrong_blk(context);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001837 xfs_trans_brelse(NULL, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 bp = NULL;
1839 }
1840 break;
1841 default:
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001842 trace_xfs_attr_list_wrong_blk(context);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001843 xfs_trans_brelse(NULL, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 bp = NULL;
1845 }
1846 }
1847 }
1848
1849 /*
1850 * We did not find what we expected given the cursor's contents,
1851 * so we start from the top and work down based on the hash value.
1852 * Note that start of node block is same as start of leaf block.
1853 */
1854 if (bp == NULL) {
1855 cursor->blkno = 0;
1856 for (;;) {
Dave Chinner517c2222013-04-24 18:58:55 +10001857 __uint16_t magic;
1858
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001859 error = xfs_da3_node_read(NULL, context->dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 cursor->blkno, -1, &bp,
Dave Chinnerd9392a42012-11-12 22:54:17 +11001861 XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 if (error)
1863 return(error);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001864 node = bp->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10001865 magic = be16_to_cpu(node->hdr.info.magic);
1866 if (magic == XFS_ATTR_LEAF_MAGIC ||
1867 magic == XFS_ATTR3_LEAF_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 break;
Dave Chinner517c2222013-04-24 18:58:55 +10001869 if (magic != XFS_DA_NODE_MAGIC &&
1870 magic != XFS_DA3_NODE_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 XFS_CORRUPTION_ERROR("xfs_attr_node_list(3)",
1872 XFS_ERRLEVEL_LOW,
1873 context->dp->i_mount,
1874 node);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001875 xfs_trans_brelse(NULL, bp);
Dave Chinner517c2222013-04-24 18:58:55 +10001876 return XFS_ERROR(EFSCORRUPTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 }
Dave Chinner517c2222013-04-24 18:58:55 +10001878
1879 xfs_da3_node_hdr_from_disk(&nodehdr, node);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001880 btree = xfs_da3_node_tree_p(node);
Dave Chinner517c2222013-04-24 18:58:55 +10001881 for (i = 0; i < nodehdr.count; btree++, i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 if (cursor->hashval
Nathan Scott403432d2006-03-17 17:29:46 +11001883 <= be32_to_cpu(btree->hashval)) {
1884 cursor->blkno = be32_to_cpu(btree->before);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001885 trace_xfs_attr_list_node_descend(context,
1886 btree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 break;
1888 }
1889 }
Dave Chinner517c2222013-04-24 18:58:55 +10001890 if (i == nodehdr.count) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001891 xfs_trans_brelse(NULL, bp);
Dave Chinner517c2222013-04-24 18:58:55 +10001892 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001894 xfs_trans_brelse(NULL, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 }
1896 }
1897 ASSERT(bp != NULL);
1898
1899 /*
1900 * Roll upward through the blocks, processing each leaf block in
1901 * order. As long as there is space in the result buffer, keep
1902 * adding the information.
1903 */
1904 for (;;) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001905 leaf = bp->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10001906 error = xfs_attr3_leaf_list_int(bp, context);
Tim Shimmin726801b2006-09-28 11:01:37 +10001907 if (error) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001908 xfs_trans_brelse(NULL, bp);
Tim Shimmin726801b2006-09-28 11:01:37 +10001909 return error;
1910 }
Dave Chinner517c2222013-04-24 18:58:55 +10001911 xfs_attr3_leaf_hdr_from_disk(&leafhdr, leaf);
1912 if (context->seen_enough || leafhdr.forw == 0)
Tim Shimmin726801b2006-09-28 11:01:37 +10001913 break;
Dave Chinner517c2222013-04-24 18:58:55 +10001914 cursor->blkno = leafhdr.forw;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001915 xfs_trans_brelse(NULL, bp);
Dave Chinner517c2222013-04-24 18:58:55 +10001916 error = xfs_attr3_leaf_read(NULL, context->dp, cursor->blkno, -1,
Dave Chinnerad14c332012-11-12 22:54:16 +11001917 &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +11001919 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001921 xfs_trans_brelse(NULL, bp);
Dave Chinner517c2222013-04-24 18:58:55 +10001922 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923}