blob: 91acc38f664b3cbae87e08b6e13d5bf2a5fe0b19 [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 Hellwig739bfb22007-08-29 10:58:01 +100043#include "xfs_vnodeops.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000044#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46/*
47 * xfs_attr.c
48 *
49 * Provide the external interfaces to manage attribute lists.
50 */
51
52/*========================================================================
53 * Function prototypes for the kernel.
54 *========================================================================*/
55
56/*
57 * Internal routines when attribute list fits inside the inode.
58 */
59STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
60
61/*
62 * Internal routines when attribute list is one block.
63 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100064STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065STATIC int xfs_attr_leaf_addname(xfs_da_args_t *args);
66STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68/*
69 * Internal routines when attribute list is more than one block.
70 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100071STATIC int xfs_attr_node_get(xfs_da_args_t *args);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072STATIC int xfs_attr_node_addname(xfs_da_args_t *args);
73STATIC int xfs_attr_node_removename(xfs_da_args_t *args);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074STATIC 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
Dave Chinnerabec5f22013-08-12 20:49:38 +100093int
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +100094xfs_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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615/*========================================================================
616 * External routines when attribute list is inside the inode
617 *========================================================================*/
618
619/*
620 * Add a name to the shortform attribute list structure
621 * This is the external routine.
622 */
623STATIC int
624xfs_attr_shortform_addname(xfs_da_args_t *args)
625{
Nathan Scottd8cc8902005-11-02 10:34:53 +1100626 int newsize, forkoff, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Dave Chinner5a5881c2012-03-22 05:15:13 +0000628 trace_xfs_attr_sf_addname(args);
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 retval = xfs_attr_shortform_lookup(args);
631 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
632 return(retval);
633 } else if (retval == EEXIST) {
634 if (args->flags & ATTR_CREATE)
635 return(retval);
636 retval = xfs_attr_shortform_remove(args);
637 ASSERT(retval == 0);
638 }
639
Nathan Scottd8cc8902005-11-02 10:34:53 +1100640 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
641 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
642 return(XFS_ERROR(ENOSPC));
643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
645 newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100646
647 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
648 if (!forkoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return(XFS_ERROR(ENOSPC));
Nathan Scottd8cc8902005-11-02 10:34:53 +1100650
651 xfs_attr_shortform_add(args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 return(0);
653}
654
655
656/*========================================================================
657 * External routines when attribute list is one block
658 *========================================================================*/
659
660/*
661 * Add a name to the leaf attribute list structure
662 *
663 * This leaf block cannot have a "remote" value, we only call this routine
664 * if bmap_one_block() says there is only one block (ie: no remote blks).
665 */
David Chinnera8272ce2007-11-23 16:28:09 +1100666STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667xfs_attr_leaf_addname(xfs_da_args_t *args)
668{
669 xfs_inode_t *dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000670 struct xfs_buf *bp;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100671 int retval, error, committed, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Dave Chinner5a5881c2012-03-22 05:15:13 +0000673 trace_xfs_attr_leaf_addname(args);
674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 /*
676 * Read the (only) block in the attribute list in.
677 */
678 dp = args->dp;
679 args->blkno = 0;
Dave Chinner517c2222013-04-24 18:58:55 +1000680 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100682 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
684 /*
685 * Look up the given attribute in the leaf block. Figure out if
686 * the given flags produce an error or call for an atomic rename.
687 */
Dave Chinner517c2222013-04-24 18:58:55 +1000688 retval = xfs_attr3_leaf_lookup_int(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000690 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000691 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 } else if (retval == EEXIST) {
693 if (args->flags & ATTR_CREATE) { /* pure create op */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000694 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000695 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 }
Dave Chinner5a5881c2012-03-22 05:15:13 +0000697
698 trace_xfs_attr_leaf_replace(args);
699
Barry Naujok6a178102008-05-21 16:42:05 +1000700 args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 args->blkno2 = args->blkno; /* set 2nd entry info*/
702 args->index2 = args->index;
703 args->rmtblkno2 = args->rmtblkno;
704 args->rmtblkcnt2 = args->rmtblkcnt;
705 }
706
707 /*
708 * Add the attribute to the leaf block, transitioning to a Btree
709 * if required.
710 */
Dave Chinner517c2222013-04-24 18:58:55 +1000711 retval = xfs_attr3_leaf_add(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 if (retval == ENOSPC) {
713 /*
714 * Promote the attribute list to the Btree format, then
715 * Commit that transaction so that the node_addname() call
716 * can manage its own transactions.
717 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600718 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000719 error = xfs_attr3_leaf_to_node(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 if (!error) {
721 error = xfs_bmap_finish(&args->trans, args->flist,
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100722 &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 }
724 if (error) {
725 ASSERT(committed);
726 args->trans = NULL;
727 xfs_bmap_cancel(args->flist);
728 return(error);
729 }
730
731 /*
732 * bmap_finish() may have committed the last trans and started
733 * a new one. We need the inode to be in all transactions.
734 */
Christoph Hellwig898621d2010-06-24 11:36:58 +1000735 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +0000736 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
738 /*
739 * Commit the current trans (including the inode) and start
740 * a new one.
741 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000742 error = xfs_trans_roll(&args->trans, dp);
743 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 return (error);
745
746 /*
747 * Fob the whole rest of the problem off on the Btree code.
748 */
749 error = xfs_attr_node_addname(args);
750 return(error);
751 }
752
753 /*
754 * Commit the transaction that added the attr name so that
755 * later routines can manage their own transactions.
756 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000757 error = xfs_trans_roll(&args->trans, dp);
758 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 return (error);
760
761 /*
762 * If there was an out-of-line value, allocate the blocks we
763 * identified for its storage and copy the value. This is done
764 * after we create the attribute so that we don't overflow the
765 * maximum size of a transaction and/or hit a deadlock.
766 */
767 if (args->rmtblkno > 0) {
768 error = xfs_attr_rmtval_set(args);
769 if (error)
770 return(error);
771 }
772
773 /*
774 * If this is an atomic rename operation, we must "flip" the
775 * incomplete flags on the "new" and "old" attribute/value pairs
776 * so that one disappears and one appears atomically. Then we
777 * must remove the "old" attribute/value pair.
778 */
Barry Naujok6a178102008-05-21 16:42:05 +1000779 if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 /*
781 * In a separate transaction, set the incomplete flag on the
782 * "old" attr and clear the incomplete flag on the "new" attr.
783 */
Dave Chinner517c2222013-04-24 18:58:55 +1000784 error = xfs_attr3_leaf_flipflags(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 if (error)
786 return(error);
787
788 /*
789 * Dismantle the "old" attribute/value pair by removing
790 * a "remote" value (if it exists).
791 */
792 args->index = args->index2;
793 args->blkno = args->blkno2;
794 args->rmtblkno = args->rmtblkno2;
795 args->rmtblkcnt = args->rmtblkcnt2;
796 if (args->rmtblkno) {
797 error = xfs_attr_rmtval_remove(args);
798 if (error)
799 return(error);
800 }
801
802 /*
803 * Read in the block containing the "old" attr, then
804 * remove the "old" attr from that block (neat, huh!)
805 */
Dave Chinner517c2222013-04-24 18:58:55 +1000806 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
Dave Chinnerad14c332012-11-12 22:54:16 +1100807 -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100809 return error;
810
Dave Chinner517c2222013-04-24 18:58:55 +1000811 xfs_attr3_leaf_remove(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
813 /*
814 * If the result is small enough, shrink it all into the inode.
815 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100816 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Eric Sandeen9d87c312009-01-14 23:22:07 -0600817 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000818 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 /* bp is gone due to xfs_da_shrink_inode */
820 if (!error) {
821 error = xfs_bmap_finish(&args->trans,
822 args->flist,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 &committed);
824 }
825 if (error) {
826 ASSERT(committed);
827 args->trans = NULL;
828 xfs_bmap_cancel(args->flist);
829 return(error);
830 }
831
832 /*
833 * bmap_finish() may have committed the last trans
834 * and started a new one. We need the inode to be
835 * in all transactions.
836 */
Christoph Hellwig898621d2010-06-24 11:36:58 +1000837 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +0000838 xfs_trans_ijoin(args->trans, dp, 0);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841 /*
842 * Commit the remove and start the next trans in series.
843 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000844 error = xfs_trans_roll(&args->trans, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
846 } else if (args->rmtblkno > 0) {
847 /*
848 * Added a "remote" value, just clear the incomplete flag.
849 */
Dave Chinner517c2222013-04-24 18:58:55 +1000850 error = xfs_attr3_leaf_clearflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 }
Dave Chinner517c2222013-04-24 18:58:55 +1000852 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853}
854
855/*
856 * Remove a name from the leaf attribute list structure
857 *
858 * This leaf block cannot have a "remote" value, we only call this routine
859 * if bmap_one_block() says there is only one block (ie: no remote blks).
860 */
861STATIC int
862xfs_attr_leaf_removename(xfs_da_args_t *args)
863{
864 xfs_inode_t *dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000865 struct xfs_buf *bp;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100866 int error, committed, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Dave Chinner5a5881c2012-03-22 05:15:13 +0000868 trace_xfs_attr_leaf_removename(args);
869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 /*
871 * Remove the attribute.
872 */
873 dp = args->dp;
874 args->blkno = 0;
Dave Chinner517c2222013-04-24 18:58:55 +1000875 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Dave Chinnerad14c332012-11-12 22:54:16 +1100876 if (error)
877 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Dave Chinner517c2222013-04-24 18:58:55 +1000879 error = xfs_attr3_leaf_lookup_int(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 if (error == ENOATTR) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000881 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000882 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 }
884
Dave Chinner517c2222013-04-24 18:58:55 +1000885 xfs_attr3_leaf_remove(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
887 /*
888 * If the result is small enough, shrink it all into the inode.
889 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100890 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Eric Sandeen9d87c312009-01-14 23:22:07 -0600891 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000892 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 /* bp is gone due to xfs_da_shrink_inode */
894 if (!error) {
895 error = xfs_bmap_finish(&args->trans, args->flist,
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100896 &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 }
898 if (error) {
899 ASSERT(committed);
900 args->trans = NULL;
901 xfs_bmap_cancel(args->flist);
Dave Chinner517c2222013-04-24 18:58:55 +1000902 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 }
904
905 /*
906 * bmap_finish() may have committed the last trans and started
907 * a new one. We need the inode to be in all transactions.
908 */
Christoph Hellwig898621d2010-06-24 11:36:58 +1000909 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +0000910 xfs_trans_ijoin(args->trans, dp, 0);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000911 }
Dave Chinner517c2222013-04-24 18:58:55 +1000912 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913}
914
915/*
916 * Look up a name in a leaf attribute list structure.
917 *
918 * This leaf block cannot have a "remote" value, we only call this routine
919 * if bmap_one_block() says there is only one block (ie: no remote blks).
920 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000921STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922xfs_attr_leaf_get(xfs_da_args_t *args)
923{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000924 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 int error;
926
Dave Chinneree732592012-11-12 22:53:53 +1100927 trace_xfs_attr_leaf_get(args);
928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 args->blkno = 0;
Dave Chinner517c2222013-04-24 18:58:55 +1000930 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100932 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Dave Chinner517c2222013-04-24 18:58:55 +1000934 error = xfs_attr3_leaf_lookup_int(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 if (error != EEXIST) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000936 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000937 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 }
Dave Chinner517c2222013-04-24 18:58:55 +1000939 error = xfs_attr3_leaf_getvalue(bp, args);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000940 xfs_trans_brelse(args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
942 error = xfs_attr_rmtval_get(args);
943 }
Dave Chinner517c2222013-04-24 18:58:55 +1000944 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945}
946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947/*========================================================================
948 * External routines when attribute list size > XFS_LBSIZE(mp).
949 *========================================================================*/
950
951/*
952 * Add a name to a Btree-format attribute list.
953 *
954 * This will involve walking down the Btree, and may involve splitting
955 * leaf nodes and even splitting intermediate nodes up to and including
956 * the root node (a special case of an intermediate node).
957 *
958 * "Remote" attribute values confuse the issue and atomic rename operations
959 * add a whole extra layer of confusion on top of that.
960 */
961STATIC int
962xfs_attr_node_addname(xfs_da_args_t *args)
963{
964 xfs_da_state_t *state;
965 xfs_da_state_blk_t *blk;
966 xfs_inode_t *dp;
967 xfs_mount_t *mp;
968 int committed, retval, error;
969
Dave Chinner5a5881c2012-03-22 05:15:13 +0000970 trace_xfs_attr_node_addname(args);
971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 /*
973 * Fill in bucket of arguments/results/context to carry around.
974 */
975 dp = args->dp;
976 mp = dp->i_mount;
977restart:
978 state = xfs_da_state_alloc();
979 state->args = args;
980 state->mp = mp;
981 state->blocksize = state->mp->m_sb.sb_blocksize;
982 state->node_ents = state->mp->m_attr_node_ents;
983
984 /*
985 * Search to see if name already exists, and get back a pointer
986 * to where it should go.
987 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000988 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 if (error)
990 goto out;
991 blk = &state->path.blk[ state->path.active-1 ];
992 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
993 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
994 goto out;
995 } else if (retval == EEXIST) {
996 if (args->flags & ATTR_CREATE)
997 goto out;
Dave Chinner5a5881c2012-03-22 05:15:13 +0000998
999 trace_xfs_attr_node_replace(args);
1000
Barry Naujok6a178102008-05-21 16:42:05 +10001001 args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 args->blkno2 = args->blkno; /* set 2nd entry info*/
1003 args->index2 = args->index;
1004 args->rmtblkno2 = args->rmtblkno;
1005 args->rmtblkcnt2 = args->rmtblkcnt;
1006 args->rmtblkno = 0;
1007 args->rmtblkcnt = 0;
1008 }
1009
Dave Chinner517c2222013-04-24 18:58:55 +10001010 retval = xfs_attr3_leaf_add(blk->bp, state->args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 if (retval == ENOSPC) {
1012 if (state->path.active == 1) {
1013 /*
1014 * Its really a single leaf node, but it had
1015 * out-of-line values so it looked like it *might*
1016 * have been a b-tree.
1017 */
1018 xfs_da_state_free(state);
Eric Sandeen9d87c312009-01-14 23:22:07 -06001019 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +10001020 error = xfs_attr3_leaf_to_node(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 if (!error) {
1022 error = xfs_bmap_finish(&args->trans,
1023 args->flist,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 &committed);
1025 }
1026 if (error) {
1027 ASSERT(committed);
1028 args->trans = NULL;
1029 xfs_bmap_cancel(args->flist);
1030 goto out;
1031 }
1032
1033 /*
1034 * bmap_finish() may have committed the last trans
1035 * and started a new one. We need the inode to be
1036 * in all transactions.
1037 */
Christoph Hellwig898621d2010-06-24 11:36:58 +10001038 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +00001039 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
1041 /*
1042 * Commit the node conversion and start the next
1043 * trans in the chain.
1044 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10001045 error = xfs_trans_roll(&args->trans, dp);
1046 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 goto out;
1048
1049 goto restart;
1050 }
1051
1052 /*
1053 * Split as many Btree elements as required.
1054 * This code tracks the new and old attr's location
1055 * in the index/blkno/rmtblkno/rmtblkcnt fields and
1056 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
1057 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06001058 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001059 error = xfs_da3_split(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 if (!error) {
1061 error = xfs_bmap_finish(&args->trans, args->flist,
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001062 &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 }
1064 if (error) {
1065 ASSERT(committed);
1066 args->trans = NULL;
1067 xfs_bmap_cancel(args->flist);
1068 goto out;
1069 }
1070
1071 /*
1072 * bmap_finish() may have committed the last trans and started
1073 * a new one. We need the inode to be in all transactions.
1074 */
Christoph Hellwig898621d2010-06-24 11:36:58 +10001075 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +00001076 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 } else {
1078 /*
1079 * Addition succeeded, update Btree hashvals.
1080 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001081 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 }
1083
1084 /*
1085 * Kill the state structure, we're done with it and need to
1086 * allow the buffers to come back later.
1087 */
1088 xfs_da_state_free(state);
1089 state = NULL;
1090
1091 /*
1092 * Commit the leaf addition or btree split and start the next
1093 * trans in the chain.
1094 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10001095 error = xfs_trans_roll(&args->trans, dp);
1096 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 goto out;
1098
1099 /*
1100 * If there was an out-of-line value, allocate the blocks we
1101 * identified for its storage and copy the value. This is done
1102 * after we create the attribute so that we don't overflow the
1103 * maximum size of a transaction and/or hit a deadlock.
1104 */
1105 if (args->rmtblkno > 0) {
1106 error = xfs_attr_rmtval_set(args);
1107 if (error)
1108 return(error);
1109 }
1110
1111 /*
1112 * If this is an atomic rename operation, we must "flip" the
1113 * incomplete flags on the "new" and "old" attribute/value pairs
1114 * so that one disappears and one appears atomically. Then we
1115 * must remove the "old" attribute/value pair.
1116 */
Barry Naujok6a178102008-05-21 16:42:05 +10001117 if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 /*
1119 * In a separate transaction, set the incomplete flag on the
1120 * "old" attr and clear the incomplete flag on the "new" attr.
1121 */
Dave Chinner517c2222013-04-24 18:58:55 +10001122 error = xfs_attr3_leaf_flipflags(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 if (error)
1124 goto out;
1125
1126 /*
1127 * Dismantle the "old" attribute/value pair by removing
1128 * a "remote" value (if it exists).
1129 */
1130 args->index = args->index2;
1131 args->blkno = args->blkno2;
1132 args->rmtblkno = args->rmtblkno2;
1133 args->rmtblkcnt = args->rmtblkcnt2;
1134 if (args->rmtblkno) {
1135 error = xfs_attr_rmtval_remove(args);
1136 if (error)
1137 return(error);
1138 }
1139
1140 /*
1141 * Re-find the "old" attribute entry after any split ops.
1142 * The INCOMPLETE flag means that we will find the "old"
1143 * attr, not the "new" one.
1144 */
1145 args->flags |= XFS_ATTR_INCOMPLETE;
1146 state = xfs_da_state_alloc();
1147 state->args = args;
1148 state->mp = mp;
1149 state->blocksize = state->mp->m_sb.sb_blocksize;
1150 state->node_ents = state->mp->m_attr_node_ents;
1151 state->inleaf = 0;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001152 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 if (error)
1154 goto out;
1155
1156 /*
1157 * Remove the name and update the hashvals in the tree.
1158 */
1159 blk = &state->path.blk[ state->path.active-1 ];
1160 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner517c2222013-04-24 18:58:55 +10001161 error = xfs_attr3_leaf_remove(blk->bp, args);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001162 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164 /*
1165 * Check to see if the tree needs to be collapsed.
1166 */
1167 if (retval && (state->path.active > 1)) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001168 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001169 error = xfs_da3_join(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 if (!error) {
1171 error = xfs_bmap_finish(&args->trans,
1172 args->flist,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 &committed);
1174 }
1175 if (error) {
1176 ASSERT(committed);
1177 args->trans = NULL;
1178 xfs_bmap_cancel(args->flist);
1179 goto out;
1180 }
1181
1182 /*
1183 * bmap_finish() may have committed the last trans
1184 * and started a new one. We need the inode to be
1185 * in all transactions.
1186 */
Christoph Hellwig898621d2010-06-24 11:36:58 +10001187 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +00001188 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 }
1190
1191 /*
1192 * Commit and start the next trans in the chain.
1193 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10001194 error = xfs_trans_roll(&args->trans, dp);
1195 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 goto out;
1197
1198 } else if (args->rmtblkno > 0) {
1199 /*
1200 * Added a "remote" value, just clear the incomplete flag.
1201 */
Dave Chinner517c2222013-04-24 18:58:55 +10001202 error = xfs_attr3_leaf_clearflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 if (error)
1204 goto out;
1205 }
1206 retval = error = 0;
1207
1208out:
1209 if (state)
1210 xfs_da_state_free(state);
1211 if (error)
1212 return(error);
1213 return(retval);
1214}
1215
1216/*
1217 * Remove a name from a B-tree attribute list.
1218 *
1219 * This will involve walking down the Btree, and may involve joining
1220 * leaf nodes and even joining intermediate nodes up to and including
1221 * the root node (a special case of an intermediate node).
1222 */
1223STATIC int
1224xfs_attr_node_removename(xfs_da_args_t *args)
1225{
1226 xfs_da_state_t *state;
1227 xfs_da_state_blk_t *blk;
1228 xfs_inode_t *dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001229 struct xfs_buf *bp;
Nathan Scottd8cc8902005-11-02 10:34:53 +11001230 int retval, error, committed, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Dave Chinner5a5881c2012-03-22 05:15:13 +00001232 trace_xfs_attr_node_removename(args);
1233
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 /*
1235 * Tie a string around our finger to remind us where we are.
1236 */
1237 dp = args->dp;
1238 state = xfs_da_state_alloc();
1239 state->args = args;
1240 state->mp = dp->i_mount;
1241 state->blocksize = state->mp->m_sb.sb_blocksize;
1242 state->node_ents = state->mp->m_attr_node_ents;
1243
1244 /*
1245 * Search to see if name exists, and get back a pointer to it.
1246 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001247 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 if (error || (retval != EEXIST)) {
1249 if (error == 0)
1250 error = retval;
1251 goto out;
1252 }
1253
1254 /*
1255 * If there is an out-of-line value, de-allocate the blocks.
1256 * This is done before we remove the attribute so that we don't
1257 * overflow the maximum size of a transaction and/or hit a deadlock.
1258 */
1259 blk = &state->path.blk[ state->path.active-1 ];
1260 ASSERT(blk->bp != NULL);
1261 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1262 if (args->rmtblkno > 0) {
1263 /*
1264 * Fill in disk block numbers in the state structure
1265 * so that we can get the buffers back after we commit
1266 * several transactions in the following calls.
1267 */
1268 error = xfs_attr_fillstate(state);
1269 if (error)
1270 goto out;
1271
1272 /*
1273 * Mark the attribute as INCOMPLETE, then bunmapi() the
1274 * remote value.
1275 */
Dave Chinner517c2222013-04-24 18:58:55 +10001276 error = xfs_attr3_leaf_setflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 if (error)
1278 goto out;
1279 error = xfs_attr_rmtval_remove(args);
1280 if (error)
1281 goto out;
1282
1283 /*
1284 * Refill the state structure with buffers, the prior calls
1285 * released our buffers.
1286 */
1287 error = xfs_attr_refillstate(state);
1288 if (error)
1289 goto out;
1290 }
1291
1292 /*
1293 * Remove the name and update the hashvals in the tree.
1294 */
1295 blk = &state->path.blk[ state->path.active-1 ];
1296 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner517c2222013-04-24 18:58:55 +10001297 retval = xfs_attr3_leaf_remove(blk->bp, args);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001298 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
1300 /*
1301 * Check to see if the tree needs to be collapsed.
1302 */
1303 if (retval && (state->path.active > 1)) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001304 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001305 error = xfs_da3_join(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 if (!error) {
1307 error = xfs_bmap_finish(&args->trans, args->flist,
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001308 &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 }
1310 if (error) {
1311 ASSERT(committed);
1312 args->trans = NULL;
1313 xfs_bmap_cancel(args->flist);
1314 goto out;
1315 }
1316
1317 /*
1318 * bmap_finish() may have committed the last trans and started
1319 * a new one. We need the inode to be in all transactions.
1320 */
Christoph Hellwig898621d2010-06-24 11:36:58 +10001321 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +00001322 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
1324 /*
1325 * Commit the Btree join operation and start a new trans.
1326 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10001327 error = xfs_trans_roll(&args->trans, dp);
1328 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 goto out;
1330 }
1331
1332 /*
1333 * If the result is small enough, push it all into the inode.
1334 */
1335 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
1336 /*
1337 * Have to get rid of the copy of this dabuf in the state.
1338 */
1339 ASSERT(state->path.active == 1);
1340 ASSERT(state->path.blk[0].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 state->path.blk[0].bp = NULL;
1342
Dave Chinner517c2222013-04-24 18:58:55 +10001343 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 if (error)
1345 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
Nathan Scottd8cc8902005-11-02 10:34:53 +11001347 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001348 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +10001349 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 /* bp is gone due to xfs_da_shrink_inode */
1351 if (!error) {
1352 error = xfs_bmap_finish(&args->trans,
1353 args->flist,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 &committed);
1355 }
1356 if (error) {
1357 ASSERT(committed);
1358 args->trans = NULL;
1359 xfs_bmap_cancel(args->flist);
1360 goto out;
1361 }
1362
1363 /*
1364 * bmap_finish() may have committed the last trans
1365 * and started a new one. We need the inode to be
1366 * in all transactions.
1367 */
Christoph Hellwig898621d2010-06-24 11:36:58 +10001368 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +00001369 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 } else
Dave Chinner1d9025e2012-06-22 18:50:14 +10001371 xfs_trans_brelse(args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 }
1373 error = 0;
1374
1375out:
1376 xfs_da_state_free(state);
1377 return(error);
1378}
1379
1380/*
1381 * Fill in the disk block numbers in the state structure for the buffers
1382 * that are attached to the state structure.
1383 * This is done so that we can quickly reattach ourselves to those buffers
Nathan Scottc41564b2006-03-29 08:55:14 +10001384 * after some set of transaction commits have released these buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 */
1386STATIC int
1387xfs_attr_fillstate(xfs_da_state_t *state)
1388{
1389 xfs_da_state_path_t *path;
1390 xfs_da_state_blk_t *blk;
1391 int level;
1392
Dave Chinneree732592012-11-12 22:53:53 +11001393 trace_xfs_attr_fillstate(state->args);
1394
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 /*
1396 * Roll down the "path" in the state structure, storing the on-disk
1397 * block number for those buffers in the "path".
1398 */
1399 path = &state->path;
1400 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1401 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1402 if (blk->bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001403 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 blk->bp = NULL;
1405 } else {
1406 blk->disk_blkno = 0;
1407 }
1408 }
1409
1410 /*
1411 * Roll down the "altpath" in the state structure, storing the on-disk
1412 * block number for those buffers in the "altpath".
1413 */
1414 path = &state->altpath;
1415 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1416 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1417 if (blk->bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001418 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 blk->bp = NULL;
1420 } else {
1421 blk->disk_blkno = 0;
1422 }
1423 }
1424
1425 return(0);
1426}
1427
1428/*
1429 * Reattach the buffers to the state structure based on the disk block
1430 * numbers stored in the state structure.
Nathan Scottc41564b2006-03-29 08:55:14 +10001431 * This is done after some set of transaction commits have released those
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 * buffers from our grip.
1433 */
1434STATIC int
1435xfs_attr_refillstate(xfs_da_state_t *state)
1436{
1437 xfs_da_state_path_t *path;
1438 xfs_da_state_blk_t *blk;
1439 int level, error;
1440
Dave Chinneree732592012-11-12 22:53:53 +11001441 trace_xfs_attr_refillstate(state->args);
1442
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 /*
1444 * Roll down the "path" in the state structure, storing the on-disk
1445 * block number for those buffers in the "path".
1446 */
1447 path = &state->path;
1448 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1449 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1450 if (blk->disk_blkno) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001451 error = xfs_da3_node_read(state->args->trans,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 state->args->dp,
1453 blk->blkno, blk->disk_blkno,
Dave Chinnerd9392a42012-11-12 22:54:17 +11001454 &blk->bp, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 if (error)
1456 return(error);
1457 } else {
1458 blk->bp = NULL;
1459 }
1460 }
1461
1462 /*
1463 * Roll down the "altpath" in the state structure, storing the on-disk
1464 * block number for those buffers in the "altpath".
1465 */
1466 path = &state->altpath;
1467 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1468 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1469 if (blk->disk_blkno) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001470 error = xfs_da3_node_read(state->args->trans,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 state->args->dp,
1472 blk->blkno, blk->disk_blkno,
Dave Chinnerd9392a42012-11-12 22:54:17 +11001473 &blk->bp, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 if (error)
1475 return(error);
1476 } else {
1477 blk->bp = NULL;
1478 }
1479 }
1480
1481 return(0);
1482}
1483
1484/*
1485 * Look up a filename in a node attribute list.
1486 *
1487 * This routine gets called for any attribute fork that has more than one
1488 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1489 * "remote" values taking up more blocks.
1490 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001491STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492xfs_attr_node_get(xfs_da_args_t *args)
1493{
1494 xfs_da_state_t *state;
1495 xfs_da_state_blk_t *blk;
1496 int error, retval;
1497 int i;
1498
Dave Chinneree732592012-11-12 22:53:53 +11001499 trace_xfs_attr_node_get(args);
1500
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 state = xfs_da_state_alloc();
1502 state->args = args;
1503 state->mp = args->dp->i_mount;
1504 state->blocksize = state->mp->m_sb.sb_blocksize;
1505 state->node_ents = state->mp->m_attr_node_ents;
1506
1507 /*
1508 * Search to see if name exists, and get back a pointer to it.
1509 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001510 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 if (error) {
1512 retval = error;
1513 } else if (retval == EEXIST) {
1514 blk = &state->path.blk[ state->path.active-1 ];
1515 ASSERT(blk->bp != NULL);
1516 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1517
1518 /*
1519 * Get the value, local or "remote"
1520 */
Dave Chinner517c2222013-04-24 18:58:55 +10001521 retval = xfs_attr3_leaf_getvalue(blk->bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 if (!retval && (args->rmtblkno > 0)
1523 && !(args->flags & ATTR_KERNOVAL)) {
1524 retval = xfs_attr_rmtval_get(args);
1525 }
1526 }
1527
1528 /*
1529 * If not in a transaction, we have to release all the buffers.
1530 */
1531 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001532 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 state->path.blk[i].bp = NULL;
1534 }
1535
1536 xfs_da_state_free(state);
1537 return(retval);
1538}