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