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