blob: 20fe3fe9d3417aabcd566ddad7719d3653ad4443 [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.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"
24#include "xfs_sb.h"
25#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110027#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110029#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_dinode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110032#include "xfs_alloc.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_attr.h"
36#include "xfs_attr_leaf.h"
Dave Chinner95920cd2013-04-03 16:11:27 +110037#include "xfs_attr_remote.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "xfs_quota.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include "xfs_trans_space.h"
Christoph Hellwig739bfb22007-08-29 10:58:01 +100041#include "xfs_vnodeops.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000042#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44/*
45 * xfs_attr.c
46 *
47 * Provide the external interfaces to manage attribute lists.
48 */
49
50/*========================================================================
51 * Function prototypes for the kernel.
52 *========================================================================*/
53
54/*
55 * Internal routines when attribute list fits inside the inode.
56 */
57STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
58
59/*
60 * Internal routines when attribute list is one block.
61 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100062STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063STATIC int xfs_attr_leaf_addname(xfs_da_args_t *args);
64STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
65STATIC int xfs_attr_leaf_list(xfs_attr_list_context_t *context);
66
67/*
68 * Internal routines when attribute list is more than one block.
69 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100070STATIC int xfs_attr_node_get(xfs_da_args_t *args);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071STATIC int xfs_attr_node_addname(xfs_da_args_t *args);
72STATIC int xfs_attr_node_removename(xfs_da_args_t *args);
73STATIC int xfs_attr_node_list(xfs_attr_list_context_t *context);
74STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
75STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Barry Naujoke8b0eba2008-04-22 17:34:31 +100078STATIC int
79xfs_attr_name_to_xname(
80 struct xfs_name *xname,
Dave Chinnera9273ca2010-01-20 10:47:48 +110081 const unsigned char *aname)
Barry Naujoke8b0eba2008-04-22 17:34:31 +100082{
83 if (!aname)
84 return EINVAL;
85 xname->name = aname;
Dave Chinnera9273ca2010-01-20 10:47:48 +110086 xname->len = strlen((char *)aname);
Barry Naujoke8b0eba2008-04-22 17:34:31 +100087 if (xname->len >= MAXNAMELEN)
88 return EFAULT; /* match IRIX behaviour */
89
90 return 0;
91}
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +100093STATIC int
94xfs_inode_hasattr(
95 struct xfs_inode *ip)
96{
97 if (!XFS_IFORK_Q(ip) ||
98 (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
99 ip->i_d.di_anextents == 0))
100 return 0;
101 return 1;
102}
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104/*========================================================================
105 * Overall external interface routines.
106 *========================================================================*/
107
Christoph Hellwige82fa0c2009-11-14 16:17:20 +0000108STATIC int
109xfs_attr_get_int(
110 struct xfs_inode *ip,
111 struct xfs_name *name,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100112 unsigned char *value,
Christoph Hellwige82fa0c2009-11-14 16:17:20 +0000113 int *valuelenp,
114 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
116 xfs_da_args_t args;
117 int error;
118
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +1000119 if (!xfs_inode_hasattr(ip))
120 return ENOATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 /*
123 * Fill in the arg structure for this request.
124 */
125 memset((char *)&args, 0, sizeof(args));
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000126 args.name = name->name;
127 args.namelen = name->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 args.value = value;
129 args.valuelen = *valuelenp;
130 args.flags = flags;
131 args.hashval = xfs_da_hashname(args.name, args.namelen);
132 args.dp = ip;
133 args.whichfork = XFS_ATTR_FORK;
134
135 /*
136 * Decide on what work routines to call based on the inode size.
137 */
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +1000138 if (ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 error = xfs_attr_shortform_getvalue(&args);
140 } else if (xfs_bmap_one_block(ip, XFS_ATTR_FORK)) {
141 error = xfs_attr_leaf_get(&args);
142 } else {
143 error = xfs_attr_node_get(&args);
144 }
145
146 /*
147 * Return the number of bytes in the value to the caller.
148 */
149 *valuelenp = args.valuelen;
150
151 if (error == EEXIST)
152 error = 0;
153 return(error);
154}
155
156int
Christoph Hellwig993386c2007-08-28 16:12:30 +1000157xfs_attr_get(
158 xfs_inode_t *ip,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100159 const unsigned char *name,
160 unsigned char *value,
Christoph Hellwig993386c2007-08-28 16:12:30 +1000161 int *valuelenp,
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000162 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000164 int error;
165 struct xfs_name xname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 XFS_STATS_INC(xs_attr_get);
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
170 return(EIO);
171
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000172 error = xfs_attr_name_to_xname(&xname, name);
173 if (error)
174 return error;
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 xfs_ilock(ip, XFS_ILOCK_SHARED);
Christoph Hellwige82fa0c2009-11-14 16:17:20 +0000177 error = xfs_attr_get_int(ip, &xname, value, valuelenp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 xfs_iunlock(ip, XFS_ILOCK_SHARED);
179 return(error);
180}
181
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000182/*
183 * Calculate how many blocks we need for the new attribute,
184 */
Eric Sandeen5d77c0d2009-11-19 15:52:00 +0000185STATIC int
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000186xfs_attr_calc_size(
187 struct xfs_inode *ip,
188 int namelen,
189 int valuelen,
190 int *local)
191{
192 struct xfs_mount *mp = ip->i_mount;
193 int size;
194 int nblks;
195
196 /*
197 * Determine space new attribute will use, and if it would be
198 * "local" or "remote" (note: local != inline).
199 */
200 size = xfs_attr_leaf_newentsize(namelen, valuelen,
201 mp->m_sb.sb_blocksize, local);
202
203 nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
204 if (*local) {
205 if (size > (mp->m_sb.sb_blocksize >> 1)) {
206 /* Double split possible */
207 nblks *= 2;
208 }
209 } else {
210 /*
211 * Out of line attribute, cannot double split, but
212 * make room for the attribute value itself.
213 */
214 uint dblocks = XFS_B_TO_FSB(mp, valuelen);
215 nblks += dblocks;
216 nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
217 }
218
219 return nblks;
220}
221
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000222STATIC int
Dave Chinnera9273ca2010-01-20 10:47:48 +1100223xfs_attr_set_int(
224 struct xfs_inode *dp,
225 struct xfs_name *name,
226 unsigned char *value,
227 int valuelen,
228 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
230 xfs_da_args_t args;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 xfs_fsblock_t firstblock;
232 xfs_bmap_free_t flist;
233 int error, err2, committed;
Nathan Scottaa82daa2005-11-02 10:33:33 +1100234 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 int rsvd = (flags & ATTR_ROOT) != 0;
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000236 int local;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238 /*
239 * Attach the dquots to the inode.
240 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200241 error = xfs_qm_dqattach(dp, 0);
242 if (error)
243 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245 /*
246 * If the inode doesn't have an attribute fork, add one.
247 * (inode must not be locked when we call this routine)
248 */
249 if (XFS_IFORK_Q(dp) == 0) {
Barry Naujoke5889e92007-02-10 18:35:58 +1100250 int sf_size = sizeof(xfs_attr_sf_hdr_t) +
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000251 XFS_ATTR_SF_ENTSIZE_BYNAME(name->len, valuelen);
Barry Naujoke5889e92007-02-10 18:35:58 +1100252
253 if ((error = xfs_bmap_add_attrfork(dp, sf_size, rsvd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 return(error);
255 }
256
257 /*
258 * Fill in the arg structure for this request.
259 */
260 memset((char *)&args, 0, sizeof(args));
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000261 args.name = name->name;
262 args.namelen = name->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 args.value = value;
264 args.valuelen = valuelen;
265 args.flags = flags;
266 args.hashval = xfs_da_hashname(args.name, args.namelen);
267 args.dp = dp;
268 args.firstblock = &firstblock;
269 args.flist = &flist;
270 args.whichfork = XFS_ATTR_FORK;
Barry Naujok6a178102008-05-21 16:42:05 +1000271 args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 /* Size is now blocks for attribute data */
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000274 args.total = xfs_attr_calc_size(dp, name->len, valuelen, &local);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 /*
277 * Start our first transaction of the day.
278 *
279 * All future transactions during this code must be "chained" off
280 * this one via the trans_dup() call. All transactions will contain
281 * the inode, and the inode will always be marked with trans_ihold().
282 * Since the inode will be locked in all transactions, we must log
283 * the inode in every transaction to let it float upward through
284 * the log.
285 */
286 args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_SET);
287
288 /*
289 * Root fork attributes can use reserved data blocks for this
290 * operation if necessary
291 */
292
293 if (rsvd)
294 args.trans->t_flags |= XFS_TRANS_RESERVE;
295
Jeff Liua21cd502013-01-28 21:27:53 +0800296 error = xfs_trans_reserve(args.trans, args.total,
297 XFS_ATTRSETM_LOG_RES(mp) +
298 XFS_ATTRSETRT_LOG_RES(mp) * args.total,
299 0, XFS_TRANS_PERM_LOG_RES,
300 XFS_ATTRSET_LOG_COUNT);
301 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 xfs_trans_cancel(args.trans, 0);
303 return(error);
304 }
305 xfs_ilock(dp, XFS_ILOCK_EXCL);
306
Christoph Hellwig7d095252009-06-08 15:33:32 +0200307 error = xfs_trans_reserve_quota_nblks(args.trans, dp, args.total, 0,
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000308 rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
309 XFS_QMOPT_RES_REGBLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 if (error) {
311 xfs_iunlock(dp, XFS_ILOCK_EXCL);
312 xfs_trans_cancel(args.trans, XFS_TRANS_RELEASE_LOG_RES);
313 return (error);
314 }
315
Christoph Hellwigddc34152011-09-19 15:00:54 +0000316 xfs_trans_ijoin(args.trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000319 * If the attribute list is non-existent or a shortform list,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 * upgrade it to a single-leaf-block attribute list.
321 */
322 if ((dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) ||
323 ((dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS) &&
324 (dp->i_d.di_anextents == 0))) {
325
326 /*
327 * Build initial attribute list (if required).
328 */
329 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
Nathan Scottd8cc8902005-11-02 10:34:53 +1100330 xfs_attr_shortform_create(&args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 /*
333 * Try to add the attr to the attribute list in
334 * the inode.
335 */
336 error = xfs_attr_shortform_addname(&args);
337 if (error != ENOSPC) {
338 /*
339 * Commit the shortform mods, and we're done.
340 * NOTE: this is also the error path (EEXIST, etc).
341 */
342 ASSERT(args.trans != NULL);
343
344 /*
345 * If this is a synchronous mount, make sure that
346 * the transaction goes to disk before returning
347 * to the user.
348 */
349 if (mp->m_flags & XFS_MOUNT_WSYNC) {
350 xfs_trans_set_sync(args.trans);
351 }
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000352
353 if (!error && (flags & ATTR_KERNOTIME) == 0) {
354 xfs_trans_ichgtime(args.trans, dp,
355 XFS_ICHGTIME_CHG);
356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 err2 = xfs_trans_commit(args.trans,
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000358 XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 xfs_iunlock(dp, XFS_ILOCK_EXCL);
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 return(error == 0 ? err2 : error);
362 }
363
364 /*
365 * It won't fit in the shortform, transform to a leaf block.
366 * GROT: another possible req'mt for a double-split btree op.
367 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600368 xfs_bmap_init(args.flist, args.firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 error = xfs_attr_shortform_to_leaf(&args);
370 if (!error) {
371 error = xfs_bmap_finish(&args.trans, args.flist,
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100372 &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 }
374 if (error) {
375 ASSERT(committed);
376 args.trans = NULL;
377 xfs_bmap_cancel(&flist);
378 goto out;
379 }
380
381 /*
382 * bmap_finish() may have committed the last trans and started
383 * a new one. We need the inode to be in all transactions.
384 */
Christoph Hellwig898621d2010-06-24 11:36:58 +1000385 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +0000386 xfs_trans_ijoin(args.trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 /*
389 * Commit the leaf transformation. We'll need another (linked)
390 * transaction to add the new attribute to the leaf.
391 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000392
393 error = xfs_trans_roll(&args.trans, dp);
394 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 goto out;
396
397 }
398
399 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
400 error = xfs_attr_leaf_addname(&args);
401 } else {
402 error = xfs_attr_node_addname(&args);
403 }
404 if (error) {
405 goto out;
406 }
407
408 /*
409 * If this is a synchronous mount, make sure that the
410 * transaction goes to disk before returning to the user.
411 */
412 if (mp->m_flags & XFS_MOUNT_WSYNC) {
413 xfs_trans_set_sync(args.trans);
414 }
415
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000416 if ((flags & ATTR_KERNOTIME) == 0)
417 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 /*
420 * Commit the last in the sequence of transactions.
421 */
422 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000423 error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 xfs_iunlock(dp, XFS_ILOCK_EXCL);
425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 return(error);
427
428out:
429 if (args.trans)
430 xfs_trans_cancel(args.trans,
431 XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
432 xfs_iunlock(dp, XFS_ILOCK_EXCL);
433 return(error);
434}
435
Nathan Scottaa82daa2005-11-02 10:33:33 +1100436int
Christoph Hellwig993386c2007-08-28 16:12:30 +1000437xfs_attr_set(
438 xfs_inode_t *dp,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100439 const unsigned char *name,
440 unsigned char *value,
Christoph Hellwig993386c2007-08-28 16:12:30 +1000441 int valuelen,
442 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000444 int error;
445 struct xfs_name xname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Nathan Scottaa82daa2005-11-02 10:33:33 +1100447 XFS_STATS_INC(xs_attr_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Nathan Scottaa82daa2005-11-02 10:33:33 +1100449 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 return (EIO);
451
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000452 error = xfs_attr_name_to_xname(&xname, name);
453 if (error)
454 return error;
455
456 return xfs_attr_set_int(dp, &xname, value, valuelen, flags);
Nathan Scottaa82daa2005-11-02 10:33:33 +1100457}
458
459/*
460 * Generic handler routine to remove a name from an attribute list.
461 * Transitions attribute list from Btree to shortform as necessary.
462 */
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000463STATIC int
464xfs_attr_remove_int(xfs_inode_t *dp, struct xfs_name *name, int flags)
Nathan Scottaa82daa2005-11-02 10:33:33 +1100465{
466 xfs_da_args_t args;
467 xfs_fsblock_t firstblock;
468 xfs_bmap_free_t flist;
469 int error;
470 xfs_mount_t *mp = dp->i_mount;
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 /*
473 * Fill in the arg structure for this request.
474 */
475 memset((char *)&args, 0, sizeof(args));
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000476 args.name = name->name;
477 args.namelen = name->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 args.flags = flags;
479 args.hashval = xfs_da_hashname(args.name, args.namelen);
480 args.dp = dp;
481 args.firstblock = &firstblock;
482 args.flist = &flist;
483 args.total = 0;
484 args.whichfork = XFS_ATTR_FORK;
485
486 /*
Dave Chinner4a338212011-06-23 01:35:01 +0000487 * we have no control over the attribute names that userspace passes us
488 * to remove, so we have to allow the name lookup prior to attribute
489 * removal to fail.
490 */
491 args.op_flags = XFS_DA_OP_OKNOENT;
492
493 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 * Attach the dquots to the inode.
495 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200496 error = xfs_qm_dqattach(dp, 0);
497 if (error)
498 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 /*
501 * Start our first transaction of the day.
502 *
503 * All future transactions during this code must be "chained" off
504 * this one via the trans_dup() call. All transactions will contain
505 * the inode, and the inode will always be marked with trans_ihold().
506 * Since the inode will be locked in all transactions, we must log
507 * the inode in every transaction to let it float upward through
508 * the log.
509 */
510 args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_RM);
511
512 /*
513 * Root fork attributes can use reserved data blocks for this
514 * operation if necessary
515 */
516
517 if (flags & ATTR_ROOT)
518 args.trans->t_flags |= XFS_TRANS_RESERVE;
519
520 if ((error = xfs_trans_reserve(args.trans,
521 XFS_ATTRRM_SPACE_RES(mp),
522 XFS_ATTRRM_LOG_RES(mp),
523 0, XFS_TRANS_PERM_LOG_RES,
524 XFS_ATTRRM_LOG_COUNT))) {
525 xfs_trans_cancel(args.trans, 0);
526 return(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 }
528
529 xfs_ilock(dp, XFS_ILOCK_EXCL);
530 /*
531 * No need to make quota reservations here. We expect to release some
532 * blocks not allocate in the common case.
533 */
Christoph Hellwigddc34152011-09-19 15:00:54 +0000534 xfs_trans_ijoin(args.trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536 /*
537 * Decide on what work routines to call based on the inode size.
538 */
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +1000539 if (!xfs_inode_hasattr(dp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 error = XFS_ERROR(ENOATTR);
541 goto out;
542 }
543 if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
544 ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
545 error = xfs_attr_shortform_remove(&args);
546 if (error) {
547 goto out;
548 }
549 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
550 error = xfs_attr_leaf_removename(&args);
551 } else {
552 error = xfs_attr_node_removename(&args);
553 }
554 if (error) {
555 goto out;
556 }
557
558 /*
559 * If this is a synchronous mount, make sure that the
560 * transaction goes to disk before returning to the user.
561 */
562 if (mp->m_flags & XFS_MOUNT_WSYNC) {
563 xfs_trans_set_sync(args.trans);
564 }
565
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000566 if ((flags & ATTR_KERNOTIME) == 0)
567 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 /*
570 * Commit the last in the sequence of transactions.
571 */
572 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000573 error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 xfs_iunlock(dp, XFS_ILOCK_EXCL);
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 return(error);
577
578out:
579 if (args.trans)
580 xfs_trans_cancel(args.trans,
581 XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
582 xfs_iunlock(dp, XFS_ILOCK_EXCL);
583 return(error);
584}
585
Nathan Scottaa82daa2005-11-02 10:33:33 +1100586int
Christoph Hellwig993386c2007-08-28 16:12:30 +1000587xfs_attr_remove(
588 xfs_inode_t *dp,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100589 const unsigned char *name,
Christoph Hellwig993386c2007-08-28 16:12:30 +1000590 int flags)
Nathan Scottaa82daa2005-11-02 10:33:33 +1100591{
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000592 int error;
593 struct xfs_name xname;
Nathan Scottaa82daa2005-11-02 10:33:33 +1100594
595 XFS_STATS_INC(xs_attr_remove);
596
Nathan Scottaa82daa2005-11-02 10:33:33 +1100597 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
598 return (EIO);
599
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000600 error = xfs_attr_name_to_xname(&xname, name);
601 if (error)
602 return error;
603
Nathan Scottaa82daa2005-11-02 10:33:33 +1100604 xfs_ilock(dp, XFS_ILOCK_SHARED);
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +1000605 if (!xfs_inode_hasattr(dp)) {
Nathan Scottaa82daa2005-11-02 10:33:33 +1100606 xfs_iunlock(dp, XFS_ILOCK_SHARED);
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +1000607 return XFS_ERROR(ENOATTR);
Nathan Scottaa82daa2005-11-02 10:33:33 +1100608 }
609 xfs_iunlock(dp, XFS_ILOCK_SHARED);
610
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000611 return xfs_attr_remove_int(dp, &xname, flags);
Nathan Scottaa82daa2005-11-02 10:33:33 +1100612}
613
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000614int
Tim Shimmin726801b2006-09-28 11:01:37 +1000615xfs_attr_list_int(xfs_attr_list_context_t *context)
616{
617 int error;
618 xfs_inode_t *dp = context->dp;
619
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000620 XFS_STATS_INC(xs_attr_list);
621
622 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
623 return EIO;
624
625 xfs_ilock(dp, XFS_ILOCK_SHARED);
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000626
Tim Shimmin726801b2006-09-28 11:01:37 +1000627 /*
628 * Decide on what work routines to call based on the inode size.
629 */
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +1000630 if (!xfs_inode_hasattr(dp)) {
Tim Shimmin726801b2006-09-28 11:01:37 +1000631 error = 0;
632 } else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
633 error = xfs_attr_shortform_list(context);
634 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
635 error = xfs_attr_leaf_list(context);
636 } else {
637 error = xfs_attr_node_list(context);
638 }
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000639
640 xfs_iunlock(dp, XFS_ILOCK_SHARED);
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000641
Tim Shimmin726801b2006-09-28 11:01:37 +1000642 return error;
643}
644
645#define ATTR_ENTBASESIZE /* minimum bytes used by an attr */ \
646 (((struct attrlist_ent *) 0)->a_name - (char *) 0)
647#define ATTR_ENTSIZE(namelen) /* actual bytes used by an attr */ \
648 ((ATTR_ENTBASESIZE + (namelen) + 1 + sizeof(u_int32_t)-1) \
649 & ~(sizeof(u_int32_t)-1))
650
651/*
652 * Format an attribute and copy it out to the user's buffer.
653 * Take care to check values and protect against them changing later,
654 * we may be reading them directly out of a user buffer.
655 */
656/*ARGSUSED*/
657STATIC int
Dave Chinnera9273ca2010-01-20 10:47:48 +1100658xfs_attr_put_listent(
659 xfs_attr_list_context_t *context,
660 int flags,
661 unsigned char *name,
662 int namelen,
663 int valuelen,
664 unsigned char *value)
Tim Shimmin726801b2006-09-28 11:01:37 +1000665{
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000666 struct attrlist *alist = (struct attrlist *)context->alist;
Tim Shimmin726801b2006-09-28 11:01:37 +1000667 attrlist_ent_t *aep;
668 int arraytop;
669
670 ASSERT(!(context->flags & ATTR_KERNOVAL));
671 ASSERT(context->count >= 0);
672 ASSERT(context->count < (ATTR_MAX_VALUELEN/8));
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000673 ASSERT(context->firstu >= sizeof(*alist));
Tim Shimmin726801b2006-09-28 11:01:37 +1000674 ASSERT(context->firstu <= context->bufsize);
675
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000676 /*
677 * Only list entries in the right namespace.
678 */
679 if (((context->flags & ATTR_SECURE) == 0) !=
680 ((flags & XFS_ATTR_SECURE) == 0))
681 return 0;
682 if (((context->flags & ATTR_ROOT) == 0) !=
683 ((flags & XFS_ATTR_ROOT) == 0))
684 return 0;
685
686 arraytop = sizeof(*alist) +
687 context->count * sizeof(alist->al_offset[0]);
Tim Shimmin726801b2006-09-28 11:01:37 +1000688 context->firstu -= ATTR_ENTSIZE(namelen);
689 if (context->firstu < arraytop) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000690 trace_xfs_attr_list_full(context);
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000691 alist->al_more = 1;
Tim Shimmin726801b2006-09-28 11:01:37 +1000692 context->seen_enough = 1;
693 return 1;
694 }
695
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000696 aep = (attrlist_ent_t *)&context->alist[context->firstu];
Tim Shimmin726801b2006-09-28 11:01:37 +1000697 aep->a_valuelen = valuelen;
698 memcpy(aep->a_name, name, namelen);
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000699 aep->a_name[namelen] = 0;
700 alist->al_offset[context->count++] = context->firstu;
701 alist->al_count = context->count;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000702 trace_xfs_attr_list_add(context);
Tim Shimmin726801b2006-09-28 11:01:37 +1000703 return 0;
704}
705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706/*
707 * Generate a list of extended attribute names and optionally
708 * also value lengths. Positive return value follows the XFS
709 * convention of being an error, zero or negative return code
710 * is the length of the buffer returned (negated), indicating
711 * success.
712 */
713int
Christoph Hellwig993386c2007-08-28 16:12:30 +1000714xfs_attr_list(
715 xfs_inode_t *dp,
716 char *buffer,
717 int bufsize,
718 int flags,
719 attrlist_cursor_kern_t *cursor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
721 xfs_attr_list_context_t context;
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000722 struct attrlist *alist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 int error;
724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 /*
726 * Validate the cursor.
727 */
728 if (cursor->pad1 || cursor->pad2)
729 return(XFS_ERROR(EINVAL));
730 if ((cursor->initted == 0) &&
731 (cursor->hashval || cursor->blkno || cursor->offset))
Tim Shimmin726801b2006-09-28 11:01:37 +1000732 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
734 /*
735 * Check for a properly aligned buffer.
736 */
737 if (((long)buffer) & (sizeof(int)-1))
Tim Shimmin726801b2006-09-28 11:01:37 +1000738 return XFS_ERROR(EFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 if (flags & ATTR_KERNOVAL)
740 bufsize = 0;
741
742 /*
743 * Initialize the output buffer.
744 */
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000745 memset(&context, 0, sizeof(context));
Christoph Hellwig993386c2007-08-28 16:12:30 +1000746 context.dp = dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 context.cursor = cursor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 context.resynch = 1;
749 context.flags = flags;
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000750 context.alist = buffer;
751 context.bufsize = (bufsize & ~(sizeof(int)-1)); /* align */
752 context.firstu = context.bufsize;
753 context.put_listent = xfs_attr_put_listent;
Tim Shimmin726801b2006-09-28 11:01:37 +1000754
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000755 alist = (struct attrlist *)context.alist;
756 alist->al_count = 0;
757 alist->al_more = 0;
758 alist->al_offset[0] = context.bufsize;
Tim Shimmin726801b2006-09-28 11:01:37 +1000759
760 error = xfs_attr_list_int(&context);
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000761 ASSERT(error >= 0);
Tim Shimmin726801b2006-09-28 11:01:37 +1000762 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763}
764
765int /* error */
766xfs_attr_inactive(xfs_inode_t *dp)
767{
768 xfs_trans_t *trans;
769 xfs_mount_t *mp;
770 int error;
771
772 mp = dp->i_mount;
773 ASSERT(! XFS_NOT_DQATTACHED(mp, dp));
774
775 xfs_ilock(dp, XFS_ILOCK_SHARED);
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +1000776 if (!xfs_inode_hasattr(dp) ||
777 dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 xfs_iunlock(dp, XFS_ILOCK_SHARED);
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +1000779 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 }
781 xfs_iunlock(dp, XFS_ILOCK_SHARED);
782
783 /*
784 * Start our first transaction of the day.
785 *
786 * All future transactions during this code must be "chained" off
787 * this one via the trans_dup() call. All transactions will contain
788 * the inode, and the inode will always be marked with trans_ihold().
789 * Since the inode will be locked in all transactions, we must log
790 * the inode in every transaction to let it float upward through
791 * the log.
792 */
793 trans = xfs_trans_alloc(mp, XFS_TRANS_ATTRINVAL);
794 if ((error = xfs_trans_reserve(trans, 0, XFS_ATTRINVAL_LOG_RES(mp), 0,
795 XFS_TRANS_PERM_LOG_RES,
796 XFS_ATTRINVAL_LOG_COUNT))) {
797 xfs_trans_cancel(trans, 0);
798 return(error);
799 }
800 xfs_ilock(dp, XFS_ILOCK_EXCL);
801
802 /*
803 * No need to make quota reservations here. We expect to release some
804 * blocks, not allocate, in the common case.
805 */
Christoph Hellwigddc34152011-09-19 15:00:54 +0000806 xfs_trans_ijoin(trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
808 /*
809 * Decide on what work routines to call based on the inode size.
810 */
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +1000811 if (!xfs_inode_hasattr(dp) ||
812 dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 error = 0;
814 goto out;
815 }
Dave Chinner517c2222013-04-24 18:58:55 +1000816 error = xfs_attr3_root_inactive(&trans, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 if (error)
818 goto out;
Christoph Hellwig8f04c472011-07-08 14:34:34 +0200819
Christoph Hellwig8f04c472011-07-08 14:34:34 +0200820 error = xfs_itruncate_extents(&trans, dp, XFS_ATTR_FORK, 0);
821 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 goto out;
823
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000824 error = xfs_trans_commit(trans, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 xfs_iunlock(dp, XFS_ILOCK_EXCL);
826
827 return(error);
828
829out:
830 xfs_trans_cancel(trans, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
831 xfs_iunlock(dp, XFS_ILOCK_EXCL);
832 return(error);
833}
834
835
836
837/*========================================================================
838 * External routines when attribute list is inside the inode
839 *========================================================================*/
840
841/*
842 * Add a name to the shortform attribute list structure
843 * This is the external routine.
844 */
845STATIC int
846xfs_attr_shortform_addname(xfs_da_args_t *args)
847{
Nathan Scottd8cc8902005-11-02 10:34:53 +1100848 int newsize, forkoff, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Dave Chinner5a5881c2012-03-22 05:15:13 +0000850 trace_xfs_attr_sf_addname(args);
851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 retval = xfs_attr_shortform_lookup(args);
853 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
854 return(retval);
855 } else if (retval == EEXIST) {
856 if (args->flags & ATTR_CREATE)
857 return(retval);
858 retval = xfs_attr_shortform_remove(args);
859 ASSERT(retval == 0);
860 }
861
Nathan Scottd8cc8902005-11-02 10:34:53 +1100862 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
863 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
864 return(XFS_ERROR(ENOSPC));
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
867 newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100868
869 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
870 if (!forkoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 return(XFS_ERROR(ENOSPC));
Nathan Scottd8cc8902005-11-02 10:34:53 +1100872
873 xfs_attr_shortform_add(args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 return(0);
875}
876
877
878/*========================================================================
879 * External routines when attribute list is one block
880 *========================================================================*/
881
882/*
883 * Add a name to the leaf attribute list structure
884 *
885 * This leaf block cannot have a "remote" value, we only call this routine
886 * if bmap_one_block() says there is only one block (ie: no remote blks).
887 */
David Chinnera8272ce2007-11-23 16:28:09 +1100888STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889xfs_attr_leaf_addname(xfs_da_args_t *args)
890{
891 xfs_inode_t *dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000892 struct xfs_buf *bp;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100893 int retval, error, committed, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Dave Chinner5a5881c2012-03-22 05:15:13 +0000895 trace_xfs_attr_leaf_addname(args);
896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 /*
898 * Read the (only) block in the attribute list in.
899 */
900 dp = args->dp;
901 args->blkno = 0;
Dave Chinner517c2222013-04-24 18:58:55 +1000902 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100904 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
906 /*
907 * Look up the given attribute in the leaf block. Figure out if
908 * the given flags produce an error or call for an atomic rename.
909 */
Dave Chinner517c2222013-04-24 18:58:55 +1000910 retval = xfs_attr3_leaf_lookup_int(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000912 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000913 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 } else if (retval == EEXIST) {
915 if (args->flags & ATTR_CREATE) { /* pure create op */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000916 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000917 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 }
Dave Chinner5a5881c2012-03-22 05:15:13 +0000919
920 trace_xfs_attr_leaf_replace(args);
921
Barry Naujok6a178102008-05-21 16:42:05 +1000922 args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 args->blkno2 = args->blkno; /* set 2nd entry info*/
924 args->index2 = args->index;
925 args->rmtblkno2 = args->rmtblkno;
926 args->rmtblkcnt2 = args->rmtblkcnt;
927 }
928
929 /*
930 * Add the attribute to the leaf block, transitioning to a Btree
931 * if required.
932 */
Dave Chinner517c2222013-04-24 18:58:55 +1000933 retval = xfs_attr3_leaf_add(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 if (retval == ENOSPC) {
935 /*
936 * Promote the attribute list to the Btree format, then
937 * Commit that transaction so that the node_addname() call
938 * can manage its own transactions.
939 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600940 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000941 error = xfs_attr3_leaf_to_node(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 if (!error) {
943 error = xfs_bmap_finish(&args->trans, args->flist,
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100944 &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 }
946 if (error) {
947 ASSERT(committed);
948 args->trans = NULL;
949 xfs_bmap_cancel(args->flist);
950 return(error);
951 }
952
953 /*
954 * bmap_finish() may have committed the last trans and started
955 * a new one. We need the inode to be in all transactions.
956 */
Christoph Hellwig898621d2010-06-24 11:36:58 +1000957 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +0000958 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
960 /*
961 * Commit the current trans (including the inode) and start
962 * a new one.
963 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000964 error = xfs_trans_roll(&args->trans, dp);
965 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 return (error);
967
968 /*
969 * Fob the whole rest of the problem off on the Btree code.
970 */
971 error = xfs_attr_node_addname(args);
972 return(error);
973 }
974
975 /*
976 * Commit the transaction that added the attr name so that
977 * later routines can manage their own transactions.
978 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000979 error = xfs_trans_roll(&args->trans, dp);
980 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 return (error);
982
983 /*
984 * If there was an out-of-line value, allocate the blocks we
985 * identified for its storage and copy the value. This is done
986 * after we create the attribute so that we don't overflow the
987 * maximum size of a transaction and/or hit a deadlock.
988 */
989 if (args->rmtblkno > 0) {
990 error = xfs_attr_rmtval_set(args);
991 if (error)
992 return(error);
993 }
994
995 /*
996 * If this is an atomic rename operation, we must "flip" the
997 * incomplete flags on the "new" and "old" attribute/value pairs
998 * so that one disappears and one appears atomically. Then we
999 * must remove the "old" attribute/value pair.
1000 */
Barry Naujok6a178102008-05-21 16:42:05 +10001001 if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 /*
1003 * In a separate transaction, set the incomplete flag on the
1004 * "old" attr and clear the incomplete flag on the "new" attr.
1005 */
Dave Chinner517c2222013-04-24 18:58:55 +10001006 error = xfs_attr3_leaf_flipflags(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 if (error)
1008 return(error);
1009
1010 /*
1011 * Dismantle the "old" attribute/value pair by removing
1012 * a "remote" value (if it exists).
1013 */
1014 args->index = args->index2;
1015 args->blkno = args->blkno2;
1016 args->rmtblkno = args->rmtblkno2;
1017 args->rmtblkcnt = args->rmtblkcnt2;
1018 if (args->rmtblkno) {
1019 error = xfs_attr_rmtval_remove(args);
1020 if (error)
1021 return(error);
1022 }
1023
1024 /*
1025 * Read in the block containing the "old" attr, then
1026 * remove the "old" attr from that block (neat, huh!)
1027 */
Dave Chinner517c2222013-04-24 18:58:55 +10001028 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
Dave Chinnerad14c332012-11-12 22:54:16 +11001029 -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +11001031 return error;
1032
Dave Chinner517c2222013-04-24 18:58:55 +10001033 xfs_attr3_leaf_remove(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
1035 /*
1036 * If the result is small enough, shrink it all into the inode.
1037 */
Nathan Scottd8cc8902005-11-02 10:34:53 +11001038 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001039 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +10001040 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 /* bp is gone due to xfs_da_shrink_inode */
1042 if (!error) {
1043 error = xfs_bmap_finish(&args->trans,
1044 args->flist,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 &committed);
1046 }
1047 if (error) {
1048 ASSERT(committed);
1049 args->trans = NULL;
1050 xfs_bmap_cancel(args->flist);
1051 return(error);
1052 }
1053
1054 /*
1055 * bmap_finish() may have committed the last trans
1056 * and started a new one. We need the inode to be
1057 * in all transactions.
1058 */
Christoph Hellwig898621d2010-06-24 11:36:58 +10001059 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +00001060 xfs_trans_ijoin(args->trans, dp, 0);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
1063 /*
1064 * Commit the remove and start the next trans in series.
1065 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10001066 error = xfs_trans_roll(&args->trans, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
1068 } else if (args->rmtblkno > 0) {
1069 /*
1070 * Added a "remote" value, just clear the incomplete flag.
1071 */
Dave Chinner517c2222013-04-24 18:58:55 +10001072 error = xfs_attr3_leaf_clearflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 }
Dave Chinner517c2222013-04-24 18:58:55 +10001074 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075}
1076
1077/*
1078 * Remove a name from the leaf attribute list structure
1079 *
1080 * This leaf block cannot have a "remote" value, we only call this routine
1081 * if bmap_one_block() says there is only one block (ie: no remote blks).
1082 */
1083STATIC int
1084xfs_attr_leaf_removename(xfs_da_args_t *args)
1085{
1086 xfs_inode_t *dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001087 struct xfs_buf *bp;
Nathan Scottd8cc8902005-11-02 10:34:53 +11001088 int error, committed, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
Dave Chinner5a5881c2012-03-22 05:15:13 +00001090 trace_xfs_attr_leaf_removename(args);
1091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 /*
1093 * Remove the attribute.
1094 */
1095 dp = args->dp;
1096 args->blkno = 0;
Dave Chinner517c2222013-04-24 18:58:55 +10001097 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Dave Chinnerad14c332012-11-12 22:54:16 +11001098 if (error)
1099 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Dave Chinner517c2222013-04-24 18:58:55 +10001101 error = xfs_attr3_leaf_lookup_int(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 if (error == ENOATTR) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001103 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +10001104 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 }
1106
Dave Chinner517c2222013-04-24 18:58:55 +10001107 xfs_attr3_leaf_remove(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
1109 /*
1110 * If the result is small enough, shrink it all into the inode.
1111 */
Nathan Scottd8cc8902005-11-02 10:34:53 +11001112 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001113 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +10001114 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 /* bp is gone due to xfs_da_shrink_inode */
1116 if (!error) {
1117 error = xfs_bmap_finish(&args->trans, args->flist,
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001118 &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 }
1120 if (error) {
1121 ASSERT(committed);
1122 args->trans = NULL;
1123 xfs_bmap_cancel(args->flist);
Dave Chinner517c2222013-04-24 18:58:55 +10001124 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 }
1126
1127 /*
1128 * bmap_finish() may have committed the last trans and started
1129 * a new one. We need the inode to be in all transactions.
1130 */
Christoph Hellwig898621d2010-06-24 11:36:58 +10001131 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +00001132 xfs_trans_ijoin(args->trans, dp, 0);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001133 }
Dave Chinner517c2222013-04-24 18:58:55 +10001134 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135}
1136
1137/*
1138 * Look up a name in a leaf attribute list structure.
1139 *
1140 * This leaf block cannot have a "remote" value, we only call this routine
1141 * if bmap_one_block() says there is only one block (ie: no remote blks).
1142 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001143STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144xfs_attr_leaf_get(xfs_da_args_t *args)
1145{
Dave Chinner1d9025e2012-06-22 18:50:14 +10001146 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 int error;
1148
Dave Chinneree732592012-11-12 22:53:53 +11001149 trace_xfs_attr_leaf_get(args);
1150
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 args->blkno = 0;
Dave Chinner517c2222013-04-24 18:58:55 +10001152 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +11001154 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
Dave Chinner517c2222013-04-24 18:58:55 +10001156 error = xfs_attr3_leaf_lookup_int(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 if (error != EEXIST) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001158 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +10001159 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 }
Dave Chinner517c2222013-04-24 18:58:55 +10001161 error = xfs_attr3_leaf_getvalue(bp, args);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001162 xfs_trans_brelse(args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
1164 error = xfs_attr_rmtval_get(args);
1165 }
Dave Chinner517c2222013-04-24 18:58:55 +10001166 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167}
1168
1169/*
1170 * Copy out attribute entries for attr_list(), for leaf attribute lists.
1171 */
1172STATIC int
1173xfs_attr_leaf_list(xfs_attr_list_context_t *context)
1174{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 int error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001176 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Dave Chinneree732592012-11-12 22:53:53 +11001178 trace_xfs_attr_leaf_list(context);
1179
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 context->cursor->blkno = 0;
Dave Chinner517c2222013-04-24 18:58:55 +10001181 error = xfs_attr3_leaf_read(NULL, context->dp, 0, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 if (error)
Tim Shimmin726801b2006-09-28 11:01:37 +10001183 return XFS_ERROR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
Dave Chinner517c2222013-04-24 18:58:55 +10001185 error = xfs_attr3_leaf_list_int(bp, context);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001186 xfs_trans_brelse(NULL, bp);
Tim Shimmin726801b2006-09-28 11:01:37 +10001187 return XFS_ERROR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188}
1189
1190
1191/*========================================================================
1192 * External routines when attribute list size > XFS_LBSIZE(mp).
1193 *========================================================================*/
1194
1195/*
1196 * Add a name to a Btree-format attribute list.
1197 *
1198 * This will involve walking down the Btree, and may involve splitting
1199 * leaf nodes and even splitting intermediate nodes up to and including
1200 * the root node (a special case of an intermediate node).
1201 *
1202 * "Remote" attribute values confuse the issue and atomic rename operations
1203 * add a whole extra layer of confusion on top of that.
1204 */
1205STATIC int
1206xfs_attr_node_addname(xfs_da_args_t *args)
1207{
1208 xfs_da_state_t *state;
1209 xfs_da_state_blk_t *blk;
1210 xfs_inode_t *dp;
1211 xfs_mount_t *mp;
1212 int committed, retval, error;
1213
Dave Chinner5a5881c2012-03-22 05:15:13 +00001214 trace_xfs_attr_node_addname(args);
1215
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 /*
1217 * Fill in bucket of arguments/results/context to carry around.
1218 */
1219 dp = args->dp;
1220 mp = dp->i_mount;
1221restart:
1222 state = xfs_da_state_alloc();
1223 state->args = args;
1224 state->mp = mp;
1225 state->blocksize = state->mp->m_sb.sb_blocksize;
1226 state->node_ents = state->mp->m_attr_node_ents;
1227
1228 /*
1229 * Search to see if name already exists, and get back a pointer
1230 * to where it should go.
1231 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001232 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 if (error)
1234 goto out;
1235 blk = &state->path.blk[ state->path.active-1 ];
1236 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1237 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
1238 goto out;
1239 } else if (retval == EEXIST) {
1240 if (args->flags & ATTR_CREATE)
1241 goto out;
Dave Chinner5a5881c2012-03-22 05:15:13 +00001242
1243 trace_xfs_attr_node_replace(args);
1244
Barry Naujok6a178102008-05-21 16:42:05 +10001245 args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 args->blkno2 = args->blkno; /* set 2nd entry info*/
1247 args->index2 = args->index;
1248 args->rmtblkno2 = args->rmtblkno;
1249 args->rmtblkcnt2 = args->rmtblkcnt;
1250 args->rmtblkno = 0;
1251 args->rmtblkcnt = 0;
1252 }
1253
Dave Chinner517c2222013-04-24 18:58:55 +10001254 retval = xfs_attr3_leaf_add(blk->bp, state->args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 if (retval == ENOSPC) {
1256 if (state->path.active == 1) {
1257 /*
1258 * Its really a single leaf node, but it had
1259 * out-of-line values so it looked like it *might*
1260 * have been a b-tree.
1261 */
1262 xfs_da_state_free(state);
Eric Sandeen9d87c312009-01-14 23:22:07 -06001263 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +10001264 error = xfs_attr3_leaf_to_node(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 if (!error) {
1266 error = xfs_bmap_finish(&args->trans,
1267 args->flist,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 &committed);
1269 }
1270 if (error) {
1271 ASSERT(committed);
1272 args->trans = NULL;
1273 xfs_bmap_cancel(args->flist);
1274 goto out;
1275 }
1276
1277 /*
1278 * bmap_finish() may have committed the last trans
1279 * and started a new one. We need the inode to be
1280 * in all transactions.
1281 */
Christoph Hellwig898621d2010-06-24 11:36:58 +10001282 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +00001283 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285 /*
1286 * Commit the node conversion and start the next
1287 * trans in the chain.
1288 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10001289 error = xfs_trans_roll(&args->trans, dp);
1290 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 goto out;
1292
1293 goto restart;
1294 }
1295
1296 /*
1297 * Split as many Btree elements as required.
1298 * This code tracks the new and old attr's location
1299 * in the index/blkno/rmtblkno/rmtblkcnt fields and
1300 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
1301 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06001302 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001303 error = xfs_da3_split(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 if (!error) {
1305 error = xfs_bmap_finish(&args->trans, args->flist,
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001306 &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 }
1308 if (error) {
1309 ASSERT(committed);
1310 args->trans = NULL;
1311 xfs_bmap_cancel(args->flist);
1312 goto out;
1313 }
1314
1315 /*
1316 * bmap_finish() may have committed the last trans and started
1317 * a new one. We need the inode to be in all transactions.
1318 */
Christoph Hellwig898621d2010-06-24 11:36:58 +10001319 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +00001320 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 } else {
1322 /*
1323 * Addition succeeded, update Btree hashvals.
1324 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001325 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 }
1327
1328 /*
1329 * Kill the state structure, we're done with it and need to
1330 * allow the buffers to come back later.
1331 */
1332 xfs_da_state_free(state);
1333 state = NULL;
1334
1335 /*
1336 * Commit the leaf addition or btree split and start the next
1337 * trans in the chain.
1338 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10001339 error = xfs_trans_roll(&args->trans, dp);
1340 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 goto out;
1342
1343 /*
1344 * If there was an out-of-line value, allocate the blocks we
1345 * identified for its storage and copy the value. This is done
1346 * after we create the attribute so that we don't overflow the
1347 * maximum size of a transaction and/or hit a deadlock.
1348 */
1349 if (args->rmtblkno > 0) {
1350 error = xfs_attr_rmtval_set(args);
1351 if (error)
1352 return(error);
1353 }
1354
1355 /*
1356 * If this is an atomic rename operation, we must "flip" the
1357 * incomplete flags on the "new" and "old" attribute/value pairs
1358 * so that one disappears and one appears atomically. Then we
1359 * must remove the "old" attribute/value pair.
1360 */
Barry Naujok6a178102008-05-21 16:42:05 +10001361 if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 /*
1363 * In a separate transaction, set the incomplete flag on the
1364 * "old" attr and clear the incomplete flag on the "new" attr.
1365 */
Dave Chinner517c2222013-04-24 18:58:55 +10001366 error = xfs_attr3_leaf_flipflags(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 if (error)
1368 goto out;
1369
1370 /*
1371 * Dismantle the "old" attribute/value pair by removing
1372 * a "remote" value (if it exists).
1373 */
1374 args->index = args->index2;
1375 args->blkno = args->blkno2;
1376 args->rmtblkno = args->rmtblkno2;
1377 args->rmtblkcnt = args->rmtblkcnt2;
1378 if (args->rmtblkno) {
1379 error = xfs_attr_rmtval_remove(args);
1380 if (error)
1381 return(error);
1382 }
1383
1384 /*
1385 * Re-find the "old" attribute entry after any split ops.
1386 * The INCOMPLETE flag means that we will find the "old"
1387 * attr, not the "new" one.
1388 */
1389 args->flags |= XFS_ATTR_INCOMPLETE;
1390 state = xfs_da_state_alloc();
1391 state->args = args;
1392 state->mp = mp;
1393 state->blocksize = state->mp->m_sb.sb_blocksize;
1394 state->node_ents = state->mp->m_attr_node_ents;
1395 state->inleaf = 0;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001396 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 if (error)
1398 goto out;
1399
1400 /*
1401 * Remove the name and update the hashvals in the tree.
1402 */
1403 blk = &state->path.blk[ state->path.active-1 ];
1404 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner517c2222013-04-24 18:58:55 +10001405 error = xfs_attr3_leaf_remove(blk->bp, args);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001406 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
1408 /*
1409 * Check to see if the tree needs to be collapsed.
1410 */
1411 if (retval && (state->path.active > 1)) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001412 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001413 error = xfs_da3_join(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 if (!error) {
1415 error = xfs_bmap_finish(&args->trans,
1416 args->flist,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 &committed);
1418 }
1419 if (error) {
1420 ASSERT(committed);
1421 args->trans = NULL;
1422 xfs_bmap_cancel(args->flist);
1423 goto out;
1424 }
1425
1426 /*
1427 * bmap_finish() may have committed the last trans
1428 * and started a new one. We need the inode to be
1429 * in all transactions.
1430 */
Christoph Hellwig898621d2010-06-24 11:36:58 +10001431 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +00001432 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 }
1434
1435 /*
1436 * Commit and start the next trans in the chain.
1437 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10001438 error = xfs_trans_roll(&args->trans, dp);
1439 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 goto out;
1441
1442 } else if (args->rmtblkno > 0) {
1443 /*
1444 * Added a "remote" value, just clear the incomplete flag.
1445 */
Dave Chinner517c2222013-04-24 18:58:55 +10001446 error = xfs_attr3_leaf_clearflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 if (error)
1448 goto out;
1449 }
1450 retval = error = 0;
1451
1452out:
1453 if (state)
1454 xfs_da_state_free(state);
1455 if (error)
1456 return(error);
1457 return(retval);
1458}
1459
1460/*
1461 * Remove a name from a B-tree attribute list.
1462 *
1463 * This will involve walking down the Btree, and may involve joining
1464 * leaf nodes and even joining intermediate nodes up to and including
1465 * the root node (a special case of an intermediate node).
1466 */
1467STATIC int
1468xfs_attr_node_removename(xfs_da_args_t *args)
1469{
1470 xfs_da_state_t *state;
1471 xfs_da_state_blk_t *blk;
1472 xfs_inode_t *dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001473 struct xfs_buf *bp;
Nathan Scottd8cc8902005-11-02 10:34:53 +11001474 int retval, error, committed, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Dave Chinner5a5881c2012-03-22 05:15:13 +00001476 trace_xfs_attr_node_removename(args);
1477
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 /*
1479 * Tie a string around our finger to remind us where we are.
1480 */
1481 dp = args->dp;
1482 state = xfs_da_state_alloc();
1483 state->args = args;
1484 state->mp = dp->i_mount;
1485 state->blocksize = state->mp->m_sb.sb_blocksize;
1486 state->node_ents = state->mp->m_attr_node_ents;
1487
1488 /*
1489 * Search to see if name exists, and get back a pointer to it.
1490 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001491 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 if (error || (retval != EEXIST)) {
1493 if (error == 0)
1494 error = retval;
1495 goto out;
1496 }
1497
1498 /*
1499 * If there is an out-of-line value, de-allocate the blocks.
1500 * This is done before we remove the attribute so that we don't
1501 * overflow the maximum size of a transaction and/or hit a deadlock.
1502 */
1503 blk = &state->path.blk[ state->path.active-1 ];
1504 ASSERT(blk->bp != NULL);
1505 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1506 if (args->rmtblkno > 0) {
1507 /*
1508 * Fill in disk block numbers in the state structure
1509 * so that we can get the buffers back after we commit
1510 * several transactions in the following calls.
1511 */
1512 error = xfs_attr_fillstate(state);
1513 if (error)
1514 goto out;
1515
1516 /*
1517 * Mark the attribute as INCOMPLETE, then bunmapi() the
1518 * remote value.
1519 */
Dave Chinner517c2222013-04-24 18:58:55 +10001520 error = xfs_attr3_leaf_setflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 if (error)
1522 goto out;
1523 error = xfs_attr_rmtval_remove(args);
1524 if (error)
1525 goto out;
1526
1527 /*
1528 * Refill the state structure with buffers, the prior calls
1529 * released our buffers.
1530 */
1531 error = xfs_attr_refillstate(state);
1532 if (error)
1533 goto out;
1534 }
1535
1536 /*
1537 * Remove the name and update the hashvals in the tree.
1538 */
1539 blk = &state->path.blk[ state->path.active-1 ];
1540 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner517c2222013-04-24 18:58:55 +10001541 retval = xfs_attr3_leaf_remove(blk->bp, args);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001542 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
1544 /*
1545 * Check to see if the tree needs to be collapsed.
1546 */
1547 if (retval && (state->path.active > 1)) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001548 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001549 error = xfs_da3_join(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 if (!error) {
1551 error = xfs_bmap_finish(&args->trans, args->flist,
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001552 &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 }
1554 if (error) {
1555 ASSERT(committed);
1556 args->trans = NULL;
1557 xfs_bmap_cancel(args->flist);
1558 goto out;
1559 }
1560
1561 /*
1562 * bmap_finish() may have committed the last trans and started
1563 * a new one. We need the inode to be in all transactions.
1564 */
Christoph Hellwig898621d2010-06-24 11:36:58 +10001565 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +00001566 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
1568 /*
1569 * Commit the Btree join operation and start a new trans.
1570 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10001571 error = xfs_trans_roll(&args->trans, dp);
1572 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 goto out;
1574 }
1575
1576 /*
1577 * If the result is small enough, push it all into the inode.
1578 */
1579 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
1580 /*
1581 * Have to get rid of the copy of this dabuf in the state.
1582 */
1583 ASSERT(state->path.active == 1);
1584 ASSERT(state->path.blk[0].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 state->path.blk[0].bp = NULL;
1586
Dave Chinner517c2222013-04-24 18:58:55 +10001587 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 if (error)
1589 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
Nathan Scottd8cc8902005-11-02 10:34:53 +11001591 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001592 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +10001593 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 /* bp is gone due to xfs_da_shrink_inode */
1595 if (!error) {
1596 error = xfs_bmap_finish(&args->trans,
1597 args->flist,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 &committed);
1599 }
1600 if (error) {
1601 ASSERT(committed);
1602 args->trans = NULL;
1603 xfs_bmap_cancel(args->flist);
1604 goto out;
1605 }
1606
1607 /*
1608 * bmap_finish() may have committed the last trans
1609 * and started a new one. We need the inode to be
1610 * in all transactions.
1611 */
Christoph Hellwig898621d2010-06-24 11:36:58 +10001612 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +00001613 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 } else
Dave Chinner1d9025e2012-06-22 18:50:14 +10001615 xfs_trans_brelse(args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 }
1617 error = 0;
1618
1619out:
1620 xfs_da_state_free(state);
1621 return(error);
1622}
1623
1624/*
1625 * Fill in the disk block numbers in the state structure for the buffers
1626 * that are attached to the state structure.
1627 * This is done so that we can quickly reattach ourselves to those buffers
Nathan Scottc41564b2006-03-29 08:55:14 +10001628 * after some set of transaction commits have released these buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 */
1630STATIC int
1631xfs_attr_fillstate(xfs_da_state_t *state)
1632{
1633 xfs_da_state_path_t *path;
1634 xfs_da_state_blk_t *blk;
1635 int level;
1636
Dave Chinneree732592012-11-12 22:53:53 +11001637 trace_xfs_attr_fillstate(state->args);
1638
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 /*
1640 * Roll down the "path" in the state structure, storing the on-disk
1641 * block number for those buffers in the "path".
1642 */
1643 path = &state->path;
1644 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1645 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1646 if (blk->bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001647 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 blk->bp = NULL;
1649 } else {
1650 blk->disk_blkno = 0;
1651 }
1652 }
1653
1654 /*
1655 * Roll down the "altpath" in the state structure, storing the on-disk
1656 * block number for those buffers in the "altpath".
1657 */
1658 path = &state->altpath;
1659 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1660 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1661 if (blk->bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001662 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 blk->bp = NULL;
1664 } else {
1665 blk->disk_blkno = 0;
1666 }
1667 }
1668
1669 return(0);
1670}
1671
1672/*
1673 * Reattach the buffers to the state structure based on the disk block
1674 * numbers stored in the state structure.
Nathan Scottc41564b2006-03-29 08:55:14 +10001675 * This is done after some set of transaction commits have released those
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 * buffers from our grip.
1677 */
1678STATIC int
1679xfs_attr_refillstate(xfs_da_state_t *state)
1680{
1681 xfs_da_state_path_t *path;
1682 xfs_da_state_blk_t *blk;
1683 int level, error;
1684
Dave Chinneree732592012-11-12 22:53:53 +11001685 trace_xfs_attr_refillstate(state->args);
1686
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 /*
1688 * Roll down the "path" in the state structure, storing the on-disk
1689 * block number for those buffers in the "path".
1690 */
1691 path = &state->path;
1692 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1693 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1694 if (blk->disk_blkno) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001695 error = xfs_da3_node_read(state->args->trans,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 state->args->dp,
1697 blk->blkno, blk->disk_blkno,
Dave Chinnerd9392a42012-11-12 22:54:17 +11001698 &blk->bp, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 if (error)
1700 return(error);
1701 } else {
1702 blk->bp = NULL;
1703 }
1704 }
1705
1706 /*
1707 * Roll down the "altpath" in the state structure, storing the on-disk
1708 * block number for those buffers in the "altpath".
1709 */
1710 path = &state->altpath;
1711 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1712 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1713 if (blk->disk_blkno) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001714 error = xfs_da3_node_read(state->args->trans,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 state->args->dp,
1716 blk->blkno, blk->disk_blkno,
Dave Chinnerd9392a42012-11-12 22:54:17 +11001717 &blk->bp, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 if (error)
1719 return(error);
1720 } else {
1721 blk->bp = NULL;
1722 }
1723 }
1724
1725 return(0);
1726}
1727
1728/*
1729 * Look up a filename in a node attribute list.
1730 *
1731 * This routine gets called for any attribute fork that has more than one
1732 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1733 * "remote" values taking up more blocks.
1734 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001735STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736xfs_attr_node_get(xfs_da_args_t *args)
1737{
1738 xfs_da_state_t *state;
1739 xfs_da_state_blk_t *blk;
1740 int error, retval;
1741 int i;
1742
Dave Chinneree732592012-11-12 22:53:53 +11001743 trace_xfs_attr_node_get(args);
1744
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 state = xfs_da_state_alloc();
1746 state->args = args;
1747 state->mp = args->dp->i_mount;
1748 state->blocksize = state->mp->m_sb.sb_blocksize;
1749 state->node_ents = state->mp->m_attr_node_ents;
1750
1751 /*
1752 * Search to see if name exists, and get back a pointer to it.
1753 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001754 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 if (error) {
1756 retval = error;
1757 } else if (retval == EEXIST) {
1758 blk = &state->path.blk[ state->path.active-1 ];
1759 ASSERT(blk->bp != NULL);
1760 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1761
1762 /*
1763 * Get the value, local or "remote"
1764 */
Dave Chinner517c2222013-04-24 18:58:55 +10001765 retval = xfs_attr3_leaf_getvalue(blk->bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 if (!retval && (args->rmtblkno > 0)
1767 && !(args->flags & ATTR_KERNOVAL)) {
1768 retval = xfs_attr_rmtval_get(args);
1769 }
1770 }
1771
1772 /*
1773 * If not in a transaction, we have to release all the buffers.
1774 */
1775 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001776 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 state->path.blk[i].bp = NULL;
1778 }
1779
1780 xfs_da_state_free(state);
1781 return(retval);
1782}
1783
1784STATIC int /* error */
1785xfs_attr_node_list(xfs_attr_list_context_t *context)
1786{
1787 attrlist_cursor_kern_t *cursor;
1788 xfs_attr_leafblock_t *leaf;
1789 xfs_da_intnode_t *node;
Dave Chinner517c2222013-04-24 18:58:55 +10001790 struct xfs_attr3_icleaf_hdr leafhdr;
1791 struct xfs_da3_icnode_hdr nodehdr;
1792 struct xfs_da_node_entry *btree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 int error, i;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001794 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
Dave Chinneree732592012-11-12 22:53:53 +11001796 trace_xfs_attr_node_list(context);
1797
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 cursor = context->cursor;
1799 cursor->initted = 1;
1800
1801 /*
1802 * Do all sorts of validation on the passed-in cursor structure.
1803 * If anything is amiss, ignore the cursor and look up the hashval
1804 * starting from the btree root.
1805 */
1806 bp = NULL;
1807 if (cursor->blkno > 0) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001808 error = xfs_da3_node_read(NULL, context->dp, cursor->blkno, -1,
Dave Chinnerd9392a42012-11-12 22:54:17 +11001809 &bp, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 if ((error != 0) && (error != EFSCORRUPTED))
1811 return(error);
1812 if (bp) {
Dave Chinner517c2222013-04-24 18:58:55 +10001813 struct xfs_attr_leaf_entry *entries;
1814
Dave Chinner1d9025e2012-06-22 18:50:14 +10001815 node = bp->b_addr;
Nathan Scott89da0542006-03-17 17:28:40 +11001816 switch (be16_to_cpu(node->hdr.info.magic)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 case XFS_DA_NODE_MAGIC:
Dave Chinner517c2222013-04-24 18:58:55 +10001818 case XFS_DA3_NODE_MAGIC:
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001819 trace_xfs_attr_list_wrong_blk(context);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001820 xfs_trans_brelse(NULL, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 bp = NULL;
1822 break;
1823 case XFS_ATTR_LEAF_MAGIC:
Dave Chinner517c2222013-04-24 18:58:55 +10001824 case XFS_ATTR3_LEAF_MAGIC:
Dave Chinner1d9025e2012-06-22 18:50:14 +10001825 leaf = bp->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10001826 xfs_attr3_leaf_hdr_from_disk(&leafhdr, leaf);
1827 entries = xfs_attr3_leaf_entryp(leaf);
1828 if (cursor->hashval > be32_to_cpu(
1829 entries[leafhdr.count - 1].hashval)) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001830 trace_xfs_attr_list_wrong_blk(context);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001831 xfs_trans_brelse(NULL, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 bp = NULL;
Dave Chinner517c2222013-04-24 18:58:55 +10001833 } else if (cursor->hashval <= be32_to_cpu(
1834 entries[0].hashval)) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001835 trace_xfs_attr_list_wrong_blk(context);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001836 xfs_trans_brelse(NULL, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 bp = NULL;
1838 }
1839 break;
1840 default:
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001841 trace_xfs_attr_list_wrong_blk(context);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001842 xfs_trans_brelse(NULL, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 bp = NULL;
1844 }
1845 }
1846 }
1847
1848 /*
1849 * We did not find what we expected given the cursor's contents,
1850 * so we start from the top and work down based on the hash value.
1851 * Note that start of node block is same as start of leaf block.
1852 */
1853 if (bp == NULL) {
1854 cursor->blkno = 0;
1855 for (;;) {
Dave Chinner517c2222013-04-24 18:58:55 +10001856 __uint16_t magic;
1857
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001858 error = xfs_da3_node_read(NULL, context->dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 cursor->blkno, -1, &bp,
Dave Chinnerd9392a42012-11-12 22:54:17 +11001860 XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 if (error)
1862 return(error);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001863 node = bp->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10001864 magic = be16_to_cpu(node->hdr.info.magic);
1865 if (magic == XFS_ATTR_LEAF_MAGIC ||
1866 magic == XFS_ATTR3_LEAF_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 break;
Dave Chinner517c2222013-04-24 18:58:55 +10001868 if (magic != XFS_DA_NODE_MAGIC &&
1869 magic != XFS_DA3_NODE_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 XFS_CORRUPTION_ERROR("xfs_attr_node_list(3)",
1871 XFS_ERRLEVEL_LOW,
1872 context->dp->i_mount,
1873 node);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001874 xfs_trans_brelse(NULL, bp);
Dave Chinner517c2222013-04-24 18:58:55 +10001875 return XFS_ERROR(EFSCORRUPTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 }
Dave Chinner517c2222013-04-24 18:58:55 +10001877
1878 xfs_da3_node_hdr_from_disk(&nodehdr, node);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001879 btree = xfs_da3_node_tree_p(node);
Dave Chinner517c2222013-04-24 18:58:55 +10001880 for (i = 0; i < nodehdr.count; btree++, i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 if (cursor->hashval
Nathan Scott403432d2006-03-17 17:29:46 +11001882 <= be32_to_cpu(btree->hashval)) {
1883 cursor->blkno = be32_to_cpu(btree->before);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001884 trace_xfs_attr_list_node_descend(context,
1885 btree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 break;
1887 }
1888 }
Dave Chinner517c2222013-04-24 18:58:55 +10001889 if (i == nodehdr.count) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001890 xfs_trans_brelse(NULL, bp);
Dave Chinner517c2222013-04-24 18:58:55 +10001891 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001893 xfs_trans_brelse(NULL, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 }
1895 }
1896 ASSERT(bp != NULL);
1897
1898 /*
1899 * Roll upward through the blocks, processing each leaf block in
1900 * order. As long as there is space in the result buffer, keep
1901 * adding the information.
1902 */
1903 for (;;) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001904 leaf = bp->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10001905 error = xfs_attr3_leaf_list_int(bp, context);
Tim Shimmin726801b2006-09-28 11:01:37 +10001906 if (error) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001907 xfs_trans_brelse(NULL, bp);
Tim Shimmin726801b2006-09-28 11:01:37 +10001908 return error;
1909 }
Dave Chinner517c2222013-04-24 18:58:55 +10001910 xfs_attr3_leaf_hdr_from_disk(&leafhdr, leaf);
1911 if (context->seen_enough || leafhdr.forw == 0)
Tim Shimmin726801b2006-09-28 11:01:37 +10001912 break;
Dave Chinner517c2222013-04-24 18:58:55 +10001913 cursor->blkno = leafhdr.forw;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001914 xfs_trans_brelse(NULL, bp);
Dave Chinner517c2222013-04-24 18:58:55 +10001915 error = xfs_attr3_leaf_read(NULL, context->dp, cursor->blkno, -1,
Dave Chinnerad14c332012-11-12 22:54:16 +11001916 &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +11001918 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001920 xfs_trans_brelse(NULL, bp);
Dave Chinner517c2222013-04-24 18:58:55 +10001921 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922}