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_platform.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 #include "xfs_rtbitmap.h"
23
24 #include <linux/iversion.h>
25
26 struct kmem_cache *xfs_ili_cache; /* inode log item */
27
INODE_ITEM(struct xfs_log_item * lip)28 static inline struct xfs_inode_log_item *INODE_ITEM(struct xfs_log_item *lip)
29 {
30 return container_of(lip, struct xfs_inode_log_item, ili_item);
31 }
32
33 static uint64_t
xfs_inode_item_sort(struct xfs_log_item * lip)34 xfs_inode_item_sort(
35 struct xfs_log_item *lip)
36 {
37 return INODE_ITEM(lip)->ili_inode->i_ino;
38 }
39
40 #ifdef DEBUG_EXPENSIVE
41 static void
xfs_inode_item_precommit_check(struct xfs_inode * ip)42 xfs_inode_item_precommit_check(
43 struct xfs_inode *ip)
44 {
45 struct xfs_mount *mp = ip->i_mount;
46 struct xfs_dinode *dip;
47 xfs_failaddr_t fa;
48
49 dip = kzalloc(mp->m_sb.sb_inodesize, GFP_KERNEL | GFP_NOFS);
50 if (!dip) {
51 ASSERT(dip != NULL);
52 return;
53 }
54
55 xfs_inode_to_disk(ip, dip, 0);
56 xfs_dinode_calc_crc(mp, dip);
57 fa = xfs_dinode_verify(mp, ip->i_ino, dip);
58 if (fa) {
59 xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__, dip,
60 sizeof(*dip), fa);
61 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
62 ASSERT(fa == NULL);
63 }
64 kfree(dip);
65 }
66 #else
67 # define xfs_inode_item_precommit_check(ip) ((void)0)
68 #endif
69
70 /*
71 * Prior to finally logging the inode, we have to ensure that all the
72 * per-modification inode state changes are applied. This includes VFS inode
73 * state updates, format conversions, verifier state synchronisation and
74 * ensuring the inode buffer remains in memory whilst the inode is dirty.
75 *
76 * We have to be careful when we grab the inode cluster buffer due to lock
77 * ordering constraints. The unlinked inode modifications (xfs_iunlink_item)
78 * require AGI -> inode cluster buffer lock order. The inode cluster buffer is
79 * not locked until ->precommit, so it happens after everything else has been
80 * modified.
81 *
82 * Further, we have AGI -> AGF lock ordering, and with O_TMPFILE handling we
83 * have AGI -> AGF -> iunlink item -> inode cluster buffer lock order. Hence we
84 * cannot safely lock the inode cluster buffer in xfs_trans_log_inode() because
85 * it can be called on a inode (e.g. via bumplink/droplink) before we take the
86 * AGF lock modifying directory blocks.
87 *
88 * Rather than force a complete rework of all the transactions to call
89 * xfs_trans_log_inode() once and once only at the end of every transaction, we
90 * move the pinning of the inode cluster buffer to a ->precommit operation. This
91 * matches how the xfs_iunlink_item locks the inode cluster buffer, and it
92 * ensures that the inode cluster buffer locking is always done last in a
93 * transaction. i.e. we ensure the lock order is always AGI -> AGF -> inode
94 * cluster buffer.
95 *
96 * If we return the inode number as the precommit sort key then we'll also
97 * guarantee that the order all inode cluster buffer locking is the same all the
98 * inodes and unlink items in the transaction.
99 */
100 static int
xfs_inode_item_precommit(struct xfs_trans * tp,struct xfs_log_item * lip)101 xfs_inode_item_precommit(
102 struct xfs_trans *tp,
103 struct xfs_log_item *lip)
104 {
105 struct xfs_inode_log_item *iip = INODE_ITEM(lip);
106 struct xfs_inode *ip = iip->ili_inode;
107 struct inode *inode = VFS_I(ip);
108 unsigned int flags = iip->ili_dirty_flags;
109
110 /*
111 * Don't bother with i_lock for the I_DIRTY_TIME check here, as races
112 * don't matter - we either will need an extra transaction in 24 hours
113 * to log the timestamps, or will clear already cleared fields in the
114 * worst case.
115 */
116 if (inode_state_read_once(inode) & I_DIRTY_TIME) {
117 spin_lock(&inode->i_lock);
118 inode_state_clear(inode, I_DIRTY_TIME);
119 spin_unlock(&inode->i_lock);
120 }
121
122 /*
123 * If we're updating the inode core or the timestamps and it's possible
124 * to upgrade this inode to bigtime format, do so now.
125 */
126 if ((flags & (XFS_ILOG_CORE | XFS_ILOG_TIMESTAMP)) &&
127 xfs_has_bigtime(ip->i_mount) &&
128 !xfs_inode_has_bigtime(ip)) {
129 ip->i_diflags2 |= XFS_DIFLAG2_BIGTIME;
130 flags |= XFS_ILOG_CORE;
131 }
132
133 /*
134 * Inode verifiers do not check that the extent size hints are an
135 * integer multiple of the rt extent size on a directory with
136 * rtinherit flags set. If we're logging a directory that is
137 * misconfigured in this way, clear the bad hints.
138 */
139 if (ip->i_diflags & XFS_DIFLAG_RTINHERIT) {
140 if ((ip->i_diflags & XFS_DIFLAG_EXTSZINHERIT) &&
141 xfs_extlen_to_rtxmod(ip->i_mount, ip->i_extsize) > 0) {
142 ip->i_diflags &= ~(XFS_DIFLAG_EXTSIZE |
143 XFS_DIFLAG_EXTSZINHERIT);
144 ip->i_extsize = 0;
145 flags |= XFS_ILOG_CORE;
146 }
147 if ((ip->i_diflags2 & XFS_DIFLAG2_COWEXTSIZE) &&
148 xfs_extlen_to_rtxmod(ip->i_mount, ip->i_cowextsize) > 0) {
149 ip->i_diflags2 &= ~XFS_DIFLAG2_COWEXTSIZE;
150 ip->i_cowextsize = 0;
151 flags |= XFS_ILOG_CORE;
152 }
153 }
154
155 spin_lock(&iip->ili_lock);
156 if (!iip->ili_item.li_buf) {
157 struct xfs_buf *bp;
158 int error;
159
160 /*
161 * We hold the ILOCK here, so this inode is not going to be
162 * flushed while we are here. Further, because there is no
163 * buffer attached to the item, we know that there is no IO in
164 * progress, so nothing will clear the ili_fields while we read
165 * in the buffer. Hence we can safely drop the spin lock and
166 * read the buffer knowing that the state will not change from
167 * here.
168 */
169 spin_unlock(&iip->ili_lock);
170 error = xfs_imap_to_bp(ip->i_mount, tp, &ip->i_imap, &bp);
171 if (error)
172 return error;
173
174 /*
175 * We need an explicit buffer reference for the log item but
176 * don't want the buffer to remain attached to the transaction.
177 * Hold the buffer but release the transaction reference once
178 * we've attached the inode log item to the buffer log item
179 * list.
180 */
181 xfs_buf_hold(bp);
182 spin_lock(&iip->ili_lock);
183 iip->ili_item.li_buf = bp;
184 bp->b_iodone = xfs_buf_inode_iodone;
185 list_add_tail(&iip->ili_item.li_bio_list, &bp->b_li_list);
186 xfs_trans_brelse(tp, bp);
187 }
188
189 /*
190 * Store the dirty flags back into the inode item as this state is used
191 * later on in xfs_inode_item_committing() to determine whether the
192 * transaction is relevant to fsync state or not.
193 */
194 iip->ili_dirty_flags = flags;
195
196 /*
197 * Convert the flags on-disk fields that have been modified in the
198 * transaction so that ili_fields tracks the changes correctly.
199 */
200 if (flags & XFS_ILOG_IVERSION)
201 flags = ((flags & ~XFS_ILOG_IVERSION) | XFS_ILOG_CORE);
202
203 /*
204 * Always OR in the bits from the ili_last_fields field. This is to
205 * coordinate with the xfs_iflush() and xfs_buf_inode_iodone() routines
206 * in the eventual clearing of the ili_fields bits. See the big comment
207 * in xfs_iflush() for an explanation of this coordination mechanism.
208 */
209 iip->ili_fields |= (flags | iip->ili_last_fields);
210 spin_unlock(&iip->ili_lock);
211
212 xfs_inode_item_precommit_check(ip);
213 return 0;
214 }
215
216 /*
217 * The logged size of an inode fork is always the current size of the inode
218 * fork. This means that when an inode fork is relogged, the size of the logged
219 * region is determined by the current state, not the combination of the
220 * previously logged state + the current state. This is different relogging
221 * behaviour to most other log items which will retain the size of the
222 * previously logged changes when smaller regions are relogged.
223 *
224 * Hence operations that remove data from the inode fork (e.g. shortform
225 * dir/attr remove, extent form extent removal, etc), the size of the relogged
226 * inode gets -smaller- rather than stays the same size as the previously logged
227 * size and this can result in the committing transaction reducing the amount of
228 * space being consumed by the CIL.
229 */
230 STATIC void
xfs_inode_item_data_fork_size(struct xfs_inode_log_item * iip,int * nvecs,int * nbytes)231 xfs_inode_item_data_fork_size(
232 struct xfs_inode_log_item *iip,
233 int *nvecs,
234 int *nbytes)
235 {
236 struct xfs_inode *ip = iip->ili_inode;
237
238 switch (ip->i_df.if_format) {
239 case XFS_DINODE_FMT_EXTENTS:
240 if ((iip->ili_fields & XFS_ILOG_DEXT) &&
241 ip->i_df.if_nextents > 0 &&
242 ip->i_df.if_bytes > 0) {
243 /* worst case, doesn't subtract delalloc extents */
244 *nbytes += xfs_inode_data_fork_size(ip);
245 *nvecs += 1;
246 }
247 break;
248 case XFS_DINODE_FMT_BTREE:
249 case XFS_DINODE_FMT_META_BTREE:
250 if ((iip->ili_fields & XFS_ILOG_DBROOT) &&
251 ip->i_df.if_broot_bytes > 0) {
252 *nbytes += ip->i_df.if_broot_bytes;
253 *nvecs += 1;
254 }
255 break;
256 case XFS_DINODE_FMT_LOCAL:
257 if ((iip->ili_fields & XFS_ILOG_DDATA) &&
258 ip->i_df.if_bytes > 0) {
259 *nbytes += xlog_calc_iovec_len(ip->i_df.if_bytes);
260 *nvecs += 1;
261 }
262 break;
263
264 case XFS_DINODE_FMT_DEV:
265 break;
266 default:
267 ASSERT(0);
268 break;
269 }
270 }
271
272 STATIC void
xfs_inode_item_attr_fork_size(struct xfs_inode_log_item * iip,int * nvecs,int * nbytes)273 xfs_inode_item_attr_fork_size(
274 struct xfs_inode_log_item *iip,
275 int *nvecs,
276 int *nbytes)
277 {
278 struct xfs_inode *ip = iip->ili_inode;
279
280 switch (ip->i_af.if_format) {
281 case XFS_DINODE_FMT_EXTENTS:
282 if ((iip->ili_fields & XFS_ILOG_AEXT) &&
283 ip->i_af.if_nextents > 0 &&
284 ip->i_af.if_bytes > 0) {
285 /* worst case, doesn't subtract unused space */
286 *nbytes += xfs_inode_attr_fork_size(ip);
287 *nvecs += 1;
288 }
289 break;
290 case XFS_DINODE_FMT_BTREE:
291 if ((iip->ili_fields & XFS_ILOG_ABROOT) &&
292 ip->i_af.if_broot_bytes > 0) {
293 *nbytes += ip->i_af.if_broot_bytes;
294 *nvecs += 1;
295 }
296 break;
297 case XFS_DINODE_FMT_LOCAL:
298 if ((iip->ili_fields & XFS_ILOG_ADATA) &&
299 ip->i_af.if_bytes > 0) {
300 *nbytes += xlog_calc_iovec_len(ip->i_af.if_bytes);
301 *nvecs += 1;
302 }
303 break;
304 default:
305 ASSERT(0);
306 break;
307 }
308 }
309
310 /*
311 * This returns the number of iovecs needed to log the given inode item.
312 *
313 * We need one iovec for the inode log format structure, one for the
314 * inode core, and possibly one for the inode data/extents/b-tree root
315 * and one for the inode attribute data/extents/b-tree root.
316 */
317 STATIC void
xfs_inode_item_size(struct xfs_log_item * lip,int * nvecs,int * nbytes)318 xfs_inode_item_size(
319 struct xfs_log_item *lip,
320 int *nvecs,
321 int *nbytes)
322 {
323 struct xfs_inode_log_item *iip = INODE_ITEM(lip);
324 struct xfs_inode *ip = iip->ili_inode;
325
326 *nvecs += 2;
327 *nbytes += sizeof(struct xfs_inode_log_format) +
328 xfs_log_dinode_size(ip->i_mount);
329
330 xfs_inode_item_data_fork_size(iip, nvecs, nbytes);
331 if (xfs_inode_has_attr_fork(ip))
332 xfs_inode_item_attr_fork_size(iip, nvecs, nbytes);
333 }
334
335 STATIC void
xfs_inode_item_format_data_fork(struct xfs_inode_log_item * iip,struct xfs_inode_log_format * ilf,struct xlog_format_buf * lfb)336 xfs_inode_item_format_data_fork(
337 struct xfs_inode_log_item *iip,
338 struct xfs_inode_log_format *ilf,
339 struct xlog_format_buf *lfb)
340 {
341 struct xfs_inode *ip = iip->ili_inode;
342 size_t data_bytes;
343
344 switch (ip->i_df.if_format) {
345 case XFS_DINODE_FMT_EXTENTS:
346 iip->ili_fields &=
347 ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT | XFS_ILOG_DEV);
348
349 if ((iip->ili_fields & XFS_ILOG_DEXT) &&
350 ip->i_df.if_nextents > 0 &&
351 ip->i_df.if_bytes > 0) {
352 struct xfs_bmbt_rec *p;
353
354 ASSERT(xfs_iext_count(&ip->i_df) > 0);
355
356 p = xlog_format_start(lfb, XLOG_REG_TYPE_IEXT);
357 data_bytes = xfs_iextents_copy(ip, p, XFS_DATA_FORK);
358 xlog_format_commit(lfb, data_bytes);
359
360 ASSERT(data_bytes <= ip->i_df.if_bytes);
361
362 ilf->ilf_dsize = data_bytes;
363 ilf->ilf_size++;
364 } else {
365 iip->ili_fields &= ~XFS_ILOG_DEXT;
366 }
367 break;
368 case XFS_DINODE_FMT_BTREE:
369 case XFS_DINODE_FMT_META_BTREE:
370 iip->ili_fields &=
371 ~(XFS_ILOG_DDATA | XFS_ILOG_DEXT | XFS_ILOG_DEV);
372
373 if ((iip->ili_fields & XFS_ILOG_DBROOT) &&
374 ip->i_df.if_broot_bytes > 0) {
375 ASSERT(ip->i_df.if_broot != NULL);
376 xlog_format_copy(lfb, XLOG_REG_TYPE_IBROOT,
377 ip->i_df.if_broot,
378 ip->i_df.if_broot_bytes);
379 ilf->ilf_dsize = ip->i_df.if_broot_bytes;
380 ilf->ilf_size++;
381 } else {
382 ASSERT(!(iip->ili_fields &
383 XFS_ILOG_DBROOT));
384 iip->ili_fields &= ~XFS_ILOG_DBROOT;
385 }
386 break;
387 case XFS_DINODE_FMT_LOCAL:
388 iip->ili_fields &=
389 ~(XFS_ILOG_DEXT | XFS_ILOG_DBROOT | XFS_ILOG_DEV);
390 if ((iip->ili_fields & XFS_ILOG_DDATA) &&
391 ip->i_df.if_bytes > 0) {
392 ASSERT(ip->i_df.if_data != NULL);
393 ASSERT(ip->i_disk_size > 0);
394 xlog_format_copy(lfb, XLOG_REG_TYPE_ILOCAL,
395 ip->i_df.if_data,
396 ip->i_df.if_bytes);
397 ilf->ilf_dsize = (unsigned)ip->i_df.if_bytes;
398 ilf->ilf_size++;
399 } else {
400 iip->ili_fields &= ~XFS_ILOG_DDATA;
401 }
402 break;
403 case XFS_DINODE_FMT_DEV:
404 iip->ili_fields &=
405 ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT | XFS_ILOG_DEXT);
406 if (iip->ili_fields & XFS_ILOG_DEV)
407 ilf->ilf_u.ilfu_rdev = sysv_encode_dev(VFS_I(ip)->i_rdev);
408 break;
409 default:
410 ASSERT(0);
411 break;
412 }
413 }
414
415 STATIC void
xfs_inode_item_format_attr_fork(struct xfs_inode_log_item * iip,struct xfs_inode_log_format * ilf,struct xlog_format_buf * lfb)416 xfs_inode_item_format_attr_fork(
417 struct xfs_inode_log_item *iip,
418 struct xfs_inode_log_format *ilf,
419 struct xlog_format_buf *lfb)
420 {
421 struct xfs_inode *ip = iip->ili_inode;
422 size_t data_bytes;
423
424 switch (ip->i_af.if_format) {
425 case XFS_DINODE_FMT_EXTENTS:
426 iip->ili_fields &=
427 ~(XFS_ILOG_ADATA | XFS_ILOG_ABROOT);
428
429 if ((iip->ili_fields & XFS_ILOG_AEXT) &&
430 ip->i_af.if_nextents > 0 &&
431 ip->i_af.if_bytes > 0) {
432 struct xfs_bmbt_rec *p;
433
434 ASSERT(xfs_iext_count(&ip->i_af) ==
435 ip->i_af.if_nextents);
436
437 p = xlog_format_start(lfb, XLOG_REG_TYPE_IATTR_EXT);
438 data_bytes = xfs_iextents_copy(ip, p, XFS_ATTR_FORK);
439 xlog_format_commit(lfb, data_bytes);
440
441 ilf->ilf_asize = data_bytes;
442 ilf->ilf_size++;
443 } else {
444 iip->ili_fields &= ~XFS_ILOG_AEXT;
445 }
446 break;
447 case XFS_DINODE_FMT_BTREE:
448 iip->ili_fields &=
449 ~(XFS_ILOG_ADATA | XFS_ILOG_AEXT);
450
451 if ((iip->ili_fields & XFS_ILOG_ABROOT) &&
452 ip->i_af.if_broot_bytes > 0) {
453 ASSERT(ip->i_af.if_broot != NULL);
454
455 xlog_format_copy(lfb, XLOG_REG_TYPE_IATTR_BROOT,
456 ip->i_af.if_broot,
457 ip->i_af.if_broot_bytes);
458 ilf->ilf_asize = ip->i_af.if_broot_bytes;
459 ilf->ilf_size++;
460 } else {
461 iip->ili_fields &= ~XFS_ILOG_ABROOT;
462 }
463 break;
464 case XFS_DINODE_FMT_LOCAL:
465 iip->ili_fields &=
466 ~(XFS_ILOG_AEXT | XFS_ILOG_ABROOT);
467
468 if ((iip->ili_fields & XFS_ILOG_ADATA) &&
469 ip->i_af.if_bytes > 0) {
470 ASSERT(ip->i_af.if_data != NULL);
471 xlog_format_copy(lfb, XLOG_REG_TYPE_IATTR_LOCAL,
472 ip->i_af.if_data,
473 ip->i_af.if_bytes);
474 ilf->ilf_asize = (unsigned)ip->i_af.if_bytes;
475 ilf->ilf_size++;
476 } else {
477 iip->ili_fields &= ~XFS_ILOG_ADATA;
478 }
479 break;
480 default:
481 ASSERT(0);
482 break;
483 }
484 }
485
486 /*
487 * Convert an incore timestamp to a log timestamp. Note that the log format
488 * specifies host endian format!
489 */
490 static inline xfs_log_timestamp_t
xfs_inode_to_log_dinode_ts(struct xfs_inode * ip,const struct timespec64 tv)491 xfs_inode_to_log_dinode_ts(
492 struct xfs_inode *ip,
493 const struct timespec64 tv)
494 {
495 struct xfs_log_legacy_timestamp *lits;
496 xfs_log_timestamp_t its;
497
498 if (xfs_inode_has_bigtime(ip))
499 return xfs_inode_encode_bigtime(tv);
500
501 lits = (struct xfs_log_legacy_timestamp *)&its;
502 lits->t_sec = tv.tv_sec;
503 lits->t_nsec = tv.tv_nsec;
504
505 return its;
506 }
507
508 /*
509 * The legacy DMAPI fields are only present in the on-disk and in-log inodes,
510 * but not in the in-memory one. But we are guaranteed to have an inode buffer
511 * in memory when logging an inode, so we can just copy it from the on-disk
512 * inode to the in-log inode here so that recovery of file system with these
513 * fields set to non-zero values doesn't lose them. For all other cases we zero
514 * the fields.
515 */
516 static void
xfs_copy_dm_fields_to_log_dinode(struct xfs_inode * ip,struct xfs_log_dinode * to)517 xfs_copy_dm_fields_to_log_dinode(
518 struct xfs_inode *ip,
519 struct xfs_log_dinode *to)
520 {
521 struct xfs_dinode *dip;
522
523 dip = xfs_buf_offset(ip->i_itemp->ili_item.li_buf,
524 ip->i_imap.im_boffset);
525
526 if (xfs_iflags_test(ip, XFS_IPRESERVE_DM_FIELDS)) {
527 to->di_dmevmask = be32_to_cpu(dip->di_dmevmask);
528 to->di_dmstate = be16_to_cpu(dip->di_dmstate);
529 } else {
530 to->di_dmevmask = 0;
531 to->di_dmstate = 0;
532 }
533 }
534
535 static inline void
xfs_inode_to_log_dinode_iext_counters(struct xfs_inode * ip,struct xfs_log_dinode * to)536 xfs_inode_to_log_dinode_iext_counters(
537 struct xfs_inode *ip,
538 struct xfs_log_dinode *to)
539 {
540 if (xfs_inode_has_large_extent_counts(ip)) {
541 to->di_big_nextents = xfs_ifork_nextents(&ip->i_df);
542 to->di_big_anextents = xfs_ifork_nextents(&ip->i_af);
543 to->di_nrext64_pad = 0;
544 } else {
545 to->di_nextents = xfs_ifork_nextents(&ip->i_df);
546 to->di_anextents = xfs_ifork_nextents(&ip->i_af);
547 }
548 }
549
550 static void
xfs_inode_to_log_dinode(struct xfs_inode * ip,struct xfs_log_dinode * to,xfs_lsn_t lsn)551 xfs_inode_to_log_dinode(
552 struct xfs_inode *ip,
553 struct xfs_log_dinode *to,
554 xfs_lsn_t lsn)
555 {
556 struct inode *inode = VFS_I(ip);
557
558 to->di_magic = XFS_DINODE_MAGIC;
559 to->di_format = xfs_ifork_format(&ip->i_df);
560 to->di_uid = i_uid_read(inode);
561 to->di_gid = i_gid_read(inode);
562 to->di_projid_lo = ip->i_projid & 0xffff;
563 to->di_projid_hi = ip->i_projid >> 16;
564
565 to->di_atime = xfs_inode_to_log_dinode_ts(ip, inode_get_atime(inode));
566 to->di_mtime = xfs_inode_to_log_dinode_ts(ip, inode_get_mtime(inode));
567 to->di_ctime = xfs_inode_to_log_dinode_ts(ip, inode_get_ctime(inode));
568 to->di_nlink = inode->i_nlink;
569 to->di_gen = inode->i_generation;
570 to->di_mode = inode->i_mode;
571
572 to->di_size = ip->i_disk_size;
573 to->di_nblocks = ip->i_nblocks;
574 to->di_extsize = ip->i_extsize;
575 to->di_forkoff = ip->i_forkoff;
576 to->di_aformat = xfs_ifork_format(&ip->i_af);
577 to->di_flags = ip->i_diflags;
578
579 xfs_copy_dm_fields_to_log_dinode(ip, to);
580
581 /* log a dummy value to ensure log structure is fully initialised */
582 to->di_next_unlinked = NULLAGINO;
583
584 if (xfs_has_v3inodes(ip->i_mount)) {
585 to->di_version = 3;
586 to->di_changecount = inode_peek_iversion(inode);
587 to->di_crtime = xfs_inode_to_log_dinode_ts(ip, ip->i_crtime);
588 to->di_flags2 = ip->i_diflags2;
589 /* also covers the di_used_blocks union arm: */
590 to->di_cowextsize = ip->i_cowextsize;
591 to->di_ino = ip->i_ino;
592 to->di_lsn = lsn;
593 memset(to->di_pad2, 0, sizeof(to->di_pad2));
594 uuid_copy(&to->di_uuid, &ip->i_mount->m_sb.sb_meta_uuid);
595 to->di_v3_pad = 0;
596
597 /* dummy value for initialisation */
598 to->di_crc = 0;
599
600 if (xfs_is_metadir_inode(ip))
601 to->di_metatype = ip->i_metatype;
602 else
603 to->di_metatype = 0;
604 } else {
605 to->di_version = 2;
606 to->di_flushiter = ip->i_flushiter;
607 memset(to->di_v2_pad, 0, sizeof(to->di_v2_pad));
608 to->di_metatype = 0;
609 }
610
611 xfs_inode_to_log_dinode_iext_counters(ip, to);
612 }
613
614 /*
615 * Format the inode core. Current timestamp data is only in the VFS inode
616 * fields, so we need to grab them from there. Hence rather than just copying
617 * the XFS inode core structure, format the fields directly into the iovec.
618 */
619 static void
xfs_inode_item_format_core(struct xfs_inode * ip,struct xlog_format_buf * lfb)620 xfs_inode_item_format_core(
621 struct xfs_inode *ip,
622 struct xlog_format_buf *lfb)
623 {
624 struct xfs_log_dinode *dic;
625
626 dic = xlog_format_start(lfb, XLOG_REG_TYPE_ICORE);
627 xfs_inode_to_log_dinode(ip, dic, ip->i_itemp->ili_item.li_lsn);
628 xlog_format_commit(lfb, xfs_log_dinode_size(ip->i_mount));
629 }
630
631 /*
632 * This is called to fill in the vector of log iovecs for the given inode
633 * log item. It fills the first item with an inode log format structure,
634 * the second with the on-disk inode structure, and a possible third and/or
635 * fourth with the inode data/extents/b-tree root and inode attributes
636 * data/extents/b-tree root.
637 *
638 * Note: Always use the 64 bit inode log format structure so we don't
639 * leave an uninitialised hole in the format item on 64 bit systems. Log
640 * recovery on 32 bit systems handles this just fine, so there's no reason
641 * for not using an initialising the properly padded structure all the time.
642 */
643 STATIC void
xfs_inode_item_format(struct xfs_log_item * lip,struct xlog_format_buf * lfb)644 xfs_inode_item_format(
645 struct xfs_log_item *lip,
646 struct xlog_format_buf *lfb)
647 {
648 struct xfs_inode_log_item *iip = INODE_ITEM(lip);
649 struct xfs_inode *ip = iip->ili_inode;
650 struct xfs_inode_log_format *ilf;
651
652 ilf = xlog_format_start(lfb, XLOG_REG_TYPE_IFORMAT);
653 ilf->ilf_type = XFS_LI_INODE;
654 ilf->ilf_ino = ip->i_ino;
655 ilf->ilf_blkno = ip->i_imap.im_blkno;
656 ilf->ilf_len = ip->i_imap.im_len;
657 ilf->ilf_boffset = ip->i_imap.im_boffset;
658 ilf->ilf_fields = XFS_ILOG_CORE;
659 ilf->ilf_size = 2; /* format + core */
660
661 /*
662 * make sure we don't leak uninitialised data into the log in the case
663 * when we don't log every field in the inode.
664 */
665 ilf->ilf_dsize = 0;
666 ilf->ilf_asize = 0;
667 ilf->ilf_pad = 0;
668 memset(&ilf->ilf_u, 0, sizeof(ilf->ilf_u));
669 xlog_format_commit(lfb, sizeof(*ilf));
670
671 xfs_inode_item_format_core(ip, lfb);
672 xfs_inode_item_format_data_fork(iip, ilf, lfb);
673 if (xfs_inode_has_attr_fork(ip)) {
674 xfs_inode_item_format_attr_fork(iip, ilf, lfb);
675 } else {
676 iip->ili_fields &=
677 ~(XFS_ILOG_ADATA | XFS_ILOG_ABROOT | XFS_ILOG_AEXT);
678 }
679
680 /* update the format with the exact fields we actually logged */
681 ilf->ilf_fields |= (iip->ili_fields & ~XFS_ILOG_TIMESTAMP);
682 }
683
684 /*
685 * This is called to pin the inode associated with the inode log
686 * item in memory so it cannot be written out.
687 */
688 STATIC void
xfs_inode_item_pin(struct xfs_log_item * lip)689 xfs_inode_item_pin(
690 struct xfs_log_item *lip)
691 {
692 struct xfs_inode *ip = INODE_ITEM(lip)->ili_inode;
693
694 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
695 ASSERT(lip->li_buf);
696
697 trace_xfs_inode_pin(ip, _RET_IP_);
698 atomic_inc(&ip->i_pincount);
699 }
700
701
702 /*
703 * This is called to unpin the inode associated with the inode log
704 * item which was previously pinned with a call to xfs_inode_item_pin().
705 *
706 * Also wake up anyone in xfs_iunpin_wait() if the count goes to 0.
707 *
708 * Note that unpin can race with inode cluster buffer freeing marking the buffer
709 * stale. In that case, flush completions are run from the buffer unpin call,
710 * which may happen before the inode is unpinned. If we lose the race, there
711 * will be no buffer attached to the log item, but the inode will be marked
712 * XFS_ISTALE.
713 */
714 STATIC void
xfs_inode_item_unpin(struct xfs_log_item * lip,int remove)715 xfs_inode_item_unpin(
716 struct xfs_log_item *lip,
717 int remove)
718 {
719 struct xfs_inode_log_item *iip = INODE_ITEM(lip);
720 struct xfs_inode *ip = iip->ili_inode;
721
722 trace_xfs_inode_unpin(ip, _RET_IP_);
723 ASSERT(lip->li_buf || xfs_iflags_test(ip, XFS_ISTALE));
724 ASSERT(atomic_read(&ip->i_pincount) > 0);
725
726 /*
727 * If this is the last unpin, then the inode no longer needs a journal
728 * flush to persist it. Hence we can clear the commit sequence numbers
729 * as a fsync/fdatasync operation on the inode at this point is a no-op.
730 */
731 if (atomic_dec_and_lock(&ip->i_pincount, &iip->ili_lock)) {
732 iip->ili_commit_seq = 0;
733 iip->ili_datasync_seq = 0;
734 spin_unlock(&iip->ili_lock);
735 wake_up_bit(&ip->i_flags, __XFS_IPINNED_BIT);
736 }
737 }
738
739 STATIC uint
xfs_inode_item_push(struct xfs_log_item * lip,struct list_head * buffer_list)740 xfs_inode_item_push(
741 struct xfs_log_item *lip,
742 struct list_head *buffer_list)
743 __releases(&lip->li_ailp->ail_lock)
744 __acquires(&lip->li_ailp->ail_lock)
745 {
746 struct xfs_inode_log_item *iip = INODE_ITEM(lip);
747 struct xfs_inode *ip = iip->ili_inode;
748 struct xfs_buf *bp = lip->li_buf;
749 struct xfs_ail *ailp = lip->li_ailp;
750 uint rval = XFS_ITEM_SUCCESS;
751 int error;
752
753 if (!bp || (ip->i_flags & XFS_ISTALE)) {
754 /*
755 * Inode item/buffer is being aborted due to cluster
756 * buffer deletion. Trigger a log force to have that operation
757 * completed and items removed from the AIL before the next push
758 * attempt.
759 */
760 trace_xfs_inode_push_stale(ip, _RET_IP_);
761 return XFS_ITEM_PINNED;
762 }
763
764 if (xfs_ipincount(ip) > 0 || xfs_buf_ispinned(bp)) {
765 trace_xfs_inode_push_pinned(ip, _RET_IP_);
766 return XFS_ITEM_PINNED;
767 }
768
769 if (xfs_iflags_test(ip, XFS_IFLUSHING))
770 return XFS_ITEM_FLUSHING;
771
772 if (!xfs_buf_trylock(bp))
773 return XFS_ITEM_LOCKED;
774
775 spin_unlock(&ailp->ail_lock);
776
777 /*
778 * We need to hold a reference for flushing the cluster buffer as it may
779 * fail the buffer without IO submission. In which case, we better get a
780 * reference for that completion because otherwise we don't get a
781 * reference for IO until we queue the buffer for delwri submission.
782 */
783 xfs_buf_hold(bp);
784 error = xfs_iflush_cluster(bp);
785 if (!error) {
786 if (!xfs_buf_delwri_queue(bp, buffer_list))
787 rval = XFS_ITEM_FLUSHING;
788 xfs_buf_relse(bp);
789 } else {
790 /*
791 * Release the buffer if we were unable to flush anything. On
792 * any other error, the buffer has already been released.
793 */
794 if (error == -EAGAIN)
795 xfs_buf_relse(bp);
796 rval = XFS_ITEM_LOCKED;
797 }
798
799 /*
800 * The buffer no longer protects the log item from reclaim, so
801 * do not reference lip after this point.
802 */
803 spin_lock(&ailp->ail_lock);
804 return rval;
805 }
806
807 /*
808 * Unlock the inode associated with the inode log item.
809 */
810 STATIC void
xfs_inode_item_release(struct xfs_log_item * lip)811 xfs_inode_item_release(
812 struct xfs_log_item *lip)
813 {
814 struct xfs_inode_log_item *iip = INODE_ITEM(lip);
815 struct xfs_inode *ip = iip->ili_inode;
816 unsigned short lock_flags;
817
818 ASSERT(ip->i_itemp != NULL);
819 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
820
821 lock_flags = iip->ili_lock_flags;
822 iip->ili_lock_flags = 0;
823 if (lock_flags)
824 xfs_iunlock(ip, lock_flags);
825 }
826
827 /*
828 * This is called to find out where the oldest active copy of the inode log
829 * item in the on disk log resides now that the last log write of it completed
830 * at the given lsn. Since we always re-log all dirty data in an inode, the
831 * latest copy in the on disk log is the only one that matters. Therefore,
832 * simply return the given lsn.
833 *
834 * If the inode has been marked stale because the cluster is being freed, we
835 * don't want to (re-)insert this inode into the AIL. There is a race condition
836 * where the cluster buffer may be unpinned before the inode is inserted into
837 * the AIL during transaction committed processing. If the buffer is unpinned
838 * before the inode item has been committed and inserted, then it is possible
839 * for the buffer to be written and IO completes before the inode is inserted
840 * into the AIL. In that case, we'd be inserting a clean, stale inode into the
841 * AIL which will never get removed. It will, however, get reclaimed which
842 * triggers an assert in xfs_inode_free() complaining about freein an inode
843 * still in the AIL.
844 *
845 * To avoid this, just unpin the inode directly and return a LSN of -1 so the
846 * transaction committed code knows that it does not need to do any further
847 * processing on the item.
848 */
849 STATIC xfs_lsn_t
xfs_inode_item_committed(struct xfs_log_item * lip,xfs_lsn_t lsn)850 xfs_inode_item_committed(
851 struct xfs_log_item *lip,
852 xfs_lsn_t lsn)
853 {
854 struct xfs_inode_log_item *iip = INODE_ITEM(lip);
855 struct xfs_inode *ip = iip->ili_inode;
856
857 if (xfs_iflags_test(ip, XFS_ISTALE)) {
858 xfs_inode_item_unpin(lip, 0);
859 return -1;
860 }
861 return lsn;
862 }
863
864 /*
865 * The modification is now complete, so before we unlock the inode we need to
866 * update the commit sequence numbers for data integrity journal flushes. We
867 * always record the commit sequence number (ili_commit_seq) so that anything
868 * that needs a full journal sync will capture all of this modification.
869 *
870 * We then
871 * check if the changes will impact a datasync (O_DSYNC) journal flush. If the
872 * changes will require a datasync flush, then we also record the sequence in
873 * ili_datasync_seq.
874 *
875 * These commit sequence numbers will get cleared atomically with the inode being
876 * unpinned (i.e. pin count goes to zero), and so it will only be set when the
877 * inode is dirty in the journal. This removes the need for checking if the
878 * inode is pinned to determine if a journal flush is necessary, and hence
879 * removes the need for holding the ILOCK_SHARED in xfs_file_fsync() to
880 * serialise pin counts against commit sequence number updates.
881 *
882 */
883 STATIC void
xfs_inode_item_committing(struct xfs_log_item * lip,xfs_csn_t seq)884 xfs_inode_item_committing(
885 struct xfs_log_item *lip,
886 xfs_csn_t seq)
887 {
888 struct xfs_inode_log_item *iip = INODE_ITEM(lip);
889
890 spin_lock(&iip->ili_lock);
891 iip->ili_commit_seq = seq;
892 if (iip->ili_dirty_flags & ~(XFS_ILOG_IVERSION | XFS_ILOG_TIMESTAMP))
893 iip->ili_datasync_seq = seq;
894 spin_unlock(&iip->ili_lock);
895
896 /*
897 * Clear the per-transaction dirty flags now that we have finished
898 * recording the transaction's inode modifications in the CIL and are
899 * about to release and (maybe) unlock the inode.
900 */
901 iip->ili_dirty_flags = 0;
902
903 return xfs_inode_item_release(lip);
904 }
905
906 static const struct xfs_item_ops xfs_inode_item_ops = {
907 .iop_sort = xfs_inode_item_sort,
908 .iop_precommit = xfs_inode_item_precommit,
909 .iop_size = xfs_inode_item_size,
910 .iop_format = xfs_inode_item_format,
911 .iop_pin = xfs_inode_item_pin,
912 .iop_unpin = xfs_inode_item_unpin,
913 .iop_release = xfs_inode_item_release,
914 .iop_committed = xfs_inode_item_committed,
915 .iop_push = xfs_inode_item_push,
916 .iop_committing = xfs_inode_item_committing,
917 };
918
919
920 /*
921 * Initialize the inode log item for a newly allocated (in-core) inode.
922 */
923 void
xfs_inode_item_init(struct xfs_inode * ip,struct xfs_mount * mp)924 xfs_inode_item_init(
925 struct xfs_inode *ip,
926 struct xfs_mount *mp)
927 {
928 struct xfs_inode_log_item *iip;
929
930 ASSERT(ip->i_itemp == NULL);
931 iip = ip->i_itemp = kmem_cache_zalloc(xfs_ili_cache,
932 GFP_KERNEL | __GFP_NOFAIL);
933
934 iip->ili_inode = ip;
935 spin_lock_init(&iip->ili_lock);
936 xfs_log_item_init(mp, &iip->ili_item, XFS_LI_INODE,
937 &xfs_inode_item_ops);
938 }
939
940 /*
941 * Free the inode log item and any memory hanging off of it.
942 */
943 void
xfs_inode_item_destroy(struct xfs_inode * ip)944 xfs_inode_item_destroy(
945 struct xfs_inode *ip)
946 {
947 struct xfs_inode_log_item *iip = ip->i_itemp;
948
949 ASSERT(iip->ili_item.li_buf == NULL);
950
951 ip->i_itemp = NULL;
952 kvfree(iip->ili_item.li_lv_shadow);
953 kmem_cache_free(xfs_ili_cache, iip);
954 }
955
956
957 /*
958 * We only want to pull the item from the AIL if it is actually there
959 * and its location in the log has not changed since we started the
960 * flush. Thus, we only bother if the inode's lsn has not changed.
961 */
962 static void
xfs_iflush_ail_updates(struct xfs_ail * ailp,struct list_head * list)963 xfs_iflush_ail_updates(
964 struct xfs_ail *ailp,
965 struct list_head *list)
966 {
967 struct xfs_log_item *lip;
968 xfs_lsn_t tail_lsn = 0;
969
970 /* this is an opencoded batch version of xfs_trans_ail_delete */
971 spin_lock(&ailp->ail_lock);
972 list_for_each_entry(lip, list, li_bio_list) {
973 xfs_lsn_t lsn;
974
975 clear_bit(XFS_LI_FAILED, &lip->li_flags);
976 if (INODE_ITEM(lip)->ili_flush_lsn != lip->li_lsn)
977 continue;
978
979 /*
980 * dgc: Not sure how this happens, but it happens very
981 * occassionaly via generic/388. xfs_iflush_abort() also
982 * silently handles this same "under writeback but not in AIL at
983 * shutdown" condition via xfs_trans_ail_delete().
984 */
985 if (!test_bit(XFS_LI_IN_AIL, &lip->li_flags)) {
986 ASSERT(xlog_is_shutdown(lip->li_log));
987 continue;
988 }
989
990 lsn = xfs_ail_delete_one(ailp, lip);
991 if (!tail_lsn && lsn)
992 tail_lsn = lsn;
993 }
994 xfs_ail_update_finish(ailp, tail_lsn);
995 }
996
997 /*
998 * Walk the list of inodes that have completed their IOs. If they are clean
999 * remove them from the list and dissociate them from the buffer. Buffers that
1000 * are still dirty remain linked to the buffer and on the list. Caller must
1001 * handle them appropriately.
1002 */
1003 static void
xfs_iflush_finish(struct xfs_buf * bp,struct list_head * list)1004 xfs_iflush_finish(
1005 struct xfs_buf *bp,
1006 struct list_head *list)
1007 {
1008 struct xfs_log_item *lip, *n;
1009
1010 list_for_each_entry_safe(lip, n, list, li_bio_list) {
1011 struct xfs_inode_log_item *iip = INODE_ITEM(lip);
1012 bool drop_buffer = false;
1013
1014 spin_lock(&iip->ili_lock);
1015
1016 /*
1017 * Remove the reference to the cluster buffer if the inode is
1018 * clean in memory and drop the buffer reference once we've
1019 * dropped the locks we hold.
1020 */
1021 ASSERT(iip->ili_item.li_buf == bp);
1022 if (!iip->ili_fields) {
1023 iip->ili_item.li_buf = NULL;
1024 list_del_init(&lip->li_bio_list);
1025 drop_buffer = true;
1026 }
1027 iip->ili_last_fields = 0;
1028 iip->ili_flush_lsn = 0;
1029 clear_bit(XFS_LI_FLUSHING, &lip->li_flags);
1030 spin_unlock(&iip->ili_lock);
1031 xfs_iflags_clear(iip->ili_inode, XFS_IFLUSHING);
1032 if (drop_buffer)
1033 xfs_buf_rele(bp);
1034 }
1035 }
1036
1037 /*
1038 * Inode buffer IO completion routine. It is responsible for removing inodes
1039 * attached to the buffer from the AIL if they have not been re-logged and
1040 * completing the inode flush.
1041 */
1042 void
xfs_buf_inode_iodone(struct xfs_buf * bp)1043 xfs_buf_inode_iodone(
1044 struct xfs_buf *bp)
1045 {
1046 struct xfs_log_item *lip, *n;
1047 LIST_HEAD(flushed_inodes);
1048 LIST_HEAD(ail_updates);
1049
1050 /*
1051 * Pull the attached inodes from the buffer one at a time and take the
1052 * appropriate action on them.
1053 */
1054 list_for_each_entry_safe(lip, n, &bp->b_li_list, li_bio_list) {
1055 struct xfs_inode_log_item *iip = INODE_ITEM(lip);
1056
1057 if (xfs_iflags_test(iip->ili_inode, XFS_ISTALE)) {
1058 xfs_iflush_abort(iip->ili_inode);
1059 continue;
1060 }
1061 if (!iip->ili_last_fields)
1062 continue;
1063
1064 /* Do an unlocked check for needing the AIL lock. */
1065 if (iip->ili_flush_lsn == lip->li_lsn ||
1066 test_bit(XFS_LI_FAILED, &lip->li_flags))
1067 list_move_tail(&lip->li_bio_list, &ail_updates);
1068 else
1069 list_move_tail(&lip->li_bio_list, &flushed_inodes);
1070 }
1071
1072 if (!list_empty(&ail_updates)) {
1073 xfs_iflush_ail_updates(bp->b_mount->m_ail, &ail_updates);
1074 list_splice_tail(&ail_updates, &flushed_inodes);
1075 }
1076
1077 xfs_iflush_finish(bp, &flushed_inodes);
1078 if (!list_empty(&flushed_inodes))
1079 list_splice_tail(&flushed_inodes, &bp->b_li_list);
1080 }
1081
1082 /*
1083 * Clear the inode logging fields so no more flushes are attempted. If we are
1084 * on a buffer list, it is now safe to remove it because the buffer is
1085 * guaranteed to be locked. The caller will drop the reference to the buffer
1086 * the log item held.
1087 */
1088 static void
xfs_iflush_abort_clean(struct xfs_inode_log_item * iip)1089 xfs_iflush_abort_clean(
1090 struct xfs_inode_log_item *iip)
1091 {
1092 iip->ili_last_fields = 0;
1093 iip->ili_fields = 0;
1094 iip->ili_flush_lsn = 0;
1095 iip->ili_item.li_buf = NULL;
1096 list_del_init(&iip->ili_item.li_bio_list);
1097 clear_bit(XFS_LI_FLUSHING, &iip->ili_item.li_flags);
1098 }
1099
1100 /*
1101 * Abort flushing the inode from a context holding the cluster buffer locked.
1102 *
1103 * This is the normal runtime method of aborting writeback of an inode that is
1104 * attached to a cluster buffer. It occurs when the inode and the backing
1105 * cluster buffer have been freed (i.e. inode is XFS_ISTALE), or when cluster
1106 * flushing or buffer IO completion encounters a log shutdown situation.
1107 *
1108 * If we need to abort inode writeback and we don't already hold the buffer
1109 * locked, call xfs_iflush_shutdown_abort() instead as this should only ever be
1110 * necessary in a shutdown situation.
1111 */
1112 void
xfs_iflush_abort(struct xfs_inode * ip)1113 xfs_iflush_abort(
1114 struct xfs_inode *ip)
1115 {
1116 struct xfs_inode_log_item *iip = ip->i_itemp;
1117 struct xfs_buf *bp;
1118
1119 if (!iip) {
1120 /* clean inode, nothing to do */
1121 xfs_iflags_clear(ip, XFS_IFLUSHING);
1122 return;
1123 }
1124
1125 /*
1126 * Remove the inode item from the AIL before we clear its internal
1127 * state. Whilst the inode is in the AIL, it should have a valid buffer
1128 * pointer for push operations to access - it is only safe to remove the
1129 * inode from the buffer once it has been removed from the AIL.
1130 */
1131 xfs_trans_ail_delete(&iip->ili_item, 0);
1132
1133 /*
1134 * Grab the inode buffer so can we release the reference the inode log
1135 * item holds on it.
1136 */
1137 spin_lock(&iip->ili_lock);
1138 bp = iip->ili_item.li_buf;
1139 xfs_iflush_abort_clean(iip);
1140 spin_unlock(&iip->ili_lock);
1141
1142 xfs_iflags_clear(ip, XFS_IFLUSHING);
1143 if (bp)
1144 xfs_buf_rele(bp);
1145 }
1146
1147 /*
1148 * Abort an inode flush in the case of a shutdown filesystem. This can be called
1149 * from anywhere with just an inode reference and does not require holding the
1150 * inode cluster buffer locked. If the inode is attached to a cluster buffer,
1151 * it will grab and lock it safely, then abort the inode flush.
1152 */
1153 void
xfs_iflush_shutdown_abort(struct xfs_inode * ip)1154 xfs_iflush_shutdown_abort(
1155 struct xfs_inode *ip)
1156 {
1157 struct xfs_inode_log_item *iip = ip->i_itemp;
1158 struct xfs_buf *bp;
1159
1160 if (!iip) {
1161 /* clean inode, nothing to do */
1162 xfs_iflags_clear(ip, XFS_IFLUSHING);
1163 return;
1164 }
1165
1166 spin_lock(&iip->ili_lock);
1167 bp = iip->ili_item.li_buf;
1168 if (!bp) {
1169 spin_unlock(&iip->ili_lock);
1170 xfs_iflush_abort(ip);
1171 return;
1172 }
1173
1174 /*
1175 * We have to take a reference to the buffer so that it doesn't get
1176 * freed when we drop the ili_lock and then wait to lock the buffer.
1177 * We'll clean up the extra reference after we pick up the ili_lock
1178 * again.
1179 */
1180 xfs_buf_hold(bp);
1181 spin_unlock(&iip->ili_lock);
1182 xfs_buf_lock(bp);
1183
1184 spin_lock(&iip->ili_lock);
1185 if (!iip->ili_item.li_buf) {
1186 /*
1187 * Raced with another removal, hold the only reference
1188 * to bp now. Inode should not be in the AIL now, so just clean
1189 * up and return;
1190 */
1191 ASSERT(list_empty(&iip->ili_item.li_bio_list));
1192 ASSERT(!test_bit(XFS_LI_IN_AIL, &iip->ili_item.li_flags));
1193 xfs_iflush_abort_clean(iip);
1194 spin_unlock(&iip->ili_lock);
1195 xfs_iflags_clear(ip, XFS_IFLUSHING);
1196 xfs_buf_relse(bp);
1197 return;
1198 }
1199
1200 /*
1201 * Got two references to bp. The first will get dropped by
1202 * xfs_iflush_abort() when the item is removed from the buffer list, but
1203 * we can't drop our reference until _abort() returns because we have to
1204 * unlock the buffer as well. Hence we abort and then unlock and release
1205 * our reference to the buffer.
1206 */
1207 ASSERT(iip->ili_item.li_buf == bp);
1208 spin_unlock(&iip->ili_lock);
1209 xfs_iflush_abort(ip);
1210 xfs_buf_relse(bp);
1211 }
1212
1213
1214 /*
1215 * convert an xfs_inode_log_format struct from the old 32 bit version
1216 * (which can have different field alignments) to the native 64 bit version
1217 */
1218 int
xfs_inode_item_format_convert(struct kvec * buf,struct xfs_inode_log_format * in_f)1219 xfs_inode_item_format_convert(
1220 struct kvec *buf,
1221 struct xfs_inode_log_format *in_f)
1222 {
1223 struct xfs_inode_log_format_32 *in_f32 = buf->iov_base;
1224
1225 if (buf->iov_len != sizeof(*in_f32)) {
1226 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, NULL);
1227 return -EFSCORRUPTED;
1228 }
1229
1230 in_f->ilf_type = in_f32->ilf_type;
1231 in_f->ilf_size = in_f32->ilf_size;
1232 in_f->ilf_fields = in_f32->ilf_fields;
1233 in_f->ilf_asize = in_f32->ilf_asize;
1234 in_f->ilf_dsize = in_f32->ilf_dsize;
1235 in_f->ilf_ino = in_f32->ilf_ino;
1236 memcpy(&in_f->ilf_u, &in_f32->ilf_u, sizeof(in_f->ilf_u));
1237 in_f->ilf_blkno = in_f32->ilf_blkno;
1238 in_f->ilf_len = in_f32->ilf_len;
1239 in_f->ilf_boffset = in_f32->ilf_boffset;
1240 return 0;
1241 }
1242