Linux Audio

Check our new training course

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