blob: 3349c9a1e84569201cb5df93ae39dd1941f72209 [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 }
Christoph Hellwig70393312015-06-04 13:48:08 +1000323 err2 = xfs_trans_commit(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 xfs_iunlock(dp, XFS_ILOCK_EXCL);
325
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000326 return error ? error : err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 }
328
329 /*
330 * It won't fit in the shortform, transform to a leaf block.
331 * GROT: another possible req'mt for a double-split btree op.
332 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600333 xfs_bmap_init(args.flist, args.firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 error = xfs_attr_shortform_to_leaf(&args);
335 if (!error) {
336 error = xfs_bmap_finish(&args.trans, args.flist,
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100337 &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
339 if (error) {
340 ASSERT(committed);
341 args.trans = NULL;
342 xfs_bmap_cancel(&flist);
343 goto out;
344 }
345
346 /*
347 * bmap_finish() may have committed the last trans and started
348 * a new one. We need the inode to be in all transactions.
349 */
Christoph Hellwig898621d2010-06-24 11:36:58 +1000350 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +0000351 xfs_trans_ijoin(args.trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353 /*
354 * Commit the leaf transformation. We'll need another (linked)
355 * transaction to add the new attribute to the leaf.
356 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000357
358 error = xfs_trans_roll(&args.trans, dp);
359 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 goto out;
361
362 }
363
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000364 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 error = xfs_attr_leaf_addname(&args);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000366 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 error = xfs_attr_node_addname(&args);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000368 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 /*
372 * If this is a synchronous mount, make sure that the
373 * transaction goes to disk before returning to the user.
374 */
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000375 if (mp->m_flags & XFS_MOUNT_WSYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 xfs_trans_set_sync(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000378 if ((flags & ATTR_KERNOTIME) == 0)
379 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 /*
382 * Commit the last in the sequence of transactions.
383 */
384 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
Christoph Hellwig70393312015-06-04 13:48:08 +1000385 error = xfs_trans_commit(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 xfs_iunlock(dp, XFS_ILOCK_EXCL);
387
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000388 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390out:
Christoph Hellwig4906e212015-06-04 13:47:56 +1000391 if (args.trans)
392 xfs_trans_cancel(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000394 return error;
Nathan Scottaa82daa2005-11-02 10:33:33 +1100395}
396
397/*
398 * Generic handler routine to remove a name from an attribute list.
399 * Transitions attribute list from Btree to shortform as necessary.
400 */
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000401int
402xfs_attr_remove(
403 struct xfs_inode *dp,
404 const unsigned char *name,
405 int flags)
Nathan Scottaa82daa2005-11-02 10:33:33 +1100406{
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000407 struct xfs_mount *mp = dp->i_mount;
408 struct xfs_da_args args;
409 struct xfs_bmap_free flist;
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000410 xfs_fsblock_t firstblock;
411 int error;
Nathan Scottaa82daa2005-11-02 10:33:33 +1100412
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000413 XFS_STATS_INC(xs_attr_remove);
414
415 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
Dave Chinner24513372014-06-25 14:58:08 +1000416 return -EIO;
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000417
418 if (!xfs_inode_hasattr(dp))
Dave Chinner24513372014-06-25 14:58:08 +1000419 return -ENOATTR;
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000420
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000421 error = xfs_attr_args_init(&args, dp, name, flags);
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000422 if (error)
423 return error;
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 args.firstblock = &firstblock;
426 args.flist = &flist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 /*
Dave Chinner4a338212011-06-23 01:35:01 +0000429 * we have no control over the attribute names that userspace passes us
430 * to remove, so we have to allow the name lookup prior to attribute
431 * removal to fail.
432 */
433 args.op_flags = XFS_DA_OP_OKNOENT;
434
Christoph Hellwig7d095252009-06-08 15:33:32 +0200435 error = xfs_qm_dqattach(dp, 0);
436 if (error)
437 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 /*
440 * Start our first transaction of the day.
441 *
442 * All future transactions during this code must be "chained" off
443 * this one via the trans_dup() call. All transactions will contain
444 * the inode, and the inode will always be marked with trans_ihold().
445 * Since the inode will be locked in all transactions, we must log
446 * the inode in every transaction to let it float upward through
447 * the log.
448 */
449 args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_RM);
450
451 /*
452 * Root fork attributes can use reserved data blocks for this
453 * operation if necessary
454 */
455
456 if (flags & ATTR_ROOT)
457 args.trans->t_flags |= XFS_TRANS_RESERVE;
458
Jie Liu3d3c8b52013-08-12 20:49:59 +1000459 error = xfs_trans_reserve(args.trans, &M_RES(mp)->tr_attrrm,
460 XFS_ATTRRM_SPACE_RES(mp), 0);
461 if (error) {
Christoph Hellwig4906e212015-06-04 13:47:56 +1000462 xfs_trans_cancel(args.trans);
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000463 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 }
465
466 xfs_ilock(dp, XFS_ILOCK_EXCL);
467 /*
468 * No need to make quota reservations here. We expect to release some
469 * blocks not allocate in the common case.
470 */
Christoph Hellwigddc34152011-09-19 15:00:54 +0000471 xfs_trans_ijoin(args.trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +1000473 if (!xfs_inode_hasattr(dp)) {
Dave Chinner24513372014-06-25 14:58:08 +1000474 error = -ENOATTR;
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000475 } else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
477 error = xfs_attr_shortform_remove(&args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
479 error = xfs_attr_leaf_removename(&args);
480 } else {
481 error = xfs_attr_node_removename(&args);
482 }
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000483
484 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 /*
488 * If this is a synchronous mount, make sure that the
489 * transaction goes to disk before returning to the user.
490 */
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000491 if (mp->m_flags & XFS_MOUNT_WSYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 xfs_trans_set_sync(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000494 if ((flags & ATTR_KERNOTIME) == 0)
495 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 /*
498 * Commit the last in the sequence of transactions.
499 */
500 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
Christoph Hellwig70393312015-06-04 13:48:08 +1000501 error = xfs_trans_commit(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 xfs_iunlock(dp, XFS_ILOCK_EXCL);
503
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000504 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506out:
Christoph Hellwig4906e212015-06-04 13:47:56 +1000507 if (args.trans)
508 xfs_trans_cancel(args.trans);
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000509 xfs_iunlock(dp, XFS_ILOCK_EXCL);
510 return error;
Nathan Scottaa82daa2005-11-02 10:33:33 +1100511}
512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513/*========================================================================
514 * External routines when attribute list is inside the inode
515 *========================================================================*/
516
517/*
518 * Add a name to the shortform attribute list structure
519 * This is the external routine.
520 */
521STATIC int
522xfs_attr_shortform_addname(xfs_da_args_t *args)
523{
Nathan Scottd8cc8902005-11-02 10:34:53 +1100524 int newsize, forkoff, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Dave Chinner5a5881c2012-03-22 05:15:13 +0000526 trace_xfs_attr_sf_addname(args);
527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 retval = xfs_attr_shortform_lookup(args);
Dave Chinner24513372014-06-25 14:58:08 +1000529 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
Eric Sandeend99831f2014-06-22 15:03:54 +1000530 return retval;
Dave Chinner24513372014-06-25 14:58:08 +1000531 } else if (retval == -EEXIST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 if (args->flags & ATTR_CREATE)
Eric Sandeend99831f2014-06-22 15:03:54 +1000533 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 retval = xfs_attr_shortform_remove(args);
535 ASSERT(retval == 0);
536 }
537
Nathan Scottd8cc8902005-11-02 10:34:53 +1100538 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
539 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
Dave Chinner24513372014-06-25 14:58:08 +1000540 return -ENOSPC;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
543 newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100544
545 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
546 if (!forkoff)
Dave Chinner24513372014-06-25 14:58:08 +1000547 return -ENOSPC;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100548
549 xfs_attr_shortform_add(args, forkoff);
Eric Sandeend99831f2014-06-22 15:03:54 +1000550 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551}
552
553
554/*========================================================================
555 * External routines when attribute list is one block
556 *========================================================================*/
557
558/*
559 * Add a name to the leaf attribute list structure
560 *
561 * This leaf block cannot have a "remote" value, we only call this routine
562 * if bmap_one_block() says there is only one block (ie: no remote blks).
563 */
David Chinnera8272ce2007-11-23 16:28:09 +1100564STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565xfs_attr_leaf_addname(xfs_da_args_t *args)
566{
567 xfs_inode_t *dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000568 struct xfs_buf *bp;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100569 int retval, error, committed, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Dave Chinner5a5881c2012-03-22 05:15:13 +0000571 trace_xfs_attr_leaf_addname(args);
572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 /*
574 * Read the (only) block in the attribute list in.
575 */
576 dp = args->dp;
577 args->blkno = 0;
Dave Chinner517c2222013-04-24 18:58:55 +1000578 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100580 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 /*
583 * Look up the given attribute in the leaf block. Figure out if
584 * the given flags produce an error or call for an atomic rename.
585 */
Dave Chinner517c2222013-04-24 18:58:55 +1000586 retval = xfs_attr3_leaf_lookup_int(bp, args);
Dave Chinner24513372014-06-25 14:58:08 +1000587 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000588 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000589 return retval;
Dave Chinner24513372014-06-25 14:58:08 +1000590 } else if (retval == -EEXIST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 if (args->flags & ATTR_CREATE) { /* pure create op */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000592 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000593 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
Dave Chinner5a5881c2012-03-22 05:15:13 +0000595
596 trace_xfs_attr_leaf_replace(args);
597
Dave Chinner8275cdd2014-05-06 07:37:31 +1000598 /* save the attribute state for later removal*/
Barry Naujok6a178102008-05-21 16:42:05 +1000599 args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 args->blkno2 = args->blkno; /* set 2nd entry info*/
601 args->index2 = args->index;
602 args->rmtblkno2 = args->rmtblkno;
603 args->rmtblkcnt2 = args->rmtblkcnt;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000604 args->rmtvaluelen2 = args->rmtvaluelen;
605
606 /*
607 * clear the remote attr state now that it is saved so that the
608 * values reflect the state of the attribute we are about to
609 * add, not the attribute we just found and will remove later.
610 */
611 args->rmtblkno = 0;
612 args->rmtblkcnt = 0;
613 args->rmtvaluelen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 }
615
616 /*
617 * Add the attribute to the leaf block, transitioning to a Btree
618 * if required.
619 */
Dave Chinner517c2222013-04-24 18:58:55 +1000620 retval = xfs_attr3_leaf_add(bp, args);
Dave Chinner24513372014-06-25 14:58:08 +1000621 if (retval == -ENOSPC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 /*
623 * Promote the attribute list to the Btree format, then
624 * Commit that transaction so that the node_addname() call
625 * can manage its own transactions.
626 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600627 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000628 error = xfs_attr3_leaf_to_node(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 if (!error) {
630 error = xfs_bmap_finish(&args->trans, args->flist,
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100631 &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 }
633 if (error) {
634 ASSERT(committed);
635 args->trans = NULL;
636 xfs_bmap_cancel(args->flist);
Eric Sandeend99831f2014-06-22 15:03:54 +1000637 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 }
639
640 /*
641 * bmap_finish() may have committed the last trans and started
642 * a new one. We need the inode to be in all transactions.
643 */
Christoph Hellwig898621d2010-06-24 11:36:58 +1000644 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +0000645 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647 /*
648 * Commit the current trans (including the inode) and start
649 * a new one.
650 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000651 error = xfs_trans_roll(&args->trans, dp);
652 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000653 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 /*
656 * Fob the whole rest of the problem off on the Btree code.
657 */
658 error = xfs_attr_node_addname(args);
Eric Sandeend99831f2014-06-22 15:03:54 +1000659 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 }
661
662 /*
663 * Commit the transaction that added the attr name so that
664 * later routines can manage their own transactions.
665 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000666 error = xfs_trans_roll(&args->trans, dp);
667 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000668 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670 /*
671 * If there was an out-of-line value, allocate the blocks we
672 * identified for its storage and copy the value. This is done
673 * after we create the attribute so that we don't overflow the
674 * maximum size of a transaction and/or hit a deadlock.
675 */
676 if (args->rmtblkno > 0) {
677 error = xfs_attr_rmtval_set(args);
678 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000679 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 }
681
682 /*
683 * If this is an atomic rename operation, we must "flip" the
684 * incomplete flags on the "new" and "old" attribute/value pairs
685 * so that one disappears and one appears atomically. Then we
686 * must remove the "old" attribute/value pair.
687 */
Barry Naujok6a178102008-05-21 16:42:05 +1000688 if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 /*
690 * In a separate transaction, set the incomplete flag on the
691 * "old" attr and clear the incomplete flag on the "new" attr.
692 */
Dave Chinner517c2222013-04-24 18:58:55 +1000693 error = xfs_attr3_leaf_flipflags(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000695 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 /*
698 * Dismantle the "old" attribute/value pair by removing
699 * a "remote" value (if it exists).
700 */
701 args->index = args->index2;
702 args->blkno = args->blkno2;
703 args->rmtblkno = args->rmtblkno2;
704 args->rmtblkcnt = args->rmtblkcnt2;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000705 args->rmtvaluelen = args->rmtvaluelen2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 if (args->rmtblkno) {
707 error = xfs_attr_rmtval_remove(args);
708 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000709 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 }
711
712 /*
713 * Read in the block containing the "old" attr, then
714 * remove the "old" attr from that block (neat, huh!)
715 */
Dave Chinner517c2222013-04-24 18:58:55 +1000716 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
Dave Chinnerad14c332012-11-12 22:54:16 +1100717 -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100719 return error;
720
Dave Chinner517c2222013-04-24 18:58:55 +1000721 xfs_attr3_leaf_remove(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 /*
724 * If the result is small enough, shrink it all into the inode.
725 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100726 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Eric Sandeen9d87c312009-01-14 23:22:07 -0600727 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000728 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 /* bp is gone due to xfs_da_shrink_inode */
730 if (!error) {
731 error = xfs_bmap_finish(&args->trans,
732 args->flist,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 &committed);
734 }
735 if (error) {
736 ASSERT(committed);
737 args->trans = NULL;
738 xfs_bmap_cancel(args->flist);
Eric Sandeend99831f2014-06-22 15:03:54 +1000739 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 }
741
742 /*
743 * bmap_finish() may have committed the last trans
744 * and started a new one. We need the inode to be
745 * in all transactions.
746 */
Christoph Hellwig898621d2010-06-24 11:36:58 +1000747 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +0000748 xfs_trans_ijoin(args->trans, dp, 0);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751 /*
752 * Commit the remove and start the next trans in series.
753 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000754 error = xfs_trans_roll(&args->trans, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756 } else if (args->rmtblkno > 0) {
757 /*
758 * Added a "remote" value, just clear the incomplete flag.
759 */
Dave Chinner517c2222013-04-24 18:58:55 +1000760 error = xfs_attr3_leaf_clearflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 }
Dave Chinner517c2222013-04-24 18:58:55 +1000762 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763}
764
765/*
766 * Remove a name from the leaf attribute list structure
767 *
768 * This leaf block cannot have a "remote" value, we only call this routine
769 * if bmap_one_block() says there is only one block (ie: no remote blks).
770 */
771STATIC int
772xfs_attr_leaf_removename(xfs_da_args_t *args)
773{
774 xfs_inode_t *dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000775 struct xfs_buf *bp;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100776 int error, committed, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Dave Chinner5a5881c2012-03-22 05:15:13 +0000778 trace_xfs_attr_leaf_removename(args);
779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 /*
781 * Remove the attribute.
782 */
783 dp = args->dp;
784 args->blkno = 0;
Dave Chinner517c2222013-04-24 18:58:55 +1000785 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Dave Chinnerad14c332012-11-12 22:54:16 +1100786 if (error)
787 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Dave Chinner517c2222013-04-24 18:58:55 +1000789 error = xfs_attr3_leaf_lookup_int(bp, args);
Dave Chinner24513372014-06-25 14:58:08 +1000790 if (error == -ENOATTR) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000791 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000792 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 }
794
Dave Chinner517c2222013-04-24 18:58:55 +1000795 xfs_attr3_leaf_remove(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797 /*
798 * If the result is small enough, shrink it all into the inode.
799 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100800 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Eric Sandeen9d87c312009-01-14 23:22:07 -0600801 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000802 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 /* bp is gone due to xfs_da_shrink_inode */
804 if (!error) {
805 error = xfs_bmap_finish(&args->trans, args->flist,
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100806 &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 }
808 if (error) {
809 ASSERT(committed);
810 args->trans = NULL;
811 xfs_bmap_cancel(args->flist);
Dave Chinner517c2222013-04-24 18:58:55 +1000812 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 }
814
815 /*
816 * bmap_finish() may have committed the last trans and started
817 * a new one. We need the inode to be in all transactions.
818 */
Christoph Hellwig898621d2010-06-24 11:36:58 +1000819 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +0000820 xfs_trans_ijoin(args->trans, dp, 0);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000821 }
Dave Chinner517c2222013-04-24 18:58:55 +1000822 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823}
824
825/*
826 * Look up a name in a leaf attribute list structure.
827 *
828 * This leaf block cannot have a "remote" value, we only call this routine
829 * if bmap_one_block() says there is only one block (ie: no remote blks).
830 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000831STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832xfs_attr_leaf_get(xfs_da_args_t *args)
833{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000834 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 int error;
836
Dave Chinneree732592012-11-12 22:53:53 +1100837 trace_xfs_attr_leaf_get(args);
838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 args->blkno = 0;
Dave Chinner517c2222013-04-24 18:58:55 +1000840 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100842 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Dave Chinner517c2222013-04-24 18:58:55 +1000844 error = xfs_attr3_leaf_lookup_int(bp, args);
Dave Chinner24513372014-06-25 14:58:08 +1000845 if (error != -EEXIST) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000846 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000847 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 }
Dave Chinner517c2222013-04-24 18:58:55 +1000849 error = xfs_attr3_leaf_getvalue(bp, args);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000850 xfs_trans_brelse(args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
852 error = xfs_attr_rmtval_get(args);
853 }
Dave Chinner517c2222013-04-24 18:58:55 +1000854 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855}
856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857/*========================================================================
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000858 * External routines when attribute list size > geo->blksize
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 *========================================================================*/
860
861/*
862 * Add a name to a Btree-format attribute list.
863 *
864 * This will involve walking down the Btree, and may involve splitting
865 * leaf nodes and even splitting intermediate nodes up to and including
866 * the root node (a special case of an intermediate node).
867 *
868 * "Remote" attribute values confuse the issue and atomic rename operations
869 * add a whole extra layer of confusion on top of that.
870 */
871STATIC int
872xfs_attr_node_addname(xfs_da_args_t *args)
873{
874 xfs_da_state_t *state;
875 xfs_da_state_blk_t *blk;
876 xfs_inode_t *dp;
877 xfs_mount_t *mp;
878 int committed, retval, error;
879
Dave Chinner5a5881c2012-03-22 05:15:13 +0000880 trace_xfs_attr_node_addname(args);
881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 /*
883 * Fill in bucket of arguments/results/context to carry around.
884 */
885 dp = args->dp;
886 mp = dp->i_mount;
887restart:
888 state = xfs_da_state_alloc();
889 state->args = args;
890 state->mp = mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
892 /*
893 * Search to see if name already exists, and get back a pointer
894 * to where it should go.
895 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000896 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 if (error)
898 goto out;
899 blk = &state->path.blk[ state->path.active-1 ];
900 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner24513372014-06-25 14:58:08 +1000901 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 goto out;
Dave Chinner24513372014-06-25 14:58:08 +1000903 } else if (retval == -EEXIST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 if (args->flags & ATTR_CREATE)
905 goto out;
Dave Chinner5a5881c2012-03-22 05:15:13 +0000906
907 trace_xfs_attr_node_replace(args);
908
Dave Chinner8275cdd2014-05-06 07:37:31 +1000909 /* save the attribute state for later removal*/
Barry Naujok6a178102008-05-21 16:42:05 +1000910 args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 args->blkno2 = args->blkno; /* set 2nd entry info*/
912 args->index2 = args->index;
913 args->rmtblkno2 = args->rmtblkno;
914 args->rmtblkcnt2 = args->rmtblkcnt;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000915 args->rmtvaluelen2 = args->rmtvaluelen;
916
917 /*
918 * clear the remote attr state now that it is saved so that the
919 * values reflect the state of the attribute we are about to
920 * add, not the attribute we just found and will remove later.
921 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 args->rmtblkno = 0;
923 args->rmtblkcnt = 0;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000924 args->rmtvaluelen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 }
926
Dave Chinner517c2222013-04-24 18:58:55 +1000927 retval = xfs_attr3_leaf_add(blk->bp, state->args);
Dave Chinner24513372014-06-25 14:58:08 +1000928 if (retval == -ENOSPC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 if (state->path.active == 1) {
930 /*
931 * Its really a single leaf node, but it had
932 * out-of-line values so it looked like it *might*
933 * have been a b-tree.
934 */
935 xfs_da_state_free(state);
Eric Sandeen6dd93e92013-07-31 20:18:54 -0500936 state = NULL;
Eric Sandeen9d87c312009-01-14 23:22:07 -0600937 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000938 error = xfs_attr3_leaf_to_node(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 if (!error) {
940 error = xfs_bmap_finish(&args->trans,
941 args->flist,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 &committed);
943 }
944 if (error) {
945 ASSERT(committed);
946 args->trans = NULL;
947 xfs_bmap_cancel(args->flist);
948 goto out;
949 }
950
951 /*
952 * bmap_finish() may have committed the last trans
953 * and started a new one. We need the inode to be
954 * in all transactions.
955 */
Christoph Hellwig898621d2010-06-24 11:36:58 +1000956 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +0000957 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 /*
960 * Commit the node conversion and start the next
961 * trans in the chain.
962 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000963 error = xfs_trans_roll(&args->trans, dp);
964 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 goto out;
966
967 goto restart;
968 }
969
970 /*
971 * Split as many Btree elements as required.
972 * This code tracks the new and old attr's location
973 * in the index/blkno/rmtblkno/rmtblkcnt fields and
974 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
975 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600976 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000977 error = xfs_da3_split(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 if (!error) {
979 error = xfs_bmap_finish(&args->trans, args->flist,
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100980 &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 }
982 if (error) {
983 ASSERT(committed);
984 args->trans = NULL;
985 xfs_bmap_cancel(args->flist);
986 goto out;
987 }
988
989 /*
990 * bmap_finish() may have committed the last trans and started
991 * a new one. We need the inode to be in all transactions.
992 */
Christoph Hellwig898621d2010-06-24 11:36:58 +1000993 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +0000994 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 } else {
996 /*
997 * Addition succeeded, update Btree hashvals.
998 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000999 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 }
1001
1002 /*
1003 * Kill the state structure, we're done with it and need to
1004 * allow the buffers to come back later.
1005 */
1006 xfs_da_state_free(state);
1007 state = NULL;
1008
1009 /*
1010 * Commit the leaf addition or btree split and start the next
1011 * trans in the chain.
1012 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10001013 error = xfs_trans_roll(&args->trans, dp);
1014 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 goto out;
1016
1017 /*
1018 * If there was an out-of-line value, allocate the blocks we
1019 * identified for its storage and copy the value. This is done
1020 * after we create the attribute so that we don't overflow the
1021 * maximum size of a transaction and/or hit a deadlock.
1022 */
1023 if (args->rmtblkno > 0) {
1024 error = xfs_attr_rmtval_set(args);
1025 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001026 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 }
1028
1029 /*
1030 * If this is an atomic rename operation, we must "flip" the
1031 * incomplete flags on the "new" and "old" attribute/value pairs
1032 * so that one disappears and one appears atomically. Then we
1033 * must remove the "old" attribute/value pair.
1034 */
Barry Naujok6a178102008-05-21 16:42:05 +10001035 if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 /*
1037 * In a separate transaction, set the incomplete flag on the
1038 * "old" attr and clear the incomplete flag on the "new" attr.
1039 */
Dave Chinner517c2222013-04-24 18:58:55 +10001040 error = xfs_attr3_leaf_flipflags(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 if (error)
1042 goto out;
1043
1044 /*
1045 * Dismantle the "old" attribute/value pair by removing
1046 * a "remote" value (if it exists).
1047 */
1048 args->index = args->index2;
1049 args->blkno = args->blkno2;
1050 args->rmtblkno = args->rmtblkno2;
1051 args->rmtblkcnt = args->rmtblkcnt2;
Dave Chinner8275cdd2014-05-06 07:37:31 +10001052 args->rmtvaluelen = args->rmtvaluelen2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 if (args->rmtblkno) {
1054 error = xfs_attr_rmtval_remove(args);
1055 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001056 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 }
1058
1059 /*
1060 * Re-find the "old" attribute entry after any split ops.
1061 * The INCOMPLETE flag means that we will find the "old"
1062 * attr, not the "new" one.
1063 */
1064 args->flags |= XFS_ATTR_INCOMPLETE;
1065 state = xfs_da_state_alloc();
1066 state->args = args;
1067 state->mp = mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 state->inleaf = 0;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001069 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 if (error)
1071 goto out;
1072
1073 /*
1074 * Remove the name and update the hashvals in the tree.
1075 */
1076 blk = &state->path.blk[ state->path.active-1 ];
1077 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner517c2222013-04-24 18:58:55 +10001078 error = xfs_attr3_leaf_remove(blk->bp, args);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001079 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
1081 /*
1082 * Check to see if the tree needs to be collapsed.
1083 */
1084 if (retval && (state->path.active > 1)) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001085 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001086 error = xfs_da3_join(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 if (!error) {
1088 error = xfs_bmap_finish(&args->trans,
1089 args->flist,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 &committed);
1091 }
1092 if (error) {
1093 ASSERT(committed);
1094 args->trans = NULL;
1095 xfs_bmap_cancel(args->flist);
1096 goto out;
1097 }
1098
1099 /*
1100 * bmap_finish() may have committed the last trans
1101 * and started a new one. We need the inode to be
1102 * in all transactions.
1103 */
Christoph Hellwig898621d2010-06-24 11:36:58 +10001104 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +00001105 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 }
1107
1108 /*
1109 * Commit and start the next trans in the chain.
1110 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10001111 error = xfs_trans_roll(&args->trans, dp);
1112 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 goto out;
1114
1115 } else if (args->rmtblkno > 0) {
1116 /*
1117 * Added a "remote" value, just clear the incomplete flag.
1118 */
Dave Chinner517c2222013-04-24 18:58:55 +10001119 error = xfs_attr3_leaf_clearflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 if (error)
1121 goto out;
1122 }
1123 retval = error = 0;
1124
1125out:
1126 if (state)
1127 xfs_da_state_free(state);
1128 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001129 return error;
1130 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131}
1132
1133/*
1134 * Remove a name from a B-tree attribute list.
1135 *
1136 * This will involve walking down the Btree, and may involve joining
1137 * leaf nodes and even joining intermediate nodes up to and including
1138 * the root node (a special case of an intermediate node).
1139 */
1140STATIC int
1141xfs_attr_node_removename(xfs_da_args_t *args)
1142{
1143 xfs_da_state_t *state;
1144 xfs_da_state_blk_t *blk;
1145 xfs_inode_t *dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001146 struct xfs_buf *bp;
Nathan Scottd8cc8902005-11-02 10:34:53 +11001147 int retval, error, committed, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Dave Chinner5a5881c2012-03-22 05:15:13 +00001149 trace_xfs_attr_node_removename(args);
1150
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 /*
1152 * Tie a string around our finger to remind us where we are.
1153 */
1154 dp = args->dp;
1155 state = xfs_da_state_alloc();
1156 state->args = args;
1157 state->mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
1159 /*
1160 * Search to see if name exists, and get back a pointer to it.
1161 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001162 error = xfs_da3_node_lookup_int(state, &retval);
Dave Chinner24513372014-06-25 14:58:08 +10001163 if (error || (retval != -EEXIST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 if (error == 0)
1165 error = retval;
1166 goto out;
1167 }
1168
1169 /*
1170 * If there is an out-of-line value, de-allocate the blocks.
1171 * This is done before we remove the attribute so that we don't
1172 * overflow the maximum size of a transaction and/or hit a deadlock.
1173 */
1174 blk = &state->path.blk[ state->path.active-1 ];
1175 ASSERT(blk->bp != NULL);
1176 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1177 if (args->rmtblkno > 0) {
1178 /*
1179 * Fill in disk block numbers in the state structure
1180 * so that we can get the buffers back after we commit
1181 * several transactions in the following calls.
1182 */
1183 error = xfs_attr_fillstate(state);
1184 if (error)
1185 goto out;
1186
1187 /*
1188 * Mark the attribute as INCOMPLETE, then bunmapi() the
1189 * remote value.
1190 */
Dave Chinner517c2222013-04-24 18:58:55 +10001191 error = xfs_attr3_leaf_setflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 if (error)
1193 goto out;
1194 error = xfs_attr_rmtval_remove(args);
1195 if (error)
1196 goto out;
1197
1198 /*
1199 * Refill the state structure with buffers, the prior calls
1200 * released our buffers.
1201 */
1202 error = xfs_attr_refillstate(state);
1203 if (error)
1204 goto out;
1205 }
1206
1207 /*
1208 * Remove the name and update the hashvals in the tree.
1209 */
1210 blk = &state->path.blk[ state->path.active-1 ];
1211 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner517c2222013-04-24 18:58:55 +10001212 retval = xfs_attr3_leaf_remove(blk->bp, args);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001213 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
1215 /*
1216 * Check to see if the tree needs to be collapsed.
1217 */
1218 if (retval && (state->path.active > 1)) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001219 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001220 error = xfs_da3_join(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 if (!error) {
1222 error = xfs_bmap_finish(&args->trans, args->flist,
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001223 &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 }
1225 if (error) {
1226 ASSERT(committed);
1227 args->trans = NULL;
1228 xfs_bmap_cancel(args->flist);
1229 goto out;
1230 }
1231
1232 /*
1233 * bmap_finish() may have committed the last trans and started
1234 * a new one. We need the inode to be in all transactions.
1235 */
Christoph Hellwig898621d2010-06-24 11:36:58 +10001236 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +00001237 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
1239 /*
1240 * Commit the Btree join operation and start a new trans.
1241 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10001242 error = xfs_trans_roll(&args->trans, dp);
1243 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 goto out;
1245 }
1246
1247 /*
1248 * If the result is small enough, push it all into the inode.
1249 */
1250 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
1251 /*
1252 * Have to get rid of the copy of this dabuf in the state.
1253 */
1254 ASSERT(state->path.active == 1);
1255 ASSERT(state->path.blk[0].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 state->path.blk[0].bp = NULL;
1257
Dave Chinner517c2222013-04-24 18:58:55 +10001258 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 if (error)
1260 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Nathan Scottd8cc8902005-11-02 10:34:53 +11001262 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001263 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +10001264 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 /* bp is gone due to xfs_da_shrink_inode */
1266 if (!error) {
1267 error = xfs_bmap_finish(&args->trans,
1268 args->flist,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 &committed);
1270 }
1271 if (error) {
1272 ASSERT(committed);
1273 args->trans = NULL;
1274 xfs_bmap_cancel(args->flist);
1275 goto out;
1276 }
1277
1278 /*
1279 * bmap_finish() may have committed the last trans
1280 * and started a new one. We need the inode to be
1281 * in all transactions.
1282 */
Christoph Hellwig898621d2010-06-24 11:36:58 +10001283 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +00001284 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 } else
Dave Chinner1d9025e2012-06-22 18:50:14 +10001286 xfs_trans_brelse(args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 }
1288 error = 0;
1289
1290out:
1291 xfs_da_state_free(state);
Eric Sandeend99831f2014-06-22 15:03:54 +10001292 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293}
1294
1295/*
1296 * Fill in the disk block numbers in the state structure for the buffers
1297 * that are attached to the state structure.
1298 * This is done so that we can quickly reattach ourselves to those buffers
Nathan Scottc41564b2006-03-29 08:55:14 +10001299 * after some set of transaction commits have released these buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 */
1301STATIC int
1302xfs_attr_fillstate(xfs_da_state_t *state)
1303{
1304 xfs_da_state_path_t *path;
1305 xfs_da_state_blk_t *blk;
1306 int level;
1307
Dave Chinneree732592012-11-12 22:53:53 +11001308 trace_xfs_attr_fillstate(state->args);
1309
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 /*
1311 * Roll down the "path" in the state structure, storing the on-disk
1312 * block number for those buffers in the "path".
1313 */
1314 path = &state->path;
1315 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1316 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1317 if (blk->bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001318 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 blk->bp = NULL;
1320 } else {
1321 blk->disk_blkno = 0;
1322 }
1323 }
1324
1325 /*
1326 * Roll down the "altpath" in the state structure, storing the on-disk
1327 * block number for those buffers in the "altpath".
1328 */
1329 path = &state->altpath;
1330 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1331 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1332 if (blk->bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001333 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 blk->bp = NULL;
1335 } else {
1336 blk->disk_blkno = 0;
1337 }
1338 }
1339
Eric Sandeend99831f2014-06-22 15:03:54 +10001340 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341}
1342
1343/*
1344 * Reattach the buffers to the state structure based on the disk block
1345 * numbers stored in the state structure.
Nathan Scottc41564b2006-03-29 08:55:14 +10001346 * This is done after some set of transaction commits have released those
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 * buffers from our grip.
1348 */
1349STATIC int
1350xfs_attr_refillstate(xfs_da_state_t *state)
1351{
1352 xfs_da_state_path_t *path;
1353 xfs_da_state_blk_t *blk;
1354 int level, error;
1355
Dave Chinneree732592012-11-12 22:53:53 +11001356 trace_xfs_attr_refillstate(state->args);
1357
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 /*
1359 * Roll down the "path" in the state structure, storing the on-disk
1360 * block number for those buffers in the "path".
1361 */
1362 path = &state->path;
1363 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1364 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1365 if (blk->disk_blkno) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001366 error = xfs_da3_node_read(state->args->trans,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 state->args->dp,
1368 blk->blkno, blk->disk_blkno,
Dave Chinnerd9392a42012-11-12 22:54:17 +11001369 &blk->bp, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001371 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 } else {
1373 blk->bp = NULL;
1374 }
1375 }
1376
1377 /*
1378 * Roll down the "altpath" in the state structure, storing the on-disk
1379 * block number for those buffers in the "altpath".
1380 */
1381 path = &state->altpath;
1382 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1383 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1384 if (blk->disk_blkno) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001385 error = xfs_da3_node_read(state->args->trans,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 state->args->dp,
1387 blk->blkno, blk->disk_blkno,
Dave Chinnerd9392a42012-11-12 22:54:17 +11001388 &blk->bp, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001390 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 } else {
1392 blk->bp = NULL;
1393 }
1394 }
1395
Eric Sandeend99831f2014-06-22 15:03:54 +10001396 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397}
1398
1399/*
1400 * Look up a filename in a node attribute list.
1401 *
1402 * This routine gets called for any attribute fork that has more than one
1403 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1404 * "remote" values taking up more blocks.
1405 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001406STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407xfs_attr_node_get(xfs_da_args_t *args)
1408{
1409 xfs_da_state_t *state;
1410 xfs_da_state_blk_t *blk;
1411 int error, retval;
1412 int i;
1413
Dave Chinneree732592012-11-12 22:53:53 +11001414 trace_xfs_attr_node_get(args);
1415
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 state = xfs_da_state_alloc();
1417 state->args = args;
1418 state->mp = args->dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
1420 /*
1421 * Search to see if name exists, and get back a pointer to it.
1422 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001423 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 if (error) {
1425 retval = error;
Dave Chinner24513372014-06-25 14:58:08 +10001426 } else if (retval == -EEXIST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 blk = &state->path.blk[ state->path.active-1 ];
1428 ASSERT(blk->bp != NULL);
1429 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1430
1431 /*
1432 * Get the value, local or "remote"
1433 */
Dave Chinner517c2222013-04-24 18:58:55 +10001434 retval = xfs_attr3_leaf_getvalue(blk->bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 if (!retval && (args->rmtblkno > 0)
1436 && !(args->flags & ATTR_KERNOVAL)) {
1437 retval = xfs_attr_rmtval_get(args);
1438 }
1439 }
1440
1441 /*
1442 * If not in a transaction, we have to release all the buffers.
1443 */
1444 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001445 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 state->path.blk[i].bp = NULL;
1447 }
1448
1449 xfs_da_state_free(state);
Eric Sandeend99831f2014-06-22 15:03:54 +10001450 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451}