xref: /openbmc/linux/fs/xfs/xfs_attr_item.c (revision f59a3ee6)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2022 Oracle.  All Rights Reserved.
4  * Author: Allison Henderson <allison.henderson@oracle.com>
5  */
6 
7 #include "xfs.h"
8 #include "xfs_fs.h"
9 #include "xfs_format.h"
10 #include "xfs_trans_resv.h"
11 #include "xfs_shared.h"
12 #include "xfs_mount.h"
13 #include "xfs_defer.h"
14 #include "xfs_log_format.h"
15 #include "xfs_trans.h"
16 #include "xfs_bmap_btree.h"
17 #include "xfs_trans_priv.h"
18 #include "xfs_log.h"
19 #include "xfs_inode.h"
20 #include "xfs_da_format.h"
21 #include "xfs_da_btree.h"
22 #include "xfs_attr.h"
23 #include "xfs_attr_item.h"
24 #include "xfs_trace.h"
25 #include "xfs_trans_space.h"
26 #include "xfs_errortag.h"
27 #include "xfs_error.h"
28 #include "xfs_log_priv.h"
29 #include "xfs_log_recover.h"
30 
31 struct kmem_cache		*xfs_attri_cache;
32 struct kmem_cache		*xfs_attrd_cache;
33 
34 static const struct xfs_item_ops xfs_attri_item_ops;
35 static const struct xfs_item_ops xfs_attrd_item_ops;
36 static struct xfs_attrd_log_item *xfs_trans_get_attrd(struct xfs_trans *tp,
37 					struct xfs_attri_log_item *attrip);
38 
39 static inline struct xfs_attri_log_item *ATTRI_ITEM(struct xfs_log_item *lip)
40 {
41 	return container_of(lip, struct xfs_attri_log_item, attri_item);
42 }
43 
44 /*
45  * Shared xattr name/value buffers for logged extended attribute operations
46  *
47  * When logging updates to extended attributes, we can create quite a few
48  * attribute log intent items for a single xattr update.  To avoid cycling the
49  * memory allocator and memcpy overhead, the name (and value, for setxattr)
50  * are kept in a refcounted object that is shared across all related log items
51  * and the upper-level deferred work state structure.  The shared buffer has
52  * a control structure, followed by the name, and then the value.
53  */
54 
55 static inline struct xfs_attri_log_nameval *
56 xfs_attri_log_nameval_get(
57 	struct xfs_attri_log_nameval	*nv)
58 {
59 	if (!refcount_inc_not_zero(&nv->refcount))
60 		return NULL;
61 	return nv;
62 }
63 
64 static inline void
65 xfs_attri_log_nameval_put(
66 	struct xfs_attri_log_nameval	*nv)
67 {
68 	if (!nv)
69 		return;
70 	if (refcount_dec_and_test(&nv->refcount))
71 		kvfree(nv);
72 }
73 
74 static inline struct xfs_attri_log_nameval *
75 xfs_attri_log_nameval_alloc(
76 	const void			*name,
77 	unsigned int			name_len,
78 	const void			*value,
79 	unsigned int			value_len)
80 {
81 	struct xfs_attri_log_nameval	*nv;
82 
83 	/*
84 	 * This could be over 64kB in length, so we have to use kvmalloc() for
85 	 * this. But kvmalloc() utterly sucks, so we use our own version.
86 	 */
87 	nv = xlog_kvmalloc(sizeof(struct xfs_attri_log_nameval) +
88 					name_len + value_len);
89 
90 	nv->name.i_addr = nv + 1;
91 	nv->name.i_len = name_len;
92 	nv->name.i_type = XLOG_REG_TYPE_ATTR_NAME;
93 	memcpy(nv->name.i_addr, name, name_len);
94 
95 	if (value_len) {
96 		nv->value.i_addr = nv->name.i_addr + name_len;
97 		nv->value.i_len = value_len;
98 		memcpy(nv->value.i_addr, value, value_len);
99 	} else {
100 		nv->value.i_addr = NULL;
101 		nv->value.i_len = 0;
102 	}
103 	nv->value.i_type = XLOG_REG_TYPE_ATTR_VALUE;
104 
105 	refcount_set(&nv->refcount, 1);
106 	return nv;
107 }
108 
109 STATIC void
110 xfs_attri_item_free(
111 	struct xfs_attri_log_item	*attrip)
112 {
113 	kmem_free(attrip->attri_item.li_lv_shadow);
114 	xfs_attri_log_nameval_put(attrip->attri_nameval);
115 	kmem_cache_free(xfs_attri_cache, attrip);
116 }
117 
118 /*
119  * Freeing the attrip requires that we remove it from the AIL if it has already
120  * been placed there. However, the ATTRI may not yet have been placed in the
121  * AIL when called by xfs_attri_release() from ATTRD processing due to the
122  * ordering of committed vs unpin operations in bulk insert operations. Hence
123  * the reference count to ensure only the last caller frees the ATTRI.
124  */
125 STATIC void
126 xfs_attri_release(
127 	struct xfs_attri_log_item	*attrip)
128 {
129 	ASSERT(atomic_read(&attrip->attri_refcount) > 0);
130 	if (!atomic_dec_and_test(&attrip->attri_refcount))
131 		return;
132 
133 	xfs_trans_ail_delete(&attrip->attri_item, 0);
134 	xfs_attri_item_free(attrip);
135 }
136 
137 STATIC void
138 xfs_attri_item_size(
139 	struct xfs_log_item		*lip,
140 	int				*nvecs,
141 	int				*nbytes)
142 {
143 	struct xfs_attri_log_item       *attrip = ATTRI_ITEM(lip);
144 	struct xfs_attri_log_nameval	*nv = attrip->attri_nameval;
145 
146 	*nvecs += 2;
147 	*nbytes += sizeof(struct xfs_attri_log_format) +
148 			xlog_calc_iovec_len(nv->name.i_len);
149 
150 	if (!nv->value.i_len)
151 		return;
152 
153 	*nvecs += 1;
154 	*nbytes += xlog_calc_iovec_len(nv->value.i_len);
155 }
156 
157 /*
158  * This is called to fill in the log iovecs for the given attri log
159  * item. We use  1 iovec for the attri_format_item, 1 for the name, and
160  * another for the value if it is present
161  */
162 STATIC void
163 xfs_attri_item_format(
164 	struct xfs_log_item		*lip,
165 	struct xfs_log_vec		*lv)
166 {
167 	struct xfs_attri_log_item	*attrip = ATTRI_ITEM(lip);
168 	struct xfs_log_iovec		*vecp = NULL;
169 	struct xfs_attri_log_nameval	*nv = attrip->attri_nameval;
170 
171 	attrip->attri_format.alfi_type = XFS_LI_ATTRI;
172 	attrip->attri_format.alfi_size = 1;
173 
174 	/*
175 	 * This size accounting must be done before copying the attrip into the
176 	 * iovec.  If we do it after, the wrong size will be recorded to the log
177 	 * and we trip across assertion checks for bad region sizes later during
178 	 * the log recovery.
179 	 */
180 
181 	ASSERT(nv->name.i_len > 0);
182 	attrip->attri_format.alfi_size++;
183 
184 	if (nv->value.i_len > 0)
185 		attrip->attri_format.alfi_size++;
186 
187 	xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_ATTRI_FORMAT,
188 			&attrip->attri_format,
189 			sizeof(struct xfs_attri_log_format));
190 	xlog_copy_from_iovec(lv, &vecp, &nv->name);
191 	if (nv->value.i_len > 0)
192 		xlog_copy_from_iovec(lv, &vecp, &nv->value);
193 }
194 
195 /*
196  * The unpin operation is the last place an ATTRI is manipulated in the log. It
197  * is either inserted in the AIL or aborted in the event of a log I/O error. In
198  * either case, the ATTRI transaction has been successfully committed to make
199  * it this far. Therefore, we expect whoever committed the ATTRI to either
200  * construct and commit the ATTRD or drop the ATTRD's reference in the event of
201  * error. Simply drop the log's ATTRI reference now that the log is done with
202  * it.
203  */
204 STATIC void
205 xfs_attri_item_unpin(
206 	struct xfs_log_item	*lip,
207 	int			remove)
208 {
209 	xfs_attri_release(ATTRI_ITEM(lip));
210 }
211 
212 
213 STATIC void
214 xfs_attri_item_release(
215 	struct xfs_log_item	*lip)
216 {
217 	xfs_attri_release(ATTRI_ITEM(lip));
218 }
219 
220 /*
221  * Allocate and initialize an attri item.  Caller may allocate an additional
222  * trailing buffer for name and value
223  */
224 STATIC struct xfs_attri_log_item *
225 xfs_attri_init(
226 	struct xfs_mount		*mp,
227 	struct xfs_attri_log_nameval	*nv)
228 {
229 	struct xfs_attri_log_item	*attrip;
230 
231 	attrip = kmem_cache_zalloc(xfs_attri_cache, GFP_NOFS | __GFP_NOFAIL);
232 
233 	/*
234 	 * Grab an extra reference to the name/value buffer for this log item.
235 	 * The caller retains its own reference!
236 	 */
237 	attrip->attri_nameval = xfs_attri_log_nameval_get(nv);
238 	ASSERT(attrip->attri_nameval);
239 
240 	xfs_log_item_init(mp, &attrip->attri_item, XFS_LI_ATTRI,
241 			  &xfs_attri_item_ops);
242 	attrip->attri_format.alfi_id = (uintptr_t)(void *)attrip;
243 	atomic_set(&attrip->attri_refcount, 2);
244 
245 	return attrip;
246 }
247 
248 /*
249  * Copy an attr format buffer from the given buf, and into the destination attr
250  * format structure.
251  */
252 STATIC int
253 xfs_attri_copy_format(
254 	struct xfs_log_iovec		*buf,
255 	struct xfs_attri_log_format	*dst_attr_fmt)
256 {
257 	struct xfs_attri_log_format	*src_attr_fmt = buf->i_addr;
258 	size_t				len;
259 
260 	len = sizeof(struct xfs_attri_log_format);
261 	if (buf->i_len != len) {
262 		XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, NULL);
263 		return -EFSCORRUPTED;
264 	}
265 
266 	memcpy((char *)dst_attr_fmt, (char *)src_attr_fmt, len);
267 	return 0;
268 }
269 
270 static inline struct xfs_attrd_log_item *ATTRD_ITEM(struct xfs_log_item *lip)
271 {
272 	return container_of(lip, struct xfs_attrd_log_item, attrd_item);
273 }
274 
275 STATIC void
276 xfs_attrd_item_free(struct xfs_attrd_log_item *attrdp)
277 {
278 	kmem_free(attrdp->attrd_item.li_lv_shadow);
279 	kmem_cache_free(xfs_attrd_cache, attrdp);
280 }
281 
282 STATIC void
283 xfs_attrd_item_size(
284 	struct xfs_log_item		*lip,
285 	int				*nvecs,
286 	int				*nbytes)
287 {
288 	*nvecs += 1;
289 	*nbytes += sizeof(struct xfs_attrd_log_format);
290 }
291 
292 /*
293  * This is called to fill in the log iovecs for the given attrd log item. We use
294  * only 1 iovec for the attrd_format, and we point that at the attr_log_format
295  * structure embedded in the attrd item.
296  */
297 STATIC void
298 xfs_attrd_item_format(
299 	struct xfs_log_item	*lip,
300 	struct xfs_log_vec	*lv)
301 {
302 	struct xfs_attrd_log_item	*attrdp = ATTRD_ITEM(lip);
303 	struct xfs_log_iovec		*vecp = NULL;
304 
305 	attrdp->attrd_format.alfd_type = XFS_LI_ATTRD;
306 	attrdp->attrd_format.alfd_size = 1;
307 
308 	xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_ATTRD_FORMAT,
309 			&attrdp->attrd_format,
310 			sizeof(struct xfs_attrd_log_format));
311 }
312 
313 /*
314  * The ATTRD is either committed or aborted if the transaction is canceled. If
315  * the transaction is canceled, drop our reference to the ATTRI and free the
316  * ATTRD.
317  */
318 STATIC void
319 xfs_attrd_item_release(
320 	struct xfs_log_item		*lip)
321 {
322 	struct xfs_attrd_log_item	*attrdp = ATTRD_ITEM(lip);
323 
324 	xfs_attri_release(attrdp->attrd_attrip);
325 	xfs_attrd_item_free(attrdp);
326 }
327 
328 static struct xfs_log_item *
329 xfs_attrd_item_intent(
330 	struct xfs_log_item	*lip)
331 {
332 	return &ATTRD_ITEM(lip)->attrd_attrip->attri_item;
333 }
334 
335 /*
336  * Performs one step of an attribute update intent and marks the attrd item
337  * dirty..  An attr operation may be a set or a remove.  Note that the
338  * transaction is marked dirty regardless of whether the operation succeeds or
339  * fails to support the ATTRI/ATTRD lifecycle rules.
340  */
341 STATIC int
342 xfs_xattri_finish_update(
343 	struct xfs_attr_intent		*attr,
344 	struct xfs_attrd_log_item	*attrdp)
345 {
346 	struct xfs_da_args		*args = attr->xattri_da_args;
347 	int				error;
348 
349 	if (XFS_TEST_ERROR(false, args->dp->i_mount, XFS_ERRTAG_LARP)) {
350 		error = -EIO;
351 		goto out;
352 	}
353 
354 	error = xfs_attr_set_iter(attr);
355 	if (!error && attr->xattri_dela_state != XFS_DAS_DONE)
356 		error = -EAGAIN;
357 out:
358 	/*
359 	 * Mark the transaction dirty, even on error. This ensures the
360 	 * transaction is aborted, which:
361 	 *
362 	 * 1.) releases the ATTRI and frees the ATTRD
363 	 * 2.) shuts down the filesystem
364 	 */
365 	args->trans->t_flags |= XFS_TRANS_DIRTY | XFS_TRANS_HAS_INTENT_DONE;
366 
367 	/*
368 	 * attr intent/done items are null when logged attributes are disabled
369 	 */
370 	if (attrdp)
371 		set_bit(XFS_LI_DIRTY, &attrdp->attrd_item.li_flags);
372 
373 	return error;
374 }
375 
376 /* Log an attr to the intent item. */
377 STATIC void
378 xfs_attr_log_item(
379 	struct xfs_trans		*tp,
380 	struct xfs_attri_log_item	*attrip,
381 	const struct xfs_attr_intent	*attr)
382 {
383 	struct xfs_attri_log_format	*attrp;
384 
385 	tp->t_flags |= XFS_TRANS_DIRTY;
386 	set_bit(XFS_LI_DIRTY, &attrip->attri_item.li_flags);
387 
388 	/*
389 	 * At this point the xfs_attr_intent has been constructed, and we've
390 	 * created the log intent. Fill in the attri log item and log format
391 	 * structure with fields from this xfs_attr_intent
392 	 */
393 	attrp = &attrip->attri_format;
394 	attrp->alfi_ino = attr->xattri_da_args->dp->i_ino;
395 	ASSERT(!(attr->xattri_op_flags & ~XFS_ATTRI_OP_FLAGS_TYPE_MASK));
396 	attrp->alfi_op_flags = attr->xattri_op_flags;
397 	attrp->alfi_value_len = attr->xattri_nameval->value.i_len;
398 	attrp->alfi_name_len = attr->xattri_nameval->name.i_len;
399 	ASSERT(!(attr->xattri_da_args->attr_filter & ~XFS_ATTRI_FILTER_MASK));
400 	attrp->alfi_attr_filter = attr->xattri_da_args->attr_filter;
401 }
402 
403 /* Get an ATTRI. */
404 static struct xfs_log_item *
405 xfs_attr_create_intent(
406 	struct xfs_trans		*tp,
407 	struct list_head		*items,
408 	unsigned int			count,
409 	bool				sort)
410 {
411 	struct xfs_mount		*mp = tp->t_mountp;
412 	struct xfs_attri_log_item	*attrip;
413 	struct xfs_attr_intent		*attr;
414 	struct xfs_da_args		*args;
415 
416 	ASSERT(count == 1);
417 
418 	/*
419 	 * Each attr item only performs one attribute operation at a time, so
420 	 * this is a list of one
421 	 */
422 	attr = list_first_entry_or_null(items, struct xfs_attr_intent,
423 			xattri_list);
424 	args = attr->xattri_da_args;
425 
426 	if (!(args->op_flags & XFS_DA_OP_LOGGED))
427 		return NULL;
428 
429 	/*
430 	 * Create a buffer to store the attribute name and value.  This buffer
431 	 * will be shared between the higher level deferred xattr work state
432 	 * and the lower level xattr log items.
433 	 */
434 	if (!attr->xattri_nameval) {
435 		/*
436 		 * Transfer our reference to the name/value buffer to the
437 		 * deferred work state structure.
438 		 */
439 		attr->xattri_nameval = xfs_attri_log_nameval_alloc(args->name,
440 				args->namelen, args->value, args->valuelen);
441 	}
442 
443 	attrip = xfs_attri_init(mp, attr->xattri_nameval);
444 	xfs_trans_add_item(tp, &attrip->attri_item);
445 	xfs_attr_log_item(tp, attrip, attr);
446 
447 	return &attrip->attri_item;
448 }
449 
450 static inline void
451 xfs_attr_free_item(
452 	struct xfs_attr_intent		*attr)
453 {
454 	if (attr->xattri_da_state)
455 		xfs_da_state_free(attr->xattri_da_state);
456 	xfs_attri_log_nameval_put(attr->xattri_nameval);
457 	if (attr->xattri_da_args->op_flags & XFS_DA_OP_RECOVERY)
458 		kmem_free(attr);
459 	else
460 		kmem_cache_free(xfs_attr_intent_cache, attr);
461 }
462 
463 /* Process an attr. */
464 STATIC int
465 xfs_attr_finish_item(
466 	struct xfs_trans		*tp,
467 	struct xfs_log_item		*done,
468 	struct list_head		*item,
469 	struct xfs_btree_cur		**state)
470 {
471 	struct xfs_attr_intent		*attr;
472 	struct xfs_attrd_log_item	*done_item = NULL;
473 	int				error;
474 
475 	attr = container_of(item, struct xfs_attr_intent, xattri_list);
476 	if (done)
477 		done_item = ATTRD_ITEM(done);
478 
479 	/*
480 	 * Always reset trans after EAGAIN cycle
481 	 * since the transaction is new
482 	 */
483 	attr->xattri_da_args->trans = tp;
484 
485 	error = xfs_xattri_finish_update(attr, done_item);
486 	if (error != -EAGAIN)
487 		xfs_attr_free_item(attr);
488 
489 	return error;
490 }
491 
492 /* Abort all pending ATTRs. */
493 STATIC void
494 xfs_attr_abort_intent(
495 	struct xfs_log_item		*intent)
496 {
497 	xfs_attri_release(ATTRI_ITEM(intent));
498 }
499 
500 /* Cancel an attr */
501 STATIC void
502 xfs_attr_cancel_item(
503 	struct list_head		*item)
504 {
505 	struct xfs_attr_intent		*attr;
506 
507 	attr = container_of(item, struct xfs_attr_intent, xattri_list);
508 	xfs_attr_free_item(attr);
509 }
510 
511 STATIC bool
512 xfs_attri_item_match(
513 	struct xfs_log_item	*lip,
514 	uint64_t		intent_id)
515 {
516 	return ATTRI_ITEM(lip)->attri_format.alfi_id == intent_id;
517 }
518 
519 /* Is this recovered ATTRI format ok? */
520 static inline bool
521 xfs_attri_validate(
522 	struct xfs_mount		*mp,
523 	struct xfs_attri_log_format	*attrp)
524 {
525 	unsigned int			op = attrp->alfi_op_flags &
526 					     XFS_ATTRI_OP_FLAGS_TYPE_MASK;
527 
528 	if (attrp->__pad != 0)
529 		return false;
530 
531 	if (attrp->alfi_op_flags & ~XFS_ATTRI_OP_FLAGS_TYPE_MASK)
532 		return false;
533 
534 	if (attrp->alfi_attr_filter & ~XFS_ATTRI_FILTER_MASK)
535 		return false;
536 
537 	/* alfi_op_flags should be either a set or remove */
538 	switch (op) {
539 	case XFS_ATTRI_OP_FLAGS_SET:
540 	case XFS_ATTRI_OP_FLAGS_REPLACE:
541 	case XFS_ATTRI_OP_FLAGS_REMOVE:
542 		break;
543 	default:
544 		return false;
545 	}
546 
547 	if (attrp->alfi_value_len > XATTR_SIZE_MAX)
548 		return false;
549 
550 	if ((attrp->alfi_name_len > XATTR_NAME_MAX) ||
551 	    (attrp->alfi_name_len == 0))
552 		return false;
553 
554 	return xfs_verify_ino(mp, attrp->alfi_ino);
555 }
556 
557 /*
558  * Process an attr intent item that was recovered from the log.  We need to
559  * delete the attr that it describes.
560  */
561 STATIC int
562 xfs_attri_item_recover(
563 	struct xfs_log_item		*lip,
564 	struct list_head		*capture_list)
565 {
566 	struct xfs_attri_log_item	*attrip = ATTRI_ITEM(lip);
567 	struct xfs_attr_intent		*attr;
568 	struct xfs_mount		*mp = lip->li_log->l_mp;
569 	struct xfs_inode		*ip;
570 	struct xfs_da_args		*args;
571 	struct xfs_trans		*tp;
572 	struct xfs_trans_res		tres;
573 	struct xfs_attri_log_format	*attrp;
574 	struct xfs_attri_log_nameval	*nv = attrip->attri_nameval;
575 	int				error;
576 	int				total;
577 	int				local;
578 	struct xfs_attrd_log_item	*done_item = NULL;
579 
580 	/*
581 	 * First check the validity of the attr described by the ATTRI.  If any
582 	 * are bad, then assume that all are bad and just toss the ATTRI.
583 	 */
584 	attrp = &attrip->attri_format;
585 	if (!xfs_attri_validate(mp, attrp) ||
586 	    !xfs_attr_namecheck(nv->name.i_addr, nv->name.i_len))
587 		return -EFSCORRUPTED;
588 
589 	error = xlog_recover_iget(mp,  attrp->alfi_ino, &ip);
590 	if (error)
591 		return error;
592 
593 	attr = kmem_zalloc(sizeof(struct xfs_attr_intent) +
594 			   sizeof(struct xfs_da_args), KM_NOFS);
595 	args = (struct xfs_da_args *)(attr + 1);
596 
597 	attr->xattri_da_args = args;
598 	attr->xattri_op_flags = attrp->alfi_op_flags &
599 						XFS_ATTRI_OP_FLAGS_TYPE_MASK;
600 
601 	/*
602 	 * We're reconstructing the deferred work state structure from the
603 	 * recovered log item.  Grab a reference to the name/value buffer and
604 	 * attach it to the new work state.
605 	 */
606 	attr->xattri_nameval = xfs_attri_log_nameval_get(nv);
607 	ASSERT(attr->xattri_nameval);
608 
609 	args->dp = ip;
610 	args->geo = mp->m_attr_geo;
611 	args->whichfork = XFS_ATTR_FORK;
612 	args->name = nv->name.i_addr;
613 	args->namelen = nv->name.i_len;
614 	args->hashval = xfs_da_hashname(args->name, args->namelen);
615 	args->attr_filter = attrp->alfi_attr_filter & XFS_ATTRI_FILTER_MASK;
616 	args->op_flags = XFS_DA_OP_RECOVERY | XFS_DA_OP_OKNOENT |
617 			 XFS_DA_OP_LOGGED;
618 
619 	ASSERT(xfs_sb_version_haslogxattrs(&mp->m_sb));
620 
621 	switch (attr->xattri_op_flags) {
622 	case XFS_ATTRI_OP_FLAGS_SET:
623 	case XFS_ATTRI_OP_FLAGS_REPLACE:
624 		args->value = nv->value.i_addr;
625 		args->valuelen = nv->value.i_len;
626 		args->total = xfs_attr_calc_size(args, &local);
627 		if (xfs_inode_hasattr(args->dp))
628 			attr->xattri_dela_state = xfs_attr_init_replace_state(args);
629 		else
630 			attr->xattri_dela_state = xfs_attr_init_add_state(args);
631 		break;
632 	case XFS_ATTRI_OP_FLAGS_REMOVE:
633 		if (!xfs_inode_hasattr(args->dp))
634 			goto out;
635 		attr->xattri_dela_state = xfs_attr_init_remove_state(args);
636 		break;
637 	default:
638 		ASSERT(0);
639 		error = -EFSCORRUPTED;
640 		goto out;
641 	}
642 
643 	xfs_init_attr_trans(args, &tres, &total);
644 	error = xfs_trans_alloc(mp, &tres, total, 0, XFS_TRANS_RESERVE, &tp);
645 	if (error)
646 		goto out;
647 
648 	args->trans = tp;
649 	done_item = xfs_trans_get_attrd(tp, attrip);
650 
651 	xfs_ilock(ip, XFS_ILOCK_EXCL);
652 	xfs_trans_ijoin(tp, ip, 0);
653 
654 	error = xfs_xattri_finish_update(attr, done_item);
655 	if (error == -EAGAIN) {
656 		/*
657 		 * There's more work to do, so add the intent item to this
658 		 * transaction so that we can continue it later.
659 		 */
660 		xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_ATTR, &attr->xattri_list);
661 		error = xfs_defer_ops_capture_and_commit(tp, capture_list);
662 		if (error)
663 			goto out_unlock;
664 
665 		xfs_iunlock(ip, XFS_ILOCK_EXCL);
666 		xfs_irele(ip);
667 		return 0;
668 	}
669 	if (error) {
670 		xfs_trans_cancel(tp);
671 		goto out_unlock;
672 	}
673 
674 	error = xfs_defer_ops_capture_and_commit(tp, capture_list);
675 out_unlock:
676 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
677 	xfs_irele(ip);
678 out:
679 	xfs_attr_free_item(attr);
680 	return error;
681 }
682 
683 /* Re-log an intent item to push the log tail forward. */
684 static struct xfs_log_item *
685 xfs_attri_item_relog(
686 	struct xfs_log_item		*intent,
687 	struct xfs_trans		*tp)
688 {
689 	struct xfs_attrd_log_item	*attrdp;
690 	struct xfs_attri_log_item	*old_attrip;
691 	struct xfs_attri_log_item	*new_attrip;
692 	struct xfs_attri_log_format	*new_attrp;
693 	struct xfs_attri_log_format	*old_attrp;
694 
695 	old_attrip = ATTRI_ITEM(intent);
696 	old_attrp = &old_attrip->attri_format;
697 
698 	tp->t_flags |= XFS_TRANS_DIRTY;
699 	attrdp = xfs_trans_get_attrd(tp, old_attrip);
700 	set_bit(XFS_LI_DIRTY, &attrdp->attrd_item.li_flags);
701 
702 	/*
703 	 * Create a new log item that shares the same name/value buffer as the
704 	 * old log item.
705 	 */
706 	new_attrip = xfs_attri_init(tp->t_mountp, old_attrip->attri_nameval);
707 	new_attrp = &new_attrip->attri_format;
708 
709 	new_attrp->alfi_ino = old_attrp->alfi_ino;
710 	new_attrp->alfi_op_flags = old_attrp->alfi_op_flags;
711 	new_attrp->alfi_value_len = old_attrp->alfi_value_len;
712 	new_attrp->alfi_name_len = old_attrp->alfi_name_len;
713 	new_attrp->alfi_attr_filter = old_attrp->alfi_attr_filter;
714 
715 	xfs_trans_add_item(tp, &new_attrip->attri_item);
716 	set_bit(XFS_LI_DIRTY, &new_attrip->attri_item.li_flags);
717 
718 	return &new_attrip->attri_item;
719 }
720 
721 STATIC int
722 xlog_recover_attri_commit_pass2(
723 	struct xlog                     *log,
724 	struct list_head		*buffer_list,
725 	struct xlog_recover_item        *item,
726 	xfs_lsn_t                       lsn)
727 {
728 	struct xfs_mount                *mp = log->l_mp;
729 	struct xfs_attri_log_item       *attrip;
730 	struct xfs_attri_log_format     *attri_formatp;
731 	struct xfs_attri_log_nameval	*nv;
732 	const void			*attr_value = NULL;
733 	const void			*attr_name;
734 	int                             error;
735 
736 	attri_formatp = item->ri_buf[0].i_addr;
737 	attr_name = item->ri_buf[1].i_addr;
738 
739 	/* Validate xfs_attri_log_format before the large memory allocation */
740 	if (!xfs_attri_validate(mp, attri_formatp)) {
741 		XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
742 		return -EFSCORRUPTED;
743 	}
744 
745 	if (!xfs_attr_namecheck(attr_name, attri_formatp->alfi_name_len)) {
746 		XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
747 		return -EFSCORRUPTED;
748 	}
749 
750 	if (attri_formatp->alfi_value_len)
751 		attr_value = item->ri_buf[2].i_addr;
752 
753 	/*
754 	 * Memory alloc failure will cause replay to abort.  We attach the
755 	 * name/value buffer to the recovered incore log item and drop our
756 	 * reference.
757 	 */
758 	nv = xfs_attri_log_nameval_alloc(attr_name,
759 			attri_formatp->alfi_name_len, attr_value,
760 			attri_formatp->alfi_value_len);
761 
762 	attrip = xfs_attri_init(mp, nv);
763 	error = xfs_attri_copy_format(&item->ri_buf[0], &attrip->attri_format);
764 	if (error)
765 		goto out;
766 
767 	/*
768 	 * The ATTRI has two references. One for the ATTRD and one for ATTRI to
769 	 * ensure it makes it into the AIL. Insert the ATTRI into the AIL
770 	 * directly and drop the ATTRI reference. Note that
771 	 * xfs_trans_ail_update() drops the AIL lock.
772 	 */
773 	xfs_trans_ail_insert(log->l_ailp, &attrip->attri_item, lsn);
774 	xfs_attri_release(attrip);
775 	xfs_attri_log_nameval_put(nv);
776 	return 0;
777 out:
778 	xfs_attri_item_free(attrip);
779 	xfs_attri_log_nameval_put(nv);
780 	return error;
781 }
782 
783 /*
784  * This routine is called to allocate an "attr free done" log item.
785  */
786 static struct xfs_attrd_log_item *
787 xfs_trans_get_attrd(struct xfs_trans		*tp,
788 		  struct xfs_attri_log_item	*attrip)
789 {
790 	struct xfs_attrd_log_item		*attrdp;
791 
792 	ASSERT(tp != NULL);
793 
794 	attrdp = kmem_cache_zalloc(xfs_attrd_cache, GFP_NOFS | __GFP_NOFAIL);
795 
796 	xfs_log_item_init(tp->t_mountp, &attrdp->attrd_item, XFS_LI_ATTRD,
797 			  &xfs_attrd_item_ops);
798 	attrdp->attrd_attrip = attrip;
799 	attrdp->attrd_format.alfd_alf_id = attrip->attri_format.alfi_id;
800 
801 	xfs_trans_add_item(tp, &attrdp->attrd_item);
802 	return attrdp;
803 }
804 
805 /* Get an ATTRD so we can process all the attrs. */
806 static struct xfs_log_item *
807 xfs_attr_create_done(
808 	struct xfs_trans		*tp,
809 	struct xfs_log_item		*intent,
810 	unsigned int			count)
811 {
812 	if (!intent)
813 		return NULL;
814 
815 	return &xfs_trans_get_attrd(tp, ATTRI_ITEM(intent))->attrd_item;
816 }
817 
818 const struct xfs_defer_op_type xfs_attr_defer_type = {
819 	.max_items	= 1,
820 	.create_intent	= xfs_attr_create_intent,
821 	.abort_intent	= xfs_attr_abort_intent,
822 	.create_done	= xfs_attr_create_done,
823 	.finish_item	= xfs_attr_finish_item,
824 	.cancel_item	= xfs_attr_cancel_item,
825 };
826 
827 /*
828  * This routine is called when an ATTRD format structure is found in a committed
829  * transaction in the log. Its purpose is to cancel the corresponding ATTRI if
830  * it was still in the log. To do this it searches the AIL for the ATTRI with
831  * an id equal to that in the ATTRD format structure. If we find it we drop
832  * the ATTRD reference, which removes the ATTRI from the AIL and frees it.
833  */
834 STATIC int
835 xlog_recover_attrd_commit_pass2(
836 	struct xlog			*log,
837 	struct list_head		*buffer_list,
838 	struct xlog_recover_item	*item,
839 	xfs_lsn_t			lsn)
840 {
841 	struct xfs_attrd_log_format	*attrd_formatp;
842 
843 	attrd_formatp = item->ri_buf[0].i_addr;
844 	if (item->ri_buf[0].i_len != sizeof(struct xfs_attrd_log_format)) {
845 		XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, NULL);
846 		return -EFSCORRUPTED;
847 	}
848 
849 	xlog_recover_release_intent(log, XFS_LI_ATTRI,
850 				    attrd_formatp->alfd_alf_id);
851 	return 0;
852 }
853 
854 static const struct xfs_item_ops xfs_attri_item_ops = {
855 	.flags		= XFS_ITEM_INTENT,
856 	.iop_size	= xfs_attri_item_size,
857 	.iop_format	= xfs_attri_item_format,
858 	.iop_unpin	= xfs_attri_item_unpin,
859 	.iop_release    = xfs_attri_item_release,
860 	.iop_recover	= xfs_attri_item_recover,
861 	.iop_match	= xfs_attri_item_match,
862 	.iop_relog	= xfs_attri_item_relog,
863 };
864 
865 const struct xlog_recover_item_ops xlog_attri_item_ops = {
866 	.item_type	= XFS_LI_ATTRI,
867 	.commit_pass2	= xlog_recover_attri_commit_pass2,
868 };
869 
870 static const struct xfs_item_ops xfs_attrd_item_ops = {
871 	.flags		= XFS_ITEM_RELEASE_WHEN_COMMITTED |
872 			  XFS_ITEM_INTENT_DONE,
873 	.iop_size	= xfs_attrd_item_size,
874 	.iop_format	= xfs_attrd_item_format,
875 	.iop_release    = xfs_attrd_item_release,
876 	.iop_intent	= xfs_attrd_item_intent,
877 };
878 
879 const struct xlog_recover_item_ops xlog_attrd_item_ops = {
880 	.item_type	= XFS_LI_ATTRD,
881 	.commit_pass2	= xlog_recover_attrd_commit_pass2,
882 };
883