blob: 35a124400d60a11a468ec298335835a2666f087c [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;
Darrick J. Wong6e643cd2017-12-07 19:07:02 -0800215 struct xfs_buf *leaf_bp = NULL;
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000216 struct xfs_da_args args;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000217 struct xfs_defer_ops dfops;
Jie Liu3d3c8b52013-08-12 20:49:59 +1000218 struct xfs_trans_res tres;
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000219 xfs_fsblock_t firstblock;
Jie Liu3d3c8b52013-08-12 20:49:59 +1000220 int rsvd = (flags & ATTR_ROOT) != 0;
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100221 int error, err2, local;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100223 XFS_STATS_INC(mp, xs_attr_set);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000224
225 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
Dave Chinner24513372014-06-25 14:58:08 +1000226 return -EIO;
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000227
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000228 error = xfs_attr_args_init(&args, dp, name, flags);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000229 if (error)
230 return error;
231
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000232 args.value = value;
233 args.valuelen = valuelen;
234 args.firstblock = &firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000235 args.dfops = &dfops;
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000236 args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
Christoph Hellwig6c888af2014-05-13 16:40:19 +1000237 args.total = xfs_attr_calc_size(&args, &local);
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000238
Christoph Hellwig7d095252009-06-08 15:33:32 +0200239 error = xfs_qm_dqattach(dp, 0);
240 if (error)
241 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243 /*
244 * If the inode doesn't have an attribute fork, add one.
245 * (inode must not be locked when we call this routine)
246 */
247 if (XFS_IFORK_Q(dp) == 0) {
Barry Naujoke5889e92007-02-10 18:35:58 +1100248 int sf_size = sizeof(xfs_attr_sf_hdr_t) +
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000249 XFS_ATTR_SF_ENTSIZE_BYNAME(args.namelen, valuelen);
Barry Naujoke5889e92007-02-10 18:35:58 +1100250
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000251 error = xfs_bmap_add_attrfork(dp, sf_size, rsvd);
252 if (error)
253 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 }
255
Christoph Hellwig253f4912016-04-06 09:19:55 +1000256 tres.tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
257 M_RES(mp)->tr_attrsetrt.tr_logres * args.total;
258 tres.tr_logcount = XFS_ATTRSET_LOG_COUNT;
259 tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 /*
262 * Root fork attributes can use reserved data blocks for this
263 * operation if necessary
264 */
Christoph Hellwig253f4912016-04-06 09:19:55 +1000265 error = xfs_trans_alloc(mp, &tres, args.total, 0,
266 rsvd ? XFS_TRANS_RESERVE : 0, &args.trans);
267 if (error)
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000268 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Christoph Hellwig253f4912016-04-06 09:19:55 +1000270 xfs_ilock(dp, XFS_ILOCK_EXCL);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200271 error = xfs_trans_reserve_quota_nblks(args.trans, dp, args.total, 0,
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000272 rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
273 XFS_QMOPT_RES_REGBLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 if (error) {
275 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Christoph Hellwig4906e212015-06-04 13:47:56 +1000276 xfs_trans_cancel(args.trans);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000277 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 }
279
Christoph Hellwigddc34152011-09-19 15:00:54 +0000280 xfs_trans_ijoin(args.trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000283 * If the attribute list is non-existent or a shortform list,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 * upgrade it to a single-leaf-block attribute list.
285 */
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000286 if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL ||
287 (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
288 dp->i_d.di_anextents == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 /*
291 * Build initial attribute list (if required).
292 */
293 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
Nathan Scottd8cc8902005-11-02 10:34:53 +1100294 xfs_attr_shortform_create(&args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296 /*
297 * Try to add the attr to the attribute list in
298 * the inode.
299 */
300 error = xfs_attr_shortform_addname(&args);
Dave Chinner24513372014-06-25 14:58:08 +1000301 if (error != -ENOSPC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 /*
303 * Commit the shortform mods, and we're done.
304 * NOTE: this is also the error path (EEXIST, etc).
305 */
306 ASSERT(args.trans != NULL);
307
308 /*
309 * If this is a synchronous mount, make sure that
310 * the transaction goes to disk before returning
311 * to the user.
312 */
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000313 if (mp->m_flags & XFS_MOUNT_WSYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 xfs_trans_set_sync(args.trans);
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000315
316 if (!error && (flags & ATTR_KERNOTIME) == 0) {
317 xfs_trans_ichgtime(args.trans, dp,
318 XFS_ICHGTIME_CHG);
319 }
Christoph Hellwig70393312015-06-04 13:48:08 +1000320 err2 = xfs_trans_commit(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 xfs_iunlock(dp, XFS_ILOCK_EXCL);
322
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000323 return error ? error : err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 }
325
326 /*
327 * It won't fit in the shortform, transform to a leaf block.
328 * GROT: another possible req'mt for a double-split btree op.
329 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000330 xfs_defer_init(args.dfops, args.firstblock);
Darrick J. Wong6e643cd2017-12-07 19:07:02 -0800331 error = xfs_attr_shortform_to_leaf(&args, &leaf_bp);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700332 if (error)
333 goto out_defer_cancel;
Darrick J. Wong6e643cd2017-12-07 19:07:02 -0800334 /*
335 * Prevent the leaf buffer from being unlocked so that a
336 * concurrent AIL push cannot grab the half-baked leaf
337 * buffer and run into problems with the write verifier.
338 */
339 xfs_trans_bhold(args.trans, leaf_bp);
340 xfs_defer_bjoin(args.dfops, leaf_bp);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700341 xfs_defer_ijoin(args.dfops, dp);
342 error = xfs_defer_finish(&args.trans, args.dfops);
343 if (error)
344 goto out_defer_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 * Commit the leaf transformation. We'll need another (linked)
Darrick J. Wong6e643cd2017-12-07 19:07:02 -0800348 * transaction to add the new attribute to the leaf, which
349 * means that we have to hold & join the leaf buffer here too.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 */
Christoph Hellwig411350d2017-08-28 10:21:03 -0700351 error = xfs_trans_roll_inode(&args.trans, dp);
Niv Sardi322ff6b2008-08-13 16:05:49 +1000352 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 goto out;
Darrick J. Wong6e643cd2017-12-07 19:07:02 -0800354 xfs_trans_bjoin(args.trans, leaf_bp);
355 leaf_bp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 }
357
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000358 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 error = xfs_attr_leaf_addname(&args);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000360 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 error = xfs_attr_node_addname(&args);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000362 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 /*
366 * If this is a synchronous mount, make sure that the
367 * transaction goes to disk before returning to the user.
368 */
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000369 if (mp->m_flags & XFS_MOUNT_WSYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 xfs_trans_set_sync(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000372 if ((flags & ATTR_KERNOTIME) == 0)
373 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 /*
376 * Commit the last in the sequence of transactions.
377 */
378 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
Christoph Hellwig70393312015-06-04 13:48:08 +1000379 error = xfs_trans_commit(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 xfs_iunlock(dp, XFS_ILOCK_EXCL);
381
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000382 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700384out_defer_cancel:
385 xfs_defer_cancel(&dfops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386out:
Darrick J. Wong6e643cd2017-12-07 19:07:02 -0800387 if (leaf_bp)
388 xfs_trans_brelse(args.trans, leaf_bp);
Christoph Hellwig4906e212015-06-04 13:47:56 +1000389 if (args.trans)
390 xfs_trans_cancel(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000392 return error;
Nathan Scottaa82daa2005-11-02 10:33:33 +1100393}
394
395/*
396 * Generic handler routine to remove a name from an attribute list.
397 * Transitions attribute list from Btree to shortform as necessary.
398 */
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000399int
400xfs_attr_remove(
401 struct xfs_inode *dp,
402 const unsigned char *name,
403 int flags)
Nathan Scottaa82daa2005-11-02 10:33:33 +1100404{
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000405 struct xfs_mount *mp = dp->i_mount;
406 struct xfs_da_args args;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000407 struct xfs_defer_ops dfops;
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000408 xfs_fsblock_t firstblock;
409 int error;
Nathan Scottaa82daa2005-11-02 10:33:33 +1100410
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100411 XFS_STATS_INC(mp, xs_attr_remove);
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000412
413 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
Dave Chinner24513372014-06-25 14:58:08 +1000414 return -EIO;
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000415
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000416 error = xfs_attr_args_init(&args, dp, name, flags);
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000417 if (error)
418 return error;
419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 args.firstblock = &firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000421 args.dfops = &dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423 /*
Dave Chinner4a338212011-06-23 01:35:01 +0000424 * we have no control over the attribute names that userspace passes us
425 * to remove, so we have to allow the name lookup prior to attribute
426 * removal to fail.
427 */
428 args.op_flags = XFS_DA_OP_OKNOENT;
429
Christoph Hellwig7d095252009-06-08 15:33:32 +0200430 error = xfs_qm_dqattach(dp, 0);
431 if (error)
432 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 * Root fork attributes can use reserved data blocks for this
436 * operation if necessary
437 */
Christoph Hellwig253f4912016-04-06 09:19:55 +1000438 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_attrrm,
439 XFS_ATTRRM_SPACE_RES(mp), 0,
440 (flags & ATTR_ROOT) ? XFS_TRANS_RESERVE : 0,
441 &args.trans);
442 if (error)
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000443 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 xfs_ilock(dp, XFS_ILOCK_EXCL);
446 /*
447 * No need to make quota reservations here. We expect to release some
448 * blocks not allocate in the common case.
449 */
Christoph Hellwigddc34152011-09-19 15:00:54 +0000450 xfs_trans_ijoin(args.trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +1000452 if (!xfs_inode_hasattr(dp)) {
Dave Chinner24513372014-06-25 14:58:08 +1000453 error = -ENOATTR;
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000454 } else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
456 error = xfs_attr_shortform_remove(&args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
458 error = xfs_attr_leaf_removename(&args);
459 } else {
460 error = xfs_attr_node_removename(&args);
461 }
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000462
463 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 /*
467 * If this is a synchronous mount, make sure that the
468 * transaction goes to disk before returning to the user.
469 */
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000470 if (mp->m_flags & XFS_MOUNT_WSYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 xfs_trans_set_sync(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000473 if ((flags & ATTR_KERNOTIME) == 0)
474 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 /*
477 * Commit the last in the sequence of transactions.
478 */
479 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
Christoph Hellwig70393312015-06-04 13:48:08 +1000480 error = xfs_trans_commit(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 xfs_iunlock(dp, XFS_ILOCK_EXCL);
482
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000483 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485out:
Christoph Hellwig4906e212015-06-04 13:47:56 +1000486 if (args.trans)
487 xfs_trans_cancel(args.trans);
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000488 xfs_iunlock(dp, XFS_ILOCK_EXCL);
489 return error;
Nathan Scottaa82daa2005-11-02 10:33:33 +1100490}
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492/*========================================================================
493 * External routines when attribute list is inside the inode
494 *========================================================================*/
495
496/*
497 * Add a name to the shortform attribute list structure
498 * This is the external routine.
499 */
500STATIC int
501xfs_attr_shortform_addname(xfs_da_args_t *args)
502{
Nathan Scottd8cc8902005-11-02 10:34:53 +1100503 int newsize, forkoff, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Dave Chinner5a5881c2012-03-22 05:15:13 +0000505 trace_xfs_attr_sf_addname(args);
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 retval = xfs_attr_shortform_lookup(args);
Dave Chinner24513372014-06-25 14:58:08 +1000508 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
Eric Sandeend99831f2014-06-22 15:03:54 +1000509 return retval;
Dave Chinner24513372014-06-25 14:58:08 +1000510 } else if (retval == -EEXIST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if (args->flags & ATTR_CREATE)
Eric Sandeend99831f2014-06-22 15:03:54 +1000512 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 retval = xfs_attr_shortform_remove(args);
Darrick J. Wong7b384602018-04-17 19:10:15 -0700514 if (retval)
515 return retval;
516 /*
517 * Since we have removed the old attr, clear ATTR_REPLACE so
518 * that the leaf format add routine won't trip over the attr
519 * not being around.
520 */
521 args->flags &= ~ATTR_REPLACE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 }
523
Nathan Scottd8cc8902005-11-02 10:34:53 +1100524 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
525 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
Dave Chinner24513372014-06-25 14:58:08 +1000526 return -ENOSPC;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
529 newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100530
531 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
532 if (!forkoff)
Dave Chinner24513372014-06-25 14:58:08 +1000533 return -ENOSPC;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100534
535 xfs_attr_shortform_add(args, forkoff);
Eric Sandeend99831f2014-06-22 15:03:54 +1000536 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
539
540/*========================================================================
541 * External routines when attribute list is one block
542 *========================================================================*/
543
544/*
545 * Add a name to the leaf attribute list structure
546 *
547 * This leaf block cannot have a "remote" value, we only call this routine
548 * if bmap_one_block() says there is only one block (ie: no remote blks).
549 */
David Chinnera8272ce2007-11-23 16:28:09 +1100550STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551xfs_attr_leaf_addname(xfs_da_args_t *args)
552{
553 xfs_inode_t *dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000554 struct xfs_buf *bp;
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100555 int retval, error, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Dave Chinner5a5881c2012-03-22 05:15:13 +0000557 trace_xfs_attr_leaf_addname(args);
558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 /*
560 * Read the (only) block in the attribute list in.
561 */
562 dp = args->dp;
563 args->blkno = 0;
Dave Chinner517c2222013-04-24 18:58:55 +1000564 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100566 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568 /*
569 * Look up the given attribute in the leaf block. Figure out if
570 * the given flags produce an error or call for an atomic rename.
571 */
Dave Chinner517c2222013-04-24 18:58:55 +1000572 retval = xfs_attr3_leaf_lookup_int(bp, args);
Dave Chinner24513372014-06-25 14:58:08 +1000573 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000574 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000575 return retval;
Dave Chinner24513372014-06-25 14:58:08 +1000576 } else if (retval == -EEXIST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 if (args->flags & ATTR_CREATE) { /* pure create op */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000578 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000579 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 }
Dave Chinner5a5881c2012-03-22 05:15:13 +0000581
582 trace_xfs_attr_leaf_replace(args);
583
Dave Chinner8275cdd2014-05-06 07:37:31 +1000584 /* save the attribute state for later removal*/
Barry Naujok6a178102008-05-21 16:42:05 +1000585 args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 args->blkno2 = args->blkno; /* set 2nd entry info*/
587 args->index2 = args->index;
588 args->rmtblkno2 = args->rmtblkno;
589 args->rmtblkcnt2 = args->rmtblkcnt;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000590 args->rmtvaluelen2 = args->rmtvaluelen;
591
592 /*
593 * clear the remote attr state now that it is saved so that the
594 * values reflect the state of the attribute we are about to
595 * add, not the attribute we just found and will remove later.
596 */
597 args->rmtblkno = 0;
598 args->rmtblkcnt = 0;
599 args->rmtvaluelen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 }
601
602 /*
603 * Add the attribute to the leaf block, transitioning to a Btree
604 * if required.
605 */
Dave Chinner517c2222013-04-24 18:58:55 +1000606 retval = xfs_attr3_leaf_add(bp, args);
Dave Chinner24513372014-06-25 14:58:08 +1000607 if (retval == -ENOSPC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 /*
609 * Promote the attribute list to the Btree format, then
610 * Commit that transaction so that the node_addname() call
611 * can manage its own transactions.
612 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000613 xfs_defer_init(args->dfops, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000614 error = xfs_attr3_leaf_to_node(args);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700615 if (error)
616 goto out_defer_cancel;
617 xfs_defer_ijoin(args->dfops, dp);
618 error = xfs_defer_finish(&args->trans, args->dfops);
619 if (error)
620 goto out_defer_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 * Commit the current trans (including the inode) and start
624 * a new one.
625 */
Christoph Hellwig411350d2017-08-28 10:21:03 -0700626 error = xfs_trans_roll_inode(&args->trans, dp);
Niv Sardi322ff6b2008-08-13 16:05:49 +1000627 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000628 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
630 /*
631 * Fob the whole rest of the problem off on the Btree code.
632 */
633 error = xfs_attr_node_addname(args);
Eric Sandeend99831f2014-06-22 15:03:54 +1000634 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 }
636
637 /*
638 * Commit the transaction that added the attr name so that
639 * later routines can manage their own transactions.
640 */
Christoph Hellwig411350d2017-08-28 10:21:03 -0700641 error = xfs_trans_roll_inode(&args->trans, dp);
Niv Sardi322ff6b2008-08-13 16:05:49 +1000642 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000643 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645 /*
646 * If there was an out-of-line value, allocate the blocks we
647 * identified for its storage and copy the value. This is done
648 * after we create the attribute so that we don't overflow the
649 * maximum size of a transaction and/or hit a deadlock.
650 */
651 if (args->rmtblkno > 0) {
652 error = xfs_attr_rmtval_set(args);
653 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000654 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 }
656
657 /*
658 * If this is an atomic rename operation, we must "flip" the
659 * incomplete flags on the "new" and "old" attribute/value pairs
660 * so that one disappears and one appears atomically. Then we
661 * must remove the "old" attribute/value pair.
662 */
Barry Naujok6a178102008-05-21 16:42:05 +1000663 if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 /*
665 * In a separate transaction, set the incomplete flag on the
666 * "old" attr and clear the incomplete flag on the "new" attr.
667 */
Dave Chinner517c2222013-04-24 18:58:55 +1000668 error = xfs_attr3_leaf_flipflags(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000670 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
672 /*
673 * Dismantle the "old" attribute/value pair by removing
674 * a "remote" value (if it exists).
675 */
676 args->index = args->index2;
677 args->blkno = args->blkno2;
678 args->rmtblkno = args->rmtblkno2;
679 args->rmtblkcnt = args->rmtblkcnt2;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000680 args->rmtvaluelen = args->rmtvaluelen2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 if (args->rmtblkno) {
682 error = xfs_attr_rmtval_remove(args);
683 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000684 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 }
686
687 /*
688 * Read in the block containing the "old" attr, then
689 * remove the "old" attr from that block (neat, huh!)
690 */
Dave Chinner517c2222013-04-24 18:58:55 +1000691 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
Dave Chinnerad14c332012-11-12 22:54:16 +1100692 -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100694 return error;
695
Dave Chinner517c2222013-04-24 18:58:55 +1000696 xfs_attr3_leaf_remove(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 /*
699 * If the result is small enough, shrink it all into the inode.
700 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100701 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000702 xfs_defer_init(args->dfops, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000703 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 /* bp is gone due to xfs_da_shrink_inode */
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700705 if (error)
706 goto out_defer_cancel;
707 xfs_defer_ijoin(args->dfops, dp);
708 error = xfs_defer_finish(&args->trans, args->dfops);
709 if (error)
710 goto out_defer_cancel;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000711 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
713 /*
714 * Commit the remove and start the next trans in series.
715 */
Christoph Hellwig411350d2017-08-28 10:21:03 -0700716 error = xfs_trans_roll_inode(&args->trans, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718 } else if (args->rmtblkno > 0) {
719 /*
720 * Added a "remote" value, just clear the incomplete flag.
721 */
Dave Chinner517c2222013-04-24 18:58:55 +1000722 error = xfs_attr3_leaf_clearflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 }
Dave Chinner517c2222013-04-24 18:58:55 +1000724 return error;
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700725out_defer_cancel:
726 xfs_defer_cancel(args->dfops);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700727 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728}
729
730/*
731 * Remove a name from the leaf attribute list structure
732 *
733 * This leaf block cannot have a "remote" value, we only call this routine
734 * if bmap_one_block() says there is only one block (ie: no remote blks).
735 */
736STATIC int
737xfs_attr_leaf_removename(xfs_da_args_t *args)
738{
739 xfs_inode_t *dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000740 struct xfs_buf *bp;
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100741 int error, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Dave Chinner5a5881c2012-03-22 05:15:13 +0000743 trace_xfs_attr_leaf_removename(args);
744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 /*
746 * Remove the attribute.
747 */
748 dp = args->dp;
749 args->blkno = 0;
Dave Chinner517c2222013-04-24 18:58:55 +1000750 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Dave Chinnerad14c332012-11-12 22:54:16 +1100751 if (error)
752 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Dave Chinner517c2222013-04-24 18:58:55 +1000754 error = xfs_attr3_leaf_lookup_int(bp, args);
Dave Chinner24513372014-06-25 14:58:08 +1000755 if (error == -ENOATTR) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000756 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000757 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 }
759
Dave Chinner517c2222013-04-24 18:58:55 +1000760 xfs_attr3_leaf_remove(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762 /*
763 * If the result is small enough, shrink it all into the inode.
764 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100765 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000766 xfs_defer_init(args->dfops, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000767 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 /* bp is gone due to xfs_da_shrink_inode */
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700769 if (error)
770 goto out_defer_cancel;
771 xfs_defer_ijoin(args->dfops, dp);
772 error = xfs_defer_finish(&args->trans, args->dfops);
773 if (error)
774 goto out_defer_cancel;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000775 }
Dave Chinner517c2222013-04-24 18:58:55 +1000776 return 0;
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700777out_defer_cancel:
778 xfs_defer_cancel(args->dfops);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700779 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780}
781
782/*
783 * Look up a name in a leaf attribute list structure.
784 *
785 * This leaf block cannot have a "remote" value, we only call this routine
786 * if bmap_one_block() says there is only one block (ie: no remote blks).
787 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000788STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789xfs_attr_leaf_get(xfs_da_args_t *args)
790{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000791 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 int error;
793
Dave Chinneree732592012-11-12 22:53:53 +1100794 trace_xfs_attr_leaf_get(args);
795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 args->blkno = 0;
Dave Chinner517c2222013-04-24 18:58:55 +1000797 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100799 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Dave Chinner517c2222013-04-24 18:58:55 +1000801 error = xfs_attr3_leaf_lookup_int(bp, args);
Dave Chinner24513372014-06-25 14:58:08 +1000802 if (error != -EEXIST) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000803 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000804 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 }
Dave Chinner517c2222013-04-24 18:58:55 +1000806 error = xfs_attr3_leaf_getvalue(bp, args);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000807 xfs_trans_brelse(args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
809 error = xfs_attr_rmtval_get(args);
810 }
Dave Chinner517c2222013-04-24 18:58:55 +1000811 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812}
813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814/*========================================================================
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000815 * External routines when attribute list size > geo->blksize
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 *========================================================================*/
817
818/*
819 * Add a name to a Btree-format attribute list.
820 *
821 * This will involve walking down the Btree, and may involve splitting
822 * leaf nodes and even splitting intermediate nodes up to and including
823 * the root node (a special case of an intermediate node).
824 *
825 * "Remote" attribute values confuse the issue and atomic rename operations
826 * add a whole extra layer of confusion on top of that.
827 */
828STATIC int
829xfs_attr_node_addname(xfs_da_args_t *args)
830{
831 xfs_da_state_t *state;
832 xfs_da_state_blk_t *blk;
833 xfs_inode_t *dp;
834 xfs_mount_t *mp;
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100835 int retval, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Dave Chinner5a5881c2012-03-22 05:15:13 +0000837 trace_xfs_attr_node_addname(args);
838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 /*
840 * Fill in bucket of arguments/results/context to carry around.
841 */
842 dp = args->dp;
843 mp = dp->i_mount;
844restart:
845 state = xfs_da_state_alloc();
846 state->args = args;
847 state->mp = mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 /*
850 * Search to see if name already exists, and get back a pointer
851 * to where it should go.
852 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000853 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 if (error)
855 goto out;
856 blk = &state->path.blk[ state->path.active-1 ];
857 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner24513372014-06-25 14:58:08 +1000858 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 goto out;
Dave Chinner24513372014-06-25 14:58:08 +1000860 } else if (retval == -EEXIST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 if (args->flags & ATTR_CREATE)
862 goto out;
Dave Chinner5a5881c2012-03-22 05:15:13 +0000863
864 trace_xfs_attr_node_replace(args);
865
Dave Chinner8275cdd2014-05-06 07:37:31 +1000866 /* save the attribute state for later removal*/
Barry Naujok6a178102008-05-21 16:42:05 +1000867 args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 args->blkno2 = args->blkno; /* set 2nd entry info*/
869 args->index2 = args->index;
870 args->rmtblkno2 = args->rmtblkno;
871 args->rmtblkcnt2 = args->rmtblkcnt;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000872 args->rmtvaluelen2 = args->rmtvaluelen;
873
874 /*
875 * clear the remote attr state now that it is saved so that the
876 * values reflect the state of the attribute we are about to
877 * add, not the attribute we just found and will remove later.
878 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 args->rmtblkno = 0;
880 args->rmtblkcnt = 0;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000881 args->rmtvaluelen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 }
883
Dave Chinner517c2222013-04-24 18:58:55 +1000884 retval = xfs_attr3_leaf_add(blk->bp, state->args);
Dave Chinner24513372014-06-25 14:58:08 +1000885 if (retval == -ENOSPC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 if (state->path.active == 1) {
887 /*
888 * Its really a single leaf node, but it had
889 * out-of-line values so it looked like it *might*
890 * have been a b-tree.
891 */
892 xfs_da_state_free(state);
Eric Sandeen6dd93e92013-07-31 20:18:54 -0500893 state = NULL;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000894 xfs_defer_init(args->dfops, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000895 error = xfs_attr3_leaf_to_node(args);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700896 if (error)
897 goto out_defer_cancel;
898 xfs_defer_ijoin(args->dfops, dp);
899 error = xfs_defer_finish(&args->trans, args->dfops);
900 if (error)
901 goto out_defer_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 * Commit the node conversion and start the next
905 * trans in the chain.
906 */
Christoph Hellwig411350d2017-08-28 10:21:03 -0700907 error = xfs_trans_roll_inode(&args->trans, dp);
Niv Sardi322ff6b2008-08-13 16:05:49 +1000908 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 goto out;
910
911 goto restart;
912 }
913
914 /*
915 * Split as many Btree elements as required.
916 * This code tracks the new and old attr's location
917 * in the index/blkno/rmtblkno/rmtblkcnt fields and
918 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
919 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000920 xfs_defer_init(args->dfops, args->firstblock);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000921 error = xfs_da3_split(state);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700922 if (error)
923 goto out_defer_cancel;
924 xfs_defer_ijoin(args->dfops, dp);
925 error = xfs_defer_finish(&args->trans, args->dfops);
926 if (error)
927 goto out_defer_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 } else {
929 /*
930 * Addition succeeded, update Btree hashvals.
931 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000932 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 }
934
935 /*
936 * Kill the state structure, we're done with it and need to
937 * allow the buffers to come back later.
938 */
939 xfs_da_state_free(state);
940 state = NULL;
941
942 /*
943 * Commit the leaf addition or btree split and start the next
944 * trans in the chain.
945 */
Christoph Hellwig411350d2017-08-28 10:21:03 -0700946 error = xfs_trans_roll_inode(&args->trans, dp);
Niv Sardi322ff6b2008-08-13 16:05:49 +1000947 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 goto out;
949
950 /*
951 * If there was an out-of-line value, allocate the blocks we
952 * identified for its storage and copy the value. This is done
953 * after we create the attribute so that we don't overflow the
954 * maximum size of a transaction and/or hit a deadlock.
955 */
956 if (args->rmtblkno > 0) {
957 error = xfs_attr_rmtval_set(args);
958 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000959 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 }
961
962 /*
963 * If this is an atomic rename operation, we must "flip" the
964 * incomplete flags on the "new" and "old" attribute/value pairs
965 * so that one disappears and one appears atomically. Then we
966 * must remove the "old" attribute/value pair.
967 */
Barry Naujok6a178102008-05-21 16:42:05 +1000968 if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 /*
970 * In a separate transaction, set the incomplete flag on the
971 * "old" attr and clear the incomplete flag on the "new" attr.
972 */
Dave Chinner517c2222013-04-24 18:58:55 +1000973 error = xfs_attr3_leaf_flipflags(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 if (error)
975 goto out;
976
977 /*
978 * Dismantle the "old" attribute/value pair by removing
979 * a "remote" value (if it exists).
980 */
981 args->index = args->index2;
982 args->blkno = args->blkno2;
983 args->rmtblkno = args->rmtblkno2;
984 args->rmtblkcnt = args->rmtblkcnt2;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000985 args->rmtvaluelen = args->rmtvaluelen2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 if (args->rmtblkno) {
987 error = xfs_attr_rmtval_remove(args);
988 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000989 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 }
991
992 /*
993 * Re-find the "old" attribute entry after any split ops.
994 * The INCOMPLETE flag means that we will find the "old"
995 * attr, not the "new" one.
996 */
997 args->flags |= XFS_ATTR_INCOMPLETE;
998 state = xfs_da_state_alloc();
999 state->args = args;
1000 state->mp = mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 state->inleaf = 0;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001002 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 if (error)
1004 goto out;
1005
1006 /*
1007 * Remove the name and update the hashvals in the tree.
1008 */
1009 blk = &state->path.blk[ state->path.active-1 ];
1010 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner517c2222013-04-24 18:58:55 +10001011 error = xfs_attr3_leaf_remove(blk->bp, args);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001012 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
1014 /*
1015 * Check to see if the tree needs to be collapsed.
1016 */
1017 if (retval && (state->path.active > 1)) {
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001018 xfs_defer_init(args->dfops, args->firstblock);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001019 error = xfs_da3_join(state);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001020 if (error)
1021 goto out_defer_cancel;
1022 xfs_defer_ijoin(args->dfops, dp);
1023 error = xfs_defer_finish(&args->trans, args->dfops);
1024 if (error)
1025 goto out_defer_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 }
1027
1028 /*
1029 * Commit and start the next trans in the chain.
1030 */
Christoph Hellwig411350d2017-08-28 10:21:03 -07001031 error = xfs_trans_roll_inode(&args->trans, dp);
Niv Sardi322ff6b2008-08-13 16:05:49 +10001032 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 goto out;
1034
1035 } else if (args->rmtblkno > 0) {
1036 /*
1037 * Added a "remote" value, just clear the incomplete flag.
1038 */
Dave Chinner517c2222013-04-24 18:58:55 +10001039 error = xfs_attr3_leaf_clearflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 if (error)
1041 goto out;
1042 }
1043 retval = error = 0;
1044
1045out:
1046 if (state)
1047 xfs_da_state_free(state);
1048 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001049 return error;
1050 return retval;
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001051out_defer_cancel:
1052 xfs_defer_cancel(args->dfops);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001053 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054}
1055
1056/*
1057 * Remove a name from a B-tree attribute list.
1058 *
1059 * This will involve walking down the Btree, and may involve joining
1060 * leaf nodes and even joining intermediate nodes up to and including
1061 * the root node (a special case of an intermediate node).
1062 */
1063STATIC int
1064xfs_attr_node_removename(xfs_da_args_t *args)
1065{
1066 xfs_da_state_t *state;
1067 xfs_da_state_blk_t *blk;
1068 xfs_inode_t *dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001069 struct xfs_buf *bp;
Eric Sandeenf6106ef2016-01-11 11:34:01 +11001070 int retval, error, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
Dave Chinner5a5881c2012-03-22 05:15:13 +00001072 trace_xfs_attr_node_removename(args);
1073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 /*
1075 * Tie a string around our finger to remind us where we are.
1076 */
1077 dp = args->dp;
1078 state = xfs_da_state_alloc();
1079 state->args = args;
1080 state->mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082 /*
1083 * Search to see if name exists, and get back a pointer to it.
1084 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001085 error = xfs_da3_node_lookup_int(state, &retval);
Dave Chinner24513372014-06-25 14:58:08 +10001086 if (error || (retval != -EEXIST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 if (error == 0)
1088 error = retval;
1089 goto out;
1090 }
1091
1092 /*
1093 * If there is an out-of-line value, de-allocate the blocks.
1094 * This is done before we remove the attribute so that we don't
1095 * overflow the maximum size of a transaction and/or hit a deadlock.
1096 */
1097 blk = &state->path.blk[ state->path.active-1 ];
1098 ASSERT(blk->bp != NULL);
1099 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1100 if (args->rmtblkno > 0) {
1101 /*
1102 * Fill in disk block numbers in the state structure
1103 * so that we can get the buffers back after we commit
1104 * several transactions in the following calls.
1105 */
1106 error = xfs_attr_fillstate(state);
1107 if (error)
1108 goto out;
1109
1110 /*
1111 * Mark the attribute as INCOMPLETE, then bunmapi() the
1112 * remote value.
1113 */
Dave Chinner517c2222013-04-24 18:58:55 +10001114 error = xfs_attr3_leaf_setflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 if (error)
1116 goto out;
1117 error = xfs_attr_rmtval_remove(args);
1118 if (error)
1119 goto out;
1120
1121 /*
1122 * Refill the state structure with buffers, the prior calls
1123 * released our buffers.
1124 */
1125 error = xfs_attr_refillstate(state);
1126 if (error)
1127 goto out;
1128 }
1129
1130 /*
1131 * Remove the name and update the hashvals in the tree.
1132 */
1133 blk = &state->path.blk[ state->path.active-1 ];
1134 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner517c2222013-04-24 18:58:55 +10001135 retval = xfs_attr3_leaf_remove(blk->bp, args);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001136 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
1138 /*
1139 * Check to see if the tree needs to be collapsed.
1140 */
1141 if (retval && (state->path.active > 1)) {
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001142 xfs_defer_init(args->dfops, args->firstblock);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001143 error = xfs_da3_join(state);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001144 if (error)
1145 goto out_defer_cancel;
1146 xfs_defer_ijoin(args->dfops, dp);
1147 error = xfs_defer_finish(&args->trans, args->dfops);
1148 if (error)
1149 goto out_defer_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 /*
1151 * Commit the Btree join operation and start a new trans.
1152 */
Christoph Hellwig411350d2017-08-28 10:21:03 -07001153 error = xfs_trans_roll_inode(&args->trans, dp);
Niv Sardi322ff6b2008-08-13 16:05:49 +10001154 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 goto out;
1156 }
1157
1158 /*
1159 * If the result is small enough, push it all into the inode.
1160 */
1161 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
1162 /*
1163 * Have to get rid of the copy of this dabuf in the state.
1164 */
1165 ASSERT(state->path.active == 1);
1166 ASSERT(state->path.blk[0].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 state->path.blk[0].bp = NULL;
1168
Dave Chinner517c2222013-04-24 18:58:55 +10001169 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 if (error)
1171 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Nathan Scottd8cc8902005-11-02 10:34:53 +11001173 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001174 xfs_defer_init(args->dfops, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +10001175 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 /* bp is gone due to xfs_da_shrink_inode */
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001177 if (error)
1178 goto out_defer_cancel;
1179 xfs_defer_ijoin(args->dfops, dp);
1180 error = xfs_defer_finish(&args->trans, args->dfops);
1181 if (error)
1182 goto out_defer_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 } else
Dave Chinner1d9025e2012-06-22 18:50:14 +10001184 xfs_trans_brelse(args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 }
1186 error = 0;
1187
1188out:
1189 xfs_da_state_free(state);
Eric Sandeend99831f2014-06-22 15:03:54 +10001190 return error;
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001191out_defer_cancel:
1192 xfs_defer_cancel(args->dfops);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001193 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194}
1195
1196/*
1197 * Fill in the disk block numbers in the state structure for the buffers
1198 * that are attached to the state structure.
1199 * This is done so that we can quickly reattach ourselves to those buffers
Nathan Scottc41564b2006-03-29 08:55:14 +10001200 * after some set of transaction commits have released these buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 */
1202STATIC int
1203xfs_attr_fillstate(xfs_da_state_t *state)
1204{
1205 xfs_da_state_path_t *path;
1206 xfs_da_state_blk_t *blk;
1207 int level;
1208
Dave Chinneree732592012-11-12 22:53:53 +11001209 trace_xfs_attr_fillstate(state->args);
1210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 /*
1212 * Roll down the "path" in the state structure, storing the on-disk
1213 * block number for those buffers in the "path".
1214 */
1215 path = &state->path;
1216 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1217 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1218 if (blk->bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001219 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 blk->bp = NULL;
1221 } else {
1222 blk->disk_blkno = 0;
1223 }
1224 }
1225
1226 /*
1227 * Roll down the "altpath" in the state structure, storing the on-disk
1228 * block number for those buffers in the "altpath".
1229 */
1230 path = &state->altpath;
1231 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1232 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1233 if (blk->bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001234 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 blk->bp = NULL;
1236 } else {
1237 blk->disk_blkno = 0;
1238 }
1239 }
1240
Eric Sandeend99831f2014-06-22 15:03:54 +10001241 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242}
1243
1244/*
1245 * Reattach the buffers to the state structure based on the disk block
1246 * numbers stored in the state structure.
Nathan Scottc41564b2006-03-29 08:55:14 +10001247 * This is done after some set of transaction commits have released those
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 * buffers from our grip.
1249 */
1250STATIC int
1251xfs_attr_refillstate(xfs_da_state_t *state)
1252{
1253 xfs_da_state_path_t *path;
1254 xfs_da_state_blk_t *blk;
1255 int level, error;
1256
Dave Chinneree732592012-11-12 22:53:53 +11001257 trace_xfs_attr_refillstate(state->args);
1258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 /*
1260 * Roll down the "path" in the state structure, storing the on-disk
1261 * block number for those buffers in the "path".
1262 */
1263 path = &state->path;
1264 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1265 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1266 if (blk->disk_blkno) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001267 error = xfs_da3_node_read(state->args->trans,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 state->args->dp,
1269 blk->blkno, blk->disk_blkno,
Dave Chinnerd9392a42012-11-12 22:54:17 +11001270 &blk->bp, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001272 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 } else {
1274 blk->bp = NULL;
1275 }
1276 }
1277
1278 /*
1279 * Roll down the "altpath" in the state structure, storing the on-disk
1280 * block number for those buffers in the "altpath".
1281 */
1282 path = &state->altpath;
1283 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1284 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1285 if (blk->disk_blkno) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001286 error = xfs_da3_node_read(state->args->trans,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 state->args->dp,
1288 blk->blkno, blk->disk_blkno,
Dave Chinnerd9392a42012-11-12 22:54:17 +11001289 &blk->bp, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001291 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 } else {
1293 blk->bp = NULL;
1294 }
1295 }
1296
Eric Sandeend99831f2014-06-22 15:03:54 +10001297 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298}
1299
1300/*
1301 * Look up a filename in a node attribute list.
1302 *
1303 * This routine gets called for any attribute fork that has more than one
1304 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1305 * "remote" values taking up more blocks.
1306 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001307STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308xfs_attr_node_get(xfs_da_args_t *args)
1309{
1310 xfs_da_state_t *state;
1311 xfs_da_state_blk_t *blk;
1312 int error, retval;
1313 int i;
1314
Dave Chinneree732592012-11-12 22:53:53 +11001315 trace_xfs_attr_node_get(args);
1316
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 state = xfs_da_state_alloc();
1318 state->args = args;
1319 state->mp = args->dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
1321 /*
1322 * Search to see if name exists, and get back a pointer to it.
1323 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001324 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 if (error) {
1326 retval = error;
Dave Chinner24513372014-06-25 14:58:08 +10001327 } else if (retval == -EEXIST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 blk = &state->path.blk[ state->path.active-1 ];
1329 ASSERT(blk->bp != NULL);
1330 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1331
1332 /*
1333 * Get the value, local or "remote"
1334 */
Dave Chinner517c2222013-04-24 18:58:55 +10001335 retval = xfs_attr3_leaf_getvalue(blk->bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 if (!retval && (args->rmtblkno > 0)
1337 && !(args->flags & ATTR_KERNOVAL)) {
1338 retval = xfs_attr_rmtval_get(args);
1339 }
1340 }
1341
1342 /*
1343 * If not in a transaction, we have to release all the buffers.
1344 */
1345 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001346 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 state->path.blk[i].bp = NULL;
1348 }
1349
1350 xfs_da_state_free(state);
Eric Sandeend99831f2014-06-22 15:03:54 +10001351 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352}