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