2ad12dcf83113942bf024af5b3341147f8c6d6ec
[platform/adaptation/renesas_rcar/renesas_kernel.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_dinode.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 /*
148  * xfs_inode_item_format_extents - convert in-core extents to on-disk form
149  *
150  * For either the data or attr fork in extent format, we need to endian convert
151  * the in-core extent as we place them into the on-disk inode. In this case, we
152  * need to do this conversion before we write the extents into the log. Because
153  * we don't have the disk inode to write into here, we allocate a buffer and
154  * format the extents into it via xfs_iextents_copy(). We free the buffer in
155  * the unlock routine after the copy for the log has been made.
156  *
157  * In the case of the data fork, the in-core and on-disk fork sizes can be
158  * different due to delayed allocation extents. We only log on-disk extents
159  * here, so always use the physical fork size to determine the size of the
160  * buffer we need to allocate.
161  */
162 STATIC void
163 xfs_inode_item_format_extents(
164         struct xfs_inode        *ip,
165         struct xfs_log_iovec    *vecp,
166         int                     whichfork,
167         int                     type)
168 {
169         xfs_bmbt_rec_t          *ext_buffer;
170
171         ext_buffer = kmem_alloc(XFS_IFORK_SIZE(ip, whichfork), KM_SLEEP);
172         if (whichfork == XFS_DATA_FORK)
173                 ip->i_itemp->ili_extents_buf = ext_buffer;
174         else
175                 ip->i_itemp->ili_aextents_buf = ext_buffer;
176
177         vecp->i_addr = ext_buffer;
178         vecp->i_len = xfs_iextents_copy(ip, ext_buffer, whichfork);
179         vecp->i_type = type;
180 }
181
182 /*
183  * If this is a v1 format inode, then we need to log it as such.  This means
184  * that we have to copy the link count from the new field to the old.  We
185  * don't have to worry about the new fields, because nothing trusts them as
186  * long as the old inode version number is there.
187  */
188 STATIC void
189 xfs_inode_item_format_v1_inode(
190         struct xfs_inode        *ip)
191 {
192         if (!xfs_sb_version_hasnlink(&ip->i_mount->m_sb)) {
193                 /*
194                  * Convert it back.
195                  */
196                 ASSERT(ip->i_d.di_nlink <= XFS_MAXLINK_1);
197                 ip->i_d.di_onlink = ip->i_d.di_nlink;
198         } else {
199                 /*
200                  * The superblock version has already been bumped,
201                  * so just make the conversion to the new inode
202                  * format permanent.
203                  */
204                 ip->i_d.di_version = 2;
205                 ip->i_d.di_onlink = 0;
206                 memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
207         }
208 }
209
210 STATIC struct xfs_log_iovec *
211 xfs_inode_item_format_data_fork(
212         struct xfs_inode_log_item *iip,
213         struct xfs_log_iovec    *vecp,
214         int                     *nvecs)
215 {
216         struct xfs_inode        *ip = iip->ili_inode;
217         size_t                  data_bytes;
218
219         switch (ip->i_d.di_format) {
220         case XFS_DINODE_FMT_EXTENTS:
221                 iip->ili_fields &=
222                         ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT |
223                           XFS_ILOG_DEV | XFS_ILOG_UUID);
224
225                 if ((iip->ili_fields & XFS_ILOG_DEXT) &&
226                     ip->i_d.di_nextents > 0 &&
227                     ip->i_df.if_bytes > 0) {
228                         ASSERT(ip->i_df.if_u1.if_extents != NULL);
229                         ASSERT(ip->i_df.if_bytes / sizeof(xfs_bmbt_rec_t) > 0);
230                         ASSERT(iip->ili_extents_buf == NULL);
231
232 #ifdef XFS_NATIVE_HOST
233                        if (ip->i_d.di_nextents == ip->i_df.if_bytes /
234                                                (uint)sizeof(xfs_bmbt_rec_t)) {
235                                 /*
236                                  * There are no delayed allocation
237                                  * extents, so just point to the
238                                  * real extents array.
239                                  */
240                                 vecp->i_addr = ip->i_df.if_u1.if_extents;
241                                 vecp->i_len = ip->i_df.if_bytes;
242                                 vecp->i_type = XLOG_REG_TYPE_IEXT;
243                         } else
244 #endif
245                         {
246                                 xfs_inode_item_format_extents(ip, vecp,
247                                         XFS_DATA_FORK, XLOG_REG_TYPE_IEXT);
248                         }
249                         ASSERT(vecp->i_len <= ip->i_df.if_bytes);
250                         iip->ili_format.ilf_dsize = vecp->i_len;
251                         vecp++;
252                         (*nvecs)++;
253                 } else {
254                         iip->ili_fields &= ~XFS_ILOG_DEXT;
255                 }
256                 break;
257         case XFS_DINODE_FMT_BTREE:
258                 iip->ili_fields &=
259                         ~(XFS_ILOG_DDATA | XFS_ILOG_DEXT |
260                           XFS_ILOG_DEV | XFS_ILOG_UUID);
261
262                 if ((iip->ili_fields & XFS_ILOG_DBROOT) &&
263                     ip->i_df.if_broot_bytes > 0) {
264                         ASSERT(ip->i_df.if_broot != NULL);
265                         vecp->i_addr = ip->i_df.if_broot;
266                         vecp->i_len = ip->i_df.if_broot_bytes;
267                         vecp->i_type = XLOG_REG_TYPE_IBROOT;
268                         vecp++;
269                         (*nvecs)++;
270                         iip->ili_format.ilf_dsize = ip->i_df.if_broot_bytes;
271                 } else {
272                         ASSERT(!(iip->ili_fields &
273                                  XFS_ILOG_DBROOT));
274                         iip->ili_fields &= ~XFS_ILOG_DBROOT;
275                 }
276                 break;
277         case XFS_DINODE_FMT_LOCAL:
278                 iip->ili_fields &=
279                         ~(XFS_ILOG_DEXT | XFS_ILOG_DBROOT |
280                           XFS_ILOG_DEV | XFS_ILOG_UUID);
281                 if ((iip->ili_fields & XFS_ILOG_DDATA) &&
282                     ip->i_df.if_bytes > 0) {
283                         ASSERT(ip->i_df.if_u1.if_data != NULL);
284                         ASSERT(ip->i_d.di_size > 0);
285
286                         vecp->i_addr = ip->i_df.if_u1.if_data;
287                         /*
288                          * Round i_bytes up to a word boundary.
289                          * The underlying memory is guaranteed to
290                          * to be there by xfs_idata_realloc().
291                          */
292                         data_bytes = roundup(ip->i_df.if_bytes, 4);
293                         ASSERT((ip->i_df.if_real_bytes == 0) ||
294                                (ip->i_df.if_real_bytes == data_bytes));
295                         vecp->i_len = (int)data_bytes;
296                         vecp->i_type = XLOG_REG_TYPE_ILOCAL;
297                         vecp++;
298                         (*nvecs)++;
299                         iip->ili_format.ilf_dsize = (unsigned)data_bytes;
300                 } else {
301                         iip->ili_fields &= ~XFS_ILOG_DDATA;
302                 }
303                 break;
304         case XFS_DINODE_FMT_DEV:
305                 iip->ili_fields &=
306                         ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT |
307                           XFS_ILOG_DEXT | XFS_ILOG_UUID);
308                 if (iip->ili_fields & XFS_ILOG_DEV) {
309                         iip->ili_format.ilf_u.ilfu_rdev =
310                                 ip->i_df.if_u2.if_rdev;
311                 }
312                 break;
313         case XFS_DINODE_FMT_UUID:
314                 iip->ili_fields &=
315                         ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT |
316                           XFS_ILOG_DEXT | XFS_ILOG_DEV);
317                 if (iip->ili_fields & XFS_ILOG_UUID) {
318                         iip->ili_format.ilf_u.ilfu_uuid =
319                                 ip->i_df.if_u2.if_uuid;
320                 }
321                 break;
322         default:
323                 ASSERT(0);
324                 break;
325         }
326
327         return vecp;
328 }
329
330 STATIC struct xfs_log_iovec *
331 xfs_inode_item_format_attr_fork(
332         struct xfs_inode_log_item *iip,
333         struct xfs_log_iovec    *vecp,
334         int                     *nvecs)
335 {
336         struct xfs_inode        *ip = iip->ili_inode;
337         size_t                  data_bytes;
338
339         switch (ip->i_d.di_aformat) {
340         case XFS_DINODE_FMT_EXTENTS:
341                 iip->ili_fields &=
342                         ~(XFS_ILOG_ADATA | XFS_ILOG_ABROOT);
343
344                 if ((iip->ili_fields & XFS_ILOG_AEXT) &&
345                     ip->i_d.di_anextents > 0 &&
346                     ip->i_afp->if_bytes > 0) {
347                         ASSERT(ip->i_afp->if_bytes / sizeof(xfs_bmbt_rec_t) ==
348                                 ip->i_d.di_anextents);
349                         ASSERT(ip->i_afp->if_u1.if_extents != NULL);
350 #ifdef XFS_NATIVE_HOST
351                         /*
352                          * There are not delayed allocation extents
353                          * for attributes, so just point at the array.
354                          */
355                         vecp->i_addr = ip->i_afp->if_u1.if_extents;
356                         vecp->i_len = ip->i_afp->if_bytes;
357                         vecp->i_type = XLOG_REG_TYPE_IATTR_EXT;
358 #else
359                         ASSERT(iip->ili_aextents_buf == NULL);
360                         xfs_inode_item_format_extents(ip, vecp,
361                                         XFS_ATTR_FORK, XLOG_REG_TYPE_IATTR_EXT);
362 #endif
363                         iip->ili_format.ilf_asize = vecp->i_len;
364                         vecp++;
365                         (*nvecs)++;
366                 } else {
367                         iip->ili_fields &= ~XFS_ILOG_AEXT;
368                 }
369                 break;
370         case XFS_DINODE_FMT_BTREE:
371                 iip->ili_fields &=
372                         ~(XFS_ILOG_ADATA | XFS_ILOG_AEXT);
373
374                 if ((iip->ili_fields & XFS_ILOG_ABROOT) &&
375                     ip->i_afp->if_broot_bytes > 0) {
376                         ASSERT(ip->i_afp->if_broot != NULL);
377
378                         vecp->i_addr = ip->i_afp->if_broot;
379                         vecp->i_len = ip->i_afp->if_broot_bytes;
380                         vecp->i_type = XLOG_REG_TYPE_IATTR_BROOT;
381                         vecp++;
382                         (*nvecs)++;
383                         iip->ili_format.ilf_asize = ip->i_afp->if_broot_bytes;
384                 } else {
385                         iip->ili_fields &= ~XFS_ILOG_ABROOT;
386                 }
387                 break;
388         case XFS_DINODE_FMT_LOCAL:
389                 iip->ili_fields &=
390                         ~(XFS_ILOG_AEXT | XFS_ILOG_ABROOT);
391
392                 if ((iip->ili_fields & XFS_ILOG_ADATA) &&
393                     ip->i_afp->if_bytes > 0) {
394                         ASSERT(ip->i_afp->if_u1.if_data != NULL);
395
396                         vecp->i_addr = ip->i_afp->if_u1.if_data;
397                         /*
398                          * Round i_bytes up to a word boundary.
399                          * The underlying memory is guaranteed to
400                          * to be there by xfs_idata_realloc().
401                          */
402                         data_bytes = roundup(ip->i_afp->if_bytes, 4);
403                         ASSERT((ip->i_afp->if_real_bytes == 0) ||
404                                (ip->i_afp->if_real_bytes == data_bytes));
405                         vecp->i_len = (int)data_bytes;
406                         vecp->i_type = XLOG_REG_TYPE_IATTR_LOCAL;
407                         vecp++;
408                         (*nvecs)++;
409                         iip->ili_format.ilf_asize = (unsigned)data_bytes;
410                 } else {
411                         iip->ili_fields &= ~XFS_ILOG_ADATA;
412                 }
413                 break;
414         default:
415                 ASSERT(0);
416                 break;
417         }
418
419         return vecp;
420 }
421
422 /*
423  * This is called to fill in the vector of log iovecs for the given inode
424  * log item.  It fills the first item with an inode log format structure,
425  * the second with the on-disk inode structure, and a possible third and/or
426  * fourth with the inode data/extents/b-tree root and inode attributes
427  * data/extents/b-tree root.
428  */
429 STATIC void
430 xfs_inode_item_format(
431         struct xfs_log_item     *lip,
432         struct xfs_log_iovec    *vecp)
433 {
434         struct xfs_inode_log_item *iip = INODE_ITEM(lip);
435         struct xfs_inode        *ip = iip->ili_inode;
436         uint                    nvecs;
437
438         vecp->i_addr = &iip->ili_format;
439         vecp->i_len  = sizeof(xfs_inode_log_format_t);
440         vecp->i_type = XLOG_REG_TYPE_IFORMAT;
441         vecp++;
442         nvecs        = 1;
443
444         vecp->i_addr = &ip->i_d;
445         vecp->i_len  = xfs_icdinode_size(ip->i_d.di_version);
446         vecp->i_type = XLOG_REG_TYPE_ICORE;
447         vecp++;
448         nvecs++;
449
450         if (ip->i_d.di_version == 1)
451                 xfs_inode_item_format_v1_inode(ip);
452
453         vecp = xfs_inode_item_format_data_fork(iip, vecp, &nvecs);
454         if (XFS_IFORK_Q(ip)) {
455                 vecp = xfs_inode_item_format_attr_fork(iip, vecp, &nvecs);
456         } else {
457                 iip->ili_fields &=
458                         ~(XFS_ILOG_ADATA | XFS_ILOG_ABROOT | XFS_ILOG_AEXT);
459         }
460
461         /*
462          * Now update the log format that goes out to disk from the in-core
463          * values.  We always write the inode core to make the arithmetic
464          * games in recovery easier, which isn't a big deal as just about any
465          * transaction would dirty it anyway.
466          */
467         iip->ili_format.ilf_fields = XFS_ILOG_CORE |
468                 (iip->ili_fields & ~XFS_ILOG_TIMESTAMP);
469         iip->ili_format.ilf_size = nvecs;
470 }
471
472 /*
473  * This is called to pin the inode associated with the inode log
474  * item in memory so it cannot be written out.
475  */
476 STATIC void
477 xfs_inode_item_pin(
478         struct xfs_log_item     *lip)
479 {
480         struct xfs_inode        *ip = INODE_ITEM(lip)->ili_inode;
481
482         ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
483
484         trace_xfs_inode_pin(ip, _RET_IP_);
485         atomic_inc(&ip->i_pincount);
486 }
487
488
489 /*
490  * This is called to unpin the inode associated with the inode log
491  * item which was previously pinned with a call to xfs_inode_item_pin().
492  *
493  * Also wake up anyone in xfs_iunpin_wait() if the count goes to 0.
494  */
495 STATIC void
496 xfs_inode_item_unpin(
497         struct xfs_log_item     *lip,
498         int                     remove)
499 {
500         struct xfs_inode        *ip = INODE_ITEM(lip)->ili_inode;
501
502         trace_xfs_inode_unpin(ip, _RET_IP_);
503         ASSERT(atomic_read(&ip->i_pincount) > 0);
504         if (atomic_dec_and_test(&ip->i_pincount))
505                 wake_up_bit(&ip->i_flags, __XFS_IPINNED_BIT);
506 }
507
508 STATIC uint
509 xfs_inode_item_push(
510         struct xfs_log_item     *lip,
511         struct list_head        *buffer_list)
512 {
513         struct xfs_inode_log_item *iip = INODE_ITEM(lip);
514         struct xfs_inode        *ip = iip->ili_inode;
515         struct xfs_buf          *bp = NULL;
516         uint                    rval = XFS_ITEM_SUCCESS;
517         int                     error;
518
519         if (xfs_ipincount(ip) > 0)
520                 return XFS_ITEM_PINNED;
521
522         if (!xfs_ilock_nowait(ip, XFS_ILOCK_SHARED))
523                 return XFS_ITEM_LOCKED;
524
525         /*
526          * Re-check the pincount now that we stabilized the value by
527          * taking the ilock.
528          */
529         if (xfs_ipincount(ip) > 0) {
530                 rval = XFS_ITEM_PINNED;
531                 goto out_unlock;
532         }
533
534         /*
535          * Stale inode items should force out the iclog.
536          */
537         if (ip->i_flags & XFS_ISTALE) {
538                 rval = XFS_ITEM_PINNED;
539                 goto out_unlock;
540         }
541
542         /*
543          * Someone else is already flushing the inode.  Nothing we can do
544          * here but wait for the flush to finish and remove the item from
545          * the AIL.
546          */
547         if (!xfs_iflock_nowait(ip)) {
548                 rval = XFS_ITEM_FLUSHING;
549                 goto out_unlock;
550         }
551
552         ASSERT(iip->ili_fields != 0 || XFS_FORCED_SHUTDOWN(ip->i_mount));
553         ASSERT(iip->ili_logged == 0 || XFS_FORCED_SHUTDOWN(ip->i_mount));
554
555         spin_unlock(&lip->li_ailp->xa_lock);
556
557         error = xfs_iflush(ip, &bp);
558         if (!error) {
559                 if (!xfs_buf_delwri_queue(bp, buffer_list))
560                         rval = XFS_ITEM_FLUSHING;
561                 xfs_buf_relse(bp);
562         }
563
564         spin_lock(&lip->li_ailp->xa_lock);
565 out_unlock:
566         xfs_iunlock(ip, XFS_ILOCK_SHARED);
567         return rval;
568 }
569
570 /*
571  * Unlock the inode associated with the inode log item.
572  * Clear the fields of the inode and inode log item that
573  * are specific to the current transaction.  If the
574  * hold flags is set, do not unlock the inode.
575  */
576 STATIC void
577 xfs_inode_item_unlock(
578         struct xfs_log_item     *lip)
579 {
580         struct xfs_inode_log_item *iip = INODE_ITEM(lip);
581         struct xfs_inode        *ip = iip->ili_inode;
582         unsigned short          lock_flags;
583
584         ASSERT(ip->i_itemp != NULL);
585         ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
586
587         /*
588          * If the inode needed a separate buffer with which to log
589          * its extents, then free it now.
590          */
591         if (iip->ili_extents_buf != NULL) {
592                 ASSERT(ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS);
593                 ASSERT(ip->i_d.di_nextents > 0);
594                 ASSERT(iip->ili_fields & XFS_ILOG_DEXT);
595                 ASSERT(ip->i_df.if_bytes > 0);
596                 kmem_free(iip->ili_extents_buf);
597                 iip->ili_extents_buf = NULL;
598         }
599         if (iip->ili_aextents_buf != NULL) {
600                 ASSERT(ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS);
601                 ASSERT(ip->i_d.di_anextents > 0);
602                 ASSERT(iip->ili_fields & XFS_ILOG_AEXT);
603                 ASSERT(ip->i_afp->if_bytes > 0);
604                 kmem_free(iip->ili_aextents_buf);
605                 iip->ili_aextents_buf = NULL;
606         }
607
608         lock_flags = iip->ili_lock_flags;
609         iip->ili_lock_flags = 0;
610         if (lock_flags)
611                 xfs_iunlock(ip, lock_flags);
612 }
613
614 /*
615  * This is called to find out where the oldest active copy of the inode log
616  * item in the on disk log resides now that the last log write of it completed
617  * at the given lsn.  Since we always re-log all dirty data in an inode, the
618  * latest copy in the on disk log is the only one that matters.  Therefore,
619  * simply return the given lsn.
620  *
621  * If the inode has been marked stale because the cluster is being freed, we
622  * don't want to (re-)insert this inode into the AIL. There is a race condition
623  * where the cluster buffer may be unpinned before the inode is inserted into
624  * the AIL during transaction committed processing. If the buffer is unpinned
625  * before the inode item has been committed and inserted, then it is possible
626  * for the buffer to be written and IO completes before the inode is inserted
627  * into the AIL. In that case, we'd be inserting a clean, stale inode into the
628  * AIL which will never get removed. It will, however, get reclaimed which
629  * triggers an assert in xfs_inode_free() complaining about freein an inode
630  * still in the AIL.
631  *
632  * To avoid this, just unpin the inode directly and return a LSN of -1 so the
633  * transaction committed code knows that it does not need to do any further
634  * processing on the item.
635  */
636 STATIC xfs_lsn_t
637 xfs_inode_item_committed(
638         struct xfs_log_item     *lip,
639         xfs_lsn_t               lsn)
640 {
641         struct xfs_inode_log_item *iip = INODE_ITEM(lip);
642         struct xfs_inode        *ip = iip->ili_inode;
643
644         if (xfs_iflags_test(ip, XFS_ISTALE)) {
645                 xfs_inode_item_unpin(lip, 0);
646                 return -1;
647         }
648         return lsn;
649 }
650
651 /*
652  * XXX rcc - this one really has to do something.  Probably needs
653  * to stamp in a new field in the incore inode.
654  */
655 STATIC void
656 xfs_inode_item_committing(
657         struct xfs_log_item     *lip,
658         xfs_lsn_t               lsn)
659 {
660         INODE_ITEM(lip)->ili_last_lsn = lsn;
661 }
662
663 /*
664  * This is the ops vector shared by all buf log items.
665  */
666 static const struct xfs_item_ops xfs_inode_item_ops = {
667         .iop_size       = xfs_inode_item_size,
668         .iop_format     = xfs_inode_item_format,
669         .iop_pin        = xfs_inode_item_pin,
670         .iop_unpin      = xfs_inode_item_unpin,
671         .iop_unlock     = xfs_inode_item_unlock,
672         .iop_committed  = xfs_inode_item_committed,
673         .iop_push       = xfs_inode_item_push,
674         .iop_committing = xfs_inode_item_committing
675 };
676
677
678 /*
679  * Initialize the inode log item for a newly allocated (in-core) inode.
680  */
681 void
682 xfs_inode_item_init(
683         struct xfs_inode        *ip,
684         struct xfs_mount        *mp)
685 {
686         struct xfs_inode_log_item *iip;
687
688         ASSERT(ip->i_itemp == NULL);
689         iip = ip->i_itemp = kmem_zone_zalloc(xfs_ili_zone, KM_SLEEP);
690
691         iip->ili_inode = ip;
692         xfs_log_item_init(mp, &iip->ili_item, XFS_LI_INODE,
693                                                 &xfs_inode_item_ops);
694         iip->ili_format.ilf_type = XFS_LI_INODE;
695         iip->ili_format.ilf_ino = ip->i_ino;
696         iip->ili_format.ilf_blkno = ip->i_imap.im_blkno;
697         iip->ili_format.ilf_len = ip->i_imap.im_len;
698         iip->ili_format.ilf_boffset = ip->i_imap.im_boffset;
699 }
700
701 /*
702  * Free the inode log item and any memory hanging off of it.
703  */
704 void
705 xfs_inode_item_destroy(
706         xfs_inode_t     *ip)
707 {
708         kmem_zone_free(xfs_ili_zone, ip->i_itemp);
709 }
710
711
712 /*
713  * This is the inode flushing I/O completion routine.  It is called
714  * from interrupt level when the buffer containing the inode is
715  * flushed to disk.  It is responsible for removing the inode item
716  * from the AIL if it has not been re-logged, and unlocking the inode's
717  * flush lock.
718  *
719  * To reduce AIL lock traffic as much as possible, we scan the buffer log item
720  * list for other inodes that will run this function. We remove them from the
721  * buffer list so we can process all the inode IO completions in one AIL lock
722  * traversal.
723  */
724 void
725 xfs_iflush_done(
726         struct xfs_buf          *bp,
727         struct xfs_log_item     *lip)
728 {
729         struct xfs_inode_log_item *iip;
730         struct xfs_log_item     *blip;
731         struct xfs_log_item     *next;
732         struct xfs_log_item     *prev;
733         struct xfs_ail          *ailp = lip->li_ailp;
734         int                     need_ail = 0;
735
736         /*
737          * Scan the buffer IO completions for other inodes being completed and
738          * attach them to the current inode log item.
739          */
740         blip = bp->b_fspriv;
741         prev = NULL;
742         while (blip != NULL) {
743                 if (lip->li_cb != xfs_iflush_done) {
744                         prev = blip;
745                         blip = blip->li_bio_list;
746                         continue;
747                 }
748
749                 /* remove from list */
750                 next = blip->li_bio_list;
751                 if (!prev) {
752                         bp->b_fspriv = next;
753                 } else {
754                         prev->li_bio_list = next;
755                 }
756
757                 /* add to current list */
758                 blip->li_bio_list = lip->li_bio_list;
759                 lip->li_bio_list = blip;
760
761                 /*
762                  * while we have the item, do the unlocked check for needing
763                  * the AIL lock.
764                  */
765                 iip = INODE_ITEM(blip);
766                 if (iip->ili_logged && blip->li_lsn == iip->ili_flush_lsn)
767                         need_ail++;
768
769                 blip = next;
770         }
771
772         /* make sure we capture the state of the initial inode. */
773         iip = INODE_ITEM(lip);
774         if (iip->ili_logged && lip->li_lsn == iip->ili_flush_lsn)
775                 need_ail++;
776
777         /*
778          * We only want to pull the item from the AIL if it is
779          * actually there and its location in the log has not
780          * changed since we started the flush.  Thus, we only bother
781          * if the ili_logged flag is set and the inode's lsn has not
782          * changed.  First we check the lsn outside
783          * the lock since it's cheaper, and then we recheck while
784          * holding the lock before removing the inode from the AIL.
785          */
786         if (need_ail) {
787                 struct xfs_log_item *log_items[need_ail];
788                 int i = 0;
789                 spin_lock(&ailp->xa_lock);
790                 for (blip = lip; blip; blip = blip->li_bio_list) {
791                         iip = INODE_ITEM(blip);
792                         if (iip->ili_logged &&
793                             blip->li_lsn == iip->ili_flush_lsn) {
794                                 log_items[i++] = blip;
795                         }
796                         ASSERT(i <= need_ail);
797                 }
798                 /* xfs_trans_ail_delete_bulk() drops the AIL lock. */
799                 xfs_trans_ail_delete_bulk(ailp, log_items, i,
800                                           SHUTDOWN_CORRUPT_INCORE);
801         }
802
803
804         /*
805          * clean up and unlock the flush lock now we are done. We can clear the
806          * ili_last_fields bits now that we know that the data corresponding to
807          * them is safely on disk.
808          */
809         for (blip = lip; blip; blip = next) {
810                 next = blip->li_bio_list;
811                 blip->li_bio_list = NULL;
812
813                 iip = INODE_ITEM(blip);
814                 iip->ili_logged = 0;
815                 iip->ili_last_fields = 0;
816                 xfs_ifunlock(iip->ili_inode);
817         }
818 }
819
820 /*
821  * This is the inode flushing abort routine.  It is called from xfs_iflush when
822  * the filesystem is shutting down to clean up the inode state.  It is
823  * responsible for removing the inode item from the AIL if it has not been
824  * re-logged, and unlocking the inode's flush lock.
825  */
826 void
827 xfs_iflush_abort(
828         xfs_inode_t             *ip,
829         bool                    stale)
830 {
831         xfs_inode_log_item_t    *iip = ip->i_itemp;
832
833         if (iip) {
834                 struct xfs_ail  *ailp = iip->ili_item.li_ailp;
835                 if (iip->ili_item.li_flags & XFS_LI_IN_AIL) {
836                         spin_lock(&ailp->xa_lock);
837                         if (iip->ili_item.li_flags & XFS_LI_IN_AIL) {
838                                 /* xfs_trans_ail_delete() drops the AIL lock. */
839                                 xfs_trans_ail_delete(ailp, &iip->ili_item,
840                                                 stale ?
841                                                      SHUTDOWN_LOG_IO_ERROR :
842                                                      SHUTDOWN_CORRUPT_INCORE);
843                         } else
844                                 spin_unlock(&ailp->xa_lock);
845                 }
846                 iip->ili_logged = 0;
847                 /*
848                  * Clear the ili_last_fields bits now that we know that the
849                  * data corresponding to them is safely on disk.
850                  */
851                 iip->ili_last_fields = 0;
852                 /*
853                  * Clear the inode logging fields so no more flushes are
854                  * attempted.
855                  */
856                 iip->ili_fields = 0;
857         }
858         /*
859          * Release the inode's flush lock since we're done with it.
860          */
861         xfs_ifunlock(ip);
862 }
863
864 void
865 xfs_istale_done(
866         struct xfs_buf          *bp,
867         struct xfs_log_item     *lip)
868 {
869         xfs_iflush_abort(INODE_ITEM(lip)->ili_inode, true);
870 }
871
872 /*
873  * convert an xfs_inode_log_format struct from either 32 or 64 bit versions
874  * (which can have different field alignments) to the native version
875  */
876 int
877 xfs_inode_item_format_convert(
878         xfs_log_iovec_t         *buf,
879         xfs_inode_log_format_t  *in_f)
880 {
881         if (buf->i_len == sizeof(xfs_inode_log_format_32_t)) {
882                 xfs_inode_log_format_32_t *in_f32 = buf->i_addr;
883
884                 in_f->ilf_type = in_f32->ilf_type;
885                 in_f->ilf_size = in_f32->ilf_size;
886                 in_f->ilf_fields = in_f32->ilf_fields;
887                 in_f->ilf_asize = in_f32->ilf_asize;
888                 in_f->ilf_dsize = in_f32->ilf_dsize;
889                 in_f->ilf_ino = in_f32->ilf_ino;
890                 /* copy biggest field of ilf_u */
891                 memcpy(in_f->ilf_u.ilfu_uuid.__u_bits,
892                        in_f32->ilf_u.ilfu_uuid.__u_bits,
893                        sizeof(uuid_t));
894                 in_f->ilf_blkno = in_f32->ilf_blkno;
895                 in_f->ilf_len = in_f32->ilf_len;
896                 in_f->ilf_boffset = in_f32->ilf_boffset;
897                 return 0;
898         } else if (buf->i_len == sizeof(xfs_inode_log_format_64_t)){
899                 xfs_inode_log_format_64_t *in_f64 = buf->i_addr;
900
901                 in_f->ilf_type = in_f64->ilf_type;
902                 in_f->ilf_size = in_f64->ilf_size;
903                 in_f->ilf_fields = in_f64->ilf_fields;
904                 in_f->ilf_asize = in_f64->ilf_asize;
905                 in_f->ilf_dsize = in_f64->ilf_dsize;
906                 in_f->ilf_ino = in_f64->ilf_ino;
907                 /* copy biggest field of ilf_u */
908                 memcpy(in_f->ilf_u.ilfu_uuid.__u_bits,
909                        in_f64->ilf_u.ilfu_uuid.__u_bits,
910                        sizeof(uuid_t));
911                 in_f->ilf_blkno = in_f64->ilf_blkno;
912                 in_f->ilf_len = in_f64->ilf_len;
913                 in_f->ilf_boffset = in_f64->ilf_boffset;
914                 return 0;
915         }
916         return EFSCORRUPTED;
917 }