blob: ef8a1c75a467ac9475c3625103ccdcdbbaf81912 [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"
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
Darrick J. Wongad017f62017-06-16 11:00:14 -0700117/* Retrieve an extended attribute and its value. Must have iolock. */
118int
119xfs_attr_get_ilocked(
120 struct xfs_inode *ip,
121 struct xfs_da_args *args)
122{
123 if (!xfs_inode_hasattr(ip))
124 return -ENOATTR;
125 else if (ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL)
126 return xfs_attr_shortform_getvalue(args);
127 else if (xfs_bmap_one_block(ip, XFS_ATTR_FORK))
128 return xfs_attr_leaf_get(args);
129 else
130 return xfs_attr_node_get(args);
131}
132
133/* Retrieve an extended attribute by name, and its value. */
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000134int
135xfs_attr_get(
Christoph Hellwige82fa0c2009-11-14 16:17:20 +0000136 struct xfs_inode *ip,
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000137 const unsigned char *name,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100138 unsigned char *value,
Christoph Hellwige82fa0c2009-11-14 16:17:20 +0000139 int *valuelenp,
140 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000142 struct xfs_da_args args;
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000143 uint lock_mode;
144 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100146 XFS_STATS_INC(ip->i_mount, xs_attr_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
Dave Chinner24513372014-06-25 14:58:08 +1000149 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000151 error = xfs_attr_args_init(&args, ip, name, flags);
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000152 if (error)
153 return error;
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 args.value = value;
156 args.valuelen = *valuelenp;
Eric Sandeenc400ee32015-08-19 10:30:48 +1000157 /* Entirely possible to look up a name which doesn't exist */
158 args.op_flags = XFS_DA_OP_OKNOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Christoph Hellwig683cb942013-12-06 12:30:15 -0800160 lock_mode = xfs_ilock_attr_map_shared(ip);
Darrick J. Wongad017f62017-06-16 11:00:14 -0700161 error = xfs_attr_get_ilocked(ip, &args);
Christoph Hellwig683cb942013-12-06 12:30:15 -0800162 xfs_iunlock(ip, lock_mode);
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000163
164 *valuelenp = args.valuelen;
Dave Chinner24513372014-06-25 14:58:08 +1000165 return error == -EEXIST ? 0 : error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
167
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000168/*
169 * Calculate how many blocks we need for the new attribute,
170 */
Eric Sandeen5d77c0d2009-11-19 15:52:00 +0000171STATIC int
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000172xfs_attr_calc_size(
Christoph Hellwig6c888af2014-05-13 16:40:19 +1000173 struct xfs_da_args *args,
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000174 int *local)
175{
Christoph Hellwig6c888af2014-05-13 16:40:19 +1000176 struct xfs_mount *mp = args->dp->i_mount;
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000177 int size;
178 int nblks;
179
180 /*
181 * Determine space new attribute will use, and if it would be
182 * "local" or "remote" (note: local != inline).
183 */
Dave Chinnerc59f0ad2014-06-06 15:21:27 +1000184 size = xfs_attr_leaf_newentsize(args, local);
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000185 nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
186 if (*local) {
Dave Chinner33a60392014-06-06 15:21:10 +1000187 if (size > (args->geo->blksize / 2)) {
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000188 /* Double split possible */
189 nblks *= 2;
190 }
191 } else {
192 /*
193 * Out of line attribute, cannot double split, but
194 * make room for the attribute value itself.
195 */
Dave Chinner2d6dcc62014-05-15 09:39:28 +1000196 uint dblocks = xfs_attr3_rmt_blocks(mp, args->valuelen);
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000197 nblks += dblocks;
198 nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
199 }
200
201 return nblks;
202}
203
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000204int
205xfs_attr_set(
206 struct xfs_inode *dp,
207 const unsigned char *name,
208 unsigned char *value,
209 int valuelen,
210 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
Jie Liu3d3c8b52013-08-12 20:49:59 +1000212 struct xfs_mount *mp = dp->i_mount;
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000213 struct xfs_da_args args;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000214 struct xfs_defer_ops dfops;
Jie Liu3d3c8b52013-08-12 20:49:59 +1000215 struct xfs_trans_res tres;
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000216 xfs_fsblock_t firstblock;
Jie Liu3d3c8b52013-08-12 20:49:59 +1000217 int rsvd = (flags & ATTR_ROOT) != 0;
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100218 int error, err2, local;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100220 XFS_STATS_INC(mp, xs_attr_set);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000221
222 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
Dave Chinner24513372014-06-25 14:58:08 +1000223 return -EIO;
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000224
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000225 error = xfs_attr_args_init(&args, dp, name, flags);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000226 if (error)
227 return error;
228
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000229 args.value = value;
230 args.valuelen = valuelen;
231 args.firstblock = &firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000232 args.dfops = &dfops;
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000233 args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
Christoph Hellwig6c888af2014-05-13 16:40:19 +1000234 args.total = xfs_attr_calc_size(&args, &local);
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000235
Christoph Hellwig7d095252009-06-08 15:33:32 +0200236 error = xfs_qm_dqattach(dp, 0);
237 if (error)
238 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 /*
241 * If the inode doesn't have an attribute fork, add one.
242 * (inode must not be locked when we call this routine)
243 */
244 if (XFS_IFORK_Q(dp) == 0) {
Barry Naujoke5889e92007-02-10 18:35:58 +1100245 int sf_size = sizeof(xfs_attr_sf_hdr_t) +
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000246 XFS_ATTR_SF_ENTSIZE_BYNAME(args.namelen, valuelen);
Barry Naujoke5889e92007-02-10 18:35:58 +1100247
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000248 error = xfs_bmap_add_attrfork(dp, sf_size, rsvd);
249 if (error)
250 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
252
Christoph Hellwig253f4912016-04-06 09:19:55 +1000253 tres.tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
254 M_RES(mp)->tr_attrsetrt.tr_logres * args.total;
255 tres.tr_logcount = XFS_ATTRSET_LOG_COUNT;
256 tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 /*
259 * Root fork attributes can use reserved data blocks for this
260 * operation if necessary
261 */
Christoph Hellwig253f4912016-04-06 09:19:55 +1000262 error = xfs_trans_alloc(mp, &tres, args.total, 0,
263 rsvd ? XFS_TRANS_RESERVE : 0, &args.trans);
264 if (error)
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000265 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Christoph Hellwig253f4912016-04-06 09:19:55 +1000267 xfs_ilock(dp, XFS_ILOCK_EXCL);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200268 error = xfs_trans_reserve_quota_nblks(args.trans, dp, args.total, 0,
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000269 rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
270 XFS_QMOPT_RES_REGBLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 if (error) {
272 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Christoph Hellwig4906e212015-06-04 13:47:56 +1000273 xfs_trans_cancel(args.trans);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000274 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 }
276
Christoph Hellwigddc34152011-09-19 15:00:54 +0000277 xfs_trans_ijoin(args.trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000280 * If the attribute list is non-existent or a shortform list,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 * upgrade it to a single-leaf-block attribute list.
282 */
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000283 if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL ||
284 (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
285 dp->i_d.di_anextents == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 /*
288 * Build initial attribute list (if required).
289 */
290 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
Nathan Scottd8cc8902005-11-02 10:34:53 +1100291 xfs_attr_shortform_create(&args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293 /*
294 * Try to add the attr to the attribute list in
295 * the inode.
296 */
297 error = xfs_attr_shortform_addname(&args);
Dave Chinner24513372014-06-25 14:58:08 +1000298 if (error != -ENOSPC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 /*
300 * Commit the shortform mods, and we're done.
301 * NOTE: this is also the error path (EEXIST, etc).
302 */
303 ASSERT(args.trans != NULL);
304
305 /*
306 * If this is a synchronous mount, make sure that
307 * the transaction goes to disk before returning
308 * to the user.
309 */
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000310 if (mp->m_flags & XFS_MOUNT_WSYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 xfs_trans_set_sync(args.trans);
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000312
313 if (!error && (flags & ATTR_KERNOTIME) == 0) {
314 xfs_trans_ichgtime(args.trans, dp,
315 XFS_ICHGTIME_CHG);
316 }
Christoph Hellwig70393312015-06-04 13:48:08 +1000317 err2 = xfs_trans_commit(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 xfs_iunlock(dp, XFS_ILOCK_EXCL);
319
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000320 return error ? error : err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 }
322
323 /*
324 * It won't fit in the shortform, transform to a leaf block.
325 * GROT: another possible req'mt for a double-split btree op.
326 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000327 xfs_defer_init(args.dfops, args.firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 error = xfs_attr_shortform_to_leaf(&args);
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100329 if (!error)
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000330 error = xfs_defer_finish(&args.trans, args.dfops, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 args.trans = NULL;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000333 xfs_defer_cancel(&dfops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 goto out;
335 }
336
337 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 * Commit the leaf transformation. We'll need another (linked)
339 * transaction to add the new attribute to the leaf.
340 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000341
342 error = xfs_trans_roll(&args.trans, dp);
343 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 goto out;
345
346 }
347
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000348 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 error = xfs_attr_leaf_addname(&args);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000350 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 error = xfs_attr_node_addname(&args);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000352 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 /*
356 * If this is a synchronous mount, make sure that the
357 * transaction goes to disk before returning to the user.
358 */
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000359 if (mp->m_flags & XFS_MOUNT_WSYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 xfs_trans_set_sync(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000362 if ((flags & ATTR_KERNOTIME) == 0)
363 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 /*
366 * Commit the last in the sequence of transactions.
367 */
368 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
Christoph Hellwig70393312015-06-04 13:48:08 +1000369 error = xfs_trans_commit(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 xfs_iunlock(dp, XFS_ILOCK_EXCL);
371
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000372 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374out:
Christoph Hellwig4906e212015-06-04 13:47:56 +1000375 if (args.trans)
376 xfs_trans_cancel(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000378 return error;
Nathan Scottaa82daa2005-11-02 10:33:33 +1100379}
380
381/*
382 * Generic handler routine to remove a name from an attribute list.
383 * Transitions attribute list from Btree to shortform as necessary.
384 */
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000385int
386xfs_attr_remove(
387 struct xfs_inode *dp,
388 const unsigned char *name,
389 int flags)
Nathan Scottaa82daa2005-11-02 10:33:33 +1100390{
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000391 struct xfs_mount *mp = dp->i_mount;
392 struct xfs_da_args args;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000393 struct xfs_defer_ops dfops;
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000394 xfs_fsblock_t firstblock;
395 int error;
Nathan Scottaa82daa2005-11-02 10:33:33 +1100396
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100397 XFS_STATS_INC(mp, xs_attr_remove);
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000398
399 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
Dave Chinner24513372014-06-25 14:58:08 +1000400 return -EIO;
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000401
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000402 error = xfs_attr_args_init(&args, dp, name, flags);
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000403 if (error)
404 return error;
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 args.firstblock = &firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000407 args.dfops = &dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 /*
Dave Chinner4a338212011-06-23 01:35:01 +0000410 * we have no control over the attribute names that userspace passes us
411 * to remove, so we have to allow the name lookup prior to attribute
412 * removal to fail.
413 */
414 args.op_flags = XFS_DA_OP_OKNOENT;
415
Christoph Hellwig7d095252009-06-08 15:33:32 +0200416 error = xfs_qm_dqattach(dp, 0);
417 if (error)
418 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 * Root fork attributes can use reserved data blocks for this
422 * operation if necessary
423 */
Christoph Hellwig253f4912016-04-06 09:19:55 +1000424 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_attrrm,
425 XFS_ATTRRM_SPACE_RES(mp), 0,
426 (flags & ATTR_ROOT) ? XFS_TRANS_RESERVE : 0,
427 &args.trans);
428 if (error)
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000429 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 xfs_ilock(dp, XFS_ILOCK_EXCL);
432 /*
433 * No need to make quota reservations here. We expect to release some
434 * blocks not allocate in the common case.
435 */
Christoph Hellwigddc34152011-09-19 15:00:54 +0000436 xfs_trans_ijoin(args.trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +1000438 if (!xfs_inode_hasattr(dp)) {
Dave Chinner24513372014-06-25 14:58:08 +1000439 error = -ENOATTR;
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000440 } else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
442 error = xfs_attr_shortform_remove(&args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
444 error = xfs_attr_leaf_removename(&args);
445 } else {
446 error = xfs_attr_node_removename(&args);
447 }
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000448
449 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 /*
453 * If this is a synchronous mount, make sure that the
454 * transaction goes to disk before returning to the user.
455 */
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000456 if (mp->m_flags & XFS_MOUNT_WSYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 xfs_trans_set_sync(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000459 if ((flags & ATTR_KERNOTIME) == 0)
460 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 /*
463 * Commit the last in the sequence of transactions.
464 */
465 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
Christoph Hellwig70393312015-06-04 13:48:08 +1000466 error = xfs_trans_commit(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 xfs_iunlock(dp, XFS_ILOCK_EXCL);
468
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000469 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471out:
Christoph Hellwig4906e212015-06-04 13:47:56 +1000472 if (args.trans)
473 xfs_trans_cancel(args.trans);
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000474 xfs_iunlock(dp, XFS_ILOCK_EXCL);
475 return error;
Nathan Scottaa82daa2005-11-02 10:33:33 +1100476}
477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478/*========================================================================
479 * External routines when attribute list is inside the inode
480 *========================================================================*/
481
482/*
483 * Add a name to the shortform attribute list structure
484 * This is the external routine.
485 */
486STATIC int
487xfs_attr_shortform_addname(xfs_da_args_t *args)
488{
Nathan Scottd8cc8902005-11-02 10:34:53 +1100489 int newsize, forkoff, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Dave Chinner5a5881c2012-03-22 05:15:13 +0000491 trace_xfs_attr_sf_addname(args);
492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 retval = xfs_attr_shortform_lookup(args);
Dave Chinner24513372014-06-25 14:58:08 +1000494 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
Eric Sandeend99831f2014-06-22 15:03:54 +1000495 return retval;
Dave Chinner24513372014-06-25 14:58:08 +1000496 } else if (retval == -EEXIST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 if (args->flags & ATTR_CREATE)
Eric Sandeend99831f2014-06-22 15:03:54 +1000498 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 retval = xfs_attr_shortform_remove(args);
500 ASSERT(retval == 0);
501 }
502
Nathan Scottd8cc8902005-11-02 10:34:53 +1100503 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
504 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
Dave Chinner24513372014-06-25 14:58:08 +1000505 return -ENOSPC;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
508 newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100509
510 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
511 if (!forkoff)
Dave Chinner24513372014-06-25 14:58:08 +1000512 return -ENOSPC;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100513
514 xfs_attr_shortform_add(args, forkoff);
Eric Sandeend99831f2014-06-22 15:03:54 +1000515 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516}
517
518
519/*========================================================================
520 * External routines when attribute list is one block
521 *========================================================================*/
522
523/*
524 * Add a name to the leaf attribute list structure
525 *
526 * This leaf block cannot have a "remote" value, we only call this routine
527 * if bmap_one_block() says there is only one block (ie: no remote blks).
528 */
David Chinnera8272ce2007-11-23 16:28:09 +1100529STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530xfs_attr_leaf_addname(xfs_da_args_t *args)
531{
532 xfs_inode_t *dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000533 struct xfs_buf *bp;
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100534 int retval, error, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Dave Chinner5a5881c2012-03-22 05:15:13 +0000536 trace_xfs_attr_leaf_addname(args);
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 /*
539 * Read the (only) block in the attribute list in.
540 */
541 dp = args->dp;
542 args->blkno = 0;
Dave Chinner517c2222013-04-24 18:58:55 +1000543 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100545 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 /*
548 * Look up the given attribute in the leaf block. Figure out if
549 * the given flags produce an error or call for an atomic rename.
550 */
Dave Chinner517c2222013-04-24 18:58:55 +1000551 retval = xfs_attr3_leaf_lookup_int(bp, args);
Dave Chinner24513372014-06-25 14:58:08 +1000552 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000553 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000554 return retval;
Dave Chinner24513372014-06-25 14:58:08 +1000555 } else if (retval == -EEXIST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 if (args->flags & ATTR_CREATE) { /* pure create op */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000557 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000558 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 }
Dave Chinner5a5881c2012-03-22 05:15:13 +0000560
561 trace_xfs_attr_leaf_replace(args);
562
Dave Chinner8275cdd2014-05-06 07:37:31 +1000563 /* save the attribute state for later removal*/
Barry Naujok6a178102008-05-21 16:42:05 +1000564 args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 args->blkno2 = args->blkno; /* set 2nd entry info*/
566 args->index2 = args->index;
567 args->rmtblkno2 = args->rmtblkno;
568 args->rmtblkcnt2 = args->rmtblkcnt;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000569 args->rmtvaluelen2 = args->rmtvaluelen;
570
571 /*
572 * clear the remote attr state now that it is saved so that the
573 * values reflect the state of the attribute we are about to
574 * add, not the attribute we just found and will remove later.
575 */
576 args->rmtblkno = 0;
577 args->rmtblkcnt = 0;
578 args->rmtvaluelen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 }
580
581 /*
582 * Add the attribute to the leaf block, transitioning to a Btree
583 * if required.
584 */
Dave Chinner517c2222013-04-24 18:58:55 +1000585 retval = xfs_attr3_leaf_add(bp, args);
Dave Chinner24513372014-06-25 14:58:08 +1000586 if (retval == -ENOSPC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 /*
588 * Promote the attribute list to the Btree format, then
589 * Commit that transaction so that the node_addname() call
590 * can manage its own transactions.
591 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000592 xfs_defer_init(args->dfops, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000593 error = xfs_attr3_leaf_to_node(args);
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100594 if (!error)
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000595 error = xfs_defer_finish(&args->trans, args->dfops, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 args->trans = NULL;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000598 xfs_defer_cancel(args->dfops);
Eric Sandeend99831f2014-06-22 15:03:54 +1000599 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 }
601
602 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 * Commit the current trans (including the inode) and start
604 * a new one.
605 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000606 error = xfs_trans_roll(&args->trans, dp);
607 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000608 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 /*
611 * Fob the whole rest of the problem off on the Btree code.
612 */
613 error = xfs_attr_node_addname(args);
Eric Sandeend99831f2014-06-22 15:03:54 +1000614 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 }
616
617 /*
618 * Commit the transaction that added the attr name so that
619 * later routines can manage their own transactions.
620 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000621 error = xfs_trans_roll(&args->trans, dp);
622 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000623 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 /*
626 * If there was an out-of-line value, allocate the blocks we
627 * identified for its storage and copy the value. This is done
628 * after we create the attribute so that we don't overflow the
629 * maximum size of a transaction and/or hit a deadlock.
630 */
631 if (args->rmtblkno > 0) {
632 error = xfs_attr_rmtval_set(args);
633 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000634 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 }
636
637 /*
638 * If this is an atomic rename operation, we must "flip" the
639 * incomplete flags on the "new" and "old" attribute/value pairs
640 * so that one disappears and one appears atomically. Then we
641 * must remove the "old" attribute/value pair.
642 */
Barry Naujok6a178102008-05-21 16:42:05 +1000643 if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 /*
645 * In a separate transaction, set the incomplete flag on the
646 * "old" attr and clear the incomplete flag on the "new" attr.
647 */
Dave Chinner517c2222013-04-24 18:58:55 +1000648 error = xfs_attr3_leaf_flipflags(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000650 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652 /*
653 * Dismantle the "old" attribute/value pair by removing
654 * a "remote" value (if it exists).
655 */
656 args->index = args->index2;
657 args->blkno = args->blkno2;
658 args->rmtblkno = args->rmtblkno2;
659 args->rmtblkcnt = args->rmtblkcnt2;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000660 args->rmtvaluelen = args->rmtvaluelen2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 if (args->rmtblkno) {
662 error = xfs_attr_rmtval_remove(args);
663 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000664 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 }
666
667 /*
668 * Read in the block containing the "old" attr, then
669 * remove the "old" attr from that block (neat, huh!)
670 */
Dave Chinner517c2222013-04-24 18:58:55 +1000671 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
Dave Chinnerad14c332012-11-12 22:54:16 +1100672 -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100674 return error;
675
Dave Chinner517c2222013-04-24 18:58:55 +1000676 xfs_attr3_leaf_remove(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678 /*
679 * If the result is small enough, shrink it all into the inode.
680 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100681 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000682 xfs_defer_init(args->dfops, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000683 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 /* bp is gone due to xfs_da_shrink_inode */
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100685 if (!error)
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000686 error = xfs_defer_finish(&args->trans,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000687 args->dfops, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 args->trans = NULL;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000690 xfs_defer_cancel(args->dfops);
Eric Sandeend99831f2014-06-22 15:03:54 +1000691 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 }
Dave Chinner1d9025e2012-06-22 18:50:14 +1000693 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 /*
696 * Commit the remove and start the next trans in series.
697 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000698 error = xfs_trans_roll(&args->trans, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
700 } else if (args->rmtblkno > 0) {
701 /*
702 * Added a "remote" value, just clear the incomplete flag.
703 */
Dave Chinner517c2222013-04-24 18:58:55 +1000704 error = xfs_attr3_leaf_clearflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
Dave Chinner517c2222013-04-24 18:58:55 +1000706 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707}
708
709/*
710 * Remove a name from the leaf attribute list structure
711 *
712 * This leaf block cannot have a "remote" value, we only call this routine
713 * if bmap_one_block() says there is only one block (ie: no remote blks).
714 */
715STATIC int
716xfs_attr_leaf_removename(xfs_da_args_t *args)
717{
718 xfs_inode_t *dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000719 struct xfs_buf *bp;
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100720 int error, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Dave Chinner5a5881c2012-03-22 05:15:13 +0000722 trace_xfs_attr_leaf_removename(args);
723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 /*
725 * Remove the attribute.
726 */
727 dp = args->dp;
728 args->blkno = 0;
Dave Chinner517c2222013-04-24 18:58:55 +1000729 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Dave Chinnerad14c332012-11-12 22:54:16 +1100730 if (error)
731 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Dave Chinner517c2222013-04-24 18:58:55 +1000733 error = xfs_attr3_leaf_lookup_int(bp, args);
Dave Chinner24513372014-06-25 14:58:08 +1000734 if (error == -ENOATTR) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000735 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000736 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 }
738
Dave Chinner517c2222013-04-24 18:58:55 +1000739 xfs_attr3_leaf_remove(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
741 /*
742 * If the result is small enough, shrink it all into the inode.
743 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100744 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000745 xfs_defer_init(args->dfops, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000746 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 /* bp is gone due to xfs_da_shrink_inode */
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100748 if (!error)
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000749 error = xfs_defer_finish(&args->trans, args->dfops, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 args->trans = NULL;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000752 xfs_defer_cancel(args->dfops);
Dave Chinner517c2222013-04-24 18:58:55 +1000753 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 }
Dave Chinner1d9025e2012-06-22 18:50:14 +1000755 }
Dave Chinner517c2222013-04-24 18:58:55 +1000756 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757}
758
759/*
760 * Look up a name in a leaf attribute list structure.
761 *
762 * This leaf block cannot have a "remote" value, we only call this routine
763 * if bmap_one_block() says there is only one block (ie: no remote blks).
764 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000765STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766xfs_attr_leaf_get(xfs_da_args_t *args)
767{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000768 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 int error;
770
Dave Chinneree732592012-11-12 22:53:53 +1100771 trace_xfs_attr_leaf_get(args);
772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 args->blkno = 0;
Dave Chinner517c2222013-04-24 18:58:55 +1000774 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100776 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Dave Chinner517c2222013-04-24 18:58:55 +1000778 error = xfs_attr3_leaf_lookup_int(bp, args);
Dave Chinner24513372014-06-25 14:58:08 +1000779 if (error != -EEXIST) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000780 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000781 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 }
Dave Chinner517c2222013-04-24 18:58:55 +1000783 error = xfs_attr3_leaf_getvalue(bp, args);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000784 xfs_trans_brelse(args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
786 error = xfs_attr_rmtval_get(args);
787 }
Dave Chinner517c2222013-04-24 18:58:55 +1000788 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789}
790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791/*========================================================================
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000792 * External routines when attribute list size > geo->blksize
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 *========================================================================*/
794
795/*
796 * Add a name to a Btree-format attribute list.
797 *
798 * This will involve walking down the Btree, and may involve splitting
799 * leaf nodes and even splitting intermediate nodes up to and including
800 * the root node (a special case of an intermediate node).
801 *
802 * "Remote" attribute values confuse the issue and atomic rename operations
803 * add a whole extra layer of confusion on top of that.
804 */
805STATIC int
806xfs_attr_node_addname(xfs_da_args_t *args)
807{
808 xfs_da_state_t *state;
809 xfs_da_state_blk_t *blk;
810 xfs_inode_t *dp;
811 xfs_mount_t *mp;
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100812 int retval, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Dave Chinner5a5881c2012-03-22 05:15:13 +0000814 trace_xfs_attr_node_addname(args);
815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 /*
817 * Fill in bucket of arguments/results/context to carry around.
818 */
819 dp = args->dp;
820 mp = dp->i_mount;
821restart:
822 state = xfs_da_state_alloc();
823 state->args = args;
824 state->mp = mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826 /*
827 * Search to see if name already exists, and get back a pointer
828 * to where it should go.
829 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000830 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 if (error)
832 goto out;
833 blk = &state->path.blk[ state->path.active-1 ];
834 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner24513372014-06-25 14:58:08 +1000835 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 goto out;
Dave Chinner24513372014-06-25 14:58:08 +1000837 } else if (retval == -EEXIST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 if (args->flags & ATTR_CREATE)
839 goto out;
Dave Chinner5a5881c2012-03-22 05:15:13 +0000840
841 trace_xfs_attr_node_replace(args);
842
Dave Chinner8275cdd2014-05-06 07:37:31 +1000843 /* save the attribute state for later removal*/
Barry Naujok6a178102008-05-21 16:42:05 +1000844 args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 args->blkno2 = args->blkno; /* set 2nd entry info*/
846 args->index2 = args->index;
847 args->rmtblkno2 = args->rmtblkno;
848 args->rmtblkcnt2 = args->rmtblkcnt;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000849 args->rmtvaluelen2 = args->rmtvaluelen;
850
851 /*
852 * clear the remote attr state now that it is saved so that the
853 * values reflect the state of the attribute we are about to
854 * add, not the attribute we just found and will remove later.
855 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 args->rmtblkno = 0;
857 args->rmtblkcnt = 0;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000858 args->rmtvaluelen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 }
860
Dave Chinner517c2222013-04-24 18:58:55 +1000861 retval = xfs_attr3_leaf_add(blk->bp, state->args);
Dave Chinner24513372014-06-25 14:58:08 +1000862 if (retval == -ENOSPC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 if (state->path.active == 1) {
864 /*
865 * Its really a single leaf node, but it had
866 * out-of-line values so it looked like it *might*
867 * have been a b-tree.
868 */
869 xfs_da_state_free(state);
Eric Sandeen6dd93e92013-07-31 20:18:54 -0500870 state = NULL;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000871 xfs_defer_init(args->dfops, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000872 error = xfs_attr3_leaf_to_node(args);
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100873 if (!error)
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000874 error = xfs_defer_finish(&args->trans,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000875 args->dfops, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 args->trans = NULL;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000878 xfs_defer_cancel(args->dfops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 goto out;
880 }
881
882 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 * Commit the node conversion and start the next
884 * trans in the chain.
885 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000886 error = xfs_trans_roll(&args->trans, dp);
887 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 goto out;
889
890 goto restart;
891 }
892
893 /*
894 * Split as many Btree elements as required.
895 * This code tracks the new and old attr's location
896 * in the index/blkno/rmtblkno/rmtblkcnt fields and
897 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
898 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000899 xfs_defer_init(args->dfops, args->firstblock);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000900 error = xfs_da3_split(state);
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100901 if (!error)
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000902 error = xfs_defer_finish(&args->trans, args->dfops, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 args->trans = NULL;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000905 xfs_defer_cancel(args->dfops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 goto out;
907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 } else {
909 /*
910 * Addition succeeded, update Btree hashvals.
911 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000912 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 }
914
915 /*
916 * Kill the state structure, we're done with it and need to
917 * allow the buffers to come back later.
918 */
919 xfs_da_state_free(state);
920 state = NULL;
921
922 /*
923 * Commit the leaf addition or btree split and start the next
924 * trans in the chain.
925 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000926 error = xfs_trans_roll(&args->trans, dp);
927 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 goto out;
929
930 /*
931 * If there was an out-of-line value, allocate the blocks we
932 * identified for its storage and copy the value. This is done
933 * after we create the attribute so that we don't overflow the
934 * maximum size of a transaction and/or hit a deadlock.
935 */
936 if (args->rmtblkno > 0) {
937 error = xfs_attr_rmtval_set(args);
938 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000939 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 }
941
942 /*
943 * If this is an atomic rename operation, we must "flip" the
944 * incomplete flags on the "new" and "old" attribute/value pairs
945 * so that one disappears and one appears atomically. Then we
946 * must remove the "old" attribute/value pair.
947 */
Barry Naujok6a178102008-05-21 16:42:05 +1000948 if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 /*
950 * In a separate transaction, set the incomplete flag on the
951 * "old" attr and clear the incomplete flag on the "new" attr.
952 */
Dave Chinner517c2222013-04-24 18:58:55 +1000953 error = xfs_attr3_leaf_flipflags(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 if (error)
955 goto out;
956
957 /*
958 * Dismantle the "old" attribute/value pair by removing
959 * a "remote" value (if it exists).
960 */
961 args->index = args->index2;
962 args->blkno = args->blkno2;
963 args->rmtblkno = args->rmtblkno2;
964 args->rmtblkcnt = args->rmtblkcnt2;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000965 args->rmtvaluelen = args->rmtvaluelen2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 if (args->rmtblkno) {
967 error = xfs_attr_rmtval_remove(args);
968 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000969 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 }
971
972 /*
973 * Re-find the "old" attribute entry after any split ops.
974 * The INCOMPLETE flag means that we will find the "old"
975 * attr, not the "new" one.
976 */
977 args->flags |= XFS_ATTR_INCOMPLETE;
978 state = xfs_da_state_alloc();
979 state->args = args;
980 state->mp = mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 state->inleaf = 0;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000982 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 if (error)
984 goto out;
985
986 /*
987 * Remove the name and update the hashvals in the tree.
988 */
989 blk = &state->path.blk[ state->path.active-1 ];
990 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner517c2222013-04-24 18:58:55 +1000991 error = xfs_attr3_leaf_remove(blk->bp, args);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000992 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
994 /*
995 * Check to see if the tree needs to be collapsed.
996 */
997 if (retval && (state->path.active > 1)) {
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000998 xfs_defer_init(args->dfops, args->firstblock);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000999 error = xfs_da3_join(state);
Eric Sandeenf6106ef2016-01-11 11:34:01 +11001000 if (!error)
Darrick J. Wong310a75a2016-08-03 11:18:10 +10001001 error = xfs_defer_finish(&args->trans,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001002 args->dfops, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 args->trans = NULL;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001005 xfs_defer_cancel(args->dfops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 goto out;
1007 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 }
1009
1010 /*
1011 * Commit and start the next trans in the chain.
1012 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10001013 error = xfs_trans_roll(&args->trans, dp);
1014 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 goto out;
1016
1017 } else if (args->rmtblkno > 0) {
1018 /*
1019 * Added a "remote" value, just clear the incomplete flag.
1020 */
Dave Chinner517c2222013-04-24 18:58:55 +10001021 error = xfs_attr3_leaf_clearflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 if (error)
1023 goto out;
1024 }
1025 retval = error = 0;
1026
1027out:
1028 if (state)
1029 xfs_da_state_free(state);
1030 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001031 return error;
1032 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033}
1034
1035/*
1036 * Remove a name from a B-tree attribute list.
1037 *
1038 * This will involve walking down the Btree, and may involve joining
1039 * leaf nodes and even joining intermediate nodes up to and including
1040 * the root node (a special case of an intermediate node).
1041 */
1042STATIC int
1043xfs_attr_node_removename(xfs_da_args_t *args)
1044{
1045 xfs_da_state_t *state;
1046 xfs_da_state_blk_t *blk;
1047 xfs_inode_t *dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001048 struct xfs_buf *bp;
Eric Sandeenf6106ef2016-01-11 11:34:01 +11001049 int retval, error, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
Dave Chinner5a5881c2012-03-22 05:15:13 +00001051 trace_xfs_attr_node_removename(args);
1052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 /*
1054 * Tie a string around our finger to remind us where we are.
1055 */
1056 dp = args->dp;
1057 state = xfs_da_state_alloc();
1058 state->args = args;
1059 state->mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
1061 /*
1062 * Search to see if name exists, and get back a pointer to it.
1063 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001064 error = xfs_da3_node_lookup_int(state, &retval);
Dave Chinner24513372014-06-25 14:58:08 +10001065 if (error || (retval != -EEXIST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 if (error == 0)
1067 error = retval;
1068 goto out;
1069 }
1070
1071 /*
1072 * If there is an out-of-line value, de-allocate the blocks.
1073 * This is done before we remove the attribute so that we don't
1074 * overflow the maximum size of a transaction and/or hit a deadlock.
1075 */
1076 blk = &state->path.blk[ state->path.active-1 ];
1077 ASSERT(blk->bp != NULL);
1078 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1079 if (args->rmtblkno > 0) {
1080 /*
1081 * Fill in disk block numbers in the state structure
1082 * so that we can get the buffers back after we commit
1083 * several transactions in the following calls.
1084 */
1085 error = xfs_attr_fillstate(state);
1086 if (error)
1087 goto out;
1088
1089 /*
1090 * Mark the attribute as INCOMPLETE, then bunmapi() the
1091 * remote value.
1092 */
Dave Chinner517c2222013-04-24 18:58:55 +10001093 error = xfs_attr3_leaf_setflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 if (error)
1095 goto out;
1096 error = xfs_attr_rmtval_remove(args);
1097 if (error)
1098 goto out;
1099
1100 /*
1101 * Refill the state structure with buffers, the prior calls
1102 * released our buffers.
1103 */
1104 error = xfs_attr_refillstate(state);
1105 if (error)
1106 goto out;
1107 }
1108
1109 /*
1110 * Remove the name and update the hashvals in the tree.
1111 */
1112 blk = &state->path.blk[ state->path.active-1 ];
1113 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner517c2222013-04-24 18:58:55 +10001114 retval = xfs_attr3_leaf_remove(blk->bp, args);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001115 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117 /*
1118 * Check to see if the tree needs to be collapsed.
1119 */
1120 if (retval && (state->path.active > 1)) {
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001121 xfs_defer_init(args->dfops, args->firstblock);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001122 error = xfs_da3_join(state);
Eric Sandeenf6106ef2016-01-11 11:34:01 +11001123 if (!error)
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001124 error = xfs_defer_finish(&args->trans, args->dfops, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 args->trans = NULL;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001127 xfs_defer_cancel(args->dfops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 goto out;
1129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 /*
1131 * Commit the Btree join operation and start a new trans.
1132 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10001133 error = xfs_trans_roll(&args->trans, dp);
1134 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 goto out;
1136 }
1137
1138 /*
1139 * If the result is small enough, push it all into the inode.
1140 */
1141 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
1142 /*
1143 * Have to get rid of the copy of this dabuf in the state.
1144 */
1145 ASSERT(state->path.active == 1);
1146 ASSERT(state->path.blk[0].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 state->path.blk[0].bp = NULL;
1148
Dave Chinner517c2222013-04-24 18:58:55 +10001149 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 if (error)
1151 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
Nathan Scottd8cc8902005-11-02 10:34:53 +11001153 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001154 xfs_defer_init(args->dfops, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +10001155 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 /* bp is gone due to xfs_da_shrink_inode */
Eric Sandeenf6106ef2016-01-11 11:34:01 +11001157 if (!error)
Darrick J. Wong310a75a2016-08-03 11:18:10 +10001158 error = xfs_defer_finish(&args->trans,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001159 args->dfops, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 args->trans = NULL;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001162 xfs_defer_cancel(args->dfops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 goto out;
1164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 } else
Dave Chinner1d9025e2012-06-22 18:50:14 +10001166 xfs_trans_brelse(args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 }
1168 error = 0;
1169
1170out:
1171 xfs_da_state_free(state);
Eric Sandeend99831f2014-06-22 15:03:54 +10001172 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173}
1174
1175/*
1176 * Fill in the disk block numbers in the state structure for the buffers
1177 * that are attached to the state structure.
1178 * This is done so that we can quickly reattach ourselves to those buffers
Nathan Scottc41564b2006-03-29 08:55:14 +10001179 * after some set of transaction commits have released these buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 */
1181STATIC int
1182xfs_attr_fillstate(xfs_da_state_t *state)
1183{
1184 xfs_da_state_path_t *path;
1185 xfs_da_state_blk_t *blk;
1186 int level;
1187
Dave Chinneree732592012-11-12 22:53:53 +11001188 trace_xfs_attr_fillstate(state->args);
1189
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 /*
1191 * Roll down the "path" in the state structure, storing the on-disk
1192 * block number for those buffers in the "path".
1193 */
1194 path = &state->path;
1195 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1196 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1197 if (blk->bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001198 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 blk->bp = NULL;
1200 } else {
1201 blk->disk_blkno = 0;
1202 }
1203 }
1204
1205 /*
1206 * Roll down the "altpath" in the state structure, storing the on-disk
1207 * block number for those buffers in the "altpath".
1208 */
1209 path = &state->altpath;
1210 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1211 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1212 if (blk->bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001213 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 blk->bp = NULL;
1215 } else {
1216 blk->disk_blkno = 0;
1217 }
1218 }
1219
Eric Sandeend99831f2014-06-22 15:03:54 +10001220 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221}
1222
1223/*
1224 * Reattach the buffers to the state structure based on the disk block
1225 * numbers stored in the state structure.
Nathan Scottc41564b2006-03-29 08:55:14 +10001226 * This is done after some set of transaction commits have released those
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 * buffers from our grip.
1228 */
1229STATIC int
1230xfs_attr_refillstate(xfs_da_state_t *state)
1231{
1232 xfs_da_state_path_t *path;
1233 xfs_da_state_blk_t *blk;
1234 int level, error;
1235
Dave Chinneree732592012-11-12 22:53:53 +11001236 trace_xfs_attr_refillstate(state->args);
1237
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 /*
1239 * Roll down the "path" in the state structure, storing the on-disk
1240 * block number for those buffers in the "path".
1241 */
1242 path = &state->path;
1243 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1244 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1245 if (blk->disk_blkno) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001246 error = xfs_da3_node_read(state->args->trans,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 state->args->dp,
1248 blk->blkno, blk->disk_blkno,
Dave Chinnerd9392a42012-11-12 22:54:17 +11001249 &blk->bp, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001251 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 } else {
1253 blk->bp = NULL;
1254 }
1255 }
1256
1257 /*
1258 * Roll down the "altpath" in the state structure, storing the on-disk
1259 * block number for those buffers in the "altpath".
1260 */
1261 path = &state->altpath;
1262 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1263 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1264 if (blk->disk_blkno) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001265 error = xfs_da3_node_read(state->args->trans,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 state->args->dp,
1267 blk->blkno, blk->disk_blkno,
Dave Chinnerd9392a42012-11-12 22:54:17 +11001268 &blk->bp, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001270 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 } else {
1272 blk->bp = NULL;
1273 }
1274 }
1275
Eric Sandeend99831f2014-06-22 15:03:54 +10001276 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277}
1278
1279/*
1280 * Look up a filename in a node attribute list.
1281 *
1282 * This routine gets called for any attribute fork that has more than one
1283 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1284 * "remote" values taking up more blocks.
1285 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001286STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287xfs_attr_node_get(xfs_da_args_t *args)
1288{
1289 xfs_da_state_t *state;
1290 xfs_da_state_blk_t *blk;
1291 int error, retval;
1292 int i;
1293
Dave Chinneree732592012-11-12 22:53:53 +11001294 trace_xfs_attr_node_get(args);
1295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 state = xfs_da_state_alloc();
1297 state->args = args;
1298 state->mp = args->dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
1300 /*
1301 * Search to see if name exists, and get back a pointer to it.
1302 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001303 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 if (error) {
1305 retval = error;
Dave Chinner24513372014-06-25 14:58:08 +10001306 } else if (retval == -EEXIST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 blk = &state->path.blk[ state->path.active-1 ];
1308 ASSERT(blk->bp != NULL);
1309 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1310
1311 /*
1312 * Get the value, local or "remote"
1313 */
Dave Chinner517c2222013-04-24 18:58:55 +10001314 retval = xfs_attr3_leaf_getvalue(blk->bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 if (!retval && (args->rmtblkno > 0)
1316 && !(args->flags & ATTR_KERNOVAL)) {
1317 retval = xfs_attr_rmtval_get(args);
1318 }
1319 }
1320
1321 /*
1322 * If not in a transaction, we have to release all the buffers.
1323 */
1324 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001325 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 state->path.blk[i].bp = NULL;
1327 }
1328
1329 xfs_da_state_free(state);
Eric Sandeend99831f2014-06-22 15:03:54 +10001330 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331}