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