xref: /openbmc/linux/fs/xfs/xfs_inode_item.c (revision 22a41e9a5044bf3519f05b4a00e99af34bfeb40c)
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 	ASSERT(iip->ili_item.li_buf);
548 
549 	if (xfs_ipincount(ip) > 0 || xfs_buf_ispinned(bp) ||
550 	    (ip->i_flags & XFS_ISTALE))
551 		return XFS_ITEM_PINNED;
552 
553 	if (xfs_iflags_test(ip, XFS_IFLUSHING))
554 		return XFS_ITEM_FLUSHING;
555 
556 	if (!xfs_buf_trylock(bp))
557 		return XFS_ITEM_LOCKED;
558 
559 	spin_unlock(&lip->li_ailp->ail_lock);
560 
561 	/*
562 	 * We need to hold a reference for flushing the cluster buffer as it may
563 	 * fail the buffer without IO submission. In which case, we better get a
564 	 * reference for that completion because otherwise we don't get a
565 	 * reference for IO until we queue the buffer for delwri submission.
566 	 */
567 	xfs_buf_hold(bp);
568 	error = xfs_iflush_cluster(bp);
569 	if (!error) {
570 		if (!xfs_buf_delwri_queue(bp, buffer_list))
571 			rval = XFS_ITEM_FLUSHING;
572 		xfs_buf_relse(bp);
573 	} else {
574 		/*
575 		 * Release the buffer if we were unable to flush anything. On
576 		 * any other error, the buffer has already been released.
577 		 */
578 		if (error == -EAGAIN)
579 			xfs_buf_relse(bp);
580 		rval = XFS_ITEM_LOCKED;
581 	}
582 
583 	spin_lock(&lip->li_ailp->ail_lock);
584 	return rval;
585 }
586 
587 /*
588  * Unlock the inode associated with the inode log item.
589  */
590 STATIC void
591 xfs_inode_item_release(
592 	struct xfs_log_item	*lip)
593 {
594 	struct xfs_inode_log_item *iip = INODE_ITEM(lip);
595 	struct xfs_inode	*ip = iip->ili_inode;
596 	unsigned short		lock_flags;
597 
598 	ASSERT(ip->i_itemp != NULL);
599 	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
600 
601 	lock_flags = iip->ili_lock_flags;
602 	iip->ili_lock_flags = 0;
603 	if (lock_flags)
604 		xfs_iunlock(ip, lock_flags);
605 }
606 
607 /*
608  * This is called to find out where the oldest active copy of the inode log
609  * item in the on disk log resides now that the last log write of it completed
610  * at the given lsn.  Since we always re-log all dirty data in an inode, the
611  * latest copy in the on disk log is the only one that matters.  Therefore,
612  * simply return the given lsn.
613  *
614  * If the inode has been marked stale because the cluster is being freed, we
615  * don't want to (re-)insert this inode into the AIL. There is a race condition
616  * where the cluster buffer may be unpinned before the inode is inserted into
617  * the AIL during transaction committed processing. If the buffer is unpinned
618  * before the inode item has been committed and inserted, then it is possible
619  * for the buffer to be written and IO completes before the inode is inserted
620  * into the AIL. In that case, we'd be inserting a clean, stale inode into the
621  * AIL which will never get removed. It will, however, get reclaimed which
622  * triggers an assert in xfs_inode_free() complaining about freein an inode
623  * still in the AIL.
624  *
625  * To avoid this, just unpin the inode directly and return a LSN of -1 so the
626  * transaction committed code knows that it does not need to do any further
627  * processing on the item.
628  */
629 STATIC xfs_lsn_t
630 xfs_inode_item_committed(
631 	struct xfs_log_item	*lip,
632 	xfs_lsn_t		lsn)
633 {
634 	struct xfs_inode_log_item *iip = INODE_ITEM(lip);
635 	struct xfs_inode	*ip = iip->ili_inode;
636 
637 	if (xfs_iflags_test(ip, XFS_ISTALE)) {
638 		xfs_inode_item_unpin(lip, 0);
639 		return -1;
640 	}
641 	return lsn;
642 }
643 
644 STATIC void
645 xfs_inode_item_committing(
646 	struct xfs_log_item	*lip,
647 	xfs_csn_t		seq)
648 {
649 	INODE_ITEM(lip)->ili_commit_seq = seq;
650 	return xfs_inode_item_release(lip);
651 }
652 
653 static const struct xfs_item_ops xfs_inode_item_ops = {
654 	.iop_size	= xfs_inode_item_size,
655 	.iop_format	= xfs_inode_item_format,
656 	.iop_pin	= xfs_inode_item_pin,
657 	.iop_unpin	= xfs_inode_item_unpin,
658 	.iop_release	= xfs_inode_item_release,
659 	.iop_committed	= xfs_inode_item_committed,
660 	.iop_push	= xfs_inode_item_push,
661 	.iop_committing	= xfs_inode_item_committing,
662 };
663 
664 
665 /*
666  * Initialize the inode log item for a newly allocated (in-core) inode.
667  */
668 void
669 xfs_inode_item_init(
670 	struct xfs_inode	*ip,
671 	struct xfs_mount	*mp)
672 {
673 	struct xfs_inode_log_item *iip;
674 
675 	ASSERT(ip->i_itemp == NULL);
676 	iip = ip->i_itemp = kmem_cache_zalloc(xfs_ili_cache,
677 					      GFP_KERNEL | __GFP_NOFAIL);
678 
679 	iip->ili_inode = ip;
680 	spin_lock_init(&iip->ili_lock);
681 	xfs_log_item_init(mp, &iip->ili_item, XFS_LI_INODE,
682 						&xfs_inode_item_ops);
683 }
684 
685 /*
686  * Free the inode log item and any memory hanging off of it.
687  */
688 void
689 xfs_inode_item_destroy(
690 	struct xfs_inode	*ip)
691 {
692 	struct xfs_inode_log_item *iip = ip->i_itemp;
693 
694 	ASSERT(iip->ili_item.li_buf == NULL);
695 
696 	ip->i_itemp = NULL;
697 	kmem_free(iip->ili_item.li_lv_shadow);
698 	kmem_cache_free(xfs_ili_cache, iip);
699 }
700 
701 
702 /*
703  * We only want to pull the item from the AIL if it is actually there
704  * and its location in the log has not changed since we started the
705  * flush.  Thus, we only bother if the inode's lsn has not changed.
706  */
707 static void
708 xfs_iflush_ail_updates(
709 	struct xfs_ail		*ailp,
710 	struct list_head	*list)
711 {
712 	struct xfs_log_item	*lip;
713 	xfs_lsn_t		tail_lsn = 0;
714 
715 	/* this is an opencoded batch version of xfs_trans_ail_delete */
716 	spin_lock(&ailp->ail_lock);
717 	list_for_each_entry(lip, list, li_bio_list) {
718 		xfs_lsn_t	lsn;
719 
720 		clear_bit(XFS_LI_FAILED, &lip->li_flags);
721 		if (INODE_ITEM(lip)->ili_flush_lsn != lip->li_lsn)
722 			continue;
723 
724 		/*
725 		 * dgc: Not sure how this happens, but it happens very
726 		 * occassionaly via generic/388.  xfs_iflush_abort() also
727 		 * silently handles this same "under writeback but not in AIL at
728 		 * shutdown" condition via xfs_trans_ail_delete().
729 		 */
730 		if (!test_bit(XFS_LI_IN_AIL, &lip->li_flags)) {
731 			ASSERT(xlog_is_shutdown(lip->li_log));
732 			continue;
733 		}
734 
735 		lsn = xfs_ail_delete_one(ailp, lip);
736 		if (!tail_lsn && lsn)
737 			tail_lsn = lsn;
738 	}
739 	xfs_ail_update_finish(ailp, tail_lsn);
740 }
741 
742 /*
743  * Walk the list of inodes that have completed their IOs. If they are clean
744  * remove them from the list and dissociate them from the buffer. Buffers that
745  * are still dirty remain linked to the buffer and on the list. Caller must
746  * handle them appropriately.
747  */
748 static void
749 xfs_iflush_finish(
750 	struct xfs_buf		*bp,
751 	struct list_head	*list)
752 {
753 	struct xfs_log_item	*lip, *n;
754 
755 	list_for_each_entry_safe(lip, n, list, li_bio_list) {
756 		struct xfs_inode_log_item *iip = INODE_ITEM(lip);
757 		bool	drop_buffer = false;
758 
759 		spin_lock(&iip->ili_lock);
760 
761 		/*
762 		 * Remove the reference to the cluster buffer if the inode is
763 		 * clean in memory and drop the buffer reference once we've
764 		 * dropped the locks we hold.
765 		 */
766 		ASSERT(iip->ili_item.li_buf == bp);
767 		if (!iip->ili_fields) {
768 			iip->ili_item.li_buf = NULL;
769 			list_del_init(&lip->li_bio_list);
770 			drop_buffer = true;
771 		}
772 		iip->ili_last_fields = 0;
773 		iip->ili_flush_lsn = 0;
774 		spin_unlock(&iip->ili_lock);
775 		xfs_iflags_clear(iip->ili_inode, XFS_IFLUSHING);
776 		if (drop_buffer)
777 			xfs_buf_rele(bp);
778 	}
779 }
780 
781 /*
782  * Inode buffer IO completion routine.  It is responsible for removing inodes
783  * attached to the buffer from the AIL if they have not been re-logged and
784  * completing the inode flush.
785  */
786 void
787 xfs_buf_inode_iodone(
788 	struct xfs_buf		*bp)
789 {
790 	struct xfs_log_item	*lip, *n;
791 	LIST_HEAD(flushed_inodes);
792 	LIST_HEAD(ail_updates);
793 
794 	/*
795 	 * Pull the attached inodes from the buffer one at a time and take the
796 	 * appropriate action on them.
797 	 */
798 	list_for_each_entry_safe(lip, n, &bp->b_li_list, li_bio_list) {
799 		struct xfs_inode_log_item *iip = INODE_ITEM(lip);
800 
801 		if (xfs_iflags_test(iip->ili_inode, XFS_ISTALE)) {
802 			xfs_iflush_abort(iip->ili_inode);
803 			continue;
804 		}
805 		if (!iip->ili_last_fields)
806 			continue;
807 
808 		/* Do an unlocked check for needing the AIL lock. */
809 		if (iip->ili_flush_lsn == lip->li_lsn ||
810 		    test_bit(XFS_LI_FAILED, &lip->li_flags))
811 			list_move_tail(&lip->li_bio_list, &ail_updates);
812 		else
813 			list_move_tail(&lip->li_bio_list, &flushed_inodes);
814 	}
815 
816 	if (!list_empty(&ail_updates)) {
817 		xfs_iflush_ail_updates(bp->b_mount->m_ail, &ail_updates);
818 		list_splice_tail(&ail_updates, &flushed_inodes);
819 	}
820 
821 	xfs_iflush_finish(bp, &flushed_inodes);
822 	if (!list_empty(&flushed_inodes))
823 		list_splice_tail(&flushed_inodes, &bp->b_li_list);
824 }
825 
826 void
827 xfs_buf_inode_io_fail(
828 	struct xfs_buf		*bp)
829 {
830 	struct xfs_log_item	*lip;
831 
832 	list_for_each_entry(lip, &bp->b_li_list, li_bio_list)
833 		set_bit(XFS_LI_FAILED, &lip->li_flags);
834 }
835 
836 /*
837  * This is the inode flushing abort routine.  It is called when
838  * the filesystem is shutting down to clean up the inode state.  It is
839  * responsible for removing the inode item from the AIL if it has not been
840  * re-logged and clearing the inode's flush state.
841  */
842 void
843 xfs_iflush_abort(
844 	struct xfs_inode	*ip)
845 {
846 	struct xfs_inode_log_item *iip = ip->i_itemp;
847 	struct xfs_buf		*bp = NULL;
848 
849 	if (iip) {
850 		/*
851 		 * Clear the failed bit before removing the item from the AIL so
852 		 * xfs_trans_ail_delete() doesn't try to clear and release the
853 		 * buffer attached to the log item before we are done with it.
854 		 */
855 		clear_bit(XFS_LI_FAILED, &iip->ili_item.li_flags);
856 		xfs_trans_ail_delete(&iip->ili_item, 0);
857 
858 		/*
859 		 * Clear the inode logging fields so no more flushes are
860 		 * attempted.
861 		 */
862 		spin_lock(&iip->ili_lock);
863 		iip->ili_last_fields = 0;
864 		iip->ili_fields = 0;
865 		iip->ili_fsync_fields = 0;
866 		iip->ili_flush_lsn = 0;
867 		bp = iip->ili_item.li_buf;
868 		iip->ili_item.li_buf = NULL;
869 		list_del_init(&iip->ili_item.li_bio_list);
870 		spin_unlock(&iip->ili_lock);
871 	}
872 	xfs_iflags_clear(ip, XFS_IFLUSHING);
873 	if (bp)
874 		xfs_buf_rele(bp);
875 }
876 
877 /*
878  * convert an xfs_inode_log_format struct from the old 32 bit version
879  * (which can have different field alignments) to the native 64 bit version
880  */
881 int
882 xfs_inode_item_format_convert(
883 	struct xfs_log_iovec		*buf,
884 	struct xfs_inode_log_format	*in_f)
885 {
886 	struct xfs_inode_log_format_32	*in_f32 = buf->i_addr;
887 
888 	if (buf->i_len != sizeof(*in_f32)) {
889 		XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, NULL);
890 		return -EFSCORRUPTED;
891 	}
892 
893 	in_f->ilf_type = in_f32->ilf_type;
894 	in_f->ilf_size = in_f32->ilf_size;
895 	in_f->ilf_fields = in_f32->ilf_fields;
896 	in_f->ilf_asize = in_f32->ilf_asize;
897 	in_f->ilf_dsize = in_f32->ilf_dsize;
898 	in_f->ilf_ino = in_f32->ilf_ino;
899 	memcpy(&in_f->ilf_u, &in_f32->ilf_u, sizeof(in_f->ilf_u));
900 	in_f->ilf_blkno = in_f32->ilf_blkno;
901 	in_f->ilf_len = in_f32->ilf_len;
902 	in_f->ilf_boffset = in_f32->ilf_boffset;
903 	return 0;
904 }
905