blob: 126da7fc5dedaab11cc529814027243966c4927a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Dave Chinner70a9883c2013-10-23 10:36:05 +110020#include "xfs_shared.h"
Dave Chinner239880e2013-10-23 10:50:10 +110021#include "xfs_format.h"
22#include "xfs_log_format.h"
23#include "xfs_trans_resv.h"
Nathan Scotta844f452005-11-02 14:38:42 +110024#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_mount.h"
Dave Chinner57062782013-10-15 09:17:51 +110026#include "xfs_da_format.h"
Nathan Scotta844f452005-11-02 14:38:42 +110027#include "xfs_da_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110028#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110030#include "xfs_alloc.h"
Dave Chinner239880e2013-10-23 10:50:10 +110031#include "xfs_trans.h"
Nathan Scotta844f452005-11-02 14:38:42 +110032#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_bmap.h"
Dave Chinner68988112013-08-12 20:49:42 +100034#include "xfs_bmap_util.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110035#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_attr.h"
37#include "xfs_attr_leaf.h"
Dave Chinner95920cd2013-04-03 16:11:27 +110038#include "xfs_attr_remote.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include "xfs_quota.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include "xfs_trans_space.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000042#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44/*
45 * xfs_attr.c
46 *
47 * Provide the external interfaces to manage attribute lists.
48 */
49
50/*========================================================================
51 * Function prototypes for the kernel.
52 *========================================================================*/
53
54/*
55 * Internal routines when attribute list fits inside the inode.
56 */
57STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
58
59/*
60 * Internal routines when attribute list is one block.
61 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100062STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063STATIC int xfs_attr_leaf_addname(xfs_da_args_t *args);
64STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66/*
67 * Internal routines when attribute list is more than one block.
68 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100069STATIC int xfs_attr_node_get(xfs_da_args_t *args);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070STATIC int xfs_attr_node_addname(xfs_da_args_t *args);
71STATIC int xfs_attr_node_removename(xfs_da_args_t *args);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
73STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Barry Naujoke8b0eba2008-04-22 17:34:31 +100076STATIC int
Christoph Hellwig67fd7182014-05-13 16:34:43 +100077xfs_attr_args_init(
78 struct xfs_da_args *args,
79 struct xfs_inode *dp,
80 const unsigned char *name,
81 int flags)
Barry Naujoke8b0eba2008-04-22 17:34:31 +100082{
Christoph Hellwig67fd7182014-05-13 16:34:43 +100083
84 if (!name)
Dave Chinner24513372014-06-25 14:58:08 +100085 return -EINVAL;
Christoph Hellwig67fd7182014-05-13 16:34:43 +100086
87 memset(args, 0, sizeof(*args));
Dave Chinner0650b552014-06-06 15:01:58 +100088 args->geo = dp->i_mount->m_attr_geo;
Christoph Hellwig67fd7182014-05-13 16:34:43 +100089 args->whichfork = XFS_ATTR_FORK;
90 args->dp = dp;
91 args->flags = flags;
92 args->name = name;
93 args->namelen = strlen((const char *)name);
94 if (args->namelen >= MAXNAMELEN)
Dave Chinner24513372014-06-25 14:58:08 +100095 return -EFAULT; /* match IRIX behaviour */
Barry Naujoke8b0eba2008-04-22 17:34:31 +100096
Christoph Hellwig67fd7182014-05-13 16:34:43 +100097 args->hashval = xfs_da_hashname(args->name, args->namelen);
Barry Naujoke8b0eba2008-04-22 17:34:31 +100098 return 0;
99}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Dave Chinnerabec5f22013-08-12 20:49:38 +1000101int
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +1000102xfs_inode_hasattr(
103 struct xfs_inode *ip)
104{
105 if (!XFS_IFORK_Q(ip) ||
106 (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
107 ip->i_d.di_anextents == 0))
108 return 0;
109 return 1;
110}
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112/*========================================================================
113 * Overall external interface routines.
114 *========================================================================*/
115
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000116int
117xfs_attr_get(
Christoph Hellwige82fa0c2009-11-14 16:17:20 +0000118 struct xfs_inode *ip,
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000119 const unsigned char *name,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100120 unsigned char *value,
Christoph Hellwige82fa0c2009-11-14 16:17:20 +0000121 int *valuelenp,
122 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000124 struct xfs_da_args args;
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000125 uint lock_mode;
126 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 XFS_STATS_INC(xs_attr_get);
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
Dave Chinner24513372014-06-25 14:58:08 +1000131 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 if (!xfs_inode_hasattr(ip))
Dave Chinner24513372014-06-25 14:58:08 +1000134 return -ENOATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000136 error = xfs_attr_args_init(&args, ip, name, flags);
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000137 if (error)
138 return error;
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 args.value = value;
141 args.valuelen = *valuelenp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Christoph Hellwig683cb942013-12-06 12:30:15 -0800143 lock_mode = xfs_ilock_attr_map_shared(ip);
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000144 if (!xfs_inode_hasattr(ip))
Dave Chinner24513372014-06-25 14:58:08 +1000145 error = -ENOATTR;
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000146 else if (ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL)
147 error = xfs_attr_shortform_getvalue(&args);
148 else if (xfs_bmap_one_block(ip, XFS_ATTR_FORK))
149 error = xfs_attr_leaf_get(&args);
150 else
151 error = xfs_attr_node_get(&args);
Christoph Hellwig683cb942013-12-06 12:30:15 -0800152 xfs_iunlock(ip, lock_mode);
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000153
154 *valuelenp = args.valuelen;
Dave Chinner24513372014-06-25 14:58:08 +1000155 return error == -EEXIST ? 0 : error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000158/*
159 * Calculate how many blocks we need for the new attribute,
160 */
Eric Sandeen5d77c0d2009-11-19 15:52:00 +0000161STATIC int
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000162xfs_attr_calc_size(
Christoph Hellwig6c888af2014-05-13 16:40:19 +1000163 struct xfs_da_args *args,
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000164 int *local)
165{
Christoph Hellwig6c888af2014-05-13 16:40:19 +1000166 struct xfs_mount *mp = args->dp->i_mount;
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000167 int size;
168 int nblks;
169
170 /*
171 * Determine space new attribute will use, and if it would be
172 * "local" or "remote" (note: local != inline).
173 */
Dave Chinnerc59f0ad2014-06-06 15:21:27 +1000174 size = xfs_attr_leaf_newentsize(args, local);
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000175 nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
176 if (*local) {
Dave Chinner33a60392014-06-06 15:21:10 +1000177 if (size > (args->geo->blksize / 2)) {
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000178 /* Double split possible */
179 nblks *= 2;
180 }
181 } else {
182 /*
183 * Out of line attribute, cannot double split, but
184 * make room for the attribute value itself.
185 */
Dave Chinner2d6dcc62014-05-15 09:39:28 +1000186 uint dblocks = xfs_attr3_rmt_blocks(mp, args->valuelen);
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000187 nblks += dblocks;
188 nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
189 }
190
191 return nblks;
192}
193
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000194int
195xfs_attr_set(
196 struct xfs_inode *dp,
197 const unsigned char *name,
198 unsigned char *value,
199 int valuelen,
200 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Jie Liu3d3c8b52013-08-12 20:49:59 +1000202 struct xfs_mount *mp = dp->i_mount;
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000203 struct xfs_da_args args;
204 struct xfs_bmap_free flist;
Jie Liu3d3c8b52013-08-12 20:49:59 +1000205 struct xfs_trans_res tres;
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000206 xfs_fsblock_t firstblock;
Jie Liu3d3c8b52013-08-12 20:49:59 +1000207 int rsvd = (flags & ATTR_ROOT) != 0;
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000208 int error, err2, committed, local;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000210 XFS_STATS_INC(xs_attr_set);
211
212 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
Dave Chinner24513372014-06-25 14:58:08 +1000213 return -EIO;
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000214
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000215 error = xfs_attr_args_init(&args, dp, name, flags);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000216 if (error)
217 return error;
218
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000219 args.value = value;
220 args.valuelen = valuelen;
221 args.firstblock = &firstblock;
222 args.flist = &flist;
223 args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
Christoph Hellwig6c888af2014-05-13 16:40:19 +1000224 args.total = xfs_attr_calc_size(&args, &local);
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000225
Christoph Hellwig7d095252009-06-08 15:33:32 +0200226 error = xfs_qm_dqattach(dp, 0);
227 if (error)
228 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 /*
231 * If the inode doesn't have an attribute fork, add one.
232 * (inode must not be locked when we call this routine)
233 */
234 if (XFS_IFORK_Q(dp) == 0) {
Barry Naujoke5889e92007-02-10 18:35:58 +1100235 int sf_size = sizeof(xfs_attr_sf_hdr_t) +
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000236 XFS_ATTR_SF_ENTSIZE_BYNAME(args.namelen, valuelen);
Barry Naujoke5889e92007-02-10 18:35:58 +1100237
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000238 error = xfs_bmap_add_attrfork(dp, sf_size, rsvd);
239 if (error)
240 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 }
242
243 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 * Start our first transaction of the day.
245 *
246 * All future transactions during this code must be "chained" off
247 * this one via the trans_dup() call. All transactions will contain
248 * the inode, and the inode will always be marked with trans_ihold().
249 * Since the inode will be locked in all transactions, we must log
250 * the inode in every transaction to let it float upward through
251 * the log.
252 */
253 args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_SET);
254
255 /*
256 * Root fork attributes can use reserved data blocks for this
257 * operation if necessary
258 */
259
260 if (rsvd)
261 args.trans->t_flags |= XFS_TRANS_RESERVE;
262
Jie Liu3d3c8b52013-08-12 20:49:59 +1000263 tres.tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
264 M_RES(mp)->tr_attrsetrt.tr_logres * args.total;
265 tres.tr_logcount = XFS_ATTRSET_LOG_COUNT;
266 tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
267 error = xfs_trans_reserve(args.trans, &tres, args.total, 0);
Jeff Liua21cd502013-01-28 21:27:53 +0800268 if (error) {
Christoph Hellwig4906e212015-06-04 13:47:56 +1000269 xfs_trans_cancel(args.trans);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000270 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 }
272 xfs_ilock(dp, XFS_ILOCK_EXCL);
273
Christoph Hellwig7d095252009-06-08 15:33:32 +0200274 error = xfs_trans_reserve_quota_nblks(args.trans, dp, args.total, 0,
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000275 rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
276 XFS_QMOPT_RES_REGBLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 if (error) {
278 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Christoph Hellwig4906e212015-06-04 13:47:56 +1000279 xfs_trans_cancel(args.trans);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000280 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 }
282
Christoph Hellwigddc34152011-09-19 15:00:54 +0000283 xfs_trans_ijoin(args.trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000286 * If the attribute list is non-existent or a shortform list,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 * upgrade it to a single-leaf-block attribute list.
288 */
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000289 if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL ||
290 (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
291 dp->i_d.di_anextents == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293 /*
294 * Build initial attribute list (if required).
295 */
296 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
Nathan Scottd8cc8902005-11-02 10:34:53 +1100297 xfs_attr_shortform_create(&args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299 /*
300 * Try to add the attr to the attribute list in
301 * the inode.
302 */
303 error = xfs_attr_shortform_addname(&args);
Dave Chinner24513372014-06-25 14:58:08 +1000304 if (error != -ENOSPC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 /*
306 * Commit the shortform mods, and we're done.
307 * NOTE: this is also the error path (EEXIST, etc).
308 */
309 ASSERT(args.trans != NULL);
310
311 /*
312 * If this is a synchronous mount, make sure that
313 * the transaction goes to disk before returning
314 * to the user.
315 */
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000316 if (mp->m_flags & XFS_MOUNT_WSYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 xfs_trans_set_sync(args.trans);
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000318
319 if (!error && (flags & ATTR_KERNOTIME) == 0) {
320 xfs_trans_ichgtime(args.trans, dp,
321 XFS_ICHGTIME_CHG);
322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 err2 = xfs_trans_commit(args.trans,
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000324 XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 xfs_iunlock(dp, XFS_ILOCK_EXCL);
326
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000327 return error ? error : err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 }
329
330 /*
331 * It won't fit in the shortform, transform to a leaf block.
332 * GROT: another possible req'mt for a double-split btree op.
333 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600334 xfs_bmap_init(args.flist, args.firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 error = xfs_attr_shortform_to_leaf(&args);
336 if (!error) {
337 error = xfs_bmap_finish(&args.trans, args.flist,
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100338 &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
340 if (error) {
341 ASSERT(committed);
342 args.trans = NULL;
343 xfs_bmap_cancel(&flist);
344 goto out;
345 }
346
347 /*
348 * bmap_finish() may have committed the last trans and started
349 * a new one. We need the inode to be in all transactions.
350 */
Christoph Hellwig898621d2010-06-24 11:36:58 +1000351 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +0000352 xfs_trans_ijoin(args.trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 /*
355 * Commit the leaf transformation. We'll need another (linked)
356 * transaction to add the new attribute to the leaf.
357 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000358
359 error = xfs_trans_roll(&args.trans, dp);
360 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 goto out;
362
363 }
364
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000365 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 error = xfs_attr_leaf_addname(&args);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000367 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 error = xfs_attr_node_addname(&args);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000369 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 /*
373 * If this is a synchronous mount, make sure that the
374 * transaction goes to disk before returning to the user.
375 */
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000376 if (mp->m_flags & XFS_MOUNT_WSYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 xfs_trans_set_sync(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000379 if ((flags & ATTR_KERNOTIME) == 0)
380 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 /*
383 * Commit the last in the sequence of transactions.
384 */
385 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000386 error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 xfs_iunlock(dp, XFS_ILOCK_EXCL);
388
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000389 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391out:
Christoph Hellwig4906e212015-06-04 13:47:56 +1000392 if (args.trans)
393 xfs_trans_cancel(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000395 return error;
Nathan Scottaa82daa2005-11-02 10:33:33 +1100396}
397
398/*
399 * Generic handler routine to remove a name from an attribute list.
400 * Transitions attribute list from Btree to shortform as necessary.
401 */
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000402int
403xfs_attr_remove(
404 struct xfs_inode *dp,
405 const unsigned char *name,
406 int flags)
Nathan Scottaa82daa2005-11-02 10:33:33 +1100407{
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000408 struct xfs_mount *mp = dp->i_mount;
409 struct xfs_da_args args;
410 struct xfs_bmap_free flist;
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000411 xfs_fsblock_t firstblock;
412 int error;
Nathan Scottaa82daa2005-11-02 10:33:33 +1100413
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000414 XFS_STATS_INC(xs_attr_remove);
415
416 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
Dave Chinner24513372014-06-25 14:58:08 +1000417 return -EIO;
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000418
419 if (!xfs_inode_hasattr(dp))
Dave Chinner24513372014-06-25 14:58:08 +1000420 return -ENOATTR;
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000421
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000422 error = xfs_attr_args_init(&args, dp, name, flags);
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000423 if (error)
424 return error;
425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 args.firstblock = &firstblock;
427 args.flist = &flist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 /*
Dave Chinner4a338212011-06-23 01:35:01 +0000430 * we have no control over the attribute names that userspace passes us
431 * to remove, so we have to allow the name lookup prior to attribute
432 * removal to fail.
433 */
434 args.op_flags = XFS_DA_OP_OKNOENT;
435
Christoph Hellwig7d095252009-06-08 15:33:32 +0200436 error = xfs_qm_dqattach(dp, 0);
437 if (error)
438 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 /*
441 * Start our first transaction of the day.
442 *
443 * All future transactions during this code must be "chained" off
444 * this one via the trans_dup() call. All transactions will contain
445 * the inode, and the inode will always be marked with trans_ihold().
446 * Since the inode will be locked in all transactions, we must log
447 * the inode in every transaction to let it float upward through
448 * the log.
449 */
450 args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_RM);
451
452 /*
453 * Root fork attributes can use reserved data blocks for this
454 * operation if necessary
455 */
456
457 if (flags & ATTR_ROOT)
458 args.trans->t_flags |= XFS_TRANS_RESERVE;
459
Jie Liu3d3c8b52013-08-12 20:49:59 +1000460 error = xfs_trans_reserve(args.trans, &M_RES(mp)->tr_attrrm,
461 XFS_ATTRRM_SPACE_RES(mp), 0);
462 if (error) {
Christoph Hellwig4906e212015-06-04 13:47:56 +1000463 xfs_trans_cancel(args.trans);
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000464 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 }
466
467 xfs_ilock(dp, XFS_ILOCK_EXCL);
468 /*
469 * No need to make quota reservations here. We expect to release some
470 * blocks not allocate in the common case.
471 */
Christoph Hellwigddc34152011-09-19 15:00:54 +0000472 xfs_trans_ijoin(args.trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +1000474 if (!xfs_inode_hasattr(dp)) {
Dave Chinner24513372014-06-25 14:58:08 +1000475 error = -ENOATTR;
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000476 } else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
478 error = xfs_attr_shortform_remove(&args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
480 error = xfs_attr_leaf_removename(&args);
481 } else {
482 error = xfs_attr_node_removename(&args);
483 }
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000484
485 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 /*
489 * If this is a synchronous mount, make sure that the
490 * transaction goes to disk before returning to the user.
491 */
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000492 if (mp->m_flags & XFS_MOUNT_WSYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 xfs_trans_set_sync(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000495 if ((flags & ATTR_KERNOTIME) == 0)
496 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 /*
499 * Commit the last in the sequence of transactions.
500 */
501 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000502 error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 xfs_iunlock(dp, XFS_ILOCK_EXCL);
504
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000505 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507out:
Christoph Hellwig4906e212015-06-04 13:47:56 +1000508 if (args.trans)
509 xfs_trans_cancel(args.trans);
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000510 xfs_iunlock(dp, XFS_ILOCK_EXCL);
511 return error;
Nathan Scottaa82daa2005-11-02 10:33:33 +1100512}
513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514/*========================================================================
515 * External routines when attribute list is inside the inode
516 *========================================================================*/
517
518/*
519 * Add a name to the shortform attribute list structure
520 * This is the external routine.
521 */
522STATIC int
523xfs_attr_shortform_addname(xfs_da_args_t *args)
524{
Nathan Scottd8cc8902005-11-02 10:34:53 +1100525 int newsize, forkoff, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Dave Chinner5a5881c2012-03-22 05:15:13 +0000527 trace_xfs_attr_sf_addname(args);
528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 retval = xfs_attr_shortform_lookup(args);
Dave Chinner24513372014-06-25 14:58:08 +1000530 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
Eric Sandeend99831f2014-06-22 15:03:54 +1000531 return retval;
Dave Chinner24513372014-06-25 14:58:08 +1000532 } else if (retval == -EEXIST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 if (args->flags & ATTR_CREATE)
Eric Sandeend99831f2014-06-22 15:03:54 +1000534 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 retval = xfs_attr_shortform_remove(args);
536 ASSERT(retval == 0);
537 }
538
Nathan Scottd8cc8902005-11-02 10:34:53 +1100539 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
540 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
Dave Chinner24513372014-06-25 14:58:08 +1000541 return -ENOSPC;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
544 newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100545
546 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
547 if (!forkoff)
Dave Chinner24513372014-06-25 14:58:08 +1000548 return -ENOSPC;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100549
550 xfs_attr_shortform_add(args, forkoff);
Eric Sandeend99831f2014-06-22 15:03:54 +1000551 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
553
554
555/*========================================================================
556 * External routines when attribute list is one block
557 *========================================================================*/
558
559/*
560 * Add a name to the leaf attribute list structure
561 *
562 * This leaf block cannot have a "remote" value, we only call this routine
563 * if bmap_one_block() says there is only one block (ie: no remote blks).
564 */
David Chinnera8272ce2007-11-23 16:28:09 +1100565STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566xfs_attr_leaf_addname(xfs_da_args_t *args)
567{
568 xfs_inode_t *dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000569 struct xfs_buf *bp;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100570 int retval, error, committed, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Dave Chinner5a5881c2012-03-22 05:15:13 +0000572 trace_xfs_attr_leaf_addname(args);
573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 /*
575 * Read the (only) block in the attribute list in.
576 */
577 dp = args->dp;
578 args->blkno = 0;
Dave Chinner517c2222013-04-24 18:58:55 +1000579 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100581 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 /*
584 * Look up the given attribute in the leaf block. Figure out if
585 * the given flags produce an error or call for an atomic rename.
586 */
Dave Chinner517c2222013-04-24 18:58:55 +1000587 retval = xfs_attr3_leaf_lookup_int(bp, args);
Dave Chinner24513372014-06-25 14:58:08 +1000588 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000589 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000590 return retval;
Dave Chinner24513372014-06-25 14:58:08 +1000591 } else if (retval == -EEXIST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 if (args->flags & ATTR_CREATE) { /* pure create op */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000593 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000594 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 }
Dave Chinner5a5881c2012-03-22 05:15:13 +0000596
597 trace_xfs_attr_leaf_replace(args);
598
Dave Chinner8275cdd2014-05-06 07:37:31 +1000599 /* save the attribute state for later removal*/
Barry Naujok6a178102008-05-21 16:42:05 +1000600 args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 args->blkno2 = args->blkno; /* set 2nd entry info*/
602 args->index2 = args->index;
603 args->rmtblkno2 = args->rmtblkno;
604 args->rmtblkcnt2 = args->rmtblkcnt;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000605 args->rmtvaluelen2 = args->rmtvaluelen;
606
607 /*
608 * clear the remote attr state now that it is saved so that the
609 * values reflect the state of the attribute we are about to
610 * add, not the attribute we just found and will remove later.
611 */
612 args->rmtblkno = 0;
613 args->rmtblkcnt = 0;
614 args->rmtvaluelen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 }
616
617 /*
618 * Add the attribute to the leaf block, transitioning to a Btree
619 * if required.
620 */
Dave Chinner517c2222013-04-24 18:58:55 +1000621 retval = xfs_attr3_leaf_add(bp, args);
Dave Chinner24513372014-06-25 14:58:08 +1000622 if (retval == -ENOSPC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 /*
624 * Promote the attribute list to the Btree format, then
625 * Commit that transaction so that the node_addname() call
626 * can manage its own transactions.
627 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600628 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000629 error = xfs_attr3_leaf_to_node(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 if (!error) {
631 error = xfs_bmap_finish(&args->trans, args->flist,
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100632 &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 }
634 if (error) {
635 ASSERT(committed);
636 args->trans = NULL;
637 xfs_bmap_cancel(args->flist);
Eric Sandeend99831f2014-06-22 15:03:54 +1000638 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 }
640
641 /*
642 * bmap_finish() may have committed the last trans and started
643 * a new one. We need the inode to be in all transactions.
644 */
Christoph Hellwig898621d2010-06-24 11:36:58 +1000645 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +0000646 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 /*
649 * Commit the current trans (including the inode) and start
650 * a new one.
651 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000652 error = xfs_trans_roll(&args->trans, dp);
653 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000654 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656 /*
657 * Fob the whole rest of the problem off on the Btree code.
658 */
659 error = xfs_attr_node_addname(args);
Eric Sandeend99831f2014-06-22 15:03:54 +1000660 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 }
662
663 /*
664 * Commit the transaction that added the attr name so that
665 * later routines can manage their own transactions.
666 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000667 error = xfs_trans_roll(&args->trans, dp);
668 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000669 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671 /*
672 * If there was an out-of-line value, allocate the blocks we
673 * identified for its storage and copy the value. This is done
674 * after we create the attribute so that we don't overflow the
675 * maximum size of a transaction and/or hit a deadlock.
676 */
677 if (args->rmtblkno > 0) {
678 error = xfs_attr_rmtval_set(args);
679 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000680 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 }
682
683 /*
684 * If this is an atomic rename operation, we must "flip" the
685 * incomplete flags on the "new" and "old" attribute/value pairs
686 * so that one disappears and one appears atomically. Then we
687 * must remove the "old" attribute/value pair.
688 */
Barry Naujok6a178102008-05-21 16:42:05 +1000689 if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 /*
691 * In a separate transaction, set the incomplete flag on the
692 * "old" attr and clear the incomplete flag on the "new" attr.
693 */
Dave Chinner517c2222013-04-24 18:58:55 +1000694 error = xfs_attr3_leaf_flipflags(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000696 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 /*
699 * Dismantle the "old" attribute/value pair by removing
700 * a "remote" value (if it exists).
701 */
702 args->index = args->index2;
703 args->blkno = args->blkno2;
704 args->rmtblkno = args->rmtblkno2;
705 args->rmtblkcnt = args->rmtblkcnt2;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000706 args->rmtvaluelen = args->rmtvaluelen2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 if (args->rmtblkno) {
708 error = xfs_attr_rmtval_remove(args);
709 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000710 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 }
712
713 /*
714 * Read in the block containing the "old" attr, then
715 * remove the "old" attr from that block (neat, huh!)
716 */
Dave Chinner517c2222013-04-24 18:58:55 +1000717 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
Dave Chinnerad14c332012-11-12 22:54:16 +1100718 -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100720 return error;
721
Dave Chinner517c2222013-04-24 18:58:55 +1000722 xfs_attr3_leaf_remove(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724 /*
725 * If the result is small enough, shrink it all into the inode.
726 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100727 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Eric Sandeen9d87c312009-01-14 23:22:07 -0600728 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000729 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 /* bp is gone due to xfs_da_shrink_inode */
731 if (!error) {
732 error = xfs_bmap_finish(&args->trans,
733 args->flist,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 &committed);
735 }
736 if (error) {
737 ASSERT(committed);
738 args->trans = NULL;
739 xfs_bmap_cancel(args->flist);
Eric Sandeend99831f2014-06-22 15:03:54 +1000740 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 }
742
743 /*
744 * bmap_finish() may have committed the last trans
745 * and started a new one. We need the inode to be
746 * in all transactions.
747 */
Christoph Hellwig898621d2010-06-24 11:36:58 +1000748 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +0000749 xfs_trans_ijoin(args->trans, dp, 0);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
752 /*
753 * Commit the remove and start the next trans in series.
754 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000755 error = xfs_trans_roll(&args->trans, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757 } else if (args->rmtblkno > 0) {
758 /*
759 * Added a "remote" value, just clear the incomplete flag.
760 */
Dave Chinner517c2222013-04-24 18:58:55 +1000761 error = xfs_attr3_leaf_clearflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 }
Dave Chinner517c2222013-04-24 18:58:55 +1000763 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
765
766/*
767 * Remove a name from the leaf attribute list structure
768 *
769 * This leaf block cannot have a "remote" value, we only call this routine
770 * if bmap_one_block() says there is only one block (ie: no remote blks).
771 */
772STATIC int
773xfs_attr_leaf_removename(xfs_da_args_t *args)
774{
775 xfs_inode_t *dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000776 struct xfs_buf *bp;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100777 int error, committed, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Dave Chinner5a5881c2012-03-22 05:15:13 +0000779 trace_xfs_attr_leaf_removename(args);
780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 /*
782 * Remove the attribute.
783 */
784 dp = args->dp;
785 args->blkno = 0;
Dave Chinner517c2222013-04-24 18:58:55 +1000786 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Dave Chinnerad14c332012-11-12 22:54:16 +1100787 if (error)
788 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Dave Chinner517c2222013-04-24 18:58:55 +1000790 error = xfs_attr3_leaf_lookup_int(bp, args);
Dave Chinner24513372014-06-25 14:58:08 +1000791 if (error == -ENOATTR) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000792 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000793 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 }
795
Dave Chinner517c2222013-04-24 18:58:55 +1000796 xfs_attr3_leaf_remove(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798 /*
799 * If the result is small enough, shrink it all into the inode.
800 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100801 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Eric Sandeen9d87c312009-01-14 23:22:07 -0600802 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000803 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 /* bp is gone due to xfs_da_shrink_inode */
805 if (!error) {
806 error = xfs_bmap_finish(&args->trans, args->flist,
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100807 &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 }
809 if (error) {
810 ASSERT(committed);
811 args->trans = NULL;
812 xfs_bmap_cancel(args->flist);
Dave Chinner517c2222013-04-24 18:58:55 +1000813 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 }
815
816 /*
817 * bmap_finish() may have committed the last trans and started
818 * a new one. We need the inode to be in all transactions.
819 */
Christoph Hellwig898621d2010-06-24 11:36:58 +1000820 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +0000821 xfs_trans_ijoin(args->trans, dp, 0);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000822 }
Dave Chinner517c2222013-04-24 18:58:55 +1000823 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824}
825
826/*
827 * Look up a name in a leaf attribute list structure.
828 *
829 * This leaf block cannot have a "remote" value, we only call this routine
830 * if bmap_one_block() says there is only one block (ie: no remote blks).
831 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000832STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833xfs_attr_leaf_get(xfs_da_args_t *args)
834{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000835 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 int error;
837
Dave Chinneree732592012-11-12 22:53:53 +1100838 trace_xfs_attr_leaf_get(args);
839
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 args->blkno = 0;
Dave Chinner517c2222013-04-24 18:58:55 +1000841 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100843 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Dave Chinner517c2222013-04-24 18:58:55 +1000845 error = xfs_attr3_leaf_lookup_int(bp, args);
Dave Chinner24513372014-06-25 14:58:08 +1000846 if (error != -EEXIST) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000847 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000848 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 }
Dave Chinner517c2222013-04-24 18:58:55 +1000850 error = xfs_attr3_leaf_getvalue(bp, args);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000851 xfs_trans_brelse(args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
853 error = xfs_attr_rmtval_get(args);
854 }
Dave Chinner517c2222013-04-24 18:58:55 +1000855 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856}
857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858/*========================================================================
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000859 * External routines when attribute list size > geo->blksize
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 *========================================================================*/
861
862/*
863 * Add a name to a Btree-format attribute list.
864 *
865 * This will involve walking down the Btree, and may involve splitting
866 * leaf nodes and even splitting intermediate nodes up to and including
867 * the root node (a special case of an intermediate node).
868 *
869 * "Remote" attribute values confuse the issue and atomic rename operations
870 * add a whole extra layer of confusion on top of that.
871 */
872STATIC int
873xfs_attr_node_addname(xfs_da_args_t *args)
874{
875 xfs_da_state_t *state;
876 xfs_da_state_blk_t *blk;
877 xfs_inode_t *dp;
878 xfs_mount_t *mp;
879 int committed, retval, error;
880
Dave Chinner5a5881c2012-03-22 05:15:13 +0000881 trace_xfs_attr_node_addname(args);
882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 /*
884 * Fill in bucket of arguments/results/context to carry around.
885 */
886 dp = args->dp;
887 mp = dp->i_mount;
888restart:
889 state = xfs_da_state_alloc();
890 state->args = args;
891 state->mp = mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
893 /*
894 * Search to see if name already exists, and get back a pointer
895 * to where it should go.
896 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000897 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 if (error)
899 goto out;
900 blk = &state->path.blk[ state->path.active-1 ];
901 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner24513372014-06-25 14:58:08 +1000902 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 goto out;
Dave Chinner24513372014-06-25 14:58:08 +1000904 } else if (retval == -EEXIST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 if (args->flags & ATTR_CREATE)
906 goto out;
Dave Chinner5a5881c2012-03-22 05:15:13 +0000907
908 trace_xfs_attr_node_replace(args);
909
Dave Chinner8275cdd2014-05-06 07:37:31 +1000910 /* save the attribute state for later removal*/
Barry Naujok6a178102008-05-21 16:42:05 +1000911 args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 args->blkno2 = args->blkno; /* set 2nd entry info*/
913 args->index2 = args->index;
914 args->rmtblkno2 = args->rmtblkno;
915 args->rmtblkcnt2 = args->rmtblkcnt;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000916 args->rmtvaluelen2 = args->rmtvaluelen;
917
918 /*
919 * clear the remote attr state now that it is saved so that the
920 * values reflect the state of the attribute we are about to
921 * add, not the attribute we just found and will remove later.
922 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 args->rmtblkno = 0;
924 args->rmtblkcnt = 0;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000925 args->rmtvaluelen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 }
927
Dave Chinner517c2222013-04-24 18:58:55 +1000928 retval = xfs_attr3_leaf_add(blk->bp, state->args);
Dave Chinner24513372014-06-25 14:58:08 +1000929 if (retval == -ENOSPC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 if (state->path.active == 1) {
931 /*
932 * Its really a single leaf node, but it had
933 * out-of-line values so it looked like it *might*
934 * have been a b-tree.
935 */
936 xfs_da_state_free(state);
Eric Sandeen6dd93e92013-07-31 20:18:54 -0500937 state = NULL;
Eric Sandeen9d87c312009-01-14 23:22:07 -0600938 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000939 error = xfs_attr3_leaf_to_node(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 if (!error) {
941 error = xfs_bmap_finish(&args->trans,
942 args->flist,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 &committed);
944 }
945 if (error) {
946 ASSERT(committed);
947 args->trans = NULL;
948 xfs_bmap_cancel(args->flist);
949 goto out;
950 }
951
952 /*
953 * bmap_finish() may have committed the last trans
954 * and started a new one. We need the inode to be
955 * in all transactions.
956 */
Christoph Hellwig898621d2010-06-24 11:36:58 +1000957 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +0000958 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
960 /*
961 * Commit the node conversion and start the next
962 * trans in the chain.
963 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000964 error = xfs_trans_roll(&args->trans, dp);
965 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 goto out;
967
968 goto restart;
969 }
970
971 /*
972 * Split as many Btree elements as required.
973 * This code tracks the new and old attr's location
974 * in the index/blkno/rmtblkno/rmtblkcnt fields and
975 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
976 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600977 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000978 error = xfs_da3_split(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 if (!error) {
980 error = xfs_bmap_finish(&args->trans, args->flist,
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100981 &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 }
983 if (error) {
984 ASSERT(committed);
985 args->trans = NULL;
986 xfs_bmap_cancel(args->flist);
987 goto out;
988 }
989
990 /*
991 * bmap_finish() may have committed the last trans and started
992 * a new one. We need the inode to be in all transactions.
993 */
Christoph Hellwig898621d2010-06-24 11:36:58 +1000994 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +0000995 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 } else {
997 /*
998 * Addition succeeded, update Btree hashvals.
999 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001000 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 }
1002
1003 /*
1004 * Kill the state structure, we're done with it and need to
1005 * allow the buffers to come back later.
1006 */
1007 xfs_da_state_free(state);
1008 state = NULL;
1009
1010 /*
1011 * Commit the leaf addition or btree split and start the next
1012 * trans in the chain.
1013 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10001014 error = xfs_trans_roll(&args->trans, dp);
1015 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 goto out;
1017
1018 /*
1019 * If there was an out-of-line value, allocate the blocks we
1020 * identified for its storage and copy the value. This is done
1021 * after we create the attribute so that we don't overflow the
1022 * maximum size of a transaction and/or hit a deadlock.
1023 */
1024 if (args->rmtblkno > 0) {
1025 error = xfs_attr_rmtval_set(args);
1026 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001027 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 }
1029
1030 /*
1031 * If this is an atomic rename operation, we must "flip" the
1032 * incomplete flags on the "new" and "old" attribute/value pairs
1033 * so that one disappears and one appears atomically. Then we
1034 * must remove the "old" attribute/value pair.
1035 */
Barry Naujok6a178102008-05-21 16:42:05 +10001036 if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 /*
1038 * In a separate transaction, set the incomplete flag on the
1039 * "old" attr and clear the incomplete flag on the "new" attr.
1040 */
Dave Chinner517c2222013-04-24 18:58:55 +10001041 error = xfs_attr3_leaf_flipflags(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 if (error)
1043 goto out;
1044
1045 /*
1046 * Dismantle the "old" attribute/value pair by removing
1047 * a "remote" value (if it exists).
1048 */
1049 args->index = args->index2;
1050 args->blkno = args->blkno2;
1051 args->rmtblkno = args->rmtblkno2;
1052 args->rmtblkcnt = args->rmtblkcnt2;
Dave Chinner8275cdd2014-05-06 07:37:31 +10001053 args->rmtvaluelen = args->rmtvaluelen2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 if (args->rmtblkno) {
1055 error = xfs_attr_rmtval_remove(args);
1056 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001057 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 }
1059
1060 /*
1061 * Re-find the "old" attribute entry after any split ops.
1062 * The INCOMPLETE flag means that we will find the "old"
1063 * attr, not the "new" one.
1064 */
1065 args->flags |= XFS_ATTR_INCOMPLETE;
1066 state = xfs_da_state_alloc();
1067 state->args = args;
1068 state->mp = mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 state->inleaf = 0;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001070 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 if (error)
1072 goto out;
1073
1074 /*
1075 * Remove the name and update the hashvals in the tree.
1076 */
1077 blk = &state->path.blk[ state->path.active-1 ];
1078 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner517c2222013-04-24 18:58:55 +10001079 error = xfs_attr3_leaf_remove(blk->bp, args);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001080 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082 /*
1083 * Check to see if the tree needs to be collapsed.
1084 */
1085 if (retval && (state->path.active > 1)) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001086 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001087 error = xfs_da3_join(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 if (!error) {
1089 error = xfs_bmap_finish(&args->trans,
1090 args->flist,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 &committed);
1092 }
1093 if (error) {
1094 ASSERT(committed);
1095 args->trans = NULL;
1096 xfs_bmap_cancel(args->flist);
1097 goto out;
1098 }
1099
1100 /*
1101 * bmap_finish() may have committed the last trans
1102 * and started a new one. We need the inode to be
1103 * in all transactions.
1104 */
Christoph Hellwig898621d2010-06-24 11:36:58 +10001105 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +00001106 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 }
1108
1109 /*
1110 * Commit and start the next trans in the chain.
1111 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10001112 error = xfs_trans_roll(&args->trans, dp);
1113 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 goto out;
1115
1116 } else if (args->rmtblkno > 0) {
1117 /*
1118 * Added a "remote" value, just clear the incomplete flag.
1119 */
Dave Chinner517c2222013-04-24 18:58:55 +10001120 error = xfs_attr3_leaf_clearflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 if (error)
1122 goto out;
1123 }
1124 retval = error = 0;
1125
1126out:
1127 if (state)
1128 xfs_da_state_free(state);
1129 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001130 return error;
1131 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132}
1133
1134/*
1135 * Remove a name from a B-tree attribute list.
1136 *
1137 * This will involve walking down the Btree, and may involve joining
1138 * leaf nodes and even joining intermediate nodes up to and including
1139 * the root node (a special case of an intermediate node).
1140 */
1141STATIC int
1142xfs_attr_node_removename(xfs_da_args_t *args)
1143{
1144 xfs_da_state_t *state;
1145 xfs_da_state_blk_t *blk;
1146 xfs_inode_t *dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001147 struct xfs_buf *bp;
Nathan Scottd8cc8902005-11-02 10:34:53 +11001148 int retval, error, committed, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
Dave Chinner5a5881c2012-03-22 05:15:13 +00001150 trace_xfs_attr_node_removename(args);
1151
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 /*
1153 * Tie a string around our finger to remind us where we are.
1154 */
1155 dp = args->dp;
1156 state = xfs_da_state_alloc();
1157 state->args = args;
1158 state->mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
1160 /*
1161 * Search to see if name exists, and get back a pointer to it.
1162 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001163 error = xfs_da3_node_lookup_int(state, &retval);
Dave Chinner24513372014-06-25 14:58:08 +10001164 if (error || (retval != -EEXIST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 if (error == 0)
1166 error = retval;
1167 goto out;
1168 }
1169
1170 /*
1171 * If there is an out-of-line value, de-allocate the blocks.
1172 * This is done before we remove the attribute so that we don't
1173 * overflow the maximum size of a transaction and/or hit a deadlock.
1174 */
1175 blk = &state->path.blk[ state->path.active-1 ];
1176 ASSERT(blk->bp != NULL);
1177 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1178 if (args->rmtblkno > 0) {
1179 /*
1180 * Fill in disk block numbers in the state structure
1181 * so that we can get the buffers back after we commit
1182 * several transactions in the following calls.
1183 */
1184 error = xfs_attr_fillstate(state);
1185 if (error)
1186 goto out;
1187
1188 /*
1189 * Mark the attribute as INCOMPLETE, then bunmapi() the
1190 * remote value.
1191 */
Dave Chinner517c2222013-04-24 18:58:55 +10001192 error = xfs_attr3_leaf_setflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 if (error)
1194 goto out;
1195 error = xfs_attr_rmtval_remove(args);
1196 if (error)
1197 goto out;
1198
1199 /*
1200 * Refill the state structure with buffers, the prior calls
1201 * released our buffers.
1202 */
1203 error = xfs_attr_refillstate(state);
1204 if (error)
1205 goto out;
1206 }
1207
1208 /*
1209 * Remove the name and update the hashvals in the tree.
1210 */
1211 blk = &state->path.blk[ state->path.active-1 ];
1212 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner517c2222013-04-24 18:58:55 +10001213 retval = xfs_attr3_leaf_remove(blk->bp, args);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001214 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
1216 /*
1217 * Check to see if the tree needs to be collapsed.
1218 */
1219 if (retval && (state->path.active > 1)) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001220 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001221 error = xfs_da3_join(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 if (!error) {
1223 error = xfs_bmap_finish(&args->trans, args->flist,
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001224 &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 }
1226 if (error) {
1227 ASSERT(committed);
1228 args->trans = NULL;
1229 xfs_bmap_cancel(args->flist);
1230 goto out;
1231 }
1232
1233 /*
1234 * bmap_finish() may have committed the last trans and started
1235 * a new one. We need the inode to be in all transactions.
1236 */
Christoph Hellwig898621d2010-06-24 11:36:58 +10001237 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +00001238 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
1240 /*
1241 * Commit the Btree join operation and start a new trans.
1242 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10001243 error = xfs_trans_roll(&args->trans, dp);
1244 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 goto out;
1246 }
1247
1248 /*
1249 * If the result is small enough, push it all into the inode.
1250 */
1251 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
1252 /*
1253 * Have to get rid of the copy of this dabuf in the state.
1254 */
1255 ASSERT(state->path.active == 1);
1256 ASSERT(state->path.blk[0].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 state->path.blk[0].bp = NULL;
1258
Dave Chinner517c2222013-04-24 18:58:55 +10001259 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 if (error)
1261 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Nathan Scottd8cc8902005-11-02 10:34:53 +11001263 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001264 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +10001265 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 /* bp is gone due to xfs_da_shrink_inode */
1267 if (!error) {
1268 error = xfs_bmap_finish(&args->trans,
1269 args->flist,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 &committed);
1271 }
1272 if (error) {
1273 ASSERT(committed);
1274 args->trans = NULL;
1275 xfs_bmap_cancel(args->flist);
1276 goto out;
1277 }
1278
1279 /*
1280 * bmap_finish() may have committed the last trans
1281 * and started a new one. We need the inode to be
1282 * in all transactions.
1283 */
Christoph Hellwig898621d2010-06-24 11:36:58 +10001284 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +00001285 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 } else
Dave Chinner1d9025e2012-06-22 18:50:14 +10001287 xfs_trans_brelse(args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 }
1289 error = 0;
1290
1291out:
1292 xfs_da_state_free(state);
Eric Sandeend99831f2014-06-22 15:03:54 +10001293 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294}
1295
1296/*
1297 * Fill in the disk block numbers in the state structure for the buffers
1298 * that are attached to the state structure.
1299 * This is done so that we can quickly reattach ourselves to those buffers
Nathan Scottc41564b2006-03-29 08:55:14 +10001300 * after some set of transaction commits have released these buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 */
1302STATIC int
1303xfs_attr_fillstate(xfs_da_state_t *state)
1304{
1305 xfs_da_state_path_t *path;
1306 xfs_da_state_blk_t *blk;
1307 int level;
1308
Dave Chinneree732592012-11-12 22:53:53 +11001309 trace_xfs_attr_fillstate(state->args);
1310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 /*
1312 * Roll down the "path" in the state structure, storing the on-disk
1313 * block number for those buffers in the "path".
1314 */
1315 path = &state->path;
1316 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1317 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1318 if (blk->bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001319 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 blk->bp = NULL;
1321 } else {
1322 blk->disk_blkno = 0;
1323 }
1324 }
1325
1326 /*
1327 * Roll down the "altpath" in the state structure, storing the on-disk
1328 * block number for those buffers in the "altpath".
1329 */
1330 path = &state->altpath;
1331 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1332 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1333 if (blk->bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001334 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 blk->bp = NULL;
1336 } else {
1337 blk->disk_blkno = 0;
1338 }
1339 }
1340
Eric Sandeend99831f2014-06-22 15:03:54 +10001341 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342}
1343
1344/*
1345 * Reattach the buffers to the state structure based on the disk block
1346 * numbers stored in the state structure.
Nathan Scottc41564b2006-03-29 08:55:14 +10001347 * This is done after some set of transaction commits have released those
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 * buffers from our grip.
1349 */
1350STATIC int
1351xfs_attr_refillstate(xfs_da_state_t *state)
1352{
1353 xfs_da_state_path_t *path;
1354 xfs_da_state_blk_t *blk;
1355 int level, error;
1356
Dave Chinneree732592012-11-12 22:53:53 +11001357 trace_xfs_attr_refillstate(state->args);
1358
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 /*
1360 * Roll down the "path" in the state structure, storing the on-disk
1361 * block number for those buffers in the "path".
1362 */
1363 path = &state->path;
1364 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1365 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1366 if (blk->disk_blkno) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001367 error = xfs_da3_node_read(state->args->trans,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 state->args->dp,
1369 blk->blkno, blk->disk_blkno,
Dave Chinnerd9392a42012-11-12 22:54:17 +11001370 &blk->bp, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001372 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 } else {
1374 blk->bp = NULL;
1375 }
1376 }
1377
1378 /*
1379 * Roll down the "altpath" in the state structure, storing the on-disk
1380 * block number for those buffers in the "altpath".
1381 */
1382 path = &state->altpath;
1383 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1384 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1385 if (blk->disk_blkno) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001386 error = xfs_da3_node_read(state->args->trans,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 state->args->dp,
1388 blk->blkno, blk->disk_blkno,
Dave Chinnerd9392a42012-11-12 22:54:17 +11001389 &blk->bp, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001391 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 } else {
1393 blk->bp = NULL;
1394 }
1395 }
1396
Eric Sandeend99831f2014-06-22 15:03:54 +10001397 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398}
1399
1400/*
1401 * Look up a filename in a node attribute list.
1402 *
1403 * This routine gets called for any attribute fork that has more than one
1404 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1405 * "remote" values taking up more blocks.
1406 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001407STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408xfs_attr_node_get(xfs_da_args_t *args)
1409{
1410 xfs_da_state_t *state;
1411 xfs_da_state_blk_t *blk;
1412 int error, retval;
1413 int i;
1414
Dave Chinneree732592012-11-12 22:53:53 +11001415 trace_xfs_attr_node_get(args);
1416
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 state = xfs_da_state_alloc();
1418 state->args = args;
1419 state->mp = args->dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
1421 /*
1422 * Search to see if name exists, and get back a pointer to it.
1423 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001424 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 if (error) {
1426 retval = error;
Dave Chinner24513372014-06-25 14:58:08 +10001427 } else if (retval == -EEXIST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 blk = &state->path.blk[ state->path.active-1 ];
1429 ASSERT(blk->bp != NULL);
1430 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1431
1432 /*
1433 * Get the value, local or "remote"
1434 */
Dave Chinner517c2222013-04-24 18:58:55 +10001435 retval = xfs_attr3_leaf_getvalue(blk->bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 if (!retval && (args->rmtblkno > 0)
1437 && !(args->flags & ATTR_KERNOVAL)) {
1438 retval = xfs_attr_rmtval_get(args);
1439 }
1440 }
1441
1442 /*
1443 * If not in a transaction, we have to release all the buffers.
1444 */
1445 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001446 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 state->path.blk[i].bp = NULL;
1448 }
1449
1450 xfs_da_state_free(state);
Eric Sandeend99831f2014-06-22 15:03:54 +10001451 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452}