blob: 046cf3d7f52fbd748d3f776e55587865564f2a5b [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"
Dave Chinner68988112013-08-12 20:49:42 +100036#include "xfs_bmap_util.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "xfs_attr.h"
38#include "xfs_attr_leaf.h"
Dave Chinner95920cd2013-04-03 16:11:27 +110039#include "xfs_attr_remote.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include "xfs_quota.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include "xfs_trans_space.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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
74STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Barry Naujoke8b0eba2008-04-22 17:34:31 +100077STATIC int
78xfs_attr_name_to_xname(
79 struct xfs_name *xname,
Dave Chinnera9273ca2010-01-20 10:47:48 +110080 const unsigned char *aname)
Barry Naujoke8b0eba2008-04-22 17:34:31 +100081{
82 if (!aname)
83 return EINVAL;
84 xname->name = aname;
Dave Chinnera9273ca2010-01-20 10:47:48 +110085 xname->len = strlen((char *)aname);
Barry Naujoke8b0eba2008-04-22 17:34:31 +100086 if (xname->len >= MAXNAMELEN)
87 return EFAULT; /* match IRIX behaviour */
88
89 return 0;
90}
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Dave Chinnerabec5f22013-08-12 20:49:38 +100092int
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +100093xfs_inode_hasattr(
94 struct xfs_inode *ip)
95{
96 if (!XFS_IFORK_Q(ip) ||
97 (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
98 ip->i_d.di_anextents == 0))
99 return 0;
100 return 1;
101}
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103/*========================================================================
104 * Overall external interface routines.
105 *========================================================================*/
106
Christoph Hellwige82fa0c2009-11-14 16:17:20 +0000107STATIC int
108xfs_attr_get_int(
109 struct xfs_inode *ip,
110 struct xfs_name *name,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100111 unsigned char *value,
Christoph Hellwige82fa0c2009-11-14 16:17:20 +0000112 int *valuelenp,
113 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
115 xfs_da_args_t args;
116 int error;
117
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +1000118 if (!xfs_inode_hasattr(ip))
119 return ENOATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 /*
122 * Fill in the arg structure for this request.
123 */
124 memset((char *)&args, 0, sizeof(args));
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000125 args.name = name->name;
126 args.namelen = name->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 args.value = value;
128 args.valuelen = *valuelenp;
129 args.flags = flags;
130 args.hashval = xfs_da_hashname(args.name, args.namelen);
131 args.dp = ip;
132 args.whichfork = XFS_ATTR_FORK;
133
134 /*
135 * Decide on what work routines to call based on the inode size.
136 */
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +1000137 if (ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 error = xfs_attr_shortform_getvalue(&args);
139 } else if (xfs_bmap_one_block(ip, XFS_ATTR_FORK)) {
140 error = xfs_attr_leaf_get(&args);
141 } else {
142 error = xfs_attr_node_get(&args);
143 }
144
145 /*
146 * Return the number of bytes in the value to the caller.
147 */
148 *valuelenp = args.valuelen;
149
150 if (error == EEXIST)
151 error = 0;
152 return(error);
153}
154
155int
Christoph Hellwig993386c2007-08-28 16:12:30 +1000156xfs_attr_get(
157 xfs_inode_t *ip,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100158 const unsigned char *name,
159 unsigned char *value,
Christoph Hellwig993386c2007-08-28 16:12:30 +1000160 int *valuelenp,
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000161 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000163 int error;
164 struct xfs_name xname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 XFS_STATS_INC(xs_attr_get);
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
169 return(EIO);
170
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000171 error = xfs_attr_name_to_xname(&xname, name);
172 if (error)
173 return error;
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 xfs_ilock(ip, XFS_ILOCK_SHARED);
Christoph Hellwige82fa0c2009-11-14 16:17:20 +0000176 error = xfs_attr_get_int(ip, &xname, value, valuelenp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 xfs_iunlock(ip, XFS_ILOCK_SHARED);
178 return(error);
179}
180
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000181/*
182 * Calculate how many blocks we need for the new attribute,
183 */
Eric Sandeen5d77c0d2009-11-19 15:52:00 +0000184STATIC int
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000185xfs_attr_calc_size(
186 struct xfs_inode *ip,
187 int namelen,
188 int valuelen,
189 int *local)
190{
191 struct xfs_mount *mp = ip->i_mount;
192 int size;
193 int nblks;
194
195 /*
196 * Determine space new attribute will use, and if it would be
197 * "local" or "remote" (note: local != inline).
198 */
199 size = xfs_attr_leaf_newentsize(namelen, valuelen,
200 mp->m_sb.sb_blocksize, local);
201
202 nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
203 if (*local) {
204 if (size > (mp->m_sb.sb_blocksize >> 1)) {
205 /* Double split possible */
206 nblks *= 2;
207 }
208 } else {
209 /*
210 * Out of line attribute, cannot double split, but
211 * make room for the attribute value itself.
212 */
213 uint dblocks = XFS_B_TO_FSB(mp, valuelen);
214 nblks += dblocks;
215 nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
216 }
217
218 return nblks;
219}
220
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000221STATIC int
Dave Chinnera9273ca2010-01-20 10:47:48 +1100222xfs_attr_set_int(
223 struct xfs_inode *dp,
224 struct xfs_name *name,
225 unsigned char *value,
226 int valuelen,
227 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
229 xfs_da_args_t args;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 xfs_fsblock_t firstblock;
231 xfs_bmap_free_t flist;
232 int error, err2, committed;
Nathan Scottaa82daa2005-11-02 10:33:33 +1100233 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 int rsvd = (flags & ATTR_ROOT) != 0;
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000235 int local;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 /*
238 * Attach the dquots to the inode.
239 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200240 error = xfs_qm_dqattach(dp, 0);
241 if (error)
242 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 /*
245 * If the inode doesn't have an attribute fork, add one.
246 * (inode must not be locked when we call this routine)
247 */
248 if (XFS_IFORK_Q(dp) == 0) {
Barry Naujoke5889e92007-02-10 18:35:58 +1100249 int sf_size = sizeof(xfs_attr_sf_hdr_t) +
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000250 XFS_ATTR_SF_ENTSIZE_BYNAME(name->len, valuelen);
Barry Naujoke5889e92007-02-10 18:35:58 +1100251
252 if ((error = xfs_bmap_add_attrfork(dp, sf_size, rsvd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 return(error);
254 }
255
256 /*
257 * Fill in the arg structure for this request.
258 */
259 memset((char *)&args, 0, sizeof(args));
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000260 args.name = name->name;
261 args.namelen = name->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 args.value = value;
263 args.valuelen = valuelen;
264 args.flags = flags;
265 args.hashval = xfs_da_hashname(args.name, args.namelen);
266 args.dp = dp;
267 args.firstblock = &firstblock;
268 args.flist = &flist;
269 args.whichfork = XFS_ATTR_FORK;
Barry Naujok6a178102008-05-21 16:42:05 +1000270 args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 /* Size is now blocks for attribute data */
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000273 args.total = xfs_attr_calc_size(dp, name->len, valuelen, &local);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 /*
276 * Start our first transaction of the day.
277 *
278 * All future transactions during this code must be "chained" off
279 * this one via the trans_dup() call. All transactions will contain
280 * the inode, and the inode will always be marked with trans_ihold().
281 * Since the inode will be locked in all transactions, we must log
282 * the inode in every transaction to let it float upward through
283 * the log.
284 */
285 args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_SET);
286
287 /*
288 * Root fork attributes can use reserved data blocks for this
289 * operation if necessary
290 */
291
292 if (rsvd)
293 args.trans->t_flags |= XFS_TRANS_RESERVE;
294
Jeff Liua21cd502013-01-28 21:27:53 +0800295 error = xfs_trans_reserve(args.trans, args.total,
296 XFS_ATTRSETM_LOG_RES(mp) +
297 XFS_ATTRSETRT_LOG_RES(mp) * args.total,
298 0, XFS_TRANS_PERM_LOG_RES,
299 XFS_ATTRSET_LOG_COUNT);
300 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 xfs_trans_cancel(args.trans, 0);
302 return(error);
303 }
304 xfs_ilock(dp, XFS_ILOCK_EXCL);
305
Christoph Hellwig7d095252009-06-08 15:33:32 +0200306 error = xfs_trans_reserve_quota_nblks(args.trans, dp, args.total, 0,
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000307 rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
308 XFS_QMOPT_RES_REGBLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 if (error) {
310 xfs_iunlock(dp, XFS_ILOCK_EXCL);
311 xfs_trans_cancel(args.trans, XFS_TRANS_RELEASE_LOG_RES);
312 return (error);
313 }
314
Christoph Hellwigddc34152011-09-19 15:00:54 +0000315 xfs_trans_ijoin(args.trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000318 * If the attribute list is non-existent or a shortform list,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 * upgrade it to a single-leaf-block attribute list.
320 */
321 if ((dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) ||
322 ((dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS) &&
323 (dp->i_d.di_anextents == 0))) {
324
325 /*
326 * Build initial attribute list (if required).
327 */
328 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
Nathan Scottd8cc8902005-11-02 10:34:53 +1100329 xfs_attr_shortform_create(&args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 /*
332 * Try to add the attr to the attribute list in
333 * the inode.
334 */
335 error = xfs_attr_shortform_addname(&args);
336 if (error != ENOSPC) {
337 /*
338 * Commit the shortform mods, and we're done.
339 * NOTE: this is also the error path (EEXIST, etc).
340 */
341 ASSERT(args.trans != NULL);
342
343 /*
344 * If this is a synchronous mount, make sure that
345 * the transaction goes to disk before returning
346 * to the user.
347 */
348 if (mp->m_flags & XFS_MOUNT_WSYNC) {
349 xfs_trans_set_sync(args.trans);
350 }
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000351
352 if (!error && (flags & ATTR_KERNOTIME) == 0) {
353 xfs_trans_ichgtime(args.trans, dp,
354 XFS_ICHGTIME_CHG);
355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 err2 = xfs_trans_commit(args.trans,
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000357 XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 xfs_iunlock(dp, XFS_ILOCK_EXCL);
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return(error == 0 ? err2 : error);
361 }
362
363 /*
364 * It won't fit in the shortform, transform to a leaf block.
365 * GROT: another possible req'mt for a double-split btree op.
366 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600367 xfs_bmap_init(args.flist, args.firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 error = xfs_attr_shortform_to_leaf(&args);
369 if (!error) {
370 error = xfs_bmap_finish(&args.trans, args.flist,
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100371 &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 }
373 if (error) {
374 ASSERT(committed);
375 args.trans = NULL;
376 xfs_bmap_cancel(&flist);
377 goto out;
378 }
379
380 /*
381 * bmap_finish() may have committed the last trans and started
382 * a new one. We need the inode to be in all transactions.
383 */
Christoph Hellwig898621d2010-06-24 11:36:58 +1000384 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +0000385 xfs_trans_ijoin(args.trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 /*
388 * Commit the leaf transformation. We'll need another (linked)
389 * transaction to add the new attribute to the leaf.
390 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000391
392 error = xfs_trans_roll(&args.trans, dp);
393 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 goto out;
395
396 }
397
398 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
399 error = xfs_attr_leaf_addname(&args);
400 } else {
401 error = xfs_attr_node_addname(&args);
402 }
403 if (error) {
404 goto out;
405 }
406
407 /*
408 * If this is a synchronous mount, make sure that the
409 * transaction goes to disk before returning to the user.
410 */
411 if (mp->m_flags & XFS_MOUNT_WSYNC) {
412 xfs_trans_set_sync(args.trans);
413 }
414
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000415 if ((flags & ATTR_KERNOTIME) == 0)
416 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 /*
419 * Commit the last in the sequence of transactions.
420 */
421 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000422 error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 xfs_iunlock(dp, XFS_ILOCK_EXCL);
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 return(error);
426
427out:
428 if (args.trans)
429 xfs_trans_cancel(args.trans,
430 XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
431 xfs_iunlock(dp, XFS_ILOCK_EXCL);
432 return(error);
433}
434
Nathan Scottaa82daa2005-11-02 10:33:33 +1100435int
Christoph Hellwig993386c2007-08-28 16:12:30 +1000436xfs_attr_set(
437 xfs_inode_t *dp,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100438 const unsigned char *name,
439 unsigned char *value,
Christoph Hellwig993386c2007-08-28 16:12:30 +1000440 int valuelen,
441 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000443 int error;
444 struct xfs_name xname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Nathan Scottaa82daa2005-11-02 10:33:33 +1100446 XFS_STATS_INC(xs_attr_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Nathan Scottaa82daa2005-11-02 10:33:33 +1100448 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 return (EIO);
450
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000451 error = xfs_attr_name_to_xname(&xname, name);
452 if (error)
453 return error;
454
455 return xfs_attr_set_int(dp, &xname, value, valuelen, flags);
Nathan Scottaa82daa2005-11-02 10:33:33 +1100456}
457
458/*
459 * Generic handler routine to remove a name from an attribute list.
460 * Transitions attribute list from Btree to shortform as necessary.
461 */
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000462STATIC int
463xfs_attr_remove_int(xfs_inode_t *dp, struct xfs_name *name, int flags)
Nathan Scottaa82daa2005-11-02 10:33:33 +1100464{
465 xfs_da_args_t args;
466 xfs_fsblock_t firstblock;
467 xfs_bmap_free_t flist;
468 int error;
469 xfs_mount_t *mp = dp->i_mount;
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 /*
472 * Fill in the arg structure for this request.
473 */
474 memset((char *)&args, 0, sizeof(args));
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000475 args.name = name->name;
476 args.namelen = name->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 args.flags = flags;
478 args.hashval = xfs_da_hashname(args.name, args.namelen);
479 args.dp = dp;
480 args.firstblock = &firstblock;
481 args.flist = &flist;
482 args.total = 0;
483 args.whichfork = XFS_ATTR_FORK;
484
485 /*
Dave Chinner4a338212011-06-23 01:35:01 +0000486 * we have no control over the attribute names that userspace passes us
487 * to remove, so we have to allow the name lookup prior to attribute
488 * removal to fail.
489 */
490 args.op_flags = XFS_DA_OP_OKNOENT;
491
492 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 * Attach the dquots to the inode.
494 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200495 error = xfs_qm_dqattach(dp, 0);
496 if (error)
497 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 /*
500 * Start our first transaction of the day.
501 *
502 * All future transactions during this code must be "chained" off
503 * this one via the trans_dup() call. All transactions will contain
504 * the inode, and the inode will always be marked with trans_ihold().
505 * Since the inode will be locked in all transactions, we must log
506 * the inode in every transaction to let it float upward through
507 * the log.
508 */
509 args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_RM);
510
511 /*
512 * Root fork attributes can use reserved data blocks for this
513 * operation if necessary
514 */
515
516 if (flags & ATTR_ROOT)
517 args.trans->t_flags |= XFS_TRANS_RESERVE;
518
519 if ((error = xfs_trans_reserve(args.trans,
520 XFS_ATTRRM_SPACE_RES(mp),
521 XFS_ATTRRM_LOG_RES(mp),
522 0, XFS_TRANS_PERM_LOG_RES,
523 XFS_ATTRRM_LOG_COUNT))) {
524 xfs_trans_cancel(args.trans, 0);
525 return(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 }
527
528 xfs_ilock(dp, XFS_ILOCK_EXCL);
529 /*
530 * No need to make quota reservations here. We expect to release some
531 * blocks not allocate in the common case.
532 */
Christoph Hellwigddc34152011-09-19 15:00:54 +0000533 xfs_trans_ijoin(args.trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535 /*
536 * Decide on what work routines to call based on the inode size.
537 */
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +1000538 if (!xfs_inode_hasattr(dp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 error = XFS_ERROR(ENOATTR);
540 goto out;
541 }
542 if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
543 ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
544 error = xfs_attr_shortform_remove(&args);
545 if (error) {
546 goto out;
547 }
548 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
549 error = xfs_attr_leaf_removename(&args);
550 } else {
551 error = xfs_attr_node_removename(&args);
552 }
553 if (error) {
554 goto out;
555 }
556
557 /*
558 * If this is a synchronous mount, make sure that the
559 * transaction goes to disk before returning to the user.
560 */
561 if (mp->m_flags & XFS_MOUNT_WSYNC) {
562 xfs_trans_set_sync(args.trans);
563 }
564
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000565 if ((flags & ATTR_KERNOTIME) == 0)
566 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 /*
569 * Commit the last in the sequence of transactions.
570 */
571 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000572 error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 xfs_iunlock(dp, XFS_ILOCK_EXCL);
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 return(error);
576
577out:
578 if (args.trans)
579 xfs_trans_cancel(args.trans,
580 XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
581 xfs_iunlock(dp, XFS_ILOCK_EXCL);
582 return(error);
583}
584
Nathan Scottaa82daa2005-11-02 10:33:33 +1100585int
Christoph Hellwig993386c2007-08-28 16:12:30 +1000586xfs_attr_remove(
587 xfs_inode_t *dp,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100588 const unsigned char *name,
Christoph Hellwig993386c2007-08-28 16:12:30 +1000589 int flags)
Nathan Scottaa82daa2005-11-02 10:33:33 +1100590{
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000591 int error;
592 struct xfs_name xname;
Nathan Scottaa82daa2005-11-02 10:33:33 +1100593
594 XFS_STATS_INC(xs_attr_remove);
595
Nathan Scottaa82daa2005-11-02 10:33:33 +1100596 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
597 return (EIO);
598
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000599 error = xfs_attr_name_to_xname(&xname, name);
600 if (error)
601 return error;
602
Nathan Scottaa82daa2005-11-02 10:33:33 +1100603 xfs_ilock(dp, XFS_ILOCK_SHARED);
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +1000604 if (!xfs_inode_hasattr(dp)) {
Nathan Scottaa82daa2005-11-02 10:33:33 +1100605 xfs_iunlock(dp, XFS_ILOCK_SHARED);
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +1000606 return XFS_ERROR(ENOATTR);
Nathan Scottaa82daa2005-11-02 10:33:33 +1100607 }
608 xfs_iunlock(dp, XFS_ILOCK_SHARED);
609
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000610 return xfs_attr_remove_int(dp, &xname, flags);
Nathan Scottaa82daa2005-11-02 10:33:33 +1100611}
612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614/*========================================================================
615 * External routines when attribute list is inside the inode
616 *========================================================================*/
617
618/*
619 * Add a name to the shortform attribute list structure
620 * This is the external routine.
621 */
622STATIC int
623xfs_attr_shortform_addname(xfs_da_args_t *args)
624{
Nathan Scottd8cc8902005-11-02 10:34:53 +1100625 int newsize, forkoff, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Dave Chinner5a5881c2012-03-22 05:15:13 +0000627 trace_xfs_attr_sf_addname(args);
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 retval = xfs_attr_shortform_lookup(args);
630 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
631 return(retval);
632 } else if (retval == EEXIST) {
633 if (args->flags & ATTR_CREATE)
634 return(retval);
635 retval = xfs_attr_shortform_remove(args);
636 ASSERT(retval == 0);
637 }
638
Nathan Scottd8cc8902005-11-02 10:34:53 +1100639 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
640 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
641 return(XFS_ERROR(ENOSPC));
642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
644 newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100645
646 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
647 if (!forkoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 return(XFS_ERROR(ENOSPC));
Nathan Scottd8cc8902005-11-02 10:34:53 +1100649
650 xfs_attr_shortform_add(args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 return(0);
652}
653
654
655/*========================================================================
656 * External routines when attribute list is one block
657 *========================================================================*/
658
659/*
660 * Add a name to the leaf attribute list structure
661 *
662 * This leaf block cannot have a "remote" value, we only call this routine
663 * if bmap_one_block() says there is only one block (ie: no remote blks).
664 */
David Chinnera8272ce2007-11-23 16:28:09 +1100665STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666xfs_attr_leaf_addname(xfs_da_args_t *args)
667{
668 xfs_inode_t *dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000669 struct xfs_buf *bp;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100670 int retval, error, committed, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Dave Chinner5a5881c2012-03-22 05:15:13 +0000672 trace_xfs_attr_leaf_addname(args);
673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 /*
675 * Read the (only) block in the attribute list in.
676 */
677 dp = args->dp;
678 args->blkno = 0;
Dave Chinner517c2222013-04-24 18:58:55 +1000679 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100681 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
683 /*
684 * Look up the given attribute in the leaf block. Figure out if
685 * the given flags produce an error or call for an atomic rename.
686 */
Dave Chinner517c2222013-04-24 18:58:55 +1000687 retval = xfs_attr3_leaf_lookup_int(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000689 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000690 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 } else if (retval == EEXIST) {
692 if (args->flags & ATTR_CREATE) { /* pure create op */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000693 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000694 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 }
Dave Chinner5a5881c2012-03-22 05:15:13 +0000696
697 trace_xfs_attr_leaf_replace(args);
698
Barry Naujok6a178102008-05-21 16:42:05 +1000699 args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 args->blkno2 = args->blkno; /* set 2nd entry info*/
701 args->index2 = args->index;
702 args->rmtblkno2 = args->rmtblkno;
703 args->rmtblkcnt2 = args->rmtblkcnt;
704 }
705
706 /*
707 * Add the attribute to the leaf block, transitioning to a Btree
708 * if required.
709 */
Dave Chinner517c2222013-04-24 18:58:55 +1000710 retval = xfs_attr3_leaf_add(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 if (retval == ENOSPC) {
712 /*
713 * Promote the attribute list to the Btree format, then
714 * Commit that transaction so that the node_addname() call
715 * can manage its own transactions.
716 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600717 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000718 error = xfs_attr3_leaf_to_node(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 if (!error) {
720 error = xfs_bmap_finish(&args->trans, args->flist,
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100721 &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 }
723 if (error) {
724 ASSERT(committed);
725 args->trans = NULL;
726 xfs_bmap_cancel(args->flist);
727 return(error);
728 }
729
730 /*
731 * bmap_finish() may have committed the last trans and started
732 * a new one. We need the inode to be in all transactions.
733 */
Christoph Hellwig898621d2010-06-24 11:36:58 +1000734 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +0000735 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 /*
738 * Commit the current trans (including the inode) and start
739 * a new one.
740 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000741 error = xfs_trans_roll(&args->trans, dp);
742 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 return (error);
744
745 /*
746 * Fob the whole rest of the problem off on the Btree code.
747 */
748 error = xfs_attr_node_addname(args);
749 return(error);
750 }
751
752 /*
753 * Commit the transaction that added the attr name so that
754 * later routines can manage their own transactions.
755 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000756 error = xfs_trans_roll(&args->trans, dp);
757 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 return (error);
759
760 /*
761 * If there was an out-of-line value, allocate the blocks we
762 * identified for its storage and copy the value. This is done
763 * after we create the attribute so that we don't overflow the
764 * maximum size of a transaction and/or hit a deadlock.
765 */
766 if (args->rmtblkno > 0) {
767 error = xfs_attr_rmtval_set(args);
768 if (error)
769 return(error);
770 }
771
772 /*
773 * If this is an atomic rename operation, we must "flip" the
774 * incomplete flags on the "new" and "old" attribute/value pairs
775 * so that one disappears and one appears atomically. Then we
776 * must remove the "old" attribute/value pair.
777 */
Barry Naujok6a178102008-05-21 16:42:05 +1000778 if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 /*
780 * In a separate transaction, set the incomplete flag on the
781 * "old" attr and clear the incomplete flag on the "new" attr.
782 */
Dave Chinner517c2222013-04-24 18:58:55 +1000783 error = xfs_attr3_leaf_flipflags(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 if (error)
785 return(error);
786
787 /*
788 * Dismantle the "old" attribute/value pair by removing
789 * a "remote" value (if it exists).
790 */
791 args->index = args->index2;
792 args->blkno = args->blkno2;
793 args->rmtblkno = args->rmtblkno2;
794 args->rmtblkcnt = args->rmtblkcnt2;
795 if (args->rmtblkno) {
796 error = xfs_attr_rmtval_remove(args);
797 if (error)
798 return(error);
799 }
800
801 /*
802 * Read in the block containing the "old" attr, then
803 * remove the "old" attr from that block (neat, huh!)
804 */
Dave Chinner517c2222013-04-24 18:58:55 +1000805 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
Dave Chinnerad14c332012-11-12 22:54:16 +1100806 -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100808 return error;
809
Dave Chinner517c2222013-04-24 18:58:55 +1000810 xfs_attr3_leaf_remove(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 /*
813 * If the result is small enough, shrink it all into the inode.
814 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100815 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Eric Sandeen9d87c312009-01-14 23:22:07 -0600816 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000817 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 /* bp is gone due to xfs_da_shrink_inode */
819 if (!error) {
820 error = xfs_bmap_finish(&args->trans,
821 args->flist,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 &committed);
823 }
824 if (error) {
825 ASSERT(committed);
826 args->trans = NULL;
827 xfs_bmap_cancel(args->flist);
828 return(error);
829 }
830
831 /*
832 * bmap_finish() may have committed the last trans
833 * and started a new one. We need the inode to be
834 * in all transactions.
835 */
Christoph Hellwig898621d2010-06-24 11:36:58 +1000836 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +0000837 xfs_trans_ijoin(args->trans, dp, 0);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000838 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
840 /*
841 * Commit the remove and start the next trans in series.
842 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000843 error = xfs_trans_roll(&args->trans, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845 } else if (args->rmtblkno > 0) {
846 /*
847 * Added a "remote" value, just clear the incomplete flag.
848 */
Dave Chinner517c2222013-04-24 18:58:55 +1000849 error = xfs_attr3_leaf_clearflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 }
Dave Chinner517c2222013-04-24 18:58:55 +1000851 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852}
853
854/*
855 * Remove a name from the leaf attribute list structure
856 *
857 * This leaf block cannot have a "remote" value, we only call this routine
858 * if bmap_one_block() says there is only one block (ie: no remote blks).
859 */
860STATIC int
861xfs_attr_leaf_removename(xfs_da_args_t *args)
862{
863 xfs_inode_t *dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000864 struct xfs_buf *bp;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100865 int error, committed, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Dave Chinner5a5881c2012-03-22 05:15:13 +0000867 trace_xfs_attr_leaf_removename(args);
868
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 /*
870 * Remove the attribute.
871 */
872 dp = args->dp;
873 args->blkno = 0;
Dave Chinner517c2222013-04-24 18:58:55 +1000874 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Dave Chinnerad14c332012-11-12 22:54:16 +1100875 if (error)
876 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Dave Chinner517c2222013-04-24 18:58:55 +1000878 error = xfs_attr3_leaf_lookup_int(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 if (error == ENOATTR) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000880 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000881 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 }
883
Dave Chinner517c2222013-04-24 18:58:55 +1000884 xfs_attr3_leaf_remove(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
886 /*
887 * If the result is small enough, shrink it all into the inode.
888 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100889 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Eric Sandeen9d87c312009-01-14 23:22:07 -0600890 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000891 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 /* bp is gone due to xfs_da_shrink_inode */
893 if (!error) {
894 error = xfs_bmap_finish(&args->trans, args->flist,
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100895 &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 }
897 if (error) {
898 ASSERT(committed);
899 args->trans = NULL;
900 xfs_bmap_cancel(args->flist);
Dave Chinner517c2222013-04-24 18:58:55 +1000901 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 }
903
904 /*
905 * bmap_finish() may have committed the last trans and started
906 * a new one. We need the inode to be in all transactions.
907 */
Christoph Hellwig898621d2010-06-24 11:36:58 +1000908 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +0000909 xfs_trans_ijoin(args->trans, dp, 0);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000910 }
Dave Chinner517c2222013-04-24 18:58:55 +1000911 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912}
913
914/*
915 * Look up a name in a leaf attribute list structure.
916 *
917 * This leaf block cannot have a "remote" value, we only call this routine
918 * if bmap_one_block() says there is only one block (ie: no remote blks).
919 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000920STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921xfs_attr_leaf_get(xfs_da_args_t *args)
922{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000923 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 int error;
925
Dave Chinneree732592012-11-12 22:53:53 +1100926 trace_xfs_attr_leaf_get(args);
927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 args->blkno = 0;
Dave Chinner517c2222013-04-24 18:58:55 +1000929 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100931 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Dave Chinner517c2222013-04-24 18:58:55 +1000933 error = xfs_attr3_leaf_lookup_int(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 if (error != EEXIST) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000935 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000936 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 }
Dave Chinner517c2222013-04-24 18:58:55 +1000938 error = xfs_attr3_leaf_getvalue(bp, args);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000939 xfs_trans_brelse(args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
941 error = xfs_attr_rmtval_get(args);
942 }
Dave Chinner517c2222013-04-24 18:58:55 +1000943 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944}
945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946/*========================================================================
947 * External routines when attribute list size > XFS_LBSIZE(mp).
948 *========================================================================*/
949
950/*
951 * Add a name to a Btree-format attribute list.
952 *
953 * This will involve walking down the Btree, and may involve splitting
954 * leaf nodes and even splitting intermediate nodes up to and including
955 * the root node (a special case of an intermediate node).
956 *
957 * "Remote" attribute values confuse the issue and atomic rename operations
958 * add a whole extra layer of confusion on top of that.
959 */
960STATIC int
961xfs_attr_node_addname(xfs_da_args_t *args)
962{
963 xfs_da_state_t *state;
964 xfs_da_state_blk_t *blk;
965 xfs_inode_t *dp;
966 xfs_mount_t *mp;
967 int committed, retval, error;
968
Dave Chinner5a5881c2012-03-22 05:15:13 +0000969 trace_xfs_attr_node_addname(args);
970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 /*
972 * Fill in bucket of arguments/results/context to carry around.
973 */
974 dp = args->dp;
975 mp = dp->i_mount;
976restart:
977 state = xfs_da_state_alloc();
978 state->args = args;
979 state->mp = mp;
980 state->blocksize = state->mp->m_sb.sb_blocksize;
981 state->node_ents = state->mp->m_attr_node_ents;
982
983 /*
984 * Search to see if name already exists, and get back a pointer
985 * to where it should go.
986 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000987 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 if (error)
989 goto out;
990 blk = &state->path.blk[ state->path.active-1 ];
991 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
992 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
993 goto out;
994 } else if (retval == EEXIST) {
995 if (args->flags & ATTR_CREATE)
996 goto out;
Dave Chinner5a5881c2012-03-22 05:15:13 +0000997
998 trace_xfs_attr_node_replace(args);
999
Barry Naujok6a178102008-05-21 16:42:05 +10001000 args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 args->blkno2 = args->blkno; /* set 2nd entry info*/
1002 args->index2 = args->index;
1003 args->rmtblkno2 = args->rmtblkno;
1004 args->rmtblkcnt2 = args->rmtblkcnt;
1005 args->rmtblkno = 0;
1006 args->rmtblkcnt = 0;
1007 }
1008
Dave Chinner517c2222013-04-24 18:58:55 +10001009 retval = xfs_attr3_leaf_add(blk->bp, state->args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 if (retval == ENOSPC) {
1011 if (state->path.active == 1) {
1012 /*
1013 * Its really a single leaf node, but it had
1014 * out-of-line values so it looked like it *might*
1015 * have been a b-tree.
1016 */
1017 xfs_da_state_free(state);
Eric Sandeen9d87c312009-01-14 23:22:07 -06001018 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +10001019 error = xfs_attr3_leaf_to_node(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 if (!error) {
1021 error = xfs_bmap_finish(&args->trans,
1022 args->flist,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 &committed);
1024 }
1025 if (error) {
1026 ASSERT(committed);
1027 args->trans = NULL;
1028 xfs_bmap_cancel(args->flist);
1029 goto out;
1030 }
1031
1032 /*
1033 * bmap_finish() may have committed the last trans
1034 * and started a new one. We need the inode to be
1035 * in all transactions.
1036 */
Christoph Hellwig898621d2010-06-24 11:36:58 +10001037 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +00001038 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
1040 /*
1041 * Commit the node conversion and start the next
1042 * trans in the chain.
1043 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10001044 error = xfs_trans_roll(&args->trans, dp);
1045 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 goto out;
1047
1048 goto restart;
1049 }
1050
1051 /*
1052 * Split as many Btree elements as required.
1053 * This code tracks the new and old attr's location
1054 * in the index/blkno/rmtblkno/rmtblkcnt fields and
1055 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
1056 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06001057 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001058 error = xfs_da3_split(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 if (!error) {
1060 error = xfs_bmap_finish(&args->trans, args->flist,
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001061 &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 }
1063 if (error) {
1064 ASSERT(committed);
1065 args->trans = NULL;
1066 xfs_bmap_cancel(args->flist);
1067 goto out;
1068 }
1069
1070 /*
1071 * bmap_finish() may have committed the last trans and started
1072 * a new one. We need the inode to be in all transactions.
1073 */
Christoph Hellwig898621d2010-06-24 11:36:58 +10001074 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +00001075 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 } else {
1077 /*
1078 * Addition succeeded, update Btree hashvals.
1079 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001080 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 }
1082
1083 /*
1084 * Kill the state structure, we're done with it and need to
1085 * allow the buffers to come back later.
1086 */
1087 xfs_da_state_free(state);
1088 state = NULL;
1089
1090 /*
1091 * Commit the leaf addition or btree split and start the next
1092 * trans in the chain.
1093 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10001094 error = xfs_trans_roll(&args->trans, dp);
1095 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 goto out;
1097
1098 /*
1099 * If there was an out-of-line value, allocate the blocks we
1100 * identified for its storage and copy the value. This is done
1101 * after we create the attribute so that we don't overflow the
1102 * maximum size of a transaction and/or hit a deadlock.
1103 */
1104 if (args->rmtblkno > 0) {
1105 error = xfs_attr_rmtval_set(args);
1106 if (error)
1107 return(error);
1108 }
1109
1110 /*
1111 * If this is an atomic rename operation, we must "flip" the
1112 * incomplete flags on the "new" and "old" attribute/value pairs
1113 * so that one disappears and one appears atomically. Then we
1114 * must remove the "old" attribute/value pair.
1115 */
Barry Naujok6a178102008-05-21 16:42:05 +10001116 if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 /*
1118 * In a separate transaction, set the incomplete flag on the
1119 * "old" attr and clear the incomplete flag on the "new" attr.
1120 */
Dave Chinner517c2222013-04-24 18:58:55 +10001121 error = xfs_attr3_leaf_flipflags(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 if (error)
1123 goto out;
1124
1125 /*
1126 * Dismantle the "old" attribute/value pair by removing
1127 * a "remote" value (if it exists).
1128 */
1129 args->index = args->index2;
1130 args->blkno = args->blkno2;
1131 args->rmtblkno = args->rmtblkno2;
1132 args->rmtblkcnt = args->rmtblkcnt2;
1133 if (args->rmtblkno) {
1134 error = xfs_attr_rmtval_remove(args);
1135 if (error)
1136 return(error);
1137 }
1138
1139 /*
1140 * Re-find the "old" attribute entry after any split ops.
1141 * The INCOMPLETE flag means that we will find the "old"
1142 * attr, not the "new" one.
1143 */
1144 args->flags |= XFS_ATTR_INCOMPLETE;
1145 state = xfs_da_state_alloc();
1146 state->args = args;
1147 state->mp = mp;
1148 state->blocksize = state->mp->m_sb.sb_blocksize;
1149 state->node_ents = state->mp->m_attr_node_ents;
1150 state->inleaf = 0;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001151 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 if (error)
1153 goto out;
1154
1155 /*
1156 * Remove the name and update the hashvals in the tree.
1157 */
1158 blk = &state->path.blk[ state->path.active-1 ];
1159 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner517c2222013-04-24 18:58:55 +10001160 error = xfs_attr3_leaf_remove(blk->bp, args);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001161 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
1163 /*
1164 * Check to see if the tree needs to be collapsed.
1165 */
1166 if (retval && (state->path.active > 1)) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001167 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001168 error = xfs_da3_join(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 if (!error) {
1170 error = xfs_bmap_finish(&args->trans,
1171 args->flist,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 &committed);
1173 }
1174 if (error) {
1175 ASSERT(committed);
1176 args->trans = NULL;
1177 xfs_bmap_cancel(args->flist);
1178 goto out;
1179 }
1180
1181 /*
1182 * bmap_finish() may have committed the last trans
1183 * and started a new one. We need the inode to be
1184 * in all transactions.
1185 */
Christoph Hellwig898621d2010-06-24 11:36:58 +10001186 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +00001187 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 }
1189
1190 /*
1191 * Commit and start the next trans in the chain.
1192 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10001193 error = xfs_trans_roll(&args->trans, dp);
1194 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 goto out;
1196
1197 } else if (args->rmtblkno > 0) {
1198 /*
1199 * Added a "remote" value, just clear the incomplete flag.
1200 */
Dave Chinner517c2222013-04-24 18:58:55 +10001201 error = xfs_attr3_leaf_clearflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 if (error)
1203 goto out;
1204 }
1205 retval = error = 0;
1206
1207out:
1208 if (state)
1209 xfs_da_state_free(state);
1210 if (error)
1211 return(error);
1212 return(retval);
1213}
1214
1215/*
1216 * Remove a name from a B-tree attribute list.
1217 *
1218 * This will involve walking down the Btree, and may involve joining
1219 * leaf nodes and even joining intermediate nodes up to and including
1220 * the root node (a special case of an intermediate node).
1221 */
1222STATIC int
1223xfs_attr_node_removename(xfs_da_args_t *args)
1224{
1225 xfs_da_state_t *state;
1226 xfs_da_state_blk_t *blk;
1227 xfs_inode_t *dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001228 struct xfs_buf *bp;
Nathan Scottd8cc8902005-11-02 10:34:53 +11001229 int retval, error, committed, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Dave Chinner5a5881c2012-03-22 05:15:13 +00001231 trace_xfs_attr_node_removename(args);
1232
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 /*
1234 * Tie a string around our finger to remind us where we are.
1235 */
1236 dp = args->dp;
1237 state = xfs_da_state_alloc();
1238 state->args = args;
1239 state->mp = dp->i_mount;
1240 state->blocksize = state->mp->m_sb.sb_blocksize;
1241 state->node_ents = state->mp->m_attr_node_ents;
1242
1243 /*
1244 * Search to see if name exists, and get back a pointer to it.
1245 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001246 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 if (error || (retval != EEXIST)) {
1248 if (error == 0)
1249 error = retval;
1250 goto out;
1251 }
1252
1253 /*
1254 * If there is an out-of-line value, de-allocate the blocks.
1255 * This is done before we remove the attribute so that we don't
1256 * overflow the maximum size of a transaction and/or hit a deadlock.
1257 */
1258 blk = &state->path.blk[ state->path.active-1 ];
1259 ASSERT(blk->bp != NULL);
1260 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1261 if (args->rmtblkno > 0) {
1262 /*
1263 * Fill in disk block numbers in the state structure
1264 * so that we can get the buffers back after we commit
1265 * several transactions in the following calls.
1266 */
1267 error = xfs_attr_fillstate(state);
1268 if (error)
1269 goto out;
1270
1271 /*
1272 * Mark the attribute as INCOMPLETE, then bunmapi() the
1273 * remote value.
1274 */
Dave Chinner517c2222013-04-24 18:58:55 +10001275 error = xfs_attr3_leaf_setflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 if (error)
1277 goto out;
1278 error = xfs_attr_rmtval_remove(args);
1279 if (error)
1280 goto out;
1281
1282 /*
1283 * Refill the state structure with buffers, the prior calls
1284 * released our buffers.
1285 */
1286 error = xfs_attr_refillstate(state);
1287 if (error)
1288 goto out;
1289 }
1290
1291 /*
1292 * Remove the name and update the hashvals in the tree.
1293 */
1294 blk = &state->path.blk[ state->path.active-1 ];
1295 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner517c2222013-04-24 18:58:55 +10001296 retval = xfs_attr3_leaf_remove(blk->bp, args);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001297 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
1299 /*
1300 * Check to see if the tree needs to be collapsed.
1301 */
1302 if (retval && (state->path.active > 1)) {
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_join(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
1323 /*
1324 * Commit the Btree join operation and start a new trans.
1325 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10001326 error = xfs_trans_roll(&args->trans, dp);
1327 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 goto out;
1329 }
1330
1331 /*
1332 * If the result is small enough, push it all into the inode.
1333 */
1334 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
1335 /*
1336 * Have to get rid of the copy of this dabuf in the state.
1337 */
1338 ASSERT(state->path.active == 1);
1339 ASSERT(state->path.blk[0].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 state->path.blk[0].bp = NULL;
1341
Dave Chinner517c2222013-04-24 18:58:55 +10001342 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 if (error)
1344 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
Nathan Scottd8cc8902005-11-02 10:34:53 +11001346 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001347 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +10001348 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 /* bp is gone due to xfs_da_shrink_inode */
1350 if (!error) {
1351 error = xfs_bmap_finish(&args->trans,
1352 args->flist,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 &committed);
1354 }
1355 if (error) {
1356 ASSERT(committed);
1357 args->trans = NULL;
1358 xfs_bmap_cancel(args->flist);
1359 goto out;
1360 }
1361
1362 /*
1363 * bmap_finish() may have committed the last trans
1364 * and started a new one. We need the inode to be
1365 * in all transactions.
1366 */
Christoph Hellwig898621d2010-06-24 11:36:58 +10001367 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +00001368 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 } else
Dave Chinner1d9025e2012-06-22 18:50:14 +10001370 xfs_trans_brelse(args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 }
1372 error = 0;
1373
1374out:
1375 xfs_da_state_free(state);
1376 return(error);
1377}
1378
1379/*
1380 * Fill in the disk block numbers in the state structure for the buffers
1381 * that are attached to the state structure.
1382 * This is done so that we can quickly reattach ourselves to those buffers
Nathan Scottc41564b2006-03-29 08:55:14 +10001383 * after some set of transaction commits have released these buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 */
1385STATIC int
1386xfs_attr_fillstate(xfs_da_state_t *state)
1387{
1388 xfs_da_state_path_t *path;
1389 xfs_da_state_blk_t *blk;
1390 int level;
1391
Dave Chinneree732592012-11-12 22:53:53 +11001392 trace_xfs_attr_fillstate(state->args);
1393
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 /*
1395 * Roll down the "path" in the state structure, storing the on-disk
1396 * block number for those buffers in the "path".
1397 */
1398 path = &state->path;
1399 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1400 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1401 if (blk->bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001402 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 blk->bp = NULL;
1404 } else {
1405 blk->disk_blkno = 0;
1406 }
1407 }
1408
1409 /*
1410 * Roll down the "altpath" in the state structure, storing the on-disk
1411 * block number for those buffers in the "altpath".
1412 */
1413 path = &state->altpath;
1414 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1415 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1416 if (blk->bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001417 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 blk->bp = NULL;
1419 } else {
1420 blk->disk_blkno = 0;
1421 }
1422 }
1423
1424 return(0);
1425}
1426
1427/*
1428 * Reattach the buffers to the state structure based on the disk block
1429 * numbers stored in the state structure.
Nathan Scottc41564b2006-03-29 08:55:14 +10001430 * This is done after some set of transaction commits have released those
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 * buffers from our grip.
1432 */
1433STATIC int
1434xfs_attr_refillstate(xfs_da_state_t *state)
1435{
1436 xfs_da_state_path_t *path;
1437 xfs_da_state_blk_t *blk;
1438 int level, error;
1439
Dave Chinneree732592012-11-12 22:53:53 +11001440 trace_xfs_attr_refillstate(state->args);
1441
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 /*
1443 * Roll down the "path" in the state structure, storing the on-disk
1444 * block number for those buffers in the "path".
1445 */
1446 path = &state->path;
1447 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1448 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1449 if (blk->disk_blkno) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001450 error = xfs_da3_node_read(state->args->trans,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 state->args->dp,
1452 blk->blkno, blk->disk_blkno,
Dave Chinnerd9392a42012-11-12 22:54:17 +11001453 &blk->bp, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 if (error)
1455 return(error);
1456 } else {
1457 blk->bp = NULL;
1458 }
1459 }
1460
1461 /*
1462 * Roll down the "altpath" in the state structure, storing the on-disk
1463 * block number for those buffers in the "altpath".
1464 */
1465 path = &state->altpath;
1466 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1467 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1468 if (blk->disk_blkno) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001469 error = xfs_da3_node_read(state->args->trans,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 state->args->dp,
1471 blk->blkno, blk->disk_blkno,
Dave Chinnerd9392a42012-11-12 22:54:17 +11001472 &blk->bp, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 if (error)
1474 return(error);
1475 } else {
1476 blk->bp = NULL;
1477 }
1478 }
1479
1480 return(0);
1481}
1482
1483/*
1484 * Look up a filename in a node attribute list.
1485 *
1486 * This routine gets called for any attribute fork that has more than one
1487 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1488 * "remote" values taking up more blocks.
1489 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001490STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491xfs_attr_node_get(xfs_da_args_t *args)
1492{
1493 xfs_da_state_t *state;
1494 xfs_da_state_blk_t *blk;
1495 int error, retval;
1496 int i;
1497
Dave Chinneree732592012-11-12 22:53:53 +11001498 trace_xfs_attr_node_get(args);
1499
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 state = xfs_da_state_alloc();
1501 state->args = args;
1502 state->mp = args->dp->i_mount;
1503 state->blocksize = state->mp->m_sb.sb_blocksize;
1504 state->node_ents = state->mp->m_attr_node_ents;
1505
1506 /*
1507 * Search to see if name exists, and get back a pointer to it.
1508 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001509 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 if (error) {
1511 retval = error;
1512 } else if (retval == EEXIST) {
1513 blk = &state->path.blk[ state->path.active-1 ];
1514 ASSERT(blk->bp != NULL);
1515 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1516
1517 /*
1518 * Get the value, local or "remote"
1519 */
Dave Chinner517c2222013-04-24 18:58:55 +10001520 retval = xfs_attr3_leaf_getvalue(blk->bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 if (!retval && (args->rmtblkno > 0)
1522 && !(args->flags & ATTR_KERNOVAL)) {
1523 retval = xfs_attr_rmtval_get(args);
1524 }
1525 }
1526
1527 /*
1528 * If not in a transaction, we have to release all the buffers.
1529 */
1530 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001531 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 state->path.blk[i].bp = NULL;
1533 }
1534
1535 xfs_da_state_free(state);
1536 return(retval);
1537}