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