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