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