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