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