blob: 86f482e5798fced6e9a5f9892666d32b22d0f258 [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 Chinner70a9883c2013-10-23 10:36:05 +110020#include "xfs_shared.h"
Dave Chinner239880e2013-10-23 10:50:10 +110021#include "xfs_format.h"
22#include "xfs_log_format.h"
23#include "xfs_trans_resv.h"
Nathan Scotta844f452005-11-02 14:38:42 +110024#include "xfs_bit.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"
Dave Chinner57062782013-10-15 09:17:51 +110028#include "xfs_da_format.h"
Nathan Scotta844f452005-11-02 14:38:42 +110029#include "xfs_da_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_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110032#include "xfs_alloc.h"
Dave Chinner239880e2013-10-23 10:50:10 +110033#include "xfs_trans.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"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110037#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include "xfs_attr.h"
39#include "xfs_attr_leaf.h"
Dave Chinner95920cd2013-04-03 16:11:27 +110040#include "xfs_attr_remote.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include "xfs_quota.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include "xfs_trans_space.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000044#include "xfs_trace.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110045#include "xfs_dinode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47/*
48 * xfs_attr.c
49 *
50 * Provide the external interfaces to manage attribute lists.
51 */
52
53/*========================================================================
54 * Function prototypes for the kernel.
55 *========================================================================*/
56
57/*
58 * Internal routines when attribute list fits inside the inode.
59 */
60STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
61
62/*
63 * Internal routines when attribute list is one block.
64 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100065STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066STATIC int xfs_attr_leaf_addname(xfs_da_args_t *args);
67STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69/*
70 * Internal routines when attribute list is more than one block.
71 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100072STATIC int xfs_attr_node_get(xfs_da_args_t *args);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073STATIC int xfs_attr_node_addname(xfs_da_args_t *args);
74STATIC int xfs_attr_node_removename(xfs_da_args_t *args);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
76STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Barry Naujoke8b0eba2008-04-22 17:34:31 +100079STATIC int
Christoph Hellwig67fd7182014-05-13 16:34:43 +100080xfs_attr_args_init(
81 struct xfs_da_args *args,
82 struct xfs_inode *dp,
83 const unsigned char *name,
84 int flags)
Barry Naujoke8b0eba2008-04-22 17:34:31 +100085{
Christoph Hellwig67fd7182014-05-13 16:34:43 +100086
87 if (!name)
Barry Naujoke8b0eba2008-04-22 17:34:31 +100088 return EINVAL;
Christoph Hellwig67fd7182014-05-13 16:34:43 +100089
90 memset(args, 0, sizeof(*args));
91 args->whichfork = XFS_ATTR_FORK;
92 args->dp = dp;
93 args->flags = flags;
94 args->name = name;
95 args->namelen = strlen((const char *)name);
96 if (args->namelen >= MAXNAMELEN)
Barry Naujoke8b0eba2008-04-22 17:34:31 +100097 return EFAULT; /* match IRIX behaviour */
98
Christoph Hellwig67fd7182014-05-13 16:34:43 +100099 args->hashval = xfs_da_hashname(args->name, args->namelen);
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000100 return 0;
101}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Dave Chinnerabec5f22013-08-12 20:49:38 +1000103int
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +1000104xfs_inode_hasattr(
105 struct xfs_inode *ip)
106{
107 if (!XFS_IFORK_Q(ip) ||
108 (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
109 ip->i_d.di_anextents == 0))
110 return 0;
111 return 1;
112}
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114/*========================================================================
115 * Overall external interface routines.
116 *========================================================================*/
117
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000118int
119xfs_attr_get(
Christoph Hellwige82fa0c2009-11-14 16:17:20 +0000120 struct xfs_inode *ip,
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000121 const unsigned char *name,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100122 unsigned char *value,
Christoph Hellwige82fa0c2009-11-14 16:17:20 +0000123 int *valuelenp,
124 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000126 struct xfs_da_args args;
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000127 uint lock_mode;
128 int error;
129
130 XFS_STATS_INC(xs_attr_get);
131
132 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
133 return EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +1000135 if (!xfs_inode_hasattr(ip))
136 return ENOATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000138 error = xfs_attr_args_init(&args, ip, name, flags);
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000139 if (error)
140 return error;
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 args.value = value;
143 args.valuelen = *valuelenp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Christoph Hellwig683cb942013-12-06 12:30:15 -0800145 lock_mode = xfs_ilock_attr_map_shared(ip);
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000146 if (!xfs_inode_hasattr(ip))
147 error = ENOATTR;
148 else if (ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL)
149 error = xfs_attr_shortform_getvalue(&args);
150 else if (xfs_bmap_one_block(ip, XFS_ATTR_FORK))
151 error = xfs_attr_leaf_get(&args);
152 else
153 error = xfs_attr_node_get(&args);
Christoph Hellwig683cb942013-12-06 12:30:15 -0800154 xfs_iunlock(ip, lock_mode);
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000155
156 *valuelenp = args.valuelen;
157 return error == EEXIST ? 0 : error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158}
159
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000160/*
161 * Calculate how many blocks we need for the new attribute,
162 */
Eric Sandeen5d77c0d2009-11-19 15:52:00 +0000163STATIC int
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000164xfs_attr_calc_size(
Christoph Hellwig6c888af2014-05-13 16:40:19 +1000165 struct xfs_da_args *args,
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000166 int *local)
167{
Christoph Hellwig6c888af2014-05-13 16:40:19 +1000168 struct xfs_mount *mp = args->dp->i_mount;
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000169 int size;
170 int nblks;
171
172 /*
173 * Determine space new attribute will use, and if it would be
174 * "local" or "remote" (note: local != inline).
175 */
Christoph Hellwig6c888af2014-05-13 16:40:19 +1000176 size = xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000177 mp->m_sb.sb_blocksize, local);
178
179 nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
180 if (*local) {
181 if (size > (mp->m_sb.sb_blocksize >> 1)) {
182 /* Double split possible */
183 nblks *= 2;
184 }
185 } else {
186 /*
187 * Out of line attribute, cannot double split, but
188 * make room for the attribute value itself.
189 */
Christoph Hellwig6c888af2014-05-13 16:40:19 +1000190 uint dblocks = XFS_B_TO_FSB(mp, args->valuelen);
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000191 nblks += dblocks;
192 nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
193 }
194
195 return nblks;
196}
197
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000198int
199xfs_attr_set(
200 struct xfs_inode *dp,
201 const unsigned char *name,
202 unsigned char *value,
203 int valuelen,
204 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
Jie Liu3d3c8b52013-08-12 20:49:59 +1000206 struct xfs_mount *mp = dp->i_mount;
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000207 struct xfs_da_args args;
208 struct xfs_bmap_free flist;
Jie Liu3d3c8b52013-08-12 20:49:59 +1000209 struct xfs_trans_res tres;
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000210 xfs_fsblock_t firstblock;
Jie Liu3d3c8b52013-08-12 20:49:59 +1000211 int rsvd = (flags & ATTR_ROOT) != 0;
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000212 int error, err2, committed, local;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000214 XFS_STATS_INC(xs_attr_set);
215
216 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
217 return EIO;
218
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000219 error = xfs_attr_args_init(&args, dp, name, flags);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000220 if (error)
221 return error;
222
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000223 args.value = value;
224 args.valuelen = valuelen;
225 args.firstblock = &firstblock;
226 args.flist = &flist;
227 args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
Christoph Hellwig6c888af2014-05-13 16:40:19 +1000228 args.total = xfs_attr_calc_size(&args, &local);
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000229
Christoph Hellwig7d095252009-06-08 15:33:32 +0200230 error = xfs_qm_dqattach(dp, 0);
231 if (error)
232 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 /*
235 * If the inode doesn't have an attribute fork, add one.
236 * (inode must not be locked when we call this routine)
237 */
238 if (XFS_IFORK_Q(dp) == 0) {
Barry Naujoke5889e92007-02-10 18:35:58 +1100239 int sf_size = sizeof(xfs_attr_sf_hdr_t) +
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000240 XFS_ATTR_SF_ENTSIZE_BYNAME(args.namelen, valuelen);
Barry Naujoke5889e92007-02-10 18:35:58 +1100241
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000242 error = xfs_bmap_add_attrfork(dp, sf_size, rsvd);
243 if (error)
244 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 /*
248 * Start our first transaction of the day.
249 *
250 * All future transactions during this code must be "chained" off
251 * this one via the trans_dup() call. All transactions will contain
252 * the inode, and the inode will always be marked with trans_ihold().
253 * Since the inode will be locked in all transactions, we must log
254 * the inode in every transaction to let it float upward through
255 * the log.
256 */
257 args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_SET);
258
259 /*
260 * Root fork attributes can use reserved data blocks for this
261 * operation if necessary
262 */
263
264 if (rsvd)
265 args.trans->t_flags |= XFS_TRANS_RESERVE;
266
Jie Liu3d3c8b52013-08-12 20:49:59 +1000267 tres.tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
268 M_RES(mp)->tr_attrsetrt.tr_logres * args.total;
269 tres.tr_logcount = XFS_ATTRSET_LOG_COUNT;
270 tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
271 error = xfs_trans_reserve(args.trans, &tres, args.total, 0);
Jeff Liua21cd502013-01-28 21:27:53 +0800272 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 xfs_trans_cancel(args.trans, 0);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000274 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 }
276 xfs_ilock(dp, XFS_ILOCK_EXCL);
277
Christoph Hellwig7d095252009-06-08 15:33:32 +0200278 error = xfs_trans_reserve_quota_nblks(args.trans, dp, args.total, 0,
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000279 rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
280 XFS_QMOPT_RES_REGBLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 if (error) {
282 xfs_iunlock(dp, XFS_ILOCK_EXCL);
283 xfs_trans_cancel(args.trans, XFS_TRANS_RELEASE_LOG_RES);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000284 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
286
Christoph Hellwigddc34152011-09-19 15:00:54 +0000287 xfs_trans_ijoin(args.trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000290 * If the attribute list is non-existent or a shortform list,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 * upgrade it to a single-leaf-block attribute list.
292 */
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000293 if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL ||
294 (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
295 dp->i_d.di_anextents == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297 /*
298 * Build initial attribute list (if required).
299 */
300 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
Nathan Scottd8cc8902005-11-02 10:34:53 +1100301 xfs_attr_shortform_create(&args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303 /*
304 * Try to add the attr to the attribute list in
305 * the inode.
306 */
307 error = xfs_attr_shortform_addname(&args);
308 if (error != ENOSPC) {
309 /*
310 * Commit the shortform mods, and we're done.
311 * NOTE: this is also the error path (EEXIST, etc).
312 */
313 ASSERT(args.trans != NULL);
314
315 /*
316 * If this is a synchronous mount, make sure that
317 * the transaction goes to disk before returning
318 * to the user.
319 */
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000320 if (mp->m_flags & XFS_MOUNT_WSYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 xfs_trans_set_sync(args.trans);
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000322
323 if (!error && (flags & ATTR_KERNOTIME) == 0) {
324 xfs_trans_ichgtime(args.trans, dp,
325 XFS_ICHGTIME_CHG);
326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 err2 = xfs_trans_commit(args.trans,
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000328 XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 xfs_iunlock(dp, XFS_ILOCK_EXCL);
330
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000331 return error ? error : err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 }
333
334 /*
335 * It won't fit in the shortform, transform to a leaf block.
336 * GROT: another possible req'mt for a double-split btree op.
337 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600338 xfs_bmap_init(args.flist, args.firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 error = xfs_attr_shortform_to_leaf(&args);
340 if (!error) {
341 error = xfs_bmap_finish(&args.trans, args.flist,
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100342 &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
344 if (error) {
345 ASSERT(committed);
346 args.trans = NULL;
347 xfs_bmap_cancel(&flist);
348 goto out;
349 }
350
351 /*
352 * bmap_finish() may have committed the last trans and started
353 * a new one. We need the inode to be in all transactions.
354 */
Christoph Hellwig898621d2010-06-24 11:36:58 +1000355 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +0000356 xfs_trans_ijoin(args.trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358 /*
359 * Commit the leaf transformation. We'll need another (linked)
360 * transaction to add the new attribute to the leaf.
361 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000362
363 error = xfs_trans_roll(&args.trans, dp);
364 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 goto out;
366
367 }
368
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000369 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 error = xfs_attr_leaf_addname(&args);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000371 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 error = xfs_attr_node_addname(&args);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000373 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 /*
377 * If this is a synchronous mount, make sure that the
378 * transaction goes to disk before returning to the user.
379 */
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000380 if (mp->m_flags & XFS_MOUNT_WSYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 xfs_trans_set_sync(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000383 if ((flags & ATTR_KERNOTIME) == 0)
384 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 /*
387 * Commit the last in the sequence of transactions.
388 */
389 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000390 error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 xfs_iunlock(dp, XFS_ILOCK_EXCL);
392
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000393 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395out:
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000396 if (args.trans) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 xfs_trans_cancel(args.trans,
398 XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000401 return error;
Nathan Scottaa82daa2005-11-02 10:33:33 +1100402}
403
404/*
405 * Generic handler routine to remove a name from an attribute list.
406 * Transitions attribute list from Btree to shortform as necessary.
407 */
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000408int
409xfs_attr_remove(
410 struct xfs_inode *dp,
411 const unsigned char *name,
412 int flags)
Nathan Scottaa82daa2005-11-02 10:33:33 +1100413{
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000414 struct xfs_mount *mp = dp->i_mount;
415 struct xfs_da_args args;
416 struct xfs_bmap_free flist;
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000417 xfs_fsblock_t firstblock;
418 int error;
Nathan Scottaa82daa2005-11-02 10:33:33 +1100419
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000420 XFS_STATS_INC(xs_attr_remove);
421
422 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
423 return EIO;
424
425 if (!xfs_inode_hasattr(dp))
426 return ENOATTR;
427
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000428 error = xfs_attr_args_init(&args, dp, name, flags);
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000429 if (error)
430 return error;
431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 args.firstblock = &firstblock;
433 args.flist = &flist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 /*
Dave Chinner4a338212011-06-23 01:35:01 +0000436 * we have no control over the attribute names that userspace passes us
437 * to remove, so we have to allow the name lookup prior to attribute
438 * removal to fail.
439 */
440 args.op_flags = XFS_DA_OP_OKNOENT;
441
Christoph Hellwig7d095252009-06-08 15:33:32 +0200442 error = xfs_qm_dqattach(dp, 0);
443 if (error)
444 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 /*
447 * Start our first transaction of the day.
448 *
449 * All future transactions during this code must be "chained" off
450 * this one via the trans_dup() call. All transactions will contain
451 * the inode, and the inode will always be marked with trans_ihold().
452 * Since the inode will be locked in all transactions, we must log
453 * the inode in every transaction to let it float upward through
454 * the log.
455 */
456 args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_RM);
457
458 /*
459 * Root fork attributes can use reserved data blocks for this
460 * operation if necessary
461 */
462
463 if (flags & ATTR_ROOT)
464 args.trans->t_flags |= XFS_TRANS_RESERVE;
465
Jie Liu3d3c8b52013-08-12 20:49:59 +1000466 error = xfs_trans_reserve(args.trans, &M_RES(mp)->tr_attrrm,
467 XFS_ATTRRM_SPACE_RES(mp), 0);
468 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 xfs_trans_cancel(args.trans, 0);
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000470 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 }
472
473 xfs_ilock(dp, XFS_ILOCK_EXCL);
474 /*
475 * No need to make quota reservations here. We expect to release some
476 * blocks not allocate in the common case.
477 */
Christoph Hellwigddc34152011-09-19 15:00:54 +0000478 xfs_trans_ijoin(args.trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +1000480 if (!xfs_inode_hasattr(dp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 error = XFS_ERROR(ENOATTR);
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000482 } else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
484 error = xfs_attr_shortform_remove(&args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
486 error = xfs_attr_leaf_removename(&args);
487 } else {
488 error = xfs_attr_node_removename(&args);
489 }
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000490
491 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 /*
495 * If this is a synchronous mount, make sure that the
496 * transaction goes to disk before returning to the user.
497 */
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000498 if (mp->m_flags & XFS_MOUNT_WSYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 xfs_trans_set_sync(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000501 if ((flags & ATTR_KERNOTIME) == 0)
502 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 /*
505 * Commit the last in the sequence of transactions.
506 */
507 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000508 error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 xfs_iunlock(dp, XFS_ILOCK_EXCL);
510
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000511 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513out:
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000514 if (args.trans) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 xfs_trans_cancel(args.trans,
516 XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
Nathan Scottaa82daa2005-11-02 10:33:33 +1100517 }
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000518 xfs_iunlock(dp, XFS_ILOCK_EXCL);
519 return error;
Nathan Scottaa82daa2005-11-02 10:33:33 +1100520}
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522/*========================================================================
523 * External routines when attribute list is inside the inode
524 *========================================================================*/
525
526/*
527 * Add a name to the shortform attribute list structure
528 * This is the external routine.
529 */
530STATIC int
531xfs_attr_shortform_addname(xfs_da_args_t *args)
532{
Nathan Scottd8cc8902005-11-02 10:34:53 +1100533 int newsize, forkoff, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Dave Chinner5a5881c2012-03-22 05:15:13 +0000535 trace_xfs_attr_sf_addname(args);
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 retval = xfs_attr_shortform_lookup(args);
538 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
539 return(retval);
540 } else if (retval == EEXIST) {
541 if (args->flags & ATTR_CREATE)
542 return(retval);
543 retval = xfs_attr_shortform_remove(args);
544 ASSERT(retval == 0);
545 }
546
Nathan Scottd8cc8902005-11-02 10:34:53 +1100547 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
548 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
549 return(XFS_ERROR(ENOSPC));
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
552 newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100553
554 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
555 if (!forkoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 return(XFS_ERROR(ENOSPC));
Nathan Scottd8cc8902005-11-02 10:34:53 +1100557
558 xfs_attr_shortform_add(args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return(0);
560}
561
562
563/*========================================================================
564 * External routines when attribute list is one block
565 *========================================================================*/
566
567/*
568 * Add a name to the leaf attribute list structure
569 *
570 * This leaf block cannot have a "remote" value, we only call this routine
571 * if bmap_one_block() says there is only one block (ie: no remote blks).
572 */
David Chinnera8272ce2007-11-23 16:28:09 +1100573STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574xfs_attr_leaf_addname(xfs_da_args_t *args)
575{
576 xfs_inode_t *dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000577 struct xfs_buf *bp;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100578 int retval, error, committed, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Dave Chinner5a5881c2012-03-22 05:15:13 +0000580 trace_xfs_attr_leaf_addname(args);
581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 /*
583 * Read the (only) block in the attribute list in.
584 */
585 dp = args->dp;
586 args->blkno = 0;
Dave Chinner517c2222013-04-24 18:58:55 +1000587 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100589 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591 /*
592 * Look up the given attribute in the leaf block. Figure out if
593 * the given flags produce an error or call for an atomic rename.
594 */
Dave Chinner517c2222013-04-24 18:58:55 +1000595 retval = xfs_attr3_leaf_lookup_int(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000597 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000598 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 } else if (retval == EEXIST) {
600 if (args->flags & ATTR_CREATE) { /* pure create op */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000601 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000602 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 }
Dave Chinner5a5881c2012-03-22 05:15:13 +0000604
605 trace_xfs_attr_leaf_replace(args);
606
Barry Naujok6a178102008-05-21 16:42:05 +1000607 args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 args->blkno2 = args->blkno; /* set 2nd entry info*/
609 args->index2 = args->index;
610 args->rmtblkno2 = args->rmtblkno;
611 args->rmtblkcnt2 = args->rmtblkcnt;
612 }
613
614 /*
615 * Add the attribute to the leaf block, transitioning to a Btree
616 * if required.
617 */
Dave Chinner517c2222013-04-24 18:58:55 +1000618 retval = xfs_attr3_leaf_add(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 if (retval == ENOSPC) {
620 /*
621 * Promote the attribute list to the Btree format, then
622 * Commit that transaction so that the node_addname() call
623 * can manage its own transactions.
624 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600625 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000626 error = xfs_attr3_leaf_to_node(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 if (!error) {
628 error = xfs_bmap_finish(&args->trans, args->flist,
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100629 &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 }
631 if (error) {
632 ASSERT(committed);
633 args->trans = NULL;
634 xfs_bmap_cancel(args->flist);
635 return(error);
636 }
637
638 /*
639 * bmap_finish() may have committed the last trans and started
640 * a new one. We need the inode to be in all transactions.
641 */
Christoph Hellwig898621d2010-06-24 11:36:58 +1000642 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +0000643 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645 /*
646 * Commit the current trans (including the inode) and start
647 * a new one.
648 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000649 error = xfs_trans_roll(&args->trans, dp);
650 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 return (error);
652
653 /*
654 * Fob the whole rest of the problem off on the Btree code.
655 */
656 error = xfs_attr_node_addname(args);
657 return(error);
658 }
659
660 /*
661 * Commit the transaction that added the attr name so that
662 * later routines can manage their own transactions.
663 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000664 error = xfs_trans_roll(&args->trans, dp);
665 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return (error);
667
668 /*
669 * If there was an out-of-line value, allocate the blocks we
670 * identified for its storage and copy the value. This is done
671 * after we create the attribute so that we don't overflow the
672 * maximum size of a transaction and/or hit a deadlock.
673 */
674 if (args->rmtblkno > 0) {
675 error = xfs_attr_rmtval_set(args);
676 if (error)
677 return(error);
678 }
679
680 /*
681 * If this is an atomic rename operation, we must "flip" the
682 * incomplete flags on the "new" and "old" attribute/value pairs
683 * so that one disappears and one appears atomically. Then we
684 * must remove the "old" attribute/value pair.
685 */
Barry Naujok6a178102008-05-21 16:42:05 +1000686 if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 /*
688 * In a separate transaction, set the incomplete flag on the
689 * "old" attr and clear the incomplete flag on the "new" attr.
690 */
Dave Chinner517c2222013-04-24 18:58:55 +1000691 error = xfs_attr3_leaf_flipflags(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 if (error)
693 return(error);
694
695 /*
696 * Dismantle the "old" attribute/value pair by removing
697 * a "remote" value (if it exists).
698 */
699 args->index = args->index2;
700 args->blkno = args->blkno2;
701 args->rmtblkno = args->rmtblkno2;
702 args->rmtblkcnt = args->rmtblkcnt2;
703 if (args->rmtblkno) {
704 error = xfs_attr_rmtval_remove(args);
705 if (error)
706 return(error);
707 }
708
709 /*
710 * Read in the block containing the "old" attr, then
711 * remove the "old" attr from that block (neat, huh!)
712 */
Dave Chinner517c2222013-04-24 18:58:55 +1000713 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
Dave Chinnerad14c332012-11-12 22:54:16 +1100714 -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100716 return error;
717
Dave Chinner517c2222013-04-24 18:58:55 +1000718 xfs_attr3_leaf_remove(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 /*
721 * If the result is small enough, shrink it all into the inode.
722 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100723 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Eric Sandeen9d87c312009-01-14 23:22:07 -0600724 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000725 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 /* bp is gone due to xfs_da_shrink_inode */
727 if (!error) {
728 error = xfs_bmap_finish(&args->trans,
729 args->flist,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 &committed);
731 }
732 if (error) {
733 ASSERT(committed);
734 args->trans = NULL;
735 xfs_bmap_cancel(args->flist);
736 return(error);
737 }
738
739 /*
740 * bmap_finish() may have committed the last trans
741 * and started a new one. We need the inode to be
742 * in all transactions.
743 */
Christoph Hellwig898621d2010-06-24 11:36:58 +1000744 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +0000745 xfs_trans_ijoin(args->trans, dp, 0);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748 /*
749 * Commit the remove and start the next trans in series.
750 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000751 error = xfs_trans_roll(&args->trans, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753 } else if (args->rmtblkno > 0) {
754 /*
755 * Added a "remote" value, just clear the incomplete flag.
756 */
Dave Chinner517c2222013-04-24 18:58:55 +1000757 error = xfs_attr3_leaf_clearflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 }
Dave Chinner517c2222013-04-24 18:58:55 +1000759 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760}
761
762/*
763 * Remove a name from the leaf attribute list structure
764 *
765 * This leaf block cannot have a "remote" value, we only call this routine
766 * if bmap_one_block() says there is only one block (ie: no remote blks).
767 */
768STATIC int
769xfs_attr_leaf_removename(xfs_da_args_t *args)
770{
771 xfs_inode_t *dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000772 struct xfs_buf *bp;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100773 int error, committed, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Dave Chinner5a5881c2012-03-22 05:15:13 +0000775 trace_xfs_attr_leaf_removename(args);
776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 /*
778 * Remove the attribute.
779 */
780 dp = args->dp;
781 args->blkno = 0;
Dave Chinner517c2222013-04-24 18:58:55 +1000782 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Dave Chinnerad14c332012-11-12 22:54:16 +1100783 if (error)
784 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Dave Chinner517c2222013-04-24 18:58:55 +1000786 error = xfs_attr3_leaf_lookup_int(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 if (error == ENOATTR) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000788 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000789 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 }
791
Dave Chinner517c2222013-04-24 18:58:55 +1000792 xfs_attr3_leaf_remove(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794 /*
795 * If the result is small enough, shrink it all into the inode.
796 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100797 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Eric Sandeen9d87c312009-01-14 23:22:07 -0600798 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000799 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 /* bp is gone due to xfs_da_shrink_inode */
801 if (!error) {
802 error = xfs_bmap_finish(&args->trans, args->flist,
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100803 &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 }
805 if (error) {
806 ASSERT(committed);
807 args->trans = NULL;
808 xfs_bmap_cancel(args->flist);
Dave Chinner517c2222013-04-24 18:58:55 +1000809 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 }
811
812 /*
813 * bmap_finish() may have committed the last trans and started
814 * a new one. We need the inode to be in all transactions.
815 */
Christoph Hellwig898621d2010-06-24 11:36:58 +1000816 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +0000817 xfs_trans_ijoin(args->trans, dp, 0);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000818 }
Dave Chinner517c2222013-04-24 18:58:55 +1000819 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820}
821
822/*
823 * Look up a name in a leaf attribute list structure.
824 *
825 * This leaf block cannot have a "remote" value, we only call this routine
826 * if bmap_one_block() says there is only one block (ie: no remote blks).
827 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000828STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829xfs_attr_leaf_get(xfs_da_args_t *args)
830{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000831 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 int error;
833
Dave Chinneree732592012-11-12 22:53:53 +1100834 trace_xfs_attr_leaf_get(args);
835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 args->blkno = 0;
Dave Chinner517c2222013-04-24 18:58:55 +1000837 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100839 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Dave Chinner517c2222013-04-24 18:58:55 +1000841 error = xfs_attr3_leaf_lookup_int(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 if (error != EEXIST) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000843 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000844 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 }
Dave Chinner517c2222013-04-24 18:58:55 +1000846 error = xfs_attr3_leaf_getvalue(bp, args);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000847 xfs_trans_brelse(args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
849 error = xfs_attr_rmtval_get(args);
850 }
Dave Chinner517c2222013-04-24 18:58:55 +1000851 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852}
853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854/*========================================================================
855 * External routines when attribute list size > XFS_LBSIZE(mp).
856 *========================================================================*/
857
858/*
859 * Add a name to a Btree-format attribute list.
860 *
861 * This will involve walking down the Btree, and may involve splitting
862 * leaf nodes and even splitting intermediate nodes up to and including
863 * the root node (a special case of an intermediate node).
864 *
865 * "Remote" attribute values confuse the issue and atomic rename operations
866 * add a whole extra layer of confusion on top of that.
867 */
868STATIC int
869xfs_attr_node_addname(xfs_da_args_t *args)
870{
871 xfs_da_state_t *state;
872 xfs_da_state_blk_t *blk;
873 xfs_inode_t *dp;
874 xfs_mount_t *mp;
875 int committed, retval, error;
876
Dave Chinner5a5881c2012-03-22 05:15:13 +0000877 trace_xfs_attr_node_addname(args);
878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 /*
880 * Fill in bucket of arguments/results/context to carry around.
881 */
882 dp = args->dp;
883 mp = dp->i_mount;
884restart:
885 state = xfs_da_state_alloc();
886 state->args = args;
887 state->mp = mp;
888 state->blocksize = state->mp->m_sb.sb_blocksize;
889 state->node_ents = state->mp->m_attr_node_ents;
890
891 /*
892 * Search to see if name already exists, and get back a pointer
893 * to where it should go.
894 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000895 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 if (error)
897 goto out;
898 blk = &state->path.blk[ state->path.active-1 ];
899 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
900 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
901 goto out;
902 } else if (retval == EEXIST) {
903 if (args->flags & ATTR_CREATE)
904 goto out;
Dave Chinner5a5881c2012-03-22 05:15:13 +0000905
906 trace_xfs_attr_node_replace(args);
907
Barry Naujok6a178102008-05-21 16:42:05 +1000908 args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 args->blkno2 = args->blkno; /* set 2nd entry info*/
910 args->index2 = args->index;
911 args->rmtblkno2 = args->rmtblkno;
912 args->rmtblkcnt2 = args->rmtblkcnt;
913 args->rmtblkno = 0;
914 args->rmtblkcnt = 0;
915 }
916
Dave Chinner517c2222013-04-24 18:58:55 +1000917 retval = xfs_attr3_leaf_add(blk->bp, state->args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 if (retval == ENOSPC) {
919 if (state->path.active == 1) {
920 /*
921 * Its really a single leaf node, but it had
922 * out-of-line values so it looked like it *might*
923 * have been a b-tree.
924 */
925 xfs_da_state_free(state);
Eric Sandeen6dd93e92013-07-31 20:18:54 -0500926 state = NULL;
Eric Sandeen9d87c312009-01-14 23:22:07 -0600927 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000928 error = xfs_attr3_leaf_to_node(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 if (!error) {
930 error = xfs_bmap_finish(&args->trans,
931 args->flist,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 &committed);
933 }
934 if (error) {
935 ASSERT(committed);
936 args->trans = NULL;
937 xfs_bmap_cancel(args->flist);
938 goto out;
939 }
940
941 /*
942 * bmap_finish() may have committed the last trans
943 * and started a new one. We need the inode to be
944 * in all transactions.
945 */
Christoph Hellwig898621d2010-06-24 11:36:58 +1000946 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +0000947 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 /*
950 * Commit the node conversion and start the next
951 * trans in the chain.
952 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000953 error = xfs_trans_roll(&args->trans, dp);
954 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 goto out;
956
957 goto restart;
958 }
959
960 /*
961 * Split as many Btree elements as required.
962 * This code tracks the new and old attr's location
963 * in the index/blkno/rmtblkno/rmtblkcnt fields and
964 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
965 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600966 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000967 error = xfs_da3_split(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 if (!error) {
969 error = xfs_bmap_finish(&args->trans, args->flist,
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100970 &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 }
972 if (error) {
973 ASSERT(committed);
974 args->trans = NULL;
975 xfs_bmap_cancel(args->flist);
976 goto out;
977 }
978
979 /*
980 * bmap_finish() may have committed the last trans and started
981 * a new one. We need the inode to be in all transactions.
982 */
Christoph Hellwig898621d2010-06-24 11:36:58 +1000983 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +0000984 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 } else {
986 /*
987 * Addition succeeded, update Btree hashvals.
988 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000989 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 }
991
992 /*
993 * Kill the state structure, we're done with it and need to
994 * allow the buffers to come back later.
995 */
996 xfs_da_state_free(state);
997 state = NULL;
998
999 /*
1000 * Commit the leaf addition or btree split and start the next
1001 * trans in the chain.
1002 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10001003 error = xfs_trans_roll(&args->trans, dp);
1004 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 goto out;
1006
1007 /*
1008 * If there was an out-of-line value, allocate the blocks we
1009 * identified for its storage and copy the value. This is done
1010 * after we create the attribute so that we don't overflow the
1011 * maximum size of a transaction and/or hit a deadlock.
1012 */
1013 if (args->rmtblkno > 0) {
1014 error = xfs_attr_rmtval_set(args);
1015 if (error)
1016 return(error);
1017 }
1018
1019 /*
1020 * If this is an atomic rename operation, we must "flip" the
1021 * incomplete flags on the "new" and "old" attribute/value pairs
1022 * so that one disappears and one appears atomically. Then we
1023 * must remove the "old" attribute/value pair.
1024 */
Barry Naujok6a178102008-05-21 16:42:05 +10001025 if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 /*
1027 * In a separate transaction, set the incomplete flag on the
1028 * "old" attr and clear the incomplete flag on the "new" attr.
1029 */
Dave Chinner517c2222013-04-24 18:58:55 +10001030 error = xfs_attr3_leaf_flipflags(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 if (error)
1032 goto out;
1033
1034 /*
1035 * Dismantle the "old" attribute/value pair by removing
1036 * a "remote" value (if it exists).
1037 */
1038 args->index = args->index2;
1039 args->blkno = args->blkno2;
1040 args->rmtblkno = args->rmtblkno2;
1041 args->rmtblkcnt = args->rmtblkcnt2;
1042 if (args->rmtblkno) {
1043 error = xfs_attr_rmtval_remove(args);
1044 if (error)
1045 return(error);
1046 }
1047
1048 /*
1049 * Re-find the "old" attribute entry after any split ops.
1050 * The INCOMPLETE flag means that we will find the "old"
1051 * attr, not the "new" one.
1052 */
1053 args->flags |= XFS_ATTR_INCOMPLETE;
1054 state = xfs_da_state_alloc();
1055 state->args = args;
1056 state->mp = mp;
1057 state->blocksize = state->mp->m_sb.sb_blocksize;
1058 state->node_ents = state->mp->m_attr_node_ents;
1059 state->inleaf = 0;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001060 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 if (error)
1062 goto out;
1063
1064 /*
1065 * Remove the name and update the hashvals in the tree.
1066 */
1067 blk = &state->path.blk[ state->path.active-1 ];
1068 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner517c2222013-04-24 18:58:55 +10001069 error = xfs_attr3_leaf_remove(blk->bp, args);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001070 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
1072 /*
1073 * Check to see if the tree needs to be collapsed.
1074 */
1075 if (retval && (state->path.active > 1)) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001076 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001077 error = xfs_da3_join(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 if (!error) {
1079 error = xfs_bmap_finish(&args->trans,
1080 args->flist,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 &committed);
1082 }
1083 if (error) {
1084 ASSERT(committed);
1085 args->trans = NULL;
1086 xfs_bmap_cancel(args->flist);
1087 goto out;
1088 }
1089
1090 /*
1091 * bmap_finish() may have committed the last trans
1092 * and started a new one. We need the inode to be
1093 * in all transactions.
1094 */
Christoph Hellwig898621d2010-06-24 11:36:58 +10001095 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +00001096 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 }
1098
1099 /*
1100 * Commit and start the next trans in the chain.
1101 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10001102 error = xfs_trans_roll(&args->trans, dp);
1103 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 goto out;
1105
1106 } else if (args->rmtblkno > 0) {
1107 /*
1108 * Added a "remote" value, just clear the incomplete flag.
1109 */
Dave Chinner517c2222013-04-24 18:58:55 +10001110 error = xfs_attr3_leaf_clearflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 if (error)
1112 goto out;
1113 }
1114 retval = error = 0;
1115
1116out:
1117 if (state)
1118 xfs_da_state_free(state);
1119 if (error)
1120 return(error);
1121 return(retval);
1122}
1123
1124/*
1125 * Remove a name from a B-tree attribute list.
1126 *
1127 * This will involve walking down the Btree, and may involve joining
1128 * leaf nodes and even joining intermediate nodes up to and including
1129 * the root node (a special case of an intermediate node).
1130 */
1131STATIC int
1132xfs_attr_node_removename(xfs_da_args_t *args)
1133{
1134 xfs_da_state_t *state;
1135 xfs_da_state_blk_t *blk;
1136 xfs_inode_t *dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001137 struct xfs_buf *bp;
Nathan Scottd8cc8902005-11-02 10:34:53 +11001138 int retval, error, committed, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Dave Chinner5a5881c2012-03-22 05:15:13 +00001140 trace_xfs_attr_node_removename(args);
1141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 /*
1143 * Tie a string around our finger to remind us where we are.
1144 */
1145 dp = args->dp;
1146 state = xfs_da_state_alloc();
1147 state->args = args;
1148 state->mp = dp->i_mount;
1149 state->blocksize = state->mp->m_sb.sb_blocksize;
1150 state->node_ents = state->mp->m_attr_node_ents;
1151
1152 /*
1153 * Search to see if name exists, and get back a pointer to it.
1154 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001155 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 if (error || (retval != EEXIST)) {
1157 if (error == 0)
1158 error = retval;
1159 goto out;
1160 }
1161
1162 /*
1163 * If there is an out-of-line value, de-allocate the blocks.
1164 * This is done before we remove the attribute so that we don't
1165 * overflow the maximum size of a transaction and/or hit a deadlock.
1166 */
1167 blk = &state->path.blk[ state->path.active-1 ];
1168 ASSERT(blk->bp != NULL);
1169 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1170 if (args->rmtblkno > 0) {
1171 /*
1172 * Fill in disk block numbers in the state structure
1173 * so that we can get the buffers back after we commit
1174 * several transactions in the following calls.
1175 */
1176 error = xfs_attr_fillstate(state);
1177 if (error)
1178 goto out;
1179
1180 /*
1181 * Mark the attribute as INCOMPLETE, then bunmapi() the
1182 * remote value.
1183 */
Dave Chinner517c2222013-04-24 18:58:55 +10001184 error = xfs_attr3_leaf_setflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 if (error)
1186 goto out;
1187 error = xfs_attr_rmtval_remove(args);
1188 if (error)
1189 goto out;
1190
1191 /*
1192 * Refill the state structure with buffers, the prior calls
1193 * released our buffers.
1194 */
1195 error = xfs_attr_refillstate(state);
1196 if (error)
1197 goto out;
1198 }
1199
1200 /*
1201 * Remove the name and update the hashvals in the tree.
1202 */
1203 blk = &state->path.blk[ state->path.active-1 ];
1204 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner517c2222013-04-24 18:58:55 +10001205 retval = xfs_attr3_leaf_remove(blk->bp, args);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001206 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
1208 /*
1209 * Check to see if the tree needs to be collapsed.
1210 */
1211 if (retval && (state->path.active > 1)) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001212 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001213 error = xfs_da3_join(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 if (!error) {
1215 error = xfs_bmap_finish(&args->trans, args->flist,
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001216 &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 }
1218 if (error) {
1219 ASSERT(committed);
1220 args->trans = NULL;
1221 xfs_bmap_cancel(args->flist);
1222 goto out;
1223 }
1224
1225 /*
1226 * bmap_finish() may have committed the last trans and started
1227 * a new one. We need the inode to be in all transactions.
1228 */
Christoph Hellwig898621d2010-06-24 11:36:58 +10001229 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +00001230 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
1232 /*
1233 * Commit the Btree join operation and start a new trans.
1234 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10001235 error = xfs_trans_roll(&args->trans, dp);
1236 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 goto out;
1238 }
1239
1240 /*
1241 * If the result is small enough, push it all into the inode.
1242 */
1243 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
1244 /*
1245 * Have to get rid of the copy of this dabuf in the state.
1246 */
1247 ASSERT(state->path.active == 1);
1248 ASSERT(state->path.blk[0].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 state->path.blk[0].bp = NULL;
1250
Dave Chinner517c2222013-04-24 18:58:55 +10001251 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 if (error)
1253 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Nathan Scottd8cc8902005-11-02 10:34:53 +11001255 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001256 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +10001257 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 /* bp is gone due to xfs_da_shrink_inode */
1259 if (!error) {
1260 error = xfs_bmap_finish(&args->trans,
1261 args->flist,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 &committed);
1263 }
1264 if (error) {
1265 ASSERT(committed);
1266 args->trans = NULL;
1267 xfs_bmap_cancel(args->flist);
1268 goto out;
1269 }
1270
1271 /*
1272 * bmap_finish() may have committed the last trans
1273 * and started a new one. We need the inode to be
1274 * in all transactions.
1275 */
Christoph Hellwig898621d2010-06-24 11:36:58 +10001276 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +00001277 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 } else
Dave Chinner1d9025e2012-06-22 18:50:14 +10001279 xfs_trans_brelse(args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 }
1281 error = 0;
1282
1283out:
1284 xfs_da_state_free(state);
1285 return(error);
1286}
1287
1288/*
1289 * Fill in the disk block numbers in the state structure for the buffers
1290 * that are attached to the state structure.
1291 * This is done so that we can quickly reattach ourselves to those buffers
Nathan Scottc41564b2006-03-29 08:55:14 +10001292 * after some set of transaction commits have released these buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 */
1294STATIC int
1295xfs_attr_fillstate(xfs_da_state_t *state)
1296{
1297 xfs_da_state_path_t *path;
1298 xfs_da_state_blk_t *blk;
1299 int level;
1300
Dave Chinneree732592012-11-12 22:53:53 +11001301 trace_xfs_attr_fillstate(state->args);
1302
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 /*
1304 * Roll down the "path" in the state structure, storing the on-disk
1305 * block number for those buffers in the "path".
1306 */
1307 path = &state->path;
1308 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1309 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1310 if (blk->bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001311 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 blk->bp = NULL;
1313 } else {
1314 blk->disk_blkno = 0;
1315 }
1316 }
1317
1318 /*
1319 * Roll down the "altpath" in the state structure, storing the on-disk
1320 * block number for those buffers in the "altpath".
1321 */
1322 path = &state->altpath;
1323 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1324 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1325 if (blk->bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001326 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 blk->bp = NULL;
1328 } else {
1329 blk->disk_blkno = 0;
1330 }
1331 }
1332
1333 return(0);
1334}
1335
1336/*
1337 * Reattach the buffers to the state structure based on the disk block
1338 * numbers stored in the state structure.
Nathan Scottc41564b2006-03-29 08:55:14 +10001339 * This is done after some set of transaction commits have released those
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 * buffers from our grip.
1341 */
1342STATIC int
1343xfs_attr_refillstate(xfs_da_state_t *state)
1344{
1345 xfs_da_state_path_t *path;
1346 xfs_da_state_blk_t *blk;
1347 int level, error;
1348
Dave Chinneree732592012-11-12 22:53:53 +11001349 trace_xfs_attr_refillstate(state->args);
1350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 /*
1352 * Roll down the "path" in the state structure, storing the on-disk
1353 * block number for those buffers in the "path".
1354 */
1355 path = &state->path;
1356 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1357 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1358 if (blk->disk_blkno) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001359 error = xfs_da3_node_read(state->args->trans,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 state->args->dp,
1361 blk->blkno, blk->disk_blkno,
Dave Chinnerd9392a42012-11-12 22:54:17 +11001362 &blk->bp, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 if (error)
1364 return(error);
1365 } else {
1366 blk->bp = NULL;
1367 }
1368 }
1369
1370 /*
1371 * Roll down the "altpath" in the state structure, storing the on-disk
1372 * block number for those buffers in the "altpath".
1373 */
1374 path = &state->altpath;
1375 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1376 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1377 if (blk->disk_blkno) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001378 error = xfs_da3_node_read(state->args->trans,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 state->args->dp,
1380 blk->blkno, blk->disk_blkno,
Dave Chinnerd9392a42012-11-12 22:54:17 +11001381 &blk->bp, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 if (error)
1383 return(error);
1384 } else {
1385 blk->bp = NULL;
1386 }
1387 }
1388
1389 return(0);
1390}
1391
1392/*
1393 * Look up a filename in a node attribute list.
1394 *
1395 * This routine gets called for any attribute fork that has more than one
1396 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1397 * "remote" values taking up more blocks.
1398 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001399STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400xfs_attr_node_get(xfs_da_args_t *args)
1401{
1402 xfs_da_state_t *state;
1403 xfs_da_state_blk_t *blk;
1404 int error, retval;
1405 int i;
1406
Dave Chinneree732592012-11-12 22:53:53 +11001407 trace_xfs_attr_node_get(args);
1408
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 state = xfs_da_state_alloc();
1410 state->args = args;
1411 state->mp = args->dp->i_mount;
1412 state->blocksize = state->mp->m_sb.sb_blocksize;
1413 state->node_ents = state->mp->m_attr_node_ents;
1414
1415 /*
1416 * Search to see if name exists, and get back a pointer to it.
1417 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001418 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 if (error) {
1420 retval = error;
1421 } else if (retval == EEXIST) {
1422 blk = &state->path.blk[ state->path.active-1 ];
1423 ASSERT(blk->bp != NULL);
1424 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1425
1426 /*
1427 * Get the value, local or "remote"
1428 */
Dave Chinner517c2222013-04-24 18:58:55 +10001429 retval = xfs_attr3_leaf_getvalue(blk->bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 if (!retval && (args->rmtblkno > 0)
1431 && !(args->flags & ATTR_KERNOVAL)) {
1432 retval = xfs_attr_rmtval_get(args);
1433 }
1434 }
1435
1436 /*
1437 * If not in a transaction, we have to release all the buffers.
1438 */
1439 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001440 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 state->path.blk[i].bp = NULL;
1442 }
1443
1444 xfs_da_state_free(state);
1445 return(retval);
1446}