blob: a96e27bfc58b89fb6bf88364c159b0a2eb4cb8d1 [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
80xfs_attr_name_to_xname(
81 struct xfs_name *xname,
Dave Chinnera9273ca2010-01-20 10:47:48 +110082 const unsigned char *aname)
Barry Naujoke8b0eba2008-04-22 17:34:31 +100083{
84 if (!aname)
85 return EINVAL;
86 xname->name = aname;
Dave Chinnera9273ca2010-01-20 10:47:48 +110087 xname->len = strlen((char *)aname);
Barry Naujoke8b0eba2008-04-22 17:34:31 +100088 if (xname->len >= MAXNAMELEN)
89 return EFAULT; /* match IRIX behaviour */
90
91 return 0;
92}
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Dave Chinnerabec5f22013-08-12 20:49:38 +100094int
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +100095xfs_inode_hasattr(
96 struct xfs_inode *ip)
97{
98 if (!XFS_IFORK_Q(ip) ||
99 (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
100 ip->i_d.di_anextents == 0))
101 return 0;
102 return 1;
103}
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105/*========================================================================
106 * Overall external interface routines.
107 *========================================================================*/
108
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000109int
110xfs_attr_get(
Christoph Hellwige82fa0c2009-11-14 16:17:20 +0000111 struct xfs_inode *ip,
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000112 const unsigned char *name,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100113 unsigned char *value,
Christoph Hellwige82fa0c2009-11-14 16:17:20 +0000114 int *valuelenp,
115 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000117 struct xfs_da_args args;
118 struct xfs_name xname;
119 uint lock_mode;
120 int error;
121
122 XFS_STATS_INC(xs_attr_get);
123
124 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
125 return EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +1000127 if (!xfs_inode_hasattr(ip))
128 return ENOATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000130 error = xfs_attr_name_to_xname(&xname, name);
131 if (error)
132 return error;
133
134 memset(&args, 0, sizeof(args));
135 args.name = xname.name;
136 args.namelen = xname.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 args.value = value;
138 args.valuelen = *valuelenp;
139 args.flags = flags;
140 args.hashval = xfs_da_hashname(args.name, args.namelen);
141 args.dp = ip;
142 args.whichfork = XFS_ATTR_FORK;
143
Christoph Hellwig683cb942013-12-06 12:30:15 -0800144 lock_mode = xfs_ilock_attr_map_shared(ip);
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000145 if (!xfs_inode_hasattr(ip))
146 error = ENOATTR;
147 else if (ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL)
148 error = xfs_attr_shortform_getvalue(&args);
149 else if (xfs_bmap_one_block(ip, XFS_ATTR_FORK))
150 error = xfs_attr_leaf_get(&args);
151 else
152 error = xfs_attr_node_get(&args);
Christoph Hellwig683cb942013-12-06 12:30:15 -0800153 xfs_iunlock(ip, lock_mode);
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000154
155 *valuelenp = args.valuelen;
156 return error == EEXIST ? 0 : error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
158
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000159/*
160 * Calculate how many blocks we need for the new attribute,
161 */
Eric Sandeen5d77c0d2009-11-19 15:52:00 +0000162STATIC int
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000163xfs_attr_calc_size(
164 struct xfs_inode *ip,
165 int namelen,
166 int valuelen,
167 int *local)
168{
169 struct xfs_mount *mp = ip->i_mount;
170 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 */
177 size = xfs_attr_leaf_newentsize(namelen, valuelen,
178 mp->m_sb.sb_blocksize, local);
179
180 nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
181 if (*local) {
182 if (size > (mp->m_sb.sb_blocksize >> 1)) {
183 /* Double split possible */
184 nblks *= 2;
185 }
186 } else {
187 /*
188 * Out of line attribute, cannot double split, but
189 * make room for the attribute value itself.
190 */
191 uint dblocks = XFS_B_TO_FSB(mp, valuelen);
192 nblks += dblocks;
193 nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
194 }
195
196 return nblks;
197}
198
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000199int
200xfs_attr_set(
201 struct xfs_inode *dp,
202 const unsigned char *name,
203 unsigned char *value,
204 int valuelen,
205 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
Jie Liu3d3c8b52013-08-12 20:49:59 +1000207 struct xfs_mount *mp = dp->i_mount;
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000208 struct xfs_da_args args;
209 struct xfs_bmap_free flist;
Jie Liu3d3c8b52013-08-12 20:49:59 +1000210 struct xfs_trans_res tres;
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000211 struct xfs_name xname;
212 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
221 error = xfs_attr_name_to_xname(&xname, name);
222 if (error)
223 return error;
224
Christoph Hellwig7d095252009-06-08 15:33:32 +0200225 error = xfs_qm_dqattach(dp, 0);
226 if (error)
227 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 /*
230 * If the inode doesn't have an attribute fork, add one.
231 * (inode must not be locked when we call this routine)
232 */
233 if (XFS_IFORK_Q(dp) == 0) {
Barry Naujoke5889e92007-02-10 18:35:58 +1100234 int sf_size = sizeof(xfs_attr_sf_hdr_t) +
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000235 XFS_ATTR_SF_ENTSIZE_BYNAME(xname.len, valuelen);
Barry Naujoke5889e92007-02-10 18:35:58 +1100236
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000237 error = xfs_bmap_add_attrfork(dp, sf_size, rsvd);
238 if (error)
239 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 }
241
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000242 memset(&args, 0, sizeof(args));
243 args.name = xname.name;
244 args.namelen = xname.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 args.value = value;
246 args.valuelen = valuelen;
247 args.flags = flags;
248 args.hashval = xfs_da_hashname(args.name, args.namelen);
249 args.dp = dp;
250 args.firstblock = &firstblock;
251 args.flist = &flist;
252 args.whichfork = XFS_ATTR_FORK;
Barry Naujok6a178102008-05-21 16:42:05 +1000253 args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 /* Size is now blocks for attribute data */
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000256 args.total = xfs_attr_calc_size(dp, xname.len, valuelen, &local);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 /*
259 * Start our first transaction of the day.
260 *
261 * All future transactions during this code must be "chained" off
262 * this one via the trans_dup() call. All transactions will contain
263 * the inode, and the inode will always be marked with trans_ihold().
264 * Since the inode will be locked in all transactions, we must log
265 * the inode in every transaction to let it float upward through
266 * the log.
267 */
268 args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_SET);
269
270 /*
271 * Root fork attributes can use reserved data blocks for this
272 * operation if necessary
273 */
274
275 if (rsvd)
276 args.trans->t_flags |= XFS_TRANS_RESERVE;
277
Jie Liu3d3c8b52013-08-12 20:49:59 +1000278 tres.tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
279 M_RES(mp)->tr_attrsetrt.tr_logres * args.total;
280 tres.tr_logcount = XFS_ATTRSET_LOG_COUNT;
281 tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
282 error = xfs_trans_reserve(args.trans, &tres, args.total, 0);
Jeff Liua21cd502013-01-28 21:27:53 +0800283 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 xfs_trans_cancel(args.trans, 0);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000285 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
287 xfs_ilock(dp, XFS_ILOCK_EXCL);
288
Christoph Hellwig7d095252009-06-08 15:33:32 +0200289 error = xfs_trans_reserve_quota_nblks(args.trans, dp, args.total, 0,
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000290 rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
291 XFS_QMOPT_RES_REGBLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 if (error) {
293 xfs_iunlock(dp, XFS_ILOCK_EXCL);
294 xfs_trans_cancel(args.trans, XFS_TRANS_RELEASE_LOG_RES);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000295 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 }
297
Christoph Hellwigddc34152011-09-19 15:00:54 +0000298 xfs_trans_ijoin(args.trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000301 * If the attribute list is non-existent or a shortform list,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 * upgrade it to a single-leaf-block attribute list.
303 */
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000304 if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL ||
305 (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
306 dp->i_d.di_anextents == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 /*
309 * Build initial attribute list (if required).
310 */
311 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
Nathan Scottd8cc8902005-11-02 10:34:53 +1100312 xfs_attr_shortform_create(&args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 /*
315 * Try to add the attr to the attribute list in
316 * the inode.
317 */
318 error = xfs_attr_shortform_addname(&args);
319 if (error != ENOSPC) {
320 /*
321 * Commit the shortform mods, and we're done.
322 * NOTE: this is also the error path (EEXIST, etc).
323 */
324 ASSERT(args.trans != NULL);
325
326 /*
327 * If this is a synchronous mount, make sure that
328 * the transaction goes to disk before returning
329 * to the user.
330 */
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000331 if (mp->m_flags & XFS_MOUNT_WSYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 xfs_trans_set_sync(args.trans);
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000333
334 if (!error && (flags & ATTR_KERNOTIME) == 0) {
335 xfs_trans_ichgtime(args.trans, dp,
336 XFS_ICHGTIME_CHG);
337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 err2 = xfs_trans_commit(args.trans,
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000339 XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 xfs_iunlock(dp, XFS_ILOCK_EXCL);
341
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000342 return error ? error : err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
344
345 /*
346 * It won't fit in the shortform, transform to a leaf block.
347 * GROT: another possible req'mt for a double-split btree op.
348 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600349 xfs_bmap_init(args.flist, args.firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 error = xfs_attr_shortform_to_leaf(&args);
351 if (!error) {
352 error = xfs_bmap_finish(&args.trans, args.flist,
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100353 &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 }
355 if (error) {
356 ASSERT(committed);
357 args.trans = NULL;
358 xfs_bmap_cancel(&flist);
359 goto out;
360 }
361
362 /*
363 * bmap_finish() may have committed the last trans and started
364 * a new one. We need the inode to be in all transactions.
365 */
Christoph Hellwig898621d2010-06-24 11:36:58 +1000366 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +0000367 xfs_trans_ijoin(args.trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 /*
370 * Commit the leaf transformation. We'll need another (linked)
371 * transaction to add the new attribute to the leaf.
372 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000373
374 error = xfs_trans_roll(&args.trans, dp);
375 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 goto out;
377
378 }
379
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000380 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 error = xfs_attr_leaf_addname(&args);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000382 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 error = xfs_attr_node_addname(&args);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000384 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 /*
388 * If this is a synchronous mount, make sure that the
389 * transaction goes to disk before returning to the user.
390 */
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000391 if (mp->m_flags & XFS_MOUNT_WSYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 xfs_trans_set_sync(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000394 if ((flags & ATTR_KERNOTIME) == 0)
395 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 /*
398 * Commit the last in the sequence of transactions.
399 */
400 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000401 error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 xfs_iunlock(dp, XFS_ILOCK_EXCL);
403
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000404 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406out:
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000407 if (args.trans) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 xfs_trans_cancel(args.trans,
409 XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000412 return error;
Nathan Scottaa82daa2005-11-02 10:33:33 +1100413}
414
415/*
416 * Generic handler routine to remove a name from an attribute list.
417 * Transitions attribute list from Btree to shortform as necessary.
418 */
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000419int
420xfs_attr_remove(
421 struct xfs_inode *dp,
422 const unsigned char *name,
423 int flags)
Nathan Scottaa82daa2005-11-02 10:33:33 +1100424{
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000425 struct xfs_mount *mp = dp->i_mount;
426 struct xfs_da_args args;
427 struct xfs_bmap_free flist;
428 struct xfs_name xname;
429 xfs_fsblock_t firstblock;
430 int error;
Nathan Scottaa82daa2005-11-02 10:33:33 +1100431
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000432 XFS_STATS_INC(xs_attr_remove);
433
434 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
435 return EIO;
436
437 if (!xfs_inode_hasattr(dp))
438 return ENOATTR;
439
440 error = xfs_attr_name_to_xname(&xname, name);
441 if (error)
442 return error;
443
444 memset(&args, 0, sizeof(args));
445 args.name = xname.name;
446 args.namelen = xname.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 args.flags = flags;
448 args.hashval = xfs_da_hashname(args.name, args.namelen);
449 args.dp = dp;
450 args.firstblock = &firstblock;
451 args.flist = &flist;
452 args.total = 0;
453 args.whichfork = XFS_ATTR_FORK;
454
455 /*
Dave Chinner4a338212011-06-23 01:35:01 +0000456 * we have no control over the attribute names that userspace passes us
457 * to remove, so we have to allow the name lookup prior to attribute
458 * removal to fail.
459 */
460 args.op_flags = XFS_DA_OP_OKNOENT;
461
Christoph Hellwig7d095252009-06-08 15:33:32 +0200462 error = xfs_qm_dqattach(dp, 0);
463 if (error)
464 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 /*
467 * Start our first transaction of the day.
468 *
469 * All future transactions during this code must be "chained" off
470 * this one via the trans_dup() call. All transactions will contain
471 * the inode, and the inode will always be marked with trans_ihold().
472 * Since the inode will be locked in all transactions, we must log
473 * the inode in every transaction to let it float upward through
474 * the log.
475 */
476 args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_RM);
477
478 /*
479 * Root fork attributes can use reserved data blocks for this
480 * operation if necessary
481 */
482
483 if (flags & ATTR_ROOT)
484 args.trans->t_flags |= XFS_TRANS_RESERVE;
485
Jie Liu3d3c8b52013-08-12 20:49:59 +1000486 error = xfs_trans_reserve(args.trans, &M_RES(mp)->tr_attrrm,
487 XFS_ATTRRM_SPACE_RES(mp), 0);
488 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 xfs_trans_cancel(args.trans, 0);
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000490 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 }
492
493 xfs_ilock(dp, XFS_ILOCK_EXCL);
494 /*
495 * No need to make quota reservations here. We expect to release some
496 * blocks not allocate in the common case.
497 */
Christoph Hellwigddc34152011-09-19 15:00:54 +0000498 xfs_trans_ijoin(args.trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +1000500 if (!xfs_inode_hasattr(dp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 error = XFS_ERROR(ENOATTR);
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000502 } else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
504 error = xfs_attr_shortform_remove(&args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
506 error = xfs_attr_leaf_removename(&args);
507 } else {
508 error = xfs_attr_node_removename(&args);
509 }
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000510
511 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 /*
515 * If this is a synchronous mount, make sure that the
516 * transaction goes to disk before returning to the user.
517 */
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000518 if (mp->m_flags & XFS_MOUNT_WSYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 xfs_trans_set_sync(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000521 if ((flags & ATTR_KERNOTIME) == 0)
522 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 /*
525 * Commit the last in the sequence of transactions.
526 */
527 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000528 error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 xfs_iunlock(dp, XFS_ILOCK_EXCL);
530
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000531 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533out:
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000534 if (args.trans) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 xfs_trans_cancel(args.trans,
536 XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
Nathan Scottaa82daa2005-11-02 10:33:33 +1100537 }
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000538 xfs_iunlock(dp, XFS_ILOCK_EXCL);
539 return error;
Nathan Scottaa82daa2005-11-02 10:33:33 +1100540}
541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542/*========================================================================
543 * External routines when attribute list is inside the inode
544 *========================================================================*/
545
546/*
547 * Add a name to the shortform attribute list structure
548 * This is the external routine.
549 */
550STATIC int
551xfs_attr_shortform_addname(xfs_da_args_t *args)
552{
Nathan Scottd8cc8902005-11-02 10:34:53 +1100553 int newsize, forkoff, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Dave Chinner5a5881c2012-03-22 05:15:13 +0000555 trace_xfs_attr_sf_addname(args);
556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 retval = xfs_attr_shortform_lookup(args);
558 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
559 return(retval);
560 } else if (retval == EEXIST) {
561 if (args->flags & ATTR_CREATE)
562 return(retval);
563 retval = xfs_attr_shortform_remove(args);
564 ASSERT(retval == 0);
565 }
566
Nathan Scottd8cc8902005-11-02 10:34:53 +1100567 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
568 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
569 return(XFS_ERROR(ENOSPC));
570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
572 newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100573
574 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
575 if (!forkoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 return(XFS_ERROR(ENOSPC));
Nathan Scottd8cc8902005-11-02 10:34:53 +1100577
578 xfs_attr_shortform_add(args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 return(0);
580}
581
582
583/*========================================================================
584 * External routines when attribute list is one block
585 *========================================================================*/
586
587/*
588 * Add a name to the leaf attribute list structure
589 *
590 * This leaf block cannot have a "remote" value, we only call this routine
591 * if bmap_one_block() says there is only one block (ie: no remote blks).
592 */
David Chinnera8272ce2007-11-23 16:28:09 +1100593STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594xfs_attr_leaf_addname(xfs_da_args_t *args)
595{
596 xfs_inode_t *dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000597 struct xfs_buf *bp;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100598 int retval, error, committed, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Dave Chinner5a5881c2012-03-22 05:15:13 +0000600 trace_xfs_attr_leaf_addname(args);
601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 /*
603 * Read the (only) block in the attribute list in.
604 */
605 dp = args->dp;
606 args->blkno = 0;
Dave Chinner517c2222013-04-24 18:58:55 +1000607 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100609 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 /*
612 * Look up the given attribute in the leaf block. Figure out if
613 * the given flags produce an error or call for an atomic rename.
614 */
Dave Chinner517c2222013-04-24 18:58:55 +1000615 retval = xfs_attr3_leaf_lookup_int(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000617 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000618 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 } else if (retval == EEXIST) {
620 if (args->flags & ATTR_CREATE) { /* pure create op */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000621 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000622 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 }
Dave Chinner5a5881c2012-03-22 05:15:13 +0000624
625 trace_xfs_attr_leaf_replace(args);
626
Barry Naujok6a178102008-05-21 16:42:05 +1000627 args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 args->blkno2 = args->blkno; /* set 2nd entry info*/
629 args->index2 = args->index;
630 args->rmtblkno2 = args->rmtblkno;
631 args->rmtblkcnt2 = args->rmtblkcnt;
632 }
633
634 /*
635 * Add the attribute to the leaf block, transitioning to a Btree
636 * if required.
637 */
Dave Chinner517c2222013-04-24 18:58:55 +1000638 retval = xfs_attr3_leaf_add(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 if (retval == ENOSPC) {
640 /*
641 * Promote the attribute list to the Btree format, then
642 * Commit that transaction so that the node_addname() call
643 * can manage its own transactions.
644 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600645 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000646 error = xfs_attr3_leaf_to_node(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 if (!error) {
648 error = xfs_bmap_finish(&args->trans, args->flist,
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100649 &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 }
651 if (error) {
652 ASSERT(committed);
653 args->trans = NULL;
654 xfs_bmap_cancel(args->flist);
655 return(error);
656 }
657
658 /*
659 * bmap_finish() may have committed the last trans and started
660 * a new one. We need the inode to be in all transactions.
661 */
Christoph Hellwig898621d2010-06-24 11:36:58 +1000662 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +0000663 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 /*
666 * Commit the current trans (including the inode) and start
667 * a new one.
668 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000669 error = xfs_trans_roll(&args->trans, dp);
670 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 return (error);
672
673 /*
674 * Fob the whole rest of the problem off on the Btree code.
675 */
676 error = xfs_attr_node_addname(args);
677 return(error);
678 }
679
680 /*
681 * Commit the transaction that added the attr name so that
682 * later routines can manage their own transactions.
683 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000684 error = xfs_trans_roll(&args->trans, dp);
685 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 return (error);
687
688 /*
689 * If there was an out-of-line value, allocate the blocks we
690 * identified for its storage and copy the value. This is done
691 * after we create the attribute so that we don't overflow the
692 * maximum size of a transaction and/or hit a deadlock.
693 */
694 if (args->rmtblkno > 0) {
695 error = xfs_attr_rmtval_set(args);
696 if (error)
697 return(error);
698 }
699
700 /*
701 * If this is an atomic rename operation, we must "flip" the
702 * incomplete flags on the "new" and "old" attribute/value pairs
703 * so that one disappears and one appears atomically. Then we
704 * must remove the "old" attribute/value pair.
705 */
Barry Naujok6a178102008-05-21 16:42:05 +1000706 if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 /*
708 * In a separate transaction, set the incomplete flag on the
709 * "old" attr and clear the incomplete flag on the "new" attr.
710 */
Dave Chinner517c2222013-04-24 18:58:55 +1000711 error = xfs_attr3_leaf_flipflags(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 if (error)
713 return(error);
714
715 /*
716 * Dismantle the "old" attribute/value pair by removing
717 * a "remote" value (if it exists).
718 */
719 args->index = args->index2;
720 args->blkno = args->blkno2;
721 args->rmtblkno = args->rmtblkno2;
722 args->rmtblkcnt = args->rmtblkcnt2;
723 if (args->rmtblkno) {
724 error = xfs_attr_rmtval_remove(args);
725 if (error)
726 return(error);
727 }
728
729 /*
730 * Read in the block containing the "old" attr, then
731 * remove the "old" attr from that block (neat, huh!)
732 */
Dave Chinner517c2222013-04-24 18:58:55 +1000733 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
Dave Chinnerad14c332012-11-12 22:54:16 +1100734 -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100736 return error;
737
Dave Chinner517c2222013-04-24 18:58:55 +1000738 xfs_attr3_leaf_remove(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740 /*
741 * If the result is small enough, shrink it all into the inode.
742 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100743 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Eric Sandeen9d87c312009-01-14 23:22:07 -0600744 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000745 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 /* bp is gone due to xfs_da_shrink_inode */
747 if (!error) {
748 error = xfs_bmap_finish(&args->trans,
749 args->flist,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 &committed);
751 }
752 if (error) {
753 ASSERT(committed);
754 args->trans = NULL;
755 xfs_bmap_cancel(args->flist);
756 return(error);
757 }
758
759 /*
760 * bmap_finish() may have committed the last trans
761 * and started a new one. We need the inode to be
762 * in all transactions.
763 */
Christoph Hellwig898621d2010-06-24 11:36:58 +1000764 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +0000765 xfs_trans_ijoin(args->trans, dp, 0);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768 /*
769 * Commit the remove and start the next trans in series.
770 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000771 error = xfs_trans_roll(&args->trans, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 } else if (args->rmtblkno > 0) {
774 /*
775 * Added a "remote" value, just clear the incomplete flag.
776 */
Dave Chinner517c2222013-04-24 18:58:55 +1000777 error = xfs_attr3_leaf_clearflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 }
Dave Chinner517c2222013-04-24 18:58:55 +1000779 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780}
781
782/*
783 * Remove a name from the leaf attribute list structure
784 *
785 * This leaf block cannot have a "remote" value, we only call this routine
786 * if bmap_one_block() says there is only one block (ie: no remote blks).
787 */
788STATIC int
789xfs_attr_leaf_removename(xfs_da_args_t *args)
790{
791 xfs_inode_t *dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000792 struct xfs_buf *bp;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100793 int error, committed, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Dave Chinner5a5881c2012-03-22 05:15:13 +0000795 trace_xfs_attr_leaf_removename(args);
796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 /*
798 * Remove the attribute.
799 */
800 dp = args->dp;
801 args->blkno = 0;
Dave Chinner517c2222013-04-24 18:58:55 +1000802 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Dave Chinnerad14c332012-11-12 22:54:16 +1100803 if (error)
804 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Dave Chinner517c2222013-04-24 18:58:55 +1000806 error = xfs_attr3_leaf_lookup_int(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 if (error == ENOATTR) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000808 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000809 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 }
811
Dave Chinner517c2222013-04-24 18:58:55 +1000812 xfs_attr3_leaf_remove(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814 /*
815 * If the result is small enough, shrink it all into the inode.
816 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100817 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Eric Sandeen9d87c312009-01-14 23:22:07 -0600818 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000819 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 /* bp is gone due to xfs_da_shrink_inode */
821 if (!error) {
822 error = xfs_bmap_finish(&args->trans, args->flist,
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100823 &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 }
825 if (error) {
826 ASSERT(committed);
827 args->trans = NULL;
828 xfs_bmap_cancel(args->flist);
Dave Chinner517c2222013-04-24 18:58:55 +1000829 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 }
831
832 /*
833 * bmap_finish() may have committed the last trans and started
834 * a new one. We need the inode to be in all transactions.
835 */
Christoph Hellwig898621d2010-06-24 11:36:58 +1000836 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +0000837 xfs_trans_ijoin(args->trans, dp, 0);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000838 }
Dave Chinner517c2222013-04-24 18:58:55 +1000839 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840}
841
842/*
843 * Look up a name in a leaf attribute list structure.
844 *
845 * This leaf block cannot have a "remote" value, we only call this routine
846 * if bmap_one_block() says there is only one block (ie: no remote blks).
847 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000848STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849xfs_attr_leaf_get(xfs_da_args_t *args)
850{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000851 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 int error;
853
Dave Chinneree732592012-11-12 22:53:53 +1100854 trace_xfs_attr_leaf_get(args);
855
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 args->blkno = 0;
Dave Chinner517c2222013-04-24 18:58:55 +1000857 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100859 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Dave Chinner517c2222013-04-24 18:58:55 +1000861 error = xfs_attr3_leaf_lookup_int(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 if (error != EEXIST) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000863 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000864 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 }
Dave Chinner517c2222013-04-24 18:58:55 +1000866 error = xfs_attr3_leaf_getvalue(bp, args);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000867 xfs_trans_brelse(args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
869 error = xfs_attr_rmtval_get(args);
870 }
Dave Chinner517c2222013-04-24 18:58:55 +1000871 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872}
873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874/*========================================================================
875 * External routines when attribute list size > XFS_LBSIZE(mp).
876 *========================================================================*/
877
878/*
879 * Add a name to a Btree-format attribute list.
880 *
881 * This will involve walking down the Btree, and may involve splitting
882 * leaf nodes and even splitting intermediate nodes up to and including
883 * the root node (a special case of an intermediate node).
884 *
885 * "Remote" attribute values confuse the issue and atomic rename operations
886 * add a whole extra layer of confusion on top of that.
887 */
888STATIC int
889xfs_attr_node_addname(xfs_da_args_t *args)
890{
891 xfs_da_state_t *state;
892 xfs_da_state_blk_t *blk;
893 xfs_inode_t *dp;
894 xfs_mount_t *mp;
895 int committed, retval, error;
896
Dave Chinner5a5881c2012-03-22 05:15:13 +0000897 trace_xfs_attr_node_addname(args);
898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 /*
900 * Fill in bucket of arguments/results/context to carry around.
901 */
902 dp = args->dp;
903 mp = dp->i_mount;
904restart:
905 state = xfs_da_state_alloc();
906 state->args = args;
907 state->mp = mp;
908 state->blocksize = state->mp->m_sb.sb_blocksize;
909 state->node_ents = state->mp->m_attr_node_ents;
910
911 /*
912 * Search to see if name already exists, and get back a pointer
913 * to where it should go.
914 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000915 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 if (error)
917 goto out;
918 blk = &state->path.blk[ state->path.active-1 ];
919 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
920 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
921 goto out;
922 } else if (retval == EEXIST) {
923 if (args->flags & ATTR_CREATE)
924 goto out;
Dave Chinner5a5881c2012-03-22 05:15:13 +0000925
926 trace_xfs_attr_node_replace(args);
927
Barry Naujok6a178102008-05-21 16:42:05 +1000928 args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 args->blkno2 = args->blkno; /* set 2nd entry info*/
930 args->index2 = args->index;
931 args->rmtblkno2 = args->rmtblkno;
932 args->rmtblkcnt2 = args->rmtblkcnt;
933 args->rmtblkno = 0;
934 args->rmtblkcnt = 0;
935 }
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;
1062 if (args->rmtblkno) {
1063 error = xfs_attr_rmtval_remove(args);
1064 if (error)
1065 return(error);
1066 }
1067
1068 /*
1069 * Re-find the "old" attribute entry after any split ops.
1070 * The INCOMPLETE flag means that we will find the "old"
1071 * attr, not the "new" one.
1072 */
1073 args->flags |= XFS_ATTR_INCOMPLETE;
1074 state = xfs_da_state_alloc();
1075 state->args = args;
1076 state->mp = mp;
1077 state->blocksize = state->mp->m_sb.sb_blocksize;
1078 state->node_ents = state->mp->m_attr_node_ents;
1079 state->inleaf = 0;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001080 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 if (error)
1082 goto out;
1083
1084 /*
1085 * Remove the name and update the hashvals in the tree.
1086 */
1087 blk = &state->path.blk[ state->path.active-1 ];
1088 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner517c2222013-04-24 18:58:55 +10001089 error = xfs_attr3_leaf_remove(blk->bp, args);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001090 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
1092 /*
1093 * Check to see if the tree needs to be collapsed.
1094 */
1095 if (retval && (state->path.active > 1)) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001096 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001097 error = xfs_da3_join(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 if (!error) {
1099 error = xfs_bmap_finish(&args->trans,
1100 args->flist,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 &committed);
1102 }
1103 if (error) {
1104 ASSERT(committed);
1105 args->trans = NULL;
1106 xfs_bmap_cancel(args->flist);
1107 goto out;
1108 }
1109
1110 /*
1111 * bmap_finish() may have committed the last trans
1112 * and started a new one. We need the inode to be
1113 * in all transactions.
1114 */
Christoph Hellwig898621d2010-06-24 11:36:58 +10001115 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +00001116 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 }
1118
1119 /*
1120 * Commit and start the next trans in the chain.
1121 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10001122 error = xfs_trans_roll(&args->trans, dp);
1123 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 goto out;
1125
1126 } else if (args->rmtblkno > 0) {
1127 /*
1128 * Added a "remote" value, just clear the incomplete flag.
1129 */
Dave Chinner517c2222013-04-24 18:58:55 +10001130 error = xfs_attr3_leaf_clearflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 if (error)
1132 goto out;
1133 }
1134 retval = error = 0;
1135
1136out:
1137 if (state)
1138 xfs_da_state_free(state);
1139 if (error)
1140 return(error);
1141 return(retval);
1142}
1143
1144/*
1145 * Remove a name from a B-tree attribute list.
1146 *
1147 * This will involve walking down the Btree, and may involve joining
1148 * leaf nodes and even joining intermediate nodes up to and including
1149 * the root node (a special case of an intermediate node).
1150 */
1151STATIC int
1152xfs_attr_node_removename(xfs_da_args_t *args)
1153{
1154 xfs_da_state_t *state;
1155 xfs_da_state_blk_t *blk;
1156 xfs_inode_t *dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001157 struct xfs_buf *bp;
Nathan Scottd8cc8902005-11-02 10:34:53 +11001158 int retval, error, committed, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Dave Chinner5a5881c2012-03-22 05:15:13 +00001160 trace_xfs_attr_node_removename(args);
1161
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 /*
1163 * Tie a string around our finger to remind us where we are.
1164 */
1165 dp = args->dp;
1166 state = xfs_da_state_alloc();
1167 state->args = args;
1168 state->mp = dp->i_mount;
1169 state->blocksize = state->mp->m_sb.sb_blocksize;
1170 state->node_ents = state->mp->m_attr_node_ents;
1171
1172 /*
1173 * Search to see if name exists, and get back a pointer to it.
1174 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001175 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 if (error || (retval != EEXIST)) {
1177 if (error == 0)
1178 error = retval;
1179 goto out;
1180 }
1181
1182 /*
1183 * If there is an out-of-line value, de-allocate the blocks.
1184 * This is done before we remove the attribute so that we don't
1185 * overflow the maximum size of a transaction and/or hit a deadlock.
1186 */
1187 blk = &state->path.blk[ state->path.active-1 ];
1188 ASSERT(blk->bp != NULL);
1189 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1190 if (args->rmtblkno > 0) {
1191 /*
1192 * Fill in disk block numbers in the state structure
1193 * so that we can get the buffers back after we commit
1194 * several transactions in the following calls.
1195 */
1196 error = xfs_attr_fillstate(state);
1197 if (error)
1198 goto out;
1199
1200 /*
1201 * Mark the attribute as INCOMPLETE, then bunmapi() the
1202 * remote value.
1203 */
Dave Chinner517c2222013-04-24 18:58:55 +10001204 error = xfs_attr3_leaf_setflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 if (error)
1206 goto out;
1207 error = xfs_attr_rmtval_remove(args);
1208 if (error)
1209 goto out;
1210
1211 /*
1212 * Refill the state structure with buffers, the prior calls
1213 * released our buffers.
1214 */
1215 error = xfs_attr_refillstate(state);
1216 if (error)
1217 goto out;
1218 }
1219
1220 /*
1221 * Remove the name and update the hashvals in the tree.
1222 */
1223 blk = &state->path.blk[ state->path.active-1 ];
1224 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner517c2222013-04-24 18:58:55 +10001225 retval = xfs_attr3_leaf_remove(blk->bp, args);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001226 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
1228 /*
1229 * Check to see if the tree needs to be collapsed.
1230 */
1231 if (retval && (state->path.active > 1)) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001232 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001233 error = xfs_da3_join(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 if (!error) {
1235 error = xfs_bmap_finish(&args->trans, args->flist,
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001236 &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 }
1238 if (error) {
1239 ASSERT(committed);
1240 args->trans = NULL;
1241 xfs_bmap_cancel(args->flist);
1242 goto out;
1243 }
1244
1245 /*
1246 * bmap_finish() may have committed the last trans and started
1247 * a new one. We need the inode to be in all transactions.
1248 */
Christoph Hellwig898621d2010-06-24 11:36:58 +10001249 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +00001250 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
1252 /*
1253 * Commit the Btree join operation and start a new trans.
1254 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10001255 error = xfs_trans_roll(&args->trans, dp);
1256 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 goto out;
1258 }
1259
1260 /*
1261 * If the result is small enough, push it all into the inode.
1262 */
1263 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
1264 /*
1265 * Have to get rid of the copy of this dabuf in the state.
1266 */
1267 ASSERT(state->path.active == 1);
1268 ASSERT(state->path.blk[0].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 state->path.blk[0].bp = NULL;
1270
Dave Chinner517c2222013-04-24 18:58:55 +10001271 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 if (error)
1273 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
Nathan Scottd8cc8902005-11-02 10:34:53 +11001275 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001276 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +10001277 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 /* bp is gone due to xfs_da_shrink_inode */
1279 if (!error) {
1280 error = xfs_bmap_finish(&args->trans,
1281 args->flist,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 &committed);
1283 }
1284 if (error) {
1285 ASSERT(committed);
1286 args->trans = NULL;
1287 xfs_bmap_cancel(args->flist);
1288 goto out;
1289 }
1290
1291 /*
1292 * bmap_finish() may have committed the last trans
1293 * and started a new one. We need the inode to be
1294 * in all transactions.
1295 */
Christoph Hellwig898621d2010-06-24 11:36:58 +10001296 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +00001297 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 } else
Dave Chinner1d9025e2012-06-22 18:50:14 +10001299 xfs_trans_brelse(args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 }
1301 error = 0;
1302
1303out:
1304 xfs_da_state_free(state);
1305 return(error);
1306}
1307
1308/*
1309 * Fill in the disk block numbers in the state structure for the buffers
1310 * that are attached to the state structure.
1311 * This is done so that we can quickly reattach ourselves to those buffers
Nathan Scottc41564b2006-03-29 08:55:14 +10001312 * after some set of transaction commits have released these buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 */
1314STATIC int
1315xfs_attr_fillstate(xfs_da_state_t *state)
1316{
1317 xfs_da_state_path_t *path;
1318 xfs_da_state_blk_t *blk;
1319 int level;
1320
Dave Chinneree732592012-11-12 22:53:53 +11001321 trace_xfs_attr_fillstate(state->args);
1322
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 /*
1324 * Roll down the "path" in the state structure, storing the on-disk
1325 * block number for those buffers in the "path".
1326 */
1327 path = &state->path;
1328 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1329 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1330 if (blk->bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001331 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 blk->bp = NULL;
1333 } else {
1334 blk->disk_blkno = 0;
1335 }
1336 }
1337
1338 /*
1339 * Roll down the "altpath" in the state structure, storing the on-disk
1340 * block number for those buffers in the "altpath".
1341 */
1342 path = &state->altpath;
1343 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1344 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1345 if (blk->bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001346 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 blk->bp = NULL;
1348 } else {
1349 blk->disk_blkno = 0;
1350 }
1351 }
1352
1353 return(0);
1354}
1355
1356/*
1357 * Reattach the buffers to the state structure based on the disk block
1358 * numbers stored in the state structure.
Nathan Scottc41564b2006-03-29 08:55:14 +10001359 * This is done after some set of transaction commits have released those
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 * buffers from our grip.
1361 */
1362STATIC int
1363xfs_attr_refillstate(xfs_da_state_t *state)
1364{
1365 xfs_da_state_path_t *path;
1366 xfs_da_state_blk_t *blk;
1367 int level, error;
1368
Dave Chinneree732592012-11-12 22:53:53 +11001369 trace_xfs_attr_refillstate(state->args);
1370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 /*
1372 * Roll down the "path" in the state structure, storing the on-disk
1373 * block number for those buffers in the "path".
1374 */
1375 path = &state->path;
1376 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1377 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1378 if (blk->disk_blkno) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001379 error = xfs_da3_node_read(state->args->trans,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 state->args->dp,
1381 blk->blkno, blk->disk_blkno,
Dave Chinnerd9392a42012-11-12 22:54:17 +11001382 &blk->bp, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 if (error)
1384 return(error);
1385 } else {
1386 blk->bp = NULL;
1387 }
1388 }
1389
1390 /*
1391 * Roll down the "altpath" in the state structure, storing the on-disk
1392 * block number for those buffers in the "altpath".
1393 */
1394 path = &state->altpath;
1395 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1396 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1397 if (blk->disk_blkno) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001398 error = xfs_da3_node_read(state->args->trans,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 state->args->dp,
1400 blk->blkno, blk->disk_blkno,
Dave Chinnerd9392a42012-11-12 22:54:17 +11001401 &blk->bp, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 if (error)
1403 return(error);
1404 } else {
1405 blk->bp = NULL;
1406 }
1407 }
1408
1409 return(0);
1410}
1411
1412/*
1413 * Look up a filename in a node attribute list.
1414 *
1415 * This routine gets called for any attribute fork that has more than one
1416 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1417 * "remote" values taking up more blocks.
1418 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001419STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420xfs_attr_node_get(xfs_da_args_t *args)
1421{
1422 xfs_da_state_t *state;
1423 xfs_da_state_blk_t *blk;
1424 int error, retval;
1425 int i;
1426
Dave Chinneree732592012-11-12 22:53:53 +11001427 trace_xfs_attr_node_get(args);
1428
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 state = xfs_da_state_alloc();
1430 state->args = args;
1431 state->mp = args->dp->i_mount;
1432 state->blocksize = state->mp->m_sb.sb_blocksize;
1433 state->node_ents = state->mp->m_attr_node_ents;
1434
1435 /*
1436 * Search to see if name exists, and get back a pointer to it.
1437 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001438 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 if (error) {
1440 retval = error;
1441 } else if (retval == EEXIST) {
1442 blk = &state->path.blk[ state->path.active-1 ];
1443 ASSERT(blk->bp != NULL);
1444 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1445
1446 /*
1447 * Get the value, local or "remote"
1448 */
Dave Chinner517c2222013-04-24 18:58:55 +10001449 retval = xfs_attr3_leaf_getvalue(blk->bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 if (!retval && (args->rmtblkno > 0)
1451 && !(args->flags & ATTR_KERNOVAL)) {
1452 retval = xfs_attr_rmtval_get(args);
1453 }
1454 }
1455
1456 /*
1457 * If not in a transaction, we have to release all the buffers.
1458 */
1459 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001460 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 state->path.blk[i].bp = NULL;
1462 }
1463
1464 xfs_da_state_free(state);
1465 return(retval);
1466}