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" |
Dave Chinner | 6ca1c90 | 2013-08-12 20:49:26 +1000 | [diff] [blame] | 22 | #include "xfs_format.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" |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame^] | 42 | #include "xfs_bmap_util.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include "xfs_error.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #include "xfs_utils.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include "xfs_quota.h" |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 46 | #include "xfs_filestream.h" |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 47 | #include "xfs_vnodeops.h" |
Christoph Hellwig | 93848a9 | 2013-04-03 16:11:17 +1100 | [diff] [blame] | 48 | #include "xfs_cksum.h" |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 49 | #include "xfs_trace.h" |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 50 | #include "xfs_icache.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | kmem_zone_t *xfs_inode_zone; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
| 54 | /* |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 55 | * Used in xfs_itruncate_extents(). This is the maximum number of extents |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | * freed from a file in a single transaction. |
| 57 | */ |
| 58 | #define XFS_ITRUNC_MAX_EXTENTS 2 |
| 59 | |
| 60 | STATIC int xfs_iflush_int(xfs_inode_t *, xfs_buf_t *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
Dave Chinner | 2a0ec1d | 2012-04-23 15:59:02 +1000 | [diff] [blame] | 62 | /* |
| 63 | * helper function to extract extent size hint from inode |
| 64 | */ |
| 65 | xfs_extlen_t |
| 66 | xfs_get_extsz_hint( |
| 67 | struct xfs_inode *ip) |
| 68 | { |
| 69 | if ((ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE) && ip->i_d.di_extsize) |
| 70 | return ip->i_d.di_extsize; |
| 71 | if (XFS_IS_REALTIME_INODE(ip)) |
| 72 | return ip->i_mount->m_sb.sb_rextsize; |
| 73 | return 0; |
| 74 | } |
| 75 | |
Dave Chinner | fa96aca | 2012-10-08 21:56:10 +1100 | [diff] [blame] | 76 | /* |
| 77 | * This is a wrapper routine around the xfs_ilock() routine used to centralize |
| 78 | * some grungy code. It is used in places that wish to lock the inode solely |
| 79 | * for reading the extents. The reason these places can't just call |
| 80 | * xfs_ilock(SHARED) is that the inode lock also guards to bringing in of the |
| 81 | * extents from disk for a file in b-tree format. If the inode is in b-tree |
| 82 | * format, then we need to lock the inode exclusively until the extents are read |
| 83 | * in. Locking it exclusively all the time would limit our parallelism |
| 84 | * unnecessarily, though. What we do instead is check to see if the extents |
| 85 | * have been read in yet, and only lock the inode exclusively if they have not. |
| 86 | * |
| 87 | * The function returns a value which should be given to the corresponding |
| 88 | * xfs_iunlock_map_shared(). This value is the mode in which the lock was |
| 89 | * actually taken. |
| 90 | */ |
| 91 | uint |
| 92 | xfs_ilock_map_shared( |
| 93 | xfs_inode_t *ip) |
| 94 | { |
| 95 | uint lock_mode; |
| 96 | |
| 97 | if ((ip->i_d.di_format == XFS_DINODE_FMT_BTREE) && |
| 98 | ((ip->i_df.if_flags & XFS_IFEXTENTS) == 0)) { |
| 99 | lock_mode = XFS_ILOCK_EXCL; |
| 100 | } else { |
| 101 | lock_mode = XFS_ILOCK_SHARED; |
| 102 | } |
| 103 | |
| 104 | xfs_ilock(ip, lock_mode); |
| 105 | |
| 106 | return lock_mode; |
| 107 | } |
| 108 | |
| 109 | /* |
| 110 | * This is simply the unlock routine to go with xfs_ilock_map_shared(). |
| 111 | * All it does is call xfs_iunlock() with the given lock_mode. |
| 112 | */ |
| 113 | void |
| 114 | xfs_iunlock_map_shared( |
| 115 | xfs_inode_t *ip, |
| 116 | unsigned int lock_mode) |
| 117 | { |
| 118 | xfs_iunlock(ip, lock_mode); |
| 119 | } |
| 120 | |
| 121 | /* |
| 122 | * The xfs inode contains 2 locks: a multi-reader lock called the |
| 123 | * i_iolock and a multi-reader lock called the i_lock. This routine |
| 124 | * allows either or both of the locks to be obtained. |
| 125 | * |
| 126 | * The 2 locks should always be ordered so that the IO lock is |
| 127 | * obtained first in order to prevent deadlock. |
| 128 | * |
| 129 | * ip -- the inode being locked |
| 130 | * lock_flags -- this parameter indicates the inode's locks |
| 131 | * to be locked. It can be: |
| 132 | * XFS_IOLOCK_SHARED, |
| 133 | * XFS_IOLOCK_EXCL, |
| 134 | * XFS_ILOCK_SHARED, |
| 135 | * XFS_ILOCK_EXCL, |
| 136 | * XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED, |
| 137 | * XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL, |
| 138 | * XFS_IOLOCK_EXCL | XFS_ILOCK_SHARED, |
| 139 | * XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL |
| 140 | */ |
| 141 | void |
| 142 | xfs_ilock( |
| 143 | xfs_inode_t *ip, |
| 144 | uint lock_flags) |
| 145 | { |
| 146 | trace_xfs_ilock(ip, lock_flags, _RET_IP_); |
| 147 | |
| 148 | /* |
| 149 | * You can't set both SHARED and EXCL for the same lock, |
| 150 | * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED, |
| 151 | * and XFS_ILOCK_EXCL are valid values to set in lock_flags. |
| 152 | */ |
| 153 | ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) != |
| 154 | (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)); |
| 155 | ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) != |
| 156 | (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)); |
| 157 | ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0); |
| 158 | |
| 159 | if (lock_flags & XFS_IOLOCK_EXCL) |
| 160 | mrupdate_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags)); |
| 161 | else if (lock_flags & XFS_IOLOCK_SHARED) |
| 162 | mraccess_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags)); |
| 163 | |
| 164 | if (lock_flags & XFS_ILOCK_EXCL) |
| 165 | mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags)); |
| 166 | else if (lock_flags & XFS_ILOCK_SHARED) |
| 167 | mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags)); |
| 168 | } |
| 169 | |
| 170 | /* |
| 171 | * This is just like xfs_ilock(), except that the caller |
| 172 | * is guaranteed not to sleep. It returns 1 if it gets |
| 173 | * the requested locks and 0 otherwise. If the IO lock is |
| 174 | * obtained but the inode lock cannot be, then the IO lock |
| 175 | * is dropped before returning. |
| 176 | * |
| 177 | * ip -- the inode being locked |
| 178 | * lock_flags -- this parameter indicates the inode's locks to be |
| 179 | * to be locked. See the comment for xfs_ilock() for a list |
| 180 | * of valid values. |
| 181 | */ |
| 182 | int |
| 183 | xfs_ilock_nowait( |
| 184 | xfs_inode_t *ip, |
| 185 | uint lock_flags) |
| 186 | { |
| 187 | trace_xfs_ilock_nowait(ip, lock_flags, _RET_IP_); |
| 188 | |
| 189 | /* |
| 190 | * You can't set both SHARED and EXCL for the same lock, |
| 191 | * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED, |
| 192 | * and XFS_ILOCK_EXCL are valid values to set in lock_flags. |
| 193 | */ |
| 194 | ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) != |
| 195 | (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)); |
| 196 | ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) != |
| 197 | (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)); |
| 198 | ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0); |
| 199 | |
| 200 | if (lock_flags & XFS_IOLOCK_EXCL) { |
| 201 | if (!mrtryupdate(&ip->i_iolock)) |
| 202 | goto out; |
| 203 | } else if (lock_flags & XFS_IOLOCK_SHARED) { |
| 204 | if (!mrtryaccess(&ip->i_iolock)) |
| 205 | goto out; |
| 206 | } |
| 207 | if (lock_flags & XFS_ILOCK_EXCL) { |
| 208 | if (!mrtryupdate(&ip->i_lock)) |
| 209 | goto out_undo_iolock; |
| 210 | } else if (lock_flags & XFS_ILOCK_SHARED) { |
| 211 | if (!mrtryaccess(&ip->i_lock)) |
| 212 | goto out_undo_iolock; |
| 213 | } |
| 214 | return 1; |
| 215 | |
| 216 | out_undo_iolock: |
| 217 | if (lock_flags & XFS_IOLOCK_EXCL) |
| 218 | mrunlock_excl(&ip->i_iolock); |
| 219 | else if (lock_flags & XFS_IOLOCK_SHARED) |
| 220 | mrunlock_shared(&ip->i_iolock); |
| 221 | out: |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | /* |
| 226 | * xfs_iunlock() is used to drop the inode locks acquired with |
| 227 | * xfs_ilock() and xfs_ilock_nowait(). The caller must pass |
| 228 | * in the flags given to xfs_ilock() or xfs_ilock_nowait() so |
| 229 | * that we know which locks to drop. |
| 230 | * |
| 231 | * ip -- the inode being unlocked |
| 232 | * lock_flags -- this parameter indicates the inode's locks to be |
| 233 | * to be unlocked. See the comment for xfs_ilock() for a list |
| 234 | * of valid values for this parameter. |
| 235 | * |
| 236 | */ |
| 237 | void |
| 238 | xfs_iunlock( |
| 239 | xfs_inode_t *ip, |
| 240 | uint lock_flags) |
| 241 | { |
| 242 | /* |
| 243 | * You can't set both SHARED and EXCL for the same lock, |
| 244 | * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED, |
| 245 | * and XFS_ILOCK_EXCL are valid values to set in lock_flags. |
| 246 | */ |
| 247 | ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) != |
| 248 | (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)); |
| 249 | ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) != |
| 250 | (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)); |
| 251 | ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0); |
| 252 | ASSERT(lock_flags != 0); |
| 253 | |
| 254 | if (lock_flags & XFS_IOLOCK_EXCL) |
| 255 | mrunlock_excl(&ip->i_iolock); |
| 256 | else if (lock_flags & XFS_IOLOCK_SHARED) |
| 257 | mrunlock_shared(&ip->i_iolock); |
| 258 | |
| 259 | if (lock_flags & XFS_ILOCK_EXCL) |
| 260 | mrunlock_excl(&ip->i_lock); |
| 261 | else if (lock_flags & XFS_ILOCK_SHARED) |
| 262 | mrunlock_shared(&ip->i_lock); |
| 263 | |
| 264 | trace_xfs_iunlock(ip, lock_flags, _RET_IP_); |
| 265 | } |
| 266 | |
| 267 | /* |
| 268 | * give up write locks. the i/o lock cannot be held nested |
| 269 | * if it is being demoted. |
| 270 | */ |
| 271 | void |
| 272 | xfs_ilock_demote( |
| 273 | xfs_inode_t *ip, |
| 274 | uint lock_flags) |
| 275 | { |
| 276 | ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)); |
| 277 | ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0); |
| 278 | |
| 279 | if (lock_flags & XFS_ILOCK_EXCL) |
| 280 | mrdemote(&ip->i_lock); |
| 281 | if (lock_flags & XFS_IOLOCK_EXCL) |
| 282 | mrdemote(&ip->i_iolock); |
| 283 | |
| 284 | trace_xfs_ilock_demote(ip, lock_flags, _RET_IP_); |
| 285 | } |
| 286 | |
Dave Chinner | 742ae1e | 2013-04-30 21:39:34 +1000 | [diff] [blame] | 287 | #if defined(DEBUG) || defined(XFS_WARN) |
Dave Chinner | fa96aca | 2012-10-08 21:56:10 +1100 | [diff] [blame] | 288 | int |
| 289 | xfs_isilocked( |
| 290 | xfs_inode_t *ip, |
| 291 | uint lock_flags) |
| 292 | { |
| 293 | if (lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) { |
| 294 | if (!(lock_flags & XFS_ILOCK_SHARED)) |
| 295 | return !!ip->i_lock.mr_writer; |
| 296 | return rwsem_is_locked(&ip->i_lock.mr_lock); |
| 297 | } |
| 298 | |
| 299 | if (lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)) { |
| 300 | if (!(lock_flags & XFS_IOLOCK_SHARED)) |
| 301 | return !!ip->i_iolock.mr_writer; |
| 302 | return rwsem_is_locked(&ip->i_iolock.mr_lock); |
| 303 | } |
| 304 | |
| 305 | ASSERT(0); |
| 306 | return 0; |
| 307 | } |
| 308 | #endif |
| 309 | |
| 310 | void |
| 311 | __xfs_iflock( |
| 312 | struct xfs_inode *ip) |
| 313 | { |
| 314 | wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IFLOCK_BIT); |
| 315 | DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IFLOCK_BIT); |
| 316 | |
| 317 | do { |
| 318 | prepare_to_wait_exclusive(wq, &wait.wait, TASK_UNINTERRUPTIBLE); |
| 319 | if (xfs_isiflocked(ip)) |
| 320 | io_schedule(); |
| 321 | } while (!xfs_iflock_nowait(ip)); |
| 322 | |
| 323 | finish_wait(wq, &wait.wait); |
| 324 | } |
| 325 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | STATIC uint |
| 327 | _xfs_dic2xflags( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | __uint16_t di_flags) |
| 329 | { |
| 330 | uint flags = 0; |
| 331 | |
| 332 | if (di_flags & XFS_DIFLAG_ANY) { |
| 333 | if (di_flags & XFS_DIFLAG_REALTIME) |
| 334 | flags |= XFS_XFLAG_REALTIME; |
| 335 | if (di_flags & XFS_DIFLAG_PREALLOC) |
| 336 | flags |= XFS_XFLAG_PREALLOC; |
| 337 | if (di_flags & XFS_DIFLAG_IMMUTABLE) |
| 338 | flags |= XFS_XFLAG_IMMUTABLE; |
| 339 | if (di_flags & XFS_DIFLAG_APPEND) |
| 340 | flags |= XFS_XFLAG_APPEND; |
| 341 | if (di_flags & XFS_DIFLAG_SYNC) |
| 342 | flags |= XFS_XFLAG_SYNC; |
| 343 | if (di_flags & XFS_DIFLAG_NOATIME) |
| 344 | flags |= XFS_XFLAG_NOATIME; |
| 345 | if (di_flags & XFS_DIFLAG_NODUMP) |
| 346 | flags |= XFS_XFLAG_NODUMP; |
| 347 | if (di_flags & XFS_DIFLAG_RTINHERIT) |
| 348 | flags |= XFS_XFLAG_RTINHERIT; |
| 349 | if (di_flags & XFS_DIFLAG_PROJINHERIT) |
| 350 | flags |= XFS_XFLAG_PROJINHERIT; |
| 351 | if (di_flags & XFS_DIFLAG_NOSYMLINKS) |
| 352 | flags |= XFS_XFLAG_NOSYMLINKS; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 353 | if (di_flags & XFS_DIFLAG_EXTSIZE) |
| 354 | flags |= XFS_XFLAG_EXTSIZE; |
| 355 | if (di_flags & XFS_DIFLAG_EXTSZINHERIT) |
| 356 | flags |= XFS_XFLAG_EXTSZINHERIT; |
Barry Naujok | d3446ea | 2006-06-09 14:54:19 +1000 | [diff] [blame] | 357 | if (di_flags & XFS_DIFLAG_NODEFRAG) |
| 358 | flags |= XFS_XFLAG_NODEFRAG; |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 359 | if (di_flags & XFS_DIFLAG_FILESTREAM) |
| 360 | flags |= XFS_XFLAG_FILESTREAM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | return flags; |
| 364 | } |
| 365 | |
| 366 | uint |
| 367 | xfs_ip2xflags( |
| 368 | xfs_inode_t *ip) |
| 369 | { |
Christoph Hellwig | 347d1c0 | 2007-08-28 13:57:51 +1000 | [diff] [blame] | 370 | xfs_icdinode_t *dic = &ip->i_d; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | |
Nathan Scott | a916e2b | 2006-06-09 17:12:17 +1000 | [diff] [blame] | 372 | return _xfs_dic2xflags(dic->di_flags) | |
Christoph Hellwig | 45ba598 | 2007-12-07 14:07:20 +1100 | [diff] [blame] | 373 | (XFS_IFORK_Q(ip) ? XFS_XFLAG_HASATTR : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | uint |
| 377 | xfs_dic2xflags( |
Christoph Hellwig | 45ba598 | 2007-12-07 14:07:20 +1100 | [diff] [blame] | 378 | xfs_dinode_t *dip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | { |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 380 | return _xfs_dic2xflags(be16_to_cpu(dip->di_flags)) | |
Christoph Hellwig | 45ba598 | 2007-12-07 14:07:20 +1100 | [diff] [blame] | 381 | (XFS_DFORK_Q(dip) ? XFS_XFLAG_HASATTR : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | } |
| 383 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | * Allocate an inode on disk and return a copy of its in-core version. |
| 386 | * The in-core inode is locked exclusively. Set mode, nlink, and rdev |
| 387 | * appropriately within the inode. The uid and gid for the inode are |
| 388 | * set according to the contents of the given cred structure. |
| 389 | * |
| 390 | * Use xfs_dialloc() to allocate the on-disk inode. If xfs_dialloc() |
Carlos Maiolino | cd856db | 2012-10-20 11:08:19 -0300 | [diff] [blame] | 391 | * has a free inode available, call xfs_iget() to obtain the in-core |
| 392 | * version of the allocated inode. Finally, fill in the inode and |
| 393 | * log its initial contents. In this case, ialloc_context would be |
| 394 | * set to NULL. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | * |
Carlos Maiolino | cd856db | 2012-10-20 11:08:19 -0300 | [diff] [blame] | 396 | * If xfs_dialloc() does not have an available inode, it will replenish |
| 397 | * its supply by doing an allocation. Since we can only do one |
| 398 | * allocation within a transaction without deadlocks, we must commit |
| 399 | * the current transaction before returning the inode itself. |
| 400 | * In this case, therefore, we will set ialloc_context and return. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | * The caller should then commit the current transaction, start a new |
| 402 | * transaction, and call xfs_ialloc() again to actually get the inode. |
| 403 | * |
| 404 | * To ensure that some other process does not grab the inode that |
| 405 | * was allocated during the first call to xfs_ialloc(), this routine |
| 406 | * also returns the [locked] bp pointing to the head of the freelist |
| 407 | * as ialloc_context. The caller should hold this buffer across |
| 408 | * 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] | 409 | * |
| 410 | * If we are allocating quota inodes, we do not have a parent inode |
| 411 | * to attach to or associate with (i.e. pip == NULL) because they |
| 412 | * are not linked into the directory structure - they are attached |
| 413 | * directly to the superblock - and so have no parent. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | */ |
| 415 | int |
| 416 | xfs_ialloc( |
| 417 | xfs_trans_t *tp, |
| 418 | xfs_inode_t *pip, |
Al Viro | 576b1d6 | 2011-07-26 02:50:15 -0400 | [diff] [blame] | 419 | umode_t mode, |
Nathan Scott | 31b084a | 2005-05-05 13:25:00 -0700 | [diff] [blame] | 420 | xfs_nlink_t nlink, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | xfs_dev_t rdev, |
Arkadiusz Mi?kiewicz | 6743099 | 2010-09-26 06:10:18 +0000 | [diff] [blame] | 422 | prid_t prid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | int okalloc, |
| 424 | xfs_buf_t **ialloc_context, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | xfs_inode_t **ipp) |
| 426 | { |
Christoph Hellwig | 93848a9 | 2013-04-03 16:11:17 +1100 | [diff] [blame] | 427 | struct xfs_mount *mp = tp->t_mountp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | xfs_ino_t ino; |
| 429 | xfs_inode_t *ip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | uint flags; |
| 431 | int error; |
Christoph Hellwig | dff35fd | 2008-08-13 16:44:15 +1000 | [diff] [blame] | 432 | timespec_t tv; |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 433 | int filestreams = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | |
| 435 | /* |
| 436 | * Call the space management code to pick |
| 437 | * the on-disk inode to be allocated. |
| 438 | */ |
David Chinner | b11f94d | 2007-07-11 11:09:33 +1000 | [diff] [blame] | 439 | error = xfs_dialloc(tp, pip ? pip->i_ino : 0, mode, okalloc, |
Christoph Hellwig | 0835890 | 2012-07-04 10:54:47 -0400 | [diff] [blame] | 440 | ialloc_context, &ino); |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 441 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | return error; |
Christoph Hellwig | 0835890 | 2012-07-04 10:54:47 -0400 | [diff] [blame] | 443 | if (*ialloc_context || ino == NULLFSINO) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | *ipp = NULL; |
| 445 | return 0; |
| 446 | } |
| 447 | ASSERT(*ialloc_context == NULL); |
| 448 | |
| 449 | /* |
| 450 | * Get the in-core inode with the lock held exclusively. |
| 451 | * This is because we're setting fields here we need |
| 452 | * to prevent others from looking at until we're done. |
| 453 | */ |
Christoph Hellwig | 93848a9 | 2013-04-03 16:11:17 +1100 | [diff] [blame] | 454 | error = xfs_iget(mp, tp, ino, XFS_IGET_CREATE, |
Christoph Hellwig | ec3ba85 | 2011-02-13 13:26:42 +0000 | [diff] [blame] | 455 | XFS_ILOCK_EXCL, &ip); |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 456 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | ASSERT(ip != NULL); |
| 459 | |
Al Viro | 576b1d6 | 2011-07-26 02:50:15 -0400 | [diff] [blame] | 460 | ip->i_d.di_mode = mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | ip->i_d.di_onlink = 0; |
| 462 | ip->i_d.di_nlink = nlink; |
| 463 | ASSERT(ip->i_d.di_nlink == nlink); |
David Howells | 9e2b2dc | 2008-08-13 16:20:04 +0100 | [diff] [blame] | 464 | ip->i_d.di_uid = current_fsuid(); |
| 465 | ip->i_d.di_gid = current_fsgid(); |
Arkadiusz Mi?kiewicz | 6743099 | 2010-09-26 06:10:18 +0000 | [diff] [blame] | 466 | xfs_set_projid(ip, prid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad)); |
| 468 | |
| 469 | /* |
| 470 | * If the superblock version is up to where we support new format |
| 471 | * inodes and this is currently an old format inode, then change |
| 472 | * the inode version number now. This way we only do the conversion |
| 473 | * here rather than here and in the flush/logging code. |
| 474 | */ |
Christoph Hellwig | 93848a9 | 2013-04-03 16:11:17 +1100 | [diff] [blame] | 475 | if (xfs_sb_version_hasnlink(&mp->m_sb) && |
Christoph Hellwig | 51ce16d | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 476 | ip->i_d.di_version == 1) { |
| 477 | ip->i_d.di_version = 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | /* |
| 479 | * We've already zeroed the old link count, the projid field, |
| 480 | * and the pad field. |
| 481 | */ |
| 482 | } |
| 483 | |
| 484 | /* |
| 485 | * Project ids won't be stored on disk if we are using a version 1 inode. |
| 486 | */ |
Christoph Hellwig | 51ce16d | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 487 | if ((prid != 0) && (ip->i_d.di_version == 1)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | xfs_bump_ino_vers2(tp, ip); |
| 489 | |
Christoph Hellwig | bd186aa | 2007-08-30 17:21:12 +1000 | [diff] [blame] | 490 | if (pip && XFS_INHERIT_GID(pip)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | ip->i_d.di_gid = pip->i_d.di_gid; |
Al Viro | abbede1 | 2011-07-26 02:31:30 -0400 | [diff] [blame] | 492 | if ((pip->i_d.di_mode & S_ISGID) && S_ISDIR(mode)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | ip->i_d.di_mode |= S_ISGID; |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | /* |
| 498 | * If the group ID of the new file does not match the effective group |
| 499 | * ID or one of the supplementary group IDs, the S_ISGID bit is cleared |
| 500 | * (and only if the irix_sgid_inherit compatibility variable is set). |
| 501 | */ |
| 502 | if ((irix_sgid_inherit) && |
| 503 | (ip->i_d.di_mode & S_ISGID) && |
| 504 | (!in_group_p((gid_t)ip->i_d.di_gid))) { |
| 505 | ip->i_d.di_mode &= ~S_ISGID; |
| 506 | } |
| 507 | |
| 508 | ip->i_d.di_size = 0; |
| 509 | ip->i_d.di_nextents = 0; |
| 510 | ASSERT(ip->i_d.di_nblocks == 0); |
Christoph Hellwig | dff35fd | 2008-08-13 16:44:15 +1000 | [diff] [blame] | 511 | |
| 512 | nanotime(&tv); |
| 513 | ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec; |
| 514 | ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec; |
| 515 | ip->i_d.di_atime = ip->i_d.di_mtime; |
| 516 | ip->i_d.di_ctime = ip->i_d.di_mtime; |
| 517 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | /* |
| 519 | * di_gen will have been taken care of in xfs_iread. |
| 520 | */ |
| 521 | ip->i_d.di_extsize = 0; |
| 522 | ip->i_d.di_dmevmask = 0; |
| 523 | ip->i_d.di_dmstate = 0; |
| 524 | ip->i_d.di_flags = 0; |
Christoph Hellwig | 93848a9 | 2013-04-03 16:11:17 +1100 | [diff] [blame] | 525 | |
| 526 | if (ip->i_d.di_version == 3) { |
| 527 | ASSERT(ip->i_d.di_ino == ino); |
| 528 | ASSERT(uuid_equal(&ip->i_d.di_uuid, &mp->m_sb.sb_uuid)); |
| 529 | ip->i_d.di_crc = 0; |
| 530 | ip->i_d.di_changecount = 1; |
| 531 | ip->i_d.di_lsn = 0; |
| 532 | ip->i_d.di_flags2 = 0; |
| 533 | memset(&(ip->i_d.di_pad2[0]), 0, sizeof(ip->i_d.di_pad2)); |
| 534 | ip->i_d.di_crtime = ip->i_d.di_mtime; |
| 535 | } |
| 536 | |
| 537 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | flags = XFS_ILOG_CORE; |
| 539 | switch (mode & S_IFMT) { |
| 540 | case S_IFIFO: |
| 541 | case S_IFCHR: |
| 542 | case S_IFBLK: |
| 543 | case S_IFSOCK: |
| 544 | ip->i_d.di_format = XFS_DINODE_FMT_DEV; |
| 545 | ip->i_df.if_u2.if_rdev = rdev; |
| 546 | ip->i_df.if_flags = 0; |
| 547 | flags |= XFS_ILOG_DEV; |
| 548 | break; |
| 549 | case S_IFREG: |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 550 | /* |
| 551 | * we can't set up filestreams until after the VFS inode |
| 552 | * is set up properly. |
| 553 | */ |
| 554 | if (pip && xfs_inode_is_filestream(pip)) |
| 555 | filestreams = 1; |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 556 | /* fall through */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | case S_IFDIR: |
David Chinner | b11f94d | 2007-07-11 11:09:33 +1000 | [diff] [blame] | 558 | if (pip && (pip->i_d.di_flags & XFS_DIFLAG_ANY)) { |
Nathan Scott | 365ca83 | 2005-06-21 15:39:12 +1000 | [diff] [blame] | 559 | uint di_flags = 0; |
| 560 | |
Al Viro | abbede1 | 2011-07-26 02:31:30 -0400 | [diff] [blame] | 561 | if (S_ISDIR(mode)) { |
Nathan Scott | 365ca83 | 2005-06-21 15:39:12 +1000 | [diff] [blame] | 562 | if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT) |
| 563 | di_flags |= XFS_DIFLAG_RTINHERIT; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 564 | if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) { |
| 565 | di_flags |= XFS_DIFLAG_EXTSZINHERIT; |
| 566 | ip->i_d.di_extsize = pip->i_d.di_extsize; |
| 567 | } |
Al Viro | abbede1 | 2011-07-26 02:31:30 -0400 | [diff] [blame] | 568 | } else if (S_ISREG(mode)) { |
Christoph Hellwig | 613d704 | 2007-10-11 17:44:08 +1000 | [diff] [blame] | 569 | if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT) |
Nathan Scott | 365ca83 | 2005-06-21 15:39:12 +1000 | [diff] [blame] | 570 | di_flags |= XFS_DIFLAG_REALTIME; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 571 | if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) { |
| 572 | di_flags |= XFS_DIFLAG_EXTSIZE; |
| 573 | ip->i_d.di_extsize = pip->i_d.di_extsize; |
| 574 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | } |
| 576 | if ((pip->i_d.di_flags & XFS_DIFLAG_NOATIME) && |
| 577 | xfs_inherit_noatime) |
Nathan Scott | 365ca83 | 2005-06-21 15:39:12 +1000 | [diff] [blame] | 578 | di_flags |= XFS_DIFLAG_NOATIME; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | if ((pip->i_d.di_flags & XFS_DIFLAG_NODUMP) && |
| 580 | xfs_inherit_nodump) |
Nathan Scott | 365ca83 | 2005-06-21 15:39:12 +1000 | [diff] [blame] | 581 | di_flags |= XFS_DIFLAG_NODUMP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | if ((pip->i_d.di_flags & XFS_DIFLAG_SYNC) && |
| 583 | xfs_inherit_sync) |
Nathan Scott | 365ca83 | 2005-06-21 15:39:12 +1000 | [diff] [blame] | 584 | di_flags |= XFS_DIFLAG_SYNC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | if ((pip->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) && |
| 586 | xfs_inherit_nosymlinks) |
Nathan Scott | 365ca83 | 2005-06-21 15:39:12 +1000 | [diff] [blame] | 587 | di_flags |= XFS_DIFLAG_NOSYMLINKS; |
| 588 | if (pip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) |
| 589 | di_flags |= XFS_DIFLAG_PROJINHERIT; |
Barry Naujok | d3446ea | 2006-06-09 14:54:19 +1000 | [diff] [blame] | 590 | if ((pip->i_d.di_flags & XFS_DIFLAG_NODEFRAG) && |
| 591 | xfs_inherit_nodefrag) |
| 592 | di_flags |= XFS_DIFLAG_NODEFRAG; |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 593 | if (pip->i_d.di_flags & XFS_DIFLAG_FILESTREAM) |
| 594 | di_flags |= XFS_DIFLAG_FILESTREAM; |
Nathan Scott | 365ca83 | 2005-06-21 15:39:12 +1000 | [diff] [blame] | 595 | ip->i_d.di_flags |= di_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | } |
| 597 | /* FALLTHROUGH */ |
| 598 | case S_IFLNK: |
| 599 | ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS; |
| 600 | ip->i_df.if_flags = XFS_IFEXTENTS; |
| 601 | ip->i_df.if_bytes = ip->i_df.if_real_bytes = 0; |
| 602 | ip->i_df.if_u1.if_extents = NULL; |
| 603 | break; |
| 604 | default: |
| 605 | ASSERT(0); |
| 606 | } |
| 607 | /* |
| 608 | * Attribute fork settings for new inode. |
| 609 | */ |
| 610 | ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS; |
| 611 | ip->i_d.di_anextents = 0; |
| 612 | |
| 613 | /* |
| 614 | * Log the new values stuffed into the inode. |
| 615 | */ |
Christoph Hellwig | ddc3415 | 2011-09-19 15:00:54 +0000 | [diff] [blame] | 616 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | xfs_trans_log_inode(tp, ip, flags); |
| 618 | |
Nathan Scott | b83bd13 | 2006-06-09 16:48:30 +1000 | [diff] [blame] | 619 | /* 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] | 620 | xfs_setup_inode(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 622 | /* now we have set up the vfs inode we can associate the filestream */ |
| 623 | if (filestreams) { |
| 624 | error = xfs_filestream_associate(pip, ip); |
| 625 | if (error < 0) |
| 626 | return -error; |
| 627 | if (!error) |
| 628 | xfs_iflags_set(ip, XFS_IFILESTREAM); |
| 629 | } |
| 630 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | *ipp = ip; |
| 632 | return 0; |
| 633 | } |
| 634 | |
| 635 | /* |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 636 | * Free up the underlying blocks past new_size. The new size must be smaller |
| 637 | * than the current size. This routine can be used both for the attribute and |
| 638 | * 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] | 639 | * |
David Chinner | f648505 | 2008-04-17 16:50:04 +1000 | [diff] [blame] | 640 | * The transaction passed to this routine must have made a permanent log |
| 641 | * reservation of at least XFS_ITRUNCATE_LOG_RES. This routine may commit the |
| 642 | * given transaction and start new ones, so make sure everything involved in |
| 643 | * the transaction is tidy before calling here. Some transaction will be |
| 644 | * returned to the caller to be committed. The incoming transaction must |
| 645 | * already include the inode, and both inode locks must be held exclusively. |
| 646 | * The inode must also be "held" within the transaction. On return the inode |
| 647 | * will be "held" within the returned transaction. This routine does NOT |
| 648 | * require any disk space to be reserved for it within the transaction. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | * |
David Chinner | f648505 | 2008-04-17 16:50:04 +1000 | [diff] [blame] | 650 | * If we get an error, we must return with the inode locked and linked into the |
| 651 | * current transaction. This keeps things simple for the higher level code, |
| 652 | * because it always knows that the inode is locked and held in the transaction |
| 653 | * that returns to it whether errors occur or not. We don't mark the inode |
| 654 | * dirty on error so that transactions can be easily aborted if possible. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | */ |
| 656 | int |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 657 | xfs_itruncate_extents( |
| 658 | struct xfs_trans **tpp, |
| 659 | struct xfs_inode *ip, |
| 660 | int whichfork, |
| 661 | xfs_fsize_t new_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | { |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 663 | struct xfs_mount *mp = ip->i_mount; |
| 664 | struct xfs_trans *tp = *tpp; |
| 665 | struct xfs_trans *ntp; |
| 666 | xfs_bmap_free_t free_list; |
| 667 | xfs_fsblock_t first_block; |
| 668 | xfs_fileoff_t first_unmap_block; |
| 669 | xfs_fileoff_t last_block; |
| 670 | xfs_filblks_t unmap_len; |
| 671 | int committed; |
| 672 | int error = 0; |
| 673 | int done = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | |
Christoph Hellwig | 0b56185 | 2012-07-04 11:13:31 -0400 | [diff] [blame] | 675 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
| 676 | ASSERT(!atomic_read(&VFS_I(ip)->i_count) || |
| 677 | xfs_isilocked(ip, XFS_IOLOCK_EXCL)); |
Christoph Hellwig | ce7ae151 | 2011-12-18 20:00:11 +0000 | [diff] [blame] | 678 | ASSERT(new_size <= XFS_ISIZE(ip)); |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 679 | ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | ASSERT(ip->i_itemp != NULL); |
Christoph Hellwig | 898621d | 2010-06-24 11:36:58 +1000 | [diff] [blame] | 681 | ASSERT(ip->i_itemp->ili_lock_flags == 0); |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 682 | ASSERT(!XFS_NOT_DQATTACHED(mp, ip)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | |
Christoph Hellwig | 673e8e5 | 2011-12-18 20:00:04 +0000 | [diff] [blame] | 684 | trace_xfs_itruncate_extents_start(ip, new_size); |
| 685 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | /* |
| 687 | * Since it is possible for space to become allocated beyond |
| 688 | * the end of the file (in a crash where the space is allocated |
| 689 | * but the inode size is not yet updated), simply remove any |
| 690 | * blocks which show up between the new EOF and the maximum |
| 691 | * possible file size. If the first block to be removed is |
| 692 | * beyond the maximum file size (ie it is the same as last_block), |
| 693 | * then there is nothing to do. |
| 694 | */ |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 695 | 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] | 696 | last_block = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes); |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 697 | if (first_unmap_block == last_block) |
| 698 | return 0; |
| 699 | |
| 700 | ASSERT(first_unmap_block < last_block); |
| 701 | unmap_len = last_block - first_unmap_block + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | while (!done) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 703 | xfs_bmap_init(&free_list, &first_block); |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 704 | error = xfs_bunmapi(tp, ip, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 705 | first_unmap_block, unmap_len, |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 706 | xfs_bmapi_aflag(whichfork), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | XFS_ITRUNC_MAX_EXTENTS, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 708 | &first_block, &free_list, |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 709 | &done); |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 710 | if (error) |
| 711 | goto out_bmap_cancel; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | |
| 713 | /* |
| 714 | * Duplicate the transaction that has the permanent |
| 715 | * reservation and commit the old transaction. |
| 716 | */ |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 717 | error = xfs_bmap_finish(&tp, &free_list, &committed); |
Christoph Hellwig | 898621d | 2010-06-24 11:36:58 +1000 | [diff] [blame] | 718 | if (committed) |
Christoph Hellwig | ddc3415 | 2011-09-19 15:00:54 +0000 | [diff] [blame] | 719 | xfs_trans_ijoin(tp, ip, 0); |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 720 | if (error) |
| 721 | goto out_bmap_cancel; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | |
| 723 | if (committed) { |
| 724 | /* |
David Chinner | f648505 | 2008-04-17 16:50:04 +1000 | [diff] [blame] | 725 | * Mark the inode dirty so it will be logged and |
David Chinner | e5720ee | 2008-04-10 12:21:18 +1000 | [diff] [blame] | 726 | * moved forward in the log as part of every commit. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | */ |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 728 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | } |
David Chinner | f648505 | 2008-04-17 16:50:04 +1000 | [diff] [blame] | 730 | |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 731 | ntp = xfs_trans_dup(tp); |
| 732 | error = xfs_trans_commit(tp, 0); |
| 733 | tp = ntp; |
David Chinner | e5720ee | 2008-04-10 12:21:18 +1000 | [diff] [blame] | 734 | |
Christoph Hellwig | ddc3415 | 2011-09-19 15:00:54 +0000 | [diff] [blame] | 735 | xfs_trans_ijoin(tp, ip, 0); |
David Chinner | f648505 | 2008-04-17 16:50:04 +1000 | [diff] [blame] | 736 | |
Dave Chinner | cc09c0d | 2008-11-17 17:37:10 +1100 | [diff] [blame] | 737 | if (error) |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 738 | goto out; |
| 739 | |
Dave Chinner | cc09c0d | 2008-11-17 17:37:10 +1100 | [diff] [blame] | 740 | /* |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 741 | * Transaction commit worked ok so we can drop the extra ticket |
Dave Chinner | cc09c0d | 2008-11-17 17:37:10 +1100 | [diff] [blame] | 742 | * reference that we gained in xfs_trans_dup() |
| 743 | */ |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 744 | xfs_log_ticket_put(tp->t_ticket); |
| 745 | error = xfs_trans_reserve(tp, 0, |
David Chinner | f648505 | 2008-04-17 16:50:04 +1000 | [diff] [blame] | 746 | XFS_ITRUNCATE_LOG_RES(mp), 0, |
| 747 | XFS_TRANS_PERM_LOG_RES, |
| 748 | XFS_ITRUNCATE_LOG_COUNT); |
| 749 | if (error) |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 750 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | } |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 752 | |
Christoph Hellwig | 673e8e5 | 2011-12-18 20:00:04 +0000 | [diff] [blame] | 753 | /* |
| 754 | * Always re-log the inode so that our permanent transaction can keep |
| 755 | * on rolling it forward in the log. |
| 756 | */ |
| 757 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 758 | |
| 759 | trace_xfs_itruncate_extents_end(ip, new_size); |
| 760 | |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 761 | out: |
| 762 | *tpp = tp; |
| 763 | return error; |
| 764 | out_bmap_cancel: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | /* |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 766 | * If the bunmapi call encounters an error, return to the caller where |
| 767 | * the transaction can be properly aborted. We just need to make sure |
| 768 | * 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] | 769 | */ |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 770 | xfs_bmap_cancel(&free_list); |
| 771 | goto out; |
| 772 | } |
| 773 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | /* |
| 775 | * This is called when the inode's link count goes to 0. |
| 776 | * We place the on-disk inode on a list in the AGI. It |
| 777 | * will be pulled from this list when the inode is freed. |
| 778 | */ |
| 779 | int |
| 780 | xfs_iunlink( |
| 781 | xfs_trans_t *tp, |
| 782 | xfs_inode_t *ip) |
| 783 | { |
| 784 | xfs_mount_t *mp; |
| 785 | xfs_agi_t *agi; |
| 786 | xfs_dinode_t *dip; |
| 787 | xfs_buf_t *agibp; |
| 788 | xfs_buf_t *ibp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | xfs_agino_t agino; |
| 790 | short bucket_index; |
| 791 | int offset; |
| 792 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | |
| 794 | ASSERT(ip->i_d.di_nlink == 0); |
| 795 | ASSERT(ip->i_d.di_mode != 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | |
| 797 | mp = tp->t_mountp; |
| 798 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | /* |
| 800 | * Get the agi buffer first. It ensures lock ordering |
| 801 | * on the list. |
| 802 | */ |
Christoph Hellwig | 5e1be0f | 2008-11-28 14:23:37 +1100 | [diff] [blame] | 803 | 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] | 804 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | agi = XFS_BUF_TO_AGI(agibp); |
Christoph Hellwig | 5e1be0f | 2008-11-28 14:23:37 +1100 | [diff] [blame] | 807 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | /* |
| 809 | * Get the index into the agi hash table for the |
| 810 | * list this inode will go on. |
| 811 | */ |
| 812 | agino = XFS_INO_TO_AGINO(mp, ip->i_ino); |
| 813 | ASSERT(agino != 0); |
| 814 | bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS; |
| 815 | ASSERT(agi->agi_unlinked[bucket_index]); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 816 | ASSERT(be32_to_cpu(agi->agi_unlinked[bucket_index]) != agino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 818 | if (agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | /* |
| 820 | * There is already another inode in the bucket we need |
| 821 | * to add ourselves to. Add us at the front of the list. |
| 822 | * Here we put the head pointer into our next pointer, |
| 823 | * and then we fall through to point the head at us. |
| 824 | */ |
Christoph Hellwig | 475ee41 | 2012-07-03 12:21:22 -0400 | [diff] [blame] | 825 | error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp, |
| 826 | 0, 0); |
Vlad Apostolov | c319b58 | 2007-11-23 16:27:51 +1100 | [diff] [blame] | 827 | if (error) |
| 828 | return error; |
| 829 | |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 830 | ASSERT(dip->di_next_unlinked == cpu_to_be32(NULLAGINO)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | dip->di_next_unlinked = agi->agi_unlinked[bucket_index]; |
Christoph Hellwig | 92bfc6e | 2008-11-28 14:23:41 +1100 | [diff] [blame] | 832 | offset = ip->i_imap.im_boffset + |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | offsetof(xfs_dinode_t, di_next_unlinked); |
Dave Chinner | 0a32c26 | 2013-06-05 12:09:08 +1000 | [diff] [blame] | 834 | |
| 835 | /* need to recalc the inode CRC if appropriate */ |
| 836 | xfs_dinode_calc_crc(mp, dip); |
| 837 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | xfs_trans_inode_buf(tp, ibp); |
| 839 | xfs_trans_log_buf(tp, ibp, offset, |
| 840 | (offset + sizeof(xfs_agino_t) - 1)); |
| 841 | xfs_inobp_check(mp, ibp); |
| 842 | } |
| 843 | |
| 844 | /* |
| 845 | * Point the bucket head pointer at the inode being inserted. |
| 846 | */ |
| 847 | ASSERT(agino != 0); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 848 | agi->agi_unlinked[bucket_index] = cpu_to_be32(agino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | offset = offsetof(xfs_agi_t, agi_unlinked) + |
| 850 | (sizeof(xfs_agino_t) * bucket_index); |
| 851 | xfs_trans_log_buf(tp, agibp, offset, |
| 852 | (offset + sizeof(xfs_agino_t) - 1)); |
| 853 | return 0; |
| 854 | } |
| 855 | |
| 856 | /* |
| 857 | * Pull the on-disk inode from the AGI unlinked list. |
| 858 | */ |
| 859 | STATIC int |
| 860 | xfs_iunlink_remove( |
| 861 | xfs_trans_t *tp, |
| 862 | xfs_inode_t *ip) |
| 863 | { |
| 864 | xfs_ino_t next_ino; |
| 865 | xfs_mount_t *mp; |
| 866 | xfs_agi_t *agi; |
| 867 | xfs_dinode_t *dip; |
| 868 | xfs_buf_t *agibp; |
| 869 | xfs_buf_t *ibp; |
| 870 | xfs_agnumber_t agno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | xfs_agino_t agino; |
| 872 | xfs_agino_t next_agino; |
| 873 | xfs_buf_t *last_ibp; |
Nathan Scott | 6fdf8cc | 2006-06-28 10:13:52 +1000 | [diff] [blame] | 874 | xfs_dinode_t *last_dip = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | short bucket_index; |
Nathan Scott | 6fdf8cc | 2006-06-28 10:13:52 +1000 | [diff] [blame] | 876 | int offset, last_offset = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | mp = tp->t_mountp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | agno = XFS_INO_TO_AGNO(mp, ip->i_ino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | |
| 882 | /* |
| 883 | * Get the agi buffer first. It ensures lock ordering |
| 884 | * on the list. |
| 885 | */ |
Christoph Hellwig | 5e1be0f | 2008-11-28 14:23:37 +1100 | [diff] [blame] | 886 | error = xfs_read_agi(mp, tp, agno, &agibp); |
| 887 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | return error; |
Christoph Hellwig | 5e1be0f | 2008-11-28 14:23:37 +1100 | [diff] [blame] | 889 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | agi = XFS_BUF_TO_AGI(agibp); |
Christoph Hellwig | 5e1be0f | 2008-11-28 14:23:37 +1100 | [diff] [blame] | 891 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | /* |
| 893 | * Get the index into the agi hash table for the |
| 894 | * list this inode will go on. |
| 895 | */ |
| 896 | agino = XFS_INO_TO_AGINO(mp, ip->i_ino); |
| 897 | ASSERT(agino != 0); |
| 898 | bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS; |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 899 | ASSERT(agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | ASSERT(agi->agi_unlinked[bucket_index]); |
| 901 | |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 902 | if (be32_to_cpu(agi->agi_unlinked[bucket_index]) == agino) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | /* |
Christoph Hellwig | 475ee41 | 2012-07-03 12:21:22 -0400 | [diff] [blame] | 904 | * We're at the head of the list. Get the inode's on-disk |
| 905 | * buffer to see if there is anyone after us on the list. |
| 906 | * Only modify our next pointer if it is not already NULLAGINO. |
| 907 | * This saves us the overhead of dealing with the buffer when |
| 908 | * there is no need to change it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | */ |
Christoph Hellwig | 475ee41 | 2012-07-03 12:21:22 -0400 | [diff] [blame] | 910 | error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp, |
| 911 | 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | if (error) { |
Christoph Hellwig | 475ee41 | 2012-07-03 12:21:22 -0400 | [diff] [blame] | 913 | xfs_warn(mp, "%s: xfs_imap_to_bp returned error %d.", |
Dave Chinner | 0b932cc | 2011-03-07 10:08:35 +1100 | [diff] [blame] | 914 | __func__, error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | return error; |
| 916 | } |
Christoph Hellwig | 347d1c0 | 2007-08-28 13:57:51 +1000 | [diff] [blame] | 917 | next_agino = be32_to_cpu(dip->di_next_unlinked); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | ASSERT(next_agino != 0); |
| 919 | if (next_agino != NULLAGINO) { |
Christoph Hellwig | 347d1c0 | 2007-08-28 13:57:51 +1000 | [diff] [blame] | 920 | dip->di_next_unlinked = cpu_to_be32(NULLAGINO); |
Christoph Hellwig | 92bfc6e | 2008-11-28 14:23:41 +1100 | [diff] [blame] | 921 | offset = ip->i_imap.im_boffset + |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | offsetof(xfs_dinode_t, di_next_unlinked); |
Dave Chinner | 0a32c26 | 2013-06-05 12:09:08 +1000 | [diff] [blame] | 923 | |
| 924 | /* need to recalc the inode CRC if appropriate */ |
| 925 | xfs_dinode_calc_crc(mp, dip); |
| 926 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | xfs_trans_inode_buf(tp, ibp); |
| 928 | xfs_trans_log_buf(tp, ibp, offset, |
| 929 | (offset + sizeof(xfs_agino_t) - 1)); |
| 930 | xfs_inobp_check(mp, ibp); |
| 931 | } else { |
| 932 | xfs_trans_brelse(tp, ibp); |
| 933 | } |
| 934 | /* |
| 935 | * Point the bucket head pointer at the next inode. |
| 936 | */ |
| 937 | ASSERT(next_agino != 0); |
| 938 | ASSERT(next_agino != agino); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 939 | agi->agi_unlinked[bucket_index] = cpu_to_be32(next_agino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | offset = offsetof(xfs_agi_t, agi_unlinked) + |
| 941 | (sizeof(xfs_agino_t) * bucket_index); |
| 942 | xfs_trans_log_buf(tp, agibp, offset, |
| 943 | (offset + sizeof(xfs_agino_t) - 1)); |
| 944 | } else { |
| 945 | /* |
| 946 | * We need to search the list for the inode being freed. |
| 947 | */ |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 948 | next_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | last_ibp = NULL; |
| 950 | while (next_agino != agino) { |
Christoph Hellwig | 129dbc9 | 2012-07-03 12:21:51 -0400 | [diff] [blame] | 951 | struct xfs_imap imap; |
| 952 | |
| 953 | if (last_ibp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | xfs_trans_brelse(tp, last_ibp); |
Christoph Hellwig | 129dbc9 | 2012-07-03 12:21:51 -0400 | [diff] [blame] | 955 | |
| 956 | imap.im_blkno = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | next_ino = XFS_AGINO_TO_INO(mp, agno, next_agino); |
Christoph Hellwig | 129dbc9 | 2012-07-03 12:21:51 -0400 | [diff] [blame] | 958 | |
| 959 | error = xfs_imap(mp, tp, next_ino, &imap, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | if (error) { |
Dave Chinner | 0b932cc | 2011-03-07 10:08:35 +1100 | [diff] [blame] | 961 | xfs_warn(mp, |
Christoph Hellwig | 129dbc9 | 2012-07-03 12:21:51 -0400 | [diff] [blame] | 962 | "%s: xfs_imap returned error %d.", |
| 963 | __func__, error); |
| 964 | return error; |
| 965 | } |
| 966 | |
| 967 | error = xfs_imap_to_bp(mp, tp, &imap, &last_dip, |
| 968 | &last_ibp, 0, 0); |
| 969 | if (error) { |
| 970 | xfs_warn(mp, |
| 971 | "%s: xfs_imap_to_bp returned error %d.", |
Dave Chinner | 0b932cc | 2011-03-07 10:08:35 +1100 | [diff] [blame] | 972 | __func__, error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | return error; |
| 974 | } |
Christoph Hellwig | 129dbc9 | 2012-07-03 12:21:51 -0400 | [diff] [blame] | 975 | |
| 976 | last_offset = imap.im_boffset; |
Christoph Hellwig | 347d1c0 | 2007-08-28 13:57:51 +1000 | [diff] [blame] | 977 | next_agino = be32_to_cpu(last_dip->di_next_unlinked); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | ASSERT(next_agino != NULLAGINO); |
| 979 | ASSERT(next_agino != 0); |
| 980 | } |
Christoph Hellwig | 475ee41 | 2012-07-03 12:21:22 -0400 | [diff] [blame] | 981 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | /* |
Christoph Hellwig | 475ee41 | 2012-07-03 12:21:22 -0400 | [diff] [blame] | 983 | * Now last_ibp points to the buffer previous to us on the |
| 984 | * unlinked list. Pull us from the list. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | */ |
Christoph Hellwig | 475ee41 | 2012-07-03 12:21:22 -0400 | [diff] [blame] | 986 | error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp, |
| 987 | 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | if (error) { |
Christoph Hellwig | 475ee41 | 2012-07-03 12:21:22 -0400 | [diff] [blame] | 989 | xfs_warn(mp, "%s: xfs_imap_to_bp(2) returned error %d.", |
Dave Chinner | 0b932cc | 2011-03-07 10:08:35 +1100 | [diff] [blame] | 990 | __func__, error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | return error; |
| 992 | } |
Christoph Hellwig | 347d1c0 | 2007-08-28 13:57:51 +1000 | [diff] [blame] | 993 | next_agino = be32_to_cpu(dip->di_next_unlinked); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | ASSERT(next_agino != 0); |
| 995 | ASSERT(next_agino != agino); |
| 996 | if (next_agino != NULLAGINO) { |
Christoph Hellwig | 347d1c0 | 2007-08-28 13:57:51 +1000 | [diff] [blame] | 997 | dip->di_next_unlinked = cpu_to_be32(NULLAGINO); |
Christoph Hellwig | 92bfc6e | 2008-11-28 14:23:41 +1100 | [diff] [blame] | 998 | offset = ip->i_imap.im_boffset + |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | offsetof(xfs_dinode_t, di_next_unlinked); |
Dave Chinner | 0a32c26 | 2013-06-05 12:09:08 +1000 | [diff] [blame] | 1000 | |
| 1001 | /* need to recalc the inode CRC if appropriate */ |
| 1002 | xfs_dinode_calc_crc(mp, dip); |
| 1003 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | xfs_trans_inode_buf(tp, ibp); |
| 1005 | xfs_trans_log_buf(tp, ibp, offset, |
| 1006 | (offset + sizeof(xfs_agino_t) - 1)); |
| 1007 | xfs_inobp_check(mp, ibp); |
| 1008 | } else { |
| 1009 | xfs_trans_brelse(tp, ibp); |
| 1010 | } |
| 1011 | /* |
| 1012 | * Point the previous inode on the list to the next inode. |
| 1013 | */ |
Christoph Hellwig | 347d1c0 | 2007-08-28 13:57:51 +1000 | [diff] [blame] | 1014 | last_dip->di_next_unlinked = cpu_to_be32(next_agino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | ASSERT(next_agino != 0); |
| 1016 | offset = last_offset + offsetof(xfs_dinode_t, di_next_unlinked); |
Dave Chinner | 0a32c26 | 2013-06-05 12:09:08 +1000 | [diff] [blame] | 1017 | |
| 1018 | /* need to recalc the inode CRC if appropriate */ |
| 1019 | xfs_dinode_calc_crc(mp, last_dip); |
| 1020 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | xfs_trans_inode_buf(tp, last_ibp); |
| 1022 | xfs_trans_log_buf(tp, last_ibp, offset, |
| 1023 | (offset + sizeof(xfs_agino_t) - 1)); |
| 1024 | xfs_inobp_check(mp, last_ibp); |
| 1025 | } |
| 1026 | return 0; |
| 1027 | } |
| 1028 | |
Dave Chinner | 5b3eed7 | 2010-08-24 11:42:41 +1000 | [diff] [blame] | 1029 | /* |
| 1030 | * A big issue when freeing the inode cluster is is that we _cannot_ skip any |
| 1031 | * inodes that are in memory - they all must be marked stale and attached to |
| 1032 | * the cluster buffer. |
| 1033 | */ |
Chandra Seetharaman | 2a30f36d | 2011-09-20 13:56:55 +0000 | [diff] [blame] | 1034 | STATIC int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | xfs_ifree_cluster( |
| 1036 | xfs_inode_t *free_ip, |
| 1037 | xfs_trans_t *tp, |
| 1038 | xfs_ino_t inum) |
| 1039 | { |
| 1040 | xfs_mount_t *mp = free_ip->i_mount; |
| 1041 | int blks_per_cluster; |
| 1042 | int nbufs; |
| 1043 | int ninodes; |
Dave Chinner | 5b257b4 | 2010-06-03 16:22:29 +1000 | [diff] [blame] | 1044 | int i, j; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | xfs_daddr_t blkno; |
| 1046 | xfs_buf_t *bp; |
Dave Chinner | 5b257b4 | 2010-06-03 16:22:29 +1000 | [diff] [blame] | 1047 | xfs_inode_t *ip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | xfs_inode_log_item_t *iip; |
| 1049 | xfs_log_item_t *lip; |
Dave Chinner | 5017e97 | 2010-01-11 11:47:40 +0000 | [diff] [blame] | 1050 | struct xfs_perag *pag; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | |
Dave Chinner | 5017e97 | 2010-01-11 11:47:40 +0000 | [diff] [blame] | 1052 | pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, inum)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | if (mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) { |
| 1054 | blks_per_cluster = 1; |
| 1055 | ninodes = mp->m_sb.sb_inopblock; |
| 1056 | nbufs = XFS_IALLOC_BLOCKS(mp); |
| 1057 | } else { |
| 1058 | blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) / |
| 1059 | mp->m_sb.sb_blocksize; |
| 1060 | ninodes = blks_per_cluster * mp->m_sb.sb_inopblock; |
| 1061 | nbufs = XFS_IALLOC_BLOCKS(mp) / blks_per_cluster; |
| 1062 | } |
| 1063 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | for (j = 0; j < nbufs; j++, inum += ninodes) { |
| 1065 | blkno = XFS_AGB_TO_DADDR(mp, XFS_INO_TO_AGNO(mp, inum), |
| 1066 | XFS_INO_TO_AGBNO(mp, inum)); |
| 1067 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | /* |
Dave Chinner | 5b257b4 | 2010-06-03 16:22:29 +1000 | [diff] [blame] | 1069 | * We obtain and lock the backing buffer first in the process |
| 1070 | * here, as we have to ensure that any dirty inode that we |
| 1071 | * can't get the flush lock on is attached to the buffer. |
| 1072 | * If we scan the in-memory inodes first, then buffer IO can |
| 1073 | * complete before we get a lock on it, and hence we may fail |
| 1074 | * to mark all the active inodes on the buffer stale. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | */ |
Dave Chinner | 5b257b4 | 2010-06-03 16:22:29 +1000 | [diff] [blame] | 1076 | bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno, |
Dave Chinner | b6aff29 | 2012-11-02 11:38:42 +1100 | [diff] [blame] | 1077 | mp->m_bsize * blks_per_cluster, |
| 1078 | XBF_UNMAPPED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | |
Chandra Seetharaman | 2a30f36d | 2011-09-20 13:56:55 +0000 | [diff] [blame] | 1080 | if (!bp) |
| 1081 | return ENOMEM; |
Dave Chinner | b0f539d | 2012-11-14 17:53:49 +1100 | [diff] [blame] | 1082 | |
| 1083 | /* |
| 1084 | * This buffer may not have been correctly initialised as we |
| 1085 | * didn't read it from disk. That's not important because we are |
| 1086 | * only using to mark the buffer as stale in the log, and to |
| 1087 | * attach stale cached inodes on it. That means it will never be |
| 1088 | * dispatched for IO. If it is, we want to know about it, and we |
| 1089 | * want it to fail. We can acheive this by adding a write |
| 1090 | * verifier to the buffer. |
| 1091 | */ |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 1092 | bp->b_ops = &xfs_inode_buf_ops; |
Dave Chinner | b0f539d | 2012-11-14 17:53:49 +1100 | [diff] [blame] | 1093 | |
Dave Chinner | 5b257b4 | 2010-06-03 16:22:29 +1000 | [diff] [blame] | 1094 | /* |
| 1095 | * Walk the inodes already attached to the buffer and mark them |
| 1096 | * stale. These will all have the flush locks held, so an |
Dave Chinner | 5b3eed7 | 2010-08-24 11:42:41 +1000 | [diff] [blame] | 1097 | * in-memory inode walk can't lock them. By marking them all |
| 1098 | * stale first, we will not attempt to lock them in the loop |
| 1099 | * below as the XFS_ISTALE flag will be set. |
Dave Chinner | 5b257b4 | 2010-06-03 16:22:29 +1000 | [diff] [blame] | 1100 | */ |
Christoph Hellwig | adadbee | 2011-07-13 13:43:49 +0200 | [diff] [blame] | 1101 | lip = bp->b_fspriv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | while (lip) { |
| 1103 | if (lip->li_type == XFS_LI_INODE) { |
| 1104 | iip = (xfs_inode_log_item_t *)lip; |
| 1105 | ASSERT(iip->ili_logged == 1); |
Christoph Hellwig | ca30b2a | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 1106 | lip->li_cb = xfs_istale_done; |
David Chinner | 7b2e2a3 | 2008-10-30 17:39:12 +1100 | [diff] [blame] | 1107 | xfs_trans_ail_copy_lsn(mp->m_ail, |
| 1108 | &iip->ili_flush_lsn, |
| 1109 | &iip->ili_item.li_lsn); |
David Chinner | e5ffd2b | 2006-11-21 18:55:33 +1100 | [diff] [blame] | 1110 | xfs_iflags_set(iip->ili_inode, XFS_ISTALE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | } |
| 1112 | lip = lip->li_bio_list; |
| 1113 | } |
| 1114 | |
Dave Chinner | 5b3eed7 | 2010-08-24 11:42:41 +1000 | [diff] [blame] | 1115 | |
Dave Chinner | 5b257b4 | 2010-06-03 16:22:29 +1000 | [diff] [blame] | 1116 | /* |
| 1117 | * For each inode in memory attempt to add it to the inode |
| 1118 | * buffer and set it up for being staled on buffer IO |
| 1119 | * completion. This is safe as we've locked out tail pushing |
| 1120 | * and flushing by locking the buffer. |
| 1121 | * |
| 1122 | * We have already marked every inode that was part of a |
| 1123 | * transaction stale above, which means there is no point in |
| 1124 | * even trying to lock them. |
| 1125 | */ |
| 1126 | for (i = 0; i < ninodes; i++) { |
Dave Chinner | 5b3eed7 | 2010-08-24 11:42:41 +1000 | [diff] [blame] | 1127 | retry: |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 1128 | rcu_read_lock(); |
Dave Chinner | 5b257b4 | 2010-06-03 16:22:29 +1000 | [diff] [blame] | 1129 | ip = radix_tree_lookup(&pag->pag_ici_root, |
| 1130 | XFS_INO_TO_AGINO(mp, (inum + i))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 | |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 1132 | /* Inode not in memory, nothing to do */ |
| 1133 | if (!ip) { |
| 1134 | rcu_read_unlock(); |
Dave Chinner | 5b257b4 | 2010-06-03 16:22:29 +1000 | [diff] [blame] | 1135 | continue; |
| 1136 | } |
| 1137 | |
Dave Chinner | 5b3eed7 | 2010-08-24 11:42:41 +1000 | [diff] [blame] | 1138 | /* |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 1139 | * because this is an RCU protected lookup, we could |
| 1140 | * find a recently freed or even reallocated inode |
| 1141 | * during the lookup. We need to check under the |
| 1142 | * i_flags_lock for a valid inode here. Skip it if it |
| 1143 | * is not valid, the wrong inode or stale. |
| 1144 | */ |
| 1145 | spin_lock(&ip->i_flags_lock); |
| 1146 | if (ip->i_ino != inum + i || |
| 1147 | __xfs_iflags_test(ip, XFS_ISTALE)) { |
| 1148 | spin_unlock(&ip->i_flags_lock); |
| 1149 | rcu_read_unlock(); |
| 1150 | continue; |
| 1151 | } |
| 1152 | spin_unlock(&ip->i_flags_lock); |
| 1153 | |
| 1154 | /* |
Dave Chinner | 5b3eed7 | 2010-08-24 11:42:41 +1000 | [diff] [blame] | 1155 | * Don't try to lock/unlock the current inode, but we |
| 1156 | * _cannot_ skip the other inodes that we did not find |
| 1157 | * in the list attached to the buffer and are not |
| 1158 | * already marked stale. If we can't lock it, back off |
| 1159 | * and retry. |
| 1160 | */ |
Dave Chinner | 5b257b4 | 2010-06-03 16:22:29 +1000 | [diff] [blame] | 1161 | if (ip != free_ip && |
| 1162 | !xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) { |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 1163 | rcu_read_unlock(); |
Dave Chinner | 5b3eed7 | 2010-08-24 11:42:41 +1000 | [diff] [blame] | 1164 | delay(1); |
| 1165 | goto retry; |
Dave Chinner | 5b257b4 | 2010-06-03 16:22:29 +1000 | [diff] [blame] | 1166 | } |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 1167 | rcu_read_unlock(); |
Dave Chinner | 5b257b4 | 2010-06-03 16:22:29 +1000 | [diff] [blame] | 1168 | |
Dave Chinner | 5b3eed7 | 2010-08-24 11:42:41 +1000 | [diff] [blame] | 1169 | xfs_iflock(ip); |
Dave Chinner | 5b257b4 | 2010-06-03 16:22:29 +1000 | [diff] [blame] | 1170 | xfs_iflags_set(ip, XFS_ISTALE); |
Dave Chinner | 5b257b4 | 2010-06-03 16:22:29 +1000 | [diff] [blame] | 1171 | |
Dave Chinner | 5b3eed7 | 2010-08-24 11:42:41 +1000 | [diff] [blame] | 1172 | /* |
| 1173 | * we don't need to attach clean inodes or those only |
| 1174 | * with unlogged changes (which we throw away, anyway). |
| 1175 | */ |
Dave Chinner | 5b257b4 | 2010-06-03 16:22:29 +1000 | [diff] [blame] | 1176 | iip = ip->i_itemp; |
Dave Chinner | 5b3eed7 | 2010-08-24 11:42:41 +1000 | [diff] [blame] | 1177 | if (!iip || xfs_inode_clean(ip)) { |
Dave Chinner | 5b257b4 | 2010-06-03 16:22:29 +1000 | [diff] [blame] | 1178 | ASSERT(ip != free_ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1179 | xfs_ifunlock(ip); |
| 1180 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 1181 | continue; |
| 1182 | } |
| 1183 | |
Christoph Hellwig | f5d8d5c | 2012-02-29 09:53:54 +0000 | [diff] [blame] | 1184 | iip->ili_last_fields = iip->ili_fields; |
| 1185 | iip->ili_fields = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | iip->ili_logged = 1; |
David Chinner | 7b2e2a3 | 2008-10-30 17:39:12 +1100 | [diff] [blame] | 1187 | xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn, |
| 1188 | &iip->ili_item.li_lsn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | |
Christoph Hellwig | ca30b2a | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 1190 | xfs_buf_attach_iodone(bp, xfs_istale_done, |
| 1191 | &iip->ili_item); |
Dave Chinner | 5b257b4 | 2010-06-03 16:22:29 +1000 | [diff] [blame] | 1192 | |
| 1193 | if (ip != free_ip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | } |
| 1196 | |
Dave Chinner | 5b3eed7 | 2010-08-24 11:42:41 +1000 | [diff] [blame] | 1197 | xfs_trans_stale_inode_buf(tp, bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1198 | xfs_trans_binval(tp, bp); |
| 1199 | } |
| 1200 | |
Dave Chinner | 5017e97 | 2010-01-11 11:47:40 +0000 | [diff] [blame] | 1201 | xfs_perag_put(pag); |
Chandra Seetharaman | 2a30f36d | 2011-09-20 13:56:55 +0000 | [diff] [blame] | 1202 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1203 | } |
| 1204 | |
| 1205 | /* |
| 1206 | * This is called to return an inode to the inode free list. |
| 1207 | * The inode should already be truncated to 0 length and have |
| 1208 | * no pages associated with it. This routine also assumes that |
| 1209 | * the inode is already a part of the transaction. |
| 1210 | * |
| 1211 | * The on-disk copy of the inode will have been added to the list |
| 1212 | * of unlinked inodes in the AGI. We need to remove the inode from |
| 1213 | * that list atomically with respect to freeing it here. |
| 1214 | */ |
| 1215 | int |
| 1216 | xfs_ifree( |
| 1217 | xfs_trans_t *tp, |
| 1218 | xfs_inode_t *ip, |
| 1219 | xfs_bmap_free_t *flist) |
| 1220 | { |
| 1221 | int error; |
| 1222 | int delete; |
| 1223 | xfs_ino_t first_ino; |
| 1224 | |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 1225 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1226 | ASSERT(ip->i_d.di_nlink == 0); |
| 1227 | ASSERT(ip->i_d.di_nextents == 0); |
| 1228 | ASSERT(ip->i_d.di_anextents == 0); |
Christoph Hellwig | ce7ae151 | 2011-12-18 20:00:11 +0000 | [diff] [blame] | 1229 | 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] | 1230 | ASSERT(ip->i_d.di_nblocks == 0); |
| 1231 | |
| 1232 | /* |
| 1233 | * Pull the on-disk inode from the AGI unlinked list. |
| 1234 | */ |
| 1235 | error = xfs_iunlink_remove(tp, ip); |
Dave Chinner | 1baaed8 | 2013-06-27 16:04:50 +1000 | [diff] [blame] | 1236 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1237 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | |
| 1239 | error = xfs_difree(tp, ip->i_ino, flist, &delete, &first_ino); |
Dave Chinner | 1baaed8 | 2013-06-27 16:04:50 +1000 | [diff] [blame] | 1240 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | return error; |
Dave Chinner | 1baaed8 | 2013-06-27 16:04:50 +1000 | [diff] [blame] | 1242 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1243 | ip->i_d.di_mode = 0; /* mark incore inode as free */ |
| 1244 | ip->i_d.di_flags = 0; |
| 1245 | ip->i_d.di_dmevmask = 0; |
| 1246 | 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] | 1247 | ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS; |
| 1248 | ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS; |
| 1249 | /* |
| 1250 | * Bump the generation count so no one will be confused |
| 1251 | * by reincarnations of this inode. |
| 1252 | */ |
| 1253 | ip->i_d.di_gen++; |
| 1254 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 1255 | |
Dave Chinner | 1baaed8 | 2013-06-27 16:04:50 +1000 | [diff] [blame] | 1256 | if (delete) |
Chandra Seetharaman | 2a30f36d | 2011-09-20 13:56:55 +0000 | [diff] [blame] | 1257 | error = xfs_ifree_cluster(ip, tp, first_ino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | |
Chandra Seetharaman | 2a30f36d | 2011-09-20 13:56:55 +0000 | [diff] [blame] | 1259 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1260 | } |
| 1261 | |
| 1262 | /* |
Christoph Hellwig | 60ec6783 | 2010-02-17 19:43:56 +0000 | [diff] [blame] | 1263 | * This is called to unpin an inode. The caller must have the inode locked |
| 1264 | * in at least shared mode so that the buffer cannot be subsequently pinned |
| 1265 | * once someone is waiting for it to be unpinned. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1266 | */ |
Christoph Hellwig | 60ec6783 | 2010-02-17 19:43:56 +0000 | [diff] [blame] | 1267 | static void |
Christoph Hellwig | f392e63 | 2011-12-18 20:00:10 +0000 | [diff] [blame] | 1268 | xfs_iunpin( |
Christoph Hellwig | 60ec6783 | 2010-02-17 19:43:56 +0000 | [diff] [blame] | 1269 | struct xfs_inode *ip) |
David Chinner | a3f74ff | 2008-03-06 13:43:42 +1100 | [diff] [blame] | 1270 | { |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 1271 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)); |
David Chinner | a3f74ff | 2008-03-06 13:43:42 +1100 | [diff] [blame] | 1272 | |
Dave Chinner | 4aaf15d | 2010-03-08 11:24:07 +1100 | [diff] [blame] | 1273 | trace_xfs_inode_unpin_nowait(ip, _RET_IP_); |
| 1274 | |
David Chinner | a3f74ff | 2008-03-06 13:43:42 +1100 | [diff] [blame] | 1275 | /* Give the log a push to start the unpinning I/O */ |
Christoph Hellwig | 60ec6783 | 2010-02-17 19:43:56 +0000 | [diff] [blame] | 1276 | 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] | 1277 | |
David Chinner | a3f74ff | 2008-03-06 13:43:42 +1100 | [diff] [blame] | 1278 | } |
| 1279 | |
Christoph Hellwig | f392e63 | 2011-12-18 20:00:10 +0000 | [diff] [blame] | 1280 | static void |
| 1281 | __xfs_iunpin_wait( |
| 1282 | struct xfs_inode *ip) |
| 1283 | { |
| 1284 | wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IPINNED_BIT); |
| 1285 | DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IPINNED_BIT); |
| 1286 | |
| 1287 | xfs_iunpin(ip); |
| 1288 | |
| 1289 | do { |
| 1290 | prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE); |
| 1291 | if (xfs_ipincount(ip)) |
| 1292 | io_schedule(); |
| 1293 | } while (xfs_ipincount(ip)); |
| 1294 | finish_wait(wq, &wait.wait); |
| 1295 | } |
| 1296 | |
Dave Chinner | 777df5a | 2010-02-06 12:37:26 +1100 | [diff] [blame] | 1297 | void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1298 | xfs_iunpin_wait( |
Christoph Hellwig | 60ec6783 | 2010-02-17 19:43:56 +0000 | [diff] [blame] | 1299 | struct xfs_inode *ip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | { |
Christoph Hellwig | f392e63 | 2011-12-18 20:00:10 +0000 | [diff] [blame] | 1301 | if (xfs_ipincount(ip)) |
| 1302 | __xfs_iunpin_wait(ip); |
David Chinner | a3f74ff | 2008-03-06 13:43:42 +1100 | [diff] [blame] | 1303 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 1305 | STATIC int |
| 1306 | xfs_iflush_cluster( |
| 1307 | xfs_inode_t *ip, |
| 1308 | xfs_buf_t *bp) |
| 1309 | { |
| 1310 | xfs_mount_t *mp = ip->i_mount; |
Dave Chinner | 5017e97 | 2010-01-11 11:47:40 +0000 | [diff] [blame] | 1311 | struct xfs_perag *pag; |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 1312 | unsigned long first_index, mask; |
David Chinner | c8f5f12 | 2008-05-20 11:30:15 +1000 | [diff] [blame] | 1313 | unsigned long inodes_per_cluster; |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 1314 | int ilist_size; |
| 1315 | xfs_inode_t **ilist; |
| 1316 | xfs_inode_t *iq; |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 1317 | int nr_found; |
| 1318 | int clcount = 0; |
| 1319 | int bufwasdelwri; |
| 1320 | int i; |
| 1321 | |
Dave Chinner | 5017e97 | 2010-01-11 11:47:40 +0000 | [diff] [blame] | 1322 | 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] | 1323 | |
David Chinner | c8f5f12 | 2008-05-20 11:30:15 +1000 | [diff] [blame] | 1324 | inodes_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog; |
| 1325 | ilist_size = inodes_per_cluster * sizeof(xfs_inode_t *); |
David Chinner | 49383b0 | 2008-05-19 16:29:34 +1000 | [diff] [blame] | 1326 | ilist = kmem_alloc(ilist_size, KM_MAYFAIL|KM_NOFS); |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 1327 | if (!ilist) |
Dave Chinner | 44b56e0 | 2010-01-11 11:47:43 +0000 | [diff] [blame] | 1328 | goto out_put; |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 1329 | |
| 1330 | mask = ~(((XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog)) - 1); |
| 1331 | first_index = XFS_INO_TO_AGINO(mp, ip->i_ino) & mask; |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 1332 | rcu_read_lock(); |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 1333 | /* really need a gang lookup range call here */ |
| 1334 | nr_found = radix_tree_gang_lookup(&pag->pag_ici_root, (void**)ilist, |
David Chinner | c8f5f12 | 2008-05-20 11:30:15 +1000 | [diff] [blame] | 1335 | first_index, inodes_per_cluster); |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 1336 | if (nr_found == 0) |
| 1337 | goto out_free; |
| 1338 | |
| 1339 | for (i = 0; i < nr_found; i++) { |
| 1340 | iq = ilist[i]; |
| 1341 | if (iq == ip) |
| 1342 | continue; |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 1343 | |
| 1344 | /* |
| 1345 | * because this is an RCU protected lookup, we could find a |
| 1346 | * recently freed or even reallocated inode during the lookup. |
| 1347 | * We need to check under the i_flags_lock for a valid inode |
| 1348 | * here. Skip it if it is not valid or the wrong inode. |
| 1349 | */ |
| 1350 | spin_lock(&ip->i_flags_lock); |
| 1351 | if (!ip->i_ino || |
| 1352 | (XFS_INO_TO_AGINO(mp, iq->i_ino) & mask) != first_index) { |
| 1353 | spin_unlock(&ip->i_flags_lock); |
| 1354 | continue; |
| 1355 | } |
| 1356 | spin_unlock(&ip->i_flags_lock); |
| 1357 | |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 1358 | /* |
| 1359 | * Do an un-protected check to see if the inode is dirty and |
| 1360 | * is a candidate for flushing. These checks will be repeated |
| 1361 | * later after the appropriate locks are acquired. |
| 1362 | */ |
David Chinner | 3354040 | 2008-03-06 13:43:59 +1100 | [diff] [blame] | 1363 | if (xfs_inode_clean(iq) && xfs_ipincount(iq) == 0) |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 1364 | continue; |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 1365 | |
| 1366 | /* |
| 1367 | * Try to get locks. If any are unavailable or it is pinned, |
| 1368 | * then this inode cannot be flushed and is skipped. |
| 1369 | */ |
| 1370 | |
| 1371 | if (!xfs_ilock_nowait(iq, XFS_ILOCK_SHARED)) |
| 1372 | continue; |
| 1373 | if (!xfs_iflock_nowait(iq)) { |
| 1374 | xfs_iunlock(iq, XFS_ILOCK_SHARED); |
| 1375 | continue; |
| 1376 | } |
| 1377 | if (xfs_ipincount(iq)) { |
| 1378 | xfs_ifunlock(iq); |
| 1379 | xfs_iunlock(iq, XFS_ILOCK_SHARED); |
| 1380 | continue; |
| 1381 | } |
| 1382 | |
| 1383 | /* |
| 1384 | * arriving here means that this inode can be flushed. First |
| 1385 | * re-check that it's dirty before flushing. |
| 1386 | */ |
David Chinner | 3354040 | 2008-03-06 13:43:59 +1100 | [diff] [blame] | 1387 | if (!xfs_inode_clean(iq)) { |
| 1388 | int error; |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 1389 | error = xfs_iflush_int(iq, bp); |
| 1390 | if (error) { |
| 1391 | xfs_iunlock(iq, XFS_ILOCK_SHARED); |
| 1392 | goto cluster_corrupt_out; |
| 1393 | } |
| 1394 | clcount++; |
| 1395 | } else { |
| 1396 | xfs_ifunlock(iq); |
| 1397 | } |
| 1398 | xfs_iunlock(iq, XFS_ILOCK_SHARED); |
| 1399 | } |
| 1400 | |
| 1401 | if (clcount) { |
| 1402 | XFS_STATS_INC(xs_icluster_flushcnt); |
| 1403 | XFS_STATS_ADD(xs_icluster_flushinode, clcount); |
| 1404 | } |
| 1405 | |
| 1406 | out_free: |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 1407 | rcu_read_unlock(); |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 1408 | kmem_free(ilist); |
Dave Chinner | 44b56e0 | 2010-01-11 11:47:43 +0000 | [diff] [blame] | 1409 | out_put: |
| 1410 | xfs_perag_put(pag); |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 1411 | return 0; |
| 1412 | |
| 1413 | |
| 1414 | cluster_corrupt_out: |
| 1415 | /* |
| 1416 | * Corruption detected in the clustering loop. Invalidate the |
| 1417 | * inode buffer and shut down the filesystem. |
| 1418 | */ |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 1419 | rcu_read_unlock(); |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 1420 | /* |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 1421 | * Clean up the buffer. If it was delwri, just release it -- |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 1422 | * brelse can handle it with no problems. If not, shut down the |
| 1423 | * filesystem before releasing the buffer. |
| 1424 | */ |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 1425 | bufwasdelwri = (bp->b_flags & _XBF_DELWRI_Q); |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 1426 | if (bufwasdelwri) |
| 1427 | xfs_buf_relse(bp); |
| 1428 | |
| 1429 | xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); |
| 1430 | |
| 1431 | if (!bufwasdelwri) { |
| 1432 | /* |
| 1433 | * Just like incore_relse: if we have b_iodone functions, |
| 1434 | * mark the buffer as an error and call them. Otherwise |
| 1435 | * mark it as stale and brelse. |
| 1436 | */ |
Christoph Hellwig | cb669ca | 2011-07-13 13:43:49 +0200 | [diff] [blame] | 1437 | if (bp->b_iodone) { |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 1438 | XFS_BUF_UNDONE(bp); |
Christoph Hellwig | c867cb6 | 2011-10-10 16:52:46 +0000 | [diff] [blame] | 1439 | xfs_buf_stale(bp); |
Chandra Seetharaman | 5a52c2a58 | 2011-07-22 23:39:51 +0000 | [diff] [blame] | 1440 | xfs_buf_ioerror(bp, EIO); |
Christoph Hellwig | 1a1a3e9 | 2010-10-06 18:41:18 +0000 | [diff] [blame] | 1441 | xfs_buf_ioend(bp, 0); |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 1442 | } else { |
Christoph Hellwig | c867cb6 | 2011-10-10 16:52:46 +0000 | [diff] [blame] | 1443 | xfs_buf_stale(bp); |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 1444 | xfs_buf_relse(bp); |
| 1445 | } |
| 1446 | } |
| 1447 | |
| 1448 | /* |
| 1449 | * Unlocks the flush lock |
| 1450 | */ |
Dave Chinner | 04913fd | 2012-04-23 15:58:41 +1000 | [diff] [blame] | 1451 | xfs_iflush_abort(iq, false); |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 1452 | kmem_free(ilist); |
Dave Chinner | 44b56e0 | 2010-01-11 11:47:43 +0000 | [diff] [blame] | 1453 | xfs_perag_put(pag); |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 1454 | return XFS_ERROR(EFSCORRUPTED); |
| 1455 | } |
| 1456 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1457 | /* |
Christoph Hellwig | 4c46819 | 2012-04-23 15:58:36 +1000 | [diff] [blame] | 1458 | * Flush dirty inode metadata into the backing buffer. |
| 1459 | * |
| 1460 | * The caller must have the inode lock and the inode flush lock held. The |
| 1461 | * inode lock will still be held upon return to the caller, and the inode |
| 1462 | * flush lock will be released after the inode has reached the disk. |
| 1463 | * |
| 1464 | * 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] | 1465 | */ |
| 1466 | int |
| 1467 | xfs_iflush( |
Christoph Hellwig | 4c46819 | 2012-04-23 15:58:36 +1000 | [diff] [blame] | 1468 | struct xfs_inode *ip, |
| 1469 | struct xfs_buf **bpp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1470 | { |
Christoph Hellwig | 4c46819 | 2012-04-23 15:58:36 +1000 | [diff] [blame] | 1471 | struct xfs_mount *mp = ip->i_mount; |
| 1472 | struct xfs_buf *bp; |
| 1473 | struct xfs_dinode *dip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1474 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1475 | |
| 1476 | XFS_STATS_INC(xs_iflush_count); |
| 1477 | |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 1478 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)); |
Christoph Hellwig | 474fce0 | 2011-12-18 20:00:09 +0000 | [diff] [blame] | 1479 | ASSERT(xfs_isiflocked(ip)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1480 | ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE || |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 1481 | ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1482 | |
Christoph Hellwig | 4c46819 | 2012-04-23 15:58:36 +1000 | [diff] [blame] | 1483 | *bpp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1484 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 | xfs_iunpin_wait(ip); |
| 1486 | |
| 1487 | /* |
Dave Chinner | 4b6a468 | 2010-01-11 11:45:21 +0000 | [diff] [blame] | 1488 | * For stale inodes we cannot rely on the backing buffer remaining |
| 1489 | * 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] | 1490 | * 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] | 1491 | * inodes below. We have to check this after ensuring the inode is |
| 1492 | * unpinned so that it is safe to reclaim the stale inode after the |
| 1493 | * flush call. |
| 1494 | */ |
| 1495 | if (xfs_iflags_test(ip, XFS_ISTALE)) { |
| 1496 | xfs_ifunlock(ip); |
| 1497 | return 0; |
| 1498 | } |
| 1499 | |
| 1500 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1501 | * This may have been unpinned because the filesystem is shutting |
| 1502 | * 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] | 1503 | * to disk, because the log record didn't make it to disk. |
| 1504 | * |
| 1505 | * We also have to remove the log item from the AIL in this case, |
| 1506 | * 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] | 1507 | */ |
| 1508 | if (XFS_FORCED_SHUTDOWN(mp)) { |
Christoph Hellwig | 32ce90a | 2012-04-23 15:58:32 +1000 | [diff] [blame] | 1509 | error = XFS_ERROR(EIO); |
| 1510 | goto abort_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1511 | } |
| 1512 | |
| 1513 | /* |
David Chinner | a3f74ff | 2008-03-06 13:43:42 +1100 | [diff] [blame] | 1514 | * Get the buffer containing the on-disk inode. |
| 1515 | */ |
Christoph Hellwig | 475ee41 | 2012-07-03 12:21:22 -0400 | [diff] [blame] | 1516 | error = xfs_imap_to_bp(mp, NULL, &ip->i_imap, &dip, &bp, XBF_TRYLOCK, |
| 1517 | 0); |
David Chinner | a3f74ff | 2008-03-06 13:43:42 +1100 | [diff] [blame] | 1518 | if (error || !bp) { |
| 1519 | xfs_ifunlock(ip); |
| 1520 | return error; |
| 1521 | } |
| 1522 | |
| 1523 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1524 | * First flush out the inode that xfs_iflush was called with. |
| 1525 | */ |
| 1526 | error = xfs_iflush_int(ip, bp); |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 1527 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1528 | goto corrupt_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1529 | |
| 1530 | /* |
David Chinner | a3f74ff | 2008-03-06 13:43:42 +1100 | [diff] [blame] | 1531 | * If the buffer is pinned then push on the log now so we won't |
| 1532 | * get stuck waiting in the write for too long. |
| 1533 | */ |
Chandra Seetharaman | 811e64c | 2011-07-22 23:40:27 +0000 | [diff] [blame] | 1534 | if (xfs_buf_ispinned(bp)) |
Christoph Hellwig | a14a348 | 2010-01-19 09:56:46 +0000 | [diff] [blame] | 1535 | xfs_log_force(mp, 0); |
David Chinner | a3f74ff | 2008-03-06 13:43:42 +1100 | [diff] [blame] | 1536 | |
| 1537 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1538 | * inode clustering: |
| 1539 | * see if other inodes can be gathered into this write |
| 1540 | */ |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 1541 | error = xfs_iflush_cluster(ip, bp); |
| 1542 | if (error) |
| 1543 | goto cluster_corrupt_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1544 | |
Christoph Hellwig | 4c46819 | 2012-04-23 15:58:36 +1000 | [diff] [blame] | 1545 | *bpp = bp; |
| 1546 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1547 | |
| 1548 | corrupt_out: |
| 1549 | xfs_buf_relse(bp); |
Nathan Scott | 7d04a33 | 2006-06-09 14:58:38 +1000 | [diff] [blame] | 1550 | xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | cluster_corrupt_out: |
Christoph Hellwig | 32ce90a | 2012-04-23 15:58:32 +1000 | [diff] [blame] | 1552 | error = XFS_ERROR(EFSCORRUPTED); |
| 1553 | abort_out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1554 | /* |
| 1555 | * Unlocks the flush lock |
| 1556 | */ |
Dave Chinner | 04913fd | 2012-04-23 15:58:41 +1000 | [diff] [blame] | 1557 | xfs_iflush_abort(ip, false); |
Christoph Hellwig | 32ce90a | 2012-04-23 15:58:32 +1000 | [diff] [blame] | 1558 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1559 | } |
| 1560 | |
| 1561 | |
| 1562 | STATIC int |
| 1563 | xfs_iflush_int( |
Christoph Hellwig | 93848a9 | 2013-04-03 16:11:17 +1100 | [diff] [blame] | 1564 | struct xfs_inode *ip, |
| 1565 | struct xfs_buf *bp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1566 | { |
Christoph Hellwig | 93848a9 | 2013-04-03 16:11:17 +1100 | [diff] [blame] | 1567 | struct xfs_inode_log_item *iip = ip->i_itemp; |
| 1568 | struct xfs_dinode *dip; |
| 1569 | struct xfs_mount *mp = ip->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1570 | |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 1571 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)); |
Christoph Hellwig | 474fce0 | 2011-12-18 20:00:09 +0000 | [diff] [blame] | 1572 | ASSERT(xfs_isiflocked(ip)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1573 | ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE || |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 1574 | ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK)); |
Christoph Hellwig | 93848a9 | 2013-04-03 16:11:17 +1100 | [diff] [blame] | 1575 | ASSERT(iip != NULL && iip->ili_fields != 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1576 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1577 | /* set *dip = inode's place in the buffer */ |
Christoph Hellwig | 92bfc6e | 2008-11-28 14:23:41 +1100 | [diff] [blame] | 1578 | 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] | 1579 | |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 1580 | 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] | 1581 | mp, XFS_ERRTAG_IFLUSH_1, XFS_RANDOM_IFLUSH_1)) { |
Dave Chinner | 6a19d93 | 2011-03-07 10:02:35 +1100 | [diff] [blame] | 1582 | xfs_alert_tag(mp, XFS_PTAG_IFLUSH, |
| 1583 | "%s: Bad inode %Lu magic number 0x%x, ptr 0x%p", |
| 1584 | __func__, ip->i_ino, be16_to_cpu(dip->di_magic), dip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1585 | goto corrupt_out; |
| 1586 | } |
| 1587 | if (XFS_TEST_ERROR(ip->i_d.di_magic != XFS_DINODE_MAGIC, |
| 1588 | mp, XFS_ERRTAG_IFLUSH_2, XFS_RANDOM_IFLUSH_2)) { |
Dave Chinner | 6a19d93 | 2011-03-07 10:02:35 +1100 | [diff] [blame] | 1589 | xfs_alert_tag(mp, XFS_PTAG_IFLUSH, |
| 1590 | "%s: Bad inode %Lu, ptr 0x%p, magic number 0x%x", |
| 1591 | __func__, ip->i_ino, ip, ip->i_d.di_magic); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1592 | goto corrupt_out; |
| 1593 | } |
Al Viro | abbede1 | 2011-07-26 02:31:30 -0400 | [diff] [blame] | 1594 | if (S_ISREG(ip->i_d.di_mode)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1595 | if (XFS_TEST_ERROR( |
| 1596 | (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) && |
| 1597 | (ip->i_d.di_format != XFS_DINODE_FMT_BTREE), |
| 1598 | mp, XFS_ERRTAG_IFLUSH_3, XFS_RANDOM_IFLUSH_3)) { |
Dave Chinner | 6a19d93 | 2011-03-07 10:02:35 +1100 | [diff] [blame] | 1599 | xfs_alert_tag(mp, XFS_PTAG_IFLUSH, |
| 1600 | "%s: Bad regular inode %Lu, ptr 0x%p", |
| 1601 | __func__, ip->i_ino, ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1602 | goto corrupt_out; |
| 1603 | } |
Al Viro | abbede1 | 2011-07-26 02:31:30 -0400 | [diff] [blame] | 1604 | } else if (S_ISDIR(ip->i_d.di_mode)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1605 | if (XFS_TEST_ERROR( |
| 1606 | (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) && |
| 1607 | (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) && |
| 1608 | (ip->i_d.di_format != XFS_DINODE_FMT_LOCAL), |
| 1609 | mp, XFS_ERRTAG_IFLUSH_4, XFS_RANDOM_IFLUSH_4)) { |
Dave Chinner | 6a19d93 | 2011-03-07 10:02:35 +1100 | [diff] [blame] | 1610 | xfs_alert_tag(mp, XFS_PTAG_IFLUSH, |
| 1611 | "%s: Bad directory inode %Lu, ptr 0x%p", |
| 1612 | __func__, ip->i_ino, ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1613 | goto corrupt_out; |
| 1614 | } |
| 1615 | } |
| 1616 | if (XFS_TEST_ERROR(ip->i_d.di_nextents + ip->i_d.di_anextents > |
| 1617 | ip->i_d.di_nblocks, mp, XFS_ERRTAG_IFLUSH_5, |
| 1618 | XFS_RANDOM_IFLUSH_5)) { |
Dave Chinner | 6a19d93 | 2011-03-07 10:02:35 +1100 | [diff] [blame] | 1619 | xfs_alert_tag(mp, XFS_PTAG_IFLUSH, |
| 1620 | "%s: detected corrupt incore inode %Lu, " |
| 1621 | "total extents = %d, nblocks = %Ld, ptr 0x%p", |
| 1622 | __func__, ip->i_ino, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1623 | ip->i_d.di_nextents + ip->i_d.di_anextents, |
Dave Chinner | 6a19d93 | 2011-03-07 10:02:35 +1100 | [diff] [blame] | 1624 | ip->i_d.di_nblocks, ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1625 | goto corrupt_out; |
| 1626 | } |
| 1627 | if (XFS_TEST_ERROR(ip->i_d.di_forkoff > mp->m_sb.sb_inodesize, |
| 1628 | mp, XFS_ERRTAG_IFLUSH_6, XFS_RANDOM_IFLUSH_6)) { |
Dave Chinner | 6a19d93 | 2011-03-07 10:02:35 +1100 | [diff] [blame] | 1629 | xfs_alert_tag(mp, XFS_PTAG_IFLUSH, |
| 1630 | "%s: bad inode %Lu, forkoff 0x%x, ptr 0x%p", |
| 1631 | __func__, ip->i_ino, ip->i_d.di_forkoff, ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1632 | goto corrupt_out; |
| 1633 | } |
Dave Chinner | e60896d | 2013-07-24 15:47:30 +1000 | [diff] [blame] | 1634 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1635 | /* |
Dave Chinner | e60896d | 2013-07-24 15:47:30 +1000 | [diff] [blame] | 1636 | * Inode item log recovery for v1/v2 inodes are dependent on the |
| 1637 | * di_flushiter count for correct sequencing. We bump the flush |
| 1638 | * iteration count so we can detect flushes which postdate a log record |
| 1639 | * during recovery. This is redundant as we now log every change and |
| 1640 | * hence this can't happen but we need to still do it to ensure |
| 1641 | * backwards compatibility with old kernels that predate logging all |
| 1642 | * inode changes. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1643 | */ |
Dave Chinner | e60896d | 2013-07-24 15:47:30 +1000 | [diff] [blame] | 1644 | if (ip->i_d.di_version < 3) |
| 1645 | ip->i_d.di_flushiter++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1646 | |
| 1647 | /* |
| 1648 | * Copy the dirty parts of the inode into the on-disk |
| 1649 | * inode. We always copy out the core of the inode, |
| 1650 | * because if the inode is dirty at all the core must |
| 1651 | * be. |
| 1652 | */ |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 1653 | xfs_dinode_to_disk(dip, &ip->i_d); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1654 | |
| 1655 | /* Wrap, we never let the log put out DI_MAX_FLUSH */ |
| 1656 | if (ip->i_d.di_flushiter == DI_MAX_FLUSH) |
| 1657 | ip->i_d.di_flushiter = 0; |
| 1658 | |
| 1659 | /* |
| 1660 | * If this is really an old format inode and the superblock version |
| 1661 | * has not been updated to support only new format inodes, then |
| 1662 | * convert back to the old inode format. If the superblock version |
| 1663 | * has been updated, then make the conversion permanent. |
| 1664 | */ |
Christoph Hellwig | 51ce16d | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 1665 | ASSERT(ip->i_d.di_version == 1 || xfs_sb_version_hasnlink(&mp->m_sb)); |
| 1666 | if (ip->i_d.di_version == 1) { |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 1667 | if (!xfs_sb_version_hasnlink(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1668 | /* |
| 1669 | * Convert it back. |
| 1670 | */ |
| 1671 | ASSERT(ip->i_d.di_nlink <= XFS_MAXLINK_1); |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 1672 | dip->di_onlink = cpu_to_be16(ip->i_d.di_nlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1673 | } else { |
| 1674 | /* |
| 1675 | * The superblock version has already been bumped, |
| 1676 | * so just make the conversion to the new inode |
| 1677 | * format permanent. |
| 1678 | */ |
Christoph Hellwig | 51ce16d | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 1679 | ip->i_d.di_version = 2; |
| 1680 | dip->di_version = 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1681 | ip->i_d.di_onlink = 0; |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 1682 | dip->di_onlink = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1683 | 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] | 1684 | memset(&(dip->di_pad[0]), 0, |
| 1685 | sizeof(dip->di_pad)); |
Arkadiusz Mi?kiewicz | 6743099 | 2010-09-26 06:10:18 +0000 | [diff] [blame] | 1686 | ASSERT(xfs_get_projid(ip) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1687 | } |
| 1688 | } |
| 1689 | |
David Chinner | e4ac967 | 2008-04-10 12:23:58 +1000 | [diff] [blame] | 1690 | xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK, bp); |
| 1691 | if (XFS_IFORK_Q(ip)) |
| 1692 | xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK, bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1693 | xfs_inobp_check(mp, bp); |
| 1694 | |
| 1695 | /* |
Christoph Hellwig | f5d8d5c | 2012-02-29 09:53:54 +0000 | [diff] [blame] | 1696 | * We've recorded everything logged in the inode, so we'd like to clear |
| 1697 | * the ili_fields bits so we don't log and flush things unnecessarily. |
| 1698 | * However, we can't stop logging all this information until the data |
| 1699 | * we've copied into the disk buffer is written to disk. If we did we |
| 1700 | * might overwrite the copy of the inode in the log with all the data |
| 1701 | * after re-logging only part of it, and in the face of a crash we |
| 1702 | * wouldn't have all the data we need to recover. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1703 | * |
Christoph Hellwig | f5d8d5c | 2012-02-29 09:53:54 +0000 | [diff] [blame] | 1704 | * What we do is move the bits to the ili_last_fields field. When |
| 1705 | * logging the inode, these bits are moved back to the ili_fields field. |
| 1706 | * In the xfs_iflush_done() routine we clear ili_last_fields, since we |
| 1707 | * know that the information those bits represent is permanently on |
| 1708 | * disk. As long as the flush completes before the inode is logged |
| 1709 | * again, then both ili_fields and ili_last_fields will be cleared. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1710 | * |
Christoph Hellwig | f5d8d5c | 2012-02-29 09:53:54 +0000 | [diff] [blame] | 1711 | * We can play with the ili_fields bits here, because the inode lock |
| 1712 | * must be held exclusively in order to set bits there and the flush |
| 1713 | * lock protects the ili_last_fields bits. Set ili_logged so the flush |
| 1714 | * done routine can tell whether or not to look in the AIL. Also, store |
| 1715 | * the current LSN of the inode so that we can tell whether the item has |
| 1716 | * moved in the AIL from xfs_iflush_done(). In order to read the lsn we |
| 1717 | * need the AIL lock, because it is a 64 bit value that cannot be read |
| 1718 | * atomically. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1719 | */ |
Christoph Hellwig | 93848a9 | 2013-04-03 16:11:17 +1100 | [diff] [blame] | 1720 | iip->ili_last_fields = iip->ili_fields; |
| 1721 | iip->ili_fields = 0; |
| 1722 | iip->ili_logged = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1723 | |
Christoph Hellwig | 93848a9 | 2013-04-03 16:11:17 +1100 | [diff] [blame] | 1724 | xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn, |
| 1725 | &iip->ili_item.li_lsn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1726 | |
Christoph Hellwig | 93848a9 | 2013-04-03 16:11:17 +1100 | [diff] [blame] | 1727 | /* |
| 1728 | * Attach the function xfs_iflush_done to the inode's |
| 1729 | * buffer. This will remove the inode from the AIL |
| 1730 | * and unlock the inode's flush lock when the inode is |
| 1731 | * completely written to disk. |
| 1732 | */ |
| 1733 | xfs_buf_attach_iodone(bp, xfs_iflush_done, &iip->ili_item); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1734 | |
Christoph Hellwig | 93848a9 | 2013-04-03 16:11:17 +1100 | [diff] [blame] | 1735 | /* update the lsn in the on disk inode if required */ |
| 1736 | if (ip->i_d.di_version == 3) |
| 1737 | dip->di_lsn = cpu_to_be64(iip->ili_item.li_lsn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1738 | |
Christoph Hellwig | 93848a9 | 2013-04-03 16:11:17 +1100 | [diff] [blame] | 1739 | /* generate the checksum. */ |
| 1740 | xfs_dinode_calc_crc(mp, dip); |
| 1741 | |
| 1742 | ASSERT(bp->b_fspriv != NULL); |
| 1743 | ASSERT(bp->b_iodone != NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1744 | return 0; |
| 1745 | |
| 1746 | corrupt_out: |
| 1747 | return XFS_ERROR(EFSCORRUPTED); |
| 1748 | } |
| 1749 | |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1750 | /* |
Brian Foster | 72b53ef | 2012-11-06 09:50:40 -0500 | [diff] [blame] | 1751 | * Test whether it is appropriate to check an inode for and free post EOF |
| 1752 | * blocks. The 'force' parameter determines whether we should also consider |
| 1753 | * regular files that are marked preallocated or append-only. |
| 1754 | */ |
| 1755 | bool |
| 1756 | xfs_can_free_eofblocks(struct xfs_inode *ip, bool force) |
| 1757 | { |
| 1758 | /* prealloc/delalloc exists only on regular files */ |
| 1759 | if (!S_ISREG(ip->i_d.di_mode)) |
| 1760 | return false; |
| 1761 | |
| 1762 | /* |
| 1763 | * Zero sized files with no cached pages and delalloc blocks will not |
| 1764 | * have speculative prealloc/delalloc blocks to remove. |
| 1765 | */ |
| 1766 | if (VFS_I(ip)->i_size == 0 && |
| 1767 | VN_CACHED(VFS_I(ip)) == 0 && |
| 1768 | ip->i_delayed_blks == 0) |
| 1769 | return false; |
| 1770 | |
| 1771 | /* If we haven't read in the extent list, then don't do it now. */ |
| 1772 | if (!(ip->i_df.if_flags & XFS_IFEXTENTS)) |
| 1773 | return false; |
| 1774 | |
| 1775 | /* |
| 1776 | * Do not free real preallocated or append-only files unless the file |
| 1777 | * has delalloc blocks and we are forced to remove them. |
| 1778 | */ |
| 1779 | if (ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) |
| 1780 | if (!force || ip->i_delayed_blks == 0) |
| 1781 | return false; |
| 1782 | |
| 1783 | return true; |
| 1784 | } |