blob: 9687208c676f51f14ac1b07fa085980d55a571cd [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_mount.h"
Darrick J. Wong3ab78df2016-08-03 11:15:38 +100026#include "xfs_defer.h"
Dave Chinner57062782013-10-15 09:17:51 +110027#include "xfs_da_format.h"
Nathan Scotta844f452005-11-02 14:38:42 +110028#include "xfs_da_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110029#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110031#include "xfs_alloc.h"
Dave Chinner239880e2013-10-23 10:50:10 +110032#include "xfs_trans.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_bmap.h"
Dave Chinner68988112013-08-12 20:49:42 +100035#include "xfs_bmap_util.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110036#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "xfs_attr.h"
38#include "xfs_attr_leaf.h"
Dave Chinner95920cd2013-04-03 16:11:27 +110039#include "xfs_attr_remote.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include "xfs_quota.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include "xfs_trans_space.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000043#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45/*
46 * xfs_attr.c
47 *
48 * Provide the external interfaces to manage attribute lists.
49 */
50
51/*========================================================================
52 * Function prototypes for the kernel.
53 *========================================================================*/
54
55/*
56 * Internal routines when attribute list fits inside the inode.
57 */
58STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
59
60/*
61 * Internal routines when attribute list is one block.
62 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100063STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064STATIC int xfs_attr_leaf_addname(xfs_da_args_t *args);
65STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67/*
68 * Internal routines when attribute list is more than one block.
69 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100070STATIC int xfs_attr_node_get(xfs_da_args_t *args);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071STATIC int xfs_attr_node_addname(xfs_da_args_t *args);
72STATIC int xfs_attr_node_removename(xfs_da_args_t *args);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
74STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Barry Naujoke8b0eba2008-04-22 17:34:31 +100077STATIC int
Christoph Hellwig67fd7182014-05-13 16:34:43 +100078xfs_attr_args_init(
79 struct xfs_da_args *args,
80 struct xfs_inode *dp,
81 const unsigned char *name,
82 int flags)
Barry Naujoke8b0eba2008-04-22 17:34:31 +100083{
Christoph Hellwig67fd7182014-05-13 16:34:43 +100084
85 if (!name)
Dave Chinner24513372014-06-25 14:58:08 +100086 return -EINVAL;
Christoph Hellwig67fd7182014-05-13 16:34:43 +100087
88 memset(args, 0, sizeof(*args));
Dave Chinner0650b552014-06-06 15:01:58 +100089 args->geo = dp->i_mount->m_attr_geo;
Christoph Hellwig67fd7182014-05-13 16:34:43 +100090 args->whichfork = XFS_ATTR_FORK;
91 args->dp = dp;
92 args->flags = flags;
93 args->name = name;
94 args->namelen = strlen((const char *)name);
95 if (args->namelen >= MAXNAMELEN)
Dave Chinner24513372014-06-25 14:58:08 +100096 return -EFAULT; /* match IRIX behaviour */
Barry Naujoke8b0eba2008-04-22 17:34:31 +100097
Christoph Hellwig67fd7182014-05-13 16:34:43 +100098 args->hashval = xfs_da_hashname(args->name, args->namelen);
Barry Naujoke8b0eba2008-04-22 17:34:31 +100099 return 0;
100}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Dave Chinnerabec5f22013-08-12 20:49:38 +1000102int
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +1000103xfs_inode_hasattr(
104 struct xfs_inode *ip)
105{
106 if (!XFS_IFORK_Q(ip) ||
107 (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
108 ip->i_d.di_anextents == 0))
109 return 0;
110 return 1;
111}
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113/*========================================================================
114 * Overall external interface routines.
115 *========================================================================*/
116
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000117int
118xfs_attr_get(
Christoph Hellwige82fa0c2009-11-14 16:17:20 +0000119 struct xfs_inode *ip,
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000120 const unsigned char *name,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100121 unsigned char *value,
Christoph Hellwige82fa0c2009-11-14 16:17:20 +0000122 int *valuelenp,
123 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000125 struct xfs_da_args args;
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000126 uint lock_mode;
127 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100129 XFS_STATS_INC(ip->i_mount, xs_attr_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
Dave Chinner24513372014-06-25 14:58:08 +1000132 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000134 error = xfs_attr_args_init(&args, ip, name, flags);
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000135 if (error)
136 return error;
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 args.value = value;
139 args.valuelen = *valuelenp;
Eric Sandeenc400ee32015-08-19 10:30:48 +1000140 /* Entirely possible to look up a name which doesn't exist */
141 args.op_flags = XFS_DA_OP_OKNOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Christoph Hellwig683cb942013-12-06 12:30:15 -0800143 lock_mode = xfs_ilock_attr_map_shared(ip);
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000144 if (!xfs_inode_hasattr(ip))
Dave Chinner24513372014-06-25 14:58:08 +1000145 error = -ENOATTR;
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000146 else if (ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL)
147 error = xfs_attr_shortform_getvalue(&args);
148 else if (xfs_bmap_one_block(ip, XFS_ATTR_FORK))
149 error = xfs_attr_leaf_get(&args);
150 else
151 error = xfs_attr_node_get(&args);
Christoph Hellwig683cb942013-12-06 12:30:15 -0800152 xfs_iunlock(ip, lock_mode);
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000153
154 *valuelenp = args.valuelen;
Dave Chinner24513372014-06-25 14:58:08 +1000155 return error == -EEXIST ? 0 : error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000158/*
159 * Calculate how many blocks we need for the new attribute,
160 */
Eric Sandeen5d77c0d2009-11-19 15:52:00 +0000161STATIC int
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000162xfs_attr_calc_size(
Christoph Hellwig6c888af2014-05-13 16:40:19 +1000163 struct xfs_da_args *args,
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000164 int *local)
165{
Christoph Hellwig6c888af2014-05-13 16:40:19 +1000166 struct xfs_mount *mp = args->dp->i_mount;
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000167 int size;
168 int nblks;
169
170 /*
171 * Determine space new attribute will use, and if it would be
172 * "local" or "remote" (note: local != inline).
173 */
Dave Chinnerc59f0ad2014-06-06 15:21:27 +1000174 size = xfs_attr_leaf_newentsize(args, local);
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000175 nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
176 if (*local) {
Dave Chinner33a60392014-06-06 15:21:10 +1000177 if (size > (args->geo->blksize / 2)) {
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000178 /* Double split possible */
179 nblks *= 2;
180 }
181 } else {
182 /*
183 * Out of line attribute, cannot double split, but
184 * make room for the attribute value itself.
185 */
Dave Chinner2d6dcc62014-05-15 09:39:28 +1000186 uint dblocks = xfs_attr3_rmt_blocks(mp, args->valuelen);
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000187 nblks += dblocks;
188 nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
189 }
190
191 return nblks;
192}
193
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000194int
195xfs_attr_set(
196 struct xfs_inode *dp,
197 const unsigned char *name,
198 unsigned char *value,
199 int valuelen,
200 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Jie Liu3d3c8b52013-08-12 20:49:59 +1000202 struct xfs_mount *mp = dp->i_mount;
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000203 struct xfs_da_args args;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000204 struct xfs_defer_ops dfops;
Jie Liu3d3c8b52013-08-12 20:49:59 +1000205 struct xfs_trans_res tres;
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000206 xfs_fsblock_t firstblock;
Jie Liu3d3c8b52013-08-12 20:49:59 +1000207 int rsvd = (flags & ATTR_ROOT) != 0;
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100208 int error, err2, local;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100210 XFS_STATS_INC(mp, xs_attr_set);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000211
212 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
Dave Chinner24513372014-06-25 14:58:08 +1000213 return -EIO;
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000214
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000215 error = xfs_attr_args_init(&args, dp, name, flags);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000216 if (error)
217 return error;
218
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000219 args.value = value;
220 args.valuelen = valuelen;
221 args.firstblock = &firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000222 args.dfops = &dfops;
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000223 args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
Christoph Hellwig6c888af2014-05-13 16:40:19 +1000224 args.total = xfs_attr_calc_size(&args, &local);
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000225
Christoph Hellwig7d095252009-06-08 15:33:32 +0200226 error = xfs_qm_dqattach(dp, 0);
227 if (error)
228 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 /*
231 * If the inode doesn't have an attribute fork, add one.
232 * (inode must not be locked when we call this routine)
233 */
234 if (XFS_IFORK_Q(dp) == 0) {
Barry Naujoke5889e92007-02-10 18:35:58 +1100235 int sf_size = sizeof(xfs_attr_sf_hdr_t) +
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000236 XFS_ATTR_SF_ENTSIZE_BYNAME(args.namelen, valuelen);
Barry Naujoke5889e92007-02-10 18:35:58 +1100237
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000238 error = xfs_bmap_add_attrfork(dp, sf_size, rsvd);
239 if (error)
240 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 }
242
Christoph Hellwig253f4912016-04-06 09:19:55 +1000243 tres.tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
244 M_RES(mp)->tr_attrsetrt.tr_logres * args.total;
245 tres.tr_logcount = XFS_ATTRSET_LOG_COUNT;
246 tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 /*
249 * Root fork attributes can use reserved data blocks for this
250 * operation if necessary
251 */
Christoph Hellwig253f4912016-04-06 09:19:55 +1000252 error = xfs_trans_alloc(mp, &tres, args.total, 0,
253 rsvd ? XFS_TRANS_RESERVE : 0, &args.trans);
254 if (error)
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000255 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Christoph Hellwig253f4912016-04-06 09:19:55 +1000257 xfs_ilock(dp, XFS_ILOCK_EXCL);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200258 error = xfs_trans_reserve_quota_nblks(args.trans, dp, args.total, 0,
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000259 rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
260 XFS_QMOPT_RES_REGBLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 if (error) {
262 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Christoph Hellwig4906e212015-06-04 13:47:56 +1000263 xfs_trans_cancel(args.trans);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000264 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 }
266
Christoph Hellwigddc34152011-09-19 15:00:54 +0000267 xfs_trans_ijoin(args.trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000270 * If the attribute list is non-existent or a shortform list,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 * upgrade it to a single-leaf-block attribute list.
272 */
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000273 if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL ||
274 (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
275 dp->i_d.di_anextents == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277 /*
278 * Build initial attribute list (if required).
279 */
280 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
Nathan Scottd8cc8902005-11-02 10:34:53 +1100281 xfs_attr_shortform_create(&args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 /*
284 * Try to add the attr to the attribute list in
285 * the inode.
286 */
287 error = xfs_attr_shortform_addname(&args);
Dave Chinner24513372014-06-25 14:58:08 +1000288 if (error != -ENOSPC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 /*
290 * Commit the shortform mods, and we're done.
291 * NOTE: this is also the error path (EEXIST, etc).
292 */
293 ASSERT(args.trans != NULL);
294
295 /*
296 * If this is a synchronous mount, make sure that
297 * the transaction goes to disk before returning
298 * to the user.
299 */
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000300 if (mp->m_flags & XFS_MOUNT_WSYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 xfs_trans_set_sync(args.trans);
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000302
303 if (!error && (flags & ATTR_KERNOTIME) == 0) {
304 xfs_trans_ichgtime(args.trans, dp,
305 XFS_ICHGTIME_CHG);
306 }
Christoph Hellwig70393312015-06-04 13:48:08 +1000307 err2 = xfs_trans_commit(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 xfs_iunlock(dp, XFS_ILOCK_EXCL);
309
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000310 return error ? error : err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 }
312
313 /*
314 * It won't fit in the shortform, transform to a leaf block.
315 * GROT: another possible req'mt for a double-split btree op.
316 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000317 xfs_defer_init(args.dfops, args.firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 error = xfs_attr_shortform_to_leaf(&args);
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100319 if (!error)
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000320 error = xfs_defer_finish(&args.trans, args.dfops, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 args.trans = NULL;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000323 xfs_defer_cancel(&dfops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 goto out;
325 }
326
327 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 * Commit the leaf transformation. We'll need another (linked)
329 * transaction to add the new attribute to the leaf.
330 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000331
332 error = xfs_trans_roll(&args.trans, dp);
333 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 goto out;
335
336 }
337
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000338 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 error = xfs_attr_leaf_addname(&args);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000340 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 error = xfs_attr_node_addname(&args);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000342 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345 /*
346 * If this is a synchronous mount, make sure that the
347 * transaction goes to disk before returning to the user.
348 */
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000349 if (mp->m_flags & XFS_MOUNT_WSYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 xfs_trans_set_sync(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000352 if ((flags & ATTR_KERNOTIME) == 0)
353 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 /*
356 * Commit the last in the sequence of transactions.
357 */
358 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
Christoph Hellwig70393312015-06-04 13:48:08 +1000359 error = xfs_trans_commit(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 xfs_iunlock(dp, XFS_ILOCK_EXCL);
361
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000362 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364out:
Christoph Hellwig4906e212015-06-04 13:47:56 +1000365 if (args.trans)
366 xfs_trans_cancel(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000368 return error;
Nathan Scottaa82daa2005-11-02 10:33:33 +1100369}
370
371/*
372 * Generic handler routine to remove a name from an attribute list.
373 * Transitions attribute list from Btree to shortform as necessary.
374 */
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000375int
376xfs_attr_remove(
377 struct xfs_inode *dp,
378 const unsigned char *name,
379 int flags)
Nathan Scottaa82daa2005-11-02 10:33:33 +1100380{
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000381 struct xfs_mount *mp = dp->i_mount;
382 struct xfs_da_args args;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000383 struct xfs_defer_ops dfops;
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000384 xfs_fsblock_t firstblock;
385 int error;
Nathan Scottaa82daa2005-11-02 10:33:33 +1100386
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100387 XFS_STATS_INC(mp, xs_attr_remove);
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000388
389 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
Dave Chinner24513372014-06-25 14:58:08 +1000390 return -EIO;
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000391
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000392 error = xfs_attr_args_init(&args, dp, name, flags);
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000393 if (error)
394 return error;
395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 args.firstblock = &firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000397 args.dfops = &dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 /*
Dave Chinner4a338212011-06-23 01:35:01 +0000400 * we have no control over the attribute names that userspace passes us
401 * to remove, so we have to allow the name lookup prior to attribute
402 * removal to fail.
403 */
404 args.op_flags = XFS_DA_OP_OKNOENT;
405
Christoph Hellwig7d095252009-06-08 15:33:32 +0200406 error = xfs_qm_dqattach(dp, 0);
407 if (error)
408 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 * Root fork attributes can use reserved data blocks for this
412 * operation if necessary
413 */
Christoph Hellwig253f4912016-04-06 09:19:55 +1000414 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_attrrm,
415 XFS_ATTRRM_SPACE_RES(mp), 0,
416 (flags & ATTR_ROOT) ? XFS_TRANS_RESERVE : 0,
417 &args.trans);
418 if (error)
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000419 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 xfs_ilock(dp, XFS_ILOCK_EXCL);
422 /*
423 * No need to make quota reservations here. We expect to release some
424 * blocks not allocate in the common case.
425 */
Christoph Hellwigddc34152011-09-19 15:00:54 +0000426 xfs_trans_ijoin(args.trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +1000428 if (!xfs_inode_hasattr(dp)) {
Dave Chinner24513372014-06-25 14:58:08 +1000429 error = -ENOATTR;
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000430 } else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
432 error = xfs_attr_shortform_remove(&args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
434 error = xfs_attr_leaf_removename(&args);
435 } else {
436 error = xfs_attr_node_removename(&args);
437 }
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000438
439 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 /*
443 * If this is a synchronous mount, make sure that the
444 * transaction goes to disk before returning to the user.
445 */
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000446 if (mp->m_flags & XFS_MOUNT_WSYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 xfs_trans_set_sync(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000449 if ((flags & ATTR_KERNOTIME) == 0)
450 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 /*
453 * Commit the last in the sequence of transactions.
454 */
455 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
Christoph Hellwig70393312015-06-04 13:48:08 +1000456 error = xfs_trans_commit(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 xfs_iunlock(dp, XFS_ILOCK_EXCL);
458
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000459 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461out:
Christoph Hellwig4906e212015-06-04 13:47:56 +1000462 if (args.trans)
463 xfs_trans_cancel(args.trans);
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000464 xfs_iunlock(dp, XFS_ILOCK_EXCL);
465 return error;
Nathan Scottaa82daa2005-11-02 10:33:33 +1100466}
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468/*========================================================================
469 * External routines when attribute list is inside the inode
470 *========================================================================*/
471
472/*
473 * Add a name to the shortform attribute list structure
474 * This is the external routine.
475 */
476STATIC int
477xfs_attr_shortform_addname(xfs_da_args_t *args)
478{
Nathan Scottd8cc8902005-11-02 10:34:53 +1100479 int newsize, forkoff, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Dave Chinner5a5881c2012-03-22 05:15:13 +0000481 trace_xfs_attr_sf_addname(args);
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 retval = xfs_attr_shortform_lookup(args);
Dave Chinner24513372014-06-25 14:58:08 +1000484 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
Eric Sandeend99831f2014-06-22 15:03:54 +1000485 return retval;
Dave Chinner24513372014-06-25 14:58:08 +1000486 } else if (retval == -EEXIST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 if (args->flags & ATTR_CREATE)
Eric Sandeend99831f2014-06-22 15:03:54 +1000488 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 retval = xfs_attr_shortform_remove(args);
Darrick J. Wong4ec44e92018-04-17 19:10:15 -0700490 if (retval)
491 return retval;
492 /*
493 * Since we have removed the old attr, clear ATTR_REPLACE so
494 * that the leaf format add routine won't trip over the attr
495 * not being around.
496 */
497 args->flags &= ~ATTR_REPLACE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 }
499
Nathan Scottd8cc8902005-11-02 10:34:53 +1100500 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
501 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
Dave Chinner24513372014-06-25 14:58:08 +1000502 return -ENOSPC;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
505 newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100506
507 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
508 if (!forkoff)
Dave Chinner24513372014-06-25 14:58:08 +1000509 return -ENOSPC;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100510
511 xfs_attr_shortform_add(args, forkoff);
Eric Sandeend99831f2014-06-22 15:03:54 +1000512 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
514
515
516/*========================================================================
517 * External routines when attribute list is one block
518 *========================================================================*/
519
520/*
521 * Add a name to the leaf attribute list structure
522 *
523 * This leaf block cannot have a "remote" value, we only call this routine
524 * if bmap_one_block() says there is only one block (ie: no remote blks).
525 */
David Chinnera8272ce2007-11-23 16:28:09 +1100526STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527xfs_attr_leaf_addname(xfs_da_args_t *args)
528{
529 xfs_inode_t *dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000530 struct xfs_buf *bp;
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100531 int retval, error, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Dave Chinner5a5881c2012-03-22 05:15:13 +0000533 trace_xfs_attr_leaf_addname(args);
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 /*
536 * Read the (only) block in the attribute list in.
537 */
538 dp = args->dp;
539 args->blkno = 0;
Dave Chinner517c2222013-04-24 18:58:55 +1000540 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100542 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544 /*
545 * Look up the given attribute in the leaf block. Figure out if
546 * the given flags produce an error or call for an atomic rename.
547 */
Dave Chinner517c2222013-04-24 18:58:55 +1000548 retval = xfs_attr3_leaf_lookup_int(bp, args);
Dave Chinner24513372014-06-25 14:58:08 +1000549 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000550 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000551 return retval;
Dave Chinner24513372014-06-25 14:58:08 +1000552 } else if (retval == -EEXIST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 if (args->flags & ATTR_CREATE) { /* pure create op */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000554 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000555 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 }
Dave Chinner5a5881c2012-03-22 05:15:13 +0000557
558 trace_xfs_attr_leaf_replace(args);
559
Dave Chinner8275cdd2014-05-06 07:37:31 +1000560 /* save the attribute state for later removal*/
Barry Naujok6a178102008-05-21 16:42:05 +1000561 args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 args->blkno2 = args->blkno; /* set 2nd entry info*/
563 args->index2 = args->index;
564 args->rmtblkno2 = args->rmtblkno;
565 args->rmtblkcnt2 = args->rmtblkcnt;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000566 args->rmtvaluelen2 = args->rmtvaluelen;
567
568 /*
569 * clear the remote attr state now that it is saved so that the
570 * values reflect the state of the attribute we are about to
571 * add, not the attribute we just found and will remove later.
572 */
573 args->rmtblkno = 0;
574 args->rmtblkcnt = 0;
575 args->rmtvaluelen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 }
577
578 /*
579 * Add the attribute to the leaf block, transitioning to a Btree
580 * if required.
581 */
Dave Chinner517c2222013-04-24 18:58:55 +1000582 retval = xfs_attr3_leaf_add(bp, args);
Dave Chinner24513372014-06-25 14:58:08 +1000583 if (retval == -ENOSPC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 /*
585 * Promote the attribute list to the Btree format, then
586 * Commit that transaction so that the node_addname() call
587 * can manage its own transactions.
588 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000589 xfs_defer_init(args->dfops, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000590 error = xfs_attr3_leaf_to_node(args);
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100591 if (!error)
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000592 error = xfs_defer_finish(&args->trans, args->dfops, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 args->trans = NULL;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000595 xfs_defer_cancel(args->dfops);
Eric Sandeend99831f2014-06-22 15:03:54 +1000596 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 }
598
599 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 * Commit the current trans (including the inode) and start
601 * a new one.
602 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000603 error = xfs_trans_roll(&args->trans, dp);
604 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000605 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607 /*
608 * Fob the whole rest of the problem off on the Btree code.
609 */
610 error = xfs_attr_node_addname(args);
Eric Sandeend99831f2014-06-22 15:03:54 +1000611 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 }
613
614 /*
615 * Commit the transaction that added the attr name so that
616 * later routines can manage their own transactions.
617 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000618 error = xfs_trans_roll(&args->trans, dp);
619 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000620 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 /*
623 * If there was an out-of-line value, allocate the blocks we
624 * identified for its storage and copy the value. This is done
625 * after we create the attribute so that we don't overflow the
626 * maximum size of a transaction and/or hit a deadlock.
627 */
628 if (args->rmtblkno > 0) {
629 error = xfs_attr_rmtval_set(args);
630 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000631 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 }
633
634 /*
635 * If this is an atomic rename operation, we must "flip" the
636 * incomplete flags on the "new" and "old" attribute/value pairs
637 * so that one disappears and one appears atomically. Then we
638 * must remove the "old" attribute/value pair.
639 */
Barry Naujok6a178102008-05-21 16:42:05 +1000640 if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 /*
642 * In a separate transaction, set the incomplete flag on the
643 * "old" attr and clear the incomplete flag on the "new" attr.
644 */
Dave Chinner517c2222013-04-24 18:58:55 +1000645 error = xfs_attr3_leaf_flipflags(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000647 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
649 /*
650 * Dismantle the "old" attribute/value pair by removing
651 * a "remote" value (if it exists).
652 */
653 args->index = args->index2;
654 args->blkno = args->blkno2;
655 args->rmtblkno = args->rmtblkno2;
656 args->rmtblkcnt = args->rmtblkcnt2;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000657 args->rmtvaluelen = args->rmtvaluelen2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 if (args->rmtblkno) {
659 error = xfs_attr_rmtval_remove(args);
660 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000661 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 }
663
664 /*
665 * Read in the block containing the "old" attr, then
666 * remove the "old" attr from that block (neat, huh!)
667 */
Dave Chinner517c2222013-04-24 18:58:55 +1000668 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
Dave Chinnerad14c332012-11-12 22:54:16 +1100669 -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100671 return error;
672
Dave Chinner517c2222013-04-24 18:58:55 +1000673 xfs_attr3_leaf_remove(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
675 /*
676 * If the result is small enough, shrink it all into the inode.
677 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100678 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000679 xfs_defer_init(args->dfops, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000680 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 /* bp is gone due to xfs_da_shrink_inode */
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100682 if (!error)
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000683 error = xfs_defer_finish(&args->trans,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000684 args->dfops, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 args->trans = NULL;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000687 xfs_defer_cancel(args->dfops);
Eric Sandeend99831f2014-06-22 15:03:54 +1000688 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 }
Dave Chinner1d9025e2012-06-22 18:50:14 +1000690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 /*
693 * Commit the remove and start the next trans in series.
694 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000695 error = xfs_trans_roll(&args->trans, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 } else if (args->rmtblkno > 0) {
698 /*
699 * Added a "remote" value, just clear the incomplete flag.
700 */
Dave Chinner517c2222013-04-24 18:58:55 +1000701 error = xfs_attr3_leaf_clearflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 }
Dave Chinner517c2222013-04-24 18:58:55 +1000703 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704}
705
706/*
707 * Remove a name from the leaf attribute list structure
708 *
709 * This leaf block cannot have a "remote" value, we only call this routine
710 * if bmap_one_block() says there is only one block (ie: no remote blks).
711 */
712STATIC int
713xfs_attr_leaf_removename(xfs_da_args_t *args)
714{
715 xfs_inode_t *dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000716 struct xfs_buf *bp;
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100717 int error, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Dave Chinner5a5881c2012-03-22 05:15:13 +0000719 trace_xfs_attr_leaf_removename(args);
720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 /*
722 * Remove the attribute.
723 */
724 dp = args->dp;
725 args->blkno = 0;
Dave Chinner517c2222013-04-24 18:58:55 +1000726 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Dave Chinnerad14c332012-11-12 22:54:16 +1100727 if (error)
728 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Dave Chinner517c2222013-04-24 18:58:55 +1000730 error = xfs_attr3_leaf_lookup_int(bp, args);
Dave Chinner24513372014-06-25 14:58:08 +1000731 if (error == -ENOATTR) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000732 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000733 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
735
Dave Chinner517c2222013-04-24 18:58:55 +1000736 xfs_attr3_leaf_remove(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
738 /*
739 * If the result is small enough, shrink it all into the inode.
740 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100741 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000742 xfs_defer_init(args->dfops, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000743 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 /* bp is gone due to xfs_da_shrink_inode */
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100745 if (!error)
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000746 error = xfs_defer_finish(&args->trans, args->dfops, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 args->trans = NULL;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000749 xfs_defer_cancel(args->dfops);
Dave Chinner517c2222013-04-24 18:58:55 +1000750 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 }
Dave Chinner1d9025e2012-06-22 18:50:14 +1000752 }
Dave Chinner517c2222013-04-24 18:58:55 +1000753 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
755
756/*
757 * Look up a name in a leaf attribute list structure.
758 *
759 * This leaf block cannot have a "remote" value, we only call this routine
760 * if bmap_one_block() says there is only one block (ie: no remote blks).
761 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000762STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763xfs_attr_leaf_get(xfs_da_args_t *args)
764{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000765 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 int error;
767
Dave Chinneree732592012-11-12 22:53:53 +1100768 trace_xfs_attr_leaf_get(args);
769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 args->blkno = 0;
Dave Chinner517c2222013-04-24 18:58:55 +1000771 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100773 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Dave Chinner517c2222013-04-24 18:58:55 +1000775 error = xfs_attr3_leaf_lookup_int(bp, args);
Dave Chinner24513372014-06-25 14:58:08 +1000776 if (error != -EEXIST) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000777 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000778 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 }
Dave Chinner517c2222013-04-24 18:58:55 +1000780 error = xfs_attr3_leaf_getvalue(bp, args);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000781 xfs_trans_brelse(args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
783 error = xfs_attr_rmtval_get(args);
784 }
Dave Chinner517c2222013-04-24 18:58:55 +1000785 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786}
787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788/*========================================================================
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000789 * External routines when attribute list size > geo->blksize
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 *========================================================================*/
791
792/*
793 * Add a name to a Btree-format attribute list.
794 *
795 * This will involve walking down the Btree, and may involve splitting
796 * leaf nodes and even splitting intermediate nodes up to and including
797 * the root node (a special case of an intermediate node).
798 *
799 * "Remote" attribute values confuse the issue and atomic rename operations
800 * add a whole extra layer of confusion on top of that.
801 */
802STATIC int
803xfs_attr_node_addname(xfs_da_args_t *args)
804{
805 xfs_da_state_t *state;
806 xfs_da_state_blk_t *blk;
807 xfs_inode_t *dp;
808 xfs_mount_t *mp;
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100809 int retval, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Dave Chinner5a5881c2012-03-22 05:15:13 +0000811 trace_xfs_attr_node_addname(args);
812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 /*
814 * Fill in bucket of arguments/results/context to carry around.
815 */
816 dp = args->dp;
817 mp = dp->i_mount;
818restart:
819 state = xfs_da_state_alloc();
820 state->args = args;
821 state->mp = mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 /*
824 * Search to see if name already exists, and get back a pointer
825 * to where it should go.
826 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000827 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 if (error)
829 goto out;
830 blk = &state->path.blk[ state->path.active-1 ];
831 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner24513372014-06-25 14:58:08 +1000832 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 goto out;
Dave Chinner24513372014-06-25 14:58:08 +1000834 } else if (retval == -EEXIST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 if (args->flags & ATTR_CREATE)
836 goto out;
Dave Chinner5a5881c2012-03-22 05:15:13 +0000837
838 trace_xfs_attr_node_replace(args);
839
Dave Chinner8275cdd2014-05-06 07:37:31 +1000840 /* save the attribute state for later removal*/
Barry Naujok6a178102008-05-21 16:42:05 +1000841 args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 args->blkno2 = args->blkno; /* set 2nd entry info*/
843 args->index2 = args->index;
844 args->rmtblkno2 = args->rmtblkno;
845 args->rmtblkcnt2 = args->rmtblkcnt;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000846 args->rmtvaluelen2 = args->rmtvaluelen;
847
848 /*
849 * clear the remote attr state now that it is saved so that the
850 * values reflect the state of the attribute we are about to
851 * add, not the attribute we just found and will remove later.
852 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 args->rmtblkno = 0;
854 args->rmtblkcnt = 0;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000855 args->rmtvaluelen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 }
857
Dave Chinner517c2222013-04-24 18:58:55 +1000858 retval = xfs_attr3_leaf_add(blk->bp, state->args);
Dave Chinner24513372014-06-25 14:58:08 +1000859 if (retval == -ENOSPC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 if (state->path.active == 1) {
861 /*
862 * Its really a single leaf node, but it had
863 * out-of-line values so it looked like it *might*
864 * have been a b-tree.
865 */
866 xfs_da_state_free(state);
Eric Sandeen6dd93e92013-07-31 20:18:54 -0500867 state = NULL;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000868 xfs_defer_init(args->dfops, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000869 error = xfs_attr3_leaf_to_node(args);
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100870 if (!error)
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000871 error = xfs_defer_finish(&args->trans,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000872 args->dfops, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 args->trans = NULL;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000875 xfs_defer_cancel(args->dfops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 goto out;
877 }
878
879 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 * Commit the node conversion and start the next
881 * trans in the chain.
882 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000883 error = xfs_trans_roll(&args->trans, dp);
884 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 goto out;
886
887 goto restart;
888 }
889
890 /*
891 * Split as many Btree elements as required.
892 * This code tracks the new and old attr's location
893 * in the index/blkno/rmtblkno/rmtblkcnt fields and
894 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
895 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000896 xfs_defer_init(args->dfops, args->firstblock);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000897 error = xfs_da3_split(state);
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100898 if (!error)
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000899 error = xfs_defer_finish(&args->trans, args->dfops, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 args->trans = NULL;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000902 xfs_defer_cancel(args->dfops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 goto out;
904 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 } else {
906 /*
907 * Addition succeeded, update Btree hashvals.
908 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000909 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 }
911
912 /*
913 * Kill the state structure, we're done with it and need to
914 * allow the buffers to come back later.
915 */
916 xfs_da_state_free(state);
917 state = NULL;
918
919 /*
920 * Commit the leaf addition or btree split and start the next
921 * trans in the chain.
922 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000923 error = xfs_trans_roll(&args->trans, dp);
924 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 goto out;
926
927 /*
928 * If there was an out-of-line value, allocate the blocks we
929 * identified for its storage and copy the value. This is done
930 * after we create the attribute so that we don't overflow the
931 * maximum size of a transaction and/or hit a deadlock.
932 */
933 if (args->rmtblkno > 0) {
934 error = xfs_attr_rmtval_set(args);
935 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000936 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 }
938
939 /*
940 * If this is an atomic rename operation, we must "flip" the
941 * incomplete flags on the "new" and "old" attribute/value pairs
942 * so that one disappears and one appears atomically. Then we
943 * must remove the "old" attribute/value pair.
944 */
Barry Naujok6a178102008-05-21 16:42:05 +1000945 if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 /*
947 * In a separate transaction, set the incomplete flag on the
948 * "old" attr and clear the incomplete flag on the "new" attr.
949 */
Dave Chinner517c2222013-04-24 18:58:55 +1000950 error = xfs_attr3_leaf_flipflags(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 if (error)
952 goto out;
953
954 /*
955 * Dismantle the "old" attribute/value pair by removing
956 * a "remote" value (if it exists).
957 */
958 args->index = args->index2;
959 args->blkno = args->blkno2;
960 args->rmtblkno = args->rmtblkno2;
961 args->rmtblkcnt = args->rmtblkcnt2;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000962 args->rmtvaluelen = args->rmtvaluelen2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 if (args->rmtblkno) {
964 error = xfs_attr_rmtval_remove(args);
965 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000966 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 }
968
969 /*
970 * Re-find the "old" attribute entry after any split ops.
971 * The INCOMPLETE flag means that we will find the "old"
972 * attr, not the "new" one.
973 */
974 args->flags |= XFS_ATTR_INCOMPLETE;
975 state = xfs_da_state_alloc();
976 state->args = args;
977 state->mp = mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 state->inleaf = 0;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000979 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 if (error)
981 goto out;
982
983 /*
984 * Remove the name and update the hashvals in the tree.
985 */
986 blk = &state->path.blk[ state->path.active-1 ];
987 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner517c2222013-04-24 18:58:55 +1000988 error = xfs_attr3_leaf_remove(blk->bp, args);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000989 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991 /*
992 * Check to see if the tree needs to be collapsed.
993 */
994 if (retval && (state->path.active > 1)) {
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000995 xfs_defer_init(args->dfops, args->firstblock);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000996 error = xfs_da3_join(state);
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100997 if (!error)
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000998 error = xfs_defer_finish(&args->trans,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000999 args->dfops, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 args->trans = NULL;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001002 xfs_defer_cancel(args->dfops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 goto out;
1004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 }
1006
1007 /*
1008 * Commit and start the next trans in the chain.
1009 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10001010 error = xfs_trans_roll(&args->trans, dp);
1011 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 goto out;
1013
1014 } else if (args->rmtblkno > 0) {
1015 /*
1016 * Added a "remote" value, just clear the incomplete flag.
1017 */
Dave Chinner517c2222013-04-24 18:58:55 +10001018 error = xfs_attr3_leaf_clearflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 if (error)
1020 goto out;
1021 }
1022 retval = error = 0;
1023
1024out:
1025 if (state)
1026 xfs_da_state_free(state);
1027 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001028 return error;
1029 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030}
1031
1032/*
1033 * Remove a name from a B-tree attribute list.
1034 *
1035 * This will involve walking down the Btree, and may involve joining
1036 * leaf nodes and even joining intermediate nodes up to and including
1037 * the root node (a special case of an intermediate node).
1038 */
1039STATIC int
1040xfs_attr_node_removename(xfs_da_args_t *args)
1041{
1042 xfs_da_state_t *state;
1043 xfs_da_state_blk_t *blk;
1044 xfs_inode_t *dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001045 struct xfs_buf *bp;
Eric Sandeenf6106ef2016-01-11 11:34:01 +11001046 int retval, error, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Dave Chinner5a5881c2012-03-22 05:15:13 +00001048 trace_xfs_attr_node_removename(args);
1049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 /*
1051 * Tie a string around our finger to remind us where we are.
1052 */
1053 dp = args->dp;
1054 state = xfs_da_state_alloc();
1055 state->args = args;
1056 state->mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
1058 /*
1059 * Search to see if name exists, and get back a pointer to it.
1060 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001061 error = xfs_da3_node_lookup_int(state, &retval);
Dave Chinner24513372014-06-25 14:58:08 +10001062 if (error || (retval != -EEXIST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 if (error == 0)
1064 error = retval;
1065 goto out;
1066 }
1067
1068 /*
1069 * If there is an out-of-line value, de-allocate the blocks.
1070 * This is done before we remove the attribute so that we don't
1071 * overflow the maximum size of a transaction and/or hit a deadlock.
1072 */
1073 blk = &state->path.blk[ state->path.active-1 ];
1074 ASSERT(blk->bp != NULL);
1075 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1076 if (args->rmtblkno > 0) {
1077 /*
1078 * Fill in disk block numbers in the state structure
1079 * so that we can get the buffers back after we commit
1080 * several transactions in the following calls.
1081 */
1082 error = xfs_attr_fillstate(state);
1083 if (error)
1084 goto out;
1085
1086 /*
1087 * Mark the attribute as INCOMPLETE, then bunmapi() the
1088 * remote value.
1089 */
Dave Chinner517c2222013-04-24 18:58:55 +10001090 error = xfs_attr3_leaf_setflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 if (error)
1092 goto out;
1093 error = xfs_attr_rmtval_remove(args);
1094 if (error)
1095 goto out;
1096
1097 /*
1098 * Refill the state structure with buffers, the prior calls
1099 * released our buffers.
1100 */
1101 error = xfs_attr_refillstate(state);
1102 if (error)
1103 goto out;
1104 }
1105
1106 /*
1107 * Remove the name and update the hashvals in the tree.
1108 */
1109 blk = &state->path.blk[ state->path.active-1 ];
1110 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner517c2222013-04-24 18:58:55 +10001111 retval = xfs_attr3_leaf_remove(blk->bp, args);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001112 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
1114 /*
1115 * Check to see if the tree needs to be collapsed.
1116 */
1117 if (retval && (state->path.active > 1)) {
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001118 xfs_defer_init(args->dfops, args->firstblock);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001119 error = xfs_da3_join(state);
Eric Sandeenf6106ef2016-01-11 11:34:01 +11001120 if (!error)
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001121 error = xfs_defer_finish(&args->trans, args->dfops, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 args->trans = NULL;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001124 xfs_defer_cancel(args->dfops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 goto out;
1126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 /*
1128 * Commit the Btree join operation and start a new trans.
1129 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10001130 error = xfs_trans_roll(&args->trans, dp);
1131 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 goto out;
1133 }
1134
1135 /*
1136 * If the result is small enough, push it all into the inode.
1137 */
1138 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
1139 /*
1140 * Have to get rid of the copy of this dabuf in the state.
1141 */
1142 ASSERT(state->path.active == 1);
1143 ASSERT(state->path.blk[0].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 state->path.blk[0].bp = NULL;
1145
Dave Chinner517c2222013-04-24 18:58:55 +10001146 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 if (error)
1148 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
Nathan Scottd8cc8902005-11-02 10:34:53 +11001150 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001151 xfs_defer_init(args->dfops, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +10001152 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 /* bp is gone due to xfs_da_shrink_inode */
Eric Sandeenf6106ef2016-01-11 11:34:01 +11001154 if (!error)
Darrick J. Wong310a75a2016-08-03 11:18:10 +10001155 error = xfs_defer_finish(&args->trans,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001156 args->dfops, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 args->trans = NULL;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001159 xfs_defer_cancel(args->dfops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 goto out;
1161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 } else
Dave Chinner1d9025e2012-06-22 18:50:14 +10001163 xfs_trans_brelse(args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 }
1165 error = 0;
1166
1167out:
1168 xfs_da_state_free(state);
Eric Sandeend99831f2014-06-22 15:03:54 +10001169 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170}
1171
1172/*
1173 * Fill in the disk block numbers in the state structure for the buffers
1174 * that are attached to the state structure.
1175 * This is done so that we can quickly reattach ourselves to those buffers
Nathan Scottc41564b2006-03-29 08:55:14 +10001176 * after some set of transaction commits have released these buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 */
1178STATIC int
1179xfs_attr_fillstate(xfs_da_state_t *state)
1180{
1181 xfs_da_state_path_t *path;
1182 xfs_da_state_blk_t *blk;
1183 int level;
1184
Dave Chinneree732592012-11-12 22:53:53 +11001185 trace_xfs_attr_fillstate(state->args);
1186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 /*
1188 * Roll down the "path" in the state structure, storing the on-disk
1189 * block number for those buffers in the "path".
1190 */
1191 path = &state->path;
1192 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1193 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1194 if (blk->bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001195 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 blk->bp = NULL;
1197 } else {
1198 blk->disk_blkno = 0;
1199 }
1200 }
1201
1202 /*
1203 * Roll down the "altpath" in the state structure, storing the on-disk
1204 * block number for those buffers in the "altpath".
1205 */
1206 path = &state->altpath;
1207 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1208 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1209 if (blk->bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001210 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 blk->bp = NULL;
1212 } else {
1213 blk->disk_blkno = 0;
1214 }
1215 }
1216
Eric Sandeend99831f2014-06-22 15:03:54 +10001217 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218}
1219
1220/*
1221 * Reattach the buffers to the state structure based on the disk block
1222 * numbers stored in the state structure.
Nathan Scottc41564b2006-03-29 08:55:14 +10001223 * This is done after some set of transaction commits have released those
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 * buffers from our grip.
1225 */
1226STATIC int
1227xfs_attr_refillstate(xfs_da_state_t *state)
1228{
1229 xfs_da_state_path_t *path;
1230 xfs_da_state_blk_t *blk;
1231 int level, error;
1232
Dave Chinneree732592012-11-12 22:53:53 +11001233 trace_xfs_attr_refillstate(state->args);
1234
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 /*
1236 * Roll down the "path" in the state structure, storing the on-disk
1237 * block number for those buffers in the "path".
1238 */
1239 path = &state->path;
1240 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1241 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1242 if (blk->disk_blkno) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001243 error = xfs_da3_node_read(state->args->trans,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 state->args->dp,
1245 blk->blkno, blk->disk_blkno,
Dave Chinnerd9392a42012-11-12 22:54:17 +11001246 &blk->bp, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001248 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 } else {
1250 blk->bp = NULL;
1251 }
1252 }
1253
1254 /*
1255 * Roll down the "altpath" in the state structure, storing the on-disk
1256 * block number for those buffers in the "altpath".
1257 */
1258 path = &state->altpath;
1259 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1260 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1261 if (blk->disk_blkno) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001262 error = xfs_da3_node_read(state->args->trans,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 state->args->dp,
1264 blk->blkno, blk->disk_blkno,
Dave Chinnerd9392a42012-11-12 22:54:17 +11001265 &blk->bp, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001267 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 } else {
1269 blk->bp = NULL;
1270 }
1271 }
1272
Eric Sandeend99831f2014-06-22 15:03:54 +10001273 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274}
1275
1276/*
1277 * Look up a filename in a node attribute list.
1278 *
1279 * This routine gets called for any attribute fork that has more than one
1280 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1281 * "remote" values taking up more blocks.
1282 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001283STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284xfs_attr_node_get(xfs_da_args_t *args)
1285{
1286 xfs_da_state_t *state;
1287 xfs_da_state_blk_t *blk;
1288 int error, retval;
1289 int i;
1290
Dave Chinneree732592012-11-12 22:53:53 +11001291 trace_xfs_attr_node_get(args);
1292
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 state = xfs_da_state_alloc();
1294 state->args = args;
1295 state->mp = args->dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297 /*
1298 * Search to see if name exists, and get back a pointer to it.
1299 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001300 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 if (error) {
1302 retval = error;
Dave Chinner24513372014-06-25 14:58:08 +10001303 } else if (retval == -EEXIST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 blk = &state->path.blk[ state->path.active-1 ];
1305 ASSERT(blk->bp != NULL);
1306 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1307
1308 /*
1309 * Get the value, local or "remote"
1310 */
Dave Chinner517c2222013-04-24 18:58:55 +10001311 retval = xfs_attr3_leaf_getvalue(blk->bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 if (!retval && (args->rmtblkno > 0)
1313 && !(args->flags & ATTR_KERNOVAL)) {
1314 retval = xfs_attr_rmtval_get(args);
1315 }
1316 }
1317
1318 /*
1319 * If not in a transaction, we have to release all the buffers.
1320 */
1321 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001322 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 state->path.blk[i].bp = NULL;
1324 }
1325
1326 xfs_da_state_free(state);
Eric Sandeend99831f2014-06-22 15:03:54 +10001327 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328}