Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
7b718769 NS |
2 | * Copyright (c) 2000-2005 Silicon Graphics, Inc. |
3 | * All Rights Reserved. | |
1da177e4 | 4 | * |
7b718769 NS |
5 | * This program is free software; you can redistribute it and/or |
6 | * modify it under the terms of the GNU General Public License as | |
1da177e4 LT |
7 | * published by the Free Software Foundation. |
8 | * | |
7b718769 NS |
9 | * 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. | |
1da177e4 | 13 | * |
7b718769 NS |
14 | * 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 | |
1da177e4 | 17 | */ |
1da177e4 | 18 | #include "xfs.h" |
a844f451 | 19 | #include "xfs_fs.h" |
70a9883c | 20 | #include "xfs_shared.h" |
239880ef DC |
21 | #include "xfs_format.h" |
22 | #include "xfs_log_format.h" | |
23 | #include "xfs_trans_resv.h" | |
a844f451 | 24 | #include "xfs_bit.h" |
1da177e4 | 25 | #include "xfs_mount.h" |
57062787 | 26 | #include "xfs_da_format.h" |
a844f451 | 27 | #include "xfs_da_btree.h" |
a844f451 | 28 | #include "xfs_attr_sf.h" |
1da177e4 | 29 | #include "xfs_inode.h" |
a844f451 | 30 | #include "xfs_alloc.h" |
239880ef | 31 | #include "xfs_trans.h" |
a844f451 | 32 | #include "xfs_inode_item.h" |
1da177e4 | 33 | #include "xfs_bmap.h" |
68988114 | 34 | #include "xfs_bmap_util.h" |
a4fbe6ab | 35 | #include "xfs_bmap_btree.h" |
1da177e4 LT |
36 | #include "xfs_attr.h" |
37 | #include "xfs_attr_leaf.h" | |
95920cd6 | 38 | #include "xfs_attr_remote.h" |
1da177e4 | 39 | #include "xfs_error.h" |
1da177e4 | 40 | #include "xfs_quota.h" |
1da177e4 | 41 | #include "xfs_trans_space.h" |
0b1b213f | 42 | #include "xfs_trace.h" |
1da177e4 LT |
43 | |
44 | /* | |
45 | * xfs_attr.c | |
46 | * | |
47 | * Provide the external interfaces to manage attribute lists. | |
48 | */ | |
49 | ||
50 | /*======================================================================== | |
51 | * Function prototypes for the kernel. | |
52 | *========================================================================*/ | |
53 | ||
54 | /* | |
55 | * Internal routines when attribute list fits inside the inode. | |
56 | */ | |
57 | STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args); | |
58 | ||
59 | /* | |
60 | * Internal routines when attribute list is one block. | |
61 | */ | |
ba0f32d4 | 62 | STATIC int xfs_attr_leaf_get(xfs_da_args_t *args); |
1da177e4 LT |
63 | STATIC int xfs_attr_leaf_addname(xfs_da_args_t *args); |
64 | STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args); | |
1da177e4 LT |
65 | |
66 | /* | |
67 | * Internal routines when attribute list is more than one block. | |
68 | */ | |
ba0f32d4 | 69 | STATIC int xfs_attr_node_get(xfs_da_args_t *args); |
1da177e4 LT |
70 | STATIC int xfs_attr_node_addname(xfs_da_args_t *args); |
71 | STATIC int xfs_attr_node_removename(xfs_da_args_t *args); | |
1da177e4 LT |
72 | STATIC int xfs_attr_fillstate(xfs_da_state_t *state); |
73 | STATIC int xfs_attr_refillstate(xfs_da_state_t *state); | |
74 | ||
1da177e4 | 75 | |
e8b0ebaa | 76 | STATIC int |
67fd718f CH |
77 | xfs_attr_args_init( |
78 | struct xfs_da_args *args, | |
79 | struct xfs_inode *dp, | |
80 | const unsigned char *name, | |
81 | int flags) | |
e8b0ebaa | 82 | { |
67fd718f CH |
83 | |
84 | if (!name) | |
2451337d | 85 | return -EINVAL; |
67fd718f CH |
86 | |
87 | memset(args, 0, sizeof(*args)); | |
0650b554 | 88 | args->geo = dp->i_mount->m_attr_geo; |
67fd718f CH |
89 | args->whichfork = XFS_ATTR_FORK; |
90 | args->dp = dp; | |
91 | args->flags = flags; | |
92 | args->name = name; | |
93 | args->namelen = strlen((const char *)name); | |
94 | if (args->namelen >= MAXNAMELEN) | |
2451337d | 95 | return -EFAULT; /* match IRIX behaviour */ |
e8b0ebaa | 96 | |
67fd718f | 97 | args->hashval = xfs_da_hashname(args->name, args->namelen); |
e8b0ebaa BN |
98 | return 0; |
99 | } | |
1da177e4 | 100 | |
abec5f2b | 101 | int |
caf8aabd CH |
102 | xfs_inode_hasattr( |
103 | struct xfs_inode *ip) | |
104 | { | |
105 | if (!XFS_IFORK_Q(ip) || | |
106 | (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS && | |
107 | ip->i_d.di_anextents == 0)) | |
108 | return 0; | |
109 | return 1; | |
110 | } | |
111 | ||
1da177e4 LT |
112 | /*======================================================================== |
113 | * Overall external interface routines. | |
114 | *========================================================================*/ | |
115 | ||
b87d022c CH |
116 | int |
117 | xfs_attr_get( | |
e82fa0c7 | 118 | struct xfs_inode *ip, |
b87d022c | 119 | const unsigned char *name, |
a9273ca5 | 120 | unsigned char *value, |
e82fa0c7 CH |
121 | int *valuelenp, |
122 | int flags) | |
1da177e4 | 123 | { |
b87d022c | 124 | struct xfs_da_args args; |
b87d022c CH |
125 | uint lock_mode; |
126 | int error; | |
127 | ||
ff6d6af2 | 128 | XFS_STATS_INC(ip->i_mount, xs_attr_get); |
b87d022c CH |
129 | |
130 | if (XFS_FORCED_SHUTDOWN(ip->i_mount)) | |
2451337d | 131 | return -EIO; |
1da177e4 | 132 | |
caf8aabd | 133 | if (!xfs_inode_hasattr(ip)) |
2451337d | 134 | return -ENOATTR; |
1da177e4 | 135 | |
67fd718f | 136 | error = xfs_attr_args_init(&args, ip, name, flags); |
b87d022c CH |
137 | if (error) |
138 | return error; | |
139 | ||
1da177e4 LT |
140 | args.value = value; |
141 | args.valuelen = *valuelenp; | |
c400ee3e ES |
142 | /* Entirely possible to look up a name which doesn't exist */ |
143 | args.op_flags = XFS_DA_OP_OKNOENT; | |
1da177e4 | 144 | |
b87d022c CH |
145 | lock_mode = xfs_ilock_attr_map_shared(ip); |
146 | if (!xfs_inode_hasattr(ip)) | |
2451337d | 147 | error = -ENOATTR; |
b87d022c | 148 | else if (ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) |
1da177e4 | 149 | error = xfs_attr_shortform_getvalue(&args); |
b87d022c | 150 | else if (xfs_bmap_one_block(ip, XFS_ATTR_FORK)) |
1da177e4 | 151 | error = xfs_attr_leaf_get(&args); |
b87d022c | 152 | else |
1da177e4 | 153 | error = xfs_attr_node_get(&args); |
b87d022c | 154 | xfs_iunlock(ip, lock_mode); |
1da177e4 | 155 | |
1da177e4 | 156 | *valuelenp = args.valuelen; |
2451337d | 157 | return error == -EEXIST ? 0 : error; |
1da177e4 LT |
158 | } |
159 | ||
5e9da7b7 NS |
160 | /* |
161 | * Calculate how many blocks we need for the new attribute, | |
162 | */ | |
5d77c0dc | 163 | STATIC int |
5e9da7b7 | 164 | xfs_attr_calc_size( |
6c888af0 | 165 | struct xfs_da_args *args, |
5e9da7b7 NS |
166 | int *local) |
167 | { | |
6c888af0 | 168 | struct xfs_mount *mp = args->dp->i_mount; |
5e9da7b7 NS |
169 | 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 | */ | |
c59f0ad2 | 176 | size = xfs_attr_leaf_newentsize(args, local); |
5e9da7b7 NS |
177 | nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK); |
178 | if (*local) { | |
33a60390 | 179 | if (size > (args->geo->blksize / 2)) { |
5e9da7b7 NS |
180 | /* Double split possible */ |
181 | nblks *= 2; | |
182 | } | |
183 | } else { | |
184 | /* | |
185 | * Out of line attribute, cannot double split, but | |
186 | * make room for the attribute value itself. | |
187 | */ | |
2d6dcc6d | 188 | uint dblocks = xfs_attr3_rmt_blocks(mp, args->valuelen); |
5e9da7b7 NS |
189 | nblks += dblocks; |
190 | nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK); | |
191 | } | |
192 | ||
193 | return nblks; | |
194 | } | |
195 | ||
c5b4ac39 CH |
196 | int |
197 | xfs_attr_set( | |
198 | struct xfs_inode *dp, | |
199 | const unsigned char *name, | |
200 | unsigned char *value, | |
201 | int valuelen, | |
202 | int flags) | |
1da177e4 | 203 | { |
3d3c8b52 | 204 | struct xfs_mount *mp = dp->i_mount; |
c5b4ac39 CH |
205 | struct xfs_da_args args; |
206 | struct xfs_bmap_free flist; | |
3d3c8b52 | 207 | struct xfs_trans_res tres; |
c5b4ac39 | 208 | xfs_fsblock_t firstblock; |
3d3c8b52 | 209 | int rsvd = (flags & ATTR_ROOT) != 0; |
f6106efa | 210 | int error, err2, local; |
c5b4ac39 | 211 | |
ff6d6af2 | 212 | XFS_STATS_INC(mp, xs_attr_set); |
c5b4ac39 CH |
213 | |
214 | if (XFS_FORCED_SHUTDOWN(dp->i_mount)) | |
2451337d | 215 | return -EIO; |
c5b4ac39 | 216 | |
67fd718f | 217 | error = xfs_attr_args_init(&args, dp, name, flags); |
c5b4ac39 CH |
218 | if (error) |
219 | return error; | |
1da177e4 | 220 | |
67fd718f CH |
221 | args.value = value; |
222 | args.valuelen = valuelen; | |
223 | args.firstblock = &firstblock; | |
224 | args.flist = &flist; | |
225 | args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT; | |
6c888af0 | 226 | args.total = xfs_attr_calc_size(&args, &local); |
1da177e4 | 227 | |
7d095257 CH |
228 | error = xfs_qm_dqattach(dp, 0); |
229 | if (error) | |
230 | return error; | |
1da177e4 LT |
231 | |
232 | /* | |
233 | * If the inode doesn't have an attribute fork, add one. | |
234 | * (inode must not be locked when we call this routine) | |
235 | */ | |
236 | if (XFS_IFORK_Q(dp) == 0) { | |
e5889e90 | 237 | int sf_size = sizeof(xfs_attr_sf_hdr_t) + |
67fd718f | 238 | XFS_ATTR_SF_ENTSIZE_BYNAME(args.namelen, valuelen); |
e5889e90 | 239 | |
c5b4ac39 CH |
240 | error = xfs_bmap_add_attrfork(dp, sf_size, rsvd); |
241 | if (error) | |
242 | return error; | |
1da177e4 LT |
243 | } |
244 | ||
253f4911 CH |
245 | tres.tr_logres = M_RES(mp)->tr_attrsetm.tr_logres + |
246 | M_RES(mp)->tr_attrsetrt.tr_logres * args.total; | |
247 | tres.tr_logcount = XFS_ATTRSET_LOG_COUNT; | |
248 | tres.tr_logflags = XFS_TRANS_PERM_LOG_RES; | |
1da177e4 LT |
249 | |
250 | /* | |
251 | * Root fork attributes can use reserved data blocks for this | |
252 | * operation if necessary | |
253 | */ | |
253f4911 CH |
254 | error = xfs_trans_alloc(mp, &tres, args.total, 0, |
255 | rsvd ? XFS_TRANS_RESERVE : 0, &args.trans); | |
256 | if (error) | |
c5b4ac39 | 257 | return error; |
1da177e4 | 258 | |
253f4911 | 259 | xfs_ilock(dp, XFS_ILOCK_EXCL); |
7d095257 | 260 | error = xfs_trans_reserve_quota_nblks(args.trans, dp, args.total, 0, |
5e9da7b7 NS |
261 | rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES : |
262 | XFS_QMOPT_RES_REGBLKS); | |
1da177e4 LT |
263 | if (error) { |
264 | xfs_iunlock(dp, XFS_ILOCK_EXCL); | |
4906e215 | 265 | xfs_trans_cancel(args.trans); |
c5b4ac39 | 266 | return error; |
1da177e4 LT |
267 | } |
268 | ||
ddc3415a | 269 | xfs_trans_ijoin(args.trans, dp, 0); |
1da177e4 LT |
270 | |
271 | /* | |
c41564b5 | 272 | * If the attribute list is non-existent or a shortform list, |
1da177e4 LT |
273 | * upgrade it to a single-leaf-block attribute list. |
274 | */ | |
c5b4ac39 CH |
275 | if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL || |
276 | (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS && | |
277 | dp->i_d.di_anextents == 0)) { | |
1da177e4 LT |
278 | |
279 | /* | |
280 | * Build initial attribute list (if required). | |
281 | */ | |
282 | if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS) | |
d8cc890d | 283 | xfs_attr_shortform_create(&args); |
1da177e4 LT |
284 | |
285 | /* | |
286 | * Try to add the attr to the attribute list in | |
287 | * the inode. | |
288 | */ | |
289 | error = xfs_attr_shortform_addname(&args); | |
2451337d | 290 | if (error != -ENOSPC) { |
1da177e4 LT |
291 | /* |
292 | * Commit the shortform mods, and we're done. | |
293 | * NOTE: this is also the error path (EEXIST, etc). | |
294 | */ | |
295 | ASSERT(args.trans != NULL); | |
296 | ||
297 | /* | |
298 | * If this is a synchronous mount, make sure that | |
299 | * the transaction goes to disk before returning | |
300 | * to the user. | |
301 | */ | |
c5b4ac39 | 302 | if (mp->m_flags & XFS_MOUNT_WSYNC) |
1da177e4 | 303 | xfs_trans_set_sync(args.trans); |
dcd79a14 DC |
304 | |
305 | if (!error && (flags & ATTR_KERNOTIME) == 0) { | |
306 | xfs_trans_ichgtime(args.trans, dp, | |
307 | XFS_ICHGTIME_CHG); | |
308 | } | |
70393313 | 309 | err2 = xfs_trans_commit(args.trans); |
1da177e4 LT |
310 | xfs_iunlock(dp, XFS_ILOCK_EXCL); |
311 | ||
c5b4ac39 | 312 | return error ? error : err2; |
1da177e4 LT |
313 | } |
314 | ||
315 | /* | |
316 | * It won't fit in the shortform, transform to a leaf block. | |
317 | * GROT: another possible req'mt for a double-split btree op. | |
318 | */ | |
9d87c319 | 319 | xfs_bmap_init(args.flist, args.firstblock); |
1da177e4 | 320 | error = xfs_attr_shortform_to_leaf(&args); |
f6106efa ES |
321 | if (!error) |
322 | error = xfs_bmap_finish(&args.trans, args.flist, dp); | |
1da177e4 | 323 | if (error) { |
1da177e4 LT |
324 | args.trans = NULL; |
325 | xfs_bmap_cancel(&flist); | |
326 | goto out; | |
327 | } | |
328 | ||
1da177e4 LT |
329 | /* |
330 | * Commit the leaf transformation. We'll need another (linked) | |
331 | * transaction to add the new attribute to the leaf. | |
332 | */ | |
322ff6b8 NS |
333 | |
334 | error = xfs_trans_roll(&args.trans, dp); | |
335 | if (error) | |
1da177e4 LT |
336 | goto out; |
337 | ||
338 | } | |
339 | ||
c5b4ac39 | 340 | if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) |
1da177e4 | 341 | error = xfs_attr_leaf_addname(&args); |
c5b4ac39 | 342 | else |
1da177e4 | 343 | error = xfs_attr_node_addname(&args); |
c5b4ac39 | 344 | if (error) |
1da177e4 | 345 | goto out; |
1da177e4 LT |
346 | |
347 | /* | |
348 | * If this is a synchronous mount, make sure that the | |
349 | * transaction goes to disk before returning to the user. | |
350 | */ | |
c5b4ac39 | 351 | if (mp->m_flags & XFS_MOUNT_WSYNC) |
1da177e4 | 352 | xfs_trans_set_sync(args.trans); |
1da177e4 | 353 | |
dcd79a14 DC |
354 | if ((flags & ATTR_KERNOTIME) == 0) |
355 | xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG); | |
356 | ||
1da177e4 LT |
357 | /* |
358 | * Commit the last in the sequence of transactions. | |
359 | */ | |
360 | xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE); | |
70393313 | 361 | error = xfs_trans_commit(args.trans); |
1da177e4 LT |
362 | xfs_iunlock(dp, XFS_ILOCK_EXCL); |
363 | ||
c5b4ac39 | 364 | return error; |
1da177e4 LT |
365 | |
366 | out: | |
4906e215 CH |
367 | if (args.trans) |
368 | xfs_trans_cancel(args.trans); | |
1da177e4 | 369 | xfs_iunlock(dp, XFS_ILOCK_EXCL); |
c5b4ac39 | 370 | return error; |
1da177e4 LT |
371 | } |
372 | ||
aa82daa0 NS |
373 | /* |
374 | * Generic handler routine to remove a name from an attribute list. | |
375 | * Transitions attribute list from Btree to shortform as necessary. | |
376 | */ | |
aa82daa0 | 377 | int |
1bc426a7 CH |
378 | xfs_attr_remove( |
379 | struct xfs_inode *dp, | |
380 | const unsigned char *name, | |
381 | int flags) | |
1da177e4 | 382 | { |
1bc426a7 CH |
383 | struct xfs_mount *mp = dp->i_mount; |
384 | struct xfs_da_args args; | |
385 | struct xfs_bmap_free flist; | |
1bc426a7 CH |
386 | xfs_fsblock_t firstblock; |
387 | int error; | |
1da177e4 | 388 | |
ff6d6af2 | 389 | XFS_STATS_INC(mp, xs_attr_remove); |
1da177e4 | 390 | |
aa82daa0 | 391 | if (XFS_FORCED_SHUTDOWN(dp->i_mount)) |
2451337d | 392 | return -EIO; |
1bc426a7 CH |
393 | |
394 | if (!xfs_inode_hasattr(dp)) | |
2451337d | 395 | return -ENOATTR; |
1da177e4 | 396 | |
67fd718f | 397 | error = xfs_attr_args_init(&args, dp, name, flags); |
e8b0ebaa BN |
398 | if (error) |
399 | return error; | |
400 | ||
1da177e4 LT |
401 | args.firstblock = &firstblock; |
402 | args.flist = &flist; | |
1da177e4 | 403 | |
4a338212 DC |
404 | /* |
405 | * we have no control over the attribute names that userspace passes us | |
406 | * to remove, so we have to allow the name lookup prior to attribute | |
407 | * removal to fail. | |
408 | */ | |
409 | args.op_flags = XFS_DA_OP_OKNOENT; | |
410 | ||
7d095257 CH |
411 | error = xfs_qm_dqattach(dp, 0); |
412 | if (error) | |
413 | return error; | |
1da177e4 | 414 | |
1da177e4 LT |
415 | /* |
416 | * Root fork attributes can use reserved data blocks for this | |
417 | * operation if necessary | |
418 | */ | |
253f4911 CH |
419 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_attrrm, |
420 | XFS_ATTRRM_SPACE_RES(mp), 0, | |
421 | (flags & ATTR_ROOT) ? XFS_TRANS_RESERVE : 0, | |
422 | &args.trans); | |
423 | if (error) | |
1bc426a7 | 424 | return error; |
1da177e4 LT |
425 | |
426 | xfs_ilock(dp, XFS_ILOCK_EXCL); | |
427 | /* | |
428 | * No need to make quota reservations here. We expect to release some | |
429 | * blocks not allocate in the common case. | |
430 | */ | |
ddc3415a | 431 | xfs_trans_ijoin(args.trans, dp, 0); |
1da177e4 | 432 | |
caf8aabd | 433 | if (!xfs_inode_hasattr(dp)) { |
2451337d | 434 | error = -ENOATTR; |
1bc426a7 | 435 | } else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) { |
1da177e4 LT |
436 | ASSERT(dp->i_afp->if_flags & XFS_IFINLINE); |
437 | error = xfs_attr_shortform_remove(&args); | |
1da177e4 LT |
438 | } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) { |
439 | error = xfs_attr_leaf_removename(&args); | |
440 | } else { | |
441 | error = xfs_attr_node_removename(&args); | |
442 | } | |
1bc426a7 CH |
443 | |
444 | if (error) | |
1da177e4 | 445 | goto out; |
1da177e4 LT |
446 | |
447 | /* | |
448 | * If this is a synchronous mount, make sure that the | |
449 | * transaction goes to disk before returning to the user. | |
450 | */ | |
1bc426a7 | 451 | if (mp->m_flags & XFS_MOUNT_WSYNC) |
1da177e4 | 452 | xfs_trans_set_sync(args.trans); |
1da177e4 | 453 | |
dcd79a14 DC |
454 | if ((flags & ATTR_KERNOTIME) == 0) |
455 | xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG); | |
456 | ||
1da177e4 LT |
457 | /* |
458 | * Commit the last in the sequence of transactions. | |
459 | */ | |
460 | xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE); | |
70393313 | 461 | error = xfs_trans_commit(args.trans); |
1da177e4 LT |
462 | xfs_iunlock(dp, XFS_ILOCK_EXCL); |
463 | ||
1bc426a7 | 464 | return error; |
1da177e4 LT |
465 | |
466 | out: | |
4906e215 CH |
467 | if (args.trans) |
468 | xfs_trans_cancel(args.trans); | |
1bc426a7 CH |
469 | xfs_iunlock(dp, XFS_ILOCK_EXCL); |
470 | return error; | |
aa82daa0 NS |
471 | } |
472 | ||
1da177e4 LT |
473 | /*======================================================================== |
474 | * External routines when attribute list is inside the inode | |
475 | *========================================================================*/ | |
476 | ||
477 | /* | |
478 | * Add a name to the shortform attribute list structure | |
479 | * This is the external routine. | |
480 | */ | |
481 | STATIC int | |
482 | xfs_attr_shortform_addname(xfs_da_args_t *args) | |
483 | { | |
d8cc890d | 484 | int newsize, forkoff, retval; |
1da177e4 | 485 | |
5a5881cd DC |
486 | trace_xfs_attr_sf_addname(args); |
487 | ||
1da177e4 | 488 | retval = xfs_attr_shortform_lookup(args); |
2451337d | 489 | if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) { |
d99831ff | 490 | return retval; |
2451337d | 491 | } else if (retval == -EEXIST) { |
1da177e4 | 492 | if (args->flags & ATTR_CREATE) |
d99831ff | 493 | return retval; |
1da177e4 LT |
494 | retval = xfs_attr_shortform_remove(args); |
495 | ASSERT(retval == 0); | |
496 | } | |
497 | ||
d8cc890d NS |
498 | if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX || |
499 | args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX) | |
2451337d | 500 | return -ENOSPC; |
d8cc890d | 501 | |
1da177e4 LT |
502 | newsize = XFS_ATTR_SF_TOTSIZE(args->dp); |
503 | newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen); | |
d8cc890d NS |
504 | |
505 | forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize); | |
506 | if (!forkoff) | |
2451337d | 507 | return -ENOSPC; |
d8cc890d NS |
508 | |
509 | xfs_attr_shortform_add(args, forkoff); | |
d99831ff | 510 | return 0; |
1da177e4 LT |
511 | } |
512 | ||
513 | ||
514 | /*======================================================================== | |
515 | * External routines when attribute list is one block | |
516 | *========================================================================*/ | |
517 | ||
518 | /* | |
519 | * Add a name to the leaf attribute list structure | |
520 | * | |
521 | * This leaf block cannot have a "remote" value, we only call this routine | |
522 | * if bmap_one_block() says there is only one block (ie: no remote blks). | |
523 | */ | |
a8272ce0 | 524 | STATIC int |
1da177e4 LT |
525 | xfs_attr_leaf_addname(xfs_da_args_t *args) |
526 | { | |
527 | xfs_inode_t *dp; | |
1d9025e5 | 528 | struct xfs_buf *bp; |
f6106efa | 529 | int retval, error, forkoff; |
1da177e4 | 530 | |
5a5881cd DC |
531 | trace_xfs_attr_leaf_addname(args); |
532 | ||
1da177e4 LT |
533 | /* |
534 | * Read the (only) block in the attribute list in. | |
535 | */ | |
536 | dp = args->dp; | |
537 | args->blkno = 0; | |
517c2220 | 538 | error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp); |
1da177e4 | 539 | if (error) |
ad14c33a | 540 | return error; |
1da177e4 LT |
541 | |
542 | /* | |
543 | * Look up the given attribute in the leaf block. Figure out if | |
544 | * the given flags produce an error or call for an atomic rename. | |
545 | */ | |
517c2220 | 546 | retval = xfs_attr3_leaf_lookup_int(bp, args); |
2451337d | 547 | if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) { |
1d9025e5 | 548 | xfs_trans_brelse(args->trans, bp); |
517c2220 | 549 | return retval; |
2451337d | 550 | } else if (retval == -EEXIST) { |
1da177e4 | 551 | if (args->flags & ATTR_CREATE) { /* pure create op */ |
1d9025e5 | 552 | xfs_trans_brelse(args->trans, bp); |
517c2220 | 553 | return retval; |
1da177e4 | 554 | } |
5a5881cd DC |
555 | |
556 | trace_xfs_attr_leaf_replace(args); | |
557 | ||
8275cdd0 | 558 | /* save the attribute state for later removal*/ |
6a178100 | 559 | args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */ |
1da177e4 LT |
560 | args->blkno2 = args->blkno; /* set 2nd entry info*/ |
561 | args->index2 = args->index; | |
562 | args->rmtblkno2 = args->rmtblkno; | |
563 | args->rmtblkcnt2 = args->rmtblkcnt; | |
8275cdd0 DC |
564 | args->rmtvaluelen2 = args->rmtvaluelen; |
565 | ||
566 | /* | |
567 | * clear the remote attr state now that it is saved so that the | |
568 | * values reflect the state of the attribute we are about to | |
569 | * add, not the attribute we just found and will remove later. | |
570 | */ | |
571 | args->rmtblkno = 0; | |
572 | args->rmtblkcnt = 0; | |
573 | args->rmtvaluelen = 0; | |
1da177e4 LT |
574 | } |
575 | ||
576 | /* | |
577 | * Add the attribute to the leaf block, transitioning to a Btree | |
578 | * if required. | |
579 | */ | |
517c2220 | 580 | retval = xfs_attr3_leaf_add(bp, args); |
2451337d | 581 | if (retval == -ENOSPC) { |
1da177e4 LT |
582 | /* |
583 | * Promote the attribute list to the Btree format, then | |
584 | * Commit that transaction so that the node_addname() call | |
585 | * can manage its own transactions. | |
586 | */ | |
9d87c319 | 587 | xfs_bmap_init(args->flist, args->firstblock); |
517c2220 | 588 | error = xfs_attr3_leaf_to_node(args); |
f6106efa ES |
589 | if (!error) |
590 | error = xfs_bmap_finish(&args->trans, args->flist, dp); | |
1da177e4 | 591 | if (error) { |
1da177e4 LT |
592 | args->trans = NULL; |
593 | xfs_bmap_cancel(args->flist); | |
d99831ff | 594 | return error; |
1da177e4 LT |
595 | } |
596 | ||
1da177e4 LT |
597 | /* |
598 | * Commit the current trans (including the inode) and start | |
599 | * a new one. | |
600 | */ | |
322ff6b8 NS |
601 | error = xfs_trans_roll(&args->trans, dp); |
602 | if (error) | |
d99831ff | 603 | return error; |
1da177e4 LT |
604 | |
605 | /* | |
606 | * Fob the whole rest of the problem off on the Btree code. | |
607 | */ | |
608 | error = xfs_attr_node_addname(args); | |
d99831ff | 609 | return error; |
1da177e4 LT |
610 | } |
611 | ||
612 | /* | |
613 | * Commit the transaction that added the attr name so that | |
614 | * later routines can manage their own transactions. | |
615 | */ | |
322ff6b8 NS |
616 | error = xfs_trans_roll(&args->trans, dp); |
617 | if (error) | |
d99831ff | 618 | return error; |
1da177e4 LT |
619 | |
620 | /* | |
621 | * If there was an out-of-line value, allocate the blocks we | |
622 | * identified for its storage and copy the value. This is done | |
623 | * after we create the attribute so that we don't overflow the | |
624 | * maximum size of a transaction and/or hit a deadlock. | |
625 | */ | |
626 | if (args->rmtblkno > 0) { | |
627 | error = xfs_attr_rmtval_set(args); | |
628 | if (error) | |
d99831ff | 629 | return error; |
1da177e4 LT |
630 | } |
631 | ||
632 | /* | |
633 | * If this is an atomic rename operation, we must "flip" the | |
634 | * incomplete flags on the "new" and "old" attribute/value pairs | |
635 | * so that one disappears and one appears atomically. Then we | |
636 | * must remove the "old" attribute/value pair. | |
637 | */ | |
6a178100 | 638 | if (args->op_flags & XFS_DA_OP_RENAME) { |
1da177e4 LT |
639 | /* |
640 | * In a separate transaction, set the incomplete flag on the | |
641 | * "old" attr and clear the incomplete flag on the "new" attr. | |
642 | */ | |
517c2220 | 643 | error = xfs_attr3_leaf_flipflags(args); |
1da177e4 | 644 | if (error) |
d99831ff | 645 | return error; |
1da177e4 LT |
646 | |
647 | /* | |
648 | * Dismantle the "old" attribute/value pair by removing | |
649 | * a "remote" value (if it exists). | |
650 | */ | |
651 | args->index = args->index2; | |
652 | args->blkno = args->blkno2; | |
653 | args->rmtblkno = args->rmtblkno2; | |
654 | args->rmtblkcnt = args->rmtblkcnt2; | |
8275cdd0 | 655 | args->rmtvaluelen = args->rmtvaluelen2; |
1da177e4 LT |
656 | if (args->rmtblkno) { |
657 | error = xfs_attr_rmtval_remove(args); | |
658 | if (error) | |
d99831ff | 659 | return error; |
1da177e4 LT |
660 | } |
661 | ||
662 | /* | |
663 | * Read in the block containing the "old" attr, then | |
664 | * remove the "old" attr from that block (neat, huh!) | |
665 | */ | |
517c2220 | 666 | error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, |
ad14c33a | 667 | -1, &bp); |
1da177e4 | 668 | if (error) |
ad14c33a DC |
669 | return error; |
670 | ||
517c2220 | 671 | xfs_attr3_leaf_remove(bp, args); |
1da177e4 LT |
672 | |
673 | /* | |
674 | * If the result is small enough, shrink it all into the inode. | |
675 | */ | |
d8cc890d | 676 | if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) { |
9d87c319 | 677 | xfs_bmap_init(args->flist, args->firstblock); |
517c2220 | 678 | error = xfs_attr3_leaf_to_shortform(bp, args, forkoff); |
1da177e4 | 679 | /* bp is gone due to xfs_da_shrink_inode */ |
f6106efa | 680 | if (!error) |
1da177e4 | 681 | error = xfs_bmap_finish(&args->trans, |
f6106efa | 682 | args->flist, dp); |
1da177e4 | 683 | if (error) { |
1da177e4 LT |
684 | args->trans = NULL; |
685 | xfs_bmap_cancel(args->flist); | |
d99831ff | 686 | return error; |
1da177e4 | 687 | } |
1d9025e5 | 688 | } |
1da177e4 LT |
689 | |
690 | /* | |
691 | * Commit the remove and start the next trans in series. | |
692 | */ | |
322ff6b8 | 693 | error = xfs_trans_roll(&args->trans, dp); |
1da177e4 LT |
694 | |
695 | } else if (args->rmtblkno > 0) { | |
696 | /* | |
697 | * Added a "remote" value, just clear the incomplete flag. | |
698 | */ | |
517c2220 | 699 | error = xfs_attr3_leaf_clearflag(args); |
1da177e4 | 700 | } |
517c2220 | 701 | return error; |
1da177e4 LT |
702 | } |
703 | ||
704 | /* | |
705 | * Remove a name from the leaf attribute list structure | |
706 | * | |
707 | * This leaf block cannot have a "remote" value, we only call this routine | |
708 | * if bmap_one_block() says there is only one block (ie: no remote blks). | |
709 | */ | |
710 | STATIC int | |
711 | xfs_attr_leaf_removename(xfs_da_args_t *args) | |
712 | { | |
713 | xfs_inode_t *dp; | |
1d9025e5 | 714 | struct xfs_buf *bp; |
f6106efa | 715 | int error, forkoff; |
1da177e4 | 716 | |
5a5881cd DC |
717 | trace_xfs_attr_leaf_removename(args); |
718 | ||
1da177e4 LT |
719 | /* |
720 | * Remove the attribute. | |
721 | */ | |
722 | dp = args->dp; | |
723 | args->blkno = 0; | |
517c2220 | 724 | error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp); |
ad14c33a DC |
725 | if (error) |
726 | return error; | |
1da177e4 | 727 | |
517c2220 | 728 | error = xfs_attr3_leaf_lookup_int(bp, args); |
2451337d | 729 | if (error == -ENOATTR) { |
1d9025e5 | 730 | xfs_trans_brelse(args->trans, bp); |
517c2220 | 731 | return error; |
1da177e4 LT |
732 | } |
733 | ||
517c2220 | 734 | xfs_attr3_leaf_remove(bp, args); |
1da177e4 LT |
735 | |
736 | /* | |
737 | * If the result is small enough, shrink it all into the inode. | |
738 | */ | |
d8cc890d | 739 | if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) { |
9d87c319 | 740 | xfs_bmap_init(args->flist, args->firstblock); |
517c2220 | 741 | error = xfs_attr3_leaf_to_shortform(bp, args, forkoff); |
1da177e4 | 742 | /* bp is gone due to xfs_da_shrink_inode */ |
f6106efa ES |
743 | if (!error) |
744 | error = xfs_bmap_finish(&args->trans, args->flist, dp); | |
1da177e4 | 745 | if (error) { |
1da177e4 LT |
746 | args->trans = NULL; |
747 | xfs_bmap_cancel(args->flist); | |
517c2220 | 748 | return error; |
1da177e4 | 749 | } |
1d9025e5 | 750 | } |
517c2220 | 751 | return 0; |
1da177e4 LT |
752 | } |
753 | ||
754 | /* | |
755 | * Look up a name in a leaf attribute list structure. | |
756 | * | |
757 | * This leaf block cannot have a "remote" value, we only call this routine | |
758 | * if bmap_one_block() says there is only one block (ie: no remote blks). | |
759 | */ | |
ba0f32d4 | 760 | STATIC int |
1da177e4 LT |
761 | xfs_attr_leaf_get(xfs_da_args_t *args) |
762 | { | |
1d9025e5 | 763 | struct xfs_buf *bp; |
1da177e4 LT |
764 | int error; |
765 | ||
ee73259b DC |
766 | trace_xfs_attr_leaf_get(args); |
767 | ||
1da177e4 | 768 | args->blkno = 0; |
517c2220 | 769 | error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp); |
1da177e4 | 770 | if (error) |
ad14c33a | 771 | return error; |
1da177e4 | 772 | |
517c2220 | 773 | error = xfs_attr3_leaf_lookup_int(bp, args); |
2451337d | 774 | if (error != -EEXIST) { |
1d9025e5 | 775 | xfs_trans_brelse(args->trans, bp); |
517c2220 | 776 | return error; |
1da177e4 | 777 | } |
517c2220 | 778 | error = xfs_attr3_leaf_getvalue(bp, args); |
1d9025e5 | 779 | xfs_trans_brelse(args->trans, bp); |
1da177e4 LT |
780 | if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) { |
781 | error = xfs_attr_rmtval_get(args); | |
782 | } | |
517c2220 | 783 | return error; |
1da177e4 LT |
784 | } |
785 | ||
1da177e4 | 786 | /*======================================================================== |
c2c4c477 | 787 | * External routines when attribute list size > geo->blksize |
1da177e4 LT |
788 | *========================================================================*/ |
789 | ||
790 | /* | |
791 | * Add a name to a Btree-format attribute list. | |
792 | * | |
793 | * This will involve walking down the Btree, and may involve splitting | |
794 | * leaf nodes and even splitting intermediate nodes up to and including | |
795 | * the root node (a special case of an intermediate node). | |
796 | * | |
797 | * "Remote" attribute values confuse the issue and atomic rename operations | |
798 | * add a whole extra layer of confusion on top of that. | |
799 | */ | |
800 | STATIC int | |
801 | xfs_attr_node_addname(xfs_da_args_t *args) | |
802 | { | |
803 | xfs_da_state_t *state; | |
804 | xfs_da_state_blk_t *blk; | |
805 | xfs_inode_t *dp; | |
806 | xfs_mount_t *mp; | |
f6106efa | 807 | int retval, error; |
1da177e4 | 808 | |
5a5881cd DC |
809 | trace_xfs_attr_node_addname(args); |
810 | ||
1da177e4 LT |
811 | /* |
812 | * Fill in bucket of arguments/results/context to carry around. | |
813 | */ | |
814 | dp = args->dp; | |
815 | mp = dp->i_mount; | |
816 | restart: | |
817 | state = xfs_da_state_alloc(); | |
818 | state->args = args; | |
819 | state->mp = mp; | |
1da177e4 LT |
820 | |
821 | /* | |
822 | * Search to see if name already exists, and get back a pointer | |
823 | * to where it should go. | |
824 | */ | |
f5ea1100 | 825 | error = xfs_da3_node_lookup_int(state, &retval); |
1da177e4 LT |
826 | if (error) |
827 | goto out; | |
828 | blk = &state->path.blk[ state->path.active-1 ]; | |
829 | ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC); | |
2451337d | 830 | if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) { |
1da177e4 | 831 | goto out; |
2451337d | 832 | } else if (retval == -EEXIST) { |
1da177e4 LT |
833 | if (args->flags & ATTR_CREATE) |
834 | goto out; | |
5a5881cd DC |
835 | |
836 | trace_xfs_attr_node_replace(args); | |
837 | ||
8275cdd0 | 838 | /* save the attribute state for later removal*/ |
6a178100 | 839 | args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */ |
1da177e4 LT |
840 | args->blkno2 = args->blkno; /* set 2nd entry info*/ |
841 | args->index2 = args->index; | |
842 | args->rmtblkno2 = args->rmtblkno; | |
843 | args->rmtblkcnt2 = args->rmtblkcnt; | |
8275cdd0 DC |
844 | args->rmtvaluelen2 = args->rmtvaluelen; |
845 | ||
846 | /* | |
847 | * clear the remote attr state now that it is saved so that the | |
848 | * values reflect the state of the attribute we are about to | |
849 | * add, not the attribute we just found and will remove later. | |
850 | */ | |
1da177e4 LT |
851 | args->rmtblkno = 0; |
852 | args->rmtblkcnt = 0; | |
8275cdd0 | 853 | args->rmtvaluelen = 0; |
1da177e4 LT |
854 | } |
855 | ||
517c2220 | 856 | retval = xfs_attr3_leaf_add(blk->bp, state->args); |
2451337d | 857 | if (retval == -ENOSPC) { |
1da177e4 LT |
858 | if (state->path.active == 1) { |
859 | /* | |
860 | * Its really a single leaf node, but it had | |
861 | * out-of-line values so it looked like it *might* | |
862 | * have been a b-tree. | |
863 | */ | |
864 | xfs_da_state_free(state); | |
6dd93e9e | 865 | state = NULL; |
9d87c319 | 866 | xfs_bmap_init(args->flist, args->firstblock); |
517c2220 | 867 | error = xfs_attr3_leaf_to_node(args); |
f6106efa | 868 | if (!error) |
1da177e4 | 869 | error = xfs_bmap_finish(&args->trans, |
f6106efa | 870 | args->flist, dp); |
1da177e4 | 871 | if (error) { |
1da177e4 LT |
872 | args->trans = NULL; |
873 | xfs_bmap_cancel(args->flist); | |
874 | goto out; | |
875 | } | |
876 | ||
1da177e4 LT |
877 | /* |
878 | * Commit the node conversion and start the next | |
879 | * trans in the chain. | |
880 | */ | |
322ff6b8 NS |
881 | error = xfs_trans_roll(&args->trans, dp); |
882 | if (error) | |
1da177e4 LT |
883 | goto out; |
884 | ||
885 | goto restart; | |
886 | } | |
887 | ||
888 | /* | |
889 | * Split as many Btree elements as required. | |
890 | * This code tracks the new and old attr's location | |
891 | * in the index/blkno/rmtblkno/rmtblkcnt fields and | |
892 | * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields. | |
893 | */ | |
9d87c319 | 894 | xfs_bmap_init(args->flist, args->firstblock); |
f5ea1100 | 895 | error = xfs_da3_split(state); |
f6106efa ES |
896 | if (!error) |
897 | error = xfs_bmap_finish(&args->trans, args->flist, dp); | |
1da177e4 | 898 | if (error) { |
1da177e4 LT |
899 | args->trans = NULL; |
900 | xfs_bmap_cancel(args->flist); | |
901 | goto out; | |
902 | } | |
1da177e4 LT |
903 | } else { |
904 | /* | |
905 | * Addition succeeded, update Btree hashvals. | |
906 | */ | |
f5ea1100 | 907 | xfs_da3_fixhashpath(state, &state->path); |
1da177e4 LT |
908 | } |
909 | ||
910 | /* | |
911 | * Kill the state structure, we're done with it and need to | |
912 | * allow the buffers to come back later. | |
913 | */ | |
914 | xfs_da_state_free(state); | |
915 | state = NULL; | |
916 | ||
917 | /* | |
918 | * Commit the leaf addition or btree split and start the next | |
919 | * trans in the chain. | |
920 | */ | |
322ff6b8 NS |
921 | error = xfs_trans_roll(&args->trans, dp); |
922 | if (error) | |
1da177e4 LT |
923 | goto out; |
924 | ||
925 | /* | |
926 | * If there was an out-of-line value, allocate the blocks we | |
927 | * identified for its storage and copy the value. This is done | |
928 | * after we create the attribute so that we don't overflow the | |
929 | * maximum size of a transaction and/or hit a deadlock. | |
930 | */ | |
931 | if (args->rmtblkno > 0) { | |
932 | error = xfs_attr_rmtval_set(args); | |
933 | if (error) | |
d99831ff | 934 | return error; |
1da177e4 LT |
935 | } |
936 | ||
937 | /* | |
938 | * If this is an atomic rename operation, we must "flip" the | |
939 | * incomplete flags on the "new" and "old" attribute/value pairs | |
940 | * so that one disappears and one appears atomically. Then we | |
941 | * must remove the "old" attribute/value pair. | |
942 | */ | |
6a178100 | 943 | if (args->op_flags & XFS_DA_OP_RENAME) { |
1da177e4 LT |
944 | /* |
945 | * In a separate transaction, set the incomplete flag on the | |
946 | * "old" attr and clear the incomplete flag on the "new" attr. | |
947 | */ | |
517c2220 | 948 | error = xfs_attr3_leaf_flipflags(args); |
1da177e4 LT |
949 | if (error) |
950 | goto out; | |
951 | ||
952 | /* | |
953 | * Dismantle the "old" attribute/value pair by removing | |
954 | * a "remote" value (if it exists). | |
955 | */ | |
956 | args->index = args->index2; | |
957 | args->blkno = args->blkno2; | |
958 | args->rmtblkno = args->rmtblkno2; | |
959 | args->rmtblkcnt = args->rmtblkcnt2; | |
8275cdd0 | 960 | args->rmtvaluelen = args->rmtvaluelen2; |
1da177e4 LT |
961 | if (args->rmtblkno) { |
962 | error = xfs_attr_rmtval_remove(args); | |
963 | if (error) | |
d99831ff | 964 | return error; |
1da177e4 LT |
965 | } |
966 | ||
967 | /* | |
968 | * Re-find the "old" attribute entry after any split ops. | |
969 | * The INCOMPLETE flag means that we will find the "old" | |
970 | * attr, not the "new" one. | |
971 | */ | |
972 | args->flags |= XFS_ATTR_INCOMPLETE; | |
973 | state = xfs_da_state_alloc(); | |
974 | state->args = args; | |
975 | state->mp = mp; | |
1da177e4 | 976 | state->inleaf = 0; |
f5ea1100 | 977 | error = xfs_da3_node_lookup_int(state, &retval); |
1da177e4 LT |
978 | if (error) |
979 | goto out; | |
980 | ||
981 | /* | |
982 | * Remove the name and update the hashvals in the tree. | |
983 | */ | |
984 | blk = &state->path.blk[ state->path.active-1 ]; | |
985 | ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC); | |
517c2220 | 986 | error = xfs_attr3_leaf_remove(blk->bp, args); |
f5ea1100 | 987 | xfs_da3_fixhashpath(state, &state->path); |
1da177e4 LT |
988 | |
989 | /* | |
990 | * Check to see if the tree needs to be collapsed. | |
991 | */ | |
992 | if (retval && (state->path.active > 1)) { | |
9d87c319 | 993 | xfs_bmap_init(args->flist, args->firstblock); |
f5ea1100 | 994 | error = xfs_da3_join(state); |
f6106efa | 995 | if (!error) |
1da177e4 | 996 | error = xfs_bmap_finish(&args->trans, |
f6106efa | 997 | args->flist, dp); |
1da177e4 | 998 | if (error) { |
1da177e4 LT |
999 | args->trans = NULL; |
1000 | xfs_bmap_cancel(args->flist); | |
1001 | goto out; | |
1002 | } | |
1da177e4 LT |
1003 | } |
1004 | ||
1005 | /* | |
1006 | * Commit and start the next trans in the chain. | |
1007 | */ | |
322ff6b8 NS |
1008 | error = xfs_trans_roll(&args->trans, dp); |
1009 | if (error) | |
1da177e4 LT |
1010 | goto out; |
1011 | ||
1012 | } else if (args->rmtblkno > 0) { | |
1013 | /* | |
1014 | * Added a "remote" value, just clear the incomplete flag. | |
1015 | */ | |
517c2220 | 1016 | error = xfs_attr3_leaf_clearflag(args); |
1da177e4 LT |
1017 | if (error) |
1018 | goto out; | |
1019 | } | |
1020 | retval = error = 0; | |
1021 | ||
1022 | out: | |
1023 | if (state) | |
1024 | xfs_da_state_free(state); | |
1025 | if (error) | |
d99831ff ES |
1026 | return error; |
1027 | return retval; | |
1da177e4 LT |
1028 | } |
1029 | ||
1030 | /* | |
1031 | * Remove a name from a B-tree attribute list. | |
1032 | * | |
1033 | * This will involve walking down the Btree, and may involve joining | |
1034 | * leaf nodes and even joining intermediate nodes up to and including | |
1035 | * the root node (a special case of an intermediate node). | |
1036 | */ | |
1037 | STATIC int | |
1038 | xfs_attr_node_removename(xfs_da_args_t *args) | |
1039 | { | |
1040 | xfs_da_state_t *state; | |
1041 | xfs_da_state_blk_t *blk; | |
1042 | xfs_inode_t *dp; | |
1d9025e5 | 1043 | struct xfs_buf *bp; |
f6106efa | 1044 | int retval, error, forkoff; |
1da177e4 | 1045 | |
5a5881cd DC |
1046 | trace_xfs_attr_node_removename(args); |
1047 | ||
1da177e4 LT |
1048 | /* |
1049 | * Tie a string around our finger to remind us where we are. | |
1050 | */ | |
1051 | dp = args->dp; | |
1052 | state = xfs_da_state_alloc(); | |
1053 | state->args = args; | |
1054 | state->mp = dp->i_mount; | |
1da177e4 LT |
1055 | |
1056 | /* | |
1057 | * Search to see if name exists, and get back a pointer to it. | |
1058 | */ | |
f5ea1100 | 1059 | error = xfs_da3_node_lookup_int(state, &retval); |
2451337d | 1060 | if (error || (retval != -EEXIST)) { |
1da177e4 LT |
1061 | if (error == 0) |
1062 | error = retval; | |
1063 | goto out; | |
1064 | } | |
1065 | ||
1066 | /* | |
1067 | * If there is an out-of-line value, de-allocate the blocks. | |
1068 | * This is done before we remove the attribute so that we don't | |
1069 | * overflow the maximum size of a transaction and/or hit a deadlock. | |
1070 | */ | |
1071 | blk = &state->path.blk[ state->path.active-1 ]; | |
1072 | ASSERT(blk->bp != NULL); | |
1073 | ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC); | |
1074 | if (args->rmtblkno > 0) { | |
1075 | /* | |
1076 | * Fill in disk block numbers in the state structure | |
1077 | * so that we can get the buffers back after we commit | |
1078 | * several transactions in the following calls. | |
1079 | */ | |
1080 | error = xfs_attr_fillstate(state); | |
1081 | if (error) | |
1082 | goto out; | |
1083 | ||
1084 | /* | |
1085 | * Mark the attribute as INCOMPLETE, then bunmapi() the | |
1086 | * remote value. | |
1087 | */ | |
517c2220 | 1088 | error = xfs_attr3_leaf_setflag(args); |
1da177e4 LT |
1089 | if (error) |
1090 | goto out; | |
1091 | error = xfs_attr_rmtval_remove(args); | |
1092 | if (error) | |
1093 | goto out; | |
1094 | ||
1095 | /* | |
1096 | * Refill the state structure with buffers, the prior calls | |
1097 | * released our buffers. | |
1098 | */ | |
1099 | error = xfs_attr_refillstate(state); | |
1100 | if (error) | |
1101 | goto out; | |
1102 | } | |
1103 | ||
1104 | /* | |
1105 | * Remove the name and update the hashvals in the tree. | |
1106 | */ | |
1107 | blk = &state->path.blk[ state->path.active-1 ]; | |
1108 | ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC); | |
517c2220 | 1109 | retval = xfs_attr3_leaf_remove(blk->bp, args); |
f5ea1100 | 1110 | xfs_da3_fixhashpath(state, &state->path); |
1da177e4 LT |
1111 | |
1112 | /* | |
1113 | * Check to see if the tree needs to be collapsed. | |
1114 | */ | |
1115 | if (retval && (state->path.active > 1)) { | |
9d87c319 | 1116 | xfs_bmap_init(args->flist, args->firstblock); |
f5ea1100 | 1117 | error = xfs_da3_join(state); |
f6106efa ES |
1118 | if (!error) |
1119 | error = xfs_bmap_finish(&args->trans, args->flist, dp); | |
1da177e4 | 1120 | if (error) { |
1da177e4 LT |
1121 | args->trans = NULL; |
1122 | xfs_bmap_cancel(args->flist); | |
1123 | goto out; | |
1124 | } | |
1da177e4 LT |
1125 | /* |
1126 | * Commit the Btree join operation and start a new trans. | |
1127 | */ | |
322ff6b8 NS |
1128 | error = xfs_trans_roll(&args->trans, dp); |
1129 | if (error) | |
1da177e4 LT |
1130 | goto out; |
1131 | } | |
1132 | ||
1133 | /* | |
1134 | * If the result is small enough, push it all into the inode. | |
1135 | */ | |
1136 | if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) { | |
1137 | /* | |
1138 | * Have to get rid of the copy of this dabuf in the state. | |
1139 | */ | |
1140 | ASSERT(state->path.active == 1); | |
1141 | ASSERT(state->path.blk[0].bp); | |
1da177e4 LT |
1142 | state->path.blk[0].bp = NULL; |
1143 | ||
517c2220 | 1144 | error = xfs_attr3_leaf_read(args->trans, args->dp, 0, -1, &bp); |
1da177e4 LT |
1145 | if (error) |
1146 | goto out; | |
1da177e4 | 1147 | |
d8cc890d | 1148 | if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) { |
9d87c319 | 1149 | xfs_bmap_init(args->flist, args->firstblock); |
517c2220 | 1150 | error = xfs_attr3_leaf_to_shortform(bp, args, forkoff); |
1da177e4 | 1151 | /* bp is gone due to xfs_da_shrink_inode */ |
f6106efa | 1152 | if (!error) |
1da177e4 | 1153 | error = xfs_bmap_finish(&args->trans, |
f6106efa | 1154 | args->flist, dp); |
1da177e4 | 1155 | if (error) { |
1da177e4 LT |
1156 | args->trans = NULL; |
1157 | xfs_bmap_cancel(args->flist); | |
1158 | goto out; | |
1159 | } | |
1da177e4 | 1160 | } else |
1d9025e5 | 1161 | xfs_trans_brelse(args->trans, bp); |
1da177e4 LT |
1162 | } |
1163 | error = 0; | |
1164 | ||
1165 | out: | |
1166 | xfs_da_state_free(state); | |
d99831ff | 1167 | return error; |
1da177e4 LT |
1168 | } |
1169 | ||
1170 | /* | |
1171 | * Fill in the disk block numbers in the state structure for the buffers | |
1172 | * that are attached to the state structure. | |
1173 | * This is done so that we can quickly reattach ourselves to those buffers | |
c41564b5 | 1174 | * after some set of transaction commits have released these buffers. |
1da177e4 LT |
1175 | */ |
1176 | STATIC int | |
1177 | xfs_attr_fillstate(xfs_da_state_t *state) | |
1178 | { | |
1179 | xfs_da_state_path_t *path; | |
1180 | xfs_da_state_blk_t *blk; | |
1181 | int level; | |
1182 | ||
ee73259b DC |
1183 | trace_xfs_attr_fillstate(state->args); |
1184 | ||
1da177e4 LT |
1185 | /* |
1186 | * Roll down the "path" in the state structure, storing the on-disk | |
1187 | * block number for those buffers in the "path". | |
1188 | */ | |
1189 | path = &state->path; | |
1190 | ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH)); | |
1191 | for (blk = path->blk, level = 0; level < path->active; blk++, level++) { | |
1192 | if (blk->bp) { | |
1d9025e5 | 1193 | blk->disk_blkno = XFS_BUF_ADDR(blk->bp); |
1da177e4 LT |
1194 | blk->bp = NULL; |
1195 | } else { | |
1196 | blk->disk_blkno = 0; | |
1197 | } | |
1198 | } | |
1199 | ||
1200 | /* | |
1201 | * Roll down the "altpath" in the state structure, storing the on-disk | |
1202 | * block number for those buffers in the "altpath". | |
1203 | */ | |
1204 | path = &state->altpath; | |
1205 | ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH)); | |
1206 | for (blk = path->blk, level = 0; level < path->active; blk++, level++) { | |
1207 | if (blk->bp) { | |
1d9025e5 | 1208 | blk->disk_blkno = XFS_BUF_ADDR(blk->bp); |
1da177e4 LT |
1209 | blk->bp = NULL; |
1210 | } else { | |
1211 | blk->disk_blkno = 0; | |
1212 | } | |
1213 | } | |
1214 | ||
d99831ff | 1215 | return 0; |
1da177e4 LT |
1216 | } |
1217 | ||
1218 | /* | |
1219 | * Reattach the buffers to the state structure based on the disk block | |
1220 | * numbers stored in the state structure. | |
c41564b5 | 1221 | * This is done after some set of transaction commits have released those |
1da177e4 LT |
1222 | * buffers from our grip. |
1223 | */ | |
1224 | STATIC int | |
1225 | xfs_attr_refillstate(xfs_da_state_t *state) | |
1226 | { | |
1227 | xfs_da_state_path_t *path; | |
1228 | xfs_da_state_blk_t *blk; | |
1229 | int level, error; | |
1230 | ||
ee73259b DC |
1231 | trace_xfs_attr_refillstate(state->args); |
1232 | ||
1da177e4 LT |
1233 | /* |
1234 | * Roll down the "path" in the state structure, storing the on-disk | |
1235 | * block number for those buffers in the "path". | |
1236 | */ | |
1237 | path = &state->path; | |
1238 | ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH)); | |
1239 | for (blk = path->blk, level = 0; level < path->active; blk++, level++) { | |
1240 | if (blk->disk_blkno) { | |
f5ea1100 | 1241 | error = xfs_da3_node_read(state->args->trans, |
1da177e4 LT |
1242 | state->args->dp, |
1243 | blk->blkno, blk->disk_blkno, | |
d9392a4b | 1244 | &blk->bp, XFS_ATTR_FORK); |
1da177e4 | 1245 | if (error) |
d99831ff | 1246 | return error; |
1da177e4 LT |
1247 | } else { |
1248 | blk->bp = NULL; | |
1249 | } | |
1250 | } | |
1251 | ||
1252 | /* | |
1253 | * Roll down the "altpath" in the state structure, storing the on-disk | |
1254 | * block number for those buffers in the "altpath". | |
1255 | */ | |
1256 | path = &state->altpath; | |
1257 | ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH)); | |
1258 | for (blk = path->blk, level = 0; level < path->active; blk++, level++) { | |
1259 | if (blk->disk_blkno) { | |
f5ea1100 | 1260 | error = xfs_da3_node_read(state->args->trans, |
1da177e4 LT |
1261 | state->args->dp, |
1262 | blk->blkno, blk->disk_blkno, | |
d9392a4b | 1263 | &blk->bp, XFS_ATTR_FORK); |
1da177e4 | 1264 | if (error) |
d99831ff | 1265 | return error; |
1da177e4 LT |
1266 | } else { |
1267 | blk->bp = NULL; | |
1268 | } | |
1269 | } | |
1270 | ||
d99831ff | 1271 | return 0; |
1da177e4 LT |
1272 | } |
1273 | ||
1274 | /* | |
1275 | * Look up a filename in a node attribute list. | |
1276 | * | |
1277 | * This routine gets called for any attribute fork that has more than one | |
1278 | * block, ie: both true Btree attr lists and for single-leaf-blocks with | |
1279 | * "remote" values taking up more blocks. | |
1280 | */ | |
ba0f32d4 | 1281 | STATIC int |
1da177e4 LT |
1282 | xfs_attr_node_get(xfs_da_args_t *args) |
1283 | { | |
1284 | xfs_da_state_t *state; | |
1285 | xfs_da_state_blk_t *blk; | |
1286 | int error, retval; | |
1287 | int i; | |
1288 | ||
ee73259b DC |
1289 | trace_xfs_attr_node_get(args); |
1290 | ||
1da177e4 LT |
1291 | state = xfs_da_state_alloc(); |
1292 | state->args = args; | |
1293 | state->mp = args->dp->i_mount; | |
1da177e4 LT |
1294 | |
1295 | /* | |
1296 | * Search to see if name exists, and get back a pointer to it. | |
1297 | */ | |
f5ea1100 | 1298 | error = xfs_da3_node_lookup_int(state, &retval); |
1da177e4 LT |
1299 | if (error) { |
1300 | retval = error; | |
2451337d | 1301 | } else if (retval == -EEXIST) { |
1da177e4 LT |
1302 | blk = &state->path.blk[ state->path.active-1 ]; |
1303 | ASSERT(blk->bp != NULL); | |
1304 | ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC); | |
1305 | ||
1306 | /* | |
1307 | * Get the value, local or "remote" | |
1308 | */ | |
517c2220 | 1309 | retval = xfs_attr3_leaf_getvalue(blk->bp, args); |
1da177e4 LT |
1310 | if (!retval && (args->rmtblkno > 0) |
1311 | && !(args->flags & ATTR_KERNOVAL)) { | |
1312 | retval = xfs_attr_rmtval_get(args); | |
1313 | } | |
1314 | } | |
1315 | ||
1316 | /* | |
1317 | * If not in a transaction, we have to release all the buffers. | |
1318 | */ | |
1319 | for (i = 0; i < state->path.active; i++) { | |
1d9025e5 | 1320 | xfs_trans_brelse(args->trans, state->path.blk[i].bp); |
1da177e4 LT |
1321 | state->path.blk[i].bp = NULL; |
1322 | } | |
1323 | ||
1324 | xfs_da_state_free(state); | |
d99831ff | 1325 | return retval; |
1da177e4 | 1326 | } |