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" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 23 | #include "xfs_bit.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include "xfs_log.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 25 | #include "xfs_inum.h" |
| 26 | #include "xfs_imap.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include "xfs_trans.h" |
| 28 | #include "xfs_trans_priv.h" |
| 29 | #include "xfs_sb.h" |
| 30 | #include "xfs_ag.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include "xfs_dir2.h" |
| 32 | #include "xfs_dmapi.h" |
| 33 | #include "xfs_mount.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include "xfs_bmap_btree.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 35 | #include "xfs_alloc_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include "xfs_ialloc_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include "xfs_dir2_sf.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 38 | #include "xfs_attr_sf.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include "xfs_dinode.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include "xfs_inode.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include "xfs_buf_item.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 42 | #include "xfs_inode_item.h" |
| 43 | #include "xfs_btree.h" |
Christoph Hellwig | 8c4ed63 | 2008-10-30 16:55:13 +1100 | [diff] [blame] | 44 | #include "xfs_btree_trace.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 45 | #include "xfs_alloc.h" |
| 46 | #include "xfs_ialloc.h" |
| 47 | #include "xfs_bmap.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #include "xfs_rw.h" |
| 49 | #include "xfs_error.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | #include "xfs_utils.h" |
| 51 | #include "xfs_dir2_trace.h" |
| 52 | #include "xfs_quota.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | #include "xfs_acl.h" |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 54 | #include "xfs_filestream.h" |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 55 | #include "xfs_vnodeops.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | kmem_zone_t *xfs_ifork_zone; |
| 58 | kmem_zone_t *xfs_inode_zone; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
| 60 | /* |
| 61 | * Used in xfs_itruncate(). This is the maximum number of extents |
| 62 | * freed from a file in a single transaction. |
| 63 | */ |
| 64 | #define XFS_ITRUNC_MAX_EXTENTS 2 |
| 65 | |
| 66 | STATIC int xfs_iflush_int(xfs_inode_t *, xfs_buf_t *); |
| 67 | STATIC int xfs_iformat_local(xfs_inode_t *, xfs_dinode_t *, int, int); |
| 68 | STATIC int xfs_iformat_extents(xfs_inode_t *, xfs_dinode_t *, int); |
| 69 | STATIC int xfs_iformat_btree(xfs_inode_t *, xfs_dinode_t *, int); |
| 70 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | #ifdef DEBUG |
| 72 | /* |
| 73 | * Make sure that the extents in the given memory buffer |
| 74 | * are valid. |
| 75 | */ |
| 76 | STATIC void |
| 77 | xfs_validate_extents( |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 78 | xfs_ifork_t *ifp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | int nrecs, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | xfs_exntfmt_t fmt) |
| 81 | { |
| 82 | xfs_bmbt_irec_t irec; |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 83 | xfs_bmbt_rec_host_t rec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | int i; |
| 85 | |
| 86 | for (i = 0; i < nrecs; i++) { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 87 | xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i); |
| 88 | rec.l0 = get_unaligned(&ep->l0); |
| 89 | rec.l1 = get_unaligned(&ep->l1); |
| 90 | xfs_bmbt_get_all(&rec, &irec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | if (fmt == XFS_EXTFMT_NOSTATE) |
| 92 | ASSERT(irec.br_state == XFS_EXT_NORM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | #else /* DEBUG */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 96 | #define xfs_validate_extents(ifp, nrecs, fmt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | #endif /* DEBUG */ |
| 98 | |
| 99 | /* |
| 100 | * Check that none of the inode's in the buffer have a next |
| 101 | * unlinked field of 0. |
| 102 | */ |
| 103 | #if defined(DEBUG) |
| 104 | void |
| 105 | xfs_inobp_check( |
| 106 | xfs_mount_t *mp, |
| 107 | xfs_buf_t *bp) |
| 108 | { |
| 109 | int i; |
| 110 | int j; |
| 111 | xfs_dinode_t *dip; |
| 112 | |
| 113 | j = mp->m_inode_cluster_size >> mp->m_sb.sb_inodelog; |
| 114 | |
| 115 | for (i = 0; i < j; i++) { |
| 116 | dip = (xfs_dinode_t *)xfs_buf_offset(bp, |
| 117 | i * mp->m_sb.sb_inodesize); |
| 118 | if (!dip->di_next_unlinked) { |
| 119 | xfs_fs_cmn_err(CE_ALERT, mp, |
| 120 | "Detected a bogus zero next_unlinked field in incore inode buffer 0x%p. About to pop an ASSERT.", |
| 121 | bp); |
| 122 | ASSERT(dip->di_next_unlinked); |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | #endif |
| 127 | |
| 128 | /* |
David Chinner | 4ae29b4 | 2008-03-06 13:43:34 +1100 | [diff] [blame] | 129 | * Find the buffer associated with the given inode map |
| 130 | * We do basic validation checks on the buffer once it has been |
| 131 | * retrieved from disk. |
| 132 | */ |
| 133 | STATIC int |
| 134 | xfs_imap_to_bp( |
| 135 | xfs_mount_t *mp, |
| 136 | xfs_trans_t *tp, |
| 137 | xfs_imap_t *imap, |
| 138 | xfs_buf_t **bpp, |
| 139 | uint buf_flags, |
| 140 | uint imap_flags) |
| 141 | { |
| 142 | int error; |
| 143 | int i; |
| 144 | int ni; |
| 145 | xfs_buf_t *bp; |
| 146 | |
| 147 | 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] | 148 | (int)imap->im_len, buf_flags, &bp); |
David Chinner | 4ae29b4 | 2008-03-06 13:43:34 +1100 | [diff] [blame] | 149 | if (error) { |
David Chinner | a3f74ff | 2008-03-06 13:43:42 +1100 | [diff] [blame] | 150 | if (error != EAGAIN) { |
| 151 | cmn_err(CE_WARN, |
| 152 | "xfs_imap_to_bp: xfs_trans_read_buf()returned " |
David Chinner | 4ae29b4 | 2008-03-06 13:43:34 +1100 | [diff] [blame] | 153 | "an error %d on %s. Returning error.", |
| 154 | error, mp->m_fsname); |
David Chinner | a3f74ff | 2008-03-06 13:43:42 +1100 | [diff] [blame] | 155 | } else { |
| 156 | ASSERT(buf_flags & XFS_BUF_TRYLOCK); |
| 157 | } |
David Chinner | 4ae29b4 | 2008-03-06 13:43:34 +1100 | [diff] [blame] | 158 | return error; |
| 159 | } |
| 160 | |
| 161 | /* |
| 162 | * Validate the magic number and version of every inode in the buffer |
| 163 | * (if DEBUG kernel) or the first inode in the buffer, otherwise. |
| 164 | */ |
| 165 | #ifdef DEBUG |
| 166 | ni = BBTOB(imap->im_len) >> mp->m_sb.sb_inodelog; |
| 167 | #else /* usual case */ |
| 168 | ni = 1; |
| 169 | #endif |
| 170 | |
| 171 | for (i = 0; i < ni; i++) { |
| 172 | int di_ok; |
| 173 | xfs_dinode_t *dip; |
| 174 | |
| 175 | dip = (xfs_dinode_t *)xfs_buf_offset(bp, |
| 176 | (i << mp->m_sb.sb_inodelog)); |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 177 | di_ok = be16_to_cpu(dip->di_magic) == XFS_DINODE_MAGIC && |
| 178 | XFS_DINODE_GOOD_VERSION(dip->di_version); |
David Chinner | 4ae29b4 | 2008-03-06 13:43:34 +1100 | [diff] [blame] | 179 | if (unlikely(XFS_TEST_ERROR(!di_ok, mp, |
| 180 | XFS_ERRTAG_ITOBP_INOTOBP, |
| 181 | XFS_RANDOM_ITOBP_INOTOBP))) { |
| 182 | if (imap_flags & XFS_IMAP_BULKSTAT) { |
| 183 | xfs_trans_brelse(tp, bp); |
| 184 | return XFS_ERROR(EINVAL); |
| 185 | } |
| 186 | XFS_CORRUPTION_ERROR("xfs_imap_to_bp", |
| 187 | XFS_ERRLEVEL_HIGH, mp, dip); |
| 188 | #ifdef DEBUG |
| 189 | cmn_err(CE_PANIC, |
| 190 | "Device %s - bad inode magic/vsn " |
| 191 | "daddr %lld #%d (magic=%x)", |
| 192 | XFS_BUFTARG_NAME(mp->m_ddev_targp), |
| 193 | (unsigned long long)imap->im_blkno, i, |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 194 | be16_to_cpu(dip->di_magic)); |
David Chinner | 4ae29b4 | 2008-03-06 13:43:34 +1100 | [diff] [blame] | 195 | #endif |
| 196 | xfs_trans_brelse(tp, bp); |
| 197 | return XFS_ERROR(EFSCORRUPTED); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | xfs_inobp_check(mp, bp); |
| 202 | |
| 203 | /* |
| 204 | * Mark the buffer as an inode buffer now that it looks good |
| 205 | */ |
| 206 | XFS_BUF_SET_VTYPE(bp, B_FS_INO); |
| 207 | |
| 208 | *bpp = bp; |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | * This routine is called to map an inode number within a file |
| 214 | * system to the buffer containing the on-disk version of the |
| 215 | * inode. It returns a pointer to the buffer containing the |
| 216 | * on-disk inode in the bpp parameter, and in the dip parameter |
| 217 | * it returns a pointer to the on-disk inode within that buffer. |
| 218 | * |
| 219 | * If a non-zero error is returned, then the contents of bpp and |
| 220 | * dipp are undefined. |
| 221 | * |
| 222 | * Use xfs_imap() to determine the size and location of the |
| 223 | * buffer to read from disk. |
| 224 | */ |
Christoph Hellwig | c679eef | 2008-10-30 18:04:13 +1100 | [diff] [blame] | 225 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | xfs_inotobp( |
| 227 | xfs_mount_t *mp, |
| 228 | xfs_trans_t *tp, |
| 229 | xfs_ino_t ino, |
| 230 | xfs_dinode_t **dipp, |
| 231 | xfs_buf_t **bpp, |
Christoph Hellwig | c679eef | 2008-10-30 18:04:13 +1100 | [diff] [blame] | 232 | int *offset, |
| 233 | uint imap_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | xfs_imap_t imap; |
| 236 | xfs_buf_t *bp; |
| 237 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | imap.im_blkno = 0; |
Christoph Hellwig | c679eef | 2008-10-30 18:04:13 +1100 | [diff] [blame] | 240 | error = xfs_imap(mp, tp, ino, &imap, imap_flags | XFS_IMAP_LOOKUP); |
David Chinner | 4ae29b4 | 2008-03-06 13:43:34 +1100 | [diff] [blame] | 241 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | |
Christoph Hellwig | c679eef | 2008-10-30 18:04:13 +1100 | [diff] [blame] | 244 | error = xfs_imap_to_bp(mp, tp, &imap, &bp, XFS_BUF_LOCK, imap_flags); |
David Chinner | 4ae29b4 | 2008-03-06 13:43:34 +1100 | [diff] [blame] | 245 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | *dipp = (xfs_dinode_t *)xfs_buf_offset(bp, imap.im_boffset); |
| 249 | *bpp = bp; |
| 250 | *offset = imap.im_boffset; |
| 251 | return 0; |
| 252 | } |
| 253 | |
| 254 | |
| 255 | /* |
| 256 | * This routine is called to map an inode to the buffer containing |
| 257 | * the on-disk version of the inode. It returns a pointer to the |
| 258 | * buffer containing the on-disk inode in the bpp parameter, and in |
| 259 | * the dip parameter it returns a pointer to the on-disk inode within |
| 260 | * that buffer. |
| 261 | * |
| 262 | * If a non-zero error is returned, then the contents of bpp and |
| 263 | * dipp are undefined. |
| 264 | * |
Christoph Hellwig | 76d8b27 | 2008-11-28 14:23:40 +1100 | [diff] [blame^] | 265 | * The inode is expected to already been mapped to its buffer and read |
| 266 | * in once, thus we can use the mapping information stored in the inode |
| 267 | * rather than calling xfs_imap(). This allows us to avoid the overhead |
| 268 | * of looking at the inode btree for small block file systems |
| 269 | * (see xfs_dilocate()). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | */ |
| 271 | int |
| 272 | xfs_itobp( |
| 273 | xfs_mount_t *mp, |
| 274 | xfs_trans_t *tp, |
| 275 | xfs_inode_t *ip, |
| 276 | xfs_dinode_t **dipp, |
| 277 | xfs_buf_t **bpp, |
David Chinner | a3f74ff | 2008-03-06 13:43:42 +1100 | [diff] [blame] | 278 | uint buf_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | { |
Nathan Scott | 4d1a2ed | 2006-06-09 17:12:28 +1000 | [diff] [blame] | 280 | xfs_imap_t imap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | xfs_buf_t *bp; |
| 282 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | |
Christoph Hellwig | 76d8b27 | 2008-11-28 14:23:40 +1100 | [diff] [blame^] | 284 | ASSERT(ip->i_blkno != 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | |
Christoph Hellwig | 76d8b27 | 2008-11-28 14:23:40 +1100 | [diff] [blame^] | 286 | imap.im_blkno = ip->i_blkno; |
| 287 | imap.im_len = ip->i_len; |
| 288 | imap.im_boffset = ip->i_boffset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | |
Christoph Hellwig | 76d8b27 | 2008-11-28 14:23:40 +1100 | [diff] [blame^] | 290 | error = xfs_imap_to_bp(mp, tp, &imap, &bp, buf_flags, 0); |
David Chinner | 4ae29b4 | 2008-03-06 13:43:34 +1100 | [diff] [blame] | 291 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | return error; |
Nathan Scott | 4d1a2ed | 2006-06-09 17:12:28 +1000 | [diff] [blame] | 293 | |
David Chinner | a3f74ff | 2008-03-06 13:43:42 +1100 | [diff] [blame] | 294 | if (!bp) { |
| 295 | ASSERT(buf_flags & XFS_BUF_TRYLOCK); |
| 296 | ASSERT(tp == NULL); |
| 297 | *bpp = NULL; |
| 298 | return EAGAIN; |
| 299 | } |
| 300 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | *dipp = (xfs_dinode_t *)xfs_buf_offset(bp, imap.im_boffset); |
| 302 | *bpp = bp; |
| 303 | return 0; |
| 304 | } |
| 305 | |
| 306 | /* |
| 307 | * Move inode type and inode format specific information from the |
| 308 | * on-disk inode to the in-core inode. For fifos, devs, and sockets |
| 309 | * this means set if_rdev to the proper value. For files, directories, |
| 310 | * and symlinks this means to bring in the in-line data or extent |
| 311 | * pointers. For a file in B-tree format, only the root is immediately |
| 312 | * brought in-core. The rest will be in-lined in if_extents when it |
| 313 | * is first referenced (see xfs_iread_extents()). |
| 314 | */ |
| 315 | STATIC int |
| 316 | xfs_iformat( |
| 317 | xfs_inode_t *ip, |
| 318 | xfs_dinode_t *dip) |
| 319 | { |
| 320 | xfs_attr_shortform_t *atp; |
| 321 | int size; |
| 322 | int error; |
| 323 | xfs_fsize_t di_size; |
| 324 | ip->i_df.if_ext_max = |
| 325 | XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t); |
| 326 | error = 0; |
| 327 | |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 328 | if (unlikely(be32_to_cpu(dip->di_nextents) + |
| 329 | be16_to_cpu(dip->di_anextents) > |
| 330 | be64_to_cpu(dip->di_nblocks))) { |
Nathan Scott | 3762ec6 | 2006-01-12 10:29:53 +1100 | [diff] [blame] | 331 | xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount, |
| 332 | "corrupt dinode %Lu, extent total = %d, nblocks = %Lu.", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | (unsigned long long)ip->i_ino, |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 334 | (int)(be32_to_cpu(dip->di_nextents) + |
| 335 | be16_to_cpu(dip->di_anextents)), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | (unsigned long long) |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 337 | be64_to_cpu(dip->di_nblocks)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | XFS_CORRUPTION_ERROR("xfs_iformat(1)", XFS_ERRLEVEL_LOW, |
| 339 | ip->i_mount, dip); |
| 340 | return XFS_ERROR(EFSCORRUPTED); |
| 341 | } |
| 342 | |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 343 | if (unlikely(dip->di_forkoff > ip->i_mount->m_sb.sb_inodesize)) { |
Nathan Scott | 3762ec6 | 2006-01-12 10:29:53 +1100 | [diff] [blame] | 344 | xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount, |
| 345 | "corrupt dinode %Lu, forkoff = 0x%x.", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | (unsigned long long)ip->i_ino, |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 347 | dip->di_forkoff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | XFS_CORRUPTION_ERROR("xfs_iformat(2)", XFS_ERRLEVEL_LOW, |
| 349 | ip->i_mount, dip); |
| 350 | return XFS_ERROR(EFSCORRUPTED); |
| 351 | } |
| 352 | |
| 353 | switch (ip->i_d.di_mode & S_IFMT) { |
| 354 | case S_IFIFO: |
| 355 | case S_IFCHR: |
| 356 | case S_IFBLK: |
| 357 | case S_IFSOCK: |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 358 | if (unlikely(dip->di_format != XFS_DINODE_FMT_DEV)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | XFS_CORRUPTION_ERROR("xfs_iformat(3)", XFS_ERRLEVEL_LOW, |
| 360 | ip->i_mount, dip); |
| 361 | return XFS_ERROR(EFSCORRUPTED); |
| 362 | } |
| 363 | ip->i_d.di_size = 0; |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 364 | ip->i_size = 0; |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 365 | ip->i_df.if_u2.if_rdev = xfs_dinode_get_rdev(dip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | break; |
| 367 | |
| 368 | case S_IFREG: |
| 369 | case S_IFLNK: |
| 370 | case S_IFDIR: |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 371 | switch (dip->di_format) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | case XFS_DINODE_FMT_LOCAL: |
| 373 | /* |
| 374 | * no local regular files yet |
| 375 | */ |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 376 | if (unlikely((be16_to_cpu(dip->di_mode) & S_IFMT) == S_IFREG)) { |
Nathan Scott | 3762ec6 | 2006-01-12 10:29:53 +1100 | [diff] [blame] | 377 | xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount, |
| 378 | "corrupt inode %Lu " |
| 379 | "(local format for regular file).", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | (unsigned long long) ip->i_ino); |
| 381 | XFS_CORRUPTION_ERROR("xfs_iformat(4)", |
| 382 | XFS_ERRLEVEL_LOW, |
| 383 | ip->i_mount, dip); |
| 384 | return XFS_ERROR(EFSCORRUPTED); |
| 385 | } |
| 386 | |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 387 | di_size = be64_to_cpu(dip->di_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | if (unlikely(di_size > XFS_DFORK_DSIZE(dip, ip->i_mount))) { |
Nathan Scott | 3762ec6 | 2006-01-12 10:29:53 +1100 | [diff] [blame] | 389 | xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount, |
| 390 | "corrupt inode %Lu " |
| 391 | "(bad size %Ld for local inode).", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | (unsigned long long) ip->i_ino, |
| 393 | (long long) di_size); |
| 394 | XFS_CORRUPTION_ERROR("xfs_iformat(5)", |
| 395 | XFS_ERRLEVEL_LOW, |
| 396 | ip->i_mount, dip); |
| 397 | return XFS_ERROR(EFSCORRUPTED); |
| 398 | } |
| 399 | |
| 400 | size = (int)di_size; |
| 401 | error = xfs_iformat_local(ip, dip, XFS_DATA_FORK, size); |
| 402 | break; |
| 403 | case XFS_DINODE_FMT_EXTENTS: |
| 404 | error = xfs_iformat_extents(ip, dip, XFS_DATA_FORK); |
| 405 | break; |
| 406 | case XFS_DINODE_FMT_BTREE: |
| 407 | error = xfs_iformat_btree(ip, dip, XFS_DATA_FORK); |
| 408 | break; |
| 409 | default: |
| 410 | XFS_ERROR_REPORT("xfs_iformat(6)", XFS_ERRLEVEL_LOW, |
| 411 | ip->i_mount); |
| 412 | return XFS_ERROR(EFSCORRUPTED); |
| 413 | } |
| 414 | break; |
| 415 | |
| 416 | default: |
| 417 | XFS_ERROR_REPORT("xfs_iformat(7)", XFS_ERRLEVEL_LOW, ip->i_mount); |
| 418 | return XFS_ERROR(EFSCORRUPTED); |
| 419 | } |
| 420 | if (error) { |
| 421 | return error; |
| 422 | } |
| 423 | if (!XFS_DFORK_Q(dip)) |
| 424 | return 0; |
| 425 | ASSERT(ip->i_afp == NULL); |
| 426 | ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP); |
| 427 | ip->i_afp->if_ext_max = |
| 428 | XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t); |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 429 | switch (dip->di_aformat) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | case XFS_DINODE_FMT_LOCAL: |
| 431 | atp = (xfs_attr_shortform_t *)XFS_DFORK_APTR(dip); |
Nathan Scott | 3b244aa | 2006-03-17 17:29:25 +1100 | [diff] [blame] | 432 | size = be16_to_cpu(atp->hdr.totsize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK, size); |
| 434 | break; |
| 435 | case XFS_DINODE_FMT_EXTENTS: |
| 436 | error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK); |
| 437 | break; |
| 438 | case XFS_DINODE_FMT_BTREE: |
| 439 | error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK); |
| 440 | break; |
| 441 | default: |
| 442 | error = XFS_ERROR(EFSCORRUPTED); |
| 443 | break; |
| 444 | } |
| 445 | if (error) { |
| 446 | kmem_zone_free(xfs_ifork_zone, ip->i_afp); |
| 447 | ip->i_afp = NULL; |
| 448 | xfs_idestroy_fork(ip, XFS_DATA_FORK); |
| 449 | } |
| 450 | return error; |
| 451 | } |
| 452 | |
| 453 | /* |
| 454 | * The file is in-lined in the on-disk inode. |
| 455 | * If it fits into if_inline_data, then copy |
| 456 | * it there, otherwise allocate a buffer for it |
| 457 | * and copy the data there. Either way, set |
| 458 | * if_data to point at the data. |
| 459 | * If we allocate a buffer for the data, make |
| 460 | * sure that its size is a multiple of 4 and |
| 461 | * record the real size in i_real_bytes. |
| 462 | */ |
| 463 | STATIC int |
| 464 | xfs_iformat_local( |
| 465 | xfs_inode_t *ip, |
| 466 | xfs_dinode_t *dip, |
| 467 | int whichfork, |
| 468 | int size) |
| 469 | { |
| 470 | xfs_ifork_t *ifp; |
| 471 | int real_size; |
| 472 | |
| 473 | /* |
| 474 | * If the size is unreasonable, then something |
| 475 | * is wrong and we just bail out rather than crash in |
| 476 | * kmem_alloc() or memcpy() below. |
| 477 | */ |
| 478 | if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) { |
Nathan Scott | 3762ec6 | 2006-01-12 10:29:53 +1100 | [diff] [blame] | 479 | xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount, |
| 480 | "corrupt inode %Lu " |
| 481 | "(bad size %d for local fork, size = %d).", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | (unsigned long long) ip->i_ino, size, |
| 483 | XFS_DFORK_SIZE(dip, ip->i_mount, whichfork)); |
| 484 | XFS_CORRUPTION_ERROR("xfs_iformat_local", XFS_ERRLEVEL_LOW, |
| 485 | ip->i_mount, dip); |
| 486 | return XFS_ERROR(EFSCORRUPTED); |
| 487 | } |
| 488 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 489 | real_size = 0; |
| 490 | if (size == 0) |
| 491 | ifp->if_u1.if_data = NULL; |
| 492 | else if (size <= sizeof(ifp->if_u2.if_inline_data)) |
| 493 | ifp->if_u1.if_data = ifp->if_u2.if_inline_data; |
| 494 | else { |
| 495 | real_size = roundup(size, 4); |
| 496 | ifp->if_u1.if_data = kmem_alloc(real_size, KM_SLEEP); |
| 497 | } |
| 498 | ifp->if_bytes = size; |
| 499 | ifp->if_real_bytes = real_size; |
| 500 | if (size) |
| 501 | memcpy(ifp->if_u1.if_data, XFS_DFORK_PTR(dip, whichfork), size); |
| 502 | ifp->if_flags &= ~XFS_IFEXTENTS; |
| 503 | ifp->if_flags |= XFS_IFINLINE; |
| 504 | return 0; |
| 505 | } |
| 506 | |
| 507 | /* |
| 508 | * The file consists of a set of extents all |
| 509 | * of which fit into the on-disk inode. |
| 510 | * If there are few enough extents to fit into |
| 511 | * the if_inline_ext, then copy them there. |
| 512 | * Otherwise allocate a buffer for them and copy |
| 513 | * them into it. Either way, set if_extents |
| 514 | * to point at the extents. |
| 515 | */ |
| 516 | STATIC int |
| 517 | xfs_iformat_extents( |
| 518 | xfs_inode_t *ip, |
| 519 | xfs_dinode_t *dip, |
| 520 | int whichfork) |
| 521 | { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 522 | xfs_bmbt_rec_t *dp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | xfs_ifork_t *ifp; |
| 524 | int nex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | int size; |
| 526 | int i; |
| 527 | |
| 528 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 529 | nex = XFS_DFORK_NEXTENTS(dip, whichfork); |
| 530 | size = nex * (uint)sizeof(xfs_bmbt_rec_t); |
| 531 | |
| 532 | /* |
| 533 | * If the number of extents is unreasonable, then something |
| 534 | * is wrong and we just bail out rather than crash in |
| 535 | * kmem_alloc() or memcpy() below. |
| 536 | */ |
| 537 | if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) { |
Nathan Scott | 3762ec6 | 2006-01-12 10:29:53 +1100 | [diff] [blame] | 538 | xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount, |
| 539 | "corrupt inode %Lu ((a)extents = %d).", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | (unsigned long long) ip->i_ino, nex); |
| 541 | XFS_CORRUPTION_ERROR("xfs_iformat_extents(1)", XFS_ERRLEVEL_LOW, |
| 542 | ip->i_mount, dip); |
| 543 | return XFS_ERROR(EFSCORRUPTED); |
| 544 | } |
| 545 | |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 546 | ifp->if_real_bytes = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | if (nex == 0) |
| 548 | ifp->if_u1.if_extents = NULL; |
| 549 | else if (nex <= XFS_INLINE_EXTS) |
| 550 | ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 551 | else |
| 552 | xfs_iext_add(ifp, 0, nex); |
| 553 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | ifp->if_bytes = size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | if (size) { |
| 556 | dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork); |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 557 | xfs_validate_extents(ifp, nex, XFS_EXTFMT_INODE(ip)); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 558 | for (i = 0; i < nex; i++, dp++) { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 559 | xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i); |
Harvey Harrison | 597bca6 | 2008-08-13 16:29:21 +1000 | [diff] [blame] | 560 | ep->l0 = get_unaligned_be64(&dp->l0); |
| 561 | ep->l1 = get_unaligned_be64(&dp->l1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | } |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 563 | XFS_BMAP_TRACE_EXLIST(ip, nex, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | if (whichfork != XFS_DATA_FORK || |
| 565 | XFS_EXTFMT_INODE(ip) == XFS_EXTFMT_NOSTATE) |
| 566 | if (unlikely(xfs_check_nostate_extents( |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 567 | ifp, 0, nex))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | XFS_ERROR_REPORT("xfs_iformat_extents(2)", |
| 569 | XFS_ERRLEVEL_LOW, |
| 570 | ip->i_mount); |
| 571 | return XFS_ERROR(EFSCORRUPTED); |
| 572 | } |
| 573 | } |
| 574 | ifp->if_flags |= XFS_IFEXTENTS; |
| 575 | return 0; |
| 576 | } |
| 577 | |
| 578 | /* |
| 579 | * The file has too many extents to fit into |
| 580 | * the inode, so they are in B-tree format. |
| 581 | * Allocate a buffer for the root of the B-tree |
| 582 | * and copy the root into it. The i_extents |
| 583 | * field will remain NULL until all of the |
| 584 | * extents are read in (when they are needed). |
| 585 | */ |
| 586 | STATIC int |
| 587 | xfs_iformat_btree( |
| 588 | xfs_inode_t *ip, |
| 589 | xfs_dinode_t *dip, |
| 590 | int whichfork) |
| 591 | { |
| 592 | xfs_bmdr_block_t *dfp; |
| 593 | xfs_ifork_t *ifp; |
| 594 | /* REFERENCED */ |
| 595 | int nrecs; |
| 596 | int size; |
| 597 | |
| 598 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 599 | dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork); |
| 600 | size = XFS_BMAP_BROOT_SPACE(dfp); |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 601 | nrecs = be16_to_cpu(dfp->bb_numrecs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | |
| 603 | /* |
| 604 | * blow out if -- fork has less extents than can fit in |
| 605 | * fork (fork shouldn't be a btree format), root btree |
| 606 | * block has more records than can fit into the fork, |
| 607 | * or the number of extents is greater than the number of |
| 608 | * blocks. |
| 609 | */ |
| 610 | if (unlikely(XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max |
| 611 | || XFS_BMDR_SPACE_CALC(nrecs) > |
| 612 | XFS_DFORK_SIZE(dip, ip->i_mount, whichfork) |
| 613 | || XFS_IFORK_NEXTENTS(ip, whichfork) > ip->i_d.di_nblocks)) { |
Nathan Scott | 3762ec6 | 2006-01-12 10:29:53 +1100 | [diff] [blame] | 614 | xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount, |
| 615 | "corrupt inode %Lu (btree).", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | (unsigned long long) ip->i_ino); |
| 617 | XFS_ERROR_REPORT("xfs_iformat_btree", XFS_ERRLEVEL_LOW, |
| 618 | ip->i_mount); |
| 619 | return XFS_ERROR(EFSCORRUPTED); |
| 620 | } |
| 621 | |
| 622 | ifp->if_broot_bytes = size; |
| 623 | ifp->if_broot = kmem_alloc(size, KM_SLEEP); |
| 624 | ASSERT(ifp->if_broot != NULL); |
| 625 | /* |
| 626 | * Copy and convert from the on-disk structure |
| 627 | * to the in-memory structure. |
| 628 | */ |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 629 | xfs_bmdr_to_bmbt(ip->i_mount, dfp, |
| 630 | XFS_DFORK_SIZE(dip, ip->i_mount, whichfork), |
| 631 | ifp->if_broot, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | ifp->if_flags &= ~XFS_IFEXTENTS; |
| 633 | ifp->if_flags |= XFS_IFBROOT; |
| 634 | |
| 635 | return 0; |
| 636 | } |
| 637 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | void |
Christoph Hellwig | 347d1c0 | 2007-08-28 13:57:51 +1000 | [diff] [blame] | 639 | xfs_dinode_from_disk( |
| 640 | xfs_icdinode_t *to, |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 641 | xfs_dinode_t *from) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | { |
Christoph Hellwig | 347d1c0 | 2007-08-28 13:57:51 +1000 | [diff] [blame] | 643 | to->di_magic = be16_to_cpu(from->di_magic); |
| 644 | to->di_mode = be16_to_cpu(from->di_mode); |
| 645 | to->di_version = from ->di_version; |
| 646 | to->di_format = from->di_format; |
| 647 | to->di_onlink = be16_to_cpu(from->di_onlink); |
| 648 | to->di_uid = be32_to_cpu(from->di_uid); |
| 649 | to->di_gid = be32_to_cpu(from->di_gid); |
| 650 | to->di_nlink = be32_to_cpu(from->di_nlink); |
| 651 | to->di_projid = be16_to_cpu(from->di_projid); |
| 652 | memcpy(to->di_pad, from->di_pad, sizeof(to->di_pad)); |
| 653 | to->di_flushiter = be16_to_cpu(from->di_flushiter); |
| 654 | to->di_atime.t_sec = be32_to_cpu(from->di_atime.t_sec); |
| 655 | to->di_atime.t_nsec = be32_to_cpu(from->di_atime.t_nsec); |
| 656 | to->di_mtime.t_sec = be32_to_cpu(from->di_mtime.t_sec); |
| 657 | to->di_mtime.t_nsec = be32_to_cpu(from->di_mtime.t_nsec); |
| 658 | to->di_ctime.t_sec = be32_to_cpu(from->di_ctime.t_sec); |
| 659 | to->di_ctime.t_nsec = be32_to_cpu(from->di_ctime.t_nsec); |
| 660 | to->di_size = be64_to_cpu(from->di_size); |
| 661 | to->di_nblocks = be64_to_cpu(from->di_nblocks); |
| 662 | to->di_extsize = be32_to_cpu(from->di_extsize); |
| 663 | to->di_nextents = be32_to_cpu(from->di_nextents); |
| 664 | to->di_anextents = be16_to_cpu(from->di_anextents); |
| 665 | to->di_forkoff = from->di_forkoff; |
| 666 | to->di_aformat = from->di_aformat; |
| 667 | to->di_dmevmask = be32_to_cpu(from->di_dmevmask); |
| 668 | to->di_dmstate = be16_to_cpu(from->di_dmstate); |
| 669 | to->di_flags = be16_to_cpu(from->di_flags); |
| 670 | to->di_gen = be32_to_cpu(from->di_gen); |
| 671 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | |
Christoph Hellwig | 347d1c0 | 2007-08-28 13:57:51 +1000 | [diff] [blame] | 673 | void |
| 674 | xfs_dinode_to_disk( |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 675 | xfs_dinode_t *to, |
Christoph Hellwig | 347d1c0 | 2007-08-28 13:57:51 +1000 | [diff] [blame] | 676 | xfs_icdinode_t *from) |
| 677 | { |
| 678 | to->di_magic = cpu_to_be16(from->di_magic); |
| 679 | to->di_mode = cpu_to_be16(from->di_mode); |
| 680 | to->di_version = from ->di_version; |
| 681 | to->di_format = from->di_format; |
| 682 | to->di_onlink = cpu_to_be16(from->di_onlink); |
| 683 | to->di_uid = cpu_to_be32(from->di_uid); |
| 684 | to->di_gid = cpu_to_be32(from->di_gid); |
| 685 | to->di_nlink = cpu_to_be32(from->di_nlink); |
| 686 | to->di_projid = cpu_to_be16(from->di_projid); |
| 687 | memcpy(to->di_pad, from->di_pad, sizeof(to->di_pad)); |
| 688 | to->di_flushiter = cpu_to_be16(from->di_flushiter); |
| 689 | to->di_atime.t_sec = cpu_to_be32(from->di_atime.t_sec); |
| 690 | to->di_atime.t_nsec = cpu_to_be32(from->di_atime.t_nsec); |
| 691 | to->di_mtime.t_sec = cpu_to_be32(from->di_mtime.t_sec); |
| 692 | to->di_mtime.t_nsec = cpu_to_be32(from->di_mtime.t_nsec); |
| 693 | to->di_ctime.t_sec = cpu_to_be32(from->di_ctime.t_sec); |
| 694 | to->di_ctime.t_nsec = cpu_to_be32(from->di_ctime.t_nsec); |
| 695 | to->di_size = cpu_to_be64(from->di_size); |
| 696 | to->di_nblocks = cpu_to_be64(from->di_nblocks); |
| 697 | to->di_extsize = cpu_to_be32(from->di_extsize); |
| 698 | to->di_nextents = cpu_to_be32(from->di_nextents); |
| 699 | to->di_anextents = cpu_to_be16(from->di_anextents); |
| 700 | to->di_forkoff = from->di_forkoff; |
| 701 | to->di_aformat = from->di_aformat; |
| 702 | to->di_dmevmask = cpu_to_be32(from->di_dmevmask); |
| 703 | to->di_dmstate = cpu_to_be16(from->di_dmstate); |
| 704 | to->di_flags = cpu_to_be16(from->di_flags); |
| 705 | to->di_gen = cpu_to_be32(from->di_gen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | STATIC uint |
| 709 | _xfs_dic2xflags( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | __uint16_t di_flags) |
| 711 | { |
| 712 | uint flags = 0; |
| 713 | |
| 714 | if (di_flags & XFS_DIFLAG_ANY) { |
| 715 | if (di_flags & XFS_DIFLAG_REALTIME) |
| 716 | flags |= XFS_XFLAG_REALTIME; |
| 717 | if (di_flags & XFS_DIFLAG_PREALLOC) |
| 718 | flags |= XFS_XFLAG_PREALLOC; |
| 719 | if (di_flags & XFS_DIFLAG_IMMUTABLE) |
| 720 | flags |= XFS_XFLAG_IMMUTABLE; |
| 721 | if (di_flags & XFS_DIFLAG_APPEND) |
| 722 | flags |= XFS_XFLAG_APPEND; |
| 723 | if (di_flags & XFS_DIFLAG_SYNC) |
| 724 | flags |= XFS_XFLAG_SYNC; |
| 725 | if (di_flags & XFS_DIFLAG_NOATIME) |
| 726 | flags |= XFS_XFLAG_NOATIME; |
| 727 | if (di_flags & XFS_DIFLAG_NODUMP) |
| 728 | flags |= XFS_XFLAG_NODUMP; |
| 729 | if (di_flags & XFS_DIFLAG_RTINHERIT) |
| 730 | flags |= XFS_XFLAG_RTINHERIT; |
| 731 | if (di_flags & XFS_DIFLAG_PROJINHERIT) |
| 732 | flags |= XFS_XFLAG_PROJINHERIT; |
| 733 | if (di_flags & XFS_DIFLAG_NOSYMLINKS) |
| 734 | flags |= XFS_XFLAG_NOSYMLINKS; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 735 | if (di_flags & XFS_DIFLAG_EXTSIZE) |
| 736 | flags |= XFS_XFLAG_EXTSIZE; |
| 737 | if (di_flags & XFS_DIFLAG_EXTSZINHERIT) |
| 738 | flags |= XFS_XFLAG_EXTSZINHERIT; |
Barry Naujok | d3446ea | 2006-06-09 14:54:19 +1000 | [diff] [blame] | 739 | if (di_flags & XFS_DIFLAG_NODEFRAG) |
| 740 | flags |= XFS_XFLAG_NODEFRAG; |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 741 | if (di_flags & XFS_DIFLAG_FILESTREAM) |
| 742 | flags |= XFS_XFLAG_FILESTREAM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | return flags; |
| 746 | } |
| 747 | |
| 748 | uint |
| 749 | xfs_ip2xflags( |
| 750 | xfs_inode_t *ip) |
| 751 | { |
Christoph Hellwig | 347d1c0 | 2007-08-28 13:57:51 +1000 | [diff] [blame] | 752 | xfs_icdinode_t *dic = &ip->i_d; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | |
Nathan Scott | a916e2b | 2006-06-09 17:12:17 +1000 | [diff] [blame] | 754 | return _xfs_dic2xflags(dic->di_flags) | |
Christoph Hellwig | 45ba598 | 2007-12-07 14:07:20 +1100 | [diff] [blame] | 755 | (XFS_IFORK_Q(ip) ? XFS_XFLAG_HASATTR : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | uint |
| 759 | xfs_dic2xflags( |
Christoph Hellwig | 45ba598 | 2007-12-07 14:07:20 +1100 | [diff] [blame] | 760 | xfs_dinode_t *dip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | { |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 762 | return _xfs_dic2xflags(be16_to_cpu(dip->di_flags)) | |
Christoph Hellwig | 45ba598 | 2007-12-07 14:07:20 +1100 | [diff] [blame] | 763 | (XFS_DFORK_Q(dip) ? XFS_XFLAG_HASATTR : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | /* |
David Chinner | 07c8f67 | 2008-10-30 16:11:59 +1100 | [diff] [blame] | 767 | * Allocate and initialise an xfs_inode. |
| 768 | */ |
Christoph Hellwig | c679eef | 2008-10-30 18:04:13 +1100 | [diff] [blame] | 769 | STATIC struct xfs_inode * |
David Chinner | 07c8f67 | 2008-10-30 16:11:59 +1100 | [diff] [blame] | 770 | xfs_inode_alloc( |
| 771 | struct xfs_mount *mp, |
| 772 | xfs_ino_t ino) |
| 773 | { |
| 774 | struct xfs_inode *ip; |
| 775 | |
| 776 | /* |
| 777 | * if this didn't occur in transactions, we could use |
| 778 | * KM_MAYFAIL and return NULL here on ENOMEM. Set the |
| 779 | * code up to do this anyway. |
| 780 | */ |
| 781 | ip = kmem_zone_alloc(xfs_inode_zone, KM_SLEEP); |
| 782 | if (!ip) |
| 783 | return NULL; |
| 784 | |
| 785 | ASSERT(atomic_read(&ip->i_iocount) == 0); |
| 786 | ASSERT(atomic_read(&ip->i_pincount) == 0); |
| 787 | ASSERT(!spin_is_locked(&ip->i_flags_lock)); |
David Chinner | 1165451 | 2008-10-30 17:37:49 +1100 | [diff] [blame] | 788 | ASSERT(completion_done(&ip->i_flush)); |
David Chinner | 07c8f67 | 2008-10-30 16:11:59 +1100 | [diff] [blame] | 789 | |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 790 | /* |
| 791 | * initialise the VFS inode here to get failures |
| 792 | * out of the way early. |
| 793 | */ |
| 794 | if (!inode_init_always(mp->m_super, VFS_I(ip))) { |
| 795 | kmem_zone_free(xfs_inode_zone, ip); |
| 796 | return NULL; |
| 797 | } |
| 798 | |
| 799 | /* initialise the xfs inode */ |
David Chinner | 07c8f67 | 2008-10-30 16:11:59 +1100 | [diff] [blame] | 800 | ip->i_ino = ino; |
| 801 | ip->i_mount = mp; |
| 802 | ip->i_blkno = 0; |
| 803 | ip->i_len = 0; |
| 804 | ip->i_boffset =0; |
| 805 | ip->i_afp = NULL; |
| 806 | memset(&ip->i_df, 0, sizeof(xfs_ifork_t)); |
| 807 | ip->i_flags = 0; |
| 808 | ip->i_update_core = 0; |
| 809 | ip->i_update_size = 0; |
| 810 | ip->i_delayed_blks = 0; |
| 811 | memset(&ip->i_d, 0, sizeof(xfs_icdinode_t)); |
| 812 | ip->i_size = 0; |
| 813 | ip->i_new_size = 0; |
| 814 | |
| 815 | /* |
| 816 | * Initialize inode's trace buffers. |
| 817 | */ |
| 818 | #ifdef XFS_INODE_TRACE |
| 819 | ip->i_trace = ktrace_alloc(INODE_TRACE_SIZE, KM_NOFS); |
| 820 | #endif |
| 821 | #ifdef XFS_BMAP_TRACE |
| 822 | ip->i_xtrace = ktrace_alloc(XFS_BMAP_KTRACE_SIZE, KM_NOFS); |
| 823 | #endif |
Christoph Hellwig | 8c4ed63 | 2008-10-30 16:55:13 +1100 | [diff] [blame] | 824 | #ifdef XFS_BTREE_TRACE |
David Chinner | 07c8f67 | 2008-10-30 16:11:59 +1100 | [diff] [blame] | 825 | ip->i_btrace = ktrace_alloc(XFS_BMBT_KTRACE_SIZE, KM_NOFS); |
| 826 | #endif |
| 827 | #ifdef XFS_RW_TRACE |
| 828 | ip->i_rwtrace = ktrace_alloc(XFS_RW_KTRACE_SIZE, KM_NOFS); |
| 829 | #endif |
| 830 | #ifdef XFS_ILOCK_TRACE |
| 831 | ip->i_lock_trace = ktrace_alloc(XFS_ILOCK_KTRACE_SIZE, KM_NOFS); |
| 832 | #endif |
| 833 | #ifdef XFS_DIR2_TRACE |
| 834 | ip->i_dir_trace = ktrace_alloc(XFS_DIR2_KTRACE_SIZE, KM_NOFS); |
| 835 | #endif |
| 836 | |
| 837 | return ip; |
| 838 | } |
| 839 | |
| 840 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | * Given a mount structure and an inode number, return a pointer |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 842 | * to a newly allocated in-core inode corresponding to the given |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | * inode number. |
| 844 | * |
| 845 | * Initialize the inode's attributes and extent pointers if it |
| 846 | * already has them (it will not if the inode has no links). |
| 847 | */ |
| 848 | int |
| 849 | xfs_iread( |
| 850 | xfs_mount_t *mp, |
| 851 | xfs_trans_t *tp, |
| 852 | xfs_ino_t ino, |
| 853 | xfs_inode_t **ipp, |
Nathan Scott | 745b1f47 | 2006-09-28 11:02:23 +1000 | [diff] [blame] | 854 | xfs_daddr_t bno, |
| 855 | uint imap_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | { |
| 857 | xfs_buf_t *bp; |
| 858 | xfs_dinode_t *dip; |
| 859 | xfs_inode_t *ip; |
Christoph Hellwig | 76d8b27 | 2008-11-28 14:23:40 +1100 | [diff] [blame^] | 860 | xfs_imap_t imap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | int error; |
| 862 | |
David Chinner | 07c8f67 | 2008-10-30 16:11:59 +1100 | [diff] [blame] | 863 | ip = xfs_inode_alloc(mp, ino); |
| 864 | if (!ip) |
| 865 | return ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | |
| 867 | /* |
Christoph Hellwig | 76d8b27 | 2008-11-28 14:23:40 +1100 | [diff] [blame^] | 868 | * Get pointers to the on-disk inode and the buffer containing it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | */ |
Christoph Hellwig | 76d8b27 | 2008-11-28 14:23:40 +1100 | [diff] [blame^] | 870 | imap.im_blkno = bno; |
| 871 | error = xfs_imap(mp, tp, ip->i_ino, &imap, |
| 872 | XFS_IMAP_LOOKUP | imap_flags); |
Christoph Hellwig | 9ed0451 | 2008-10-30 18:26:04 +1100 | [diff] [blame] | 873 | if (error) |
| 874 | goto out_destroy_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | |
| 876 | /* |
Christoph Hellwig | 76d8b27 | 2008-11-28 14:23:40 +1100 | [diff] [blame^] | 877 | * Fill in the fields in the inode that will be used to |
| 878 | * map the inode to its buffer from now on. |
| 879 | */ |
| 880 | ip->i_blkno = imap.im_blkno; |
| 881 | ip->i_len = imap.im_len; |
| 882 | ip->i_boffset = imap.im_boffset; |
| 883 | ASSERT(bno == 0 || bno == imap.im_blkno); |
| 884 | |
| 885 | error = xfs_imap_to_bp(mp, tp, &imap, &bp, XFS_BUF_LOCK, imap_flags); |
| 886 | if (error) |
| 887 | goto out_destroy_inode; |
| 888 | dip = (xfs_dinode_t *)xfs_buf_offset(bp, imap.im_boffset); |
| 889 | |
| 890 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | * If we got something that isn't an inode it means someone |
| 892 | * (nfs or dmi) has a stale handle. |
| 893 | */ |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 894 | if (be16_to_cpu(dip->di_magic) != XFS_DINODE_MAGIC) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | #ifdef DEBUG |
| 896 | xfs_fs_cmn_err(CE_ALERT, mp, "xfs_iread: " |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 897 | "dip->di_magic (0x%x) != " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | "XFS_DINODE_MAGIC (0x%x)", |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 899 | be16_to_cpu(dip->di_magic), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | XFS_DINODE_MAGIC); |
| 901 | #endif /* DEBUG */ |
Christoph Hellwig | 9ed0451 | 2008-10-30 18:26:04 +1100 | [diff] [blame] | 902 | error = XFS_ERROR(EINVAL); |
| 903 | goto out_brelse; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | /* |
| 907 | * If the on-disk inode is already linked to a directory |
| 908 | * entry, copy all of the inode into the in-core inode. |
| 909 | * xfs_iformat() handles copying in the inode format |
| 910 | * specific information. |
| 911 | * Otherwise, just get the truly permanent information. |
| 912 | */ |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 913 | if (dip->di_mode) { |
| 914 | xfs_dinode_from_disk(&ip->i_d, dip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | error = xfs_iformat(ip, dip); |
| 916 | if (error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | #ifdef DEBUG |
| 918 | xfs_fs_cmn_err(CE_ALERT, mp, "xfs_iread: " |
| 919 | "xfs_iformat() returned error %d", |
| 920 | error); |
| 921 | #endif /* DEBUG */ |
Christoph Hellwig | 9ed0451 | 2008-10-30 18:26:04 +1100 | [diff] [blame] | 922 | goto out_brelse; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | } |
| 924 | } else { |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 925 | ip->i_d.di_magic = be16_to_cpu(dip->di_magic); |
| 926 | ip->i_d.di_version = dip->di_version; |
| 927 | ip->i_d.di_gen = be32_to_cpu(dip->di_gen); |
| 928 | ip->i_d.di_flushiter = be16_to_cpu(dip->di_flushiter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | /* |
| 930 | * Make sure to pull in the mode here as well in |
| 931 | * case the inode is released without being used. |
| 932 | * This ensures that xfs_inactive() will see that |
| 933 | * the inode is already free and not try to mess |
| 934 | * with the uninitialized part of it. |
| 935 | */ |
| 936 | ip->i_d.di_mode = 0; |
| 937 | /* |
| 938 | * Initialize the per-fork minima and maxima for a new |
| 939 | * inode here. xfs_iformat will do it for old inodes. |
| 940 | */ |
| 941 | ip->i_df.if_ext_max = |
| 942 | XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t); |
| 943 | } |
| 944 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | /* |
| 946 | * The inode format changed when we moved the link count and |
| 947 | * made it 32 bits long. If this is an old format inode, |
| 948 | * convert it in memory to look like a new one. If it gets |
| 949 | * flushed to disk we will convert back before flushing or |
| 950 | * logging it. We zero out the new projid field and the old link |
| 951 | * count field. We'll handle clearing the pad field (the remains |
| 952 | * of the old uuid field) when we actually convert the inode to |
| 953 | * the new format. We don't change the version number so that we |
| 954 | * can distinguish this from a real new format inode. |
| 955 | */ |
Christoph Hellwig | 51ce16d | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 956 | if (ip->i_d.di_version == 1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | ip->i_d.di_nlink = ip->i_d.di_onlink; |
| 958 | ip->i_d.di_onlink = 0; |
| 959 | ip->i_d.di_projid = 0; |
| 960 | } |
| 961 | |
| 962 | ip->i_delayed_blks = 0; |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 963 | ip->i_size = ip->i_d.di_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | |
| 965 | /* |
| 966 | * Mark the buffer containing the inode as something to keep |
| 967 | * around for a while. This helps to keep recently accessed |
| 968 | * meta-data in-core longer. |
| 969 | */ |
| 970 | XFS_BUF_SET_REF(bp, XFS_INO_REF); |
| 971 | |
| 972 | /* |
| 973 | * Use xfs_trans_brelse() to release the buffer containing the |
| 974 | * on-disk inode, because it was acquired with xfs_trans_read_buf() |
| 975 | * in xfs_itobp() above. If tp is NULL, this is just a normal |
| 976 | * brelse(). If we're within a transaction, then xfs_trans_brelse() |
| 977 | * will only release the buffer if it is not dirty within the |
| 978 | * transaction. It will be OK to release the buffer in this case, |
| 979 | * because inodes on disk are never destroyed and we will be |
| 980 | * locking the new in-core inode before putting it in the hash |
| 981 | * table where other processes can find it. Thus we don't have |
| 982 | * to worry about the inode being changed just because we released |
| 983 | * the buffer. |
| 984 | */ |
| 985 | xfs_trans_brelse(tp, bp); |
| 986 | *ipp = ip; |
| 987 | return 0; |
Christoph Hellwig | 9ed0451 | 2008-10-30 18:26:04 +1100 | [diff] [blame] | 988 | |
| 989 | out_brelse: |
| 990 | xfs_trans_brelse(tp, bp); |
| 991 | out_destroy_inode: |
| 992 | xfs_destroy_inode(ip); |
| 993 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | /* |
| 997 | * Read in extents from a btree-format inode. |
| 998 | * Allocate and fill in if_extents. Real work is done in xfs_bmap.c. |
| 999 | */ |
| 1000 | int |
| 1001 | xfs_iread_extents( |
| 1002 | xfs_trans_t *tp, |
| 1003 | xfs_inode_t *ip, |
| 1004 | int whichfork) |
| 1005 | { |
| 1006 | int error; |
| 1007 | xfs_ifork_t *ifp; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1008 | xfs_extnum_t nextents; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | size_t size; |
| 1010 | |
| 1011 | if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) { |
| 1012 | XFS_ERROR_REPORT("xfs_iread_extents", XFS_ERRLEVEL_LOW, |
| 1013 | ip->i_mount); |
| 1014 | return XFS_ERROR(EFSCORRUPTED); |
| 1015 | } |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1016 | nextents = XFS_IFORK_NEXTENTS(ip, whichfork); |
| 1017 | size = nextents * sizeof(xfs_bmbt_rec_t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1019 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | /* |
| 1021 | * We know that the size is valid (it's checked in iformat_btree) |
| 1022 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | ifp->if_lastex = NULLEXTNUM; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1024 | ifp->if_bytes = ifp->if_real_bytes = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | ifp->if_flags |= XFS_IFEXTENTS; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1026 | xfs_iext_add(ifp, 0, nextents); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | error = xfs_bmap_read_extents(tp, ip, whichfork); |
| 1028 | if (error) { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1029 | xfs_iext_destroy(ifp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | ifp->if_flags &= ~XFS_IFEXTENTS; |
| 1031 | return error; |
| 1032 | } |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 1033 | xfs_validate_extents(ifp, nextents, XFS_EXTFMT_INODE(ip)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | return 0; |
| 1035 | } |
| 1036 | |
| 1037 | /* |
| 1038 | * Allocate an inode on disk and return a copy of its in-core version. |
| 1039 | * The in-core inode is locked exclusively. Set mode, nlink, and rdev |
| 1040 | * appropriately within the inode. The uid and gid for the inode are |
| 1041 | * set according to the contents of the given cred structure. |
| 1042 | * |
| 1043 | * Use xfs_dialloc() to allocate the on-disk inode. If xfs_dialloc() |
| 1044 | * has a free inode available, call xfs_iget() |
| 1045 | * to obtain the in-core version of the allocated inode. Finally, |
| 1046 | * fill in the inode and log its initial contents. In this case, |
| 1047 | * ialloc_context would be set to NULL and call_again set to false. |
| 1048 | * |
| 1049 | * If xfs_dialloc() does not have an available inode, |
| 1050 | * it will replenish its supply by doing an allocation. Since we can |
| 1051 | * only do one allocation within a transaction without deadlocks, we |
| 1052 | * must commit the current transaction before returning the inode itself. |
| 1053 | * In this case, therefore, we will set call_again to true and return. |
| 1054 | * The caller should then commit the current transaction, start a new |
| 1055 | * transaction, and call xfs_ialloc() again to actually get the inode. |
| 1056 | * |
| 1057 | * To ensure that some other process does not grab the inode that |
| 1058 | * was allocated during the first call to xfs_ialloc(), this routine |
| 1059 | * also returns the [locked] bp pointing to the head of the freelist |
| 1060 | * as ialloc_context. The caller should hold this buffer across |
| 1061 | * 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] | 1062 | * |
| 1063 | * If we are allocating quota inodes, we do not have a parent inode |
| 1064 | * to attach to or associate with (i.e. pip == NULL) because they |
| 1065 | * are not linked into the directory structure - they are attached |
| 1066 | * directly to the superblock - and so have no parent. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | */ |
| 1068 | int |
| 1069 | xfs_ialloc( |
| 1070 | xfs_trans_t *tp, |
| 1071 | xfs_inode_t *pip, |
| 1072 | mode_t mode, |
Nathan Scott | 31b084a | 2005-05-05 13:25:00 -0700 | [diff] [blame] | 1073 | xfs_nlink_t nlink, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | xfs_dev_t rdev, |
| 1075 | cred_t *cr, |
| 1076 | xfs_prid_t prid, |
| 1077 | int okalloc, |
| 1078 | xfs_buf_t **ialloc_context, |
| 1079 | boolean_t *call_again, |
| 1080 | xfs_inode_t **ipp) |
| 1081 | { |
| 1082 | xfs_ino_t ino; |
| 1083 | xfs_inode_t *ip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | uint flags; |
| 1085 | int error; |
Christoph Hellwig | dff35fd | 2008-08-13 16:44:15 +1000 | [diff] [blame] | 1086 | timespec_t tv; |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 1087 | int filestreams = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1088 | |
| 1089 | /* |
| 1090 | * Call the space management code to pick |
| 1091 | * the on-disk inode to be allocated. |
| 1092 | */ |
David Chinner | b11f94d | 2007-07-11 11:09:33 +1000 | [diff] [blame] | 1093 | error = xfs_dialloc(tp, pip ? pip->i_ino : 0, mode, okalloc, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1094 | ialloc_context, call_again, &ino); |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 1095 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | if (*call_again || ino == NULLFSINO) { |
| 1098 | *ipp = NULL; |
| 1099 | return 0; |
| 1100 | } |
| 1101 | ASSERT(*ialloc_context == NULL); |
| 1102 | |
| 1103 | /* |
| 1104 | * Get the in-core inode with the lock held exclusively. |
| 1105 | * This is because we're setting fields here we need |
| 1106 | * to prevent others from looking at until we're done. |
| 1107 | */ |
| 1108 | error = xfs_trans_iget(tp->t_mountp, tp, ino, |
Nathan Scott | 745b1f47 | 2006-09-28 11:02:23 +1000 | [diff] [blame] | 1109 | XFS_IGET_CREATE, XFS_ILOCK_EXCL, &ip); |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 1110 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1112 | ASSERT(ip != NULL); |
| 1113 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | ip->i_d.di_mode = (__uint16_t)mode; |
| 1115 | ip->i_d.di_onlink = 0; |
| 1116 | ip->i_d.di_nlink = nlink; |
| 1117 | ASSERT(ip->i_d.di_nlink == nlink); |
David Howells | 9e2b2dc | 2008-08-13 16:20:04 +0100 | [diff] [blame] | 1118 | ip->i_d.di_uid = current_fsuid(); |
| 1119 | ip->i_d.di_gid = current_fsgid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | ip->i_d.di_projid = prid; |
| 1121 | memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad)); |
| 1122 | |
| 1123 | /* |
| 1124 | * If the superblock version is up to where we support new format |
| 1125 | * inodes and this is currently an old format inode, then change |
| 1126 | * the inode version number now. This way we only do the conversion |
| 1127 | * here rather than here and in the flush/logging code. |
| 1128 | */ |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 1129 | if (xfs_sb_version_hasnlink(&tp->t_mountp->m_sb) && |
Christoph Hellwig | 51ce16d | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 1130 | ip->i_d.di_version == 1) { |
| 1131 | ip->i_d.di_version = 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1132 | /* |
| 1133 | * We've already zeroed the old link count, the projid field, |
| 1134 | * and the pad field. |
| 1135 | */ |
| 1136 | } |
| 1137 | |
| 1138 | /* |
| 1139 | * Project ids won't be stored on disk if we are using a version 1 inode. |
| 1140 | */ |
Christoph Hellwig | 51ce16d | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 1141 | if ((prid != 0) && (ip->i_d.di_version == 1)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1142 | xfs_bump_ino_vers2(tp, ip); |
| 1143 | |
Christoph Hellwig | bd186aa | 2007-08-30 17:21:12 +1000 | [diff] [blame] | 1144 | if (pip && XFS_INHERIT_GID(pip)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | ip->i_d.di_gid = pip->i_d.di_gid; |
| 1146 | if ((pip->i_d.di_mode & S_ISGID) && (mode & S_IFMT) == S_IFDIR) { |
| 1147 | ip->i_d.di_mode |= S_ISGID; |
| 1148 | } |
| 1149 | } |
| 1150 | |
| 1151 | /* |
| 1152 | * If the group ID of the new file does not match the effective group |
| 1153 | * ID or one of the supplementary group IDs, the S_ISGID bit is cleared |
| 1154 | * (and only if the irix_sgid_inherit compatibility variable is set). |
| 1155 | */ |
| 1156 | if ((irix_sgid_inherit) && |
| 1157 | (ip->i_d.di_mode & S_ISGID) && |
| 1158 | (!in_group_p((gid_t)ip->i_d.di_gid))) { |
| 1159 | ip->i_d.di_mode &= ~S_ISGID; |
| 1160 | } |
| 1161 | |
| 1162 | ip->i_d.di_size = 0; |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 1163 | ip->i_size = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1164 | ip->i_d.di_nextents = 0; |
| 1165 | ASSERT(ip->i_d.di_nblocks == 0); |
Christoph Hellwig | dff35fd | 2008-08-13 16:44:15 +1000 | [diff] [blame] | 1166 | |
| 1167 | nanotime(&tv); |
| 1168 | ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec; |
| 1169 | ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec; |
| 1170 | ip->i_d.di_atime = ip->i_d.di_mtime; |
| 1171 | ip->i_d.di_ctime = ip->i_d.di_mtime; |
| 1172 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1173 | /* |
| 1174 | * di_gen will have been taken care of in xfs_iread. |
| 1175 | */ |
| 1176 | ip->i_d.di_extsize = 0; |
| 1177 | ip->i_d.di_dmevmask = 0; |
| 1178 | ip->i_d.di_dmstate = 0; |
| 1179 | ip->i_d.di_flags = 0; |
| 1180 | flags = XFS_ILOG_CORE; |
| 1181 | switch (mode & S_IFMT) { |
| 1182 | case S_IFIFO: |
| 1183 | case S_IFCHR: |
| 1184 | case S_IFBLK: |
| 1185 | case S_IFSOCK: |
| 1186 | ip->i_d.di_format = XFS_DINODE_FMT_DEV; |
| 1187 | ip->i_df.if_u2.if_rdev = rdev; |
| 1188 | ip->i_df.if_flags = 0; |
| 1189 | flags |= XFS_ILOG_DEV; |
| 1190 | break; |
| 1191 | case S_IFREG: |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 1192 | /* |
| 1193 | * we can't set up filestreams until after the VFS inode |
| 1194 | * is set up properly. |
| 1195 | */ |
| 1196 | if (pip && xfs_inode_is_filestream(pip)) |
| 1197 | filestreams = 1; |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 1198 | /* fall through */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1199 | case S_IFDIR: |
David Chinner | b11f94d | 2007-07-11 11:09:33 +1000 | [diff] [blame] | 1200 | if (pip && (pip->i_d.di_flags & XFS_DIFLAG_ANY)) { |
Nathan Scott | 365ca83 | 2005-06-21 15:39:12 +1000 | [diff] [blame] | 1201 | uint di_flags = 0; |
| 1202 | |
| 1203 | if ((mode & S_IFMT) == S_IFDIR) { |
| 1204 | if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT) |
| 1205 | di_flags |= XFS_DIFLAG_RTINHERIT; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 1206 | if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) { |
| 1207 | di_flags |= XFS_DIFLAG_EXTSZINHERIT; |
| 1208 | ip->i_d.di_extsize = pip->i_d.di_extsize; |
| 1209 | } |
| 1210 | } else if ((mode & S_IFMT) == S_IFREG) { |
Christoph Hellwig | 613d704 | 2007-10-11 17:44:08 +1000 | [diff] [blame] | 1211 | if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT) |
Nathan Scott | 365ca83 | 2005-06-21 15:39:12 +1000 | [diff] [blame] | 1212 | di_flags |= XFS_DIFLAG_REALTIME; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 1213 | if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) { |
| 1214 | di_flags |= XFS_DIFLAG_EXTSIZE; |
| 1215 | ip->i_d.di_extsize = pip->i_d.di_extsize; |
| 1216 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1217 | } |
| 1218 | if ((pip->i_d.di_flags & XFS_DIFLAG_NOATIME) && |
| 1219 | xfs_inherit_noatime) |
Nathan Scott | 365ca83 | 2005-06-21 15:39:12 +1000 | [diff] [blame] | 1220 | di_flags |= XFS_DIFLAG_NOATIME; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | if ((pip->i_d.di_flags & XFS_DIFLAG_NODUMP) && |
| 1222 | xfs_inherit_nodump) |
Nathan Scott | 365ca83 | 2005-06-21 15:39:12 +1000 | [diff] [blame] | 1223 | di_flags |= XFS_DIFLAG_NODUMP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1224 | if ((pip->i_d.di_flags & XFS_DIFLAG_SYNC) && |
| 1225 | xfs_inherit_sync) |
Nathan Scott | 365ca83 | 2005-06-21 15:39:12 +1000 | [diff] [blame] | 1226 | di_flags |= XFS_DIFLAG_SYNC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1227 | if ((pip->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) && |
| 1228 | xfs_inherit_nosymlinks) |
Nathan Scott | 365ca83 | 2005-06-21 15:39:12 +1000 | [diff] [blame] | 1229 | di_flags |= XFS_DIFLAG_NOSYMLINKS; |
| 1230 | if (pip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) |
| 1231 | di_flags |= XFS_DIFLAG_PROJINHERIT; |
Barry Naujok | d3446ea | 2006-06-09 14:54:19 +1000 | [diff] [blame] | 1232 | if ((pip->i_d.di_flags & XFS_DIFLAG_NODEFRAG) && |
| 1233 | xfs_inherit_nodefrag) |
| 1234 | di_flags |= XFS_DIFLAG_NODEFRAG; |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 1235 | if (pip->i_d.di_flags & XFS_DIFLAG_FILESTREAM) |
| 1236 | di_flags |= XFS_DIFLAG_FILESTREAM; |
Nathan Scott | 365ca83 | 2005-06-21 15:39:12 +1000 | [diff] [blame] | 1237 | ip->i_d.di_flags |= di_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | } |
| 1239 | /* FALLTHROUGH */ |
| 1240 | case S_IFLNK: |
| 1241 | ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS; |
| 1242 | ip->i_df.if_flags = XFS_IFEXTENTS; |
| 1243 | ip->i_df.if_bytes = ip->i_df.if_real_bytes = 0; |
| 1244 | ip->i_df.if_u1.if_extents = NULL; |
| 1245 | break; |
| 1246 | default: |
| 1247 | ASSERT(0); |
| 1248 | } |
| 1249 | /* |
| 1250 | * Attribute fork settings for new inode. |
| 1251 | */ |
| 1252 | ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS; |
| 1253 | ip->i_d.di_anextents = 0; |
| 1254 | |
| 1255 | /* |
| 1256 | * Log the new values stuffed into the inode. |
| 1257 | */ |
| 1258 | xfs_trans_log_inode(tp, ip, flags); |
| 1259 | |
Nathan Scott | b83bd13 | 2006-06-09 16:48:30 +1000 | [diff] [blame] | 1260 | /* 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] | 1261 | xfs_setup_inode(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 1263 | /* now we have set up the vfs inode we can associate the filestream */ |
| 1264 | if (filestreams) { |
| 1265 | error = xfs_filestream_associate(pip, ip); |
| 1266 | if (error < 0) |
| 1267 | return -error; |
| 1268 | if (!error) |
| 1269 | xfs_iflags_set(ip, XFS_IFILESTREAM); |
| 1270 | } |
| 1271 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1272 | *ipp = ip; |
| 1273 | return 0; |
| 1274 | } |
| 1275 | |
| 1276 | /* |
| 1277 | * Check to make sure that there are no blocks allocated to the |
| 1278 | * file beyond the size of the file. We don't check this for |
| 1279 | * files with fixed size extents or real time extents, but we |
| 1280 | * at least do it for regular files. |
| 1281 | */ |
| 1282 | #ifdef DEBUG |
| 1283 | void |
| 1284 | xfs_isize_check( |
| 1285 | xfs_mount_t *mp, |
| 1286 | xfs_inode_t *ip, |
| 1287 | xfs_fsize_t isize) |
| 1288 | { |
| 1289 | xfs_fileoff_t map_first; |
| 1290 | int nimaps; |
| 1291 | xfs_bmbt_irec_t imaps[2]; |
| 1292 | |
| 1293 | if ((ip->i_d.di_mode & S_IFMT) != S_IFREG) |
| 1294 | return; |
| 1295 | |
Eric Sandeen | 71ddabb | 2007-11-23 16:29:42 +1100 | [diff] [blame] | 1296 | if (XFS_IS_REALTIME_INODE(ip)) |
| 1297 | return; |
| 1298 | |
| 1299 | if (ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | return; |
| 1301 | |
| 1302 | nimaps = 2; |
| 1303 | map_first = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize); |
| 1304 | /* |
| 1305 | * The filesystem could be shutting down, so bmapi may return |
| 1306 | * an error. |
| 1307 | */ |
| 1308 | if (xfs_bmapi(NULL, ip, map_first, |
| 1309 | (XFS_B_TO_FSB(mp, |
| 1310 | (xfs_ufsize_t)XFS_MAXIOFFSET(mp)) - |
| 1311 | map_first), |
| 1312 | XFS_BMAPI_ENTIRE, NULL, 0, imaps, &nimaps, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1313 | NULL, NULL)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | return; |
| 1315 | ASSERT(nimaps == 1); |
| 1316 | ASSERT(imaps[0].br_startblock == HOLESTARTBLOCK); |
| 1317 | } |
| 1318 | #endif /* DEBUG */ |
| 1319 | |
| 1320 | /* |
| 1321 | * Calculate the last possible buffered byte in a file. This must |
| 1322 | * include data that was buffered beyond the EOF by the write code. |
| 1323 | * This also needs to deal with overflowing the xfs_fsize_t type |
| 1324 | * which can happen for sizes near the limit. |
| 1325 | * |
| 1326 | * We also need to take into account any blocks beyond the EOF. It |
| 1327 | * may be the case that they were buffered by a write which failed. |
| 1328 | * In that case the pages will still be in memory, but the inode size |
| 1329 | * will never have been updated. |
| 1330 | */ |
| 1331 | xfs_fsize_t |
| 1332 | xfs_file_last_byte( |
| 1333 | xfs_inode_t *ip) |
| 1334 | { |
| 1335 | xfs_mount_t *mp; |
| 1336 | xfs_fsize_t last_byte; |
| 1337 | xfs_fileoff_t last_block; |
| 1338 | xfs_fileoff_t size_last_block; |
| 1339 | int error; |
| 1340 | |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 1341 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1342 | |
| 1343 | mp = ip->i_mount; |
| 1344 | /* |
| 1345 | * Only check for blocks beyond the EOF if the extents have |
| 1346 | * been read in. This eliminates the need for the inode lock, |
| 1347 | * and it also saves us from looking when it really isn't |
| 1348 | * necessary. |
| 1349 | */ |
| 1350 | if (ip->i_df.if_flags & XFS_IFEXTENTS) { |
| 1351 | error = xfs_bmap_last_offset(NULL, ip, &last_block, |
| 1352 | XFS_DATA_FORK); |
| 1353 | if (error) { |
| 1354 | last_block = 0; |
| 1355 | } |
| 1356 | } else { |
| 1357 | last_block = 0; |
| 1358 | } |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 1359 | size_last_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)ip->i_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | last_block = XFS_FILEOFF_MAX(last_block, size_last_block); |
| 1361 | |
| 1362 | last_byte = XFS_FSB_TO_B(mp, last_block); |
| 1363 | if (last_byte < 0) { |
| 1364 | return XFS_MAXIOFFSET(mp); |
| 1365 | } |
| 1366 | last_byte += (1 << mp->m_writeio_log); |
| 1367 | if (last_byte < 0) { |
| 1368 | return XFS_MAXIOFFSET(mp); |
| 1369 | } |
| 1370 | return last_byte; |
| 1371 | } |
| 1372 | |
| 1373 | #if defined(XFS_RW_TRACE) |
| 1374 | STATIC void |
| 1375 | xfs_itrunc_trace( |
| 1376 | int tag, |
| 1377 | xfs_inode_t *ip, |
| 1378 | int flag, |
| 1379 | xfs_fsize_t new_size, |
| 1380 | xfs_off_t toss_start, |
| 1381 | xfs_off_t toss_finish) |
| 1382 | { |
| 1383 | if (ip->i_rwtrace == NULL) { |
| 1384 | return; |
| 1385 | } |
| 1386 | |
| 1387 | ktrace_enter(ip->i_rwtrace, |
| 1388 | (void*)((long)tag), |
| 1389 | (void*)ip, |
| 1390 | (void*)(unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff), |
| 1391 | (void*)(unsigned long)(ip->i_d.di_size & 0xffffffff), |
| 1392 | (void*)((long)flag), |
| 1393 | (void*)(unsigned long)((new_size >> 32) & 0xffffffff), |
| 1394 | (void*)(unsigned long)(new_size & 0xffffffff), |
| 1395 | (void*)(unsigned long)((toss_start >> 32) & 0xffffffff), |
| 1396 | (void*)(unsigned long)(toss_start & 0xffffffff), |
| 1397 | (void*)(unsigned long)((toss_finish >> 32) & 0xffffffff), |
| 1398 | (void*)(unsigned long)(toss_finish & 0xffffffff), |
| 1399 | (void*)(unsigned long)current_cpu(), |
Yingping Lu | f1fdc84 | 2006-03-22 12:44:15 +1100 | [diff] [blame] | 1400 | (void*)(unsigned long)current_pid(), |
| 1401 | (void*)NULL, |
| 1402 | (void*)NULL, |
| 1403 | (void*)NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1404 | } |
| 1405 | #else |
| 1406 | #define xfs_itrunc_trace(tag, ip, flag, new_size, toss_start, toss_finish) |
| 1407 | #endif |
| 1408 | |
| 1409 | /* |
| 1410 | * Start the truncation of the file to new_size. The new size |
| 1411 | * must be smaller than the current size. This routine will |
| 1412 | * clear the buffer and page caches of file data in the removed |
| 1413 | * range, and xfs_itruncate_finish() will remove the underlying |
| 1414 | * disk blocks. |
| 1415 | * |
| 1416 | * The inode must have its I/O lock locked EXCLUSIVELY, and it |
| 1417 | * must NOT have the inode lock held at all. This is because we're |
| 1418 | * calling into the buffer/page cache code and we can't hold the |
| 1419 | * inode lock when we do so. |
| 1420 | * |
David Chinner | 38e2299 | 2006-03-22 12:47:15 +1100 | [diff] [blame] | 1421 | * We need to wait for any direct I/Os in flight to complete before we |
| 1422 | * proceed with the truncate. This is needed to prevent the extents |
| 1423 | * being read or written by the direct I/Os from being removed while the |
| 1424 | * I/O is in flight as there is no other method of synchronising |
| 1425 | * direct I/O with the truncate operation. Also, because we hold |
| 1426 | * the IOLOCK in exclusive mode, we prevent new direct I/Os from being |
| 1427 | * started until the truncate completes and drops the lock. Essentially, |
| 1428 | * the vn_iowait() call forms an I/O barrier that provides strict ordering |
| 1429 | * between direct I/Os and the truncate operation. |
| 1430 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1431 | * The flags parameter can have either the value XFS_ITRUNC_DEFINITE |
| 1432 | * or XFS_ITRUNC_MAYBE. The XFS_ITRUNC_MAYBE value should be used |
| 1433 | * in the case that the caller is locking things out of order and |
| 1434 | * may not be able to call xfs_itruncate_finish() with the inode lock |
| 1435 | * held without dropping the I/O lock. If the caller must drop the |
| 1436 | * I/O lock before calling xfs_itruncate_finish(), then xfs_itruncate_start() |
| 1437 | * must be called again with all the same restrictions as the initial |
| 1438 | * call. |
| 1439 | */ |
Lachlan McIlroy | d3cf209 | 2007-05-08 13:49:27 +1000 | [diff] [blame] | 1440 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1441 | xfs_itruncate_start( |
| 1442 | xfs_inode_t *ip, |
| 1443 | uint flags, |
| 1444 | xfs_fsize_t new_size) |
| 1445 | { |
| 1446 | xfs_fsize_t last_byte; |
| 1447 | xfs_off_t toss_start; |
| 1448 | xfs_mount_t *mp; |
Lachlan McIlroy | d3cf209 | 2007-05-08 13:49:27 +1000 | [diff] [blame] | 1449 | int error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1450 | |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 1451 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL)); |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 1452 | ASSERT((new_size == 0) || (new_size <= ip->i_size)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1453 | ASSERT((flags == XFS_ITRUNC_DEFINITE) || |
| 1454 | (flags == XFS_ITRUNC_MAYBE)); |
| 1455 | |
| 1456 | mp = ip->i_mount; |
Yingping Lu | 9fa8046 | 2006-03-22 12:44:35 +1100 | [diff] [blame] | 1457 | |
Lachlan McIlroy | c734c79 | 2007-12-18 16:17:41 +1100 | [diff] [blame] | 1458 | /* wait for the completion of any pending DIOs */ |
Lachlan McIlroy | d112f29 | 2008-10-30 16:59:06 +1100 | [diff] [blame] | 1459 | if (new_size == 0 || new_size < ip->i_size) |
Lachlan McIlroy | c734c79 | 2007-12-18 16:17:41 +1100 | [diff] [blame] | 1460 | vn_iowait(ip); |
| 1461 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1462 | /* |
Nathan Scott | 67fcaa7 | 2006-06-09 17:00:52 +1000 | [diff] [blame] | 1463 | * Call toss_pages or flushinval_pages to get rid of pages |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1464 | * overlapping the region being removed. We have to use |
Nathan Scott | 67fcaa7 | 2006-06-09 17:00:52 +1000 | [diff] [blame] | 1465 | * the less efficient flushinval_pages in the case that the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1466 | * caller may not be able to finish the truncate without |
| 1467 | * dropping the inode's I/O lock. Make sure |
| 1468 | * to catch any pages brought in by buffers overlapping |
| 1469 | * the EOF by searching out beyond the isize by our |
| 1470 | * block size. We round new_size up to a block boundary |
| 1471 | * so that we don't toss things on the same block as |
| 1472 | * new_size but before it. |
| 1473 | * |
Nathan Scott | 67fcaa7 | 2006-06-09 17:00:52 +1000 | [diff] [blame] | 1474 | * Before calling toss_page or flushinval_pages, make sure to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1475 | * call remapf() over the same region if the file is mapped. |
| 1476 | * This frees up mapped file references to the pages in the |
Nathan Scott | 67fcaa7 | 2006-06-09 17:00:52 +1000 | [diff] [blame] | 1477 | * given range and for the flushinval_pages case it ensures |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1478 | * that we get the latest mapped changes flushed out. |
| 1479 | */ |
| 1480 | toss_start = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size); |
| 1481 | toss_start = XFS_FSB_TO_B(mp, toss_start); |
| 1482 | if (toss_start < 0) { |
| 1483 | /* |
| 1484 | * The place to start tossing is beyond our maximum |
| 1485 | * file size, so there is no way that the data extended |
| 1486 | * out there. |
| 1487 | */ |
Lachlan McIlroy | d3cf209 | 2007-05-08 13:49:27 +1000 | [diff] [blame] | 1488 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1489 | } |
| 1490 | last_byte = xfs_file_last_byte(ip); |
| 1491 | xfs_itrunc_trace(XFS_ITRUNC_START, ip, flags, new_size, toss_start, |
| 1492 | last_byte); |
| 1493 | if (last_byte > toss_start) { |
| 1494 | if (flags & XFS_ITRUNC_DEFINITE) { |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 1495 | xfs_tosspages(ip, toss_start, |
| 1496 | -1, FI_REMAPF_LOCKED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 | } else { |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 1498 | error = xfs_flushinval_pages(ip, toss_start, |
| 1499 | -1, FI_REMAPF_LOCKED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1500 | } |
| 1501 | } |
| 1502 | |
| 1503 | #ifdef DEBUG |
| 1504 | if (new_size == 0) { |
Christoph Hellwig | df80c93 | 2008-08-13 16:22:09 +1000 | [diff] [blame] | 1505 | ASSERT(VN_CACHED(VFS_I(ip)) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1506 | } |
| 1507 | #endif |
Lachlan McIlroy | d3cf209 | 2007-05-08 13:49:27 +1000 | [diff] [blame] | 1508 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1509 | } |
| 1510 | |
| 1511 | /* |
David Chinner | f648505 | 2008-04-17 16:50:04 +1000 | [diff] [blame] | 1512 | * Shrink the file to the given new_size. The new size must be smaller than |
| 1513 | * the current size. This will free up the underlying blocks in the removed |
| 1514 | * range after a call to xfs_itruncate_start() or xfs_atruncate_start(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1515 | * |
David Chinner | f648505 | 2008-04-17 16:50:04 +1000 | [diff] [blame] | 1516 | * The transaction passed to this routine must have made a permanent log |
| 1517 | * reservation of at least XFS_ITRUNCATE_LOG_RES. This routine may commit the |
| 1518 | * given transaction and start new ones, so make sure everything involved in |
| 1519 | * the transaction is tidy before calling here. Some transaction will be |
| 1520 | * returned to the caller to be committed. The incoming transaction must |
| 1521 | * already include the inode, and both inode locks must be held exclusively. |
| 1522 | * The inode must also be "held" within the transaction. On return the inode |
| 1523 | * will be "held" within the returned transaction. This routine does NOT |
| 1524 | * require any disk space to be reserved for it within the transaction. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1525 | * |
David Chinner | f648505 | 2008-04-17 16:50:04 +1000 | [diff] [blame] | 1526 | * The fork parameter must be either xfs_attr_fork or xfs_data_fork, and it |
| 1527 | * indicates the fork which is to be truncated. For the attribute fork we only |
| 1528 | * support truncation to size 0. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1529 | * |
David Chinner | f648505 | 2008-04-17 16:50:04 +1000 | [diff] [blame] | 1530 | * We use the sync parameter to indicate whether or not the first transaction |
| 1531 | * we perform might have to be synchronous. For the attr fork, it needs to be |
| 1532 | * so if the unlink of the inode is not yet known to be permanent in the log. |
| 1533 | * This keeps us from freeing and reusing the blocks of the attribute fork |
| 1534 | * before the unlink of the inode becomes permanent. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1535 | * |
David Chinner | f648505 | 2008-04-17 16:50:04 +1000 | [diff] [blame] | 1536 | * For the data fork, we normally have to run synchronously if we're being |
| 1537 | * called out of the inactive path or we're being called out of the create path |
| 1538 | * where we're truncating an existing file. Either way, the truncate needs to |
| 1539 | * be sync so blocks don't reappear in the file with altered data in case of a |
| 1540 | * crash. wsync filesystems can run the first case async because anything that |
| 1541 | * shrinks the inode has to run sync so by the time we're called here from |
| 1542 | * inactive, the inode size is permanently set to 0. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1543 | * |
David Chinner | f648505 | 2008-04-17 16:50:04 +1000 | [diff] [blame] | 1544 | * Calls from the truncate path always need to be sync unless we're in a wsync |
| 1545 | * filesystem and the file has already been unlinked. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1546 | * |
David Chinner | f648505 | 2008-04-17 16:50:04 +1000 | [diff] [blame] | 1547 | * The caller is responsible for correctly setting the sync parameter. It gets |
| 1548 | * too hard for us to guess here which path we're being called out of just |
| 1549 | * based on inode state. |
| 1550 | * |
| 1551 | * If we get an error, we must return with the inode locked and linked into the |
| 1552 | * current transaction. This keeps things simple for the higher level code, |
| 1553 | * because it always knows that the inode is locked and held in the transaction |
| 1554 | * that returns to it whether errors occur or not. We don't mark the inode |
| 1555 | * dirty on error so that transactions can be easily aborted if possible. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1556 | */ |
| 1557 | int |
| 1558 | xfs_itruncate_finish( |
| 1559 | xfs_trans_t **tp, |
| 1560 | xfs_inode_t *ip, |
| 1561 | xfs_fsize_t new_size, |
| 1562 | int fork, |
| 1563 | int sync) |
| 1564 | { |
| 1565 | xfs_fsblock_t first_block; |
| 1566 | xfs_fileoff_t first_unmap_block; |
| 1567 | xfs_fileoff_t last_block; |
| 1568 | xfs_filblks_t unmap_len=0; |
| 1569 | xfs_mount_t *mp; |
| 1570 | xfs_trans_t *ntp; |
| 1571 | int done; |
| 1572 | int committed; |
| 1573 | xfs_bmap_free_t free_list; |
| 1574 | int error; |
| 1575 | |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 1576 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL)); |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 1577 | ASSERT((new_size == 0) || (new_size <= ip->i_size)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1578 | ASSERT(*tp != NULL); |
| 1579 | ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES); |
| 1580 | ASSERT(ip->i_transp == *tp); |
| 1581 | ASSERT(ip->i_itemp != NULL); |
| 1582 | ASSERT(ip->i_itemp->ili_flags & XFS_ILI_HOLD); |
| 1583 | |
| 1584 | |
| 1585 | ntp = *tp; |
| 1586 | mp = (ntp)->t_mountp; |
| 1587 | ASSERT(! XFS_NOT_DQATTACHED(mp, ip)); |
| 1588 | |
| 1589 | /* |
| 1590 | * We only support truncating the entire attribute fork. |
| 1591 | */ |
| 1592 | if (fork == XFS_ATTR_FORK) { |
| 1593 | new_size = 0LL; |
| 1594 | } |
| 1595 | first_unmap_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size); |
| 1596 | xfs_itrunc_trace(XFS_ITRUNC_FINISH1, ip, 0, new_size, 0, 0); |
| 1597 | /* |
| 1598 | * The first thing we do is set the size to new_size permanently |
| 1599 | * on disk. This way we don't have to worry about anyone ever |
| 1600 | * being able to look at the data being freed even in the face |
| 1601 | * of a crash. What we're getting around here is the case where |
| 1602 | * we free a block, it is allocated to another file, it is written |
| 1603 | * to, and then we crash. If the new data gets written to the |
| 1604 | * file but the log buffers containing the free and reallocation |
| 1605 | * don't, then we'd end up with garbage in the blocks being freed. |
| 1606 | * As long as we make the new_size permanent before actually |
| 1607 | * freeing any blocks it doesn't matter if they get writtten to. |
| 1608 | * |
| 1609 | * The callers must signal into us whether or not the size |
| 1610 | * setting here must be synchronous. There are a few cases |
| 1611 | * where it doesn't have to be synchronous. Those cases |
| 1612 | * occur if the file is unlinked and we know the unlink is |
| 1613 | * permanent or if the blocks being truncated are guaranteed |
| 1614 | * to be beyond the inode eof (regardless of the link count) |
| 1615 | * and the eof value is permanent. Both of these cases occur |
| 1616 | * only on wsync-mounted filesystems. In those cases, we're |
| 1617 | * guaranteed that no user will ever see the data in the blocks |
| 1618 | * that are being truncated so the truncate can run async. |
| 1619 | * In the free beyond eof case, the file may wind up with |
| 1620 | * more blocks allocated to it than it needs if we crash |
| 1621 | * and that won't get fixed until the next time the file |
| 1622 | * is re-opened and closed but that's ok as that shouldn't |
| 1623 | * be too many blocks. |
| 1624 | * |
| 1625 | * However, we can't just make all wsync xactions run async |
| 1626 | * because there's one call out of the create path that needs |
| 1627 | * to run sync where it's truncating an existing file to size |
| 1628 | * 0 whose size is > 0. |
| 1629 | * |
| 1630 | * It's probably possible to come up with a test in this |
| 1631 | * routine that would correctly distinguish all the above |
| 1632 | * cases from the values of the function parameters and the |
| 1633 | * inode state but for sanity's sake, I've decided to let the |
| 1634 | * layers above just tell us. It's simpler to correctly figure |
| 1635 | * out in the layer above exactly under what conditions we |
| 1636 | * can run async and I think it's easier for others read and |
| 1637 | * follow the logic in case something has to be changed. |
| 1638 | * cscope is your friend -- rcc. |
| 1639 | * |
| 1640 | * The attribute fork is much simpler. |
| 1641 | * |
| 1642 | * For the attribute fork we allow the caller to tell us whether |
| 1643 | * the unlink of the inode that led to this call is yet permanent |
| 1644 | * in the on disk log. If it is not and we will be freeing extents |
| 1645 | * in this inode then we make the first transaction synchronous |
| 1646 | * to make sure that the unlink is permanent by the time we free |
| 1647 | * the blocks. |
| 1648 | */ |
| 1649 | if (fork == XFS_DATA_FORK) { |
| 1650 | if (ip->i_d.di_nextents > 0) { |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 1651 | /* |
| 1652 | * If we are not changing the file size then do |
| 1653 | * not update the on-disk file size - we may be |
| 1654 | * called from xfs_inactive_free_eofblocks(). If we |
| 1655 | * update the on-disk file size and then the system |
| 1656 | * crashes before the contents of the file are |
| 1657 | * flushed to disk then the files may be full of |
| 1658 | * holes (ie NULL files bug). |
| 1659 | */ |
| 1660 | if (ip->i_size != new_size) { |
| 1661 | ip->i_d.di_size = new_size; |
| 1662 | ip->i_size = new_size; |
| 1663 | xfs_trans_log_inode(ntp, ip, XFS_ILOG_CORE); |
| 1664 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1665 | } |
| 1666 | } else if (sync) { |
| 1667 | ASSERT(!(mp->m_flags & XFS_MOUNT_WSYNC)); |
| 1668 | if (ip->i_d.di_anextents > 0) |
| 1669 | xfs_trans_set_sync(ntp); |
| 1670 | } |
| 1671 | ASSERT(fork == XFS_DATA_FORK || |
| 1672 | (fork == XFS_ATTR_FORK && |
| 1673 | ((sync && !(mp->m_flags & XFS_MOUNT_WSYNC)) || |
| 1674 | (sync == 0 && (mp->m_flags & XFS_MOUNT_WSYNC))))); |
| 1675 | |
| 1676 | /* |
| 1677 | * Since it is possible for space to become allocated beyond |
| 1678 | * the end of the file (in a crash where the space is allocated |
| 1679 | * but the inode size is not yet updated), simply remove any |
| 1680 | * blocks which show up between the new EOF and the maximum |
| 1681 | * possible file size. If the first block to be removed is |
| 1682 | * beyond the maximum file size (ie it is the same as last_block), |
| 1683 | * then there is nothing to do. |
| 1684 | */ |
| 1685 | last_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp)); |
| 1686 | ASSERT(first_unmap_block <= last_block); |
| 1687 | done = 0; |
| 1688 | if (last_block == first_unmap_block) { |
| 1689 | done = 1; |
| 1690 | } else { |
| 1691 | unmap_len = last_block - first_unmap_block + 1; |
| 1692 | } |
| 1693 | while (!done) { |
| 1694 | /* |
| 1695 | * Free up up to XFS_ITRUNC_MAX_EXTENTS. xfs_bunmapi() |
| 1696 | * will tell us whether it freed the entire range or |
| 1697 | * not. If this is a synchronous mount (wsync), |
| 1698 | * then we can tell bunmapi to keep all the |
| 1699 | * transactions asynchronous since the unlink |
| 1700 | * transaction that made this inode inactive has |
| 1701 | * already hit the disk. There's no danger of |
| 1702 | * the freed blocks being reused, there being a |
| 1703 | * crash, and the reused blocks suddenly reappearing |
| 1704 | * in this file with garbage in them once recovery |
| 1705 | * runs. |
| 1706 | */ |
| 1707 | XFS_BMAP_INIT(&free_list, &first_block); |
Lachlan McIlroy | 541d7d3 | 2007-10-11 17:34:33 +1000 | [diff] [blame] | 1708 | error = xfs_bunmapi(ntp, ip, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1709 | first_unmap_block, unmap_len, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1710 | XFS_BMAPI_AFLAG(fork) | |
| 1711 | (sync ? 0 : XFS_BMAPI_ASYNC), |
| 1712 | XFS_ITRUNC_MAX_EXTENTS, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1713 | &first_block, &free_list, |
| 1714 | NULL, &done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1715 | if (error) { |
| 1716 | /* |
| 1717 | * If the bunmapi call encounters an error, |
| 1718 | * return to the caller where the transaction |
| 1719 | * can be properly aborted. We just need to |
| 1720 | * make sure we're not holding any resources |
| 1721 | * that we were not when we came in. |
| 1722 | */ |
| 1723 | xfs_bmap_cancel(&free_list); |
| 1724 | return error; |
| 1725 | } |
| 1726 | |
| 1727 | /* |
| 1728 | * Duplicate the transaction that has the permanent |
| 1729 | * reservation and commit the old transaction. |
| 1730 | */ |
Eric Sandeen | f7c99b6 | 2007-02-10 18:37:16 +1100 | [diff] [blame] | 1731 | error = xfs_bmap_finish(tp, &free_list, &committed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1732 | ntp = *tp; |
David Chinner | f648505 | 2008-04-17 16:50:04 +1000 | [diff] [blame] | 1733 | if (committed) { |
| 1734 | /* link the inode into the next xact in the chain */ |
| 1735 | xfs_trans_ijoin(ntp, ip, |
| 1736 | XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL); |
| 1737 | xfs_trans_ihold(ntp, ip); |
| 1738 | } |
| 1739 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1740 | if (error) { |
| 1741 | /* |
David Chinner | f648505 | 2008-04-17 16:50:04 +1000 | [diff] [blame] | 1742 | * If the bmap finish call encounters an error, return |
| 1743 | * to the caller where the transaction can be properly |
| 1744 | * aborted. We just need to make sure we're not |
| 1745 | * holding any resources that we were not when we came |
| 1746 | * in. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1747 | * |
David Chinner | f648505 | 2008-04-17 16:50:04 +1000 | [diff] [blame] | 1748 | * Aborting from this point might lose some blocks in |
| 1749 | * the file system, but oh well. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1750 | */ |
| 1751 | xfs_bmap_cancel(&free_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1752 | return error; |
| 1753 | } |
| 1754 | |
| 1755 | if (committed) { |
| 1756 | /* |
David Chinner | f648505 | 2008-04-17 16:50:04 +1000 | [diff] [blame] | 1757 | * Mark the inode dirty so it will be logged and |
David Chinner | e5720ee | 2008-04-10 12:21:18 +1000 | [diff] [blame] | 1758 | * moved forward in the log as part of every commit. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1759 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1760 | xfs_trans_log_inode(ntp, ip, XFS_ILOG_CORE); |
| 1761 | } |
David Chinner | f648505 | 2008-04-17 16:50:04 +1000 | [diff] [blame] | 1762 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1763 | ntp = xfs_trans_dup(ntp); |
David Chinner | e5720ee | 2008-04-10 12:21:18 +1000 | [diff] [blame] | 1764 | error = xfs_trans_commit(*tp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1765 | *tp = ntp; |
David Chinner | e5720ee | 2008-04-10 12:21:18 +1000 | [diff] [blame] | 1766 | |
David Chinner | f648505 | 2008-04-17 16:50:04 +1000 | [diff] [blame] | 1767 | /* link the inode into the next transaction in the chain */ |
| 1768 | xfs_trans_ijoin(ntp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL); |
| 1769 | xfs_trans_ihold(ntp, ip); |
| 1770 | |
Dave Chinner | cc09c0d | 2008-11-17 17:37:10 +1100 | [diff] [blame] | 1771 | if (error) |
| 1772 | return error; |
| 1773 | /* |
| 1774 | * transaction commit worked ok so we can drop the extra ticket |
| 1775 | * reference that we gained in xfs_trans_dup() |
| 1776 | */ |
| 1777 | xfs_log_ticket_put(ntp->t_ticket); |
| 1778 | error = xfs_trans_reserve(ntp, 0, |
David Chinner | f648505 | 2008-04-17 16:50:04 +1000 | [diff] [blame] | 1779 | XFS_ITRUNCATE_LOG_RES(mp), 0, |
| 1780 | XFS_TRANS_PERM_LOG_RES, |
| 1781 | XFS_ITRUNCATE_LOG_COUNT); |
| 1782 | if (error) |
| 1783 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1784 | } |
| 1785 | /* |
| 1786 | * Only update the size in the case of the data fork, but |
| 1787 | * always re-log the inode so that our permanent transaction |
| 1788 | * can keep on rolling it forward in the log. |
| 1789 | */ |
| 1790 | if (fork == XFS_DATA_FORK) { |
| 1791 | xfs_isize_check(mp, ip, new_size); |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 1792 | /* |
| 1793 | * If we are not changing the file size then do |
| 1794 | * not update the on-disk file size - we may be |
| 1795 | * called from xfs_inactive_free_eofblocks(). If we |
| 1796 | * update the on-disk file size and then the system |
| 1797 | * crashes before the contents of the file are |
| 1798 | * flushed to disk then the files may be full of |
| 1799 | * holes (ie NULL files bug). |
| 1800 | */ |
| 1801 | if (ip->i_size != new_size) { |
| 1802 | ip->i_d.di_size = new_size; |
| 1803 | ip->i_size = new_size; |
| 1804 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1805 | } |
| 1806 | xfs_trans_log_inode(ntp, ip, XFS_ILOG_CORE); |
| 1807 | ASSERT((new_size != 0) || |
| 1808 | (fork == XFS_ATTR_FORK) || |
| 1809 | (ip->i_delayed_blks == 0)); |
| 1810 | ASSERT((new_size != 0) || |
| 1811 | (fork == XFS_ATTR_FORK) || |
| 1812 | (ip->i_d.di_nextents == 0)); |
| 1813 | xfs_itrunc_trace(XFS_ITRUNC_FINISH2, ip, 0, new_size, 0, 0); |
| 1814 | return 0; |
| 1815 | } |
| 1816 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1817 | /* |
| 1818 | * This is called when the inode's link count goes to 0. |
| 1819 | * We place the on-disk inode on a list in the AGI. It |
| 1820 | * will be pulled from this list when the inode is freed. |
| 1821 | */ |
| 1822 | int |
| 1823 | xfs_iunlink( |
| 1824 | xfs_trans_t *tp, |
| 1825 | xfs_inode_t *ip) |
| 1826 | { |
| 1827 | xfs_mount_t *mp; |
| 1828 | xfs_agi_t *agi; |
| 1829 | xfs_dinode_t *dip; |
| 1830 | xfs_buf_t *agibp; |
| 1831 | xfs_buf_t *ibp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1832 | xfs_agino_t agino; |
| 1833 | short bucket_index; |
| 1834 | int offset; |
| 1835 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1836 | |
| 1837 | ASSERT(ip->i_d.di_nlink == 0); |
| 1838 | ASSERT(ip->i_d.di_mode != 0); |
| 1839 | ASSERT(ip->i_transp == tp); |
| 1840 | |
| 1841 | mp = tp->t_mountp; |
| 1842 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1843 | /* |
| 1844 | * Get the agi buffer first. It ensures lock ordering |
| 1845 | * on the list. |
| 1846 | */ |
Christoph Hellwig | 5e1be0f | 2008-11-28 14:23:37 +1100 | [diff] [blame] | 1847 | 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] | 1848 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1849 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1850 | agi = XFS_BUF_TO_AGI(agibp); |
Christoph Hellwig | 5e1be0f | 2008-11-28 14:23:37 +1100 | [diff] [blame] | 1851 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1852 | /* |
| 1853 | * Get the index into the agi hash table for the |
| 1854 | * list this inode will go on. |
| 1855 | */ |
| 1856 | agino = XFS_INO_TO_AGINO(mp, ip->i_ino); |
| 1857 | ASSERT(agino != 0); |
| 1858 | bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS; |
| 1859 | ASSERT(agi->agi_unlinked[bucket_index]); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1860 | ASSERT(be32_to_cpu(agi->agi_unlinked[bucket_index]) != agino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1861 | |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1862 | if (be32_to_cpu(agi->agi_unlinked[bucket_index]) != NULLAGINO) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1863 | /* |
| 1864 | * There is already another inode in the bucket we need |
| 1865 | * to add ourselves to. Add us at the front of the list. |
| 1866 | * Here we put the head pointer into our next pointer, |
| 1867 | * and then we fall through to point the head at us. |
| 1868 | */ |
Christoph Hellwig | 76d8b27 | 2008-11-28 14:23:40 +1100 | [diff] [blame^] | 1869 | error = xfs_itobp(mp, tp, ip, &dip, &ibp, XFS_BUF_LOCK); |
Vlad Apostolov | c319b58 | 2007-11-23 16:27:51 +1100 | [diff] [blame] | 1870 | if (error) |
| 1871 | return error; |
| 1872 | |
Christoph Hellwig | 347d1c0 | 2007-08-28 13:57:51 +1000 | [diff] [blame] | 1873 | ASSERT(be32_to_cpu(dip->di_next_unlinked) == NULLAGINO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1874 | /* both on-disk, don't endian flip twice */ |
| 1875 | dip->di_next_unlinked = agi->agi_unlinked[bucket_index]; |
| 1876 | offset = ip->i_boffset + |
| 1877 | offsetof(xfs_dinode_t, di_next_unlinked); |
| 1878 | xfs_trans_inode_buf(tp, ibp); |
| 1879 | xfs_trans_log_buf(tp, ibp, offset, |
| 1880 | (offset + sizeof(xfs_agino_t) - 1)); |
| 1881 | xfs_inobp_check(mp, ibp); |
| 1882 | } |
| 1883 | |
| 1884 | /* |
| 1885 | * Point the bucket head pointer at the inode being inserted. |
| 1886 | */ |
| 1887 | ASSERT(agino != 0); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1888 | agi->agi_unlinked[bucket_index] = cpu_to_be32(agino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1889 | offset = offsetof(xfs_agi_t, agi_unlinked) + |
| 1890 | (sizeof(xfs_agino_t) * bucket_index); |
| 1891 | xfs_trans_log_buf(tp, agibp, offset, |
| 1892 | (offset + sizeof(xfs_agino_t) - 1)); |
| 1893 | return 0; |
| 1894 | } |
| 1895 | |
| 1896 | /* |
| 1897 | * Pull the on-disk inode from the AGI unlinked list. |
| 1898 | */ |
| 1899 | STATIC int |
| 1900 | xfs_iunlink_remove( |
| 1901 | xfs_trans_t *tp, |
| 1902 | xfs_inode_t *ip) |
| 1903 | { |
| 1904 | xfs_ino_t next_ino; |
| 1905 | xfs_mount_t *mp; |
| 1906 | xfs_agi_t *agi; |
| 1907 | xfs_dinode_t *dip; |
| 1908 | xfs_buf_t *agibp; |
| 1909 | xfs_buf_t *ibp; |
| 1910 | xfs_agnumber_t agno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1911 | xfs_agino_t agino; |
| 1912 | xfs_agino_t next_agino; |
| 1913 | xfs_buf_t *last_ibp; |
Nathan Scott | 6fdf8cc | 2006-06-28 10:13:52 +1000 | [diff] [blame] | 1914 | xfs_dinode_t *last_dip = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1915 | short bucket_index; |
Nathan Scott | 6fdf8cc | 2006-06-28 10:13:52 +1000 | [diff] [blame] | 1916 | int offset, last_offset = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1917 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1918 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1919 | mp = tp->t_mountp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1920 | agno = XFS_INO_TO_AGNO(mp, ip->i_ino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1921 | |
| 1922 | /* |
| 1923 | * Get the agi buffer first. It ensures lock ordering |
| 1924 | * on the list. |
| 1925 | */ |
Christoph Hellwig | 5e1be0f | 2008-11-28 14:23:37 +1100 | [diff] [blame] | 1926 | error = xfs_read_agi(mp, tp, agno, &agibp); |
| 1927 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1928 | return error; |
Christoph Hellwig | 5e1be0f | 2008-11-28 14:23:37 +1100 | [diff] [blame] | 1929 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1930 | agi = XFS_BUF_TO_AGI(agibp); |
Christoph Hellwig | 5e1be0f | 2008-11-28 14:23:37 +1100 | [diff] [blame] | 1931 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1932 | /* |
| 1933 | * Get the index into the agi hash table for the |
| 1934 | * list this inode will go on. |
| 1935 | */ |
| 1936 | agino = XFS_INO_TO_AGINO(mp, ip->i_ino); |
| 1937 | ASSERT(agino != 0); |
| 1938 | bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS; |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1939 | ASSERT(be32_to_cpu(agi->agi_unlinked[bucket_index]) != NULLAGINO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1940 | ASSERT(agi->agi_unlinked[bucket_index]); |
| 1941 | |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1942 | if (be32_to_cpu(agi->agi_unlinked[bucket_index]) == agino) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1943 | /* |
| 1944 | * We're at the head of the list. Get the inode's |
| 1945 | * on-disk buffer to see if there is anyone after us |
| 1946 | * on the list. Only modify our next pointer if it |
| 1947 | * is not already NULLAGINO. This saves us the overhead |
| 1948 | * of dealing with the buffer when there is no need to |
| 1949 | * change it. |
| 1950 | */ |
Christoph Hellwig | 76d8b27 | 2008-11-28 14:23:40 +1100 | [diff] [blame^] | 1951 | error = xfs_itobp(mp, tp, ip, &dip, &ibp, XFS_BUF_LOCK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1952 | if (error) { |
| 1953 | cmn_err(CE_WARN, |
| 1954 | "xfs_iunlink_remove: xfs_itobp() returned an error %d on %s. Returning error.", |
| 1955 | error, mp->m_fsname); |
| 1956 | return error; |
| 1957 | } |
Christoph Hellwig | 347d1c0 | 2007-08-28 13:57:51 +1000 | [diff] [blame] | 1958 | next_agino = be32_to_cpu(dip->di_next_unlinked); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1959 | ASSERT(next_agino != 0); |
| 1960 | if (next_agino != NULLAGINO) { |
Christoph Hellwig | 347d1c0 | 2007-08-28 13:57:51 +1000 | [diff] [blame] | 1961 | dip->di_next_unlinked = cpu_to_be32(NULLAGINO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1962 | offset = ip->i_boffset + |
| 1963 | offsetof(xfs_dinode_t, di_next_unlinked); |
| 1964 | xfs_trans_inode_buf(tp, ibp); |
| 1965 | xfs_trans_log_buf(tp, ibp, offset, |
| 1966 | (offset + sizeof(xfs_agino_t) - 1)); |
| 1967 | xfs_inobp_check(mp, ibp); |
| 1968 | } else { |
| 1969 | xfs_trans_brelse(tp, ibp); |
| 1970 | } |
| 1971 | /* |
| 1972 | * Point the bucket head pointer at the next inode. |
| 1973 | */ |
| 1974 | ASSERT(next_agino != 0); |
| 1975 | ASSERT(next_agino != agino); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1976 | agi->agi_unlinked[bucket_index] = cpu_to_be32(next_agino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1977 | offset = offsetof(xfs_agi_t, agi_unlinked) + |
| 1978 | (sizeof(xfs_agino_t) * bucket_index); |
| 1979 | xfs_trans_log_buf(tp, agibp, offset, |
| 1980 | (offset + sizeof(xfs_agino_t) - 1)); |
| 1981 | } else { |
| 1982 | /* |
| 1983 | * We need to search the list for the inode being freed. |
| 1984 | */ |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1985 | next_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1986 | last_ibp = NULL; |
| 1987 | while (next_agino != agino) { |
| 1988 | /* |
| 1989 | * If the last inode wasn't the one pointing to |
| 1990 | * us, then release its buffer since we're not |
| 1991 | * going to do anything with it. |
| 1992 | */ |
| 1993 | if (last_ibp != NULL) { |
| 1994 | xfs_trans_brelse(tp, last_ibp); |
| 1995 | } |
| 1996 | next_ino = XFS_AGINO_TO_INO(mp, agno, next_agino); |
| 1997 | error = xfs_inotobp(mp, tp, next_ino, &last_dip, |
Christoph Hellwig | c679eef | 2008-10-30 18:04:13 +1100 | [diff] [blame] | 1998 | &last_ibp, &last_offset, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1999 | if (error) { |
| 2000 | cmn_err(CE_WARN, |
| 2001 | "xfs_iunlink_remove: xfs_inotobp() returned an error %d on %s. Returning error.", |
| 2002 | error, mp->m_fsname); |
| 2003 | return error; |
| 2004 | } |
Christoph Hellwig | 347d1c0 | 2007-08-28 13:57:51 +1000 | [diff] [blame] | 2005 | next_agino = be32_to_cpu(last_dip->di_next_unlinked); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2006 | ASSERT(next_agino != NULLAGINO); |
| 2007 | ASSERT(next_agino != 0); |
| 2008 | } |
| 2009 | /* |
| 2010 | * Now last_ibp points to the buffer previous to us on |
| 2011 | * the unlinked list. Pull us from the list. |
| 2012 | */ |
Christoph Hellwig | 76d8b27 | 2008-11-28 14:23:40 +1100 | [diff] [blame^] | 2013 | error = xfs_itobp(mp, tp, ip, &dip, &ibp, XFS_BUF_LOCK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2014 | if (error) { |
| 2015 | cmn_err(CE_WARN, |
| 2016 | "xfs_iunlink_remove: xfs_itobp() returned an error %d on %s. Returning error.", |
| 2017 | error, mp->m_fsname); |
| 2018 | return error; |
| 2019 | } |
Christoph Hellwig | 347d1c0 | 2007-08-28 13:57:51 +1000 | [diff] [blame] | 2020 | next_agino = be32_to_cpu(dip->di_next_unlinked); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2021 | ASSERT(next_agino != 0); |
| 2022 | ASSERT(next_agino != agino); |
| 2023 | if (next_agino != NULLAGINO) { |
Christoph Hellwig | 347d1c0 | 2007-08-28 13:57:51 +1000 | [diff] [blame] | 2024 | dip->di_next_unlinked = cpu_to_be32(NULLAGINO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2025 | offset = ip->i_boffset + |
| 2026 | offsetof(xfs_dinode_t, di_next_unlinked); |
| 2027 | xfs_trans_inode_buf(tp, ibp); |
| 2028 | xfs_trans_log_buf(tp, ibp, offset, |
| 2029 | (offset + sizeof(xfs_agino_t) - 1)); |
| 2030 | xfs_inobp_check(mp, ibp); |
| 2031 | } else { |
| 2032 | xfs_trans_brelse(tp, ibp); |
| 2033 | } |
| 2034 | /* |
| 2035 | * Point the previous inode on the list to the next inode. |
| 2036 | */ |
Christoph Hellwig | 347d1c0 | 2007-08-28 13:57:51 +1000 | [diff] [blame] | 2037 | last_dip->di_next_unlinked = cpu_to_be32(next_agino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2038 | ASSERT(next_agino != 0); |
| 2039 | offset = last_offset + offsetof(xfs_dinode_t, di_next_unlinked); |
| 2040 | xfs_trans_inode_buf(tp, last_ibp); |
| 2041 | xfs_trans_log_buf(tp, last_ibp, offset, |
| 2042 | (offset + sizeof(xfs_agino_t) - 1)); |
| 2043 | xfs_inobp_check(mp, last_ibp); |
| 2044 | } |
| 2045 | return 0; |
| 2046 | } |
| 2047 | |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 2048 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2049 | xfs_ifree_cluster( |
| 2050 | xfs_inode_t *free_ip, |
| 2051 | xfs_trans_t *tp, |
| 2052 | xfs_ino_t inum) |
| 2053 | { |
| 2054 | xfs_mount_t *mp = free_ip->i_mount; |
| 2055 | int blks_per_cluster; |
| 2056 | int nbufs; |
| 2057 | int ninodes; |
| 2058 | int i, j, found, pre_flushed; |
| 2059 | xfs_daddr_t blkno; |
| 2060 | xfs_buf_t *bp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2061 | xfs_inode_t *ip, **ip_found; |
| 2062 | xfs_inode_log_item_t *iip; |
| 2063 | xfs_log_item_t *lip; |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 2064 | xfs_perag_t *pag = xfs_get_perag(mp, inum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2065 | |
| 2066 | if (mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) { |
| 2067 | blks_per_cluster = 1; |
| 2068 | ninodes = mp->m_sb.sb_inopblock; |
| 2069 | nbufs = XFS_IALLOC_BLOCKS(mp); |
| 2070 | } else { |
| 2071 | blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) / |
| 2072 | mp->m_sb.sb_blocksize; |
| 2073 | ninodes = blks_per_cluster * mp->m_sb.sb_inopblock; |
| 2074 | nbufs = XFS_IALLOC_BLOCKS(mp) / blks_per_cluster; |
| 2075 | } |
| 2076 | |
| 2077 | ip_found = kmem_alloc(ninodes * sizeof(xfs_inode_t *), KM_NOFS); |
| 2078 | |
| 2079 | for (j = 0; j < nbufs; j++, inum += ninodes) { |
| 2080 | blkno = XFS_AGB_TO_DADDR(mp, XFS_INO_TO_AGNO(mp, inum), |
| 2081 | XFS_INO_TO_AGBNO(mp, inum)); |
| 2082 | |
| 2083 | |
| 2084 | /* |
| 2085 | * Look for each inode in memory and attempt to lock it, |
| 2086 | * we can be racing with flush and tail pushing here. |
| 2087 | * any inode we get the locks on, add to an array of |
| 2088 | * inode items to process later. |
| 2089 | * |
| 2090 | * The get the buffer lock, we could beat a flush |
| 2091 | * or tail pushing thread to the lock here, in which |
| 2092 | * case they will go looking for the inode buffer |
| 2093 | * and fail, we need some other form of interlock |
| 2094 | * here. |
| 2095 | */ |
| 2096 | found = 0; |
| 2097 | for (i = 0; i < ninodes; i++) { |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 2098 | read_lock(&pag->pag_ici_lock); |
| 2099 | ip = radix_tree_lookup(&pag->pag_ici_root, |
| 2100 | XFS_INO_TO_AGINO(mp, (inum + i))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2101 | |
| 2102 | /* Inode not in memory or we found it already, |
| 2103 | * nothing to do |
| 2104 | */ |
David Chinner | 7a18c38 | 2006-11-11 18:04:54 +1100 | [diff] [blame] | 2105 | if (!ip || xfs_iflags_test(ip, XFS_ISTALE)) { |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 2106 | read_unlock(&pag->pag_ici_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2107 | continue; |
| 2108 | } |
| 2109 | |
| 2110 | if (xfs_inode_clean(ip)) { |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 2111 | read_unlock(&pag->pag_ici_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2112 | continue; |
| 2113 | } |
| 2114 | |
| 2115 | /* If we can get the locks then add it to the |
| 2116 | * list, otherwise by the time we get the bp lock |
| 2117 | * below it will already be attached to the |
| 2118 | * inode buffer. |
| 2119 | */ |
| 2120 | |
| 2121 | /* This inode will already be locked - by us, lets |
| 2122 | * keep it that way. |
| 2123 | */ |
| 2124 | |
| 2125 | if (ip == free_ip) { |
| 2126 | if (xfs_iflock_nowait(ip)) { |
David Chinner | 7a18c38 | 2006-11-11 18:04:54 +1100 | [diff] [blame] | 2127 | xfs_iflags_set(ip, XFS_ISTALE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2128 | if (xfs_inode_clean(ip)) { |
| 2129 | xfs_ifunlock(ip); |
| 2130 | } else { |
| 2131 | ip_found[found++] = ip; |
| 2132 | } |
| 2133 | } |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 2134 | read_unlock(&pag->pag_ici_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2135 | continue; |
| 2136 | } |
| 2137 | |
| 2138 | if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) { |
| 2139 | if (xfs_iflock_nowait(ip)) { |
David Chinner | 7a18c38 | 2006-11-11 18:04:54 +1100 | [diff] [blame] | 2140 | xfs_iflags_set(ip, XFS_ISTALE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2141 | |
| 2142 | if (xfs_inode_clean(ip)) { |
| 2143 | xfs_ifunlock(ip); |
| 2144 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 2145 | } else { |
| 2146 | ip_found[found++] = ip; |
| 2147 | } |
| 2148 | } else { |
| 2149 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 2150 | } |
| 2151 | } |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 2152 | read_unlock(&pag->pag_ici_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2153 | } |
| 2154 | |
| 2155 | bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno, |
| 2156 | mp->m_bsize * blks_per_cluster, |
| 2157 | XFS_BUF_LOCK); |
| 2158 | |
| 2159 | pre_flushed = 0; |
| 2160 | lip = XFS_BUF_FSPRIVATE(bp, xfs_log_item_t *); |
| 2161 | while (lip) { |
| 2162 | if (lip->li_type == XFS_LI_INODE) { |
| 2163 | iip = (xfs_inode_log_item_t *)lip; |
| 2164 | ASSERT(iip->ili_logged == 1); |
| 2165 | lip->li_cb = (void(*)(xfs_buf_t*,xfs_log_item_t*)) xfs_istale_done; |
David Chinner | 7b2e2a3 | 2008-10-30 17:39:12 +1100 | [diff] [blame] | 2166 | xfs_trans_ail_copy_lsn(mp->m_ail, |
| 2167 | &iip->ili_flush_lsn, |
| 2168 | &iip->ili_item.li_lsn); |
David Chinner | e5ffd2b | 2006-11-21 18:55:33 +1100 | [diff] [blame] | 2169 | xfs_iflags_set(iip->ili_inode, XFS_ISTALE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2170 | pre_flushed++; |
| 2171 | } |
| 2172 | lip = lip->li_bio_list; |
| 2173 | } |
| 2174 | |
| 2175 | for (i = 0; i < found; i++) { |
| 2176 | ip = ip_found[i]; |
| 2177 | iip = ip->i_itemp; |
| 2178 | |
| 2179 | if (!iip) { |
| 2180 | ip->i_update_core = 0; |
| 2181 | xfs_ifunlock(ip); |
| 2182 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 2183 | continue; |
| 2184 | } |
| 2185 | |
| 2186 | iip->ili_last_fields = iip->ili_format.ilf_fields; |
| 2187 | iip->ili_format.ilf_fields = 0; |
| 2188 | iip->ili_logged = 1; |
David Chinner | 7b2e2a3 | 2008-10-30 17:39:12 +1100 | [diff] [blame] | 2189 | xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn, |
| 2190 | &iip->ili_item.li_lsn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2191 | |
| 2192 | xfs_buf_attach_iodone(bp, |
| 2193 | (void(*)(xfs_buf_t*,xfs_log_item_t*)) |
| 2194 | xfs_istale_done, (xfs_log_item_t *)iip); |
| 2195 | if (ip != free_ip) { |
| 2196 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 2197 | } |
| 2198 | } |
| 2199 | |
| 2200 | if (found || pre_flushed) |
| 2201 | xfs_trans_stale_inode_buf(tp, bp); |
| 2202 | xfs_trans_binval(tp, bp); |
| 2203 | } |
| 2204 | |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 2205 | kmem_free(ip_found); |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 2206 | xfs_put_perag(mp, pag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2207 | } |
| 2208 | |
| 2209 | /* |
| 2210 | * This is called to return an inode to the inode free list. |
| 2211 | * The inode should already be truncated to 0 length and have |
| 2212 | * no pages associated with it. This routine also assumes that |
| 2213 | * the inode is already a part of the transaction. |
| 2214 | * |
| 2215 | * The on-disk copy of the inode will have been added to the list |
| 2216 | * of unlinked inodes in the AGI. We need to remove the inode from |
| 2217 | * that list atomically with respect to freeing it here. |
| 2218 | */ |
| 2219 | int |
| 2220 | xfs_ifree( |
| 2221 | xfs_trans_t *tp, |
| 2222 | xfs_inode_t *ip, |
| 2223 | xfs_bmap_free_t *flist) |
| 2224 | { |
| 2225 | int error; |
| 2226 | int delete; |
| 2227 | xfs_ino_t first_ino; |
Vlad Apostolov | c319b58 | 2007-11-23 16:27:51 +1100 | [diff] [blame] | 2228 | xfs_dinode_t *dip; |
| 2229 | xfs_buf_t *ibp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2230 | |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 2231 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2232 | ASSERT(ip->i_transp == tp); |
| 2233 | ASSERT(ip->i_d.di_nlink == 0); |
| 2234 | ASSERT(ip->i_d.di_nextents == 0); |
| 2235 | ASSERT(ip->i_d.di_anextents == 0); |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 2236 | ASSERT((ip->i_d.di_size == 0 && ip->i_size == 0) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2237 | ((ip->i_d.di_mode & S_IFMT) != S_IFREG)); |
| 2238 | ASSERT(ip->i_d.di_nblocks == 0); |
| 2239 | |
| 2240 | /* |
| 2241 | * Pull the on-disk inode from the AGI unlinked list. |
| 2242 | */ |
| 2243 | error = xfs_iunlink_remove(tp, ip); |
| 2244 | if (error != 0) { |
| 2245 | return error; |
| 2246 | } |
| 2247 | |
| 2248 | error = xfs_difree(tp, ip->i_ino, flist, &delete, &first_ino); |
| 2249 | if (error != 0) { |
| 2250 | return error; |
| 2251 | } |
| 2252 | ip->i_d.di_mode = 0; /* mark incore inode as free */ |
| 2253 | ip->i_d.di_flags = 0; |
| 2254 | ip->i_d.di_dmevmask = 0; |
| 2255 | ip->i_d.di_forkoff = 0; /* mark the attr fork not in use */ |
| 2256 | ip->i_df.if_ext_max = |
| 2257 | XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t); |
| 2258 | ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS; |
| 2259 | ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS; |
| 2260 | /* |
| 2261 | * Bump the generation count so no one will be confused |
| 2262 | * by reincarnations of this inode. |
| 2263 | */ |
| 2264 | ip->i_d.di_gen++; |
Vlad Apostolov | c319b58 | 2007-11-23 16:27:51 +1100 | [diff] [blame] | 2265 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2266 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 2267 | |
Christoph Hellwig | 76d8b27 | 2008-11-28 14:23:40 +1100 | [diff] [blame^] | 2268 | error = xfs_itobp(ip->i_mount, tp, ip, &dip, &ibp, XFS_BUF_LOCK); |
Vlad Apostolov | c319b58 | 2007-11-23 16:27:51 +1100 | [diff] [blame] | 2269 | if (error) |
| 2270 | return error; |
| 2271 | |
| 2272 | /* |
| 2273 | * Clear the on-disk di_mode. This is to prevent xfs_bulkstat |
| 2274 | * from picking up this inode when it is reclaimed (its incore state |
| 2275 | * initialzed but not flushed to disk yet). The in-core di_mode is |
| 2276 | * already cleared and a corresponding transaction logged. |
| 2277 | * The hack here just synchronizes the in-core to on-disk |
| 2278 | * di_mode value in advance before the actual inode sync to disk. |
| 2279 | * This is OK because the inode is already unlinked and would never |
| 2280 | * change its di_mode again for this inode generation. |
| 2281 | * This is a temporary hack that would require a proper fix |
| 2282 | * in the future. |
| 2283 | */ |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 2284 | dip->di_mode = 0; |
Vlad Apostolov | c319b58 | 2007-11-23 16:27:51 +1100 | [diff] [blame] | 2285 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2286 | if (delete) { |
| 2287 | xfs_ifree_cluster(ip, tp, first_ino); |
| 2288 | } |
| 2289 | |
| 2290 | return 0; |
| 2291 | } |
| 2292 | |
| 2293 | /* |
| 2294 | * Reallocate the space for if_broot based on the number of records |
| 2295 | * being added or deleted as indicated in rec_diff. Move the records |
| 2296 | * and pointers in if_broot to fit the new size. When shrinking this |
| 2297 | * will eliminate holes between the records and pointers created by |
| 2298 | * the caller. When growing this will create holes to be filled in |
| 2299 | * by the caller. |
| 2300 | * |
| 2301 | * The caller must not request to add more records than would fit in |
| 2302 | * the on-disk inode root. If the if_broot is currently NULL, then |
| 2303 | * if we adding records one will be allocated. The caller must also |
| 2304 | * not request that the number of records go below zero, although |
| 2305 | * it can go to zero. |
| 2306 | * |
| 2307 | * ip -- the inode whose if_broot area is changing |
| 2308 | * ext_diff -- the change in the number of records, positive or negative, |
| 2309 | * requested for the if_broot array. |
| 2310 | */ |
| 2311 | void |
| 2312 | xfs_iroot_realloc( |
| 2313 | xfs_inode_t *ip, |
| 2314 | int rec_diff, |
| 2315 | int whichfork) |
| 2316 | { |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 2317 | struct xfs_mount *mp = ip->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2318 | int cur_max; |
| 2319 | xfs_ifork_t *ifp; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 2320 | struct xfs_btree_block *new_broot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2321 | int new_max; |
| 2322 | size_t new_size; |
| 2323 | char *np; |
| 2324 | char *op; |
| 2325 | |
| 2326 | /* |
| 2327 | * Handle the degenerate case quietly. |
| 2328 | */ |
| 2329 | if (rec_diff == 0) { |
| 2330 | return; |
| 2331 | } |
| 2332 | |
| 2333 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 2334 | if (rec_diff > 0) { |
| 2335 | /* |
| 2336 | * If there wasn't any memory allocated before, just |
| 2337 | * allocate it now and get out. |
| 2338 | */ |
| 2339 | if (ifp->if_broot_bytes == 0) { |
| 2340 | new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(rec_diff); |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 2341 | ifp->if_broot = kmem_alloc(new_size, KM_SLEEP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2342 | ifp->if_broot_bytes = (int)new_size; |
| 2343 | return; |
| 2344 | } |
| 2345 | |
| 2346 | /* |
| 2347 | * If there is already an existing if_broot, then we need |
| 2348 | * to realloc() it and shift the pointers to their new |
| 2349 | * location. The records don't change location because |
| 2350 | * they are kept butted up against the btree block header. |
| 2351 | */ |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 2352 | cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2353 | new_max = cur_max + rec_diff; |
| 2354 | new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(new_max); |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 2355 | ifp->if_broot = kmem_realloc(ifp->if_broot, new_size, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2356 | (size_t)XFS_BMAP_BROOT_SPACE_CALC(cur_max), /* old size */ |
| 2357 | KM_SLEEP); |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 2358 | op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1, |
| 2359 | ifp->if_broot_bytes); |
| 2360 | np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1, |
| 2361 | (int)new_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2362 | ifp->if_broot_bytes = (int)new_size; |
| 2363 | ASSERT(ifp->if_broot_bytes <= |
| 2364 | XFS_IFORK_SIZE(ip, whichfork) + XFS_BROOT_SIZE_ADJ); |
| 2365 | memmove(np, op, cur_max * (uint)sizeof(xfs_dfsbno_t)); |
| 2366 | return; |
| 2367 | } |
| 2368 | |
| 2369 | /* |
| 2370 | * rec_diff is less than 0. In this case, we are shrinking the |
| 2371 | * if_broot buffer. It must already exist. If we go to zero |
| 2372 | * records, just get rid of the root and clear the status bit. |
| 2373 | */ |
| 2374 | ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0)); |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 2375 | cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2376 | new_max = cur_max + rec_diff; |
| 2377 | ASSERT(new_max >= 0); |
| 2378 | if (new_max > 0) |
| 2379 | new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(new_max); |
| 2380 | else |
| 2381 | new_size = 0; |
| 2382 | if (new_size > 0) { |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 2383 | new_broot = kmem_alloc(new_size, KM_SLEEP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2384 | /* |
| 2385 | * First copy over the btree block header. |
| 2386 | */ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 2387 | memcpy(new_broot, ifp->if_broot, XFS_BTREE_LBLOCK_LEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2388 | } else { |
| 2389 | new_broot = NULL; |
| 2390 | ifp->if_flags &= ~XFS_IFBROOT; |
| 2391 | } |
| 2392 | |
| 2393 | /* |
| 2394 | * Only copy the records and pointers if there are any. |
| 2395 | */ |
| 2396 | if (new_max > 0) { |
| 2397 | /* |
| 2398 | * First copy the records. |
| 2399 | */ |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 2400 | op = (char *)XFS_BMBT_REC_ADDR(mp, ifp->if_broot, 1); |
| 2401 | np = (char *)XFS_BMBT_REC_ADDR(mp, new_broot, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2402 | memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t)); |
| 2403 | |
| 2404 | /* |
| 2405 | * Then copy the pointers. |
| 2406 | */ |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 2407 | op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2408 | ifp->if_broot_bytes); |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 2409 | np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, new_broot, 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2410 | (int)new_size); |
| 2411 | memcpy(np, op, new_max * (uint)sizeof(xfs_dfsbno_t)); |
| 2412 | } |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 2413 | kmem_free(ifp->if_broot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2414 | ifp->if_broot = new_broot; |
| 2415 | ifp->if_broot_bytes = (int)new_size; |
| 2416 | ASSERT(ifp->if_broot_bytes <= |
| 2417 | XFS_IFORK_SIZE(ip, whichfork) + XFS_BROOT_SIZE_ADJ); |
| 2418 | return; |
| 2419 | } |
| 2420 | |
| 2421 | |
| 2422 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2423 | * This is called when the amount of space needed for if_data |
| 2424 | * is increased or decreased. The change in size is indicated by |
| 2425 | * the number of bytes that need to be added or deleted in the |
| 2426 | * byte_diff parameter. |
| 2427 | * |
| 2428 | * If the amount of space needed has decreased below the size of the |
| 2429 | * inline buffer, then switch to using the inline buffer. Otherwise, |
| 2430 | * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer |
| 2431 | * to what is needed. |
| 2432 | * |
| 2433 | * ip -- the inode whose if_data area is changing |
| 2434 | * byte_diff -- the change in the number of bytes, positive or negative, |
| 2435 | * requested for the if_data array. |
| 2436 | */ |
| 2437 | void |
| 2438 | xfs_idata_realloc( |
| 2439 | xfs_inode_t *ip, |
| 2440 | int byte_diff, |
| 2441 | int whichfork) |
| 2442 | { |
| 2443 | xfs_ifork_t *ifp; |
| 2444 | int new_size; |
| 2445 | int real_size; |
| 2446 | |
| 2447 | if (byte_diff == 0) { |
| 2448 | return; |
| 2449 | } |
| 2450 | |
| 2451 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 2452 | new_size = (int)ifp->if_bytes + byte_diff; |
| 2453 | ASSERT(new_size >= 0); |
| 2454 | |
| 2455 | if (new_size == 0) { |
| 2456 | if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) { |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 2457 | kmem_free(ifp->if_u1.if_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2458 | } |
| 2459 | ifp->if_u1.if_data = NULL; |
| 2460 | real_size = 0; |
| 2461 | } else if (new_size <= sizeof(ifp->if_u2.if_inline_data)) { |
| 2462 | /* |
| 2463 | * If the valid extents/data can fit in if_inline_ext/data, |
| 2464 | * copy them from the malloc'd vector and free it. |
| 2465 | */ |
| 2466 | if (ifp->if_u1.if_data == NULL) { |
| 2467 | ifp->if_u1.if_data = ifp->if_u2.if_inline_data; |
| 2468 | } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) { |
| 2469 | ASSERT(ifp->if_real_bytes != 0); |
| 2470 | memcpy(ifp->if_u2.if_inline_data, ifp->if_u1.if_data, |
| 2471 | new_size); |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 2472 | kmem_free(ifp->if_u1.if_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2473 | ifp->if_u1.if_data = ifp->if_u2.if_inline_data; |
| 2474 | } |
| 2475 | real_size = 0; |
| 2476 | } else { |
| 2477 | /* |
| 2478 | * Stuck with malloc/realloc. |
| 2479 | * For inline data, the underlying buffer must be |
| 2480 | * a multiple of 4 bytes in size so that it can be |
| 2481 | * logged and stay on word boundaries. We enforce |
| 2482 | * that here. |
| 2483 | */ |
| 2484 | real_size = roundup(new_size, 4); |
| 2485 | if (ifp->if_u1.if_data == NULL) { |
| 2486 | ASSERT(ifp->if_real_bytes == 0); |
| 2487 | ifp->if_u1.if_data = kmem_alloc(real_size, KM_SLEEP); |
| 2488 | } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) { |
| 2489 | /* |
| 2490 | * Only do the realloc if the underlying size |
| 2491 | * is really changing. |
| 2492 | */ |
| 2493 | if (ifp->if_real_bytes != real_size) { |
| 2494 | ifp->if_u1.if_data = |
| 2495 | kmem_realloc(ifp->if_u1.if_data, |
| 2496 | real_size, |
| 2497 | ifp->if_real_bytes, |
| 2498 | KM_SLEEP); |
| 2499 | } |
| 2500 | } else { |
| 2501 | ASSERT(ifp->if_real_bytes == 0); |
| 2502 | ifp->if_u1.if_data = kmem_alloc(real_size, KM_SLEEP); |
| 2503 | memcpy(ifp->if_u1.if_data, ifp->if_u2.if_inline_data, |
| 2504 | ifp->if_bytes); |
| 2505 | } |
| 2506 | } |
| 2507 | ifp->if_real_bytes = real_size; |
| 2508 | ifp->if_bytes = new_size; |
| 2509 | ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork)); |
| 2510 | } |
| 2511 | |
| 2512 | |
| 2513 | |
| 2514 | |
| 2515 | /* |
| 2516 | * Map inode to disk block and offset. |
| 2517 | * |
| 2518 | * mp -- the mount point structure for the current file system |
| 2519 | * tp -- the current transaction |
| 2520 | * ino -- the inode number of the inode to be located |
| 2521 | * imap -- this structure is filled in with the information necessary |
| 2522 | * to retrieve the given inode from disk |
| 2523 | * flags -- flags to pass to xfs_dilocate indicating whether or not |
| 2524 | * lookups in the inode btree were OK or not |
| 2525 | */ |
| 2526 | int |
| 2527 | xfs_imap( |
| 2528 | xfs_mount_t *mp, |
| 2529 | xfs_trans_t *tp, |
| 2530 | xfs_ino_t ino, |
| 2531 | xfs_imap_t *imap, |
| 2532 | uint flags) |
| 2533 | { |
| 2534 | xfs_fsblock_t fsbno; |
| 2535 | int len; |
| 2536 | int off; |
| 2537 | int error; |
| 2538 | |
| 2539 | fsbno = imap->im_blkno ? |
| 2540 | XFS_DADDR_TO_FSB(mp, imap->im_blkno) : NULLFSBLOCK; |
| 2541 | error = xfs_dilocate(mp, tp, ino, &fsbno, &len, &off, flags); |
David Chinner | 4ae29b4 | 2008-03-06 13:43:34 +1100 | [diff] [blame] | 2542 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2543 | return error; |
David Chinner | 4ae29b4 | 2008-03-06 13:43:34 +1100 | [diff] [blame] | 2544 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2545 | imap->im_blkno = XFS_FSB_TO_DADDR(mp, fsbno); |
| 2546 | imap->im_len = XFS_FSB_TO_BB(mp, len); |
| 2547 | imap->im_agblkno = XFS_FSB_TO_AGBNO(mp, fsbno); |
| 2548 | imap->im_ioffset = (ushort)off; |
| 2549 | imap->im_boffset = (ushort)(off << mp->m_sb.sb_inodelog); |
David Chinner | 4ae29b4 | 2008-03-06 13:43:34 +1100 | [diff] [blame] | 2550 | |
| 2551 | /* |
| 2552 | * If the inode number maps to a block outside the bounds |
| 2553 | * of the file system then return NULL rather than calling |
| 2554 | * read_buf and panicing when we get an error from the |
| 2555 | * driver. |
| 2556 | */ |
| 2557 | if ((imap->im_blkno + imap->im_len) > |
| 2558 | XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)) { |
| 2559 | xfs_fs_cmn_err(CE_ALERT, mp, "xfs_imap: " |
| 2560 | "(imap->im_blkno (0x%llx) + imap->im_len (0x%llx)) > " |
| 2561 | " XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks) (0x%llx)", |
| 2562 | (unsigned long long) imap->im_blkno, |
| 2563 | (unsigned long long) imap->im_len, |
| 2564 | XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)); |
| 2565 | return EINVAL; |
| 2566 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2567 | return 0; |
| 2568 | } |
| 2569 | |
| 2570 | void |
| 2571 | xfs_idestroy_fork( |
| 2572 | xfs_inode_t *ip, |
| 2573 | int whichfork) |
| 2574 | { |
| 2575 | xfs_ifork_t *ifp; |
| 2576 | |
| 2577 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 2578 | if (ifp->if_broot != NULL) { |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 2579 | kmem_free(ifp->if_broot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2580 | ifp->if_broot = NULL; |
| 2581 | } |
| 2582 | |
| 2583 | /* |
| 2584 | * If the format is local, then we can't have an extents |
| 2585 | * array so just look for an inline data array. If we're |
| 2586 | * not local then we may or may not have an extents list, |
| 2587 | * so check and free it up if we do. |
| 2588 | */ |
| 2589 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { |
| 2590 | if ((ifp->if_u1.if_data != ifp->if_u2.if_inline_data) && |
| 2591 | (ifp->if_u1.if_data != NULL)) { |
| 2592 | ASSERT(ifp->if_real_bytes != 0); |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 2593 | kmem_free(ifp->if_u1.if_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2594 | ifp->if_u1.if_data = NULL; |
| 2595 | ifp->if_real_bytes = 0; |
| 2596 | } |
| 2597 | } else if ((ifp->if_flags & XFS_IFEXTENTS) && |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 2598 | ((ifp->if_flags & XFS_IFEXTIREC) || |
| 2599 | ((ifp->if_u1.if_extents != NULL) && |
| 2600 | (ifp->if_u1.if_extents != ifp->if_u2.if_inline_ext)))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2601 | ASSERT(ifp->if_real_bytes != 0); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2602 | xfs_iext_destroy(ifp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2603 | } |
| 2604 | ASSERT(ifp->if_u1.if_extents == NULL || |
| 2605 | ifp->if_u1.if_extents == ifp->if_u2.if_inline_ext); |
| 2606 | ASSERT(ifp->if_real_bytes == 0); |
| 2607 | if (whichfork == XFS_ATTR_FORK) { |
| 2608 | kmem_zone_free(xfs_ifork_zone, ip->i_afp); |
| 2609 | ip->i_afp = NULL; |
| 2610 | } |
| 2611 | } |
| 2612 | |
| 2613 | /* |
| 2614 | * This is called free all the memory associated with an inode. |
| 2615 | * It must free the inode itself and any buffers allocated for |
| 2616 | * if_extents/if_data and if_broot. It must also free the lock |
| 2617 | * associated with the inode. |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 2618 | * |
| 2619 | * Note: because we don't initialise everything on reallocation out |
| 2620 | * of the zone, we must ensure we nullify everything correctly before |
| 2621 | * freeing the structure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2622 | */ |
| 2623 | void |
| 2624 | xfs_idestroy( |
| 2625 | xfs_inode_t *ip) |
| 2626 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2627 | switch (ip->i_d.di_mode & S_IFMT) { |
| 2628 | case S_IFREG: |
| 2629 | case S_IFDIR: |
| 2630 | case S_IFLNK: |
| 2631 | xfs_idestroy_fork(ip, XFS_DATA_FORK); |
| 2632 | break; |
| 2633 | } |
| 2634 | if (ip->i_afp) |
| 2635 | xfs_idestroy_fork(ip, XFS_ATTR_FORK); |
Christoph Hellwig | 1543d79 | 2007-08-29 11:46:47 +1000 | [diff] [blame] | 2636 | |
Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 2637 | #ifdef XFS_INODE_TRACE |
Christoph Hellwig | 1543d79 | 2007-08-29 11:46:47 +1000 | [diff] [blame] | 2638 | ktrace_free(ip->i_trace); |
| 2639 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2640 | #ifdef XFS_BMAP_TRACE |
| 2641 | ktrace_free(ip->i_xtrace); |
| 2642 | #endif |
Christoph Hellwig | 8c4ed63 | 2008-10-30 16:55:13 +1100 | [diff] [blame] | 2643 | #ifdef XFS_BTREE_TRACE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2644 | ktrace_free(ip->i_btrace); |
| 2645 | #endif |
| 2646 | #ifdef XFS_RW_TRACE |
| 2647 | ktrace_free(ip->i_rwtrace); |
| 2648 | #endif |
| 2649 | #ifdef XFS_ILOCK_TRACE |
| 2650 | ktrace_free(ip->i_lock_trace); |
| 2651 | #endif |
| 2652 | #ifdef XFS_DIR2_TRACE |
| 2653 | ktrace_free(ip->i_dir_trace); |
| 2654 | #endif |
| 2655 | if (ip->i_itemp) { |
David Chinner | f74eaf5 | 2007-02-10 18:36:04 +1100 | [diff] [blame] | 2656 | /* |
| 2657 | * Only if we are shutting down the fs will we see an |
| 2658 | * inode still in the AIL. If it is there, we should remove |
| 2659 | * it to prevent a use-after-free from occurring. |
| 2660 | */ |
David Chinner | f74eaf5 | 2007-02-10 18:36:04 +1100 | [diff] [blame] | 2661 | xfs_log_item_t *lip = &ip->i_itemp->ili_item; |
David Chinner | 783a2f6 | 2008-10-30 17:39:58 +1100 | [diff] [blame] | 2662 | struct xfs_ail *ailp = lip->li_ailp; |
David Chinner | f74eaf5 | 2007-02-10 18:36:04 +1100 | [diff] [blame] | 2663 | |
| 2664 | ASSERT(((lip->li_flags & XFS_LI_IN_AIL) == 0) || |
| 2665 | XFS_FORCED_SHUTDOWN(ip->i_mount)); |
| 2666 | if (lip->li_flags & XFS_LI_IN_AIL) { |
David Chinner | 783a2f6 | 2008-10-30 17:39:58 +1100 | [diff] [blame] | 2667 | spin_lock(&ailp->xa_lock); |
David Chinner | f74eaf5 | 2007-02-10 18:36:04 +1100 | [diff] [blame] | 2668 | if (lip->li_flags & XFS_LI_IN_AIL) |
David Chinner | 783a2f6 | 2008-10-30 17:39:58 +1100 | [diff] [blame] | 2669 | xfs_trans_ail_delete(ailp, lip); |
David Chinner | f74eaf5 | 2007-02-10 18:36:04 +1100 | [diff] [blame] | 2670 | else |
David Chinner | 783a2f6 | 2008-10-30 17:39:58 +1100 | [diff] [blame] | 2671 | spin_unlock(&ailp->xa_lock); |
David Chinner | f74eaf5 | 2007-02-10 18:36:04 +1100 | [diff] [blame] | 2672 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2673 | xfs_inode_item_destroy(ip); |
David Chinner | 07c8f67 | 2008-10-30 16:11:59 +1100 | [diff] [blame] | 2674 | ip->i_itemp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2675 | } |
David Chinner | 07c8f67 | 2008-10-30 16:11:59 +1100 | [diff] [blame] | 2676 | /* asserts to verify all state is correct here */ |
| 2677 | ASSERT(atomic_read(&ip->i_iocount) == 0); |
| 2678 | ASSERT(atomic_read(&ip->i_pincount) == 0); |
| 2679 | ASSERT(!spin_is_locked(&ip->i_flags_lock)); |
David Chinner | 1165451 | 2008-10-30 17:37:49 +1100 | [diff] [blame] | 2680 | ASSERT(completion_done(&ip->i_flush)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2681 | kmem_zone_free(xfs_inode_zone, ip); |
| 2682 | } |
| 2683 | |
| 2684 | |
| 2685 | /* |
| 2686 | * Increment the pin count of the given buffer. |
| 2687 | * This value is protected by ipinlock spinlock in the mount structure. |
| 2688 | */ |
| 2689 | void |
| 2690 | xfs_ipin( |
| 2691 | xfs_inode_t *ip) |
| 2692 | { |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 2693 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2694 | |
| 2695 | atomic_inc(&ip->i_pincount); |
| 2696 | } |
| 2697 | |
| 2698 | /* |
| 2699 | * Decrement the pin count of the given inode, and wake up |
| 2700 | * anyone in xfs_iwait_unpin() if the count goes to 0. The |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 2701 | * inode must have been previously pinned with a call to xfs_ipin(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2702 | */ |
| 2703 | void |
| 2704 | xfs_iunpin( |
| 2705 | xfs_inode_t *ip) |
| 2706 | { |
| 2707 | ASSERT(atomic_read(&ip->i_pincount) > 0); |
| 2708 | |
David Chinner | 5d51eff | 2007-11-23 16:29:18 +1100 | [diff] [blame] | 2709 | if (atomic_dec_and_test(&ip->i_pincount)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2710 | wake_up(&ip->i_ipin_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2711 | } |
| 2712 | |
| 2713 | /* |
David Chinner | a3f74ff | 2008-03-06 13:43:42 +1100 | [diff] [blame] | 2714 | * This is called to unpin an inode. It can be directed to wait or to return |
| 2715 | * immediately without waiting for the inode to be unpinned. The caller must |
| 2716 | * have the inode locked in at least shared mode so that the buffer cannot be |
| 2717 | * subsequently pinned once someone is waiting for it to be unpinned. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2718 | */ |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 2719 | STATIC void |
David Chinner | a3f74ff | 2008-03-06 13:43:42 +1100 | [diff] [blame] | 2720 | __xfs_iunpin_wait( |
| 2721 | xfs_inode_t *ip, |
| 2722 | int wait) |
| 2723 | { |
| 2724 | xfs_inode_log_item_t *iip = ip->i_itemp; |
| 2725 | |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 2726 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)); |
David Chinner | a3f74ff | 2008-03-06 13:43:42 +1100 | [diff] [blame] | 2727 | if (atomic_read(&ip->i_pincount) == 0) |
| 2728 | return; |
| 2729 | |
| 2730 | /* Give the log a push to start the unpinning I/O */ |
| 2731 | xfs_log_force(ip->i_mount, (iip && iip->ili_last_lsn) ? |
| 2732 | iip->ili_last_lsn : 0, XFS_LOG_FORCE); |
| 2733 | if (wait) |
| 2734 | wait_event(ip->i_ipin_wait, (atomic_read(&ip->i_pincount) == 0)); |
| 2735 | } |
| 2736 | |
| 2737 | static inline void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2738 | xfs_iunpin_wait( |
| 2739 | xfs_inode_t *ip) |
| 2740 | { |
David Chinner | a3f74ff | 2008-03-06 13:43:42 +1100 | [diff] [blame] | 2741 | __xfs_iunpin_wait(ip, 1); |
| 2742 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2743 | |
David Chinner | a3f74ff | 2008-03-06 13:43:42 +1100 | [diff] [blame] | 2744 | static inline void |
| 2745 | xfs_iunpin_nowait( |
| 2746 | xfs_inode_t *ip) |
| 2747 | { |
| 2748 | __xfs_iunpin_wait(ip, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2749 | } |
| 2750 | |
| 2751 | |
| 2752 | /* |
| 2753 | * xfs_iextents_copy() |
| 2754 | * |
| 2755 | * This is called to copy the REAL extents (as opposed to the delayed |
| 2756 | * allocation extents) from the inode into the given buffer. It |
| 2757 | * returns the number of bytes copied into the buffer. |
| 2758 | * |
| 2759 | * If there are no delayed allocation extents, then we can just |
| 2760 | * memcpy() the extents into the buffer. Otherwise, we need to |
| 2761 | * examine each extent in turn and skip those which are delayed. |
| 2762 | */ |
| 2763 | int |
| 2764 | xfs_iextents_copy( |
| 2765 | xfs_inode_t *ip, |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 2766 | xfs_bmbt_rec_t *dp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2767 | int whichfork) |
| 2768 | { |
| 2769 | int copied; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2770 | int i; |
| 2771 | xfs_ifork_t *ifp; |
| 2772 | int nrecs; |
| 2773 | xfs_fsblock_t start_block; |
| 2774 | |
| 2775 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 2776 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2777 | ASSERT(ifp->if_bytes > 0); |
| 2778 | |
| 2779 | nrecs = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 2780 | XFS_BMAP_TRACE_EXLIST(ip, nrecs, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2781 | ASSERT(nrecs > 0); |
| 2782 | |
| 2783 | /* |
| 2784 | * There are some delayed allocation extents in the |
| 2785 | * inode, so copy the extents one at a time and skip |
| 2786 | * the delayed ones. There must be at least one |
| 2787 | * non-delayed extent. |
| 2788 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2789 | copied = 0; |
| 2790 | for (i = 0; i < nrecs; i++) { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 2791 | xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2792 | start_block = xfs_bmbt_get_startblock(ep); |
| 2793 | if (ISNULLSTARTBLOCK(start_block)) { |
| 2794 | /* |
| 2795 | * It's a delayed allocation extent, so skip it. |
| 2796 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2797 | continue; |
| 2798 | } |
| 2799 | |
| 2800 | /* Translate to on disk format */ |
Christoph Hellwig | cd8b0a9 | 2007-08-16 16:24:15 +1000 | [diff] [blame] | 2801 | put_unaligned(cpu_to_be64(ep->l0), &dp->l0); |
| 2802 | put_unaligned(cpu_to_be64(ep->l1), &dp->l1); |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 2803 | dp++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2804 | copied++; |
| 2805 | } |
| 2806 | ASSERT(copied != 0); |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 2807 | xfs_validate_extents(ifp, copied, XFS_EXTFMT_INODE(ip)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2808 | |
| 2809 | return (copied * (uint)sizeof(xfs_bmbt_rec_t)); |
| 2810 | } |
| 2811 | |
| 2812 | /* |
| 2813 | * Each of the following cases stores data into the same region |
| 2814 | * of the on-disk inode, so only one of them can be valid at |
| 2815 | * any given time. While it is possible to have conflicting formats |
| 2816 | * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is |
| 2817 | * in EXTENTS format, this can only happen when the fork has |
| 2818 | * changed formats after being modified but before being flushed. |
| 2819 | * In these cases, the format always takes precedence, because the |
| 2820 | * format indicates the current state of the fork. |
| 2821 | */ |
| 2822 | /*ARGSUSED*/ |
David Chinner | e4ac967 | 2008-04-10 12:23:58 +1000 | [diff] [blame] | 2823 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2824 | xfs_iflush_fork( |
| 2825 | xfs_inode_t *ip, |
| 2826 | xfs_dinode_t *dip, |
| 2827 | xfs_inode_log_item_t *iip, |
| 2828 | int whichfork, |
| 2829 | xfs_buf_t *bp) |
| 2830 | { |
| 2831 | char *cp; |
| 2832 | xfs_ifork_t *ifp; |
| 2833 | xfs_mount_t *mp; |
| 2834 | #ifdef XFS_TRANS_DEBUG |
| 2835 | int first; |
| 2836 | #endif |
| 2837 | static const short brootflag[2] = |
| 2838 | { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT }; |
| 2839 | static const short dataflag[2] = |
| 2840 | { XFS_ILOG_DDATA, XFS_ILOG_ADATA }; |
| 2841 | static const short extflag[2] = |
| 2842 | { XFS_ILOG_DEXT, XFS_ILOG_AEXT }; |
| 2843 | |
David Chinner | e4ac967 | 2008-04-10 12:23:58 +1000 | [diff] [blame] | 2844 | if (!iip) |
| 2845 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2846 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 2847 | /* |
| 2848 | * This can happen if we gave up in iformat in an error path, |
| 2849 | * for the attribute fork. |
| 2850 | */ |
David Chinner | e4ac967 | 2008-04-10 12:23:58 +1000 | [diff] [blame] | 2851 | if (!ifp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2852 | ASSERT(whichfork == XFS_ATTR_FORK); |
David Chinner | e4ac967 | 2008-04-10 12:23:58 +1000 | [diff] [blame] | 2853 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2854 | } |
| 2855 | cp = XFS_DFORK_PTR(dip, whichfork); |
| 2856 | mp = ip->i_mount; |
| 2857 | switch (XFS_IFORK_FORMAT(ip, whichfork)) { |
| 2858 | case XFS_DINODE_FMT_LOCAL: |
| 2859 | if ((iip->ili_format.ilf_fields & dataflag[whichfork]) && |
| 2860 | (ifp->if_bytes > 0)) { |
| 2861 | ASSERT(ifp->if_u1.if_data != NULL); |
| 2862 | ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork)); |
| 2863 | memcpy(cp, ifp->if_u1.if_data, ifp->if_bytes); |
| 2864 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2865 | break; |
| 2866 | |
| 2867 | case XFS_DINODE_FMT_EXTENTS: |
| 2868 | ASSERT((ifp->if_flags & XFS_IFEXTENTS) || |
| 2869 | !(iip->ili_format.ilf_fields & extflag[whichfork])); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2870 | ASSERT((xfs_iext_get_ext(ifp, 0) != NULL) || |
| 2871 | (ifp->if_bytes == 0)); |
| 2872 | ASSERT((xfs_iext_get_ext(ifp, 0) == NULL) || |
| 2873 | (ifp->if_bytes > 0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2874 | if ((iip->ili_format.ilf_fields & extflag[whichfork]) && |
| 2875 | (ifp->if_bytes > 0)) { |
| 2876 | ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) > 0); |
| 2877 | (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp, |
| 2878 | whichfork); |
| 2879 | } |
| 2880 | break; |
| 2881 | |
| 2882 | case XFS_DINODE_FMT_BTREE: |
| 2883 | if ((iip->ili_format.ilf_fields & brootflag[whichfork]) && |
| 2884 | (ifp->if_broot_bytes > 0)) { |
| 2885 | ASSERT(ifp->if_broot != NULL); |
| 2886 | ASSERT(ifp->if_broot_bytes <= |
| 2887 | (XFS_IFORK_SIZE(ip, whichfork) + |
| 2888 | XFS_BROOT_SIZE_ADJ)); |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 2889 | xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2890 | (xfs_bmdr_block_t *)cp, |
| 2891 | XFS_DFORK_SIZE(dip, mp, whichfork)); |
| 2892 | } |
| 2893 | break; |
| 2894 | |
| 2895 | case XFS_DINODE_FMT_DEV: |
| 2896 | if (iip->ili_format.ilf_fields & XFS_ILOG_DEV) { |
| 2897 | ASSERT(whichfork == XFS_DATA_FORK); |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 2898 | xfs_dinode_put_rdev(dip, ip->i_df.if_u2.if_rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2899 | } |
| 2900 | break; |
| 2901 | |
| 2902 | case XFS_DINODE_FMT_UUID: |
| 2903 | if (iip->ili_format.ilf_fields & XFS_ILOG_UUID) { |
| 2904 | ASSERT(whichfork == XFS_DATA_FORK); |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 2905 | memcpy(XFS_DFORK_DPTR(dip), |
| 2906 | &ip->i_df.if_u2.if_uuid, |
| 2907 | sizeof(uuid_t)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2908 | } |
| 2909 | break; |
| 2910 | |
| 2911 | default: |
| 2912 | ASSERT(0); |
| 2913 | break; |
| 2914 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2915 | } |
| 2916 | |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 2917 | STATIC int |
| 2918 | xfs_iflush_cluster( |
| 2919 | xfs_inode_t *ip, |
| 2920 | xfs_buf_t *bp) |
| 2921 | { |
| 2922 | xfs_mount_t *mp = ip->i_mount; |
| 2923 | xfs_perag_t *pag = xfs_get_perag(mp, ip->i_ino); |
| 2924 | unsigned long first_index, mask; |
David Chinner | c8f5f12 | 2008-05-20 11:30:15 +1000 | [diff] [blame] | 2925 | unsigned long inodes_per_cluster; |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 2926 | int ilist_size; |
| 2927 | xfs_inode_t **ilist; |
| 2928 | xfs_inode_t *iq; |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 2929 | int nr_found; |
| 2930 | int clcount = 0; |
| 2931 | int bufwasdelwri; |
| 2932 | int i; |
| 2933 | |
| 2934 | ASSERT(pag->pagi_inodeok); |
| 2935 | ASSERT(pag->pag_ici_init); |
| 2936 | |
David Chinner | c8f5f12 | 2008-05-20 11:30:15 +1000 | [diff] [blame] | 2937 | inodes_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog; |
| 2938 | ilist_size = inodes_per_cluster * sizeof(xfs_inode_t *); |
David Chinner | 49383b0 | 2008-05-19 16:29:34 +1000 | [diff] [blame] | 2939 | ilist = kmem_alloc(ilist_size, KM_MAYFAIL|KM_NOFS); |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 2940 | if (!ilist) |
| 2941 | return 0; |
| 2942 | |
| 2943 | mask = ~(((XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog)) - 1); |
| 2944 | first_index = XFS_INO_TO_AGINO(mp, ip->i_ino) & mask; |
| 2945 | read_lock(&pag->pag_ici_lock); |
| 2946 | /* really need a gang lookup range call here */ |
| 2947 | nr_found = radix_tree_gang_lookup(&pag->pag_ici_root, (void**)ilist, |
David Chinner | c8f5f12 | 2008-05-20 11:30:15 +1000 | [diff] [blame] | 2948 | first_index, inodes_per_cluster); |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 2949 | if (nr_found == 0) |
| 2950 | goto out_free; |
| 2951 | |
| 2952 | for (i = 0; i < nr_found; i++) { |
| 2953 | iq = ilist[i]; |
| 2954 | if (iq == ip) |
| 2955 | continue; |
| 2956 | /* if the inode lies outside this cluster, we're done. */ |
| 2957 | if ((XFS_INO_TO_AGINO(mp, iq->i_ino) & mask) != first_index) |
| 2958 | break; |
| 2959 | /* |
| 2960 | * Do an un-protected check to see if the inode is dirty and |
| 2961 | * is a candidate for flushing. These checks will be repeated |
| 2962 | * later after the appropriate locks are acquired. |
| 2963 | */ |
David Chinner | 3354040 | 2008-03-06 13:43:59 +1100 | [diff] [blame] | 2964 | if (xfs_inode_clean(iq) && xfs_ipincount(iq) == 0) |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 2965 | continue; |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 2966 | |
| 2967 | /* |
| 2968 | * Try to get locks. If any are unavailable or it is pinned, |
| 2969 | * then this inode cannot be flushed and is skipped. |
| 2970 | */ |
| 2971 | |
| 2972 | if (!xfs_ilock_nowait(iq, XFS_ILOCK_SHARED)) |
| 2973 | continue; |
| 2974 | if (!xfs_iflock_nowait(iq)) { |
| 2975 | xfs_iunlock(iq, XFS_ILOCK_SHARED); |
| 2976 | continue; |
| 2977 | } |
| 2978 | if (xfs_ipincount(iq)) { |
| 2979 | xfs_ifunlock(iq); |
| 2980 | xfs_iunlock(iq, XFS_ILOCK_SHARED); |
| 2981 | continue; |
| 2982 | } |
| 2983 | |
| 2984 | /* |
| 2985 | * arriving here means that this inode can be flushed. First |
| 2986 | * re-check that it's dirty before flushing. |
| 2987 | */ |
David Chinner | 3354040 | 2008-03-06 13:43:59 +1100 | [diff] [blame] | 2988 | if (!xfs_inode_clean(iq)) { |
| 2989 | int error; |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 2990 | error = xfs_iflush_int(iq, bp); |
| 2991 | if (error) { |
| 2992 | xfs_iunlock(iq, XFS_ILOCK_SHARED); |
| 2993 | goto cluster_corrupt_out; |
| 2994 | } |
| 2995 | clcount++; |
| 2996 | } else { |
| 2997 | xfs_ifunlock(iq); |
| 2998 | } |
| 2999 | xfs_iunlock(iq, XFS_ILOCK_SHARED); |
| 3000 | } |
| 3001 | |
| 3002 | if (clcount) { |
| 3003 | XFS_STATS_INC(xs_icluster_flushcnt); |
| 3004 | XFS_STATS_ADD(xs_icluster_flushinode, clcount); |
| 3005 | } |
| 3006 | |
| 3007 | out_free: |
| 3008 | read_unlock(&pag->pag_ici_lock); |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 3009 | kmem_free(ilist); |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 3010 | return 0; |
| 3011 | |
| 3012 | |
| 3013 | cluster_corrupt_out: |
| 3014 | /* |
| 3015 | * Corruption detected in the clustering loop. Invalidate the |
| 3016 | * inode buffer and shut down the filesystem. |
| 3017 | */ |
| 3018 | read_unlock(&pag->pag_ici_lock); |
| 3019 | /* |
| 3020 | * Clean up the buffer. If it was B_DELWRI, just release it -- |
| 3021 | * brelse can handle it with no problems. If not, shut down the |
| 3022 | * filesystem before releasing the buffer. |
| 3023 | */ |
| 3024 | bufwasdelwri = XFS_BUF_ISDELAYWRITE(bp); |
| 3025 | if (bufwasdelwri) |
| 3026 | xfs_buf_relse(bp); |
| 3027 | |
| 3028 | xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); |
| 3029 | |
| 3030 | if (!bufwasdelwri) { |
| 3031 | /* |
| 3032 | * Just like incore_relse: if we have b_iodone functions, |
| 3033 | * mark the buffer as an error and call them. Otherwise |
| 3034 | * mark it as stale and brelse. |
| 3035 | */ |
| 3036 | if (XFS_BUF_IODONE_FUNC(bp)) { |
| 3037 | XFS_BUF_CLR_BDSTRAT_FUNC(bp); |
| 3038 | XFS_BUF_UNDONE(bp); |
| 3039 | XFS_BUF_STALE(bp); |
| 3040 | XFS_BUF_SHUT(bp); |
| 3041 | XFS_BUF_ERROR(bp,EIO); |
| 3042 | xfs_biodone(bp); |
| 3043 | } else { |
| 3044 | XFS_BUF_STALE(bp); |
| 3045 | xfs_buf_relse(bp); |
| 3046 | } |
| 3047 | } |
| 3048 | |
| 3049 | /* |
| 3050 | * Unlocks the flush lock |
| 3051 | */ |
| 3052 | xfs_iflush_abort(iq); |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 3053 | kmem_free(ilist); |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 3054 | return XFS_ERROR(EFSCORRUPTED); |
| 3055 | } |
| 3056 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3057 | /* |
| 3058 | * xfs_iflush() will write a modified inode's changes out to the |
| 3059 | * inode's on disk home. The caller must have the inode lock held |
David Chinner | c63942d | 2008-08-13 16:41:16 +1000 | [diff] [blame] | 3060 | * in at least shared mode and the inode flush completion must be |
| 3061 | * active as well. The inode lock will still be held upon return from |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3062 | * the call and the caller is free to unlock it. |
David Chinner | c63942d | 2008-08-13 16:41:16 +1000 | [diff] [blame] | 3063 | * The inode flush will be completed when the inode reaches the disk. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3064 | * The flags indicate how the inode's buffer should be written out. |
| 3065 | */ |
| 3066 | int |
| 3067 | xfs_iflush( |
| 3068 | xfs_inode_t *ip, |
| 3069 | uint flags) |
| 3070 | { |
| 3071 | xfs_inode_log_item_t *iip; |
| 3072 | xfs_buf_t *bp; |
| 3073 | xfs_dinode_t *dip; |
| 3074 | xfs_mount_t *mp; |
| 3075 | int error; |
David Chinner | a3f74ff | 2008-03-06 13:43:42 +1100 | [diff] [blame] | 3076 | int noblock = (flags == XFS_IFLUSH_ASYNC_NOBLOCK); |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 3077 | enum { INT_DELWRI = (1 << 0), INT_ASYNC = (1 << 1) }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3078 | |
| 3079 | XFS_STATS_INC(xs_iflush_count); |
| 3080 | |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 3081 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)); |
David Chinner | c63942d | 2008-08-13 16:41:16 +1000 | [diff] [blame] | 3082 | ASSERT(!completion_done(&ip->i_flush)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3083 | ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE || |
| 3084 | ip->i_d.di_nextents > ip->i_df.if_ext_max); |
| 3085 | |
| 3086 | iip = ip->i_itemp; |
| 3087 | mp = ip->i_mount; |
| 3088 | |
| 3089 | /* |
| 3090 | * If the inode isn't dirty, then just release the inode |
| 3091 | * flush lock and do nothing. |
| 3092 | */ |
David Chinner | 3354040 | 2008-03-06 13:43:59 +1100 | [diff] [blame] | 3093 | if (xfs_inode_clean(ip)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3094 | xfs_ifunlock(ip); |
| 3095 | return 0; |
| 3096 | } |
| 3097 | |
| 3098 | /* |
David Chinner | a3f74ff | 2008-03-06 13:43:42 +1100 | [diff] [blame] | 3099 | * We can't flush the inode until it is unpinned, so wait for it if we |
| 3100 | * are allowed to block. We know noone new can pin it, because we are |
| 3101 | * holding the inode lock shared and you need to hold it exclusively to |
| 3102 | * pin the inode. |
| 3103 | * |
| 3104 | * If we are not allowed to block, force the log out asynchronously so |
| 3105 | * that when we come back the inode will be unpinned. If other inodes |
| 3106 | * in the same cluster are dirty, they will probably write the inode |
| 3107 | * out for us if they occur after the log force completes. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3108 | */ |
David Chinner | a3f74ff | 2008-03-06 13:43:42 +1100 | [diff] [blame] | 3109 | if (noblock && xfs_ipincount(ip)) { |
| 3110 | xfs_iunpin_nowait(ip); |
| 3111 | xfs_ifunlock(ip); |
| 3112 | return EAGAIN; |
| 3113 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3114 | xfs_iunpin_wait(ip); |
| 3115 | |
| 3116 | /* |
| 3117 | * This may have been unpinned because the filesystem is shutting |
| 3118 | * down forcibly. If that's the case we must not write this inode |
| 3119 | * to disk, because the log record didn't make it to disk! |
| 3120 | */ |
| 3121 | if (XFS_FORCED_SHUTDOWN(mp)) { |
| 3122 | ip->i_update_core = 0; |
| 3123 | if (iip) |
| 3124 | iip->ili_format.ilf_fields = 0; |
| 3125 | xfs_ifunlock(ip); |
| 3126 | return XFS_ERROR(EIO); |
| 3127 | } |
| 3128 | |
| 3129 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3130 | * Decide how buffer will be flushed out. This is done before |
| 3131 | * the call to xfs_iflush_int because this field is zeroed by it. |
| 3132 | */ |
| 3133 | if (iip != NULL && iip->ili_format.ilf_fields != 0) { |
| 3134 | /* |
| 3135 | * Flush out the inode buffer according to the directions |
| 3136 | * of the caller. In the cases where the caller has given |
| 3137 | * us a choice choose the non-delwri case. This is because |
| 3138 | * the inode is in the AIL and we need to get it out soon. |
| 3139 | */ |
| 3140 | switch (flags) { |
| 3141 | case XFS_IFLUSH_SYNC: |
| 3142 | case XFS_IFLUSH_DELWRI_ELSE_SYNC: |
| 3143 | flags = 0; |
| 3144 | break; |
David Chinner | a3f74ff | 2008-03-06 13:43:42 +1100 | [diff] [blame] | 3145 | case XFS_IFLUSH_ASYNC_NOBLOCK: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3146 | case XFS_IFLUSH_ASYNC: |
| 3147 | case XFS_IFLUSH_DELWRI_ELSE_ASYNC: |
| 3148 | flags = INT_ASYNC; |
| 3149 | break; |
| 3150 | case XFS_IFLUSH_DELWRI: |
| 3151 | flags = INT_DELWRI; |
| 3152 | break; |
| 3153 | default: |
| 3154 | ASSERT(0); |
| 3155 | flags = 0; |
| 3156 | break; |
| 3157 | } |
| 3158 | } else { |
| 3159 | switch (flags) { |
| 3160 | case XFS_IFLUSH_DELWRI_ELSE_SYNC: |
| 3161 | case XFS_IFLUSH_DELWRI_ELSE_ASYNC: |
| 3162 | case XFS_IFLUSH_DELWRI: |
| 3163 | flags = INT_DELWRI; |
| 3164 | break; |
David Chinner | a3f74ff | 2008-03-06 13:43:42 +1100 | [diff] [blame] | 3165 | case XFS_IFLUSH_ASYNC_NOBLOCK: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3166 | case XFS_IFLUSH_ASYNC: |
| 3167 | flags = INT_ASYNC; |
| 3168 | break; |
| 3169 | case XFS_IFLUSH_SYNC: |
| 3170 | flags = 0; |
| 3171 | break; |
| 3172 | default: |
| 3173 | ASSERT(0); |
| 3174 | flags = 0; |
| 3175 | break; |
| 3176 | } |
| 3177 | } |
| 3178 | |
| 3179 | /* |
David Chinner | a3f74ff | 2008-03-06 13:43:42 +1100 | [diff] [blame] | 3180 | * Get the buffer containing the on-disk inode. |
| 3181 | */ |
Christoph Hellwig | 76d8b27 | 2008-11-28 14:23:40 +1100 | [diff] [blame^] | 3182 | error = xfs_itobp(mp, NULL, ip, &dip, &bp, |
David Chinner | a3f74ff | 2008-03-06 13:43:42 +1100 | [diff] [blame] | 3183 | noblock ? XFS_BUF_TRYLOCK : XFS_BUF_LOCK); |
| 3184 | if (error || !bp) { |
| 3185 | xfs_ifunlock(ip); |
| 3186 | return error; |
| 3187 | } |
| 3188 | |
| 3189 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3190 | * First flush out the inode that xfs_iflush was called with. |
| 3191 | */ |
| 3192 | error = xfs_iflush_int(ip, bp); |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 3193 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3194 | goto corrupt_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3195 | |
| 3196 | /* |
David Chinner | a3f74ff | 2008-03-06 13:43:42 +1100 | [diff] [blame] | 3197 | * If the buffer is pinned then push on the log now so we won't |
| 3198 | * get stuck waiting in the write for too long. |
| 3199 | */ |
| 3200 | if (XFS_BUF_ISPINNED(bp)) |
| 3201 | xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE); |
| 3202 | |
| 3203 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3204 | * inode clustering: |
| 3205 | * see if other inodes can be gathered into this write |
| 3206 | */ |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 3207 | error = xfs_iflush_cluster(ip, bp); |
| 3208 | if (error) |
| 3209 | goto cluster_corrupt_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3210 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3211 | if (flags & INT_DELWRI) { |
| 3212 | xfs_bdwrite(mp, bp); |
| 3213 | } else if (flags & INT_ASYNC) { |
David Chinner | db7a19f | 2008-04-10 12:22:24 +1000 | [diff] [blame] | 3214 | error = xfs_bawrite(mp, bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3215 | } else { |
| 3216 | error = xfs_bwrite(mp, bp); |
| 3217 | } |
| 3218 | return error; |
| 3219 | |
| 3220 | corrupt_out: |
| 3221 | xfs_buf_relse(bp); |
Nathan Scott | 7d04a33 | 2006-06-09 14:58:38 +1000 | [diff] [blame] | 3222 | xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3223 | cluster_corrupt_out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3224 | /* |
| 3225 | * Unlocks the flush lock |
| 3226 | */ |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 3227 | xfs_iflush_abort(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3228 | return XFS_ERROR(EFSCORRUPTED); |
| 3229 | } |
| 3230 | |
| 3231 | |
| 3232 | STATIC int |
| 3233 | xfs_iflush_int( |
| 3234 | xfs_inode_t *ip, |
| 3235 | xfs_buf_t *bp) |
| 3236 | { |
| 3237 | xfs_inode_log_item_t *iip; |
| 3238 | xfs_dinode_t *dip; |
| 3239 | xfs_mount_t *mp; |
| 3240 | #ifdef XFS_TRANS_DEBUG |
| 3241 | int first; |
| 3242 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3243 | |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 3244 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)); |
David Chinner | c63942d | 2008-08-13 16:41:16 +1000 | [diff] [blame] | 3245 | ASSERT(!completion_done(&ip->i_flush)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3246 | ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE || |
| 3247 | ip->i_d.di_nextents > ip->i_df.if_ext_max); |
| 3248 | |
| 3249 | iip = ip->i_itemp; |
| 3250 | mp = ip->i_mount; |
| 3251 | |
| 3252 | |
| 3253 | /* |
| 3254 | * If the inode isn't dirty, then just release the inode |
| 3255 | * flush lock and do nothing. |
| 3256 | */ |
David Chinner | 3354040 | 2008-03-06 13:43:59 +1100 | [diff] [blame] | 3257 | if (xfs_inode_clean(ip)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3258 | xfs_ifunlock(ip); |
| 3259 | return 0; |
| 3260 | } |
| 3261 | |
| 3262 | /* set *dip = inode's place in the buffer */ |
| 3263 | dip = (xfs_dinode_t *)xfs_buf_offset(bp, ip->i_boffset); |
| 3264 | |
| 3265 | /* |
| 3266 | * Clear i_update_core before copying out the data. |
| 3267 | * This is for coordination with our timestamp updates |
| 3268 | * that don't hold the inode lock. They will always |
| 3269 | * update the timestamps BEFORE setting i_update_core, |
| 3270 | * so if we clear i_update_core after they set it we |
| 3271 | * are guaranteed to see their updates to the timestamps. |
| 3272 | * I believe that this depends on strongly ordered memory |
| 3273 | * semantics, but we have that. We use the SYNCHRONIZE |
| 3274 | * macro to make sure that the compiler does not reorder |
| 3275 | * the i_update_core access below the data copy below. |
| 3276 | */ |
| 3277 | ip->i_update_core = 0; |
| 3278 | SYNCHRONIZE(); |
| 3279 | |
Christoph Hellwig | 42fe2b1 | 2006-01-11 15:35:17 +1100 | [diff] [blame] | 3280 | /* |
| 3281 | * Make sure to get the latest atime from the Linux inode. |
| 3282 | */ |
| 3283 | xfs_synchronize_atime(ip); |
| 3284 | |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 3285 | if (XFS_TEST_ERROR(be16_to_cpu(dip->di_magic) != XFS_DINODE_MAGIC, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3286 | mp, XFS_ERRTAG_IFLUSH_1, XFS_RANDOM_IFLUSH_1)) { |
| 3287 | xfs_cmn_err(XFS_PTAG_IFLUSH, CE_ALERT, mp, |
| 3288 | "xfs_iflush: Bad inode %Lu magic number 0x%x, ptr 0x%p", |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 3289 | ip->i_ino, be16_to_cpu(dip->di_magic), dip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3290 | goto corrupt_out; |
| 3291 | } |
| 3292 | if (XFS_TEST_ERROR(ip->i_d.di_magic != XFS_DINODE_MAGIC, |
| 3293 | mp, XFS_ERRTAG_IFLUSH_2, XFS_RANDOM_IFLUSH_2)) { |
| 3294 | xfs_cmn_err(XFS_PTAG_IFLUSH, CE_ALERT, mp, |
| 3295 | "xfs_iflush: Bad inode %Lu, ptr 0x%p, magic number 0x%x", |
| 3296 | ip->i_ino, ip, ip->i_d.di_magic); |
| 3297 | goto corrupt_out; |
| 3298 | } |
| 3299 | if ((ip->i_d.di_mode & S_IFMT) == S_IFREG) { |
| 3300 | if (XFS_TEST_ERROR( |
| 3301 | (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) && |
| 3302 | (ip->i_d.di_format != XFS_DINODE_FMT_BTREE), |
| 3303 | mp, XFS_ERRTAG_IFLUSH_3, XFS_RANDOM_IFLUSH_3)) { |
| 3304 | xfs_cmn_err(XFS_PTAG_IFLUSH, CE_ALERT, mp, |
| 3305 | "xfs_iflush: Bad regular inode %Lu, ptr 0x%p", |
| 3306 | ip->i_ino, ip); |
| 3307 | goto corrupt_out; |
| 3308 | } |
| 3309 | } else if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) { |
| 3310 | if (XFS_TEST_ERROR( |
| 3311 | (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) && |
| 3312 | (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) && |
| 3313 | (ip->i_d.di_format != XFS_DINODE_FMT_LOCAL), |
| 3314 | mp, XFS_ERRTAG_IFLUSH_4, XFS_RANDOM_IFLUSH_4)) { |
| 3315 | xfs_cmn_err(XFS_PTAG_IFLUSH, CE_ALERT, mp, |
| 3316 | "xfs_iflush: Bad directory inode %Lu, ptr 0x%p", |
| 3317 | ip->i_ino, ip); |
| 3318 | goto corrupt_out; |
| 3319 | } |
| 3320 | } |
| 3321 | if (XFS_TEST_ERROR(ip->i_d.di_nextents + ip->i_d.di_anextents > |
| 3322 | ip->i_d.di_nblocks, mp, XFS_ERRTAG_IFLUSH_5, |
| 3323 | XFS_RANDOM_IFLUSH_5)) { |
| 3324 | xfs_cmn_err(XFS_PTAG_IFLUSH, CE_ALERT, mp, |
| 3325 | "xfs_iflush: detected corrupt incore inode %Lu, total extents = %d, nblocks = %Ld, ptr 0x%p", |
| 3326 | ip->i_ino, |
| 3327 | ip->i_d.di_nextents + ip->i_d.di_anextents, |
| 3328 | ip->i_d.di_nblocks, |
| 3329 | ip); |
| 3330 | goto corrupt_out; |
| 3331 | } |
| 3332 | if (XFS_TEST_ERROR(ip->i_d.di_forkoff > mp->m_sb.sb_inodesize, |
| 3333 | mp, XFS_ERRTAG_IFLUSH_6, XFS_RANDOM_IFLUSH_6)) { |
| 3334 | xfs_cmn_err(XFS_PTAG_IFLUSH, CE_ALERT, mp, |
| 3335 | "xfs_iflush: bad inode %Lu, forkoff 0x%x, ptr 0x%p", |
| 3336 | ip->i_ino, ip->i_d.di_forkoff, ip); |
| 3337 | goto corrupt_out; |
| 3338 | } |
| 3339 | /* |
| 3340 | * bump the flush iteration count, used to detect flushes which |
| 3341 | * postdate a log record during recovery. |
| 3342 | */ |
| 3343 | |
| 3344 | ip->i_d.di_flushiter++; |
| 3345 | |
| 3346 | /* |
| 3347 | * Copy the dirty parts of the inode into the on-disk |
| 3348 | * inode. We always copy out the core of the inode, |
| 3349 | * because if the inode is dirty at all the core must |
| 3350 | * be. |
| 3351 | */ |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 3352 | xfs_dinode_to_disk(dip, &ip->i_d); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3353 | |
| 3354 | /* Wrap, we never let the log put out DI_MAX_FLUSH */ |
| 3355 | if (ip->i_d.di_flushiter == DI_MAX_FLUSH) |
| 3356 | ip->i_d.di_flushiter = 0; |
| 3357 | |
| 3358 | /* |
| 3359 | * If this is really an old format inode and the superblock version |
| 3360 | * has not been updated to support only new format inodes, then |
| 3361 | * convert back to the old inode format. If the superblock version |
| 3362 | * has been updated, then make the conversion permanent. |
| 3363 | */ |
Christoph Hellwig | 51ce16d | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 3364 | ASSERT(ip->i_d.di_version == 1 || xfs_sb_version_hasnlink(&mp->m_sb)); |
| 3365 | if (ip->i_d.di_version == 1) { |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 3366 | if (!xfs_sb_version_hasnlink(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3367 | /* |
| 3368 | * Convert it back. |
| 3369 | */ |
| 3370 | ASSERT(ip->i_d.di_nlink <= XFS_MAXLINK_1); |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 3371 | dip->di_onlink = cpu_to_be16(ip->i_d.di_nlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3372 | } else { |
| 3373 | /* |
| 3374 | * The superblock version has already been bumped, |
| 3375 | * so just make the conversion to the new inode |
| 3376 | * format permanent. |
| 3377 | */ |
Christoph Hellwig | 51ce16d | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 3378 | ip->i_d.di_version = 2; |
| 3379 | dip->di_version = 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3380 | ip->i_d.di_onlink = 0; |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 3381 | dip->di_onlink = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3382 | 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] | 3383 | memset(&(dip->di_pad[0]), 0, |
| 3384 | sizeof(dip->di_pad)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3385 | ASSERT(ip->i_d.di_projid == 0); |
| 3386 | } |
| 3387 | } |
| 3388 | |
David Chinner | e4ac967 | 2008-04-10 12:23:58 +1000 | [diff] [blame] | 3389 | xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK, bp); |
| 3390 | if (XFS_IFORK_Q(ip)) |
| 3391 | xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK, bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3392 | xfs_inobp_check(mp, bp); |
| 3393 | |
| 3394 | /* |
| 3395 | * We've recorded everything logged in the inode, so we'd |
| 3396 | * like to clear the ilf_fields bits so we don't log and |
| 3397 | * flush things unnecessarily. However, we can't stop |
| 3398 | * logging all this information until the data we've copied |
| 3399 | * into the disk buffer is written to disk. If we did we might |
| 3400 | * overwrite the copy of the inode in the log with all the |
| 3401 | * data after re-logging only part of it, and in the face of |
| 3402 | * a crash we wouldn't have all the data we need to recover. |
| 3403 | * |
| 3404 | * What we do is move the bits to the ili_last_fields field. |
| 3405 | * When logging the inode, these bits are moved back to the |
| 3406 | * ilf_fields field. In the xfs_iflush_done() routine we |
| 3407 | * clear ili_last_fields, since we know that the information |
| 3408 | * those bits represent is permanently on disk. As long as |
| 3409 | * the flush completes before the inode is logged again, then |
| 3410 | * both ilf_fields and ili_last_fields will be cleared. |
| 3411 | * |
| 3412 | * We can play with the ilf_fields bits here, because the inode |
| 3413 | * lock must be held exclusively in order to set bits there |
| 3414 | * and the flush lock protects the ili_last_fields bits. |
| 3415 | * Set ili_logged so the flush done |
| 3416 | * routine can tell whether or not to look in the AIL. |
| 3417 | * Also, store the current LSN of the inode so that we can tell |
| 3418 | * whether the item has moved in the AIL from xfs_iflush_done(). |
| 3419 | * In order to read the lsn we need the AIL lock, because |
| 3420 | * it is a 64 bit value that cannot be read atomically. |
| 3421 | */ |
| 3422 | if (iip != NULL && iip->ili_format.ilf_fields != 0) { |
| 3423 | iip->ili_last_fields = iip->ili_format.ilf_fields; |
| 3424 | iip->ili_format.ilf_fields = 0; |
| 3425 | iip->ili_logged = 1; |
| 3426 | |
David Chinner | 7b2e2a3 | 2008-10-30 17:39:12 +1100 | [diff] [blame] | 3427 | xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn, |
| 3428 | &iip->ili_item.li_lsn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3429 | |
| 3430 | /* |
| 3431 | * Attach the function xfs_iflush_done to the inode's |
| 3432 | * buffer. This will remove the inode from the AIL |
| 3433 | * and unlock the inode's flush lock when the inode is |
| 3434 | * completely written to disk. |
| 3435 | */ |
| 3436 | xfs_buf_attach_iodone(bp, (void(*)(xfs_buf_t*,xfs_log_item_t*)) |
| 3437 | xfs_iflush_done, (xfs_log_item_t *)iip); |
| 3438 | |
| 3439 | ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL); |
| 3440 | ASSERT(XFS_BUF_IODONE_FUNC(bp) != NULL); |
| 3441 | } else { |
| 3442 | /* |
| 3443 | * We're flushing an inode which is not in the AIL and has |
| 3444 | * not been logged but has i_update_core set. For this |
| 3445 | * case we can use a B_DELWRI flush and immediately drop |
| 3446 | * the inode flush lock because we can avoid the whole |
| 3447 | * AIL state thing. It's OK to drop the flush lock now, |
| 3448 | * because we've already locked the buffer and to do anything |
| 3449 | * you really need both. |
| 3450 | */ |
| 3451 | if (iip != NULL) { |
| 3452 | ASSERT(iip->ili_logged == 0); |
| 3453 | ASSERT(iip->ili_last_fields == 0); |
| 3454 | ASSERT((iip->ili_item.li_flags & XFS_LI_IN_AIL) == 0); |
| 3455 | } |
| 3456 | xfs_ifunlock(ip); |
| 3457 | } |
| 3458 | |
| 3459 | return 0; |
| 3460 | |
| 3461 | corrupt_out: |
| 3462 | return XFS_ERROR(EFSCORRUPTED); |
| 3463 | } |
| 3464 | |
| 3465 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3466 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3467 | #ifdef XFS_ILOCK_TRACE |
| 3468 | ktrace_t *xfs_ilock_trace_buf; |
| 3469 | |
| 3470 | void |
| 3471 | xfs_ilock_trace(xfs_inode_t *ip, int lock, unsigned int lockflags, inst_t *ra) |
| 3472 | { |
| 3473 | ktrace_enter(ip->i_lock_trace, |
| 3474 | (void *)ip, |
| 3475 | (void *)(unsigned long)lock, /* 1 = LOCK, 3=UNLOCK, etc */ |
| 3476 | (void *)(unsigned long)lockflags, /* XFS_ILOCK_EXCL etc */ |
| 3477 | (void *)ra, /* caller of ilock */ |
| 3478 | (void *)(unsigned long)current_cpu(), |
| 3479 | (void *)(unsigned long)current_pid(), |
| 3480 | NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL); |
| 3481 | } |
| 3482 | #endif |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3483 | |
| 3484 | /* |
| 3485 | * Return a pointer to the extent record at file index idx. |
| 3486 | */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3487 | xfs_bmbt_rec_host_t * |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3488 | xfs_iext_get_ext( |
| 3489 | xfs_ifork_t *ifp, /* inode fork pointer */ |
| 3490 | xfs_extnum_t idx) /* index of target extent */ |
| 3491 | { |
| 3492 | ASSERT(idx >= 0); |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3493 | if ((ifp->if_flags & XFS_IFEXTIREC) && (idx == 0)) { |
| 3494 | return ifp->if_u1.if_ext_irec->er_extbuf; |
| 3495 | } else if (ifp->if_flags & XFS_IFEXTIREC) { |
| 3496 | xfs_ext_irec_t *erp; /* irec pointer */ |
| 3497 | int erp_idx = 0; /* irec index */ |
| 3498 | xfs_extnum_t page_idx = idx; /* ext index in target list */ |
| 3499 | |
| 3500 | erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0); |
| 3501 | return &erp->er_extbuf[page_idx]; |
| 3502 | } else if (ifp->if_bytes) { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3503 | return &ifp->if_u1.if_extents[idx]; |
| 3504 | } else { |
| 3505 | return NULL; |
| 3506 | } |
| 3507 | } |
| 3508 | |
| 3509 | /* |
| 3510 | * Insert new item(s) into the extent records for incore inode |
| 3511 | * fork 'ifp'. 'count' new items are inserted at index 'idx'. |
| 3512 | */ |
| 3513 | void |
| 3514 | xfs_iext_insert( |
| 3515 | xfs_ifork_t *ifp, /* inode fork pointer */ |
| 3516 | xfs_extnum_t idx, /* starting index of new items */ |
| 3517 | xfs_extnum_t count, /* number of inserted items */ |
| 3518 | xfs_bmbt_irec_t *new) /* items to insert */ |
| 3519 | { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3520 | xfs_extnum_t i; /* extent record index */ |
| 3521 | |
| 3522 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
| 3523 | xfs_iext_add(ifp, idx, count); |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3524 | for (i = idx; i < idx + count; i++, new++) |
| 3525 | xfs_bmbt_set_all(xfs_iext_get_ext(ifp, i), new); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3526 | } |
| 3527 | |
| 3528 | /* |
| 3529 | * This is called when the amount of space required for incore file |
| 3530 | * extents needs to be increased. The ext_diff parameter stores the |
| 3531 | * number of new extents being added and the idx parameter contains |
| 3532 | * the extent index where the new extents will be added. If the new |
| 3533 | * extents are being appended, then we just need to (re)allocate and |
| 3534 | * initialize the space. Otherwise, if the new extents are being |
| 3535 | * inserted into the middle of the existing entries, a bit more work |
| 3536 | * is required to make room for the new extents to be inserted. The |
| 3537 | * caller is responsible for filling in the new extent entries upon |
| 3538 | * return. |
| 3539 | */ |
| 3540 | void |
| 3541 | xfs_iext_add( |
| 3542 | xfs_ifork_t *ifp, /* inode fork pointer */ |
| 3543 | xfs_extnum_t idx, /* index to begin adding exts */ |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 3544 | int ext_diff) /* number of extents to add */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3545 | { |
| 3546 | int byte_diff; /* new bytes being added */ |
| 3547 | int new_size; /* size of extents after adding */ |
| 3548 | xfs_extnum_t nextents; /* number of extents in file */ |
| 3549 | |
| 3550 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 3551 | ASSERT((idx >= 0) && (idx <= nextents)); |
| 3552 | byte_diff = ext_diff * sizeof(xfs_bmbt_rec_t); |
| 3553 | new_size = ifp->if_bytes + byte_diff; |
| 3554 | /* |
| 3555 | * If the new number of extents (nextents + ext_diff) |
| 3556 | * fits inside the inode, then continue to use the inline |
| 3557 | * extent buffer. |
| 3558 | */ |
| 3559 | if (nextents + ext_diff <= XFS_INLINE_EXTS) { |
| 3560 | if (idx < nextents) { |
| 3561 | memmove(&ifp->if_u2.if_inline_ext[idx + ext_diff], |
| 3562 | &ifp->if_u2.if_inline_ext[idx], |
| 3563 | (nextents - idx) * sizeof(xfs_bmbt_rec_t)); |
| 3564 | memset(&ifp->if_u2.if_inline_ext[idx], 0, byte_diff); |
| 3565 | } |
| 3566 | ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext; |
| 3567 | ifp->if_real_bytes = 0; |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3568 | ifp->if_lastex = nextents + ext_diff; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3569 | } |
| 3570 | /* |
| 3571 | * Otherwise use a linear (direct) extent list. |
| 3572 | * If the extents are currently inside the inode, |
| 3573 | * xfs_iext_realloc_direct will switch us from |
| 3574 | * inline to direct extent allocation mode. |
| 3575 | */ |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3576 | else if (nextents + ext_diff <= XFS_LINEAR_EXTS) { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3577 | xfs_iext_realloc_direct(ifp, new_size); |
| 3578 | if (idx < nextents) { |
| 3579 | memmove(&ifp->if_u1.if_extents[idx + ext_diff], |
| 3580 | &ifp->if_u1.if_extents[idx], |
| 3581 | (nextents - idx) * sizeof(xfs_bmbt_rec_t)); |
| 3582 | memset(&ifp->if_u1.if_extents[idx], 0, byte_diff); |
| 3583 | } |
| 3584 | } |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3585 | /* Indirection array */ |
| 3586 | else { |
| 3587 | xfs_ext_irec_t *erp; |
| 3588 | int erp_idx = 0; |
| 3589 | int page_idx = idx; |
| 3590 | |
| 3591 | ASSERT(nextents + ext_diff > XFS_LINEAR_EXTS); |
| 3592 | if (ifp->if_flags & XFS_IFEXTIREC) { |
| 3593 | erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 1); |
| 3594 | } else { |
| 3595 | xfs_iext_irec_init(ifp); |
| 3596 | ASSERT(ifp->if_flags & XFS_IFEXTIREC); |
| 3597 | erp = ifp->if_u1.if_ext_irec; |
| 3598 | } |
| 3599 | /* Extents fit in target extent page */ |
| 3600 | if (erp && erp->er_extcount + ext_diff <= XFS_LINEAR_EXTS) { |
| 3601 | if (page_idx < erp->er_extcount) { |
| 3602 | memmove(&erp->er_extbuf[page_idx + ext_diff], |
| 3603 | &erp->er_extbuf[page_idx], |
| 3604 | (erp->er_extcount - page_idx) * |
| 3605 | sizeof(xfs_bmbt_rec_t)); |
| 3606 | memset(&erp->er_extbuf[page_idx], 0, byte_diff); |
| 3607 | } |
| 3608 | erp->er_extcount += ext_diff; |
| 3609 | xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff); |
| 3610 | } |
| 3611 | /* Insert a new extent page */ |
| 3612 | else if (erp) { |
| 3613 | xfs_iext_add_indirect_multi(ifp, |
| 3614 | erp_idx, page_idx, ext_diff); |
| 3615 | } |
| 3616 | /* |
| 3617 | * If extent(s) are being appended to the last page in |
| 3618 | * the indirection array and the new extent(s) don't fit |
| 3619 | * in the page, then erp is NULL and erp_idx is set to |
| 3620 | * the next index needed in the indirection array. |
| 3621 | */ |
| 3622 | else { |
| 3623 | int count = ext_diff; |
| 3624 | |
| 3625 | while (count) { |
| 3626 | erp = xfs_iext_irec_new(ifp, erp_idx); |
| 3627 | erp->er_extcount = count; |
| 3628 | count -= MIN(count, (int)XFS_LINEAR_EXTS); |
| 3629 | if (count) { |
| 3630 | erp_idx++; |
| 3631 | } |
| 3632 | } |
| 3633 | } |
| 3634 | } |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3635 | ifp->if_bytes = new_size; |
| 3636 | } |
| 3637 | |
| 3638 | /* |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3639 | * This is called when incore extents are being added to the indirection |
| 3640 | * array and the new extents do not fit in the target extent list. The |
| 3641 | * erp_idx parameter contains the irec index for the target extent list |
| 3642 | * in the indirection array, and the idx parameter contains the extent |
| 3643 | * index within the list. The number of extents being added is stored |
| 3644 | * in the count parameter. |
| 3645 | * |
| 3646 | * |-------| |-------| |
| 3647 | * | | | | idx - number of extents before idx |
| 3648 | * | idx | | count | |
| 3649 | * | | | | count - number of extents being inserted at idx |
| 3650 | * |-------| |-------| |
| 3651 | * | count | | nex2 | nex2 - number of extents after idx + count |
| 3652 | * |-------| |-------| |
| 3653 | */ |
| 3654 | void |
| 3655 | xfs_iext_add_indirect_multi( |
| 3656 | xfs_ifork_t *ifp, /* inode fork pointer */ |
| 3657 | int erp_idx, /* target extent irec index */ |
| 3658 | xfs_extnum_t idx, /* index within target list */ |
| 3659 | int count) /* new extents being added */ |
| 3660 | { |
| 3661 | int byte_diff; /* new bytes being added */ |
| 3662 | xfs_ext_irec_t *erp; /* pointer to irec entry */ |
| 3663 | xfs_extnum_t ext_diff; /* number of extents to add */ |
| 3664 | xfs_extnum_t ext_cnt; /* new extents still needed */ |
| 3665 | xfs_extnum_t nex2; /* extents after idx + count */ |
| 3666 | xfs_bmbt_rec_t *nex2_ep = NULL; /* temp list for nex2 extents */ |
| 3667 | int nlists; /* number of irec's (lists) */ |
| 3668 | |
| 3669 | ASSERT(ifp->if_flags & XFS_IFEXTIREC); |
| 3670 | erp = &ifp->if_u1.if_ext_irec[erp_idx]; |
| 3671 | nex2 = erp->er_extcount - idx; |
| 3672 | nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ; |
| 3673 | |
| 3674 | /* |
| 3675 | * Save second part of target extent list |
| 3676 | * (all extents past */ |
| 3677 | if (nex2) { |
| 3678 | byte_diff = nex2 * sizeof(xfs_bmbt_rec_t); |
David Chinner | 6785073 | 2008-08-13 16:02:51 +1000 | [diff] [blame] | 3679 | nex2_ep = (xfs_bmbt_rec_t *) kmem_alloc(byte_diff, KM_NOFS); |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3680 | memmove(nex2_ep, &erp->er_extbuf[idx], byte_diff); |
| 3681 | erp->er_extcount -= nex2; |
| 3682 | xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -nex2); |
| 3683 | memset(&erp->er_extbuf[idx], 0, byte_diff); |
| 3684 | } |
| 3685 | |
| 3686 | /* |
| 3687 | * Add the new extents to the end of the target |
| 3688 | * list, then allocate new irec record(s) and |
| 3689 | * extent buffer(s) as needed to store the rest |
| 3690 | * of the new extents. |
| 3691 | */ |
| 3692 | ext_cnt = count; |
| 3693 | ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS - erp->er_extcount); |
| 3694 | if (ext_diff) { |
| 3695 | erp->er_extcount += ext_diff; |
| 3696 | xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff); |
| 3697 | ext_cnt -= ext_diff; |
| 3698 | } |
| 3699 | while (ext_cnt) { |
| 3700 | erp_idx++; |
| 3701 | erp = xfs_iext_irec_new(ifp, erp_idx); |
| 3702 | ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS); |
| 3703 | erp->er_extcount = ext_diff; |
| 3704 | xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff); |
| 3705 | ext_cnt -= ext_diff; |
| 3706 | } |
| 3707 | |
| 3708 | /* Add nex2 extents back to indirection array */ |
| 3709 | if (nex2) { |
| 3710 | xfs_extnum_t ext_avail; |
| 3711 | int i; |
| 3712 | |
| 3713 | byte_diff = nex2 * sizeof(xfs_bmbt_rec_t); |
| 3714 | ext_avail = XFS_LINEAR_EXTS - erp->er_extcount; |
| 3715 | i = 0; |
| 3716 | /* |
| 3717 | * If nex2 extents fit in the current page, append |
| 3718 | * nex2_ep after the new extents. |
| 3719 | */ |
| 3720 | if (nex2 <= ext_avail) { |
| 3721 | i = erp->er_extcount; |
| 3722 | } |
| 3723 | /* |
| 3724 | * Otherwise, check if space is available in the |
| 3725 | * next page. |
| 3726 | */ |
| 3727 | else if ((erp_idx < nlists - 1) && |
| 3728 | (nex2 <= (ext_avail = XFS_LINEAR_EXTS - |
| 3729 | ifp->if_u1.if_ext_irec[erp_idx+1].er_extcount))) { |
| 3730 | erp_idx++; |
| 3731 | erp++; |
| 3732 | /* Create a hole for nex2 extents */ |
| 3733 | memmove(&erp->er_extbuf[nex2], erp->er_extbuf, |
| 3734 | erp->er_extcount * sizeof(xfs_bmbt_rec_t)); |
| 3735 | } |
| 3736 | /* |
| 3737 | * Final choice, create a new extent page for |
| 3738 | * nex2 extents. |
| 3739 | */ |
| 3740 | else { |
| 3741 | erp_idx++; |
| 3742 | erp = xfs_iext_irec_new(ifp, erp_idx); |
| 3743 | } |
| 3744 | memmove(&erp->er_extbuf[i], nex2_ep, byte_diff); |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 3745 | kmem_free(nex2_ep); |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3746 | erp->er_extcount += nex2; |
| 3747 | xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, nex2); |
| 3748 | } |
| 3749 | } |
| 3750 | |
| 3751 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3752 | * This is called when the amount of space required for incore file |
| 3753 | * extents needs to be decreased. The ext_diff parameter stores the |
| 3754 | * number of extents to be removed and the idx parameter contains |
| 3755 | * the extent index where the extents will be removed from. |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3756 | * |
| 3757 | * If the amount of space needed has decreased below the linear |
| 3758 | * limit, XFS_IEXT_BUFSZ, then switch to using the contiguous |
| 3759 | * extent array. Otherwise, use kmem_realloc() to adjust the |
| 3760 | * size to what is needed. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3761 | */ |
| 3762 | void |
| 3763 | xfs_iext_remove( |
| 3764 | xfs_ifork_t *ifp, /* inode fork pointer */ |
| 3765 | xfs_extnum_t idx, /* index to begin removing exts */ |
| 3766 | int ext_diff) /* number of extents to remove */ |
| 3767 | { |
| 3768 | xfs_extnum_t nextents; /* number of extents in file */ |
| 3769 | int new_size; /* size of extents after removal */ |
| 3770 | |
| 3771 | ASSERT(ext_diff > 0); |
| 3772 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 3773 | new_size = (nextents - ext_diff) * sizeof(xfs_bmbt_rec_t); |
| 3774 | |
| 3775 | if (new_size == 0) { |
| 3776 | xfs_iext_destroy(ifp); |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3777 | } else if (ifp->if_flags & XFS_IFEXTIREC) { |
| 3778 | xfs_iext_remove_indirect(ifp, idx, ext_diff); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3779 | } else if (ifp->if_real_bytes) { |
| 3780 | xfs_iext_remove_direct(ifp, idx, ext_diff); |
| 3781 | } else { |
| 3782 | xfs_iext_remove_inline(ifp, idx, ext_diff); |
| 3783 | } |
| 3784 | ifp->if_bytes = new_size; |
| 3785 | } |
| 3786 | |
| 3787 | /* |
| 3788 | * This removes ext_diff extents from the inline buffer, beginning |
| 3789 | * at extent index idx. |
| 3790 | */ |
| 3791 | void |
| 3792 | xfs_iext_remove_inline( |
| 3793 | xfs_ifork_t *ifp, /* inode fork pointer */ |
| 3794 | xfs_extnum_t idx, /* index to begin removing exts */ |
| 3795 | int ext_diff) /* number of extents to remove */ |
| 3796 | { |
| 3797 | int nextents; /* number of extents in file */ |
| 3798 | |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3799 | ASSERT(!(ifp->if_flags & XFS_IFEXTIREC)); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3800 | ASSERT(idx < XFS_INLINE_EXTS); |
| 3801 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 3802 | ASSERT(((nextents - ext_diff) > 0) && |
| 3803 | (nextents - ext_diff) < XFS_INLINE_EXTS); |
| 3804 | |
| 3805 | if (idx + ext_diff < nextents) { |
| 3806 | memmove(&ifp->if_u2.if_inline_ext[idx], |
| 3807 | &ifp->if_u2.if_inline_ext[idx + ext_diff], |
| 3808 | (nextents - (idx + ext_diff)) * |
| 3809 | sizeof(xfs_bmbt_rec_t)); |
| 3810 | memset(&ifp->if_u2.if_inline_ext[nextents - ext_diff], |
| 3811 | 0, ext_diff * sizeof(xfs_bmbt_rec_t)); |
| 3812 | } else { |
| 3813 | memset(&ifp->if_u2.if_inline_ext[idx], 0, |
| 3814 | ext_diff * sizeof(xfs_bmbt_rec_t)); |
| 3815 | } |
| 3816 | } |
| 3817 | |
| 3818 | /* |
| 3819 | * This removes ext_diff extents from a linear (direct) extent list, |
| 3820 | * beginning at extent index idx. If the extents are being removed |
| 3821 | * from the end of the list (ie. truncate) then we just need to re- |
| 3822 | * allocate the list to remove the extra space. Otherwise, if the |
| 3823 | * extents are being removed from the middle of the existing extent |
| 3824 | * entries, then we first need to move the extent records beginning |
| 3825 | * at idx + ext_diff up in the list to overwrite the records being |
| 3826 | * removed, then remove the extra space via kmem_realloc. |
| 3827 | */ |
| 3828 | void |
| 3829 | xfs_iext_remove_direct( |
| 3830 | xfs_ifork_t *ifp, /* inode fork pointer */ |
| 3831 | xfs_extnum_t idx, /* index to begin removing exts */ |
| 3832 | int ext_diff) /* number of extents to remove */ |
| 3833 | { |
| 3834 | xfs_extnum_t nextents; /* number of extents in file */ |
| 3835 | int new_size; /* size of extents after removal */ |
| 3836 | |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3837 | ASSERT(!(ifp->if_flags & XFS_IFEXTIREC)); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3838 | new_size = ifp->if_bytes - |
| 3839 | (ext_diff * sizeof(xfs_bmbt_rec_t)); |
| 3840 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 3841 | |
| 3842 | if (new_size == 0) { |
| 3843 | xfs_iext_destroy(ifp); |
| 3844 | return; |
| 3845 | } |
| 3846 | /* Move extents up in the list (if needed) */ |
| 3847 | if (idx + ext_diff < nextents) { |
| 3848 | memmove(&ifp->if_u1.if_extents[idx], |
| 3849 | &ifp->if_u1.if_extents[idx + ext_diff], |
| 3850 | (nextents - (idx + ext_diff)) * |
| 3851 | sizeof(xfs_bmbt_rec_t)); |
| 3852 | } |
| 3853 | memset(&ifp->if_u1.if_extents[nextents - ext_diff], |
| 3854 | 0, ext_diff * sizeof(xfs_bmbt_rec_t)); |
| 3855 | /* |
| 3856 | * Reallocate the direct extent list. If the extents |
| 3857 | * will fit inside the inode then xfs_iext_realloc_direct |
| 3858 | * will switch from direct to inline extent allocation |
| 3859 | * mode for us. |
| 3860 | */ |
| 3861 | xfs_iext_realloc_direct(ifp, new_size); |
| 3862 | ifp->if_bytes = new_size; |
| 3863 | } |
| 3864 | |
| 3865 | /* |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3866 | * This is called when incore extents are being removed from the |
| 3867 | * indirection array and the extents being removed span multiple extent |
| 3868 | * buffers. The idx parameter contains the file extent index where we |
| 3869 | * want to begin removing extents, and the count parameter contains |
| 3870 | * how many extents need to be removed. |
| 3871 | * |
| 3872 | * |-------| |-------| |
| 3873 | * | nex1 | | | nex1 - number of extents before idx |
| 3874 | * |-------| | count | |
| 3875 | * | | | | count - number of extents being removed at idx |
| 3876 | * | count | |-------| |
| 3877 | * | | | nex2 | nex2 - number of extents after idx + count |
| 3878 | * |-------| |-------| |
| 3879 | */ |
| 3880 | void |
| 3881 | xfs_iext_remove_indirect( |
| 3882 | xfs_ifork_t *ifp, /* inode fork pointer */ |
| 3883 | xfs_extnum_t idx, /* index to begin removing extents */ |
| 3884 | int count) /* number of extents to remove */ |
| 3885 | { |
| 3886 | xfs_ext_irec_t *erp; /* indirection array pointer */ |
| 3887 | int erp_idx = 0; /* indirection array index */ |
| 3888 | xfs_extnum_t ext_cnt; /* extents left to remove */ |
| 3889 | xfs_extnum_t ext_diff; /* extents to remove in current list */ |
| 3890 | xfs_extnum_t nex1; /* number of extents before idx */ |
| 3891 | xfs_extnum_t nex2; /* extents after idx + count */ |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 3892 | int nlists; /* entries in indirection array */ |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3893 | int page_idx = idx; /* index in target extent list */ |
| 3894 | |
| 3895 | ASSERT(ifp->if_flags & XFS_IFEXTIREC); |
| 3896 | erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0); |
| 3897 | ASSERT(erp != NULL); |
| 3898 | nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ; |
| 3899 | nex1 = page_idx; |
| 3900 | ext_cnt = count; |
| 3901 | while (ext_cnt) { |
| 3902 | nex2 = MAX((erp->er_extcount - (nex1 + ext_cnt)), 0); |
| 3903 | ext_diff = MIN(ext_cnt, (erp->er_extcount - nex1)); |
| 3904 | /* |
| 3905 | * Check for deletion of entire list; |
| 3906 | * xfs_iext_irec_remove() updates extent offsets. |
| 3907 | */ |
| 3908 | if (ext_diff == erp->er_extcount) { |
| 3909 | xfs_iext_irec_remove(ifp, erp_idx); |
| 3910 | ext_cnt -= ext_diff; |
| 3911 | nex1 = 0; |
| 3912 | if (ext_cnt) { |
| 3913 | ASSERT(erp_idx < ifp->if_real_bytes / |
| 3914 | XFS_IEXT_BUFSZ); |
| 3915 | erp = &ifp->if_u1.if_ext_irec[erp_idx]; |
| 3916 | nex1 = 0; |
| 3917 | continue; |
| 3918 | } else { |
| 3919 | break; |
| 3920 | } |
| 3921 | } |
| 3922 | /* Move extents up (if needed) */ |
| 3923 | if (nex2) { |
| 3924 | memmove(&erp->er_extbuf[nex1], |
| 3925 | &erp->er_extbuf[nex1 + ext_diff], |
| 3926 | nex2 * sizeof(xfs_bmbt_rec_t)); |
| 3927 | } |
| 3928 | /* Zero out rest of page */ |
| 3929 | memset(&erp->er_extbuf[nex1 + nex2], 0, (XFS_IEXT_BUFSZ - |
| 3930 | ((nex1 + nex2) * sizeof(xfs_bmbt_rec_t)))); |
| 3931 | /* Update remaining counters */ |
| 3932 | erp->er_extcount -= ext_diff; |
| 3933 | xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -ext_diff); |
| 3934 | ext_cnt -= ext_diff; |
| 3935 | nex1 = 0; |
| 3936 | erp_idx++; |
| 3937 | erp++; |
| 3938 | } |
| 3939 | ifp->if_bytes -= count * sizeof(xfs_bmbt_rec_t); |
| 3940 | xfs_iext_irec_compact(ifp); |
| 3941 | } |
| 3942 | |
| 3943 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3944 | * Create, destroy, or resize a linear (direct) block of extents. |
| 3945 | */ |
| 3946 | void |
| 3947 | xfs_iext_realloc_direct( |
| 3948 | xfs_ifork_t *ifp, /* inode fork pointer */ |
| 3949 | int new_size) /* new size of extents */ |
| 3950 | { |
| 3951 | int rnew_size; /* real new size of extents */ |
| 3952 | |
| 3953 | rnew_size = new_size; |
| 3954 | |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3955 | ASSERT(!(ifp->if_flags & XFS_IFEXTIREC) || |
| 3956 | ((new_size >= 0) && (new_size <= XFS_IEXT_BUFSZ) && |
| 3957 | (new_size != ifp->if_real_bytes))); |
| 3958 | |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3959 | /* Free extent records */ |
| 3960 | if (new_size == 0) { |
| 3961 | xfs_iext_destroy(ifp); |
| 3962 | } |
| 3963 | /* Resize direct extent list and zero any new bytes */ |
| 3964 | else if (ifp->if_real_bytes) { |
| 3965 | /* Check if extents will fit inside the inode */ |
| 3966 | if (new_size <= XFS_INLINE_EXTS * sizeof(xfs_bmbt_rec_t)) { |
| 3967 | xfs_iext_direct_to_inline(ifp, new_size / |
| 3968 | (uint)sizeof(xfs_bmbt_rec_t)); |
| 3969 | ifp->if_bytes = new_size; |
| 3970 | return; |
| 3971 | } |
Vignesh Babu | 16a087d | 2007-06-28 16:46:37 +1000 | [diff] [blame] | 3972 | if (!is_power_of_2(new_size)){ |
Robert P. J. Day | 40ebd81 | 2007-11-23 16:30:51 +1100 | [diff] [blame] | 3973 | rnew_size = roundup_pow_of_two(new_size); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3974 | } |
| 3975 | if (rnew_size != ifp->if_real_bytes) { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3976 | ifp->if_u1.if_extents = |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3977 | kmem_realloc(ifp->if_u1.if_extents, |
| 3978 | rnew_size, |
David Chinner | 6785073 | 2008-08-13 16:02:51 +1000 | [diff] [blame] | 3979 | ifp->if_real_bytes, KM_NOFS); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3980 | } |
| 3981 | if (rnew_size > ifp->if_real_bytes) { |
| 3982 | memset(&ifp->if_u1.if_extents[ifp->if_bytes / |
| 3983 | (uint)sizeof(xfs_bmbt_rec_t)], 0, |
| 3984 | rnew_size - ifp->if_real_bytes); |
| 3985 | } |
| 3986 | } |
| 3987 | /* |
| 3988 | * Switch from the inline extent buffer to a direct |
| 3989 | * extent list. Be sure to include the inline extent |
| 3990 | * bytes in new_size. |
| 3991 | */ |
| 3992 | else { |
| 3993 | new_size += ifp->if_bytes; |
Vignesh Babu | 16a087d | 2007-06-28 16:46:37 +1000 | [diff] [blame] | 3994 | if (!is_power_of_2(new_size)) { |
Robert P. J. Day | 40ebd81 | 2007-11-23 16:30:51 +1100 | [diff] [blame] | 3995 | rnew_size = roundup_pow_of_two(new_size); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3996 | } |
| 3997 | xfs_iext_inline_to_direct(ifp, rnew_size); |
| 3998 | } |
| 3999 | ifp->if_real_bytes = rnew_size; |
| 4000 | ifp->if_bytes = new_size; |
| 4001 | } |
| 4002 | |
| 4003 | /* |
| 4004 | * Switch from linear (direct) extent records to inline buffer. |
| 4005 | */ |
| 4006 | void |
| 4007 | xfs_iext_direct_to_inline( |
| 4008 | xfs_ifork_t *ifp, /* inode fork pointer */ |
| 4009 | xfs_extnum_t nextents) /* number of extents in file */ |
| 4010 | { |
| 4011 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
| 4012 | ASSERT(nextents <= XFS_INLINE_EXTS); |
| 4013 | /* |
| 4014 | * The inline buffer was zeroed when we switched |
| 4015 | * from inline to direct extent allocation mode, |
| 4016 | * so we don't need to clear it here. |
| 4017 | */ |
| 4018 | memcpy(ifp->if_u2.if_inline_ext, ifp->if_u1.if_extents, |
| 4019 | nextents * sizeof(xfs_bmbt_rec_t)); |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 4020 | kmem_free(ifp->if_u1.if_extents); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4021 | ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext; |
| 4022 | ifp->if_real_bytes = 0; |
| 4023 | } |
| 4024 | |
| 4025 | /* |
| 4026 | * Switch from inline buffer to linear (direct) extent records. |
| 4027 | * new_size should already be rounded up to the next power of 2 |
| 4028 | * by the caller (when appropriate), so use new_size as it is. |
| 4029 | * However, since new_size may be rounded up, we can't update |
| 4030 | * if_bytes here. It is the caller's responsibility to update |
| 4031 | * if_bytes upon return. |
| 4032 | */ |
| 4033 | void |
| 4034 | xfs_iext_inline_to_direct( |
| 4035 | xfs_ifork_t *ifp, /* inode fork pointer */ |
| 4036 | int new_size) /* number of extents in file */ |
| 4037 | { |
David Chinner | 6785073 | 2008-08-13 16:02:51 +1000 | [diff] [blame] | 4038 | ifp->if_u1.if_extents = kmem_alloc(new_size, KM_NOFS); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4039 | memset(ifp->if_u1.if_extents, 0, new_size); |
| 4040 | if (ifp->if_bytes) { |
| 4041 | memcpy(ifp->if_u1.if_extents, ifp->if_u2.if_inline_ext, |
| 4042 | ifp->if_bytes); |
| 4043 | memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS * |
| 4044 | sizeof(xfs_bmbt_rec_t)); |
| 4045 | } |
| 4046 | ifp->if_real_bytes = new_size; |
| 4047 | } |
| 4048 | |
| 4049 | /* |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 4050 | * Resize an extent indirection array to new_size bytes. |
| 4051 | */ |
| 4052 | void |
| 4053 | xfs_iext_realloc_indirect( |
| 4054 | xfs_ifork_t *ifp, /* inode fork pointer */ |
| 4055 | int new_size) /* new indirection array size */ |
| 4056 | { |
| 4057 | int nlists; /* number of irec's (ex lists) */ |
| 4058 | int size; /* current indirection array size */ |
| 4059 | |
| 4060 | ASSERT(ifp->if_flags & XFS_IFEXTIREC); |
| 4061 | nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ; |
| 4062 | size = nlists * sizeof(xfs_ext_irec_t); |
| 4063 | ASSERT(ifp->if_real_bytes); |
| 4064 | ASSERT((new_size >= 0) && (new_size != size)); |
| 4065 | if (new_size == 0) { |
| 4066 | xfs_iext_destroy(ifp); |
| 4067 | } else { |
| 4068 | ifp->if_u1.if_ext_irec = (xfs_ext_irec_t *) |
| 4069 | kmem_realloc(ifp->if_u1.if_ext_irec, |
David Chinner | 6785073 | 2008-08-13 16:02:51 +1000 | [diff] [blame] | 4070 | new_size, size, KM_NOFS); |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 4071 | } |
| 4072 | } |
| 4073 | |
| 4074 | /* |
| 4075 | * Switch from indirection array to linear (direct) extent allocations. |
| 4076 | */ |
| 4077 | void |
| 4078 | xfs_iext_indirect_to_direct( |
| 4079 | xfs_ifork_t *ifp) /* inode fork pointer */ |
| 4080 | { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 4081 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 4082 | xfs_extnum_t nextents; /* number of extents in file */ |
| 4083 | int size; /* size of file extents */ |
| 4084 | |
| 4085 | ASSERT(ifp->if_flags & XFS_IFEXTIREC); |
| 4086 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 4087 | ASSERT(nextents <= XFS_LINEAR_EXTS); |
| 4088 | size = nextents * sizeof(xfs_bmbt_rec_t); |
| 4089 | |
Lachlan McIlroy | 71a8c87 | 2008-09-26 12:17:57 +1000 | [diff] [blame] | 4090 | xfs_iext_irec_compact_pages(ifp); |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 4091 | ASSERT(ifp->if_real_bytes == XFS_IEXT_BUFSZ); |
| 4092 | |
| 4093 | ep = ifp->if_u1.if_ext_irec->er_extbuf; |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 4094 | kmem_free(ifp->if_u1.if_ext_irec); |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 4095 | ifp->if_flags &= ~XFS_IFEXTIREC; |
| 4096 | ifp->if_u1.if_extents = ep; |
| 4097 | ifp->if_bytes = size; |
| 4098 | if (nextents < XFS_LINEAR_EXTS) { |
| 4099 | xfs_iext_realloc_direct(ifp, size); |
| 4100 | } |
| 4101 | } |
| 4102 | |
| 4103 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4104 | * Free incore file extents. |
| 4105 | */ |
| 4106 | void |
| 4107 | xfs_iext_destroy( |
| 4108 | xfs_ifork_t *ifp) /* inode fork pointer */ |
| 4109 | { |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 4110 | if (ifp->if_flags & XFS_IFEXTIREC) { |
| 4111 | int erp_idx; |
| 4112 | int nlists; |
| 4113 | |
| 4114 | nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ; |
| 4115 | for (erp_idx = nlists - 1; erp_idx >= 0 ; erp_idx--) { |
| 4116 | xfs_iext_irec_remove(ifp, erp_idx); |
| 4117 | } |
| 4118 | ifp->if_flags &= ~XFS_IFEXTIREC; |
| 4119 | } else if (ifp->if_real_bytes) { |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 4120 | kmem_free(ifp->if_u1.if_extents); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4121 | } else if (ifp->if_bytes) { |
| 4122 | memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS * |
| 4123 | sizeof(xfs_bmbt_rec_t)); |
| 4124 | } |
| 4125 | ifp->if_u1.if_extents = NULL; |
| 4126 | ifp->if_real_bytes = 0; |
| 4127 | ifp->if_bytes = 0; |
| 4128 | } |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 4129 | |
| 4130 | /* |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 4131 | * Return a pointer to the extent record for file system block bno. |
| 4132 | */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 4133 | xfs_bmbt_rec_host_t * /* pointer to found extent record */ |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 4134 | xfs_iext_bno_to_ext( |
| 4135 | xfs_ifork_t *ifp, /* inode fork pointer */ |
| 4136 | xfs_fileoff_t bno, /* block number to search for */ |
| 4137 | xfs_extnum_t *idxp) /* index of target extent */ |
| 4138 | { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 4139 | xfs_bmbt_rec_host_t *base; /* pointer to first extent */ |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 4140 | xfs_filblks_t blockcount = 0; /* number of blocks in extent */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 4141 | xfs_bmbt_rec_host_t *ep = NULL; /* pointer to target extent */ |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 4142 | xfs_ext_irec_t *erp = NULL; /* indirection array pointer */ |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 4143 | int high; /* upper boundary in search */ |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 4144 | xfs_extnum_t idx = 0; /* index of target extent */ |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 4145 | int low; /* lower boundary in search */ |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 4146 | xfs_extnum_t nextents; /* number of file extents */ |
| 4147 | xfs_fileoff_t startoff = 0; /* start offset of extent */ |
| 4148 | |
| 4149 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 4150 | if (nextents == 0) { |
| 4151 | *idxp = 0; |
| 4152 | return NULL; |
| 4153 | } |
| 4154 | low = 0; |
| 4155 | if (ifp->if_flags & XFS_IFEXTIREC) { |
| 4156 | /* Find target extent list */ |
| 4157 | int erp_idx = 0; |
| 4158 | erp = xfs_iext_bno_to_irec(ifp, bno, &erp_idx); |
| 4159 | base = erp->er_extbuf; |
| 4160 | high = erp->er_extcount - 1; |
| 4161 | } else { |
| 4162 | base = ifp->if_u1.if_extents; |
| 4163 | high = nextents - 1; |
| 4164 | } |
| 4165 | /* Binary search extent records */ |
| 4166 | while (low <= high) { |
| 4167 | idx = (low + high) >> 1; |
| 4168 | ep = base + idx; |
| 4169 | startoff = xfs_bmbt_get_startoff(ep); |
| 4170 | blockcount = xfs_bmbt_get_blockcount(ep); |
| 4171 | if (bno < startoff) { |
| 4172 | high = idx - 1; |
| 4173 | } else if (bno >= startoff + blockcount) { |
| 4174 | low = idx + 1; |
| 4175 | } else { |
| 4176 | /* Convert back to file-based extent index */ |
| 4177 | if (ifp->if_flags & XFS_IFEXTIREC) { |
| 4178 | idx += erp->er_extoff; |
| 4179 | } |
| 4180 | *idxp = idx; |
| 4181 | return ep; |
| 4182 | } |
| 4183 | } |
| 4184 | /* Convert back to file-based extent index */ |
| 4185 | if (ifp->if_flags & XFS_IFEXTIREC) { |
| 4186 | idx += erp->er_extoff; |
| 4187 | } |
| 4188 | if (bno >= startoff + blockcount) { |
| 4189 | if (++idx == nextents) { |
| 4190 | ep = NULL; |
| 4191 | } else { |
| 4192 | ep = xfs_iext_get_ext(ifp, idx); |
| 4193 | } |
| 4194 | } |
| 4195 | *idxp = idx; |
| 4196 | return ep; |
| 4197 | } |
| 4198 | |
| 4199 | /* |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 4200 | * Return a pointer to the indirection array entry containing the |
| 4201 | * extent record for filesystem block bno. Store the index of the |
| 4202 | * target irec in *erp_idxp. |
| 4203 | */ |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 4204 | xfs_ext_irec_t * /* pointer to found extent record */ |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 4205 | xfs_iext_bno_to_irec( |
| 4206 | xfs_ifork_t *ifp, /* inode fork pointer */ |
| 4207 | xfs_fileoff_t bno, /* block number to search for */ |
| 4208 | int *erp_idxp) /* irec index of target ext list */ |
| 4209 | { |
| 4210 | xfs_ext_irec_t *erp = NULL; /* indirection array pointer */ |
| 4211 | xfs_ext_irec_t *erp_next; /* next indirection array entry */ |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 4212 | int erp_idx; /* indirection array index */ |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 4213 | int nlists; /* number of extent irec's (lists) */ |
| 4214 | int high; /* binary search upper limit */ |
| 4215 | int low; /* binary search lower limit */ |
| 4216 | |
| 4217 | ASSERT(ifp->if_flags & XFS_IFEXTIREC); |
| 4218 | nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ; |
| 4219 | erp_idx = 0; |
| 4220 | low = 0; |
| 4221 | high = nlists - 1; |
| 4222 | while (low <= high) { |
| 4223 | erp_idx = (low + high) >> 1; |
| 4224 | erp = &ifp->if_u1.if_ext_irec[erp_idx]; |
| 4225 | erp_next = erp_idx < nlists - 1 ? erp + 1 : NULL; |
| 4226 | if (bno < xfs_bmbt_get_startoff(erp->er_extbuf)) { |
| 4227 | high = erp_idx - 1; |
| 4228 | } else if (erp_next && bno >= |
| 4229 | xfs_bmbt_get_startoff(erp_next->er_extbuf)) { |
| 4230 | low = erp_idx + 1; |
| 4231 | } else { |
| 4232 | break; |
| 4233 | } |
| 4234 | } |
| 4235 | *erp_idxp = erp_idx; |
| 4236 | return erp; |
| 4237 | } |
| 4238 | |
| 4239 | /* |
| 4240 | * Return a pointer to the indirection array entry containing the |
| 4241 | * extent record at file extent index *idxp. Store the index of the |
| 4242 | * target irec in *erp_idxp and store the page index of the target |
| 4243 | * extent record in *idxp. |
| 4244 | */ |
| 4245 | xfs_ext_irec_t * |
| 4246 | xfs_iext_idx_to_irec( |
| 4247 | xfs_ifork_t *ifp, /* inode fork pointer */ |
| 4248 | xfs_extnum_t *idxp, /* extent index (file -> page) */ |
| 4249 | int *erp_idxp, /* pointer to target irec */ |
| 4250 | int realloc) /* new bytes were just added */ |
| 4251 | { |
| 4252 | xfs_ext_irec_t *prev; /* pointer to previous irec */ |
| 4253 | xfs_ext_irec_t *erp = NULL; /* pointer to current irec */ |
| 4254 | int erp_idx; /* indirection array index */ |
| 4255 | int nlists; /* number of irec's (ex lists) */ |
| 4256 | int high; /* binary search upper limit */ |
| 4257 | int low; /* binary search lower limit */ |
| 4258 | xfs_extnum_t page_idx = *idxp; /* extent index in target list */ |
| 4259 | |
| 4260 | ASSERT(ifp->if_flags & XFS_IFEXTIREC); |
| 4261 | ASSERT(page_idx >= 0 && page_idx <= |
| 4262 | ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)); |
| 4263 | nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ; |
| 4264 | erp_idx = 0; |
| 4265 | low = 0; |
| 4266 | high = nlists - 1; |
| 4267 | |
| 4268 | /* Binary search extent irec's */ |
| 4269 | while (low <= high) { |
| 4270 | erp_idx = (low + high) >> 1; |
| 4271 | erp = &ifp->if_u1.if_ext_irec[erp_idx]; |
| 4272 | prev = erp_idx > 0 ? erp - 1 : NULL; |
| 4273 | if (page_idx < erp->er_extoff || (page_idx == erp->er_extoff && |
| 4274 | realloc && prev && prev->er_extcount < XFS_LINEAR_EXTS)) { |
| 4275 | high = erp_idx - 1; |
| 4276 | } else if (page_idx > erp->er_extoff + erp->er_extcount || |
| 4277 | (page_idx == erp->er_extoff + erp->er_extcount && |
| 4278 | !realloc)) { |
| 4279 | low = erp_idx + 1; |
| 4280 | } else if (page_idx == erp->er_extoff + erp->er_extcount && |
| 4281 | erp->er_extcount == XFS_LINEAR_EXTS) { |
| 4282 | ASSERT(realloc); |
| 4283 | page_idx = 0; |
| 4284 | erp_idx++; |
| 4285 | erp = erp_idx < nlists ? erp + 1 : NULL; |
| 4286 | break; |
| 4287 | } else { |
| 4288 | page_idx -= erp->er_extoff; |
| 4289 | break; |
| 4290 | } |
| 4291 | } |
| 4292 | *idxp = page_idx; |
| 4293 | *erp_idxp = erp_idx; |
| 4294 | return(erp); |
| 4295 | } |
| 4296 | |
| 4297 | /* |
| 4298 | * Allocate and initialize an indirection array once the space needed |
| 4299 | * for incore extents increases above XFS_IEXT_BUFSZ. |
| 4300 | */ |
| 4301 | void |
| 4302 | xfs_iext_irec_init( |
| 4303 | xfs_ifork_t *ifp) /* inode fork pointer */ |
| 4304 | { |
| 4305 | xfs_ext_irec_t *erp; /* indirection array pointer */ |
| 4306 | xfs_extnum_t nextents; /* number of extents in file */ |
| 4307 | |
| 4308 | ASSERT(!(ifp->if_flags & XFS_IFEXTIREC)); |
| 4309 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 4310 | ASSERT(nextents <= XFS_LINEAR_EXTS); |
| 4311 | |
David Chinner | 6785073 | 2008-08-13 16:02:51 +1000 | [diff] [blame] | 4312 | erp = kmem_alloc(sizeof(xfs_ext_irec_t), KM_NOFS); |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 4313 | |
| 4314 | if (nextents == 0) { |
David Chinner | 6785073 | 2008-08-13 16:02:51 +1000 | [diff] [blame] | 4315 | ifp->if_u1.if_extents = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS); |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 4316 | } else if (!ifp->if_real_bytes) { |
| 4317 | xfs_iext_inline_to_direct(ifp, XFS_IEXT_BUFSZ); |
| 4318 | } else if (ifp->if_real_bytes < XFS_IEXT_BUFSZ) { |
| 4319 | xfs_iext_realloc_direct(ifp, XFS_IEXT_BUFSZ); |
| 4320 | } |
| 4321 | erp->er_extbuf = ifp->if_u1.if_extents; |
| 4322 | erp->er_extcount = nextents; |
| 4323 | erp->er_extoff = 0; |
| 4324 | |
| 4325 | ifp->if_flags |= XFS_IFEXTIREC; |
| 4326 | ifp->if_real_bytes = XFS_IEXT_BUFSZ; |
| 4327 | ifp->if_bytes = nextents * sizeof(xfs_bmbt_rec_t); |
| 4328 | ifp->if_u1.if_ext_irec = erp; |
| 4329 | |
| 4330 | return; |
| 4331 | } |
| 4332 | |
| 4333 | /* |
| 4334 | * Allocate and initialize a new entry in the indirection array. |
| 4335 | */ |
| 4336 | xfs_ext_irec_t * |
| 4337 | xfs_iext_irec_new( |
| 4338 | xfs_ifork_t *ifp, /* inode fork pointer */ |
| 4339 | int erp_idx) /* index for new irec */ |
| 4340 | { |
| 4341 | xfs_ext_irec_t *erp; /* indirection array pointer */ |
| 4342 | int i; /* loop counter */ |
| 4343 | int nlists; /* number of irec's (ex lists) */ |
| 4344 | |
| 4345 | ASSERT(ifp->if_flags & XFS_IFEXTIREC); |
| 4346 | nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ; |
| 4347 | |
| 4348 | /* Resize indirection array */ |
| 4349 | xfs_iext_realloc_indirect(ifp, ++nlists * |
| 4350 | sizeof(xfs_ext_irec_t)); |
| 4351 | /* |
| 4352 | * Move records down in the array so the |
| 4353 | * new page can use erp_idx. |
| 4354 | */ |
| 4355 | erp = ifp->if_u1.if_ext_irec; |
| 4356 | for (i = nlists - 1; i > erp_idx; i--) { |
| 4357 | memmove(&erp[i], &erp[i-1], sizeof(xfs_ext_irec_t)); |
| 4358 | } |
| 4359 | ASSERT(i == erp_idx); |
| 4360 | |
| 4361 | /* Initialize new extent record */ |
| 4362 | erp = ifp->if_u1.if_ext_irec; |
David Chinner | 6785073 | 2008-08-13 16:02:51 +1000 | [diff] [blame] | 4363 | erp[erp_idx].er_extbuf = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS); |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 4364 | ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ; |
| 4365 | memset(erp[erp_idx].er_extbuf, 0, XFS_IEXT_BUFSZ); |
| 4366 | erp[erp_idx].er_extcount = 0; |
| 4367 | erp[erp_idx].er_extoff = erp_idx > 0 ? |
| 4368 | erp[erp_idx-1].er_extoff + erp[erp_idx-1].er_extcount : 0; |
| 4369 | return (&erp[erp_idx]); |
| 4370 | } |
| 4371 | |
| 4372 | /* |
| 4373 | * Remove a record from the indirection array. |
| 4374 | */ |
| 4375 | void |
| 4376 | xfs_iext_irec_remove( |
| 4377 | xfs_ifork_t *ifp, /* inode fork pointer */ |
| 4378 | int erp_idx) /* irec index to remove */ |
| 4379 | { |
| 4380 | xfs_ext_irec_t *erp; /* indirection array pointer */ |
| 4381 | int i; /* loop counter */ |
| 4382 | int nlists; /* number of irec's (ex lists) */ |
| 4383 | |
| 4384 | ASSERT(ifp->if_flags & XFS_IFEXTIREC); |
| 4385 | nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ; |
| 4386 | erp = &ifp->if_u1.if_ext_irec[erp_idx]; |
| 4387 | if (erp->er_extbuf) { |
| 4388 | xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, |
| 4389 | -erp->er_extcount); |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 4390 | kmem_free(erp->er_extbuf); |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 4391 | } |
| 4392 | /* Compact extent records */ |
| 4393 | erp = ifp->if_u1.if_ext_irec; |
| 4394 | for (i = erp_idx; i < nlists - 1; i++) { |
| 4395 | memmove(&erp[i], &erp[i+1], sizeof(xfs_ext_irec_t)); |
| 4396 | } |
| 4397 | /* |
| 4398 | * Manually free the last extent record from the indirection |
| 4399 | * array. A call to xfs_iext_realloc_indirect() with a size |
| 4400 | * of zero would result in a call to xfs_iext_destroy() which |
| 4401 | * would in turn call this function again, creating a nasty |
| 4402 | * infinite loop. |
| 4403 | */ |
| 4404 | if (--nlists) { |
| 4405 | xfs_iext_realloc_indirect(ifp, |
| 4406 | nlists * sizeof(xfs_ext_irec_t)); |
| 4407 | } else { |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 4408 | kmem_free(ifp->if_u1.if_ext_irec); |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 4409 | } |
| 4410 | ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ; |
| 4411 | } |
| 4412 | |
| 4413 | /* |
| 4414 | * This is called to clean up large amounts of unused memory allocated |
| 4415 | * by the indirection array. Before compacting anything though, verify |
| 4416 | * that the indirection array is still needed and switch back to the |
| 4417 | * linear extent list (or even the inline buffer) if possible. The |
| 4418 | * compaction policy is as follows: |
| 4419 | * |
| 4420 | * Full Compaction: Extents fit into a single page (or inline buffer) |
Lachlan McIlroy | 71a8c87 | 2008-09-26 12:17:57 +1000 | [diff] [blame] | 4421 | * Partial Compaction: Extents occupy less than 50% of allocated space |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 4422 | * No Compaction: Extents occupy at least 50% of allocated space |
| 4423 | */ |
| 4424 | void |
| 4425 | xfs_iext_irec_compact( |
| 4426 | xfs_ifork_t *ifp) /* inode fork pointer */ |
| 4427 | { |
| 4428 | xfs_extnum_t nextents; /* number of extents in file */ |
| 4429 | int nlists; /* number of irec's (ex lists) */ |
| 4430 | |
| 4431 | ASSERT(ifp->if_flags & XFS_IFEXTIREC); |
| 4432 | nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ; |
| 4433 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 4434 | |
| 4435 | if (nextents == 0) { |
| 4436 | xfs_iext_destroy(ifp); |
| 4437 | } else if (nextents <= XFS_INLINE_EXTS) { |
| 4438 | xfs_iext_indirect_to_direct(ifp); |
| 4439 | xfs_iext_direct_to_inline(ifp, nextents); |
| 4440 | } else if (nextents <= XFS_LINEAR_EXTS) { |
| 4441 | xfs_iext_indirect_to_direct(ifp); |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 4442 | } else if (nextents < (nlists * XFS_LINEAR_EXTS) >> 1) { |
| 4443 | xfs_iext_irec_compact_pages(ifp); |
| 4444 | } |
| 4445 | } |
| 4446 | |
| 4447 | /* |
| 4448 | * Combine extents from neighboring extent pages. |
| 4449 | */ |
| 4450 | void |
| 4451 | xfs_iext_irec_compact_pages( |
| 4452 | xfs_ifork_t *ifp) /* inode fork pointer */ |
| 4453 | { |
| 4454 | xfs_ext_irec_t *erp, *erp_next;/* pointers to irec entries */ |
| 4455 | int erp_idx = 0; /* indirection array index */ |
| 4456 | int nlists; /* number of irec's (ex lists) */ |
| 4457 | |
| 4458 | ASSERT(ifp->if_flags & XFS_IFEXTIREC); |
| 4459 | nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ; |
| 4460 | while (erp_idx < nlists - 1) { |
| 4461 | erp = &ifp->if_u1.if_ext_irec[erp_idx]; |
| 4462 | erp_next = erp + 1; |
| 4463 | if (erp_next->er_extcount <= |
| 4464 | (XFS_LINEAR_EXTS - erp->er_extcount)) { |
Lachlan McIlroy | 71a8c87 | 2008-09-26 12:17:57 +1000 | [diff] [blame] | 4465 | memcpy(&erp->er_extbuf[erp->er_extcount], |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 4466 | erp_next->er_extbuf, erp_next->er_extcount * |
| 4467 | sizeof(xfs_bmbt_rec_t)); |
| 4468 | erp->er_extcount += erp_next->er_extcount; |
| 4469 | /* |
| 4470 | * Free page before removing extent record |
| 4471 | * so er_extoffs don't get modified in |
| 4472 | * xfs_iext_irec_remove. |
| 4473 | */ |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 4474 | kmem_free(erp_next->er_extbuf); |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 4475 | erp_next->er_extbuf = NULL; |
| 4476 | xfs_iext_irec_remove(ifp, erp_idx + 1); |
| 4477 | nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ; |
| 4478 | } else { |
| 4479 | erp_idx++; |
| 4480 | } |
| 4481 | } |
| 4482 | } |
| 4483 | |
| 4484 | /* |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 4485 | * This is called to update the er_extoff field in the indirection |
| 4486 | * array when extents have been added or removed from one of the |
| 4487 | * extent lists. erp_idx contains the irec index to begin updating |
| 4488 | * at and ext_diff contains the number of extents that were added |
| 4489 | * or removed. |
| 4490 | */ |
| 4491 | void |
| 4492 | xfs_iext_irec_update_extoffs( |
| 4493 | xfs_ifork_t *ifp, /* inode fork pointer */ |
| 4494 | int erp_idx, /* irec index to update */ |
| 4495 | int ext_diff) /* number of new extents */ |
| 4496 | { |
| 4497 | int i; /* loop counter */ |
| 4498 | int nlists; /* number of irec's (ex lists */ |
| 4499 | |
| 4500 | ASSERT(ifp->if_flags & XFS_IFEXTIREC); |
| 4501 | nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ; |
| 4502 | for (i = erp_idx; i < nlists; i++) { |
| 4503 | ifp->if_u1.if_ext_irec[i].er_extoff += ext_diff; |
| 4504 | } |
| 4505 | } |