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