Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2 | * Copyright (c) 2000-2006 Silicon Graphics, Inc. |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 3 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * published by the Free Software Foundation. |
| 8 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | */ |
Robert P. J. Day | 40ebd81 | 2007-11-23 16:30:51 +1100 | [diff] [blame] | 18 | #include <linux/log2.h> |
| 19 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include "xfs.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 21 | #include "xfs_fs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include "xfs_types.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include "xfs_log.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 24 | #include "xfs_inum.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include "xfs_trans.h" |
| 26 | #include "xfs_trans_priv.h" |
| 27 | #include "xfs_sb.h" |
| 28 | #include "xfs_ag.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include "xfs_mount.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include "xfs_bmap_btree.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 31 | #include "xfs_alloc_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include "xfs_ialloc_btree.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 33 | #include "xfs_attr_sf.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include "xfs_dinode.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include "xfs_inode.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include "xfs_buf_item.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 37 | #include "xfs_inode_item.h" |
| 38 | #include "xfs_btree.h" |
| 39 | #include "xfs_alloc.h" |
| 40 | #include "xfs_ialloc.h" |
| 41 | #include "xfs_bmap.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include "xfs_error.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include "xfs_utils.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #include "xfs_quota.h" |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 45 | #include "xfs_filestream.h" |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 46 | #include "xfs_vnodeops.h" |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 47 | #include "xfs_trace.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | kmem_zone_t *xfs_ifork_zone; |
| 50 | kmem_zone_t *xfs_inode_zone; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
| 52 | /* |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 53 | * Used in xfs_itruncate_extents(). This is the maximum number of extents |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | * freed from a file in a single transaction. |
| 55 | */ |
| 56 | #define XFS_ITRUNC_MAX_EXTENTS 2 |
| 57 | |
| 58 | STATIC int xfs_iflush_int(xfs_inode_t *, xfs_buf_t *); |
| 59 | STATIC int xfs_iformat_local(xfs_inode_t *, xfs_dinode_t *, int, int); |
| 60 | STATIC int xfs_iformat_extents(xfs_inode_t *, xfs_dinode_t *, int); |
| 61 | STATIC int xfs_iformat_btree(xfs_inode_t *, xfs_dinode_t *, int); |
| 62 | |
Dave Chinner | 2a0ec1d | 2012-04-23 15:59:02 +1000 | [diff] [blame] | 63 | /* |
| 64 | * helper function to extract extent size hint from inode |
| 65 | */ |
| 66 | xfs_extlen_t |
| 67 | xfs_get_extsz_hint( |
| 68 | struct xfs_inode *ip) |
| 69 | { |
| 70 | if ((ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE) && ip->i_d.di_extsize) |
| 71 | return ip->i_d.di_extsize; |
| 72 | if (XFS_IS_REALTIME_INODE(ip)) |
| 73 | return ip->i_mount->m_sb.sb_rextsize; |
| 74 | return 0; |
| 75 | } |
| 76 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | #ifdef DEBUG |
| 78 | /* |
| 79 | * Make sure that the extents in the given memory buffer |
| 80 | * are valid. |
| 81 | */ |
| 82 | STATIC void |
| 83 | xfs_validate_extents( |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 84 | xfs_ifork_t *ifp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | int nrecs, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | xfs_exntfmt_t fmt) |
| 87 | { |
| 88 | xfs_bmbt_irec_t irec; |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 89 | xfs_bmbt_rec_host_t rec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | int i; |
| 91 | |
| 92 | for (i = 0; i < nrecs; i++) { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 93 | xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i); |
| 94 | rec.l0 = get_unaligned(&ep->l0); |
| 95 | rec.l1 = get_unaligned(&ep->l1); |
| 96 | xfs_bmbt_get_all(&rec, &irec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | if (fmt == XFS_EXTFMT_NOSTATE) |
| 98 | ASSERT(irec.br_state == XFS_EXT_NORM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | #else /* DEBUG */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 102 | #define xfs_validate_extents(ifp, nrecs, fmt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | #endif /* DEBUG */ |
| 104 | |
| 105 | /* |
| 106 | * Check that none of the inode's in the buffer have a next |
| 107 | * unlinked field of 0. |
| 108 | */ |
| 109 | #if defined(DEBUG) |
| 110 | void |
| 111 | xfs_inobp_check( |
| 112 | xfs_mount_t *mp, |
| 113 | xfs_buf_t *bp) |
| 114 | { |
| 115 | int i; |
| 116 | int j; |
| 117 | xfs_dinode_t *dip; |
| 118 | |
| 119 | j = mp->m_inode_cluster_size >> mp->m_sb.sb_inodelog; |
| 120 | |
| 121 | for (i = 0; i < j; i++) { |
| 122 | dip = (xfs_dinode_t *)xfs_buf_offset(bp, |
| 123 | i * mp->m_sb.sb_inodesize); |
| 124 | if (!dip->di_next_unlinked) { |
Dave Chinner | 5348778 | 2011-03-07 10:05:35 +1100 | [diff] [blame] | 125 | xfs_alert(mp, |
| 126 | "Detected bogus zero next_unlinked field in incore inode buffer 0x%p.", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | bp); |
| 128 | ASSERT(dip->di_next_unlinked); |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | #endif |
| 133 | |
| 134 | /* |
Christoph Hellwig | 475ee41 | 2012-07-03 12:21:22 -0400 | [diff] [blame^] | 135 | * This routine is called to map an inode to the buffer containing the on-disk |
| 136 | * version of the inode. It returns a pointer to the buffer containing the |
| 137 | * on-disk inode in the bpp parameter, and in the dipp parameter it returns a |
| 138 | * pointer to the on-disk inode within that buffer. |
| 139 | * |
| 140 | * If a non-zero error is returned, then the contents of bpp and dipp are |
| 141 | * undefined. |
David Chinner | 4ae29b4 | 2008-03-06 13:43:34 +1100 | [diff] [blame] | 142 | */ |
Christoph Hellwig | 475ee41 | 2012-07-03 12:21:22 -0400 | [diff] [blame^] | 143 | int |
David Chinner | 4ae29b4 | 2008-03-06 13:43:34 +1100 | [diff] [blame] | 144 | xfs_imap_to_bp( |
Christoph Hellwig | 475ee41 | 2012-07-03 12:21:22 -0400 | [diff] [blame^] | 145 | struct xfs_mount *mp, |
| 146 | struct xfs_trans *tp, |
| 147 | struct xfs_imap *imap, |
| 148 | struct xfs_dinode **dipp, |
| 149 | struct xfs_buf **bpp, |
| 150 | uint buf_flags, |
| 151 | uint iget_flags) |
David Chinner | 4ae29b4 | 2008-03-06 13:43:34 +1100 | [diff] [blame] | 152 | { |
Christoph Hellwig | 475ee41 | 2012-07-03 12:21:22 -0400 | [diff] [blame^] | 153 | struct xfs_buf *bp; |
| 154 | int error; |
| 155 | int i; |
| 156 | int ni; |
David Chinner | 4ae29b4 | 2008-03-06 13:43:34 +1100 | [diff] [blame] | 157 | |
Dave Chinner | 611c994 | 2012-04-23 15:59:07 +1000 | [diff] [blame] | 158 | buf_flags |= XBF_UNMAPPED; |
David Chinner | 4ae29b4 | 2008-03-06 13:43:34 +1100 | [diff] [blame] | 159 | error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, imap->im_blkno, |
David Chinner | a3f74ff | 2008-03-06 13:43:42 +1100 | [diff] [blame] | 160 | (int)imap->im_len, buf_flags, &bp); |
David Chinner | 4ae29b4 | 2008-03-06 13:43:34 +1100 | [diff] [blame] | 161 | if (error) { |
David Chinner | a3f74ff | 2008-03-06 13:43:42 +1100 | [diff] [blame] | 162 | if (error != EAGAIN) { |
Dave Chinner | 0b932cc | 2011-03-07 10:08:35 +1100 | [diff] [blame] | 163 | xfs_warn(mp, |
| 164 | "%s: xfs_trans_read_buf() returned error %d.", |
| 165 | __func__, error); |
David Chinner | a3f74ff | 2008-03-06 13:43:42 +1100 | [diff] [blame] | 166 | } else { |
Christoph Hellwig | 0cadda1 | 2010-01-19 09:56:44 +0000 | [diff] [blame] | 167 | ASSERT(buf_flags & XBF_TRYLOCK); |
David Chinner | a3f74ff | 2008-03-06 13:43:42 +1100 | [diff] [blame] | 168 | } |
David Chinner | 4ae29b4 | 2008-03-06 13:43:34 +1100 | [diff] [blame] | 169 | return error; |
| 170 | } |
| 171 | |
| 172 | /* |
| 173 | * Validate the magic number and version of every inode in the buffer |
| 174 | * (if DEBUG kernel) or the first inode in the buffer, otherwise. |
| 175 | */ |
| 176 | #ifdef DEBUG |
| 177 | ni = BBTOB(imap->im_len) >> mp->m_sb.sb_inodelog; |
| 178 | #else /* usual case */ |
| 179 | ni = 1; |
| 180 | #endif |
| 181 | |
| 182 | for (i = 0; i < ni; i++) { |
| 183 | int di_ok; |
| 184 | xfs_dinode_t *dip; |
| 185 | |
| 186 | dip = (xfs_dinode_t *)xfs_buf_offset(bp, |
| 187 | (i << mp->m_sb.sb_inodelog)); |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 188 | di_ok = dip->di_magic == cpu_to_be16(XFS_DINODE_MAGIC) && |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 189 | XFS_DINODE_GOOD_VERSION(dip->di_version); |
David Chinner | 4ae29b4 | 2008-03-06 13:43:34 +1100 | [diff] [blame] | 190 | if (unlikely(XFS_TEST_ERROR(!di_ok, mp, |
| 191 | XFS_ERRTAG_ITOBP_INOTOBP, |
| 192 | XFS_RANDOM_ITOBP_INOTOBP))) { |
Dave Chinner | 1920779 | 2010-06-24 11:15:47 +1000 | [diff] [blame] | 193 | if (iget_flags & XFS_IGET_UNTRUSTED) { |
David Chinner | 4ae29b4 | 2008-03-06 13:43:34 +1100 | [diff] [blame] | 194 | xfs_trans_brelse(tp, bp); |
| 195 | return XFS_ERROR(EINVAL); |
| 196 | } |
Christoph Hellwig | 475ee41 | 2012-07-03 12:21:22 -0400 | [diff] [blame^] | 197 | XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_HIGH, |
| 198 | mp, dip); |
David Chinner | 4ae29b4 | 2008-03-06 13:43:34 +1100 | [diff] [blame] | 199 | #ifdef DEBUG |
Dave Chinner | 0b932cc | 2011-03-07 10:08:35 +1100 | [diff] [blame] | 200 | xfs_emerg(mp, |
| 201 | "bad inode magic/vsn daddr %lld #%d (magic=%x)", |
David Chinner | 4ae29b4 | 2008-03-06 13:43:34 +1100 | [diff] [blame] | 202 | (unsigned long long)imap->im_blkno, i, |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 203 | be16_to_cpu(dip->di_magic)); |
Dave Chinner | 0b932cc | 2011-03-07 10:08:35 +1100 | [diff] [blame] | 204 | ASSERT(0); |
David Chinner | 4ae29b4 | 2008-03-06 13:43:34 +1100 | [diff] [blame] | 205 | #endif |
| 206 | xfs_trans_brelse(tp, bp); |
| 207 | return XFS_ERROR(EFSCORRUPTED); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | xfs_inobp_check(mp, bp); |
Christoph Hellwig | 475ee41 | 2012-07-03 12:21:22 -0400 | [diff] [blame^] | 212 | |
David Chinner | 4ae29b4 | 2008-03-06 13:43:34 +1100 | [diff] [blame] | 213 | *bpp = bp; |
Christoph Hellwig | 475ee41 | 2012-07-03 12:21:22 -0400 | [diff] [blame^] | 214 | *dipp = (struct xfs_dinode *)xfs_buf_offset(bp, imap->im_boffset); |
David Chinner | 4ae29b4 | 2008-03-06 13:43:34 +1100 | [diff] [blame] | 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | * This routine is called to map an inode number within a file |
| 220 | * system to the buffer containing the on-disk version of the |
| 221 | * inode. It returns a pointer to the buffer containing the |
| 222 | * on-disk inode in the bpp parameter, and in the dip parameter |
| 223 | * it returns a pointer to the on-disk inode within that buffer. |
| 224 | * |
| 225 | * If a non-zero error is returned, then the contents of bpp and |
| 226 | * dipp are undefined. |
| 227 | * |
| 228 | * Use xfs_imap() to determine the size and location of the |
| 229 | * buffer to read from disk. |
| 230 | */ |
Christoph Hellwig | c679eef | 2008-10-30 18:04:13 +1100 | [diff] [blame] | 231 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | xfs_inotobp( |
| 233 | xfs_mount_t *mp, |
| 234 | xfs_trans_t *tp, |
| 235 | xfs_ino_t ino, |
| 236 | xfs_dinode_t **dipp, |
| 237 | xfs_buf_t **bpp, |
Christoph Hellwig | c679eef | 2008-10-30 18:04:13 +1100 | [diff] [blame] | 238 | int *offset, |
| 239 | uint imap_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | { |
Christoph Hellwig | 92bfc6e | 2008-11-28 14:23:41 +1100 | [diff] [blame] | 241 | struct xfs_imap imap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | xfs_buf_t *bp; |
| 243 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | imap.im_blkno = 0; |
Christoph Hellwig | a194189 | 2008-11-28 14:23:40 +1100 | [diff] [blame] | 246 | error = xfs_imap(mp, tp, ino, &imap, imap_flags); |
David Chinner | 4ae29b4 | 2008-03-06 13:43:34 +1100 | [diff] [blame] | 247 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | |
Christoph Hellwig | 475ee41 | 2012-07-03 12:21:22 -0400 | [diff] [blame^] | 250 | error = xfs_imap_to_bp(mp, tp, &imap, dipp, &bp, 0, imap_flags); |
David Chinner | 4ae29b4 | 2008-03-06 13:43:34 +1100 | [diff] [blame] | 251 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | *bpp = bp; |
| 255 | *offset = imap.im_boffset; |
| 256 | return 0; |
| 257 | } |
| 258 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | /* |
| 260 | * Move inode type and inode format specific information from the |
| 261 | * on-disk inode to the in-core inode. For fifos, devs, and sockets |
| 262 | * this means set if_rdev to the proper value. For files, directories, |
| 263 | * and symlinks this means to bring in the in-line data or extent |
| 264 | * pointers. For a file in B-tree format, only the root is immediately |
| 265 | * brought in-core. The rest will be in-lined in if_extents when it |
| 266 | * is first referenced (see xfs_iread_extents()). |
| 267 | */ |
| 268 | STATIC int |
| 269 | xfs_iformat( |
| 270 | xfs_inode_t *ip, |
| 271 | xfs_dinode_t *dip) |
| 272 | { |
| 273 | xfs_attr_shortform_t *atp; |
| 274 | int size; |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 275 | int error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | xfs_fsize_t di_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 278 | if (unlikely(be32_to_cpu(dip->di_nextents) + |
| 279 | be16_to_cpu(dip->di_anextents) > |
| 280 | be64_to_cpu(dip->di_nblocks))) { |
Dave Chinner | 65333b4 | 2011-03-07 10:03:35 +1100 | [diff] [blame] | 281 | xfs_warn(ip->i_mount, |
Nathan Scott | 3762ec6 | 2006-01-12 10:29:53 +1100 | [diff] [blame] | 282 | "corrupt dinode %Lu, extent total = %d, nblocks = %Lu.", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | (unsigned long long)ip->i_ino, |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 284 | (int)(be32_to_cpu(dip->di_nextents) + |
| 285 | be16_to_cpu(dip->di_anextents)), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | (unsigned long long) |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 287 | be64_to_cpu(dip->di_nblocks)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | XFS_CORRUPTION_ERROR("xfs_iformat(1)", XFS_ERRLEVEL_LOW, |
| 289 | ip->i_mount, dip); |
| 290 | return XFS_ERROR(EFSCORRUPTED); |
| 291 | } |
| 292 | |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 293 | if (unlikely(dip->di_forkoff > ip->i_mount->m_sb.sb_inodesize)) { |
Dave Chinner | 65333b4 | 2011-03-07 10:03:35 +1100 | [diff] [blame] | 294 | xfs_warn(ip->i_mount, "corrupt dinode %Lu, forkoff = 0x%x.", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | (unsigned long long)ip->i_ino, |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 296 | dip->di_forkoff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | XFS_CORRUPTION_ERROR("xfs_iformat(2)", XFS_ERRLEVEL_LOW, |
| 298 | ip->i_mount, dip); |
| 299 | return XFS_ERROR(EFSCORRUPTED); |
| 300 | } |
| 301 | |
Christoph Hellwig | b89d420 | 2009-08-10 11:32:18 -0300 | [diff] [blame] | 302 | if (unlikely((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) && |
| 303 | !ip->i_mount->m_rtdev_targp)) { |
Dave Chinner | 65333b4 | 2011-03-07 10:03:35 +1100 | [diff] [blame] | 304 | xfs_warn(ip->i_mount, |
Christoph Hellwig | b89d420 | 2009-08-10 11:32:18 -0300 | [diff] [blame] | 305 | "corrupt dinode %Lu, has realtime flag set.", |
| 306 | ip->i_ino); |
| 307 | XFS_CORRUPTION_ERROR("xfs_iformat(realtime)", |
| 308 | XFS_ERRLEVEL_LOW, ip->i_mount, dip); |
| 309 | return XFS_ERROR(EFSCORRUPTED); |
| 310 | } |
| 311 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | switch (ip->i_d.di_mode & S_IFMT) { |
| 313 | case S_IFIFO: |
| 314 | case S_IFCHR: |
| 315 | case S_IFBLK: |
| 316 | case S_IFSOCK: |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 317 | if (unlikely(dip->di_format != XFS_DINODE_FMT_DEV)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | XFS_CORRUPTION_ERROR("xfs_iformat(3)", XFS_ERRLEVEL_LOW, |
| 319 | ip->i_mount, dip); |
| 320 | return XFS_ERROR(EFSCORRUPTED); |
| 321 | } |
| 322 | ip->i_d.di_size = 0; |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 323 | ip->i_df.if_u2.if_rdev = xfs_dinode_get_rdev(dip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | break; |
| 325 | |
| 326 | case S_IFREG: |
| 327 | case S_IFLNK: |
| 328 | case S_IFDIR: |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 329 | switch (dip->di_format) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | case XFS_DINODE_FMT_LOCAL: |
| 331 | /* |
| 332 | * no local regular files yet |
| 333 | */ |
Al Viro | abbede1 | 2011-07-26 02:31:30 -0400 | [diff] [blame] | 334 | if (unlikely(S_ISREG(be16_to_cpu(dip->di_mode)))) { |
Dave Chinner | 65333b4 | 2011-03-07 10:03:35 +1100 | [diff] [blame] | 335 | xfs_warn(ip->i_mount, |
| 336 | "corrupt inode %Lu (local format for regular file).", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | (unsigned long long) ip->i_ino); |
| 338 | XFS_CORRUPTION_ERROR("xfs_iformat(4)", |
| 339 | XFS_ERRLEVEL_LOW, |
| 340 | ip->i_mount, dip); |
| 341 | return XFS_ERROR(EFSCORRUPTED); |
| 342 | } |
| 343 | |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 344 | di_size = be64_to_cpu(dip->di_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | if (unlikely(di_size > XFS_DFORK_DSIZE(dip, ip->i_mount))) { |
Dave Chinner | 65333b4 | 2011-03-07 10:03:35 +1100 | [diff] [blame] | 346 | xfs_warn(ip->i_mount, |
| 347 | "corrupt inode %Lu (bad size %Ld for local inode).", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | (unsigned long long) ip->i_ino, |
| 349 | (long long) di_size); |
| 350 | XFS_CORRUPTION_ERROR("xfs_iformat(5)", |
| 351 | XFS_ERRLEVEL_LOW, |
| 352 | ip->i_mount, dip); |
| 353 | return XFS_ERROR(EFSCORRUPTED); |
| 354 | } |
| 355 | |
| 356 | size = (int)di_size; |
| 357 | error = xfs_iformat_local(ip, dip, XFS_DATA_FORK, size); |
| 358 | break; |
| 359 | case XFS_DINODE_FMT_EXTENTS: |
| 360 | error = xfs_iformat_extents(ip, dip, XFS_DATA_FORK); |
| 361 | break; |
| 362 | case XFS_DINODE_FMT_BTREE: |
| 363 | error = xfs_iformat_btree(ip, dip, XFS_DATA_FORK); |
| 364 | break; |
| 365 | default: |
| 366 | XFS_ERROR_REPORT("xfs_iformat(6)", XFS_ERRLEVEL_LOW, |
| 367 | ip->i_mount); |
| 368 | return XFS_ERROR(EFSCORRUPTED); |
| 369 | } |
| 370 | break; |
| 371 | |
| 372 | default: |
| 373 | XFS_ERROR_REPORT("xfs_iformat(7)", XFS_ERRLEVEL_LOW, ip->i_mount); |
| 374 | return XFS_ERROR(EFSCORRUPTED); |
| 375 | } |
| 376 | if (error) { |
| 377 | return error; |
| 378 | } |
| 379 | if (!XFS_DFORK_Q(dip)) |
| 380 | return 0; |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 381 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | ASSERT(ip->i_afp == NULL); |
Dave Chinner | 4a7eddd | 2010-07-20 17:53:59 +1000 | [diff] [blame] | 383 | ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP | KM_NOFS); |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 384 | |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 385 | switch (dip->di_aformat) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | case XFS_DINODE_FMT_LOCAL: |
| 387 | atp = (xfs_attr_shortform_t *)XFS_DFORK_APTR(dip); |
Nathan Scott | 3b244aa | 2006-03-17 17:29:25 +1100 | [diff] [blame] | 388 | size = be16_to_cpu(atp->hdr.totsize); |
Christoph Hellwig | 2809f76 | 2009-01-19 02:04:16 +0100 | [diff] [blame] | 389 | |
| 390 | if (unlikely(size < sizeof(struct xfs_attr_sf_hdr))) { |
Dave Chinner | 65333b4 | 2011-03-07 10:03:35 +1100 | [diff] [blame] | 391 | xfs_warn(ip->i_mount, |
| 392 | "corrupt inode %Lu (bad attr fork size %Ld).", |
Christoph Hellwig | 2809f76 | 2009-01-19 02:04:16 +0100 | [diff] [blame] | 393 | (unsigned long long) ip->i_ino, |
| 394 | (long long) size); |
| 395 | XFS_CORRUPTION_ERROR("xfs_iformat(8)", |
| 396 | XFS_ERRLEVEL_LOW, |
| 397 | ip->i_mount, dip); |
| 398 | return XFS_ERROR(EFSCORRUPTED); |
| 399 | } |
| 400 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK, size); |
| 402 | break; |
| 403 | case XFS_DINODE_FMT_EXTENTS: |
| 404 | error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK); |
| 405 | break; |
| 406 | case XFS_DINODE_FMT_BTREE: |
| 407 | error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK); |
| 408 | break; |
| 409 | default: |
| 410 | error = XFS_ERROR(EFSCORRUPTED); |
| 411 | break; |
| 412 | } |
| 413 | if (error) { |
| 414 | kmem_zone_free(xfs_ifork_zone, ip->i_afp); |
| 415 | ip->i_afp = NULL; |
| 416 | xfs_idestroy_fork(ip, XFS_DATA_FORK); |
| 417 | } |
| 418 | return error; |
| 419 | } |
| 420 | |
| 421 | /* |
| 422 | * The file is in-lined in the on-disk inode. |
| 423 | * If it fits into if_inline_data, then copy |
| 424 | * it there, otherwise allocate a buffer for it |
| 425 | * and copy the data there. Either way, set |
| 426 | * if_data to point at the data. |
| 427 | * If we allocate a buffer for the data, make |
| 428 | * sure that its size is a multiple of 4 and |
| 429 | * record the real size in i_real_bytes. |
| 430 | */ |
| 431 | STATIC int |
| 432 | xfs_iformat_local( |
| 433 | xfs_inode_t *ip, |
| 434 | xfs_dinode_t *dip, |
| 435 | int whichfork, |
| 436 | int size) |
| 437 | { |
| 438 | xfs_ifork_t *ifp; |
| 439 | int real_size; |
| 440 | |
| 441 | /* |
| 442 | * If the size is unreasonable, then something |
| 443 | * is wrong and we just bail out rather than crash in |
| 444 | * kmem_alloc() or memcpy() below. |
| 445 | */ |
| 446 | if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) { |
Dave Chinner | 65333b4 | 2011-03-07 10:03:35 +1100 | [diff] [blame] | 447 | xfs_warn(ip->i_mount, |
| 448 | "corrupt inode %Lu (bad size %d for local fork, size = %d).", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | (unsigned long long) ip->i_ino, size, |
| 450 | XFS_DFORK_SIZE(dip, ip->i_mount, whichfork)); |
| 451 | XFS_CORRUPTION_ERROR("xfs_iformat_local", XFS_ERRLEVEL_LOW, |
| 452 | ip->i_mount, dip); |
| 453 | return XFS_ERROR(EFSCORRUPTED); |
| 454 | } |
| 455 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 456 | real_size = 0; |
| 457 | if (size == 0) |
| 458 | ifp->if_u1.if_data = NULL; |
| 459 | else if (size <= sizeof(ifp->if_u2.if_inline_data)) |
| 460 | ifp->if_u1.if_data = ifp->if_u2.if_inline_data; |
| 461 | else { |
| 462 | real_size = roundup(size, 4); |
Dave Chinner | 4a7eddd | 2010-07-20 17:53:59 +1000 | [diff] [blame] | 463 | ifp->if_u1.if_data = kmem_alloc(real_size, KM_SLEEP | KM_NOFS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | } |
| 465 | ifp->if_bytes = size; |
| 466 | ifp->if_real_bytes = real_size; |
| 467 | if (size) |
| 468 | memcpy(ifp->if_u1.if_data, XFS_DFORK_PTR(dip, whichfork), size); |
| 469 | ifp->if_flags &= ~XFS_IFEXTENTS; |
| 470 | ifp->if_flags |= XFS_IFINLINE; |
| 471 | return 0; |
| 472 | } |
| 473 | |
| 474 | /* |
| 475 | * The file consists of a set of extents all |
| 476 | * of which fit into the on-disk inode. |
| 477 | * If there are few enough extents to fit into |
| 478 | * the if_inline_ext, then copy them there. |
| 479 | * Otherwise allocate a buffer for them and copy |
| 480 | * them into it. Either way, set if_extents |
| 481 | * to point at the extents. |
| 482 | */ |
| 483 | STATIC int |
| 484 | xfs_iformat_extents( |
| 485 | xfs_inode_t *ip, |
| 486 | xfs_dinode_t *dip, |
| 487 | int whichfork) |
| 488 | { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 489 | xfs_bmbt_rec_t *dp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | xfs_ifork_t *ifp; |
| 491 | int nex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | int size; |
| 493 | int i; |
| 494 | |
| 495 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 496 | nex = XFS_DFORK_NEXTENTS(dip, whichfork); |
| 497 | size = nex * (uint)sizeof(xfs_bmbt_rec_t); |
| 498 | |
| 499 | /* |
| 500 | * If the number of extents is unreasonable, then something |
| 501 | * is wrong and we just bail out rather than crash in |
| 502 | * kmem_alloc() or memcpy() below. |
| 503 | */ |
| 504 | if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) { |
Dave Chinner | 65333b4 | 2011-03-07 10:03:35 +1100 | [diff] [blame] | 505 | xfs_warn(ip->i_mount, "corrupt inode %Lu ((a)extents = %d).", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | (unsigned long long) ip->i_ino, nex); |
| 507 | XFS_CORRUPTION_ERROR("xfs_iformat_extents(1)", XFS_ERRLEVEL_LOW, |
| 508 | ip->i_mount, dip); |
| 509 | return XFS_ERROR(EFSCORRUPTED); |
| 510 | } |
| 511 | |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 512 | ifp->if_real_bytes = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | if (nex == 0) |
| 514 | ifp->if_u1.if_extents = NULL; |
| 515 | else if (nex <= XFS_INLINE_EXTS) |
| 516 | ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 517 | else |
| 518 | xfs_iext_add(ifp, 0, nex); |
| 519 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | ifp->if_bytes = size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | if (size) { |
| 522 | dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork); |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 523 | xfs_validate_extents(ifp, nex, XFS_EXTFMT_INODE(ip)); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 524 | for (i = 0; i < nex; i++, dp++) { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 525 | xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i); |
Harvey Harrison | 597bca6 | 2008-08-13 16:29:21 +1000 | [diff] [blame] | 526 | ep->l0 = get_unaligned_be64(&dp->l0); |
| 527 | ep->l1 = get_unaligned_be64(&dp->l1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | } |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 529 | XFS_BMAP_TRACE_EXLIST(ip, nex, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | if (whichfork != XFS_DATA_FORK || |
| 531 | XFS_EXTFMT_INODE(ip) == XFS_EXTFMT_NOSTATE) |
| 532 | if (unlikely(xfs_check_nostate_extents( |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 533 | ifp, 0, nex))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | XFS_ERROR_REPORT("xfs_iformat_extents(2)", |
| 535 | XFS_ERRLEVEL_LOW, |
| 536 | ip->i_mount); |
| 537 | return XFS_ERROR(EFSCORRUPTED); |
| 538 | } |
| 539 | } |
| 540 | ifp->if_flags |= XFS_IFEXTENTS; |
| 541 | return 0; |
| 542 | } |
| 543 | |
| 544 | /* |
| 545 | * The file has too many extents to fit into |
| 546 | * the inode, so they are in B-tree format. |
| 547 | * Allocate a buffer for the root of the B-tree |
| 548 | * and copy the root into it. The i_extents |
| 549 | * field will remain NULL until all of the |
| 550 | * extents are read in (when they are needed). |
| 551 | */ |
| 552 | STATIC int |
| 553 | xfs_iformat_btree( |
| 554 | xfs_inode_t *ip, |
| 555 | xfs_dinode_t *dip, |
| 556 | int whichfork) |
| 557 | { |
| 558 | xfs_bmdr_block_t *dfp; |
| 559 | xfs_ifork_t *ifp; |
| 560 | /* REFERENCED */ |
| 561 | int nrecs; |
| 562 | int size; |
| 563 | |
| 564 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 565 | dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork); |
| 566 | size = XFS_BMAP_BROOT_SPACE(dfp); |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 567 | nrecs = be16_to_cpu(dfp->bb_numrecs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | |
| 569 | /* |
| 570 | * blow out if -- fork has less extents than can fit in |
| 571 | * fork (fork shouldn't be a btree format), root btree |
| 572 | * block has more records than can fit into the fork, |
| 573 | * or the number of extents is greater than the number of |
| 574 | * blocks. |
| 575 | */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 576 | if (unlikely(XFS_IFORK_NEXTENTS(ip, whichfork) <= |
| 577 | XFS_IFORK_MAXEXT(ip, whichfork) || |
| 578 | XFS_BMDR_SPACE_CALC(nrecs) > |
| 579 | XFS_DFORK_SIZE(dip, ip->i_mount, whichfork) || |
| 580 | XFS_IFORK_NEXTENTS(ip, whichfork) > ip->i_d.di_nblocks)) { |
Dave Chinner | 65333b4 | 2011-03-07 10:03:35 +1100 | [diff] [blame] | 581 | xfs_warn(ip->i_mount, "corrupt inode %Lu (btree).", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | (unsigned long long) ip->i_ino); |
Dave Chinner | 65333b4 | 2011-03-07 10:03:35 +1100 | [diff] [blame] | 583 | XFS_CORRUPTION_ERROR("xfs_iformat_btree", XFS_ERRLEVEL_LOW, |
| 584 | ip->i_mount, dip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | return XFS_ERROR(EFSCORRUPTED); |
| 586 | } |
| 587 | |
| 588 | ifp->if_broot_bytes = size; |
Dave Chinner | 4a7eddd | 2010-07-20 17:53:59 +1000 | [diff] [blame] | 589 | ifp->if_broot = kmem_alloc(size, KM_SLEEP | KM_NOFS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | ASSERT(ifp->if_broot != NULL); |
| 591 | /* |
| 592 | * Copy and convert from the on-disk structure |
| 593 | * to the in-memory structure. |
| 594 | */ |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 595 | xfs_bmdr_to_bmbt(ip->i_mount, dfp, |
| 596 | XFS_DFORK_SIZE(dip, ip->i_mount, whichfork), |
| 597 | ifp->if_broot, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | ifp->if_flags &= ~XFS_IFEXTENTS; |
| 599 | ifp->if_flags |= XFS_IFBROOT; |
| 600 | |
| 601 | return 0; |
| 602 | } |
| 603 | |
Eric Sandeen | d96f8f8 | 2009-07-02 00:09:33 -0500 | [diff] [blame] | 604 | STATIC void |
Christoph Hellwig | 347d1c0 | 2007-08-28 13:57:51 +1000 | [diff] [blame] | 605 | xfs_dinode_from_disk( |
| 606 | xfs_icdinode_t *to, |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 607 | xfs_dinode_t *from) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | { |
Christoph Hellwig | 347d1c0 | 2007-08-28 13:57:51 +1000 | [diff] [blame] | 609 | to->di_magic = be16_to_cpu(from->di_magic); |
| 610 | to->di_mode = be16_to_cpu(from->di_mode); |
| 611 | to->di_version = from ->di_version; |
| 612 | to->di_format = from->di_format; |
| 613 | to->di_onlink = be16_to_cpu(from->di_onlink); |
| 614 | to->di_uid = be32_to_cpu(from->di_uid); |
| 615 | to->di_gid = be32_to_cpu(from->di_gid); |
| 616 | to->di_nlink = be32_to_cpu(from->di_nlink); |
Arkadiusz Mi?kiewicz | 6743099 | 2010-09-26 06:10:18 +0000 | [diff] [blame] | 617 | to->di_projid_lo = be16_to_cpu(from->di_projid_lo); |
| 618 | to->di_projid_hi = be16_to_cpu(from->di_projid_hi); |
Christoph Hellwig | 347d1c0 | 2007-08-28 13:57:51 +1000 | [diff] [blame] | 619 | memcpy(to->di_pad, from->di_pad, sizeof(to->di_pad)); |
| 620 | to->di_flushiter = be16_to_cpu(from->di_flushiter); |
| 621 | to->di_atime.t_sec = be32_to_cpu(from->di_atime.t_sec); |
| 622 | to->di_atime.t_nsec = be32_to_cpu(from->di_atime.t_nsec); |
| 623 | to->di_mtime.t_sec = be32_to_cpu(from->di_mtime.t_sec); |
| 624 | to->di_mtime.t_nsec = be32_to_cpu(from->di_mtime.t_nsec); |
| 625 | to->di_ctime.t_sec = be32_to_cpu(from->di_ctime.t_sec); |
| 626 | to->di_ctime.t_nsec = be32_to_cpu(from->di_ctime.t_nsec); |
| 627 | to->di_size = be64_to_cpu(from->di_size); |
| 628 | to->di_nblocks = be64_to_cpu(from->di_nblocks); |
| 629 | to->di_extsize = be32_to_cpu(from->di_extsize); |
| 630 | to->di_nextents = be32_to_cpu(from->di_nextents); |
| 631 | to->di_anextents = be16_to_cpu(from->di_anextents); |
| 632 | to->di_forkoff = from->di_forkoff; |
| 633 | to->di_aformat = from->di_aformat; |
| 634 | to->di_dmevmask = be32_to_cpu(from->di_dmevmask); |
| 635 | to->di_dmstate = be16_to_cpu(from->di_dmstate); |
| 636 | to->di_flags = be16_to_cpu(from->di_flags); |
| 637 | to->di_gen = be32_to_cpu(from->di_gen); |
| 638 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | |
Christoph Hellwig | 347d1c0 | 2007-08-28 13:57:51 +1000 | [diff] [blame] | 640 | void |
| 641 | xfs_dinode_to_disk( |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 642 | xfs_dinode_t *to, |
Christoph Hellwig | 347d1c0 | 2007-08-28 13:57:51 +1000 | [diff] [blame] | 643 | xfs_icdinode_t *from) |
| 644 | { |
| 645 | to->di_magic = cpu_to_be16(from->di_magic); |
| 646 | to->di_mode = cpu_to_be16(from->di_mode); |
| 647 | to->di_version = from ->di_version; |
| 648 | to->di_format = from->di_format; |
| 649 | to->di_onlink = cpu_to_be16(from->di_onlink); |
| 650 | to->di_uid = cpu_to_be32(from->di_uid); |
| 651 | to->di_gid = cpu_to_be32(from->di_gid); |
| 652 | to->di_nlink = cpu_to_be32(from->di_nlink); |
Arkadiusz Mi?kiewicz | 6743099 | 2010-09-26 06:10:18 +0000 | [diff] [blame] | 653 | to->di_projid_lo = cpu_to_be16(from->di_projid_lo); |
| 654 | to->di_projid_hi = cpu_to_be16(from->di_projid_hi); |
Christoph Hellwig | 347d1c0 | 2007-08-28 13:57:51 +1000 | [diff] [blame] | 655 | memcpy(to->di_pad, from->di_pad, sizeof(to->di_pad)); |
| 656 | to->di_flushiter = cpu_to_be16(from->di_flushiter); |
| 657 | to->di_atime.t_sec = cpu_to_be32(from->di_atime.t_sec); |
| 658 | to->di_atime.t_nsec = cpu_to_be32(from->di_atime.t_nsec); |
| 659 | to->di_mtime.t_sec = cpu_to_be32(from->di_mtime.t_sec); |
| 660 | to->di_mtime.t_nsec = cpu_to_be32(from->di_mtime.t_nsec); |
| 661 | to->di_ctime.t_sec = cpu_to_be32(from->di_ctime.t_sec); |
| 662 | to->di_ctime.t_nsec = cpu_to_be32(from->di_ctime.t_nsec); |
| 663 | to->di_size = cpu_to_be64(from->di_size); |
| 664 | to->di_nblocks = cpu_to_be64(from->di_nblocks); |
| 665 | to->di_extsize = cpu_to_be32(from->di_extsize); |
| 666 | to->di_nextents = cpu_to_be32(from->di_nextents); |
| 667 | to->di_anextents = cpu_to_be16(from->di_anextents); |
| 668 | to->di_forkoff = from->di_forkoff; |
| 669 | to->di_aformat = from->di_aformat; |
| 670 | to->di_dmevmask = cpu_to_be32(from->di_dmevmask); |
| 671 | to->di_dmstate = cpu_to_be16(from->di_dmstate); |
| 672 | to->di_flags = cpu_to_be16(from->di_flags); |
| 673 | to->di_gen = cpu_to_be32(from->di_gen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | STATIC uint |
| 677 | _xfs_dic2xflags( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | __uint16_t di_flags) |
| 679 | { |
| 680 | uint flags = 0; |
| 681 | |
| 682 | if (di_flags & XFS_DIFLAG_ANY) { |
| 683 | if (di_flags & XFS_DIFLAG_REALTIME) |
| 684 | flags |= XFS_XFLAG_REALTIME; |
| 685 | if (di_flags & XFS_DIFLAG_PREALLOC) |
| 686 | flags |= XFS_XFLAG_PREALLOC; |
| 687 | if (di_flags & XFS_DIFLAG_IMMUTABLE) |
| 688 | flags |= XFS_XFLAG_IMMUTABLE; |
| 689 | if (di_flags & XFS_DIFLAG_APPEND) |
| 690 | flags |= XFS_XFLAG_APPEND; |
| 691 | if (di_flags & XFS_DIFLAG_SYNC) |
| 692 | flags |= XFS_XFLAG_SYNC; |
| 693 | if (di_flags & XFS_DIFLAG_NOATIME) |
| 694 | flags |= XFS_XFLAG_NOATIME; |
| 695 | if (di_flags & XFS_DIFLAG_NODUMP) |
| 696 | flags |= XFS_XFLAG_NODUMP; |
| 697 | if (di_flags & XFS_DIFLAG_RTINHERIT) |
| 698 | flags |= XFS_XFLAG_RTINHERIT; |
| 699 | if (di_flags & XFS_DIFLAG_PROJINHERIT) |
| 700 | flags |= XFS_XFLAG_PROJINHERIT; |
| 701 | if (di_flags & XFS_DIFLAG_NOSYMLINKS) |
| 702 | flags |= XFS_XFLAG_NOSYMLINKS; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 703 | if (di_flags & XFS_DIFLAG_EXTSIZE) |
| 704 | flags |= XFS_XFLAG_EXTSIZE; |
| 705 | if (di_flags & XFS_DIFLAG_EXTSZINHERIT) |
| 706 | flags |= XFS_XFLAG_EXTSZINHERIT; |
Barry Naujok | d3446ea | 2006-06-09 14:54:19 +1000 | [diff] [blame] | 707 | if (di_flags & XFS_DIFLAG_NODEFRAG) |
| 708 | flags |= XFS_XFLAG_NODEFRAG; |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 709 | if (di_flags & XFS_DIFLAG_FILESTREAM) |
| 710 | flags |= XFS_XFLAG_FILESTREAM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | return flags; |
| 714 | } |
| 715 | |
| 716 | uint |
| 717 | xfs_ip2xflags( |
| 718 | xfs_inode_t *ip) |
| 719 | { |
Christoph Hellwig | 347d1c0 | 2007-08-28 13:57:51 +1000 | [diff] [blame] | 720 | xfs_icdinode_t *dic = &ip->i_d; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | |
Nathan Scott | a916e2b | 2006-06-09 17:12:17 +1000 | [diff] [blame] | 722 | return _xfs_dic2xflags(dic->di_flags) | |
Christoph Hellwig | 45ba598 | 2007-12-07 14:07:20 +1100 | [diff] [blame] | 723 | (XFS_IFORK_Q(ip) ? XFS_XFLAG_HASATTR : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | uint |
| 727 | xfs_dic2xflags( |
Christoph Hellwig | 45ba598 | 2007-12-07 14:07:20 +1100 | [diff] [blame] | 728 | xfs_dinode_t *dip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | { |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 730 | return _xfs_dic2xflags(be16_to_cpu(dip->di_flags)) | |
Christoph Hellwig | 45ba598 | 2007-12-07 14:07:20 +1100 | [diff] [blame] | 731 | (XFS_DFORK_Q(dip) ? XFS_XFLAG_HASATTR : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | /* |
Christoph Hellwig | 24f211b | 2008-11-28 14:23:42 +1100 | [diff] [blame] | 735 | * Read the disk inode attributes into the in-core inode structure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | */ |
| 737 | int |
| 738 | xfs_iread( |
| 739 | xfs_mount_t *mp, |
| 740 | xfs_trans_t *tp, |
Christoph Hellwig | 24f211b | 2008-11-28 14:23:42 +1100 | [diff] [blame] | 741 | xfs_inode_t *ip, |
Christoph Hellwig | 24f211b | 2008-11-28 14:23:42 +1100 | [diff] [blame] | 742 | uint iget_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | { |
| 744 | xfs_buf_t *bp; |
| 745 | xfs_dinode_t *dip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | int error; |
| 747 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | /* |
Christoph Hellwig | 92bfc6e | 2008-11-28 14:23:41 +1100 | [diff] [blame] | 749 | * Fill in the location information in the in-core inode. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | */ |
Christoph Hellwig | 24f211b | 2008-11-28 14:23:42 +1100 | [diff] [blame] | 751 | error = xfs_imap(mp, tp, ip->i_ino, &ip->i_imap, iget_flags); |
Christoph Hellwig | 9ed0451 | 2008-10-30 18:26:04 +1100 | [diff] [blame] | 752 | if (error) |
Christoph Hellwig | 24f211b | 2008-11-28 14:23:42 +1100 | [diff] [blame] | 753 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | |
| 755 | /* |
Christoph Hellwig | 92bfc6e | 2008-11-28 14:23:41 +1100 | [diff] [blame] | 756 | * Get pointers to the on-disk inode and the buffer containing it. |
Christoph Hellwig | 76d8b27 | 2008-11-28 14:23:40 +1100 | [diff] [blame] | 757 | */ |
Christoph Hellwig | 475ee41 | 2012-07-03 12:21:22 -0400 | [diff] [blame^] | 758 | error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &bp, 0, iget_flags); |
Christoph Hellwig | 76d8b27 | 2008-11-28 14:23:40 +1100 | [diff] [blame] | 759 | if (error) |
Christoph Hellwig | 24f211b | 2008-11-28 14:23:42 +1100 | [diff] [blame] | 760 | return error; |
Christoph Hellwig | 76d8b27 | 2008-11-28 14:23:40 +1100 | [diff] [blame] | 761 | |
| 762 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | * If we got something that isn't an inode it means someone |
| 764 | * (nfs or dmi) has a stale handle. |
| 765 | */ |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 766 | if (dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | #ifdef DEBUG |
Dave Chinner | 5348778 | 2011-03-07 10:05:35 +1100 | [diff] [blame] | 768 | xfs_alert(mp, |
| 769 | "%s: dip->di_magic (0x%x) != XFS_DINODE_MAGIC (0x%x)", |
| 770 | __func__, be16_to_cpu(dip->di_magic), XFS_DINODE_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | #endif /* DEBUG */ |
Christoph Hellwig | 9ed0451 | 2008-10-30 18:26:04 +1100 | [diff] [blame] | 772 | error = XFS_ERROR(EINVAL); |
| 773 | goto out_brelse; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | /* |
| 777 | * If the on-disk inode is already linked to a directory |
| 778 | * entry, copy all of the inode into the in-core inode. |
| 779 | * xfs_iformat() handles copying in the inode format |
| 780 | * specific information. |
| 781 | * Otherwise, just get the truly permanent information. |
| 782 | */ |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 783 | if (dip->di_mode) { |
| 784 | xfs_dinode_from_disk(&ip->i_d, dip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | error = xfs_iformat(ip, dip); |
| 786 | if (error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | #ifdef DEBUG |
Dave Chinner | 5348778 | 2011-03-07 10:05:35 +1100 | [diff] [blame] | 788 | xfs_alert(mp, "%s: xfs_iformat() returned error %d", |
| 789 | __func__, error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | #endif /* DEBUG */ |
Christoph Hellwig | 9ed0451 | 2008-10-30 18:26:04 +1100 | [diff] [blame] | 791 | goto out_brelse; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | } |
| 793 | } else { |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 794 | ip->i_d.di_magic = be16_to_cpu(dip->di_magic); |
| 795 | ip->i_d.di_version = dip->di_version; |
| 796 | ip->i_d.di_gen = be32_to_cpu(dip->di_gen); |
| 797 | ip->i_d.di_flushiter = be16_to_cpu(dip->di_flushiter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | /* |
| 799 | * Make sure to pull in the mode here as well in |
| 800 | * case the inode is released without being used. |
| 801 | * This ensures that xfs_inactive() will see that |
| 802 | * the inode is already free and not try to mess |
| 803 | * with the uninitialized part of it. |
| 804 | */ |
| 805 | ip->i_d.di_mode = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | } |
| 807 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | /* |
| 809 | * The inode format changed when we moved the link count and |
| 810 | * made it 32 bits long. If this is an old format inode, |
| 811 | * convert it in memory to look like a new one. If it gets |
| 812 | * flushed to disk we will convert back before flushing or |
| 813 | * logging it. We zero out the new projid field and the old link |
| 814 | * count field. We'll handle clearing the pad field (the remains |
| 815 | * of the old uuid field) when we actually convert the inode to |
| 816 | * the new format. We don't change the version number so that we |
| 817 | * can distinguish this from a real new format inode. |
| 818 | */ |
Christoph Hellwig | 51ce16d | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 819 | if (ip->i_d.di_version == 1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | ip->i_d.di_nlink = ip->i_d.di_onlink; |
| 821 | ip->i_d.di_onlink = 0; |
Arkadiusz Mi?kiewicz | 6743099 | 2010-09-26 06:10:18 +0000 | [diff] [blame] | 822 | xfs_set_projid(ip, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | ip->i_delayed_blks = 0; |
| 826 | |
| 827 | /* |
| 828 | * Mark the buffer containing the inode as something to keep |
| 829 | * around for a while. This helps to keep recently accessed |
| 830 | * meta-data in-core longer. |
| 831 | */ |
Dave Chinner | 821eb21 | 2010-12-02 16:31:13 +1100 | [diff] [blame] | 832 | xfs_buf_set_ref(bp, XFS_INO_REF); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | |
| 834 | /* |
| 835 | * Use xfs_trans_brelse() to release the buffer containing the |
| 836 | * on-disk inode, because it was acquired with xfs_trans_read_buf() |
Christoph Hellwig | 475ee41 | 2012-07-03 12:21:22 -0400 | [diff] [blame^] | 837 | * in xfs_imap_to_bp() above. If tp is NULL, this is just a normal |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | * brelse(). If we're within a transaction, then xfs_trans_brelse() |
| 839 | * will only release the buffer if it is not dirty within the |
| 840 | * transaction. It will be OK to release the buffer in this case, |
| 841 | * because inodes on disk are never destroyed and we will be |
| 842 | * locking the new in-core inode before putting it in the hash |
| 843 | * table where other processes can find it. Thus we don't have |
| 844 | * to worry about the inode being changed just because we released |
| 845 | * the buffer. |
| 846 | */ |
Christoph Hellwig | 9ed0451 | 2008-10-30 18:26:04 +1100 | [diff] [blame] | 847 | out_brelse: |
| 848 | xfs_trans_brelse(tp, bp); |
Christoph Hellwig | 9ed0451 | 2008-10-30 18:26:04 +1100 | [diff] [blame] | 849 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | } |
| 851 | |
| 852 | /* |
| 853 | * Read in extents from a btree-format inode. |
| 854 | * Allocate and fill in if_extents. Real work is done in xfs_bmap.c. |
| 855 | */ |
| 856 | int |
| 857 | xfs_iread_extents( |
| 858 | xfs_trans_t *tp, |
| 859 | xfs_inode_t *ip, |
| 860 | int whichfork) |
| 861 | { |
| 862 | int error; |
| 863 | xfs_ifork_t *ifp; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 864 | xfs_extnum_t nextents; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | |
| 866 | if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) { |
| 867 | XFS_ERROR_REPORT("xfs_iread_extents", XFS_ERRLEVEL_LOW, |
| 868 | ip->i_mount); |
| 869 | return XFS_ERROR(EFSCORRUPTED); |
| 870 | } |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 871 | nextents = XFS_IFORK_NEXTENTS(ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 873 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | /* |
| 875 | * We know that the size is valid (it's checked in iformat_btree) |
| 876 | */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 877 | ifp->if_bytes = ifp->if_real_bytes = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | ifp->if_flags |= XFS_IFEXTENTS; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 879 | xfs_iext_add(ifp, 0, nextents); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | error = xfs_bmap_read_extents(tp, ip, whichfork); |
| 881 | if (error) { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 882 | xfs_iext_destroy(ifp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | ifp->if_flags &= ~XFS_IFEXTENTS; |
| 884 | return error; |
| 885 | } |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 886 | xfs_validate_extents(ifp, nextents, XFS_EXTFMT_INODE(ip)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | return 0; |
| 888 | } |
| 889 | |
| 890 | /* |
| 891 | * Allocate an inode on disk and return a copy of its in-core version. |
| 892 | * The in-core inode is locked exclusively. Set mode, nlink, and rdev |
| 893 | * appropriately within the inode. The uid and gid for the inode are |
| 894 | * set according to the contents of the given cred structure. |
| 895 | * |
| 896 | * Use xfs_dialloc() to allocate the on-disk inode. If xfs_dialloc() |
| 897 | * has a free inode available, call xfs_iget() |
| 898 | * to obtain the in-core version of the allocated inode. Finally, |
| 899 | * fill in the inode and log its initial contents. In this case, |
| 900 | * ialloc_context would be set to NULL and call_again set to false. |
| 901 | * |
| 902 | * If xfs_dialloc() does not have an available inode, |
| 903 | * it will replenish its supply by doing an allocation. Since we can |
| 904 | * only do one allocation within a transaction without deadlocks, we |
| 905 | * must commit the current transaction before returning the inode itself. |
| 906 | * In this case, therefore, we will set call_again to true and return. |
| 907 | * The caller should then commit the current transaction, start a new |
| 908 | * transaction, and call xfs_ialloc() again to actually get the inode. |
| 909 | * |
| 910 | * To ensure that some other process does not grab the inode that |
| 911 | * was allocated during the first call to xfs_ialloc(), this routine |
| 912 | * also returns the [locked] bp pointing to the head of the freelist |
| 913 | * as ialloc_context. The caller should hold this buffer across |
| 914 | * the commit and pass it back into this routine on the second call. |
David Chinner | b11f94d | 2007-07-11 11:09:33 +1000 | [diff] [blame] | 915 | * |
| 916 | * If we are allocating quota inodes, we do not have a parent inode |
| 917 | * to attach to or associate with (i.e. pip == NULL) because they |
| 918 | * are not linked into the directory structure - they are attached |
| 919 | * directly to the superblock - and so have no parent. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | */ |
| 921 | int |
| 922 | xfs_ialloc( |
| 923 | xfs_trans_t *tp, |
| 924 | xfs_inode_t *pip, |
Al Viro | 576b1d6 | 2011-07-26 02:50:15 -0400 | [diff] [blame] | 925 | umode_t mode, |
Nathan Scott | 31b084a | 2005-05-05 13:25:00 -0700 | [diff] [blame] | 926 | xfs_nlink_t nlink, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | xfs_dev_t rdev, |
Arkadiusz Mi?kiewicz | 6743099 | 2010-09-26 06:10:18 +0000 | [diff] [blame] | 928 | prid_t prid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | int okalloc, |
| 930 | xfs_buf_t **ialloc_context, |
| 931 | boolean_t *call_again, |
| 932 | xfs_inode_t **ipp) |
| 933 | { |
| 934 | xfs_ino_t ino; |
| 935 | xfs_inode_t *ip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | uint flags; |
| 937 | int error; |
Christoph Hellwig | dff35fd | 2008-08-13 16:44:15 +1000 | [diff] [blame] | 938 | timespec_t tv; |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 939 | int filestreams = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | |
| 941 | /* |
| 942 | * Call the space management code to pick |
| 943 | * the on-disk inode to be allocated. |
| 944 | */ |
David Chinner | b11f94d | 2007-07-11 11:09:33 +1000 | [diff] [blame] | 945 | error = xfs_dialloc(tp, pip ? pip->i_ino : 0, mode, okalloc, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | ialloc_context, call_again, &ino); |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 947 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | if (*call_again || ino == NULLFSINO) { |
| 950 | *ipp = NULL; |
| 951 | return 0; |
| 952 | } |
| 953 | ASSERT(*ialloc_context == NULL); |
| 954 | |
| 955 | /* |
| 956 | * Get the in-core inode with the lock held exclusively. |
| 957 | * This is because we're setting fields here we need |
| 958 | * to prevent others from looking at until we're done. |
| 959 | */ |
Christoph Hellwig | ec3ba85 | 2011-02-13 13:26:42 +0000 | [diff] [blame] | 960 | error = xfs_iget(tp->t_mountp, tp, ino, XFS_IGET_CREATE, |
| 961 | XFS_ILOCK_EXCL, &ip); |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 962 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | ASSERT(ip != NULL); |
| 965 | |
Al Viro | 576b1d6 | 2011-07-26 02:50:15 -0400 | [diff] [blame] | 966 | ip->i_d.di_mode = mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | ip->i_d.di_onlink = 0; |
| 968 | ip->i_d.di_nlink = nlink; |
| 969 | ASSERT(ip->i_d.di_nlink == nlink); |
David Howells | 9e2b2dc | 2008-08-13 16:20:04 +0100 | [diff] [blame] | 970 | ip->i_d.di_uid = current_fsuid(); |
| 971 | ip->i_d.di_gid = current_fsgid(); |
Arkadiusz Mi?kiewicz | 6743099 | 2010-09-26 06:10:18 +0000 | [diff] [blame] | 972 | xfs_set_projid(ip, prid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad)); |
| 974 | |
| 975 | /* |
| 976 | * If the superblock version is up to where we support new format |
| 977 | * inodes and this is currently an old format inode, then change |
| 978 | * the inode version number now. This way we only do the conversion |
| 979 | * here rather than here and in the flush/logging code. |
| 980 | */ |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 981 | if (xfs_sb_version_hasnlink(&tp->t_mountp->m_sb) && |
Christoph Hellwig | 51ce16d | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 982 | ip->i_d.di_version == 1) { |
| 983 | ip->i_d.di_version = 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | /* |
| 985 | * We've already zeroed the old link count, the projid field, |
| 986 | * and the pad field. |
| 987 | */ |
| 988 | } |
| 989 | |
| 990 | /* |
| 991 | * Project ids won't be stored on disk if we are using a version 1 inode. |
| 992 | */ |
Christoph Hellwig | 51ce16d | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 993 | if ((prid != 0) && (ip->i_d.di_version == 1)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | xfs_bump_ino_vers2(tp, ip); |
| 995 | |
Christoph Hellwig | bd186aa | 2007-08-30 17:21:12 +1000 | [diff] [blame] | 996 | if (pip && XFS_INHERIT_GID(pip)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | ip->i_d.di_gid = pip->i_d.di_gid; |
Al Viro | abbede1 | 2011-07-26 02:31:30 -0400 | [diff] [blame] | 998 | if ((pip->i_d.di_mode & S_ISGID) && S_ISDIR(mode)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | ip->i_d.di_mode |= S_ISGID; |
| 1000 | } |
| 1001 | } |
| 1002 | |
| 1003 | /* |
| 1004 | * If the group ID of the new file does not match the effective group |
| 1005 | * ID or one of the supplementary group IDs, the S_ISGID bit is cleared |
| 1006 | * (and only if the irix_sgid_inherit compatibility variable is set). |
| 1007 | */ |
| 1008 | if ((irix_sgid_inherit) && |
| 1009 | (ip->i_d.di_mode & S_ISGID) && |
| 1010 | (!in_group_p((gid_t)ip->i_d.di_gid))) { |
| 1011 | ip->i_d.di_mode &= ~S_ISGID; |
| 1012 | } |
| 1013 | |
| 1014 | ip->i_d.di_size = 0; |
| 1015 | ip->i_d.di_nextents = 0; |
| 1016 | ASSERT(ip->i_d.di_nblocks == 0); |
Christoph Hellwig | dff35fd | 2008-08-13 16:44:15 +1000 | [diff] [blame] | 1017 | |
| 1018 | nanotime(&tv); |
| 1019 | ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec; |
| 1020 | ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec; |
| 1021 | ip->i_d.di_atime = ip->i_d.di_mtime; |
| 1022 | ip->i_d.di_ctime = ip->i_d.di_mtime; |
| 1023 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | /* |
| 1025 | * di_gen will have been taken care of in xfs_iread. |
| 1026 | */ |
| 1027 | ip->i_d.di_extsize = 0; |
| 1028 | ip->i_d.di_dmevmask = 0; |
| 1029 | ip->i_d.di_dmstate = 0; |
| 1030 | ip->i_d.di_flags = 0; |
| 1031 | flags = XFS_ILOG_CORE; |
| 1032 | switch (mode & S_IFMT) { |
| 1033 | case S_IFIFO: |
| 1034 | case S_IFCHR: |
| 1035 | case S_IFBLK: |
| 1036 | case S_IFSOCK: |
| 1037 | ip->i_d.di_format = XFS_DINODE_FMT_DEV; |
| 1038 | ip->i_df.if_u2.if_rdev = rdev; |
| 1039 | ip->i_df.if_flags = 0; |
| 1040 | flags |= XFS_ILOG_DEV; |
| 1041 | break; |
| 1042 | case S_IFREG: |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 1043 | /* |
| 1044 | * we can't set up filestreams until after the VFS inode |
| 1045 | * is set up properly. |
| 1046 | */ |
| 1047 | if (pip && xfs_inode_is_filestream(pip)) |
| 1048 | filestreams = 1; |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 1049 | /* fall through */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | case S_IFDIR: |
David Chinner | b11f94d | 2007-07-11 11:09:33 +1000 | [diff] [blame] | 1051 | if (pip && (pip->i_d.di_flags & XFS_DIFLAG_ANY)) { |
Nathan Scott | 365ca83 | 2005-06-21 15:39:12 +1000 | [diff] [blame] | 1052 | uint di_flags = 0; |
| 1053 | |
Al Viro | abbede1 | 2011-07-26 02:31:30 -0400 | [diff] [blame] | 1054 | if (S_ISDIR(mode)) { |
Nathan Scott | 365ca83 | 2005-06-21 15:39:12 +1000 | [diff] [blame] | 1055 | if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT) |
| 1056 | di_flags |= XFS_DIFLAG_RTINHERIT; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 1057 | if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) { |
| 1058 | di_flags |= XFS_DIFLAG_EXTSZINHERIT; |
| 1059 | ip->i_d.di_extsize = pip->i_d.di_extsize; |
| 1060 | } |
Al Viro | abbede1 | 2011-07-26 02:31:30 -0400 | [diff] [blame] | 1061 | } else if (S_ISREG(mode)) { |
Christoph Hellwig | 613d704 | 2007-10-11 17:44:08 +1000 | [diff] [blame] | 1062 | if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT) |
Nathan Scott | 365ca83 | 2005-06-21 15:39:12 +1000 | [diff] [blame] | 1063 | di_flags |= XFS_DIFLAG_REALTIME; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 1064 | if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) { |
| 1065 | di_flags |= XFS_DIFLAG_EXTSIZE; |
| 1066 | ip->i_d.di_extsize = pip->i_d.di_extsize; |
| 1067 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | } |
| 1069 | if ((pip->i_d.di_flags & XFS_DIFLAG_NOATIME) && |
| 1070 | xfs_inherit_noatime) |
Nathan Scott | 365ca83 | 2005-06-21 15:39:12 +1000 | [diff] [blame] | 1071 | di_flags |= XFS_DIFLAG_NOATIME; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | if ((pip->i_d.di_flags & XFS_DIFLAG_NODUMP) && |
| 1073 | xfs_inherit_nodump) |
Nathan Scott | 365ca83 | 2005-06-21 15:39:12 +1000 | [diff] [blame] | 1074 | di_flags |= XFS_DIFLAG_NODUMP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | if ((pip->i_d.di_flags & XFS_DIFLAG_SYNC) && |
| 1076 | xfs_inherit_sync) |
Nathan Scott | 365ca83 | 2005-06-21 15:39:12 +1000 | [diff] [blame] | 1077 | di_flags |= XFS_DIFLAG_SYNC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | if ((pip->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) && |
| 1079 | xfs_inherit_nosymlinks) |
Nathan Scott | 365ca83 | 2005-06-21 15:39:12 +1000 | [diff] [blame] | 1080 | di_flags |= XFS_DIFLAG_NOSYMLINKS; |
| 1081 | if (pip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) |
| 1082 | di_flags |= XFS_DIFLAG_PROJINHERIT; |
Barry Naujok | d3446ea | 2006-06-09 14:54:19 +1000 | [diff] [blame] | 1083 | if ((pip->i_d.di_flags & XFS_DIFLAG_NODEFRAG) && |
| 1084 | xfs_inherit_nodefrag) |
| 1085 | di_flags |= XFS_DIFLAG_NODEFRAG; |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 1086 | if (pip->i_d.di_flags & XFS_DIFLAG_FILESTREAM) |
| 1087 | di_flags |= XFS_DIFLAG_FILESTREAM; |
Nathan Scott | 365ca83 | 2005-06-21 15:39:12 +1000 | [diff] [blame] | 1088 | ip->i_d.di_flags |= di_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | } |
| 1090 | /* FALLTHROUGH */ |
| 1091 | case S_IFLNK: |
| 1092 | ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS; |
| 1093 | ip->i_df.if_flags = XFS_IFEXTENTS; |
| 1094 | ip->i_df.if_bytes = ip->i_df.if_real_bytes = 0; |
| 1095 | ip->i_df.if_u1.if_extents = NULL; |
| 1096 | break; |
| 1097 | default: |
| 1098 | ASSERT(0); |
| 1099 | } |
| 1100 | /* |
| 1101 | * Attribute fork settings for new inode. |
| 1102 | */ |
| 1103 | ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS; |
| 1104 | ip->i_d.di_anextents = 0; |
| 1105 | |
| 1106 | /* |
| 1107 | * Log the new values stuffed into the inode. |
| 1108 | */ |
Christoph Hellwig | ddc3415 | 2011-09-19 15:00:54 +0000 | [diff] [blame] | 1109 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | xfs_trans_log_inode(tp, ip, flags); |
| 1111 | |
Nathan Scott | b83bd13 | 2006-06-09 16:48:30 +1000 | [diff] [blame] | 1112 | /* now that we have an i_mode we can setup inode ops and unlock */ |
Christoph Hellwig | 41be8be | 2008-08-13 16:23:13 +1000 | [diff] [blame] | 1113 | xfs_setup_inode(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 1115 | /* now we have set up the vfs inode we can associate the filestream */ |
| 1116 | if (filestreams) { |
| 1117 | error = xfs_filestream_associate(pip, ip); |
| 1118 | if (error < 0) |
| 1119 | return -error; |
| 1120 | if (!error) |
| 1121 | xfs_iflags_set(ip, XFS_IFILESTREAM); |
| 1122 | } |
| 1123 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1124 | *ipp = ip; |
| 1125 | return 0; |
| 1126 | } |
| 1127 | |
| 1128 | /* |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 1129 | * Free up the underlying blocks past new_size. The new size must be smaller |
| 1130 | * than the current size. This routine can be used both for the attribute and |
| 1131 | * data fork, and does not modify the inode size, which is left to the caller. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1132 | * |
David Chinner | f648505 | 2008-04-17 16:50:04 +1000 | [diff] [blame] | 1133 | * The transaction passed to this routine must have made a permanent log |
| 1134 | * reservation of at least XFS_ITRUNCATE_LOG_RES. This routine may commit the |
| 1135 | * given transaction and start new ones, so make sure everything involved in |
| 1136 | * the transaction is tidy before calling here. Some transaction will be |
| 1137 | * returned to the caller to be committed. The incoming transaction must |
| 1138 | * already include the inode, and both inode locks must be held exclusively. |
| 1139 | * The inode must also be "held" within the transaction. On return the inode |
| 1140 | * will be "held" within the returned transaction. This routine does NOT |
| 1141 | * require any disk space to be reserved for it within the transaction. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1142 | * |
David Chinner | f648505 | 2008-04-17 16:50:04 +1000 | [diff] [blame] | 1143 | * If we get an error, we must return with the inode locked and linked into the |
| 1144 | * current transaction. This keeps things simple for the higher level code, |
| 1145 | * because it always knows that the inode is locked and held in the transaction |
| 1146 | * that returns to it whether errors occur or not. We don't mark the inode |
| 1147 | * dirty on error so that transactions can be easily aborted if possible. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | */ |
| 1149 | int |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 1150 | xfs_itruncate_extents( |
| 1151 | struct xfs_trans **tpp, |
| 1152 | struct xfs_inode *ip, |
| 1153 | int whichfork, |
| 1154 | xfs_fsize_t new_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1155 | { |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 1156 | struct xfs_mount *mp = ip->i_mount; |
| 1157 | struct xfs_trans *tp = *tpp; |
| 1158 | struct xfs_trans *ntp; |
| 1159 | xfs_bmap_free_t free_list; |
| 1160 | xfs_fsblock_t first_block; |
| 1161 | xfs_fileoff_t first_unmap_block; |
| 1162 | xfs_fileoff_t last_block; |
| 1163 | xfs_filblks_t unmap_len; |
| 1164 | int committed; |
| 1165 | int error = 0; |
| 1166 | int done = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 1168 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL)); |
Christoph Hellwig | ce7ae151 | 2011-12-18 20:00:11 +0000 | [diff] [blame] | 1169 | ASSERT(new_size <= XFS_ISIZE(ip)); |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 1170 | ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | ASSERT(ip->i_itemp != NULL); |
Christoph Hellwig | 898621d | 2010-06-24 11:36:58 +1000 | [diff] [blame] | 1172 | ASSERT(ip->i_itemp->ili_lock_flags == 0); |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 1173 | ASSERT(!XFS_NOT_DQATTACHED(mp, ip)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | |
Christoph Hellwig | 673e8e5 | 2011-12-18 20:00:04 +0000 | [diff] [blame] | 1175 | trace_xfs_itruncate_extents_start(ip, new_size); |
| 1176 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | /* |
| 1178 | * Since it is possible for space to become allocated beyond |
| 1179 | * the end of the file (in a crash where the space is allocated |
| 1180 | * but the inode size is not yet updated), simply remove any |
| 1181 | * blocks which show up between the new EOF and the maximum |
| 1182 | * possible file size. If the first block to be removed is |
| 1183 | * beyond the maximum file size (ie it is the same as last_block), |
| 1184 | * then there is nothing to do. |
| 1185 | */ |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 1186 | first_unmap_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size); |
Dave Chinner | 3297238 | 2012-06-08 15:44:54 +1000 | [diff] [blame] | 1187 | last_block = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes); |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 1188 | if (first_unmap_block == last_block) |
| 1189 | return 0; |
| 1190 | |
| 1191 | ASSERT(first_unmap_block < last_block); |
| 1192 | unmap_len = last_block - first_unmap_block + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1193 | while (!done) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1194 | xfs_bmap_init(&free_list, &first_block); |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 1195 | error = xfs_bunmapi(tp, ip, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1196 | first_unmap_block, unmap_len, |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 1197 | xfs_bmapi_aflag(whichfork), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1198 | XFS_ITRUNC_MAX_EXTENTS, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1199 | &first_block, &free_list, |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 1200 | &done); |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 1201 | if (error) |
| 1202 | goto out_bmap_cancel; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1203 | |
| 1204 | /* |
| 1205 | * Duplicate the transaction that has the permanent |
| 1206 | * reservation and commit the old transaction. |
| 1207 | */ |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 1208 | error = xfs_bmap_finish(&tp, &free_list, &committed); |
Christoph Hellwig | 898621d | 2010-06-24 11:36:58 +1000 | [diff] [blame] | 1209 | if (committed) |
Christoph Hellwig | ddc3415 | 2011-09-19 15:00:54 +0000 | [diff] [blame] | 1210 | xfs_trans_ijoin(tp, ip, 0); |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 1211 | if (error) |
| 1212 | goto out_bmap_cancel; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1213 | |
| 1214 | if (committed) { |
| 1215 | /* |
David Chinner | f648505 | 2008-04-17 16:50:04 +1000 | [diff] [blame] | 1216 | * Mark the inode dirty so it will be logged and |
David Chinner | e5720ee | 2008-04-10 12:21:18 +1000 | [diff] [blame] | 1217 | * moved forward in the log as part of every commit. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1218 | */ |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 1219 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1220 | } |
David Chinner | f648505 | 2008-04-17 16:50:04 +1000 | [diff] [blame] | 1221 | |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 1222 | ntp = xfs_trans_dup(tp); |
| 1223 | error = xfs_trans_commit(tp, 0); |
| 1224 | tp = ntp; |
David Chinner | e5720ee | 2008-04-10 12:21:18 +1000 | [diff] [blame] | 1225 | |
Christoph Hellwig | ddc3415 | 2011-09-19 15:00:54 +0000 | [diff] [blame] | 1226 | xfs_trans_ijoin(tp, ip, 0); |
David Chinner | f648505 | 2008-04-17 16:50:04 +1000 | [diff] [blame] | 1227 | |
Dave Chinner | cc09c0d | 2008-11-17 17:37:10 +1100 | [diff] [blame] | 1228 | if (error) |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 1229 | goto out; |
| 1230 | |
Dave Chinner | cc09c0d | 2008-11-17 17:37:10 +1100 | [diff] [blame] | 1231 | /* |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 1232 | * Transaction commit worked ok so we can drop the extra ticket |
Dave Chinner | cc09c0d | 2008-11-17 17:37:10 +1100 | [diff] [blame] | 1233 | * reference that we gained in xfs_trans_dup() |
| 1234 | */ |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 1235 | xfs_log_ticket_put(tp->t_ticket); |
| 1236 | error = xfs_trans_reserve(tp, 0, |
David Chinner | f648505 | 2008-04-17 16:50:04 +1000 | [diff] [blame] | 1237 | XFS_ITRUNCATE_LOG_RES(mp), 0, |
| 1238 | XFS_TRANS_PERM_LOG_RES, |
| 1239 | XFS_ITRUNCATE_LOG_COUNT); |
| 1240 | if (error) |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 1241 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | } |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 1243 | |
Christoph Hellwig | 673e8e5 | 2011-12-18 20:00:04 +0000 | [diff] [blame] | 1244 | /* |
| 1245 | * Always re-log the inode so that our permanent transaction can keep |
| 1246 | * on rolling it forward in the log. |
| 1247 | */ |
| 1248 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 1249 | |
| 1250 | trace_xfs_itruncate_extents_end(ip, new_size); |
| 1251 | |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 1252 | out: |
| 1253 | *tpp = tp; |
| 1254 | return error; |
| 1255 | out_bmap_cancel: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | /* |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 1257 | * If the bunmapi call encounters an error, return to the caller where |
| 1258 | * the transaction can be properly aborted. We just need to make sure |
| 1259 | * we're not holding any resources that we were not when we came in. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1260 | */ |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 1261 | xfs_bmap_cancel(&free_list); |
| 1262 | goto out; |
| 1263 | } |
| 1264 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | /* |
| 1266 | * This is called when the inode's link count goes to 0. |
| 1267 | * We place the on-disk inode on a list in the AGI. It |
| 1268 | * will be pulled from this list when the inode is freed. |
| 1269 | */ |
| 1270 | int |
| 1271 | xfs_iunlink( |
| 1272 | xfs_trans_t *tp, |
| 1273 | xfs_inode_t *ip) |
| 1274 | { |
| 1275 | xfs_mount_t *mp; |
| 1276 | xfs_agi_t *agi; |
| 1277 | xfs_dinode_t *dip; |
| 1278 | xfs_buf_t *agibp; |
| 1279 | xfs_buf_t *ibp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1280 | xfs_agino_t agino; |
| 1281 | short bucket_index; |
| 1282 | int offset; |
| 1283 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1284 | |
| 1285 | ASSERT(ip->i_d.di_nlink == 0); |
| 1286 | ASSERT(ip->i_d.di_mode != 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | |
| 1288 | mp = tp->t_mountp; |
| 1289 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1290 | /* |
| 1291 | * Get the agi buffer first. It ensures lock ordering |
| 1292 | * on the list. |
| 1293 | */ |
Christoph Hellwig | 5e1be0f | 2008-11-28 14:23:37 +1100 | [diff] [blame] | 1294 | error = xfs_read_agi(mp, tp, XFS_INO_TO_AGNO(mp, ip->i_ino), &agibp); |
Vlad Apostolov | 859d718 | 2007-10-11 17:44:18 +1000 | [diff] [blame] | 1295 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1297 | agi = XFS_BUF_TO_AGI(agibp); |
Christoph Hellwig | 5e1be0f | 2008-11-28 14:23:37 +1100 | [diff] [blame] | 1298 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1299 | /* |
| 1300 | * Get the index into the agi hash table for the |
| 1301 | * list this inode will go on. |
| 1302 | */ |
| 1303 | agino = XFS_INO_TO_AGINO(mp, ip->i_ino); |
| 1304 | ASSERT(agino != 0); |
| 1305 | bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS; |
| 1306 | ASSERT(agi->agi_unlinked[bucket_index]); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1307 | ASSERT(be32_to_cpu(agi->agi_unlinked[bucket_index]) != agino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1308 | |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 1309 | if (agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1310 | /* |
| 1311 | * There is already another inode in the bucket we need |
| 1312 | * to add ourselves to. Add us at the front of the list. |
| 1313 | * Here we put the head pointer into our next pointer, |
| 1314 | * and then we fall through to point the head at us. |
| 1315 | */ |
Christoph Hellwig | 475ee41 | 2012-07-03 12:21:22 -0400 | [diff] [blame^] | 1316 | error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp, |
| 1317 | 0, 0); |
Vlad Apostolov | c319b58 | 2007-11-23 16:27:51 +1100 | [diff] [blame] | 1318 | if (error) |
| 1319 | return error; |
| 1320 | |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 1321 | ASSERT(dip->di_next_unlinked == cpu_to_be32(NULLAGINO)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | dip->di_next_unlinked = agi->agi_unlinked[bucket_index]; |
Christoph Hellwig | 92bfc6e | 2008-11-28 14:23:41 +1100 | [diff] [blame] | 1323 | offset = ip->i_imap.im_boffset + |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1324 | offsetof(xfs_dinode_t, di_next_unlinked); |
| 1325 | xfs_trans_inode_buf(tp, ibp); |
| 1326 | xfs_trans_log_buf(tp, ibp, offset, |
| 1327 | (offset + sizeof(xfs_agino_t) - 1)); |
| 1328 | xfs_inobp_check(mp, ibp); |
| 1329 | } |
| 1330 | |
| 1331 | /* |
| 1332 | * Point the bucket head pointer at the inode being inserted. |
| 1333 | */ |
| 1334 | ASSERT(agino != 0); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1335 | agi->agi_unlinked[bucket_index] = cpu_to_be32(agino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1336 | offset = offsetof(xfs_agi_t, agi_unlinked) + |
| 1337 | (sizeof(xfs_agino_t) * bucket_index); |
| 1338 | xfs_trans_log_buf(tp, agibp, offset, |
| 1339 | (offset + sizeof(xfs_agino_t) - 1)); |
| 1340 | return 0; |
| 1341 | } |
| 1342 | |
| 1343 | /* |
| 1344 | * Pull the on-disk inode from the AGI unlinked list. |
| 1345 | */ |
| 1346 | STATIC int |
| 1347 | xfs_iunlink_remove( |
| 1348 | xfs_trans_t *tp, |
| 1349 | xfs_inode_t *ip) |
| 1350 | { |
| 1351 | xfs_ino_t next_ino; |
| 1352 | xfs_mount_t *mp; |
| 1353 | xfs_agi_t *agi; |
| 1354 | xfs_dinode_t *dip; |
| 1355 | xfs_buf_t *agibp; |
| 1356 | xfs_buf_t *ibp; |
| 1357 | xfs_agnumber_t agno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1358 | xfs_agino_t agino; |
| 1359 | xfs_agino_t next_agino; |
| 1360 | xfs_buf_t *last_ibp; |
Nathan Scott | 6fdf8cc | 2006-06-28 10:13:52 +1000 | [diff] [blame] | 1361 | xfs_dinode_t *last_dip = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1362 | short bucket_index; |
Nathan Scott | 6fdf8cc | 2006-06-28 10:13:52 +1000 | [diff] [blame] | 1363 | int offset, last_offset = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1364 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1365 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1366 | mp = tp->t_mountp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1367 | agno = XFS_INO_TO_AGNO(mp, ip->i_ino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1368 | |
| 1369 | /* |
| 1370 | * Get the agi buffer first. It ensures lock ordering |
| 1371 | * on the list. |
| 1372 | */ |
Christoph Hellwig | 5e1be0f | 2008-11-28 14:23:37 +1100 | [diff] [blame] | 1373 | error = xfs_read_agi(mp, tp, agno, &agibp); |
| 1374 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | return error; |
Christoph Hellwig | 5e1be0f | 2008-11-28 14:23:37 +1100 | [diff] [blame] | 1376 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1377 | agi = XFS_BUF_TO_AGI(agibp); |
Christoph Hellwig | 5e1be0f | 2008-11-28 14:23:37 +1100 | [diff] [blame] | 1378 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | /* |
| 1380 | * Get the index into the agi hash table for the |
| 1381 | * list this inode will go on. |
| 1382 | */ |
| 1383 | agino = XFS_INO_TO_AGINO(mp, ip->i_ino); |
| 1384 | ASSERT(agino != 0); |
| 1385 | bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS; |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 1386 | ASSERT(agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1387 | ASSERT(agi->agi_unlinked[bucket_index]); |
| 1388 | |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1389 | if (be32_to_cpu(agi->agi_unlinked[bucket_index]) == agino) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1390 | /* |
Christoph Hellwig | 475ee41 | 2012-07-03 12:21:22 -0400 | [diff] [blame^] | 1391 | * We're at the head of the list. Get the inode's on-disk |
| 1392 | * buffer to see if there is anyone after us on the list. |
| 1393 | * Only modify our next pointer if it is not already NULLAGINO. |
| 1394 | * This saves us the overhead of dealing with the buffer when |
| 1395 | * there is no need to change it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1396 | */ |
Christoph Hellwig | 475ee41 | 2012-07-03 12:21:22 -0400 | [diff] [blame^] | 1397 | error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp, |
| 1398 | 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1399 | if (error) { |
Christoph Hellwig | 475ee41 | 2012-07-03 12:21:22 -0400 | [diff] [blame^] | 1400 | xfs_warn(mp, "%s: xfs_imap_to_bp returned error %d.", |
Dave Chinner | 0b932cc | 2011-03-07 10:08:35 +1100 | [diff] [blame] | 1401 | __func__, error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | return error; |
| 1403 | } |
Christoph Hellwig | 347d1c0 | 2007-08-28 13:57:51 +1000 | [diff] [blame] | 1404 | next_agino = be32_to_cpu(dip->di_next_unlinked); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1405 | ASSERT(next_agino != 0); |
| 1406 | if (next_agino != NULLAGINO) { |
Christoph Hellwig | 347d1c0 | 2007-08-28 13:57:51 +1000 | [diff] [blame] | 1407 | dip->di_next_unlinked = cpu_to_be32(NULLAGINO); |
Christoph Hellwig | 92bfc6e | 2008-11-28 14:23:41 +1100 | [diff] [blame] | 1408 | offset = ip->i_imap.im_boffset + |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | offsetof(xfs_dinode_t, di_next_unlinked); |
| 1410 | xfs_trans_inode_buf(tp, ibp); |
| 1411 | xfs_trans_log_buf(tp, ibp, offset, |
| 1412 | (offset + sizeof(xfs_agino_t) - 1)); |
| 1413 | xfs_inobp_check(mp, ibp); |
| 1414 | } else { |
| 1415 | xfs_trans_brelse(tp, ibp); |
| 1416 | } |
| 1417 | /* |
| 1418 | * Point the bucket head pointer at the next inode. |
| 1419 | */ |
| 1420 | ASSERT(next_agino != 0); |
| 1421 | ASSERT(next_agino != agino); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1422 | agi->agi_unlinked[bucket_index] = cpu_to_be32(next_agino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1423 | offset = offsetof(xfs_agi_t, agi_unlinked) + |
| 1424 | (sizeof(xfs_agino_t) * bucket_index); |
| 1425 | xfs_trans_log_buf(tp, agibp, offset, |
| 1426 | (offset + sizeof(xfs_agino_t) - 1)); |
| 1427 | } else { |
| 1428 | /* |
| 1429 | * We need to search the list for the inode being freed. |
| 1430 | */ |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1431 | next_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1432 | last_ibp = NULL; |
| 1433 | while (next_agino != agino) { |
| 1434 | /* |
| 1435 | * If the last inode wasn't the one pointing to |
| 1436 | * us, then release its buffer since we're not |
| 1437 | * going to do anything with it. |
| 1438 | */ |
| 1439 | if (last_ibp != NULL) { |
| 1440 | xfs_trans_brelse(tp, last_ibp); |
| 1441 | } |
| 1442 | next_ino = XFS_AGINO_TO_INO(mp, agno, next_agino); |
| 1443 | error = xfs_inotobp(mp, tp, next_ino, &last_dip, |
Christoph Hellwig | c679eef | 2008-10-30 18:04:13 +1100 | [diff] [blame] | 1444 | &last_ibp, &last_offset, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1445 | if (error) { |
Dave Chinner | 0b932cc | 2011-03-07 10:08:35 +1100 | [diff] [blame] | 1446 | xfs_warn(mp, |
| 1447 | "%s: xfs_inotobp() returned error %d.", |
| 1448 | __func__, error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1449 | return error; |
| 1450 | } |
Christoph Hellwig | 347d1c0 | 2007-08-28 13:57:51 +1000 | [diff] [blame] | 1451 | next_agino = be32_to_cpu(last_dip->di_next_unlinked); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1452 | ASSERT(next_agino != NULLAGINO); |
| 1453 | ASSERT(next_agino != 0); |
| 1454 | } |
Christoph Hellwig | 475ee41 | 2012-07-03 12:21:22 -0400 | [diff] [blame^] | 1455 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1456 | /* |
Christoph Hellwig | 475ee41 | 2012-07-03 12:21:22 -0400 | [diff] [blame^] | 1457 | * Now last_ibp points to the buffer previous to us on the |
| 1458 | * unlinked list. Pull us from the list. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1459 | */ |
Christoph Hellwig | 475ee41 | 2012-07-03 12:21:22 -0400 | [diff] [blame^] | 1460 | error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp, |
| 1461 | 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1462 | if (error) { |
Christoph Hellwig | 475ee41 | 2012-07-03 12:21:22 -0400 | [diff] [blame^] | 1463 | xfs_warn(mp, "%s: xfs_imap_to_bp(2) returned error %d.", |
Dave Chinner | 0b932cc | 2011-03-07 10:08:35 +1100 | [diff] [blame] | 1464 | __func__, error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1465 | return error; |
| 1466 | } |
Christoph Hellwig | 347d1c0 | 2007-08-28 13:57:51 +1000 | [diff] [blame] | 1467 | next_agino = be32_to_cpu(dip->di_next_unlinked); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1468 | ASSERT(next_agino != 0); |
| 1469 | ASSERT(next_agino != agino); |
| 1470 | if (next_agino != NULLAGINO) { |
Christoph Hellwig | 347d1c0 | 2007-08-28 13:57:51 +1000 | [diff] [blame] | 1471 | dip->di_next_unlinked = cpu_to_be32(NULLAGINO); |
Christoph Hellwig | 92bfc6e | 2008-11-28 14:23:41 +1100 | [diff] [blame] | 1472 | offset = ip->i_imap.im_boffset + |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1473 | offsetof(xfs_dinode_t, di_next_unlinked); |
| 1474 | xfs_trans_inode_buf(tp, ibp); |
| 1475 | xfs_trans_log_buf(tp, ibp, offset, |
| 1476 | (offset + sizeof(xfs_agino_t) - 1)); |
| 1477 | xfs_inobp_check(mp, ibp); |
| 1478 | } else { |
| 1479 | xfs_trans_brelse(tp, ibp); |
| 1480 | } |
| 1481 | /* |
| 1482 | * Point the previous inode on the list to the next inode. |
| 1483 | */ |
Christoph Hellwig | 347d1c0 | 2007-08-28 13:57:51 +1000 | [diff] [blame] | 1484 | last_dip->di_next_unlinked = cpu_to_be32(next_agino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 | ASSERT(next_agino != 0); |
| 1486 | offset = last_offset + offsetof(xfs_dinode_t, di_next_unlinked); |
| 1487 | xfs_trans_inode_buf(tp, last_ibp); |
| 1488 | xfs_trans_log_buf(tp, last_ibp, offset, |
| 1489 | (offset + sizeof(xfs_agino_t) - 1)); |
| 1490 | xfs_inobp_check(mp, last_ibp); |
| 1491 | } |
| 1492 | return 0; |
| 1493 | } |
| 1494 | |
Dave Chinner | 5b3eed7 | 2010-08-24 11:42:41 +1000 | [diff] [blame] | 1495 | /* |
| 1496 | * A big issue when freeing the inode cluster is is that we _cannot_ skip any |
| 1497 | * inodes that are in memory - they all must be marked stale and attached to |
| 1498 | * the cluster buffer. |
| 1499 | */ |
Chandra Seetharaman | 2a30f36d | 2011-09-20 13:56:55 +0000 | [diff] [blame] | 1500 | STATIC int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1501 | xfs_ifree_cluster( |
| 1502 | xfs_inode_t *free_ip, |
| 1503 | xfs_trans_t *tp, |
| 1504 | xfs_ino_t inum) |
| 1505 | { |
| 1506 | xfs_mount_t *mp = free_ip->i_mount; |
| 1507 | int blks_per_cluster; |
| 1508 | int nbufs; |
| 1509 | int ninodes; |
Dave Chinner | 5b257b4 | 2010-06-03 16:22:29 +1000 | [diff] [blame] | 1510 | int i, j; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1511 | xfs_daddr_t blkno; |
| 1512 | xfs_buf_t *bp; |
Dave Chinner | 5b257b4 | 2010-06-03 16:22:29 +1000 | [diff] [blame] | 1513 | xfs_inode_t *ip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1514 | xfs_inode_log_item_t *iip; |
| 1515 | xfs_log_item_t *lip; |
Dave Chinner | 5017e97 | 2010-01-11 11:47:40 +0000 | [diff] [blame] | 1516 | struct xfs_perag *pag; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1517 | |
Dave Chinner | 5017e97 | 2010-01-11 11:47:40 +0000 | [diff] [blame] | 1518 | pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, inum)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1519 | if (mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) { |
| 1520 | blks_per_cluster = 1; |
| 1521 | ninodes = mp->m_sb.sb_inopblock; |
| 1522 | nbufs = XFS_IALLOC_BLOCKS(mp); |
| 1523 | } else { |
| 1524 | blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) / |
| 1525 | mp->m_sb.sb_blocksize; |
| 1526 | ninodes = blks_per_cluster * mp->m_sb.sb_inopblock; |
| 1527 | nbufs = XFS_IALLOC_BLOCKS(mp) / blks_per_cluster; |
| 1528 | } |
| 1529 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1530 | for (j = 0; j < nbufs; j++, inum += ninodes) { |
| 1531 | blkno = XFS_AGB_TO_DADDR(mp, XFS_INO_TO_AGNO(mp, inum), |
| 1532 | XFS_INO_TO_AGBNO(mp, inum)); |
| 1533 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1534 | /* |
Dave Chinner | 5b257b4 | 2010-06-03 16:22:29 +1000 | [diff] [blame] | 1535 | * We obtain and lock the backing buffer first in the process |
| 1536 | * here, as we have to ensure that any dirty inode that we |
| 1537 | * can't get the flush lock on is attached to the buffer. |
| 1538 | * If we scan the in-memory inodes first, then buffer IO can |
| 1539 | * complete before we get a lock on it, and hence we may fail |
| 1540 | * to mark all the active inodes on the buffer stale. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1541 | */ |
Dave Chinner | 5b257b4 | 2010-06-03 16:22:29 +1000 | [diff] [blame] | 1542 | bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno, |
Dave Chinner | a8acad7 | 2012-04-23 15:58:54 +1000 | [diff] [blame] | 1543 | mp->m_bsize * blks_per_cluster, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1544 | |
Chandra Seetharaman | 2a30f36d | 2011-09-20 13:56:55 +0000 | [diff] [blame] | 1545 | if (!bp) |
| 1546 | return ENOMEM; |
Dave Chinner | 5b257b4 | 2010-06-03 16:22:29 +1000 | [diff] [blame] | 1547 | /* |
| 1548 | * Walk the inodes already attached to the buffer and mark them |
| 1549 | * stale. These will all have the flush locks held, so an |
Dave Chinner | 5b3eed7 | 2010-08-24 11:42:41 +1000 | [diff] [blame] | 1550 | * in-memory inode walk can't lock them. By marking them all |
| 1551 | * stale first, we will not attempt to lock them in the loop |
| 1552 | * below as the XFS_ISTALE flag will be set. |
Dave Chinner | 5b257b4 | 2010-06-03 16:22:29 +1000 | [diff] [blame] | 1553 | */ |
Christoph Hellwig | adadbee | 2011-07-13 13:43:49 +0200 | [diff] [blame] | 1554 | lip = bp->b_fspriv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1555 | while (lip) { |
| 1556 | if (lip->li_type == XFS_LI_INODE) { |
| 1557 | iip = (xfs_inode_log_item_t *)lip; |
| 1558 | ASSERT(iip->ili_logged == 1); |
Christoph Hellwig | ca30b2a | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 1559 | lip->li_cb = xfs_istale_done; |
David Chinner | 7b2e2a3 | 2008-10-30 17:39:12 +1100 | [diff] [blame] | 1560 | xfs_trans_ail_copy_lsn(mp->m_ail, |
| 1561 | &iip->ili_flush_lsn, |
| 1562 | &iip->ili_item.li_lsn); |
David Chinner | e5ffd2b | 2006-11-21 18:55:33 +1100 | [diff] [blame] | 1563 | xfs_iflags_set(iip->ili_inode, XFS_ISTALE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1564 | } |
| 1565 | lip = lip->li_bio_list; |
| 1566 | } |
| 1567 | |
Dave Chinner | 5b3eed7 | 2010-08-24 11:42:41 +1000 | [diff] [blame] | 1568 | |
Dave Chinner | 5b257b4 | 2010-06-03 16:22:29 +1000 | [diff] [blame] | 1569 | /* |
| 1570 | * For each inode in memory attempt to add it to the inode |
| 1571 | * buffer and set it up for being staled on buffer IO |
| 1572 | * completion. This is safe as we've locked out tail pushing |
| 1573 | * and flushing by locking the buffer. |
| 1574 | * |
| 1575 | * We have already marked every inode that was part of a |
| 1576 | * transaction stale above, which means there is no point in |
| 1577 | * even trying to lock them. |
| 1578 | */ |
| 1579 | for (i = 0; i < ninodes; i++) { |
Dave Chinner | 5b3eed7 | 2010-08-24 11:42:41 +1000 | [diff] [blame] | 1580 | retry: |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 1581 | rcu_read_lock(); |
Dave Chinner | 5b257b4 | 2010-06-03 16:22:29 +1000 | [diff] [blame] | 1582 | ip = radix_tree_lookup(&pag->pag_ici_root, |
| 1583 | XFS_INO_TO_AGINO(mp, (inum + i))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1584 | |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 1585 | /* Inode not in memory, nothing to do */ |
| 1586 | if (!ip) { |
| 1587 | rcu_read_unlock(); |
Dave Chinner | 5b257b4 | 2010-06-03 16:22:29 +1000 | [diff] [blame] | 1588 | continue; |
| 1589 | } |
| 1590 | |
Dave Chinner | 5b3eed7 | 2010-08-24 11:42:41 +1000 | [diff] [blame] | 1591 | /* |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 1592 | * because this is an RCU protected lookup, we could |
| 1593 | * find a recently freed or even reallocated inode |
| 1594 | * during the lookup. We need to check under the |
| 1595 | * i_flags_lock for a valid inode here. Skip it if it |
| 1596 | * is not valid, the wrong inode or stale. |
| 1597 | */ |
| 1598 | spin_lock(&ip->i_flags_lock); |
| 1599 | if (ip->i_ino != inum + i || |
| 1600 | __xfs_iflags_test(ip, XFS_ISTALE)) { |
| 1601 | spin_unlock(&ip->i_flags_lock); |
| 1602 | rcu_read_unlock(); |
| 1603 | continue; |
| 1604 | } |
| 1605 | spin_unlock(&ip->i_flags_lock); |
| 1606 | |
| 1607 | /* |
Dave Chinner | 5b3eed7 | 2010-08-24 11:42:41 +1000 | [diff] [blame] | 1608 | * Don't try to lock/unlock the current inode, but we |
| 1609 | * _cannot_ skip the other inodes that we did not find |
| 1610 | * in the list attached to the buffer and are not |
| 1611 | * already marked stale. If we can't lock it, back off |
| 1612 | * and retry. |
| 1613 | */ |
Dave Chinner | 5b257b4 | 2010-06-03 16:22:29 +1000 | [diff] [blame] | 1614 | if (ip != free_ip && |
| 1615 | !xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) { |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 1616 | rcu_read_unlock(); |
Dave Chinner | 5b3eed7 | 2010-08-24 11:42:41 +1000 | [diff] [blame] | 1617 | delay(1); |
| 1618 | goto retry; |
Dave Chinner | 5b257b4 | 2010-06-03 16:22:29 +1000 | [diff] [blame] | 1619 | } |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 1620 | rcu_read_unlock(); |
Dave Chinner | 5b257b4 | 2010-06-03 16:22:29 +1000 | [diff] [blame] | 1621 | |
Dave Chinner | 5b3eed7 | 2010-08-24 11:42:41 +1000 | [diff] [blame] | 1622 | xfs_iflock(ip); |
Dave Chinner | 5b257b4 | 2010-06-03 16:22:29 +1000 | [diff] [blame] | 1623 | xfs_iflags_set(ip, XFS_ISTALE); |
Dave Chinner | 5b257b4 | 2010-06-03 16:22:29 +1000 | [diff] [blame] | 1624 | |
Dave Chinner | 5b3eed7 | 2010-08-24 11:42:41 +1000 | [diff] [blame] | 1625 | /* |
| 1626 | * we don't need to attach clean inodes or those only |
| 1627 | * with unlogged changes (which we throw away, anyway). |
| 1628 | */ |
Dave Chinner | 5b257b4 | 2010-06-03 16:22:29 +1000 | [diff] [blame] | 1629 | iip = ip->i_itemp; |
Dave Chinner | 5b3eed7 | 2010-08-24 11:42:41 +1000 | [diff] [blame] | 1630 | if (!iip || xfs_inode_clean(ip)) { |
Dave Chinner | 5b257b4 | 2010-06-03 16:22:29 +1000 | [diff] [blame] | 1631 | ASSERT(ip != free_ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1632 | xfs_ifunlock(ip); |
| 1633 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 1634 | continue; |
| 1635 | } |
| 1636 | |
Christoph Hellwig | f5d8d5c | 2012-02-29 09:53:54 +0000 | [diff] [blame] | 1637 | iip->ili_last_fields = iip->ili_fields; |
| 1638 | iip->ili_fields = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1639 | iip->ili_logged = 1; |
David Chinner | 7b2e2a3 | 2008-10-30 17:39:12 +1100 | [diff] [blame] | 1640 | xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn, |
| 1641 | &iip->ili_item.li_lsn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1642 | |
Christoph Hellwig | ca30b2a | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 1643 | xfs_buf_attach_iodone(bp, xfs_istale_done, |
| 1644 | &iip->ili_item); |
Dave Chinner | 5b257b4 | 2010-06-03 16:22:29 +1000 | [diff] [blame] | 1645 | |
| 1646 | if (ip != free_ip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1647 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1648 | } |
| 1649 | |
Dave Chinner | 5b3eed7 | 2010-08-24 11:42:41 +1000 | [diff] [blame] | 1650 | xfs_trans_stale_inode_buf(tp, bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1651 | xfs_trans_binval(tp, bp); |
| 1652 | } |
| 1653 | |
Dave Chinner | 5017e97 | 2010-01-11 11:47:40 +0000 | [diff] [blame] | 1654 | xfs_perag_put(pag); |
Chandra Seetharaman | 2a30f36d | 2011-09-20 13:56:55 +0000 | [diff] [blame] | 1655 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1656 | } |
| 1657 | |
| 1658 | /* |
| 1659 | * This is called to return an inode to the inode free list. |
| 1660 | * The inode should already be truncated to 0 length and have |
| 1661 | * no pages associated with it. This routine also assumes that |
| 1662 | * the inode is already a part of the transaction. |
| 1663 | * |
| 1664 | * The on-disk copy of the inode will have been added to the list |
| 1665 | * of unlinked inodes in the AGI. We need to remove the inode from |
| 1666 | * that list atomically with respect to freeing it here. |
| 1667 | */ |
| 1668 | int |
| 1669 | xfs_ifree( |
| 1670 | xfs_trans_t *tp, |
| 1671 | xfs_inode_t *ip, |
| 1672 | xfs_bmap_free_t *flist) |
| 1673 | { |
| 1674 | int error; |
| 1675 | int delete; |
| 1676 | xfs_ino_t first_ino; |
Vlad Apostolov | c319b58 | 2007-11-23 16:27:51 +1100 | [diff] [blame] | 1677 | xfs_dinode_t *dip; |
| 1678 | xfs_buf_t *ibp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1679 | |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 1680 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1681 | ASSERT(ip->i_d.di_nlink == 0); |
| 1682 | ASSERT(ip->i_d.di_nextents == 0); |
| 1683 | ASSERT(ip->i_d.di_anextents == 0); |
Christoph Hellwig | ce7ae151 | 2011-12-18 20:00:11 +0000 | [diff] [blame] | 1684 | ASSERT(ip->i_d.di_size == 0 || !S_ISREG(ip->i_d.di_mode)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1685 | ASSERT(ip->i_d.di_nblocks == 0); |
| 1686 | |
| 1687 | /* |
| 1688 | * Pull the on-disk inode from the AGI unlinked list. |
| 1689 | */ |
| 1690 | error = xfs_iunlink_remove(tp, ip); |
| 1691 | if (error != 0) { |
| 1692 | return error; |
| 1693 | } |
| 1694 | |
| 1695 | error = xfs_difree(tp, ip->i_ino, flist, &delete, &first_ino); |
| 1696 | if (error != 0) { |
| 1697 | return error; |
| 1698 | } |
| 1699 | ip->i_d.di_mode = 0; /* mark incore inode as free */ |
| 1700 | ip->i_d.di_flags = 0; |
| 1701 | ip->i_d.di_dmevmask = 0; |
| 1702 | ip->i_d.di_forkoff = 0; /* mark the attr fork not in use */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1703 | ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS; |
| 1704 | ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS; |
| 1705 | /* |
| 1706 | * Bump the generation count so no one will be confused |
| 1707 | * by reincarnations of this inode. |
| 1708 | */ |
| 1709 | ip->i_d.di_gen++; |
Vlad Apostolov | c319b58 | 2007-11-23 16:27:51 +1100 | [diff] [blame] | 1710 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1711 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 1712 | |
Christoph Hellwig | 475ee41 | 2012-07-03 12:21:22 -0400 | [diff] [blame^] | 1713 | error = xfs_imap_to_bp(ip->i_mount, tp, &ip->i_imap, &dip, &ibp, |
| 1714 | 0, 0); |
Vlad Apostolov | c319b58 | 2007-11-23 16:27:51 +1100 | [diff] [blame] | 1715 | if (error) |
| 1716 | return error; |
| 1717 | |
| 1718 | /* |
| 1719 | * Clear the on-disk di_mode. This is to prevent xfs_bulkstat |
| 1720 | * from picking up this inode when it is reclaimed (its incore state |
| 1721 | * initialzed but not flushed to disk yet). The in-core di_mode is |
| 1722 | * already cleared and a corresponding transaction logged. |
| 1723 | * The hack here just synchronizes the in-core to on-disk |
| 1724 | * di_mode value in advance before the actual inode sync to disk. |
| 1725 | * This is OK because the inode is already unlinked and would never |
| 1726 | * change its di_mode again for this inode generation. |
| 1727 | * This is a temporary hack that would require a proper fix |
| 1728 | * in the future. |
| 1729 | */ |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 1730 | dip->di_mode = 0; |
Vlad Apostolov | c319b58 | 2007-11-23 16:27:51 +1100 | [diff] [blame] | 1731 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1732 | if (delete) { |
Chandra Seetharaman | 2a30f36d | 2011-09-20 13:56:55 +0000 | [diff] [blame] | 1733 | error = xfs_ifree_cluster(ip, tp, first_ino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1734 | } |
| 1735 | |
Chandra Seetharaman | 2a30f36d | 2011-09-20 13:56:55 +0000 | [diff] [blame] | 1736 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1737 | } |
| 1738 | |
| 1739 | /* |
| 1740 | * Reallocate the space for if_broot based on the number of records |
| 1741 | * being added or deleted as indicated in rec_diff. Move the records |
| 1742 | * and pointers in if_broot to fit the new size. When shrinking this |
| 1743 | * will eliminate holes between the records and pointers created by |
| 1744 | * the caller. When growing this will create holes to be filled in |
| 1745 | * by the caller. |
| 1746 | * |
| 1747 | * The caller must not request to add more records than would fit in |
| 1748 | * the on-disk inode root. If the if_broot is currently NULL, then |
| 1749 | * if we adding records one will be allocated. The caller must also |
| 1750 | * not request that the number of records go below zero, although |
| 1751 | * it can go to zero. |
| 1752 | * |
| 1753 | * ip -- the inode whose if_broot area is changing |
| 1754 | * ext_diff -- the change in the number of records, positive or negative, |
| 1755 | * requested for the if_broot array. |
| 1756 | */ |
| 1757 | void |
| 1758 | xfs_iroot_realloc( |
| 1759 | xfs_inode_t *ip, |
| 1760 | int rec_diff, |
| 1761 | int whichfork) |
| 1762 | { |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 1763 | struct xfs_mount *mp = ip->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1764 | int cur_max; |
| 1765 | xfs_ifork_t *ifp; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 1766 | struct xfs_btree_block *new_broot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1767 | int new_max; |
| 1768 | size_t new_size; |
| 1769 | char *np; |
| 1770 | char *op; |
| 1771 | |
| 1772 | /* |
| 1773 | * Handle the degenerate case quietly. |
| 1774 | */ |
| 1775 | if (rec_diff == 0) { |
| 1776 | return; |
| 1777 | } |
| 1778 | |
| 1779 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1780 | if (rec_diff > 0) { |
| 1781 | /* |
| 1782 | * If there wasn't any memory allocated before, just |
| 1783 | * allocate it now and get out. |
| 1784 | */ |
| 1785 | if (ifp->if_broot_bytes == 0) { |
| 1786 | new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(rec_diff); |
Dave Chinner | 4a7eddd | 2010-07-20 17:53:59 +1000 | [diff] [blame] | 1787 | ifp->if_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1788 | ifp->if_broot_bytes = (int)new_size; |
| 1789 | return; |
| 1790 | } |
| 1791 | |
| 1792 | /* |
| 1793 | * If there is already an existing if_broot, then we need |
| 1794 | * to realloc() it and shift the pointers to their new |
| 1795 | * location. The records don't change location because |
| 1796 | * they are kept butted up against the btree block header. |
| 1797 | */ |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 1798 | cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1799 | new_max = cur_max + rec_diff; |
| 1800 | new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(new_max); |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 1801 | ifp->if_broot = kmem_realloc(ifp->if_broot, new_size, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1802 | (size_t)XFS_BMAP_BROOT_SPACE_CALC(cur_max), /* old size */ |
Dave Chinner | 4a7eddd | 2010-07-20 17:53:59 +1000 | [diff] [blame] | 1803 | KM_SLEEP | KM_NOFS); |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 1804 | op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1, |
| 1805 | ifp->if_broot_bytes); |
| 1806 | np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1, |
| 1807 | (int)new_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1808 | ifp->if_broot_bytes = (int)new_size; |
| 1809 | ASSERT(ifp->if_broot_bytes <= |
| 1810 | XFS_IFORK_SIZE(ip, whichfork) + XFS_BROOT_SIZE_ADJ); |
| 1811 | memmove(np, op, cur_max * (uint)sizeof(xfs_dfsbno_t)); |
| 1812 | return; |
| 1813 | } |
| 1814 | |
| 1815 | /* |
| 1816 | * rec_diff is less than 0. In this case, we are shrinking the |
| 1817 | * if_broot buffer. It must already exist. If we go to zero |
| 1818 | * records, just get rid of the root and clear the status bit. |
| 1819 | */ |
| 1820 | ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0)); |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 1821 | cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1822 | new_max = cur_max + rec_diff; |
| 1823 | ASSERT(new_max >= 0); |
| 1824 | if (new_max > 0) |
| 1825 | new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(new_max); |
| 1826 | else |
| 1827 | new_size = 0; |
| 1828 | if (new_size > 0) { |
Dave Chinner | 4a7eddd | 2010-07-20 17:53:59 +1000 | [diff] [blame] | 1829 | new_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1830 | /* |
| 1831 | * First copy over the btree block header. |
| 1832 | */ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 1833 | memcpy(new_broot, ifp->if_broot, XFS_BTREE_LBLOCK_LEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1834 | } else { |
| 1835 | new_broot = NULL; |
| 1836 | ifp->if_flags &= ~XFS_IFBROOT; |
| 1837 | } |
| 1838 | |
| 1839 | /* |
| 1840 | * Only copy the records and pointers if there are any. |
| 1841 | */ |
| 1842 | if (new_max > 0) { |
| 1843 | /* |
| 1844 | * First copy the records. |
| 1845 | */ |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 1846 | op = (char *)XFS_BMBT_REC_ADDR(mp, ifp->if_broot, 1); |
| 1847 | np = (char *)XFS_BMBT_REC_ADDR(mp, new_broot, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1848 | memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t)); |
| 1849 | |
| 1850 | /* |
| 1851 | * Then copy the pointers. |
| 1852 | */ |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 1853 | op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1854 | ifp->if_broot_bytes); |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 1855 | np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, new_broot, 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1856 | (int)new_size); |
| 1857 | memcpy(np, op, new_max * (uint)sizeof(xfs_dfsbno_t)); |
| 1858 | } |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 1859 | kmem_free(ifp->if_broot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1860 | ifp->if_broot = new_broot; |
| 1861 | ifp->if_broot_bytes = (int)new_size; |
| 1862 | ASSERT(ifp->if_broot_bytes <= |
| 1863 | XFS_IFORK_SIZE(ip, whichfork) + XFS_BROOT_SIZE_ADJ); |
| 1864 | return; |
| 1865 | } |
| 1866 | |
| 1867 | |
| 1868 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1869 | * This is called when the amount of space needed for if_data |
| 1870 | * is increased or decreased. The change in size is indicated by |
| 1871 | * the number of bytes that need to be added or deleted in the |
| 1872 | * byte_diff parameter. |
| 1873 | * |
| 1874 | * If the amount of space needed has decreased below the size of the |
| 1875 | * inline buffer, then switch to using the inline buffer. Otherwise, |
| 1876 | * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer |
| 1877 | * to what is needed. |
| 1878 | * |
| 1879 | * ip -- the inode whose if_data area is changing |
| 1880 | * byte_diff -- the change in the number of bytes, positive or negative, |
| 1881 | * requested for the if_data array. |
| 1882 | */ |
| 1883 | void |
| 1884 | xfs_idata_realloc( |
| 1885 | xfs_inode_t *ip, |
| 1886 | int byte_diff, |
| 1887 | int whichfork) |
| 1888 | { |
| 1889 | xfs_ifork_t *ifp; |
| 1890 | int new_size; |
| 1891 | int real_size; |
| 1892 | |
| 1893 | if (byte_diff == 0) { |
| 1894 | return; |
| 1895 | } |
| 1896 | |
| 1897 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1898 | new_size = (int)ifp->if_bytes + byte_diff; |
| 1899 | ASSERT(new_size >= 0); |
| 1900 | |
| 1901 | if (new_size == 0) { |
| 1902 | if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) { |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 1903 | kmem_free(ifp->if_u1.if_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1904 | } |
| 1905 | ifp->if_u1.if_data = NULL; |
| 1906 | real_size = 0; |
| 1907 | } else if (new_size <= sizeof(ifp->if_u2.if_inline_data)) { |
| 1908 | /* |
| 1909 | * If the valid extents/data can fit in if_inline_ext/data, |
| 1910 | * copy them from the malloc'd vector and free it. |
| 1911 | */ |
| 1912 | if (ifp->if_u1.if_data == NULL) { |
| 1913 | ifp->if_u1.if_data = ifp->if_u2.if_inline_data; |
| 1914 | } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) { |
| 1915 | ASSERT(ifp->if_real_bytes != 0); |
| 1916 | memcpy(ifp->if_u2.if_inline_data, ifp->if_u1.if_data, |
| 1917 | new_size); |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 1918 | kmem_free(ifp->if_u1.if_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1919 | ifp->if_u1.if_data = ifp->if_u2.if_inline_data; |
| 1920 | } |
| 1921 | real_size = 0; |
| 1922 | } else { |
| 1923 | /* |
| 1924 | * Stuck with malloc/realloc. |
| 1925 | * For inline data, the underlying buffer must be |
| 1926 | * a multiple of 4 bytes in size so that it can be |
| 1927 | * logged and stay on word boundaries. We enforce |
| 1928 | * that here. |
| 1929 | */ |
| 1930 | real_size = roundup(new_size, 4); |
| 1931 | if (ifp->if_u1.if_data == NULL) { |
| 1932 | ASSERT(ifp->if_real_bytes == 0); |
Dave Chinner | 4a7eddd | 2010-07-20 17:53:59 +1000 | [diff] [blame] | 1933 | ifp->if_u1.if_data = kmem_alloc(real_size, |
| 1934 | KM_SLEEP | KM_NOFS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1935 | } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) { |
| 1936 | /* |
| 1937 | * Only do the realloc if the underlying size |
| 1938 | * is really changing. |
| 1939 | */ |
| 1940 | if (ifp->if_real_bytes != real_size) { |
| 1941 | ifp->if_u1.if_data = |
| 1942 | kmem_realloc(ifp->if_u1.if_data, |
| 1943 | real_size, |
| 1944 | ifp->if_real_bytes, |
Dave Chinner | 4a7eddd | 2010-07-20 17:53:59 +1000 | [diff] [blame] | 1945 | KM_SLEEP | KM_NOFS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1946 | } |
| 1947 | } else { |
| 1948 | ASSERT(ifp->if_real_bytes == 0); |
Dave Chinner | 4a7eddd | 2010-07-20 17:53:59 +1000 | [diff] [blame] | 1949 | ifp->if_u1.if_data = kmem_alloc(real_size, |
| 1950 | KM_SLEEP | KM_NOFS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1951 | memcpy(ifp->if_u1.if_data, ifp->if_u2.if_inline_data, |
| 1952 | ifp->if_bytes); |
| 1953 | } |
| 1954 | } |
| 1955 | ifp->if_real_bytes = real_size; |
| 1956 | ifp->if_bytes = new_size; |
| 1957 | ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork)); |
| 1958 | } |
| 1959 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1960 | void |
| 1961 | xfs_idestroy_fork( |
| 1962 | xfs_inode_t *ip, |
| 1963 | int whichfork) |
| 1964 | { |
| 1965 | xfs_ifork_t *ifp; |
| 1966 | |
| 1967 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1968 | if (ifp->if_broot != NULL) { |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 1969 | kmem_free(ifp->if_broot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1970 | ifp->if_broot = NULL; |
| 1971 | } |
| 1972 | |
| 1973 | /* |
| 1974 | * If the format is local, then we can't have an extents |
| 1975 | * array so just look for an inline data array. If we're |
| 1976 | * not local then we may or may not have an extents list, |
| 1977 | * so check and free it up if we do. |
| 1978 | */ |
| 1979 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { |
| 1980 | if ((ifp->if_u1.if_data != ifp->if_u2.if_inline_data) && |
| 1981 | (ifp->if_u1.if_data != NULL)) { |
| 1982 | ASSERT(ifp->if_real_bytes != 0); |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 1983 | kmem_free(ifp->if_u1.if_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1984 | ifp->if_u1.if_data = NULL; |
| 1985 | ifp->if_real_bytes = 0; |
| 1986 | } |
| 1987 | } else if ((ifp->if_flags & XFS_IFEXTENTS) && |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 1988 | ((ifp->if_flags & XFS_IFEXTIREC) || |
| 1989 | ((ifp->if_u1.if_extents != NULL) && |
| 1990 | (ifp->if_u1.if_extents != ifp->if_u2.if_inline_ext)))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1991 | ASSERT(ifp->if_real_bytes != 0); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1992 | xfs_iext_destroy(ifp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1993 | } |
| 1994 | ASSERT(ifp->if_u1.if_extents == NULL || |
| 1995 | ifp->if_u1.if_extents == ifp->if_u2.if_inline_ext); |
| 1996 | ASSERT(ifp->if_real_bytes == 0); |
| 1997 | if (whichfork == XFS_ATTR_FORK) { |
| 1998 | kmem_zone_free(xfs_ifork_zone, ip->i_afp); |
| 1999 | ip->i_afp = NULL; |
| 2000 | } |
| 2001 | } |
| 2002 | |
| 2003 | /* |
Christoph Hellwig | 60ec678 | 2010-02-17 19:43:56 +0000 | [diff] [blame] | 2004 | * This is called to unpin an inode. The caller must have the inode locked |
| 2005 | * in at least shared mode so that the buffer cannot be subsequently pinned |
| 2006 | * once someone is waiting for it to be unpinned. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2007 | */ |
Christoph Hellwig | 60ec678 | 2010-02-17 19:43:56 +0000 | [diff] [blame] | 2008 | static void |
Christoph Hellwig | f392e63 | 2011-12-18 20:00:10 +0000 | [diff] [blame] | 2009 | xfs_iunpin( |
Christoph Hellwig | 60ec678 | 2010-02-17 19:43:56 +0000 | [diff] [blame] | 2010 | struct xfs_inode *ip) |
David Chinner | a3f74ff | 2008-03-06 13:43:42 +1100 | [diff] [blame] | 2011 | { |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 2012 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)); |
David Chinner | a3f74ff | 2008-03-06 13:43:42 +1100 | [diff] [blame] | 2013 | |
Dave Chinner | 4aaf15d | 2010-03-08 11:24:07 +1100 | [diff] [blame] | 2014 | trace_xfs_inode_unpin_nowait(ip, _RET_IP_); |
| 2015 | |
David Chinner | a3f74ff | 2008-03-06 13:43:42 +1100 | [diff] [blame] | 2016 | /* Give the log a push to start the unpinning I/O */ |
Christoph Hellwig | 60ec678 | 2010-02-17 19:43:56 +0000 | [diff] [blame] | 2017 | xfs_log_force_lsn(ip->i_mount, ip->i_itemp->ili_last_lsn, 0); |
Christoph Hellwig | a14a348 | 2010-01-19 09:56:46 +0000 | [diff] [blame] | 2018 | |
David Chinner | a3f74ff | 2008-03-06 13:43:42 +1100 | [diff] [blame] | 2019 | } |
| 2020 | |
Christoph Hellwig | f392e63 | 2011-12-18 20:00:10 +0000 | [diff] [blame] | 2021 | static void |
| 2022 | __xfs_iunpin_wait( |
| 2023 | struct xfs_inode *ip) |
| 2024 | { |
| 2025 | wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IPINNED_BIT); |
| 2026 | DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IPINNED_BIT); |
| 2027 | |
| 2028 | xfs_iunpin(ip); |
| 2029 | |
| 2030 | do { |
| 2031 | prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE); |
| 2032 | if (xfs_ipincount(ip)) |
| 2033 | io_schedule(); |
| 2034 | } while (xfs_ipincount(ip)); |
| 2035 | finish_wait(wq, &wait.wait); |
| 2036 | } |
| 2037 | |
Dave Chinner | 777df5a | 2010-02-06 12:37:26 +1100 | [diff] [blame] | 2038 | void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2039 | xfs_iunpin_wait( |
Christoph Hellwig | 60ec678 | 2010-02-17 19:43:56 +0000 | [diff] [blame] | 2040 | struct xfs_inode *ip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2041 | { |
Christoph Hellwig | f392e63 | 2011-12-18 20:00:10 +0000 | [diff] [blame] | 2042 | if (xfs_ipincount(ip)) |
| 2043 | __xfs_iunpin_wait(ip); |
David Chinner | a3f74ff | 2008-03-06 13:43:42 +1100 | [diff] [blame] | 2044 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2045 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2046 | /* |
| 2047 | * xfs_iextents_copy() |
| 2048 | * |
| 2049 | * This is called to copy the REAL extents (as opposed to the delayed |
| 2050 | * allocation extents) from the inode into the given buffer. It |
| 2051 | * returns the number of bytes copied into the buffer. |
| 2052 | * |
| 2053 | * If there are no delayed allocation extents, then we can just |
| 2054 | * memcpy() the extents into the buffer. Otherwise, we need to |
| 2055 | * examine each extent in turn and skip those which are delayed. |
| 2056 | */ |
| 2057 | int |
| 2058 | xfs_iextents_copy( |
| 2059 | xfs_inode_t *ip, |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 2060 | xfs_bmbt_rec_t *dp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2061 | int whichfork) |
| 2062 | { |
| 2063 | int copied; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2064 | int i; |
| 2065 | xfs_ifork_t *ifp; |
| 2066 | int nrecs; |
| 2067 | xfs_fsblock_t start_block; |
| 2068 | |
| 2069 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 2070 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2071 | ASSERT(ifp->if_bytes > 0); |
| 2072 | |
| 2073 | nrecs = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 2074 | XFS_BMAP_TRACE_EXLIST(ip, nrecs, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2075 | ASSERT(nrecs > 0); |
| 2076 | |
| 2077 | /* |
| 2078 | * There are some delayed allocation extents in the |
| 2079 | * inode, so copy the extents one at a time and skip |
| 2080 | * the delayed ones. There must be at least one |
| 2081 | * non-delayed extent. |
| 2082 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2083 | copied = 0; |
| 2084 | for (i = 0; i < nrecs; i++) { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 2085 | xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2086 | start_block = xfs_bmbt_get_startblock(ep); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2087 | if (isnullstartblock(start_block)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2088 | /* |
| 2089 | * It's a delayed allocation extent, so skip it. |
| 2090 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2091 | continue; |
| 2092 | } |
| 2093 | |
| 2094 | /* Translate to on disk format */ |
Christoph Hellwig | cd8b0a9 | 2007-08-16 16:24:15 +1000 | [diff] [blame] | 2095 | put_unaligned(cpu_to_be64(ep->l0), &dp->l0); |
| 2096 | put_unaligned(cpu_to_be64(ep->l1), &dp->l1); |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 2097 | dp++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2098 | copied++; |
| 2099 | } |
| 2100 | ASSERT(copied != 0); |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 2101 | xfs_validate_extents(ifp, copied, XFS_EXTFMT_INODE(ip)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2102 | |
| 2103 | return (copied * (uint)sizeof(xfs_bmbt_rec_t)); |
| 2104 | } |
| 2105 | |
| 2106 | /* |
| 2107 | * Each of the following cases stores data into the same region |
| 2108 | * of the on-disk inode, so only one of them can be valid at |
| 2109 | * any given time. While it is possible to have conflicting formats |
| 2110 | * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is |
| 2111 | * in EXTENTS format, this can only happen when the fork has |
| 2112 | * changed formats after being modified but before being flushed. |
| 2113 | * In these cases, the format always takes precedence, because the |
| 2114 | * format indicates the current state of the fork. |
| 2115 | */ |
| 2116 | /*ARGSUSED*/ |
David Chinner | e4ac967 | 2008-04-10 12:23:58 +1000 | [diff] [blame] | 2117 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2118 | xfs_iflush_fork( |
| 2119 | xfs_inode_t *ip, |
| 2120 | xfs_dinode_t *dip, |
| 2121 | xfs_inode_log_item_t *iip, |
| 2122 | int whichfork, |
| 2123 | xfs_buf_t *bp) |
| 2124 | { |
| 2125 | char *cp; |
| 2126 | xfs_ifork_t *ifp; |
| 2127 | xfs_mount_t *mp; |
| 2128 | #ifdef XFS_TRANS_DEBUG |
| 2129 | int first; |
| 2130 | #endif |
| 2131 | static const short brootflag[2] = |
| 2132 | { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT }; |
| 2133 | static const short dataflag[2] = |
| 2134 | { XFS_ILOG_DDATA, XFS_ILOG_ADATA }; |
| 2135 | static const short extflag[2] = |
| 2136 | { XFS_ILOG_DEXT, XFS_ILOG_AEXT }; |
| 2137 | |
David Chinner | e4ac967 | 2008-04-10 12:23:58 +1000 | [diff] [blame] | 2138 | if (!iip) |
| 2139 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2140 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 2141 | /* |
| 2142 | * This can happen if we gave up in iformat in an error path, |
| 2143 | * for the attribute fork. |
| 2144 | */ |
David Chinner | e4ac967 | 2008-04-10 12:23:58 +1000 | [diff] [blame] | 2145 | if (!ifp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2146 | ASSERT(whichfork == XFS_ATTR_FORK); |
David Chinner | e4ac967 | 2008-04-10 12:23:58 +1000 | [diff] [blame] | 2147 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2148 | } |
| 2149 | cp = XFS_DFORK_PTR(dip, whichfork); |
| 2150 | mp = ip->i_mount; |
| 2151 | switch (XFS_IFORK_FORMAT(ip, whichfork)) { |
| 2152 | case XFS_DINODE_FMT_LOCAL: |
Christoph Hellwig | f5d8d5c | 2012-02-29 09:53:54 +0000 | [diff] [blame] | 2153 | if ((iip->ili_fields & dataflag[whichfork]) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2154 | (ifp->if_bytes > 0)) { |
| 2155 | ASSERT(ifp->if_u1.if_data != NULL); |
| 2156 | ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork)); |
| 2157 | memcpy(cp, ifp->if_u1.if_data, ifp->if_bytes); |
| 2158 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2159 | break; |
| 2160 | |
| 2161 | case XFS_DINODE_FMT_EXTENTS: |
| 2162 | ASSERT((ifp->if_flags & XFS_IFEXTENTS) || |
Christoph Hellwig | f5d8d5c | 2012-02-29 09:53:54 +0000 | [diff] [blame] | 2163 | !(iip->ili_fields & extflag[whichfork])); |
| 2164 | if ((iip->ili_fields & extflag[whichfork]) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2165 | (ifp->if_bytes > 0)) { |
Christoph Hellwig | ab1908a | 2011-05-11 15:04:10 +0000 | [diff] [blame] | 2166 | ASSERT(xfs_iext_get_ext(ifp, 0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2167 | ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) > 0); |
| 2168 | (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp, |
| 2169 | whichfork); |
| 2170 | } |
| 2171 | break; |
| 2172 | |
| 2173 | case XFS_DINODE_FMT_BTREE: |
Christoph Hellwig | f5d8d5c | 2012-02-29 09:53:54 +0000 | [diff] [blame] | 2174 | if ((iip->ili_fields & brootflag[whichfork]) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2175 | (ifp->if_broot_bytes > 0)) { |
| 2176 | ASSERT(ifp->if_broot != NULL); |
| 2177 | ASSERT(ifp->if_broot_bytes <= |
| 2178 | (XFS_IFORK_SIZE(ip, whichfork) + |
| 2179 | XFS_BROOT_SIZE_ADJ)); |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 2180 | xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2181 | (xfs_bmdr_block_t *)cp, |
| 2182 | XFS_DFORK_SIZE(dip, mp, whichfork)); |
| 2183 | } |
| 2184 | break; |
| 2185 | |
| 2186 | case XFS_DINODE_FMT_DEV: |
Christoph Hellwig | f5d8d5c | 2012-02-29 09:53:54 +0000 | [diff] [blame] | 2187 | if (iip->ili_fields & XFS_ILOG_DEV) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2188 | ASSERT(whichfork == XFS_DATA_FORK); |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 2189 | xfs_dinode_put_rdev(dip, ip->i_df.if_u2.if_rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2190 | } |
| 2191 | break; |
| 2192 | |
| 2193 | case XFS_DINODE_FMT_UUID: |
Christoph Hellwig | f5d8d5c | 2012-02-29 09:53:54 +0000 | [diff] [blame] | 2194 | if (iip->ili_fields & XFS_ILOG_UUID) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2195 | ASSERT(whichfork == XFS_DATA_FORK); |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 2196 | memcpy(XFS_DFORK_DPTR(dip), |
| 2197 | &ip->i_df.if_u2.if_uuid, |
| 2198 | sizeof(uuid_t)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2199 | } |
| 2200 | break; |
| 2201 | |
| 2202 | default: |
| 2203 | ASSERT(0); |
| 2204 | break; |
| 2205 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2206 | } |
| 2207 | |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 2208 | STATIC int |
| 2209 | xfs_iflush_cluster( |
| 2210 | xfs_inode_t *ip, |
| 2211 | xfs_buf_t *bp) |
| 2212 | { |
| 2213 | xfs_mount_t *mp = ip->i_mount; |
Dave Chinner | 5017e97 | 2010-01-11 11:47:40 +0000 | [diff] [blame] | 2214 | struct xfs_perag *pag; |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 2215 | unsigned long first_index, mask; |
David Chinner | c8f5f12 | 2008-05-20 11:30:15 +1000 | [diff] [blame] | 2216 | unsigned long inodes_per_cluster; |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 2217 | int ilist_size; |
| 2218 | xfs_inode_t **ilist; |
| 2219 | xfs_inode_t *iq; |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 2220 | int nr_found; |
| 2221 | int clcount = 0; |
| 2222 | int bufwasdelwri; |
| 2223 | int i; |
| 2224 | |
Dave Chinner | 5017e97 | 2010-01-11 11:47:40 +0000 | [diff] [blame] | 2225 | pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino)); |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 2226 | |
David Chinner | c8f5f12 | 2008-05-20 11:30:15 +1000 | [diff] [blame] | 2227 | inodes_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog; |
| 2228 | ilist_size = inodes_per_cluster * sizeof(xfs_inode_t *); |
David Chinner | 49383b0 | 2008-05-19 16:29:34 +1000 | [diff] [blame] | 2229 | ilist = kmem_alloc(ilist_size, KM_MAYFAIL|KM_NOFS); |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 2230 | if (!ilist) |
Dave Chinner | 44b56e0 | 2010-01-11 11:47:43 +0000 | [diff] [blame] | 2231 | goto out_put; |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 2232 | |
| 2233 | mask = ~(((XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog)) - 1); |
| 2234 | first_index = XFS_INO_TO_AGINO(mp, ip->i_ino) & mask; |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 2235 | rcu_read_lock(); |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 2236 | /* really need a gang lookup range call here */ |
| 2237 | nr_found = radix_tree_gang_lookup(&pag->pag_ici_root, (void**)ilist, |
David Chinner | c8f5f12 | 2008-05-20 11:30:15 +1000 | [diff] [blame] | 2238 | first_index, inodes_per_cluster); |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 2239 | if (nr_found == 0) |
| 2240 | goto out_free; |
| 2241 | |
| 2242 | for (i = 0; i < nr_found; i++) { |
| 2243 | iq = ilist[i]; |
| 2244 | if (iq == ip) |
| 2245 | continue; |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 2246 | |
| 2247 | /* |
| 2248 | * because this is an RCU protected lookup, we could find a |
| 2249 | * recently freed or even reallocated inode during the lookup. |
| 2250 | * We need to check under the i_flags_lock for a valid inode |
| 2251 | * here. Skip it if it is not valid or the wrong inode. |
| 2252 | */ |
| 2253 | spin_lock(&ip->i_flags_lock); |
| 2254 | if (!ip->i_ino || |
| 2255 | (XFS_INO_TO_AGINO(mp, iq->i_ino) & mask) != first_index) { |
| 2256 | spin_unlock(&ip->i_flags_lock); |
| 2257 | continue; |
| 2258 | } |
| 2259 | spin_unlock(&ip->i_flags_lock); |
| 2260 | |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 2261 | /* |
| 2262 | * Do an un-protected check to see if the inode is dirty and |
| 2263 | * is a candidate for flushing. These checks will be repeated |
| 2264 | * later after the appropriate locks are acquired. |
| 2265 | */ |
David Chinner | 3354040 | 2008-03-06 13:43:59 +1100 | [diff] [blame] | 2266 | if (xfs_inode_clean(iq) && xfs_ipincount(iq) == 0) |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 2267 | continue; |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 2268 | |
| 2269 | /* |
| 2270 | * Try to get locks. If any are unavailable or it is pinned, |
| 2271 | * then this inode cannot be flushed and is skipped. |
| 2272 | */ |
| 2273 | |
| 2274 | if (!xfs_ilock_nowait(iq, XFS_ILOCK_SHARED)) |
| 2275 | continue; |
| 2276 | if (!xfs_iflock_nowait(iq)) { |
| 2277 | xfs_iunlock(iq, XFS_ILOCK_SHARED); |
| 2278 | continue; |
| 2279 | } |
| 2280 | if (xfs_ipincount(iq)) { |
| 2281 | xfs_ifunlock(iq); |
| 2282 | xfs_iunlock(iq, XFS_ILOCK_SHARED); |
| 2283 | continue; |
| 2284 | } |
| 2285 | |
| 2286 | /* |
| 2287 | * arriving here means that this inode can be flushed. First |
| 2288 | * re-check that it's dirty before flushing. |
| 2289 | */ |
David Chinner | 3354040 | 2008-03-06 13:43:59 +1100 | [diff] [blame] | 2290 | if (!xfs_inode_clean(iq)) { |
| 2291 | int error; |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 2292 | error = xfs_iflush_int(iq, bp); |
| 2293 | if (error) { |
| 2294 | xfs_iunlock(iq, XFS_ILOCK_SHARED); |
| 2295 | goto cluster_corrupt_out; |
| 2296 | } |
| 2297 | clcount++; |
| 2298 | } else { |
| 2299 | xfs_ifunlock(iq); |
| 2300 | } |
| 2301 | xfs_iunlock(iq, XFS_ILOCK_SHARED); |
| 2302 | } |
| 2303 | |
| 2304 | if (clcount) { |
| 2305 | XFS_STATS_INC(xs_icluster_flushcnt); |
| 2306 | XFS_STATS_ADD(xs_icluster_flushinode, clcount); |
| 2307 | } |
| 2308 | |
| 2309 | out_free: |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 2310 | rcu_read_unlock(); |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 2311 | kmem_free(ilist); |
Dave Chinner | 44b56e0 | 2010-01-11 11:47:43 +0000 | [diff] [blame] | 2312 | out_put: |
| 2313 | xfs_perag_put(pag); |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 2314 | return 0; |
| 2315 | |
| 2316 | |
| 2317 | cluster_corrupt_out: |
| 2318 | /* |
| 2319 | * Corruption detected in the clustering loop. Invalidate the |
| 2320 | * inode buffer and shut down the filesystem. |
| 2321 | */ |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 2322 | rcu_read_unlock(); |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 2323 | /* |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 2324 | * Clean up the buffer. If it was delwri, just release it -- |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 2325 | * brelse can handle it with no problems. If not, shut down the |
| 2326 | * filesystem before releasing the buffer. |
| 2327 | */ |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 2328 | bufwasdelwri = (bp->b_flags & _XBF_DELWRI_Q); |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 2329 | if (bufwasdelwri) |
| 2330 | xfs_buf_relse(bp); |
| 2331 | |
| 2332 | xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); |
| 2333 | |
| 2334 | if (!bufwasdelwri) { |
| 2335 | /* |
| 2336 | * Just like incore_relse: if we have b_iodone functions, |
| 2337 | * mark the buffer as an error and call them. Otherwise |
| 2338 | * mark it as stale and brelse. |
| 2339 | */ |
Christoph Hellwig | cb669ca | 2011-07-13 13:43:49 +0200 | [diff] [blame] | 2340 | if (bp->b_iodone) { |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 2341 | XFS_BUF_UNDONE(bp); |
Christoph Hellwig | c867cb6 | 2011-10-10 16:52:46 +0000 | [diff] [blame] | 2342 | xfs_buf_stale(bp); |
Chandra Seetharaman | 5a52c2a58 | 2011-07-22 23:39:51 +0000 | [diff] [blame] | 2343 | xfs_buf_ioerror(bp, EIO); |
Christoph Hellwig | 1a1a3e9 | 2010-10-06 18:41:18 +0000 | [diff] [blame] | 2344 | xfs_buf_ioend(bp, 0); |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 2345 | } else { |
Christoph Hellwig | c867cb6 | 2011-10-10 16:52:46 +0000 | [diff] [blame] | 2346 | xfs_buf_stale(bp); |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 2347 | xfs_buf_relse(bp); |
| 2348 | } |
| 2349 | } |
| 2350 | |
| 2351 | /* |
| 2352 | * Unlocks the flush lock |
| 2353 | */ |
Dave Chinner | 04913fd | 2012-04-23 15:58:41 +1000 | [diff] [blame] | 2354 | xfs_iflush_abort(iq, false); |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 2355 | kmem_free(ilist); |
Dave Chinner | 44b56e0 | 2010-01-11 11:47:43 +0000 | [diff] [blame] | 2356 | xfs_perag_put(pag); |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 2357 | return XFS_ERROR(EFSCORRUPTED); |
| 2358 | } |
| 2359 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2360 | /* |
Christoph Hellwig | 4c46819 | 2012-04-23 15:58:36 +1000 | [diff] [blame] | 2361 | * Flush dirty inode metadata into the backing buffer. |
| 2362 | * |
| 2363 | * The caller must have the inode lock and the inode flush lock held. The |
| 2364 | * inode lock will still be held upon return to the caller, and the inode |
| 2365 | * flush lock will be released after the inode has reached the disk. |
| 2366 | * |
| 2367 | * The caller must write out the buffer returned in *bpp and release it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2368 | */ |
| 2369 | int |
| 2370 | xfs_iflush( |
Christoph Hellwig | 4c46819 | 2012-04-23 15:58:36 +1000 | [diff] [blame] | 2371 | struct xfs_inode *ip, |
| 2372 | struct xfs_buf **bpp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2373 | { |
Christoph Hellwig | 4c46819 | 2012-04-23 15:58:36 +1000 | [diff] [blame] | 2374 | struct xfs_mount *mp = ip->i_mount; |
| 2375 | struct xfs_buf *bp; |
| 2376 | struct xfs_dinode *dip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2377 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2378 | |
| 2379 | XFS_STATS_INC(xs_iflush_count); |
| 2380 | |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 2381 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)); |
Christoph Hellwig | 474fce0 | 2011-12-18 20:00:09 +0000 | [diff] [blame] | 2382 | ASSERT(xfs_isiflocked(ip)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2383 | ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE || |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 2384 | ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2385 | |
Christoph Hellwig | 4c46819 | 2012-04-23 15:58:36 +1000 | [diff] [blame] | 2386 | *bpp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2387 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2388 | xfs_iunpin_wait(ip); |
| 2389 | |
| 2390 | /* |
Dave Chinner | 4b6a468 | 2010-01-11 11:45:21 +0000 | [diff] [blame] | 2391 | * For stale inodes we cannot rely on the backing buffer remaining |
| 2392 | * stale in cache for the remaining life of the stale inode and so |
Christoph Hellwig | 475ee41 | 2012-07-03 12:21:22 -0400 | [diff] [blame^] | 2393 | * xfs_imap_to_bp() below may give us a buffer that no longer contains |
Dave Chinner | 4b6a468 | 2010-01-11 11:45:21 +0000 | [diff] [blame] | 2394 | * inodes below. We have to check this after ensuring the inode is |
| 2395 | * unpinned so that it is safe to reclaim the stale inode after the |
| 2396 | * flush call. |
| 2397 | */ |
| 2398 | if (xfs_iflags_test(ip, XFS_ISTALE)) { |
| 2399 | xfs_ifunlock(ip); |
| 2400 | return 0; |
| 2401 | } |
| 2402 | |
| 2403 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2404 | * This may have been unpinned because the filesystem is shutting |
| 2405 | * down forcibly. If that's the case we must not write this inode |
Christoph Hellwig | 32ce90a | 2012-04-23 15:58:32 +1000 | [diff] [blame] | 2406 | * to disk, because the log record didn't make it to disk. |
| 2407 | * |
| 2408 | * We also have to remove the log item from the AIL in this case, |
| 2409 | * as we wait for an empty AIL as part of the unmount process. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2410 | */ |
| 2411 | if (XFS_FORCED_SHUTDOWN(mp)) { |
Christoph Hellwig | 32ce90a | 2012-04-23 15:58:32 +1000 | [diff] [blame] | 2412 | error = XFS_ERROR(EIO); |
| 2413 | goto abort_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2414 | } |
| 2415 | |
| 2416 | /* |
David Chinner | a3f74ff | 2008-03-06 13:43:42 +1100 | [diff] [blame] | 2417 | * Get the buffer containing the on-disk inode. |
| 2418 | */ |
Christoph Hellwig | 475ee41 | 2012-07-03 12:21:22 -0400 | [diff] [blame^] | 2419 | error = xfs_imap_to_bp(mp, NULL, &ip->i_imap, &dip, &bp, XBF_TRYLOCK, |
| 2420 | 0); |
David Chinner | a3f74ff | 2008-03-06 13:43:42 +1100 | [diff] [blame] | 2421 | if (error || !bp) { |
| 2422 | xfs_ifunlock(ip); |
| 2423 | return error; |
| 2424 | } |
| 2425 | |
| 2426 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2427 | * First flush out the inode that xfs_iflush was called with. |
| 2428 | */ |
| 2429 | error = xfs_iflush_int(ip, bp); |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 2430 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2431 | goto corrupt_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2432 | |
| 2433 | /* |
David Chinner | a3f74ff | 2008-03-06 13:43:42 +1100 | [diff] [blame] | 2434 | * If the buffer is pinned then push on the log now so we won't |
| 2435 | * get stuck waiting in the write for too long. |
| 2436 | */ |
Chandra Seetharaman | 811e64c | 2011-07-22 23:40:27 +0000 | [diff] [blame] | 2437 | if (xfs_buf_ispinned(bp)) |
Christoph Hellwig | a14a348 | 2010-01-19 09:56:46 +0000 | [diff] [blame] | 2438 | xfs_log_force(mp, 0); |
David Chinner | a3f74ff | 2008-03-06 13:43:42 +1100 | [diff] [blame] | 2439 | |
| 2440 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2441 | * inode clustering: |
| 2442 | * see if other inodes can be gathered into this write |
| 2443 | */ |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 2444 | error = xfs_iflush_cluster(ip, bp); |
| 2445 | if (error) |
| 2446 | goto cluster_corrupt_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2447 | |
Christoph Hellwig | 4c46819 | 2012-04-23 15:58:36 +1000 | [diff] [blame] | 2448 | *bpp = bp; |
| 2449 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2450 | |
| 2451 | corrupt_out: |
| 2452 | xfs_buf_relse(bp); |
Nathan Scott | 7d04a33 | 2006-06-09 14:58:38 +1000 | [diff] [blame] | 2453 | xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2454 | cluster_corrupt_out: |
Christoph Hellwig | 32ce90a | 2012-04-23 15:58:32 +1000 | [diff] [blame] | 2455 | error = XFS_ERROR(EFSCORRUPTED); |
| 2456 | abort_out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2457 | /* |
| 2458 | * Unlocks the flush lock |
| 2459 | */ |
Dave Chinner | 04913fd | 2012-04-23 15:58:41 +1000 | [diff] [blame] | 2460 | xfs_iflush_abort(ip, false); |
Christoph Hellwig | 32ce90a | 2012-04-23 15:58:32 +1000 | [diff] [blame] | 2461 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2462 | } |
| 2463 | |
| 2464 | |
| 2465 | STATIC int |
| 2466 | xfs_iflush_int( |
| 2467 | xfs_inode_t *ip, |
| 2468 | xfs_buf_t *bp) |
| 2469 | { |
| 2470 | xfs_inode_log_item_t *iip; |
| 2471 | xfs_dinode_t *dip; |
| 2472 | xfs_mount_t *mp; |
| 2473 | #ifdef XFS_TRANS_DEBUG |
| 2474 | int first; |
| 2475 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2476 | |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 2477 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)); |
Christoph Hellwig | 474fce0 | 2011-12-18 20:00:09 +0000 | [diff] [blame] | 2478 | ASSERT(xfs_isiflocked(ip)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2479 | ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE || |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 2480 | ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2481 | |
| 2482 | iip = ip->i_itemp; |
| 2483 | mp = ip->i_mount; |
| 2484 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2485 | /* set *dip = inode's place in the buffer */ |
Christoph Hellwig | 92bfc6e | 2008-11-28 14:23:41 +1100 | [diff] [blame] | 2486 | dip = (xfs_dinode_t *)xfs_buf_offset(bp, ip->i_imap.im_boffset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2487 | |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 2488 | if (XFS_TEST_ERROR(dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2489 | mp, XFS_ERRTAG_IFLUSH_1, XFS_RANDOM_IFLUSH_1)) { |
Dave Chinner | 6a19d93 | 2011-03-07 10:02:35 +1100 | [diff] [blame] | 2490 | xfs_alert_tag(mp, XFS_PTAG_IFLUSH, |
| 2491 | "%s: Bad inode %Lu magic number 0x%x, ptr 0x%p", |
| 2492 | __func__, ip->i_ino, be16_to_cpu(dip->di_magic), dip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2493 | goto corrupt_out; |
| 2494 | } |
| 2495 | if (XFS_TEST_ERROR(ip->i_d.di_magic != XFS_DINODE_MAGIC, |
| 2496 | mp, XFS_ERRTAG_IFLUSH_2, XFS_RANDOM_IFLUSH_2)) { |
Dave Chinner | 6a19d93 | 2011-03-07 10:02:35 +1100 | [diff] [blame] | 2497 | xfs_alert_tag(mp, XFS_PTAG_IFLUSH, |
| 2498 | "%s: Bad inode %Lu, ptr 0x%p, magic number 0x%x", |
| 2499 | __func__, ip->i_ino, ip, ip->i_d.di_magic); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2500 | goto corrupt_out; |
| 2501 | } |
Al Viro | abbede1 | 2011-07-26 02:31:30 -0400 | [diff] [blame] | 2502 | if (S_ISREG(ip->i_d.di_mode)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2503 | if (XFS_TEST_ERROR( |
| 2504 | (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) && |
| 2505 | (ip->i_d.di_format != XFS_DINODE_FMT_BTREE), |
| 2506 | mp, XFS_ERRTAG_IFLUSH_3, XFS_RANDOM_IFLUSH_3)) { |
Dave Chinner | 6a19d93 | 2011-03-07 10:02:35 +1100 | [diff] [blame] | 2507 | xfs_alert_tag(mp, XFS_PTAG_IFLUSH, |
| 2508 | "%s: Bad regular inode %Lu, ptr 0x%p", |
| 2509 | __func__, ip->i_ino, ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2510 | goto corrupt_out; |
| 2511 | } |
Al Viro | abbede1 | 2011-07-26 02:31:30 -0400 | [diff] [blame] | 2512 | } else if (S_ISDIR(ip->i_d.di_mode)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2513 | if (XFS_TEST_ERROR( |
| 2514 | (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) && |
| 2515 | (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) && |
| 2516 | (ip->i_d.di_format != XFS_DINODE_FMT_LOCAL), |
| 2517 | mp, XFS_ERRTAG_IFLUSH_4, XFS_RANDOM_IFLUSH_4)) { |
Dave Chinner | 6a19d93 | 2011-03-07 10:02:35 +1100 | [diff] [blame] | 2518 | xfs_alert_tag(mp, XFS_PTAG_IFLUSH, |
| 2519 | "%s: Bad directory inode %Lu, ptr 0x%p", |
| 2520 | __func__, ip->i_ino, ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2521 | goto corrupt_out; |
| 2522 | } |
| 2523 | } |
| 2524 | if (XFS_TEST_ERROR(ip->i_d.di_nextents + ip->i_d.di_anextents > |
| 2525 | ip->i_d.di_nblocks, mp, XFS_ERRTAG_IFLUSH_5, |
| 2526 | XFS_RANDOM_IFLUSH_5)) { |
Dave Chinner | 6a19d93 | 2011-03-07 10:02:35 +1100 | [diff] [blame] | 2527 | xfs_alert_tag(mp, XFS_PTAG_IFLUSH, |
| 2528 | "%s: detected corrupt incore inode %Lu, " |
| 2529 | "total extents = %d, nblocks = %Ld, ptr 0x%p", |
| 2530 | __func__, ip->i_ino, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2531 | ip->i_d.di_nextents + ip->i_d.di_anextents, |
Dave Chinner | 6a19d93 | 2011-03-07 10:02:35 +1100 | [diff] [blame] | 2532 | ip->i_d.di_nblocks, ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2533 | goto corrupt_out; |
| 2534 | } |
| 2535 | if (XFS_TEST_ERROR(ip->i_d.di_forkoff > mp->m_sb.sb_inodesize, |
| 2536 | mp, XFS_ERRTAG_IFLUSH_6, XFS_RANDOM_IFLUSH_6)) { |
Dave Chinner | 6a19d93 | 2011-03-07 10:02:35 +1100 | [diff] [blame] | 2537 | xfs_alert_tag(mp, XFS_PTAG_IFLUSH, |
| 2538 | "%s: bad inode %Lu, forkoff 0x%x, ptr 0x%p", |
| 2539 | __func__, ip->i_ino, ip->i_d.di_forkoff, ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2540 | goto corrupt_out; |
| 2541 | } |
| 2542 | /* |
| 2543 | * bump the flush iteration count, used to detect flushes which |
| 2544 | * postdate a log record during recovery. |
| 2545 | */ |
| 2546 | |
| 2547 | ip->i_d.di_flushiter++; |
| 2548 | |
| 2549 | /* |
| 2550 | * Copy the dirty parts of the inode into the on-disk |
| 2551 | * inode. We always copy out the core of the inode, |
| 2552 | * because if the inode is dirty at all the core must |
| 2553 | * be. |
| 2554 | */ |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 2555 | xfs_dinode_to_disk(dip, &ip->i_d); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2556 | |
| 2557 | /* Wrap, we never let the log put out DI_MAX_FLUSH */ |
| 2558 | if (ip->i_d.di_flushiter == DI_MAX_FLUSH) |
| 2559 | ip->i_d.di_flushiter = 0; |
| 2560 | |
| 2561 | /* |
| 2562 | * If this is really an old format inode and the superblock version |
| 2563 | * has not been updated to support only new format inodes, then |
| 2564 | * convert back to the old inode format. If the superblock version |
| 2565 | * has been updated, then make the conversion permanent. |
| 2566 | */ |
Christoph Hellwig | 51ce16d | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 2567 | ASSERT(ip->i_d.di_version == 1 || xfs_sb_version_hasnlink(&mp->m_sb)); |
| 2568 | if (ip->i_d.di_version == 1) { |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 2569 | if (!xfs_sb_version_hasnlink(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2570 | /* |
| 2571 | * Convert it back. |
| 2572 | */ |
| 2573 | ASSERT(ip->i_d.di_nlink <= XFS_MAXLINK_1); |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 2574 | dip->di_onlink = cpu_to_be16(ip->i_d.di_nlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2575 | } else { |
| 2576 | /* |
| 2577 | * The superblock version has already been bumped, |
| 2578 | * so just make the conversion to the new inode |
| 2579 | * format permanent. |
| 2580 | */ |
Christoph Hellwig | 51ce16d | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 2581 | ip->i_d.di_version = 2; |
| 2582 | dip->di_version = 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2583 | ip->i_d.di_onlink = 0; |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 2584 | dip->di_onlink = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2585 | memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad)); |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 2586 | memset(&(dip->di_pad[0]), 0, |
| 2587 | sizeof(dip->di_pad)); |
Arkadiusz Mi?kiewicz | 6743099 | 2010-09-26 06:10:18 +0000 | [diff] [blame] | 2588 | ASSERT(xfs_get_projid(ip) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2589 | } |
| 2590 | } |
| 2591 | |
David Chinner | e4ac967 | 2008-04-10 12:23:58 +1000 | [diff] [blame] | 2592 | xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK, bp); |
| 2593 | if (XFS_IFORK_Q(ip)) |
| 2594 | xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK, bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2595 | xfs_inobp_check(mp, bp); |
| 2596 | |
| 2597 | /* |
Christoph Hellwig | f5d8d5c | 2012-02-29 09:53:54 +0000 | [diff] [blame] | 2598 | * We've recorded everything logged in the inode, so we'd like to clear |
| 2599 | * the ili_fields bits so we don't log and flush things unnecessarily. |
| 2600 | * However, we can't stop logging all this information until the data |
| 2601 | * we've copied into the disk buffer is written to disk. If we did we |
| 2602 | * might overwrite the copy of the inode in the log with all the data |
| 2603 | * after re-logging only part of it, and in the face of a crash we |
| 2604 | * wouldn't have all the data we need to recover. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2605 | * |
Christoph Hellwig | f5d8d5c | 2012-02-29 09:53:54 +0000 | [diff] [blame] | 2606 | * What we do is move the bits to the ili_last_fields field. When |
| 2607 | * logging the inode, these bits are moved back to the ili_fields field. |
| 2608 | * In the xfs_iflush_done() routine we clear ili_last_fields, since we |
| 2609 | * know that the information those bits represent is permanently on |
| 2610 | * disk. As long as the flush completes before the inode is logged |
| 2611 | * again, then both ili_fields and ili_last_fields will be cleared. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2612 | * |
Christoph Hellwig | f5d8d5c | 2012-02-29 09:53:54 +0000 | [diff] [blame] | 2613 | * We can play with the ili_fields bits here, because the inode lock |
| 2614 | * must be held exclusively in order to set bits there and the flush |
| 2615 | * lock protects the ili_last_fields bits. Set ili_logged so the flush |
| 2616 | * done routine can tell whether or not to look in the AIL. Also, store |
| 2617 | * the current LSN of the inode so that we can tell whether the item has |
| 2618 | * moved in the AIL from xfs_iflush_done(). In order to read the lsn we |
| 2619 | * need the AIL lock, because it is a 64 bit value that cannot be read |
| 2620 | * atomically. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2621 | */ |
Christoph Hellwig | f5d8d5c | 2012-02-29 09:53:54 +0000 | [diff] [blame] | 2622 | if (iip != NULL && iip->ili_fields != 0) { |
| 2623 | iip->ili_last_fields = iip->ili_fields; |
| 2624 | iip->ili_fields = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2625 | iip->ili_logged = 1; |
| 2626 | |
David Chinner | 7b2e2a3 | 2008-10-30 17:39:12 +1100 | [diff] [blame] | 2627 | xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn, |
| 2628 | &iip->ili_item.li_lsn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2629 | |
| 2630 | /* |
| 2631 | * Attach the function xfs_iflush_done to the inode's |
| 2632 | * buffer. This will remove the inode from the AIL |
| 2633 | * and unlock the inode's flush lock when the inode is |
| 2634 | * completely written to disk. |
| 2635 | */ |
Christoph Hellwig | ca30b2a | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 2636 | xfs_buf_attach_iodone(bp, xfs_iflush_done, &iip->ili_item); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2637 | |
Christoph Hellwig | adadbee | 2011-07-13 13:43:49 +0200 | [diff] [blame] | 2638 | ASSERT(bp->b_fspriv != NULL); |
Christoph Hellwig | cb669ca | 2011-07-13 13:43:49 +0200 | [diff] [blame] | 2639 | ASSERT(bp->b_iodone != NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2640 | } else { |
| 2641 | /* |
| 2642 | * We're flushing an inode which is not in the AIL and has |
Christoph Hellwig | 8a9c998 | 2012-02-29 09:53:52 +0000 | [diff] [blame] | 2643 | * not been logged. For this case we can immediately drop |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2644 | * the inode flush lock because we can avoid the whole |
| 2645 | * AIL state thing. It's OK to drop the flush lock now, |
| 2646 | * because we've already locked the buffer and to do anything |
| 2647 | * you really need both. |
| 2648 | */ |
| 2649 | if (iip != NULL) { |
| 2650 | ASSERT(iip->ili_logged == 0); |
| 2651 | ASSERT(iip->ili_last_fields == 0); |
| 2652 | ASSERT((iip->ili_item.li_flags & XFS_LI_IN_AIL) == 0); |
| 2653 | } |
| 2654 | xfs_ifunlock(ip); |
| 2655 | } |
| 2656 | |
| 2657 | return 0; |
| 2658 | |
| 2659 | corrupt_out: |
| 2660 | return XFS_ERROR(EFSCORRUPTED); |
| 2661 | } |
| 2662 | |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2663 | /* |
| 2664 | * Return a pointer to the extent record at file index idx. |
| 2665 | */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 2666 | xfs_bmbt_rec_host_t * |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2667 | xfs_iext_get_ext( |
| 2668 | xfs_ifork_t *ifp, /* inode fork pointer */ |
| 2669 | xfs_extnum_t idx) /* index of target extent */ |
| 2670 | { |
| 2671 | ASSERT(idx >= 0); |
Christoph Hellwig | 87bef18 | 2011-05-11 15:04:11 +0000 | [diff] [blame] | 2672 | ASSERT(idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t)); |
| 2673 | |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 2674 | if ((ifp->if_flags & XFS_IFEXTIREC) && (idx == 0)) { |
| 2675 | return ifp->if_u1.if_ext_irec->er_extbuf; |
| 2676 | } else if (ifp->if_flags & XFS_IFEXTIREC) { |
| 2677 | xfs_ext_irec_t *erp; /* irec pointer */ |
| 2678 | int erp_idx = 0; /* irec index */ |
| 2679 | xfs_extnum_t page_idx = idx; /* ext index in target list */ |
| 2680 | |
| 2681 | erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0); |
| 2682 | return &erp->er_extbuf[page_idx]; |
| 2683 | } else if (ifp->if_bytes) { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2684 | return &ifp->if_u1.if_extents[idx]; |
| 2685 | } else { |
| 2686 | return NULL; |
| 2687 | } |
| 2688 | } |
| 2689 | |
| 2690 | /* |
| 2691 | * Insert new item(s) into the extent records for incore inode |
| 2692 | * fork 'ifp'. 'count' new items are inserted at index 'idx'. |
| 2693 | */ |
| 2694 | void |
| 2695 | xfs_iext_insert( |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 2696 | xfs_inode_t *ip, /* incore inode pointer */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2697 | xfs_extnum_t idx, /* starting index of new items */ |
| 2698 | xfs_extnum_t count, /* number of inserted items */ |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 2699 | xfs_bmbt_irec_t *new, /* items to insert */ |
| 2700 | int state) /* type of extent conversion */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2701 | { |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 2702 | xfs_ifork_t *ifp = (state & BMAP_ATTRFORK) ? ip->i_afp : &ip->i_df; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2703 | xfs_extnum_t i; /* extent record index */ |
| 2704 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2705 | trace_xfs_iext_insert(ip, idx, new, state, _RET_IP_); |
| 2706 | |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2707 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
| 2708 | xfs_iext_add(ifp, idx, count); |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 2709 | for (i = idx; i < idx + count; i++, new++) |
| 2710 | xfs_bmbt_set_all(xfs_iext_get_ext(ifp, i), new); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2711 | } |
| 2712 | |
| 2713 | /* |
| 2714 | * This is called when the amount of space required for incore file |
| 2715 | * extents needs to be increased. The ext_diff parameter stores the |
| 2716 | * number of new extents being added and the idx parameter contains |
| 2717 | * the extent index where the new extents will be added. If the new |
| 2718 | * extents are being appended, then we just need to (re)allocate and |
| 2719 | * initialize the space. Otherwise, if the new extents are being |
| 2720 | * inserted into the middle of the existing entries, a bit more work |
| 2721 | * is required to make room for the new extents to be inserted. The |
| 2722 | * caller is responsible for filling in the new extent entries upon |
| 2723 | * return. |
| 2724 | */ |
| 2725 | void |
| 2726 | xfs_iext_add( |
| 2727 | xfs_ifork_t *ifp, /* inode fork pointer */ |
| 2728 | xfs_extnum_t idx, /* index to begin adding exts */ |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 2729 | int ext_diff) /* number of extents to add */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2730 | { |
| 2731 | int byte_diff; /* new bytes being added */ |
| 2732 | int new_size; /* size of extents after adding */ |
| 2733 | xfs_extnum_t nextents; /* number of extents in file */ |
| 2734 | |
| 2735 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 2736 | ASSERT((idx >= 0) && (idx <= nextents)); |
| 2737 | byte_diff = ext_diff * sizeof(xfs_bmbt_rec_t); |
| 2738 | new_size = ifp->if_bytes + byte_diff; |
| 2739 | /* |
| 2740 | * If the new number of extents (nextents + ext_diff) |
| 2741 | * fits inside the inode, then continue to use the inline |
| 2742 | * extent buffer. |
| 2743 | */ |
| 2744 | if (nextents + ext_diff <= XFS_INLINE_EXTS) { |
| 2745 | if (idx < nextents) { |
| 2746 | memmove(&ifp->if_u2.if_inline_ext[idx + ext_diff], |
| 2747 | &ifp->if_u2.if_inline_ext[idx], |
| 2748 | (nextents - idx) * sizeof(xfs_bmbt_rec_t)); |
| 2749 | memset(&ifp->if_u2.if_inline_ext[idx], 0, byte_diff); |
| 2750 | } |
| 2751 | ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext; |
| 2752 | ifp->if_real_bytes = 0; |
| 2753 | } |
| 2754 | /* |
| 2755 | * Otherwise use a linear (direct) extent list. |
| 2756 | * If the extents are currently inside the inode, |
| 2757 | * xfs_iext_realloc_direct will switch us from |
| 2758 | * inline to direct extent allocation mode. |
| 2759 | */ |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 2760 | else if (nextents + ext_diff <= XFS_LINEAR_EXTS) { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2761 | xfs_iext_realloc_direct(ifp, new_size); |
| 2762 | if (idx < nextents) { |
| 2763 | memmove(&ifp->if_u1.if_extents[idx + ext_diff], |
| 2764 | &ifp->if_u1.if_extents[idx], |
| 2765 | (nextents - idx) * sizeof(xfs_bmbt_rec_t)); |
| 2766 | memset(&ifp->if_u1.if_extents[idx], 0, byte_diff); |
| 2767 | } |
| 2768 | } |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 2769 | /* Indirection array */ |
| 2770 | else { |
| 2771 | xfs_ext_irec_t *erp; |
| 2772 | int erp_idx = 0; |
| 2773 | int page_idx = idx; |
| 2774 | |
| 2775 | ASSERT(nextents + ext_diff > XFS_LINEAR_EXTS); |
| 2776 | if (ifp->if_flags & XFS_IFEXTIREC) { |
| 2777 | erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 1); |
| 2778 | } else { |
| 2779 | xfs_iext_irec_init(ifp); |
| 2780 | ASSERT(ifp->if_flags & XFS_IFEXTIREC); |
| 2781 | erp = ifp->if_u1.if_ext_irec; |
| 2782 | } |
| 2783 | /* Extents fit in target extent page */ |
| 2784 | if (erp && erp->er_extcount + ext_diff <= XFS_LINEAR_EXTS) { |
| 2785 | if (page_idx < erp->er_extcount) { |
| 2786 | memmove(&erp->er_extbuf[page_idx + ext_diff], |
| 2787 | &erp->er_extbuf[page_idx], |
| 2788 | (erp->er_extcount - page_idx) * |
| 2789 | sizeof(xfs_bmbt_rec_t)); |
| 2790 | memset(&erp->er_extbuf[page_idx], 0, byte_diff); |
| 2791 | } |
| 2792 | erp->er_extcount += ext_diff; |
| 2793 | xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff); |
| 2794 | } |
| 2795 | /* Insert a new extent page */ |
| 2796 | else if (erp) { |
| 2797 | xfs_iext_add_indirect_multi(ifp, |
| 2798 | erp_idx, page_idx, ext_diff); |
| 2799 | } |
| 2800 | /* |
| 2801 | * If extent(s) are being appended to the last page in |
| 2802 | * the indirection array and the new extent(s) don't fit |
| 2803 | * in the page, then erp is NULL and erp_idx is set to |
| 2804 | * the next index needed in the indirection array. |
| 2805 | */ |
| 2806 | else { |
| 2807 | int count = ext_diff; |
| 2808 | |
| 2809 | while (count) { |
| 2810 | erp = xfs_iext_irec_new(ifp, erp_idx); |
| 2811 | erp->er_extcount = count; |
| 2812 | count -= MIN(count, (int)XFS_LINEAR_EXTS); |
| 2813 | if (count) { |
| 2814 | erp_idx++; |
| 2815 | } |
| 2816 | } |
| 2817 | } |
| 2818 | } |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2819 | ifp->if_bytes = new_size; |
| 2820 | } |
| 2821 | |
| 2822 | /* |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 2823 | * This is called when incore extents are being added to the indirection |
| 2824 | * array and the new extents do not fit in the target extent list. The |
| 2825 | * erp_idx parameter contains the irec index for the target extent list |
| 2826 | * in the indirection array, and the idx parameter contains the extent |
| 2827 | * index within the list. The number of extents being added is stored |
| 2828 | * in the count parameter. |
| 2829 | * |
| 2830 | * |-------| |-------| |
| 2831 | * | | | | idx - number of extents before idx |
| 2832 | * | idx | | count | |
| 2833 | * | | | | count - number of extents being inserted at idx |
| 2834 | * |-------| |-------| |
| 2835 | * | count | | nex2 | nex2 - number of extents after idx + count |
| 2836 | * |-------| |-------| |
| 2837 | */ |
| 2838 | void |
| 2839 | xfs_iext_add_indirect_multi( |
| 2840 | xfs_ifork_t *ifp, /* inode fork pointer */ |
| 2841 | int erp_idx, /* target extent irec index */ |
| 2842 | xfs_extnum_t idx, /* index within target list */ |
| 2843 | int count) /* new extents being added */ |
| 2844 | { |
| 2845 | int byte_diff; /* new bytes being added */ |
| 2846 | xfs_ext_irec_t *erp; /* pointer to irec entry */ |
| 2847 | xfs_extnum_t ext_diff; /* number of extents to add */ |
| 2848 | xfs_extnum_t ext_cnt; /* new extents still needed */ |
| 2849 | xfs_extnum_t nex2; /* extents after idx + count */ |
| 2850 | xfs_bmbt_rec_t *nex2_ep = NULL; /* temp list for nex2 extents */ |
| 2851 | int nlists; /* number of irec's (lists) */ |
| 2852 | |
| 2853 | ASSERT(ifp->if_flags & XFS_IFEXTIREC); |
| 2854 | erp = &ifp->if_u1.if_ext_irec[erp_idx]; |
| 2855 | nex2 = erp->er_extcount - idx; |
| 2856 | nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ; |
| 2857 | |
| 2858 | /* |
| 2859 | * Save second part of target extent list |
| 2860 | * (all extents past */ |
| 2861 | if (nex2) { |
| 2862 | byte_diff = nex2 * sizeof(xfs_bmbt_rec_t); |
David Chinner | 6785073 | 2008-08-13 16:02:51 +1000 | [diff] [blame] | 2863 | nex2_ep = (xfs_bmbt_rec_t *) kmem_alloc(byte_diff, KM_NOFS); |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 2864 | memmove(nex2_ep, &erp->er_extbuf[idx], byte_diff); |
| 2865 | erp->er_extcount -= nex2; |
| 2866 | xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -nex2); |
| 2867 | memset(&erp->er_extbuf[idx], 0, byte_diff); |
| 2868 | } |
| 2869 | |
| 2870 | /* |
| 2871 | * Add the new extents to the end of the target |
| 2872 | * list, then allocate new irec record(s) and |
| 2873 | * extent buffer(s) as needed to store the rest |
| 2874 | * of the new extents. |
| 2875 | */ |
| 2876 | ext_cnt = count; |
| 2877 | ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS - erp->er_extcount); |
| 2878 | if (ext_diff) { |
| 2879 | erp->er_extcount += ext_diff; |
| 2880 | xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff); |
| 2881 | ext_cnt -= ext_diff; |
| 2882 | } |
| 2883 | while (ext_cnt) { |
| 2884 | erp_idx++; |
| 2885 | erp = xfs_iext_irec_new(ifp, erp_idx); |
| 2886 | ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS); |
| 2887 | erp->er_extcount = ext_diff; |
| 2888 | xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff); |
| 2889 | ext_cnt -= ext_diff; |
| 2890 | } |
| 2891 | |
| 2892 | /* Add nex2 extents back to indirection array */ |
| 2893 | if (nex2) { |
| 2894 | xfs_extnum_t ext_avail; |
| 2895 | int i; |
| 2896 | |
| 2897 | byte_diff = nex2 * sizeof(xfs_bmbt_rec_t); |
| 2898 | ext_avail = XFS_LINEAR_EXTS - erp->er_extcount; |
| 2899 | i = 0; |
| 2900 | /* |
| 2901 | * If nex2 extents fit in the current page, append |
| 2902 | * nex2_ep after the new extents. |
| 2903 | */ |
| 2904 | if (nex2 <= ext_avail) { |
| 2905 | i = erp->er_extcount; |
| 2906 | } |
| 2907 | /* |
| 2908 | * Otherwise, check if space is available in the |
| 2909 | * next page. |
| 2910 | */ |
| 2911 | else if ((erp_idx < nlists - 1) && |
| 2912 | (nex2 <= (ext_avail = XFS_LINEAR_EXTS - |
| 2913 | ifp->if_u1.if_ext_irec[erp_idx+1].er_extcount))) { |
| 2914 | erp_idx++; |
| 2915 | erp++; |
| 2916 | /* Create a hole for nex2 extents */ |
| 2917 | memmove(&erp->er_extbuf[nex2], erp->er_extbuf, |
| 2918 | erp->er_extcount * sizeof(xfs_bmbt_rec_t)); |
| 2919 | } |
| 2920 | /* |
| 2921 | * Final choice, create a new extent page for |
| 2922 | * nex2 extents. |
| 2923 | */ |
| 2924 | else { |
| 2925 | erp_idx++; |
| 2926 | erp = xfs_iext_irec_new(ifp, erp_idx); |
| 2927 | } |
| 2928 | memmove(&erp->er_extbuf[i], nex2_ep, byte_diff); |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 2929 | kmem_free(nex2_ep); |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 2930 | erp->er_extcount += nex2; |
| 2931 | xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, nex2); |
| 2932 | } |
| 2933 | } |
| 2934 | |
| 2935 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2936 | * This is called when the amount of space required for incore file |
| 2937 | * extents needs to be decreased. The ext_diff parameter stores the |
| 2938 | * number of extents to be removed and the idx parameter contains |
| 2939 | * the extent index where the extents will be removed from. |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 2940 | * |
| 2941 | * If the amount of space needed has decreased below the linear |
| 2942 | * limit, XFS_IEXT_BUFSZ, then switch to using the contiguous |
| 2943 | * extent array. Otherwise, use kmem_realloc() to adjust the |
| 2944 | * size to what is needed. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2945 | */ |
| 2946 | void |
| 2947 | xfs_iext_remove( |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 2948 | xfs_inode_t *ip, /* incore inode pointer */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2949 | xfs_extnum_t idx, /* index to begin removing exts */ |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 2950 | int ext_diff, /* number of extents to remove */ |
| 2951 | int state) /* type of extent conversion */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2952 | { |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 2953 | xfs_ifork_t *ifp = (state & BMAP_ATTRFORK) ? ip->i_afp : &ip->i_df; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2954 | xfs_extnum_t nextents; /* number of extents in file */ |
| 2955 | int new_size; /* size of extents after removal */ |
| 2956 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2957 | trace_xfs_iext_remove(ip, idx, state, _RET_IP_); |
| 2958 | |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2959 | ASSERT(ext_diff > 0); |
| 2960 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 2961 | new_size = (nextents - ext_diff) * sizeof(xfs_bmbt_rec_t); |
| 2962 | |
| 2963 | if (new_size == 0) { |
| 2964 | xfs_iext_destroy(ifp); |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 2965 | } else if (ifp->if_flags & XFS_IFEXTIREC) { |
| 2966 | xfs_iext_remove_indirect(ifp, idx, ext_diff); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2967 | } else if (ifp->if_real_bytes) { |
| 2968 | xfs_iext_remove_direct(ifp, idx, ext_diff); |
| 2969 | } else { |
| 2970 | xfs_iext_remove_inline(ifp, idx, ext_diff); |
| 2971 | } |
| 2972 | ifp->if_bytes = new_size; |
| 2973 | } |
| 2974 | |
| 2975 | /* |
| 2976 | * This removes ext_diff extents from the inline buffer, beginning |
| 2977 | * at extent index idx. |
| 2978 | */ |
| 2979 | void |
| 2980 | xfs_iext_remove_inline( |
| 2981 | xfs_ifork_t *ifp, /* inode fork pointer */ |
| 2982 | xfs_extnum_t idx, /* index to begin removing exts */ |
| 2983 | int ext_diff) /* number of extents to remove */ |
| 2984 | { |
| 2985 | int nextents; /* number of extents in file */ |
| 2986 | |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 2987 | ASSERT(!(ifp->if_flags & XFS_IFEXTIREC)); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2988 | ASSERT(idx < XFS_INLINE_EXTS); |
| 2989 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 2990 | ASSERT(((nextents - ext_diff) > 0) && |
| 2991 | (nextents - ext_diff) < XFS_INLINE_EXTS); |
| 2992 | |
| 2993 | if (idx + ext_diff < nextents) { |
| 2994 | memmove(&ifp->if_u2.if_inline_ext[idx], |
| 2995 | &ifp->if_u2.if_inline_ext[idx + ext_diff], |
| 2996 | (nextents - (idx + ext_diff)) * |
| 2997 | sizeof(xfs_bmbt_rec_t)); |
| 2998 | memset(&ifp->if_u2.if_inline_ext[nextents - ext_diff], |
| 2999 | 0, ext_diff * sizeof(xfs_bmbt_rec_t)); |
| 3000 | } else { |
| 3001 | memset(&ifp->if_u2.if_inline_ext[idx], 0, |
| 3002 | ext_diff * sizeof(xfs_bmbt_rec_t)); |
| 3003 | } |
| 3004 | } |
| 3005 | |
| 3006 | /* |
| 3007 | * This removes ext_diff extents from a linear (direct) extent list, |
| 3008 | * beginning at extent index idx. If the extents are being removed |
| 3009 | * from the end of the list (ie. truncate) then we just need to re- |
| 3010 | * allocate the list to remove the extra space. Otherwise, if the |
| 3011 | * extents are being removed from the middle of the existing extent |
| 3012 | * entries, then we first need to move the extent records beginning |
| 3013 | * at idx + ext_diff up in the list to overwrite the records being |
| 3014 | * removed, then remove the extra space via kmem_realloc. |
| 3015 | */ |
| 3016 | void |
| 3017 | xfs_iext_remove_direct( |
| 3018 | xfs_ifork_t *ifp, /* inode fork pointer */ |
| 3019 | xfs_extnum_t idx, /* index to begin removing exts */ |
| 3020 | int ext_diff) /* number of extents to remove */ |
| 3021 | { |
| 3022 | xfs_extnum_t nextents; /* number of extents in file */ |
| 3023 | int new_size; /* size of extents after removal */ |
| 3024 | |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3025 | ASSERT(!(ifp->if_flags & XFS_IFEXTIREC)); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3026 | new_size = ifp->if_bytes - |
| 3027 | (ext_diff * sizeof(xfs_bmbt_rec_t)); |
| 3028 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 3029 | |
| 3030 | if (new_size == 0) { |
| 3031 | xfs_iext_destroy(ifp); |
| 3032 | return; |
| 3033 | } |
| 3034 | /* Move extents up in the list (if needed) */ |
| 3035 | if (idx + ext_diff < nextents) { |
| 3036 | memmove(&ifp->if_u1.if_extents[idx], |
| 3037 | &ifp->if_u1.if_extents[idx + ext_diff], |
| 3038 | (nextents - (idx + ext_diff)) * |
| 3039 | sizeof(xfs_bmbt_rec_t)); |
| 3040 | } |
| 3041 | memset(&ifp->if_u1.if_extents[nextents - ext_diff], |
| 3042 | 0, ext_diff * sizeof(xfs_bmbt_rec_t)); |
| 3043 | /* |
| 3044 | * Reallocate the direct extent list. If the extents |
| 3045 | * will fit inside the inode then xfs_iext_realloc_direct |
| 3046 | * will switch from direct to inline extent allocation |
| 3047 | * mode for us. |
| 3048 | */ |
| 3049 | xfs_iext_realloc_direct(ifp, new_size); |
| 3050 | ifp->if_bytes = new_size; |
| 3051 | } |
| 3052 | |
| 3053 | /* |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3054 | * This is called when incore extents are being removed from the |
| 3055 | * indirection array and the extents being removed span multiple extent |
| 3056 | * buffers. The idx parameter contains the file extent index where we |
| 3057 | * want to begin removing extents, and the count parameter contains |
| 3058 | * how many extents need to be removed. |
| 3059 | * |
| 3060 | * |-------| |-------| |
| 3061 | * | nex1 | | | nex1 - number of extents before idx |
| 3062 | * |-------| | count | |
| 3063 | * | | | | count - number of extents being removed at idx |
| 3064 | * | count | |-------| |
| 3065 | * | | | nex2 | nex2 - number of extents after idx + count |
| 3066 | * |-------| |-------| |
| 3067 | */ |
| 3068 | void |
| 3069 | xfs_iext_remove_indirect( |
| 3070 | xfs_ifork_t *ifp, /* inode fork pointer */ |
| 3071 | xfs_extnum_t idx, /* index to begin removing extents */ |
| 3072 | int count) /* number of extents to remove */ |
| 3073 | { |
| 3074 | xfs_ext_irec_t *erp; /* indirection array pointer */ |
| 3075 | int erp_idx = 0; /* indirection array index */ |
| 3076 | xfs_extnum_t ext_cnt; /* extents left to remove */ |
| 3077 | xfs_extnum_t ext_diff; /* extents to remove in current list */ |
| 3078 | xfs_extnum_t nex1; /* number of extents before idx */ |
| 3079 | xfs_extnum_t nex2; /* extents after idx + count */ |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3080 | int page_idx = idx; /* index in target extent list */ |
| 3081 | |
| 3082 | ASSERT(ifp->if_flags & XFS_IFEXTIREC); |
| 3083 | erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0); |
| 3084 | ASSERT(erp != NULL); |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3085 | nex1 = page_idx; |
| 3086 | ext_cnt = count; |
| 3087 | while (ext_cnt) { |
| 3088 | nex2 = MAX((erp->er_extcount - (nex1 + ext_cnt)), 0); |
| 3089 | ext_diff = MIN(ext_cnt, (erp->er_extcount - nex1)); |
| 3090 | /* |
| 3091 | * Check for deletion of entire list; |
| 3092 | * xfs_iext_irec_remove() updates extent offsets. |
| 3093 | */ |
| 3094 | if (ext_diff == erp->er_extcount) { |
| 3095 | xfs_iext_irec_remove(ifp, erp_idx); |
| 3096 | ext_cnt -= ext_diff; |
| 3097 | nex1 = 0; |
| 3098 | if (ext_cnt) { |
| 3099 | ASSERT(erp_idx < ifp->if_real_bytes / |
| 3100 | XFS_IEXT_BUFSZ); |
| 3101 | erp = &ifp->if_u1.if_ext_irec[erp_idx]; |
| 3102 | nex1 = 0; |
| 3103 | continue; |
| 3104 | } else { |
| 3105 | break; |
| 3106 | } |
| 3107 | } |
| 3108 | /* Move extents up (if needed) */ |
| 3109 | if (nex2) { |
| 3110 | memmove(&erp->er_extbuf[nex1], |
| 3111 | &erp->er_extbuf[nex1 + ext_diff], |
| 3112 | nex2 * sizeof(xfs_bmbt_rec_t)); |
| 3113 | } |
| 3114 | /* Zero out rest of page */ |
| 3115 | memset(&erp->er_extbuf[nex1 + nex2], 0, (XFS_IEXT_BUFSZ - |
| 3116 | ((nex1 + nex2) * sizeof(xfs_bmbt_rec_t)))); |
| 3117 | /* Update remaining counters */ |
| 3118 | erp->er_extcount -= ext_diff; |
| 3119 | xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -ext_diff); |
| 3120 | ext_cnt -= ext_diff; |
| 3121 | nex1 = 0; |
| 3122 | erp_idx++; |
| 3123 | erp++; |
| 3124 | } |
| 3125 | ifp->if_bytes -= count * sizeof(xfs_bmbt_rec_t); |
| 3126 | xfs_iext_irec_compact(ifp); |
| 3127 | } |
| 3128 | |
| 3129 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3130 | * Create, destroy, or resize a linear (direct) block of extents. |
| 3131 | */ |
| 3132 | void |
| 3133 | xfs_iext_realloc_direct( |
| 3134 | xfs_ifork_t *ifp, /* inode fork pointer */ |
| 3135 | int new_size) /* new size of extents */ |
| 3136 | { |
| 3137 | int rnew_size; /* real new size of extents */ |
| 3138 | |
| 3139 | rnew_size = new_size; |
| 3140 | |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3141 | ASSERT(!(ifp->if_flags & XFS_IFEXTIREC) || |
| 3142 | ((new_size >= 0) && (new_size <= XFS_IEXT_BUFSZ) && |
| 3143 | (new_size != ifp->if_real_bytes))); |
| 3144 | |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3145 | /* Free extent records */ |
| 3146 | if (new_size == 0) { |
| 3147 | xfs_iext_destroy(ifp); |
| 3148 | } |
| 3149 | /* Resize direct extent list and zero any new bytes */ |
| 3150 | else if (ifp->if_real_bytes) { |
| 3151 | /* Check if extents will fit inside the inode */ |
| 3152 | if (new_size <= XFS_INLINE_EXTS * sizeof(xfs_bmbt_rec_t)) { |
| 3153 | xfs_iext_direct_to_inline(ifp, new_size / |
| 3154 | (uint)sizeof(xfs_bmbt_rec_t)); |
| 3155 | ifp->if_bytes = new_size; |
| 3156 | return; |
| 3157 | } |
Vignesh Babu | 16a087d | 2007-06-28 16:46:37 +1000 | [diff] [blame] | 3158 | if (!is_power_of_2(new_size)){ |
Robert P. J. Day | 40ebd81 | 2007-11-23 16:30:51 +1100 | [diff] [blame] | 3159 | rnew_size = roundup_pow_of_two(new_size); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3160 | } |
| 3161 | if (rnew_size != ifp->if_real_bytes) { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3162 | ifp->if_u1.if_extents = |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3163 | kmem_realloc(ifp->if_u1.if_extents, |
| 3164 | rnew_size, |
David Chinner | 6785073 | 2008-08-13 16:02:51 +1000 | [diff] [blame] | 3165 | ifp->if_real_bytes, KM_NOFS); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3166 | } |
| 3167 | if (rnew_size > ifp->if_real_bytes) { |
| 3168 | memset(&ifp->if_u1.if_extents[ifp->if_bytes / |
| 3169 | (uint)sizeof(xfs_bmbt_rec_t)], 0, |
| 3170 | rnew_size - ifp->if_real_bytes); |
| 3171 | } |
| 3172 | } |
| 3173 | /* |
| 3174 | * Switch from the inline extent buffer to a direct |
| 3175 | * extent list. Be sure to include the inline extent |
| 3176 | * bytes in new_size. |
| 3177 | */ |
| 3178 | else { |
| 3179 | new_size += ifp->if_bytes; |
Vignesh Babu | 16a087d | 2007-06-28 16:46:37 +1000 | [diff] [blame] | 3180 | if (!is_power_of_2(new_size)) { |
Robert P. J. Day | 40ebd81 | 2007-11-23 16:30:51 +1100 | [diff] [blame] | 3181 | rnew_size = roundup_pow_of_two(new_size); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3182 | } |
| 3183 | xfs_iext_inline_to_direct(ifp, rnew_size); |
| 3184 | } |
| 3185 | ifp->if_real_bytes = rnew_size; |
| 3186 | ifp->if_bytes = new_size; |
| 3187 | } |
| 3188 | |
| 3189 | /* |
| 3190 | * Switch from linear (direct) extent records to inline buffer. |
| 3191 | */ |
| 3192 | void |
| 3193 | xfs_iext_direct_to_inline( |
| 3194 | xfs_ifork_t *ifp, /* inode fork pointer */ |
| 3195 | xfs_extnum_t nextents) /* number of extents in file */ |
| 3196 | { |
| 3197 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
| 3198 | ASSERT(nextents <= XFS_INLINE_EXTS); |
| 3199 | /* |
| 3200 | * The inline buffer was zeroed when we switched |
| 3201 | * from inline to direct extent allocation mode, |
| 3202 | * so we don't need to clear it here. |
| 3203 | */ |
| 3204 | memcpy(ifp->if_u2.if_inline_ext, ifp->if_u1.if_extents, |
| 3205 | nextents * sizeof(xfs_bmbt_rec_t)); |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 3206 | kmem_free(ifp->if_u1.if_extents); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3207 | ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext; |
| 3208 | ifp->if_real_bytes = 0; |
| 3209 | } |
| 3210 | |
| 3211 | /* |
| 3212 | * Switch from inline buffer to linear (direct) extent records. |
| 3213 | * new_size should already be rounded up to the next power of 2 |
| 3214 | * by the caller (when appropriate), so use new_size as it is. |
| 3215 | * However, since new_size may be rounded up, we can't update |
| 3216 | * if_bytes here. It is the caller's responsibility to update |
| 3217 | * if_bytes upon return. |
| 3218 | */ |
| 3219 | void |
| 3220 | xfs_iext_inline_to_direct( |
| 3221 | xfs_ifork_t *ifp, /* inode fork pointer */ |
| 3222 | int new_size) /* number of extents in file */ |
| 3223 | { |
David Chinner | 6785073 | 2008-08-13 16:02:51 +1000 | [diff] [blame] | 3224 | ifp->if_u1.if_extents = kmem_alloc(new_size, KM_NOFS); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3225 | memset(ifp->if_u1.if_extents, 0, new_size); |
| 3226 | if (ifp->if_bytes) { |
| 3227 | memcpy(ifp->if_u1.if_extents, ifp->if_u2.if_inline_ext, |
| 3228 | ifp->if_bytes); |
| 3229 | memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS * |
| 3230 | sizeof(xfs_bmbt_rec_t)); |
| 3231 | } |
| 3232 | ifp->if_real_bytes = new_size; |
| 3233 | } |
| 3234 | |
| 3235 | /* |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3236 | * Resize an extent indirection array to new_size bytes. |
| 3237 | */ |
Eric Sandeen | d96f8f8 | 2009-07-02 00:09:33 -0500 | [diff] [blame] | 3238 | STATIC void |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3239 | xfs_iext_realloc_indirect( |
| 3240 | xfs_ifork_t *ifp, /* inode fork pointer */ |
| 3241 | int new_size) /* new indirection array size */ |
| 3242 | { |
| 3243 | int nlists; /* number of irec's (ex lists) */ |
| 3244 | int size; /* current indirection array size */ |
| 3245 | |
| 3246 | ASSERT(ifp->if_flags & XFS_IFEXTIREC); |
| 3247 | nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ; |
| 3248 | size = nlists * sizeof(xfs_ext_irec_t); |
| 3249 | ASSERT(ifp->if_real_bytes); |
| 3250 | ASSERT((new_size >= 0) && (new_size != size)); |
| 3251 | if (new_size == 0) { |
| 3252 | xfs_iext_destroy(ifp); |
| 3253 | } else { |
| 3254 | ifp->if_u1.if_ext_irec = (xfs_ext_irec_t *) |
| 3255 | kmem_realloc(ifp->if_u1.if_ext_irec, |
David Chinner | 6785073 | 2008-08-13 16:02:51 +1000 | [diff] [blame] | 3256 | new_size, size, KM_NOFS); |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3257 | } |
| 3258 | } |
| 3259 | |
| 3260 | /* |
| 3261 | * Switch from indirection array to linear (direct) extent allocations. |
| 3262 | */ |
Eric Sandeen | d96f8f8 | 2009-07-02 00:09:33 -0500 | [diff] [blame] | 3263 | STATIC void |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3264 | xfs_iext_indirect_to_direct( |
| 3265 | xfs_ifork_t *ifp) /* inode fork pointer */ |
| 3266 | { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3267 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3268 | xfs_extnum_t nextents; /* number of extents in file */ |
| 3269 | int size; /* size of file extents */ |
| 3270 | |
| 3271 | ASSERT(ifp->if_flags & XFS_IFEXTIREC); |
| 3272 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 3273 | ASSERT(nextents <= XFS_LINEAR_EXTS); |
| 3274 | size = nextents * sizeof(xfs_bmbt_rec_t); |
| 3275 | |
Lachlan McIlroy | 71a8c87 | 2008-09-26 12:17:57 +1000 | [diff] [blame] | 3276 | xfs_iext_irec_compact_pages(ifp); |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3277 | ASSERT(ifp->if_real_bytes == XFS_IEXT_BUFSZ); |
| 3278 | |
| 3279 | ep = ifp->if_u1.if_ext_irec->er_extbuf; |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 3280 | kmem_free(ifp->if_u1.if_ext_irec); |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3281 | ifp->if_flags &= ~XFS_IFEXTIREC; |
| 3282 | ifp->if_u1.if_extents = ep; |
| 3283 | ifp->if_bytes = size; |
| 3284 | if (nextents < XFS_LINEAR_EXTS) { |
| 3285 | xfs_iext_realloc_direct(ifp, size); |
| 3286 | } |
| 3287 | } |
| 3288 | |
| 3289 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3290 | * Free incore file extents. |
| 3291 | */ |
| 3292 | void |
| 3293 | xfs_iext_destroy( |
| 3294 | xfs_ifork_t *ifp) /* inode fork pointer */ |
| 3295 | { |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3296 | if (ifp->if_flags & XFS_IFEXTIREC) { |
| 3297 | int erp_idx; |
| 3298 | int nlists; |
| 3299 | |
| 3300 | nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ; |
| 3301 | for (erp_idx = nlists - 1; erp_idx >= 0 ; erp_idx--) { |
| 3302 | xfs_iext_irec_remove(ifp, erp_idx); |
| 3303 | } |
| 3304 | ifp->if_flags &= ~XFS_IFEXTIREC; |
| 3305 | } else if (ifp->if_real_bytes) { |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 3306 | kmem_free(ifp->if_u1.if_extents); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3307 | } else if (ifp->if_bytes) { |
| 3308 | memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS * |
| 3309 | sizeof(xfs_bmbt_rec_t)); |
| 3310 | } |
| 3311 | ifp->if_u1.if_extents = NULL; |
| 3312 | ifp->if_real_bytes = 0; |
| 3313 | ifp->if_bytes = 0; |
| 3314 | } |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3315 | |
| 3316 | /* |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 3317 | * Return a pointer to the extent record for file system block bno. |
| 3318 | */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3319 | xfs_bmbt_rec_host_t * /* pointer to found extent record */ |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 3320 | xfs_iext_bno_to_ext( |
| 3321 | xfs_ifork_t *ifp, /* inode fork pointer */ |
| 3322 | xfs_fileoff_t bno, /* block number to search for */ |
| 3323 | xfs_extnum_t *idxp) /* index of target extent */ |
| 3324 | { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3325 | xfs_bmbt_rec_host_t *base; /* pointer to first extent */ |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 3326 | xfs_filblks_t blockcount = 0; /* number of blocks in extent */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3327 | xfs_bmbt_rec_host_t *ep = NULL; /* pointer to target extent */ |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 3328 | xfs_ext_irec_t *erp = NULL; /* indirection array pointer */ |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 3329 | int high; /* upper boundary in search */ |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 3330 | xfs_extnum_t idx = 0; /* index of target extent */ |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 3331 | int low; /* lower boundary in search */ |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 3332 | xfs_extnum_t nextents; /* number of file extents */ |
| 3333 | xfs_fileoff_t startoff = 0; /* start offset of extent */ |
| 3334 | |
| 3335 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 3336 | if (nextents == 0) { |
| 3337 | *idxp = 0; |
| 3338 | return NULL; |
| 3339 | } |
| 3340 | low = 0; |
| 3341 | if (ifp->if_flags & XFS_IFEXTIREC) { |
| 3342 | /* Find target extent list */ |
| 3343 | int erp_idx = 0; |
| 3344 | erp = xfs_iext_bno_to_irec(ifp, bno, &erp_idx); |
| 3345 | base = erp->er_extbuf; |
| 3346 | high = erp->er_extcount - 1; |
| 3347 | } else { |
| 3348 | base = ifp->if_u1.if_extents; |
| 3349 | high = nextents - 1; |
| 3350 | } |
| 3351 | /* Binary search extent records */ |
| 3352 | while (low <= high) { |
| 3353 | idx = (low + high) >> 1; |
| 3354 | ep = base + idx; |
| 3355 | startoff = xfs_bmbt_get_startoff(ep); |
| 3356 | blockcount = xfs_bmbt_get_blockcount(ep); |
| 3357 | if (bno < startoff) { |
| 3358 | high = idx - 1; |
| 3359 | } else if (bno >= startoff + blockcount) { |
| 3360 | low = idx + 1; |
| 3361 | } else { |
| 3362 | /* Convert back to file-based extent index */ |
| 3363 | if (ifp->if_flags & XFS_IFEXTIREC) { |
| 3364 | idx += erp->er_extoff; |
| 3365 | } |
| 3366 | *idxp = idx; |
| 3367 | return ep; |
| 3368 | } |
| 3369 | } |
| 3370 | /* Convert back to file-based extent index */ |
| 3371 | if (ifp->if_flags & XFS_IFEXTIREC) { |
| 3372 | idx += erp->er_extoff; |
| 3373 | } |
| 3374 | if (bno >= startoff + blockcount) { |
| 3375 | if (++idx == nextents) { |
| 3376 | ep = NULL; |
| 3377 | } else { |
| 3378 | ep = xfs_iext_get_ext(ifp, idx); |
| 3379 | } |
| 3380 | } |
| 3381 | *idxp = idx; |
| 3382 | return ep; |
| 3383 | } |
| 3384 | |
| 3385 | /* |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3386 | * Return a pointer to the indirection array entry containing the |
| 3387 | * extent record for filesystem block bno. Store the index of the |
| 3388 | * target irec in *erp_idxp. |
| 3389 | */ |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 3390 | xfs_ext_irec_t * /* pointer to found extent record */ |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3391 | xfs_iext_bno_to_irec( |
| 3392 | xfs_ifork_t *ifp, /* inode fork pointer */ |
| 3393 | xfs_fileoff_t bno, /* block number to search for */ |
| 3394 | int *erp_idxp) /* irec index of target ext list */ |
| 3395 | { |
| 3396 | xfs_ext_irec_t *erp = NULL; /* indirection array pointer */ |
| 3397 | xfs_ext_irec_t *erp_next; /* next indirection array entry */ |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 3398 | int erp_idx; /* indirection array index */ |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3399 | int nlists; /* number of extent irec's (lists) */ |
| 3400 | int high; /* binary search upper limit */ |
| 3401 | int low; /* binary search lower limit */ |
| 3402 | |
| 3403 | ASSERT(ifp->if_flags & XFS_IFEXTIREC); |
| 3404 | nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ; |
| 3405 | erp_idx = 0; |
| 3406 | low = 0; |
| 3407 | high = nlists - 1; |
| 3408 | while (low <= high) { |
| 3409 | erp_idx = (low + high) >> 1; |
| 3410 | erp = &ifp->if_u1.if_ext_irec[erp_idx]; |
| 3411 | erp_next = erp_idx < nlists - 1 ? erp + 1 : NULL; |
| 3412 | if (bno < xfs_bmbt_get_startoff(erp->er_extbuf)) { |
| 3413 | high = erp_idx - 1; |
| 3414 | } else if (erp_next && bno >= |
| 3415 | xfs_bmbt_get_startoff(erp_next->er_extbuf)) { |
| 3416 | low = erp_idx + 1; |
| 3417 | } else { |
| 3418 | break; |
| 3419 | } |
| 3420 | } |
| 3421 | *erp_idxp = erp_idx; |
| 3422 | return erp; |
| 3423 | } |
| 3424 | |
| 3425 | /* |
| 3426 | * Return a pointer to the indirection array entry containing the |
| 3427 | * extent record at file extent index *idxp. Store the index of the |
| 3428 | * target irec in *erp_idxp and store the page index of the target |
| 3429 | * extent record in *idxp. |
| 3430 | */ |
| 3431 | xfs_ext_irec_t * |
| 3432 | xfs_iext_idx_to_irec( |
| 3433 | xfs_ifork_t *ifp, /* inode fork pointer */ |
| 3434 | xfs_extnum_t *idxp, /* extent index (file -> page) */ |
| 3435 | int *erp_idxp, /* pointer to target irec */ |
| 3436 | int realloc) /* new bytes were just added */ |
| 3437 | { |
| 3438 | xfs_ext_irec_t *prev; /* pointer to previous irec */ |
| 3439 | xfs_ext_irec_t *erp = NULL; /* pointer to current irec */ |
| 3440 | int erp_idx; /* indirection array index */ |
| 3441 | int nlists; /* number of irec's (ex lists) */ |
| 3442 | int high; /* binary search upper limit */ |
| 3443 | int low; /* binary search lower limit */ |
| 3444 | xfs_extnum_t page_idx = *idxp; /* extent index in target list */ |
| 3445 | |
| 3446 | ASSERT(ifp->if_flags & XFS_IFEXTIREC); |
Christoph Hellwig | 87bef18 | 2011-05-11 15:04:11 +0000 | [diff] [blame] | 3447 | ASSERT(page_idx >= 0); |
| 3448 | ASSERT(page_idx <= ifp->if_bytes / sizeof(xfs_bmbt_rec_t)); |
| 3449 | ASSERT(page_idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t) || realloc); |
| 3450 | |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3451 | nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ; |
| 3452 | erp_idx = 0; |
| 3453 | low = 0; |
| 3454 | high = nlists - 1; |
| 3455 | |
| 3456 | /* Binary search extent irec's */ |
| 3457 | while (low <= high) { |
| 3458 | erp_idx = (low + high) >> 1; |
| 3459 | erp = &ifp->if_u1.if_ext_irec[erp_idx]; |
| 3460 | prev = erp_idx > 0 ? erp - 1 : NULL; |
| 3461 | if (page_idx < erp->er_extoff || (page_idx == erp->er_extoff && |
| 3462 | realloc && prev && prev->er_extcount < XFS_LINEAR_EXTS)) { |
| 3463 | high = erp_idx - 1; |
| 3464 | } else if (page_idx > erp->er_extoff + erp->er_extcount || |
| 3465 | (page_idx == erp->er_extoff + erp->er_extcount && |
| 3466 | !realloc)) { |
| 3467 | low = erp_idx + 1; |
| 3468 | } else if (page_idx == erp->er_extoff + erp->er_extcount && |
| 3469 | erp->er_extcount == XFS_LINEAR_EXTS) { |
| 3470 | ASSERT(realloc); |
| 3471 | page_idx = 0; |
| 3472 | erp_idx++; |
| 3473 | erp = erp_idx < nlists ? erp + 1 : NULL; |
| 3474 | break; |
| 3475 | } else { |
| 3476 | page_idx -= erp->er_extoff; |
| 3477 | break; |
| 3478 | } |
| 3479 | } |
| 3480 | *idxp = page_idx; |
| 3481 | *erp_idxp = erp_idx; |
| 3482 | return(erp); |
| 3483 | } |
| 3484 | |
| 3485 | /* |
| 3486 | * Allocate and initialize an indirection array once the space needed |
| 3487 | * for incore extents increases above XFS_IEXT_BUFSZ. |
| 3488 | */ |
| 3489 | void |
| 3490 | xfs_iext_irec_init( |
| 3491 | xfs_ifork_t *ifp) /* inode fork pointer */ |
| 3492 | { |
| 3493 | xfs_ext_irec_t *erp; /* indirection array pointer */ |
| 3494 | xfs_extnum_t nextents; /* number of extents in file */ |
| 3495 | |
| 3496 | ASSERT(!(ifp->if_flags & XFS_IFEXTIREC)); |
| 3497 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 3498 | ASSERT(nextents <= XFS_LINEAR_EXTS); |
| 3499 | |
David Chinner | 6785073 | 2008-08-13 16:02:51 +1000 | [diff] [blame] | 3500 | erp = kmem_alloc(sizeof(xfs_ext_irec_t), KM_NOFS); |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3501 | |
| 3502 | if (nextents == 0) { |
David Chinner | 6785073 | 2008-08-13 16:02:51 +1000 | [diff] [blame] | 3503 | ifp->if_u1.if_extents = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS); |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3504 | } else if (!ifp->if_real_bytes) { |
| 3505 | xfs_iext_inline_to_direct(ifp, XFS_IEXT_BUFSZ); |
| 3506 | } else if (ifp->if_real_bytes < XFS_IEXT_BUFSZ) { |
| 3507 | xfs_iext_realloc_direct(ifp, XFS_IEXT_BUFSZ); |
| 3508 | } |
| 3509 | erp->er_extbuf = ifp->if_u1.if_extents; |
| 3510 | erp->er_extcount = nextents; |
| 3511 | erp->er_extoff = 0; |
| 3512 | |
| 3513 | ifp->if_flags |= XFS_IFEXTIREC; |
| 3514 | ifp->if_real_bytes = XFS_IEXT_BUFSZ; |
| 3515 | ifp->if_bytes = nextents * sizeof(xfs_bmbt_rec_t); |
| 3516 | ifp->if_u1.if_ext_irec = erp; |
| 3517 | |
| 3518 | return; |
| 3519 | } |
| 3520 | |
| 3521 | /* |
| 3522 | * Allocate and initialize a new entry in the indirection array. |
| 3523 | */ |
| 3524 | xfs_ext_irec_t * |
| 3525 | xfs_iext_irec_new( |
| 3526 | xfs_ifork_t *ifp, /* inode fork pointer */ |
| 3527 | int erp_idx) /* index for new irec */ |
| 3528 | { |
| 3529 | xfs_ext_irec_t *erp; /* indirection array pointer */ |
| 3530 | int i; /* loop counter */ |
| 3531 | int nlists; /* number of irec's (ex lists) */ |
| 3532 | |
| 3533 | ASSERT(ifp->if_flags & XFS_IFEXTIREC); |
| 3534 | nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ; |
| 3535 | |
| 3536 | /* Resize indirection array */ |
| 3537 | xfs_iext_realloc_indirect(ifp, ++nlists * |
| 3538 | sizeof(xfs_ext_irec_t)); |
| 3539 | /* |
| 3540 | * Move records down in the array so the |
| 3541 | * new page can use erp_idx. |
| 3542 | */ |
| 3543 | erp = ifp->if_u1.if_ext_irec; |
| 3544 | for (i = nlists - 1; i > erp_idx; i--) { |
| 3545 | memmove(&erp[i], &erp[i-1], sizeof(xfs_ext_irec_t)); |
| 3546 | } |
| 3547 | ASSERT(i == erp_idx); |
| 3548 | |
| 3549 | /* Initialize new extent record */ |
| 3550 | erp = ifp->if_u1.if_ext_irec; |
David Chinner | 6785073 | 2008-08-13 16:02:51 +1000 | [diff] [blame] | 3551 | erp[erp_idx].er_extbuf = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS); |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3552 | ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ; |
| 3553 | memset(erp[erp_idx].er_extbuf, 0, XFS_IEXT_BUFSZ); |
| 3554 | erp[erp_idx].er_extcount = 0; |
| 3555 | erp[erp_idx].er_extoff = erp_idx > 0 ? |
| 3556 | erp[erp_idx-1].er_extoff + erp[erp_idx-1].er_extcount : 0; |
| 3557 | return (&erp[erp_idx]); |
| 3558 | } |
| 3559 | |
| 3560 | /* |
| 3561 | * Remove a record from the indirection array. |
| 3562 | */ |
| 3563 | void |
| 3564 | xfs_iext_irec_remove( |
| 3565 | xfs_ifork_t *ifp, /* inode fork pointer */ |
| 3566 | int erp_idx) /* irec index to remove */ |
| 3567 | { |
| 3568 | xfs_ext_irec_t *erp; /* indirection array pointer */ |
| 3569 | int i; /* loop counter */ |
| 3570 | int nlists; /* number of irec's (ex lists) */ |
| 3571 | |
| 3572 | ASSERT(ifp->if_flags & XFS_IFEXTIREC); |
| 3573 | nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ; |
| 3574 | erp = &ifp->if_u1.if_ext_irec[erp_idx]; |
| 3575 | if (erp->er_extbuf) { |
| 3576 | xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, |
| 3577 | -erp->er_extcount); |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 3578 | kmem_free(erp->er_extbuf); |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3579 | } |
| 3580 | /* Compact extent records */ |
| 3581 | erp = ifp->if_u1.if_ext_irec; |
| 3582 | for (i = erp_idx; i < nlists - 1; i++) { |
| 3583 | memmove(&erp[i], &erp[i+1], sizeof(xfs_ext_irec_t)); |
| 3584 | } |
| 3585 | /* |
| 3586 | * Manually free the last extent record from the indirection |
| 3587 | * array. A call to xfs_iext_realloc_indirect() with a size |
| 3588 | * of zero would result in a call to xfs_iext_destroy() which |
| 3589 | * would in turn call this function again, creating a nasty |
| 3590 | * infinite loop. |
| 3591 | */ |
| 3592 | if (--nlists) { |
| 3593 | xfs_iext_realloc_indirect(ifp, |
| 3594 | nlists * sizeof(xfs_ext_irec_t)); |
| 3595 | } else { |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 3596 | kmem_free(ifp->if_u1.if_ext_irec); |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3597 | } |
| 3598 | ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ; |
| 3599 | } |
| 3600 | |
| 3601 | /* |
| 3602 | * This is called to clean up large amounts of unused memory allocated |
| 3603 | * by the indirection array. Before compacting anything though, verify |
| 3604 | * that the indirection array is still needed and switch back to the |
| 3605 | * linear extent list (or even the inline buffer) if possible. The |
| 3606 | * compaction policy is as follows: |
| 3607 | * |
| 3608 | * Full Compaction: Extents fit into a single page (or inline buffer) |
Lachlan McIlroy | 71a8c87 | 2008-09-26 12:17:57 +1000 | [diff] [blame] | 3609 | * Partial Compaction: Extents occupy less than 50% of allocated space |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3610 | * No Compaction: Extents occupy at least 50% of allocated space |
| 3611 | */ |
| 3612 | void |
| 3613 | xfs_iext_irec_compact( |
| 3614 | xfs_ifork_t *ifp) /* inode fork pointer */ |
| 3615 | { |
| 3616 | xfs_extnum_t nextents; /* number of extents in file */ |
| 3617 | int nlists; /* number of irec's (ex lists) */ |
| 3618 | |
| 3619 | ASSERT(ifp->if_flags & XFS_IFEXTIREC); |
| 3620 | nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ; |
| 3621 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 3622 | |
| 3623 | if (nextents == 0) { |
| 3624 | xfs_iext_destroy(ifp); |
| 3625 | } else if (nextents <= XFS_INLINE_EXTS) { |
| 3626 | xfs_iext_indirect_to_direct(ifp); |
| 3627 | xfs_iext_direct_to_inline(ifp, nextents); |
| 3628 | } else if (nextents <= XFS_LINEAR_EXTS) { |
| 3629 | xfs_iext_indirect_to_direct(ifp); |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3630 | } else if (nextents < (nlists * XFS_LINEAR_EXTS) >> 1) { |
| 3631 | xfs_iext_irec_compact_pages(ifp); |
| 3632 | } |
| 3633 | } |
| 3634 | |
| 3635 | /* |
| 3636 | * Combine extents from neighboring extent pages. |
| 3637 | */ |
| 3638 | void |
| 3639 | xfs_iext_irec_compact_pages( |
| 3640 | xfs_ifork_t *ifp) /* inode fork pointer */ |
| 3641 | { |
| 3642 | xfs_ext_irec_t *erp, *erp_next;/* pointers to irec entries */ |
| 3643 | int erp_idx = 0; /* indirection array index */ |
| 3644 | int nlists; /* number of irec's (ex lists) */ |
| 3645 | |
| 3646 | ASSERT(ifp->if_flags & XFS_IFEXTIREC); |
| 3647 | nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ; |
| 3648 | while (erp_idx < nlists - 1) { |
| 3649 | erp = &ifp->if_u1.if_ext_irec[erp_idx]; |
| 3650 | erp_next = erp + 1; |
| 3651 | if (erp_next->er_extcount <= |
| 3652 | (XFS_LINEAR_EXTS - erp->er_extcount)) { |
Lachlan McIlroy | 71a8c87 | 2008-09-26 12:17:57 +1000 | [diff] [blame] | 3653 | memcpy(&erp->er_extbuf[erp->er_extcount], |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3654 | erp_next->er_extbuf, erp_next->er_extcount * |
| 3655 | sizeof(xfs_bmbt_rec_t)); |
| 3656 | erp->er_extcount += erp_next->er_extcount; |
| 3657 | /* |
| 3658 | * Free page before removing extent record |
| 3659 | * so er_extoffs don't get modified in |
| 3660 | * xfs_iext_irec_remove. |
| 3661 | */ |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 3662 | kmem_free(erp_next->er_extbuf); |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3663 | erp_next->er_extbuf = NULL; |
| 3664 | xfs_iext_irec_remove(ifp, erp_idx + 1); |
| 3665 | nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ; |
| 3666 | } else { |
| 3667 | erp_idx++; |
| 3668 | } |
| 3669 | } |
| 3670 | } |
| 3671 | |
| 3672 | /* |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3673 | * This is called to update the er_extoff field in the indirection |
| 3674 | * array when extents have been added or removed from one of the |
| 3675 | * extent lists. erp_idx contains the irec index to begin updating |
| 3676 | * at and ext_diff contains the number of extents that were added |
| 3677 | * or removed. |
| 3678 | */ |
| 3679 | void |
| 3680 | xfs_iext_irec_update_extoffs( |
| 3681 | xfs_ifork_t *ifp, /* inode fork pointer */ |
| 3682 | int erp_idx, /* irec index to update */ |
| 3683 | int ext_diff) /* number of new extents */ |
| 3684 | { |
| 3685 | int i; /* loop counter */ |
| 3686 | int nlists; /* number of irec's (ex lists */ |
| 3687 | |
| 3688 | ASSERT(ifp->if_flags & XFS_IFEXTIREC); |
| 3689 | nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ; |
| 3690 | for (i = erp_idx; i < nlists; i++) { |
| 3691 | ifp->if_u1.if_ext_irec[i].er_extoff += ext_diff; |
| 3692 | } |
| 3693 | } |