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