blob: 120862139a63c2859b6d53da2579a7c4525f7d79 [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));
91 args->whichfork = XFS_ATTR_FORK;
92 args->dp = dp;
93 args->flags = flags;
94 args->name = name;
95 args->namelen = strlen((const char *)name);
96 if (args->namelen >= MAXNAMELEN)
Barry Naujoke8b0eba2008-04-22 17:34:31 +100097 return EFAULT; /* match IRIX behaviour */
98
Christoph Hellwig67fd7182014-05-13 16:34:43 +100099 args->hashval = xfs_da_hashname(args->name, args->namelen);
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000100 return 0;
101}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Dave Chinnerabec5f22013-08-12 20:49:38 +1000103int
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +1000104xfs_inode_hasattr(
105 struct xfs_inode *ip)
106{
107 if (!XFS_IFORK_Q(ip) ||
108 (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
109 ip->i_d.di_anextents == 0))
110 return 0;
111 return 1;
112}
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114/*========================================================================
115 * Overall external interface routines.
116 *========================================================================*/
117
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000118int
119xfs_attr_get(
Christoph Hellwige82fa0c2009-11-14 16:17:20 +0000120 struct xfs_inode *ip,
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000121 const unsigned char *name,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100122 unsigned char *value,
Christoph Hellwige82fa0c2009-11-14 16:17:20 +0000123 int *valuelenp,
124 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000126 struct xfs_da_args args;
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000127 uint lock_mode;
128 int error;
129
130 XFS_STATS_INC(xs_attr_get);
131
132 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
133 return EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +1000135 if (!xfs_inode_hasattr(ip))
136 return ENOATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000138 error = xfs_attr_args_init(&args, ip, name, flags);
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000139 if (error)
140 return error;
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 args.value = value;
143 args.valuelen = *valuelenp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Christoph Hellwig683cb942013-12-06 12:30:15 -0800145 lock_mode = xfs_ilock_attr_map_shared(ip);
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000146 if (!xfs_inode_hasattr(ip))
147 error = ENOATTR;
148 else if (ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL)
149 error = xfs_attr_shortform_getvalue(&args);
150 else if (xfs_bmap_one_block(ip, XFS_ATTR_FORK))
151 error = xfs_attr_leaf_get(&args);
152 else
153 error = xfs_attr_node_get(&args);
Christoph Hellwig683cb942013-12-06 12:30:15 -0800154 xfs_iunlock(ip, lock_mode);
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000155
156 *valuelenp = args.valuelen;
157 return error == EEXIST ? 0 : error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158}
159
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000160/*
161 * Calculate how many blocks we need for the new attribute,
162 */
Eric Sandeen5d77c0d2009-11-19 15:52:00 +0000163STATIC int
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000164xfs_attr_calc_size(
165 struct xfs_inode *ip,
166 int namelen,
167 int valuelen,
168 int *local)
169{
170 struct xfs_mount *mp = ip->i_mount;
171 int size;
172 int nblks;
173
174 /*
175 * Determine space new attribute will use, and if it would be
176 * "local" or "remote" (note: local != inline).
177 */
178 size = xfs_attr_leaf_newentsize(namelen, valuelen,
179 mp->m_sb.sb_blocksize, local);
180
181 nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
182 if (*local) {
183 if (size > (mp->m_sb.sb_blocksize >> 1)) {
184 /* Double split possible */
185 nblks *= 2;
186 }
187 } else {
188 /*
189 * Out of line attribute, cannot double split, but
190 * make room for the attribute value itself.
191 */
192 uint dblocks = XFS_B_TO_FSB(mp, valuelen);
193 nblks += dblocks;
194 nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
195 }
196
197 return nblks;
198}
199
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000200int
201xfs_attr_set(
202 struct xfs_inode *dp,
203 const unsigned char *name,
204 unsigned char *value,
205 int valuelen,
206 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
Jie Liu3d3c8b52013-08-12 20:49:59 +1000208 struct xfs_mount *mp = dp->i_mount;
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000209 struct xfs_da_args args;
210 struct xfs_bmap_free flist;
Jie Liu3d3c8b52013-08-12 20:49:59 +1000211 struct xfs_trans_res tres;
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000212 xfs_fsblock_t firstblock;
Jie Liu3d3c8b52013-08-12 20:49:59 +1000213 int rsvd = (flags & ATTR_ROOT) != 0;
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000214 int error, err2, committed, local;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000216 XFS_STATS_INC(xs_attr_set);
217
218 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
219 return EIO;
220
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000221 error = xfs_attr_args_init(&args, dp, name, flags);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000222 if (error)
223 return error;
224
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000225 args.value = value;
226 args.valuelen = valuelen;
227 args.firstblock = &firstblock;
228 args.flist = &flist;
229 args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
230
231 /* Size is now blocks for attribute data */
232 args.total = xfs_attr_calc_size(dp, args.namelen, valuelen, &local);
233
Christoph Hellwig7d095252009-06-08 15:33:32 +0200234 error = xfs_qm_dqattach(dp, 0);
235 if (error)
236 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238 /*
239 * If the inode doesn't have an attribute fork, add one.
240 * (inode must not be locked when we call this routine)
241 */
242 if (XFS_IFORK_Q(dp) == 0) {
Barry Naujoke5889e92007-02-10 18:35:58 +1100243 int sf_size = sizeof(xfs_attr_sf_hdr_t) +
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000244 XFS_ATTR_SF_ENTSIZE_BYNAME(args.namelen, valuelen);
Barry Naujoke5889e92007-02-10 18:35:58 +1100245
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000246 error = xfs_bmap_add_attrfork(dp, sf_size, rsvd);
247 if (error)
248 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 }
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 /*
252 * Start our first transaction of the day.
253 *
254 * All future transactions during this code must be "chained" off
255 * this one via the trans_dup() call. All transactions will contain
256 * the inode, and the inode will always be marked with trans_ihold().
257 * Since the inode will be locked in all transactions, we must log
258 * the inode in every transaction to let it float upward through
259 * the log.
260 */
261 args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_SET);
262
263 /*
264 * Root fork attributes can use reserved data blocks for this
265 * operation if necessary
266 */
267
268 if (rsvd)
269 args.trans->t_flags |= XFS_TRANS_RESERVE;
270
Jie Liu3d3c8b52013-08-12 20:49:59 +1000271 tres.tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
272 M_RES(mp)->tr_attrsetrt.tr_logres * args.total;
273 tres.tr_logcount = XFS_ATTRSET_LOG_COUNT;
274 tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
275 error = xfs_trans_reserve(args.trans, &tres, args.total, 0);
Jeff Liua21cd502013-01-28 21:27:53 +0800276 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 xfs_trans_cancel(args.trans, 0);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000278 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 }
280 xfs_ilock(dp, XFS_ILOCK_EXCL);
281
Christoph Hellwig7d095252009-06-08 15:33:32 +0200282 error = xfs_trans_reserve_quota_nblks(args.trans, dp, args.total, 0,
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000283 rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
284 XFS_QMOPT_RES_REGBLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 if (error) {
286 xfs_iunlock(dp, XFS_ILOCK_EXCL);
287 xfs_trans_cancel(args.trans, XFS_TRANS_RELEASE_LOG_RES);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000288 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
290
Christoph Hellwigddc34152011-09-19 15:00:54 +0000291 xfs_trans_ijoin(args.trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000294 * If the attribute list is non-existent or a shortform list,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 * upgrade it to a single-leaf-block attribute list.
296 */
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000297 if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL ||
298 (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
299 dp->i_d.di_anextents == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 /*
302 * Build initial attribute list (if required).
303 */
304 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
Nathan Scottd8cc8902005-11-02 10:34:53 +1100305 xfs_attr_shortform_create(&args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307 /*
308 * Try to add the attr to the attribute list in
309 * the inode.
310 */
311 error = xfs_attr_shortform_addname(&args);
312 if (error != ENOSPC) {
313 /*
314 * Commit the shortform mods, and we're done.
315 * NOTE: this is also the error path (EEXIST, etc).
316 */
317 ASSERT(args.trans != NULL);
318
319 /*
320 * If this is a synchronous mount, make sure that
321 * the transaction goes to disk before returning
322 * to the user.
323 */
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000324 if (mp->m_flags & XFS_MOUNT_WSYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 xfs_trans_set_sync(args.trans);
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000326
327 if (!error && (flags & ATTR_KERNOTIME) == 0) {
328 xfs_trans_ichgtime(args.trans, dp,
329 XFS_ICHGTIME_CHG);
330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 err2 = xfs_trans_commit(args.trans,
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000332 XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 xfs_iunlock(dp, XFS_ILOCK_EXCL);
334
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000335 return error ? error : err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
337
338 /*
339 * It won't fit in the shortform, transform to a leaf block.
340 * GROT: another possible req'mt for a double-split btree op.
341 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600342 xfs_bmap_init(args.flist, args.firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 error = xfs_attr_shortform_to_leaf(&args);
344 if (!error) {
345 error = xfs_bmap_finish(&args.trans, args.flist,
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100346 &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
348 if (error) {
349 ASSERT(committed);
350 args.trans = NULL;
351 xfs_bmap_cancel(&flist);
352 goto out;
353 }
354
355 /*
356 * bmap_finish() may have committed the last trans and started
357 * a new one. We need the inode to be in all transactions.
358 */
Christoph Hellwig898621d2010-06-24 11:36:58 +1000359 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +0000360 xfs_trans_ijoin(args.trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 /*
363 * Commit the leaf transformation. We'll need another (linked)
364 * transaction to add the new attribute to the leaf.
365 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000366
367 error = xfs_trans_roll(&args.trans, dp);
368 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 goto out;
370
371 }
372
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000373 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 error = xfs_attr_leaf_addname(&args);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000375 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 error = xfs_attr_node_addname(&args);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000377 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380 /*
381 * If this is a synchronous mount, make sure that the
382 * transaction goes to disk before returning to the user.
383 */
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000384 if (mp->m_flags & XFS_MOUNT_WSYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 xfs_trans_set_sync(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000387 if ((flags & ATTR_KERNOTIME) == 0)
388 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 /*
391 * Commit the last in the sequence of transactions.
392 */
393 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000394 error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 xfs_iunlock(dp, XFS_ILOCK_EXCL);
396
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000397 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399out:
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000400 if (args.trans) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 xfs_trans_cancel(args.trans,
402 XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000405 return error;
Nathan Scottaa82daa2005-11-02 10:33:33 +1100406}
407
408/*
409 * Generic handler routine to remove a name from an attribute list.
410 * Transitions attribute list from Btree to shortform as necessary.
411 */
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000412int
413xfs_attr_remove(
414 struct xfs_inode *dp,
415 const unsigned char *name,
416 int flags)
Nathan Scottaa82daa2005-11-02 10:33:33 +1100417{
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000418 struct xfs_mount *mp = dp->i_mount;
419 struct xfs_da_args args;
420 struct xfs_bmap_free flist;
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000421 xfs_fsblock_t firstblock;
422 int error;
Nathan Scottaa82daa2005-11-02 10:33:33 +1100423
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000424 XFS_STATS_INC(xs_attr_remove);
425
426 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
427 return EIO;
428
429 if (!xfs_inode_hasattr(dp))
430 return ENOATTR;
431
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000432 error = xfs_attr_args_init(&args, dp, name, flags);
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000433 if (error)
434 return error;
435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 args.firstblock = &firstblock;
437 args.flist = &flist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 /*
Dave Chinner4a338212011-06-23 01:35:01 +0000440 * we have no control over the attribute names that userspace passes us
441 * to remove, so we have to allow the name lookup prior to attribute
442 * removal to fail.
443 */
444 args.op_flags = XFS_DA_OP_OKNOENT;
445
Christoph Hellwig7d095252009-06-08 15:33:32 +0200446 error = xfs_qm_dqattach(dp, 0);
447 if (error)
448 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 /*
451 * Start our first transaction of the day.
452 *
453 * All future transactions during this code must be "chained" off
454 * this one via the trans_dup() call. All transactions will contain
455 * the inode, and the inode will always be marked with trans_ihold().
456 * Since the inode will be locked in all transactions, we must log
457 * the inode in every transaction to let it float upward through
458 * the log.
459 */
460 args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_RM);
461
462 /*
463 * Root fork attributes can use reserved data blocks for this
464 * operation if necessary
465 */
466
467 if (flags & ATTR_ROOT)
468 args.trans->t_flags |= XFS_TRANS_RESERVE;
469
Jie Liu3d3c8b52013-08-12 20:49:59 +1000470 error = xfs_trans_reserve(args.trans, &M_RES(mp)->tr_attrrm,
471 XFS_ATTRRM_SPACE_RES(mp), 0);
472 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 xfs_trans_cancel(args.trans, 0);
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000474 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 }
476
477 xfs_ilock(dp, XFS_ILOCK_EXCL);
478 /*
479 * No need to make quota reservations here. We expect to release some
480 * blocks not allocate in the common case.
481 */
Christoph Hellwigddc34152011-09-19 15:00:54 +0000482 xfs_trans_ijoin(args.trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +1000484 if (!xfs_inode_hasattr(dp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 error = XFS_ERROR(ENOATTR);
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000486 } else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
488 error = xfs_attr_shortform_remove(&args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
490 error = xfs_attr_leaf_removename(&args);
491 } else {
492 error = xfs_attr_node_removename(&args);
493 }
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000494
495 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 /*
499 * If this is a synchronous mount, make sure that the
500 * transaction goes to disk before returning to the user.
501 */
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000502 if (mp->m_flags & XFS_MOUNT_WSYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 xfs_trans_set_sync(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000505 if ((flags & ATTR_KERNOTIME) == 0)
506 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 /*
509 * Commit the last in the sequence of transactions.
510 */
511 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000512 error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 xfs_iunlock(dp, XFS_ILOCK_EXCL);
514
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000515 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517out:
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000518 if (args.trans) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 xfs_trans_cancel(args.trans,
520 XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
Nathan Scottaa82daa2005-11-02 10:33:33 +1100521 }
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000522 xfs_iunlock(dp, XFS_ILOCK_EXCL);
523 return error;
Nathan Scottaa82daa2005-11-02 10:33:33 +1100524}
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526/*========================================================================
527 * External routines when attribute list is inside the inode
528 *========================================================================*/
529
530/*
531 * Add a name to the shortform attribute list structure
532 * This is the external routine.
533 */
534STATIC int
535xfs_attr_shortform_addname(xfs_da_args_t *args)
536{
Nathan Scottd8cc8902005-11-02 10:34:53 +1100537 int newsize, forkoff, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Dave Chinner5a5881c2012-03-22 05:15:13 +0000539 trace_xfs_attr_sf_addname(args);
540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 retval = xfs_attr_shortform_lookup(args);
542 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
543 return(retval);
544 } else if (retval == EEXIST) {
545 if (args->flags & ATTR_CREATE)
546 return(retval);
547 retval = xfs_attr_shortform_remove(args);
548 ASSERT(retval == 0);
549 }
550
Nathan Scottd8cc8902005-11-02 10:34:53 +1100551 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
552 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
553 return(XFS_ERROR(ENOSPC));
554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
556 newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100557
558 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
559 if (!forkoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 return(XFS_ERROR(ENOSPC));
Nathan Scottd8cc8902005-11-02 10:34:53 +1100561
562 xfs_attr_shortform_add(args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 return(0);
564}
565
566
567/*========================================================================
568 * External routines when attribute list is one block
569 *========================================================================*/
570
571/*
572 * Add a name to the leaf attribute list structure
573 *
574 * This leaf block cannot have a "remote" value, we only call this routine
575 * if bmap_one_block() says there is only one block (ie: no remote blks).
576 */
David Chinnera8272ce2007-11-23 16:28:09 +1100577STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578xfs_attr_leaf_addname(xfs_da_args_t *args)
579{
580 xfs_inode_t *dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000581 struct xfs_buf *bp;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100582 int retval, error, committed, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Dave Chinner5a5881c2012-03-22 05:15:13 +0000584 trace_xfs_attr_leaf_addname(args);
585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 /*
587 * Read the (only) block in the attribute list in.
588 */
589 dp = args->dp;
590 args->blkno = 0;
Dave Chinner517c2222013-04-24 18:58:55 +1000591 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100593 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 /*
596 * Look up the given attribute in the leaf block. Figure out if
597 * the given flags produce an error or call for an atomic rename.
598 */
Dave Chinner517c2222013-04-24 18:58:55 +1000599 retval = xfs_attr3_leaf_lookup_int(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000601 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000602 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 } else if (retval == EEXIST) {
604 if (args->flags & ATTR_CREATE) { /* pure create op */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000605 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000606 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 }
Dave Chinner5a5881c2012-03-22 05:15:13 +0000608
609 trace_xfs_attr_leaf_replace(args);
610
Barry Naujok6a178102008-05-21 16:42:05 +1000611 args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 args->blkno2 = args->blkno; /* set 2nd entry info*/
613 args->index2 = args->index;
614 args->rmtblkno2 = args->rmtblkno;
615 args->rmtblkcnt2 = args->rmtblkcnt;
616 }
617
618 /*
619 * Add the attribute to the leaf block, transitioning to a Btree
620 * if required.
621 */
Dave Chinner517c2222013-04-24 18:58:55 +1000622 retval = xfs_attr3_leaf_add(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 if (retval == ENOSPC) {
624 /*
625 * Promote the attribute list to the Btree format, then
626 * Commit that transaction so that the node_addname() call
627 * can manage its own transactions.
628 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600629 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000630 error = xfs_attr3_leaf_to_node(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 if (!error) {
632 error = xfs_bmap_finish(&args->trans, args->flist,
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100633 &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 }
635 if (error) {
636 ASSERT(committed);
637 args->trans = NULL;
638 xfs_bmap_cancel(args->flist);
639 return(error);
640 }
641
642 /*
643 * bmap_finish() may have committed the last trans and started
644 * a new one. We need the inode to be in all transactions.
645 */
Christoph Hellwig898621d2010-06-24 11:36:58 +1000646 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +0000647 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
649 /*
650 * Commit the current trans (including the inode) and start
651 * a new one.
652 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000653 error = xfs_trans_roll(&args->trans, dp);
654 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return (error);
656
657 /*
658 * Fob the whole rest of the problem off on the Btree code.
659 */
660 error = xfs_attr_node_addname(args);
661 return(error);
662 }
663
664 /*
665 * Commit the transaction that added the attr name so that
666 * later routines can manage their own transactions.
667 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000668 error = xfs_trans_roll(&args->trans, dp);
669 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 return (error);
671
672 /*
673 * If there was an out-of-line value, allocate the blocks we
674 * identified for its storage and copy the value. This is done
675 * after we create the attribute so that we don't overflow the
676 * maximum size of a transaction and/or hit a deadlock.
677 */
678 if (args->rmtblkno > 0) {
679 error = xfs_attr_rmtval_set(args);
680 if (error)
681 return(error);
682 }
683
684 /*
685 * If this is an atomic rename operation, we must "flip" the
686 * incomplete flags on the "new" and "old" attribute/value pairs
687 * so that one disappears and one appears atomically. Then we
688 * must remove the "old" attribute/value pair.
689 */
Barry Naujok6a178102008-05-21 16:42:05 +1000690 if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 /*
692 * In a separate transaction, set the incomplete flag on the
693 * "old" attr and clear the incomplete flag on the "new" attr.
694 */
Dave Chinner517c2222013-04-24 18:58:55 +1000695 error = xfs_attr3_leaf_flipflags(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 if (error)
697 return(error);
698
699 /*
700 * Dismantle the "old" attribute/value pair by removing
701 * a "remote" value (if it exists).
702 */
703 args->index = args->index2;
704 args->blkno = args->blkno2;
705 args->rmtblkno = args->rmtblkno2;
706 args->rmtblkcnt = args->rmtblkcnt2;
707 if (args->rmtblkno) {
708 error = xfs_attr_rmtval_remove(args);
709 if (error)
710 return(error);
711 }
712
713 /*
714 * Read in the block containing the "old" attr, then
715 * remove the "old" attr from that block (neat, huh!)
716 */
Dave Chinner517c2222013-04-24 18:58:55 +1000717 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
Dave Chinnerad14c332012-11-12 22:54:16 +1100718 -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100720 return error;
721
Dave Chinner517c2222013-04-24 18:58:55 +1000722 xfs_attr3_leaf_remove(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724 /*
725 * If the result is small enough, shrink it all into the inode.
726 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100727 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Eric Sandeen9d87c312009-01-14 23:22:07 -0600728 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000729 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 /* bp is gone due to xfs_da_shrink_inode */
731 if (!error) {
732 error = xfs_bmap_finish(&args->trans,
733 args->flist,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 &committed);
735 }
736 if (error) {
737 ASSERT(committed);
738 args->trans = NULL;
739 xfs_bmap_cancel(args->flist);
740 return(error);
741 }
742
743 /*
744 * bmap_finish() may have committed the last trans
745 * and started a new one. We need the inode to be
746 * in all transactions.
747 */
Christoph Hellwig898621d2010-06-24 11:36:58 +1000748 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +0000749 xfs_trans_ijoin(args->trans, dp, 0);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
752 /*
753 * Commit the remove and start the next trans in series.
754 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000755 error = xfs_trans_roll(&args->trans, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757 } else if (args->rmtblkno > 0) {
758 /*
759 * Added a "remote" value, just clear the incomplete flag.
760 */
Dave Chinner517c2222013-04-24 18:58:55 +1000761 error = xfs_attr3_leaf_clearflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 }
Dave Chinner517c2222013-04-24 18:58:55 +1000763 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
765
766/*
767 * Remove a name from the leaf attribute list structure
768 *
769 * This leaf block cannot have a "remote" value, we only call this routine
770 * if bmap_one_block() says there is only one block (ie: no remote blks).
771 */
772STATIC int
773xfs_attr_leaf_removename(xfs_da_args_t *args)
774{
775 xfs_inode_t *dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000776 struct xfs_buf *bp;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100777 int error, committed, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Dave Chinner5a5881c2012-03-22 05:15:13 +0000779 trace_xfs_attr_leaf_removename(args);
780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 /*
782 * Remove the attribute.
783 */
784 dp = args->dp;
785 args->blkno = 0;
Dave Chinner517c2222013-04-24 18:58:55 +1000786 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Dave Chinnerad14c332012-11-12 22:54:16 +1100787 if (error)
788 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Dave Chinner517c2222013-04-24 18:58:55 +1000790 error = xfs_attr3_leaf_lookup_int(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 if (error == ENOATTR) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000792 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000793 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 }
795
Dave Chinner517c2222013-04-24 18:58:55 +1000796 xfs_attr3_leaf_remove(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798 /*
799 * If the result is small enough, shrink it all into the inode.
800 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100801 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Eric Sandeen9d87c312009-01-14 23:22:07 -0600802 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000803 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 /* bp is gone due to xfs_da_shrink_inode */
805 if (!error) {
806 error = xfs_bmap_finish(&args->trans, args->flist,
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100807 &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 }
809 if (error) {
810 ASSERT(committed);
811 args->trans = NULL;
812 xfs_bmap_cancel(args->flist);
Dave Chinner517c2222013-04-24 18:58:55 +1000813 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 }
815
816 /*
817 * bmap_finish() may have committed the last trans and started
818 * a new one. We need the inode to be in all transactions.
819 */
Christoph Hellwig898621d2010-06-24 11:36:58 +1000820 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +0000821 xfs_trans_ijoin(args->trans, dp, 0);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000822 }
Dave Chinner517c2222013-04-24 18:58:55 +1000823 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824}
825
826/*
827 * Look up a name in a leaf attribute list structure.
828 *
829 * This leaf block cannot have a "remote" value, we only call this routine
830 * if bmap_one_block() says there is only one block (ie: no remote blks).
831 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000832STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833xfs_attr_leaf_get(xfs_da_args_t *args)
834{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000835 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 int error;
837
Dave Chinneree732592012-11-12 22:53:53 +1100838 trace_xfs_attr_leaf_get(args);
839
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 args->blkno = 0;
Dave Chinner517c2222013-04-24 18:58:55 +1000841 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100843 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Dave Chinner517c2222013-04-24 18:58:55 +1000845 error = xfs_attr3_leaf_lookup_int(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 if (error != EEXIST) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000847 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000848 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 }
Dave Chinner517c2222013-04-24 18:58:55 +1000850 error = xfs_attr3_leaf_getvalue(bp, args);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000851 xfs_trans_brelse(args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
853 error = xfs_attr_rmtval_get(args);
854 }
Dave Chinner517c2222013-04-24 18:58:55 +1000855 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856}
857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858/*========================================================================
859 * External routines when attribute list size > XFS_LBSIZE(mp).
860 *========================================================================*/
861
862/*
863 * Add a name to a Btree-format attribute list.
864 *
865 * This will involve walking down the Btree, and may involve splitting
866 * leaf nodes and even splitting intermediate nodes up to and including
867 * the root node (a special case of an intermediate node).
868 *
869 * "Remote" attribute values confuse the issue and atomic rename operations
870 * add a whole extra layer of confusion on top of that.
871 */
872STATIC int
873xfs_attr_node_addname(xfs_da_args_t *args)
874{
875 xfs_da_state_t *state;
876 xfs_da_state_blk_t *blk;
877 xfs_inode_t *dp;
878 xfs_mount_t *mp;
879 int committed, retval, error;
880
Dave Chinner5a5881c2012-03-22 05:15:13 +0000881 trace_xfs_attr_node_addname(args);
882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 /*
884 * Fill in bucket of arguments/results/context to carry around.
885 */
886 dp = args->dp;
887 mp = dp->i_mount;
888restart:
889 state = xfs_da_state_alloc();
890 state->args = args;
891 state->mp = mp;
892 state->blocksize = state->mp->m_sb.sb_blocksize;
893 state->node_ents = state->mp->m_attr_node_ents;
894
895 /*
896 * Search to see if name already exists, and get back a pointer
897 * to where it should go.
898 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000899 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 if (error)
901 goto out;
902 blk = &state->path.blk[ state->path.active-1 ];
903 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
904 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
905 goto out;
906 } else if (retval == EEXIST) {
907 if (args->flags & ATTR_CREATE)
908 goto out;
Dave Chinner5a5881c2012-03-22 05:15:13 +0000909
910 trace_xfs_attr_node_replace(args);
911
Barry Naujok6a178102008-05-21 16:42:05 +1000912 args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 args->blkno2 = args->blkno; /* set 2nd entry info*/
914 args->index2 = args->index;
915 args->rmtblkno2 = args->rmtblkno;
916 args->rmtblkcnt2 = args->rmtblkcnt;
917 args->rmtblkno = 0;
918 args->rmtblkcnt = 0;
919 }
920
Dave Chinner517c2222013-04-24 18:58:55 +1000921 retval = xfs_attr3_leaf_add(blk->bp, state->args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 if (retval == ENOSPC) {
923 if (state->path.active == 1) {
924 /*
925 * Its really a single leaf node, but it had
926 * out-of-line values so it looked like it *might*
927 * have been a b-tree.
928 */
929 xfs_da_state_free(state);
Eric Sandeen6dd93e92013-07-31 20:18:54 -0500930 state = NULL;
Eric Sandeen9d87c312009-01-14 23:22:07 -0600931 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000932 error = xfs_attr3_leaf_to_node(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 if (!error) {
934 error = xfs_bmap_finish(&args->trans,
935 args->flist,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 &committed);
937 }
938 if (error) {
939 ASSERT(committed);
940 args->trans = NULL;
941 xfs_bmap_cancel(args->flist);
942 goto out;
943 }
944
945 /*
946 * bmap_finish() may have committed the last trans
947 * and started a new one. We need the inode to be
948 * in all transactions.
949 */
Christoph Hellwig898621d2010-06-24 11:36:58 +1000950 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +0000951 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
953 /*
954 * Commit the node conversion and start the next
955 * trans in the chain.
956 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000957 error = xfs_trans_roll(&args->trans, dp);
958 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 goto out;
960
961 goto restart;
962 }
963
964 /*
965 * Split as many Btree elements as required.
966 * This code tracks the new and old attr's location
967 * in the index/blkno/rmtblkno/rmtblkcnt fields and
968 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
969 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600970 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000971 error = xfs_da3_split(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 if (!error) {
973 error = xfs_bmap_finish(&args->trans, args->flist,
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100974 &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 }
976 if (error) {
977 ASSERT(committed);
978 args->trans = NULL;
979 xfs_bmap_cancel(args->flist);
980 goto out;
981 }
982
983 /*
984 * bmap_finish() may have committed the last trans and started
985 * a new one. We need the inode to be in all transactions.
986 */
Christoph Hellwig898621d2010-06-24 11:36:58 +1000987 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +0000988 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 } else {
990 /*
991 * Addition succeeded, update Btree hashvals.
992 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000993 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 }
995
996 /*
997 * Kill the state structure, we're done with it and need to
998 * allow the buffers to come back later.
999 */
1000 xfs_da_state_free(state);
1001 state = NULL;
1002
1003 /*
1004 * Commit the leaf addition or btree split and start the next
1005 * trans in the chain.
1006 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10001007 error = xfs_trans_roll(&args->trans, dp);
1008 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 goto out;
1010
1011 /*
1012 * If there was an out-of-line value, allocate the blocks we
1013 * identified for its storage and copy the value. This is done
1014 * after we create the attribute so that we don't overflow the
1015 * maximum size of a transaction and/or hit a deadlock.
1016 */
1017 if (args->rmtblkno > 0) {
1018 error = xfs_attr_rmtval_set(args);
1019 if (error)
1020 return(error);
1021 }
1022
1023 /*
1024 * If this is an atomic rename operation, we must "flip" the
1025 * incomplete flags on the "new" and "old" attribute/value pairs
1026 * so that one disappears and one appears atomically. Then we
1027 * must remove the "old" attribute/value pair.
1028 */
Barry Naujok6a178102008-05-21 16:42:05 +10001029 if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 /*
1031 * In a separate transaction, set the incomplete flag on the
1032 * "old" attr and clear the incomplete flag on the "new" attr.
1033 */
Dave Chinner517c2222013-04-24 18:58:55 +10001034 error = xfs_attr3_leaf_flipflags(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 if (error)
1036 goto out;
1037
1038 /*
1039 * Dismantle the "old" attribute/value pair by removing
1040 * a "remote" value (if it exists).
1041 */
1042 args->index = args->index2;
1043 args->blkno = args->blkno2;
1044 args->rmtblkno = args->rmtblkno2;
1045 args->rmtblkcnt = args->rmtblkcnt2;
1046 if (args->rmtblkno) {
1047 error = xfs_attr_rmtval_remove(args);
1048 if (error)
1049 return(error);
1050 }
1051
1052 /*
1053 * Re-find the "old" attribute entry after any split ops.
1054 * The INCOMPLETE flag means that we will find the "old"
1055 * attr, not the "new" one.
1056 */
1057 args->flags |= XFS_ATTR_INCOMPLETE;
1058 state = xfs_da_state_alloc();
1059 state->args = args;
1060 state->mp = mp;
1061 state->blocksize = state->mp->m_sb.sb_blocksize;
1062 state->node_ents = state->mp->m_attr_node_ents;
1063 state->inleaf = 0;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001064 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 if (error)
1066 goto out;
1067
1068 /*
1069 * Remove the name and update the hashvals in the tree.
1070 */
1071 blk = &state->path.blk[ state->path.active-1 ];
1072 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner517c2222013-04-24 18:58:55 +10001073 error = xfs_attr3_leaf_remove(blk->bp, args);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001074 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
1076 /*
1077 * Check to see if the tree needs to be collapsed.
1078 */
1079 if (retval && (state->path.active > 1)) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001080 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001081 error = xfs_da3_join(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 if (!error) {
1083 error = xfs_bmap_finish(&args->trans,
1084 args->flist,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 &committed);
1086 }
1087 if (error) {
1088 ASSERT(committed);
1089 args->trans = NULL;
1090 xfs_bmap_cancel(args->flist);
1091 goto out;
1092 }
1093
1094 /*
1095 * bmap_finish() may have committed the last trans
1096 * and started a new one. We need the inode to be
1097 * in all transactions.
1098 */
Christoph Hellwig898621d2010-06-24 11:36:58 +10001099 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +00001100 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 }
1102
1103 /*
1104 * Commit and start the next trans in the chain.
1105 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10001106 error = xfs_trans_roll(&args->trans, dp);
1107 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 goto out;
1109
1110 } else if (args->rmtblkno > 0) {
1111 /*
1112 * Added a "remote" value, just clear the incomplete flag.
1113 */
Dave Chinner517c2222013-04-24 18:58:55 +10001114 error = xfs_attr3_leaf_clearflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 if (error)
1116 goto out;
1117 }
1118 retval = error = 0;
1119
1120out:
1121 if (state)
1122 xfs_da_state_free(state);
1123 if (error)
1124 return(error);
1125 return(retval);
1126}
1127
1128/*
1129 * Remove a name from a B-tree attribute list.
1130 *
1131 * This will involve walking down the Btree, and may involve joining
1132 * leaf nodes and even joining intermediate nodes up to and including
1133 * the root node (a special case of an intermediate node).
1134 */
1135STATIC int
1136xfs_attr_node_removename(xfs_da_args_t *args)
1137{
1138 xfs_da_state_t *state;
1139 xfs_da_state_blk_t *blk;
1140 xfs_inode_t *dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001141 struct xfs_buf *bp;
Nathan Scottd8cc8902005-11-02 10:34:53 +11001142 int retval, error, committed, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Dave Chinner5a5881c2012-03-22 05:15:13 +00001144 trace_xfs_attr_node_removename(args);
1145
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 /*
1147 * Tie a string around our finger to remind us where we are.
1148 */
1149 dp = args->dp;
1150 state = xfs_da_state_alloc();
1151 state->args = args;
1152 state->mp = dp->i_mount;
1153 state->blocksize = state->mp->m_sb.sb_blocksize;
1154 state->node_ents = state->mp->m_attr_node_ents;
1155
1156 /*
1157 * Search to see if name exists, and get back a pointer to it.
1158 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001159 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 if (error || (retval != EEXIST)) {
1161 if (error == 0)
1162 error = retval;
1163 goto out;
1164 }
1165
1166 /*
1167 * If there is an out-of-line value, de-allocate the blocks.
1168 * This is done before we remove the attribute so that we don't
1169 * overflow the maximum size of a transaction and/or hit a deadlock.
1170 */
1171 blk = &state->path.blk[ state->path.active-1 ];
1172 ASSERT(blk->bp != NULL);
1173 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1174 if (args->rmtblkno > 0) {
1175 /*
1176 * Fill in disk block numbers in the state structure
1177 * so that we can get the buffers back after we commit
1178 * several transactions in the following calls.
1179 */
1180 error = xfs_attr_fillstate(state);
1181 if (error)
1182 goto out;
1183
1184 /*
1185 * Mark the attribute as INCOMPLETE, then bunmapi() the
1186 * remote value.
1187 */
Dave Chinner517c2222013-04-24 18:58:55 +10001188 error = xfs_attr3_leaf_setflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 if (error)
1190 goto out;
1191 error = xfs_attr_rmtval_remove(args);
1192 if (error)
1193 goto out;
1194
1195 /*
1196 * Refill the state structure with buffers, the prior calls
1197 * released our buffers.
1198 */
1199 error = xfs_attr_refillstate(state);
1200 if (error)
1201 goto out;
1202 }
1203
1204 /*
1205 * Remove the name and update the hashvals in the tree.
1206 */
1207 blk = &state->path.blk[ state->path.active-1 ];
1208 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner517c2222013-04-24 18:58:55 +10001209 retval = xfs_attr3_leaf_remove(blk->bp, args);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001210 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
1212 /*
1213 * Check to see if the tree needs to be collapsed.
1214 */
1215 if (retval && (state->path.active > 1)) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001216 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001217 error = xfs_da3_join(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 if (!error) {
1219 error = xfs_bmap_finish(&args->trans, args->flist,
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001220 &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 }
1222 if (error) {
1223 ASSERT(committed);
1224 args->trans = NULL;
1225 xfs_bmap_cancel(args->flist);
1226 goto out;
1227 }
1228
1229 /*
1230 * bmap_finish() may have committed the last trans and started
1231 * a new one. We need the inode to be in all transactions.
1232 */
Christoph Hellwig898621d2010-06-24 11:36:58 +10001233 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +00001234 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
1236 /*
1237 * Commit the Btree join operation and start a new trans.
1238 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10001239 error = xfs_trans_roll(&args->trans, dp);
1240 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 goto out;
1242 }
1243
1244 /*
1245 * If the result is small enough, push it all into the inode.
1246 */
1247 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
1248 /*
1249 * Have to get rid of the copy of this dabuf in the state.
1250 */
1251 ASSERT(state->path.active == 1);
1252 ASSERT(state->path.blk[0].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 state->path.blk[0].bp = NULL;
1254
Dave Chinner517c2222013-04-24 18:58:55 +10001255 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 if (error)
1257 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Nathan Scottd8cc8902005-11-02 10:34:53 +11001259 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001260 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +10001261 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 /* bp is gone due to xfs_da_shrink_inode */
1263 if (!error) {
1264 error = xfs_bmap_finish(&args->trans,
1265 args->flist,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 &committed);
1267 }
1268 if (error) {
1269 ASSERT(committed);
1270 args->trans = NULL;
1271 xfs_bmap_cancel(args->flist);
1272 goto out;
1273 }
1274
1275 /*
1276 * bmap_finish() may have committed the last trans
1277 * and started a new one. We need the inode to be
1278 * in all transactions.
1279 */
Christoph Hellwig898621d2010-06-24 11:36:58 +10001280 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +00001281 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 } else
Dave Chinner1d9025e2012-06-22 18:50:14 +10001283 xfs_trans_brelse(args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 }
1285 error = 0;
1286
1287out:
1288 xfs_da_state_free(state);
1289 return(error);
1290}
1291
1292/*
1293 * Fill in the disk block numbers in the state structure for the buffers
1294 * that are attached to the state structure.
1295 * This is done so that we can quickly reattach ourselves to those buffers
Nathan Scottc41564b2006-03-29 08:55:14 +10001296 * after some set of transaction commits have released these buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 */
1298STATIC int
1299xfs_attr_fillstate(xfs_da_state_t *state)
1300{
1301 xfs_da_state_path_t *path;
1302 xfs_da_state_blk_t *blk;
1303 int level;
1304
Dave Chinneree732592012-11-12 22:53:53 +11001305 trace_xfs_attr_fillstate(state->args);
1306
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 /*
1308 * Roll down the "path" in the state structure, storing the on-disk
1309 * block number for those buffers in the "path".
1310 */
1311 path = &state->path;
1312 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1313 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1314 if (blk->bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001315 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 blk->bp = NULL;
1317 } else {
1318 blk->disk_blkno = 0;
1319 }
1320 }
1321
1322 /*
1323 * Roll down the "altpath" in the state structure, storing the on-disk
1324 * block number for those buffers in the "altpath".
1325 */
1326 path = &state->altpath;
1327 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1328 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1329 if (blk->bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001330 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 blk->bp = NULL;
1332 } else {
1333 blk->disk_blkno = 0;
1334 }
1335 }
1336
1337 return(0);
1338}
1339
1340/*
1341 * Reattach the buffers to the state structure based on the disk block
1342 * numbers stored in the state structure.
Nathan Scottc41564b2006-03-29 08:55:14 +10001343 * This is done after some set of transaction commits have released those
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 * buffers from our grip.
1345 */
1346STATIC int
1347xfs_attr_refillstate(xfs_da_state_t *state)
1348{
1349 xfs_da_state_path_t *path;
1350 xfs_da_state_blk_t *blk;
1351 int level, error;
1352
Dave Chinneree732592012-11-12 22:53:53 +11001353 trace_xfs_attr_refillstate(state->args);
1354
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 /*
1356 * Roll down the "path" in the state structure, storing the on-disk
1357 * block number for those buffers in the "path".
1358 */
1359 path = &state->path;
1360 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1361 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1362 if (blk->disk_blkno) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001363 error = xfs_da3_node_read(state->args->trans,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 state->args->dp,
1365 blk->blkno, blk->disk_blkno,
Dave Chinnerd9392a42012-11-12 22:54:17 +11001366 &blk->bp, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 if (error)
1368 return(error);
1369 } else {
1370 blk->bp = NULL;
1371 }
1372 }
1373
1374 /*
1375 * Roll down the "altpath" in the state structure, storing the on-disk
1376 * block number for those buffers in the "altpath".
1377 */
1378 path = &state->altpath;
1379 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1380 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1381 if (blk->disk_blkno) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001382 error = xfs_da3_node_read(state->args->trans,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 state->args->dp,
1384 blk->blkno, blk->disk_blkno,
Dave Chinnerd9392a42012-11-12 22:54:17 +11001385 &blk->bp, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 if (error)
1387 return(error);
1388 } else {
1389 blk->bp = NULL;
1390 }
1391 }
1392
1393 return(0);
1394}
1395
1396/*
1397 * Look up a filename in a node attribute list.
1398 *
1399 * This routine gets called for any attribute fork that has more than one
1400 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1401 * "remote" values taking up more blocks.
1402 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001403STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404xfs_attr_node_get(xfs_da_args_t *args)
1405{
1406 xfs_da_state_t *state;
1407 xfs_da_state_blk_t *blk;
1408 int error, retval;
1409 int i;
1410
Dave Chinneree732592012-11-12 22:53:53 +11001411 trace_xfs_attr_node_get(args);
1412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 state = xfs_da_state_alloc();
1414 state->args = args;
1415 state->mp = args->dp->i_mount;
1416 state->blocksize = state->mp->m_sb.sb_blocksize;
1417 state->node_ents = state->mp->m_attr_node_ents;
1418
1419 /*
1420 * Search to see if name exists, and get back a pointer to it.
1421 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001422 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 if (error) {
1424 retval = error;
1425 } else if (retval == EEXIST) {
1426 blk = &state->path.blk[ state->path.active-1 ];
1427 ASSERT(blk->bp != NULL);
1428 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1429
1430 /*
1431 * Get the value, local or "remote"
1432 */
Dave Chinner517c2222013-04-24 18:58:55 +10001433 retval = xfs_attr3_leaf_getvalue(blk->bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 if (!retval && (args->rmtblkno > 0)
1435 && !(args->flags & ATTR_KERNOVAL)) {
1436 retval = xfs_attr_rmtval_get(args);
1437 }
1438 }
1439
1440 /*
1441 * If not in a transaction, we have to release all the buffers.
1442 */
1443 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001444 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 state->path.blk[i].bp = NULL;
1446 }
1447
1448 xfs_da_state_free(state);
1449 return(retval);
1450}