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