blob: 6249c92671debe20a45e3cb0577360552e36d523 [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
Christoph Hellwigcf69f822017-07-13 12:14:33 -0700117/* Retrieve an extended attribute and its value. Must have ilock. */
Darrick J. Wongad017f62017-06-16 11:00:14 -0700118int
119xfs_attr_get_ilocked(
120 struct xfs_inode *ip,
121 struct xfs_da_args *args)
122{
Christoph Hellwigcf69f822017-07-13 12:14:33 -0700123 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
124
Darrick J. Wongad017f62017-06-16 11:00:14 -0700125 if (!xfs_inode_hasattr(ip))
126 return -ENOATTR;
127 else if (ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL)
128 return xfs_attr_shortform_getvalue(args);
129 else if (xfs_bmap_one_block(ip, XFS_ATTR_FORK))
130 return xfs_attr_leaf_get(args);
131 else
132 return xfs_attr_node_get(args);
133}
134
135/* Retrieve an extended attribute by name, and its value. */
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000136int
137xfs_attr_get(
Christoph Hellwige82fa0c2009-11-14 16:17:20 +0000138 struct xfs_inode *ip,
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000139 const unsigned char *name,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100140 unsigned char *value,
Christoph Hellwige82fa0c2009-11-14 16:17:20 +0000141 int *valuelenp,
142 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000144 struct xfs_da_args args;
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000145 uint lock_mode;
146 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100148 XFS_STATS_INC(ip->i_mount, xs_attr_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
Dave Chinner24513372014-06-25 14:58:08 +1000151 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000153 error = xfs_attr_args_init(&args, ip, name, flags);
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000154 if (error)
155 return error;
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 args.value = value;
158 args.valuelen = *valuelenp;
Eric Sandeenc400ee32015-08-19 10:30:48 +1000159 /* Entirely possible to look up a name which doesn't exist */
160 args.op_flags = XFS_DA_OP_OKNOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Christoph Hellwig683cb942013-12-06 12:30:15 -0800162 lock_mode = xfs_ilock_attr_map_shared(ip);
Darrick J. Wongad017f62017-06-16 11:00:14 -0700163 error = xfs_attr_get_ilocked(ip, &args);
Christoph Hellwig683cb942013-12-06 12:30:15 -0800164 xfs_iunlock(ip, lock_mode);
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000165
166 *valuelenp = args.valuelen;
Dave Chinner24513372014-06-25 14:58:08 +1000167 return error == -EEXIST ? 0 : error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
169
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000170/*
171 * Calculate how many blocks we need for the new attribute,
172 */
Eric Sandeen5d77c0d2009-11-19 15:52:00 +0000173STATIC int
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000174xfs_attr_calc_size(
Christoph Hellwig6c888af2014-05-13 16:40:19 +1000175 struct xfs_da_args *args,
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000176 int *local)
177{
Christoph Hellwig6c888af2014-05-13 16:40:19 +1000178 struct xfs_mount *mp = args->dp->i_mount;
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000179 int size;
180 int nblks;
181
182 /*
183 * Determine space new attribute will use, and if it would be
184 * "local" or "remote" (note: local != inline).
185 */
Dave Chinnerc59f0ad2014-06-06 15:21:27 +1000186 size = xfs_attr_leaf_newentsize(args, local);
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000187 nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
188 if (*local) {
Dave Chinner33a60392014-06-06 15:21:10 +1000189 if (size > (args->geo->blksize / 2)) {
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000190 /* Double split possible */
191 nblks *= 2;
192 }
193 } else {
194 /*
195 * Out of line attribute, cannot double split, but
196 * make room for the attribute value itself.
197 */
Dave Chinner2d6dcc62014-05-15 09:39:28 +1000198 uint dblocks = xfs_attr3_rmt_blocks(mp, args->valuelen);
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000199 nblks += dblocks;
200 nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
201 }
202
203 return nblks;
204}
205
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000206int
207xfs_attr_set(
208 struct xfs_inode *dp,
209 const unsigned char *name,
210 unsigned char *value,
211 int valuelen,
212 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
Jie Liu3d3c8b52013-08-12 20:49:59 +1000214 struct xfs_mount *mp = dp->i_mount;
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000215 struct xfs_da_args args;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000216 struct xfs_defer_ops dfops;
Jie Liu3d3c8b52013-08-12 20:49:59 +1000217 struct xfs_trans_res tres;
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000218 xfs_fsblock_t firstblock;
Jie Liu3d3c8b52013-08-12 20:49:59 +1000219 int rsvd = (flags & ATTR_ROOT) != 0;
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100220 int error, err2, local;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100222 XFS_STATS_INC(mp, xs_attr_set);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000223
224 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
Dave Chinner24513372014-06-25 14:58:08 +1000225 return -EIO;
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000226
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000227 error = xfs_attr_args_init(&args, dp, name, flags);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000228 if (error)
229 return error;
230
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000231 args.value = value;
232 args.valuelen = valuelen;
233 args.firstblock = &firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000234 args.dfops = &dfops;
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000235 args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
Christoph Hellwig6c888af2014-05-13 16:40:19 +1000236 args.total = xfs_attr_calc_size(&args, &local);
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000237
Christoph Hellwig7d095252009-06-08 15:33:32 +0200238 error = xfs_qm_dqattach(dp, 0);
239 if (error)
240 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242 /*
243 * If the inode doesn't have an attribute fork, add one.
244 * (inode must not be locked when we call this routine)
245 */
246 if (XFS_IFORK_Q(dp) == 0) {
Barry Naujoke5889e92007-02-10 18:35:58 +1100247 int sf_size = sizeof(xfs_attr_sf_hdr_t) +
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000248 XFS_ATTR_SF_ENTSIZE_BYNAME(args.namelen, valuelen);
Barry Naujoke5889e92007-02-10 18:35:58 +1100249
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000250 error = xfs_bmap_add_attrfork(dp, sf_size, rsvd);
251 if (error)
252 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 }
254
Christoph Hellwig253f4912016-04-06 09:19:55 +1000255 tres.tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
256 M_RES(mp)->tr_attrsetrt.tr_logres * args.total;
257 tres.tr_logcount = XFS_ATTRSET_LOG_COUNT;
258 tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 /*
261 * Root fork attributes can use reserved data blocks for this
262 * operation if necessary
263 */
Christoph Hellwig253f4912016-04-06 09:19:55 +1000264 error = xfs_trans_alloc(mp, &tres, args.total, 0,
265 rsvd ? XFS_TRANS_RESERVE : 0, &args.trans);
266 if (error)
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000267 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Christoph Hellwig253f4912016-04-06 09:19:55 +1000269 xfs_ilock(dp, XFS_ILOCK_EXCL);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200270 error = xfs_trans_reserve_quota_nblks(args.trans, dp, args.total, 0,
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000271 rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
272 XFS_QMOPT_RES_REGBLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 if (error) {
274 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Christoph Hellwig4906e212015-06-04 13:47:56 +1000275 xfs_trans_cancel(args.trans);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000276 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 }
278
Christoph Hellwigddc34152011-09-19 15:00:54 +0000279 xfs_trans_ijoin(args.trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000282 * If the attribute list is non-existent or a shortform list,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 * upgrade it to a single-leaf-block attribute list.
284 */
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000285 if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL ||
286 (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
287 dp->i_d.di_anextents == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289 /*
290 * Build initial attribute list (if required).
291 */
292 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
Nathan Scottd8cc8902005-11-02 10:34:53 +1100293 xfs_attr_shortform_create(&args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
295 /*
296 * Try to add the attr to the attribute list in
297 * the inode.
298 */
299 error = xfs_attr_shortform_addname(&args);
Dave Chinner24513372014-06-25 14:58:08 +1000300 if (error != -ENOSPC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 /*
302 * Commit the shortform mods, and we're done.
303 * NOTE: this is also the error path (EEXIST, etc).
304 */
305 ASSERT(args.trans != NULL);
306
307 /*
308 * If this is a synchronous mount, make sure that
309 * the transaction goes to disk before returning
310 * to the user.
311 */
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000312 if (mp->m_flags & XFS_MOUNT_WSYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 xfs_trans_set_sync(args.trans);
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000314
315 if (!error && (flags & ATTR_KERNOTIME) == 0) {
316 xfs_trans_ichgtime(args.trans, dp,
317 XFS_ICHGTIME_CHG);
318 }
Christoph Hellwig70393312015-06-04 13:48:08 +1000319 err2 = xfs_trans_commit(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 xfs_iunlock(dp, XFS_ILOCK_EXCL);
321
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000322 return error ? error : err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 }
324
325 /*
326 * It won't fit in the shortform, transform to a leaf block.
327 * GROT: another possible req'mt for a double-split btree op.
328 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000329 xfs_defer_init(args.dfops, args.firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 error = xfs_attr_shortform_to_leaf(&args);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700331 if (error)
332 goto out_defer_cancel;
333 xfs_defer_ijoin(args.dfops, dp);
334 error = xfs_defer_finish(&args.trans, args.dfops);
335 if (error)
336 goto out_defer_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 * Commit the leaf transformation. We'll need another (linked)
340 * transaction to add the new attribute to the leaf.
341 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000342
Christoph Hellwig411350d2017-08-28 10:21:03 -0700343 error = xfs_trans_roll_inode(&args.trans, dp);
Niv Sardi322ff6b2008-08-13 16:05:49 +1000344 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 goto out;
346
347 }
348
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000349 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 error = xfs_attr_leaf_addname(&args);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000351 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 error = xfs_attr_node_addname(&args);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000353 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 /*
357 * If this is a synchronous mount, make sure that the
358 * transaction goes to disk before returning to the user.
359 */
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000360 if (mp->m_flags & XFS_MOUNT_WSYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 xfs_trans_set_sync(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000363 if ((flags & ATTR_KERNOTIME) == 0)
364 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 /*
367 * Commit the last in the sequence of transactions.
368 */
369 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
Christoph Hellwig70393312015-06-04 13:48:08 +1000370 error = xfs_trans_commit(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 xfs_iunlock(dp, XFS_ILOCK_EXCL);
372
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000373 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700375out_defer_cancel:
376 xfs_defer_cancel(&dfops);
377 args.trans = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378out:
Christoph Hellwig4906e212015-06-04 13:47:56 +1000379 if (args.trans)
380 xfs_trans_cancel(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000382 return error;
Nathan Scottaa82daa2005-11-02 10:33:33 +1100383}
384
385/*
386 * Generic handler routine to remove a name from an attribute list.
387 * Transitions attribute list from Btree to shortform as necessary.
388 */
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000389int
390xfs_attr_remove(
391 struct xfs_inode *dp,
392 const unsigned char *name,
393 int flags)
Nathan Scottaa82daa2005-11-02 10:33:33 +1100394{
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000395 struct xfs_mount *mp = dp->i_mount;
396 struct xfs_da_args args;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000397 struct xfs_defer_ops dfops;
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000398 xfs_fsblock_t firstblock;
399 int error;
Nathan Scottaa82daa2005-11-02 10:33:33 +1100400
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100401 XFS_STATS_INC(mp, xs_attr_remove);
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000402
403 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
Dave Chinner24513372014-06-25 14:58:08 +1000404 return -EIO;
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000405
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000406 error = xfs_attr_args_init(&args, dp, name, flags);
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000407 if (error)
408 return error;
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 args.firstblock = &firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000411 args.dfops = &dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413 /*
Dave Chinner4a338212011-06-23 01:35:01 +0000414 * we have no control over the attribute names that userspace passes us
415 * to remove, so we have to allow the name lookup prior to attribute
416 * removal to fail.
417 */
418 args.op_flags = XFS_DA_OP_OKNOENT;
419
Christoph Hellwig7d095252009-06-08 15:33:32 +0200420 error = xfs_qm_dqattach(dp, 0);
421 if (error)
422 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 * Root fork attributes can use reserved data blocks for this
426 * operation if necessary
427 */
Christoph Hellwig253f4912016-04-06 09:19:55 +1000428 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_attrrm,
429 XFS_ATTRRM_SPACE_RES(mp), 0,
430 (flags & ATTR_ROOT) ? XFS_TRANS_RESERVE : 0,
431 &args.trans);
432 if (error)
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000433 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 xfs_ilock(dp, XFS_ILOCK_EXCL);
436 /*
437 * No need to make quota reservations here. We expect to release some
438 * blocks not allocate in the common case.
439 */
Christoph Hellwigddc34152011-09-19 15:00:54 +0000440 xfs_trans_ijoin(args.trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +1000442 if (!xfs_inode_hasattr(dp)) {
Dave Chinner24513372014-06-25 14:58:08 +1000443 error = -ENOATTR;
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000444 } else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
446 error = xfs_attr_shortform_remove(&args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
448 error = xfs_attr_leaf_removename(&args);
449 } else {
450 error = xfs_attr_node_removename(&args);
451 }
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000452
453 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 /*
457 * If this is a synchronous mount, make sure that the
458 * transaction goes to disk before returning to the user.
459 */
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000460 if (mp->m_flags & XFS_MOUNT_WSYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 xfs_trans_set_sync(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000463 if ((flags & ATTR_KERNOTIME) == 0)
464 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 /*
467 * Commit the last in the sequence of transactions.
468 */
469 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
Christoph Hellwig70393312015-06-04 13:48:08 +1000470 error = xfs_trans_commit(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 xfs_iunlock(dp, XFS_ILOCK_EXCL);
472
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000473 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475out:
Christoph Hellwig4906e212015-06-04 13:47:56 +1000476 if (args.trans)
477 xfs_trans_cancel(args.trans);
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000478 xfs_iunlock(dp, XFS_ILOCK_EXCL);
479 return error;
Nathan Scottaa82daa2005-11-02 10:33:33 +1100480}
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482/*========================================================================
483 * External routines when attribute list is inside the inode
484 *========================================================================*/
485
486/*
487 * Add a name to the shortform attribute list structure
488 * This is the external routine.
489 */
490STATIC int
491xfs_attr_shortform_addname(xfs_da_args_t *args)
492{
Nathan Scottd8cc8902005-11-02 10:34:53 +1100493 int newsize, forkoff, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Dave Chinner5a5881c2012-03-22 05:15:13 +0000495 trace_xfs_attr_sf_addname(args);
496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 retval = xfs_attr_shortform_lookup(args);
Dave Chinner24513372014-06-25 14:58:08 +1000498 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
Eric Sandeend99831f2014-06-22 15:03:54 +1000499 return retval;
Dave Chinner24513372014-06-25 14:58:08 +1000500 } else if (retval == -EEXIST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 if (args->flags & ATTR_CREATE)
Eric Sandeend99831f2014-06-22 15:03:54 +1000502 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 retval = xfs_attr_shortform_remove(args);
504 ASSERT(retval == 0);
505 }
506
Nathan Scottd8cc8902005-11-02 10:34:53 +1100507 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
508 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
Dave Chinner24513372014-06-25 14:58:08 +1000509 return -ENOSPC;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
512 newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100513
514 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
515 if (!forkoff)
Dave Chinner24513372014-06-25 14:58:08 +1000516 return -ENOSPC;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100517
518 xfs_attr_shortform_add(args, forkoff);
Eric Sandeend99831f2014-06-22 15:03:54 +1000519 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520}
521
522
523/*========================================================================
524 * External routines when attribute list is one block
525 *========================================================================*/
526
527/*
528 * Add a name to the leaf attribute list structure
529 *
530 * This leaf block cannot have a "remote" value, we only call this routine
531 * if bmap_one_block() says there is only one block (ie: no remote blks).
532 */
David Chinnera8272ce2007-11-23 16:28:09 +1100533STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534xfs_attr_leaf_addname(xfs_da_args_t *args)
535{
536 xfs_inode_t *dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000537 struct xfs_buf *bp;
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100538 int retval, error, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Dave Chinner5a5881c2012-03-22 05:15:13 +0000540 trace_xfs_attr_leaf_addname(args);
541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 /*
543 * Read the (only) block in the attribute list in.
544 */
545 dp = args->dp;
546 args->blkno = 0;
Dave Chinner517c2222013-04-24 18:58:55 +1000547 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100549 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 /*
552 * Look up the given attribute in the leaf block. Figure out if
553 * the given flags produce an error or call for an atomic rename.
554 */
Dave Chinner517c2222013-04-24 18:58:55 +1000555 retval = xfs_attr3_leaf_lookup_int(bp, args);
Dave Chinner24513372014-06-25 14:58:08 +1000556 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000557 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000558 return retval;
Dave Chinner24513372014-06-25 14:58:08 +1000559 } else if (retval == -EEXIST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 if (args->flags & ATTR_CREATE) { /* pure create op */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000561 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000562 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 }
Dave Chinner5a5881c2012-03-22 05:15:13 +0000564
565 trace_xfs_attr_leaf_replace(args);
566
Dave Chinner8275cdd2014-05-06 07:37:31 +1000567 /* save the attribute state for later removal*/
Barry Naujok6a178102008-05-21 16:42:05 +1000568 args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 args->blkno2 = args->blkno; /* set 2nd entry info*/
570 args->index2 = args->index;
571 args->rmtblkno2 = args->rmtblkno;
572 args->rmtblkcnt2 = args->rmtblkcnt;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000573 args->rmtvaluelen2 = args->rmtvaluelen;
574
575 /*
576 * clear the remote attr state now that it is saved so that the
577 * values reflect the state of the attribute we are about to
578 * add, not the attribute we just found and will remove later.
579 */
580 args->rmtblkno = 0;
581 args->rmtblkcnt = 0;
582 args->rmtvaluelen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 }
584
585 /*
586 * Add the attribute to the leaf block, transitioning to a Btree
587 * if required.
588 */
Dave Chinner517c2222013-04-24 18:58:55 +1000589 retval = xfs_attr3_leaf_add(bp, args);
Dave Chinner24513372014-06-25 14:58:08 +1000590 if (retval == -ENOSPC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 /*
592 * Promote the attribute list to the Btree format, then
593 * Commit that transaction so that the node_addname() call
594 * can manage its own transactions.
595 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000596 xfs_defer_init(args->dfops, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000597 error = xfs_attr3_leaf_to_node(args);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700598 if (error)
599 goto out_defer_cancel;
600 xfs_defer_ijoin(args->dfops, dp);
601 error = xfs_defer_finish(&args->trans, args->dfops);
602 if (error)
603 goto out_defer_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 * Commit the current trans (including the inode) and start
607 * a new one.
608 */
Christoph Hellwig411350d2017-08-28 10:21:03 -0700609 error = xfs_trans_roll_inode(&args->trans, dp);
Niv Sardi322ff6b2008-08-13 16:05:49 +1000610 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000611 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 /*
614 * Fob the whole rest of the problem off on the Btree code.
615 */
616 error = xfs_attr_node_addname(args);
Eric Sandeend99831f2014-06-22 15:03:54 +1000617 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 }
619
620 /*
621 * Commit the transaction that added the attr name so that
622 * later routines can manage their own transactions.
623 */
Christoph Hellwig411350d2017-08-28 10:21:03 -0700624 error = xfs_trans_roll_inode(&args->trans, dp);
Niv Sardi322ff6b2008-08-13 16:05:49 +1000625 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000626 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 /*
629 * If there was an out-of-line value, allocate the blocks we
630 * identified for its storage and copy the value. This is done
631 * after we create the attribute so that we don't overflow the
632 * maximum size of a transaction and/or hit a deadlock.
633 */
634 if (args->rmtblkno > 0) {
635 error = xfs_attr_rmtval_set(args);
636 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000637 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 }
639
640 /*
641 * If this is an atomic rename operation, we must "flip" the
642 * incomplete flags on the "new" and "old" attribute/value pairs
643 * so that one disappears and one appears atomically. Then we
644 * must remove the "old" attribute/value pair.
645 */
Barry Naujok6a178102008-05-21 16:42:05 +1000646 if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 /*
648 * In a separate transaction, set the incomplete flag on the
649 * "old" attr and clear the incomplete flag on the "new" attr.
650 */
Dave Chinner517c2222013-04-24 18:58:55 +1000651 error = xfs_attr3_leaf_flipflags(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000653 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 /*
656 * Dismantle the "old" attribute/value pair by removing
657 * a "remote" value (if it exists).
658 */
659 args->index = args->index2;
660 args->blkno = args->blkno2;
661 args->rmtblkno = args->rmtblkno2;
662 args->rmtblkcnt = args->rmtblkcnt2;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000663 args->rmtvaluelen = args->rmtvaluelen2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 if (args->rmtblkno) {
665 error = xfs_attr_rmtval_remove(args);
666 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000667 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 }
669
670 /*
671 * Read in the block containing the "old" attr, then
672 * remove the "old" attr from that block (neat, huh!)
673 */
Dave Chinner517c2222013-04-24 18:58:55 +1000674 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
Dave Chinnerad14c332012-11-12 22:54:16 +1100675 -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100677 return error;
678
Dave Chinner517c2222013-04-24 18:58:55 +1000679 xfs_attr3_leaf_remove(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681 /*
682 * If the result is small enough, shrink it all into the inode.
683 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100684 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000685 xfs_defer_init(args->dfops, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000686 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 /* bp is gone due to xfs_da_shrink_inode */
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700688 if (error)
689 goto out_defer_cancel;
690 xfs_defer_ijoin(args->dfops, dp);
691 error = xfs_defer_finish(&args->trans, args->dfops);
692 if (error)
693 goto out_defer_cancel;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696 /*
697 * Commit the remove and start the next trans in series.
698 */
Christoph Hellwig411350d2017-08-28 10:21:03 -0700699 error = xfs_trans_roll_inode(&args->trans, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701 } else if (args->rmtblkno > 0) {
702 /*
703 * Added a "remote" value, just clear the incomplete flag.
704 */
Dave Chinner517c2222013-04-24 18:58:55 +1000705 error = xfs_attr3_leaf_clearflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 }
Dave Chinner517c2222013-04-24 18:58:55 +1000707 return error;
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700708out_defer_cancel:
709 xfs_defer_cancel(args->dfops);
710 args->trans = NULL;
711 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712}
713
714/*
715 * Remove a name from the leaf attribute list structure
716 *
717 * This leaf block cannot have a "remote" value, we only call this routine
718 * if bmap_one_block() says there is only one block (ie: no remote blks).
719 */
720STATIC int
721xfs_attr_leaf_removename(xfs_da_args_t *args)
722{
723 xfs_inode_t *dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000724 struct xfs_buf *bp;
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100725 int error, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Dave Chinner5a5881c2012-03-22 05:15:13 +0000727 trace_xfs_attr_leaf_removename(args);
728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 /*
730 * Remove the attribute.
731 */
732 dp = args->dp;
733 args->blkno = 0;
Dave Chinner517c2222013-04-24 18:58:55 +1000734 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Dave Chinnerad14c332012-11-12 22:54:16 +1100735 if (error)
736 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Dave Chinner517c2222013-04-24 18:58:55 +1000738 error = xfs_attr3_leaf_lookup_int(bp, args);
Dave Chinner24513372014-06-25 14:58:08 +1000739 if (error == -ENOATTR) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000740 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000741 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 }
743
Dave Chinner517c2222013-04-24 18:58:55 +1000744 xfs_attr3_leaf_remove(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746 /*
747 * If the result is small enough, shrink it all into the inode.
748 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100749 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000750 xfs_defer_init(args->dfops, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000751 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 /* bp is gone due to xfs_da_shrink_inode */
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700753 if (error)
754 goto out_defer_cancel;
755 xfs_defer_ijoin(args->dfops, dp);
756 error = xfs_defer_finish(&args->trans, args->dfops);
757 if (error)
758 goto out_defer_cancel;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000759 }
Dave Chinner517c2222013-04-24 18:58:55 +1000760 return 0;
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700761out_defer_cancel:
762 xfs_defer_cancel(args->dfops);
763 args->trans = NULL;
764 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765}
766
767/*
768 * Look up a name in a leaf attribute list structure.
769 *
770 * This leaf block cannot have a "remote" value, we only call this routine
771 * if bmap_one_block() says there is only one block (ie: no remote blks).
772 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000773STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774xfs_attr_leaf_get(xfs_da_args_t *args)
775{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000776 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 int error;
778
Dave Chinneree732592012-11-12 22:53:53 +1100779 trace_xfs_attr_leaf_get(args);
780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 args->blkno = 0;
Dave Chinner517c2222013-04-24 18:58:55 +1000782 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100784 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Dave Chinner517c2222013-04-24 18:58:55 +1000786 error = xfs_attr3_leaf_lookup_int(bp, args);
Dave Chinner24513372014-06-25 14:58:08 +1000787 if (error != -EEXIST) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000788 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000789 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 }
Dave Chinner517c2222013-04-24 18:58:55 +1000791 error = xfs_attr3_leaf_getvalue(bp, args);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000792 xfs_trans_brelse(args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
794 error = xfs_attr_rmtval_get(args);
795 }
Dave Chinner517c2222013-04-24 18:58:55 +1000796 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797}
798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799/*========================================================================
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000800 * External routines when attribute list size > geo->blksize
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 *========================================================================*/
802
803/*
804 * Add a name to a Btree-format attribute list.
805 *
806 * This will involve walking down the Btree, and may involve splitting
807 * leaf nodes and even splitting intermediate nodes up to and including
808 * the root node (a special case of an intermediate node).
809 *
810 * "Remote" attribute values confuse the issue and atomic rename operations
811 * add a whole extra layer of confusion on top of that.
812 */
813STATIC int
814xfs_attr_node_addname(xfs_da_args_t *args)
815{
816 xfs_da_state_t *state;
817 xfs_da_state_blk_t *blk;
818 xfs_inode_t *dp;
819 xfs_mount_t *mp;
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100820 int retval, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Dave Chinner5a5881c2012-03-22 05:15:13 +0000822 trace_xfs_attr_node_addname(args);
823
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 /*
825 * Fill in bucket of arguments/results/context to carry around.
826 */
827 dp = args->dp;
828 mp = dp->i_mount;
829restart:
830 state = xfs_da_state_alloc();
831 state->args = args;
832 state->mp = mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
834 /*
835 * Search to see if name already exists, and get back a pointer
836 * to where it should go.
837 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000838 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 if (error)
840 goto out;
841 blk = &state->path.blk[ state->path.active-1 ];
842 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner24513372014-06-25 14:58:08 +1000843 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 goto out;
Dave Chinner24513372014-06-25 14:58:08 +1000845 } else if (retval == -EEXIST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 if (args->flags & ATTR_CREATE)
847 goto out;
Dave Chinner5a5881c2012-03-22 05:15:13 +0000848
849 trace_xfs_attr_node_replace(args);
850
Dave Chinner8275cdd2014-05-06 07:37:31 +1000851 /* save the attribute state for later removal*/
Barry Naujok6a178102008-05-21 16:42:05 +1000852 args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 args->blkno2 = args->blkno; /* set 2nd entry info*/
854 args->index2 = args->index;
855 args->rmtblkno2 = args->rmtblkno;
856 args->rmtblkcnt2 = args->rmtblkcnt;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000857 args->rmtvaluelen2 = args->rmtvaluelen;
858
859 /*
860 * clear the remote attr state now that it is saved so that the
861 * values reflect the state of the attribute we are about to
862 * add, not the attribute we just found and will remove later.
863 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 args->rmtblkno = 0;
865 args->rmtblkcnt = 0;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000866 args->rmtvaluelen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 }
868
Dave Chinner517c2222013-04-24 18:58:55 +1000869 retval = xfs_attr3_leaf_add(blk->bp, state->args);
Dave Chinner24513372014-06-25 14:58:08 +1000870 if (retval == -ENOSPC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 if (state->path.active == 1) {
872 /*
873 * Its really a single leaf node, but it had
874 * out-of-line values so it looked like it *might*
875 * have been a b-tree.
876 */
877 xfs_da_state_free(state);
Eric Sandeen6dd93e92013-07-31 20:18:54 -0500878 state = NULL;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000879 xfs_defer_init(args->dfops, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000880 error = xfs_attr3_leaf_to_node(args);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700881 if (error)
882 goto out_defer_cancel;
883 xfs_defer_ijoin(args->dfops, dp);
884 error = xfs_defer_finish(&args->trans, args->dfops);
885 if (error)
886 goto out_defer_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
888 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 * Commit the node conversion and start the next
890 * trans in the chain.
891 */
Christoph Hellwig411350d2017-08-28 10:21:03 -0700892 error = xfs_trans_roll_inode(&args->trans, dp);
Niv Sardi322ff6b2008-08-13 16:05:49 +1000893 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 goto out;
895
896 goto restart;
897 }
898
899 /*
900 * Split as many Btree elements as required.
901 * This code tracks the new and old attr's location
902 * in the index/blkno/rmtblkno/rmtblkcnt fields and
903 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
904 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000905 xfs_defer_init(args->dfops, args->firstblock);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000906 error = xfs_da3_split(state);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700907 if (error)
908 goto out_defer_cancel;
909 xfs_defer_ijoin(args->dfops, dp);
910 error = xfs_defer_finish(&args->trans, args->dfops);
911 if (error)
912 goto out_defer_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 } else {
914 /*
915 * Addition succeeded, update Btree hashvals.
916 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000917 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 }
919
920 /*
921 * Kill the state structure, we're done with it and need to
922 * allow the buffers to come back later.
923 */
924 xfs_da_state_free(state);
925 state = NULL;
926
927 /*
928 * Commit the leaf addition or btree split and start the next
929 * trans in the chain.
930 */
Christoph Hellwig411350d2017-08-28 10:21:03 -0700931 error = xfs_trans_roll_inode(&args->trans, dp);
Niv Sardi322ff6b2008-08-13 16:05:49 +1000932 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 goto out;
934
935 /*
936 * If there was an out-of-line value, allocate the blocks we
937 * identified for its storage and copy the value. This is done
938 * after we create the attribute so that we don't overflow the
939 * maximum size of a transaction and/or hit a deadlock.
940 */
941 if (args->rmtblkno > 0) {
942 error = xfs_attr_rmtval_set(args);
943 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000944 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 }
946
947 /*
948 * If this is an atomic rename operation, we must "flip" the
949 * incomplete flags on the "new" and "old" attribute/value pairs
950 * so that one disappears and one appears atomically. Then we
951 * must remove the "old" attribute/value pair.
952 */
Barry Naujok6a178102008-05-21 16:42:05 +1000953 if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 /*
955 * In a separate transaction, set the incomplete flag on the
956 * "old" attr and clear the incomplete flag on the "new" attr.
957 */
Dave Chinner517c2222013-04-24 18:58:55 +1000958 error = xfs_attr3_leaf_flipflags(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 if (error)
960 goto out;
961
962 /*
963 * Dismantle the "old" attribute/value pair by removing
964 * a "remote" value (if it exists).
965 */
966 args->index = args->index2;
967 args->blkno = args->blkno2;
968 args->rmtblkno = args->rmtblkno2;
969 args->rmtblkcnt = args->rmtblkcnt2;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000970 args->rmtvaluelen = args->rmtvaluelen2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 if (args->rmtblkno) {
972 error = xfs_attr_rmtval_remove(args);
973 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000974 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 }
976
977 /*
978 * Re-find the "old" attribute entry after any split ops.
979 * The INCOMPLETE flag means that we will find the "old"
980 * attr, not the "new" one.
981 */
982 args->flags |= XFS_ATTR_INCOMPLETE;
983 state = xfs_da_state_alloc();
984 state->args = args;
985 state->mp = mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 state->inleaf = 0;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000987 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 if (error)
989 goto out;
990
991 /*
992 * Remove the name and update the hashvals in the tree.
993 */
994 blk = &state->path.blk[ state->path.active-1 ];
995 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner517c2222013-04-24 18:58:55 +1000996 error = xfs_attr3_leaf_remove(blk->bp, args);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000997 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
999 /*
1000 * Check to see if the tree needs to be collapsed.
1001 */
1002 if (retval && (state->path.active > 1)) {
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001003 xfs_defer_init(args->dfops, args->firstblock);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001004 error = xfs_da3_join(state);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001005 if (error)
1006 goto out_defer_cancel;
1007 xfs_defer_ijoin(args->dfops, dp);
1008 error = xfs_defer_finish(&args->trans, args->dfops);
1009 if (error)
1010 goto out_defer_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 }
1012
1013 /*
1014 * Commit and start the next trans in the chain.
1015 */
Christoph Hellwig411350d2017-08-28 10:21:03 -07001016 error = xfs_trans_roll_inode(&args->trans, dp);
Niv Sardi322ff6b2008-08-13 16:05:49 +10001017 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 goto out;
1019
1020 } else if (args->rmtblkno > 0) {
1021 /*
1022 * Added a "remote" value, just clear the incomplete flag.
1023 */
Dave Chinner517c2222013-04-24 18:58:55 +10001024 error = xfs_attr3_leaf_clearflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 if (error)
1026 goto out;
1027 }
1028 retval = error = 0;
1029
1030out:
1031 if (state)
1032 xfs_da_state_free(state);
1033 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001034 return error;
1035 return retval;
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001036out_defer_cancel:
1037 xfs_defer_cancel(args->dfops);
1038 args->trans = NULL;
1039 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040}
1041
1042/*
1043 * Remove a name from a B-tree attribute list.
1044 *
1045 * This will involve walking down the Btree, and may involve joining
1046 * leaf nodes and even joining intermediate nodes up to and including
1047 * the root node (a special case of an intermediate node).
1048 */
1049STATIC int
1050xfs_attr_node_removename(xfs_da_args_t *args)
1051{
1052 xfs_da_state_t *state;
1053 xfs_da_state_blk_t *blk;
1054 xfs_inode_t *dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001055 struct xfs_buf *bp;
Eric Sandeenf6106ef2016-01-11 11:34:01 +11001056 int retval, error, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Dave Chinner5a5881c2012-03-22 05:15:13 +00001058 trace_xfs_attr_node_removename(args);
1059
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 /*
1061 * Tie a string around our finger to remind us where we are.
1062 */
1063 dp = args->dp;
1064 state = xfs_da_state_alloc();
1065 state->args = args;
1066 state->mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
1068 /*
1069 * Search to see if name exists, and get back a pointer to it.
1070 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001071 error = xfs_da3_node_lookup_int(state, &retval);
Dave Chinner24513372014-06-25 14:58:08 +10001072 if (error || (retval != -EEXIST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 if (error == 0)
1074 error = retval;
1075 goto out;
1076 }
1077
1078 /*
1079 * If there is an out-of-line value, de-allocate the blocks.
1080 * This is done before we remove the attribute so that we don't
1081 * overflow the maximum size of a transaction and/or hit a deadlock.
1082 */
1083 blk = &state->path.blk[ state->path.active-1 ];
1084 ASSERT(blk->bp != NULL);
1085 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1086 if (args->rmtblkno > 0) {
1087 /*
1088 * Fill in disk block numbers in the state structure
1089 * so that we can get the buffers back after we commit
1090 * several transactions in the following calls.
1091 */
1092 error = xfs_attr_fillstate(state);
1093 if (error)
1094 goto out;
1095
1096 /*
1097 * Mark the attribute as INCOMPLETE, then bunmapi() the
1098 * remote value.
1099 */
Dave Chinner517c2222013-04-24 18:58:55 +10001100 error = xfs_attr3_leaf_setflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 if (error)
1102 goto out;
1103 error = xfs_attr_rmtval_remove(args);
1104 if (error)
1105 goto out;
1106
1107 /*
1108 * Refill the state structure with buffers, the prior calls
1109 * released our buffers.
1110 */
1111 error = xfs_attr_refillstate(state);
1112 if (error)
1113 goto out;
1114 }
1115
1116 /*
1117 * Remove the name and update the hashvals in the tree.
1118 */
1119 blk = &state->path.blk[ state->path.active-1 ];
1120 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner517c2222013-04-24 18:58:55 +10001121 retval = xfs_attr3_leaf_remove(blk->bp, args);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001122 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
1124 /*
1125 * Check to see if the tree needs to be collapsed.
1126 */
1127 if (retval && (state->path.active > 1)) {
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001128 xfs_defer_init(args->dfops, args->firstblock);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001129 error = xfs_da3_join(state);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001130 if (error)
1131 goto out_defer_cancel;
1132 xfs_defer_ijoin(args->dfops, dp);
1133 error = xfs_defer_finish(&args->trans, args->dfops);
1134 if (error)
1135 goto out_defer_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 /*
1137 * Commit the Btree join operation and start a new trans.
1138 */
Christoph Hellwig411350d2017-08-28 10:21:03 -07001139 error = xfs_trans_roll_inode(&args->trans, dp);
Niv Sardi322ff6b2008-08-13 16:05:49 +10001140 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 goto out;
1142 }
1143
1144 /*
1145 * If the result is small enough, push it all into the inode.
1146 */
1147 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
1148 /*
1149 * Have to get rid of the copy of this dabuf in the state.
1150 */
1151 ASSERT(state->path.active == 1);
1152 ASSERT(state->path.blk[0].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 state->path.blk[0].bp = NULL;
1154
Dave Chinner517c2222013-04-24 18:58:55 +10001155 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 if (error)
1157 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Nathan Scottd8cc8902005-11-02 10:34:53 +11001159 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001160 xfs_defer_init(args->dfops, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +10001161 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 /* bp is gone due to xfs_da_shrink_inode */
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001163 if (error)
1164 goto out_defer_cancel;
1165 xfs_defer_ijoin(args->dfops, dp);
1166 error = xfs_defer_finish(&args->trans, args->dfops);
1167 if (error)
1168 goto out_defer_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 } else
Dave Chinner1d9025e2012-06-22 18:50:14 +10001170 xfs_trans_brelse(args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 }
1172 error = 0;
1173
1174out:
1175 xfs_da_state_free(state);
Eric Sandeend99831f2014-06-22 15:03:54 +10001176 return error;
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001177out_defer_cancel:
1178 xfs_defer_cancel(args->dfops);
1179 args->trans = NULL;
1180 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181}
1182
1183/*
1184 * Fill in the disk block numbers in the state structure for the buffers
1185 * that are attached to the state structure.
1186 * This is done so that we can quickly reattach ourselves to those buffers
Nathan Scottc41564b2006-03-29 08:55:14 +10001187 * after some set of transaction commits have released these buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 */
1189STATIC int
1190xfs_attr_fillstate(xfs_da_state_t *state)
1191{
1192 xfs_da_state_path_t *path;
1193 xfs_da_state_blk_t *blk;
1194 int level;
1195
Dave Chinneree732592012-11-12 22:53:53 +11001196 trace_xfs_attr_fillstate(state->args);
1197
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 /*
1199 * Roll down the "path" in the state structure, storing the on-disk
1200 * block number for those buffers in the "path".
1201 */
1202 path = &state->path;
1203 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1204 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1205 if (blk->bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001206 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 blk->bp = NULL;
1208 } else {
1209 blk->disk_blkno = 0;
1210 }
1211 }
1212
1213 /*
1214 * Roll down the "altpath" in the state structure, storing the on-disk
1215 * block number for those buffers in the "altpath".
1216 */
1217 path = &state->altpath;
1218 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1219 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1220 if (blk->bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001221 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 blk->bp = NULL;
1223 } else {
1224 blk->disk_blkno = 0;
1225 }
1226 }
1227
Eric Sandeend99831f2014-06-22 15:03:54 +10001228 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229}
1230
1231/*
1232 * Reattach the buffers to the state structure based on the disk block
1233 * numbers stored in the state structure.
Nathan Scottc41564b2006-03-29 08:55:14 +10001234 * This is done after some set of transaction commits have released those
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 * buffers from our grip.
1236 */
1237STATIC int
1238xfs_attr_refillstate(xfs_da_state_t *state)
1239{
1240 xfs_da_state_path_t *path;
1241 xfs_da_state_blk_t *blk;
1242 int level, error;
1243
Dave Chinneree732592012-11-12 22:53:53 +11001244 trace_xfs_attr_refillstate(state->args);
1245
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 /*
1247 * Roll down the "path" in the state structure, storing the on-disk
1248 * block number for those buffers in the "path".
1249 */
1250 path = &state->path;
1251 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1252 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1253 if (blk->disk_blkno) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001254 error = xfs_da3_node_read(state->args->trans,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 state->args->dp,
1256 blk->blkno, blk->disk_blkno,
Dave Chinnerd9392a42012-11-12 22:54:17 +11001257 &blk->bp, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001259 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 } else {
1261 blk->bp = NULL;
1262 }
1263 }
1264
1265 /*
1266 * Roll down the "altpath" in the state structure, storing the on-disk
1267 * block number for those buffers in the "altpath".
1268 */
1269 path = &state->altpath;
1270 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1271 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1272 if (blk->disk_blkno) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001273 error = xfs_da3_node_read(state->args->trans,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 state->args->dp,
1275 blk->blkno, blk->disk_blkno,
Dave Chinnerd9392a42012-11-12 22:54:17 +11001276 &blk->bp, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001278 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 } else {
1280 blk->bp = NULL;
1281 }
1282 }
1283
Eric Sandeend99831f2014-06-22 15:03:54 +10001284 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285}
1286
1287/*
1288 * Look up a filename in a node attribute list.
1289 *
1290 * This routine gets called for any attribute fork that has more than one
1291 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1292 * "remote" values taking up more blocks.
1293 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001294STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295xfs_attr_node_get(xfs_da_args_t *args)
1296{
1297 xfs_da_state_t *state;
1298 xfs_da_state_blk_t *blk;
1299 int error, retval;
1300 int i;
1301
Dave Chinneree732592012-11-12 22:53:53 +11001302 trace_xfs_attr_node_get(args);
1303
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 state = xfs_da_state_alloc();
1305 state->args = args;
1306 state->mp = args->dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
1308 /*
1309 * Search to see if name exists, and get back a pointer to it.
1310 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001311 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 if (error) {
1313 retval = error;
Dave Chinner24513372014-06-25 14:58:08 +10001314 } else if (retval == -EEXIST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 blk = &state->path.blk[ state->path.active-1 ];
1316 ASSERT(blk->bp != NULL);
1317 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1318
1319 /*
1320 * Get the value, local or "remote"
1321 */
Dave Chinner517c2222013-04-24 18:58:55 +10001322 retval = xfs_attr3_leaf_getvalue(blk->bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 if (!retval && (args->rmtblkno > 0)
1324 && !(args->flags & ATTR_KERNOVAL)) {
1325 retval = xfs_attr_rmtval_get(args);
1326 }
1327 }
1328
1329 /*
1330 * If not in a transaction, we have to release all the buffers.
1331 */
1332 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001333 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 state->path.blk[i].bp = NULL;
1335 }
1336
1337 xfs_da_state_free(state);
Eric Sandeend99831f2014-06-22 15:03:54 +10001338 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339}