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