xref: /linux/fs/xfs/libxfs/xfs_attr.c (revision ec8c17e5ecb4a5a74069687ccb6d2cfe1851302e)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4  * All Rights Reserved.
5  */
6 #include "xfs.h"
7 #include "xfs_fs.h"
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
12 #include "xfs_mount.h"
13 #include "xfs_defer.h"
14 #include "xfs_da_format.h"
15 #include "xfs_da_btree.h"
16 #include "xfs_attr_sf.h"
17 #include "xfs_inode.h"
18 #include "xfs_trans.h"
19 #include "xfs_bmap.h"
20 #include "xfs_bmap_btree.h"
21 #include "xfs_attr.h"
22 #include "xfs_attr_leaf.h"
23 #include "xfs_attr_remote.h"
24 #include "xfs_quota.h"
25 #include "xfs_trans_space.h"
26 #include "xfs_trace.h"
27 #include "xfs_attr_item.h"
28 #include "xfs_xattr.h"
29 #include "xfs_parent.h"
30 
31 struct kmem_cache		*xfs_attr_intent_cache;
32 
33 /*
34  * xfs_attr.c
35  *
36  * Provide the external interfaces to manage attribute lists.
37  */
38 
39 /*========================================================================
40  * Function prototypes for the kernel.
41  *========================================================================*/
42 
43 /*
44  * Internal routines when attribute list fits inside the inode.
45  */
46 STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
47 
48 /*
49  * Internal routines when attribute list is one block.
50  */
51 STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
52 STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
53 STATIC int xfs_attr_leaf_hasname(struct xfs_da_args *args, struct xfs_buf **bp);
54 
55 /*
56  * Internal routines when attribute list is more than one block.
57  */
58 STATIC int xfs_attr_node_get(xfs_da_args_t *args);
59 STATIC void xfs_attr_restore_rmt_blk(struct xfs_da_args *args);
60 static int xfs_attr_node_try_addname(struct xfs_attr_intent *attr);
61 STATIC int xfs_attr_node_addname_find_attr(struct xfs_attr_intent *attr);
62 STATIC int xfs_attr_node_remove_attr(struct xfs_attr_intent *attr);
63 STATIC int xfs_attr_node_lookup(struct xfs_da_args *args,
64 		struct xfs_da_state *state);
65 
66 int
xfs_inode_hasattr(struct xfs_inode * ip)67 xfs_inode_hasattr(
68 	struct xfs_inode	*ip)
69 {
70 	if (!xfs_inode_has_attr_fork(ip))
71 		return 0;
72 	if (ip->i_af.if_format == XFS_DINODE_FMT_EXTENTS &&
73 	    ip->i_af.if_nextents == 0)
74 		return 0;
75 	return 1;
76 }
77 
78 /*
79  * Returns true if the there is exactly only block in the attr fork, in which
80  * case the attribute fork consists of a single leaf block entry.
81  */
82 bool
xfs_attr_is_leaf(struct xfs_inode * ip)83 xfs_attr_is_leaf(
84 	struct xfs_inode	*ip)
85 {
86 	struct xfs_ifork	*ifp = &ip->i_af;
87 	struct xfs_iext_cursor	icur;
88 	struct xfs_bmbt_irec	imap;
89 
90 	ASSERT(!xfs_need_iread_extents(ifp));
91 
92 	if (ifp->if_nextents != 1 || ifp->if_format != XFS_DINODE_FMT_EXTENTS)
93 		return false;
94 
95 	xfs_iext_first(ifp, &icur);
96 	xfs_iext_get_extent(ifp, &icur, &imap);
97 	return imap.br_startoff == 0 && imap.br_blockcount == 1;
98 }
99 
100 /*
101  * XXX (dchinner): name path state saving and refilling is an optimisation to
102  * avoid needing to look up name entries after rolling transactions removing
103  * remote xattr blocks between the name entry lookup and name entry removal.
104  * This optimisation got sidelined when combining the set and remove state
105  * machines, but the code has been left in place because it is worthwhile to
106  * restore the optimisation once the combined state machine paths have settled.
107  *
108  * This comment is a public service announcement to remind Future Dave that he
109  * still needs to restore this code to working order.
110  */
111 #if 0
112 /*
113  * Fill in the disk block numbers in the state structure for the buffers
114  * that are attached to the state structure.
115  * This is done so that we can quickly reattach ourselves to those buffers
116  * after some set of transaction commits have released these buffers.
117  */
118 static int
119 xfs_attr_fillstate(xfs_da_state_t *state)
120 {
121 	xfs_da_state_path_t *path;
122 	xfs_da_state_blk_t *blk;
123 	int level;
124 
125 	trace_xfs_attr_fillstate(state->args);
126 
127 	/*
128 	 * Roll down the "path" in the state structure, storing the on-disk
129 	 * block number for those buffers in the "path".
130 	 */
131 	path = &state->path;
132 	ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
133 	for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
134 		if (blk->bp) {
135 			blk->disk_blkno = xfs_buf_daddr(blk->bp);
136 			blk->bp = NULL;
137 		} else {
138 			blk->disk_blkno = 0;
139 		}
140 	}
141 
142 	/*
143 	 * Roll down the "altpath" in the state structure, storing the on-disk
144 	 * block number for those buffers in the "altpath".
145 	 */
146 	path = &state->altpath;
147 	ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
148 	for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
149 		if (blk->bp) {
150 			blk->disk_blkno = xfs_buf_daddr(blk->bp);
151 			blk->bp = NULL;
152 		} else {
153 			blk->disk_blkno = 0;
154 		}
155 	}
156 
157 	return 0;
158 }
159 
160 /*
161  * Reattach the buffers to the state structure based on the disk block
162  * numbers stored in the state structure.
163  * This is done after some set of transaction commits have released those
164  * buffers from our grip.
165  */
166 static int
167 xfs_attr_refillstate(xfs_da_state_t *state)
168 {
169 	xfs_da_state_path_t *path;
170 	xfs_da_state_blk_t *blk;
171 	int level, error;
172 
173 	trace_xfs_attr_refillstate(state->args);
174 
175 	/*
176 	 * Roll down the "path" in the state structure, storing the on-disk
177 	 * block number for those buffers in the "path".
178 	 */
179 	path = &state->path;
180 	ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
181 	for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
182 		if (blk->disk_blkno) {
183 			error = xfs_da3_node_read_mapped(state->args->trans,
184 					state->args->dp, blk->disk_blkno,
185 					&blk->bp, XFS_ATTR_FORK);
186 			if (error)
187 				return error;
188 		} else {
189 			blk->bp = NULL;
190 		}
191 	}
192 
193 	/*
194 	 * Roll down the "altpath" in the state structure, storing the on-disk
195 	 * block number for those buffers in the "altpath".
196 	 */
197 	path = &state->altpath;
198 	ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
199 	for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
200 		if (blk->disk_blkno) {
201 			error = xfs_da3_node_read_mapped(state->args->trans,
202 					state->args->dp, blk->disk_blkno,
203 					&blk->bp, XFS_ATTR_FORK);
204 			if (error)
205 				return error;
206 		} else {
207 			blk->bp = NULL;
208 		}
209 	}
210 
211 	return 0;
212 }
213 #else
xfs_attr_fillstate(xfs_da_state_t * state)214 static int xfs_attr_fillstate(xfs_da_state_t *state) { return 0; }
215 #endif
216 
217 /*========================================================================
218  * Overall external interface routines.
219  *========================================================================*/
220 
221 /*
222  * Retrieve an extended attribute and its value.  Must have ilock.
223  * Returns 0 on successful retrieval, otherwise an error.
224  */
225 int
xfs_attr_get_ilocked(struct xfs_da_args * args)226 xfs_attr_get_ilocked(
227 	struct xfs_da_args	*args)
228 {
229 	int			error;
230 
231 	xfs_assert_ilocked(args->dp, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL);
232 
233 	if (!xfs_inode_hasattr(args->dp))
234 		return -ENOATTR;
235 
236 	/*
237 	 * The incore attr fork iext tree must be loaded for xfs_attr_is_leaf
238 	 * to work correctly.
239 	 */
240 	error = xfs_iread_extents(args->trans, args->dp, XFS_ATTR_FORK);
241 	if (error)
242 		return error;
243 
244 	if (args->dp->i_af.if_format == XFS_DINODE_FMT_LOCAL)
245 		return xfs_attr_shortform_getvalue(args);
246 	if (xfs_attr_is_leaf(args->dp))
247 		return xfs_attr_leaf_get(args);
248 	return xfs_attr_node_get(args);
249 }
250 
251 /*
252  * Retrieve an extended attribute by name, and its value if requested.
253  *
254  * If args->valuelen is zero, then the caller does not want the value, just an
255  * indication whether the attribute exists and the size of the value if it
256  * exists. The size is returned in args.valuelen.
257  *
258  * If args->value is NULL but args->valuelen is non-zero, allocate the buffer
259  * for the value after existence of the attribute has been determined. The
260  * caller always has to free args->value if it is set, no matter if this
261  * function was successful or not.
262  *
263  * If the attribute is found, but exceeds the size limit set by the caller in
264  * args->valuelen, return -ERANGE with the size of the attribute that was found
265  * in args->valuelen.
266  */
267 int
xfs_attr_get(struct xfs_da_args * args)268 xfs_attr_get(
269 	struct xfs_da_args	*args)
270 {
271 	uint			lock_mode;
272 	int			error;
273 
274 	XFS_STATS_INC(args->dp->i_mount, xs_attr_get);
275 
276 	if (xfs_is_shutdown(args->dp->i_mount))
277 		return -EIO;
278 
279 	if (!args->owner)
280 		args->owner = args->dp->i_ino;
281 	args->geo = args->dp->i_mount->m_attr_geo;
282 	args->whichfork = XFS_ATTR_FORK;
283 	xfs_attr_sethash(args);
284 
285 	/* Entirely possible to look up a name which doesn't exist */
286 	args->op_flags = XFS_DA_OP_OKNOENT;
287 
288 	lock_mode = xfs_ilock_attr_map_shared(args->dp);
289 	error = xfs_attr_get_ilocked(args);
290 	xfs_iunlock(args->dp, lock_mode);
291 
292 	return error;
293 }
294 
295 /*
296  * Calculate how many blocks we need for the new attribute,
297  */
298 int
xfs_attr_calc_size(struct xfs_da_args * args,int * local)299 xfs_attr_calc_size(
300 	struct xfs_da_args	*args,
301 	int			*local)
302 {
303 	struct xfs_mount	*mp = args->dp->i_mount;
304 	int			size;
305 	int			nblks;
306 
307 	/*
308 	 * Determine space new attribute will use, and if it would be
309 	 * "local" or "remote" (note: local != inline).
310 	 */
311 	size = xfs_attr_leaf_newentsize(args, local);
312 	nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
313 	if (*local) {
314 		if (size > (args->geo->blksize / 2)) {
315 			/* Double split possible */
316 			nblks *= 2;
317 		}
318 	} else {
319 		/*
320 		 * Out of line attribute, cannot double split, but
321 		 * make room for the attribute value itself.
322 		 */
323 		uint	dblocks = xfs_attr3_rmt_blocks(mp, args->valuelen);
324 		nblks += dblocks;
325 		nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
326 	}
327 
328 	return nblks;
329 }
330 
331 /* Initialize transaction reservation for an xattr set/replace/upsert */
332 inline struct xfs_trans_res
xfs_attr_set_resv(const struct xfs_da_args * args)333 xfs_attr_set_resv(
334 	const struct xfs_da_args	*args)
335 {
336 	struct xfs_mount		*mp = args->dp->i_mount;
337 	struct xfs_trans_res		ret = {
338 		.tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
339 			    M_RES(mp)->tr_attrsetrt.tr_logres * args->total,
340 		.tr_logcount		= XFS_ATTRSET_LOG_COUNT,
341 		.tr_logflags		= XFS_TRANS_PERM_LOG_RES,
342 	};
343 
344 	return ret;
345 }
346 
347 /*
348  * Add an attr to a shortform fork. If there is no space,
349  * xfs_attr_shortform_addname() will convert to leaf format and return -ENOSPC.
350  * to use.
351  */
352 STATIC int
xfs_attr_try_sf_addname(struct xfs_inode * dp,struct xfs_da_args * args)353 xfs_attr_try_sf_addname(
354 	struct xfs_inode	*dp,
355 	struct xfs_da_args	*args)
356 {
357 
358 	int			error;
359 
360 	/*
361 	 * Build initial attribute list (if required).
362 	 */
363 	if (dp->i_af.if_format == XFS_DINODE_FMT_EXTENTS)
364 		xfs_attr_shortform_create(args);
365 
366 	error = xfs_attr_shortform_addname(args);
367 	if (error == -ENOSPC)
368 		return error;
369 
370 	/*
371 	 * Commit the shortform mods, and we're done.
372 	 * NOTE: this is also the error path (EEXIST, etc).
373 	 */
374 	if (!error)
375 		xfs_trans_ichgtime(args->trans, dp, XFS_ICHGTIME_CHG);
376 
377 	if (xfs_has_wsync(dp->i_mount))
378 		xfs_trans_set_sync(args->trans);
379 
380 	return error;
381 }
382 
383 static int
xfs_attr_sf_addname(struct xfs_attr_intent * attr)384 xfs_attr_sf_addname(
385 	struct xfs_attr_intent		*attr)
386 {
387 	struct xfs_da_args		*args = attr->xattri_da_args;
388 	struct xfs_inode		*dp = args->dp;
389 	int				error = 0;
390 
391 	error = xfs_attr_try_sf_addname(dp, args);
392 	if (error != -ENOSPC) {
393 		ASSERT(!error || error == -EEXIST);
394 		attr->xattri_dela_state = XFS_DAS_DONE;
395 		goto out;
396 	}
397 
398 	/*
399 	 * It won't fit in the shortform, transform to a leaf block.  GROT:
400 	 * another possible req'mt for a double-split btree op.
401 	 */
402 	error = xfs_attr_shortform_to_leaf(args);
403 	if (error)
404 		return error;
405 
406 	attr->xattri_dela_state = XFS_DAS_LEAF_ADD;
407 out:
408 	trace_xfs_attr_sf_addname_return(attr->xattri_dela_state, args->dp);
409 	return error;
410 }
411 
412 /* Compute the hash value for a user/root/secure extended attribute */
413 xfs_dahash_t
xfs_attr_hashname(const uint8_t * name,int namelen)414 xfs_attr_hashname(
415 	const uint8_t		*name,
416 	int			namelen)
417 {
418 	return xfs_da_hashname(name, namelen);
419 }
420 
421 /* Compute the hash value for any extended attribute from any namespace. */
422 xfs_dahash_t
xfs_attr_hashval(struct xfs_mount * mp,unsigned int attr_flags,const uint8_t * name,int namelen,const void * value,int valuelen)423 xfs_attr_hashval(
424 	struct xfs_mount	*mp,
425 	unsigned int		attr_flags,
426 	const uint8_t		*name,
427 	int			namelen,
428 	const void		*value,
429 	int			valuelen)
430 {
431 	ASSERT(xfs_attr_check_namespace(attr_flags));
432 
433 	if (attr_flags & XFS_ATTR_PARENT)
434 		return xfs_parent_hashattr(mp, name, namelen, value, valuelen);
435 
436 	return xfs_attr_hashname(name, namelen);
437 }
438 
439 /* Save the current remote block info and clear the current pointers. */
440 static void
xfs_attr_save_rmt_blk(struct xfs_da_args * args)441 xfs_attr_save_rmt_blk(
442 	struct xfs_da_args	*args)
443 {
444 	args->blkno2 = args->blkno;
445 	args->index2 = args->index;
446 	args->rmtblkno2 = args->rmtblkno;
447 	args->rmtblkcnt2 = args->rmtblkcnt;
448 	args->rmtvaluelen2 = args->rmtvaluelen;
449 	args->rmtblkno = 0;
450 	args->rmtblkcnt = 0;
451 	args->rmtvaluelen = 0;
452 }
453 
454 /* Set stored info about a remote block */
455 static void
xfs_attr_restore_rmt_blk(struct xfs_da_args * args)456 xfs_attr_restore_rmt_blk(
457 	struct xfs_da_args	*args)
458 {
459 	args->blkno = args->blkno2;
460 	args->index = args->index2;
461 	args->rmtblkno = args->rmtblkno2;
462 	args->rmtblkcnt = args->rmtblkcnt2;
463 	args->rmtvaluelen = args->rmtvaluelen2;
464 }
465 
466 /*
467  * PPTR_REPLACE operations require the caller to set the old and new names and
468  * values explicitly.  Update the canonical fields to the new name and value
469  * here now that the removal phase has finished.
470  */
471 static void
xfs_attr_update_pptr_replace_args(struct xfs_da_args * args)472 xfs_attr_update_pptr_replace_args(
473 	struct xfs_da_args	*args)
474 {
475 	ASSERT(args->new_namelen > 0);
476 	args->name = args->new_name;
477 	args->namelen = args->new_namelen;
478 	args->value = args->new_value;
479 	args->valuelen = args->new_valuelen;
480 	xfs_attr_sethash(args);
481 }
482 
483 /*
484  * Handle the state change on completion of a multi-state attr operation.
485  *
486  * If the XFS_DA_OP_REPLACE flag is set, this means the operation was the first
487  * modification in a attr replace operation and we still have to do the second
488  * state, indicated by @replace_state.
489  *
490  * We consume the XFS_DA_OP_REPLACE flag so that when we are called again on
491  * completion of the second half of the attr replace operation we correctly
492  * signal that it is done.
493  */
494 static enum xfs_delattr_state
xfs_attr_complete_op(struct xfs_attr_intent * attr,enum xfs_delattr_state replace_state)495 xfs_attr_complete_op(
496 	struct xfs_attr_intent	*attr,
497 	enum xfs_delattr_state	replace_state)
498 {
499 	struct xfs_da_args	*args = attr->xattri_da_args;
500 
501 	if (!(args->op_flags & XFS_DA_OP_REPLACE))
502 		replace_state = XFS_DAS_DONE;
503 	else if (xfs_attr_intent_op(attr) == XFS_ATTRI_OP_FLAGS_PPTR_REPLACE)
504 		xfs_attr_update_pptr_replace_args(args);
505 
506 	args->op_flags &= ~XFS_DA_OP_REPLACE;
507 	args->attr_filter &= ~XFS_ATTR_INCOMPLETE;
508 	return replace_state;
509 }
510 
511 /*
512  * Try to add an attribute to an inode in leaf form.
513  */
514 static int
xfs_attr_leaf_addname(struct xfs_attr_intent * attr)515 xfs_attr_leaf_addname(
516 	struct xfs_attr_intent	*attr)
517 {
518 	struct xfs_da_args	*args = attr->xattri_da_args;
519 	struct xfs_buf		*bp;
520 	int			error;
521 
522 	ASSERT(xfs_attr_is_leaf(args->dp));
523 
524 	error = xfs_attr3_leaf_read(args->trans, args->dp, args->owner, 0, &bp);
525 	if (error)
526 		return error;
527 
528 	/*
529 	 * Look up the xattr name to set the insertion point for the new xattr.
530 	 */
531 	error = xfs_attr3_leaf_lookup_int(bp, args);
532 	switch (error) {
533 	case -ENOATTR:
534 		if (args->op_flags & XFS_DA_OP_REPLACE)
535 			goto out_brelse;
536 		break;
537 	case -EEXIST:
538 		if (!(args->op_flags & XFS_DA_OP_REPLACE))
539 			goto out_brelse;
540 
541 		trace_xfs_attr_leaf_replace(args);
542 		/*
543 		 * Save the existing remote attr state so that the current
544 		 * values reflect the state of the new attribute we are about to
545 		 * add, not the attribute we just found and will remove later.
546 		 */
547 		xfs_attr_save_rmt_blk(args);
548 		break;
549 	case 0:
550 		break;
551 	default:
552 		goto out_brelse;
553 	}
554 
555 	/*
556 	 * We need to commit and roll if we need to allocate remote xattr blocks
557 	 * or perform more xattr manipulations. Otherwise there is nothing more
558 	 * to do and we can return success.
559 	 */
560 	if (!xfs_attr3_leaf_add(bp, args)) {
561 		error = xfs_attr3_leaf_to_node(args);
562 		if (error)
563 			return error;
564 
565 		attr->xattri_dela_state = XFS_DAS_NODE_ADD;
566 	} else if (args->rmtblkno) {
567 		attr->xattri_dela_state = XFS_DAS_LEAF_SET_RMT;
568 	} else {
569 		attr->xattri_dela_state =
570 			xfs_attr_complete_op(attr, XFS_DAS_LEAF_REPLACE);
571 	}
572 
573 	trace_xfs_attr_leaf_addname_return(attr->xattri_dela_state, args->dp);
574 	return 0;
575 
576 out_brelse:
577 	xfs_trans_brelse(args->trans, bp);
578 	return error;
579 }
580 
581 /*
582  * Add an entry to a node format attr tree.
583  *
584  * Note that we might still have a leaf here - xfs_attr_is_leaf() cannot tell
585  * the difference between leaf + remote attr blocks and a node format tree,
586  * so we may still end up having to convert from leaf to node format here.
587  */
588 static int
xfs_attr_node_addname(struct xfs_attr_intent * attr)589 xfs_attr_node_addname(
590 	struct xfs_attr_intent	*attr)
591 {
592 	struct xfs_da_args	*args = attr->xattri_da_args;
593 	int			error;
594 
595 	error = xfs_attr_node_addname_find_attr(attr);
596 	if (error)
597 		return error;
598 
599 	error = xfs_attr_node_try_addname(attr);
600 	if (error == 1) {
601 		error = xfs_attr3_leaf_to_node(args);
602 		if (error)
603 			return error;
604 		/*
605 		 * No state change, we really are in node form now
606 		 * but we need the transaction rolled to continue.
607 		 */
608 		goto out;
609 	}
610 	if (error)
611 		return error;
612 
613 	if (args->rmtblkno)
614 		attr->xattri_dela_state = XFS_DAS_NODE_SET_RMT;
615 	else
616 		attr->xattri_dela_state = xfs_attr_complete_op(attr,
617 							XFS_DAS_NODE_REPLACE);
618 out:
619 	trace_xfs_attr_node_addname_return(attr->xattri_dela_state, args->dp);
620 	return error;
621 }
622 
623 static int
xfs_attr_rmtval_alloc(struct xfs_attr_intent * attr)624 xfs_attr_rmtval_alloc(
625 	struct xfs_attr_intent		*attr)
626 {
627 	struct xfs_da_args              *args = attr->xattri_da_args;
628 	int				error = 0;
629 
630 	/*
631 	 * If there was an out-of-line value, allocate the blocks we
632 	 * identified for its storage and copy the value.  This is done
633 	 * after we create the attribute so that we don't overflow the
634 	 * maximum size of a transaction and/or hit a deadlock.
635 	 */
636 	if (attr->xattri_blkcnt > 0) {
637 		error = xfs_attr_rmtval_set_blk(attr);
638 		if (error)
639 			return error;
640 		/* Roll the transaction only if there is more to allocate. */
641 		if (attr->xattri_blkcnt > 0)
642 			goto out;
643 	}
644 
645 	error = xfs_attr_rmtval_set_value(args);
646 	if (error)
647 		return error;
648 
649 	attr->xattri_dela_state = xfs_attr_complete_op(attr,
650 						++attr->xattri_dela_state);
651 	/*
652 	 * If we are not doing a rename, we've finished the operation but still
653 	 * have to clear the incomplete flag protecting the new attr from
654 	 * exposing partially initialised state if we crash during creation.
655 	 */
656 	if (attr->xattri_dela_state == XFS_DAS_DONE)
657 		error = xfs_attr3_leaf_clearflag(args);
658 out:
659 	trace_xfs_attr_rmtval_alloc(attr->xattri_dela_state, args->dp);
660 	return error;
661 }
662 
663 /*
664  * Mark an attribute entry INCOMPLETE and save pointers to the relevant buffers
665  * for later deletion of the entry.
666  */
667 static int
xfs_attr_leaf_mark_incomplete(struct xfs_da_args * args,struct xfs_da_state * state)668 xfs_attr_leaf_mark_incomplete(
669 	struct xfs_da_args	*args,
670 	struct xfs_da_state	*state)
671 {
672 	int			error;
673 
674 	/*
675 	 * Fill in disk block numbers in the state structure
676 	 * so that we can get the buffers back after we commit
677 	 * several transactions in the following calls.
678 	 */
679 	error = xfs_attr_fillstate(state);
680 	if (error)
681 		return error;
682 
683 	/*
684 	 * Mark the attribute as INCOMPLETE
685 	 */
686 	return xfs_attr3_leaf_setflag(args);
687 }
688 
689 /* Ensure the da state of an xattr deferred work item is ready to go. */
690 static inline void
xfs_attr_item_init_da_state(struct xfs_attr_intent * attr)691 xfs_attr_item_init_da_state(
692 	struct xfs_attr_intent	*attr)
693 {
694 	struct xfs_da_args	*args = attr->xattri_da_args;
695 
696 	if (!attr->xattri_da_state)
697 		attr->xattri_da_state = xfs_da_state_alloc(args);
698 	else
699 		xfs_da_state_reset(attr->xattri_da_state, args);
700 }
701 
702 /*
703  * Initial setup for xfs_attr_node_removename.  Make sure the attr is there and
704  * the blocks are valid.  Attr keys with remote blocks will be marked
705  * incomplete.
706  */
707 static
xfs_attr_node_removename_setup(struct xfs_attr_intent * attr)708 int xfs_attr_node_removename_setup(
709 	struct xfs_attr_intent		*attr)
710 {
711 	struct xfs_da_args		*args = attr->xattri_da_args;
712 	struct xfs_da_state		*state;
713 	int				error;
714 
715 	xfs_attr_item_init_da_state(attr);
716 	error = xfs_attr_node_lookup(args, attr->xattri_da_state);
717 	if (error != -EEXIST)
718 		goto out;
719 	error = 0;
720 
721 	state = attr->xattri_da_state;
722 	ASSERT(state->path.blk[state->path.active - 1].bp != NULL);
723 	ASSERT(state->path.blk[state->path.active - 1].magic ==
724 		XFS_ATTR_LEAF_MAGIC);
725 
726 	error = xfs_attr_leaf_mark_incomplete(args, state);
727 	if (error)
728 		goto out;
729 	if (args->rmtblkno > 0)
730 		error = xfs_attr_rmtval_invalidate(args);
731 out:
732 	if (error) {
733 		xfs_da_state_free(attr->xattri_da_state);
734 		attr->xattri_da_state = NULL;
735 	}
736 
737 	return error;
738 }
739 
740 /*
741  * Remove the original attr we have just replaced. This is dependent on the
742  * original lookup and insert placing the old attr in args->blkno/args->index
743  * and the new attr in args->blkno2/args->index2.
744  */
745 static int
xfs_attr_leaf_remove_attr(struct xfs_attr_intent * attr)746 xfs_attr_leaf_remove_attr(
747 	struct xfs_attr_intent		*attr)
748 {
749 	struct xfs_da_args              *args = attr->xattri_da_args;
750 	struct xfs_inode		*dp = args->dp;
751 	struct xfs_buf			*bp = NULL;
752 	int				forkoff;
753 	int				error;
754 
755 	error = xfs_attr3_leaf_read(args->trans, args->dp, args->owner,
756 			args->blkno, &bp);
757 	if (error)
758 		return error;
759 
760 	xfs_attr3_leaf_remove(bp, args);
761 
762 	forkoff = xfs_attr_shortform_allfit(bp, dp);
763 	if (forkoff)
764 		error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
765 		/* bp is gone due to xfs_da_shrink_inode */
766 
767 	return error;
768 }
769 
770 /*
771  * Shrink an attribute from leaf to shortform. Used by the node format remove
772  * path when the node format collapses to a single block and so we have to check
773  * if it can be collapsed further.
774  */
775 static int
xfs_attr_leaf_shrink(struct xfs_da_args * args)776 xfs_attr_leaf_shrink(
777 	struct xfs_da_args	*args)
778 {
779 	struct xfs_inode	*dp = args->dp;
780 	struct xfs_buf		*bp;
781 	int			forkoff;
782 	int			error;
783 
784 	if (!xfs_attr_is_leaf(dp))
785 		return 0;
786 
787 	error = xfs_attr3_leaf_read(args->trans, args->dp, args->owner, 0, &bp);
788 	if (error)
789 		return error;
790 
791 	forkoff = xfs_attr_shortform_allfit(bp, dp);
792 	if (forkoff) {
793 		error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
794 		/* bp is gone due to xfs_da_shrink_inode */
795 	} else {
796 		xfs_trans_brelse(args->trans, bp);
797 	}
798 
799 	return error;
800 }
801 
802 /*
803  * Run the attribute operation specified in @attr.
804  *
805  * This routine is meant to function as a delayed operation and will set the
806  * state to XFS_DAS_DONE when the operation is complete.  Calling functions will
807  * need to handle this, and recall the function until either an error or
808  * XFS_DAS_DONE is detected.
809  */
810 int
xfs_attr_set_iter(struct xfs_attr_intent * attr)811 xfs_attr_set_iter(
812 	struct xfs_attr_intent		*attr)
813 {
814 	struct xfs_da_args              *args = attr->xattri_da_args;
815 	int				error = 0;
816 
817 	/* State machine switch */
818 next_state:
819 	switch (attr->xattri_dela_state) {
820 	case XFS_DAS_UNINIT:
821 		ASSERT(0);
822 		return -EFSCORRUPTED;
823 	case XFS_DAS_SF_ADD:
824 		return xfs_attr_sf_addname(attr);
825 	case XFS_DAS_LEAF_ADD:
826 		return xfs_attr_leaf_addname(attr);
827 	case XFS_DAS_NODE_ADD:
828 		return xfs_attr_node_addname(attr);
829 
830 	case XFS_DAS_SF_REMOVE:
831 		error = xfs_attr_sf_removename(args);
832 		attr->xattri_dela_state = xfs_attr_complete_op(attr,
833 						xfs_attr_init_add_state(args));
834 		break;
835 	case XFS_DAS_LEAF_REMOVE:
836 		error = xfs_attr_leaf_removename(args);
837 		attr->xattri_dela_state = xfs_attr_complete_op(attr,
838 						xfs_attr_init_add_state(args));
839 		break;
840 	case XFS_DAS_NODE_REMOVE:
841 		error = xfs_attr_node_removename_setup(attr);
842 		if (error == -ENOATTR &&
843 		    (args->op_flags & XFS_DA_OP_RECOVERY)) {
844 			attr->xattri_dela_state = xfs_attr_complete_op(attr,
845 						xfs_attr_init_add_state(args));
846 			error = 0;
847 			break;
848 		}
849 		if (error)
850 			return error;
851 		attr->xattri_dela_state = XFS_DAS_NODE_REMOVE_RMT;
852 		if (args->rmtblkno == 0)
853 			attr->xattri_dela_state++;
854 		break;
855 
856 	case XFS_DAS_LEAF_SET_RMT:
857 	case XFS_DAS_NODE_SET_RMT:
858 		error = xfs_attr_rmtval_find_space(attr);
859 		if (error)
860 			return error;
861 		attr->xattri_dela_state++;
862 		fallthrough;
863 
864 	case XFS_DAS_LEAF_ALLOC_RMT:
865 	case XFS_DAS_NODE_ALLOC_RMT:
866 		error = xfs_attr_rmtval_alloc(attr);
867 		if (error)
868 			return error;
869 		if (attr->xattri_dela_state == XFS_DAS_DONE)
870 			break;
871 		goto next_state;
872 
873 	case XFS_DAS_LEAF_REPLACE:
874 	case XFS_DAS_NODE_REPLACE:
875 		/*
876 		 * We must "flip" the incomplete flags on the "new" and "old"
877 		 * attribute/value pairs so that one disappears and one appears
878 		 * atomically.
879 		 */
880 		error = xfs_attr3_leaf_flipflags(args);
881 		if (error)
882 			return error;
883 		/*
884 		 * We must commit the flag value change now to make it atomic
885 		 * and then we can start the next trans in series at REMOVE_OLD.
886 		 */
887 		attr->xattri_dela_state++;
888 		break;
889 
890 	case XFS_DAS_LEAF_REMOVE_OLD:
891 	case XFS_DAS_NODE_REMOVE_OLD:
892 		/*
893 		 * If we have a remote attr, start the process of removing it
894 		 * by invalidating any cached buffers.
895 		 *
896 		 * If we don't have a remote attr, we skip the remote block
897 		 * removal state altogether with a second state increment.
898 		 */
899 		xfs_attr_restore_rmt_blk(args);
900 		if (args->rmtblkno) {
901 			error = xfs_attr_rmtval_invalidate(args);
902 			if (error)
903 				return error;
904 		} else {
905 			attr->xattri_dela_state++;
906 		}
907 
908 		attr->xattri_dela_state++;
909 		goto next_state;
910 
911 	case XFS_DAS_LEAF_REMOVE_RMT:
912 	case XFS_DAS_NODE_REMOVE_RMT:
913 		error = xfs_attr_rmtval_remove(attr);
914 		if (error == -EAGAIN) {
915 			error = 0;
916 			break;
917 		}
918 		if (error)
919 			return error;
920 
921 		/*
922 		 * We've finished removing the remote attr blocks, so commit the
923 		 * transaction and move on to removing the attr name from the
924 		 * leaf/node block. Removing the attr might require a full
925 		 * transaction reservation for btree block freeing, so we
926 		 * can't do that in the same transaction where we removed the
927 		 * remote attr blocks.
928 		 */
929 		attr->xattri_dela_state++;
930 		break;
931 
932 	case XFS_DAS_LEAF_REMOVE_ATTR:
933 		error = xfs_attr_leaf_remove_attr(attr);
934 		attr->xattri_dela_state = xfs_attr_complete_op(attr,
935 						xfs_attr_init_add_state(args));
936 		break;
937 
938 	case XFS_DAS_NODE_REMOVE_ATTR:
939 		error = xfs_attr_node_remove_attr(attr);
940 		if (!error)
941 			error = xfs_attr_leaf_shrink(args);
942 		attr->xattri_dela_state = xfs_attr_complete_op(attr,
943 						xfs_attr_init_add_state(args));
944 		break;
945 	default:
946 		ASSERT(0);
947 		break;
948 	}
949 
950 	trace_xfs_attr_set_iter_return(attr->xattri_dela_state, args->dp);
951 	return error;
952 }
953 
954 
955 /*
956  * Return EEXIST if attr is found, or ENOATTR if not
957  */
958 static int
xfs_attr_lookup(struct xfs_da_args * args)959 xfs_attr_lookup(
960 	struct xfs_da_args	*args)
961 {
962 	struct xfs_inode	*dp = args->dp;
963 	struct xfs_buf		*bp = NULL;
964 	struct xfs_da_state	*state;
965 	int			error;
966 
967 	if (!xfs_inode_hasattr(dp))
968 		return -ENOATTR;
969 
970 	if (dp->i_af.if_format == XFS_DINODE_FMT_LOCAL) {
971 		if (xfs_attr_sf_findname(args))
972 			return -EEXIST;
973 		return -ENOATTR;
974 	}
975 
976 	/* Prerequisite for xfs_attr_is_leaf */
977 	error = xfs_iread_extents(args->trans, args->dp, XFS_ATTR_FORK);
978 	if (error)
979 		return error;
980 
981 	if (xfs_attr_is_leaf(dp)) {
982 		error = xfs_attr_leaf_hasname(args, &bp);
983 
984 		if (bp)
985 			xfs_trans_brelse(args->trans, bp);
986 
987 		return error;
988 	}
989 
990 	state = xfs_da_state_alloc(args);
991 	error = xfs_attr_node_lookup(args, state);
992 	xfs_da_state_free(state);
993 	return error;
994 }
995 
996 int
xfs_attr_add_fork(struct xfs_inode * ip,int size,int rsvd)997 xfs_attr_add_fork(
998 	struct xfs_inode	*ip,		/* incore inode pointer */
999 	int			size,		/* space new attribute needs */
1000 	int			rsvd)		/* xact may use reserved blks */
1001 {
1002 	struct xfs_mount	*mp = ip->i_mount;
1003 	struct xfs_trans	*tp;		/* transaction pointer */
1004 	unsigned int		blks;		/* space reservation */
1005 	int			error;		/* error return value */
1006 
1007 	if (xfs_is_metadir_inode(ip))
1008 		ASSERT(XFS_IS_DQDETACHED(ip));
1009 	else
1010 		ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
1011 
1012 	blks = XFS_ADDAFORK_SPACE_RES(mp);
1013 
1014 	error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_addafork, blks, 0,
1015 			rsvd, &tp);
1016 	if (error)
1017 		return error;
1018 
1019 	if (xfs_inode_has_attr_fork(ip))
1020 		goto trans_cancel;
1021 
1022 	error = xfs_bmap_add_attrfork(tp, ip, size, rsvd);
1023 	if (error)
1024 		goto trans_cancel;
1025 
1026 	error = xfs_trans_commit(tp);
1027 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
1028 	return error;
1029 
1030 trans_cancel:
1031 	xfs_trans_cancel(tp);
1032 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
1033 	return error;
1034 }
1035 
1036 /*
1037  * Make a change to the xattr structure.
1038  *
1039  * The caller must have initialized @args, attached dquots, and must not hold
1040  * any ILOCKs.  Reserved data blocks may be used if @rsvd is set.
1041  *
1042  * Returns -EEXIST for XFS_ATTRUPDATE_CREATE if the name already exists.
1043  * Returns -ENOATTR for XFS_ATTRUPDATE_REMOVE if the name does not exist.
1044  * Returns 0 on success, or a negative errno if something else went wrong.
1045  */
1046 int
xfs_attr_set(struct xfs_da_args * args,enum xfs_attr_update op,bool rsvd)1047 xfs_attr_set(
1048 	struct xfs_da_args	*args,
1049 	enum xfs_attr_update	op,
1050 	bool			rsvd)
1051 {
1052 	struct xfs_inode	*dp = args->dp;
1053 	struct xfs_mount	*mp = dp->i_mount;
1054 	struct xfs_trans_res	tres;
1055 	int			error, local;
1056 	int			rmt_blks = 0;
1057 	unsigned int		total = 0;
1058 
1059 	ASSERT(!args->trans);
1060 
1061 	switch (op) {
1062 	case XFS_ATTRUPDATE_UPSERT:
1063 	case XFS_ATTRUPDATE_CREATE:
1064 	case XFS_ATTRUPDATE_REPLACE:
1065 		XFS_STATS_INC(mp, xs_attr_set);
1066 		args->total = xfs_attr_calc_size(args, &local);
1067 
1068 		/*
1069 		 * If the inode doesn't have an attribute fork, add one.
1070 		 * (inode must not be locked when we call this routine)
1071 		 */
1072 		if (xfs_inode_has_attr_fork(dp) == 0) {
1073 			int sf_size = sizeof(struct xfs_attr_sf_hdr) +
1074 				xfs_attr_sf_entsize_byname(args->namelen,
1075 						args->valuelen);
1076 
1077 			error = xfs_attr_add_fork(dp, sf_size, rsvd);
1078 			if (error)
1079 				return error;
1080 		}
1081 
1082 		if (!local)
1083 			rmt_blks = xfs_attr3_rmt_blocks(mp, args->valuelen);
1084 
1085 		tres = xfs_attr_set_resv(args);
1086 		total = args->total;
1087 		break;
1088 	case XFS_ATTRUPDATE_REMOVE:
1089 		XFS_STATS_INC(mp, xs_attr_remove);
1090 		rmt_blks = xfs_attr3_max_rmt_blocks(mp);
1091 		tres = M_RES(mp)->tr_attrrm;
1092 		total = XFS_ATTRRM_SPACE_RES(mp);
1093 		break;
1094 	}
1095 
1096 	/*
1097 	 * Root fork attributes can use reserved data blocks for this
1098 	 * operation if necessary
1099 	 */
1100 	error = xfs_trans_alloc_inode(dp, &tres, total, 0, rsvd, &args->trans);
1101 	if (error)
1102 		return error;
1103 
1104 	if (op != XFS_ATTRUPDATE_REMOVE || xfs_inode_hasattr(dp)) {
1105 		error = xfs_iext_count_extend(args->trans, dp, XFS_ATTR_FORK,
1106 				XFS_IEXT_ATTR_MANIP_CNT(rmt_blks));
1107 		if (error)
1108 			goto out_trans_cancel;
1109 	}
1110 
1111 	error = xfs_attr_lookup(args);
1112 	switch (error) {
1113 	case -EEXIST:
1114 		if (op == XFS_ATTRUPDATE_REMOVE) {
1115 			/* if no value, we are performing a remove operation */
1116 			xfs_attr_defer_add(args, XFS_ATTR_DEFER_REMOVE);
1117 			break;
1118 		}
1119 
1120 		/* Pure create fails if the attr already exists */
1121 		if (op == XFS_ATTRUPDATE_CREATE)
1122 			goto out_trans_cancel;
1123 		xfs_attr_defer_add(args, XFS_ATTR_DEFER_REPLACE);
1124 		break;
1125 	case -ENOATTR:
1126 		/* Can't remove what isn't there. */
1127 		if (op == XFS_ATTRUPDATE_REMOVE)
1128 			goto out_trans_cancel;
1129 
1130 		/* Pure replace fails if no existing attr to replace. */
1131 		if (op == XFS_ATTRUPDATE_REPLACE)
1132 			goto out_trans_cancel;
1133 		xfs_attr_defer_add(args, XFS_ATTR_DEFER_SET);
1134 		break;
1135 	default:
1136 		goto out_trans_cancel;
1137 	}
1138 
1139 	/*
1140 	 * If this is a synchronous mount, make sure that the
1141 	 * transaction goes to disk before returning to the user.
1142 	 */
1143 	if (xfs_has_wsync(mp))
1144 		xfs_trans_set_sync(args->trans);
1145 
1146 	xfs_trans_ichgtime(args->trans, dp, XFS_ICHGTIME_CHG);
1147 
1148 	/*
1149 	 * Commit the last in the sequence of transactions.
1150 	 */
1151 	xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
1152 	error = xfs_trans_commit(args->trans);
1153 out_unlock:
1154 	xfs_iunlock(dp, XFS_ILOCK_EXCL);
1155 	args->trans = NULL;
1156 	return error;
1157 
1158 out_trans_cancel:
1159 	if (args->trans)
1160 		xfs_trans_cancel(args->trans);
1161 	goto out_unlock;
1162 }
1163 
1164 /*========================================================================
1165  * External routines when attribute list is inside the inode
1166  *========================================================================*/
1167 
xfs_attr_sf_totsize(struct xfs_inode * dp)1168 int xfs_attr_sf_totsize(struct xfs_inode *dp)
1169 {
1170 	struct xfs_attr_sf_hdr *sf = dp->i_af.if_data;
1171 
1172 	return be16_to_cpu(sf->totsize);
1173 }
1174 
1175 /*
1176  * Add a name to the shortform attribute list structure
1177  * This is the external routine.
1178  */
1179 static int
xfs_attr_shortform_addname(struct xfs_da_args * args)1180 xfs_attr_shortform_addname(
1181 	struct xfs_da_args	*args)
1182 {
1183 	int			newsize, forkoff;
1184 
1185 	trace_xfs_attr_sf_addname(args);
1186 
1187 	if (xfs_attr_sf_findname(args)) {
1188 		int		error;
1189 
1190 		ASSERT(args->op_flags & XFS_DA_OP_REPLACE);
1191 
1192 		error = xfs_attr_sf_removename(args);
1193 		if (error)
1194 			return error;
1195 
1196 		/*
1197 		 * Since we have removed the old attr, clear XFS_DA_OP_REPLACE
1198 		 * so that the new attr doesn't fit in shortform format, the
1199 		 * leaf format add routine won't trip over the attr not being
1200 		 * around.
1201 		 */
1202 		args->op_flags &= ~XFS_DA_OP_REPLACE;
1203 	} else {
1204 		ASSERT(!(args->op_flags & XFS_DA_OP_REPLACE));
1205 	}
1206 
1207 	if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
1208 	    args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
1209 		return -ENOSPC;
1210 
1211 	newsize = xfs_attr_sf_totsize(args->dp);
1212 	newsize += xfs_attr_sf_entsize_byname(args->namelen, args->valuelen);
1213 
1214 	forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
1215 	if (!forkoff)
1216 		return -ENOSPC;
1217 
1218 	xfs_attr_shortform_add(args, forkoff);
1219 	return 0;
1220 }
1221 
1222 
1223 /*========================================================================
1224  * External routines when attribute list is one block
1225  *========================================================================*/
1226 
1227 /*
1228  * Return EEXIST if attr is found, or ENOATTR if not
1229  */
1230 STATIC int
xfs_attr_leaf_hasname(struct xfs_da_args * args,struct xfs_buf ** bp)1231 xfs_attr_leaf_hasname(
1232 	struct xfs_da_args	*args,
1233 	struct xfs_buf		**bp)
1234 {
1235 	int                     error = 0;
1236 
1237 	error = xfs_attr3_leaf_read(args->trans, args->dp, args->owner, 0, bp);
1238 	if (error)
1239 		return error;
1240 
1241 	error = xfs_attr3_leaf_lookup_int(*bp, args);
1242 	if (error != -ENOATTR && error != -EEXIST)
1243 		xfs_trans_brelse(args->trans, *bp);
1244 
1245 	return error;
1246 }
1247 
1248 /*
1249  * Remove a name from the leaf attribute list structure
1250  *
1251  * This leaf block cannot have a "remote" value, we only call this routine
1252  * if bmap_one_block() says there is only one block (ie: no remote blks).
1253  */
1254 STATIC int
xfs_attr_leaf_removename(struct xfs_da_args * args)1255 xfs_attr_leaf_removename(
1256 	struct xfs_da_args	*args)
1257 {
1258 	struct xfs_inode	*dp;
1259 	struct xfs_buf		*bp;
1260 	int			error, forkoff;
1261 
1262 	trace_xfs_attr_leaf_removename(args);
1263 
1264 	/*
1265 	 * Remove the attribute.
1266 	 */
1267 	dp = args->dp;
1268 
1269 	error = xfs_attr_leaf_hasname(args, &bp);
1270 	if (error == -ENOATTR) {
1271 		xfs_trans_brelse(args->trans, bp);
1272 		if (args->op_flags & XFS_DA_OP_RECOVERY)
1273 			return 0;
1274 		return error;
1275 	} else if (error != -EEXIST)
1276 		return error;
1277 
1278 	xfs_attr3_leaf_remove(bp, args);
1279 
1280 	/*
1281 	 * If the result is small enough, shrink it all into the inode.
1282 	 */
1283 	forkoff = xfs_attr_shortform_allfit(bp, dp);
1284 	if (forkoff)
1285 		return xfs_attr3_leaf_to_shortform(bp, args, forkoff);
1286 		/* bp is gone due to xfs_da_shrink_inode */
1287 
1288 	return 0;
1289 }
1290 
1291 /*
1292  * Look up a name in a leaf attribute list structure.
1293  *
1294  * This leaf block cannot have a "remote" value, we only call this routine
1295  * if bmap_one_block() says there is only one block (ie: no remote blks).
1296  *
1297  * Returns 0 on successful retrieval, otherwise an error.
1298  */
1299 STATIC int
xfs_attr_leaf_get(xfs_da_args_t * args)1300 xfs_attr_leaf_get(xfs_da_args_t *args)
1301 {
1302 	struct xfs_buf *bp;
1303 	int error;
1304 
1305 	trace_xfs_attr_leaf_get(args);
1306 
1307 	error = xfs_attr_leaf_hasname(args, &bp);
1308 
1309 	if (error == -ENOATTR)  {
1310 		xfs_trans_brelse(args->trans, bp);
1311 		return error;
1312 	} else if (error != -EEXIST)
1313 		return error;
1314 
1315 
1316 	error = xfs_attr3_leaf_getvalue(bp, args);
1317 	xfs_trans_brelse(args->trans, bp);
1318 	return error;
1319 }
1320 
1321 /* Return EEXIST if attr is found, or ENOATTR if not. */
1322 STATIC int
xfs_attr_node_lookup(struct xfs_da_args * args,struct xfs_da_state * state)1323 xfs_attr_node_lookup(
1324 	struct xfs_da_args	*args,
1325 	struct xfs_da_state	*state)
1326 {
1327 	int			retval, error;
1328 
1329 	/*
1330 	 * Search to see if name exists, and get back a pointer to it.
1331 	 */
1332 	error = xfs_da3_node_lookup_int(state, &retval);
1333 	if (error)
1334 		return error;
1335 
1336 	return retval;
1337 }
1338 
1339 /*========================================================================
1340  * External routines when attribute list size > geo->blksize
1341  *========================================================================*/
1342 
1343 STATIC int
xfs_attr_node_addname_find_attr(struct xfs_attr_intent * attr)1344 xfs_attr_node_addname_find_attr(
1345 	 struct xfs_attr_intent	*attr)
1346 {
1347 	struct xfs_da_args	*args = attr->xattri_da_args;
1348 	int			error;
1349 
1350 	/*
1351 	 * Search to see if name already exists, and get back a pointer
1352 	 * to where it should go.
1353 	 */
1354 	xfs_attr_item_init_da_state(attr);
1355 	error = xfs_attr_node_lookup(args, attr->xattri_da_state);
1356 	switch (error) {
1357 	case -ENOATTR:
1358 		if (args->op_flags & XFS_DA_OP_REPLACE)
1359 			goto error;
1360 		break;
1361 	case -EEXIST:
1362 		if (!(args->op_flags & XFS_DA_OP_REPLACE))
1363 			goto error;
1364 
1365 
1366 		trace_xfs_attr_node_replace(args);
1367 		/*
1368 		 * Save the existing remote attr state so that the current
1369 		 * values reflect the state of the new attribute we are about to
1370 		 * add, not the attribute we just found and will remove later.
1371 		 */
1372 		xfs_attr_save_rmt_blk(args);
1373 		break;
1374 	case 0:
1375 		break;
1376 	default:
1377 		goto error;
1378 	}
1379 
1380 	return 0;
1381 error:
1382 	if (attr->xattri_da_state) {
1383 		xfs_da_state_free(attr->xattri_da_state);
1384 		attr->xattri_da_state = NULL;
1385 	}
1386 	return error;
1387 }
1388 
1389 /*
1390  * Add a name to a Btree-format attribute list.
1391  *
1392  * This will involve walking down the Btree, and may involve splitting leaf
1393  * nodes and even splitting intermediate nodes up to and including the root
1394  * node (a special case of an intermediate node).
1395  *
1396  * If the tree was still in single leaf format and needs to converted to
1397  * real node format return 1 and let the caller handle that.
1398  */
1399 static int
xfs_attr_node_try_addname(struct xfs_attr_intent * attr)1400 xfs_attr_node_try_addname(
1401 	struct xfs_attr_intent		*attr)
1402 {
1403 	struct xfs_da_state		*state = attr->xattri_da_state;
1404 	struct xfs_da_state_blk		*blk;
1405 	int				error = 0;
1406 
1407 	trace_xfs_attr_node_addname(state->args);
1408 
1409 	blk = &state->path.blk[state->path.active-1];
1410 	ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1411 
1412 	if (!xfs_attr3_leaf_add(blk->bp, state->args)) {
1413 		if (state->path.active == 1) {
1414 			/*
1415 			 * Its really a single leaf node, but it had
1416 			 * out-of-line values so it looked like it *might*
1417 			 * have been a b-tree. Let the caller deal with this.
1418 			 */
1419 			error = 1;
1420 			goto out;
1421 		}
1422 
1423 		/*
1424 		 * Split as many Btree elements as required.
1425 		 * This code tracks the new and old attr's location
1426 		 * in the index/blkno/rmtblkno/rmtblkcnt fields and
1427 		 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
1428 		 */
1429 		error = xfs_da3_split(state);
1430 		if (error)
1431 			goto out;
1432 	} else {
1433 		/*
1434 		 * Addition succeeded, update Btree hashvals.
1435 		 */
1436 		xfs_da3_fixhashpath(state, &state->path);
1437 	}
1438 
1439 out:
1440 	xfs_da_state_free(state);
1441 	attr->xattri_da_state = NULL;
1442 	return error;
1443 }
1444 
1445 static int
xfs_attr_node_removename(struct xfs_da_args * args,struct xfs_da_state * state)1446 xfs_attr_node_removename(
1447 	struct xfs_da_args	*args,
1448 	struct xfs_da_state	*state)
1449 {
1450 	struct xfs_da_state_blk	*blk;
1451 	int			retval;
1452 
1453 	/*
1454 	 * Remove the name and update the hashvals in the tree.
1455 	 */
1456 	blk = &state->path.blk[state->path.active-1];
1457 	ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1458 	retval = xfs_attr3_leaf_remove(blk->bp, args);
1459 	xfs_da3_fixhashpath(state, &state->path);
1460 
1461 	return retval;
1462 }
1463 
1464 static int
xfs_attr_node_remove_attr(struct xfs_attr_intent * attr)1465 xfs_attr_node_remove_attr(
1466 	struct xfs_attr_intent		*attr)
1467 {
1468 	struct xfs_da_args		*args = attr->xattri_da_args;
1469 	struct xfs_da_state		*state = xfs_da_state_alloc(args);
1470 	int				retval = 0;
1471 	int				error = 0;
1472 
1473 	/*
1474 	 * The attr we are removing has already been marked incomplete, so
1475 	 * we need to set the filter appropriately to re-find the "old"
1476 	 * attribute entry after any split ops.
1477 	 */
1478 	args->attr_filter |= XFS_ATTR_INCOMPLETE;
1479 	error = xfs_da3_node_lookup_int(state, &retval);
1480 	if (error)
1481 		goto out;
1482 
1483 	error = xfs_attr_node_removename(args, state);
1484 
1485 	/*
1486 	 * Check to see if the tree needs to be collapsed.
1487 	 */
1488 	if (retval && (state->path.active > 1)) {
1489 		error = xfs_da3_join(state);
1490 		if (error)
1491 			goto out;
1492 	}
1493 	retval = error = 0;
1494 
1495 out:
1496 	xfs_da_state_free(state);
1497 	if (error)
1498 		return error;
1499 	return retval;
1500 }
1501 
1502 /*
1503  * Retrieve the attribute data from a node attribute list.
1504  *
1505  * This routine gets called for any attribute fork that has more than one
1506  * block, ie: both true Btree attr lists and for single-leaf-blocks with
1507  * "remote" values taking up more blocks.
1508  *
1509  * Returns 0 on successful retrieval, otherwise an error.
1510  */
1511 STATIC int
xfs_attr_node_get(struct xfs_da_args * args)1512 xfs_attr_node_get(
1513 	struct xfs_da_args	*args)
1514 {
1515 	struct xfs_da_state	*state;
1516 	struct xfs_da_state_blk	*blk;
1517 	int			i;
1518 	int			error;
1519 
1520 	trace_xfs_attr_node_get(args);
1521 
1522 	/*
1523 	 * Search to see if name exists, and get back a pointer to it.
1524 	 */
1525 	state = xfs_da_state_alloc(args);
1526 	error = xfs_attr_node_lookup(args, state);
1527 	if (error != -EEXIST)
1528 		goto out_release;
1529 
1530 	/*
1531 	 * Get the value, local or "remote"
1532 	 */
1533 	blk = &state->path.blk[state->path.active - 1];
1534 	error = xfs_attr3_leaf_getvalue(blk->bp, args);
1535 
1536 	/*
1537 	 * If not in a transaction, we have to release all the buffers.
1538 	 */
1539 out_release:
1540 	for (i = 0; i < state->path.active; i++) {
1541 		xfs_trans_brelse(args->trans, state->path.blk[i].bp);
1542 		state->path.blk[i].bp = NULL;
1543 	}
1544 
1545 	xfs_da_state_free(state);
1546 	return error;
1547 }
1548 
1549 /* Enforce that there is at most one namespace bit per attr. */
xfs_attr_check_namespace(unsigned int attr_flags)1550 inline bool xfs_attr_check_namespace(unsigned int attr_flags)
1551 {
1552 	return hweight32(attr_flags & XFS_ATTR_NSP_ONDISK_MASK) < 2;
1553 }
1554 
1555 /* Returns true if the attribute entry name is valid. */
1556 bool
xfs_attr_namecheck(unsigned int attr_flags,const void * name,size_t length)1557 xfs_attr_namecheck(
1558 	unsigned int	attr_flags,
1559 	const void	*name,
1560 	size_t		length)
1561 {
1562 	/* Only one namespace bit allowed. */
1563 	if (!xfs_attr_check_namespace(attr_flags))
1564 		return false;
1565 
1566 	/*
1567 	 * MAXNAMELEN includes the trailing null, but (name/length) leave it
1568 	 * out, so use >= for the length check.
1569 	 */
1570 	if (length >= MAXNAMELEN)
1571 		return false;
1572 
1573 	/* Parent pointers have their own validation. */
1574 	if (attr_flags & XFS_ATTR_PARENT)
1575 		return xfs_parent_namecheck(attr_flags, name, length);
1576 
1577 	/* There shouldn't be any nulls here */
1578 	return !memchr(name, 0, length);
1579 }
1580 
1581 int __init
xfs_attr_intent_init_cache(void)1582 xfs_attr_intent_init_cache(void)
1583 {
1584 	xfs_attr_intent_cache = kmem_cache_create("xfs_attr_intent",
1585 			sizeof(struct xfs_attr_intent),
1586 			0, 0, NULL);
1587 
1588 	return xfs_attr_intent_cache != NULL ? 0 : -ENOMEM;
1589 }
1590 
1591 void
xfs_attr_intent_destroy_cache(void)1592 xfs_attr_intent_destroy_cache(void)
1593 {
1594 	kmem_cache_destroy(xfs_attr_intent_cache);
1595 	xfs_attr_intent_cache = NULL;
1596 }
1597