xfs: merge iop_unpin_remove into iop_unpin
[firefly-linux-kernel-4.4.55.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_types.h"
21 #include "xfs_bit.h"
22 #include "xfs_log.h"
23 #include "xfs_inum.h"
24 #include "xfs_trans.h"
25 #include "xfs_sb.h"
26 #include "xfs_ag.h"
27 #include "xfs_mount.h"
28 #include "xfs_trans_priv.h"
29 #include "xfs_bmap_btree.h"
30 #include "xfs_dinode.h"
31 #include "xfs_inode.h"
32 #include "xfs_inode_item.h"
33 #include "xfs_error.h"
34 #include "xfs_trace.h"
35
36
37 kmem_zone_t     *xfs_ili_zone;          /* inode log item zone */
38
39 /*
40  * This returns the number of iovecs needed to log the given inode item.
41  *
42  * We need one iovec for the inode log format structure, one for the
43  * inode core, and possibly one for the inode data/extents/b-tree root
44  * and one for the inode attribute data/extents/b-tree root.
45  */
46 STATIC uint
47 xfs_inode_item_size(
48         xfs_inode_log_item_t    *iip)
49 {
50         uint            nvecs;
51         xfs_inode_t     *ip;
52
53         ip = iip->ili_inode;
54         nvecs = 2;
55
56         /*
57          * Only log the data/extents/b-tree root if there is something
58          * left to log.
59          */
60         iip->ili_format.ilf_fields |= XFS_ILOG_CORE;
61
62         switch (ip->i_d.di_format) {
63         case XFS_DINODE_FMT_EXTENTS:
64                 iip->ili_format.ilf_fields &=
65                         ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT |
66                           XFS_ILOG_DEV | XFS_ILOG_UUID);
67                 if ((iip->ili_format.ilf_fields & XFS_ILOG_DEXT) &&
68                     (ip->i_d.di_nextents > 0) &&
69                     (ip->i_df.if_bytes > 0)) {
70                         ASSERT(ip->i_df.if_u1.if_extents != NULL);
71                         nvecs++;
72                 } else {
73                         iip->ili_format.ilf_fields &= ~XFS_ILOG_DEXT;
74                 }
75                 break;
76
77         case XFS_DINODE_FMT_BTREE:
78                 ASSERT(ip->i_df.if_ext_max ==
79                        XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
80                 iip->ili_format.ilf_fields &=
81                         ~(XFS_ILOG_DDATA | XFS_ILOG_DEXT |
82                           XFS_ILOG_DEV | XFS_ILOG_UUID);
83                 if ((iip->ili_format.ilf_fields & XFS_ILOG_DBROOT) &&
84                     (ip->i_df.if_broot_bytes > 0)) {
85                         ASSERT(ip->i_df.if_broot != NULL);
86                         nvecs++;
87                 } else {
88                         ASSERT(!(iip->ili_format.ilf_fields &
89                                  XFS_ILOG_DBROOT));
90 #ifdef XFS_TRANS_DEBUG
91                         if (iip->ili_root_size > 0) {
92                                 ASSERT(iip->ili_root_size ==
93                                        ip->i_df.if_broot_bytes);
94                                 ASSERT(memcmp(iip->ili_orig_root,
95                                             ip->i_df.if_broot,
96                                             iip->ili_root_size) == 0);
97                         } else {
98                                 ASSERT(ip->i_df.if_broot_bytes == 0);
99                         }
100 #endif
101                         iip->ili_format.ilf_fields &= ~XFS_ILOG_DBROOT;
102                 }
103                 break;
104
105         case XFS_DINODE_FMT_LOCAL:
106                 iip->ili_format.ilf_fields &=
107                         ~(XFS_ILOG_DEXT | XFS_ILOG_DBROOT |
108                           XFS_ILOG_DEV | XFS_ILOG_UUID);
109                 if ((iip->ili_format.ilf_fields & XFS_ILOG_DDATA) &&
110                     (ip->i_df.if_bytes > 0)) {
111                         ASSERT(ip->i_df.if_u1.if_data != NULL);
112                         ASSERT(ip->i_d.di_size > 0);
113                         nvecs++;
114                 } else {
115                         iip->ili_format.ilf_fields &= ~XFS_ILOG_DDATA;
116                 }
117                 break;
118
119         case XFS_DINODE_FMT_DEV:
120                 iip->ili_format.ilf_fields &=
121                         ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT |
122                           XFS_ILOG_DEXT | XFS_ILOG_UUID);
123                 break;
124
125         case XFS_DINODE_FMT_UUID:
126                 iip->ili_format.ilf_fields &=
127                         ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT |
128                           XFS_ILOG_DEXT | XFS_ILOG_DEV);
129                 break;
130
131         default:
132                 ASSERT(0);
133                 break;
134         }
135
136         /*
137          * If there are no attributes associated with this file,
138          * then there cannot be anything more to log.
139          * Clear all attribute-related log flags.
140          */
141         if (!XFS_IFORK_Q(ip)) {
142                 iip->ili_format.ilf_fields &=
143                         ~(XFS_ILOG_ADATA | XFS_ILOG_ABROOT | XFS_ILOG_AEXT);
144                 return nvecs;
145         }
146
147         /*
148          * Log any necessary attribute data.
149          */
150         switch (ip->i_d.di_aformat) {
151         case XFS_DINODE_FMT_EXTENTS:
152                 iip->ili_format.ilf_fields &=
153                         ~(XFS_ILOG_ADATA | XFS_ILOG_ABROOT);
154                 if ((iip->ili_format.ilf_fields & XFS_ILOG_AEXT) &&
155                     (ip->i_d.di_anextents > 0) &&
156                     (ip->i_afp->if_bytes > 0)) {
157                         ASSERT(ip->i_afp->if_u1.if_extents != NULL);
158                         nvecs++;
159                 } else {
160                         iip->ili_format.ilf_fields &= ~XFS_ILOG_AEXT;
161                 }
162                 break;
163
164         case XFS_DINODE_FMT_BTREE:
165                 iip->ili_format.ilf_fields &=
166                         ~(XFS_ILOG_ADATA | XFS_ILOG_AEXT);
167                 if ((iip->ili_format.ilf_fields & XFS_ILOG_ABROOT) &&
168                     (ip->i_afp->if_broot_bytes > 0)) {
169                         ASSERT(ip->i_afp->if_broot != NULL);
170                         nvecs++;
171                 } else {
172                         iip->ili_format.ilf_fields &= ~XFS_ILOG_ABROOT;
173                 }
174                 break;
175
176         case XFS_DINODE_FMT_LOCAL:
177                 iip->ili_format.ilf_fields &=
178                         ~(XFS_ILOG_AEXT | XFS_ILOG_ABROOT);
179                 if ((iip->ili_format.ilf_fields & XFS_ILOG_ADATA) &&
180                     (ip->i_afp->if_bytes > 0)) {
181                         ASSERT(ip->i_afp->if_u1.if_data != NULL);
182                         nvecs++;
183                 } else {
184                         iip->ili_format.ilf_fields &= ~XFS_ILOG_ADATA;
185                 }
186                 break;
187
188         default:
189                 ASSERT(0);
190                 break;
191         }
192
193         return nvecs;
194 }
195
196 /*
197  * This is called to fill in the vector of log iovecs for the
198  * given inode log item.  It fills the first item with an inode
199  * log format structure, the second with the on-disk inode structure,
200  * and a possible third and/or fourth with the inode data/extents/b-tree
201  * root and inode attributes data/extents/b-tree root.
202  */
203 STATIC void
204 xfs_inode_item_format(
205         xfs_inode_log_item_t    *iip,
206         xfs_log_iovec_t         *log_vector)
207 {
208         uint                    nvecs;
209         xfs_log_iovec_t         *vecp;
210         xfs_inode_t             *ip;
211         size_t                  data_bytes;
212         xfs_bmbt_rec_t          *ext_buffer;
213         int                     nrecs;
214         xfs_mount_t             *mp;
215
216         ip = iip->ili_inode;
217         vecp = log_vector;
218
219         vecp->i_addr = (xfs_caddr_t)&iip->ili_format;
220         vecp->i_len  = sizeof(xfs_inode_log_format_t);
221         vecp->i_type = XLOG_REG_TYPE_IFORMAT;
222         vecp++;
223         nvecs        = 1;
224
225         /*
226          * Make sure the linux inode is dirty. We do this before
227          * clearing i_update_core as the VFS will call back into
228          * XFS here and set i_update_core, so we need to dirty the
229          * inode first so that the ordering of i_update_core and
230          * unlogged modifications still works as described below.
231          */
232         xfs_mark_inode_dirty_sync(ip);
233
234         /*
235          * Clear i_update_core if the timestamps (or any other
236          * non-transactional modification) need flushing/logging
237          * and we're about to log them with the rest of the core.
238          *
239          * This is the same logic as xfs_iflush() but this code can't
240          * run at the same time as xfs_iflush because we're in commit
241          * processing here and so we have the inode lock held in
242          * exclusive mode.  Although it doesn't really matter
243          * for the timestamps if both routines were to grab the
244          * timestamps or not.  That would be ok.
245          *
246          * We clear i_update_core before copying out the data.
247          * This is for coordination with our timestamp updates
248          * that don't hold the inode lock. They will always
249          * update the timestamps BEFORE setting i_update_core,
250          * so if we clear i_update_core after they set it we
251          * are guaranteed to see their updates to the timestamps
252          * either here.  Likewise, if they set it after we clear it
253          * here, we'll see it either on the next commit of this
254          * inode or the next time the inode gets flushed via
255          * xfs_iflush().  This depends on strongly ordered memory
256          * semantics, but we have that.  We use the SYNCHRONIZE
257          * macro to make sure that the compiler does not reorder
258          * the i_update_core access below the data copy below.
259          */
260         if (ip->i_update_core)  {
261                 ip->i_update_core = 0;
262                 SYNCHRONIZE();
263         }
264
265         /*
266          * Make sure to get the latest timestamps from the Linux inode.
267          */
268         xfs_synchronize_times(ip);
269
270         vecp->i_addr = (xfs_caddr_t)&ip->i_d;
271         vecp->i_len  = sizeof(struct xfs_icdinode);
272         vecp->i_type = XLOG_REG_TYPE_ICORE;
273         vecp++;
274         nvecs++;
275         iip->ili_format.ilf_fields |= XFS_ILOG_CORE;
276
277         /*
278          * If this is really an old format inode, then we need to
279          * log it as such.  This means that we have to copy the link
280          * count from the new field to the old.  We don't have to worry
281          * about the new fields, because nothing trusts them as long as
282          * the old inode version number is there.  If the superblock already
283          * has a new version number, then we don't bother converting back.
284          */
285         mp = ip->i_mount;
286         ASSERT(ip->i_d.di_version == 1 || xfs_sb_version_hasnlink(&mp->m_sb));
287         if (ip->i_d.di_version == 1) {
288                 if (!xfs_sb_version_hasnlink(&mp->m_sb)) {
289                         /*
290                          * Convert it back.
291                          */
292                         ASSERT(ip->i_d.di_nlink <= XFS_MAXLINK_1);
293                         ip->i_d.di_onlink = ip->i_d.di_nlink;
294                 } else {
295                         /*
296                          * The superblock version has already been bumped,
297                          * so just make the conversion to the new inode
298                          * format permanent.
299                          */
300                         ip->i_d.di_version = 2;
301                         ip->i_d.di_onlink = 0;
302                         memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
303                 }
304         }
305
306         switch (ip->i_d.di_format) {
307         case XFS_DINODE_FMT_EXTENTS:
308                 ASSERT(!(iip->ili_format.ilf_fields &
309                          (XFS_ILOG_DDATA | XFS_ILOG_DBROOT |
310                           XFS_ILOG_DEV | XFS_ILOG_UUID)));
311                 if (iip->ili_format.ilf_fields & XFS_ILOG_DEXT) {
312                         ASSERT(ip->i_df.if_bytes > 0);
313                         ASSERT(ip->i_df.if_u1.if_extents != NULL);
314                         ASSERT(ip->i_d.di_nextents > 0);
315                         ASSERT(iip->ili_extents_buf == NULL);
316                         nrecs = ip->i_df.if_bytes /
317                                 (uint)sizeof(xfs_bmbt_rec_t);
318                         ASSERT(nrecs > 0);
319 #ifdef XFS_NATIVE_HOST
320                         if (nrecs == ip->i_d.di_nextents) {
321                                 /*
322                                  * There are no delayed allocation
323                                  * extents, so just point to the
324                                  * real extents array.
325                                  */
326                                 vecp->i_addr =
327                                         (char *)(ip->i_df.if_u1.if_extents);
328                                 vecp->i_len = ip->i_df.if_bytes;
329                                 vecp->i_type = XLOG_REG_TYPE_IEXT;
330                         } else
331 #endif
332                         {
333                                 /*
334                                  * There are delayed allocation extents
335                                  * in the inode, or we need to convert
336                                  * the extents to on disk format.
337                                  * Use xfs_iextents_copy()
338                                  * to copy only the real extents into
339                                  * a separate buffer.  We'll free the
340                                  * buffer in the unlock routine.
341                                  */
342                                 ext_buffer = kmem_alloc(ip->i_df.if_bytes,
343                                         KM_SLEEP);
344                                 iip->ili_extents_buf = ext_buffer;
345                                 vecp->i_addr = (xfs_caddr_t)ext_buffer;
346                                 vecp->i_len = xfs_iextents_copy(ip, ext_buffer,
347                                                 XFS_DATA_FORK);
348                                 vecp->i_type = XLOG_REG_TYPE_IEXT;
349                         }
350                         ASSERT(vecp->i_len <= ip->i_df.if_bytes);
351                         iip->ili_format.ilf_dsize = vecp->i_len;
352                         vecp++;
353                         nvecs++;
354                 }
355                 break;
356
357         case XFS_DINODE_FMT_BTREE:
358                 ASSERT(!(iip->ili_format.ilf_fields &
359                          (XFS_ILOG_DDATA | XFS_ILOG_DEXT |
360                           XFS_ILOG_DEV | XFS_ILOG_UUID)));
361                 if (iip->ili_format.ilf_fields & XFS_ILOG_DBROOT) {
362                         ASSERT(ip->i_df.if_broot_bytes > 0);
363                         ASSERT(ip->i_df.if_broot != NULL);
364                         vecp->i_addr = (xfs_caddr_t)ip->i_df.if_broot;
365                         vecp->i_len = ip->i_df.if_broot_bytes;
366                         vecp->i_type = XLOG_REG_TYPE_IBROOT;
367                         vecp++;
368                         nvecs++;
369                         iip->ili_format.ilf_dsize = ip->i_df.if_broot_bytes;
370                 }
371                 break;
372
373         case XFS_DINODE_FMT_LOCAL:
374                 ASSERT(!(iip->ili_format.ilf_fields &
375                          (XFS_ILOG_DBROOT | XFS_ILOG_DEXT |
376                           XFS_ILOG_DEV | XFS_ILOG_UUID)));
377                 if (iip->ili_format.ilf_fields & XFS_ILOG_DDATA) {
378                         ASSERT(ip->i_df.if_bytes > 0);
379                         ASSERT(ip->i_df.if_u1.if_data != NULL);
380                         ASSERT(ip->i_d.di_size > 0);
381
382                         vecp->i_addr = (xfs_caddr_t)ip->i_df.if_u1.if_data;
383                         /*
384                          * Round i_bytes up to a word boundary.
385                          * The underlying memory is guaranteed to
386                          * to be there by xfs_idata_realloc().
387                          */
388                         data_bytes = roundup(ip->i_df.if_bytes, 4);
389                         ASSERT((ip->i_df.if_real_bytes == 0) ||
390                                (ip->i_df.if_real_bytes == data_bytes));
391                         vecp->i_len = (int)data_bytes;
392                         vecp->i_type = XLOG_REG_TYPE_ILOCAL;
393                         vecp++;
394                         nvecs++;
395                         iip->ili_format.ilf_dsize = (unsigned)data_bytes;
396                 }
397                 break;
398
399         case XFS_DINODE_FMT_DEV:
400                 ASSERT(!(iip->ili_format.ilf_fields &
401                          (XFS_ILOG_DBROOT | XFS_ILOG_DEXT |
402                           XFS_ILOG_DDATA | XFS_ILOG_UUID)));
403                 if (iip->ili_format.ilf_fields & XFS_ILOG_DEV) {
404                         iip->ili_format.ilf_u.ilfu_rdev =
405                                 ip->i_df.if_u2.if_rdev;
406                 }
407                 break;
408
409         case XFS_DINODE_FMT_UUID:
410                 ASSERT(!(iip->ili_format.ilf_fields &
411                          (XFS_ILOG_DBROOT | XFS_ILOG_DEXT |
412                           XFS_ILOG_DDATA | XFS_ILOG_DEV)));
413                 if (iip->ili_format.ilf_fields & XFS_ILOG_UUID) {
414                         iip->ili_format.ilf_u.ilfu_uuid =
415                                 ip->i_df.if_u2.if_uuid;
416                 }
417                 break;
418
419         default:
420                 ASSERT(0);
421                 break;
422         }
423
424         /*
425          * If there are no attributes associated with the file,
426          * then we're done.
427          * Assert that no attribute-related log flags are set.
428          */
429         if (!XFS_IFORK_Q(ip)) {
430                 ASSERT(nvecs == iip->ili_item.li_desc->lid_size);
431                 iip->ili_format.ilf_size = nvecs;
432                 ASSERT(!(iip->ili_format.ilf_fields &
433                          (XFS_ILOG_ADATA | XFS_ILOG_ABROOT | XFS_ILOG_AEXT)));
434                 return;
435         }
436
437         switch (ip->i_d.di_aformat) {
438         case XFS_DINODE_FMT_EXTENTS:
439                 ASSERT(!(iip->ili_format.ilf_fields &
440                          (XFS_ILOG_ADATA | XFS_ILOG_ABROOT)));
441                 if (iip->ili_format.ilf_fields & XFS_ILOG_AEXT) {
442                         ASSERT(ip->i_afp->if_bytes > 0);
443                         ASSERT(ip->i_afp->if_u1.if_extents != NULL);
444                         ASSERT(ip->i_d.di_anextents > 0);
445 #ifdef DEBUG
446                         nrecs = ip->i_afp->if_bytes /
447                                 (uint)sizeof(xfs_bmbt_rec_t);
448 #endif
449                         ASSERT(nrecs > 0);
450                         ASSERT(nrecs == ip->i_d.di_anextents);
451 #ifdef XFS_NATIVE_HOST
452                         /*
453                          * There are not delayed allocation extents
454                          * for attributes, so just point at the array.
455                          */
456                         vecp->i_addr = (char *)(ip->i_afp->if_u1.if_extents);
457                         vecp->i_len = ip->i_afp->if_bytes;
458 #else
459                         ASSERT(iip->ili_aextents_buf == NULL);
460                         /*
461                          * Need to endian flip before logging
462                          */
463                         ext_buffer = kmem_alloc(ip->i_afp->if_bytes,
464                                 KM_SLEEP);
465                         iip->ili_aextents_buf = ext_buffer;
466                         vecp->i_addr = (xfs_caddr_t)ext_buffer;
467                         vecp->i_len = xfs_iextents_copy(ip, ext_buffer,
468                                         XFS_ATTR_FORK);
469 #endif
470                         vecp->i_type = XLOG_REG_TYPE_IATTR_EXT;
471                         iip->ili_format.ilf_asize = vecp->i_len;
472                         vecp++;
473                         nvecs++;
474                 }
475                 break;
476
477         case XFS_DINODE_FMT_BTREE:
478                 ASSERT(!(iip->ili_format.ilf_fields &
479                          (XFS_ILOG_ADATA | XFS_ILOG_AEXT)));
480                 if (iip->ili_format.ilf_fields & XFS_ILOG_ABROOT) {
481                         ASSERT(ip->i_afp->if_broot_bytes > 0);
482                         ASSERT(ip->i_afp->if_broot != NULL);
483                         vecp->i_addr = (xfs_caddr_t)ip->i_afp->if_broot;
484                         vecp->i_len = ip->i_afp->if_broot_bytes;
485                         vecp->i_type = XLOG_REG_TYPE_IATTR_BROOT;
486                         vecp++;
487                         nvecs++;
488                         iip->ili_format.ilf_asize = ip->i_afp->if_broot_bytes;
489                 }
490                 break;
491
492         case XFS_DINODE_FMT_LOCAL:
493                 ASSERT(!(iip->ili_format.ilf_fields &
494                          (XFS_ILOG_ABROOT | XFS_ILOG_AEXT)));
495                 if (iip->ili_format.ilf_fields & XFS_ILOG_ADATA) {
496                         ASSERT(ip->i_afp->if_bytes > 0);
497                         ASSERT(ip->i_afp->if_u1.if_data != NULL);
498
499                         vecp->i_addr = (xfs_caddr_t)ip->i_afp->if_u1.if_data;
500                         /*
501                          * Round i_bytes up to a word boundary.
502                          * The underlying memory is guaranteed to
503                          * to be there by xfs_idata_realloc().
504                          */
505                         data_bytes = roundup(ip->i_afp->if_bytes, 4);
506                         ASSERT((ip->i_afp->if_real_bytes == 0) ||
507                                (ip->i_afp->if_real_bytes == data_bytes));
508                         vecp->i_len = (int)data_bytes;
509                         vecp->i_type = XLOG_REG_TYPE_IATTR_LOCAL;
510                         vecp++;
511                         nvecs++;
512                         iip->ili_format.ilf_asize = (unsigned)data_bytes;
513                 }
514                 break;
515
516         default:
517                 ASSERT(0);
518                 break;
519         }
520
521         ASSERT(nvecs == iip->ili_item.li_desc->lid_size);
522         iip->ili_format.ilf_size = nvecs;
523 }
524
525
526 /*
527  * This is called to pin the inode associated with the inode log
528  * item in memory so it cannot be written out.
529  */
530 STATIC void
531 xfs_inode_item_pin(
532         xfs_inode_log_item_t    *iip)
533 {
534         ASSERT(xfs_isilocked(iip->ili_inode, XFS_ILOCK_EXCL));
535
536         trace_xfs_inode_pin(iip->ili_inode, _RET_IP_);
537         atomic_inc(&iip->ili_inode->i_pincount);
538 }
539
540
541 /*
542  * This is called to unpin the inode associated with the inode log
543  * item which was previously pinned with a call to xfs_inode_item_pin().
544  *
545  * Also wake up anyone in xfs_iunpin_wait() if the count goes to 0.
546  */
547 STATIC void
548 xfs_inode_item_unpin(
549         xfs_inode_log_item_t    *iip,
550         int                     remove)
551 {
552         struct xfs_inode        *ip = iip->ili_inode;
553
554         trace_xfs_inode_unpin(ip, _RET_IP_);
555         ASSERT(atomic_read(&ip->i_pincount) > 0);
556         if (atomic_dec_and_test(&ip->i_pincount))
557                 wake_up(&ip->i_ipin_wait);
558 }
559
560 /*
561  * This is called to attempt to lock the inode associated with this
562  * inode log item, in preparation for the push routine which does the actual
563  * iflush.  Don't sleep on the inode lock or the flush lock.
564  *
565  * If the flush lock is already held, indicating that the inode has
566  * been or is in the process of being flushed, then (ideally) we'd like to
567  * see if the inode's buffer is still incore, and if so give it a nudge.
568  * We delay doing so until the pushbuf routine, though, to avoid holding
569  * the AIL lock across a call to the blackhole which is the buffer cache.
570  * Also we don't want to sleep in any device strategy routines, which can happen
571  * if we do the subsequent bawrite in here.
572  */
573 STATIC uint
574 xfs_inode_item_trylock(
575         xfs_inode_log_item_t    *iip)
576 {
577         register xfs_inode_t    *ip;
578
579         ip = iip->ili_inode;
580
581         if (xfs_ipincount(ip) > 0) {
582                 return XFS_ITEM_PINNED;
583         }
584
585         if (!xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
586                 return XFS_ITEM_LOCKED;
587         }
588
589         if (!xfs_iflock_nowait(ip)) {
590                 /*
591                  * inode has already been flushed to the backing buffer,
592                  * leave it locked in shared mode, pushbuf routine will
593                  * unlock it.
594                  */
595                 return XFS_ITEM_PUSHBUF;
596         }
597
598         /* Stale items should force out the iclog */
599         if (ip->i_flags & XFS_ISTALE) {
600                 xfs_ifunlock(ip);
601                 /*
602                  * we hold the AIL lock - notify the unlock routine of this
603                  * so it doesn't try to get the lock again.
604                  */
605                 xfs_iunlock(ip, XFS_ILOCK_SHARED|XFS_IUNLOCK_NONOTIFY);
606                 return XFS_ITEM_PINNED;
607         }
608
609 #ifdef DEBUG
610         if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
611                 ASSERT(iip->ili_format.ilf_fields != 0);
612                 ASSERT(iip->ili_logged == 0);
613                 ASSERT(iip->ili_item.li_flags & XFS_LI_IN_AIL);
614         }
615 #endif
616         return XFS_ITEM_SUCCESS;
617 }
618
619 /*
620  * Unlock the inode associated with the inode log item.
621  * Clear the fields of the inode and inode log item that
622  * are specific to the current transaction.  If the
623  * hold flags is set, do not unlock the inode.
624  */
625 STATIC void
626 xfs_inode_item_unlock(
627         xfs_inode_log_item_t    *iip)
628 {
629         uint            hold;
630         uint            iolocked;
631         uint            lock_flags;
632         xfs_inode_t     *ip;
633
634         ASSERT(iip != NULL);
635         ASSERT(iip->ili_inode->i_itemp != NULL);
636         ASSERT(xfs_isilocked(iip->ili_inode, XFS_ILOCK_EXCL));
637         ASSERT((!(iip->ili_inode->i_itemp->ili_flags &
638                   XFS_ILI_IOLOCKED_EXCL)) ||
639                xfs_isilocked(iip->ili_inode, XFS_IOLOCK_EXCL));
640         ASSERT((!(iip->ili_inode->i_itemp->ili_flags &
641                   XFS_ILI_IOLOCKED_SHARED)) ||
642                xfs_isilocked(iip->ili_inode, XFS_IOLOCK_SHARED));
643         /*
644          * Clear the transaction pointer in the inode.
645          */
646         ip = iip->ili_inode;
647         ip->i_transp = NULL;
648
649         /*
650          * If the inode needed a separate buffer with which to log
651          * its extents, then free it now.
652          */
653         if (iip->ili_extents_buf != NULL) {
654                 ASSERT(ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS);
655                 ASSERT(ip->i_d.di_nextents > 0);
656                 ASSERT(iip->ili_format.ilf_fields & XFS_ILOG_DEXT);
657                 ASSERT(ip->i_df.if_bytes > 0);
658                 kmem_free(iip->ili_extents_buf);
659                 iip->ili_extents_buf = NULL;
660         }
661         if (iip->ili_aextents_buf != NULL) {
662                 ASSERT(ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS);
663                 ASSERT(ip->i_d.di_anextents > 0);
664                 ASSERT(iip->ili_format.ilf_fields & XFS_ILOG_AEXT);
665                 ASSERT(ip->i_afp->if_bytes > 0);
666                 kmem_free(iip->ili_aextents_buf);
667                 iip->ili_aextents_buf = NULL;
668         }
669
670         /*
671          * Figure out if we should unlock the inode or not.
672          */
673         hold = iip->ili_flags & XFS_ILI_HOLD;
674
675         /*
676          * Before clearing out the flags, remember whether we
677          * are holding the inode's IO lock.
678          */
679         iolocked = iip->ili_flags & XFS_ILI_IOLOCKED_ANY;
680
681         /*
682          * Clear out the fields of the inode log item particular
683          * to the current transaction.
684          */
685         iip->ili_flags = 0;
686
687         /*
688          * Unlock the inode if XFS_ILI_HOLD was not set.
689          */
690         if (!hold) {
691                 lock_flags = XFS_ILOCK_EXCL;
692                 if (iolocked & XFS_ILI_IOLOCKED_EXCL) {
693                         lock_flags |= XFS_IOLOCK_EXCL;
694                 } else if (iolocked & XFS_ILI_IOLOCKED_SHARED) {
695                         lock_flags |= XFS_IOLOCK_SHARED;
696                 }
697                 xfs_iput(iip->ili_inode, lock_flags);
698         }
699 }
700
701 /*
702  * This is called to find out where the oldest active copy of the
703  * inode log item in the on disk log resides now that the last log
704  * write of it completed at the given lsn.  Since we always re-log
705  * all dirty data in an inode, the latest copy in the on disk log
706  * is the only one that matters.  Therefore, simply return the
707  * given lsn.
708  */
709 /*ARGSUSED*/
710 STATIC xfs_lsn_t
711 xfs_inode_item_committed(
712         xfs_inode_log_item_t    *iip,
713         xfs_lsn_t               lsn)
714 {
715         return (lsn);
716 }
717
718 /*
719  * This gets called by xfs_trans_push_ail(), when IOP_TRYLOCK
720  * failed to get the inode flush lock but did get the inode locked SHARED.
721  * Here we're trying to see if the inode buffer is incore, and if so whether it's
722  * marked delayed write. If that's the case, we'll promote it and that will
723  * allow the caller to write the buffer by triggering the xfsbufd to run.
724  */
725 STATIC void
726 xfs_inode_item_pushbuf(
727         xfs_inode_log_item_t    *iip)
728 {
729         xfs_inode_t     *ip;
730         xfs_mount_t     *mp;
731         xfs_buf_t       *bp;
732
733         ip = iip->ili_inode;
734         ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED));
735
736         /*
737          * If a flush is not in progress anymore, chances are that the
738          * inode was taken off the AIL. So, just get out.
739          */
740         if (completion_done(&ip->i_flush) ||
741             ((iip->ili_item.li_flags & XFS_LI_IN_AIL) == 0)) {
742                 xfs_iunlock(ip, XFS_ILOCK_SHARED);
743                 return;
744         }
745
746         mp = ip->i_mount;
747         bp = xfs_incore(mp->m_ddev_targp, iip->ili_format.ilf_blkno,
748                     iip->ili_format.ilf_len, XBF_TRYLOCK);
749
750         xfs_iunlock(ip, XFS_ILOCK_SHARED);
751         if (!bp)
752                 return;
753         if (XFS_BUF_ISDELAYWRITE(bp))
754                 xfs_buf_delwri_promote(bp);
755         xfs_buf_relse(bp);
756         return;
757 }
758
759
760 /*
761  * This is called to asynchronously write the inode associated with this
762  * inode log item out to disk. The inode will already have been locked by
763  * a successful call to xfs_inode_item_trylock().
764  */
765 STATIC void
766 xfs_inode_item_push(
767         xfs_inode_log_item_t    *iip)
768 {
769         xfs_inode_t     *ip;
770
771         ip = iip->ili_inode;
772
773         ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED));
774         ASSERT(!completion_done(&ip->i_flush));
775         /*
776          * Since we were able to lock the inode's flush lock and
777          * we found it on the AIL, the inode must be dirty.  This
778          * is because the inode is removed from the AIL while still
779          * holding the flush lock in xfs_iflush_done().  Thus, if
780          * we found it in the AIL and were able to obtain the flush
781          * lock without sleeping, then there must not have been
782          * anyone in the process of flushing the inode.
783          */
784         ASSERT(XFS_FORCED_SHUTDOWN(ip->i_mount) ||
785                iip->ili_format.ilf_fields != 0);
786
787         /*
788          * Push the inode to it's backing buffer. This will not remove the
789          * inode from the AIL - a further push will be required to trigger a
790          * buffer push. However, this allows all the dirty inodes to be pushed
791          * to the buffer before it is pushed to disk. THe buffer IO completion
792          * will pull th einode from the AIL, mark it clean and unlock the flush
793          * lock.
794          */
795         (void) xfs_iflush(ip, 0);
796         xfs_iunlock(ip, XFS_ILOCK_SHARED);
797
798         return;
799 }
800
801 /*
802  * XXX rcc - this one really has to do something.  Probably needs
803  * to stamp in a new field in the incore inode.
804  */
805 /* ARGSUSED */
806 STATIC void
807 xfs_inode_item_committing(
808         xfs_inode_log_item_t    *iip,
809         xfs_lsn_t               lsn)
810 {
811         iip->ili_last_lsn = lsn;
812         return;
813 }
814
815 /*
816  * This is the ops vector shared by all buf log items.
817  */
818 static struct xfs_item_ops xfs_inode_item_ops = {
819         .iop_size       = (uint(*)(xfs_log_item_t*))xfs_inode_item_size,
820         .iop_format     = (void(*)(xfs_log_item_t*, xfs_log_iovec_t*))
821                                         xfs_inode_item_format,
822         .iop_pin        = (void(*)(xfs_log_item_t*))xfs_inode_item_pin,
823         .iop_unpin      = (void(*)(xfs_log_item_t*, int))xfs_inode_item_unpin,
824         .iop_trylock    = (uint(*)(xfs_log_item_t*))xfs_inode_item_trylock,
825         .iop_unlock     = (void(*)(xfs_log_item_t*))xfs_inode_item_unlock,
826         .iop_committed  = (xfs_lsn_t(*)(xfs_log_item_t*, xfs_lsn_t))
827                                         xfs_inode_item_committed,
828         .iop_push       = (void(*)(xfs_log_item_t*))xfs_inode_item_push,
829         .iop_pushbuf    = (void(*)(xfs_log_item_t*))xfs_inode_item_pushbuf,
830         .iop_committing = (void(*)(xfs_log_item_t*, xfs_lsn_t))
831                                         xfs_inode_item_committing
832 };
833
834
835 /*
836  * Initialize the inode log item for a newly allocated (in-core) inode.
837  */
838 void
839 xfs_inode_item_init(
840         xfs_inode_t     *ip,
841         xfs_mount_t     *mp)
842 {
843         xfs_inode_log_item_t    *iip;
844
845         ASSERT(ip->i_itemp == NULL);
846         iip = ip->i_itemp = kmem_zone_zalloc(xfs_ili_zone, KM_SLEEP);
847
848         iip->ili_inode = ip;
849         xfs_log_item_init(mp, &iip->ili_item, XFS_LI_INODE,
850                                                 &xfs_inode_item_ops);
851         iip->ili_format.ilf_type = XFS_LI_INODE;
852         iip->ili_format.ilf_ino = ip->i_ino;
853         iip->ili_format.ilf_blkno = ip->i_imap.im_blkno;
854         iip->ili_format.ilf_len = ip->i_imap.im_len;
855         iip->ili_format.ilf_boffset = ip->i_imap.im_boffset;
856 }
857
858 /*
859  * Free the inode log item and any memory hanging off of it.
860  */
861 void
862 xfs_inode_item_destroy(
863         xfs_inode_t     *ip)
864 {
865 #ifdef XFS_TRANS_DEBUG
866         if (ip->i_itemp->ili_root_size != 0) {
867                 kmem_free(ip->i_itemp->ili_orig_root);
868         }
869 #endif
870         kmem_zone_free(xfs_ili_zone, ip->i_itemp);
871 }
872
873
874 /*
875  * This is the inode flushing I/O completion routine.  It is called
876  * from interrupt level when the buffer containing the inode is
877  * flushed to disk.  It is responsible for removing the inode item
878  * from the AIL if it has not been re-logged, and unlocking the inode's
879  * flush lock.
880  */
881 /*ARGSUSED*/
882 void
883 xfs_iflush_done(
884         xfs_buf_t               *bp,
885         xfs_inode_log_item_t    *iip)
886 {
887         xfs_inode_t             *ip = iip->ili_inode;
888         struct xfs_ail          *ailp = iip->ili_item.li_ailp;
889
890         /*
891          * We only want to pull the item from the AIL if it is
892          * actually there and its location in the log has not
893          * changed since we started the flush.  Thus, we only bother
894          * if the ili_logged flag is set and the inode's lsn has not
895          * changed.  First we check the lsn outside
896          * the lock since it's cheaper, and then we recheck while
897          * holding the lock before removing the inode from the AIL.
898          */
899         if (iip->ili_logged &&
900             (iip->ili_item.li_lsn == iip->ili_flush_lsn)) {
901                 spin_lock(&ailp->xa_lock);
902                 if (iip->ili_item.li_lsn == iip->ili_flush_lsn) {
903                         /* xfs_trans_ail_delete() drops the AIL lock. */
904                         xfs_trans_ail_delete(ailp, (xfs_log_item_t*)iip);
905                 } else {
906                         spin_unlock(&ailp->xa_lock);
907                 }
908         }
909
910         iip->ili_logged = 0;
911
912         /*
913          * Clear the ili_last_fields bits now that we know that the
914          * data corresponding to them is safely on disk.
915          */
916         iip->ili_last_fields = 0;
917
918         /*
919          * Release the inode's flush lock since we're done with it.
920          */
921         xfs_ifunlock(ip);
922
923         return;
924 }
925
926 /*
927  * This is the inode flushing abort routine.  It is called
928  * from xfs_iflush when the filesystem is shutting down to clean
929  * up the inode state.
930  * It is responsible for removing the inode item
931  * from the AIL if it has not been re-logged, and unlocking the inode's
932  * flush lock.
933  */
934 void
935 xfs_iflush_abort(
936         xfs_inode_t             *ip)
937 {
938         xfs_inode_log_item_t    *iip = ip->i_itemp;
939         xfs_mount_t             *mp;
940
941         iip = ip->i_itemp;
942         mp = ip->i_mount;
943         if (iip) {
944                 struct xfs_ail  *ailp = iip->ili_item.li_ailp;
945                 if (iip->ili_item.li_flags & XFS_LI_IN_AIL) {
946                         spin_lock(&ailp->xa_lock);
947                         if (iip->ili_item.li_flags & XFS_LI_IN_AIL) {
948                                 /* xfs_trans_ail_delete() drops the AIL lock. */
949                                 xfs_trans_ail_delete(ailp, (xfs_log_item_t *)iip);
950                         } else
951                                 spin_unlock(&ailp->xa_lock);
952                 }
953                 iip->ili_logged = 0;
954                 /*
955                  * Clear the ili_last_fields bits now that we know that the
956                  * data corresponding to them is safely on disk.
957                  */
958                 iip->ili_last_fields = 0;
959                 /*
960                  * Clear the inode logging fields so no more flushes are
961                  * attempted.
962                  */
963                 iip->ili_format.ilf_fields = 0;
964         }
965         /*
966          * Release the inode's flush lock since we're done with it.
967          */
968         xfs_ifunlock(ip);
969 }
970
971 void
972 xfs_istale_done(
973         xfs_buf_t               *bp,
974         xfs_inode_log_item_t    *iip)
975 {
976         xfs_iflush_abort(iip->ili_inode);
977 }
978
979 /*
980  * convert an xfs_inode_log_format struct from either 32 or 64 bit versions
981  * (which can have different field alignments) to the native version
982  */
983 int
984 xfs_inode_item_format_convert(
985         xfs_log_iovec_t         *buf,
986         xfs_inode_log_format_t  *in_f)
987 {
988         if (buf->i_len == sizeof(xfs_inode_log_format_32_t)) {
989                 xfs_inode_log_format_32_t *in_f32;
990
991                 in_f32 = (xfs_inode_log_format_32_t *)buf->i_addr;
992                 in_f->ilf_type = in_f32->ilf_type;
993                 in_f->ilf_size = in_f32->ilf_size;
994                 in_f->ilf_fields = in_f32->ilf_fields;
995                 in_f->ilf_asize = in_f32->ilf_asize;
996                 in_f->ilf_dsize = in_f32->ilf_dsize;
997                 in_f->ilf_ino = in_f32->ilf_ino;
998                 /* copy biggest field of ilf_u */
999                 memcpy(in_f->ilf_u.ilfu_uuid.__u_bits,
1000                        in_f32->ilf_u.ilfu_uuid.__u_bits,
1001                        sizeof(uuid_t));
1002                 in_f->ilf_blkno = in_f32->ilf_blkno;
1003                 in_f->ilf_len = in_f32->ilf_len;
1004                 in_f->ilf_boffset = in_f32->ilf_boffset;
1005                 return 0;
1006         } else if (buf->i_len == sizeof(xfs_inode_log_format_64_t)){
1007                 xfs_inode_log_format_64_t *in_f64;
1008
1009                 in_f64 = (xfs_inode_log_format_64_t *)buf->i_addr;
1010                 in_f->ilf_type = in_f64->ilf_type;
1011                 in_f->ilf_size = in_f64->ilf_size;
1012                 in_f->ilf_fields = in_f64->ilf_fields;
1013                 in_f->ilf_asize = in_f64->ilf_asize;
1014                 in_f->ilf_dsize = in_f64->ilf_dsize;
1015                 in_f->ilf_ino = in_f64->ilf_ino;
1016                 /* copy biggest field of ilf_u */
1017                 memcpy(in_f->ilf_u.ilfu_uuid.__u_bits,
1018                        in_f64->ilf_u.ilfu_uuid.__u_bits,
1019                        sizeof(uuid_t));
1020                 in_f->ilf_blkno = in_f64->ilf_blkno;
1021                 in_f->ilf_len = in_f64->ilf_len;
1022                 in_f->ilf_boffset = in_f64->ilf_boffset;
1023                 return 0;
1024         }
1025         return EFSCORRUPTED;
1026 }