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