blob: 4e126f41a0aa97d1f73773ea8efb89ef7a52746c [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 Chinner70a98832013-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_mount.h"
Dave Chinner57062782013-10-15 09:17:51 +110026#include "xfs_da_format.h"
Nathan Scotta844f452005-11-02 14:38:42 +110027#include "xfs_da_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110028#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110030#include "xfs_alloc.h"
Dave Chinner239880e2013-10-23 10:50:10 +110031#include "xfs_trans.h"
Nathan Scotta844f452005-11-02 14:38:42 +110032#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_bmap.h"
Dave Chinner68988112013-08-12 20:49:42 +100034#include "xfs_bmap_util.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110035#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_attr.h"
37#include "xfs_attr_leaf.h"
Dave Chinner95920cd2013-04-03 16:11:27 +110038#include "xfs_attr_remote.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include "xfs_quota.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include "xfs_trans_space.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000042#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44/*
45 * xfs_attr.c
46 *
47 * Provide the external interfaces to manage attribute lists.
48 */
49
50/*========================================================================
51 * Function prototypes for the kernel.
52 *========================================================================*/
53
54/*
55 * Internal routines when attribute list fits inside the inode.
56 */
57STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
58
59/*
60 * Internal routines when attribute list is one block.
61 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100062STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063STATIC int xfs_attr_leaf_addname(xfs_da_args_t *args);
64STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66/*
67 * Internal routines when attribute list is more than one block.
68 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100069STATIC int xfs_attr_node_get(xfs_da_args_t *args);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070STATIC int xfs_attr_node_addname(xfs_da_args_t *args);
71STATIC int xfs_attr_node_removename(xfs_da_args_t *args);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
73STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Barry Naujoke8b0eba2008-04-22 17:34:31 +100076STATIC int
Christoph Hellwig67fd7182014-05-13 16:34:43 +100077xfs_attr_args_init(
78 struct xfs_da_args *args,
79 struct xfs_inode *dp,
80 const unsigned char *name,
81 int flags)
Barry Naujoke8b0eba2008-04-22 17:34:31 +100082{
Christoph Hellwig67fd7182014-05-13 16:34:43 +100083
84 if (!name)
Dave Chinner24513372014-06-25 14:58:08 +100085 return -EINVAL;
Christoph Hellwig67fd7182014-05-13 16:34:43 +100086
87 memset(args, 0, sizeof(*args));
Dave Chinner0650b552014-06-06 15:01:58 +100088 args->geo = dp->i_mount->m_attr_geo;
Christoph Hellwig67fd7182014-05-13 16:34:43 +100089 args->whichfork = XFS_ATTR_FORK;
90 args->dp = dp;
91 args->flags = flags;
92 args->name = name;
93 args->namelen = strlen((const char *)name);
94 if (args->namelen >= MAXNAMELEN)
Dave Chinner24513372014-06-25 14:58:08 +100095 return -EFAULT; /* match IRIX behaviour */
Barry Naujoke8b0eba2008-04-22 17:34:31 +100096
Christoph Hellwig67fd7182014-05-13 16:34:43 +100097 args->hashval = xfs_da_hashname(args->name, args->namelen);
Barry Naujoke8b0eba2008-04-22 17:34:31 +100098 return 0;
99}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Dave Chinnerabec5f22013-08-12 20:49:38 +1000101int
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +1000102xfs_inode_hasattr(
103 struct xfs_inode *ip)
104{
105 if (!XFS_IFORK_Q(ip) ||
106 (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
107 ip->i_d.di_anextents == 0))
108 return 0;
109 return 1;
110}
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112/*========================================================================
113 * Overall external interface routines.
114 *========================================================================*/
115
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000116int
117xfs_attr_get(
Christoph Hellwige82fa0c2009-11-14 16:17:20 +0000118 struct xfs_inode *ip,
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000119 const unsigned char *name,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100120 unsigned char *value,
Christoph Hellwige82fa0c2009-11-14 16:17:20 +0000121 int *valuelenp,
122 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000124 struct xfs_da_args args;
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000125 uint lock_mode;
126 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100128 XFS_STATS_INC(ip->i_mount, xs_attr_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
Dave Chinner24513372014-06-25 14:58:08 +1000131 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 if (!xfs_inode_hasattr(ip))
Dave Chinner24513372014-06-25 14:58:08 +1000134 return -ENOATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000136 error = xfs_attr_args_init(&args, ip, name, flags);
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000137 if (error)
138 return error;
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 args.value = value;
141 args.valuelen = *valuelenp;
Eric Sandeenc400ee32015-08-19 10:30:48 +1000142 /* Entirely possible to look up a name which doesn't exist */
143 args.op_flags = XFS_DA_OP_OKNOENT;
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))
Dave Chinner24513372014-06-25 14:58:08 +1000147 error = -ENOATTR;
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000148 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;
Dave Chinner24513372014-06-25 14:58:08 +1000157 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 */
Dave Chinnerc59f0ad2014-06-06 15:21:27 +1000176 size = xfs_attr_leaf_newentsize(args, local);
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000177 nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
178 if (*local) {
Dave Chinner33a60392014-06-06 15:21:10 +1000179 if (size > (args->geo->blksize / 2)) {
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000180 /* Double split possible */
181 nblks *= 2;
182 }
183 } else {
184 /*
185 * Out of line attribute, cannot double split, but
186 * make room for the attribute value itself.
187 */
Dave Chinner2d6dcc62014-05-15 09:39:28 +1000188 uint dblocks = xfs_attr3_rmt_blocks(mp, args->valuelen);
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000189 nblks += dblocks;
190 nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
191 }
192
193 return nblks;
194}
195
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000196int
197xfs_attr_set(
198 struct xfs_inode *dp,
199 const unsigned char *name,
200 unsigned char *value,
201 int valuelen,
202 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
Jie Liu3d3c8b52013-08-12 20:49:59 +1000204 struct xfs_mount *mp = dp->i_mount;
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000205 struct xfs_da_args args;
206 struct xfs_bmap_free flist;
Jie Liu3d3c8b52013-08-12 20:49:59 +1000207 struct xfs_trans_res tres;
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000208 xfs_fsblock_t firstblock;
Jie Liu3d3c8b52013-08-12 20:49:59 +1000209 int rsvd = (flags & ATTR_ROOT) != 0;
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100210 int error, err2, local;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100212 XFS_STATS_INC(mp, xs_attr_set);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000213
214 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
Dave Chinner24513372014-06-25 14:58:08 +1000215 return -EIO;
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000216
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000217 error = xfs_attr_args_init(&args, dp, name, flags);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000218 if (error)
219 return error;
220
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000221 args.value = value;
222 args.valuelen = valuelen;
223 args.firstblock = &firstblock;
224 args.flist = &flist;
225 args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
Christoph Hellwig6c888af2014-05-13 16:40:19 +1000226 args.total = xfs_attr_calc_size(&args, &local);
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000227
Christoph Hellwig7d095252009-06-08 15:33:32 +0200228 error = xfs_qm_dqattach(dp, 0);
229 if (error)
230 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 /*
233 * If the inode doesn't have an attribute fork, add one.
234 * (inode must not be locked when we call this routine)
235 */
236 if (XFS_IFORK_Q(dp) == 0) {
Barry Naujoke5889e92007-02-10 18:35:58 +1100237 int sf_size = sizeof(xfs_attr_sf_hdr_t) +
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000238 XFS_ATTR_SF_ENTSIZE_BYNAME(args.namelen, valuelen);
Barry Naujoke5889e92007-02-10 18:35:58 +1100239
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000240 error = xfs_bmap_add_attrfork(dp, sf_size, rsvd);
241 if (error)
242 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 }
244
Christoph Hellwig253f4912016-04-06 09:19:55 +1000245 tres.tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
246 M_RES(mp)->tr_attrsetrt.tr_logres * args.total;
247 tres.tr_logcount = XFS_ATTRSET_LOG_COUNT;
248 tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 /*
251 * Root fork attributes can use reserved data blocks for this
252 * operation if necessary
253 */
Christoph Hellwig253f4912016-04-06 09:19:55 +1000254 error = xfs_trans_alloc(mp, &tres, args.total, 0,
255 rsvd ? XFS_TRANS_RESERVE : 0, &args.trans);
256 if (error)
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000257 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Christoph Hellwig253f4912016-04-06 09:19:55 +1000259 xfs_ilock(dp, XFS_ILOCK_EXCL);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200260 error = xfs_trans_reserve_quota_nblks(args.trans, dp, args.total, 0,
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000261 rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
262 XFS_QMOPT_RES_REGBLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 if (error) {
264 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Christoph Hellwig4906e212015-06-04 13:47:56 +1000265 xfs_trans_cancel(args.trans);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000266 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 }
268
Christoph Hellwigddc34152011-09-19 15:00:54 +0000269 xfs_trans_ijoin(args.trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000272 * If the attribute list is non-existent or a shortform list,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 * upgrade it to a single-leaf-block attribute list.
274 */
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000275 if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL ||
276 (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
277 dp->i_d.di_anextents == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 /*
280 * Build initial attribute list (if required).
281 */
282 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
Nathan Scottd8cc8902005-11-02 10:34:53 +1100283 xfs_attr_shortform_create(&args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 /*
286 * Try to add the attr to the attribute list in
287 * the inode.
288 */
289 error = xfs_attr_shortform_addname(&args);
Dave Chinner24513372014-06-25 14:58:08 +1000290 if (error != -ENOSPC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 /*
292 * Commit the shortform mods, and we're done.
293 * NOTE: this is also the error path (EEXIST, etc).
294 */
295 ASSERT(args.trans != NULL);
296
297 /*
298 * If this is a synchronous mount, make sure that
299 * the transaction goes to disk before returning
300 * to the user.
301 */
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000302 if (mp->m_flags & XFS_MOUNT_WSYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 xfs_trans_set_sync(args.trans);
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000304
305 if (!error && (flags & ATTR_KERNOTIME) == 0) {
306 xfs_trans_ichgtime(args.trans, dp,
307 XFS_ICHGTIME_CHG);
308 }
Christoph Hellwig70393312015-06-04 13:48:08 +1000309 err2 = xfs_trans_commit(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 xfs_iunlock(dp, XFS_ILOCK_EXCL);
311
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000312 return error ? error : err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
314
315 /*
316 * It won't fit in the shortform, transform to a leaf block.
317 * GROT: another possible req'mt for a double-split btree op.
318 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600319 xfs_bmap_init(args.flist, args.firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 error = xfs_attr_shortform_to_leaf(&args);
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100321 if (!error)
322 error = xfs_bmap_finish(&args.trans, args.flist, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 args.trans = NULL;
325 xfs_bmap_cancel(&flist);
326 goto out;
327 }
328
329 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 * Commit the leaf transformation. We'll need another (linked)
331 * transaction to add the new attribute to the leaf.
332 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000333
334 error = xfs_trans_roll(&args.trans, dp);
335 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 goto out;
337
338 }
339
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000340 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 error = xfs_attr_leaf_addname(&args);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000342 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 error = xfs_attr_node_addname(&args);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000344 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 /*
348 * If this is a synchronous mount, make sure that the
349 * transaction goes to disk before returning to the user.
350 */
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000351 if (mp->m_flags & XFS_MOUNT_WSYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 xfs_trans_set_sync(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000354 if ((flags & ATTR_KERNOTIME) == 0)
355 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 /*
358 * Commit the last in the sequence of transactions.
359 */
360 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
Christoph Hellwig70393312015-06-04 13:48:08 +1000361 error = xfs_trans_commit(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 xfs_iunlock(dp, XFS_ILOCK_EXCL);
363
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000364 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366out:
Christoph Hellwig4906e212015-06-04 13:47:56 +1000367 if (args.trans)
368 xfs_trans_cancel(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000370 return error;
Nathan Scottaa82daa2005-11-02 10:33:33 +1100371}
372
373/*
374 * Generic handler routine to remove a name from an attribute list.
375 * Transitions attribute list from Btree to shortform as necessary.
376 */
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000377int
378xfs_attr_remove(
379 struct xfs_inode *dp,
380 const unsigned char *name,
381 int flags)
Nathan Scottaa82daa2005-11-02 10:33:33 +1100382{
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000383 struct xfs_mount *mp = dp->i_mount;
384 struct xfs_da_args args;
385 struct xfs_bmap_free flist;
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000386 xfs_fsblock_t firstblock;
387 int error;
Nathan Scottaa82daa2005-11-02 10:33:33 +1100388
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100389 XFS_STATS_INC(mp, xs_attr_remove);
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000390
391 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
Dave Chinner24513372014-06-25 14:58:08 +1000392 return -EIO;
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000393
394 if (!xfs_inode_hasattr(dp))
Dave Chinner24513372014-06-25 14:58:08 +1000395 return -ENOATTR;
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000396
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000397 error = xfs_attr_args_init(&args, dp, name, flags);
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000398 if (error)
399 return error;
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 args.firstblock = &firstblock;
402 args.flist = &flist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 /*
Dave Chinner4a338212011-06-23 01:35:01 +0000405 * we have no control over the attribute names that userspace passes us
406 * to remove, so we have to allow the name lookup prior to attribute
407 * removal to fail.
408 */
409 args.op_flags = XFS_DA_OP_OKNOENT;
410
Christoph Hellwig7d095252009-06-08 15:33:32 +0200411 error = xfs_qm_dqattach(dp, 0);
412 if (error)
413 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 * Root fork attributes can use reserved data blocks for this
417 * operation if necessary
418 */
Christoph Hellwig253f4912016-04-06 09:19:55 +1000419 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_attrrm,
420 XFS_ATTRRM_SPACE_RES(mp), 0,
421 (flags & ATTR_ROOT) ? XFS_TRANS_RESERVE : 0,
422 &args.trans);
423 if (error)
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000424 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 xfs_ilock(dp, XFS_ILOCK_EXCL);
427 /*
428 * No need to make quota reservations here. We expect to release some
429 * blocks not allocate in the common case.
430 */
Christoph Hellwigddc34152011-09-19 15:00:54 +0000431 xfs_trans_ijoin(args.trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +1000433 if (!xfs_inode_hasattr(dp)) {
Dave Chinner24513372014-06-25 14:58:08 +1000434 error = -ENOATTR;
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000435 } else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
437 error = xfs_attr_shortform_remove(&args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
439 error = xfs_attr_leaf_removename(&args);
440 } else {
441 error = xfs_attr_node_removename(&args);
442 }
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000443
444 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 /*
448 * If this is a synchronous mount, make sure that the
449 * transaction goes to disk before returning to the user.
450 */
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000451 if (mp->m_flags & XFS_MOUNT_WSYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 xfs_trans_set_sync(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000454 if ((flags & ATTR_KERNOTIME) == 0)
455 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 /*
458 * Commit the last in the sequence of transactions.
459 */
460 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
Christoph Hellwig70393312015-06-04 13:48:08 +1000461 error = xfs_trans_commit(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 xfs_iunlock(dp, XFS_ILOCK_EXCL);
463
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000464 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466out:
Christoph Hellwig4906e212015-06-04 13:47:56 +1000467 if (args.trans)
468 xfs_trans_cancel(args.trans);
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000469 xfs_iunlock(dp, XFS_ILOCK_EXCL);
470 return error;
Nathan Scottaa82daa2005-11-02 10:33:33 +1100471}
472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473/*========================================================================
474 * External routines when attribute list is inside the inode
475 *========================================================================*/
476
477/*
478 * Add a name to the shortform attribute list structure
479 * This is the external routine.
480 */
481STATIC int
482xfs_attr_shortform_addname(xfs_da_args_t *args)
483{
Nathan Scottd8cc8902005-11-02 10:34:53 +1100484 int newsize, forkoff, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Dave Chinner5a5881c2012-03-22 05:15:13 +0000486 trace_xfs_attr_sf_addname(args);
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 retval = xfs_attr_shortform_lookup(args);
Dave Chinner24513372014-06-25 14:58:08 +1000489 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
Eric Sandeend99831f2014-06-22 15:03:54 +1000490 return retval;
Dave Chinner24513372014-06-25 14:58:08 +1000491 } else if (retval == -EEXIST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 if (args->flags & ATTR_CREATE)
Eric Sandeend99831f2014-06-22 15:03:54 +1000493 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 retval = xfs_attr_shortform_remove(args);
495 ASSERT(retval == 0);
496 }
497
Nathan Scottd8cc8902005-11-02 10:34:53 +1100498 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
499 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
Dave Chinner24513372014-06-25 14:58:08 +1000500 return -ENOSPC;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
503 newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100504
505 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
506 if (!forkoff)
Dave Chinner24513372014-06-25 14:58:08 +1000507 return -ENOSPC;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100508
509 xfs_attr_shortform_add(args, forkoff);
Eric Sandeend99831f2014-06-22 15:03:54 +1000510 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511}
512
513
514/*========================================================================
515 * External routines when attribute list is one block
516 *========================================================================*/
517
518/*
519 * Add a name to the leaf attribute list structure
520 *
521 * This leaf block cannot have a "remote" value, we only call this routine
522 * if bmap_one_block() says there is only one block (ie: no remote blks).
523 */
David Chinnera8272ce2007-11-23 16:28:09 +1100524STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525xfs_attr_leaf_addname(xfs_da_args_t *args)
526{
527 xfs_inode_t *dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000528 struct xfs_buf *bp;
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100529 int retval, error, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Dave Chinner5a5881c2012-03-22 05:15:13 +0000531 trace_xfs_attr_leaf_addname(args);
532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 /*
534 * Read the (only) block in the attribute list in.
535 */
536 dp = args->dp;
537 args->blkno = 0;
Dave Chinner517c2222013-04-24 18:58:55 +1000538 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100540 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 /*
543 * Look up the given attribute in the leaf block. Figure out if
544 * the given flags produce an error or call for an atomic rename.
545 */
Dave Chinner517c2222013-04-24 18:58:55 +1000546 retval = xfs_attr3_leaf_lookup_int(bp, args);
Dave Chinner24513372014-06-25 14:58:08 +1000547 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000548 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000549 return retval;
Dave Chinner24513372014-06-25 14:58:08 +1000550 } else if (retval == -EEXIST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 if (args->flags & ATTR_CREATE) { /* pure create op */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000552 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000553 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 }
Dave Chinner5a5881c2012-03-22 05:15:13 +0000555
556 trace_xfs_attr_leaf_replace(args);
557
Dave Chinner8275cdd2014-05-06 07:37:31 +1000558 /* save the attribute state for later removal*/
Barry Naujok6a178102008-05-21 16:42:05 +1000559 args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 args->blkno2 = args->blkno; /* set 2nd entry info*/
561 args->index2 = args->index;
562 args->rmtblkno2 = args->rmtblkno;
563 args->rmtblkcnt2 = args->rmtblkcnt;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000564 args->rmtvaluelen2 = args->rmtvaluelen;
565
566 /*
567 * clear the remote attr state now that it is saved so that the
568 * values reflect the state of the attribute we are about to
569 * add, not the attribute we just found and will remove later.
570 */
571 args->rmtblkno = 0;
572 args->rmtblkcnt = 0;
573 args->rmtvaluelen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 }
575
576 /*
577 * Add the attribute to the leaf block, transitioning to a Btree
578 * if required.
579 */
Dave Chinner517c2222013-04-24 18:58:55 +1000580 retval = xfs_attr3_leaf_add(bp, args);
Dave Chinner24513372014-06-25 14:58:08 +1000581 if (retval == -ENOSPC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 /*
583 * Promote the attribute list to the Btree format, then
584 * Commit that transaction so that the node_addname() call
585 * can manage its own transactions.
586 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600587 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000588 error = xfs_attr3_leaf_to_node(args);
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100589 if (!error)
590 error = xfs_bmap_finish(&args->trans, args->flist, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 args->trans = NULL;
593 xfs_bmap_cancel(args->flist);
Eric Sandeend99831f2014-06-22 15:03:54 +1000594 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 }
596
597 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 * Commit the current trans (including the inode) and start
599 * a new one.
600 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000601 error = xfs_trans_roll(&args->trans, dp);
602 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000603 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 /*
606 * Fob the whole rest of the problem off on the Btree code.
607 */
608 error = xfs_attr_node_addname(args);
Eric Sandeend99831f2014-06-22 15:03:54 +1000609 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 }
611
612 /*
613 * Commit the transaction that added the attr name so that
614 * later routines can manage their own transactions.
615 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000616 error = xfs_trans_roll(&args->trans, dp);
617 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000618 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620 /*
621 * If there was an out-of-line value, allocate the blocks we
622 * identified for its storage and copy the value. This is done
623 * after we create the attribute so that we don't overflow the
624 * maximum size of a transaction and/or hit a deadlock.
625 */
626 if (args->rmtblkno > 0) {
627 error = xfs_attr_rmtval_set(args);
628 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000629 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 }
631
632 /*
633 * If this is an atomic rename operation, we must "flip" the
634 * incomplete flags on the "new" and "old" attribute/value pairs
635 * so that one disappears and one appears atomically. Then we
636 * must remove the "old" attribute/value pair.
637 */
Barry Naujok6a178102008-05-21 16:42:05 +1000638 if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 /*
640 * In a separate transaction, set the incomplete flag on the
641 * "old" attr and clear the incomplete flag on the "new" attr.
642 */
Dave Chinner517c2222013-04-24 18:58:55 +1000643 error = xfs_attr3_leaf_flipflags(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000645 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647 /*
648 * Dismantle the "old" attribute/value pair by removing
649 * a "remote" value (if it exists).
650 */
651 args->index = args->index2;
652 args->blkno = args->blkno2;
653 args->rmtblkno = args->rmtblkno2;
654 args->rmtblkcnt = args->rmtblkcnt2;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000655 args->rmtvaluelen = args->rmtvaluelen2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 if (args->rmtblkno) {
657 error = xfs_attr_rmtval_remove(args);
658 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000659 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 }
661
662 /*
663 * Read in the block containing the "old" attr, then
664 * remove the "old" attr from that block (neat, huh!)
665 */
Dave Chinner517c2222013-04-24 18:58:55 +1000666 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
Dave Chinnerad14c332012-11-12 22:54:16 +1100667 -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100669 return error;
670
Dave Chinner517c2222013-04-24 18:58:55 +1000671 xfs_attr3_leaf_remove(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 /*
674 * If the result is small enough, shrink it all into the inode.
675 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100676 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Eric Sandeen9d87c312009-01-14 23:22:07 -0600677 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000678 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 /* bp is gone due to xfs_da_shrink_inode */
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100680 if (!error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 error = xfs_bmap_finish(&args->trans,
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100682 args->flist, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 args->trans = NULL;
685 xfs_bmap_cancel(args->flist);
Eric Sandeend99831f2014-06-22 15:03:54 +1000686 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 }
Dave Chinner1d9025e2012-06-22 18:50:14 +1000688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 /*
691 * Commit the remove and start the next trans in series.
692 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000693 error = xfs_trans_roll(&args->trans, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 } else if (args->rmtblkno > 0) {
696 /*
697 * Added a "remote" value, just clear the incomplete flag.
698 */
Dave Chinner517c2222013-04-24 18:58:55 +1000699 error = xfs_attr3_leaf_clearflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 }
Dave Chinner517c2222013-04-24 18:58:55 +1000701 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702}
703
704/*
705 * Remove a name from the leaf attribute list structure
706 *
707 * This leaf block cannot have a "remote" value, we only call this routine
708 * if bmap_one_block() says there is only one block (ie: no remote blks).
709 */
710STATIC int
711xfs_attr_leaf_removename(xfs_da_args_t *args)
712{
713 xfs_inode_t *dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000714 struct xfs_buf *bp;
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100715 int error, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Dave Chinner5a5881c2012-03-22 05:15:13 +0000717 trace_xfs_attr_leaf_removename(args);
718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 /*
720 * Remove the attribute.
721 */
722 dp = args->dp;
723 args->blkno = 0;
Dave Chinner517c2222013-04-24 18:58:55 +1000724 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Dave Chinnerad14c332012-11-12 22:54:16 +1100725 if (error)
726 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Dave Chinner517c2222013-04-24 18:58:55 +1000728 error = xfs_attr3_leaf_lookup_int(bp, args);
Dave Chinner24513372014-06-25 14:58:08 +1000729 if (error == -ENOATTR) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000730 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000731 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 }
733
Dave Chinner517c2222013-04-24 18:58:55 +1000734 xfs_attr3_leaf_remove(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736 /*
737 * If the result is small enough, shrink it all into the inode.
738 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100739 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Eric Sandeen9d87c312009-01-14 23:22:07 -0600740 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000741 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 /* bp is gone due to xfs_da_shrink_inode */
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100743 if (!error)
744 error = xfs_bmap_finish(&args->trans, args->flist, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 args->trans = NULL;
747 xfs_bmap_cancel(args->flist);
Dave Chinner517c2222013-04-24 18:58:55 +1000748 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 }
Dave Chinner1d9025e2012-06-22 18:50:14 +1000750 }
Dave Chinner517c2222013-04-24 18:58:55 +1000751 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752}
753
754/*
755 * Look up a name in a leaf attribute list structure.
756 *
757 * This leaf block cannot have a "remote" value, we only call this routine
758 * if bmap_one_block() says there is only one block (ie: no remote blks).
759 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000760STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761xfs_attr_leaf_get(xfs_da_args_t *args)
762{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000763 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 int error;
765
Dave Chinneree732592012-11-12 22:53:53 +1100766 trace_xfs_attr_leaf_get(args);
767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 args->blkno = 0;
Dave Chinner517c2222013-04-24 18:58:55 +1000769 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100771 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Dave Chinner517c2222013-04-24 18:58:55 +1000773 error = xfs_attr3_leaf_lookup_int(bp, args);
Dave Chinner24513372014-06-25 14:58:08 +1000774 if (error != -EEXIST) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000775 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000776 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 }
Dave Chinner517c2222013-04-24 18:58:55 +1000778 error = xfs_attr3_leaf_getvalue(bp, args);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000779 xfs_trans_brelse(args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
781 error = xfs_attr_rmtval_get(args);
782 }
Dave Chinner517c2222013-04-24 18:58:55 +1000783 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784}
785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786/*========================================================================
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000787 * External routines when attribute list size > geo->blksize
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 *========================================================================*/
789
790/*
791 * Add a name to a Btree-format attribute list.
792 *
793 * This will involve walking down the Btree, and may involve splitting
794 * leaf nodes and even splitting intermediate nodes up to and including
795 * the root node (a special case of an intermediate node).
796 *
797 * "Remote" attribute values confuse the issue and atomic rename operations
798 * add a whole extra layer of confusion on top of that.
799 */
800STATIC int
801xfs_attr_node_addname(xfs_da_args_t *args)
802{
803 xfs_da_state_t *state;
804 xfs_da_state_blk_t *blk;
805 xfs_inode_t *dp;
806 xfs_mount_t *mp;
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100807 int retval, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Dave Chinner5a5881c2012-03-22 05:15:13 +0000809 trace_xfs_attr_node_addname(args);
810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 /*
812 * Fill in bucket of arguments/results/context to carry around.
813 */
814 dp = args->dp;
815 mp = dp->i_mount;
816restart:
817 state = xfs_da_state_alloc();
818 state->args = args;
819 state->mp = mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 /*
822 * Search to see if name already exists, and get back a pointer
823 * to where it should go.
824 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000825 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 if (error)
827 goto out;
828 blk = &state->path.blk[ state->path.active-1 ];
829 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner24513372014-06-25 14:58:08 +1000830 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 goto out;
Dave Chinner24513372014-06-25 14:58:08 +1000832 } else if (retval == -EEXIST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 if (args->flags & ATTR_CREATE)
834 goto out;
Dave Chinner5a5881c2012-03-22 05:15:13 +0000835
836 trace_xfs_attr_node_replace(args);
837
Dave Chinner8275cdd2014-05-06 07:37:31 +1000838 /* save the attribute state for later removal*/
Barry Naujok6a178102008-05-21 16:42:05 +1000839 args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 args->blkno2 = args->blkno; /* set 2nd entry info*/
841 args->index2 = args->index;
842 args->rmtblkno2 = args->rmtblkno;
843 args->rmtblkcnt2 = args->rmtblkcnt;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000844 args->rmtvaluelen2 = args->rmtvaluelen;
845
846 /*
847 * clear the remote attr state now that it is saved so that the
848 * values reflect the state of the attribute we are about to
849 * add, not the attribute we just found and will remove later.
850 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 args->rmtblkno = 0;
852 args->rmtblkcnt = 0;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000853 args->rmtvaluelen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 }
855
Dave Chinner517c2222013-04-24 18:58:55 +1000856 retval = xfs_attr3_leaf_add(blk->bp, state->args);
Dave Chinner24513372014-06-25 14:58:08 +1000857 if (retval == -ENOSPC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 if (state->path.active == 1) {
859 /*
860 * Its really a single leaf node, but it had
861 * out-of-line values so it looked like it *might*
862 * have been a b-tree.
863 */
864 xfs_da_state_free(state);
Eric Sandeen6dd93e92013-07-31 20:18:54 -0500865 state = NULL;
Eric Sandeen9d87c312009-01-14 23:22:07 -0600866 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000867 error = xfs_attr3_leaf_to_node(args);
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100868 if (!error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 error = xfs_bmap_finish(&args->trans,
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100870 args->flist, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 args->trans = NULL;
873 xfs_bmap_cancel(args->flist);
874 goto out;
875 }
876
877 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 * Commit the node conversion and start the next
879 * trans in the chain.
880 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000881 error = xfs_trans_roll(&args->trans, dp);
882 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 goto out;
884
885 goto restart;
886 }
887
888 /*
889 * Split as many Btree elements as required.
890 * This code tracks the new and old attr's location
891 * in the index/blkno/rmtblkno/rmtblkcnt fields and
892 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
893 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600894 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000895 error = xfs_da3_split(state);
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100896 if (!error)
897 error = xfs_bmap_finish(&args->trans, args->flist, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 args->trans = NULL;
900 xfs_bmap_cancel(args->flist);
901 goto out;
902 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 } else {
904 /*
905 * Addition succeeded, update Btree hashvals.
906 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000907 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 }
909
910 /*
911 * Kill the state structure, we're done with it and need to
912 * allow the buffers to come back later.
913 */
914 xfs_da_state_free(state);
915 state = NULL;
916
917 /*
918 * Commit the leaf addition or btree split and start the next
919 * trans in the chain.
920 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000921 error = xfs_trans_roll(&args->trans, dp);
922 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 goto out;
924
925 /*
926 * If there was an out-of-line value, allocate the blocks we
927 * identified for its storage and copy the value. This is done
928 * after we create the attribute so that we don't overflow the
929 * maximum size of a transaction and/or hit a deadlock.
930 */
931 if (args->rmtblkno > 0) {
932 error = xfs_attr_rmtval_set(args);
933 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000934 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 }
936
937 /*
938 * If this is an atomic rename operation, we must "flip" the
939 * incomplete flags on the "new" and "old" attribute/value pairs
940 * so that one disappears and one appears atomically. Then we
941 * must remove the "old" attribute/value pair.
942 */
Barry Naujok6a178102008-05-21 16:42:05 +1000943 if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 /*
945 * In a separate transaction, set the incomplete flag on the
946 * "old" attr and clear the incomplete flag on the "new" attr.
947 */
Dave Chinner517c2222013-04-24 18:58:55 +1000948 error = xfs_attr3_leaf_flipflags(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 if (error)
950 goto out;
951
952 /*
953 * Dismantle the "old" attribute/value pair by removing
954 * a "remote" value (if it exists).
955 */
956 args->index = args->index2;
957 args->blkno = args->blkno2;
958 args->rmtblkno = args->rmtblkno2;
959 args->rmtblkcnt = args->rmtblkcnt2;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000960 args->rmtvaluelen = args->rmtvaluelen2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 if (args->rmtblkno) {
962 error = xfs_attr_rmtval_remove(args);
963 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000964 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 }
966
967 /*
968 * Re-find the "old" attribute entry after any split ops.
969 * The INCOMPLETE flag means that we will find the "old"
970 * attr, not the "new" one.
971 */
972 args->flags |= XFS_ATTR_INCOMPLETE;
973 state = xfs_da_state_alloc();
974 state->args = args;
975 state->mp = mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 state->inleaf = 0;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000977 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 if (error)
979 goto out;
980
981 /*
982 * Remove the name and update the hashvals in the tree.
983 */
984 blk = &state->path.blk[ state->path.active-1 ];
985 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner517c2222013-04-24 18:58:55 +1000986 error = xfs_attr3_leaf_remove(blk->bp, args);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000987 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
989 /*
990 * Check to see if the tree needs to be collapsed.
991 */
992 if (retval && (state->path.active > 1)) {
Eric Sandeen9d87c312009-01-14 23:22:07 -0600993 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000994 error = xfs_da3_join(state);
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100995 if (!error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 error = xfs_bmap_finish(&args->trans,
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100997 args->flist, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 args->trans = NULL;
1000 xfs_bmap_cancel(args->flist);
1001 goto out;
1002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 }
1004
1005 /*
1006 * Commit and start the next trans in the chain.
1007 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10001008 error = xfs_trans_roll(&args->trans, dp);
1009 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 goto out;
1011
1012 } else if (args->rmtblkno > 0) {
1013 /*
1014 * Added a "remote" value, just clear the incomplete flag.
1015 */
Dave Chinner517c2222013-04-24 18:58:55 +10001016 error = xfs_attr3_leaf_clearflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 if (error)
1018 goto out;
1019 }
1020 retval = error = 0;
1021
1022out:
1023 if (state)
1024 xfs_da_state_free(state);
1025 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001026 return error;
1027 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028}
1029
1030/*
1031 * Remove a name from a B-tree attribute list.
1032 *
1033 * This will involve walking down the Btree, and may involve joining
1034 * leaf nodes and even joining intermediate nodes up to and including
1035 * the root node (a special case of an intermediate node).
1036 */
1037STATIC int
1038xfs_attr_node_removename(xfs_da_args_t *args)
1039{
1040 xfs_da_state_t *state;
1041 xfs_da_state_blk_t *blk;
1042 xfs_inode_t *dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001043 struct xfs_buf *bp;
Eric Sandeenf6106ef2016-01-11 11:34:01 +11001044 int retval, error, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Dave Chinner5a5881c2012-03-22 05:15:13 +00001046 trace_xfs_attr_node_removename(args);
1047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 /*
1049 * Tie a string around our finger to remind us where we are.
1050 */
1051 dp = args->dp;
1052 state = xfs_da_state_alloc();
1053 state->args = args;
1054 state->mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
1056 /*
1057 * Search to see if name exists, and get back a pointer to it.
1058 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001059 error = xfs_da3_node_lookup_int(state, &retval);
Dave Chinner24513372014-06-25 14:58:08 +10001060 if (error || (retval != -EEXIST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 if (error == 0)
1062 error = retval;
1063 goto out;
1064 }
1065
1066 /*
1067 * If there is an out-of-line value, de-allocate the blocks.
1068 * This is done before we remove the attribute so that we don't
1069 * overflow the maximum size of a transaction and/or hit a deadlock.
1070 */
1071 blk = &state->path.blk[ state->path.active-1 ];
1072 ASSERT(blk->bp != NULL);
1073 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1074 if (args->rmtblkno > 0) {
1075 /*
1076 * Fill in disk block numbers in the state structure
1077 * so that we can get the buffers back after we commit
1078 * several transactions in the following calls.
1079 */
1080 error = xfs_attr_fillstate(state);
1081 if (error)
1082 goto out;
1083
1084 /*
1085 * Mark the attribute as INCOMPLETE, then bunmapi() the
1086 * remote value.
1087 */
Dave Chinner517c2222013-04-24 18:58:55 +10001088 error = xfs_attr3_leaf_setflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 if (error)
1090 goto out;
1091 error = xfs_attr_rmtval_remove(args);
1092 if (error)
1093 goto out;
1094
1095 /*
1096 * Refill the state structure with buffers, the prior calls
1097 * released our buffers.
1098 */
1099 error = xfs_attr_refillstate(state);
1100 if (error)
1101 goto out;
1102 }
1103
1104 /*
1105 * Remove the name and update the hashvals in the tree.
1106 */
1107 blk = &state->path.blk[ state->path.active-1 ];
1108 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner517c2222013-04-24 18:58:55 +10001109 retval = xfs_attr3_leaf_remove(blk->bp, args);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001110 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
1112 /*
1113 * Check to see if the tree needs to be collapsed.
1114 */
1115 if (retval && (state->path.active > 1)) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001116 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001117 error = xfs_da3_join(state);
Eric Sandeenf6106ef2016-01-11 11:34:01 +11001118 if (!error)
1119 error = xfs_bmap_finish(&args->trans, args->flist, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 args->trans = NULL;
1122 xfs_bmap_cancel(args->flist);
1123 goto out;
1124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 /*
1126 * Commit the Btree join operation and start a new trans.
1127 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10001128 error = xfs_trans_roll(&args->trans, dp);
1129 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 goto out;
1131 }
1132
1133 /*
1134 * If the result is small enough, push it all into the inode.
1135 */
1136 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
1137 /*
1138 * Have to get rid of the copy of this dabuf in the state.
1139 */
1140 ASSERT(state->path.active == 1);
1141 ASSERT(state->path.blk[0].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 state->path.blk[0].bp = NULL;
1143
Dave Chinner517c2222013-04-24 18:58:55 +10001144 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 if (error)
1146 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Nathan Scottd8cc8902005-11-02 10:34:53 +11001148 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001149 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +10001150 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 /* bp is gone due to xfs_da_shrink_inode */
Eric Sandeenf6106ef2016-01-11 11:34:01 +11001152 if (!error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 error = xfs_bmap_finish(&args->trans,
Eric Sandeenf6106ef2016-01-11 11:34:01 +11001154 args->flist, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 args->trans = NULL;
1157 xfs_bmap_cancel(args->flist);
1158 goto out;
1159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 } else
Dave Chinner1d9025e2012-06-22 18:50:14 +10001161 xfs_trans_brelse(args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 }
1163 error = 0;
1164
1165out:
1166 xfs_da_state_free(state);
Eric Sandeend99831f2014-06-22 15:03:54 +10001167 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168}
1169
1170/*
1171 * Fill in the disk block numbers in the state structure for the buffers
1172 * that are attached to the state structure.
1173 * This is done so that we can quickly reattach ourselves to those buffers
Nathan Scottc41564b2006-03-29 08:55:14 +10001174 * after some set of transaction commits have released these buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 */
1176STATIC int
1177xfs_attr_fillstate(xfs_da_state_t *state)
1178{
1179 xfs_da_state_path_t *path;
1180 xfs_da_state_blk_t *blk;
1181 int level;
1182
Dave Chinneree732592012-11-12 22:53:53 +11001183 trace_xfs_attr_fillstate(state->args);
1184
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 /*
1186 * Roll down the "path" in the state structure, storing the on-disk
1187 * block number for those buffers in the "path".
1188 */
1189 path = &state->path;
1190 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1191 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1192 if (blk->bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001193 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 blk->bp = NULL;
1195 } else {
1196 blk->disk_blkno = 0;
1197 }
1198 }
1199
1200 /*
1201 * Roll down the "altpath" in the state structure, storing the on-disk
1202 * block number for those buffers in the "altpath".
1203 */
1204 path = &state->altpath;
1205 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1206 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1207 if (blk->bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001208 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 blk->bp = NULL;
1210 } else {
1211 blk->disk_blkno = 0;
1212 }
1213 }
1214
Eric Sandeend99831f2014-06-22 15:03:54 +10001215 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216}
1217
1218/*
1219 * Reattach the buffers to the state structure based on the disk block
1220 * numbers stored in the state structure.
Nathan Scottc41564b2006-03-29 08:55:14 +10001221 * This is done after some set of transaction commits have released those
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 * buffers from our grip.
1223 */
1224STATIC int
1225xfs_attr_refillstate(xfs_da_state_t *state)
1226{
1227 xfs_da_state_path_t *path;
1228 xfs_da_state_blk_t *blk;
1229 int level, error;
1230
Dave Chinneree732592012-11-12 22:53:53 +11001231 trace_xfs_attr_refillstate(state->args);
1232
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 /*
1234 * Roll down the "path" in the state structure, storing the on-disk
1235 * block number for those buffers in the "path".
1236 */
1237 path = &state->path;
1238 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1239 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1240 if (blk->disk_blkno) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001241 error = xfs_da3_node_read(state->args->trans,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 state->args->dp,
1243 blk->blkno, blk->disk_blkno,
Dave Chinnerd9392a42012-11-12 22:54:17 +11001244 &blk->bp, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001246 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 } else {
1248 blk->bp = NULL;
1249 }
1250 }
1251
1252 /*
1253 * Roll down the "altpath" in the state structure, storing the on-disk
1254 * block number for those buffers in the "altpath".
1255 */
1256 path = &state->altpath;
1257 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1258 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1259 if (blk->disk_blkno) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001260 error = xfs_da3_node_read(state->args->trans,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 state->args->dp,
1262 blk->blkno, blk->disk_blkno,
Dave Chinnerd9392a42012-11-12 22:54:17 +11001263 &blk->bp, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001265 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 } else {
1267 blk->bp = NULL;
1268 }
1269 }
1270
Eric Sandeend99831f2014-06-22 15:03:54 +10001271 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272}
1273
1274/*
1275 * Look up a filename in a node attribute list.
1276 *
1277 * This routine gets called for any attribute fork that has more than one
1278 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1279 * "remote" values taking up more blocks.
1280 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001281STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282xfs_attr_node_get(xfs_da_args_t *args)
1283{
1284 xfs_da_state_t *state;
1285 xfs_da_state_blk_t *blk;
1286 int error, retval;
1287 int i;
1288
Dave Chinneree732592012-11-12 22:53:53 +11001289 trace_xfs_attr_node_get(args);
1290
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 state = xfs_da_state_alloc();
1292 state->args = args;
1293 state->mp = args->dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 /*
1296 * Search to see if name exists, and get back a pointer to it.
1297 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001298 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 if (error) {
1300 retval = error;
Dave Chinner24513372014-06-25 14:58:08 +10001301 } else if (retval == -EEXIST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 blk = &state->path.blk[ state->path.active-1 ];
1303 ASSERT(blk->bp != NULL);
1304 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1305
1306 /*
1307 * Get the value, local or "remote"
1308 */
Dave Chinner517c2222013-04-24 18:58:55 +10001309 retval = xfs_attr3_leaf_getvalue(blk->bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 if (!retval && (args->rmtblkno > 0)
1311 && !(args->flags & ATTR_KERNOVAL)) {
1312 retval = xfs_attr_rmtval_get(args);
1313 }
1314 }
1315
1316 /*
1317 * If not in a transaction, we have to release all the buffers.
1318 */
1319 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001320 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 state->path.blk[i].bp = NULL;
1322 }
1323
1324 xfs_da_state_free(state);
Eric Sandeend99831f2014-06-22 15:03:54 +10001325 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326}