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