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