blob: 1fc1f06277dabe91b0bfe07f96c6945568af38b2 [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 Chinner70a98832013-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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130 XFS_STATS_INC(xs_attr_get);
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000133 return EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 if (!xfs_inode_hasattr(ip))
136 return ENOATTR;
137
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000138 error = xfs_attr_args_init(&args, ip, name, flags);
Barry Naujoke8b0eba2008-04-22 17:34:31 +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(
Christoph Hellwig6c888af2014-05-13 16:40:19 +1000165 struct xfs_da_args *args,
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000166 int *local)
167{
Christoph Hellwig6c888af2014-05-13 16:40:19 +1000168 struct xfs_mount *mp = args->dp->i_mount;
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000169 int size;
170 int nblks;
171
172 /*
173 * Determine space new attribute will use, and if it would be
174 * "local" or "remote" (note: local != inline).
175 */
Christoph Hellwig6c888af2014-05-13 16:40:19 +1000176 size = xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000177 mp->m_sb.sb_blocksize, local);
178
179 nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
180 if (*local) {
181 if (size > (mp->m_sb.sb_blocksize >> 1)) {
182 /* Double split possible */
183 nblks *= 2;
184 }
185 } else {
186 /*
187 * Out of line attribute, cannot double split, but
188 * make room for the attribute value itself.
189 */
Dave Chinner2d6dcc62014-05-15 09:39:28 +1000190 uint dblocks = xfs_attr3_rmt_blocks(mp, args->valuelen);
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000191 nblks += dblocks;
192 nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
193 }
194
195 return nblks;
196}
197
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000198int
199xfs_attr_set(
200 struct xfs_inode *dp,
201 const unsigned char *name,
202 unsigned char *value,
203 int valuelen,
204 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
Jie Liu3d3c8b52013-08-12 20:49:59 +1000206 struct xfs_mount *mp = dp->i_mount;
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000207 struct xfs_da_args args;
208 struct xfs_bmap_free flist;
Jie Liu3d3c8b52013-08-12 20:49:59 +1000209 struct xfs_trans_res tres;
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000210 xfs_fsblock_t firstblock;
Jie Liu3d3c8b52013-08-12 20:49:59 +1000211 int rsvd = (flags & ATTR_ROOT) != 0;
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000212 int error, err2, committed, local;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000214 XFS_STATS_INC(xs_attr_set);
215
216 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
217 return EIO;
218
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000219 error = xfs_attr_args_init(&args, dp, name, flags);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000220 if (error)
221 return error;
222
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000223 args.value = value;
224 args.valuelen = valuelen;
225 args.firstblock = &firstblock;
226 args.flist = &flist;
227 args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
Christoph Hellwig6c888af2014-05-13 16:40:19 +1000228 args.total = xfs_attr_calc_size(&args, &local);
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000229
Christoph Hellwig7d095252009-06-08 15:33:32 +0200230 error = xfs_qm_dqattach(dp, 0);
231 if (error)
232 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 /*
235 * If the inode doesn't have an attribute fork, add one.
236 * (inode must not be locked when we call this routine)
237 */
238 if (XFS_IFORK_Q(dp) == 0) {
Barry Naujoke5889e92007-02-10 18:35:58 +1100239 int sf_size = sizeof(xfs_attr_sf_hdr_t) +
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000240 XFS_ATTR_SF_ENTSIZE_BYNAME(args.namelen, valuelen);
Barry Naujoke5889e92007-02-10 18:35:58 +1100241
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000242 error = xfs_bmap_add_attrfork(dp, sf_size, rsvd);
243 if (error)
244 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
246
247 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 * Start our first transaction of the day.
249 *
250 * All future transactions during this code must be "chained" off
251 * this one via the trans_dup() call. All transactions will contain
252 * the inode, and the inode will always be marked with trans_ihold().
253 * Since the inode will be locked in all transactions, we must log
254 * the inode in every transaction to let it float upward through
255 * the log.
256 */
257 args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_SET);
258
259 /*
260 * Root fork attributes can use reserved data blocks for this
261 * operation if necessary
262 */
263
264 if (rsvd)
265 args.trans->t_flags |= XFS_TRANS_RESERVE;
266
Jie Liu3d3c8b52013-08-12 20:49:59 +1000267 tres.tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
268 M_RES(mp)->tr_attrsetrt.tr_logres * args.total;
269 tres.tr_logcount = XFS_ATTRSET_LOG_COUNT;
270 tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
271 error = xfs_trans_reserve(args.trans, &tres, args.total, 0);
Jeff Liua21cd502013-01-28 21:27:53 +0800272 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 xfs_trans_cancel(args.trans, 0);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000274 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 }
276 xfs_ilock(dp, XFS_ILOCK_EXCL);
277
Christoph Hellwig7d095252009-06-08 15:33:32 +0200278 error = xfs_trans_reserve_quota_nblks(args.trans, dp, args.total, 0,
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000279 rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
280 XFS_QMOPT_RES_REGBLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 if (error) {
282 xfs_iunlock(dp, XFS_ILOCK_EXCL);
283 xfs_trans_cancel(args.trans, XFS_TRANS_RELEASE_LOG_RES);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000284 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
286
Christoph Hellwigddc34152011-09-19 15:00:54 +0000287 xfs_trans_ijoin(args.trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000290 * If the attribute list is non-existent or a shortform list,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 * upgrade it to a single-leaf-block attribute list.
292 */
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000293 if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL ||
294 (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
295 dp->i_d.di_anextents == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297 /*
298 * Build initial attribute list (if required).
299 */
300 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
Nathan Scottd8cc8902005-11-02 10:34:53 +1100301 xfs_attr_shortform_create(&args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303 /*
304 * Try to add the attr to the attribute list in
305 * the inode.
306 */
307 error = xfs_attr_shortform_addname(&args);
308 if (error != ENOSPC) {
309 /*
310 * Commit the shortform mods, and we're done.
311 * NOTE: this is also the error path (EEXIST, etc).
312 */
313 ASSERT(args.trans != NULL);
314
315 /*
316 * If this is a synchronous mount, make sure that
317 * the transaction goes to disk before returning
318 * to the user.
319 */
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000320 if (mp->m_flags & XFS_MOUNT_WSYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 xfs_trans_set_sync(args.trans);
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000322
323 if (!error && (flags & ATTR_KERNOTIME) == 0) {
324 xfs_trans_ichgtime(args.trans, dp,
325 XFS_ICHGTIME_CHG);
326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 err2 = xfs_trans_commit(args.trans,
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000328 XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 xfs_iunlock(dp, XFS_ILOCK_EXCL);
330
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000331 return error ? error : err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 }
333
334 /*
335 * It won't fit in the shortform, transform to a leaf block.
336 * GROT: another possible req'mt for a double-split btree op.
337 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600338 xfs_bmap_init(args.flist, args.firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 error = xfs_attr_shortform_to_leaf(&args);
340 if (!error) {
341 error = xfs_bmap_finish(&args.trans, args.flist,
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100342 &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
344 if (error) {
345 ASSERT(committed);
346 args.trans = NULL;
347 xfs_bmap_cancel(&flist);
348 goto out;
349 }
350
351 /*
352 * bmap_finish() may have committed the last trans and started
353 * a new one. We need the inode to be in all transactions.
354 */
Christoph Hellwig898621d2010-06-24 11:36:58 +1000355 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +0000356 xfs_trans_ijoin(args.trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358 /*
359 * Commit the leaf transformation. We'll need another (linked)
360 * transaction to add the new attribute to the leaf.
361 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000362
363 error = xfs_trans_roll(&args.trans, dp);
364 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 goto out;
366
367 }
368
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000369 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 error = xfs_attr_leaf_addname(&args);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000371 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 error = xfs_attr_node_addname(&args);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000373 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 /*
377 * If this is a synchronous mount, make sure that the
378 * transaction goes to disk before returning to the user.
379 */
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000380 if (mp->m_flags & XFS_MOUNT_WSYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 xfs_trans_set_sync(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000383 if ((flags & ATTR_KERNOTIME) == 0)
384 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 /*
387 * Commit the last in the sequence of transactions.
388 */
389 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000390 error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 xfs_iunlock(dp, XFS_ILOCK_EXCL);
392
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000393 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395out:
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000396 if (args.trans) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 xfs_trans_cancel(args.trans,
398 XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000401 return error;
Nathan Scottaa82daa2005-11-02 10:33:33 +1100402}
403
404/*
405 * Generic handler routine to remove a name from an attribute list.
406 * Transitions attribute list from Btree to shortform as necessary.
407 */
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000408int
409xfs_attr_remove(
410 struct xfs_inode *dp,
411 const unsigned char *name,
412 int flags)
Nathan Scottaa82daa2005-11-02 10:33:33 +1100413{
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000414 struct xfs_mount *mp = dp->i_mount;
415 struct xfs_da_args args;
416 struct xfs_bmap_free flist;
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000417 xfs_fsblock_t firstblock;
418 int error;
Nathan Scottaa82daa2005-11-02 10:33:33 +1100419
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000420 XFS_STATS_INC(xs_attr_remove);
421
422 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
423 return EIO;
424
425 if (!xfs_inode_hasattr(dp))
426 return ENOATTR;
427
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000428 error = xfs_attr_args_init(&args, dp, name, flags);
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000429 if (error)
430 return error;
431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 args.firstblock = &firstblock;
433 args.flist = &flist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 /*
Dave Chinner4a338212011-06-23 01:35:01 +0000436 * we have no control over the attribute names that userspace passes us
437 * to remove, so we have to allow the name lookup prior to attribute
438 * removal to fail.
439 */
440 args.op_flags = XFS_DA_OP_OKNOENT;
441
Christoph Hellwig7d095252009-06-08 15:33:32 +0200442 error = xfs_qm_dqattach(dp, 0);
443 if (error)
444 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 /*
447 * Start our first transaction of the day.
448 *
449 * All future transactions during this code must be "chained" off
450 * this one via the trans_dup() call. All transactions will contain
451 * the inode, and the inode will always be marked with trans_ihold().
452 * Since the inode will be locked in all transactions, we must log
453 * the inode in every transaction to let it float upward through
454 * the log.
455 */
456 args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_RM);
457
458 /*
459 * Root fork attributes can use reserved data blocks for this
460 * operation if necessary
461 */
462
463 if (flags & ATTR_ROOT)
464 args.trans->t_flags |= XFS_TRANS_RESERVE;
465
Jie Liu3d3c8b52013-08-12 20:49:59 +1000466 error = xfs_trans_reserve(args.trans, &M_RES(mp)->tr_attrrm,
467 XFS_ATTRRM_SPACE_RES(mp), 0);
468 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 xfs_trans_cancel(args.trans, 0);
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000470 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 }
472
473 xfs_ilock(dp, XFS_ILOCK_EXCL);
474 /*
475 * No need to make quota reservations here. We expect to release some
476 * blocks not allocate in the common case.
477 */
Christoph Hellwigddc34152011-09-19 15:00:54 +0000478 xfs_trans_ijoin(args.trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +1000480 if (!xfs_inode_hasattr(dp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 error = XFS_ERROR(ENOATTR);
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000482 } else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
484 error = xfs_attr_shortform_remove(&args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
486 error = xfs_attr_leaf_removename(&args);
487 } else {
488 error = xfs_attr_node_removename(&args);
489 }
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000490
491 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 /*
495 * If this is a synchronous mount, make sure that the
496 * transaction goes to disk before returning to the user.
497 */
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000498 if (mp->m_flags & XFS_MOUNT_WSYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 xfs_trans_set_sync(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000501 if ((flags & ATTR_KERNOTIME) == 0)
502 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 /*
505 * Commit the last in the sequence of transactions.
506 */
507 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000508 error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 xfs_iunlock(dp, XFS_ILOCK_EXCL);
510
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000511 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513out:
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000514 if (args.trans) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 xfs_trans_cancel(args.trans,
516 XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
Nathan Scottaa82daa2005-11-02 10:33:33 +1100517 }
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000518 xfs_iunlock(dp, XFS_ILOCK_EXCL);
519 return error;
Nathan Scottaa82daa2005-11-02 10:33:33 +1100520}
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522/*========================================================================
523 * External routines when attribute list is inside the inode
524 *========================================================================*/
525
526/*
527 * Add a name to the shortform attribute list structure
528 * This is the external routine.
529 */
530STATIC int
531xfs_attr_shortform_addname(xfs_da_args_t *args)
532{
Nathan Scottd8cc8902005-11-02 10:34:53 +1100533 int newsize, forkoff, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Dave Chinner5a5881c2012-03-22 05:15:13 +0000535 trace_xfs_attr_sf_addname(args);
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 retval = xfs_attr_shortform_lookup(args);
538 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
539 return(retval);
540 } else if (retval == EEXIST) {
541 if (args->flags & ATTR_CREATE)
542 return(retval);
543 retval = xfs_attr_shortform_remove(args);
544 ASSERT(retval == 0);
545 }
546
Nathan Scottd8cc8902005-11-02 10:34:53 +1100547 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
548 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
549 return(XFS_ERROR(ENOSPC));
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
552 newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100553
554 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
555 if (!forkoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 return(XFS_ERROR(ENOSPC));
Nathan Scottd8cc8902005-11-02 10:34:53 +1100557
558 xfs_attr_shortform_add(args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return(0);
560}
561
562
563/*========================================================================
564 * External routines when attribute list is one block
565 *========================================================================*/
566
567/*
568 * Add a name to the leaf attribute list structure
569 *
570 * This leaf block cannot have a "remote" value, we only call this routine
571 * if bmap_one_block() says there is only one block (ie: no remote blks).
572 */
David Chinnera8272ce2007-11-23 16:28:09 +1100573STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574xfs_attr_leaf_addname(xfs_da_args_t *args)
575{
576 xfs_inode_t *dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000577 struct xfs_buf *bp;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100578 int retval, error, committed, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Dave Chinner5a5881c2012-03-22 05:15:13 +0000580 trace_xfs_attr_leaf_addname(args);
581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 /*
583 * Read the (only) block in the attribute list in.
584 */
585 dp = args->dp;
586 args->blkno = 0;
Dave Chinner517c2222013-04-24 18:58:55 +1000587 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100589 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591 /*
592 * Look up the given attribute in the leaf block. Figure out if
593 * the given flags produce an error or call for an atomic rename.
594 */
Dave Chinner517c2222013-04-24 18:58:55 +1000595 retval = xfs_attr3_leaf_lookup_int(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000597 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000598 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 } else if (retval == EEXIST) {
600 if (args->flags & ATTR_CREATE) { /* pure create op */
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 }
Dave Chinner5a5881c2012-03-22 05:15:13 +0000604
605 trace_xfs_attr_leaf_replace(args);
606
Dave Chinner8275cdd2014-05-06 07:37:31 +1000607 /* save the attribute state for later removal*/
Barry Naujok6a178102008-05-21 16:42:05 +1000608 args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 args->blkno2 = args->blkno; /* set 2nd entry info*/
610 args->index2 = args->index;
611 args->rmtblkno2 = args->rmtblkno;
612 args->rmtblkcnt2 = args->rmtblkcnt;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000613 args->rmtvaluelen2 = args->rmtvaluelen;
614
615 /*
616 * clear the remote attr state now that it is saved so that the
617 * values reflect the state of the attribute we are about to
618 * add, not the attribute we just found and will remove later.
619 */
620 args->rmtblkno = 0;
621 args->rmtblkcnt = 0;
622 args->rmtvaluelen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 }
624
625 /*
626 * Add the attribute to the leaf block, transitioning to a Btree
627 * if required.
628 */
Dave Chinner517c2222013-04-24 18:58:55 +1000629 retval = xfs_attr3_leaf_add(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 if (retval == ENOSPC) {
631 /*
632 * Promote the attribute list to the Btree format, then
633 * Commit that transaction so that the node_addname() call
634 * can manage its own transactions.
635 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600636 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000637 error = xfs_attr3_leaf_to_node(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 if (!error) {
639 error = xfs_bmap_finish(&args->trans, args->flist,
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100640 &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 }
642 if (error) {
643 ASSERT(committed);
644 args->trans = NULL;
645 xfs_bmap_cancel(args->flist);
646 return(error);
647 }
648
649 /*
650 * bmap_finish() may have committed the last trans and started
651 * a new one. We need the inode to be in all transactions.
652 */
Christoph Hellwig898621d2010-06-24 11:36:58 +1000653 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +0000654 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656 /*
657 * Commit the current trans (including the inode) and start
658 * a new one.
659 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000660 error = xfs_trans_roll(&args->trans, dp);
661 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 return (error);
663
664 /*
665 * Fob the whole rest of the problem off on the Btree code.
666 */
667 error = xfs_attr_node_addname(args);
668 return(error);
669 }
670
671 /*
672 * Commit the transaction that added the attr name so that
673 * later routines can manage their own transactions.
674 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000675 error = xfs_trans_roll(&args->trans, dp);
676 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 return (error);
678
679 /*
680 * If there was an out-of-line value, allocate the blocks we
681 * identified for its storage and copy the value. This is done
682 * after we create the attribute so that we don't overflow the
683 * maximum size of a transaction and/or hit a deadlock.
684 */
685 if (args->rmtblkno > 0) {
686 error = xfs_attr_rmtval_set(args);
687 if (error)
688 return(error);
689 }
690
691 /*
692 * If this is an atomic rename operation, we must "flip" the
693 * incomplete flags on the "new" and "old" attribute/value pairs
694 * so that one disappears and one appears atomically. Then we
695 * must remove the "old" attribute/value pair.
696 */
Barry Naujok6a178102008-05-21 16:42:05 +1000697 if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 /*
699 * In a separate transaction, set the incomplete flag on the
700 * "old" attr and clear the incomplete flag on the "new" attr.
701 */
Dave Chinner517c2222013-04-24 18:58:55 +1000702 error = xfs_attr3_leaf_flipflags(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 if (error)
704 return(error);
705
706 /*
707 * Dismantle the "old" attribute/value pair by removing
708 * a "remote" value (if it exists).
709 */
710 args->index = args->index2;
711 args->blkno = args->blkno2;
712 args->rmtblkno = args->rmtblkno2;
713 args->rmtblkcnt = args->rmtblkcnt2;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000714 args->rmtvaluelen = args->rmtvaluelen2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 if (args->rmtblkno) {
716 error = xfs_attr_rmtval_remove(args);
717 if (error)
718 return(error);
719 }
720
721 /*
722 * Read in the block containing the "old" attr, then
723 * remove the "old" attr from that block (neat, huh!)
724 */
Dave Chinner517c2222013-04-24 18:58:55 +1000725 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
Dave Chinnerad14c332012-11-12 22:54:16 +1100726 -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100728 return error;
729
Dave Chinner517c2222013-04-24 18:58:55 +1000730 xfs_attr3_leaf_remove(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732 /*
733 * If the result is small enough, shrink it all into the inode.
734 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100735 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Eric Sandeen9d87c312009-01-14 23:22:07 -0600736 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000737 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 /* bp is gone due to xfs_da_shrink_inode */
739 if (!error) {
740 error = xfs_bmap_finish(&args->trans,
741 args->flist,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 &committed);
743 }
744 if (error) {
745 ASSERT(committed);
746 args->trans = NULL;
747 xfs_bmap_cancel(args->flist);
748 return(error);
749 }
750
751 /*
752 * bmap_finish() may have committed the last trans
753 * and started a new one. We need the inode to be
754 * in all transactions.
755 */
Christoph Hellwig898621d2010-06-24 11:36:58 +1000756 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +0000757 xfs_trans_ijoin(args->trans, dp, 0);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760 /*
761 * Commit the remove and start the next trans in series.
762 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000763 error = xfs_trans_roll(&args->trans, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765 } else if (args->rmtblkno > 0) {
766 /*
767 * Added a "remote" value, just clear the incomplete flag.
768 */
Dave Chinner517c2222013-04-24 18:58:55 +1000769 error = xfs_attr3_leaf_clearflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 }
Dave Chinner517c2222013-04-24 18:58:55 +1000771 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772}
773
774/*
775 * Remove a name from the leaf attribute list structure
776 *
777 * This leaf block cannot have a "remote" value, we only call this routine
778 * if bmap_one_block() says there is only one block (ie: no remote blks).
779 */
780STATIC int
781xfs_attr_leaf_removename(xfs_da_args_t *args)
782{
783 xfs_inode_t *dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000784 struct xfs_buf *bp;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100785 int error, committed, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Dave Chinner5a5881c2012-03-22 05:15:13 +0000787 trace_xfs_attr_leaf_removename(args);
788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 /*
790 * Remove the attribute.
791 */
792 dp = args->dp;
793 args->blkno = 0;
Dave Chinner517c2222013-04-24 18:58:55 +1000794 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Dave Chinnerad14c332012-11-12 22:54:16 +1100795 if (error)
796 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Dave Chinner517c2222013-04-24 18:58:55 +1000798 error = xfs_attr3_leaf_lookup_int(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 if (error == ENOATTR) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000800 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000801 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 }
803
Dave Chinner517c2222013-04-24 18:58:55 +1000804 xfs_attr3_leaf_remove(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
806 /*
807 * If the result is small enough, shrink it all into the inode.
808 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100809 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Eric Sandeen9d87c312009-01-14 23:22:07 -0600810 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000811 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 /* bp is gone due to xfs_da_shrink_inode */
813 if (!error) {
814 error = xfs_bmap_finish(&args->trans, args->flist,
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100815 &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 }
817 if (error) {
818 ASSERT(committed);
819 args->trans = NULL;
820 xfs_bmap_cancel(args->flist);
Dave Chinner517c2222013-04-24 18:58:55 +1000821 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 }
823
824 /*
825 * bmap_finish() may have committed the last trans and started
826 * a new one. We need the inode to be in all transactions.
827 */
Christoph Hellwig898621d2010-06-24 11:36:58 +1000828 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +0000829 xfs_trans_ijoin(args->trans, dp, 0);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000830 }
Dave Chinner517c2222013-04-24 18:58:55 +1000831 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832}
833
834/*
835 * Look up a name in a leaf attribute list structure.
836 *
837 * This leaf block cannot have a "remote" value, we only call this routine
838 * if bmap_one_block() says there is only one block (ie: no remote blks).
839 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000840STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841xfs_attr_leaf_get(xfs_da_args_t *args)
842{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000843 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 int error;
845
Dave Chinneree732592012-11-12 22:53:53 +1100846 trace_xfs_attr_leaf_get(args);
847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 args->blkno = 0;
Dave Chinner517c2222013-04-24 18:58:55 +1000849 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100851 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Dave Chinner517c2222013-04-24 18:58:55 +1000853 error = xfs_attr3_leaf_lookup_int(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 if (error != EEXIST) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000855 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000856 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 }
Dave Chinner517c2222013-04-24 18:58:55 +1000858 error = xfs_attr3_leaf_getvalue(bp, args);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000859 xfs_trans_brelse(args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
861 error = xfs_attr_rmtval_get(args);
862 }
Dave Chinner517c2222013-04-24 18:58:55 +1000863 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864}
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866/*========================================================================
867 * External routines when attribute list size > XFS_LBSIZE(mp).
868 *========================================================================*/
869
870/*
871 * Add a name to a Btree-format attribute list.
872 *
873 * This will involve walking down the Btree, and may involve splitting
874 * leaf nodes and even splitting intermediate nodes up to and including
875 * the root node (a special case of an intermediate node).
876 *
877 * "Remote" attribute values confuse the issue and atomic rename operations
878 * add a whole extra layer of confusion on top of that.
879 */
880STATIC int
881xfs_attr_node_addname(xfs_da_args_t *args)
882{
883 xfs_da_state_t *state;
884 xfs_da_state_blk_t *blk;
885 xfs_inode_t *dp;
886 xfs_mount_t *mp;
887 int committed, retval, error;
888
Dave Chinner5a5881c2012-03-22 05:15:13 +0000889 trace_xfs_attr_node_addname(args);
890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 /*
892 * Fill in bucket of arguments/results/context to carry around.
893 */
894 dp = args->dp;
895 mp = dp->i_mount;
896restart:
897 state = xfs_da_state_alloc();
898 state->args = args;
899 state->mp = mp;
900 state->blocksize = state->mp->m_sb.sb_blocksize;
901 state->node_ents = state->mp->m_attr_node_ents;
902
903 /*
904 * Search to see if name already exists, and get back a pointer
905 * to where it should go.
906 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000907 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 if (error)
909 goto out;
910 blk = &state->path.blk[ state->path.active-1 ];
911 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
912 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
913 goto out;
914 } else if (retval == EEXIST) {
915 if (args->flags & ATTR_CREATE)
916 goto out;
Dave Chinner5a5881c2012-03-22 05:15:13 +0000917
918 trace_xfs_attr_node_replace(args);
919
Dave Chinner8275cdd2014-05-06 07:37:31 +1000920 /* save the attribute state for later removal*/
Barry Naujok6a178102008-05-21 16:42:05 +1000921 args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 args->blkno2 = args->blkno; /* set 2nd entry info*/
923 args->index2 = args->index;
924 args->rmtblkno2 = args->rmtblkno;
925 args->rmtblkcnt2 = args->rmtblkcnt;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000926 args->rmtvaluelen2 = args->rmtvaluelen;
927
928 /*
929 * clear the remote attr state now that it is saved so that the
930 * values reflect the state of the attribute we are about to
931 * add, not the attribute we just found and will remove later.
932 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 args->rmtblkno = 0;
934 args->rmtblkcnt = 0;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000935 args->rmtvaluelen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 }
937
Dave Chinner517c2222013-04-24 18:58:55 +1000938 retval = xfs_attr3_leaf_add(blk->bp, state->args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 if (retval == ENOSPC) {
940 if (state->path.active == 1) {
941 /*
942 * Its really a single leaf node, but it had
943 * out-of-line values so it looked like it *might*
944 * have been a b-tree.
945 */
946 xfs_da_state_free(state);
Eric Sandeen6dd93e92013-07-31 20:18:54 -0500947 state = NULL;
Eric Sandeen9d87c312009-01-14 23:22:07 -0600948 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000949 error = xfs_attr3_leaf_to_node(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 if (!error) {
951 error = xfs_bmap_finish(&args->trans,
952 args->flist,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 &committed);
954 }
955 if (error) {
956 ASSERT(committed);
957 args->trans = NULL;
958 xfs_bmap_cancel(args->flist);
959 goto out;
960 }
961
962 /*
963 * bmap_finish() may have committed the last trans
964 * and started a new one. We need the inode to be
965 * in all transactions.
966 */
Christoph Hellwig898621d2010-06-24 11:36:58 +1000967 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +0000968 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
970 /*
971 * Commit the node conversion and start the next
972 * trans in the chain.
973 */
Niv Sardi322ff6b2008-08-13 16:05:49 +1000974 error = xfs_trans_roll(&args->trans, dp);
975 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 goto out;
977
978 goto restart;
979 }
980
981 /*
982 * Split as many Btree elements as required.
983 * This code tracks the new and old attr's location
984 * in the index/blkno/rmtblkno/rmtblkcnt fields and
985 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
986 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600987 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000988 error = xfs_da3_split(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 if (!error) {
990 error = xfs_bmap_finish(&args->trans, args->flist,
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100991 &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 }
993 if (error) {
994 ASSERT(committed);
995 args->trans = NULL;
996 xfs_bmap_cancel(args->flist);
997 goto out;
998 }
999
1000 /*
1001 * bmap_finish() may have committed the last trans and started
1002 * a new one. We need the inode to be in all transactions.
1003 */
Christoph Hellwig898621d2010-06-24 11:36:58 +10001004 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +00001005 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 } else {
1007 /*
1008 * Addition succeeded, update Btree hashvals.
1009 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001010 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 }
1012
1013 /*
1014 * Kill the state structure, we're done with it and need to
1015 * allow the buffers to come back later.
1016 */
1017 xfs_da_state_free(state);
1018 state = NULL;
1019
1020 /*
1021 * Commit the leaf addition or btree split and start the next
1022 * trans in the chain.
1023 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10001024 error = xfs_trans_roll(&args->trans, dp);
1025 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 goto out;
1027
1028 /*
1029 * If there was an out-of-line value, allocate the blocks we
1030 * identified for its storage and copy the value. This is done
1031 * after we create the attribute so that we don't overflow the
1032 * maximum size of a transaction and/or hit a deadlock.
1033 */
1034 if (args->rmtblkno > 0) {
1035 error = xfs_attr_rmtval_set(args);
1036 if (error)
1037 return(error);
1038 }
1039
1040 /*
1041 * If this is an atomic rename operation, we must "flip" the
1042 * incomplete flags on the "new" and "old" attribute/value pairs
1043 * so that one disappears and one appears atomically. Then we
1044 * must remove the "old" attribute/value pair.
1045 */
Barry Naujok6a178102008-05-21 16:42:05 +10001046 if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 /*
1048 * In a separate transaction, set the incomplete flag on the
1049 * "old" attr and clear the incomplete flag on the "new" attr.
1050 */
Dave Chinner517c2222013-04-24 18:58:55 +10001051 error = xfs_attr3_leaf_flipflags(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 if (error)
1053 goto out;
1054
1055 /*
1056 * Dismantle the "old" attribute/value pair by removing
1057 * a "remote" value (if it exists).
1058 */
1059 args->index = args->index2;
1060 args->blkno = args->blkno2;
1061 args->rmtblkno = args->rmtblkno2;
1062 args->rmtblkcnt = args->rmtblkcnt2;
Dave Chinner8275cdd2014-05-06 07:37:31 +10001063 args->rmtvaluelen = args->rmtvaluelen2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 if (args->rmtblkno) {
1065 error = xfs_attr_rmtval_remove(args);
1066 if (error)
1067 return(error);
1068 }
1069
1070 /*
1071 * Re-find the "old" attribute entry after any split ops.
1072 * The INCOMPLETE flag means that we will find the "old"
1073 * attr, not the "new" one.
1074 */
1075 args->flags |= XFS_ATTR_INCOMPLETE;
1076 state = xfs_da_state_alloc();
1077 state->args = args;
1078 state->mp = mp;
1079 state->blocksize = state->mp->m_sb.sb_blocksize;
1080 state->node_ents = state->mp->m_attr_node_ents;
1081 state->inleaf = 0;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001082 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 if (error)
1084 goto out;
1085
1086 /*
1087 * Remove the name and update the hashvals in the tree.
1088 */
1089 blk = &state->path.blk[ state->path.active-1 ];
1090 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner517c2222013-04-24 18:58:55 +10001091 error = xfs_attr3_leaf_remove(blk->bp, args);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001092 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
1094 /*
1095 * Check to see if the tree needs to be collapsed.
1096 */
1097 if (retval && (state->path.active > 1)) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001098 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001099 error = xfs_da3_join(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 if (!error) {
1101 error = xfs_bmap_finish(&args->trans,
1102 args->flist,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 &committed);
1104 }
1105 if (error) {
1106 ASSERT(committed);
1107 args->trans = NULL;
1108 xfs_bmap_cancel(args->flist);
1109 goto out;
1110 }
1111
1112 /*
1113 * bmap_finish() may have committed the last trans
1114 * and started a new one. We need the inode to be
1115 * in all transactions.
1116 */
Christoph Hellwig898621d2010-06-24 11:36:58 +10001117 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +00001118 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 }
1120
1121 /*
1122 * Commit and start the next trans in the chain.
1123 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10001124 error = xfs_trans_roll(&args->trans, dp);
1125 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 goto out;
1127
1128 } else if (args->rmtblkno > 0) {
1129 /*
1130 * Added a "remote" value, just clear the incomplete flag.
1131 */
Dave Chinner517c2222013-04-24 18:58:55 +10001132 error = xfs_attr3_leaf_clearflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 if (error)
1134 goto out;
1135 }
1136 retval = error = 0;
1137
1138out:
1139 if (state)
1140 xfs_da_state_free(state);
1141 if (error)
1142 return(error);
1143 return(retval);
1144}
1145
1146/*
1147 * Remove a name from a B-tree attribute list.
1148 *
1149 * This will involve walking down the Btree, and may involve joining
1150 * leaf nodes and even joining intermediate nodes up to and including
1151 * the root node (a special case of an intermediate node).
1152 */
1153STATIC int
1154xfs_attr_node_removename(xfs_da_args_t *args)
1155{
1156 xfs_da_state_t *state;
1157 xfs_da_state_blk_t *blk;
1158 xfs_inode_t *dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001159 struct xfs_buf *bp;
Nathan Scottd8cc8902005-11-02 10:34:53 +11001160 int retval, error, committed, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Dave Chinner5a5881c2012-03-22 05:15:13 +00001162 trace_xfs_attr_node_removename(args);
1163
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 /*
1165 * Tie a string around our finger to remind us where we are.
1166 */
1167 dp = args->dp;
1168 state = xfs_da_state_alloc();
1169 state->args = args;
1170 state->mp = dp->i_mount;
1171 state->blocksize = state->mp->m_sb.sb_blocksize;
1172 state->node_ents = state->mp->m_attr_node_ents;
1173
1174 /*
1175 * Search to see if name exists, and get back a pointer to it.
1176 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001177 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 if (error || (retval != EEXIST)) {
1179 if (error == 0)
1180 error = retval;
1181 goto out;
1182 }
1183
1184 /*
1185 * If there is an out-of-line value, de-allocate the blocks.
1186 * This is done before we remove the attribute so that we don't
1187 * overflow the maximum size of a transaction and/or hit a deadlock.
1188 */
1189 blk = &state->path.blk[ state->path.active-1 ];
1190 ASSERT(blk->bp != NULL);
1191 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1192 if (args->rmtblkno > 0) {
1193 /*
1194 * Fill in disk block numbers in the state structure
1195 * so that we can get the buffers back after we commit
1196 * several transactions in the following calls.
1197 */
1198 error = xfs_attr_fillstate(state);
1199 if (error)
1200 goto out;
1201
1202 /*
1203 * Mark the attribute as INCOMPLETE, then bunmapi() the
1204 * remote value.
1205 */
Dave Chinner517c2222013-04-24 18:58:55 +10001206 error = xfs_attr3_leaf_setflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 if (error)
1208 goto out;
1209 error = xfs_attr_rmtval_remove(args);
1210 if (error)
1211 goto out;
1212
1213 /*
1214 * Refill the state structure with buffers, the prior calls
1215 * released our buffers.
1216 */
1217 error = xfs_attr_refillstate(state);
1218 if (error)
1219 goto out;
1220 }
1221
1222 /*
1223 * Remove the name and update the hashvals in the tree.
1224 */
1225 blk = &state->path.blk[ state->path.active-1 ];
1226 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner517c2222013-04-24 18:58:55 +10001227 retval = xfs_attr3_leaf_remove(blk->bp, args);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001228 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
1230 /*
1231 * Check to see if the tree needs to be collapsed.
1232 */
1233 if (retval && (state->path.active > 1)) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001234 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001235 error = xfs_da3_join(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 if (!error) {
1237 error = xfs_bmap_finish(&args->trans, args->flist,
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001238 &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 }
1240 if (error) {
1241 ASSERT(committed);
1242 args->trans = NULL;
1243 xfs_bmap_cancel(args->flist);
1244 goto out;
1245 }
1246
1247 /*
1248 * bmap_finish() may have committed the last trans and started
1249 * a new one. We need the inode to be in all transactions.
1250 */
Christoph Hellwig898621d2010-06-24 11:36:58 +10001251 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +00001252 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
1254 /*
1255 * Commit the Btree join operation and start a new trans.
1256 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10001257 error = xfs_trans_roll(&args->trans, dp);
1258 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 goto out;
1260 }
1261
1262 /*
1263 * If the result is small enough, push it all into the inode.
1264 */
1265 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
1266 /*
1267 * Have to get rid of the copy of this dabuf in the state.
1268 */
1269 ASSERT(state->path.active == 1);
1270 ASSERT(state->path.blk[0].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 state->path.blk[0].bp = NULL;
1272
Dave Chinner517c2222013-04-24 18:58:55 +10001273 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 if (error)
1275 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Nathan Scottd8cc8902005-11-02 10:34:53 +11001277 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001278 xfs_bmap_init(args->flist, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +10001279 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 /* bp is gone due to xfs_da_shrink_inode */
1281 if (!error) {
1282 error = xfs_bmap_finish(&args->trans,
1283 args->flist,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 &committed);
1285 }
1286 if (error) {
1287 ASSERT(committed);
1288 args->trans = NULL;
1289 xfs_bmap_cancel(args->flist);
1290 goto out;
1291 }
1292
1293 /*
1294 * bmap_finish() may have committed the last trans
1295 * and started a new one. We need the inode to be
1296 * in all transactions.
1297 */
Christoph Hellwig898621d2010-06-24 11:36:58 +10001298 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +00001299 xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 } else
Dave Chinner1d9025e2012-06-22 18:50:14 +10001301 xfs_trans_brelse(args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 }
1303 error = 0;
1304
1305out:
1306 xfs_da_state_free(state);
1307 return(error);
1308}
1309
1310/*
1311 * Fill in the disk block numbers in the state structure for the buffers
1312 * that are attached to the state structure.
1313 * This is done so that we can quickly reattach ourselves to those buffers
Nathan Scottc41564b2006-03-29 08:55:14 +10001314 * after some set of transaction commits have released these buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 */
1316STATIC int
1317xfs_attr_fillstate(xfs_da_state_t *state)
1318{
1319 xfs_da_state_path_t *path;
1320 xfs_da_state_blk_t *blk;
1321 int level;
1322
Dave Chinneree732592012-11-12 22:53:53 +11001323 trace_xfs_attr_fillstate(state->args);
1324
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 /*
1326 * Roll down the "path" in the state structure, storing the on-disk
1327 * block number for those buffers in the "path".
1328 */
1329 path = &state->path;
1330 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1331 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1332 if (blk->bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001333 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 blk->bp = NULL;
1335 } else {
1336 blk->disk_blkno = 0;
1337 }
1338 }
1339
1340 /*
1341 * Roll down the "altpath" in the state structure, storing the on-disk
1342 * block number for those buffers in the "altpath".
1343 */
1344 path = &state->altpath;
1345 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1346 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1347 if (blk->bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001348 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 blk->bp = NULL;
1350 } else {
1351 blk->disk_blkno = 0;
1352 }
1353 }
1354
1355 return(0);
1356}
1357
1358/*
1359 * Reattach the buffers to the state structure based on the disk block
1360 * numbers stored in the state structure.
Nathan Scottc41564b2006-03-29 08:55:14 +10001361 * This is done after some set of transaction commits have released those
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 * buffers from our grip.
1363 */
1364STATIC int
1365xfs_attr_refillstate(xfs_da_state_t *state)
1366{
1367 xfs_da_state_path_t *path;
1368 xfs_da_state_blk_t *blk;
1369 int level, error;
1370
Dave Chinneree732592012-11-12 22:53:53 +11001371 trace_xfs_attr_refillstate(state->args);
1372
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 /*
1374 * Roll down the "path" in the state structure, storing the on-disk
1375 * block number for those buffers in the "path".
1376 */
1377 path = &state->path;
1378 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1379 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1380 if (blk->disk_blkno) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001381 error = xfs_da3_node_read(state->args->trans,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 state->args->dp,
1383 blk->blkno, blk->disk_blkno,
Dave Chinnerd9392a42012-11-12 22:54:17 +11001384 &blk->bp, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 if (error)
1386 return(error);
1387 } else {
1388 blk->bp = NULL;
1389 }
1390 }
1391
1392 /*
1393 * Roll down the "altpath" in the state structure, storing the on-disk
1394 * block number for those buffers in the "altpath".
1395 */
1396 path = &state->altpath;
1397 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1398 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1399 if (blk->disk_blkno) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001400 error = xfs_da3_node_read(state->args->trans,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 state->args->dp,
1402 blk->blkno, blk->disk_blkno,
Dave Chinnerd9392a42012-11-12 22:54:17 +11001403 &blk->bp, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 if (error)
1405 return(error);
1406 } else {
1407 blk->bp = NULL;
1408 }
1409 }
1410
1411 return(0);
1412}
1413
1414/*
1415 * Look up a filename in a node attribute list.
1416 *
1417 * This routine gets called for any attribute fork that has more than one
1418 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1419 * "remote" values taking up more blocks.
1420 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001421STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422xfs_attr_node_get(xfs_da_args_t *args)
1423{
1424 xfs_da_state_t *state;
1425 xfs_da_state_blk_t *blk;
1426 int error, retval;
1427 int i;
1428
Dave Chinneree732592012-11-12 22:53:53 +11001429 trace_xfs_attr_node_get(args);
1430
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 state = xfs_da_state_alloc();
1432 state->args = args;
1433 state->mp = args->dp->i_mount;
1434 state->blocksize = state->mp->m_sb.sb_blocksize;
1435 state->node_ents = state->mp->m_attr_node_ents;
1436
1437 /*
1438 * Search to see if name exists, and get back a pointer to it.
1439 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001440 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 if (error) {
1442 retval = error;
1443 } else if (retval == EEXIST) {
1444 blk = &state->path.blk[ state->path.active-1 ];
1445 ASSERT(blk->bp != NULL);
1446 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1447
1448 /*
1449 * Get the value, local or "remote"
1450 */
Dave Chinner517c2222013-04-24 18:58:55 +10001451 retval = xfs_attr3_leaf_getvalue(blk->bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 if (!retval && (args->rmtblkno > 0)
1453 && !(args->flags & ATTR_KERNOVAL)) {
1454 retval = xfs_attr_rmtval_get(args);
1455 }
1456 }
1457
1458 /*
1459 * If not in a transaction, we have to release all the buffers.
1460 */
1461 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001462 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 state->path.blk[i].bp = NULL;
1464 }
1465
1466 xfs_da_state_free(state);
1467 return(retval);
1468}