xref: /openbmc/linux/fs/xfs/xfs_inode_item.c (revision aadaeca4)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2000-2002,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_inode.h"
14 #include "xfs_trans.h"
15 #include "xfs_inode_item.h"
16 #include "xfs_trace.h"
17 #include "xfs_trans_priv.h"
18 #include "xfs_buf_item.h"
19 #include "xfs_log.h"
20 #include "xfs_log_priv.h"
21 #include "xfs_error.h"
22 
23 #include <linux/iversion.h>
24 
25 struct kmem_cache	*xfs_ili_cache;		/* inode log item */
26 
27 static inline struct xfs_inode_log_item *INODE_ITEM(struct xfs_log_item *lip)
28 {
29 	return container_of(lip, struct xfs_inode_log_item, ili_item);
30 }
31 
32 /*
33  * The logged size of an inode fork is always the current size of the inode
34  * fork. This means that when an inode fork is relogged, the size of the logged
35  * region is determined by the current state, not the combination of the
36  * previously logged state + the current state. This is different relogging
37  * behaviour to most other log items which will retain the size of the
38  * previously logged changes when smaller regions are relogged.
39  *
40  * Hence operations that remove data from the inode fork (e.g. shortform
41  * dir/attr remove, extent form extent removal, etc), the size of the relogged
42  * inode gets -smaller- rather than stays the same size as the previously logged
43  * size and this can result in the committing transaction reducing the amount of
44  * space being consumed by the CIL.
45  */
46 STATIC void
47 xfs_inode_item_data_fork_size(
48 	struct xfs_inode_log_item *iip,
49 	int			*nvecs,
50 	int			*nbytes)
51 {
52 	struct xfs_inode	*ip = iip->ili_inode;
53 
54 	switch (ip->i_df.if_format) {
55 	case XFS_DINODE_FMT_EXTENTS:
56 		if ((iip->ili_fields & XFS_ILOG_DEXT) &&
57 		    ip->i_df.if_nextents > 0 &&
58 		    ip->i_df.if_bytes > 0) {
59 			/* worst case, doesn't subtract delalloc extents */
60 			*nbytes += XFS_IFORK_DSIZE(ip);
61 			*nvecs += 1;
62 		}
63 		break;
64 	case XFS_DINODE_FMT_BTREE:
65 		if ((iip->ili_fields & XFS_ILOG_DBROOT) &&
66 		    ip->i_df.if_broot_bytes > 0) {
67 			*nbytes += ip->i_df.if_broot_bytes;
68 			*nvecs += 1;
69 		}
70 		break;
71 	case XFS_DINODE_FMT_LOCAL:
72 		if ((iip->ili_fields & XFS_ILOG_DDATA) &&
73 		    ip->i_df.if_bytes > 0) {
74 			*nbytes += roundup(ip->i_df.if_bytes, 4);
75 			*nvecs += 1;
76 		}
77 		break;
78 
79 	case XFS_DINODE_FMT_DEV:
80 		break;
81 	default:
82 		ASSERT(0);
83 		break;
84 	}
85 }
86 
87 STATIC void
88 xfs_inode_item_attr_fork_size(
89 	struct xfs_inode_log_item *iip,
90 	int			*nvecs,
91 	int			*nbytes)
92 {
93 	struct xfs_inode	*ip = iip->ili_inode;
94 
95 	switch (ip->i_afp->if_format) {
96 	case XFS_DINODE_FMT_EXTENTS:
97 		if ((iip->ili_fields & XFS_ILOG_AEXT) &&
98 		    ip->i_afp->if_nextents > 0 &&
99 		    ip->i_afp->if_bytes > 0) {
100 			/* worst case, doesn't subtract unused space */
101 			*nbytes += XFS_IFORK_ASIZE(ip);
102 			*nvecs += 1;
103 		}
104 		break;
105 	case XFS_DINODE_FMT_BTREE:
106 		if ((iip->ili_fields & XFS_ILOG_ABROOT) &&
107 		    ip->i_afp->if_broot_bytes > 0) {
108 			*nbytes += ip->i_afp->if_broot_bytes;
109 			*nvecs += 1;
110 		}
111 		break;
112 	case XFS_DINODE_FMT_LOCAL:
113 		if ((iip->ili_fields & XFS_ILOG_ADATA) &&
114 		    ip->i_afp->if_bytes > 0) {
115 			*nbytes += roundup(ip->i_afp->if_bytes, 4);
116 			*nvecs += 1;
117 		}
118 		break;
119 	default:
120 		ASSERT(0);
121 		break;
122 	}
123 }
124 
125 /*
126  * This returns the number of iovecs needed to log the given inode item.
127  *
128  * We need one iovec for the inode log format structure, one for the
129  * inode core, and possibly one for the inode data/extents/b-tree root
130  * and one for the inode attribute data/extents/b-tree root.
131  */
132 STATIC void
133 xfs_inode_item_size(
134 	struct xfs_log_item	*lip,
135 	int			*nvecs,
136 	int			*nbytes)
137 {
138 	struct xfs_inode_log_item *iip = INODE_ITEM(lip);
139 	struct xfs_inode	*ip = iip->ili_inode;
140 
141 	*nvecs += 2;
142 	*nbytes += sizeof(struct xfs_inode_log_format) +
143 		   xfs_log_dinode_size(ip->i_mount);
144 
145 	xfs_inode_item_data_fork_size(iip, nvecs, nbytes);
146 	if (XFS_IFORK_Q(ip))
147 		xfs_inode_item_attr_fork_size(iip, nvecs, nbytes);
148 }
149 
150 STATIC void
151 xfs_inode_item_format_data_fork(
152 	struct xfs_inode_log_item *iip,
153 	struct xfs_inode_log_format *ilf,
154 	struct xfs_log_vec	*lv,
155 	struct xfs_log_iovec	**vecp)
156 {
157 	struct xfs_inode	*ip = iip->ili_inode;
158 	size_t			data_bytes;
159 
160 	switch (ip->i_df.if_format) {
161 	case XFS_DINODE_FMT_EXTENTS:
162 		iip->ili_fields &=
163 			~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT | XFS_ILOG_DEV);
164 
165 		if ((iip->ili_fields & XFS_ILOG_DEXT) &&
166 		    ip->i_df.if_nextents > 0 &&
167 		    ip->i_df.if_bytes > 0) {
168 			struct xfs_bmbt_rec *p;
169 
170 			ASSERT(xfs_iext_count(&ip->i_df) > 0);
171 
172 			p = xlog_prepare_iovec(lv, vecp, XLOG_REG_TYPE_IEXT);
173 			data_bytes = xfs_iextents_copy(ip, p, XFS_DATA_FORK);
174 			xlog_finish_iovec(lv, *vecp, data_bytes);
175 
176 			ASSERT(data_bytes <= ip->i_df.if_bytes);
177 
178 			ilf->ilf_dsize = data_bytes;
179 			ilf->ilf_size++;
180 		} else {
181 			iip->ili_fields &= ~XFS_ILOG_DEXT;
182 		}
183 		break;
184 	case XFS_DINODE_FMT_BTREE:
185 		iip->ili_fields &=
186 			~(XFS_ILOG_DDATA | XFS_ILOG_DEXT | XFS_ILOG_DEV);
187 
188 		if ((iip->ili_fields & XFS_ILOG_DBROOT) &&
189 		    ip->i_df.if_broot_bytes > 0) {
190 			ASSERT(ip->i_df.if_broot != NULL);
191 			xlog_copy_iovec(lv, vecp, XLOG_REG_TYPE_IBROOT,
192 					ip->i_df.if_broot,
193 					ip->i_df.if_broot_bytes);
194 			ilf->ilf_dsize = ip->i_df.if_broot_bytes;
195 			ilf->ilf_size++;
196 		} else {
197 			ASSERT(!(iip->ili_fields &
198 				 XFS_ILOG_DBROOT));
199 			iip->ili_fields &= ~XFS_ILOG_DBROOT;
200 		}
201 		break;
202 	case XFS_DINODE_FMT_LOCAL:
203 		iip->ili_fields &=
204 			~(XFS_ILOG_DEXT | XFS_ILOG_DBROOT | XFS_ILOG_DEV);
205 		if ((iip->ili_fields & XFS_ILOG_DDATA) &&
206 		    ip->i_df.if_bytes > 0) {
207 			/*
208 			 * Round i_bytes up to a word boundary.
209 			 * The underlying memory is guaranteed
210 			 * to be there by xfs_idata_realloc().
211 			 */
212 			data_bytes = roundup(ip->i_df.if_bytes, 4);
213 			ASSERT(ip->i_df.if_u1.if_data != NULL);
214 			ASSERT(ip->i_disk_size > 0);
215 			xlog_copy_iovec(lv, vecp, XLOG_REG_TYPE_ILOCAL,
216 					ip->i_df.if_u1.if_data, data_bytes);
217 			ilf->ilf_dsize = (unsigned)data_bytes;
218 			ilf->ilf_size++;
219 		} else {
220 			iip->ili_fields &= ~XFS_ILOG_DDATA;
221 		}
222 		break;
223 	case XFS_DINODE_FMT_DEV:
224 		iip->ili_fields &=
225 			~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT | XFS_ILOG_DEXT);
226 		if (iip->ili_fields & XFS_ILOG_DEV)
227 			ilf->ilf_u.ilfu_rdev = sysv_encode_dev(VFS_I(ip)->i_rdev);
228 		break;
229 	default:
230 		ASSERT(0);
231 		break;
232 	}
233 }
234 
235 STATIC void
236 xfs_inode_item_format_attr_fork(
237 	struct xfs_inode_log_item *iip,
238 	struct xfs_inode_log_format *ilf,
239 	struct xfs_log_vec	*lv,
240 	struct xfs_log_iovec	**vecp)
241 {
242 	struct xfs_inode	*ip = iip->ili_inode;
243 	size_t			data_bytes;
244 
245 	switch (ip->i_afp->if_format) {
246 	case XFS_DINODE_FMT_EXTENTS:
247 		iip->ili_fields &=
248 			~(XFS_ILOG_ADATA | XFS_ILOG_ABROOT);
249 
250 		if ((iip->ili_fields & XFS_ILOG_AEXT) &&
251 		    ip->i_afp->if_nextents > 0 &&
252 		    ip->i_afp->if_bytes > 0) {
253 			struct xfs_bmbt_rec *p;
254 
255 			ASSERT(xfs_iext_count(ip->i_afp) ==
256 				ip->i_afp->if_nextents);
257 
258 			p = xlog_prepare_iovec(lv, vecp, XLOG_REG_TYPE_IATTR_EXT);
259 			data_bytes = xfs_iextents_copy(ip, p, XFS_ATTR_FORK);
260 			xlog_finish_iovec(lv, *vecp, data_bytes);
261 
262 			ilf->ilf_asize = data_bytes;
263 			ilf->ilf_size++;
264 		} else {
265 			iip->ili_fields &= ~XFS_ILOG_AEXT;
266 		}
267 		break;
268 	case XFS_DINODE_FMT_BTREE:
269 		iip->ili_fields &=
270 			~(XFS_ILOG_ADATA | XFS_ILOG_AEXT);
271 
272 		if ((iip->ili_fields & XFS_ILOG_ABROOT) &&
273 		    ip->i_afp->if_broot_bytes > 0) {
274 			ASSERT(ip->i_afp->if_broot != NULL);
275 
276 			xlog_copy_iovec(lv, vecp, XLOG_REG_TYPE_IATTR_BROOT,
277 					ip->i_afp->if_broot,
278 					ip->i_afp->if_broot_bytes);
279 			ilf->ilf_asize = ip->i_afp->if_broot_bytes;
280 			ilf->ilf_size++;
281 		} else {
282 			iip->ili_fields &= ~XFS_ILOG_ABROOT;
283 		}
284 		break;
285 	case XFS_DINODE_FMT_LOCAL:
286 		iip->ili_fields &=
287 			~(XFS_ILOG_AEXT | XFS_ILOG_ABROOT);
288 
289 		if ((iip->ili_fields & XFS_ILOG_ADATA) &&
290 		    ip->i_afp->if_bytes > 0) {
291 			/*
292 			 * Round i_bytes up to a word boundary.
293 			 * The underlying memory is guaranteed
294 			 * to be there by xfs_idata_realloc().
295 			 */
296 			data_bytes = roundup(ip->i_afp->if_bytes, 4);
297 			ASSERT(ip->i_afp->if_u1.if_data != NULL);
298 			xlog_copy_iovec(lv, vecp, XLOG_REG_TYPE_IATTR_LOCAL,
299 					ip->i_afp->if_u1.if_data,
300 					data_bytes);
301 			ilf->ilf_asize = (unsigned)data_bytes;
302 			ilf->ilf_size++;
303 		} else {
304 			iip->ili_fields &= ~XFS_ILOG_ADATA;
305 		}
306 		break;
307 	default:
308 		ASSERT(0);
309 		break;
310 	}
311 }
312 
313 /*
314  * Convert an incore timestamp to a log timestamp.  Note that the log format
315  * specifies host endian format!
316  */
317 static inline xfs_log_timestamp_t
318 xfs_inode_to_log_dinode_ts(
319 	struct xfs_inode		*ip,
320 	const struct timespec64		tv)
321 {
322 	struct xfs_log_legacy_timestamp	*lits;
323 	xfs_log_timestamp_t		its;
324 
325 	if (xfs_inode_has_bigtime(ip))
326 		return xfs_inode_encode_bigtime(tv);
327 
328 	lits = (struct xfs_log_legacy_timestamp *)&its;
329 	lits->t_sec = tv.tv_sec;
330 	lits->t_nsec = tv.tv_nsec;
331 
332 	return its;
333 }
334 
335 /*
336  * The legacy DMAPI fields are only present in the on-disk and in-log inodes,
337  * but not in the in-memory one.  But we are guaranteed to have an inode buffer
338  * in memory when logging an inode, so we can just copy it from the on-disk
339  * inode to the in-log inode here so that recovery of file system with these
340  * fields set to non-zero values doesn't lose them.  For all other cases we zero
341  * the fields.
342  */
343 static void
344 xfs_copy_dm_fields_to_log_dinode(
345 	struct xfs_inode	*ip,
346 	struct xfs_log_dinode	*to)
347 {
348 	struct xfs_dinode	*dip;
349 
350 	dip = xfs_buf_offset(ip->i_itemp->ili_item.li_buf,
351 			     ip->i_imap.im_boffset);
352 
353 	if (xfs_iflags_test(ip, XFS_IPRESERVE_DM_FIELDS)) {
354 		to->di_dmevmask = be32_to_cpu(dip->di_dmevmask);
355 		to->di_dmstate = be16_to_cpu(dip->di_dmstate);
356 	} else {
357 		to->di_dmevmask = 0;
358 		to->di_dmstate = 0;
359 	}
360 }
361 
362 static void
363 xfs_inode_to_log_dinode(
364 	struct xfs_inode	*ip,
365 	struct xfs_log_dinode	*to,
366 	xfs_lsn_t		lsn)
367 {
368 	struct inode		*inode = VFS_I(ip);
369 
370 	to->di_magic = XFS_DINODE_MAGIC;
371 	to->di_format = xfs_ifork_format(&ip->i_df);
372 	to->di_uid = i_uid_read(inode);
373 	to->di_gid = i_gid_read(inode);
374 	to->di_projid_lo = ip->i_projid & 0xffff;
375 	to->di_projid_hi = ip->i_projid >> 16;
376 
377 	memset(to->di_pad, 0, sizeof(to->di_pad));
378 	memset(to->di_pad3, 0, sizeof(to->di_pad3));
379 	to->di_atime = xfs_inode_to_log_dinode_ts(ip, inode->i_atime);
380 	to->di_mtime = xfs_inode_to_log_dinode_ts(ip, inode->i_mtime);
381 	to->di_ctime = xfs_inode_to_log_dinode_ts(ip, inode->i_ctime);
382 	to->di_nlink = inode->i_nlink;
383 	to->di_gen = inode->i_generation;
384 	to->di_mode = inode->i_mode;
385 
386 	to->di_size = ip->i_disk_size;
387 	to->di_nblocks = ip->i_nblocks;
388 	to->di_extsize = ip->i_extsize;
389 	to->di_nextents = xfs_ifork_nextents(&ip->i_df);
390 	to->di_anextents = xfs_ifork_nextents(ip->i_afp);
391 	to->di_forkoff = ip->i_forkoff;
392 	to->di_aformat = xfs_ifork_format(ip->i_afp);
393 	to->di_flags = ip->i_diflags;
394 
395 	xfs_copy_dm_fields_to_log_dinode(ip, to);
396 
397 	/* log a dummy value to ensure log structure is fully initialised */
398 	to->di_next_unlinked = NULLAGINO;
399 
400 	if (xfs_has_v3inodes(ip->i_mount)) {
401 		to->di_version = 3;
402 		to->di_changecount = inode_peek_iversion(inode);
403 		to->di_crtime = xfs_inode_to_log_dinode_ts(ip, ip->i_crtime);
404 		to->di_flags2 = ip->i_diflags2;
405 		to->di_cowextsize = ip->i_cowextsize;
406 		to->di_ino = ip->i_ino;
407 		to->di_lsn = lsn;
408 		memset(to->di_pad2, 0, sizeof(to->di_pad2));
409 		uuid_copy(&to->di_uuid, &ip->i_mount->m_sb.sb_meta_uuid);
410 		to->di_flushiter = 0;
411 	} else {
412 		to->di_version = 2;
413 		to->di_flushiter = ip->i_flushiter;
414 	}
415 }
416 
417 /*
418  * Format the inode core. Current timestamp data is only in the VFS inode
419  * fields, so we need to grab them from there. Hence rather than just copying
420  * the XFS inode core structure, format the fields directly into the iovec.
421  */
422 static void
423 xfs_inode_item_format_core(
424 	struct xfs_inode	*ip,
425 	struct xfs_log_vec	*lv,
426 	struct xfs_log_iovec	**vecp)
427 {
428 	struct xfs_log_dinode	*dic;
429 
430 	dic = xlog_prepare_iovec(lv, vecp, XLOG_REG_TYPE_ICORE);
431 	xfs_inode_to_log_dinode(ip, dic, ip->i_itemp->ili_item.li_lsn);
432 	xlog_finish_iovec(lv, *vecp, xfs_log_dinode_size(ip->i_mount));
433 }
434 
435 /*
436  * This is called to fill in the vector of log iovecs for the given inode
437  * log item.  It fills the first item with an inode log format structure,
438  * the second with the on-disk inode structure, and a possible third and/or
439  * fourth with the inode data/extents/b-tree root and inode attributes
440  * data/extents/b-tree root.
441  *
442  * Note: Always use the 64 bit inode log format structure so we don't
443  * leave an uninitialised hole in the format item on 64 bit systems. Log
444  * recovery on 32 bit systems handles this just fine, so there's no reason
445  * for not using an initialising the properly padded structure all the time.
446  */
447 STATIC void
448 xfs_inode_item_format(
449 	struct xfs_log_item	*lip,
450 	struct xfs_log_vec	*lv)
451 {
452 	struct xfs_inode_log_item *iip = INODE_ITEM(lip);
453 	struct xfs_inode	*ip = iip->ili_inode;
454 	struct xfs_log_iovec	*vecp = NULL;
455 	struct xfs_inode_log_format *ilf;
456 
457 	ilf = xlog_prepare_iovec(lv, &vecp, XLOG_REG_TYPE_IFORMAT);
458 	ilf->ilf_type = XFS_LI_INODE;
459 	ilf->ilf_ino = ip->i_ino;
460 	ilf->ilf_blkno = ip->i_imap.im_blkno;
461 	ilf->ilf_len = ip->i_imap.im_len;
462 	ilf->ilf_boffset = ip->i_imap.im_boffset;
463 	ilf->ilf_fields = XFS_ILOG_CORE;
464 	ilf->ilf_size = 2; /* format + core */
465 
466 	/*
467 	 * make sure we don't leak uninitialised data into the log in the case
468 	 * when we don't log every field in the inode.
469 	 */
470 	ilf->ilf_dsize = 0;
471 	ilf->ilf_asize = 0;
472 	ilf->ilf_pad = 0;
473 	memset(&ilf->ilf_u, 0, sizeof(ilf->ilf_u));
474 
475 	xlog_finish_iovec(lv, vecp, sizeof(*ilf));
476 
477 	xfs_inode_item_format_core(ip, lv, &vecp);
478 	xfs_inode_item_format_data_fork(iip, ilf, lv, &vecp);
479 	if (XFS_IFORK_Q(ip)) {
480 		xfs_inode_item_format_attr_fork(iip, ilf, lv, &vecp);
481 	} else {
482 		iip->ili_fields &=
483 			~(XFS_ILOG_ADATA | XFS_ILOG_ABROOT | XFS_ILOG_AEXT);
484 	}
485 
486 	/* update the format with the exact fields we actually logged */
487 	ilf->ilf_fields |= (iip->ili_fields & ~XFS_ILOG_TIMESTAMP);
488 }
489 
490 /*
491  * This is called to pin the inode associated with the inode log
492  * item in memory so it cannot be written out.
493  */
494 STATIC void
495 xfs_inode_item_pin(
496 	struct xfs_log_item	*lip)
497 {
498 	struct xfs_inode	*ip = INODE_ITEM(lip)->ili_inode;
499 
500 	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
501 	ASSERT(lip->li_buf);
502 
503 	trace_xfs_inode_pin(ip, _RET_IP_);
504 	atomic_inc(&ip->i_pincount);
505 }
506 
507 
508 /*
509  * This is called to unpin the inode associated with the inode log
510  * item which was previously pinned with a call to xfs_inode_item_pin().
511  *
512  * Also wake up anyone in xfs_iunpin_wait() if the count goes to 0.
513  *
514  * Note that unpin can race with inode cluster buffer freeing marking the buffer
515  * stale. In that case, flush completions are run from the buffer unpin call,
516  * which may happen before the inode is unpinned. If we lose the race, there
517  * will be no buffer attached to the log item, but the inode will be marked
518  * XFS_ISTALE.
519  */
520 STATIC void
521 xfs_inode_item_unpin(
522 	struct xfs_log_item	*lip,
523 	int			remove)
524 {
525 	struct xfs_inode	*ip = INODE_ITEM(lip)->ili_inode;
526 
527 	trace_xfs_inode_unpin(ip, _RET_IP_);
528 	ASSERT(lip->li_buf || xfs_iflags_test(ip, XFS_ISTALE));
529 	ASSERT(atomic_read(&ip->i_pincount) > 0);
530 	if (atomic_dec_and_test(&ip->i_pincount))
531 		wake_up_bit(&ip->i_flags, __XFS_IPINNED_BIT);
532 }
533 
534 STATIC uint
535 xfs_inode_item_push(
536 	struct xfs_log_item	*lip,
537 	struct list_head	*buffer_list)
538 		__releases(&lip->li_ailp->ail_lock)
539 		__acquires(&lip->li_ailp->ail_lock)
540 {
541 	struct xfs_inode_log_item *iip = INODE_ITEM(lip);
542 	struct xfs_inode	*ip = iip->ili_inode;
543 	struct xfs_buf		*bp = lip->li_buf;
544 	uint			rval = XFS_ITEM_SUCCESS;
545 	int			error;
546 
547 	if (!bp || (ip->i_flags & XFS_ISTALE)) {
548 		/*
549 		 * Inode item/buffer is being being aborted due to cluster
550 		 * buffer deletion. Trigger a log force to have that operation
551 		 * completed and items removed from the AIL before the next push
552 		 * attempt.
553 		 */
554 		return XFS_ITEM_PINNED;
555 	}
556 
557 	if (xfs_ipincount(ip) > 0 || xfs_buf_ispinned(bp))
558 		return XFS_ITEM_PINNED;
559 
560 	if (xfs_iflags_test(ip, XFS_IFLUSHING))
561 		return XFS_ITEM_FLUSHING;
562 
563 	if (!xfs_buf_trylock(bp))
564 		return XFS_ITEM_LOCKED;
565 
566 	spin_unlock(&lip->li_ailp->ail_lock);
567 
568 	/*
569 	 * We need to hold a reference for flushing the cluster buffer as it may
570 	 * fail the buffer without IO submission. In which case, we better get a
571 	 * reference for that completion because otherwise we don't get a
572 	 * reference for IO until we queue the buffer for delwri submission.
573 	 */
574 	xfs_buf_hold(bp);
575 	error = xfs_iflush_cluster(bp);
576 	if (!error) {
577 		if (!xfs_buf_delwri_queue(bp, buffer_list))
578 			rval = XFS_ITEM_FLUSHING;
579 		xfs_buf_relse(bp);
580 	} else {
581 		/*
582 		 * Release the buffer if we were unable to flush anything. On
583 		 * any other error, the buffer has already been released.
584 		 */
585 		if (error == -EAGAIN)
586 			xfs_buf_relse(bp);
587 		rval = XFS_ITEM_LOCKED;
588 	}
589 
590 	spin_lock(&lip->li_ailp->ail_lock);
591 	return rval;
592 }
593 
594 /*
595  * Unlock the inode associated with the inode log item.
596  */
597 STATIC void
598 xfs_inode_item_release(
599 	struct xfs_log_item	*lip)
600 {
601 	struct xfs_inode_log_item *iip = INODE_ITEM(lip);
602 	struct xfs_inode	*ip = iip->ili_inode;
603 	unsigned short		lock_flags;
604 
605 	ASSERT(ip->i_itemp != NULL);
606 	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
607 
608 	lock_flags = iip->ili_lock_flags;
609 	iip->ili_lock_flags = 0;
610 	if (lock_flags)
611 		xfs_iunlock(ip, lock_flags);
612 }
613 
614 /*
615  * This is called to find out where the oldest active copy of the inode log
616  * item in the on disk log resides now that the last log write of it completed
617  * at the given lsn.  Since we always re-log all dirty data in an inode, the
618  * latest copy in the on disk log is the only one that matters.  Therefore,
619  * simply return the given lsn.
620  *
621  * If the inode has been marked stale because the cluster is being freed, we
622  * don't want to (re-)insert this inode into the AIL. There is a race condition
623  * where the cluster buffer may be unpinned before the inode is inserted into
624  * the AIL during transaction committed processing. If the buffer is unpinned
625  * before the inode item has been committed and inserted, then it is possible
626  * for the buffer to be written and IO completes before the inode is inserted
627  * into the AIL. In that case, we'd be inserting a clean, stale inode into the
628  * AIL which will never get removed. It will, however, get reclaimed which
629  * triggers an assert in xfs_inode_free() complaining about freein an inode
630  * still in the AIL.
631  *
632  * To avoid this, just unpin the inode directly and return a LSN of -1 so the
633  * transaction committed code knows that it does not need to do any further
634  * processing on the item.
635  */
636 STATIC xfs_lsn_t
637 xfs_inode_item_committed(
638 	struct xfs_log_item	*lip,
639 	xfs_lsn_t		lsn)
640 {
641 	struct xfs_inode_log_item *iip = INODE_ITEM(lip);
642 	struct xfs_inode	*ip = iip->ili_inode;
643 
644 	if (xfs_iflags_test(ip, XFS_ISTALE)) {
645 		xfs_inode_item_unpin(lip, 0);
646 		return -1;
647 	}
648 	return lsn;
649 }
650 
651 STATIC void
652 xfs_inode_item_committing(
653 	struct xfs_log_item	*lip,
654 	xfs_csn_t		seq)
655 {
656 	INODE_ITEM(lip)->ili_commit_seq = seq;
657 	return xfs_inode_item_release(lip);
658 }
659 
660 static const struct xfs_item_ops xfs_inode_item_ops = {
661 	.iop_size	= xfs_inode_item_size,
662 	.iop_format	= xfs_inode_item_format,
663 	.iop_pin	= xfs_inode_item_pin,
664 	.iop_unpin	= xfs_inode_item_unpin,
665 	.iop_release	= xfs_inode_item_release,
666 	.iop_committed	= xfs_inode_item_committed,
667 	.iop_push	= xfs_inode_item_push,
668 	.iop_committing	= xfs_inode_item_committing,
669 };
670 
671 
672 /*
673  * Initialize the inode log item for a newly allocated (in-core) inode.
674  */
675 void
676 xfs_inode_item_init(
677 	struct xfs_inode	*ip,
678 	struct xfs_mount	*mp)
679 {
680 	struct xfs_inode_log_item *iip;
681 
682 	ASSERT(ip->i_itemp == NULL);
683 	iip = ip->i_itemp = kmem_cache_zalloc(xfs_ili_cache,
684 					      GFP_KERNEL | __GFP_NOFAIL);
685 
686 	iip->ili_inode = ip;
687 	spin_lock_init(&iip->ili_lock);
688 	xfs_log_item_init(mp, &iip->ili_item, XFS_LI_INODE,
689 						&xfs_inode_item_ops);
690 }
691 
692 /*
693  * Free the inode log item and any memory hanging off of it.
694  */
695 void
696 xfs_inode_item_destroy(
697 	struct xfs_inode	*ip)
698 {
699 	struct xfs_inode_log_item *iip = ip->i_itemp;
700 
701 	ASSERT(iip->ili_item.li_buf == NULL);
702 
703 	ip->i_itemp = NULL;
704 	kmem_free(iip->ili_item.li_lv_shadow);
705 	kmem_cache_free(xfs_ili_cache, iip);
706 }
707 
708 
709 /*
710  * We only want to pull the item from the AIL if it is actually there
711  * and its location in the log has not changed since we started the
712  * flush.  Thus, we only bother if the inode's lsn has not changed.
713  */
714 static void
715 xfs_iflush_ail_updates(
716 	struct xfs_ail		*ailp,
717 	struct list_head	*list)
718 {
719 	struct xfs_log_item	*lip;
720 	xfs_lsn_t		tail_lsn = 0;
721 
722 	/* this is an opencoded batch version of xfs_trans_ail_delete */
723 	spin_lock(&ailp->ail_lock);
724 	list_for_each_entry(lip, list, li_bio_list) {
725 		xfs_lsn_t	lsn;
726 
727 		clear_bit(XFS_LI_FAILED, &lip->li_flags);
728 		if (INODE_ITEM(lip)->ili_flush_lsn != lip->li_lsn)
729 			continue;
730 
731 		/*
732 		 * dgc: Not sure how this happens, but it happens very
733 		 * occassionaly via generic/388.  xfs_iflush_abort() also
734 		 * silently handles this same "under writeback but not in AIL at
735 		 * shutdown" condition via xfs_trans_ail_delete().
736 		 */
737 		if (!test_bit(XFS_LI_IN_AIL, &lip->li_flags)) {
738 			ASSERT(xlog_is_shutdown(lip->li_log));
739 			continue;
740 		}
741 
742 		lsn = xfs_ail_delete_one(ailp, lip);
743 		if (!tail_lsn && lsn)
744 			tail_lsn = lsn;
745 	}
746 	xfs_ail_update_finish(ailp, tail_lsn);
747 }
748 
749 /*
750  * Walk the list of inodes that have completed their IOs. If they are clean
751  * remove them from the list and dissociate them from the buffer. Buffers that
752  * are still dirty remain linked to the buffer and on the list. Caller must
753  * handle them appropriately.
754  */
755 static void
756 xfs_iflush_finish(
757 	struct xfs_buf		*bp,
758 	struct list_head	*list)
759 {
760 	struct xfs_log_item	*lip, *n;
761 
762 	list_for_each_entry_safe(lip, n, list, li_bio_list) {
763 		struct xfs_inode_log_item *iip = INODE_ITEM(lip);
764 		bool	drop_buffer = false;
765 
766 		spin_lock(&iip->ili_lock);
767 
768 		/*
769 		 * Remove the reference to the cluster buffer if the inode is
770 		 * clean in memory and drop the buffer reference once we've
771 		 * dropped the locks we hold.
772 		 */
773 		ASSERT(iip->ili_item.li_buf == bp);
774 		if (!iip->ili_fields) {
775 			iip->ili_item.li_buf = NULL;
776 			list_del_init(&lip->li_bio_list);
777 			drop_buffer = true;
778 		}
779 		iip->ili_last_fields = 0;
780 		iip->ili_flush_lsn = 0;
781 		spin_unlock(&iip->ili_lock);
782 		xfs_iflags_clear(iip->ili_inode, XFS_IFLUSHING);
783 		if (drop_buffer)
784 			xfs_buf_rele(bp);
785 	}
786 }
787 
788 /*
789  * Inode buffer IO completion routine.  It is responsible for removing inodes
790  * attached to the buffer from the AIL if they have not been re-logged and
791  * completing the inode flush.
792  */
793 void
794 xfs_buf_inode_iodone(
795 	struct xfs_buf		*bp)
796 {
797 	struct xfs_log_item	*lip, *n;
798 	LIST_HEAD(flushed_inodes);
799 	LIST_HEAD(ail_updates);
800 
801 	/*
802 	 * Pull the attached inodes from the buffer one at a time and take the
803 	 * appropriate action on them.
804 	 */
805 	list_for_each_entry_safe(lip, n, &bp->b_li_list, li_bio_list) {
806 		struct xfs_inode_log_item *iip = INODE_ITEM(lip);
807 
808 		if (xfs_iflags_test(iip->ili_inode, XFS_ISTALE)) {
809 			xfs_iflush_abort(iip->ili_inode);
810 			continue;
811 		}
812 		if (!iip->ili_last_fields)
813 			continue;
814 
815 		/* Do an unlocked check for needing the AIL lock. */
816 		if (iip->ili_flush_lsn == lip->li_lsn ||
817 		    test_bit(XFS_LI_FAILED, &lip->li_flags))
818 			list_move_tail(&lip->li_bio_list, &ail_updates);
819 		else
820 			list_move_tail(&lip->li_bio_list, &flushed_inodes);
821 	}
822 
823 	if (!list_empty(&ail_updates)) {
824 		xfs_iflush_ail_updates(bp->b_mount->m_ail, &ail_updates);
825 		list_splice_tail(&ail_updates, &flushed_inodes);
826 	}
827 
828 	xfs_iflush_finish(bp, &flushed_inodes);
829 	if (!list_empty(&flushed_inodes))
830 		list_splice_tail(&flushed_inodes, &bp->b_li_list);
831 }
832 
833 void
834 xfs_buf_inode_io_fail(
835 	struct xfs_buf		*bp)
836 {
837 	struct xfs_log_item	*lip;
838 
839 	list_for_each_entry(lip, &bp->b_li_list, li_bio_list)
840 		set_bit(XFS_LI_FAILED, &lip->li_flags);
841 }
842 
843 /*
844  * Clear the inode logging fields so no more flushes are attempted.  If we are
845  * on a buffer list, it is now safe to remove it because the buffer is
846  * guaranteed to be locked. The caller will drop the reference to the buffer
847  * the log item held.
848  */
849 static void
850 xfs_iflush_abort_clean(
851 	struct xfs_inode_log_item *iip)
852 {
853 	iip->ili_last_fields = 0;
854 	iip->ili_fields = 0;
855 	iip->ili_fsync_fields = 0;
856 	iip->ili_flush_lsn = 0;
857 	iip->ili_item.li_buf = NULL;
858 	list_del_init(&iip->ili_item.li_bio_list);
859 }
860 
861 /*
862  * Abort flushing the inode from a context holding the cluster buffer locked.
863  *
864  * This is the normal runtime method of aborting writeback of an inode that is
865  * attached to a cluster buffer. It occurs when the inode and the backing
866  * cluster buffer have been freed (i.e. inode is XFS_ISTALE), or when cluster
867  * flushing or buffer IO completion encounters a log shutdown situation.
868  *
869  * If we need to abort inode writeback and we don't already hold the buffer
870  * locked, call xfs_iflush_shutdown_abort() instead as this should only ever be
871  * necessary in a shutdown situation.
872  */
873 void
874 xfs_iflush_abort(
875 	struct xfs_inode	*ip)
876 {
877 	struct xfs_inode_log_item *iip = ip->i_itemp;
878 	struct xfs_buf		*bp;
879 
880 	if (!iip) {
881 		/* clean inode, nothing to do */
882 		xfs_iflags_clear(ip, XFS_IFLUSHING);
883 		return;
884 	}
885 
886 	/*
887 	 * Remove the inode item from the AIL before we clear its internal
888 	 * state. Whilst the inode is in the AIL, it should have a valid buffer
889 	 * pointer for push operations to access - it is only safe to remove the
890 	 * inode from the buffer once it has been removed from the AIL.
891 	 *
892 	 * We also clear the failed bit before removing the item from the AIL
893 	 * as xfs_trans_ail_delete()->xfs_clear_li_failed() will release buffer
894 	 * references the inode item owns and needs to hold until we've fully
895 	 * aborted the inode log item and detached it from the buffer.
896 	 */
897 	clear_bit(XFS_LI_FAILED, &iip->ili_item.li_flags);
898 	xfs_trans_ail_delete(&iip->ili_item, 0);
899 
900 	/*
901 	 * Grab the inode buffer so can we release the reference the inode log
902 	 * item holds on it.
903 	 */
904 	spin_lock(&iip->ili_lock);
905 	bp = iip->ili_item.li_buf;
906 	xfs_iflush_abort_clean(iip);
907 	spin_unlock(&iip->ili_lock);
908 
909 	xfs_iflags_clear(ip, XFS_IFLUSHING);
910 	if (bp)
911 		xfs_buf_rele(bp);
912 }
913 
914 /*
915  * Abort an inode flush in the case of a shutdown filesystem. This can be called
916  * from anywhere with just an inode reference and does not require holding the
917  * inode cluster buffer locked. If the inode is attached to a cluster buffer,
918  * it will grab and lock it safely, then abort the inode flush.
919  */
920 void
921 xfs_iflush_shutdown_abort(
922 	struct xfs_inode	*ip)
923 {
924 	struct xfs_inode_log_item *iip = ip->i_itemp;
925 	struct xfs_buf		*bp;
926 
927 	if (!iip) {
928 		/* clean inode, nothing to do */
929 		xfs_iflags_clear(ip, XFS_IFLUSHING);
930 		return;
931 	}
932 
933 	spin_lock(&iip->ili_lock);
934 	bp = iip->ili_item.li_buf;
935 	if (!bp) {
936 		spin_unlock(&iip->ili_lock);
937 		xfs_iflush_abort(ip);
938 		return;
939 	}
940 
941 	/*
942 	 * We have to take a reference to the buffer so that it doesn't get
943 	 * freed when we drop the ili_lock and then wait to lock the buffer.
944 	 * We'll clean up the extra reference after we pick up the ili_lock
945 	 * again.
946 	 */
947 	xfs_buf_hold(bp);
948 	spin_unlock(&iip->ili_lock);
949 	xfs_buf_lock(bp);
950 
951 	spin_lock(&iip->ili_lock);
952 	if (!iip->ili_item.li_buf) {
953 		/*
954 		 * Raced with another removal, hold the only reference
955 		 * to bp now. Inode should not be in the AIL now, so just clean
956 		 * up and return;
957 		 */
958 		ASSERT(list_empty(&iip->ili_item.li_bio_list));
959 		ASSERT(!test_bit(XFS_LI_IN_AIL, &iip->ili_item.li_flags));
960 		xfs_iflush_abort_clean(iip);
961 		spin_unlock(&iip->ili_lock);
962 		xfs_iflags_clear(ip, XFS_IFLUSHING);
963 		xfs_buf_relse(bp);
964 		return;
965 	}
966 
967 	/*
968 	 * Got two references to bp. The first will get dropped by
969 	 * xfs_iflush_abort() when the item is removed from the buffer list, but
970 	 * we can't drop our reference until _abort() returns because we have to
971 	 * unlock the buffer as well. Hence we abort and then unlock and release
972 	 * our reference to the buffer.
973 	 */
974 	ASSERT(iip->ili_item.li_buf == bp);
975 	spin_unlock(&iip->ili_lock);
976 	xfs_iflush_abort(ip);
977 	xfs_buf_relse(bp);
978 }
979 
980 
981 /*
982  * convert an xfs_inode_log_format struct from the old 32 bit version
983  * (which can have different field alignments) to the native 64 bit version
984  */
985 int
986 xfs_inode_item_format_convert(
987 	struct xfs_log_iovec		*buf,
988 	struct xfs_inode_log_format	*in_f)
989 {
990 	struct xfs_inode_log_format_32	*in_f32 = buf->i_addr;
991 
992 	if (buf->i_len != sizeof(*in_f32)) {
993 		XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, NULL);
994 		return -EFSCORRUPTED;
995 	}
996 
997 	in_f->ilf_type = in_f32->ilf_type;
998 	in_f->ilf_size = in_f32->ilf_size;
999 	in_f->ilf_fields = in_f32->ilf_fields;
1000 	in_f->ilf_asize = in_f32->ilf_asize;
1001 	in_f->ilf_dsize = in_f32->ilf_dsize;
1002 	in_f->ilf_ino = in_f32->ilf_ino;
1003 	memcpy(&in_f->ilf_u, &in_f32->ilf_u, sizeof(in_f->ilf_u));
1004 	in_f->ilf_blkno = in_f32->ilf_blkno;
1005 	in_f->ilf_len = in_f32->ilf_len;
1006 	in_f->ilf_boffset = in_f32->ilf_boffset;
1007 	return 0;
1008 }
1009