blob: 470a22d190fbe0c8818cfa72b1fad9f8e7e089c1 [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_sb.h"
26#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_mount.h"
Dave Chinner57062782013-10-15 09:17:51 +110028#include "xfs_da_format.h"
Nathan Scotta844f452005-11-02 14:38:42 +110029#include "xfs_da_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110030#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110032#include "xfs_alloc.h"
Dave Chinner239880e2013-10-23 10:50:10 +110033#include "xfs_trans.h"
Nathan Scotta844f452005-11-02 14:38:42 +110034#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_bmap.h"
Dave Chinner68988112013-08-12 20:49:42 +100036#include "xfs_bmap_util.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110037#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include "xfs_attr.h"
39#include "xfs_attr_leaf.h"
Dave Chinner95920cd2013-04-03 16:11:27 +110040#include "xfs_attr_remote.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include "xfs_quota.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include "xfs_trans_space.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000044#include "xfs_trace.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110045#include "xfs_dinode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47/*
48 * xfs_attr.c
49 *
50 * Provide the external interfaces to manage attribute lists.
51 */
52
53/*========================================================================
54 * Function prototypes for the kernel.
55 *========================================================================*/
56
57/*
58 * Internal routines when attribute list fits inside the inode.
59 */
60STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
61
62/*
63 * Internal routines when attribute list is one block.
64 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100065STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066STATIC int xfs_attr_leaf_addname(xfs_da_args_t *args);
67STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69/*
70 * Internal routines when attribute list is more than one block.
71 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100072STATIC int xfs_attr_node_get(xfs_da_args_t *args);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073STATIC int xfs_attr_node_addname(xfs_da_args_t *args);
74STATIC int xfs_attr_node_removename(xfs_da_args_t *args);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
76STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Barry Naujoke8b0eba2008-04-22 17:34:31 +100079STATIC int
Christoph Hellwig67fd7182014-05-13 16:34:43 +100080xfs_attr_args_init(
81 struct xfs_da_args *args,
82 struct xfs_inode *dp,
83 const unsigned char *name,
84 int flags)
Barry Naujoke8b0eba2008-04-22 17:34:31 +100085{
Christoph Hellwig67fd7182014-05-13 16:34:43 +100086
87 if (!name)
Barry Naujoke8b0eba2008-04-22 17:34:31 +100088 return EINVAL;
Christoph Hellwig67fd7182014-05-13 16:34:43 +100089
90 memset(args, 0, sizeof(*args));
Dave Chinner0650b552014-06-06 15:01:58 +100091 args->geo = dp->i_mount->m_attr_geo;
Christoph Hellwig67fd7182014-05-13 16:34:43 +100092 args->whichfork = XFS_ATTR_FORK;
93 args->dp = dp;
94 args->flags = flags;
95 args->name = name;
96 args->namelen = strlen((const char *)name);
97 if (args->namelen >= MAXNAMELEN)
Barry Naujoke8b0eba2008-04-22 17:34:31 +100098 return EFAULT; /* match IRIX behaviour */
99
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000100 args->hashval = xfs_da_hashname(args->name, args->namelen);
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000101 return 0;
102}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Dave Chinnerabec5f22013-08-12 20:49:38 +1000104int
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +1000105xfs_inode_hasattr(
106 struct xfs_inode *ip)
107{
108 if (!XFS_IFORK_Q(ip) ||
109 (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
110 ip->i_d.di_anextents == 0))
111 return 0;
112 return 1;
113}
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115/*========================================================================
116 * Overall external interface routines.
117 *========================================================================*/
118
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000119int
120xfs_attr_get(
Christoph Hellwige82fa0c2009-11-14 16:17:20 +0000121 struct xfs_inode *ip,
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000122 const unsigned char *name,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100123 unsigned char *value,
Christoph Hellwige82fa0c2009-11-14 16:17:20 +0000124 int *valuelenp,
125 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000127 struct xfs_da_args args;
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000128 uint lock_mode;
129 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 XFS_STATS_INC(xs_attr_get);
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000134 return EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 if (!xfs_inode_hasattr(ip))
137 return ENOATTR;
138
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000139 error = xfs_attr_args_init(&args, ip, name, flags);
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000140 if (error)
141 return error;
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 args.value = value;
144 args.valuelen = *valuelenp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Christoph Hellwig683cb942013-12-06 12:30:15 -0800146 lock_mode = xfs_ilock_attr_map_shared(ip);
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000147 if (!xfs_inode_hasattr(ip))
148 error = ENOATTR;
149 else if (ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL)
150 error = xfs_attr_shortform_getvalue(&args);
151 else if (xfs_bmap_one_block(ip, XFS_ATTR_FORK))
152 error = xfs_attr_leaf_get(&args);
153 else
154 error = xfs_attr_node_get(&args);
Christoph Hellwig683cb942013-12-06 12:30:15 -0800155 xfs_iunlock(ip, lock_mode);
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000156
157 *valuelenp = args.valuelen;
158 return error == EEXIST ? 0 : error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000161/*
162 * Calculate how many blocks we need for the new attribute,
163 */
Eric Sandeen5d77c0d2009-11-19 15:52:00 +0000164STATIC int
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000165xfs_attr_calc_size(
Christoph Hellwig6c888af2014-05-13 16:40:19 +1000166 struct xfs_da_args *args,
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000167 int *local)
168{
Christoph Hellwig6c888af2014-05-13 16:40:19 +1000169 struct xfs_mount *mp = args->dp->i_mount;
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000170 int size;
171 int nblks;
172
173 /*
174 * Determine space new attribute will use, and if it would be
175 * "local" or "remote" (note: local != inline).
176 */
Dave Chinnerc59f0ad2014-06-06 15:21:27 +1000177 size = xfs_attr_leaf_newentsize(args, local);
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000178 nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
179 if (*local) {
Dave Chinner33a60392014-06-06 15:21:10 +1000180 if (size > (args->geo->blksize / 2)) {
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000181 /* Double split possible */
182 nblks *= 2;
183 }
184 } else {
185 /*
186 * Out of line attribute, cannot double split, but
187 * make room for the attribute value itself.
188 */
Dave Chinner2d6dcc62014-05-15 09:39:28 +1000189 uint dblocks = xfs_attr3_rmt_blocks(mp, args->valuelen);
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000190 nblks += dblocks;
191 nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
192 }
193
194 return nblks;
195}
196
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000197int
198xfs_attr_set(
199 struct xfs_inode *dp,
200 const unsigned char *name,
201 unsigned char *value,
202 int valuelen,
203 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Jie Liu3d3c8b52013-08-12 20:49:59 +1000205 struct xfs_mount *mp = dp->i_mount;
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000206 struct xfs_da_args args;
207 struct xfs_bmap_free flist;
Jie Liu3d3c8b52013-08-12 20:49:59 +1000208 struct xfs_trans_res tres;
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000209 xfs_fsblock_t firstblock;
Jie Liu3d3c8b52013-08-12 20:49:59 +1000210 int rsvd = (flags & ATTR_ROOT) != 0;
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000211 int error, err2, committed, local;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000213 XFS_STATS_INC(xs_attr_set);
214
215 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
216 return EIO;
217
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000218 error = xfs_attr_args_init(&args, dp, name, flags);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000219 if (error)
220 return error;
221
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000222 args.value = value;
223 args.valuelen = valuelen;
224 args.firstblock = &firstblock;
225 args.flist = &flist;
226 args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
Christoph Hellwig6c888af2014-05-13 16:40:19 +1000227 args.total = xfs_attr_calc_size(&args, &local);
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000228
Christoph Hellwig7d095252009-06-08 15:33:32 +0200229 error = xfs_qm_dqattach(dp, 0);
230 if (error)
231 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 /*
234 * If the inode doesn't have an attribute fork, add one.
235 * (inode must not be locked when we call this routine)
236 */
237 if (XFS_IFORK_Q(dp) == 0) {
Barry Naujoke5889e92007-02-10 18:35:58 +1100238 int sf_size = sizeof(xfs_attr_sf_hdr_t) +
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000239 XFS_ATTR_SF_ENTSIZE_BYNAME(args.namelen, valuelen);
Barry Naujoke5889e92007-02-10 18:35:58 +1100240
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000241 error = xfs_bmap_add_attrfork(dp, sf_size, rsvd);
242 if (error)
243 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 }
245
246 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 * Start our first transaction of the day.
248 *
249 * All future transactions during this code must be "chained" off
250 * this one via the trans_dup() call. All transactions will contain
251 * the inode, and the inode will always be marked with trans_ihold().
252 * Since the inode will be locked in all transactions, we must log
253 * the inode in every transaction to let it float upward through
254 * the log.
255 */
256 args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_SET);
257
258 /*
259 * Root fork attributes can use reserved data blocks for this
260 * operation if necessary
261 */
262
263 if (rsvd)
264 args.trans->t_flags |= XFS_TRANS_RESERVE;
265
Jie Liu3d3c8b52013-08-12 20:49:59 +1000266 tres.tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
267 M_RES(mp)->tr_attrsetrt.tr_logres * args.total;
268 tres.tr_logcount = XFS_ATTRSET_LOG_COUNT;
269 tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
270 error = xfs_trans_reserve(args.trans, &tres, args.total, 0);
Jeff Liua21cd502013-01-28 21:27:53 +0800271 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 xfs_trans_cancel(args.trans, 0);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000273 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 }
275 xfs_ilock(dp, XFS_ILOCK_EXCL);
276
Christoph Hellwig7d095252009-06-08 15:33:32 +0200277 error = xfs_trans_reserve_quota_nblks(args.trans, dp, args.total, 0,
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000278 rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
279 XFS_QMOPT_RES_REGBLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if (error) {
281 xfs_iunlock(dp, XFS_ILOCK_EXCL);
282 xfs_trans_cancel(args.trans, XFS_TRANS_RELEASE_LOG_RES);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000283 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 }
285
Christoph Hellwigddc34152011-09-19 15:00:54 +0000286 xfs_trans_ijoin(args.trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000289 * If the attribute list is non-existent or a shortform list,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 * upgrade it to a single-leaf-block attribute list.
291 */
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000292 if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL ||
293 (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
294 dp->i_d.di_anextents == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296 /*
297 * Build initial attribute list (if required).
298 */
299 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
Nathan Scottd8cc8902005-11-02 10:34:53 +1100300 xfs_attr_shortform_create(&args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 /*
303 * Try to add the attr to the attribute list in
304 * the inode.
305 */
306 error = xfs_attr_shortform_addname(&args);
307 if (error != ENOSPC) {
308 /*
309 * Commit the shortform mods, and we're done.
310 * NOTE: this is also the error path (EEXIST, etc).
311 */
312 ASSERT(args.trans != NULL);
313
314 /*
315 * If this is a synchronous mount, make sure that
316 * the transaction goes to disk before returning
317 * to the user.
318 */
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000319 if (mp->m_flags & XFS_MOUNT_WSYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 xfs_trans_set_sync(args.trans);
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000321
322 if (!error && (flags & ATTR_KERNOTIME) == 0) {
323 xfs_trans_ichgtime(args.trans, dp,
324 XFS_ICHGTIME_CHG);
325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 err2 = xfs_trans_commit(args.trans,
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000327 XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 xfs_iunlock(dp, XFS_ILOCK_EXCL);
329
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000330 return error ? error : err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 }
332
333 /*
334 * It won't fit in the shortform, transform to a leaf block.
335 * GROT: another possible req'mt for a double-split btree op.
336 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600337 xfs_bmap_init(args.flist, args.firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 error = xfs_attr_shortform_to_leaf(&args);
339 if (!error) {
340 error = xfs_bmap_finish(&args.trans, args.flist,
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100341 &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
343 if (error) {
344 ASSERT(committed);
345 args.trans = NULL;
346 xfs_bmap_cancel(&flist);
347 goto out;
348 }
349
350 /*
351 * bmap_finish() may have committed the last trans and started
352 * a new one. We need the inode to be in all transactions.
353 */
Christoph Hellwig898621d2010-06-24 11:36:58 +1000354 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +0000355 xfs_trans_ijoin(args.trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 /*
358 * Commit the leaf transformation. We'll need another (linked)
359 * transaction to add the new attribute to the leaf.
360 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000361
362 error = xfs_trans_roll(&args.trans, dp);
363 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 goto out;
365
366 }
367
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000368 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 error = xfs_attr_leaf_addname(&args);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000370 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 error = xfs_attr_node_addname(&args);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000372 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 /*
376 * If this is a synchronous mount, make sure that the
377 * transaction goes to disk before returning to the user.
378 */
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000379 if (mp->m_flags & XFS_MOUNT_WSYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 xfs_trans_set_sync(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000382 if ((flags & ATTR_KERNOTIME) == 0)
383 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 /*
386 * Commit the last in the sequence of transactions.
387 */
388 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000389 error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 xfs_iunlock(dp, XFS_ILOCK_EXCL);
391
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000392 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
394out:
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000395 if (args.trans) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 xfs_trans_cancel(args.trans,
397 XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000400 return error;
Nathan Scottaa82daa2005-11-02 10:33:33 +1100401}
402
403/*
404 * Generic handler routine to remove a name from an attribute list.
405 * Transitions attribute list from Btree to shortform as necessary.
406 */
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000407int
408xfs_attr_remove(
409 struct xfs_inode *dp,
410 const unsigned char *name,
411 int flags)
Nathan Scottaa82daa2005-11-02 10:33:33 +1100412{
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000413 struct xfs_mount *mp = dp->i_mount;
414 struct xfs_da_args args;
415 struct xfs_bmap_free flist;
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000416 xfs_fsblock_t firstblock;
417 int error;
Nathan Scottaa82daa2005-11-02 10:33:33 +1100418
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000419 XFS_STATS_INC(xs_attr_remove);
420
421 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
422 return EIO;
423
424 if (!xfs_inode_hasattr(dp))
425 return ENOATTR;
426
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000427 error = xfs_attr_args_init(&args, dp, name, flags);
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000428 if (error)
429 return error;
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 args.firstblock = &firstblock;
432 args.flist = &flist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 /*
Dave Chinner4a338212011-06-23 01:35:01 +0000435 * we have no control over the attribute names that userspace passes us
436 * to remove, so we have to allow the name lookup prior to attribute
437 * removal to fail.
438 */
439 args.op_flags = XFS_DA_OP_OKNOENT;
440
Christoph Hellwig7d095252009-06-08 15:33:32 +0200441 error = xfs_qm_dqattach(dp, 0);
442 if (error)
443 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 /*
446 * Start our first transaction of the day.
447 *
448 * All future transactions during this code must be "chained" off
449 * this one via the trans_dup() call. All transactions will contain
450 * the inode, and the inode will always be marked with trans_ihold().
451 * Since the inode will be locked in all transactions, we must log
452 * the inode in every transaction to let it float upward through
453 * the log.
454 */
455 args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_RM);
456
457 /*
458 * Root fork attributes can use reserved data blocks for this
459 * operation if necessary
460 */
461
462 if (flags & ATTR_ROOT)
463 args.trans->t_flags |= XFS_TRANS_RESERVE;
464
Jie Liu3d3c8b52013-08-12 20:49:59 +1000465 error = xfs_trans_reserve(args.trans, &M_RES(mp)->tr_attrrm,
466 XFS_ATTRRM_SPACE_RES(mp), 0);
467 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 xfs_trans_cancel(args.trans, 0);
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000469 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 }
471
472 xfs_ilock(dp, XFS_ILOCK_EXCL);
473 /*
474 * No need to make quota reservations here. We expect to release some
475 * blocks not allocate in the common case.
476 */
Christoph Hellwigddc34152011-09-19 15:00:54 +0000477 xfs_trans_ijoin(args.trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +1000479 if (!xfs_inode_hasattr(dp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 error = XFS_ERROR(ENOATTR);
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000481 } else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
483 error = xfs_attr_shortform_remove(&args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
485 error = xfs_attr_leaf_removename(&args);
486 } else {
487 error = xfs_attr_node_removename(&args);
488 }
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000489
490 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 /*
494 * If this is a synchronous mount, make sure that the
495 * transaction goes to disk before returning to the user.
496 */
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000497 if (mp->m_flags & XFS_MOUNT_WSYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 xfs_trans_set_sync(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000500 if ((flags & ATTR_KERNOTIME) == 0)
501 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 /*
504 * Commit the last in the sequence of transactions.
505 */
506 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000507 error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 xfs_iunlock(dp, XFS_ILOCK_EXCL);
509
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000510 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512out:
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000513 if (args.trans) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 xfs_trans_cancel(args.trans,
515 XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
Nathan Scottaa82daa2005-11-02 10:33:33 +1100516 }
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000517 xfs_iunlock(dp, XFS_ILOCK_EXCL);
518 return error;
Nathan Scottaa82daa2005-11-02 10:33:33 +1100519}
520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521/*========================================================================
522 * External routines when attribute list is inside the inode
523 *========================================================================*/
524
525/*
526 * Add a name to the shortform attribute list structure
527 * This is the external routine.
528 */
529STATIC int
530xfs_attr_shortform_addname(xfs_da_args_t *args)
531{
Nathan Scottd8cc8902005-11-02 10:34:53 +1100532 int newsize, forkoff, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Dave Chinner5a5881c2012-03-22 05:15:13 +0000534 trace_xfs_attr_sf_addname(args);
535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 retval = xfs_attr_shortform_lookup(args);
537 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
538 return(retval);
539 } else if (retval == EEXIST) {
540 if (args->flags & ATTR_CREATE)
541 return(retval);
542 retval = xfs_attr_shortform_remove(args);
543 ASSERT(retval == 0);
544 }
545
Nathan Scottd8cc8902005-11-02 10:34:53 +1100546 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
547 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
548 return(XFS_ERROR(ENOSPC));
549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
551 newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100552
553 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
554 if (!forkoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 return(XFS_ERROR(ENOSPC));
Nathan Scottd8cc8902005-11-02 10:34:53 +1100556
557 xfs_attr_shortform_add(args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 return(0);
559}
560
561
562/*========================================================================
563 * External routines when attribute list is one block
564 *========================================================================*/
565
566/*
567 * Add a name to the leaf attribute list structure
568 *
569 * This leaf block cannot have a "remote" value, we only call this routine
570 * if bmap_one_block() says there is only one block (ie: no remote blks).
571 */
David Chinnera8272ce2007-11-23 16:28:09 +1100572STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573xfs_attr_leaf_addname(xfs_da_args_t *args)
574{
575 xfs_inode_t *dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000576 struct xfs_buf *bp;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100577 int retval, error, committed, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Dave Chinner5a5881c2012-03-22 05:15:13 +0000579 trace_xfs_attr_leaf_addname(args);
580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 /*
582 * Read the (only) block in the attribute list in.
583 */
584 dp = args->dp;
585 args->blkno = 0;
Dave Chinner517c2222013-04-24 18:58:55 +1000586 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100588 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590 /*
591 * Look up the given attribute in the leaf block. Figure out if
592 * the given flags produce an error or call for an atomic rename.
593 */
Dave Chinner517c2222013-04-24 18:58:55 +1000594 retval = xfs_attr3_leaf_lookup_int(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000596 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000597 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 } else if (retval == EEXIST) {
599 if (args->flags & ATTR_CREATE) { /* pure create op */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000600 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000601 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 }
Dave Chinner5a5881c2012-03-22 05:15:13 +0000603
604 trace_xfs_attr_leaf_replace(args);
605
Dave Chinner8275cdd2014-05-06 07:37:31 +1000606 /* save the attribute state for later removal*/
Barry Naujok6a178102008-05-21 16:42:05 +1000607 args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 args->blkno2 = args->blkno; /* set 2nd entry info*/
609 args->index2 = args->index;
610 args->rmtblkno2 = args->rmtblkno;
611 args->rmtblkcnt2 = args->rmtblkcnt;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000612 args->rmtvaluelen2 = args->rmtvaluelen;
613
614 /*
615 * clear the remote attr state now that it is saved so that the
616 * values reflect the state of the attribute we are about to
617 * add, not the attribute we just found and will remove later.
618 */
619 args->rmtblkno = 0;
620 args->rmtblkcnt = 0;
621 args->rmtvaluelen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 }
623
624 /*
625 * Add the attribute to the leaf block, transitioning to a Btree
626 * if required.
627 */
Dave Chinner517c2222013-04-24 18:58:55 +1000628 retval = xfs_attr3_leaf_add(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 if (retval == ENOSPC) {
630 /*
631 * Promote the attribute list to the Btree format, then
632 * Commit that transaction so that the node_addname() call
633 * can manage its own transactions.
634 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600635 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000636 error = xfs_attr3_leaf_to_node(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 if (!error) {
638 error = xfs_bmap_finish(&args->trans, args->flist,
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100639 &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 }
641 if (error) {
642 ASSERT(committed);
643 args->trans = NULL;
644 xfs_bmap_cancel(args->flist);
645 return(error);
646 }
647
648 /*
649 * bmap_finish() may have committed the last trans and started
650 * a new one. We need the inode to be in all transactions.
651 */
Christoph Hellwig898621d2010-06-24 11:36:58 +1000652 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +0000653 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 /*
656 * Commit the current trans (including the inode) and start
657 * a new one.
658 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000659 error = xfs_trans_roll(&args->trans, dp);
660 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 return (error);
662
663 /*
664 * Fob the whole rest of the problem off on the Btree code.
665 */
666 error = xfs_attr_node_addname(args);
667 return(error);
668 }
669
670 /*
671 * Commit the transaction that added the attr name so that
672 * later routines can manage their own transactions.
673 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000674 error = xfs_trans_roll(&args->trans, dp);
675 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 return (error);
677
678 /*
679 * If there was an out-of-line value, allocate the blocks we
680 * identified for its storage and copy the value. This is done
681 * after we create the attribute so that we don't overflow the
682 * maximum size of a transaction and/or hit a deadlock.
683 */
684 if (args->rmtblkno > 0) {
685 error = xfs_attr_rmtval_set(args);
686 if (error)
687 return(error);
688 }
689
690 /*
691 * If this is an atomic rename operation, we must "flip" the
692 * incomplete flags on the "new" and "old" attribute/value pairs
693 * so that one disappears and one appears atomically. Then we
694 * must remove the "old" attribute/value pair.
695 */
Barry Naujok6a178102008-05-21 16:42:05 +1000696 if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 /*
698 * In a separate transaction, set the incomplete flag on the
699 * "old" attr and clear the incomplete flag on the "new" attr.
700 */
Dave Chinner517c2222013-04-24 18:58:55 +1000701 error = xfs_attr3_leaf_flipflags(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 if (error)
703 return(error);
704
705 /*
706 * Dismantle the "old" attribute/value pair by removing
707 * a "remote" value (if it exists).
708 */
709 args->index = args->index2;
710 args->blkno = args->blkno2;
711 args->rmtblkno = args->rmtblkno2;
712 args->rmtblkcnt = args->rmtblkcnt2;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000713 args->rmtvaluelen = args->rmtvaluelen2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 if (args->rmtblkno) {
715 error = xfs_attr_rmtval_remove(args);
716 if (error)
717 return(error);
718 }
719
720 /*
721 * Read in the block containing the "old" attr, then
722 * remove the "old" attr from that block (neat, huh!)
723 */
Dave Chinner517c2222013-04-24 18:58:55 +1000724 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
Dave Chinnerad14c332012-11-12 22:54:16 +1100725 -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100727 return error;
728
Dave Chinner517c2222013-04-24 18:58:55 +1000729 xfs_attr3_leaf_remove(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731 /*
732 * If the result is small enough, shrink it all into the inode.
733 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100734 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Eric Sandeen9d87c312009-01-14 23:22:07 -0600735 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000736 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 /* bp is gone due to xfs_da_shrink_inode */
738 if (!error) {
739 error = xfs_bmap_finish(&args->trans,
740 args->flist,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 &committed);
742 }
743 if (error) {
744 ASSERT(committed);
745 args->trans = NULL;
746 xfs_bmap_cancel(args->flist);
747 return(error);
748 }
749
750 /*
751 * bmap_finish() may have committed the last trans
752 * and started a new one. We need the inode to be
753 * in all transactions.
754 */
Christoph Hellwig898621d2010-06-24 11:36:58 +1000755 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +0000756 xfs_trans_ijoin(args->trans, dp, 0);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 /*
760 * Commit the remove and start the next trans in series.
761 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000762 error = xfs_trans_roll(&args->trans, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764 } else if (args->rmtblkno > 0) {
765 /*
766 * Added a "remote" value, just clear the incomplete flag.
767 */
Dave Chinner517c2222013-04-24 18:58:55 +1000768 error = xfs_attr3_leaf_clearflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 }
Dave Chinner517c2222013-04-24 18:58:55 +1000770 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771}
772
773/*
774 * Remove a name from the leaf attribute list structure
775 *
776 * This leaf block cannot have a "remote" value, we only call this routine
777 * if bmap_one_block() says there is only one block (ie: no remote blks).
778 */
779STATIC int
780xfs_attr_leaf_removename(xfs_da_args_t *args)
781{
782 xfs_inode_t *dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000783 struct xfs_buf *bp;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100784 int error, committed, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Dave Chinner5a5881c2012-03-22 05:15:13 +0000786 trace_xfs_attr_leaf_removename(args);
787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 /*
789 * Remove the attribute.
790 */
791 dp = args->dp;
792 args->blkno = 0;
Dave Chinner517c2222013-04-24 18:58:55 +1000793 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Dave Chinnerad14c332012-11-12 22:54:16 +1100794 if (error)
795 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Dave Chinner517c2222013-04-24 18:58:55 +1000797 error = xfs_attr3_leaf_lookup_int(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (error == ENOATTR) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000799 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000800 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 }
802
Dave Chinner517c2222013-04-24 18:58:55 +1000803 xfs_attr3_leaf_remove(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
805 /*
806 * If the result is small enough, shrink it all into the inode.
807 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100808 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Eric Sandeen9d87c312009-01-14 23:22:07 -0600809 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000810 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 /* bp is gone due to xfs_da_shrink_inode */
812 if (!error) {
813 error = xfs_bmap_finish(&args->trans, args->flist,
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100814 &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 }
816 if (error) {
817 ASSERT(committed);
818 args->trans = NULL;
819 xfs_bmap_cancel(args->flist);
Dave Chinner517c2222013-04-24 18:58:55 +1000820 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 }
822
823 /*
824 * bmap_finish() may have committed the last trans and started
825 * a new one. We need the inode to be in all transactions.
826 */
Christoph Hellwig898621d2010-06-24 11:36:58 +1000827 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +0000828 xfs_trans_ijoin(args->trans, dp, 0);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000829 }
Dave Chinner517c2222013-04-24 18:58:55 +1000830 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831}
832
833/*
834 * Look up a name in a leaf attribute list structure.
835 *
836 * This leaf block cannot have a "remote" value, we only call this routine
837 * if bmap_one_block() says there is only one block (ie: no remote blks).
838 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000839STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840xfs_attr_leaf_get(xfs_da_args_t *args)
841{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000842 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 int error;
844
Dave Chinneree732592012-11-12 22:53:53 +1100845 trace_xfs_attr_leaf_get(args);
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 args->blkno = 0;
Dave Chinner517c2222013-04-24 18:58:55 +1000848 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100850 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Dave Chinner517c2222013-04-24 18:58:55 +1000852 error = xfs_attr3_leaf_lookup_int(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 if (error != EEXIST) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000854 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000855 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 }
Dave Chinner517c2222013-04-24 18:58:55 +1000857 error = xfs_attr3_leaf_getvalue(bp, args);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000858 xfs_trans_brelse(args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
860 error = xfs_attr_rmtval_get(args);
861 }
Dave Chinner517c2222013-04-24 18:58:55 +1000862 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863}
864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865/*========================================================================
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000866 * External routines when attribute list size > geo->blksize
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 *========================================================================*/
868
869/*
870 * Add a name to a Btree-format attribute list.
871 *
872 * This will involve walking down the Btree, and may involve splitting
873 * leaf nodes and even splitting intermediate nodes up to and including
874 * the root node (a special case of an intermediate node).
875 *
876 * "Remote" attribute values confuse the issue and atomic rename operations
877 * add a whole extra layer of confusion on top of that.
878 */
879STATIC int
880xfs_attr_node_addname(xfs_da_args_t *args)
881{
882 xfs_da_state_t *state;
883 xfs_da_state_blk_t *blk;
884 xfs_inode_t *dp;
885 xfs_mount_t *mp;
886 int committed, retval, error;
887
Dave Chinner5a5881c2012-03-22 05:15:13 +0000888 trace_xfs_attr_node_addname(args);
889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 /*
891 * Fill in bucket of arguments/results/context to carry around.
892 */
893 dp = args->dp;
894 mp = dp->i_mount;
895restart:
896 state = xfs_da_state_alloc();
897 state->args = args;
898 state->mp = mp;
Dave Chinner33a60392014-06-06 15:21:10 +1000899 state->blocksize = args->geo->blksize;
Dave Chinner7ab610f2014-06-06 15:20:02 +1000900 state->node_ents = args->geo->node_ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
902 /*
903 * Search to see if name already exists, and get back a pointer
904 * to where it should go.
905 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000906 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 if (error)
908 goto out;
909 blk = &state->path.blk[ state->path.active-1 ];
910 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
911 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
912 goto out;
913 } else if (retval == EEXIST) {
914 if (args->flags & ATTR_CREATE)
915 goto out;
Dave Chinner5a5881c2012-03-22 05:15:13 +0000916
917 trace_xfs_attr_node_replace(args);
918
Dave Chinner8275cdd2014-05-06 07:37:31 +1000919 /* save the attribute state for later removal*/
Barry Naujok6a178102008-05-21 16:42:05 +1000920 args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 args->blkno2 = args->blkno; /* set 2nd entry info*/
922 args->index2 = args->index;
923 args->rmtblkno2 = args->rmtblkno;
924 args->rmtblkcnt2 = args->rmtblkcnt;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000925 args->rmtvaluelen2 = args->rmtvaluelen;
926
927 /*
928 * clear the remote attr state now that it is saved so that the
929 * values reflect the state of the attribute we are about to
930 * add, not the attribute we just found and will remove later.
931 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 args->rmtblkno = 0;
933 args->rmtblkcnt = 0;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000934 args->rmtvaluelen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 }
936
Dave Chinner517c2222013-04-24 18:58:55 +1000937 retval = xfs_attr3_leaf_add(blk->bp, state->args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 if (retval == ENOSPC) {
939 if (state->path.active == 1) {
940 /*
941 * Its really a single leaf node, but it had
942 * out-of-line values so it looked like it *might*
943 * have been a b-tree.
944 */
945 xfs_da_state_free(state);
Eric Sandeen6dd93e92013-07-31 20:18:54 -0500946 state = NULL;
Eric Sandeen9d87c312009-01-14 23:22:07 -0600947 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000948 error = xfs_attr3_leaf_to_node(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 if (!error) {
950 error = xfs_bmap_finish(&args->trans,
951 args->flist,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 &committed);
953 }
954 if (error) {
955 ASSERT(committed);
956 args->trans = NULL;
957 xfs_bmap_cancel(args->flist);
958 goto out;
959 }
960
961 /*
962 * bmap_finish() may have committed the last trans
963 * and started a new one. We need the inode to be
964 * in all transactions.
965 */
Christoph Hellwig898621d2010-06-24 11:36:58 +1000966 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +0000967 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
969 /*
970 * Commit the node conversion and start the next
971 * trans in the chain.
972 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000973 error = xfs_trans_roll(&args->trans, dp);
974 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 goto out;
976
977 goto restart;
978 }
979
980 /*
981 * Split as many Btree elements as required.
982 * This code tracks the new and old attr's location
983 * in the index/blkno/rmtblkno/rmtblkcnt fields and
984 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
985 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600986 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000987 error = xfs_da3_split(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 if (!error) {
989 error = xfs_bmap_finish(&args->trans, args->flist,
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100990 &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 }
992 if (error) {
993 ASSERT(committed);
994 args->trans = NULL;
995 xfs_bmap_cancel(args->flist);
996 goto out;
997 }
998
999 /*
1000 * bmap_finish() may have committed the last trans and started
1001 * a new one. We need the inode to be in all transactions.
1002 */
Christoph Hellwig898621d2010-06-24 11:36:58 +10001003 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +00001004 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 } else {
1006 /*
1007 * Addition succeeded, update Btree hashvals.
1008 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001009 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 }
1011
1012 /*
1013 * Kill the state structure, we're done with it and need to
1014 * allow the buffers to come back later.
1015 */
1016 xfs_da_state_free(state);
1017 state = NULL;
1018
1019 /*
1020 * Commit the leaf addition or btree split and start the next
1021 * trans in the chain.
1022 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10001023 error = xfs_trans_roll(&args->trans, dp);
1024 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 goto out;
1026
1027 /*
1028 * If there was an out-of-line value, allocate the blocks we
1029 * identified for its storage and copy the value. This is done
1030 * after we create the attribute so that we don't overflow the
1031 * maximum size of a transaction and/or hit a deadlock.
1032 */
1033 if (args->rmtblkno > 0) {
1034 error = xfs_attr_rmtval_set(args);
1035 if (error)
1036 return(error);
1037 }
1038
1039 /*
1040 * If this is an atomic rename operation, we must "flip" the
1041 * incomplete flags on the "new" and "old" attribute/value pairs
1042 * so that one disappears and one appears atomically. Then we
1043 * must remove the "old" attribute/value pair.
1044 */
Barry Naujok6a178102008-05-21 16:42:05 +10001045 if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 /*
1047 * In a separate transaction, set the incomplete flag on the
1048 * "old" attr and clear the incomplete flag on the "new" attr.
1049 */
Dave Chinner517c2222013-04-24 18:58:55 +10001050 error = xfs_attr3_leaf_flipflags(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 if (error)
1052 goto out;
1053
1054 /*
1055 * Dismantle the "old" attribute/value pair by removing
1056 * a "remote" value (if it exists).
1057 */
1058 args->index = args->index2;
1059 args->blkno = args->blkno2;
1060 args->rmtblkno = args->rmtblkno2;
1061 args->rmtblkcnt = args->rmtblkcnt2;
Dave Chinner8275cdd2014-05-06 07:37:31 +10001062 args->rmtvaluelen = args->rmtvaluelen2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 if (args->rmtblkno) {
1064 error = xfs_attr_rmtval_remove(args);
1065 if (error)
1066 return(error);
1067 }
1068
1069 /*
1070 * Re-find the "old" attribute entry after any split ops.
1071 * The INCOMPLETE flag means that we will find the "old"
1072 * attr, not the "new" one.
1073 */
1074 args->flags |= XFS_ATTR_INCOMPLETE;
1075 state = xfs_da_state_alloc();
1076 state->args = args;
1077 state->mp = mp;
Dave Chinner33a60392014-06-06 15:21:10 +10001078 state->blocksize = args->geo->blksize;
Dave Chinner7ab610f2014-06-06 15:20:02 +10001079 state->node_ents = args->geo->node_ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 state->inleaf = 0;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001081 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 if (error)
1083 goto out;
1084
1085 /*
1086 * Remove the name and update the hashvals in the tree.
1087 */
1088 blk = &state->path.blk[ state->path.active-1 ];
1089 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner517c2222013-04-24 18:58:55 +10001090 error = xfs_attr3_leaf_remove(blk->bp, args);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001091 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
1093 /*
1094 * Check to see if the tree needs to be collapsed.
1095 */
1096 if (retval && (state->path.active > 1)) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001097 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001098 error = xfs_da3_join(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 if (!error) {
1100 error = xfs_bmap_finish(&args->trans,
1101 args->flist,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 &committed);
1103 }
1104 if (error) {
1105 ASSERT(committed);
1106 args->trans = NULL;
1107 xfs_bmap_cancel(args->flist);
1108 goto out;
1109 }
1110
1111 /*
1112 * bmap_finish() may have committed the last trans
1113 * and started a new one. We need the inode to be
1114 * in all transactions.
1115 */
Christoph Hellwig898621d2010-06-24 11:36:58 +10001116 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +00001117 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 }
1119
1120 /*
1121 * Commit and start the next trans in the chain.
1122 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10001123 error = xfs_trans_roll(&args->trans, dp);
1124 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 goto out;
1126
1127 } else if (args->rmtblkno > 0) {
1128 /*
1129 * Added a "remote" value, just clear the incomplete flag.
1130 */
Dave Chinner517c2222013-04-24 18:58:55 +10001131 error = xfs_attr3_leaf_clearflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 if (error)
1133 goto out;
1134 }
1135 retval = error = 0;
1136
1137out:
1138 if (state)
1139 xfs_da_state_free(state);
1140 if (error)
1141 return(error);
1142 return(retval);
1143}
1144
1145/*
1146 * Remove a name from a B-tree attribute list.
1147 *
1148 * This will involve walking down the Btree, and may involve joining
1149 * leaf nodes and even joining intermediate nodes up to and including
1150 * the root node (a special case of an intermediate node).
1151 */
1152STATIC int
1153xfs_attr_node_removename(xfs_da_args_t *args)
1154{
1155 xfs_da_state_t *state;
1156 xfs_da_state_blk_t *blk;
1157 xfs_inode_t *dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001158 struct xfs_buf *bp;
Nathan Scottd8cc8902005-11-02 10:34:53 +11001159 int retval, error, committed, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Dave Chinner5a5881c2012-03-22 05:15:13 +00001161 trace_xfs_attr_node_removename(args);
1162
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 /*
1164 * Tie a string around our finger to remind us where we are.
1165 */
1166 dp = args->dp;
1167 state = xfs_da_state_alloc();
1168 state->args = args;
1169 state->mp = dp->i_mount;
Dave Chinner33a60392014-06-06 15:21:10 +10001170 state->blocksize = args->geo->blksize;
Dave Chinner7ab610f2014-06-06 15:20:02 +10001171 state->node_ents = args->geo->node_ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
1173 /*
1174 * Search to see if name exists, and get back a pointer to it.
1175 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001176 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 if (error || (retval != EEXIST)) {
1178 if (error == 0)
1179 error = retval;
1180 goto out;
1181 }
1182
1183 /*
1184 * If there is an out-of-line value, de-allocate the blocks.
1185 * This is done before we remove the attribute so that we don't
1186 * overflow the maximum size of a transaction and/or hit a deadlock.
1187 */
1188 blk = &state->path.blk[ state->path.active-1 ];
1189 ASSERT(blk->bp != NULL);
1190 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1191 if (args->rmtblkno > 0) {
1192 /*
1193 * Fill in disk block numbers in the state structure
1194 * so that we can get the buffers back after we commit
1195 * several transactions in the following calls.
1196 */
1197 error = xfs_attr_fillstate(state);
1198 if (error)
1199 goto out;
1200
1201 /*
1202 * Mark the attribute as INCOMPLETE, then bunmapi() the
1203 * remote value.
1204 */
Dave Chinner517c2222013-04-24 18:58:55 +10001205 error = xfs_attr3_leaf_setflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 if (error)
1207 goto out;
1208 error = xfs_attr_rmtval_remove(args);
1209 if (error)
1210 goto out;
1211
1212 /*
1213 * Refill the state structure with buffers, the prior calls
1214 * released our buffers.
1215 */
1216 error = xfs_attr_refillstate(state);
1217 if (error)
1218 goto out;
1219 }
1220
1221 /*
1222 * Remove the name and update the hashvals in the tree.
1223 */
1224 blk = &state->path.blk[ state->path.active-1 ];
1225 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner517c2222013-04-24 18:58:55 +10001226 retval = xfs_attr3_leaf_remove(blk->bp, args);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001227 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
1229 /*
1230 * Check to see if the tree needs to be collapsed.
1231 */
1232 if (retval && (state->path.active > 1)) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001233 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001234 error = xfs_da3_join(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 if (!error) {
1236 error = xfs_bmap_finish(&args->trans, args->flist,
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001237 &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 }
1239 if (error) {
1240 ASSERT(committed);
1241 args->trans = NULL;
1242 xfs_bmap_cancel(args->flist);
1243 goto out;
1244 }
1245
1246 /*
1247 * bmap_finish() may have committed the last trans and started
1248 * a new one. We need the inode to be in all transactions.
1249 */
Christoph Hellwig898621d2010-06-24 11:36:58 +10001250 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +00001251 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
1253 /*
1254 * Commit the Btree join operation and start a new trans.
1255 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10001256 error = xfs_trans_roll(&args->trans, dp);
1257 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 goto out;
1259 }
1260
1261 /*
1262 * If the result is small enough, push it all into the inode.
1263 */
1264 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
1265 /*
1266 * Have to get rid of the copy of this dabuf in the state.
1267 */
1268 ASSERT(state->path.active == 1);
1269 ASSERT(state->path.blk[0].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 state->path.blk[0].bp = NULL;
1271
Dave Chinner517c2222013-04-24 18:58:55 +10001272 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 if (error)
1274 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Nathan Scottd8cc8902005-11-02 10:34:53 +11001276 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001277 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +10001278 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 /* bp is gone due to xfs_da_shrink_inode */
1280 if (!error) {
1281 error = xfs_bmap_finish(&args->trans,
1282 args->flist,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 &committed);
1284 }
1285 if (error) {
1286 ASSERT(committed);
1287 args->trans = NULL;
1288 xfs_bmap_cancel(args->flist);
1289 goto out;
1290 }
1291
1292 /*
1293 * bmap_finish() may have committed the last trans
1294 * and started a new one. We need the inode to be
1295 * in all transactions.
1296 */
Christoph Hellwig898621d2010-06-24 11:36:58 +10001297 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +00001298 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 } else
Dave Chinner1d9025e2012-06-22 18:50:14 +10001300 xfs_trans_brelse(args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 }
1302 error = 0;
1303
1304out:
1305 xfs_da_state_free(state);
1306 return(error);
1307}
1308
1309/*
1310 * Fill in the disk block numbers in the state structure for the buffers
1311 * that are attached to the state structure.
1312 * This is done so that we can quickly reattach ourselves to those buffers
Nathan Scottc41564b2006-03-29 08:55:14 +10001313 * after some set of transaction commits have released these buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 */
1315STATIC int
1316xfs_attr_fillstate(xfs_da_state_t *state)
1317{
1318 xfs_da_state_path_t *path;
1319 xfs_da_state_blk_t *blk;
1320 int level;
1321
Dave Chinneree732592012-11-12 22:53:53 +11001322 trace_xfs_attr_fillstate(state->args);
1323
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 /*
1325 * Roll down the "path" in the state structure, storing the on-disk
1326 * block number for those buffers in the "path".
1327 */
1328 path = &state->path;
1329 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1330 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1331 if (blk->bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001332 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 blk->bp = NULL;
1334 } else {
1335 blk->disk_blkno = 0;
1336 }
1337 }
1338
1339 /*
1340 * Roll down the "altpath" in the state structure, storing the on-disk
1341 * block number for those buffers in the "altpath".
1342 */
1343 path = &state->altpath;
1344 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1345 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1346 if (blk->bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001347 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 blk->bp = NULL;
1349 } else {
1350 blk->disk_blkno = 0;
1351 }
1352 }
1353
1354 return(0);
1355}
1356
1357/*
1358 * Reattach the buffers to the state structure based on the disk block
1359 * numbers stored in the state structure.
Nathan Scottc41564b2006-03-29 08:55:14 +10001360 * This is done after some set of transaction commits have released those
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 * buffers from our grip.
1362 */
1363STATIC int
1364xfs_attr_refillstate(xfs_da_state_t *state)
1365{
1366 xfs_da_state_path_t *path;
1367 xfs_da_state_blk_t *blk;
1368 int level, error;
1369
Dave Chinneree732592012-11-12 22:53:53 +11001370 trace_xfs_attr_refillstate(state->args);
1371
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 /*
1373 * Roll down the "path" in the state structure, storing the on-disk
1374 * block number for those buffers in the "path".
1375 */
1376 path = &state->path;
1377 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1378 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1379 if (blk->disk_blkno) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001380 error = xfs_da3_node_read(state->args->trans,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 state->args->dp,
1382 blk->blkno, blk->disk_blkno,
Dave Chinnerd9392a42012-11-12 22:54:17 +11001383 &blk->bp, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 if (error)
1385 return(error);
1386 } else {
1387 blk->bp = NULL;
1388 }
1389 }
1390
1391 /*
1392 * Roll down the "altpath" in the state structure, storing the on-disk
1393 * block number for those buffers in the "altpath".
1394 */
1395 path = &state->altpath;
1396 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1397 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1398 if (blk->disk_blkno) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001399 error = xfs_da3_node_read(state->args->trans,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 state->args->dp,
1401 blk->blkno, blk->disk_blkno,
Dave Chinnerd9392a42012-11-12 22:54:17 +11001402 &blk->bp, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 if (error)
1404 return(error);
1405 } else {
1406 blk->bp = NULL;
1407 }
1408 }
1409
1410 return(0);
1411}
1412
1413/*
1414 * Look up a filename in a node attribute list.
1415 *
1416 * This routine gets called for any attribute fork that has more than one
1417 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1418 * "remote" values taking up more blocks.
1419 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001420STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421xfs_attr_node_get(xfs_da_args_t *args)
1422{
1423 xfs_da_state_t *state;
1424 xfs_da_state_blk_t *blk;
1425 int error, retval;
1426 int i;
1427
Dave Chinneree732592012-11-12 22:53:53 +11001428 trace_xfs_attr_node_get(args);
1429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 state = xfs_da_state_alloc();
1431 state->args = args;
1432 state->mp = args->dp->i_mount;
Dave Chinner33a60392014-06-06 15:21:10 +10001433 state->blocksize = args->geo->blksize;
Dave Chinner7ab610f2014-06-06 15:20:02 +10001434 state->node_ents = args->geo->node_ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
1436 /*
1437 * Search to see if name exists, and get back a pointer to it.
1438 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001439 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 if (error) {
1441 retval = error;
1442 } else if (retval == EEXIST) {
1443 blk = &state->path.blk[ state->path.active-1 ];
1444 ASSERT(blk->bp != NULL);
1445 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1446
1447 /*
1448 * Get the value, local or "remote"
1449 */
Dave Chinner517c2222013-04-24 18:58:55 +10001450 retval = xfs_attr3_leaf_getvalue(blk->bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 if (!retval && (args->rmtblkno > 0)
1452 && !(args->flags & ATTR_KERNOVAL)) {
1453 retval = xfs_attr_rmtval_get(args);
1454 }
1455 }
1456
1457 /*
1458 * If not in a transaction, we have to release all the buffers.
1459 */
1460 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001461 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 state->path.blk[i].bp = NULL;
1463 }
1464
1465 xfs_da_state_free(state);
1466 return(retval);
1467}