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