efa6e5938fbafa3ddf2006109fb5cfb0e5b21bdb
[cascardo/linux.git] / fs / xfs / xfs_inode_item.c
1 /*
2  * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
3  * All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it would be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write the Free Software Foundation,
16  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_format.h"
21 #include "xfs_log_format.h"
22 #include "xfs_trans_resv.h"
23 #include "xfs_sb.h"
24 #include "xfs_ag.h"
25 #include "xfs_mount.h"
26 #include "xfs_inode.h"
27 #include "xfs_trans.h"
28 #include "xfs_inode_item.h"
29 #include "xfs_error.h"
30 #include "xfs_trace.h"
31 #include "xfs_trans_priv.h"
32 #include "xfs_log.h"
33
34
35 kmem_zone_t     *xfs_ili_zone;          /* inode log item zone */
36
37 static inline struct xfs_inode_log_item *INODE_ITEM(struct xfs_log_item *lip)
38 {
39         return container_of(lip, struct xfs_inode_log_item, ili_item);
40 }
41
42 STATIC void
43 xfs_inode_item_data_fork_size(
44         struct xfs_inode_log_item *iip,
45         int                     *nvecs,
46         int                     *nbytes)
47 {
48         struct xfs_inode        *ip = iip->ili_inode;
49
50         switch (ip->i_d.di_format) {
51         case XFS_DINODE_FMT_EXTENTS:
52                 if ((iip->ili_fields & XFS_ILOG_DEXT) &&
53                     ip->i_d.di_nextents > 0 &&
54                     ip->i_df.if_bytes > 0) {
55                         /* worst case, doesn't subtract delalloc extents */
56                         *nbytes += XFS_IFORK_DSIZE(ip);
57                         *nvecs += 1;
58                 }
59                 break;
60         case XFS_DINODE_FMT_BTREE:
61                 if ((iip->ili_fields & XFS_ILOG_DBROOT) &&
62                     ip->i_df.if_broot_bytes > 0) {
63                         *nbytes += ip->i_df.if_broot_bytes;
64                         *nvecs += 1;
65                 }
66                 break;
67         case XFS_DINODE_FMT_LOCAL:
68                 if ((iip->ili_fields & XFS_ILOG_DDATA) &&
69                     ip->i_df.if_bytes > 0) {
70                         *nbytes += roundup(ip->i_df.if_bytes, 4);
71                         *nvecs += 1;
72                 }
73                 break;
74
75         case XFS_DINODE_FMT_DEV:
76         case XFS_DINODE_FMT_UUID:
77                 break;
78         default:
79                 ASSERT(0);
80                 break;
81         }
82 }
83
84 STATIC void
85 xfs_inode_item_attr_fork_size(
86         struct xfs_inode_log_item *iip,
87         int                     *nvecs,
88         int                     *nbytes)
89 {
90         struct xfs_inode        *ip = iip->ili_inode;
91
92         switch (ip->i_d.di_aformat) {
93         case XFS_DINODE_FMT_EXTENTS:
94                 if ((iip->ili_fields & XFS_ILOG_AEXT) &&
95                     ip->i_d.di_anextents > 0 &&
96                     ip->i_afp->if_bytes > 0) {
97                         /* worst case, doesn't subtract unused space */
98                         *nbytes += XFS_IFORK_ASIZE(ip);
99                         *nvecs += 1;
100                 }
101                 break;
102         case XFS_DINODE_FMT_BTREE:
103                 if ((iip->ili_fields & XFS_ILOG_ABROOT) &&
104                     ip->i_afp->if_broot_bytes > 0) {
105                         *nbytes += ip->i_afp->if_broot_bytes;
106                         *nvecs += 1;
107                 }
108                 break;
109         case XFS_DINODE_FMT_LOCAL:
110                 if ((iip->ili_fields & XFS_ILOG_ADATA) &&
111                     ip->i_afp->if_bytes > 0) {
112                         *nbytes += roundup(ip->i_afp->if_bytes, 4);
113                         *nvecs += 1;
114                 }
115                 break;
116         default:
117                 ASSERT(0);
118                 break;
119         }
120 }
121
122 /*
123  * This returns the number of iovecs needed to log the given inode item.
124  *
125  * We need one iovec for the inode log format structure, one for the
126  * inode core, and possibly one for the inode data/extents/b-tree root
127  * and one for the inode attribute data/extents/b-tree root.
128  */
129 STATIC void
130 xfs_inode_item_size(
131         struct xfs_log_item     *lip,
132         int                     *nvecs,
133         int                     *nbytes)
134 {
135         struct xfs_inode_log_item *iip = INODE_ITEM(lip);
136         struct xfs_inode        *ip = iip->ili_inode;
137
138         *nvecs += 2;
139         *nbytes += sizeof(struct xfs_inode_log_format) +
140                    xfs_icdinode_size(ip->i_d.di_version);
141
142         xfs_inode_item_data_fork_size(iip, nvecs, nbytes);
143         if (XFS_IFORK_Q(ip))
144                 xfs_inode_item_attr_fork_size(iip, nvecs, nbytes);
145 }
146
147 STATIC void
148 xfs_inode_item_format_data_fork(
149         struct xfs_inode_log_item *iip,
150         struct xfs_inode_log_format *ilf,
151         struct xfs_log_vec      *lv,
152         struct xfs_log_iovec    **vecp)
153 {
154         struct xfs_inode        *ip = iip->ili_inode;
155         size_t                  data_bytes;
156
157         switch (ip->i_d.di_format) {
158         case XFS_DINODE_FMT_EXTENTS:
159                 iip->ili_fields &=
160                         ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT |
161                           XFS_ILOG_DEV | XFS_ILOG_UUID);
162
163                 if ((iip->ili_fields & XFS_ILOG_DEXT) &&
164                     ip->i_d.di_nextents > 0 &&
165                     ip->i_df.if_bytes > 0) {
166                         struct xfs_bmbt_rec *p;
167
168                         ASSERT(ip->i_df.if_u1.if_extents != NULL);
169                         ASSERT(ip->i_df.if_bytes / sizeof(xfs_bmbt_rec_t) > 0);
170
171                         p = xlog_prepare_iovec(lv, vecp, XLOG_REG_TYPE_IEXT);
172                         data_bytes = xfs_iextents_copy(ip, p, XFS_DATA_FORK);
173                         xlog_finish_iovec(lv, *vecp, data_bytes);
174
175                         ASSERT(data_bytes <= ip->i_df.if_bytes);
176
177                         ilf->ilf_dsize = data_bytes;
178                         ilf->ilf_size++;
179                 } else {
180                         iip->ili_fields &= ~XFS_ILOG_DEXT;
181                 }
182                 break;
183         case XFS_DINODE_FMT_BTREE:
184                 iip->ili_fields &=
185                         ~(XFS_ILOG_DDATA | XFS_ILOG_DEXT |
186                           XFS_ILOG_DEV | XFS_ILOG_UUID);
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 |
205                           XFS_ILOG_DEV | XFS_ILOG_UUID);
206                 if ((iip->ili_fields & XFS_ILOG_DDATA) &&
207                     ip->i_df.if_bytes > 0) {
208                         /*
209                          * Round i_bytes up to a word boundary.
210                          * The underlying memory is guaranteed to
211                          * to be there by xfs_idata_realloc().
212                          */
213                         data_bytes = roundup(ip->i_df.if_bytes, 4);
214                         ASSERT(ip->i_df.if_real_bytes == 0 ||
215                                ip->i_df.if_real_bytes == data_bytes);
216                         ASSERT(ip->i_df.if_u1.if_data != NULL);
217                         ASSERT(ip->i_d.di_size > 0);
218                         xlog_copy_iovec(lv, vecp, XLOG_REG_TYPE_ILOCAL,
219                                         ip->i_df.if_u1.if_data, data_bytes);
220                         ilf->ilf_dsize = (unsigned)data_bytes;
221                         ilf->ilf_size++;
222                 } else {
223                         iip->ili_fields &= ~XFS_ILOG_DDATA;
224                 }
225                 break;
226         case XFS_DINODE_FMT_DEV:
227                 iip->ili_fields &=
228                         ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT |
229                           XFS_ILOG_DEXT | XFS_ILOG_UUID);
230                 if (iip->ili_fields & XFS_ILOG_DEV)
231                         ilf->ilf_u.ilfu_rdev = ip->i_df.if_u2.if_rdev;
232                 break;
233         case XFS_DINODE_FMT_UUID:
234                 iip->ili_fields &=
235                         ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT |
236                           XFS_ILOG_DEXT | XFS_ILOG_DEV);
237                 if (iip->ili_fields & XFS_ILOG_UUID)
238                         ilf->ilf_u.ilfu_uuid = ip->i_df.if_u2.if_uuid;
239                 break;
240         default:
241                 ASSERT(0);
242                 break;
243         }
244 }
245
246 STATIC void
247 xfs_inode_item_format_attr_fork(
248         struct xfs_inode_log_item *iip,
249         struct xfs_inode_log_format *ilf,
250         struct xfs_log_vec      *lv,
251         struct xfs_log_iovec    **vecp)
252 {
253         struct xfs_inode        *ip = iip->ili_inode;
254         size_t                  data_bytes;
255
256         switch (ip->i_d.di_aformat) {
257         case XFS_DINODE_FMT_EXTENTS:
258                 iip->ili_fields &=
259                         ~(XFS_ILOG_ADATA | XFS_ILOG_ABROOT);
260
261                 if ((iip->ili_fields & XFS_ILOG_AEXT) &&
262                     ip->i_d.di_anextents > 0 &&
263                     ip->i_afp->if_bytes > 0) {
264                         struct xfs_bmbt_rec *p;
265
266                         ASSERT(ip->i_afp->if_bytes / sizeof(xfs_bmbt_rec_t) ==
267                                 ip->i_d.di_anextents);
268                         ASSERT(ip->i_afp->if_u1.if_extents != NULL);
269
270                         p = xlog_prepare_iovec(lv, vecp, XLOG_REG_TYPE_IATTR_EXT);
271                         data_bytes = xfs_iextents_copy(ip, p, XFS_ATTR_FORK);
272                         xlog_finish_iovec(lv, *vecp, data_bytes);
273
274                         ilf->ilf_asize = data_bytes;
275                         ilf->ilf_size++;
276                 } else {
277                         iip->ili_fields &= ~XFS_ILOG_AEXT;
278                 }
279                 break;
280         case XFS_DINODE_FMT_BTREE:
281                 iip->ili_fields &=
282                         ~(XFS_ILOG_ADATA | XFS_ILOG_AEXT);
283
284                 if ((iip->ili_fields & XFS_ILOG_ABROOT) &&
285                     ip->i_afp->if_broot_bytes > 0) {
286                         ASSERT(ip->i_afp->if_broot != NULL);
287
288                         xlog_copy_iovec(lv, vecp, XLOG_REG_TYPE_IATTR_BROOT,
289                                         ip->i_afp->if_broot,
290                                         ip->i_afp->if_broot_bytes);
291                         ilf->ilf_asize = ip->i_afp->if_broot_bytes;
292                         ilf->ilf_size++;
293                 } else {
294                         iip->ili_fields &= ~XFS_ILOG_ABROOT;
295                 }
296                 break;
297         case XFS_DINODE_FMT_LOCAL:
298                 iip->ili_fields &=
299                         ~(XFS_ILOG_AEXT | XFS_ILOG_ABROOT);
300
301                 if ((iip->ili_fields & XFS_ILOG_ADATA) &&
302                     ip->i_afp->if_bytes > 0) {
303                         /*
304                          * Round i_bytes up to a word boundary.
305                          * The underlying memory is guaranteed to
306                          * to be there by xfs_idata_realloc().
307                          */
308                         data_bytes = roundup(ip->i_afp->if_bytes, 4);
309                         ASSERT(ip->i_afp->if_real_bytes == 0 ||
310                                ip->i_afp->if_real_bytes == data_bytes);
311                         ASSERT(ip->i_afp->if_u1.if_data != NULL);
312                         xlog_copy_iovec(lv, vecp, XLOG_REG_TYPE_IATTR_LOCAL,
313                                         ip->i_afp->if_u1.if_data,
314                                         data_bytes);
315                         ilf->ilf_asize = (unsigned)data_bytes;
316                         ilf->ilf_size++;
317                 } else {
318                         iip->ili_fields &= ~XFS_ILOG_ADATA;
319                 }
320                 break;
321         default:
322                 ASSERT(0);
323                 break;
324         }
325 }
326
327 /*
328  * This is called to fill in the vector of log iovecs for the given inode
329  * log item.  It fills the first item with an inode log format structure,
330  * the second with the on-disk inode structure, and a possible third and/or
331  * fourth with the inode data/extents/b-tree root and inode attributes
332  * data/extents/b-tree root.
333  */
334 STATIC void
335 xfs_inode_item_format(
336         struct xfs_log_item     *lip,
337         struct xfs_log_vec      *lv)
338 {
339         struct xfs_inode_log_item *iip = INODE_ITEM(lip);
340         struct xfs_inode        *ip = iip->ili_inode;
341         struct xfs_inode_log_format *ilf;
342         struct xfs_log_iovec    *vecp = NULL;
343
344         ASSERT(ip->i_d.di_version > 1);
345
346         ilf = xlog_prepare_iovec(lv, &vecp, XLOG_REG_TYPE_IFORMAT);
347         ilf->ilf_type = XFS_LI_INODE;
348         ilf->ilf_ino = ip->i_ino;
349         ilf->ilf_blkno = ip->i_imap.im_blkno;
350         ilf->ilf_len = ip->i_imap.im_len;
351         ilf->ilf_boffset = ip->i_imap.im_boffset;
352         ilf->ilf_fields = XFS_ILOG_CORE;
353         ilf->ilf_size = 2; /* format + core */
354         xlog_finish_iovec(lv, vecp, sizeof(struct xfs_inode_log_format));
355
356         xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_ICORE,
357                         &ip->i_d,
358                         xfs_icdinode_size(ip->i_d.di_version));
359
360         xfs_inode_item_format_data_fork(iip, ilf, lv, &vecp);
361         if (XFS_IFORK_Q(ip)) {
362                 xfs_inode_item_format_attr_fork(iip, ilf, lv, &vecp);
363         } else {
364                 iip->ili_fields &=
365                         ~(XFS_ILOG_ADATA | XFS_ILOG_ABROOT | XFS_ILOG_AEXT);
366         }
367
368         /* update the format with the exact fields we actually logged */
369         ilf->ilf_fields |= (iip->ili_fields & ~XFS_ILOG_TIMESTAMP);
370 }
371
372 /*
373  * This is called to pin the inode associated with the inode log
374  * item in memory so it cannot be written out.
375  */
376 STATIC void
377 xfs_inode_item_pin(
378         struct xfs_log_item     *lip)
379 {
380         struct xfs_inode        *ip = INODE_ITEM(lip)->ili_inode;
381
382         ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
383
384         trace_xfs_inode_pin(ip, _RET_IP_);
385         atomic_inc(&ip->i_pincount);
386 }
387
388
389 /*
390  * This is called to unpin the inode associated with the inode log
391  * item which was previously pinned with a call to xfs_inode_item_pin().
392  *
393  * Also wake up anyone in xfs_iunpin_wait() if the count goes to 0.
394  */
395 STATIC void
396 xfs_inode_item_unpin(
397         struct xfs_log_item     *lip,
398         int                     remove)
399 {
400         struct xfs_inode        *ip = INODE_ITEM(lip)->ili_inode;
401
402         trace_xfs_inode_unpin(ip, _RET_IP_);
403         ASSERT(atomic_read(&ip->i_pincount) > 0);
404         if (atomic_dec_and_test(&ip->i_pincount))
405                 wake_up_bit(&ip->i_flags, __XFS_IPINNED_BIT);
406 }
407
408 STATIC uint
409 xfs_inode_item_push(
410         struct xfs_log_item     *lip,
411         struct list_head        *buffer_list)
412 {
413         struct xfs_inode_log_item *iip = INODE_ITEM(lip);
414         struct xfs_inode        *ip = iip->ili_inode;
415         struct xfs_buf          *bp = NULL;
416         uint                    rval = XFS_ITEM_SUCCESS;
417         int                     error;
418
419         if (xfs_ipincount(ip) > 0)
420                 return XFS_ITEM_PINNED;
421
422         if (!xfs_ilock_nowait(ip, XFS_ILOCK_SHARED))
423                 return XFS_ITEM_LOCKED;
424
425         /*
426          * Re-check the pincount now that we stabilized the value by
427          * taking the ilock.
428          */
429         if (xfs_ipincount(ip) > 0) {
430                 rval = XFS_ITEM_PINNED;
431                 goto out_unlock;
432         }
433
434         /*
435          * Stale inode items should force out the iclog.
436          */
437         if (ip->i_flags & XFS_ISTALE) {
438                 rval = XFS_ITEM_PINNED;
439                 goto out_unlock;
440         }
441
442         /*
443          * Someone else is already flushing the inode.  Nothing we can do
444          * here but wait for the flush to finish and remove the item from
445          * the AIL.
446          */
447         if (!xfs_iflock_nowait(ip)) {
448                 rval = XFS_ITEM_FLUSHING;
449                 goto out_unlock;
450         }
451
452         ASSERT(iip->ili_fields != 0 || XFS_FORCED_SHUTDOWN(ip->i_mount));
453         ASSERT(iip->ili_logged == 0 || XFS_FORCED_SHUTDOWN(ip->i_mount));
454
455         spin_unlock(&lip->li_ailp->xa_lock);
456
457         error = xfs_iflush(ip, &bp);
458         if (!error) {
459                 if (!xfs_buf_delwri_queue(bp, buffer_list))
460                         rval = XFS_ITEM_FLUSHING;
461                 xfs_buf_relse(bp);
462         }
463
464         spin_lock(&lip->li_ailp->xa_lock);
465 out_unlock:
466         xfs_iunlock(ip, XFS_ILOCK_SHARED);
467         return rval;
468 }
469
470 /*
471  * Unlock the inode associated with the inode log item.
472  * Clear the fields of the inode and inode log item that
473  * are specific to the current transaction.  If the
474  * hold flags is set, do not unlock the inode.
475  */
476 STATIC void
477 xfs_inode_item_unlock(
478         struct xfs_log_item     *lip)
479 {
480         struct xfs_inode_log_item *iip = INODE_ITEM(lip);
481         struct xfs_inode        *ip = iip->ili_inode;
482         unsigned short          lock_flags;
483
484         ASSERT(ip->i_itemp != NULL);
485         ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
486
487         lock_flags = iip->ili_lock_flags;
488         iip->ili_lock_flags = 0;
489         if (lock_flags)
490                 xfs_iunlock(ip, lock_flags);
491 }
492
493 /*
494  * This is called to find out where the oldest active copy of the inode log
495  * item in the on disk log resides now that the last log write of it completed
496  * at the given lsn.  Since we always re-log all dirty data in an inode, the
497  * latest copy in the on disk log is the only one that matters.  Therefore,
498  * simply return the given lsn.
499  *
500  * If the inode has been marked stale because the cluster is being freed, we
501  * don't want to (re-)insert this inode into the AIL. There is a race condition
502  * where the cluster buffer may be unpinned before the inode is inserted into
503  * the AIL during transaction committed processing. If the buffer is unpinned
504  * before the inode item has been committed and inserted, then it is possible
505  * for the buffer to be written and IO completes before the inode is inserted
506  * into the AIL. In that case, we'd be inserting a clean, stale inode into the
507  * AIL which will never get removed. It will, however, get reclaimed which
508  * triggers an assert in xfs_inode_free() complaining about freein an inode
509  * still in the AIL.
510  *
511  * To avoid this, just unpin the inode directly and return a LSN of -1 so the
512  * transaction committed code knows that it does not need to do any further
513  * processing on the item.
514  */
515 STATIC xfs_lsn_t
516 xfs_inode_item_committed(
517         struct xfs_log_item     *lip,
518         xfs_lsn_t               lsn)
519 {
520         struct xfs_inode_log_item *iip = INODE_ITEM(lip);
521         struct xfs_inode        *ip = iip->ili_inode;
522
523         if (xfs_iflags_test(ip, XFS_ISTALE)) {
524                 xfs_inode_item_unpin(lip, 0);
525                 return -1;
526         }
527         return lsn;
528 }
529
530 /*
531  * XXX rcc - this one really has to do something.  Probably needs
532  * to stamp in a new field in the incore inode.
533  */
534 STATIC void
535 xfs_inode_item_committing(
536         struct xfs_log_item     *lip,
537         xfs_lsn_t               lsn)
538 {
539         INODE_ITEM(lip)->ili_last_lsn = lsn;
540 }
541
542 /*
543  * This is the ops vector shared by all buf log items.
544  */
545 static const struct xfs_item_ops xfs_inode_item_ops = {
546         .iop_size       = xfs_inode_item_size,
547         .iop_format     = xfs_inode_item_format,
548         .iop_pin        = xfs_inode_item_pin,
549         .iop_unpin      = xfs_inode_item_unpin,
550         .iop_unlock     = xfs_inode_item_unlock,
551         .iop_committed  = xfs_inode_item_committed,
552         .iop_push       = xfs_inode_item_push,
553         .iop_committing = xfs_inode_item_committing
554 };
555
556
557 /*
558  * Initialize the inode log item for a newly allocated (in-core) inode.
559  */
560 void
561 xfs_inode_item_init(
562         struct xfs_inode        *ip,
563         struct xfs_mount        *mp)
564 {
565         struct xfs_inode_log_item *iip;
566
567         ASSERT(ip->i_itemp == NULL);
568         iip = ip->i_itemp = kmem_zone_zalloc(xfs_ili_zone, KM_SLEEP);
569
570         iip->ili_inode = ip;
571         xfs_log_item_init(mp, &iip->ili_item, XFS_LI_INODE,
572                                                 &xfs_inode_item_ops);
573 }
574
575 /*
576  * Free the inode log item and any memory hanging off of it.
577  */
578 void
579 xfs_inode_item_destroy(
580         xfs_inode_t     *ip)
581 {
582         kmem_zone_free(xfs_ili_zone, ip->i_itemp);
583 }
584
585
586 /*
587  * This is the inode flushing I/O completion routine.  It is called
588  * from interrupt level when the buffer containing the inode is
589  * flushed to disk.  It is responsible for removing the inode item
590  * from the AIL if it has not been re-logged, and unlocking the inode's
591  * flush lock.
592  *
593  * To reduce AIL lock traffic as much as possible, we scan the buffer log item
594  * list for other inodes that will run this function. We remove them from the
595  * buffer list so we can process all the inode IO completions in one AIL lock
596  * traversal.
597  */
598 void
599 xfs_iflush_done(
600         struct xfs_buf          *bp,
601         struct xfs_log_item     *lip)
602 {
603         struct xfs_inode_log_item *iip;
604         struct xfs_log_item     *blip;
605         struct xfs_log_item     *next;
606         struct xfs_log_item     *prev;
607         struct xfs_ail          *ailp = lip->li_ailp;
608         int                     need_ail = 0;
609
610         /*
611          * Scan the buffer IO completions for other inodes being completed and
612          * attach them to the current inode log item.
613          */
614         blip = bp->b_fspriv;
615         prev = NULL;
616         while (blip != NULL) {
617                 if (blip->li_cb != xfs_iflush_done) {
618                         prev = blip;
619                         blip = blip->li_bio_list;
620                         continue;
621                 }
622
623                 /* remove from list */
624                 next = blip->li_bio_list;
625                 if (!prev) {
626                         bp->b_fspriv = next;
627                 } else {
628                         prev->li_bio_list = next;
629                 }
630
631                 /* add to current list */
632                 blip->li_bio_list = lip->li_bio_list;
633                 lip->li_bio_list = blip;
634
635                 /*
636                  * while we have the item, do the unlocked check for needing
637                  * the AIL lock.
638                  */
639                 iip = INODE_ITEM(blip);
640                 if (iip->ili_logged && blip->li_lsn == iip->ili_flush_lsn)
641                         need_ail++;
642
643                 blip = next;
644         }
645
646         /* make sure we capture the state of the initial inode. */
647         iip = INODE_ITEM(lip);
648         if (iip->ili_logged && lip->li_lsn == iip->ili_flush_lsn)
649                 need_ail++;
650
651         /*
652          * We only want to pull the item from the AIL if it is
653          * actually there and its location in the log has not
654          * changed since we started the flush.  Thus, we only bother
655          * if the ili_logged flag is set and the inode's lsn has not
656          * changed.  First we check the lsn outside
657          * the lock since it's cheaper, and then we recheck while
658          * holding the lock before removing the inode from the AIL.
659          */
660         if (need_ail) {
661                 struct xfs_log_item *log_items[need_ail];
662                 int i = 0;
663                 spin_lock(&ailp->xa_lock);
664                 for (blip = lip; blip; blip = blip->li_bio_list) {
665                         iip = INODE_ITEM(blip);
666                         if (iip->ili_logged &&
667                             blip->li_lsn == iip->ili_flush_lsn) {
668                                 log_items[i++] = blip;
669                         }
670                         ASSERT(i <= need_ail);
671                 }
672                 /* xfs_trans_ail_delete_bulk() drops the AIL lock. */
673                 xfs_trans_ail_delete_bulk(ailp, log_items, i,
674                                           SHUTDOWN_CORRUPT_INCORE);
675         }
676
677
678         /*
679          * clean up and unlock the flush lock now we are done. We can clear the
680          * ili_last_fields bits now that we know that the data corresponding to
681          * them is safely on disk.
682          */
683         for (blip = lip; blip; blip = next) {
684                 next = blip->li_bio_list;
685                 blip->li_bio_list = NULL;
686
687                 iip = INODE_ITEM(blip);
688                 iip->ili_logged = 0;
689                 iip->ili_last_fields = 0;
690                 xfs_ifunlock(iip->ili_inode);
691         }
692 }
693
694 /*
695  * This is the inode flushing abort routine.  It is called from xfs_iflush when
696  * the filesystem is shutting down to clean up the inode state.  It is
697  * responsible for removing the inode item from the AIL if it has not been
698  * re-logged, and unlocking the inode's flush lock.
699  */
700 void
701 xfs_iflush_abort(
702         xfs_inode_t             *ip,
703         bool                    stale)
704 {
705         xfs_inode_log_item_t    *iip = ip->i_itemp;
706
707         if (iip) {
708                 struct xfs_ail  *ailp = iip->ili_item.li_ailp;
709                 if (iip->ili_item.li_flags & XFS_LI_IN_AIL) {
710                         spin_lock(&ailp->xa_lock);
711                         if (iip->ili_item.li_flags & XFS_LI_IN_AIL) {
712                                 /* xfs_trans_ail_delete() drops the AIL lock. */
713                                 xfs_trans_ail_delete(ailp, &iip->ili_item,
714                                                 stale ?
715                                                      SHUTDOWN_LOG_IO_ERROR :
716                                                      SHUTDOWN_CORRUPT_INCORE);
717                         } else
718                                 spin_unlock(&ailp->xa_lock);
719                 }
720                 iip->ili_logged = 0;
721                 /*
722                  * Clear the ili_last_fields bits now that we know that the
723                  * data corresponding to them is safely on disk.
724                  */
725                 iip->ili_last_fields = 0;
726                 /*
727                  * Clear the inode logging fields so no more flushes are
728                  * attempted.
729                  */
730                 iip->ili_fields = 0;
731         }
732         /*
733          * Release the inode's flush lock since we're done with it.
734          */
735         xfs_ifunlock(ip);
736 }
737
738 void
739 xfs_istale_done(
740         struct xfs_buf          *bp,
741         struct xfs_log_item     *lip)
742 {
743         xfs_iflush_abort(INODE_ITEM(lip)->ili_inode, true);
744 }
745
746 /*
747  * convert an xfs_inode_log_format struct from either 32 or 64 bit versions
748  * (which can have different field alignments) to the native version
749  */
750 int
751 xfs_inode_item_format_convert(
752         xfs_log_iovec_t         *buf,
753         xfs_inode_log_format_t  *in_f)
754 {
755         if (buf->i_len == sizeof(xfs_inode_log_format_32_t)) {
756                 xfs_inode_log_format_32_t *in_f32 = buf->i_addr;
757
758                 in_f->ilf_type = in_f32->ilf_type;
759                 in_f->ilf_size = in_f32->ilf_size;
760                 in_f->ilf_fields = in_f32->ilf_fields;
761                 in_f->ilf_asize = in_f32->ilf_asize;
762                 in_f->ilf_dsize = in_f32->ilf_dsize;
763                 in_f->ilf_ino = in_f32->ilf_ino;
764                 /* copy biggest field of ilf_u */
765                 memcpy(in_f->ilf_u.ilfu_uuid.__u_bits,
766                        in_f32->ilf_u.ilfu_uuid.__u_bits,
767                        sizeof(uuid_t));
768                 in_f->ilf_blkno = in_f32->ilf_blkno;
769                 in_f->ilf_len = in_f32->ilf_len;
770                 in_f->ilf_boffset = in_f32->ilf_boffset;
771                 return 0;
772         } else if (buf->i_len == sizeof(xfs_inode_log_format_64_t)){
773                 xfs_inode_log_format_64_t *in_f64 = buf->i_addr;
774
775                 in_f->ilf_type = in_f64->ilf_type;
776                 in_f->ilf_size = in_f64->ilf_size;
777                 in_f->ilf_fields = in_f64->ilf_fields;
778                 in_f->ilf_asize = in_f64->ilf_asize;
779                 in_f->ilf_dsize = in_f64->ilf_dsize;
780                 in_f->ilf_ino = in_f64->ilf_ino;
781                 /* copy biggest field of ilf_u */
782                 memcpy(in_f->ilf_u.ilfu_uuid.__u_bits,
783                        in_f64->ilf_u.ilfu_uuid.__u_bits,
784                        sizeof(uuid_t));
785                 in_f->ilf_blkno = in_f64->ilf_blkno;
786                 in_f->ilf_len = in_f64->ilf_len;
787                 in_f->ilf_boffset = in_f64->ilf_boffset;
788                 return 0;
789         }
790         return -EFSCORRUPTED;
791 }