2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
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.
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.
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
18 #include <linux/log2.h>
22 #include "xfs_types.h"
27 #include "xfs_trans.h"
28 #include "xfs_trans_priv.h"
32 #include "xfs_dmapi.h"
33 #include "xfs_mount.h"
34 #include "xfs_bmap_btree.h"
35 #include "xfs_alloc_btree.h"
36 #include "xfs_ialloc_btree.h"
37 #include "xfs_dir2_sf.h"
38 #include "xfs_attr_sf.h"
39 #include "xfs_dinode.h"
40 #include "xfs_inode.h"
41 #include "xfs_buf_item.h"
42 #include "xfs_inode_item.h"
43 #include "xfs_btree.h"
44 #include "xfs_alloc.h"
45 #include "xfs_ialloc.h"
48 #include "xfs_error.h"
49 #include "xfs_utils.h"
50 #include "xfs_dir2_trace.h"
51 #include "xfs_quota.h"
53 #include "xfs_filestream.h"
54 #include "xfs_vnodeops.h"
56 kmem_zone_t *xfs_ifork_zone;
57 kmem_zone_t *xfs_inode_zone;
58 kmem_zone_t *xfs_icluster_zone;
61 * Used in xfs_itruncate(). This is the maximum number of extents
62 * freed from a file in a single transaction.
64 #define XFS_ITRUNC_MAX_EXTENTS 2
66 STATIC int xfs_iflush_int(xfs_inode_t *, xfs_buf_t *);
67 STATIC int xfs_iformat_local(xfs_inode_t *, xfs_dinode_t *, int, int);
68 STATIC int xfs_iformat_extents(xfs_inode_t *, xfs_dinode_t *, int);
69 STATIC int xfs_iformat_btree(xfs_inode_t *, xfs_dinode_t *, int);
73 * Make sure that the extents in the given memory buffer
83 xfs_bmbt_rec_host_t rec;
86 for (i = 0; i < nrecs; i++) {
87 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
88 rec.l0 = get_unaligned(&ep->l0);
89 rec.l1 = get_unaligned(&ep->l1);
90 xfs_bmbt_get_all(&rec, &irec);
91 if (fmt == XFS_EXTFMT_NOSTATE)
92 ASSERT(irec.br_state == XFS_EXT_NORM);
96 #define xfs_validate_extents(ifp, nrecs, fmt)
100 * Check that none of the inode's in the buffer have a next
101 * unlinked field of 0.
113 j = mp->m_inode_cluster_size >> mp->m_sb.sb_inodelog;
115 for (i = 0; i < j; i++) {
116 dip = (xfs_dinode_t *)xfs_buf_offset(bp,
117 i * mp->m_sb.sb_inodesize);
118 if (!dip->di_next_unlinked) {
119 xfs_fs_cmn_err(CE_ALERT, mp,
120 "Detected a bogus zero next_unlinked field in incore inode buffer 0x%p. About to pop an ASSERT.",
122 ASSERT(dip->di_next_unlinked);
129 * This routine is called to map an inode number within a file
130 * system to the buffer containing the on-disk version of the
131 * inode. It returns a pointer to the buffer containing the
132 * on-disk inode in the bpp parameter, and in the dip parameter
133 * it returns a pointer to the on-disk inode within that buffer.
135 * If a non-zero error is returned, then the contents of bpp and
136 * dipp are undefined.
138 * Use xfs_imap() to determine the size and location of the
139 * buffer to read from disk.
157 * Call the space management code to find the location of the
161 error = xfs_imap(mp, tp, ino, &imap, XFS_IMAP_LOOKUP);
164 "xfs_inotobp: xfs_imap() returned an "
165 "error %d on %s. Returning error.", error, mp->m_fsname);
170 * If the inode number maps to a block outside the bounds of the
171 * file system then return NULL rather than calling read_buf
172 * and panicing when we get an error from the driver.
174 if ((imap.im_blkno + imap.im_len) >
175 XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)) {
177 "xfs_inotobp: inode number (%llu + %d) maps to a block outside the bounds "
178 "of the file system %s. Returning EINVAL.",
179 (unsigned long long)imap.im_blkno,
180 imap.im_len, mp->m_fsname);
181 return XFS_ERROR(EINVAL);
185 * Read in the buffer. If tp is NULL, xfs_trans_read_buf() will
186 * default to just a read_buf() call.
188 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, imap.im_blkno,
189 (int)imap.im_len, XFS_BUF_LOCK, &bp);
193 "xfs_inotobp: xfs_trans_read_buf() returned an "
194 "error %d on %s. Returning error.", error, mp->m_fsname);
197 dip = (xfs_dinode_t *)xfs_buf_offset(bp, 0);
199 be16_to_cpu(dip->di_core.di_magic) == XFS_DINODE_MAGIC &&
200 XFS_DINODE_GOOD_VERSION(dip->di_core.di_version);
201 if (unlikely(XFS_TEST_ERROR(!di_ok, mp, XFS_ERRTAG_ITOBP_INOTOBP,
202 XFS_RANDOM_ITOBP_INOTOBP))) {
203 XFS_CORRUPTION_ERROR("xfs_inotobp", XFS_ERRLEVEL_LOW, mp, dip);
204 xfs_trans_brelse(tp, bp);
206 "xfs_inotobp: XFS_TEST_ERROR() returned an "
207 "error on %s. Returning EFSCORRUPTED.", mp->m_fsname);
208 return XFS_ERROR(EFSCORRUPTED);
211 xfs_inobp_check(mp, bp);
214 * Set *dipp to point to the on-disk inode in the buffer.
216 *dipp = (xfs_dinode_t *)xfs_buf_offset(bp, imap.im_boffset);
218 *offset = imap.im_boffset;
224 * This routine is called to map an inode to the buffer containing
225 * the on-disk version of the inode. It returns a pointer to the
226 * buffer containing the on-disk inode in the bpp parameter, and in
227 * the dip parameter it returns a pointer to the on-disk inode within
230 * If a non-zero error is returned, then the contents of bpp and
231 * dipp are undefined.
233 * If the inode is new and has not yet been initialized, use xfs_imap()
234 * to determine the size and location of the buffer to read from disk.
235 * If the inode has already been mapped to its buffer and read in once,
236 * then use the mapping information stored in the inode rather than
237 * calling xfs_imap(). This allows us to avoid the overhead of looking
238 * at the inode btree for small block file systems (see xfs_dilocate()).
239 * We can tell whether the inode has been mapped in before by comparing
240 * its disk block address to 0. Only uninitialized inodes will have
241 * 0 for the disk block address.
259 if (ip->i_blkno == (xfs_daddr_t)0) {
261 * Call the space management code to find the location of the
265 if ((error = xfs_imap(mp, tp, ip->i_ino, &imap,
266 XFS_IMAP_LOOKUP | imap_flags)))
270 * If the inode number maps to a block outside the bounds
271 * of the file system then return NULL rather than calling
272 * read_buf and panicing when we get an error from the
275 if ((imap.im_blkno + imap.im_len) >
276 XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)) {
278 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_itobp: "
279 "(imap.im_blkno (0x%llx) "
280 "+ imap.im_len (0x%llx)) > "
281 " XFS_FSB_TO_BB(mp, "
282 "mp->m_sb.sb_dblocks) (0x%llx)",
283 (unsigned long long) imap.im_blkno,
284 (unsigned long long) imap.im_len,
285 XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks));
287 return XFS_ERROR(EINVAL);
291 * Fill in the fields in the inode that will be used to
292 * map the inode to its buffer from now on.
294 ip->i_blkno = imap.im_blkno;
295 ip->i_len = imap.im_len;
296 ip->i_boffset = imap.im_boffset;
299 * We've already mapped the inode once, so just use the
300 * mapping that we saved the first time.
302 imap.im_blkno = ip->i_blkno;
303 imap.im_len = ip->i_len;
304 imap.im_boffset = ip->i_boffset;
306 ASSERT(bno == 0 || bno == imap.im_blkno);
309 * Read in the buffer. If tp is NULL, xfs_trans_read_buf() will
310 * default to just a read_buf() call.
312 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, imap.im_blkno,
313 (int)imap.im_len, XFS_BUF_LOCK, &bp);
316 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_itobp: "
317 "xfs_trans_read_buf() returned error %d, "
318 "imap.im_blkno 0x%llx, imap.im_len 0x%llx",
319 error, (unsigned long long) imap.im_blkno,
320 (unsigned long long) imap.im_len);
326 * Validate the magic number and version of every inode in the buffer
327 * (if DEBUG kernel) or the first inode in the buffer, otherwise.
328 * No validation is done here in userspace (xfs_repair).
330 #if !defined(__KERNEL__)
333 ni = BBTOB(imap.im_len) >> mp->m_sb.sb_inodelog;
334 #else /* usual case */
338 for (i = 0; i < ni; i++) {
342 dip = (xfs_dinode_t *)xfs_buf_offset(bp,
343 (i << mp->m_sb.sb_inodelog));
344 di_ok = be16_to_cpu(dip->di_core.di_magic) == XFS_DINODE_MAGIC &&
345 XFS_DINODE_GOOD_VERSION(dip->di_core.di_version);
346 if (unlikely(XFS_TEST_ERROR(!di_ok, mp,
347 XFS_ERRTAG_ITOBP_INOTOBP,
348 XFS_RANDOM_ITOBP_INOTOBP))) {
349 if (imap_flags & XFS_IMAP_BULKSTAT) {
350 xfs_trans_brelse(tp, bp);
351 return XFS_ERROR(EINVAL);
355 "Device %s - bad inode magic/vsn "
356 "daddr %lld #%d (magic=%x)",
357 XFS_BUFTARG_NAME(mp->m_ddev_targp),
358 (unsigned long long)imap.im_blkno, i,
359 be16_to_cpu(dip->di_core.di_magic));
361 XFS_CORRUPTION_ERROR("xfs_itobp", XFS_ERRLEVEL_HIGH,
363 xfs_trans_brelse(tp, bp);
364 return XFS_ERROR(EFSCORRUPTED);
368 xfs_inobp_check(mp, bp);
371 * Mark the buffer as an inode buffer now that it looks good
373 XFS_BUF_SET_VTYPE(bp, B_FS_INO);
376 * Set *dipp to point to the on-disk inode in the buffer.
378 *dipp = (xfs_dinode_t *)xfs_buf_offset(bp, imap.im_boffset);
384 * Move inode type and inode format specific information from the
385 * on-disk inode to the in-core inode. For fifos, devs, and sockets
386 * this means set if_rdev to the proper value. For files, directories,
387 * and symlinks this means to bring in the in-line data or extent
388 * pointers. For a file in B-tree format, only the root is immediately
389 * brought in-core. The rest will be in-lined in if_extents when it
390 * is first referenced (see xfs_iread_extents()).
397 xfs_attr_shortform_t *atp;
401 ip->i_df.if_ext_max =
402 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
405 if (unlikely(be32_to_cpu(dip->di_core.di_nextents) +
406 be16_to_cpu(dip->di_core.di_anextents) >
407 be64_to_cpu(dip->di_core.di_nblocks))) {
408 xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
409 "corrupt dinode %Lu, extent total = %d, nblocks = %Lu.",
410 (unsigned long long)ip->i_ino,
411 (int)(be32_to_cpu(dip->di_core.di_nextents) +
412 be16_to_cpu(dip->di_core.di_anextents)),
414 be64_to_cpu(dip->di_core.di_nblocks));
415 XFS_CORRUPTION_ERROR("xfs_iformat(1)", XFS_ERRLEVEL_LOW,
417 return XFS_ERROR(EFSCORRUPTED);
420 if (unlikely(dip->di_core.di_forkoff > ip->i_mount->m_sb.sb_inodesize)) {
421 xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
422 "corrupt dinode %Lu, forkoff = 0x%x.",
423 (unsigned long long)ip->i_ino,
424 dip->di_core.di_forkoff);
425 XFS_CORRUPTION_ERROR("xfs_iformat(2)", XFS_ERRLEVEL_LOW,
427 return XFS_ERROR(EFSCORRUPTED);
430 switch (ip->i_d.di_mode & S_IFMT) {
435 if (unlikely(dip->di_core.di_format != XFS_DINODE_FMT_DEV)) {
436 XFS_CORRUPTION_ERROR("xfs_iformat(3)", XFS_ERRLEVEL_LOW,
438 return XFS_ERROR(EFSCORRUPTED);
442 ip->i_df.if_u2.if_rdev = be32_to_cpu(dip->di_u.di_dev);
448 switch (dip->di_core.di_format) {
449 case XFS_DINODE_FMT_LOCAL:
451 * no local regular files yet
453 if (unlikely((be16_to_cpu(dip->di_core.di_mode) & S_IFMT) == S_IFREG)) {
454 xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
456 "(local format for regular file).",
457 (unsigned long long) ip->i_ino);
458 XFS_CORRUPTION_ERROR("xfs_iformat(4)",
461 return XFS_ERROR(EFSCORRUPTED);
464 di_size = be64_to_cpu(dip->di_core.di_size);
465 if (unlikely(di_size > XFS_DFORK_DSIZE(dip, ip->i_mount))) {
466 xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
468 "(bad size %Ld for local inode).",
469 (unsigned long long) ip->i_ino,
470 (long long) di_size);
471 XFS_CORRUPTION_ERROR("xfs_iformat(5)",
474 return XFS_ERROR(EFSCORRUPTED);
478 error = xfs_iformat_local(ip, dip, XFS_DATA_FORK, size);
480 case XFS_DINODE_FMT_EXTENTS:
481 error = xfs_iformat_extents(ip, dip, XFS_DATA_FORK);
483 case XFS_DINODE_FMT_BTREE:
484 error = xfs_iformat_btree(ip, dip, XFS_DATA_FORK);
487 XFS_ERROR_REPORT("xfs_iformat(6)", XFS_ERRLEVEL_LOW,
489 return XFS_ERROR(EFSCORRUPTED);
494 XFS_ERROR_REPORT("xfs_iformat(7)", XFS_ERRLEVEL_LOW, ip->i_mount);
495 return XFS_ERROR(EFSCORRUPTED);
500 if (!XFS_DFORK_Q(dip))
502 ASSERT(ip->i_afp == NULL);
503 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
504 ip->i_afp->if_ext_max =
505 XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
506 switch (dip->di_core.di_aformat) {
507 case XFS_DINODE_FMT_LOCAL:
508 atp = (xfs_attr_shortform_t *)XFS_DFORK_APTR(dip);
509 size = be16_to_cpu(atp->hdr.totsize);
510 error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK, size);
512 case XFS_DINODE_FMT_EXTENTS:
513 error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK);
515 case XFS_DINODE_FMT_BTREE:
516 error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK);
519 error = XFS_ERROR(EFSCORRUPTED);
523 kmem_zone_free(xfs_ifork_zone, ip->i_afp);
525 xfs_idestroy_fork(ip, XFS_DATA_FORK);
531 * The file is in-lined in the on-disk inode.
532 * If it fits into if_inline_data, then copy
533 * it there, otherwise allocate a buffer for it
534 * and copy the data there. Either way, set
535 * if_data to point at the data.
536 * If we allocate a buffer for the data, make
537 * sure that its size is a multiple of 4 and
538 * record the real size in i_real_bytes.
551 * If the size is unreasonable, then something
552 * is wrong and we just bail out rather than crash in
553 * kmem_alloc() or memcpy() below.
555 if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
556 xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
558 "(bad size %d for local fork, size = %d).",
559 (unsigned long long) ip->i_ino, size,
560 XFS_DFORK_SIZE(dip, ip->i_mount, whichfork));
561 XFS_CORRUPTION_ERROR("xfs_iformat_local", XFS_ERRLEVEL_LOW,
563 return XFS_ERROR(EFSCORRUPTED);
565 ifp = XFS_IFORK_PTR(ip, whichfork);
568 ifp->if_u1.if_data = NULL;
569 else if (size <= sizeof(ifp->if_u2.if_inline_data))
570 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
572 real_size = roundup(size, 4);
573 ifp->if_u1.if_data = kmem_alloc(real_size, KM_SLEEP);
575 ifp->if_bytes = size;
576 ifp->if_real_bytes = real_size;
578 memcpy(ifp->if_u1.if_data, XFS_DFORK_PTR(dip, whichfork), size);
579 ifp->if_flags &= ~XFS_IFEXTENTS;
580 ifp->if_flags |= XFS_IFINLINE;
585 * The file consists of a set of extents all
586 * of which fit into the on-disk inode.
587 * If there are few enough extents to fit into
588 * the if_inline_ext, then copy them there.
589 * Otherwise allocate a buffer for them and copy
590 * them into it. Either way, set if_extents
591 * to point at the extents.
605 ifp = XFS_IFORK_PTR(ip, whichfork);
606 nex = XFS_DFORK_NEXTENTS(dip, whichfork);
607 size = nex * (uint)sizeof(xfs_bmbt_rec_t);
610 * If the number of extents is unreasonable, then something
611 * is wrong and we just bail out rather than crash in
612 * kmem_alloc() or memcpy() below.
614 if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
615 xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
616 "corrupt inode %Lu ((a)extents = %d).",
617 (unsigned long long) ip->i_ino, nex);
618 XFS_CORRUPTION_ERROR("xfs_iformat_extents(1)", XFS_ERRLEVEL_LOW,
620 return XFS_ERROR(EFSCORRUPTED);
623 ifp->if_real_bytes = 0;
625 ifp->if_u1.if_extents = NULL;
626 else if (nex <= XFS_INLINE_EXTS)
627 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
629 xfs_iext_add(ifp, 0, nex);
631 ifp->if_bytes = size;
633 dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork);
634 xfs_validate_extents(ifp, nex, XFS_EXTFMT_INODE(ip));
635 for (i = 0; i < nex; i++, dp++) {
636 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
637 ep->l0 = be64_to_cpu(get_unaligned(&dp->l0));
638 ep->l1 = be64_to_cpu(get_unaligned(&dp->l1));
640 XFS_BMAP_TRACE_EXLIST(ip, nex, whichfork);
641 if (whichfork != XFS_DATA_FORK ||
642 XFS_EXTFMT_INODE(ip) == XFS_EXTFMT_NOSTATE)
643 if (unlikely(xfs_check_nostate_extents(
645 XFS_ERROR_REPORT("xfs_iformat_extents(2)",
648 return XFS_ERROR(EFSCORRUPTED);
651 ifp->if_flags |= XFS_IFEXTENTS;
656 * The file has too many extents to fit into
657 * the inode, so they are in B-tree format.
658 * Allocate a buffer for the root of the B-tree
659 * and copy the root into it. The i_extents
660 * field will remain NULL until all of the
661 * extents are read in (when they are needed).
669 xfs_bmdr_block_t *dfp;
675 ifp = XFS_IFORK_PTR(ip, whichfork);
676 dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork);
677 size = XFS_BMAP_BROOT_SPACE(dfp);
678 nrecs = XFS_BMAP_BROOT_NUMRECS(dfp);
681 * blow out if -- fork has less extents than can fit in
682 * fork (fork shouldn't be a btree format), root btree
683 * block has more records than can fit into the fork,
684 * or the number of extents is greater than the number of
687 if (unlikely(XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max
688 || XFS_BMDR_SPACE_CALC(nrecs) >
689 XFS_DFORK_SIZE(dip, ip->i_mount, whichfork)
690 || XFS_IFORK_NEXTENTS(ip, whichfork) > ip->i_d.di_nblocks)) {
691 xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
692 "corrupt inode %Lu (btree).",
693 (unsigned long long) ip->i_ino);
694 XFS_ERROR_REPORT("xfs_iformat_btree", XFS_ERRLEVEL_LOW,
696 return XFS_ERROR(EFSCORRUPTED);
699 ifp->if_broot_bytes = size;
700 ifp->if_broot = kmem_alloc(size, KM_SLEEP);
701 ASSERT(ifp->if_broot != NULL);
703 * Copy and convert from the on-disk structure
704 * to the in-memory structure.
706 xfs_bmdr_to_bmbt(dfp, XFS_DFORK_SIZE(dip, ip->i_mount, whichfork),
707 ifp->if_broot, size);
708 ifp->if_flags &= ~XFS_IFEXTENTS;
709 ifp->if_flags |= XFS_IFBROOT;
715 xfs_dinode_from_disk(
717 xfs_dinode_core_t *from)
719 to->di_magic = be16_to_cpu(from->di_magic);
720 to->di_mode = be16_to_cpu(from->di_mode);
721 to->di_version = from ->di_version;
722 to->di_format = from->di_format;
723 to->di_onlink = be16_to_cpu(from->di_onlink);
724 to->di_uid = be32_to_cpu(from->di_uid);
725 to->di_gid = be32_to_cpu(from->di_gid);
726 to->di_nlink = be32_to_cpu(from->di_nlink);
727 to->di_projid = be16_to_cpu(from->di_projid);
728 memcpy(to->di_pad, from->di_pad, sizeof(to->di_pad));
729 to->di_flushiter = be16_to_cpu(from->di_flushiter);
730 to->di_atime.t_sec = be32_to_cpu(from->di_atime.t_sec);
731 to->di_atime.t_nsec = be32_to_cpu(from->di_atime.t_nsec);
732 to->di_mtime.t_sec = be32_to_cpu(from->di_mtime.t_sec);
733 to->di_mtime.t_nsec = be32_to_cpu(from->di_mtime.t_nsec);
734 to->di_ctime.t_sec = be32_to_cpu(from->di_ctime.t_sec);
735 to->di_ctime.t_nsec = be32_to_cpu(from->di_ctime.t_nsec);
736 to->di_size = be64_to_cpu(from->di_size);
737 to->di_nblocks = be64_to_cpu(from->di_nblocks);
738 to->di_extsize = be32_to_cpu(from->di_extsize);
739 to->di_nextents = be32_to_cpu(from->di_nextents);
740 to->di_anextents = be16_to_cpu(from->di_anextents);
741 to->di_forkoff = from->di_forkoff;
742 to->di_aformat = from->di_aformat;
743 to->di_dmevmask = be32_to_cpu(from->di_dmevmask);
744 to->di_dmstate = be16_to_cpu(from->di_dmstate);
745 to->di_flags = be16_to_cpu(from->di_flags);
746 to->di_gen = be32_to_cpu(from->di_gen);
751 xfs_dinode_core_t *to,
752 xfs_icdinode_t *from)
754 to->di_magic = cpu_to_be16(from->di_magic);
755 to->di_mode = cpu_to_be16(from->di_mode);
756 to->di_version = from ->di_version;
757 to->di_format = from->di_format;
758 to->di_onlink = cpu_to_be16(from->di_onlink);
759 to->di_uid = cpu_to_be32(from->di_uid);
760 to->di_gid = cpu_to_be32(from->di_gid);
761 to->di_nlink = cpu_to_be32(from->di_nlink);
762 to->di_projid = cpu_to_be16(from->di_projid);
763 memcpy(to->di_pad, from->di_pad, sizeof(to->di_pad));
764 to->di_flushiter = cpu_to_be16(from->di_flushiter);
765 to->di_atime.t_sec = cpu_to_be32(from->di_atime.t_sec);
766 to->di_atime.t_nsec = cpu_to_be32(from->di_atime.t_nsec);
767 to->di_mtime.t_sec = cpu_to_be32(from->di_mtime.t_sec);
768 to->di_mtime.t_nsec = cpu_to_be32(from->di_mtime.t_nsec);
769 to->di_ctime.t_sec = cpu_to_be32(from->di_ctime.t_sec);
770 to->di_ctime.t_nsec = cpu_to_be32(from->di_ctime.t_nsec);
771 to->di_size = cpu_to_be64(from->di_size);
772 to->di_nblocks = cpu_to_be64(from->di_nblocks);
773 to->di_extsize = cpu_to_be32(from->di_extsize);
774 to->di_nextents = cpu_to_be32(from->di_nextents);
775 to->di_anextents = cpu_to_be16(from->di_anextents);
776 to->di_forkoff = from->di_forkoff;
777 to->di_aformat = from->di_aformat;
778 to->di_dmevmask = cpu_to_be32(from->di_dmevmask);
779 to->di_dmstate = cpu_to_be16(from->di_dmstate);
780 to->di_flags = cpu_to_be16(from->di_flags);
781 to->di_gen = cpu_to_be32(from->di_gen);
790 if (di_flags & XFS_DIFLAG_ANY) {
791 if (di_flags & XFS_DIFLAG_REALTIME)
792 flags |= XFS_XFLAG_REALTIME;
793 if (di_flags & XFS_DIFLAG_PREALLOC)
794 flags |= XFS_XFLAG_PREALLOC;
795 if (di_flags & XFS_DIFLAG_IMMUTABLE)
796 flags |= XFS_XFLAG_IMMUTABLE;
797 if (di_flags & XFS_DIFLAG_APPEND)
798 flags |= XFS_XFLAG_APPEND;
799 if (di_flags & XFS_DIFLAG_SYNC)
800 flags |= XFS_XFLAG_SYNC;
801 if (di_flags & XFS_DIFLAG_NOATIME)
802 flags |= XFS_XFLAG_NOATIME;
803 if (di_flags & XFS_DIFLAG_NODUMP)
804 flags |= XFS_XFLAG_NODUMP;
805 if (di_flags & XFS_DIFLAG_RTINHERIT)
806 flags |= XFS_XFLAG_RTINHERIT;
807 if (di_flags & XFS_DIFLAG_PROJINHERIT)
808 flags |= XFS_XFLAG_PROJINHERIT;
809 if (di_flags & XFS_DIFLAG_NOSYMLINKS)
810 flags |= XFS_XFLAG_NOSYMLINKS;
811 if (di_flags & XFS_DIFLAG_EXTSIZE)
812 flags |= XFS_XFLAG_EXTSIZE;
813 if (di_flags & XFS_DIFLAG_EXTSZINHERIT)
814 flags |= XFS_XFLAG_EXTSZINHERIT;
815 if (di_flags & XFS_DIFLAG_NODEFRAG)
816 flags |= XFS_XFLAG_NODEFRAG;
817 if (di_flags & XFS_DIFLAG_FILESTREAM)
818 flags |= XFS_XFLAG_FILESTREAM;
828 xfs_icdinode_t *dic = &ip->i_d;
830 return _xfs_dic2xflags(dic->di_flags) |
831 (XFS_IFORK_Q(ip) ? XFS_XFLAG_HASATTR : 0);
838 xfs_dinode_core_t *dic = &dip->di_core;
840 return _xfs_dic2xflags(be16_to_cpu(dic->di_flags)) |
841 (XFS_DFORK_Q(dip) ? XFS_XFLAG_HASATTR : 0);
845 * Given a mount structure and an inode number, return a pointer
846 * to a newly allocated in-core inode corresponding to the given
849 * Initialize the inode's attributes and extent pointers if it
850 * already has them (it will not if the inode has no links).
866 ASSERT(xfs_inode_zone != NULL);
868 ip = kmem_zone_zalloc(xfs_inode_zone, KM_SLEEP);
871 atomic_set(&ip->i_iocount, 0);
872 spin_lock_init(&ip->i_flags_lock);
875 * Get pointer's to the on-disk inode and the buffer containing it.
876 * If the inode number refers to a block outside the file system
877 * then xfs_itobp() will return NULL. In this case we should
878 * return NULL as well. Set i_blkno to 0 so that xfs_itobp() will
879 * know that this is a new incore inode.
881 error = xfs_itobp(mp, tp, ip, &dip, &bp, bno, imap_flags);
883 kmem_zone_free(xfs_inode_zone, ip);
888 * Initialize inode's trace buffers.
889 * Do this before xfs_iformat in case it adds entries.
891 #ifdef XFS_INODE_TRACE
892 ip->i_trace = ktrace_alloc(INODE_TRACE_SIZE, KM_SLEEP);
894 #ifdef XFS_BMAP_TRACE
895 ip->i_xtrace = ktrace_alloc(XFS_BMAP_KTRACE_SIZE, KM_SLEEP);
897 #ifdef XFS_BMBT_TRACE
898 ip->i_btrace = ktrace_alloc(XFS_BMBT_KTRACE_SIZE, KM_SLEEP);
901 ip->i_rwtrace = ktrace_alloc(XFS_RW_KTRACE_SIZE, KM_SLEEP);
903 #ifdef XFS_ILOCK_TRACE
904 ip->i_lock_trace = ktrace_alloc(XFS_ILOCK_KTRACE_SIZE, KM_SLEEP);
906 #ifdef XFS_DIR2_TRACE
907 ip->i_dir_trace = ktrace_alloc(XFS_DIR2_KTRACE_SIZE, KM_SLEEP);
911 * If we got something that isn't an inode it means someone
912 * (nfs or dmi) has a stale handle.
914 if (be16_to_cpu(dip->di_core.di_magic) != XFS_DINODE_MAGIC) {
915 kmem_zone_free(xfs_inode_zone, ip);
916 xfs_trans_brelse(tp, bp);
918 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_iread: "
919 "dip->di_core.di_magic (0x%x) != "
920 "XFS_DINODE_MAGIC (0x%x)",
921 be16_to_cpu(dip->di_core.di_magic),
924 return XFS_ERROR(EINVAL);
928 * If the on-disk inode is already linked to a directory
929 * entry, copy all of the inode into the in-core inode.
930 * xfs_iformat() handles copying in the inode format
931 * specific information.
932 * Otherwise, just get the truly permanent information.
934 if (dip->di_core.di_mode) {
935 xfs_dinode_from_disk(&ip->i_d, &dip->di_core);
936 error = xfs_iformat(ip, dip);
938 kmem_zone_free(xfs_inode_zone, ip);
939 xfs_trans_brelse(tp, bp);
941 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_iread: "
942 "xfs_iformat() returned error %d",
948 ip->i_d.di_magic = be16_to_cpu(dip->di_core.di_magic);
949 ip->i_d.di_version = dip->di_core.di_version;
950 ip->i_d.di_gen = be32_to_cpu(dip->di_core.di_gen);
951 ip->i_d.di_flushiter = be16_to_cpu(dip->di_core.di_flushiter);
953 * Make sure to pull in the mode here as well in
954 * case the inode is released without being used.
955 * This ensures that xfs_inactive() will see that
956 * the inode is already free and not try to mess
957 * with the uninitialized part of it.
961 * Initialize the per-fork minima and maxima for a new
962 * inode here. xfs_iformat will do it for old inodes.
964 ip->i_df.if_ext_max =
965 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
968 INIT_LIST_HEAD(&ip->i_reclaim);
971 * The inode format changed when we moved the link count and
972 * made it 32 bits long. If this is an old format inode,
973 * convert it in memory to look like a new one. If it gets
974 * flushed to disk we will convert back before flushing or
975 * logging it. We zero out the new projid field and the old link
976 * count field. We'll handle clearing the pad field (the remains
977 * of the old uuid field) when we actually convert the inode to
978 * the new format. We don't change the version number so that we
979 * can distinguish this from a real new format inode.
981 if (ip->i_d.di_version == XFS_DINODE_VERSION_1) {
982 ip->i_d.di_nlink = ip->i_d.di_onlink;
983 ip->i_d.di_onlink = 0;
984 ip->i_d.di_projid = 0;
987 ip->i_delayed_blks = 0;
988 ip->i_size = ip->i_d.di_size;
991 * Mark the buffer containing the inode as something to keep
992 * around for a while. This helps to keep recently accessed
993 * meta-data in-core longer.
995 XFS_BUF_SET_REF(bp, XFS_INO_REF);
998 * Use xfs_trans_brelse() to release the buffer containing the
999 * on-disk inode, because it was acquired with xfs_trans_read_buf()
1000 * in xfs_itobp() above. If tp is NULL, this is just a normal
1001 * brelse(). If we're within a transaction, then xfs_trans_brelse()
1002 * will only release the buffer if it is not dirty within the
1003 * transaction. It will be OK to release the buffer in this case,
1004 * because inodes on disk are never destroyed and we will be
1005 * locking the new in-core inode before putting it in the hash
1006 * table where other processes can find it. Thus we don't have
1007 * to worry about the inode being changed just because we released
1010 xfs_trans_brelse(tp, bp);
1016 * Read in extents from a btree-format inode.
1017 * Allocate and fill in if_extents. Real work is done in xfs_bmap.c.
1027 xfs_extnum_t nextents;
1030 if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
1031 XFS_ERROR_REPORT("xfs_iread_extents", XFS_ERRLEVEL_LOW,
1033 return XFS_ERROR(EFSCORRUPTED);
1035 nextents = XFS_IFORK_NEXTENTS(ip, whichfork);
1036 size = nextents * sizeof(xfs_bmbt_rec_t);
1037 ifp = XFS_IFORK_PTR(ip, whichfork);
1040 * We know that the size is valid (it's checked in iformat_btree)
1042 ifp->if_lastex = NULLEXTNUM;
1043 ifp->if_bytes = ifp->if_real_bytes = 0;
1044 ifp->if_flags |= XFS_IFEXTENTS;
1045 xfs_iext_add(ifp, 0, nextents);
1046 error = xfs_bmap_read_extents(tp, ip, whichfork);
1048 xfs_iext_destroy(ifp);
1049 ifp->if_flags &= ~XFS_IFEXTENTS;
1052 xfs_validate_extents(ifp, nextents, XFS_EXTFMT_INODE(ip));
1057 * Allocate an inode on disk and return a copy of its in-core version.
1058 * The in-core inode is locked exclusively. Set mode, nlink, and rdev
1059 * appropriately within the inode. The uid and gid for the inode are
1060 * set according to the contents of the given cred structure.
1062 * Use xfs_dialloc() to allocate the on-disk inode. If xfs_dialloc()
1063 * has a free inode available, call xfs_iget()
1064 * to obtain the in-core version of the allocated inode. Finally,
1065 * fill in the inode and log its initial contents. In this case,
1066 * ialloc_context would be set to NULL and call_again set to false.
1068 * If xfs_dialloc() does not have an available inode,
1069 * it will replenish its supply by doing an allocation. Since we can
1070 * only do one allocation within a transaction without deadlocks, we
1071 * must commit the current transaction before returning the inode itself.
1072 * In this case, therefore, we will set call_again to true and return.
1073 * The caller should then commit the current transaction, start a new
1074 * transaction, and call xfs_ialloc() again to actually get the inode.
1076 * To ensure that some other process does not grab the inode that
1077 * was allocated during the first call to xfs_ialloc(), this routine
1078 * also returns the [locked] bp pointing to the head of the freelist
1079 * as ialloc_context. The caller should hold this buffer across
1080 * the commit and pass it back into this routine on the second call.
1082 * If we are allocating quota inodes, we do not have a parent inode
1083 * to attach to or associate with (i.e. pip == NULL) because they
1084 * are not linked into the directory structure - they are attached
1085 * directly to the superblock - and so have no parent.
1097 xfs_buf_t **ialloc_context,
1098 boolean_t *call_again,
1108 * Call the space management code to pick
1109 * the on-disk inode to be allocated.
1111 error = xfs_dialloc(tp, pip ? pip->i_ino : 0, mode, okalloc,
1112 ialloc_context, call_again, &ino);
1116 if (*call_again || ino == NULLFSINO) {
1120 ASSERT(*ialloc_context == NULL);
1123 * Get the in-core inode with the lock held exclusively.
1124 * This is because we're setting fields here we need
1125 * to prevent others from looking at until we're done.
1127 error = xfs_trans_iget(tp->t_mountp, tp, ino,
1128 XFS_IGET_CREATE, XFS_ILOCK_EXCL, &ip);
1135 ip->i_d.di_mode = (__uint16_t)mode;
1136 ip->i_d.di_onlink = 0;
1137 ip->i_d.di_nlink = nlink;
1138 ASSERT(ip->i_d.di_nlink == nlink);
1139 ip->i_d.di_uid = current_fsuid(cr);
1140 ip->i_d.di_gid = current_fsgid(cr);
1141 ip->i_d.di_projid = prid;
1142 memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
1145 * If the superblock version is up to where we support new format
1146 * inodes and this is currently an old format inode, then change
1147 * the inode version number now. This way we only do the conversion
1148 * here rather than here and in the flush/logging code.
1150 if (XFS_SB_VERSION_HASNLINK(&tp->t_mountp->m_sb) &&
1151 ip->i_d.di_version == XFS_DINODE_VERSION_1) {
1152 ip->i_d.di_version = XFS_DINODE_VERSION_2;
1154 * We've already zeroed the old link count, the projid field,
1155 * and the pad field.
1160 * Project ids won't be stored on disk if we are using a version 1 inode.
1162 if ((prid != 0) && (ip->i_d.di_version == XFS_DINODE_VERSION_1))
1163 xfs_bump_ino_vers2(tp, ip);
1165 if (pip && XFS_INHERIT_GID(pip)) {
1166 ip->i_d.di_gid = pip->i_d.di_gid;
1167 if ((pip->i_d.di_mode & S_ISGID) && (mode & S_IFMT) == S_IFDIR) {
1168 ip->i_d.di_mode |= S_ISGID;
1173 * If the group ID of the new file does not match the effective group
1174 * ID or one of the supplementary group IDs, the S_ISGID bit is cleared
1175 * (and only if the irix_sgid_inherit compatibility variable is set).
1177 if ((irix_sgid_inherit) &&
1178 (ip->i_d.di_mode & S_ISGID) &&
1179 (!in_group_p((gid_t)ip->i_d.di_gid))) {
1180 ip->i_d.di_mode &= ~S_ISGID;
1183 ip->i_d.di_size = 0;
1185 ip->i_d.di_nextents = 0;
1186 ASSERT(ip->i_d.di_nblocks == 0);
1187 xfs_ichgtime(ip, XFS_ICHGTIME_CHG|XFS_ICHGTIME_ACC|XFS_ICHGTIME_MOD);
1189 * di_gen will have been taken care of in xfs_iread.
1191 ip->i_d.di_extsize = 0;
1192 ip->i_d.di_dmevmask = 0;
1193 ip->i_d.di_dmstate = 0;
1194 ip->i_d.di_flags = 0;
1195 flags = XFS_ILOG_CORE;
1196 switch (mode & S_IFMT) {
1201 ip->i_d.di_format = XFS_DINODE_FMT_DEV;
1202 ip->i_df.if_u2.if_rdev = rdev;
1203 ip->i_df.if_flags = 0;
1204 flags |= XFS_ILOG_DEV;
1207 if (pip && xfs_inode_is_filestream(pip)) {
1208 error = xfs_filestream_associate(pip, ip);
1212 xfs_iflags_set(ip, XFS_IFILESTREAM);
1216 if (pip && (pip->i_d.di_flags & XFS_DIFLAG_ANY)) {
1219 if ((mode & S_IFMT) == S_IFDIR) {
1220 if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
1221 di_flags |= XFS_DIFLAG_RTINHERIT;
1222 if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
1223 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
1224 ip->i_d.di_extsize = pip->i_d.di_extsize;
1226 } else if ((mode & S_IFMT) == S_IFREG) {
1227 if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
1228 di_flags |= XFS_DIFLAG_REALTIME;
1229 if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
1230 di_flags |= XFS_DIFLAG_EXTSIZE;
1231 ip->i_d.di_extsize = pip->i_d.di_extsize;
1234 if ((pip->i_d.di_flags & XFS_DIFLAG_NOATIME) &&
1235 xfs_inherit_noatime)
1236 di_flags |= XFS_DIFLAG_NOATIME;
1237 if ((pip->i_d.di_flags & XFS_DIFLAG_NODUMP) &&
1239 di_flags |= XFS_DIFLAG_NODUMP;
1240 if ((pip->i_d.di_flags & XFS_DIFLAG_SYNC) &&
1242 di_flags |= XFS_DIFLAG_SYNC;
1243 if ((pip->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) &&
1244 xfs_inherit_nosymlinks)
1245 di_flags |= XFS_DIFLAG_NOSYMLINKS;
1246 if (pip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1247 di_flags |= XFS_DIFLAG_PROJINHERIT;
1248 if ((pip->i_d.di_flags & XFS_DIFLAG_NODEFRAG) &&
1249 xfs_inherit_nodefrag)
1250 di_flags |= XFS_DIFLAG_NODEFRAG;
1251 if (pip->i_d.di_flags & XFS_DIFLAG_FILESTREAM)
1252 di_flags |= XFS_DIFLAG_FILESTREAM;
1253 ip->i_d.di_flags |= di_flags;
1257 ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
1258 ip->i_df.if_flags = XFS_IFEXTENTS;
1259 ip->i_df.if_bytes = ip->i_df.if_real_bytes = 0;
1260 ip->i_df.if_u1.if_extents = NULL;
1266 * Attribute fork settings for new inode.
1268 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1269 ip->i_d.di_anextents = 0;
1272 * Log the new values stuffed into the inode.
1274 xfs_trans_log_inode(tp, ip, flags);
1276 /* now that we have an i_mode we can setup inode ops and unlock */
1277 xfs_initialize_vnode(tp->t_mountp, vp, ip);
1284 * Check to make sure that there are no blocks allocated to the
1285 * file beyond the size of the file. We don't check this for
1286 * files with fixed size extents or real time extents, but we
1287 * at least do it for regular files.
1296 xfs_fileoff_t map_first;
1298 xfs_bmbt_irec_t imaps[2];
1300 if ((ip->i_d.di_mode & S_IFMT) != S_IFREG)
1303 if (XFS_IS_REALTIME_INODE(ip))
1306 if (ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE)
1310 map_first = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize);
1312 * The filesystem could be shutting down, so bmapi may return
1315 if (xfs_bmapi(NULL, ip, map_first,
1317 (xfs_ufsize_t)XFS_MAXIOFFSET(mp)) -
1319 XFS_BMAPI_ENTIRE, NULL, 0, imaps, &nimaps,
1322 ASSERT(nimaps == 1);
1323 ASSERT(imaps[0].br_startblock == HOLESTARTBLOCK);
1328 * Calculate the last possible buffered byte in a file. This must
1329 * include data that was buffered beyond the EOF by the write code.
1330 * This also needs to deal with overflowing the xfs_fsize_t type
1331 * which can happen for sizes near the limit.
1333 * We also need to take into account any blocks beyond the EOF. It
1334 * may be the case that they were buffered by a write which failed.
1335 * In that case the pages will still be in memory, but the inode size
1336 * will never have been updated.
1343 xfs_fsize_t last_byte;
1344 xfs_fileoff_t last_block;
1345 xfs_fileoff_t size_last_block;
1348 ASSERT(ismrlocked(&(ip->i_iolock), MR_UPDATE | MR_ACCESS));
1352 * Only check for blocks beyond the EOF if the extents have
1353 * been read in. This eliminates the need for the inode lock,
1354 * and it also saves us from looking when it really isn't
1357 if (ip->i_df.if_flags & XFS_IFEXTENTS) {
1358 error = xfs_bmap_last_offset(NULL, ip, &last_block,
1366 size_last_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)ip->i_size);
1367 last_block = XFS_FILEOFF_MAX(last_block, size_last_block);
1369 last_byte = XFS_FSB_TO_B(mp, last_block);
1370 if (last_byte < 0) {
1371 return XFS_MAXIOFFSET(mp);
1373 last_byte += (1 << mp->m_writeio_log);
1374 if (last_byte < 0) {
1375 return XFS_MAXIOFFSET(mp);
1380 #if defined(XFS_RW_TRACE)
1386 xfs_fsize_t new_size,
1387 xfs_off_t toss_start,
1388 xfs_off_t toss_finish)
1390 if (ip->i_rwtrace == NULL) {
1394 ktrace_enter(ip->i_rwtrace,
1397 (void*)(unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff),
1398 (void*)(unsigned long)(ip->i_d.di_size & 0xffffffff),
1399 (void*)((long)flag),
1400 (void*)(unsigned long)((new_size >> 32) & 0xffffffff),
1401 (void*)(unsigned long)(new_size & 0xffffffff),
1402 (void*)(unsigned long)((toss_start >> 32) & 0xffffffff),
1403 (void*)(unsigned long)(toss_start & 0xffffffff),
1404 (void*)(unsigned long)((toss_finish >> 32) & 0xffffffff),
1405 (void*)(unsigned long)(toss_finish & 0xffffffff),
1406 (void*)(unsigned long)current_cpu(),
1407 (void*)(unsigned long)current_pid(),
1413 #define xfs_itrunc_trace(tag, ip, flag, new_size, toss_start, toss_finish)
1417 * Start the truncation of the file to new_size. The new size
1418 * must be smaller than the current size. This routine will
1419 * clear the buffer and page caches of file data in the removed
1420 * range, and xfs_itruncate_finish() will remove the underlying
1423 * The inode must have its I/O lock locked EXCLUSIVELY, and it
1424 * must NOT have the inode lock held at all. This is because we're
1425 * calling into the buffer/page cache code and we can't hold the
1426 * inode lock when we do so.
1428 * We need to wait for any direct I/Os in flight to complete before we
1429 * proceed with the truncate. This is needed to prevent the extents
1430 * being read or written by the direct I/Os from being removed while the
1431 * I/O is in flight as there is no other method of synchronising
1432 * direct I/O with the truncate operation. Also, because we hold
1433 * the IOLOCK in exclusive mode, we prevent new direct I/Os from being
1434 * started until the truncate completes and drops the lock. Essentially,
1435 * the vn_iowait() call forms an I/O barrier that provides strict ordering
1436 * between direct I/Os and the truncate operation.
1438 * The flags parameter can have either the value XFS_ITRUNC_DEFINITE
1439 * or XFS_ITRUNC_MAYBE. The XFS_ITRUNC_MAYBE value should be used
1440 * in the case that the caller is locking things out of order and
1441 * may not be able to call xfs_itruncate_finish() with the inode lock
1442 * held without dropping the I/O lock. If the caller must drop the
1443 * I/O lock before calling xfs_itruncate_finish(), then xfs_itruncate_start()
1444 * must be called again with all the same restrictions as the initial
1448 xfs_itruncate_start(
1451 xfs_fsize_t new_size)
1453 xfs_fsize_t last_byte;
1454 xfs_off_t toss_start;
1459 ASSERT(ismrlocked(&ip->i_iolock, MR_UPDATE) != 0);
1460 ASSERT((new_size == 0) || (new_size <= ip->i_size));
1461 ASSERT((flags == XFS_ITRUNC_DEFINITE) ||
1462 (flags == XFS_ITRUNC_MAYBE));
1467 /* wait for the completion of any pending DIOs */
1468 if (new_size < ip->i_size)
1472 * Call toss_pages or flushinval_pages to get rid of pages
1473 * overlapping the region being removed. We have to use
1474 * the less efficient flushinval_pages in the case that the
1475 * caller may not be able to finish the truncate without
1476 * dropping the inode's I/O lock. Make sure
1477 * to catch any pages brought in by buffers overlapping
1478 * the EOF by searching out beyond the isize by our
1479 * block size. We round new_size up to a block boundary
1480 * so that we don't toss things on the same block as
1481 * new_size but before it.
1483 * Before calling toss_page or flushinval_pages, make sure to
1484 * call remapf() over the same region if the file is mapped.
1485 * This frees up mapped file references to the pages in the
1486 * given range and for the flushinval_pages case it ensures
1487 * that we get the latest mapped changes flushed out.
1489 toss_start = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size);
1490 toss_start = XFS_FSB_TO_B(mp, toss_start);
1491 if (toss_start < 0) {
1493 * The place to start tossing is beyond our maximum
1494 * file size, so there is no way that the data extended
1499 last_byte = xfs_file_last_byte(ip);
1500 xfs_itrunc_trace(XFS_ITRUNC_START, ip, flags, new_size, toss_start,
1502 if (last_byte > toss_start) {
1503 if (flags & XFS_ITRUNC_DEFINITE) {
1504 xfs_tosspages(ip, toss_start,
1505 -1, FI_REMAPF_LOCKED);
1507 error = xfs_flushinval_pages(ip, toss_start,
1508 -1, FI_REMAPF_LOCKED);
1513 if (new_size == 0) {
1514 ASSERT(VN_CACHED(vp) == 0);
1521 * Shrink the file to the given new_size. The new
1522 * size must be smaller than the current size.
1523 * This will free up the underlying blocks
1524 * in the removed range after a call to xfs_itruncate_start()
1525 * or xfs_atruncate_start().
1527 * The transaction passed to this routine must have made
1528 * a permanent log reservation of at least XFS_ITRUNCATE_LOG_RES.
1529 * This routine may commit the given transaction and
1530 * start new ones, so make sure everything involved in
1531 * the transaction is tidy before calling here.
1532 * Some transaction will be returned to the caller to be
1533 * committed. The incoming transaction must already include
1534 * the inode, and both inode locks must be held exclusively.
1535 * The inode must also be "held" within the transaction. On
1536 * return the inode will be "held" within the returned transaction.
1537 * This routine does NOT require any disk space to be reserved
1538 * for it within the transaction.
1540 * The fork parameter must be either xfs_attr_fork or xfs_data_fork,
1541 * and it indicates the fork which is to be truncated. For the
1542 * attribute fork we only support truncation to size 0.
1544 * We use the sync parameter to indicate whether or not the first
1545 * transaction we perform might have to be synchronous. For the attr fork,
1546 * it needs to be so if the unlink of the inode is not yet known to be
1547 * permanent in the log. This keeps us from freeing and reusing the
1548 * blocks of the attribute fork before the unlink of the inode becomes
1551 * For the data fork, we normally have to run synchronously if we're
1552 * being called out of the inactive path or we're being called
1553 * out of the create path where we're truncating an existing file.
1554 * Either way, the truncate needs to be sync so blocks don't reappear
1555 * in the file with altered data in case of a crash. wsync filesystems
1556 * can run the first case async because anything that shrinks the inode
1557 * has to run sync so by the time we're called here from inactive, the
1558 * inode size is permanently set to 0.
1560 * Calls from the truncate path always need to be sync unless we're
1561 * in a wsync filesystem and the file has already been unlinked.
1563 * The caller is responsible for correctly setting the sync parameter.
1564 * It gets too hard for us to guess here which path we're being called
1565 * out of just based on inode state.
1568 xfs_itruncate_finish(
1571 xfs_fsize_t new_size,
1575 xfs_fsblock_t first_block;
1576 xfs_fileoff_t first_unmap_block;
1577 xfs_fileoff_t last_block;
1578 xfs_filblks_t unmap_len=0;
1583 xfs_bmap_free_t free_list;
1586 ASSERT(ismrlocked(&ip->i_iolock, MR_UPDATE) != 0);
1587 ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE) != 0);
1588 ASSERT((new_size == 0) || (new_size <= ip->i_size));
1589 ASSERT(*tp != NULL);
1590 ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
1591 ASSERT(ip->i_transp == *tp);
1592 ASSERT(ip->i_itemp != NULL);
1593 ASSERT(ip->i_itemp->ili_flags & XFS_ILI_HOLD);
1597 mp = (ntp)->t_mountp;
1598 ASSERT(! XFS_NOT_DQATTACHED(mp, ip));
1601 * We only support truncating the entire attribute fork.
1603 if (fork == XFS_ATTR_FORK) {
1606 first_unmap_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size);
1607 xfs_itrunc_trace(XFS_ITRUNC_FINISH1, ip, 0, new_size, 0, 0);
1609 * The first thing we do is set the size to new_size permanently
1610 * on disk. This way we don't have to worry about anyone ever
1611 * being able to look at the data being freed even in the face
1612 * of a crash. What we're getting around here is the case where
1613 * we free a block, it is allocated to another file, it is written
1614 * to, and then we crash. If the new data gets written to the
1615 * file but the log buffers containing the free and reallocation
1616 * don't, then we'd end up with garbage in the blocks being freed.
1617 * As long as we make the new_size permanent before actually
1618 * freeing any blocks it doesn't matter if they get writtten to.
1620 * The callers must signal into us whether or not the size
1621 * setting here must be synchronous. There are a few cases
1622 * where it doesn't have to be synchronous. Those cases
1623 * occur if the file is unlinked and we know the unlink is
1624 * permanent or if the blocks being truncated are guaranteed
1625 * to be beyond the inode eof (regardless of the link count)
1626 * and the eof value is permanent. Both of these cases occur
1627 * only on wsync-mounted filesystems. In those cases, we're
1628 * guaranteed that no user will ever see the data in the blocks
1629 * that are being truncated so the truncate can run async.
1630 * In the free beyond eof case, the file may wind up with
1631 * more blocks allocated to it than it needs if we crash
1632 * and that won't get fixed until the next time the file
1633 * is re-opened and closed but that's ok as that shouldn't
1634 * be too many blocks.
1636 * However, we can't just make all wsync xactions run async
1637 * because there's one call out of the create path that needs
1638 * to run sync where it's truncating an existing file to size
1639 * 0 whose size is > 0.
1641 * It's probably possible to come up with a test in this
1642 * routine that would correctly distinguish all the above
1643 * cases from the values of the function parameters and the
1644 * inode state but for sanity's sake, I've decided to let the
1645 * layers above just tell us. It's simpler to correctly figure
1646 * out in the layer above exactly under what conditions we
1647 * can run async and I think it's easier for others read and
1648 * follow the logic in case something has to be changed.
1649 * cscope is your friend -- rcc.
1651 * The attribute fork is much simpler.
1653 * For the attribute fork we allow the caller to tell us whether
1654 * the unlink of the inode that led to this call is yet permanent
1655 * in the on disk log. If it is not and we will be freeing extents
1656 * in this inode then we make the first transaction synchronous
1657 * to make sure that the unlink is permanent by the time we free
1660 if (fork == XFS_DATA_FORK) {
1661 if (ip->i_d.di_nextents > 0) {
1663 * If we are not changing the file size then do
1664 * not update the on-disk file size - we may be
1665 * called from xfs_inactive_free_eofblocks(). If we
1666 * update the on-disk file size and then the system
1667 * crashes before the contents of the file are
1668 * flushed to disk then the files may be full of
1669 * holes (ie NULL files bug).
1671 if (ip->i_size != new_size) {
1672 ip->i_d.di_size = new_size;
1673 ip->i_size = new_size;
1674 xfs_trans_log_inode(ntp, ip, XFS_ILOG_CORE);
1678 ASSERT(!(mp->m_flags & XFS_MOUNT_WSYNC));
1679 if (ip->i_d.di_anextents > 0)
1680 xfs_trans_set_sync(ntp);
1682 ASSERT(fork == XFS_DATA_FORK ||
1683 (fork == XFS_ATTR_FORK &&
1684 ((sync && !(mp->m_flags & XFS_MOUNT_WSYNC)) ||
1685 (sync == 0 && (mp->m_flags & XFS_MOUNT_WSYNC)))));
1688 * Since it is possible for space to become allocated beyond
1689 * the end of the file (in a crash where the space is allocated
1690 * but the inode size is not yet updated), simply remove any
1691 * blocks which show up between the new EOF and the maximum
1692 * possible file size. If the first block to be removed is
1693 * beyond the maximum file size (ie it is the same as last_block),
1694 * then there is nothing to do.
1696 last_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
1697 ASSERT(first_unmap_block <= last_block);
1699 if (last_block == first_unmap_block) {
1702 unmap_len = last_block - first_unmap_block + 1;
1706 * Free up up to XFS_ITRUNC_MAX_EXTENTS. xfs_bunmapi()
1707 * will tell us whether it freed the entire range or
1708 * not. If this is a synchronous mount (wsync),
1709 * then we can tell bunmapi to keep all the
1710 * transactions asynchronous since the unlink
1711 * transaction that made this inode inactive has
1712 * already hit the disk. There's no danger of
1713 * the freed blocks being reused, there being a
1714 * crash, and the reused blocks suddenly reappearing
1715 * in this file with garbage in them once recovery
1718 XFS_BMAP_INIT(&free_list, &first_block);
1719 error = xfs_bunmapi(ntp, ip,
1720 first_unmap_block, unmap_len,
1721 XFS_BMAPI_AFLAG(fork) |
1722 (sync ? 0 : XFS_BMAPI_ASYNC),
1723 XFS_ITRUNC_MAX_EXTENTS,
1724 &first_block, &free_list,
1728 * If the bunmapi call encounters an error,
1729 * return to the caller where the transaction
1730 * can be properly aborted. We just need to
1731 * make sure we're not holding any resources
1732 * that we were not when we came in.
1734 xfs_bmap_cancel(&free_list);
1739 * Duplicate the transaction that has the permanent
1740 * reservation and commit the old transaction.
1742 error = xfs_bmap_finish(tp, &free_list, &committed);
1746 * If the bmap finish call encounters an error,
1747 * return to the caller where the transaction
1748 * can be properly aborted. We just need to
1749 * make sure we're not holding any resources
1750 * that we were not when we came in.
1752 * Aborting from this point might lose some
1753 * blocks in the file system, but oh well.
1755 xfs_bmap_cancel(&free_list);
1758 * If the passed in transaction committed
1759 * in xfs_bmap_finish(), then we want to
1760 * add the inode to this one before returning.
1761 * This keeps things simple for the higher
1762 * level code, because it always knows that
1763 * the inode is locked and held in the
1764 * transaction that returns to it whether
1765 * errors occur or not. We don't mark the
1766 * inode dirty so that this transaction can
1767 * be easily aborted if possible.
1769 xfs_trans_ijoin(ntp, ip,
1770 XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1771 xfs_trans_ihold(ntp, ip);
1778 * The first xact was committed,
1779 * so add the inode to the new one.
1780 * Mark it dirty so it will be logged
1781 * and moved forward in the log as
1782 * part of every commit.
1784 xfs_trans_ijoin(ntp, ip,
1785 XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1786 xfs_trans_ihold(ntp, ip);
1787 xfs_trans_log_inode(ntp, ip, XFS_ILOG_CORE);
1789 ntp = xfs_trans_dup(ntp);
1790 (void) xfs_trans_commit(*tp, 0);
1792 error = xfs_trans_reserve(ntp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
1793 XFS_TRANS_PERM_LOG_RES,
1794 XFS_ITRUNCATE_LOG_COUNT);
1796 * Add the inode being truncated to the next chained
1799 xfs_trans_ijoin(ntp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1800 xfs_trans_ihold(ntp, ip);
1805 * Only update the size in the case of the data fork, but
1806 * always re-log the inode so that our permanent transaction
1807 * can keep on rolling it forward in the log.
1809 if (fork == XFS_DATA_FORK) {
1810 xfs_isize_check(mp, ip, new_size);
1812 * If we are not changing the file size then do
1813 * not update the on-disk file size - we may be
1814 * called from xfs_inactive_free_eofblocks(). If we
1815 * update the on-disk file size and then the system
1816 * crashes before the contents of the file are
1817 * flushed to disk then the files may be full of
1818 * holes (ie NULL files bug).
1820 if (ip->i_size != new_size) {
1821 ip->i_d.di_size = new_size;
1822 ip->i_size = new_size;
1825 xfs_trans_log_inode(ntp, ip, XFS_ILOG_CORE);
1826 ASSERT((new_size != 0) ||
1827 (fork == XFS_ATTR_FORK) ||
1828 (ip->i_delayed_blks == 0));
1829 ASSERT((new_size != 0) ||
1830 (fork == XFS_ATTR_FORK) ||
1831 (ip->i_d.di_nextents == 0));
1832 xfs_itrunc_trace(XFS_ITRUNC_FINISH2, ip, 0, new_size, 0, 0);
1840 * Do the first part of growing a file: zero any data in the last
1841 * block that is beyond the old EOF. We need to do this before
1842 * the inode is joined to the transaction to modify the i_size.
1843 * That way we can drop the inode lock and call into the buffer
1844 * cache to get the buffer mapping the EOF.
1849 xfs_fsize_t new_size,
1852 ASSERT(ismrlocked(&(ip->i_lock), MR_UPDATE) != 0);
1853 ASSERT(ismrlocked(&(ip->i_iolock), MR_UPDATE) != 0);
1854 ASSERT(new_size > ip->i_size);
1857 * Zero any pages that may have been created by
1858 * xfs_write_file() beyond the end of the file
1859 * and any blocks between the old and new file sizes.
1861 return xfs_zero_eof(ip, new_size, ip->i_size);
1867 * This routine is called to extend the size of a file.
1868 * The inode must have both the iolock and the ilock locked
1869 * for update and it must be a part of the current transaction.
1870 * The xfs_igrow_start() function must have been called previously.
1871 * If the change_flag is not zero, the inode change timestamp will
1878 xfs_fsize_t new_size,
1881 ASSERT(ismrlocked(&(ip->i_lock), MR_UPDATE) != 0);
1882 ASSERT(ismrlocked(&(ip->i_iolock), MR_UPDATE) != 0);
1883 ASSERT(ip->i_transp == tp);
1884 ASSERT(new_size > ip->i_size);
1887 * Update the file size. Update the inode change timestamp
1888 * if change_flag set.
1890 ip->i_d.di_size = new_size;
1891 ip->i_size = new_size;
1893 xfs_ichgtime(ip, XFS_ICHGTIME_CHG);
1894 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1900 * This is called when the inode's link count goes to 0.
1901 * We place the on-disk inode on a list in the AGI. It
1902 * will be pulled from this list when the inode is freed.
1914 xfs_agnumber_t agno;
1915 xfs_daddr_t agdaddr;
1922 ASSERT(ip->i_d.di_nlink == 0);
1923 ASSERT(ip->i_d.di_mode != 0);
1924 ASSERT(ip->i_transp == tp);
1928 agno = XFS_INO_TO_AGNO(mp, ip->i_ino);
1929 agdaddr = XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp));
1932 * Get the agi buffer first. It ensures lock ordering
1935 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, agdaddr,
1936 XFS_FSS_TO_BB(mp, 1), 0, &agibp);
1941 * Validate the magic number of the agi block.
1943 agi = XFS_BUF_TO_AGI(agibp);
1945 be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC &&
1946 XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum));
1947 if (unlikely(XFS_TEST_ERROR(!agi_ok, mp, XFS_ERRTAG_IUNLINK,
1948 XFS_RANDOM_IUNLINK))) {
1949 XFS_CORRUPTION_ERROR("xfs_iunlink", XFS_ERRLEVEL_LOW, mp, agi);
1950 xfs_trans_brelse(tp, agibp);
1951 return XFS_ERROR(EFSCORRUPTED);
1954 * Get the index into the agi hash table for the
1955 * list this inode will go on.
1957 agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
1959 bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
1960 ASSERT(agi->agi_unlinked[bucket_index]);
1961 ASSERT(be32_to_cpu(agi->agi_unlinked[bucket_index]) != agino);
1963 if (be32_to_cpu(agi->agi_unlinked[bucket_index]) != NULLAGINO) {
1965 * There is already another inode in the bucket we need
1966 * to add ourselves to. Add us at the front of the list.
1967 * Here we put the head pointer into our next pointer,
1968 * and then we fall through to point the head at us.
1970 error = xfs_itobp(mp, tp, ip, &dip, &ibp, 0, 0);
1974 ASSERT(be32_to_cpu(dip->di_next_unlinked) == NULLAGINO);
1975 /* both on-disk, don't endian flip twice */
1976 dip->di_next_unlinked = agi->agi_unlinked[bucket_index];
1977 offset = ip->i_boffset +
1978 offsetof(xfs_dinode_t, di_next_unlinked);
1979 xfs_trans_inode_buf(tp, ibp);
1980 xfs_trans_log_buf(tp, ibp, offset,
1981 (offset + sizeof(xfs_agino_t) - 1));
1982 xfs_inobp_check(mp, ibp);
1986 * Point the bucket head pointer at the inode being inserted.
1989 agi->agi_unlinked[bucket_index] = cpu_to_be32(agino);
1990 offset = offsetof(xfs_agi_t, agi_unlinked) +
1991 (sizeof(xfs_agino_t) * bucket_index);
1992 xfs_trans_log_buf(tp, agibp, offset,
1993 (offset + sizeof(xfs_agino_t) - 1));
1998 * Pull the on-disk inode from the AGI unlinked list.
2011 xfs_agnumber_t agno;
2012 xfs_daddr_t agdaddr;
2014 xfs_agino_t next_agino;
2015 xfs_buf_t *last_ibp;
2016 xfs_dinode_t *last_dip = NULL;
2018 int offset, last_offset = 0;
2023 * First pull the on-disk inode from the AGI unlinked list.
2027 agno = XFS_INO_TO_AGNO(mp, ip->i_ino);
2028 agdaddr = XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp));
2031 * Get the agi buffer first. It ensures lock ordering
2034 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, agdaddr,
2035 XFS_FSS_TO_BB(mp, 1), 0, &agibp);
2038 "xfs_iunlink_remove: xfs_trans_read_buf() returned an error %d on %s. Returning error.",
2039 error, mp->m_fsname);
2043 * Validate the magic number of the agi block.
2045 agi = XFS_BUF_TO_AGI(agibp);
2047 be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC &&
2048 XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum));
2049 if (unlikely(XFS_TEST_ERROR(!agi_ok, mp, XFS_ERRTAG_IUNLINK_REMOVE,
2050 XFS_RANDOM_IUNLINK_REMOVE))) {
2051 XFS_CORRUPTION_ERROR("xfs_iunlink_remove", XFS_ERRLEVEL_LOW,
2053 xfs_trans_brelse(tp, agibp);
2055 "xfs_iunlink_remove: XFS_TEST_ERROR() returned an error on %s. Returning EFSCORRUPTED.",
2057 return XFS_ERROR(EFSCORRUPTED);
2060 * Get the index into the agi hash table for the
2061 * list this inode will go on.
2063 agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
2065 bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
2066 ASSERT(be32_to_cpu(agi->agi_unlinked[bucket_index]) != NULLAGINO);
2067 ASSERT(agi->agi_unlinked[bucket_index]);
2069 if (be32_to_cpu(agi->agi_unlinked[bucket_index]) == agino) {
2071 * We're at the head of the list. Get the inode's
2072 * on-disk buffer to see if there is anyone after us
2073 * on the list. Only modify our next pointer if it
2074 * is not already NULLAGINO. This saves us the overhead
2075 * of dealing with the buffer when there is no need to
2078 error = xfs_itobp(mp, tp, ip, &dip, &ibp, 0, 0);
2081 "xfs_iunlink_remove: xfs_itobp() returned an error %d on %s. Returning error.",
2082 error, mp->m_fsname);
2085 next_agino = be32_to_cpu(dip->di_next_unlinked);
2086 ASSERT(next_agino != 0);
2087 if (next_agino != NULLAGINO) {
2088 dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
2089 offset = ip->i_boffset +
2090 offsetof(xfs_dinode_t, di_next_unlinked);
2091 xfs_trans_inode_buf(tp, ibp);
2092 xfs_trans_log_buf(tp, ibp, offset,
2093 (offset + sizeof(xfs_agino_t) - 1));
2094 xfs_inobp_check(mp, ibp);
2096 xfs_trans_brelse(tp, ibp);
2099 * Point the bucket head pointer at the next inode.
2101 ASSERT(next_agino != 0);
2102 ASSERT(next_agino != agino);
2103 agi->agi_unlinked[bucket_index] = cpu_to_be32(next_agino);
2104 offset = offsetof(xfs_agi_t, agi_unlinked) +
2105 (sizeof(xfs_agino_t) * bucket_index);
2106 xfs_trans_log_buf(tp, agibp, offset,
2107 (offset + sizeof(xfs_agino_t) - 1));
2110 * We need to search the list for the inode being freed.
2112 next_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]);
2114 while (next_agino != agino) {
2116 * If the last inode wasn't the one pointing to
2117 * us, then release its buffer since we're not
2118 * going to do anything with it.
2120 if (last_ibp != NULL) {
2121 xfs_trans_brelse(tp, last_ibp);
2123 next_ino = XFS_AGINO_TO_INO(mp, agno, next_agino);
2124 error = xfs_inotobp(mp, tp, next_ino, &last_dip,
2125 &last_ibp, &last_offset);
2128 "xfs_iunlink_remove: xfs_inotobp() returned an error %d on %s. Returning error.",
2129 error, mp->m_fsname);
2132 next_agino = be32_to_cpu(last_dip->di_next_unlinked);
2133 ASSERT(next_agino != NULLAGINO);
2134 ASSERT(next_agino != 0);
2137 * Now last_ibp points to the buffer previous to us on
2138 * the unlinked list. Pull us from the list.
2140 error = xfs_itobp(mp, tp, ip, &dip, &ibp, 0, 0);
2143 "xfs_iunlink_remove: xfs_itobp() returned an error %d on %s. Returning error.",
2144 error, mp->m_fsname);
2147 next_agino = be32_to_cpu(dip->di_next_unlinked);
2148 ASSERT(next_agino != 0);
2149 ASSERT(next_agino != agino);
2150 if (next_agino != NULLAGINO) {
2151 dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
2152 offset = ip->i_boffset +
2153 offsetof(xfs_dinode_t, di_next_unlinked);
2154 xfs_trans_inode_buf(tp, ibp);
2155 xfs_trans_log_buf(tp, ibp, offset,
2156 (offset + sizeof(xfs_agino_t) - 1));
2157 xfs_inobp_check(mp, ibp);
2159 xfs_trans_brelse(tp, ibp);
2162 * Point the previous inode on the list to the next inode.
2164 last_dip->di_next_unlinked = cpu_to_be32(next_agino);
2165 ASSERT(next_agino != 0);
2166 offset = last_offset + offsetof(xfs_dinode_t, di_next_unlinked);
2167 xfs_trans_inode_buf(tp, last_ibp);
2168 xfs_trans_log_buf(tp, last_ibp, offset,
2169 (offset + sizeof(xfs_agino_t) - 1));
2170 xfs_inobp_check(mp, last_ibp);
2175 STATIC_INLINE int xfs_inode_clean(xfs_inode_t *ip)
2177 return (((ip->i_itemp == NULL) ||
2178 !(ip->i_itemp->ili_format.ilf_fields & XFS_ILOG_ALL)) &&
2179 (ip->i_update_core == 0));
2184 xfs_inode_t *free_ip,
2188 xfs_mount_t *mp = free_ip->i_mount;
2189 int blks_per_cluster;
2192 int i, j, found, pre_flushed;
2195 xfs_inode_t *ip, **ip_found;
2196 xfs_inode_log_item_t *iip;
2197 xfs_log_item_t *lip;
2198 xfs_perag_t *pag = xfs_get_perag(mp, inum);
2200 if (mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) {
2201 blks_per_cluster = 1;
2202 ninodes = mp->m_sb.sb_inopblock;
2203 nbufs = XFS_IALLOC_BLOCKS(mp);
2205 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) /
2206 mp->m_sb.sb_blocksize;
2207 ninodes = blks_per_cluster * mp->m_sb.sb_inopblock;
2208 nbufs = XFS_IALLOC_BLOCKS(mp) / blks_per_cluster;
2211 ip_found = kmem_alloc(ninodes * sizeof(xfs_inode_t *), KM_NOFS);
2213 for (j = 0; j < nbufs; j++, inum += ninodes) {
2214 blkno = XFS_AGB_TO_DADDR(mp, XFS_INO_TO_AGNO(mp, inum),
2215 XFS_INO_TO_AGBNO(mp, inum));
2219 * Look for each inode in memory and attempt to lock it,
2220 * we can be racing with flush and tail pushing here.
2221 * any inode we get the locks on, add to an array of
2222 * inode items to process later.
2224 * The get the buffer lock, we could beat a flush
2225 * or tail pushing thread to the lock here, in which
2226 * case they will go looking for the inode buffer
2227 * and fail, we need some other form of interlock
2231 for (i = 0; i < ninodes; i++) {
2232 read_lock(&pag->pag_ici_lock);
2233 ip = radix_tree_lookup(&pag->pag_ici_root,
2234 XFS_INO_TO_AGINO(mp, (inum + i)));
2236 /* Inode not in memory or we found it already,
2239 if (!ip || xfs_iflags_test(ip, XFS_ISTALE)) {
2240 read_unlock(&pag->pag_ici_lock);
2244 if (xfs_inode_clean(ip)) {
2245 read_unlock(&pag->pag_ici_lock);
2249 /* If we can get the locks then add it to the
2250 * list, otherwise by the time we get the bp lock
2251 * below it will already be attached to the
2255 /* This inode will already be locked - by us, lets
2259 if (ip == free_ip) {
2260 if (xfs_iflock_nowait(ip)) {
2261 xfs_iflags_set(ip, XFS_ISTALE);
2262 if (xfs_inode_clean(ip)) {
2265 ip_found[found++] = ip;
2268 read_unlock(&pag->pag_ici_lock);
2272 if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
2273 if (xfs_iflock_nowait(ip)) {
2274 xfs_iflags_set(ip, XFS_ISTALE);
2276 if (xfs_inode_clean(ip)) {
2278 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2280 ip_found[found++] = ip;
2283 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2286 read_unlock(&pag->pag_ici_lock);
2289 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno,
2290 mp->m_bsize * blks_per_cluster,
2294 lip = XFS_BUF_FSPRIVATE(bp, xfs_log_item_t *);
2296 if (lip->li_type == XFS_LI_INODE) {
2297 iip = (xfs_inode_log_item_t *)lip;
2298 ASSERT(iip->ili_logged == 1);
2299 lip->li_cb = (void(*)(xfs_buf_t*,xfs_log_item_t*)) xfs_istale_done;
2300 spin_lock(&mp->m_ail_lock);
2301 iip->ili_flush_lsn = iip->ili_item.li_lsn;
2302 spin_unlock(&mp->m_ail_lock);
2303 xfs_iflags_set(iip->ili_inode, XFS_ISTALE);
2306 lip = lip->li_bio_list;
2309 for (i = 0; i < found; i++) {
2314 ip->i_update_core = 0;
2316 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2320 iip->ili_last_fields = iip->ili_format.ilf_fields;
2321 iip->ili_format.ilf_fields = 0;
2322 iip->ili_logged = 1;
2323 spin_lock(&mp->m_ail_lock);
2324 iip->ili_flush_lsn = iip->ili_item.li_lsn;
2325 spin_unlock(&mp->m_ail_lock);
2327 xfs_buf_attach_iodone(bp,
2328 (void(*)(xfs_buf_t*,xfs_log_item_t*))
2329 xfs_istale_done, (xfs_log_item_t *)iip);
2330 if (ip != free_ip) {
2331 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2335 if (found || pre_flushed)
2336 xfs_trans_stale_inode_buf(tp, bp);
2337 xfs_trans_binval(tp, bp);
2340 kmem_free(ip_found, ninodes * sizeof(xfs_inode_t *));
2341 xfs_put_perag(mp, pag);
2345 * This is called to return an inode to the inode free list.
2346 * The inode should already be truncated to 0 length and have
2347 * no pages associated with it. This routine also assumes that
2348 * the inode is already a part of the transaction.
2350 * The on-disk copy of the inode will have been added to the list
2351 * of unlinked inodes in the AGI. We need to remove the inode from
2352 * that list atomically with respect to freeing it here.
2358 xfs_bmap_free_t *flist)
2362 xfs_ino_t first_ino;
2366 ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE));
2367 ASSERT(ip->i_transp == tp);
2368 ASSERT(ip->i_d.di_nlink == 0);
2369 ASSERT(ip->i_d.di_nextents == 0);
2370 ASSERT(ip->i_d.di_anextents == 0);
2371 ASSERT((ip->i_d.di_size == 0 && ip->i_size == 0) ||
2372 ((ip->i_d.di_mode & S_IFMT) != S_IFREG));
2373 ASSERT(ip->i_d.di_nblocks == 0);
2376 * Pull the on-disk inode from the AGI unlinked list.
2378 error = xfs_iunlink_remove(tp, ip);
2383 error = xfs_difree(tp, ip->i_ino, flist, &delete, &first_ino);
2387 ip->i_d.di_mode = 0; /* mark incore inode as free */
2388 ip->i_d.di_flags = 0;
2389 ip->i_d.di_dmevmask = 0;
2390 ip->i_d.di_forkoff = 0; /* mark the attr fork not in use */
2391 ip->i_df.if_ext_max =
2392 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
2393 ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
2394 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
2396 * Bump the generation count so no one will be confused
2397 * by reincarnations of this inode.
2401 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2403 error = xfs_itobp(ip->i_mount, tp, ip, &dip, &ibp, 0, 0);
2408 * Clear the on-disk di_mode. This is to prevent xfs_bulkstat
2409 * from picking up this inode when it is reclaimed (its incore state
2410 * initialzed but not flushed to disk yet). The in-core di_mode is
2411 * already cleared and a corresponding transaction logged.
2412 * The hack here just synchronizes the in-core to on-disk
2413 * di_mode value in advance before the actual inode sync to disk.
2414 * This is OK because the inode is already unlinked and would never
2415 * change its di_mode again for this inode generation.
2416 * This is a temporary hack that would require a proper fix
2419 dip->di_core.di_mode = 0;
2422 xfs_ifree_cluster(ip, tp, first_ino);
2429 * Reallocate the space for if_broot based on the number of records
2430 * being added or deleted as indicated in rec_diff. Move the records
2431 * and pointers in if_broot to fit the new size. When shrinking this
2432 * will eliminate holes between the records and pointers created by
2433 * the caller. When growing this will create holes to be filled in
2436 * The caller must not request to add more records than would fit in
2437 * the on-disk inode root. If the if_broot is currently NULL, then
2438 * if we adding records one will be allocated. The caller must also
2439 * not request that the number of records go below zero, although
2440 * it can go to zero.
2442 * ip -- the inode whose if_broot area is changing
2443 * ext_diff -- the change in the number of records, positive or negative,
2444 * requested for the if_broot array.
2454 xfs_bmbt_block_t *new_broot;
2461 * Handle the degenerate case quietly.
2463 if (rec_diff == 0) {
2467 ifp = XFS_IFORK_PTR(ip, whichfork);
2470 * If there wasn't any memory allocated before, just
2471 * allocate it now and get out.
2473 if (ifp->if_broot_bytes == 0) {
2474 new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(rec_diff);
2475 ifp->if_broot = (xfs_bmbt_block_t*)kmem_alloc(new_size,
2477 ifp->if_broot_bytes = (int)new_size;
2482 * If there is already an existing if_broot, then we need
2483 * to realloc() it and shift the pointers to their new
2484 * location. The records don't change location because
2485 * they are kept butted up against the btree block header.
2487 cur_max = XFS_BMAP_BROOT_MAXRECS(ifp->if_broot_bytes);
2488 new_max = cur_max + rec_diff;
2489 new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(new_max);
2490 ifp->if_broot = (xfs_bmbt_block_t *)
2491 kmem_realloc(ifp->if_broot,
2493 (size_t)XFS_BMAP_BROOT_SPACE_CALC(cur_max), /* old size */
2495 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(ifp->if_broot, 1,
2496 ifp->if_broot_bytes);
2497 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(ifp->if_broot, 1,
2499 ifp->if_broot_bytes = (int)new_size;
2500 ASSERT(ifp->if_broot_bytes <=
2501 XFS_IFORK_SIZE(ip, whichfork) + XFS_BROOT_SIZE_ADJ);
2502 memmove(np, op, cur_max * (uint)sizeof(xfs_dfsbno_t));
2507 * rec_diff is less than 0. In this case, we are shrinking the
2508 * if_broot buffer. It must already exist. If we go to zero
2509 * records, just get rid of the root and clear the status bit.
2511 ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0));
2512 cur_max = XFS_BMAP_BROOT_MAXRECS(ifp->if_broot_bytes);
2513 new_max = cur_max + rec_diff;
2514 ASSERT(new_max >= 0);
2516 new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(new_max);
2520 new_broot = (xfs_bmbt_block_t *)kmem_alloc(new_size, KM_SLEEP);
2522 * First copy over the btree block header.
2524 memcpy(new_broot, ifp->if_broot, sizeof(xfs_bmbt_block_t));
2527 ifp->if_flags &= ~XFS_IFBROOT;
2531 * Only copy the records and pointers if there are any.
2535 * First copy the records.
2537 op = (char *)XFS_BMAP_BROOT_REC_ADDR(ifp->if_broot, 1,
2538 ifp->if_broot_bytes);
2539 np = (char *)XFS_BMAP_BROOT_REC_ADDR(new_broot, 1,
2541 memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t));
2544 * Then copy the pointers.
2546 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(ifp->if_broot, 1,
2547 ifp->if_broot_bytes);
2548 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(new_broot, 1,
2550 memcpy(np, op, new_max * (uint)sizeof(xfs_dfsbno_t));
2552 kmem_free(ifp->if_broot, ifp->if_broot_bytes);
2553 ifp->if_broot = new_broot;
2554 ifp->if_broot_bytes = (int)new_size;
2555 ASSERT(ifp->if_broot_bytes <=
2556 XFS_IFORK_SIZE(ip, whichfork) + XFS_BROOT_SIZE_ADJ);
2562 * This is called when the amount of space needed for if_data
2563 * is increased or decreased. The change in size is indicated by
2564 * the number of bytes that need to be added or deleted in the
2565 * byte_diff parameter.
2567 * If the amount of space needed has decreased below the size of the
2568 * inline buffer, then switch to using the inline buffer. Otherwise,
2569 * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer
2570 * to what is needed.
2572 * ip -- the inode whose if_data area is changing
2573 * byte_diff -- the change in the number of bytes, positive or negative,
2574 * requested for the if_data array.
2586 if (byte_diff == 0) {
2590 ifp = XFS_IFORK_PTR(ip, whichfork);
2591 new_size = (int)ifp->if_bytes + byte_diff;
2592 ASSERT(new_size >= 0);
2594 if (new_size == 0) {
2595 if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
2596 kmem_free(ifp->if_u1.if_data, ifp->if_real_bytes);
2598 ifp->if_u1.if_data = NULL;
2600 } else if (new_size <= sizeof(ifp->if_u2.if_inline_data)) {
2602 * If the valid extents/data can fit in if_inline_ext/data,
2603 * copy them from the malloc'd vector and free it.
2605 if (ifp->if_u1.if_data == NULL) {
2606 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
2607 } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
2608 ASSERT(ifp->if_real_bytes != 0);
2609 memcpy(ifp->if_u2.if_inline_data, ifp->if_u1.if_data,
2611 kmem_free(ifp->if_u1.if_data, ifp->if_real_bytes);
2612 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
2617 * Stuck with malloc/realloc.
2618 * For inline data, the underlying buffer must be
2619 * a multiple of 4 bytes in size so that it can be
2620 * logged and stay on word boundaries. We enforce
2623 real_size = roundup(new_size, 4);
2624 if (ifp->if_u1.if_data == NULL) {
2625 ASSERT(ifp->if_real_bytes == 0);
2626 ifp->if_u1.if_data = kmem_alloc(real_size, KM_SLEEP);
2627 } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
2629 * Only do the realloc if the underlying size
2630 * is really changing.
2632 if (ifp->if_real_bytes != real_size) {
2633 ifp->if_u1.if_data =
2634 kmem_realloc(ifp->if_u1.if_data,
2640 ASSERT(ifp->if_real_bytes == 0);
2641 ifp->if_u1.if_data = kmem_alloc(real_size, KM_SLEEP);
2642 memcpy(ifp->if_u1.if_data, ifp->if_u2.if_inline_data,
2646 ifp->if_real_bytes = real_size;
2647 ifp->if_bytes = new_size;
2648 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
2655 * Map inode to disk block and offset.
2657 * mp -- the mount point structure for the current file system
2658 * tp -- the current transaction
2659 * ino -- the inode number of the inode to be located
2660 * imap -- this structure is filled in with the information necessary
2661 * to retrieve the given inode from disk
2662 * flags -- flags to pass to xfs_dilocate indicating whether or not
2663 * lookups in the inode btree were OK or not
2673 xfs_fsblock_t fsbno;
2678 fsbno = imap->im_blkno ?
2679 XFS_DADDR_TO_FSB(mp, imap->im_blkno) : NULLFSBLOCK;
2680 error = xfs_dilocate(mp, tp, ino, &fsbno, &len, &off, flags);
2684 imap->im_blkno = XFS_FSB_TO_DADDR(mp, fsbno);
2685 imap->im_len = XFS_FSB_TO_BB(mp, len);
2686 imap->im_agblkno = XFS_FSB_TO_AGBNO(mp, fsbno);
2687 imap->im_ioffset = (ushort)off;
2688 imap->im_boffset = (ushort)(off << mp->m_sb.sb_inodelog);
2699 ifp = XFS_IFORK_PTR(ip, whichfork);
2700 if (ifp->if_broot != NULL) {
2701 kmem_free(ifp->if_broot, ifp->if_broot_bytes);
2702 ifp->if_broot = NULL;
2706 * If the format is local, then we can't have an extents
2707 * array so just look for an inline data array. If we're
2708 * not local then we may or may not have an extents list,
2709 * so check and free it up if we do.
2711 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
2712 if ((ifp->if_u1.if_data != ifp->if_u2.if_inline_data) &&
2713 (ifp->if_u1.if_data != NULL)) {
2714 ASSERT(ifp->if_real_bytes != 0);
2715 kmem_free(ifp->if_u1.if_data, ifp->if_real_bytes);
2716 ifp->if_u1.if_data = NULL;
2717 ifp->if_real_bytes = 0;
2719 } else if ((ifp->if_flags & XFS_IFEXTENTS) &&
2720 ((ifp->if_flags & XFS_IFEXTIREC) ||
2721 ((ifp->if_u1.if_extents != NULL) &&
2722 (ifp->if_u1.if_extents != ifp->if_u2.if_inline_ext)))) {
2723 ASSERT(ifp->if_real_bytes != 0);
2724 xfs_iext_destroy(ifp);
2726 ASSERT(ifp->if_u1.if_extents == NULL ||
2727 ifp->if_u1.if_extents == ifp->if_u2.if_inline_ext);
2728 ASSERT(ifp->if_real_bytes == 0);
2729 if (whichfork == XFS_ATTR_FORK) {
2730 kmem_zone_free(xfs_ifork_zone, ip->i_afp);
2736 * This is called free all the memory associated with an inode.
2737 * It must free the inode itself and any buffers allocated for
2738 * if_extents/if_data and if_broot. It must also free the lock
2739 * associated with the inode.
2745 switch (ip->i_d.di_mode & S_IFMT) {
2749 xfs_idestroy_fork(ip, XFS_DATA_FORK);
2753 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
2754 mrfree(&ip->i_lock);
2755 mrfree(&ip->i_iolock);
2756 freesema(&ip->i_flock);
2758 #ifdef XFS_INODE_TRACE
2759 ktrace_free(ip->i_trace);
2761 #ifdef XFS_BMAP_TRACE
2762 ktrace_free(ip->i_xtrace);
2764 #ifdef XFS_BMBT_TRACE
2765 ktrace_free(ip->i_btrace);
2768 ktrace_free(ip->i_rwtrace);
2770 #ifdef XFS_ILOCK_TRACE
2771 ktrace_free(ip->i_lock_trace);
2773 #ifdef XFS_DIR2_TRACE
2774 ktrace_free(ip->i_dir_trace);
2778 * Only if we are shutting down the fs will we see an
2779 * inode still in the AIL. If it is there, we should remove
2780 * it to prevent a use-after-free from occurring.
2782 xfs_mount_t *mp = ip->i_mount;
2783 xfs_log_item_t *lip = &ip->i_itemp->ili_item;
2785 ASSERT(((lip->li_flags & XFS_LI_IN_AIL) == 0) ||
2786 XFS_FORCED_SHUTDOWN(ip->i_mount));
2787 if (lip->li_flags & XFS_LI_IN_AIL) {
2788 spin_lock(&mp->m_ail_lock);
2789 if (lip->li_flags & XFS_LI_IN_AIL)
2790 xfs_trans_delete_ail(mp, lip);
2792 spin_unlock(&mp->m_ail_lock);
2794 xfs_inode_item_destroy(ip);
2796 kmem_zone_free(xfs_inode_zone, ip);
2801 * Increment the pin count of the given buffer.
2802 * This value is protected by ipinlock spinlock in the mount structure.
2808 ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE));
2810 atomic_inc(&ip->i_pincount);
2814 * Decrement the pin count of the given inode, and wake up
2815 * anyone in xfs_iwait_unpin() if the count goes to 0. The
2816 * inode must have been previously pinned with a call to xfs_ipin().
2822 ASSERT(atomic_read(&ip->i_pincount) > 0);
2824 if (atomic_dec_and_test(&ip->i_pincount))
2825 wake_up(&ip->i_ipin_wait);
2829 * This is called to wait for the given inode to be unpinned.
2830 * It will sleep until this happens. The caller must have the
2831 * inode locked in at least shared mode so that the buffer cannot
2832 * be subsequently pinned once someone is waiting for it to be
2839 xfs_inode_log_item_t *iip;
2842 ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE | MR_ACCESS));
2844 if (atomic_read(&ip->i_pincount) == 0) {
2849 if (iip && iip->ili_last_lsn) {
2850 lsn = iip->ili_last_lsn;
2856 * Give the log a push so we don't wait here too long.
2858 xfs_log_force(ip->i_mount, lsn, XFS_LOG_FORCE);
2860 wait_event(ip->i_ipin_wait, (atomic_read(&ip->i_pincount) == 0));
2865 * xfs_iextents_copy()
2867 * This is called to copy the REAL extents (as opposed to the delayed
2868 * allocation extents) from the inode into the given buffer. It
2869 * returns the number of bytes copied into the buffer.
2871 * If there are no delayed allocation extents, then we can just
2872 * memcpy() the extents into the buffer. Otherwise, we need to
2873 * examine each extent in turn and skip those which are delayed.
2885 xfs_fsblock_t start_block;
2887 ifp = XFS_IFORK_PTR(ip, whichfork);
2888 ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE|MR_ACCESS));
2889 ASSERT(ifp->if_bytes > 0);
2891 nrecs = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
2892 XFS_BMAP_TRACE_EXLIST(ip, nrecs, whichfork);
2896 * There are some delayed allocation extents in the
2897 * inode, so copy the extents one at a time and skip
2898 * the delayed ones. There must be at least one
2899 * non-delayed extent.
2902 for (i = 0; i < nrecs; i++) {
2903 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
2904 start_block = xfs_bmbt_get_startblock(ep);
2905 if (ISNULLSTARTBLOCK(start_block)) {
2907 * It's a delayed allocation extent, so skip it.
2912 /* Translate to on disk format */
2913 put_unaligned(cpu_to_be64(ep->l0), &dp->l0);
2914 put_unaligned(cpu_to_be64(ep->l1), &dp->l1);
2918 ASSERT(copied != 0);
2919 xfs_validate_extents(ifp, copied, XFS_EXTFMT_INODE(ip));
2921 return (copied * (uint)sizeof(xfs_bmbt_rec_t));
2925 * Each of the following cases stores data into the same region
2926 * of the on-disk inode, so only one of them can be valid at
2927 * any given time. While it is possible to have conflicting formats
2928 * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
2929 * in EXTENTS format, this can only happen when the fork has
2930 * changed formats after being modified but before being flushed.
2931 * In these cases, the format always takes precedence, because the
2932 * format indicates the current state of the fork.
2939 xfs_inode_log_item_t *iip,
2946 #ifdef XFS_TRANS_DEBUG
2949 static const short brootflag[2] =
2950 { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT };
2951 static const short dataflag[2] =
2952 { XFS_ILOG_DDATA, XFS_ILOG_ADATA };
2953 static const short extflag[2] =
2954 { XFS_ILOG_DEXT, XFS_ILOG_AEXT };
2958 ifp = XFS_IFORK_PTR(ip, whichfork);
2960 * This can happen if we gave up in iformat in an error path,
2961 * for the attribute fork.
2964 ASSERT(whichfork == XFS_ATTR_FORK);
2967 cp = XFS_DFORK_PTR(dip, whichfork);
2969 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
2970 case XFS_DINODE_FMT_LOCAL:
2971 if ((iip->ili_format.ilf_fields & dataflag[whichfork]) &&
2972 (ifp->if_bytes > 0)) {
2973 ASSERT(ifp->if_u1.if_data != NULL);
2974 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
2975 memcpy(cp, ifp->if_u1.if_data, ifp->if_bytes);
2979 case XFS_DINODE_FMT_EXTENTS:
2980 ASSERT((ifp->if_flags & XFS_IFEXTENTS) ||
2981 !(iip->ili_format.ilf_fields & extflag[whichfork]));
2982 ASSERT((xfs_iext_get_ext(ifp, 0) != NULL) ||
2983 (ifp->if_bytes == 0));
2984 ASSERT((xfs_iext_get_ext(ifp, 0) == NULL) ||
2985 (ifp->if_bytes > 0));
2986 if ((iip->ili_format.ilf_fields & extflag[whichfork]) &&
2987 (ifp->if_bytes > 0)) {
2988 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) > 0);
2989 (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp,
2994 case XFS_DINODE_FMT_BTREE:
2995 if ((iip->ili_format.ilf_fields & brootflag[whichfork]) &&
2996 (ifp->if_broot_bytes > 0)) {
2997 ASSERT(ifp->if_broot != NULL);
2998 ASSERT(ifp->if_broot_bytes <=
2999 (XFS_IFORK_SIZE(ip, whichfork) +
3000 XFS_BROOT_SIZE_ADJ));
3001 xfs_bmbt_to_bmdr(ifp->if_broot, ifp->if_broot_bytes,
3002 (xfs_bmdr_block_t *)cp,
3003 XFS_DFORK_SIZE(dip, mp, whichfork));
3007 case XFS_DINODE_FMT_DEV:
3008 if (iip->ili_format.ilf_fields & XFS_ILOG_DEV) {
3009 ASSERT(whichfork == XFS_DATA_FORK);
3010 dip->di_u.di_dev = cpu_to_be32(ip->i_df.if_u2.if_rdev);
3014 case XFS_DINODE_FMT_UUID:
3015 if (iip->ili_format.ilf_fields & XFS_ILOG_UUID) {
3016 ASSERT(whichfork == XFS_DATA_FORK);
3017 memcpy(&dip->di_u.di_muuid, &ip->i_df.if_u2.if_uuid,
3031 * xfs_iflush() will write a modified inode's changes out to the
3032 * inode's on disk home. The caller must have the inode lock held
3033 * in at least shared mode and the inode flush semaphore must be
3034 * held as well. The inode lock will still be held upon return from
3035 * the call and the caller is free to unlock it.
3036 * The inode flush lock will be unlocked when the inode reaches the disk.
3037 * The flags indicate how the inode's buffer should be written out.
3044 xfs_inode_log_item_t *iip;
3051 int clcount; /* count of inodes clustered */
3053 struct hlist_node *entry;
3054 enum { INT_DELWRI = (1 << 0), INT_ASYNC = (1 << 1) };
3056 XFS_STATS_INC(xs_iflush_count);
3058 ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE|MR_ACCESS));
3059 ASSERT(issemalocked(&(ip->i_flock)));
3060 ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
3061 ip->i_d.di_nextents > ip->i_df.if_ext_max);
3067 * If the inode isn't dirty, then just release the inode
3068 * flush lock and do nothing.
3070 if ((ip->i_update_core == 0) &&
3071 ((iip == NULL) || !(iip->ili_format.ilf_fields & XFS_ILOG_ALL))) {
3072 ASSERT((iip != NULL) ?
3073 !(iip->ili_item.li_flags & XFS_LI_IN_AIL) : 1);
3079 * We can't flush the inode until it is unpinned, so
3080 * wait for it. We know noone new can pin it, because
3081 * we are holding the inode lock shared and you need
3082 * to hold it exclusively to pin the inode.
3084 xfs_iunpin_wait(ip);
3087 * This may have been unpinned because the filesystem is shutting
3088 * down forcibly. If that's the case we must not write this inode
3089 * to disk, because the log record didn't make it to disk!
3091 if (XFS_FORCED_SHUTDOWN(mp)) {
3092 ip->i_update_core = 0;
3094 iip->ili_format.ilf_fields = 0;
3096 return XFS_ERROR(EIO);
3100 * Get the buffer containing the on-disk inode.
3102 error = xfs_itobp(mp, NULL, ip, &dip, &bp, 0, 0);
3109 * Decide how buffer will be flushed out. This is done before
3110 * the call to xfs_iflush_int because this field is zeroed by it.
3112 if (iip != NULL && iip->ili_format.ilf_fields != 0) {
3114 * Flush out the inode buffer according to the directions
3115 * of the caller. In the cases where the caller has given
3116 * us a choice choose the non-delwri case. This is because
3117 * the inode is in the AIL and we need to get it out soon.
3120 case XFS_IFLUSH_SYNC:
3121 case XFS_IFLUSH_DELWRI_ELSE_SYNC:
3124 case XFS_IFLUSH_ASYNC:
3125 case XFS_IFLUSH_DELWRI_ELSE_ASYNC:
3128 case XFS_IFLUSH_DELWRI:
3138 case XFS_IFLUSH_DELWRI_ELSE_SYNC:
3139 case XFS_IFLUSH_DELWRI_ELSE_ASYNC:
3140 case XFS_IFLUSH_DELWRI:
3143 case XFS_IFLUSH_ASYNC:
3146 case XFS_IFLUSH_SYNC:
3157 * First flush out the inode that xfs_iflush was called with.
3159 error = xfs_iflush_int(ip, bp);
3166 * see if other inodes can be gathered into this write
3168 spin_lock(&ip->i_cluster->icl_lock);
3169 ip->i_cluster->icl_buf = bp;
3172 hlist_for_each_entry(iq, entry, &ip->i_cluster->icl_inodes, i_cnode) {
3177 * Do an un-protected check to see if the inode is dirty and
3178 * is a candidate for flushing. These checks will be repeated
3179 * later after the appropriate locks are acquired.
3182 if ((iq->i_update_core == 0) &&
3184 !(iip->ili_format.ilf_fields & XFS_ILOG_ALL)) &&
3185 xfs_ipincount(iq) == 0) {
3190 * Try to get locks. If any are unavailable,
3191 * then this inode cannot be flushed and is skipped.
3194 /* get inode locks (just i_lock) */
3195 if (xfs_ilock_nowait(iq, XFS_ILOCK_SHARED)) {
3196 /* get inode flush lock */
3197 if (xfs_iflock_nowait(iq)) {
3198 /* check if pinned */
3199 if (xfs_ipincount(iq) == 0) {
3200 /* arriving here means that
3201 * this inode can be flushed.
3202 * first re-check that it's
3206 if ((iq->i_update_core != 0)||
3208 (iip->ili_format.ilf_fields & XFS_ILOG_ALL))) {
3210 error = xfs_iflush_int(iq, bp);
3214 goto cluster_corrupt_out;
3223 xfs_iunlock(iq, XFS_ILOCK_SHARED);
3226 spin_unlock(&ip->i_cluster->icl_lock);
3229 XFS_STATS_INC(xs_icluster_flushcnt);
3230 XFS_STATS_ADD(xs_icluster_flushinode, clcount);
3234 * If the buffer is pinned then push on the log so we won't
3235 * get stuck waiting in the write for too long.
3237 if (XFS_BUF_ISPINNED(bp)){
3238 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE);
3241 if (flags & INT_DELWRI) {
3242 xfs_bdwrite(mp, bp);
3243 } else if (flags & INT_ASYNC) {
3244 xfs_bawrite(mp, bp);
3246 error = xfs_bwrite(mp, bp);
3252 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
3253 xfs_iflush_abort(ip);
3255 * Unlocks the flush lock
3257 return XFS_ERROR(EFSCORRUPTED);
3259 cluster_corrupt_out:
3260 /* Corruption detected in the clustering loop. Invalidate the
3261 * inode buffer and shut down the filesystem.
3263 spin_unlock(&ip->i_cluster->icl_lock);
3266 * Clean up the buffer. If it was B_DELWRI, just release it --
3267 * brelse can handle it with no problems. If not, shut down the
3268 * filesystem before releasing the buffer.
3270 if ((bufwasdelwri= XFS_BUF_ISDELAYWRITE(bp))) {
3274 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
3278 * Just like incore_relse: if we have b_iodone functions,
3279 * mark the buffer as an error and call them. Otherwise
3280 * mark it as stale and brelse.
3282 if (XFS_BUF_IODONE_FUNC(bp)) {
3283 XFS_BUF_CLR_BDSTRAT_FUNC(bp);
3287 XFS_BUF_ERROR(bp,EIO);
3295 xfs_iflush_abort(iq);
3297 * Unlocks the flush lock
3299 return XFS_ERROR(EFSCORRUPTED);
3308 xfs_inode_log_item_t *iip;
3311 #ifdef XFS_TRANS_DEBUG
3315 ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE|MR_ACCESS));
3316 ASSERT(issemalocked(&(ip->i_flock)));
3317 ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
3318 ip->i_d.di_nextents > ip->i_df.if_ext_max);
3325 * If the inode isn't dirty, then just release the inode
3326 * flush lock and do nothing.
3328 if ((ip->i_update_core == 0) &&
3329 ((iip == NULL) || !(iip->ili_format.ilf_fields & XFS_ILOG_ALL))) {
3334 /* set *dip = inode's place in the buffer */
3335 dip = (xfs_dinode_t *)xfs_buf_offset(bp, ip->i_boffset);
3338 * Clear i_update_core before copying out the data.
3339 * This is for coordination with our timestamp updates
3340 * that don't hold the inode lock. They will always
3341 * update the timestamps BEFORE setting i_update_core,
3342 * so if we clear i_update_core after they set it we
3343 * are guaranteed to see their updates to the timestamps.
3344 * I believe that this depends on strongly ordered memory
3345 * semantics, but we have that. We use the SYNCHRONIZE
3346 * macro to make sure that the compiler does not reorder
3347 * the i_update_core access below the data copy below.
3349 ip->i_update_core = 0;
3353 * Make sure to get the latest atime from the Linux inode.
3355 xfs_synchronize_atime(ip);
3357 if (XFS_TEST_ERROR(be16_to_cpu(dip->di_core.di_magic) != XFS_DINODE_MAGIC,
3358 mp, XFS_ERRTAG_IFLUSH_1, XFS_RANDOM_IFLUSH_1)) {
3359 xfs_cmn_err(XFS_PTAG_IFLUSH, CE_ALERT, mp,
3360 "xfs_iflush: Bad inode %Lu magic number 0x%x, ptr 0x%p",
3361 ip->i_ino, be16_to_cpu(dip->di_core.di_magic), dip);
3364 if (XFS_TEST_ERROR(ip->i_d.di_magic != XFS_DINODE_MAGIC,
3365 mp, XFS_ERRTAG_IFLUSH_2, XFS_RANDOM_IFLUSH_2)) {
3366 xfs_cmn_err(XFS_PTAG_IFLUSH, CE_ALERT, mp,
3367 "xfs_iflush: Bad inode %Lu, ptr 0x%p, magic number 0x%x",
3368 ip->i_ino, ip, ip->i_d.di_magic);
3371 if ((ip->i_d.di_mode & S_IFMT) == S_IFREG) {
3373 (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
3374 (ip->i_d.di_format != XFS_DINODE_FMT_BTREE),
3375 mp, XFS_ERRTAG_IFLUSH_3, XFS_RANDOM_IFLUSH_3)) {
3376 xfs_cmn_err(XFS_PTAG_IFLUSH, CE_ALERT, mp,
3377 "xfs_iflush: Bad regular inode %Lu, ptr 0x%p",
3381 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
3383 (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
3384 (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
3385 (ip->i_d.di_format != XFS_DINODE_FMT_LOCAL),
3386 mp, XFS_ERRTAG_IFLUSH_4, XFS_RANDOM_IFLUSH_4)) {
3387 xfs_cmn_err(XFS_PTAG_IFLUSH, CE_ALERT, mp,
3388 "xfs_iflush: Bad directory inode %Lu, ptr 0x%p",
3393 if (XFS_TEST_ERROR(ip->i_d.di_nextents + ip->i_d.di_anextents >
3394 ip->i_d.di_nblocks, mp, XFS_ERRTAG_IFLUSH_5,
3395 XFS_RANDOM_IFLUSH_5)) {
3396 xfs_cmn_err(XFS_PTAG_IFLUSH, CE_ALERT, mp,
3397 "xfs_iflush: detected corrupt incore inode %Lu, total extents = %d, nblocks = %Ld, ptr 0x%p",
3399 ip->i_d.di_nextents + ip->i_d.di_anextents,
3404 if (XFS_TEST_ERROR(ip->i_d.di_forkoff > mp->m_sb.sb_inodesize,
3405 mp, XFS_ERRTAG_IFLUSH_6, XFS_RANDOM_IFLUSH_6)) {
3406 xfs_cmn_err(XFS_PTAG_IFLUSH, CE_ALERT, mp,
3407 "xfs_iflush: bad inode %Lu, forkoff 0x%x, ptr 0x%p",
3408 ip->i_ino, ip->i_d.di_forkoff, ip);
3412 * bump the flush iteration count, used to detect flushes which
3413 * postdate a log record during recovery.
3416 ip->i_d.di_flushiter++;
3419 * Copy the dirty parts of the inode into the on-disk
3420 * inode. We always copy out the core of the inode,
3421 * because if the inode is dirty at all the core must
3424 xfs_dinode_to_disk(&dip->di_core, &ip->i_d);
3426 /* Wrap, we never let the log put out DI_MAX_FLUSH */
3427 if (ip->i_d.di_flushiter == DI_MAX_FLUSH)
3428 ip->i_d.di_flushiter = 0;
3431 * If this is really an old format inode and the superblock version
3432 * has not been updated to support only new format inodes, then
3433 * convert back to the old inode format. If the superblock version
3434 * has been updated, then make the conversion permanent.
3436 ASSERT(ip->i_d.di_version == XFS_DINODE_VERSION_1 ||
3437 XFS_SB_VERSION_HASNLINK(&mp->m_sb));
3438 if (ip->i_d.di_version == XFS_DINODE_VERSION_1) {
3439 if (!XFS_SB_VERSION_HASNLINK(&mp->m_sb)) {
3443 ASSERT(ip->i_d.di_nlink <= XFS_MAXLINK_1);
3444 dip->di_core.di_onlink = cpu_to_be16(ip->i_d.di_nlink);
3447 * The superblock version has already been bumped,
3448 * so just make the conversion to the new inode
3451 ip->i_d.di_version = XFS_DINODE_VERSION_2;
3452 dip->di_core.di_version = XFS_DINODE_VERSION_2;
3453 ip->i_d.di_onlink = 0;
3454 dip->di_core.di_onlink = 0;
3455 memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
3456 memset(&(dip->di_core.di_pad[0]), 0,
3457 sizeof(dip->di_core.di_pad));
3458 ASSERT(ip->i_d.di_projid == 0);
3462 if (xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK, bp) == EFSCORRUPTED) {
3466 if (XFS_IFORK_Q(ip)) {
3468 * The only error from xfs_iflush_fork is on the data fork.
3470 (void) xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK, bp);
3472 xfs_inobp_check(mp, bp);
3475 * We've recorded everything logged in the inode, so we'd
3476 * like to clear the ilf_fields bits so we don't log and
3477 * flush things unnecessarily. However, we can't stop
3478 * logging all this information until the data we've copied
3479 * into the disk buffer is written to disk. If we did we might
3480 * overwrite the copy of the inode in the log with all the
3481 * data after re-logging only part of it, and in the face of
3482 * a crash we wouldn't have all the data we need to recover.
3484 * What we do is move the bits to the ili_last_fields field.
3485 * When logging the inode, these bits are moved back to the
3486 * ilf_fields field. In the xfs_iflush_done() routine we
3487 * clear ili_last_fields, since we know that the information
3488 * those bits represent is permanently on disk. As long as
3489 * the flush completes before the inode is logged again, then
3490 * both ilf_fields and ili_last_fields will be cleared.
3492 * We can play with the ilf_fields bits here, because the inode
3493 * lock must be held exclusively in order to set bits there
3494 * and the flush lock protects the ili_last_fields bits.
3495 * Set ili_logged so the flush done
3496 * routine can tell whether or not to look in the AIL.
3497 * Also, store the current LSN of the inode so that we can tell
3498 * whether the item has moved in the AIL from xfs_iflush_done().
3499 * In order to read the lsn we need the AIL lock, because
3500 * it is a 64 bit value that cannot be read atomically.
3502 if (iip != NULL && iip->ili_format.ilf_fields != 0) {
3503 iip->ili_last_fields = iip->ili_format.ilf_fields;
3504 iip->ili_format.ilf_fields = 0;
3505 iip->ili_logged = 1;
3507 ASSERT(sizeof(xfs_lsn_t) == 8); /* don't lock if it shrinks */
3508 spin_lock(&mp->m_ail_lock);
3509 iip->ili_flush_lsn = iip->ili_item.li_lsn;
3510 spin_unlock(&mp->m_ail_lock);
3513 * Attach the function xfs_iflush_done to the inode's
3514 * buffer. This will remove the inode from the AIL
3515 * and unlock the inode's flush lock when the inode is
3516 * completely written to disk.
3518 xfs_buf_attach_iodone(bp, (void(*)(xfs_buf_t*,xfs_log_item_t*))
3519 xfs_iflush_done, (xfs_log_item_t *)iip);
3521 ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
3522 ASSERT(XFS_BUF_IODONE_FUNC(bp) != NULL);
3525 * We're flushing an inode which is not in the AIL and has
3526 * not been logged but has i_update_core set. For this
3527 * case we can use a B_DELWRI flush and immediately drop
3528 * the inode flush lock because we can avoid the whole
3529 * AIL state thing. It's OK to drop the flush lock now,
3530 * because we've already locked the buffer and to do anything
3531 * you really need both.
3534 ASSERT(iip->ili_logged == 0);
3535 ASSERT(iip->ili_last_fields == 0);
3536 ASSERT((iip->ili_item.li_flags & XFS_LI_IN_AIL) == 0);
3544 return XFS_ERROR(EFSCORRUPTED);
3549 * Flush all inactive inodes in mp.
3559 XFS_MOUNT_ILOCK(mp);
3565 /* Make sure we skip markers inserted by sync */
3566 if (ip->i_mount == NULL) {
3571 vp = XFS_ITOV_NULL(ip);
3573 XFS_MOUNT_IUNLOCK(mp);
3574 xfs_finish_reclaim(ip, 0, XFS_IFLUSH_ASYNC);
3578 ASSERT(vn_count(vp) == 0);
3581 } while (ip != mp->m_inodes);
3583 XFS_MOUNT_IUNLOCK(mp);
3586 #ifdef XFS_ILOCK_TRACE
3587 ktrace_t *xfs_ilock_trace_buf;
3590 xfs_ilock_trace(xfs_inode_t *ip, int lock, unsigned int lockflags, inst_t *ra)
3592 ktrace_enter(ip->i_lock_trace,
3594 (void *)(unsigned long)lock, /* 1 = LOCK, 3=UNLOCK, etc */
3595 (void *)(unsigned long)lockflags, /* XFS_ILOCK_EXCL etc */
3596 (void *)ra, /* caller of ilock */
3597 (void *)(unsigned long)current_cpu(),
3598 (void *)(unsigned long)current_pid(),
3599 NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);
3604 * Return a pointer to the extent record at file index idx.
3606 xfs_bmbt_rec_host_t *
3608 xfs_ifork_t *ifp, /* inode fork pointer */
3609 xfs_extnum_t idx) /* index of target extent */
3612 if ((ifp->if_flags & XFS_IFEXTIREC) && (idx == 0)) {
3613 return ifp->if_u1.if_ext_irec->er_extbuf;
3614 } else if (ifp->if_flags & XFS_IFEXTIREC) {
3615 xfs_ext_irec_t *erp; /* irec pointer */
3616 int erp_idx = 0; /* irec index */
3617 xfs_extnum_t page_idx = idx; /* ext index in target list */
3619 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
3620 return &erp->er_extbuf[page_idx];
3621 } else if (ifp->if_bytes) {
3622 return &ifp->if_u1.if_extents[idx];
3629 * Insert new item(s) into the extent records for incore inode
3630 * fork 'ifp'. 'count' new items are inserted at index 'idx'.
3634 xfs_ifork_t *ifp, /* inode fork pointer */
3635 xfs_extnum_t idx, /* starting index of new items */
3636 xfs_extnum_t count, /* number of inserted items */
3637 xfs_bmbt_irec_t *new) /* items to insert */
3639 xfs_extnum_t i; /* extent record index */
3641 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
3642 xfs_iext_add(ifp, idx, count);
3643 for (i = idx; i < idx + count; i++, new++)
3644 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, i), new);
3648 * This is called when the amount of space required for incore file
3649 * extents needs to be increased. The ext_diff parameter stores the
3650 * number of new extents being added and the idx parameter contains
3651 * the extent index where the new extents will be added. If the new
3652 * extents are being appended, then we just need to (re)allocate and
3653 * initialize the space. Otherwise, if the new extents are being
3654 * inserted into the middle of the existing entries, a bit more work
3655 * is required to make room for the new extents to be inserted. The
3656 * caller is responsible for filling in the new extent entries upon
3661 xfs_ifork_t *ifp, /* inode fork pointer */
3662 xfs_extnum_t idx, /* index to begin adding exts */
3663 int ext_diff) /* number of extents to add */
3665 int byte_diff; /* new bytes being added */
3666 int new_size; /* size of extents after adding */
3667 xfs_extnum_t nextents; /* number of extents in file */
3669 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3670 ASSERT((idx >= 0) && (idx <= nextents));
3671 byte_diff = ext_diff * sizeof(xfs_bmbt_rec_t);
3672 new_size = ifp->if_bytes + byte_diff;
3674 * If the new number of extents (nextents + ext_diff)
3675 * fits inside the inode, then continue to use the inline
3678 if (nextents + ext_diff <= XFS_INLINE_EXTS) {
3679 if (idx < nextents) {
3680 memmove(&ifp->if_u2.if_inline_ext[idx + ext_diff],
3681 &ifp->if_u2.if_inline_ext[idx],
3682 (nextents - idx) * sizeof(xfs_bmbt_rec_t));
3683 memset(&ifp->if_u2.if_inline_ext[idx], 0, byte_diff);
3685 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
3686 ifp->if_real_bytes = 0;
3687 ifp->if_lastex = nextents + ext_diff;
3690 * Otherwise use a linear (direct) extent list.
3691 * If the extents are currently inside the inode,
3692 * xfs_iext_realloc_direct will switch us from
3693 * inline to direct extent allocation mode.
3695 else if (nextents + ext_diff <= XFS_LINEAR_EXTS) {
3696 xfs_iext_realloc_direct(ifp, new_size);
3697 if (idx < nextents) {
3698 memmove(&ifp->if_u1.if_extents[idx + ext_diff],
3699 &ifp->if_u1.if_extents[idx],
3700 (nextents - idx) * sizeof(xfs_bmbt_rec_t));
3701 memset(&ifp->if_u1.if_extents[idx], 0, byte_diff);
3704 /* Indirection array */
3706 xfs_ext_irec_t *erp;
3710 ASSERT(nextents + ext_diff > XFS_LINEAR_EXTS);
3711 if (ifp->if_flags & XFS_IFEXTIREC) {
3712 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 1);
3714 xfs_iext_irec_init(ifp);
3715 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3716 erp = ifp->if_u1.if_ext_irec;
3718 /* Extents fit in target extent page */
3719 if (erp && erp->er_extcount + ext_diff <= XFS_LINEAR_EXTS) {
3720 if (page_idx < erp->er_extcount) {
3721 memmove(&erp->er_extbuf[page_idx + ext_diff],
3722 &erp->er_extbuf[page_idx],
3723 (erp->er_extcount - page_idx) *
3724 sizeof(xfs_bmbt_rec_t));
3725 memset(&erp->er_extbuf[page_idx], 0, byte_diff);
3727 erp->er_extcount += ext_diff;
3728 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
3730 /* Insert a new extent page */
3732 xfs_iext_add_indirect_multi(ifp,
3733 erp_idx, page_idx, ext_diff);
3736 * If extent(s) are being appended to the last page in
3737 * the indirection array and the new extent(s) don't fit
3738 * in the page, then erp is NULL and erp_idx is set to
3739 * the next index needed in the indirection array.
3742 int count = ext_diff;
3745 erp = xfs_iext_irec_new(ifp, erp_idx);
3746 erp->er_extcount = count;
3747 count -= MIN(count, (int)XFS_LINEAR_EXTS);
3754 ifp->if_bytes = new_size;
3758 * This is called when incore extents are being added to the indirection
3759 * array and the new extents do not fit in the target extent list. The
3760 * erp_idx parameter contains the irec index for the target extent list
3761 * in the indirection array, and the idx parameter contains the extent
3762 * index within the list. The number of extents being added is stored
3763 * in the count parameter.
3765 * |-------| |-------|
3766 * | | | | idx - number of extents before idx
3768 * | | | | count - number of extents being inserted at idx
3769 * |-------| |-------|
3770 * | count | | nex2 | nex2 - number of extents after idx + count
3771 * |-------| |-------|
3774 xfs_iext_add_indirect_multi(
3775 xfs_ifork_t *ifp, /* inode fork pointer */
3776 int erp_idx, /* target extent irec index */
3777 xfs_extnum_t idx, /* index within target list */
3778 int count) /* new extents being added */
3780 int byte_diff; /* new bytes being added */
3781 xfs_ext_irec_t *erp; /* pointer to irec entry */
3782 xfs_extnum_t ext_diff; /* number of extents to add */
3783 xfs_extnum_t ext_cnt; /* new extents still needed */
3784 xfs_extnum_t nex2; /* extents after idx + count */
3785 xfs_bmbt_rec_t *nex2_ep = NULL; /* temp list for nex2 extents */
3786 int nlists; /* number of irec's (lists) */
3788 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3789 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3790 nex2 = erp->er_extcount - idx;
3791 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3794 * Save second part of target extent list
3795 * (all extents past */
3797 byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
3798 nex2_ep = (xfs_bmbt_rec_t *) kmem_alloc(byte_diff, KM_SLEEP);
3799 memmove(nex2_ep, &erp->er_extbuf[idx], byte_diff);
3800 erp->er_extcount -= nex2;
3801 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -nex2);
3802 memset(&erp->er_extbuf[idx], 0, byte_diff);
3806 * Add the new extents to the end of the target
3807 * list, then allocate new irec record(s) and
3808 * extent buffer(s) as needed to store the rest
3809 * of the new extents.
3812 ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS - erp->er_extcount);
3814 erp->er_extcount += ext_diff;
3815 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
3816 ext_cnt -= ext_diff;
3820 erp = xfs_iext_irec_new(ifp, erp_idx);
3821 ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS);
3822 erp->er_extcount = ext_diff;
3823 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
3824 ext_cnt -= ext_diff;
3827 /* Add nex2 extents back to indirection array */
3829 xfs_extnum_t ext_avail;
3832 byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
3833 ext_avail = XFS_LINEAR_EXTS - erp->er_extcount;
3836 * If nex2 extents fit in the current page, append
3837 * nex2_ep after the new extents.
3839 if (nex2 <= ext_avail) {
3840 i = erp->er_extcount;
3843 * Otherwise, check if space is available in the
3846 else if ((erp_idx < nlists - 1) &&
3847 (nex2 <= (ext_avail = XFS_LINEAR_EXTS -
3848 ifp->if_u1.if_ext_irec[erp_idx+1].er_extcount))) {
3851 /* Create a hole for nex2 extents */
3852 memmove(&erp->er_extbuf[nex2], erp->er_extbuf,
3853 erp->er_extcount * sizeof(xfs_bmbt_rec_t));
3856 * Final choice, create a new extent page for
3861 erp = xfs_iext_irec_new(ifp, erp_idx);
3863 memmove(&erp->er_extbuf[i], nex2_ep, byte_diff);
3864 kmem_free(nex2_ep, byte_diff);
3865 erp->er_extcount += nex2;
3866 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, nex2);
3871 * This is called when the amount of space required for incore file
3872 * extents needs to be decreased. The ext_diff parameter stores the
3873 * number of extents to be removed and the idx parameter contains
3874 * the extent index where the extents will be removed from.
3876 * If the amount of space needed has decreased below the linear
3877 * limit, XFS_IEXT_BUFSZ, then switch to using the contiguous
3878 * extent array. Otherwise, use kmem_realloc() to adjust the
3879 * size to what is needed.
3883 xfs_ifork_t *ifp, /* inode fork pointer */
3884 xfs_extnum_t idx, /* index to begin removing exts */
3885 int ext_diff) /* number of extents to remove */
3887 xfs_extnum_t nextents; /* number of extents in file */
3888 int new_size; /* size of extents after removal */
3890 ASSERT(ext_diff > 0);
3891 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3892 new_size = (nextents - ext_diff) * sizeof(xfs_bmbt_rec_t);
3894 if (new_size == 0) {
3895 xfs_iext_destroy(ifp);
3896 } else if (ifp->if_flags & XFS_IFEXTIREC) {
3897 xfs_iext_remove_indirect(ifp, idx, ext_diff);
3898 } else if (ifp->if_real_bytes) {
3899 xfs_iext_remove_direct(ifp, idx, ext_diff);
3901 xfs_iext_remove_inline(ifp, idx, ext_diff);
3903 ifp->if_bytes = new_size;
3907 * This removes ext_diff extents from the inline buffer, beginning
3908 * at extent index idx.
3911 xfs_iext_remove_inline(
3912 xfs_ifork_t *ifp, /* inode fork pointer */
3913 xfs_extnum_t idx, /* index to begin removing exts */
3914 int ext_diff) /* number of extents to remove */
3916 int nextents; /* number of extents in file */
3918 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
3919 ASSERT(idx < XFS_INLINE_EXTS);
3920 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3921 ASSERT(((nextents - ext_diff) > 0) &&
3922 (nextents - ext_diff) < XFS_INLINE_EXTS);
3924 if (idx + ext_diff < nextents) {
3925 memmove(&ifp->if_u2.if_inline_ext[idx],
3926 &ifp->if_u2.if_inline_ext[idx + ext_diff],
3927 (nextents - (idx + ext_diff)) *
3928 sizeof(xfs_bmbt_rec_t));
3929 memset(&ifp->if_u2.if_inline_ext[nextents - ext_diff],
3930 0, ext_diff * sizeof(xfs_bmbt_rec_t));
3932 memset(&ifp->if_u2.if_inline_ext[idx], 0,
3933 ext_diff * sizeof(xfs_bmbt_rec_t));
3938 * This removes ext_diff extents from a linear (direct) extent list,
3939 * beginning at extent index idx. If the extents are being removed
3940 * from the end of the list (ie. truncate) then we just need to re-
3941 * allocate the list to remove the extra space. Otherwise, if the
3942 * extents are being removed from the middle of the existing extent
3943 * entries, then we first need to move the extent records beginning
3944 * at idx + ext_diff up in the list to overwrite the records being
3945 * removed, then remove the extra space via kmem_realloc.
3948 xfs_iext_remove_direct(
3949 xfs_ifork_t *ifp, /* inode fork pointer */
3950 xfs_extnum_t idx, /* index to begin removing exts */
3951 int ext_diff) /* number of extents to remove */
3953 xfs_extnum_t nextents; /* number of extents in file */
3954 int new_size; /* size of extents after removal */
3956 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
3957 new_size = ifp->if_bytes -
3958 (ext_diff * sizeof(xfs_bmbt_rec_t));
3959 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3961 if (new_size == 0) {
3962 xfs_iext_destroy(ifp);
3965 /* Move extents up in the list (if needed) */
3966 if (idx + ext_diff < nextents) {
3967 memmove(&ifp->if_u1.if_extents[idx],
3968 &ifp->if_u1.if_extents[idx + ext_diff],
3969 (nextents - (idx + ext_diff)) *
3970 sizeof(xfs_bmbt_rec_t));
3972 memset(&ifp->if_u1.if_extents[nextents - ext_diff],
3973 0, ext_diff * sizeof(xfs_bmbt_rec_t));
3975 * Reallocate the direct extent list. If the extents
3976 * will fit inside the inode then xfs_iext_realloc_direct
3977 * will switch from direct to inline extent allocation
3980 xfs_iext_realloc_direct(ifp, new_size);
3981 ifp->if_bytes = new_size;
3985 * This is called when incore extents are being removed from the
3986 * indirection array and the extents being removed span multiple extent
3987 * buffers. The idx parameter contains the file extent index where we
3988 * want to begin removing extents, and the count parameter contains
3989 * how many extents need to be removed.
3991 * |-------| |-------|
3992 * | nex1 | | | nex1 - number of extents before idx
3993 * |-------| | count |
3994 * | | | | count - number of extents being removed at idx
3995 * | count | |-------|
3996 * | | | nex2 | nex2 - number of extents after idx + count
3997 * |-------| |-------|
4000 xfs_iext_remove_indirect(
4001 xfs_ifork_t *ifp, /* inode fork pointer */
4002 xfs_extnum_t idx, /* index to begin removing extents */
4003 int count) /* number of extents to remove */
4005 xfs_ext_irec_t *erp; /* indirection array pointer */
4006 int erp_idx = 0; /* indirection array index */
4007 xfs_extnum_t ext_cnt; /* extents left to remove */
4008 xfs_extnum_t ext_diff; /* extents to remove in current list */
4009 xfs_extnum_t nex1; /* number of extents before idx */
4010 xfs_extnum_t nex2; /* extents after idx + count */
4011 int nlists; /* entries in indirection array */
4012 int page_idx = idx; /* index in target extent list */
4014 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
4015 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
4016 ASSERT(erp != NULL);
4017 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
4021 nex2 = MAX((erp->er_extcount - (nex1 + ext_cnt)), 0);
4022 ext_diff = MIN(ext_cnt, (erp->er_extcount - nex1));
4024 * Check for deletion of entire list;
4025 * xfs_iext_irec_remove() updates extent offsets.
4027 if (ext_diff == erp->er_extcount) {
4028 xfs_iext_irec_remove(ifp, erp_idx);
4029 ext_cnt -= ext_diff;
4032 ASSERT(erp_idx < ifp->if_real_bytes /
4034 erp = &ifp->if_u1.if_ext_irec[erp_idx];
4041 /* Move extents up (if needed) */
4043 memmove(&erp->er_extbuf[nex1],
4044 &erp->er_extbuf[nex1 + ext_diff],
4045 nex2 * sizeof(xfs_bmbt_rec_t));
4047 /* Zero out rest of page */
4048 memset(&erp->er_extbuf[nex1 + nex2], 0, (XFS_IEXT_BUFSZ -
4049 ((nex1 + nex2) * sizeof(xfs_bmbt_rec_t))));
4050 /* Update remaining counters */
4051 erp->er_extcount -= ext_diff;
4052 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -ext_diff);
4053 ext_cnt -= ext_diff;
4058 ifp->if_bytes -= count * sizeof(xfs_bmbt_rec_t);
4059 xfs_iext_irec_compact(ifp);
4063 * Create, destroy, or resize a linear (direct) block of extents.
4066 xfs_iext_realloc_direct(
4067 xfs_ifork_t *ifp, /* inode fork pointer */
4068 int new_size) /* new size of extents */
4070 int rnew_size; /* real new size of extents */
4072 rnew_size = new_size;
4074 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC) ||
4075 ((new_size >= 0) && (new_size <= XFS_IEXT_BUFSZ) &&
4076 (new_size != ifp->if_real_bytes)));
4078 /* Free extent records */
4079 if (new_size == 0) {
4080 xfs_iext_destroy(ifp);
4082 /* Resize direct extent list and zero any new bytes */
4083 else if (ifp->if_real_bytes) {
4084 /* Check if extents will fit inside the inode */
4085 if (new_size <= XFS_INLINE_EXTS * sizeof(xfs_bmbt_rec_t)) {
4086 xfs_iext_direct_to_inline(ifp, new_size /
4087 (uint)sizeof(xfs_bmbt_rec_t));
4088 ifp->if_bytes = new_size;
4091 if (!is_power_of_2(new_size)){
4092 rnew_size = roundup_pow_of_two(new_size);
4094 if (rnew_size != ifp->if_real_bytes) {
4095 ifp->if_u1.if_extents =
4096 kmem_realloc(ifp->if_u1.if_extents,
4101 if (rnew_size > ifp->if_real_bytes) {
4102 memset(&ifp->if_u1.if_extents[ifp->if_bytes /
4103 (uint)sizeof(xfs_bmbt_rec_t)], 0,
4104 rnew_size - ifp->if_real_bytes);
4108 * Switch from the inline extent buffer to a direct
4109 * extent list. Be sure to include the inline extent
4110 * bytes in new_size.
4113 new_size += ifp->if_bytes;
4114 if (!is_power_of_2(new_size)) {
4115 rnew_size = roundup_pow_of_two(new_size);
4117 xfs_iext_inline_to_direct(ifp, rnew_size);
4119 ifp->if_real_bytes = rnew_size;
4120 ifp->if_bytes = new_size;
4124 * Switch from linear (direct) extent records to inline buffer.
4127 xfs_iext_direct_to_inline(
4128 xfs_ifork_t *ifp, /* inode fork pointer */
4129 xfs_extnum_t nextents) /* number of extents in file */
4131 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
4132 ASSERT(nextents <= XFS_INLINE_EXTS);
4134 * The inline buffer was zeroed when we switched
4135 * from inline to direct extent allocation mode,
4136 * so we don't need to clear it here.
4138 memcpy(ifp->if_u2.if_inline_ext, ifp->if_u1.if_extents,
4139 nextents * sizeof(xfs_bmbt_rec_t));
4140 kmem_free(ifp->if_u1.if_extents, ifp->if_real_bytes);
4141 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
4142 ifp->if_real_bytes = 0;
4146 * Switch from inline buffer to linear (direct) extent records.
4147 * new_size should already be rounded up to the next power of 2
4148 * by the caller (when appropriate), so use new_size as it is.
4149 * However, since new_size may be rounded up, we can't update
4150 * if_bytes here. It is the caller's responsibility to update
4151 * if_bytes upon return.
4154 xfs_iext_inline_to_direct(
4155 xfs_ifork_t *ifp, /* inode fork pointer */
4156 int new_size) /* number of extents in file */
4158 ifp->if_u1.if_extents = kmem_alloc(new_size, KM_SLEEP);
4159 memset(ifp->if_u1.if_extents, 0, new_size);
4160 if (ifp->if_bytes) {
4161 memcpy(ifp->if_u1.if_extents, ifp->if_u2.if_inline_ext,
4163 memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
4164 sizeof(xfs_bmbt_rec_t));
4166 ifp->if_real_bytes = new_size;
4170 * Resize an extent indirection array to new_size bytes.
4173 xfs_iext_realloc_indirect(
4174 xfs_ifork_t *ifp, /* inode fork pointer */
4175 int new_size) /* new indirection array size */
4177 int nlists; /* number of irec's (ex lists) */
4178 int size; /* current indirection array size */
4180 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
4181 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
4182 size = nlists * sizeof(xfs_ext_irec_t);
4183 ASSERT(ifp->if_real_bytes);
4184 ASSERT((new_size >= 0) && (new_size != size));
4185 if (new_size == 0) {
4186 xfs_iext_destroy(ifp);
4188 ifp->if_u1.if_ext_irec = (xfs_ext_irec_t *)
4189 kmem_realloc(ifp->if_u1.if_ext_irec,
4190 new_size, size, KM_SLEEP);
4195 * Switch from indirection array to linear (direct) extent allocations.
4198 xfs_iext_indirect_to_direct(
4199 xfs_ifork_t *ifp) /* inode fork pointer */
4201 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
4202 xfs_extnum_t nextents; /* number of extents in file */
4203 int size; /* size of file extents */
4205 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
4206 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4207 ASSERT(nextents <= XFS_LINEAR_EXTS);
4208 size = nextents * sizeof(xfs_bmbt_rec_t);
4210 xfs_iext_irec_compact_full(ifp);
4211 ASSERT(ifp->if_real_bytes == XFS_IEXT_BUFSZ);
4213 ep = ifp->if_u1.if_ext_irec->er_extbuf;
4214 kmem_free(ifp->if_u1.if_ext_irec, sizeof(xfs_ext_irec_t));
4215 ifp->if_flags &= ~XFS_IFEXTIREC;
4216 ifp->if_u1.if_extents = ep;
4217 ifp->if_bytes = size;
4218 if (nextents < XFS_LINEAR_EXTS) {
4219 xfs_iext_realloc_direct(ifp, size);
4224 * Free incore file extents.
4228 xfs_ifork_t *ifp) /* inode fork pointer */
4230 if (ifp->if_flags & XFS_IFEXTIREC) {
4234 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
4235 for (erp_idx = nlists - 1; erp_idx >= 0 ; erp_idx--) {
4236 xfs_iext_irec_remove(ifp, erp_idx);
4238 ifp->if_flags &= ~XFS_IFEXTIREC;
4239 } else if (ifp->if_real_bytes) {
4240 kmem_free(ifp->if_u1.if_extents, ifp->if_real_bytes);
4241 } else if (ifp->if_bytes) {
4242 memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
4243 sizeof(xfs_bmbt_rec_t));
4245 ifp->if_u1.if_extents = NULL;
4246 ifp->if_real_bytes = 0;
4251 * Return a pointer to the extent record for file system block bno.
4253 xfs_bmbt_rec_host_t * /* pointer to found extent record */
4254 xfs_iext_bno_to_ext(
4255 xfs_ifork_t *ifp, /* inode fork pointer */
4256 xfs_fileoff_t bno, /* block number to search for */
4257 xfs_extnum_t *idxp) /* index of target extent */
4259 xfs_bmbt_rec_host_t *base; /* pointer to first extent */
4260 xfs_filblks_t blockcount = 0; /* number of blocks in extent */
4261 xfs_bmbt_rec_host_t *ep = NULL; /* pointer to target extent */
4262 xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
4263 int high; /* upper boundary in search */
4264 xfs_extnum_t idx = 0; /* index of target extent */
4265 int low; /* lower boundary in search */
4266 xfs_extnum_t nextents; /* number of file extents */
4267 xfs_fileoff_t startoff = 0; /* start offset of extent */
4269 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4270 if (nextents == 0) {
4275 if (ifp->if_flags & XFS_IFEXTIREC) {
4276 /* Find target extent list */
4278 erp = xfs_iext_bno_to_irec(ifp, bno, &erp_idx);
4279 base = erp->er_extbuf;
4280 high = erp->er_extcount - 1;
4282 base = ifp->if_u1.if_extents;
4283 high = nextents - 1;
4285 /* Binary search extent records */
4286 while (low <= high) {
4287 idx = (low + high) >> 1;
4289 startoff = xfs_bmbt_get_startoff(ep);
4290 blockcount = xfs_bmbt_get_blockcount(ep);
4291 if (bno < startoff) {
4293 } else if (bno >= startoff + blockcount) {
4296 /* Convert back to file-based extent index */
4297 if (ifp->if_flags & XFS_IFEXTIREC) {
4298 idx += erp->er_extoff;
4304 /* Convert back to file-based extent index */
4305 if (ifp->if_flags & XFS_IFEXTIREC) {
4306 idx += erp->er_extoff;
4308 if (bno >= startoff + blockcount) {
4309 if (++idx == nextents) {
4312 ep = xfs_iext_get_ext(ifp, idx);
4320 * Return a pointer to the indirection array entry containing the
4321 * extent record for filesystem block bno. Store the index of the
4322 * target irec in *erp_idxp.
4324 xfs_ext_irec_t * /* pointer to found extent record */
4325 xfs_iext_bno_to_irec(
4326 xfs_ifork_t *ifp, /* inode fork pointer */
4327 xfs_fileoff_t bno, /* block number to search for */
4328 int *erp_idxp) /* irec index of target ext list */
4330 xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
4331 xfs_ext_irec_t *erp_next; /* next indirection array entry */
4332 int erp_idx; /* indirection array index */
4333 int nlists; /* number of extent irec's (lists) */
4334 int high; /* binary search upper limit */
4335 int low; /* binary search lower limit */
4337 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
4338 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
4342 while (low <= high) {
4343 erp_idx = (low + high) >> 1;
4344 erp = &ifp->if_u1.if_ext_irec[erp_idx];
4345 erp_next = erp_idx < nlists - 1 ? erp + 1 : NULL;
4346 if (bno < xfs_bmbt_get_startoff(erp->er_extbuf)) {
4348 } else if (erp_next && bno >=
4349 xfs_bmbt_get_startoff(erp_next->er_extbuf)) {
4355 *erp_idxp = erp_idx;
4360 * Return a pointer to the indirection array entry containing the
4361 * extent record at file extent index *idxp. Store the index of the
4362 * target irec in *erp_idxp and store the page index of the target
4363 * extent record in *idxp.
4366 xfs_iext_idx_to_irec(
4367 xfs_ifork_t *ifp, /* inode fork pointer */
4368 xfs_extnum_t *idxp, /* extent index (file -> page) */
4369 int *erp_idxp, /* pointer to target irec */
4370 int realloc) /* new bytes were just added */
4372 xfs_ext_irec_t *prev; /* pointer to previous irec */
4373 xfs_ext_irec_t *erp = NULL; /* pointer to current irec */
4374 int erp_idx; /* indirection array index */
4375 int nlists; /* number of irec's (ex lists) */
4376 int high; /* binary search upper limit */
4377 int low; /* binary search lower limit */
4378 xfs_extnum_t page_idx = *idxp; /* extent index in target list */
4380 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
4381 ASSERT(page_idx >= 0 && page_idx <=
4382 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t));
4383 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
4388 /* Binary search extent irec's */
4389 while (low <= high) {
4390 erp_idx = (low + high) >> 1;
4391 erp = &ifp->if_u1.if_ext_irec[erp_idx];
4392 prev = erp_idx > 0 ? erp - 1 : NULL;
4393 if (page_idx < erp->er_extoff || (page_idx == erp->er_extoff &&
4394 realloc && prev && prev->er_extcount < XFS_LINEAR_EXTS)) {
4396 } else if (page_idx > erp->er_extoff + erp->er_extcount ||
4397 (page_idx == erp->er_extoff + erp->er_extcount &&
4400 } else if (page_idx == erp->er_extoff + erp->er_extcount &&
4401 erp->er_extcount == XFS_LINEAR_EXTS) {
4405 erp = erp_idx < nlists ? erp + 1 : NULL;
4408 page_idx -= erp->er_extoff;
4413 *erp_idxp = erp_idx;
4418 * Allocate and initialize an indirection array once the space needed
4419 * for incore extents increases above XFS_IEXT_BUFSZ.
4423 xfs_ifork_t *ifp) /* inode fork pointer */
4425 xfs_ext_irec_t *erp; /* indirection array pointer */
4426 xfs_extnum_t nextents; /* number of extents in file */
4428 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
4429 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4430 ASSERT(nextents <= XFS_LINEAR_EXTS);
4432 erp = (xfs_ext_irec_t *)
4433 kmem_alloc(sizeof(xfs_ext_irec_t), KM_SLEEP);
4435 if (nextents == 0) {
4436 ifp->if_u1.if_extents = kmem_alloc(XFS_IEXT_BUFSZ, KM_SLEEP);
4437 } else if (!ifp->if_real_bytes) {
4438 xfs_iext_inline_to_direct(ifp, XFS_IEXT_BUFSZ);
4439 } else if (ifp->if_real_bytes < XFS_IEXT_BUFSZ) {
4440 xfs_iext_realloc_direct(ifp, XFS_IEXT_BUFSZ);
4442 erp->er_extbuf = ifp->if_u1.if_extents;
4443 erp->er_extcount = nextents;
4446 ifp->if_flags |= XFS_IFEXTIREC;
4447 ifp->if_real_bytes = XFS_IEXT_BUFSZ;
4448 ifp->if_bytes = nextents * sizeof(xfs_bmbt_rec_t);
4449 ifp->if_u1.if_ext_irec = erp;
4455 * Allocate and initialize a new entry in the indirection array.
4459 xfs_ifork_t *ifp, /* inode fork pointer */
4460 int erp_idx) /* index for new irec */
4462 xfs_ext_irec_t *erp; /* indirection array pointer */
4463 int i; /* loop counter */
4464 int nlists; /* number of irec's (ex lists) */
4466 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
4467 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
4469 /* Resize indirection array */
4470 xfs_iext_realloc_indirect(ifp, ++nlists *
4471 sizeof(xfs_ext_irec_t));
4473 * Move records down in the array so the
4474 * new page can use erp_idx.
4476 erp = ifp->if_u1.if_ext_irec;
4477 for (i = nlists - 1; i > erp_idx; i--) {
4478 memmove(&erp[i], &erp[i-1], sizeof(xfs_ext_irec_t));
4480 ASSERT(i == erp_idx);
4482 /* Initialize new extent record */
4483 erp = ifp->if_u1.if_ext_irec;
4484 erp[erp_idx].er_extbuf = kmem_alloc(XFS_IEXT_BUFSZ, KM_SLEEP);
4485 ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
4486 memset(erp[erp_idx].er_extbuf, 0, XFS_IEXT_BUFSZ);
4487 erp[erp_idx].er_extcount = 0;
4488 erp[erp_idx].er_extoff = erp_idx > 0 ?
4489 erp[erp_idx-1].er_extoff + erp[erp_idx-1].er_extcount : 0;
4490 return (&erp[erp_idx]);
4494 * Remove a record from the indirection array.
4497 xfs_iext_irec_remove(
4498 xfs_ifork_t *ifp, /* inode fork pointer */
4499 int erp_idx) /* irec index to remove */
4501 xfs_ext_irec_t *erp; /* indirection array pointer */
4502 int i; /* loop counter */
4503 int nlists; /* number of irec's (ex lists) */
4505 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
4506 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
4507 erp = &ifp->if_u1.if_ext_irec[erp_idx];
4508 if (erp->er_extbuf) {
4509 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1,
4511 kmem_free(erp->er_extbuf, XFS_IEXT_BUFSZ);
4513 /* Compact extent records */
4514 erp = ifp->if_u1.if_ext_irec;
4515 for (i = erp_idx; i < nlists - 1; i++) {
4516 memmove(&erp[i], &erp[i+1], sizeof(xfs_ext_irec_t));
4519 * Manually free the last extent record from the indirection
4520 * array. A call to xfs_iext_realloc_indirect() with a size
4521 * of zero would result in a call to xfs_iext_destroy() which
4522 * would in turn call this function again, creating a nasty
4526 xfs_iext_realloc_indirect(ifp,
4527 nlists * sizeof(xfs_ext_irec_t));
4529 kmem_free(ifp->if_u1.if_ext_irec,
4530 sizeof(xfs_ext_irec_t));
4532 ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
4536 * This is called to clean up large amounts of unused memory allocated
4537 * by the indirection array. Before compacting anything though, verify
4538 * that the indirection array is still needed and switch back to the
4539 * linear extent list (or even the inline buffer) if possible. The
4540 * compaction policy is as follows:
4542 * Full Compaction: Extents fit into a single page (or inline buffer)
4543 * Full Compaction: Extents occupy less than 10% of allocated space
4544 * Partial Compaction: Extents occupy > 10% and < 50% of allocated space
4545 * No Compaction: Extents occupy at least 50% of allocated space
4548 xfs_iext_irec_compact(
4549 xfs_ifork_t *ifp) /* inode fork pointer */
4551 xfs_extnum_t nextents; /* number of extents in file */
4552 int nlists; /* number of irec's (ex lists) */
4554 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
4555 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
4556 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4558 if (nextents == 0) {
4559 xfs_iext_destroy(ifp);
4560 } else if (nextents <= XFS_INLINE_EXTS) {
4561 xfs_iext_indirect_to_direct(ifp);
4562 xfs_iext_direct_to_inline(ifp, nextents);
4563 } else if (nextents <= XFS_LINEAR_EXTS) {
4564 xfs_iext_indirect_to_direct(ifp);
4565 } else if (nextents < (nlists * XFS_LINEAR_EXTS) >> 3) {
4566 xfs_iext_irec_compact_full(ifp);
4567 } else if (nextents < (nlists * XFS_LINEAR_EXTS) >> 1) {
4568 xfs_iext_irec_compact_pages(ifp);
4573 * Combine extents from neighboring extent pages.
4576 xfs_iext_irec_compact_pages(
4577 xfs_ifork_t *ifp) /* inode fork pointer */
4579 xfs_ext_irec_t *erp, *erp_next;/* pointers to irec entries */
4580 int erp_idx = 0; /* indirection array index */
4581 int nlists; /* number of irec's (ex lists) */
4583 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
4584 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
4585 while (erp_idx < nlists - 1) {
4586 erp = &ifp->if_u1.if_ext_irec[erp_idx];
4588 if (erp_next->er_extcount <=
4589 (XFS_LINEAR_EXTS - erp->er_extcount)) {
4590 memmove(&erp->er_extbuf[erp->er_extcount],
4591 erp_next->er_extbuf, erp_next->er_extcount *
4592 sizeof(xfs_bmbt_rec_t));
4593 erp->er_extcount += erp_next->er_extcount;
4595 * Free page before removing extent record
4596 * so er_extoffs don't get modified in
4597 * xfs_iext_irec_remove.
4599 kmem_free(erp_next->er_extbuf, XFS_IEXT_BUFSZ);
4600 erp_next->er_extbuf = NULL;
4601 xfs_iext_irec_remove(ifp, erp_idx + 1);
4602 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
4610 * Fully compact the extent records managed by the indirection array.
4613 xfs_iext_irec_compact_full(
4614 xfs_ifork_t *ifp) /* inode fork pointer */
4616 xfs_bmbt_rec_host_t *ep, *ep_next; /* extent record pointers */
4617 xfs_ext_irec_t *erp, *erp_next; /* extent irec pointers */
4618 int erp_idx = 0; /* extent irec index */
4619 int ext_avail; /* empty entries in ex list */
4620 int ext_diff; /* number of exts to add */
4621 int nlists; /* number of irec's (ex lists) */
4623 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
4624 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
4625 erp = ifp->if_u1.if_ext_irec;
4626 ep = &erp->er_extbuf[erp->er_extcount];
4628 ep_next = erp_next->er_extbuf;
4629 while (erp_idx < nlists - 1) {
4630 ext_avail = XFS_LINEAR_EXTS - erp->er_extcount;
4631 ext_diff = MIN(ext_avail, erp_next->er_extcount);
4632 memcpy(ep, ep_next, ext_diff * sizeof(xfs_bmbt_rec_t));
4633 erp->er_extcount += ext_diff;
4634 erp_next->er_extcount -= ext_diff;
4635 /* Remove next page */
4636 if (erp_next->er_extcount == 0) {
4638 * Free page before removing extent record
4639 * so er_extoffs don't get modified in
4640 * xfs_iext_irec_remove.
4642 kmem_free(erp_next->er_extbuf,
4643 erp_next->er_extcount * sizeof(xfs_bmbt_rec_t));
4644 erp_next->er_extbuf = NULL;
4645 xfs_iext_irec_remove(ifp, erp_idx + 1);
4646 erp = &ifp->if_u1.if_ext_irec[erp_idx];
4647 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
4648 /* Update next page */
4650 /* Move rest of page up to become next new page */
4651 memmove(erp_next->er_extbuf, ep_next,
4652 erp_next->er_extcount * sizeof(xfs_bmbt_rec_t));
4653 ep_next = erp_next->er_extbuf;
4654 memset(&ep_next[erp_next->er_extcount], 0,
4655 (XFS_LINEAR_EXTS - erp_next->er_extcount) *
4656 sizeof(xfs_bmbt_rec_t));
4658 if (erp->er_extcount == XFS_LINEAR_EXTS) {
4660 if (erp_idx < nlists)
4661 erp = &ifp->if_u1.if_ext_irec[erp_idx];
4665 ep = &erp->er_extbuf[erp->er_extcount];
4667 ep_next = erp_next->er_extbuf;
4672 * This is called to update the er_extoff field in the indirection
4673 * array when extents have been added or removed from one of the
4674 * extent lists. erp_idx contains the irec index to begin updating
4675 * at and ext_diff contains the number of extents that were added
4679 xfs_iext_irec_update_extoffs(
4680 xfs_ifork_t *ifp, /* inode fork pointer */
4681 int erp_idx, /* irec index to update */
4682 int ext_diff) /* number of new extents */
4684 int i; /* loop counter */
4685 int nlists; /* number of irec's (ex lists */
4687 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
4688 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
4689 for (i = erp_idx; i < nlists; i++) {
4690 ifp->if_u1.if_ext_irec[i].er_extoff += ext_diff;