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 | 70a9883 | 2013-10-23 10:36:05 +1100 | [diff] [blame] | 22 | #include "xfs_shared.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 23 | #include "xfs_format.h" |
| 24 | #include "xfs_log_format.h" |
| 25 | #include "xfs_trans_resv.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 26 | #include "xfs_inum.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 30 | #include "xfs_inode.h" |
Dave Chinner | 5706278 | 2013-10-15 09:17:51 +1100 | [diff] [blame] | 31 | #include "xfs_da_format.h" |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 32 | #include "xfs_da_btree.h" |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 33 | #include "xfs_dir2.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 34 | #include "xfs_attr_sf.h" |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 35 | #include "xfs_attr.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 36 | #include "xfs_trans_space.h" |
| 37 | #include "xfs_trans.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include "xfs_buf_item.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 39 | #include "xfs_inode_item.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 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_quota.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 45 | #include "xfs_dinode.h" |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 46 | #include "xfs_filestream.h" |
Christoph Hellwig | 93848a9 | 2013-04-03 16:11:17 +1100 | [diff] [blame] | 47 | #include "xfs_cksum.h" |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 48 | #include "xfs_trace.h" |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 49 | #include "xfs_icache.h" |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 50 | #include "xfs_symlink.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 51 | #include "xfs_trans_priv.h" |
| 52 | #include "xfs_log.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 53 | #include "xfs_bmap_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | kmem_zone_t *xfs_inode_zone; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
| 57 | /* |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 58 | * Used in xfs_itruncate_extents(). This is the maximum number of extents |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | * freed from a file in a single transaction. |
| 60 | */ |
| 61 | #define XFS_ITRUNC_MAX_EXTENTS 2 |
| 62 | |
| 63 | STATIC int xfs_iflush_int(xfs_inode_t *, xfs_buf_t *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
Dave Chinner | 2a0ec1d | 2012-04-23 15:59:02 +1000 | [diff] [blame] | 65 | /* |
| 66 | * helper function to extract extent size hint from inode |
| 67 | */ |
| 68 | xfs_extlen_t |
| 69 | xfs_get_extsz_hint( |
| 70 | struct xfs_inode *ip) |
| 71 | { |
| 72 | if ((ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE) && ip->i_d.di_extsize) |
| 73 | return ip->i_d.di_extsize; |
| 74 | if (XFS_IS_REALTIME_INODE(ip)) |
| 75 | return ip->i_mount->m_sb.sb_rextsize; |
| 76 | return 0; |
| 77 | } |
| 78 | |
Dave Chinner | fa96aca | 2012-10-08 21:56:10 +1100 | [diff] [blame] | 79 | /* |
| 80 | * This is a wrapper routine around the xfs_ilock() routine used to centralize |
| 81 | * some grungy code. It is used in places that wish to lock the inode solely |
| 82 | * for reading the extents. The reason these places can't just call |
| 83 | * xfs_ilock(SHARED) is that the inode lock also guards to bringing in of the |
| 84 | * extents from disk for a file in b-tree format. If the inode is in b-tree |
| 85 | * format, then we need to lock the inode exclusively until the extents are read |
| 86 | * in. Locking it exclusively all the time would limit our parallelism |
| 87 | * unnecessarily, though. What we do instead is check to see if the extents |
| 88 | * have been read in yet, and only lock the inode exclusively if they have not. |
| 89 | * |
| 90 | * The function returns a value which should be given to the corresponding |
Christoph Hellwig | 01f4f32 | 2013-12-06 12:30:08 -0800 | [diff] [blame^] | 91 | * xfs_iunlock() call. |
Dave Chinner | fa96aca | 2012-10-08 21:56:10 +1100 | [diff] [blame] | 92 | */ |
| 93 | uint |
| 94 | xfs_ilock_map_shared( |
| 95 | xfs_inode_t *ip) |
| 96 | { |
| 97 | uint lock_mode; |
| 98 | |
| 99 | if ((ip->i_d.di_format == XFS_DINODE_FMT_BTREE) && |
| 100 | ((ip->i_df.if_flags & XFS_IFEXTENTS) == 0)) { |
| 101 | lock_mode = XFS_ILOCK_EXCL; |
| 102 | } else { |
| 103 | lock_mode = XFS_ILOCK_SHARED; |
| 104 | } |
| 105 | |
| 106 | xfs_ilock(ip, lock_mode); |
| 107 | |
| 108 | return lock_mode; |
| 109 | } |
| 110 | |
| 111 | /* |
Dave Chinner | fa96aca | 2012-10-08 21:56:10 +1100 | [diff] [blame] | 112 | * The xfs inode contains 2 locks: a multi-reader lock called the |
| 113 | * i_iolock and a multi-reader lock called the i_lock. This routine |
| 114 | * allows either or both of the locks to be obtained. |
| 115 | * |
| 116 | * The 2 locks should always be ordered so that the IO lock is |
| 117 | * obtained first in order to prevent deadlock. |
| 118 | * |
| 119 | * ip -- the inode being locked |
| 120 | * lock_flags -- this parameter indicates the inode's locks |
| 121 | * to be locked. It can be: |
| 122 | * XFS_IOLOCK_SHARED, |
| 123 | * XFS_IOLOCK_EXCL, |
| 124 | * XFS_ILOCK_SHARED, |
| 125 | * XFS_ILOCK_EXCL, |
| 126 | * XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED, |
| 127 | * XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL, |
| 128 | * XFS_IOLOCK_EXCL | XFS_ILOCK_SHARED, |
| 129 | * XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL |
| 130 | */ |
| 131 | void |
| 132 | xfs_ilock( |
| 133 | xfs_inode_t *ip, |
| 134 | uint lock_flags) |
| 135 | { |
| 136 | trace_xfs_ilock(ip, lock_flags, _RET_IP_); |
| 137 | |
| 138 | /* |
| 139 | * You can't set both SHARED and EXCL for the same lock, |
| 140 | * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED, |
| 141 | * and XFS_ILOCK_EXCL are valid values to set in lock_flags. |
| 142 | */ |
| 143 | ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) != |
| 144 | (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)); |
| 145 | ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) != |
| 146 | (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)); |
| 147 | ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0); |
| 148 | |
| 149 | if (lock_flags & XFS_IOLOCK_EXCL) |
| 150 | mrupdate_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags)); |
| 151 | else if (lock_flags & XFS_IOLOCK_SHARED) |
| 152 | mraccess_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags)); |
| 153 | |
| 154 | if (lock_flags & XFS_ILOCK_EXCL) |
| 155 | mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags)); |
| 156 | else if (lock_flags & XFS_ILOCK_SHARED) |
| 157 | mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags)); |
| 158 | } |
| 159 | |
| 160 | /* |
| 161 | * This is just like xfs_ilock(), except that the caller |
| 162 | * is guaranteed not to sleep. It returns 1 if it gets |
| 163 | * the requested locks and 0 otherwise. If the IO lock is |
| 164 | * obtained but the inode lock cannot be, then the IO lock |
| 165 | * is dropped before returning. |
| 166 | * |
| 167 | * ip -- the inode being locked |
| 168 | * lock_flags -- this parameter indicates the inode's locks to be |
| 169 | * to be locked. See the comment for xfs_ilock() for a list |
| 170 | * of valid values. |
| 171 | */ |
| 172 | int |
| 173 | xfs_ilock_nowait( |
| 174 | xfs_inode_t *ip, |
| 175 | uint lock_flags) |
| 176 | { |
| 177 | trace_xfs_ilock_nowait(ip, lock_flags, _RET_IP_); |
| 178 | |
| 179 | /* |
| 180 | * You can't set both SHARED and EXCL for the same lock, |
| 181 | * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED, |
| 182 | * and XFS_ILOCK_EXCL are valid values to set in lock_flags. |
| 183 | */ |
| 184 | ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) != |
| 185 | (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)); |
| 186 | ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) != |
| 187 | (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)); |
| 188 | ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0); |
| 189 | |
| 190 | if (lock_flags & XFS_IOLOCK_EXCL) { |
| 191 | if (!mrtryupdate(&ip->i_iolock)) |
| 192 | goto out; |
| 193 | } else if (lock_flags & XFS_IOLOCK_SHARED) { |
| 194 | if (!mrtryaccess(&ip->i_iolock)) |
| 195 | goto out; |
| 196 | } |
| 197 | if (lock_flags & XFS_ILOCK_EXCL) { |
| 198 | if (!mrtryupdate(&ip->i_lock)) |
| 199 | goto out_undo_iolock; |
| 200 | } else if (lock_flags & XFS_ILOCK_SHARED) { |
| 201 | if (!mrtryaccess(&ip->i_lock)) |
| 202 | goto out_undo_iolock; |
| 203 | } |
| 204 | return 1; |
| 205 | |
| 206 | out_undo_iolock: |
| 207 | if (lock_flags & XFS_IOLOCK_EXCL) |
| 208 | mrunlock_excl(&ip->i_iolock); |
| 209 | else if (lock_flags & XFS_IOLOCK_SHARED) |
| 210 | mrunlock_shared(&ip->i_iolock); |
| 211 | out: |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | /* |
| 216 | * xfs_iunlock() is used to drop the inode locks acquired with |
| 217 | * xfs_ilock() and xfs_ilock_nowait(). The caller must pass |
| 218 | * in the flags given to xfs_ilock() or xfs_ilock_nowait() so |
| 219 | * that we know which locks to drop. |
| 220 | * |
| 221 | * ip -- the inode being unlocked |
| 222 | * lock_flags -- this parameter indicates the inode's locks to be |
| 223 | * to be unlocked. See the comment for xfs_ilock() for a list |
| 224 | * of valid values for this parameter. |
| 225 | * |
| 226 | */ |
| 227 | void |
| 228 | xfs_iunlock( |
| 229 | xfs_inode_t *ip, |
| 230 | uint lock_flags) |
| 231 | { |
| 232 | /* |
| 233 | * You can't set both SHARED and EXCL for the same lock, |
| 234 | * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED, |
| 235 | * and XFS_ILOCK_EXCL are valid values to set in lock_flags. |
| 236 | */ |
| 237 | ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) != |
| 238 | (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)); |
| 239 | ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) != |
| 240 | (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)); |
| 241 | ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0); |
| 242 | ASSERT(lock_flags != 0); |
| 243 | |
| 244 | if (lock_flags & XFS_IOLOCK_EXCL) |
| 245 | mrunlock_excl(&ip->i_iolock); |
| 246 | else if (lock_flags & XFS_IOLOCK_SHARED) |
| 247 | mrunlock_shared(&ip->i_iolock); |
| 248 | |
| 249 | if (lock_flags & XFS_ILOCK_EXCL) |
| 250 | mrunlock_excl(&ip->i_lock); |
| 251 | else if (lock_flags & XFS_ILOCK_SHARED) |
| 252 | mrunlock_shared(&ip->i_lock); |
| 253 | |
| 254 | trace_xfs_iunlock(ip, lock_flags, _RET_IP_); |
| 255 | } |
| 256 | |
| 257 | /* |
| 258 | * give up write locks. the i/o lock cannot be held nested |
| 259 | * if it is being demoted. |
| 260 | */ |
| 261 | void |
| 262 | xfs_ilock_demote( |
| 263 | xfs_inode_t *ip, |
| 264 | uint lock_flags) |
| 265 | { |
| 266 | ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)); |
| 267 | ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0); |
| 268 | |
| 269 | if (lock_flags & XFS_ILOCK_EXCL) |
| 270 | mrdemote(&ip->i_lock); |
| 271 | if (lock_flags & XFS_IOLOCK_EXCL) |
| 272 | mrdemote(&ip->i_iolock); |
| 273 | |
| 274 | trace_xfs_ilock_demote(ip, lock_flags, _RET_IP_); |
| 275 | } |
| 276 | |
Dave Chinner | 742ae1e | 2013-04-30 21:39:34 +1000 | [diff] [blame] | 277 | #if defined(DEBUG) || defined(XFS_WARN) |
Dave Chinner | fa96aca | 2012-10-08 21:56:10 +1100 | [diff] [blame] | 278 | int |
| 279 | xfs_isilocked( |
| 280 | xfs_inode_t *ip, |
| 281 | uint lock_flags) |
| 282 | { |
| 283 | if (lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) { |
| 284 | if (!(lock_flags & XFS_ILOCK_SHARED)) |
| 285 | return !!ip->i_lock.mr_writer; |
| 286 | return rwsem_is_locked(&ip->i_lock.mr_lock); |
| 287 | } |
| 288 | |
| 289 | if (lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)) { |
| 290 | if (!(lock_flags & XFS_IOLOCK_SHARED)) |
| 291 | return !!ip->i_iolock.mr_writer; |
| 292 | return rwsem_is_locked(&ip->i_iolock.mr_lock); |
| 293 | } |
| 294 | |
| 295 | ASSERT(0); |
| 296 | return 0; |
| 297 | } |
| 298 | #endif |
| 299 | |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 300 | #ifdef DEBUG |
| 301 | int xfs_locked_n; |
| 302 | int xfs_small_retries; |
| 303 | int xfs_middle_retries; |
| 304 | int xfs_lots_retries; |
| 305 | int xfs_lock_delays; |
| 306 | #endif |
| 307 | |
| 308 | /* |
| 309 | * Bump the subclass so xfs_lock_inodes() acquires each lock with |
| 310 | * a different value |
| 311 | */ |
| 312 | static inline int |
| 313 | xfs_lock_inumorder(int lock_mode, int subclass) |
| 314 | { |
| 315 | if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)) |
| 316 | lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT; |
| 317 | if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) |
| 318 | lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT; |
| 319 | |
| 320 | return lock_mode; |
| 321 | } |
| 322 | |
| 323 | /* |
| 324 | * The following routine will lock n inodes in exclusive mode. |
| 325 | * We assume the caller calls us with the inodes in i_ino order. |
| 326 | * |
| 327 | * We need to detect deadlock where an inode that we lock |
| 328 | * is in the AIL and we start waiting for another inode that is locked |
| 329 | * by a thread in a long running transaction (such as truncate). This can |
| 330 | * result in deadlock since the long running trans might need to wait |
| 331 | * for the inode we just locked in order to push the tail and free space |
| 332 | * in the log. |
| 333 | */ |
| 334 | void |
| 335 | xfs_lock_inodes( |
| 336 | xfs_inode_t **ips, |
| 337 | int inodes, |
| 338 | uint lock_mode) |
| 339 | { |
| 340 | int attempts = 0, i, j, try_lock; |
| 341 | xfs_log_item_t *lp; |
| 342 | |
| 343 | ASSERT(ips && (inodes >= 2)); /* we need at least two */ |
| 344 | |
| 345 | try_lock = 0; |
| 346 | i = 0; |
| 347 | |
| 348 | again: |
| 349 | for (; i < inodes; i++) { |
| 350 | ASSERT(ips[i]); |
| 351 | |
| 352 | if (i && (ips[i] == ips[i-1])) /* Already locked */ |
| 353 | continue; |
| 354 | |
| 355 | /* |
| 356 | * If try_lock is not set yet, make sure all locked inodes |
| 357 | * are not in the AIL. |
| 358 | * If any are, set try_lock to be used later. |
| 359 | */ |
| 360 | |
| 361 | if (!try_lock) { |
| 362 | for (j = (i - 1); j >= 0 && !try_lock; j--) { |
| 363 | lp = (xfs_log_item_t *)ips[j]->i_itemp; |
| 364 | if (lp && (lp->li_flags & XFS_LI_IN_AIL)) { |
| 365 | try_lock++; |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | /* |
| 371 | * If any of the previous locks we have locked is in the AIL, |
| 372 | * we must TRY to get the second and subsequent locks. If |
| 373 | * we can't get any, we must release all we have |
| 374 | * and try again. |
| 375 | */ |
| 376 | |
| 377 | if (try_lock) { |
| 378 | /* try_lock must be 0 if i is 0. */ |
| 379 | /* |
| 380 | * try_lock means we have an inode locked |
| 381 | * that is in the AIL. |
| 382 | */ |
| 383 | ASSERT(i != 0); |
| 384 | if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) { |
| 385 | attempts++; |
| 386 | |
| 387 | /* |
| 388 | * Unlock all previous guys and try again. |
| 389 | * xfs_iunlock will try to push the tail |
| 390 | * if the inode is in the AIL. |
| 391 | */ |
| 392 | |
| 393 | for(j = i - 1; j >= 0; j--) { |
| 394 | |
| 395 | /* |
| 396 | * Check to see if we've already |
| 397 | * unlocked this one. |
| 398 | * Not the first one going back, |
| 399 | * and the inode ptr is the same. |
| 400 | */ |
| 401 | if ((j != (i - 1)) && ips[j] == |
| 402 | ips[j+1]) |
| 403 | continue; |
| 404 | |
| 405 | xfs_iunlock(ips[j], lock_mode); |
| 406 | } |
| 407 | |
| 408 | if ((attempts % 5) == 0) { |
| 409 | delay(1); /* Don't just spin the CPU */ |
| 410 | #ifdef DEBUG |
| 411 | xfs_lock_delays++; |
| 412 | #endif |
| 413 | } |
| 414 | i = 0; |
| 415 | try_lock = 0; |
| 416 | goto again; |
| 417 | } |
| 418 | } else { |
| 419 | xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i)); |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | #ifdef DEBUG |
| 424 | if (attempts) { |
| 425 | if (attempts < 5) xfs_small_retries++; |
| 426 | else if (attempts < 100) xfs_middle_retries++; |
| 427 | else xfs_lots_retries++; |
| 428 | } else { |
| 429 | xfs_locked_n++; |
| 430 | } |
| 431 | #endif |
| 432 | } |
| 433 | |
| 434 | /* |
| 435 | * xfs_lock_two_inodes() can only be used to lock one type of lock |
| 436 | * at a time - the iolock or the ilock, but not both at once. If |
| 437 | * we lock both at once, lockdep will report false positives saying |
| 438 | * we have violated locking orders. |
| 439 | */ |
| 440 | void |
| 441 | xfs_lock_two_inodes( |
| 442 | xfs_inode_t *ip0, |
| 443 | xfs_inode_t *ip1, |
| 444 | uint lock_mode) |
| 445 | { |
| 446 | xfs_inode_t *temp; |
| 447 | int attempts = 0; |
| 448 | xfs_log_item_t *lp; |
| 449 | |
| 450 | if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)) |
| 451 | ASSERT((lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) == 0); |
| 452 | ASSERT(ip0->i_ino != ip1->i_ino); |
| 453 | |
| 454 | if (ip0->i_ino > ip1->i_ino) { |
| 455 | temp = ip0; |
| 456 | ip0 = ip1; |
| 457 | ip1 = temp; |
| 458 | } |
| 459 | |
| 460 | again: |
| 461 | xfs_ilock(ip0, xfs_lock_inumorder(lock_mode, 0)); |
| 462 | |
| 463 | /* |
| 464 | * If the first lock we have locked is in the AIL, we must TRY to get |
| 465 | * the second lock. If we can't get it, we must release the first one |
| 466 | * and try again. |
| 467 | */ |
| 468 | lp = (xfs_log_item_t *)ip0->i_itemp; |
| 469 | if (lp && (lp->li_flags & XFS_LI_IN_AIL)) { |
| 470 | if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(lock_mode, 1))) { |
| 471 | xfs_iunlock(ip0, lock_mode); |
| 472 | if ((++attempts % 5) == 0) |
| 473 | delay(1); /* Don't just spin the CPU */ |
| 474 | goto again; |
| 475 | } |
| 476 | } else { |
| 477 | xfs_ilock(ip1, xfs_lock_inumorder(lock_mode, 1)); |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | |
Dave Chinner | fa96aca | 2012-10-08 21:56:10 +1100 | [diff] [blame] | 482 | void |
| 483 | __xfs_iflock( |
| 484 | struct xfs_inode *ip) |
| 485 | { |
| 486 | wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IFLOCK_BIT); |
| 487 | DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IFLOCK_BIT); |
| 488 | |
| 489 | do { |
| 490 | prepare_to_wait_exclusive(wq, &wait.wait, TASK_UNINTERRUPTIBLE); |
| 491 | if (xfs_isiflocked(ip)) |
| 492 | io_schedule(); |
| 493 | } while (!xfs_iflock_nowait(ip)); |
| 494 | |
| 495 | finish_wait(wq, &wait.wait); |
| 496 | } |
| 497 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | STATIC uint |
| 499 | _xfs_dic2xflags( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | __uint16_t di_flags) |
| 501 | { |
| 502 | uint flags = 0; |
| 503 | |
| 504 | if (di_flags & XFS_DIFLAG_ANY) { |
| 505 | if (di_flags & XFS_DIFLAG_REALTIME) |
| 506 | flags |= XFS_XFLAG_REALTIME; |
| 507 | if (di_flags & XFS_DIFLAG_PREALLOC) |
| 508 | flags |= XFS_XFLAG_PREALLOC; |
| 509 | if (di_flags & XFS_DIFLAG_IMMUTABLE) |
| 510 | flags |= XFS_XFLAG_IMMUTABLE; |
| 511 | if (di_flags & XFS_DIFLAG_APPEND) |
| 512 | flags |= XFS_XFLAG_APPEND; |
| 513 | if (di_flags & XFS_DIFLAG_SYNC) |
| 514 | flags |= XFS_XFLAG_SYNC; |
| 515 | if (di_flags & XFS_DIFLAG_NOATIME) |
| 516 | flags |= XFS_XFLAG_NOATIME; |
| 517 | if (di_flags & XFS_DIFLAG_NODUMP) |
| 518 | flags |= XFS_XFLAG_NODUMP; |
| 519 | if (di_flags & XFS_DIFLAG_RTINHERIT) |
| 520 | flags |= XFS_XFLAG_RTINHERIT; |
| 521 | if (di_flags & XFS_DIFLAG_PROJINHERIT) |
| 522 | flags |= XFS_XFLAG_PROJINHERIT; |
| 523 | if (di_flags & XFS_DIFLAG_NOSYMLINKS) |
| 524 | flags |= XFS_XFLAG_NOSYMLINKS; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 525 | if (di_flags & XFS_DIFLAG_EXTSIZE) |
| 526 | flags |= XFS_XFLAG_EXTSIZE; |
| 527 | if (di_flags & XFS_DIFLAG_EXTSZINHERIT) |
| 528 | flags |= XFS_XFLAG_EXTSZINHERIT; |
Barry Naujok | d3446ea | 2006-06-09 14:54:19 +1000 | [diff] [blame] | 529 | if (di_flags & XFS_DIFLAG_NODEFRAG) |
| 530 | flags |= XFS_XFLAG_NODEFRAG; |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 531 | if (di_flags & XFS_DIFLAG_FILESTREAM) |
| 532 | flags |= XFS_XFLAG_FILESTREAM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | return flags; |
| 536 | } |
| 537 | |
| 538 | uint |
| 539 | xfs_ip2xflags( |
| 540 | xfs_inode_t *ip) |
| 541 | { |
Christoph Hellwig | 347d1c0 | 2007-08-28 13:57:51 +1000 | [diff] [blame] | 542 | xfs_icdinode_t *dic = &ip->i_d; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | |
Nathan Scott | a916e2b | 2006-06-09 17:12:17 +1000 | [diff] [blame] | 544 | return _xfs_dic2xflags(dic->di_flags) | |
Christoph Hellwig | 45ba598 | 2007-12-07 14:07:20 +1100 | [diff] [blame] | 545 | (XFS_IFORK_Q(ip) ? XFS_XFLAG_HASATTR : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | uint |
| 549 | xfs_dic2xflags( |
Christoph Hellwig | 45ba598 | 2007-12-07 14:07:20 +1100 | [diff] [blame] | 550 | xfs_dinode_t *dip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | { |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 552 | return _xfs_dic2xflags(be16_to_cpu(dip->di_flags)) | |
Christoph Hellwig | 45ba598 | 2007-12-07 14:07:20 +1100 | [diff] [blame] | 553 | (XFS_DFORK_Q(dip) ? XFS_XFLAG_HASATTR : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | } |
| 555 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | /* |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 557 | * Lookups up an inode from "name". If ci_name is not NULL, then a CI match |
| 558 | * is allowed, otherwise it has to be an exact match. If a CI match is found, |
| 559 | * ci_name->name will point to a the actual name (caller must free) or |
| 560 | * will be set to NULL if an exact match is found. |
| 561 | */ |
| 562 | int |
| 563 | xfs_lookup( |
| 564 | xfs_inode_t *dp, |
| 565 | struct xfs_name *name, |
| 566 | xfs_inode_t **ipp, |
| 567 | struct xfs_name *ci_name) |
| 568 | { |
| 569 | xfs_ino_t inum; |
| 570 | int error; |
| 571 | uint lock_mode; |
| 572 | |
| 573 | trace_xfs_lookup(dp, name); |
| 574 | |
| 575 | if (XFS_FORCED_SHUTDOWN(dp->i_mount)) |
| 576 | return XFS_ERROR(EIO); |
| 577 | |
| 578 | lock_mode = xfs_ilock_map_shared(dp); |
| 579 | error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name); |
Christoph Hellwig | 01f4f32 | 2013-12-06 12:30:08 -0800 | [diff] [blame^] | 580 | xfs_iunlock(dp, lock_mode); |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 581 | |
| 582 | if (error) |
| 583 | goto out; |
| 584 | |
| 585 | error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp); |
| 586 | if (error) |
| 587 | goto out_free_name; |
| 588 | |
| 589 | return 0; |
| 590 | |
| 591 | out_free_name: |
| 592 | if (ci_name) |
| 593 | kmem_free(ci_name->name); |
| 594 | out: |
| 595 | *ipp = NULL; |
| 596 | return error; |
| 597 | } |
| 598 | |
| 599 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | * Allocate an inode on disk and return a copy of its in-core version. |
| 601 | * The in-core inode is locked exclusively. Set mode, nlink, and rdev |
| 602 | * appropriately within the inode. The uid and gid for the inode are |
| 603 | * set according to the contents of the given cred structure. |
| 604 | * |
| 605 | * Use xfs_dialloc() to allocate the on-disk inode. If xfs_dialloc() |
Carlos Maiolino | cd856db | 2012-10-20 11:08:19 -0300 | [diff] [blame] | 606 | * has a free inode available, call xfs_iget() to obtain the in-core |
| 607 | * version of the allocated inode. Finally, fill in the inode and |
| 608 | * log its initial contents. In this case, ialloc_context would be |
| 609 | * set to NULL. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | * |
Carlos Maiolino | cd856db | 2012-10-20 11:08:19 -0300 | [diff] [blame] | 611 | * If xfs_dialloc() does not have an available inode, it will replenish |
| 612 | * its supply by doing an allocation. Since we can only do one |
| 613 | * allocation within a transaction without deadlocks, we must commit |
| 614 | * the current transaction before returning the inode itself. |
| 615 | * In this case, therefore, we will set ialloc_context and return. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | * The caller should then commit the current transaction, start a new |
| 617 | * transaction, and call xfs_ialloc() again to actually get the inode. |
| 618 | * |
| 619 | * To ensure that some other process does not grab the inode that |
| 620 | * was allocated during the first call to xfs_ialloc(), this routine |
| 621 | * also returns the [locked] bp pointing to the head of the freelist |
| 622 | * as ialloc_context. The caller should hold this buffer across |
| 623 | * 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] | 624 | * |
| 625 | * If we are allocating quota inodes, we do not have a parent inode |
| 626 | * to attach to or associate with (i.e. pip == NULL) because they |
| 627 | * are not linked into the directory structure - they are attached |
| 628 | * directly to the superblock - and so have no parent. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | */ |
| 630 | int |
| 631 | xfs_ialloc( |
| 632 | xfs_trans_t *tp, |
| 633 | xfs_inode_t *pip, |
Al Viro | 576b1d6 | 2011-07-26 02:50:15 -0400 | [diff] [blame] | 634 | umode_t mode, |
Nathan Scott | 31b084a | 2005-05-05 13:25:00 -0700 | [diff] [blame] | 635 | xfs_nlink_t nlink, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | xfs_dev_t rdev, |
Arkadiusz Mi?kiewicz | 6743099 | 2010-09-26 06:10:18 +0000 | [diff] [blame] | 637 | prid_t prid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | int okalloc, |
| 639 | xfs_buf_t **ialloc_context, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | xfs_inode_t **ipp) |
| 641 | { |
Christoph Hellwig | 93848a9 | 2013-04-03 16:11:17 +1100 | [diff] [blame] | 642 | struct xfs_mount *mp = tp->t_mountp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | xfs_ino_t ino; |
| 644 | xfs_inode_t *ip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | uint flags; |
| 646 | int error; |
Christoph Hellwig | dff35fd | 2008-08-13 16:44:15 +1000 | [diff] [blame] | 647 | timespec_t tv; |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 648 | int filestreams = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | |
| 650 | /* |
| 651 | * Call the space management code to pick |
| 652 | * the on-disk inode to be allocated. |
| 653 | */ |
David Chinner | b11f94d | 2007-07-11 11:09:33 +1000 | [diff] [blame] | 654 | error = xfs_dialloc(tp, pip ? pip->i_ino : 0, mode, okalloc, |
Christoph Hellwig | 0835890 | 2012-07-04 10:54:47 -0400 | [diff] [blame] | 655 | ialloc_context, &ino); |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 656 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | return error; |
Christoph Hellwig | 0835890 | 2012-07-04 10:54:47 -0400 | [diff] [blame] | 658 | if (*ialloc_context || ino == NULLFSINO) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | *ipp = NULL; |
| 660 | return 0; |
| 661 | } |
| 662 | ASSERT(*ialloc_context == NULL); |
| 663 | |
| 664 | /* |
| 665 | * Get the in-core inode with the lock held exclusively. |
| 666 | * This is because we're setting fields here we need |
| 667 | * to prevent others from looking at until we're done. |
| 668 | */ |
Christoph Hellwig | 93848a9 | 2013-04-03 16:11:17 +1100 | [diff] [blame] | 669 | error = xfs_iget(mp, tp, ino, XFS_IGET_CREATE, |
Christoph Hellwig | ec3ba85 | 2011-02-13 13:26:42 +0000 | [diff] [blame] | 670 | XFS_ILOCK_EXCL, &ip); |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 671 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | ASSERT(ip != NULL); |
| 674 | |
Al Viro | 576b1d6 | 2011-07-26 02:50:15 -0400 | [diff] [blame] | 675 | ip->i_d.di_mode = mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | ip->i_d.di_onlink = 0; |
| 677 | ip->i_d.di_nlink = nlink; |
| 678 | ASSERT(ip->i_d.di_nlink == nlink); |
Dwight Engen | 7aab1b2 | 2013-08-15 14:08:01 -0400 | [diff] [blame] | 679 | ip->i_d.di_uid = xfs_kuid_to_uid(current_fsuid()); |
| 680 | ip->i_d.di_gid = xfs_kgid_to_gid(current_fsgid()); |
Arkadiusz Mi?kiewicz | 6743099 | 2010-09-26 06:10:18 +0000 | [diff] [blame] | 681 | xfs_set_projid(ip, prid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad)); |
| 683 | |
| 684 | /* |
| 685 | * If the superblock version is up to where we support new format |
| 686 | * inodes and this is currently an old format inode, then change |
| 687 | * the inode version number now. This way we only do the conversion |
| 688 | * here rather than here and in the flush/logging code. |
| 689 | */ |
Christoph Hellwig | 93848a9 | 2013-04-03 16:11:17 +1100 | [diff] [blame] | 690 | if (xfs_sb_version_hasnlink(&mp->m_sb) && |
Christoph Hellwig | 51ce16d | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 691 | ip->i_d.di_version == 1) { |
| 692 | ip->i_d.di_version = 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | /* |
| 694 | * We've already zeroed the old link count, the projid field, |
| 695 | * and the pad field. |
| 696 | */ |
| 697 | } |
| 698 | |
| 699 | /* |
| 700 | * Project ids won't be stored on disk if we are using a version 1 inode. |
| 701 | */ |
Christoph Hellwig | 51ce16d | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 702 | if ((prid != 0) && (ip->i_d.di_version == 1)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | xfs_bump_ino_vers2(tp, ip); |
| 704 | |
Christoph Hellwig | bd186aa | 2007-08-30 17:21:12 +1000 | [diff] [blame] | 705 | if (pip && XFS_INHERIT_GID(pip)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | ip->i_d.di_gid = pip->i_d.di_gid; |
Al Viro | abbede1 | 2011-07-26 02:31:30 -0400 | [diff] [blame] | 707 | if ((pip->i_d.di_mode & S_ISGID) && S_ISDIR(mode)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | ip->i_d.di_mode |= S_ISGID; |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | /* |
| 713 | * If the group ID of the new file does not match the effective group |
| 714 | * ID or one of the supplementary group IDs, the S_ISGID bit is cleared |
| 715 | * (and only if the irix_sgid_inherit compatibility variable is set). |
| 716 | */ |
| 717 | if ((irix_sgid_inherit) && |
| 718 | (ip->i_d.di_mode & S_ISGID) && |
Dwight Engen | 7aab1b2 | 2013-08-15 14:08:01 -0400 | [diff] [blame] | 719 | (!in_group_p(xfs_gid_to_kgid(ip->i_d.di_gid)))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | ip->i_d.di_mode &= ~S_ISGID; |
| 721 | } |
| 722 | |
| 723 | ip->i_d.di_size = 0; |
| 724 | ip->i_d.di_nextents = 0; |
| 725 | ASSERT(ip->i_d.di_nblocks == 0); |
Christoph Hellwig | dff35fd | 2008-08-13 16:44:15 +1000 | [diff] [blame] | 726 | |
| 727 | nanotime(&tv); |
| 728 | ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec; |
| 729 | ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec; |
| 730 | ip->i_d.di_atime = ip->i_d.di_mtime; |
| 731 | ip->i_d.di_ctime = ip->i_d.di_mtime; |
| 732 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | /* |
| 734 | * di_gen will have been taken care of in xfs_iread. |
| 735 | */ |
| 736 | ip->i_d.di_extsize = 0; |
| 737 | ip->i_d.di_dmevmask = 0; |
| 738 | ip->i_d.di_dmstate = 0; |
| 739 | ip->i_d.di_flags = 0; |
Christoph Hellwig | 93848a9 | 2013-04-03 16:11:17 +1100 | [diff] [blame] | 740 | |
| 741 | if (ip->i_d.di_version == 3) { |
| 742 | ASSERT(ip->i_d.di_ino == ino); |
| 743 | ASSERT(uuid_equal(&ip->i_d.di_uuid, &mp->m_sb.sb_uuid)); |
| 744 | ip->i_d.di_crc = 0; |
| 745 | ip->i_d.di_changecount = 1; |
| 746 | ip->i_d.di_lsn = 0; |
| 747 | ip->i_d.di_flags2 = 0; |
| 748 | memset(&(ip->i_d.di_pad2[0]), 0, sizeof(ip->i_d.di_pad2)); |
| 749 | ip->i_d.di_crtime = ip->i_d.di_mtime; |
| 750 | } |
| 751 | |
| 752 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | flags = XFS_ILOG_CORE; |
| 754 | switch (mode & S_IFMT) { |
| 755 | case S_IFIFO: |
| 756 | case S_IFCHR: |
| 757 | case S_IFBLK: |
| 758 | case S_IFSOCK: |
| 759 | ip->i_d.di_format = XFS_DINODE_FMT_DEV; |
| 760 | ip->i_df.if_u2.if_rdev = rdev; |
| 761 | ip->i_df.if_flags = 0; |
| 762 | flags |= XFS_ILOG_DEV; |
| 763 | break; |
| 764 | case S_IFREG: |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 765 | /* |
| 766 | * we can't set up filestreams until after the VFS inode |
| 767 | * is set up properly. |
| 768 | */ |
| 769 | if (pip && xfs_inode_is_filestream(pip)) |
| 770 | filestreams = 1; |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 771 | /* fall through */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | case S_IFDIR: |
David Chinner | b11f94d | 2007-07-11 11:09:33 +1000 | [diff] [blame] | 773 | if (pip && (pip->i_d.di_flags & XFS_DIFLAG_ANY)) { |
Nathan Scott | 365ca83 | 2005-06-21 15:39:12 +1000 | [diff] [blame] | 774 | uint di_flags = 0; |
| 775 | |
Al Viro | abbede1 | 2011-07-26 02:31:30 -0400 | [diff] [blame] | 776 | if (S_ISDIR(mode)) { |
Nathan Scott | 365ca83 | 2005-06-21 15:39:12 +1000 | [diff] [blame] | 777 | if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT) |
| 778 | di_flags |= XFS_DIFLAG_RTINHERIT; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 779 | if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) { |
| 780 | di_flags |= XFS_DIFLAG_EXTSZINHERIT; |
| 781 | ip->i_d.di_extsize = pip->i_d.di_extsize; |
| 782 | } |
Al Viro | abbede1 | 2011-07-26 02:31:30 -0400 | [diff] [blame] | 783 | } else if (S_ISREG(mode)) { |
Christoph Hellwig | 613d704 | 2007-10-11 17:44:08 +1000 | [diff] [blame] | 784 | if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT) |
Nathan Scott | 365ca83 | 2005-06-21 15:39:12 +1000 | [diff] [blame] | 785 | di_flags |= XFS_DIFLAG_REALTIME; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 786 | if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) { |
| 787 | di_flags |= XFS_DIFLAG_EXTSIZE; |
| 788 | ip->i_d.di_extsize = pip->i_d.di_extsize; |
| 789 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | } |
| 791 | if ((pip->i_d.di_flags & XFS_DIFLAG_NOATIME) && |
| 792 | xfs_inherit_noatime) |
Nathan Scott | 365ca83 | 2005-06-21 15:39:12 +1000 | [diff] [blame] | 793 | di_flags |= XFS_DIFLAG_NOATIME; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | if ((pip->i_d.di_flags & XFS_DIFLAG_NODUMP) && |
| 795 | xfs_inherit_nodump) |
Nathan Scott | 365ca83 | 2005-06-21 15:39:12 +1000 | [diff] [blame] | 796 | di_flags |= XFS_DIFLAG_NODUMP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | if ((pip->i_d.di_flags & XFS_DIFLAG_SYNC) && |
| 798 | xfs_inherit_sync) |
Nathan Scott | 365ca83 | 2005-06-21 15:39:12 +1000 | [diff] [blame] | 799 | di_flags |= XFS_DIFLAG_SYNC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | if ((pip->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) && |
| 801 | xfs_inherit_nosymlinks) |
Nathan Scott | 365ca83 | 2005-06-21 15:39:12 +1000 | [diff] [blame] | 802 | di_flags |= XFS_DIFLAG_NOSYMLINKS; |
| 803 | if (pip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) |
| 804 | di_flags |= XFS_DIFLAG_PROJINHERIT; |
Barry Naujok | d3446ea | 2006-06-09 14:54:19 +1000 | [diff] [blame] | 805 | if ((pip->i_d.di_flags & XFS_DIFLAG_NODEFRAG) && |
| 806 | xfs_inherit_nodefrag) |
| 807 | di_flags |= XFS_DIFLAG_NODEFRAG; |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 808 | if (pip->i_d.di_flags & XFS_DIFLAG_FILESTREAM) |
| 809 | di_flags |= XFS_DIFLAG_FILESTREAM; |
Nathan Scott | 365ca83 | 2005-06-21 15:39:12 +1000 | [diff] [blame] | 810 | ip->i_d.di_flags |= di_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | } |
| 812 | /* FALLTHROUGH */ |
| 813 | case S_IFLNK: |
| 814 | ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS; |
| 815 | ip->i_df.if_flags = XFS_IFEXTENTS; |
| 816 | ip->i_df.if_bytes = ip->i_df.if_real_bytes = 0; |
| 817 | ip->i_df.if_u1.if_extents = NULL; |
| 818 | break; |
| 819 | default: |
| 820 | ASSERT(0); |
| 821 | } |
| 822 | /* |
| 823 | * Attribute fork settings for new inode. |
| 824 | */ |
| 825 | ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS; |
| 826 | ip->i_d.di_anextents = 0; |
| 827 | |
| 828 | /* |
| 829 | * Log the new values stuffed into the inode. |
| 830 | */ |
Christoph Hellwig | ddc3415 | 2011-09-19 15:00:54 +0000 | [diff] [blame] | 831 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | xfs_trans_log_inode(tp, ip, flags); |
| 833 | |
Nathan Scott | b83bd13 | 2006-06-09 16:48:30 +1000 | [diff] [blame] | 834 | /* 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] | 835 | xfs_setup_inode(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 837 | /* now we have set up the vfs inode we can associate the filestream */ |
| 838 | if (filestreams) { |
| 839 | error = xfs_filestream_associate(pip, ip); |
| 840 | if (error < 0) |
| 841 | return -error; |
| 842 | if (!error) |
| 843 | xfs_iflags_set(ip, XFS_IFILESTREAM); |
| 844 | } |
| 845 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | *ipp = ip; |
| 847 | return 0; |
| 848 | } |
| 849 | |
Dave Chinner | e546cb7 | 2013-08-12 20:49:47 +1000 | [diff] [blame] | 850 | /* |
| 851 | * Allocates a new inode from disk and return a pointer to the |
| 852 | * incore copy. This routine will internally commit the current |
| 853 | * transaction and allocate a new one if the Space Manager needed |
| 854 | * to do an allocation to replenish the inode free-list. |
| 855 | * |
| 856 | * This routine is designed to be called from xfs_create and |
| 857 | * xfs_create_dir. |
| 858 | * |
| 859 | */ |
| 860 | int |
| 861 | xfs_dir_ialloc( |
| 862 | xfs_trans_t **tpp, /* input: current transaction; |
| 863 | output: may be a new transaction. */ |
| 864 | xfs_inode_t *dp, /* directory within whose allocate |
| 865 | the inode. */ |
| 866 | umode_t mode, |
| 867 | xfs_nlink_t nlink, |
| 868 | xfs_dev_t rdev, |
| 869 | prid_t prid, /* project id */ |
| 870 | int okalloc, /* ok to allocate new space */ |
| 871 | xfs_inode_t **ipp, /* pointer to inode; it will be |
| 872 | locked. */ |
| 873 | int *committed) |
| 874 | |
| 875 | { |
| 876 | xfs_trans_t *tp; |
| 877 | xfs_trans_t *ntp; |
| 878 | xfs_inode_t *ip; |
| 879 | xfs_buf_t *ialloc_context = NULL; |
| 880 | int code; |
Dave Chinner | e546cb7 | 2013-08-12 20:49:47 +1000 | [diff] [blame] | 881 | void *dqinfo; |
| 882 | uint tflags; |
| 883 | |
| 884 | tp = *tpp; |
| 885 | ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES); |
| 886 | |
| 887 | /* |
| 888 | * xfs_ialloc will return a pointer to an incore inode if |
| 889 | * the Space Manager has an available inode on the free |
| 890 | * list. Otherwise, it will do an allocation and replenish |
| 891 | * the freelist. Since we can only do one allocation per |
| 892 | * transaction without deadlocks, we will need to commit the |
| 893 | * current transaction and start a new one. We will then |
| 894 | * need to call xfs_ialloc again to get the inode. |
| 895 | * |
| 896 | * If xfs_ialloc did an allocation to replenish the freelist, |
| 897 | * it returns the bp containing the head of the freelist as |
| 898 | * ialloc_context. We will hold a lock on it across the |
| 899 | * transaction commit so that no other process can steal |
| 900 | * the inode(s) that we've just allocated. |
| 901 | */ |
| 902 | code = xfs_ialloc(tp, dp, mode, nlink, rdev, prid, okalloc, |
| 903 | &ialloc_context, &ip); |
| 904 | |
| 905 | /* |
| 906 | * Return an error if we were unable to allocate a new inode. |
| 907 | * This should only happen if we run out of space on disk or |
| 908 | * encounter a disk error. |
| 909 | */ |
| 910 | if (code) { |
| 911 | *ipp = NULL; |
| 912 | return code; |
| 913 | } |
| 914 | if (!ialloc_context && !ip) { |
| 915 | *ipp = NULL; |
| 916 | return XFS_ERROR(ENOSPC); |
| 917 | } |
| 918 | |
| 919 | /* |
| 920 | * If the AGI buffer is non-NULL, then we were unable to get an |
| 921 | * inode in one operation. We need to commit the current |
| 922 | * transaction and call xfs_ialloc() again. It is guaranteed |
| 923 | * to succeed the second time. |
| 924 | */ |
| 925 | if (ialloc_context) { |
Jie Liu | 3d3c8b5 | 2013-08-12 20:49:59 +1000 | [diff] [blame] | 926 | struct xfs_trans_res tres; |
| 927 | |
Dave Chinner | e546cb7 | 2013-08-12 20:49:47 +1000 | [diff] [blame] | 928 | /* |
| 929 | * Normally, xfs_trans_commit releases all the locks. |
| 930 | * We call bhold to hang on to the ialloc_context across |
| 931 | * the commit. Holding this buffer prevents any other |
| 932 | * processes from doing any allocations in this |
| 933 | * allocation group. |
| 934 | */ |
| 935 | xfs_trans_bhold(tp, ialloc_context); |
| 936 | /* |
| 937 | * Save the log reservation so we can use |
| 938 | * them in the next transaction. |
| 939 | */ |
Jie Liu | 3d3c8b5 | 2013-08-12 20:49:59 +1000 | [diff] [blame] | 940 | tres.tr_logres = xfs_trans_get_log_res(tp); |
| 941 | tres.tr_logcount = xfs_trans_get_log_count(tp); |
Dave Chinner | e546cb7 | 2013-08-12 20:49:47 +1000 | [diff] [blame] | 942 | |
| 943 | /* |
| 944 | * We want the quota changes to be associated with the next |
| 945 | * transaction, NOT this one. So, detach the dqinfo from this |
| 946 | * and attach it to the next transaction. |
| 947 | */ |
| 948 | dqinfo = NULL; |
| 949 | tflags = 0; |
| 950 | if (tp->t_dqinfo) { |
| 951 | dqinfo = (void *)tp->t_dqinfo; |
| 952 | tp->t_dqinfo = NULL; |
| 953 | tflags = tp->t_flags & XFS_TRANS_DQ_DIRTY; |
| 954 | tp->t_flags &= ~(XFS_TRANS_DQ_DIRTY); |
| 955 | } |
| 956 | |
| 957 | ntp = xfs_trans_dup(tp); |
| 958 | code = xfs_trans_commit(tp, 0); |
| 959 | tp = ntp; |
| 960 | if (committed != NULL) { |
| 961 | *committed = 1; |
| 962 | } |
| 963 | /* |
| 964 | * If we get an error during the commit processing, |
| 965 | * release the buffer that is still held and return |
| 966 | * to the caller. |
| 967 | */ |
| 968 | if (code) { |
| 969 | xfs_buf_relse(ialloc_context); |
| 970 | if (dqinfo) { |
| 971 | tp->t_dqinfo = dqinfo; |
| 972 | xfs_trans_free_dqinfo(tp); |
| 973 | } |
| 974 | *tpp = ntp; |
| 975 | *ipp = NULL; |
| 976 | return code; |
| 977 | } |
| 978 | |
| 979 | /* |
| 980 | * transaction commit worked ok so we can drop the extra ticket |
| 981 | * reference that we gained in xfs_trans_dup() |
| 982 | */ |
| 983 | xfs_log_ticket_put(tp->t_ticket); |
Jie Liu | 3d3c8b5 | 2013-08-12 20:49:59 +1000 | [diff] [blame] | 984 | tres.tr_logflags = XFS_TRANS_PERM_LOG_RES; |
| 985 | code = xfs_trans_reserve(tp, &tres, 0, 0); |
| 986 | |
Dave Chinner | e546cb7 | 2013-08-12 20:49:47 +1000 | [diff] [blame] | 987 | /* |
| 988 | * Re-attach the quota info that we detached from prev trx. |
| 989 | */ |
| 990 | if (dqinfo) { |
| 991 | tp->t_dqinfo = dqinfo; |
| 992 | tp->t_flags |= tflags; |
| 993 | } |
| 994 | |
| 995 | if (code) { |
| 996 | xfs_buf_relse(ialloc_context); |
| 997 | *tpp = ntp; |
| 998 | *ipp = NULL; |
| 999 | return code; |
| 1000 | } |
| 1001 | xfs_trans_bjoin(tp, ialloc_context); |
| 1002 | |
| 1003 | /* |
| 1004 | * Call ialloc again. Since we've locked out all |
| 1005 | * other allocations in this allocation group, |
| 1006 | * this call should always succeed. |
| 1007 | */ |
| 1008 | code = xfs_ialloc(tp, dp, mode, nlink, rdev, prid, |
| 1009 | okalloc, &ialloc_context, &ip); |
| 1010 | |
| 1011 | /* |
| 1012 | * If we get an error at this point, return to the caller |
| 1013 | * so that the current transaction can be aborted. |
| 1014 | */ |
| 1015 | if (code) { |
| 1016 | *tpp = tp; |
| 1017 | *ipp = NULL; |
| 1018 | return code; |
| 1019 | } |
| 1020 | ASSERT(!ialloc_context && ip); |
| 1021 | |
| 1022 | } else { |
| 1023 | if (committed != NULL) |
| 1024 | *committed = 0; |
| 1025 | } |
| 1026 | |
| 1027 | *ipp = ip; |
| 1028 | *tpp = tp; |
| 1029 | |
| 1030 | return 0; |
| 1031 | } |
| 1032 | |
| 1033 | /* |
| 1034 | * Decrement the link count on an inode & log the change. |
| 1035 | * If this causes the link count to go to zero, initiate the |
| 1036 | * logging activity required to truncate a file. |
| 1037 | */ |
| 1038 | int /* error */ |
| 1039 | xfs_droplink( |
| 1040 | xfs_trans_t *tp, |
| 1041 | xfs_inode_t *ip) |
| 1042 | { |
| 1043 | int error; |
| 1044 | |
| 1045 | xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG); |
| 1046 | |
| 1047 | ASSERT (ip->i_d.di_nlink > 0); |
| 1048 | ip->i_d.di_nlink--; |
| 1049 | drop_nlink(VFS_I(ip)); |
| 1050 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 1051 | |
| 1052 | error = 0; |
| 1053 | if (ip->i_d.di_nlink == 0) { |
| 1054 | /* |
| 1055 | * We're dropping the last link to this file. |
| 1056 | * Move the on-disk inode to the AGI unlinked list. |
| 1057 | * From xfs_inactive() we will pull the inode from |
| 1058 | * the list and free it. |
| 1059 | */ |
| 1060 | error = xfs_iunlink(tp, ip); |
| 1061 | } |
| 1062 | return error; |
| 1063 | } |
| 1064 | |
| 1065 | /* |
| 1066 | * This gets called when the inode's version needs to be changed from 1 to 2. |
| 1067 | * Currently this happens when the nlink field overflows the old 16-bit value |
| 1068 | * or when chproj is called to change the project for the first time. |
| 1069 | * As a side effect the superblock version will also get rev'd |
| 1070 | * to contain the NLINK bit. |
| 1071 | */ |
| 1072 | void |
| 1073 | xfs_bump_ino_vers2( |
| 1074 | xfs_trans_t *tp, |
| 1075 | xfs_inode_t *ip) |
| 1076 | { |
| 1077 | xfs_mount_t *mp; |
| 1078 | |
| 1079 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
| 1080 | ASSERT(ip->i_d.di_version == 1); |
| 1081 | |
| 1082 | ip->i_d.di_version = 2; |
| 1083 | ip->i_d.di_onlink = 0; |
| 1084 | memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad)); |
| 1085 | mp = tp->t_mountp; |
| 1086 | if (!xfs_sb_version_hasnlink(&mp->m_sb)) { |
| 1087 | spin_lock(&mp->m_sb_lock); |
| 1088 | if (!xfs_sb_version_hasnlink(&mp->m_sb)) { |
| 1089 | xfs_sb_version_addnlink(&mp->m_sb); |
| 1090 | spin_unlock(&mp->m_sb_lock); |
| 1091 | xfs_mod_sb(tp, XFS_SB_VERSIONNUM); |
| 1092 | } else { |
| 1093 | spin_unlock(&mp->m_sb_lock); |
| 1094 | } |
| 1095 | } |
| 1096 | /* Caller must log the inode */ |
| 1097 | } |
| 1098 | |
| 1099 | /* |
| 1100 | * Increment the link count on an inode & log the change. |
| 1101 | */ |
| 1102 | int |
| 1103 | xfs_bumplink( |
| 1104 | xfs_trans_t *tp, |
| 1105 | xfs_inode_t *ip) |
| 1106 | { |
| 1107 | xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG); |
| 1108 | |
| 1109 | ASSERT(ip->i_d.di_nlink > 0); |
| 1110 | ip->i_d.di_nlink++; |
| 1111 | inc_nlink(VFS_I(ip)); |
| 1112 | if ((ip->i_d.di_version == 1) && |
| 1113 | (ip->i_d.di_nlink > XFS_MAXLINK_1)) { |
| 1114 | /* |
| 1115 | * The inode has increased its number of links beyond |
| 1116 | * what can fit in an old format inode. It now needs |
| 1117 | * to be converted to a version 2 inode with a 32 bit |
| 1118 | * link count. If this is the first inode in the file |
| 1119 | * system to do this, then we need to bump the superblock |
| 1120 | * version number as well. |
| 1121 | */ |
| 1122 | xfs_bump_ino_vers2(tp, ip); |
| 1123 | } |
| 1124 | |
| 1125 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 1126 | return 0; |
| 1127 | } |
| 1128 | |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 1129 | int |
| 1130 | xfs_create( |
| 1131 | xfs_inode_t *dp, |
| 1132 | struct xfs_name *name, |
| 1133 | umode_t mode, |
| 1134 | xfs_dev_t rdev, |
| 1135 | xfs_inode_t **ipp) |
| 1136 | { |
| 1137 | int is_dir = S_ISDIR(mode); |
| 1138 | struct xfs_mount *mp = dp->i_mount; |
| 1139 | struct xfs_inode *ip = NULL; |
| 1140 | struct xfs_trans *tp = NULL; |
| 1141 | int error; |
| 1142 | xfs_bmap_free_t free_list; |
| 1143 | xfs_fsblock_t first_block; |
| 1144 | bool unlock_dp_on_error = false; |
| 1145 | uint cancel_flags; |
| 1146 | int committed; |
| 1147 | prid_t prid; |
| 1148 | struct xfs_dquot *udqp = NULL; |
| 1149 | struct xfs_dquot *gdqp = NULL; |
| 1150 | struct xfs_dquot *pdqp = NULL; |
Jie Liu | 3d3c8b5 | 2013-08-12 20:49:59 +1000 | [diff] [blame] | 1151 | struct xfs_trans_res tres; |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 1152 | uint resblks; |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 1153 | |
| 1154 | trace_xfs_create(dp, name); |
| 1155 | |
| 1156 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 1157 | return XFS_ERROR(EIO); |
| 1158 | |
| 1159 | if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) |
| 1160 | prid = xfs_get_projid(dp); |
| 1161 | else |
| 1162 | prid = XFS_PROJID_DEFAULT; |
| 1163 | |
| 1164 | /* |
| 1165 | * Make sure that we have allocated dquot(s) on disk. |
| 1166 | */ |
Dwight Engen | 7aab1b2 | 2013-08-15 14:08:01 -0400 | [diff] [blame] | 1167 | error = xfs_qm_vop_dqalloc(dp, xfs_kuid_to_uid(current_fsuid()), |
| 1168 | xfs_kgid_to_gid(current_fsgid()), prid, |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 1169 | XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, |
| 1170 | &udqp, &gdqp, &pdqp); |
| 1171 | if (error) |
| 1172 | return error; |
| 1173 | |
| 1174 | if (is_dir) { |
| 1175 | rdev = 0; |
| 1176 | resblks = XFS_MKDIR_SPACE_RES(mp, name->len); |
Jie Liu | 3d3c8b5 | 2013-08-12 20:49:59 +1000 | [diff] [blame] | 1177 | tres.tr_logres = M_RES(mp)->tr_mkdir.tr_logres; |
| 1178 | tres.tr_logcount = XFS_MKDIR_LOG_COUNT; |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 1179 | tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR); |
| 1180 | } else { |
| 1181 | resblks = XFS_CREATE_SPACE_RES(mp, name->len); |
Jie Liu | 3d3c8b5 | 2013-08-12 20:49:59 +1000 | [diff] [blame] | 1182 | tres.tr_logres = M_RES(mp)->tr_create.tr_logres; |
| 1183 | tres.tr_logcount = XFS_CREATE_LOG_COUNT; |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 1184 | tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE); |
| 1185 | } |
| 1186 | |
| 1187 | cancel_flags = XFS_TRANS_RELEASE_LOG_RES; |
| 1188 | |
| 1189 | /* |
| 1190 | * Initially assume that the file does not exist and |
| 1191 | * reserve the resources for that case. If that is not |
| 1192 | * the case we'll drop the one we have and get a more |
| 1193 | * appropriate transaction later. |
| 1194 | */ |
Jie Liu | 3d3c8b5 | 2013-08-12 20:49:59 +1000 | [diff] [blame] | 1195 | tres.tr_logflags = XFS_TRANS_PERM_LOG_RES; |
| 1196 | error = xfs_trans_reserve(tp, &tres, resblks, 0); |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 1197 | if (error == ENOSPC) { |
| 1198 | /* flush outstanding delalloc blocks and retry */ |
| 1199 | xfs_flush_inodes(mp); |
Jie Liu | 3d3c8b5 | 2013-08-12 20:49:59 +1000 | [diff] [blame] | 1200 | error = xfs_trans_reserve(tp, &tres, resblks, 0); |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 1201 | } |
| 1202 | if (error == ENOSPC) { |
| 1203 | /* No space at all so try a "no-allocation" reservation */ |
| 1204 | resblks = 0; |
Jie Liu | 3d3c8b5 | 2013-08-12 20:49:59 +1000 | [diff] [blame] | 1205 | error = xfs_trans_reserve(tp, &tres, 0, 0); |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 1206 | } |
| 1207 | if (error) { |
| 1208 | cancel_flags = 0; |
| 1209 | goto out_trans_cancel; |
| 1210 | } |
| 1211 | |
| 1212 | xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT); |
| 1213 | unlock_dp_on_error = true; |
| 1214 | |
| 1215 | xfs_bmap_init(&free_list, &first_block); |
| 1216 | |
| 1217 | /* |
| 1218 | * Reserve disk quota and the inode. |
| 1219 | */ |
| 1220 | error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, |
| 1221 | pdqp, resblks, 1, 0); |
| 1222 | if (error) |
| 1223 | goto out_trans_cancel; |
| 1224 | |
| 1225 | error = xfs_dir_canenter(tp, dp, name, resblks); |
| 1226 | if (error) |
| 1227 | goto out_trans_cancel; |
| 1228 | |
| 1229 | /* |
| 1230 | * A newly created regular or special file just has one directory |
| 1231 | * entry pointing to them, but a directory also the "." entry |
| 1232 | * pointing to itself. |
| 1233 | */ |
| 1234 | error = xfs_dir_ialloc(&tp, dp, mode, is_dir ? 2 : 1, rdev, |
| 1235 | prid, resblks > 0, &ip, &committed); |
| 1236 | if (error) { |
| 1237 | if (error == ENOSPC) |
| 1238 | goto out_trans_cancel; |
| 1239 | goto out_trans_abort; |
| 1240 | } |
| 1241 | |
| 1242 | /* |
| 1243 | * Now we join the directory inode to the transaction. We do not do it |
| 1244 | * earlier because xfs_dir_ialloc might commit the previous transaction |
| 1245 | * (and release all the locks). An error from here on will result in |
| 1246 | * the transaction cancel unlocking dp so don't do it explicitly in the |
| 1247 | * error path. |
| 1248 | */ |
| 1249 | xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL); |
| 1250 | unlock_dp_on_error = false; |
| 1251 | |
| 1252 | error = xfs_dir_createname(tp, dp, name, ip->i_ino, |
| 1253 | &first_block, &free_list, resblks ? |
| 1254 | resblks - XFS_IALLOC_SPACE_RES(mp) : 0); |
| 1255 | if (error) { |
| 1256 | ASSERT(error != ENOSPC); |
| 1257 | goto out_trans_abort; |
| 1258 | } |
| 1259 | xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); |
| 1260 | xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE); |
| 1261 | |
| 1262 | if (is_dir) { |
| 1263 | error = xfs_dir_init(tp, ip, dp); |
| 1264 | if (error) |
| 1265 | goto out_bmap_cancel; |
| 1266 | |
| 1267 | error = xfs_bumplink(tp, dp); |
| 1268 | if (error) |
| 1269 | goto out_bmap_cancel; |
| 1270 | } |
| 1271 | |
| 1272 | /* |
| 1273 | * If this is a synchronous mount, make sure that the |
| 1274 | * create transaction goes to disk before returning to |
| 1275 | * the user. |
| 1276 | */ |
| 1277 | if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) |
| 1278 | xfs_trans_set_sync(tp); |
| 1279 | |
| 1280 | /* |
| 1281 | * Attach the dquot(s) to the inodes and modify them incore. |
| 1282 | * These ids of the inode couldn't have changed since the new |
| 1283 | * inode has been locked ever since it was created. |
| 1284 | */ |
| 1285 | xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp); |
| 1286 | |
| 1287 | error = xfs_bmap_finish(&tp, &free_list, &committed); |
| 1288 | if (error) |
| 1289 | goto out_bmap_cancel; |
| 1290 | |
| 1291 | error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); |
| 1292 | if (error) |
| 1293 | goto out_release_inode; |
| 1294 | |
| 1295 | xfs_qm_dqrele(udqp); |
| 1296 | xfs_qm_dqrele(gdqp); |
| 1297 | xfs_qm_dqrele(pdqp); |
| 1298 | |
| 1299 | *ipp = ip; |
| 1300 | return 0; |
| 1301 | |
| 1302 | out_bmap_cancel: |
| 1303 | xfs_bmap_cancel(&free_list); |
| 1304 | out_trans_abort: |
| 1305 | cancel_flags |= XFS_TRANS_ABORT; |
| 1306 | out_trans_cancel: |
| 1307 | xfs_trans_cancel(tp, cancel_flags); |
| 1308 | out_release_inode: |
| 1309 | /* |
| 1310 | * Wait until after the current transaction is aborted to |
| 1311 | * release the inode. This prevents recursive transactions |
| 1312 | * and deadlocks from xfs_inactive. |
| 1313 | */ |
| 1314 | if (ip) |
| 1315 | IRELE(ip); |
| 1316 | |
| 1317 | xfs_qm_dqrele(udqp); |
| 1318 | xfs_qm_dqrele(gdqp); |
| 1319 | xfs_qm_dqrele(pdqp); |
| 1320 | |
| 1321 | if (unlock_dp_on_error) |
| 1322 | xfs_iunlock(dp, XFS_ILOCK_EXCL); |
| 1323 | return error; |
| 1324 | } |
| 1325 | |
| 1326 | int |
| 1327 | xfs_link( |
| 1328 | xfs_inode_t *tdp, |
| 1329 | xfs_inode_t *sip, |
| 1330 | struct xfs_name *target_name) |
| 1331 | { |
| 1332 | xfs_mount_t *mp = tdp->i_mount; |
| 1333 | xfs_trans_t *tp; |
| 1334 | int error; |
| 1335 | xfs_bmap_free_t free_list; |
| 1336 | xfs_fsblock_t first_block; |
| 1337 | int cancel_flags; |
| 1338 | int committed; |
| 1339 | int resblks; |
| 1340 | |
| 1341 | trace_xfs_link(tdp, target_name); |
| 1342 | |
| 1343 | ASSERT(!S_ISDIR(sip->i_d.di_mode)); |
| 1344 | |
| 1345 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 1346 | return XFS_ERROR(EIO); |
| 1347 | |
| 1348 | error = xfs_qm_dqattach(sip, 0); |
| 1349 | if (error) |
| 1350 | goto std_return; |
| 1351 | |
| 1352 | error = xfs_qm_dqattach(tdp, 0); |
| 1353 | if (error) |
| 1354 | goto std_return; |
| 1355 | |
| 1356 | tp = xfs_trans_alloc(mp, XFS_TRANS_LINK); |
| 1357 | cancel_flags = XFS_TRANS_RELEASE_LOG_RES; |
| 1358 | resblks = XFS_LINK_SPACE_RES(mp, target_name->len); |
Jie Liu | 3d3c8b5 | 2013-08-12 20:49:59 +1000 | [diff] [blame] | 1359 | error = xfs_trans_reserve(tp, &M_RES(mp)->tr_link, resblks, 0); |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 1360 | if (error == ENOSPC) { |
| 1361 | resblks = 0; |
Jie Liu | 3d3c8b5 | 2013-08-12 20:49:59 +1000 | [diff] [blame] | 1362 | error = xfs_trans_reserve(tp, &M_RES(mp)->tr_link, 0, 0); |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 1363 | } |
| 1364 | if (error) { |
| 1365 | cancel_flags = 0; |
| 1366 | goto error_return; |
| 1367 | } |
| 1368 | |
| 1369 | xfs_lock_two_inodes(sip, tdp, XFS_ILOCK_EXCL); |
| 1370 | |
| 1371 | xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL); |
| 1372 | xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL); |
| 1373 | |
| 1374 | /* |
| 1375 | * If we are using project inheritance, we only allow hard link |
| 1376 | * creation in our tree when the project IDs are the same; else |
| 1377 | * the tree quota mechanism could be circumvented. |
| 1378 | */ |
| 1379 | if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) && |
| 1380 | (xfs_get_projid(tdp) != xfs_get_projid(sip)))) { |
| 1381 | error = XFS_ERROR(EXDEV); |
| 1382 | goto error_return; |
| 1383 | } |
| 1384 | |
| 1385 | error = xfs_dir_canenter(tp, tdp, target_name, resblks); |
| 1386 | if (error) |
| 1387 | goto error_return; |
| 1388 | |
| 1389 | xfs_bmap_init(&free_list, &first_block); |
| 1390 | |
| 1391 | error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino, |
| 1392 | &first_block, &free_list, resblks); |
| 1393 | if (error) |
| 1394 | goto abort_return; |
| 1395 | xfs_trans_ichgtime(tp, tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); |
| 1396 | xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE); |
| 1397 | |
| 1398 | error = xfs_bumplink(tp, sip); |
| 1399 | if (error) |
| 1400 | goto abort_return; |
| 1401 | |
| 1402 | /* |
| 1403 | * If this is a synchronous mount, make sure that the |
| 1404 | * link transaction goes to disk before returning to |
| 1405 | * the user. |
| 1406 | */ |
| 1407 | if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) { |
| 1408 | xfs_trans_set_sync(tp); |
| 1409 | } |
| 1410 | |
| 1411 | error = xfs_bmap_finish (&tp, &free_list, &committed); |
| 1412 | if (error) { |
| 1413 | xfs_bmap_cancel(&free_list); |
| 1414 | goto abort_return; |
| 1415 | } |
| 1416 | |
| 1417 | return xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); |
| 1418 | |
| 1419 | abort_return: |
| 1420 | cancel_flags |= XFS_TRANS_ABORT; |
| 1421 | error_return: |
| 1422 | xfs_trans_cancel(tp, cancel_flags); |
| 1423 | std_return: |
| 1424 | return error; |
| 1425 | } |
| 1426 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1427 | /* |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 1428 | * Free up the underlying blocks past new_size. The new size must be smaller |
| 1429 | * than the current size. This routine can be used both for the attribute and |
| 1430 | * 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] | 1431 | * |
David Chinner | f648505 | 2008-04-17 16:50:04 +1000 | [diff] [blame] | 1432 | * The transaction passed to this routine must have made a permanent log |
| 1433 | * reservation of at least XFS_ITRUNCATE_LOG_RES. This routine may commit the |
| 1434 | * given transaction and start new ones, so make sure everything involved in |
| 1435 | * the transaction is tidy before calling here. Some transaction will be |
| 1436 | * returned to the caller to be committed. The incoming transaction must |
| 1437 | * already include the inode, and both inode locks must be held exclusively. |
| 1438 | * The inode must also be "held" within the transaction. On return the inode |
| 1439 | * will be "held" within the returned transaction. This routine does NOT |
| 1440 | * require any disk space to be reserved for it within the transaction. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1441 | * |
David Chinner | f648505 | 2008-04-17 16:50:04 +1000 | [diff] [blame] | 1442 | * If we get an error, we must return with the inode locked and linked into the |
| 1443 | * current transaction. This keeps things simple for the higher level code, |
| 1444 | * because it always knows that the inode is locked and held in the transaction |
| 1445 | * that returns to it whether errors occur or not. We don't mark the inode |
| 1446 | * dirty on error so that transactions can be easily aborted if possible. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1447 | */ |
| 1448 | int |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 1449 | xfs_itruncate_extents( |
| 1450 | struct xfs_trans **tpp, |
| 1451 | struct xfs_inode *ip, |
| 1452 | int whichfork, |
| 1453 | xfs_fsize_t new_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1454 | { |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 1455 | struct xfs_mount *mp = ip->i_mount; |
| 1456 | struct xfs_trans *tp = *tpp; |
| 1457 | struct xfs_trans *ntp; |
| 1458 | xfs_bmap_free_t free_list; |
| 1459 | xfs_fsblock_t first_block; |
| 1460 | xfs_fileoff_t first_unmap_block; |
| 1461 | xfs_fileoff_t last_block; |
| 1462 | xfs_filblks_t unmap_len; |
| 1463 | int committed; |
| 1464 | int error = 0; |
| 1465 | int done = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1466 | |
Christoph Hellwig | 0b56185 | 2012-07-04 11:13:31 -0400 | [diff] [blame] | 1467 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
| 1468 | ASSERT(!atomic_read(&VFS_I(ip)->i_count) || |
| 1469 | xfs_isilocked(ip, XFS_IOLOCK_EXCL)); |
Christoph Hellwig | ce7ae151 | 2011-12-18 20:00:11 +0000 | [diff] [blame] | 1470 | ASSERT(new_size <= XFS_ISIZE(ip)); |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 1471 | ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | ASSERT(ip->i_itemp != NULL); |
Christoph Hellwig | 898621d | 2010-06-24 11:36:58 +1000 | [diff] [blame] | 1473 | ASSERT(ip->i_itemp->ili_lock_flags == 0); |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 1474 | ASSERT(!XFS_NOT_DQATTACHED(mp, ip)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1475 | |
Christoph Hellwig | 673e8e5 | 2011-12-18 20:00:04 +0000 | [diff] [blame] | 1476 | trace_xfs_itruncate_extents_start(ip, new_size); |
| 1477 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1478 | /* |
| 1479 | * Since it is possible for space to become allocated beyond |
| 1480 | * the end of the file (in a crash where the space is allocated |
| 1481 | * but the inode size is not yet updated), simply remove any |
| 1482 | * blocks which show up between the new EOF and the maximum |
| 1483 | * possible file size. If the first block to be removed is |
| 1484 | * beyond the maximum file size (ie it is the same as last_block), |
| 1485 | * then there is nothing to do. |
| 1486 | */ |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 1487 | 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] | 1488 | last_block = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes); |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 1489 | if (first_unmap_block == last_block) |
| 1490 | return 0; |
| 1491 | |
| 1492 | ASSERT(first_unmap_block < last_block); |
| 1493 | unmap_len = last_block - first_unmap_block + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1494 | while (!done) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1495 | xfs_bmap_init(&free_list, &first_block); |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 1496 | error = xfs_bunmapi(tp, ip, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1497 | first_unmap_block, unmap_len, |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 1498 | xfs_bmapi_aflag(whichfork), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1499 | XFS_ITRUNC_MAX_EXTENTS, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1500 | &first_block, &free_list, |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 1501 | &done); |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 1502 | if (error) |
| 1503 | goto out_bmap_cancel; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | |
| 1505 | /* |
| 1506 | * Duplicate the transaction that has the permanent |
| 1507 | * reservation and commit the old transaction. |
| 1508 | */ |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 1509 | error = xfs_bmap_finish(&tp, &free_list, &committed); |
Christoph Hellwig | 898621d | 2010-06-24 11:36:58 +1000 | [diff] [blame] | 1510 | if (committed) |
Christoph Hellwig | ddc3415 | 2011-09-19 15:00:54 +0000 | [diff] [blame] | 1511 | xfs_trans_ijoin(tp, ip, 0); |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 1512 | if (error) |
| 1513 | goto out_bmap_cancel; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1514 | |
| 1515 | if (committed) { |
| 1516 | /* |
David Chinner | f648505 | 2008-04-17 16:50:04 +1000 | [diff] [blame] | 1517 | * Mark the inode dirty so it will be logged and |
David Chinner | e5720ee | 2008-04-10 12:21:18 +1000 | [diff] [blame] | 1518 | * moved forward in the log as part of every commit. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1519 | */ |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 1520 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1521 | } |
David Chinner | f648505 | 2008-04-17 16:50:04 +1000 | [diff] [blame] | 1522 | |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 1523 | ntp = xfs_trans_dup(tp); |
| 1524 | error = xfs_trans_commit(tp, 0); |
| 1525 | tp = ntp; |
David Chinner | e5720ee | 2008-04-10 12:21:18 +1000 | [diff] [blame] | 1526 | |
Christoph Hellwig | ddc3415 | 2011-09-19 15:00:54 +0000 | [diff] [blame] | 1527 | xfs_trans_ijoin(tp, ip, 0); |
David Chinner | f648505 | 2008-04-17 16:50:04 +1000 | [diff] [blame] | 1528 | |
Dave Chinner | cc09c0d | 2008-11-17 17:37:10 +1100 | [diff] [blame] | 1529 | if (error) |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 1530 | goto out; |
| 1531 | |
Dave Chinner | cc09c0d | 2008-11-17 17:37:10 +1100 | [diff] [blame] | 1532 | /* |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 1533 | * Transaction commit worked ok so we can drop the extra ticket |
Dave Chinner | cc09c0d | 2008-11-17 17:37:10 +1100 | [diff] [blame] | 1534 | * reference that we gained in xfs_trans_dup() |
| 1535 | */ |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 1536 | xfs_log_ticket_put(tp->t_ticket); |
Jie Liu | 3d3c8b5 | 2013-08-12 20:49:59 +1000 | [diff] [blame] | 1537 | error = xfs_trans_reserve(tp, &M_RES(mp)->tr_itruncate, 0, 0); |
David Chinner | f648505 | 2008-04-17 16:50:04 +1000 | [diff] [blame] | 1538 | if (error) |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 1539 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1540 | } |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 1541 | |
Christoph Hellwig | 673e8e5 | 2011-12-18 20:00:04 +0000 | [diff] [blame] | 1542 | /* |
| 1543 | * Always re-log the inode so that our permanent transaction can keep |
| 1544 | * on rolling it forward in the log. |
| 1545 | */ |
| 1546 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 1547 | |
| 1548 | trace_xfs_itruncate_extents_end(ip, new_size); |
| 1549 | |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 1550 | out: |
| 1551 | *tpp = tp; |
| 1552 | return error; |
| 1553 | out_bmap_cancel: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1554 | /* |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 1555 | * If the bunmapi call encounters an error, return to the caller where |
| 1556 | * the transaction can be properly aborted. We just need to make sure |
| 1557 | * 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] | 1558 | */ |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 1559 | xfs_bmap_cancel(&free_list); |
| 1560 | goto out; |
| 1561 | } |
| 1562 | |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 1563 | int |
| 1564 | xfs_release( |
| 1565 | xfs_inode_t *ip) |
| 1566 | { |
| 1567 | xfs_mount_t *mp = ip->i_mount; |
| 1568 | int error; |
| 1569 | |
| 1570 | if (!S_ISREG(ip->i_d.di_mode) || (ip->i_d.di_mode == 0)) |
| 1571 | return 0; |
| 1572 | |
| 1573 | /* If this is a read-only mount, don't do this (would generate I/O) */ |
| 1574 | if (mp->m_flags & XFS_MOUNT_RDONLY) |
| 1575 | return 0; |
| 1576 | |
| 1577 | if (!XFS_FORCED_SHUTDOWN(mp)) { |
| 1578 | int truncated; |
| 1579 | |
| 1580 | /* |
| 1581 | * If we are using filestreams, and we have an unlinked |
| 1582 | * file that we are processing the last close on, then nothing |
| 1583 | * will be able to reopen and write to this file. Purge this |
| 1584 | * inode from the filestreams cache so that it doesn't delay |
| 1585 | * teardown of the inode. |
| 1586 | */ |
| 1587 | if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip)) |
| 1588 | xfs_filestream_deassociate(ip); |
| 1589 | |
| 1590 | /* |
| 1591 | * If we previously truncated this file and removed old data |
| 1592 | * in the process, we want to initiate "early" writeout on |
| 1593 | * the last close. This is an attempt to combat the notorious |
| 1594 | * NULL files problem which is particularly noticeable from a |
| 1595 | * truncate down, buffered (re-)write (delalloc), followed by |
| 1596 | * a crash. What we are effectively doing here is |
| 1597 | * significantly reducing the time window where we'd otherwise |
| 1598 | * be exposed to that problem. |
| 1599 | */ |
| 1600 | truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED); |
| 1601 | if (truncated) { |
| 1602 | xfs_iflags_clear(ip, XFS_IDIRTY_RELEASE); |
| 1603 | if (VN_DIRTY(VFS_I(ip)) && ip->i_delayed_blks > 0) { |
| 1604 | error = -filemap_flush(VFS_I(ip)->i_mapping); |
| 1605 | if (error) |
| 1606 | return error; |
| 1607 | } |
| 1608 | } |
| 1609 | } |
| 1610 | |
| 1611 | if (ip->i_d.di_nlink == 0) |
| 1612 | return 0; |
| 1613 | |
| 1614 | if (xfs_can_free_eofblocks(ip, false)) { |
| 1615 | |
| 1616 | /* |
| 1617 | * If we can't get the iolock just skip truncating the blocks |
| 1618 | * past EOF because we could deadlock with the mmap_sem |
| 1619 | * otherwise. We'll get another chance to drop them once the |
| 1620 | * last reference to the inode is dropped, so we'll never leak |
| 1621 | * blocks permanently. |
| 1622 | * |
| 1623 | * Further, check if the inode is being opened, written and |
| 1624 | * closed frequently and we have delayed allocation blocks |
| 1625 | * outstanding (e.g. streaming writes from the NFS server), |
| 1626 | * truncating the blocks past EOF will cause fragmentation to |
| 1627 | * occur. |
| 1628 | * |
| 1629 | * In this case don't do the truncation, either, but we have to |
| 1630 | * be careful how we detect this case. Blocks beyond EOF show |
| 1631 | * up as i_delayed_blks even when the inode is clean, so we |
| 1632 | * need to truncate them away first before checking for a dirty |
| 1633 | * release. Hence on the first dirty close we will still remove |
| 1634 | * the speculative allocation, but after that we will leave it |
| 1635 | * in place. |
| 1636 | */ |
| 1637 | if (xfs_iflags_test(ip, XFS_IDIRTY_RELEASE)) |
| 1638 | return 0; |
| 1639 | |
| 1640 | error = xfs_free_eofblocks(mp, ip, true); |
| 1641 | if (error && error != EAGAIN) |
| 1642 | return error; |
| 1643 | |
| 1644 | /* delalloc blocks after truncation means it really is dirty */ |
| 1645 | if (ip->i_delayed_blks) |
| 1646 | xfs_iflags_set(ip, XFS_IDIRTY_RELEASE); |
| 1647 | } |
| 1648 | return 0; |
| 1649 | } |
| 1650 | |
| 1651 | /* |
Brian Foster | f7be2d7 | 2013-09-20 11:06:10 -0400 | [diff] [blame] | 1652 | * xfs_inactive_truncate |
| 1653 | * |
| 1654 | * Called to perform a truncate when an inode becomes unlinked. |
| 1655 | */ |
| 1656 | STATIC int |
| 1657 | xfs_inactive_truncate( |
| 1658 | struct xfs_inode *ip) |
| 1659 | { |
| 1660 | struct xfs_mount *mp = ip->i_mount; |
| 1661 | struct xfs_trans *tp; |
| 1662 | int error; |
| 1663 | |
| 1664 | tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE); |
| 1665 | error = xfs_trans_reserve(tp, &M_RES(mp)->tr_itruncate, 0, 0); |
| 1666 | if (error) { |
| 1667 | ASSERT(XFS_FORCED_SHUTDOWN(mp)); |
| 1668 | xfs_trans_cancel(tp, 0); |
| 1669 | return error; |
| 1670 | } |
| 1671 | |
| 1672 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 1673 | xfs_trans_ijoin(tp, ip, 0); |
| 1674 | |
| 1675 | /* |
| 1676 | * Log the inode size first to prevent stale data exposure in the event |
| 1677 | * of a system crash before the truncate completes. See the related |
| 1678 | * comment in xfs_setattr_size() for details. |
| 1679 | */ |
| 1680 | ip->i_d.di_size = 0; |
| 1681 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 1682 | |
| 1683 | error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, 0); |
| 1684 | if (error) |
| 1685 | goto error_trans_cancel; |
| 1686 | |
| 1687 | ASSERT(ip->i_d.di_nextents == 0); |
| 1688 | |
| 1689 | error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); |
| 1690 | if (error) |
| 1691 | goto error_unlock; |
| 1692 | |
| 1693 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 1694 | return 0; |
| 1695 | |
| 1696 | error_trans_cancel: |
| 1697 | xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT); |
| 1698 | error_unlock: |
| 1699 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 1700 | return error; |
| 1701 | } |
| 1702 | |
| 1703 | /* |
Brian Foster | 88877d2 | 2013-09-20 11:06:11 -0400 | [diff] [blame] | 1704 | * xfs_inactive_ifree() |
| 1705 | * |
| 1706 | * Perform the inode free when an inode is unlinked. |
| 1707 | */ |
| 1708 | STATIC int |
| 1709 | xfs_inactive_ifree( |
| 1710 | struct xfs_inode *ip) |
| 1711 | { |
| 1712 | xfs_bmap_free_t free_list; |
| 1713 | xfs_fsblock_t first_block; |
| 1714 | int committed; |
| 1715 | struct xfs_mount *mp = ip->i_mount; |
| 1716 | struct xfs_trans *tp; |
| 1717 | int error; |
| 1718 | |
| 1719 | tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE); |
| 1720 | error = xfs_trans_reserve(tp, &M_RES(mp)->tr_ifree, 0, 0); |
| 1721 | if (error) { |
| 1722 | ASSERT(XFS_FORCED_SHUTDOWN(mp)); |
| 1723 | xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES); |
| 1724 | return error; |
| 1725 | } |
| 1726 | |
| 1727 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 1728 | xfs_trans_ijoin(tp, ip, 0); |
| 1729 | |
| 1730 | xfs_bmap_init(&free_list, &first_block); |
| 1731 | error = xfs_ifree(tp, ip, &free_list); |
| 1732 | if (error) { |
| 1733 | /* |
| 1734 | * If we fail to free the inode, shut down. The cancel |
| 1735 | * might do that, we need to make sure. Otherwise the |
| 1736 | * inode might be lost for a long time or forever. |
| 1737 | */ |
| 1738 | if (!XFS_FORCED_SHUTDOWN(mp)) { |
| 1739 | xfs_notice(mp, "%s: xfs_ifree returned error %d", |
| 1740 | __func__, error); |
| 1741 | xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR); |
| 1742 | } |
| 1743 | xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT); |
| 1744 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 1745 | return error; |
| 1746 | } |
| 1747 | |
| 1748 | /* |
| 1749 | * Credit the quota account(s). The inode is gone. |
| 1750 | */ |
| 1751 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, -1); |
| 1752 | |
| 1753 | /* |
| 1754 | * Just ignore errors at this point. There is nothing we can |
| 1755 | * do except to try to keep going. Make sure it's not a silent |
| 1756 | * error. |
| 1757 | */ |
| 1758 | error = xfs_bmap_finish(&tp, &free_list, &committed); |
| 1759 | if (error) |
| 1760 | xfs_notice(mp, "%s: xfs_bmap_finish returned error %d", |
| 1761 | __func__, error); |
| 1762 | error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); |
| 1763 | if (error) |
| 1764 | xfs_notice(mp, "%s: xfs_trans_commit returned error %d", |
| 1765 | __func__, error); |
| 1766 | |
| 1767 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 1768 | return 0; |
| 1769 | } |
| 1770 | |
| 1771 | /* |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 1772 | * xfs_inactive |
| 1773 | * |
| 1774 | * This is called when the vnode reference count for the vnode |
| 1775 | * goes to zero. If the file has been unlinked, then it must |
| 1776 | * now be truncated. Also, we clear all of the read-ahead state |
| 1777 | * kept for the inode here since the file is now closed. |
| 1778 | */ |
Brian Foster | 74564fb | 2013-09-20 11:06:12 -0400 | [diff] [blame] | 1779 | void |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 1780 | xfs_inactive( |
| 1781 | xfs_inode_t *ip) |
| 1782 | { |
Jie Liu | 3d3c8b5 | 2013-08-12 20:49:59 +1000 | [diff] [blame] | 1783 | struct xfs_mount *mp; |
Jie Liu | 3d3c8b5 | 2013-08-12 20:49:59 +1000 | [diff] [blame] | 1784 | int error; |
| 1785 | int truncate = 0; |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 1786 | |
| 1787 | /* |
| 1788 | * If the inode is already free, then there can be nothing |
| 1789 | * to clean up here. |
| 1790 | */ |
Ben Myers | d948709 | 2013-09-10 18:11:22 -0500 | [diff] [blame] | 1791 | if (ip->i_d.di_mode == 0) { |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 1792 | ASSERT(ip->i_df.if_real_bytes == 0); |
| 1793 | ASSERT(ip->i_df.if_broot_bytes == 0); |
Brian Foster | 74564fb | 2013-09-20 11:06:12 -0400 | [diff] [blame] | 1794 | return; |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 1795 | } |
| 1796 | |
| 1797 | mp = ip->i_mount; |
| 1798 | |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 1799 | /* If this is a read-only mount, don't do this (would generate I/O) */ |
| 1800 | if (mp->m_flags & XFS_MOUNT_RDONLY) |
Brian Foster | 74564fb | 2013-09-20 11:06:12 -0400 | [diff] [blame] | 1801 | return; |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 1802 | |
| 1803 | if (ip->i_d.di_nlink != 0) { |
| 1804 | /* |
| 1805 | * force is true because we are evicting an inode from the |
| 1806 | * cache. Post-eof blocks must be freed, lest we end up with |
| 1807 | * broken free space accounting. |
| 1808 | */ |
Brian Foster | 74564fb | 2013-09-20 11:06:12 -0400 | [diff] [blame] | 1809 | if (xfs_can_free_eofblocks(ip, true)) |
| 1810 | xfs_free_eofblocks(mp, ip, false); |
| 1811 | |
| 1812 | return; |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 1813 | } |
| 1814 | |
| 1815 | if (S_ISREG(ip->i_d.di_mode) && |
| 1816 | (ip->i_d.di_size != 0 || XFS_ISIZE(ip) != 0 || |
| 1817 | ip->i_d.di_nextents > 0 || ip->i_delayed_blks > 0)) |
| 1818 | truncate = 1; |
| 1819 | |
| 1820 | error = xfs_qm_dqattach(ip, 0); |
| 1821 | if (error) |
Brian Foster | 74564fb | 2013-09-20 11:06:12 -0400 | [diff] [blame] | 1822 | return; |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 1823 | |
Brian Foster | f7be2d7 | 2013-09-20 11:06:10 -0400 | [diff] [blame] | 1824 | if (S_ISLNK(ip->i_d.di_mode)) |
Brian Foster | 36b21dd | 2013-09-20 11:06:09 -0400 | [diff] [blame] | 1825 | error = xfs_inactive_symlink(ip); |
Brian Foster | f7be2d7 | 2013-09-20 11:06:10 -0400 | [diff] [blame] | 1826 | else if (truncate) |
| 1827 | error = xfs_inactive_truncate(ip); |
| 1828 | if (error) |
Brian Foster | 74564fb | 2013-09-20 11:06:12 -0400 | [diff] [blame] | 1829 | return; |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 1830 | |
| 1831 | /* |
| 1832 | * If there are attributes associated with the file then blow them away |
| 1833 | * now. The code calls a routine that recursively deconstructs the |
| 1834 | * attribute fork. We need to just commit the current transaction |
| 1835 | * because we can't use it for xfs_attr_inactive(). |
| 1836 | */ |
| 1837 | if (ip->i_d.di_anextents > 0) { |
| 1838 | ASSERT(ip->i_d.di_forkoff != 0); |
| 1839 | |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 1840 | error = xfs_attr_inactive(ip); |
| 1841 | if (error) |
Brian Foster | 74564fb | 2013-09-20 11:06:12 -0400 | [diff] [blame] | 1842 | return; |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 1843 | } |
| 1844 | |
| 1845 | if (ip->i_afp) |
| 1846 | xfs_idestroy_fork(ip, XFS_ATTR_FORK); |
| 1847 | |
| 1848 | ASSERT(ip->i_d.di_anextents == 0); |
| 1849 | |
| 1850 | /* |
| 1851 | * Free the inode. |
| 1852 | */ |
Brian Foster | 88877d2 | 2013-09-20 11:06:11 -0400 | [diff] [blame] | 1853 | error = xfs_inactive_ifree(ip); |
| 1854 | if (error) |
Brian Foster | 74564fb | 2013-09-20 11:06:12 -0400 | [diff] [blame] | 1855 | return; |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 1856 | |
| 1857 | /* |
| 1858 | * Release the dquots held by inode, if any. |
| 1859 | */ |
| 1860 | xfs_qm_dqdetach(ip); |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 1861 | } |
| 1862 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1863 | /* |
| 1864 | * This is called when the inode's link count goes to 0. |
| 1865 | * We place the on-disk inode on a list in the AGI. It |
| 1866 | * will be pulled from this list when the inode is freed. |
| 1867 | */ |
| 1868 | int |
| 1869 | xfs_iunlink( |
| 1870 | xfs_trans_t *tp, |
| 1871 | xfs_inode_t *ip) |
| 1872 | { |
| 1873 | xfs_mount_t *mp; |
| 1874 | xfs_agi_t *agi; |
| 1875 | xfs_dinode_t *dip; |
| 1876 | xfs_buf_t *agibp; |
| 1877 | xfs_buf_t *ibp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1878 | xfs_agino_t agino; |
| 1879 | short bucket_index; |
| 1880 | int offset; |
| 1881 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1882 | |
| 1883 | ASSERT(ip->i_d.di_nlink == 0); |
| 1884 | ASSERT(ip->i_d.di_mode != 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1885 | |
| 1886 | mp = tp->t_mountp; |
| 1887 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1888 | /* |
| 1889 | * Get the agi buffer first. It ensures lock ordering |
| 1890 | * on the list. |
| 1891 | */ |
Christoph Hellwig | 5e1be0f | 2008-11-28 14:23:37 +1100 | [diff] [blame] | 1892 | 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] | 1893 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1894 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1895 | agi = XFS_BUF_TO_AGI(agibp); |
Christoph Hellwig | 5e1be0f | 2008-11-28 14:23:37 +1100 | [diff] [blame] | 1896 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1897 | /* |
| 1898 | * Get the index into the agi hash table for the |
| 1899 | * list this inode will go on. |
| 1900 | */ |
| 1901 | agino = XFS_INO_TO_AGINO(mp, ip->i_ino); |
| 1902 | ASSERT(agino != 0); |
| 1903 | bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS; |
| 1904 | ASSERT(agi->agi_unlinked[bucket_index]); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1905 | ASSERT(be32_to_cpu(agi->agi_unlinked[bucket_index]) != agino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1906 | |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 1907 | if (agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1908 | /* |
| 1909 | * There is already another inode in the bucket we need |
| 1910 | * to add ourselves to. Add us at the front of the list. |
| 1911 | * Here we put the head pointer into our next pointer, |
| 1912 | * and then we fall through to point the head at us. |
| 1913 | */ |
Christoph Hellwig | 475ee41 | 2012-07-03 12:21:22 -0400 | [diff] [blame] | 1914 | error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp, |
| 1915 | 0, 0); |
Vlad Apostolov | c319b58 | 2007-11-23 16:27:51 +1100 | [diff] [blame] | 1916 | if (error) |
| 1917 | return error; |
| 1918 | |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 1919 | ASSERT(dip->di_next_unlinked == cpu_to_be32(NULLAGINO)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1920 | dip->di_next_unlinked = agi->agi_unlinked[bucket_index]; |
Christoph Hellwig | 92bfc6e | 2008-11-28 14:23:41 +1100 | [diff] [blame] | 1921 | offset = ip->i_imap.im_boffset + |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1922 | offsetof(xfs_dinode_t, di_next_unlinked); |
Dave Chinner | 0a32c26 | 2013-06-05 12:09:08 +1000 | [diff] [blame] | 1923 | |
| 1924 | /* need to recalc the inode CRC if appropriate */ |
| 1925 | xfs_dinode_calc_crc(mp, dip); |
| 1926 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1927 | xfs_trans_inode_buf(tp, ibp); |
| 1928 | xfs_trans_log_buf(tp, ibp, offset, |
| 1929 | (offset + sizeof(xfs_agino_t) - 1)); |
| 1930 | xfs_inobp_check(mp, ibp); |
| 1931 | } |
| 1932 | |
| 1933 | /* |
| 1934 | * Point the bucket head pointer at the inode being inserted. |
| 1935 | */ |
| 1936 | ASSERT(agino != 0); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1937 | agi->agi_unlinked[bucket_index] = cpu_to_be32(agino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1938 | offset = offsetof(xfs_agi_t, agi_unlinked) + |
| 1939 | (sizeof(xfs_agino_t) * bucket_index); |
| 1940 | xfs_trans_log_buf(tp, agibp, offset, |
| 1941 | (offset + sizeof(xfs_agino_t) - 1)); |
| 1942 | return 0; |
| 1943 | } |
| 1944 | |
| 1945 | /* |
| 1946 | * Pull the on-disk inode from the AGI unlinked list. |
| 1947 | */ |
| 1948 | STATIC int |
| 1949 | xfs_iunlink_remove( |
| 1950 | xfs_trans_t *tp, |
| 1951 | xfs_inode_t *ip) |
| 1952 | { |
| 1953 | xfs_ino_t next_ino; |
| 1954 | xfs_mount_t *mp; |
| 1955 | xfs_agi_t *agi; |
| 1956 | xfs_dinode_t *dip; |
| 1957 | xfs_buf_t *agibp; |
| 1958 | xfs_buf_t *ibp; |
| 1959 | xfs_agnumber_t agno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1960 | xfs_agino_t agino; |
| 1961 | xfs_agino_t next_agino; |
| 1962 | xfs_buf_t *last_ibp; |
Nathan Scott | 6fdf8cc | 2006-06-28 10:13:52 +1000 | [diff] [blame] | 1963 | xfs_dinode_t *last_dip = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1964 | short bucket_index; |
Nathan Scott | 6fdf8cc | 2006-06-28 10:13:52 +1000 | [diff] [blame] | 1965 | int offset, last_offset = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1966 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1967 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1968 | mp = tp->t_mountp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1969 | agno = XFS_INO_TO_AGNO(mp, ip->i_ino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1970 | |
| 1971 | /* |
| 1972 | * Get the agi buffer first. It ensures lock ordering |
| 1973 | * on the list. |
| 1974 | */ |
Christoph Hellwig | 5e1be0f | 2008-11-28 14:23:37 +1100 | [diff] [blame] | 1975 | error = xfs_read_agi(mp, tp, agno, &agibp); |
| 1976 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1977 | return error; |
Christoph Hellwig | 5e1be0f | 2008-11-28 14:23:37 +1100 | [diff] [blame] | 1978 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1979 | agi = XFS_BUF_TO_AGI(agibp); |
Christoph Hellwig | 5e1be0f | 2008-11-28 14:23:37 +1100 | [diff] [blame] | 1980 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1981 | /* |
| 1982 | * Get the index into the agi hash table for the |
| 1983 | * list this inode will go on. |
| 1984 | */ |
| 1985 | agino = XFS_INO_TO_AGINO(mp, ip->i_ino); |
| 1986 | ASSERT(agino != 0); |
| 1987 | bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS; |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 1988 | ASSERT(agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1989 | ASSERT(agi->agi_unlinked[bucket_index]); |
| 1990 | |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1991 | if (be32_to_cpu(agi->agi_unlinked[bucket_index]) == agino) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1992 | /* |
Christoph Hellwig | 475ee41 | 2012-07-03 12:21:22 -0400 | [diff] [blame] | 1993 | * We're at the head of the list. Get the inode's on-disk |
| 1994 | * buffer to see if there is anyone after us on the list. |
| 1995 | * Only modify our next pointer if it is not already NULLAGINO. |
| 1996 | * This saves us the overhead of dealing with the buffer when |
| 1997 | * there is no need to change it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1998 | */ |
Christoph Hellwig | 475ee41 | 2012-07-03 12:21:22 -0400 | [diff] [blame] | 1999 | error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp, |
| 2000 | 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2001 | if (error) { |
Christoph Hellwig | 475ee41 | 2012-07-03 12:21:22 -0400 | [diff] [blame] | 2002 | xfs_warn(mp, "%s: xfs_imap_to_bp returned error %d.", |
Dave Chinner | 0b932cc | 2011-03-07 10:08:35 +1100 | [diff] [blame] | 2003 | __func__, error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2004 | return error; |
| 2005 | } |
Christoph Hellwig | 347d1c0 | 2007-08-28 13:57:51 +1000 | [diff] [blame] | 2006 | next_agino = be32_to_cpu(dip->di_next_unlinked); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2007 | ASSERT(next_agino != 0); |
| 2008 | if (next_agino != NULLAGINO) { |
Christoph Hellwig | 347d1c0 | 2007-08-28 13:57:51 +1000 | [diff] [blame] | 2009 | dip->di_next_unlinked = cpu_to_be32(NULLAGINO); |
Christoph Hellwig | 92bfc6e | 2008-11-28 14:23:41 +1100 | [diff] [blame] | 2010 | offset = ip->i_imap.im_boffset + |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2011 | offsetof(xfs_dinode_t, di_next_unlinked); |
Dave Chinner | 0a32c26 | 2013-06-05 12:09:08 +1000 | [diff] [blame] | 2012 | |
| 2013 | /* need to recalc the inode CRC if appropriate */ |
| 2014 | xfs_dinode_calc_crc(mp, dip); |
| 2015 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2016 | xfs_trans_inode_buf(tp, ibp); |
| 2017 | xfs_trans_log_buf(tp, ibp, offset, |
| 2018 | (offset + sizeof(xfs_agino_t) - 1)); |
| 2019 | xfs_inobp_check(mp, ibp); |
| 2020 | } else { |
| 2021 | xfs_trans_brelse(tp, ibp); |
| 2022 | } |
| 2023 | /* |
| 2024 | * Point the bucket head pointer at the next inode. |
| 2025 | */ |
| 2026 | ASSERT(next_agino != 0); |
| 2027 | ASSERT(next_agino != agino); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 2028 | agi->agi_unlinked[bucket_index] = cpu_to_be32(next_agino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2029 | offset = offsetof(xfs_agi_t, agi_unlinked) + |
| 2030 | (sizeof(xfs_agino_t) * bucket_index); |
| 2031 | xfs_trans_log_buf(tp, agibp, offset, |
| 2032 | (offset + sizeof(xfs_agino_t) - 1)); |
| 2033 | } else { |
| 2034 | /* |
| 2035 | * We need to search the list for the inode being freed. |
| 2036 | */ |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 2037 | next_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2038 | last_ibp = NULL; |
| 2039 | while (next_agino != agino) { |
Christoph Hellwig | 129dbc9 | 2012-07-03 12:21:51 -0400 | [diff] [blame] | 2040 | struct xfs_imap imap; |
| 2041 | |
| 2042 | if (last_ibp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2043 | xfs_trans_brelse(tp, last_ibp); |
Christoph Hellwig | 129dbc9 | 2012-07-03 12:21:51 -0400 | [diff] [blame] | 2044 | |
| 2045 | imap.im_blkno = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2046 | next_ino = XFS_AGINO_TO_INO(mp, agno, next_agino); |
Christoph Hellwig | 129dbc9 | 2012-07-03 12:21:51 -0400 | [diff] [blame] | 2047 | |
| 2048 | error = xfs_imap(mp, tp, next_ino, &imap, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2049 | if (error) { |
Dave Chinner | 0b932cc | 2011-03-07 10:08:35 +1100 | [diff] [blame] | 2050 | xfs_warn(mp, |
Christoph Hellwig | 129dbc9 | 2012-07-03 12:21:51 -0400 | [diff] [blame] | 2051 | "%s: xfs_imap returned error %d.", |
| 2052 | __func__, error); |
| 2053 | return error; |
| 2054 | } |
| 2055 | |
| 2056 | error = xfs_imap_to_bp(mp, tp, &imap, &last_dip, |
| 2057 | &last_ibp, 0, 0); |
| 2058 | if (error) { |
| 2059 | xfs_warn(mp, |
| 2060 | "%s: xfs_imap_to_bp returned error %d.", |
Dave Chinner | 0b932cc | 2011-03-07 10:08:35 +1100 | [diff] [blame] | 2061 | __func__, error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2062 | return error; |
| 2063 | } |
Christoph Hellwig | 129dbc9 | 2012-07-03 12:21:51 -0400 | [diff] [blame] | 2064 | |
| 2065 | last_offset = imap.im_boffset; |
Christoph Hellwig | 347d1c0 | 2007-08-28 13:57:51 +1000 | [diff] [blame] | 2066 | next_agino = be32_to_cpu(last_dip->di_next_unlinked); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2067 | ASSERT(next_agino != NULLAGINO); |
| 2068 | ASSERT(next_agino != 0); |
| 2069 | } |
Christoph Hellwig | 475ee41 | 2012-07-03 12:21:22 -0400 | [diff] [blame] | 2070 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2071 | /* |
Christoph Hellwig | 475ee41 | 2012-07-03 12:21:22 -0400 | [diff] [blame] | 2072 | * Now last_ibp points to the buffer previous to us on the |
| 2073 | * unlinked list. Pull us from the list. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2074 | */ |
Christoph Hellwig | 475ee41 | 2012-07-03 12:21:22 -0400 | [diff] [blame] | 2075 | error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp, |
| 2076 | 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2077 | if (error) { |
Christoph Hellwig | 475ee41 | 2012-07-03 12:21:22 -0400 | [diff] [blame] | 2078 | xfs_warn(mp, "%s: xfs_imap_to_bp(2) returned error %d.", |
Dave Chinner | 0b932cc | 2011-03-07 10:08:35 +1100 | [diff] [blame] | 2079 | __func__, error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2080 | return error; |
| 2081 | } |
Christoph Hellwig | 347d1c0 | 2007-08-28 13:57:51 +1000 | [diff] [blame] | 2082 | next_agino = be32_to_cpu(dip->di_next_unlinked); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2083 | ASSERT(next_agino != 0); |
| 2084 | ASSERT(next_agino != agino); |
| 2085 | if (next_agino != NULLAGINO) { |
Christoph Hellwig | 347d1c0 | 2007-08-28 13:57:51 +1000 | [diff] [blame] | 2086 | dip->di_next_unlinked = cpu_to_be32(NULLAGINO); |
Christoph Hellwig | 92bfc6e | 2008-11-28 14:23:41 +1100 | [diff] [blame] | 2087 | offset = ip->i_imap.im_boffset + |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2088 | offsetof(xfs_dinode_t, di_next_unlinked); |
Dave Chinner | 0a32c26 | 2013-06-05 12:09:08 +1000 | [diff] [blame] | 2089 | |
| 2090 | /* need to recalc the inode CRC if appropriate */ |
| 2091 | xfs_dinode_calc_crc(mp, dip); |
| 2092 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2093 | xfs_trans_inode_buf(tp, ibp); |
| 2094 | xfs_trans_log_buf(tp, ibp, offset, |
| 2095 | (offset + sizeof(xfs_agino_t) - 1)); |
| 2096 | xfs_inobp_check(mp, ibp); |
| 2097 | } else { |
| 2098 | xfs_trans_brelse(tp, ibp); |
| 2099 | } |
| 2100 | /* |
| 2101 | * Point the previous inode on the list to the next inode. |
| 2102 | */ |
Christoph Hellwig | 347d1c0 | 2007-08-28 13:57:51 +1000 | [diff] [blame] | 2103 | last_dip->di_next_unlinked = cpu_to_be32(next_agino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2104 | ASSERT(next_agino != 0); |
| 2105 | offset = last_offset + offsetof(xfs_dinode_t, di_next_unlinked); |
Dave Chinner | 0a32c26 | 2013-06-05 12:09:08 +1000 | [diff] [blame] | 2106 | |
| 2107 | /* need to recalc the inode CRC if appropriate */ |
| 2108 | xfs_dinode_calc_crc(mp, last_dip); |
| 2109 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2110 | xfs_trans_inode_buf(tp, last_ibp); |
| 2111 | xfs_trans_log_buf(tp, last_ibp, offset, |
| 2112 | (offset + sizeof(xfs_agino_t) - 1)); |
| 2113 | xfs_inobp_check(mp, last_ibp); |
| 2114 | } |
| 2115 | return 0; |
| 2116 | } |
| 2117 | |
Dave Chinner | 5b3eed7 | 2010-08-24 11:42:41 +1000 | [diff] [blame] | 2118 | /* |
Zhi Yong Wu | 0b8182d | 2013-08-12 03:14:59 +0000 | [diff] [blame] | 2119 | * A big issue when freeing the inode cluster is that we _cannot_ skip any |
Dave Chinner | 5b3eed7 | 2010-08-24 11:42:41 +1000 | [diff] [blame] | 2120 | * inodes that are in memory - they all must be marked stale and attached to |
| 2121 | * the cluster buffer. |
| 2122 | */ |
Chandra Seetharaman | 2a30f36d | 2011-09-20 13:56:55 +0000 | [diff] [blame] | 2123 | STATIC int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2124 | xfs_ifree_cluster( |
| 2125 | xfs_inode_t *free_ip, |
| 2126 | xfs_trans_t *tp, |
| 2127 | xfs_ino_t inum) |
| 2128 | { |
| 2129 | xfs_mount_t *mp = free_ip->i_mount; |
| 2130 | int blks_per_cluster; |
| 2131 | int nbufs; |
| 2132 | int ninodes; |
Dave Chinner | 5b257b4 | 2010-06-03 16:22:29 +1000 | [diff] [blame] | 2133 | int i, j; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2134 | xfs_daddr_t blkno; |
| 2135 | xfs_buf_t *bp; |
Dave Chinner | 5b257b4 | 2010-06-03 16:22:29 +1000 | [diff] [blame] | 2136 | xfs_inode_t *ip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2137 | xfs_inode_log_item_t *iip; |
| 2138 | xfs_log_item_t *lip; |
Dave Chinner | 5017e97 | 2010-01-11 11:47:40 +0000 | [diff] [blame] | 2139 | struct xfs_perag *pag; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2140 | |
Dave Chinner | 5017e97 | 2010-01-11 11:47:40 +0000 | [diff] [blame] | 2141 | pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, inum)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2142 | if (mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) { |
| 2143 | blks_per_cluster = 1; |
| 2144 | ninodes = mp->m_sb.sb_inopblock; |
| 2145 | nbufs = XFS_IALLOC_BLOCKS(mp); |
| 2146 | } else { |
| 2147 | blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) / |
| 2148 | mp->m_sb.sb_blocksize; |
| 2149 | ninodes = blks_per_cluster * mp->m_sb.sb_inopblock; |
| 2150 | nbufs = XFS_IALLOC_BLOCKS(mp) / blks_per_cluster; |
| 2151 | } |
| 2152 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2153 | for (j = 0; j < nbufs; j++, inum += ninodes) { |
| 2154 | blkno = XFS_AGB_TO_DADDR(mp, XFS_INO_TO_AGNO(mp, inum), |
| 2155 | XFS_INO_TO_AGBNO(mp, inum)); |
| 2156 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2157 | /* |
Dave Chinner | 5b257b4 | 2010-06-03 16:22:29 +1000 | [diff] [blame] | 2158 | * We obtain and lock the backing buffer first in the process |
| 2159 | * here, as we have to ensure that any dirty inode that we |
| 2160 | * can't get the flush lock on is attached to the buffer. |
| 2161 | * If we scan the in-memory inodes first, then buffer IO can |
| 2162 | * complete before we get a lock on it, and hence we may fail |
| 2163 | * to mark all the active inodes on the buffer stale. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2164 | */ |
Dave Chinner | 5b257b4 | 2010-06-03 16:22:29 +1000 | [diff] [blame] | 2165 | bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno, |
Dave Chinner | b6aff29 | 2012-11-02 11:38:42 +1100 | [diff] [blame] | 2166 | mp->m_bsize * blks_per_cluster, |
| 2167 | XBF_UNMAPPED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2168 | |
Chandra Seetharaman | 2a30f36d | 2011-09-20 13:56:55 +0000 | [diff] [blame] | 2169 | if (!bp) |
| 2170 | return ENOMEM; |
Dave Chinner | b0f539d | 2012-11-14 17:53:49 +1100 | [diff] [blame] | 2171 | |
| 2172 | /* |
| 2173 | * This buffer may not have been correctly initialised as we |
| 2174 | * didn't read it from disk. That's not important because we are |
| 2175 | * only using to mark the buffer as stale in the log, and to |
| 2176 | * attach stale cached inodes on it. That means it will never be |
| 2177 | * dispatched for IO. If it is, we want to know about it, and we |
| 2178 | * want it to fail. We can acheive this by adding a write |
| 2179 | * verifier to the buffer. |
| 2180 | */ |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 2181 | bp->b_ops = &xfs_inode_buf_ops; |
Dave Chinner | b0f539d | 2012-11-14 17:53:49 +1100 | [diff] [blame] | 2182 | |
Dave Chinner | 5b257b4 | 2010-06-03 16:22:29 +1000 | [diff] [blame] | 2183 | /* |
| 2184 | * Walk the inodes already attached to the buffer and mark them |
| 2185 | * stale. These will all have the flush locks held, so an |
Dave Chinner | 5b3eed7 | 2010-08-24 11:42:41 +1000 | [diff] [blame] | 2186 | * in-memory inode walk can't lock them. By marking them all |
| 2187 | * stale first, we will not attempt to lock them in the loop |
| 2188 | * below as the XFS_ISTALE flag will be set. |
Dave Chinner | 5b257b4 | 2010-06-03 16:22:29 +1000 | [diff] [blame] | 2189 | */ |
Christoph Hellwig | adadbee | 2011-07-13 13:43:49 +0200 | [diff] [blame] | 2190 | lip = bp->b_fspriv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2191 | while (lip) { |
| 2192 | if (lip->li_type == XFS_LI_INODE) { |
| 2193 | iip = (xfs_inode_log_item_t *)lip; |
| 2194 | ASSERT(iip->ili_logged == 1); |
Christoph Hellwig | ca30b2a | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 2195 | lip->li_cb = xfs_istale_done; |
David Chinner | 7b2e2a3 | 2008-10-30 17:39:12 +1100 | [diff] [blame] | 2196 | xfs_trans_ail_copy_lsn(mp->m_ail, |
| 2197 | &iip->ili_flush_lsn, |
| 2198 | &iip->ili_item.li_lsn); |
David Chinner | e5ffd2b | 2006-11-21 18:55:33 +1100 | [diff] [blame] | 2199 | xfs_iflags_set(iip->ili_inode, XFS_ISTALE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2200 | } |
| 2201 | lip = lip->li_bio_list; |
| 2202 | } |
| 2203 | |
Dave Chinner | 5b3eed7 | 2010-08-24 11:42:41 +1000 | [diff] [blame] | 2204 | |
Dave Chinner | 5b257b4 | 2010-06-03 16:22:29 +1000 | [diff] [blame] | 2205 | /* |
| 2206 | * For each inode in memory attempt to add it to the inode |
| 2207 | * buffer and set it up for being staled on buffer IO |
| 2208 | * completion. This is safe as we've locked out tail pushing |
| 2209 | * and flushing by locking the buffer. |
| 2210 | * |
| 2211 | * We have already marked every inode that was part of a |
| 2212 | * transaction stale above, which means there is no point in |
| 2213 | * even trying to lock them. |
| 2214 | */ |
| 2215 | for (i = 0; i < ninodes; i++) { |
Dave Chinner | 5b3eed7 | 2010-08-24 11:42:41 +1000 | [diff] [blame] | 2216 | retry: |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 2217 | rcu_read_lock(); |
Dave Chinner | 5b257b4 | 2010-06-03 16:22:29 +1000 | [diff] [blame] | 2218 | ip = radix_tree_lookup(&pag->pag_ici_root, |
| 2219 | XFS_INO_TO_AGINO(mp, (inum + i))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2220 | |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 2221 | /* Inode not in memory, nothing to do */ |
| 2222 | if (!ip) { |
| 2223 | rcu_read_unlock(); |
Dave Chinner | 5b257b4 | 2010-06-03 16:22:29 +1000 | [diff] [blame] | 2224 | continue; |
| 2225 | } |
| 2226 | |
Dave Chinner | 5b3eed7 | 2010-08-24 11:42:41 +1000 | [diff] [blame] | 2227 | /* |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 2228 | * because this is an RCU protected lookup, we could |
| 2229 | * find a recently freed or even reallocated inode |
| 2230 | * during the lookup. We need to check under the |
| 2231 | * i_flags_lock for a valid inode here. Skip it if it |
| 2232 | * is not valid, the wrong inode or stale. |
| 2233 | */ |
| 2234 | spin_lock(&ip->i_flags_lock); |
| 2235 | if (ip->i_ino != inum + i || |
| 2236 | __xfs_iflags_test(ip, XFS_ISTALE)) { |
| 2237 | spin_unlock(&ip->i_flags_lock); |
| 2238 | rcu_read_unlock(); |
| 2239 | continue; |
| 2240 | } |
| 2241 | spin_unlock(&ip->i_flags_lock); |
| 2242 | |
| 2243 | /* |
Dave Chinner | 5b3eed7 | 2010-08-24 11:42:41 +1000 | [diff] [blame] | 2244 | * Don't try to lock/unlock the current inode, but we |
| 2245 | * _cannot_ skip the other inodes that we did not find |
| 2246 | * in the list attached to the buffer and are not |
| 2247 | * already marked stale. If we can't lock it, back off |
| 2248 | * and retry. |
| 2249 | */ |
Dave Chinner | 5b257b4 | 2010-06-03 16:22:29 +1000 | [diff] [blame] | 2250 | if (ip != free_ip && |
| 2251 | !xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) { |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 2252 | rcu_read_unlock(); |
Dave Chinner | 5b3eed7 | 2010-08-24 11:42:41 +1000 | [diff] [blame] | 2253 | delay(1); |
| 2254 | goto retry; |
Dave Chinner | 5b257b4 | 2010-06-03 16:22:29 +1000 | [diff] [blame] | 2255 | } |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 2256 | rcu_read_unlock(); |
Dave Chinner | 5b257b4 | 2010-06-03 16:22:29 +1000 | [diff] [blame] | 2257 | |
Dave Chinner | 5b3eed7 | 2010-08-24 11:42:41 +1000 | [diff] [blame] | 2258 | xfs_iflock(ip); |
Dave Chinner | 5b257b4 | 2010-06-03 16:22:29 +1000 | [diff] [blame] | 2259 | xfs_iflags_set(ip, XFS_ISTALE); |
Dave Chinner | 5b257b4 | 2010-06-03 16:22:29 +1000 | [diff] [blame] | 2260 | |
Dave Chinner | 5b3eed7 | 2010-08-24 11:42:41 +1000 | [diff] [blame] | 2261 | /* |
| 2262 | * we don't need to attach clean inodes or those only |
| 2263 | * with unlogged changes (which we throw away, anyway). |
| 2264 | */ |
Dave Chinner | 5b257b4 | 2010-06-03 16:22:29 +1000 | [diff] [blame] | 2265 | iip = ip->i_itemp; |
Dave Chinner | 5b3eed7 | 2010-08-24 11:42:41 +1000 | [diff] [blame] | 2266 | if (!iip || xfs_inode_clean(ip)) { |
Dave Chinner | 5b257b4 | 2010-06-03 16:22:29 +1000 | [diff] [blame] | 2267 | ASSERT(ip != free_ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2268 | xfs_ifunlock(ip); |
| 2269 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 2270 | continue; |
| 2271 | } |
| 2272 | |
Christoph Hellwig | f5d8d5c | 2012-02-29 09:53:54 +0000 | [diff] [blame] | 2273 | iip->ili_last_fields = iip->ili_fields; |
| 2274 | iip->ili_fields = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2275 | iip->ili_logged = 1; |
David Chinner | 7b2e2a3 | 2008-10-30 17:39:12 +1100 | [diff] [blame] | 2276 | xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn, |
| 2277 | &iip->ili_item.li_lsn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2278 | |
Christoph Hellwig | ca30b2a | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 2279 | xfs_buf_attach_iodone(bp, xfs_istale_done, |
| 2280 | &iip->ili_item); |
Dave Chinner | 5b257b4 | 2010-06-03 16:22:29 +1000 | [diff] [blame] | 2281 | |
| 2282 | if (ip != free_ip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2283 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2284 | } |
| 2285 | |
Dave Chinner | 5b3eed7 | 2010-08-24 11:42:41 +1000 | [diff] [blame] | 2286 | xfs_trans_stale_inode_buf(tp, bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2287 | xfs_trans_binval(tp, bp); |
| 2288 | } |
| 2289 | |
Dave Chinner | 5017e97 | 2010-01-11 11:47:40 +0000 | [diff] [blame] | 2290 | xfs_perag_put(pag); |
Chandra Seetharaman | 2a30f36d | 2011-09-20 13:56:55 +0000 | [diff] [blame] | 2291 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2292 | } |
| 2293 | |
| 2294 | /* |
| 2295 | * This is called to return an inode to the inode free list. |
| 2296 | * The inode should already be truncated to 0 length and have |
| 2297 | * no pages associated with it. This routine also assumes that |
| 2298 | * the inode is already a part of the transaction. |
| 2299 | * |
| 2300 | * The on-disk copy of the inode will have been added to the list |
| 2301 | * of unlinked inodes in the AGI. We need to remove the inode from |
| 2302 | * that list atomically with respect to freeing it here. |
| 2303 | */ |
| 2304 | int |
| 2305 | xfs_ifree( |
| 2306 | xfs_trans_t *tp, |
| 2307 | xfs_inode_t *ip, |
| 2308 | xfs_bmap_free_t *flist) |
| 2309 | { |
| 2310 | int error; |
| 2311 | int delete; |
| 2312 | xfs_ino_t first_ino; |
| 2313 | |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 2314 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2315 | ASSERT(ip->i_d.di_nlink == 0); |
| 2316 | ASSERT(ip->i_d.di_nextents == 0); |
| 2317 | ASSERT(ip->i_d.di_anextents == 0); |
Christoph Hellwig | ce7ae151 | 2011-12-18 20:00:11 +0000 | [diff] [blame] | 2318 | 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] | 2319 | ASSERT(ip->i_d.di_nblocks == 0); |
| 2320 | |
| 2321 | /* |
| 2322 | * Pull the on-disk inode from the AGI unlinked list. |
| 2323 | */ |
| 2324 | error = xfs_iunlink_remove(tp, ip); |
Dave Chinner | 1baaed8 | 2013-06-27 16:04:50 +1000 | [diff] [blame] | 2325 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2326 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2327 | |
| 2328 | error = xfs_difree(tp, ip->i_ino, flist, &delete, &first_ino); |
Dave Chinner | 1baaed8 | 2013-06-27 16:04:50 +1000 | [diff] [blame] | 2329 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2330 | return error; |
Dave Chinner | 1baaed8 | 2013-06-27 16:04:50 +1000 | [diff] [blame] | 2331 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2332 | ip->i_d.di_mode = 0; /* mark incore inode as free */ |
| 2333 | ip->i_d.di_flags = 0; |
| 2334 | ip->i_d.di_dmevmask = 0; |
| 2335 | 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] | 2336 | ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS; |
| 2337 | ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS; |
| 2338 | /* |
| 2339 | * Bump the generation count so no one will be confused |
| 2340 | * by reincarnations of this inode. |
| 2341 | */ |
| 2342 | ip->i_d.di_gen++; |
| 2343 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 2344 | |
Dave Chinner | 1baaed8 | 2013-06-27 16:04:50 +1000 | [diff] [blame] | 2345 | if (delete) |
Chandra Seetharaman | 2a30f36d | 2011-09-20 13:56:55 +0000 | [diff] [blame] | 2346 | error = xfs_ifree_cluster(ip, tp, first_ino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2347 | |
Chandra Seetharaman | 2a30f36d | 2011-09-20 13:56:55 +0000 | [diff] [blame] | 2348 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2349 | } |
| 2350 | |
| 2351 | /* |
Christoph Hellwig | 60ec678 | 2010-02-17 19:43:56 +0000 | [diff] [blame] | 2352 | * This is called to unpin an inode. The caller must have the inode locked |
| 2353 | * in at least shared mode so that the buffer cannot be subsequently pinned |
| 2354 | * once someone is waiting for it to be unpinned. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2355 | */ |
Christoph Hellwig | 60ec678 | 2010-02-17 19:43:56 +0000 | [diff] [blame] | 2356 | static void |
Christoph Hellwig | f392e63 | 2011-12-18 20:00:10 +0000 | [diff] [blame] | 2357 | xfs_iunpin( |
Christoph Hellwig | 60ec678 | 2010-02-17 19:43:56 +0000 | [diff] [blame] | 2358 | struct xfs_inode *ip) |
David Chinner | a3f74ff | 2008-03-06 13:43:42 +1100 | [diff] [blame] | 2359 | { |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 2360 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)); |
David Chinner | a3f74ff | 2008-03-06 13:43:42 +1100 | [diff] [blame] | 2361 | |
Dave Chinner | 4aaf15d | 2010-03-08 11:24:07 +1100 | [diff] [blame] | 2362 | trace_xfs_inode_unpin_nowait(ip, _RET_IP_); |
| 2363 | |
David Chinner | a3f74ff | 2008-03-06 13:43:42 +1100 | [diff] [blame] | 2364 | /* Give the log a push to start the unpinning I/O */ |
Christoph Hellwig | 60ec678 | 2010-02-17 19:43:56 +0000 | [diff] [blame] | 2365 | 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] | 2366 | |
David Chinner | a3f74ff | 2008-03-06 13:43:42 +1100 | [diff] [blame] | 2367 | } |
| 2368 | |
Christoph Hellwig | f392e63 | 2011-12-18 20:00:10 +0000 | [diff] [blame] | 2369 | static void |
| 2370 | __xfs_iunpin_wait( |
| 2371 | struct xfs_inode *ip) |
| 2372 | { |
| 2373 | wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IPINNED_BIT); |
| 2374 | DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IPINNED_BIT); |
| 2375 | |
| 2376 | xfs_iunpin(ip); |
| 2377 | |
| 2378 | do { |
| 2379 | prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE); |
| 2380 | if (xfs_ipincount(ip)) |
| 2381 | io_schedule(); |
| 2382 | } while (xfs_ipincount(ip)); |
| 2383 | finish_wait(wq, &wait.wait); |
| 2384 | } |
| 2385 | |
Dave Chinner | 777df5a | 2010-02-06 12:37:26 +1100 | [diff] [blame] | 2386 | void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2387 | xfs_iunpin_wait( |
Christoph Hellwig | 60ec678 | 2010-02-17 19:43:56 +0000 | [diff] [blame] | 2388 | struct xfs_inode *ip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2389 | { |
Christoph Hellwig | f392e63 | 2011-12-18 20:00:10 +0000 | [diff] [blame] | 2390 | if (xfs_ipincount(ip)) |
| 2391 | __xfs_iunpin_wait(ip); |
David Chinner | a3f74ff | 2008-03-06 13:43:42 +1100 | [diff] [blame] | 2392 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2393 | |
Dave Chinner | 2732036 | 2013-10-29 22:11:44 +1100 | [diff] [blame] | 2394 | /* |
| 2395 | * Removing an inode from the namespace involves removing the directory entry |
| 2396 | * and dropping the link count on the inode. Removing the directory entry can |
| 2397 | * result in locking an AGF (directory blocks were freed) and removing a link |
| 2398 | * count can result in placing the inode on an unlinked list which results in |
| 2399 | * locking an AGI. |
| 2400 | * |
| 2401 | * The big problem here is that we have an ordering constraint on AGF and AGI |
| 2402 | * locking - inode allocation locks the AGI, then can allocate a new extent for |
| 2403 | * new inodes, locking the AGF after the AGI. Similarly, freeing the inode |
| 2404 | * removes the inode from the unlinked list, requiring that we lock the AGI |
| 2405 | * first, and then freeing the inode can result in an inode chunk being freed |
| 2406 | * and hence freeing disk space requiring that we lock an AGF. |
| 2407 | * |
| 2408 | * Hence the ordering that is imposed by other parts of the code is AGI before |
| 2409 | * AGF. This means we cannot remove the directory entry before we drop the inode |
| 2410 | * reference count and put it on the unlinked list as this results in a lock |
| 2411 | * order of AGF then AGI, and this can deadlock against inode allocation and |
| 2412 | * freeing. Therefore we must drop the link counts before we remove the |
| 2413 | * directory entry. |
| 2414 | * |
| 2415 | * This is still safe from a transactional point of view - it is not until we |
| 2416 | * get to xfs_bmap_finish() that we have the possibility of multiple |
| 2417 | * transactions in this operation. Hence as long as we remove the directory |
| 2418 | * entry and drop the link count in the first transaction of the remove |
| 2419 | * operation, there are no transactional constraints on the ordering here. |
| 2420 | */ |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 2421 | int |
| 2422 | xfs_remove( |
| 2423 | xfs_inode_t *dp, |
| 2424 | struct xfs_name *name, |
| 2425 | xfs_inode_t *ip) |
| 2426 | { |
| 2427 | xfs_mount_t *mp = dp->i_mount; |
| 2428 | xfs_trans_t *tp = NULL; |
| 2429 | int is_dir = S_ISDIR(ip->i_d.di_mode); |
| 2430 | int error = 0; |
| 2431 | xfs_bmap_free_t free_list; |
| 2432 | xfs_fsblock_t first_block; |
| 2433 | int cancel_flags; |
| 2434 | int committed; |
| 2435 | int link_zero; |
| 2436 | uint resblks; |
| 2437 | uint log_count; |
| 2438 | |
| 2439 | trace_xfs_remove(dp, name); |
| 2440 | |
| 2441 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 2442 | return XFS_ERROR(EIO); |
| 2443 | |
| 2444 | error = xfs_qm_dqattach(dp, 0); |
| 2445 | if (error) |
| 2446 | goto std_return; |
| 2447 | |
| 2448 | error = xfs_qm_dqattach(ip, 0); |
| 2449 | if (error) |
| 2450 | goto std_return; |
| 2451 | |
| 2452 | if (is_dir) { |
| 2453 | tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR); |
| 2454 | log_count = XFS_DEFAULT_LOG_COUNT; |
| 2455 | } else { |
| 2456 | tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE); |
| 2457 | log_count = XFS_REMOVE_LOG_COUNT; |
| 2458 | } |
| 2459 | cancel_flags = XFS_TRANS_RELEASE_LOG_RES; |
| 2460 | |
| 2461 | /* |
| 2462 | * We try to get the real space reservation first, |
| 2463 | * allowing for directory btree deletion(s) implying |
| 2464 | * possible bmap insert(s). If we can't get the space |
| 2465 | * reservation then we use 0 instead, and avoid the bmap |
| 2466 | * btree insert(s) in the directory code by, if the bmap |
| 2467 | * insert tries to happen, instead trimming the LAST |
| 2468 | * block from the directory. |
| 2469 | */ |
| 2470 | resblks = XFS_REMOVE_SPACE_RES(mp); |
Jie Liu | 3d3c8b5 | 2013-08-12 20:49:59 +1000 | [diff] [blame] | 2471 | error = xfs_trans_reserve(tp, &M_RES(mp)->tr_remove, resblks, 0); |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 2472 | if (error == ENOSPC) { |
| 2473 | resblks = 0; |
Jie Liu | 3d3c8b5 | 2013-08-12 20:49:59 +1000 | [diff] [blame] | 2474 | error = xfs_trans_reserve(tp, &M_RES(mp)->tr_remove, 0, 0); |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 2475 | } |
| 2476 | if (error) { |
| 2477 | ASSERT(error != ENOSPC); |
| 2478 | cancel_flags = 0; |
| 2479 | goto out_trans_cancel; |
| 2480 | } |
| 2481 | |
| 2482 | xfs_lock_two_inodes(dp, ip, XFS_ILOCK_EXCL); |
| 2483 | |
| 2484 | xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL); |
| 2485 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
| 2486 | |
| 2487 | /* |
| 2488 | * If we're removing a directory perform some additional validation. |
| 2489 | */ |
Dave Chinner | 2732036 | 2013-10-29 22:11:44 +1100 | [diff] [blame] | 2490 | cancel_flags |= XFS_TRANS_ABORT; |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 2491 | if (is_dir) { |
| 2492 | ASSERT(ip->i_d.di_nlink >= 2); |
| 2493 | if (ip->i_d.di_nlink != 2) { |
| 2494 | error = XFS_ERROR(ENOTEMPTY); |
| 2495 | goto out_trans_cancel; |
| 2496 | } |
| 2497 | if (!xfs_dir_isempty(ip)) { |
| 2498 | error = XFS_ERROR(ENOTEMPTY); |
| 2499 | goto out_trans_cancel; |
| 2500 | } |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 2501 | |
Dave Chinner | 2732036 | 2013-10-29 22:11:44 +1100 | [diff] [blame] | 2502 | /* Drop the link from ip's "..". */ |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 2503 | error = xfs_droplink(tp, dp); |
| 2504 | if (error) |
Dave Chinner | 2732036 | 2013-10-29 22:11:44 +1100 | [diff] [blame] | 2505 | goto out_trans_cancel; |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 2506 | |
Dave Chinner | 2732036 | 2013-10-29 22:11:44 +1100 | [diff] [blame] | 2507 | /* Drop the "." link from ip to self. */ |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 2508 | error = xfs_droplink(tp, ip); |
| 2509 | if (error) |
Dave Chinner | 2732036 | 2013-10-29 22:11:44 +1100 | [diff] [blame] | 2510 | goto out_trans_cancel; |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 2511 | } else { |
| 2512 | /* |
| 2513 | * When removing a non-directory we need to log the parent |
| 2514 | * inode here. For a directory this is done implicitly |
| 2515 | * by the xfs_droplink call for the ".." entry. |
| 2516 | */ |
| 2517 | xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE); |
| 2518 | } |
Dave Chinner | 2732036 | 2013-10-29 22:11:44 +1100 | [diff] [blame] | 2519 | xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 2520 | |
Dave Chinner | 2732036 | 2013-10-29 22:11:44 +1100 | [diff] [blame] | 2521 | /* Drop the link from dp to ip. */ |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 2522 | error = xfs_droplink(tp, ip); |
| 2523 | if (error) |
Dave Chinner | 2732036 | 2013-10-29 22:11:44 +1100 | [diff] [blame] | 2524 | goto out_trans_cancel; |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 2525 | |
Dave Chinner | 2732036 | 2013-10-29 22:11:44 +1100 | [diff] [blame] | 2526 | /* Determine if this is the last link while the inode is locked */ |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 2527 | link_zero = (ip->i_d.di_nlink == 0); |
| 2528 | |
Dave Chinner | 2732036 | 2013-10-29 22:11:44 +1100 | [diff] [blame] | 2529 | xfs_bmap_init(&free_list, &first_block); |
| 2530 | error = xfs_dir_removename(tp, dp, name, ip->i_ino, |
| 2531 | &first_block, &free_list, resblks); |
| 2532 | if (error) { |
| 2533 | ASSERT(error != ENOENT); |
| 2534 | goto out_bmap_cancel; |
| 2535 | } |
| 2536 | |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 2537 | /* |
| 2538 | * If this is a synchronous mount, make sure that the |
| 2539 | * remove transaction goes to disk before returning to |
| 2540 | * the user. |
| 2541 | */ |
| 2542 | if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) |
| 2543 | xfs_trans_set_sync(tp); |
| 2544 | |
| 2545 | error = xfs_bmap_finish(&tp, &free_list, &committed); |
| 2546 | if (error) |
| 2547 | goto out_bmap_cancel; |
| 2548 | |
| 2549 | error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); |
| 2550 | if (error) |
| 2551 | goto std_return; |
| 2552 | |
| 2553 | /* |
| 2554 | * If we are using filestreams, kill the stream association. |
| 2555 | * If the file is still open it may get a new one but that |
| 2556 | * will get killed on last close in xfs_close() so we don't |
| 2557 | * have to worry about that. |
| 2558 | */ |
| 2559 | if (!is_dir && link_zero && xfs_inode_is_filestream(ip)) |
| 2560 | xfs_filestream_deassociate(ip); |
| 2561 | |
| 2562 | return 0; |
| 2563 | |
| 2564 | out_bmap_cancel: |
| 2565 | xfs_bmap_cancel(&free_list); |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 2566 | out_trans_cancel: |
| 2567 | xfs_trans_cancel(tp, cancel_flags); |
| 2568 | std_return: |
| 2569 | return error; |
| 2570 | } |
| 2571 | |
Dave Chinner | f6bba20 | 2013-08-12 20:49:46 +1000 | [diff] [blame] | 2572 | /* |
| 2573 | * Enter all inodes for a rename transaction into a sorted array. |
| 2574 | */ |
| 2575 | STATIC void |
| 2576 | xfs_sort_for_rename( |
| 2577 | xfs_inode_t *dp1, /* in: old (source) directory inode */ |
| 2578 | xfs_inode_t *dp2, /* in: new (target) directory inode */ |
| 2579 | xfs_inode_t *ip1, /* in: inode of old entry */ |
| 2580 | xfs_inode_t *ip2, /* in: inode of new entry, if it |
| 2581 | already exists, NULL otherwise. */ |
| 2582 | xfs_inode_t **i_tab,/* out: array of inode returned, sorted */ |
| 2583 | int *num_inodes) /* out: number of inodes in array */ |
| 2584 | { |
| 2585 | xfs_inode_t *temp; |
| 2586 | int i, j; |
| 2587 | |
| 2588 | /* |
| 2589 | * i_tab contains a list of pointers to inodes. We initialize |
| 2590 | * the table here & we'll sort it. We will then use it to |
| 2591 | * order the acquisition of the inode locks. |
| 2592 | * |
| 2593 | * Note that the table may contain duplicates. e.g., dp1 == dp2. |
| 2594 | */ |
| 2595 | i_tab[0] = dp1; |
| 2596 | i_tab[1] = dp2; |
| 2597 | i_tab[2] = ip1; |
| 2598 | if (ip2) { |
| 2599 | *num_inodes = 4; |
| 2600 | i_tab[3] = ip2; |
| 2601 | } else { |
| 2602 | *num_inodes = 3; |
| 2603 | i_tab[3] = NULL; |
| 2604 | } |
| 2605 | |
| 2606 | /* |
| 2607 | * Sort the elements via bubble sort. (Remember, there are at |
| 2608 | * most 4 elements to sort, so this is adequate.) |
| 2609 | */ |
| 2610 | for (i = 0; i < *num_inodes; i++) { |
| 2611 | for (j = 1; j < *num_inodes; j++) { |
| 2612 | if (i_tab[j]->i_ino < i_tab[j-1]->i_ino) { |
| 2613 | temp = i_tab[j]; |
| 2614 | i_tab[j] = i_tab[j-1]; |
| 2615 | i_tab[j-1] = temp; |
| 2616 | } |
| 2617 | } |
| 2618 | } |
| 2619 | } |
| 2620 | |
| 2621 | /* |
| 2622 | * xfs_rename |
| 2623 | */ |
| 2624 | int |
| 2625 | xfs_rename( |
| 2626 | xfs_inode_t *src_dp, |
| 2627 | struct xfs_name *src_name, |
| 2628 | xfs_inode_t *src_ip, |
| 2629 | xfs_inode_t *target_dp, |
| 2630 | struct xfs_name *target_name, |
| 2631 | xfs_inode_t *target_ip) |
| 2632 | { |
| 2633 | xfs_trans_t *tp = NULL; |
| 2634 | xfs_mount_t *mp = src_dp->i_mount; |
| 2635 | int new_parent; /* moving to a new dir */ |
| 2636 | int src_is_directory; /* src_name is a directory */ |
| 2637 | int error; |
| 2638 | xfs_bmap_free_t free_list; |
| 2639 | xfs_fsblock_t first_block; |
| 2640 | int cancel_flags; |
| 2641 | int committed; |
| 2642 | xfs_inode_t *inodes[4]; |
| 2643 | int spaceres; |
| 2644 | int num_inodes; |
| 2645 | |
| 2646 | trace_xfs_rename(src_dp, target_dp, src_name, target_name); |
| 2647 | |
| 2648 | new_parent = (src_dp != target_dp); |
| 2649 | src_is_directory = S_ISDIR(src_ip->i_d.di_mode); |
| 2650 | |
| 2651 | xfs_sort_for_rename(src_dp, target_dp, src_ip, target_ip, |
| 2652 | inodes, &num_inodes); |
| 2653 | |
| 2654 | xfs_bmap_init(&free_list, &first_block); |
| 2655 | tp = xfs_trans_alloc(mp, XFS_TRANS_RENAME); |
| 2656 | cancel_flags = XFS_TRANS_RELEASE_LOG_RES; |
| 2657 | spaceres = XFS_RENAME_SPACE_RES(mp, target_name->len); |
Jie Liu | 3d3c8b5 | 2013-08-12 20:49:59 +1000 | [diff] [blame] | 2658 | error = xfs_trans_reserve(tp, &M_RES(mp)->tr_rename, spaceres, 0); |
Dave Chinner | f6bba20 | 2013-08-12 20:49:46 +1000 | [diff] [blame] | 2659 | if (error == ENOSPC) { |
| 2660 | spaceres = 0; |
Jie Liu | 3d3c8b5 | 2013-08-12 20:49:59 +1000 | [diff] [blame] | 2661 | error = xfs_trans_reserve(tp, &M_RES(mp)->tr_rename, 0, 0); |
Dave Chinner | f6bba20 | 2013-08-12 20:49:46 +1000 | [diff] [blame] | 2662 | } |
| 2663 | if (error) { |
| 2664 | xfs_trans_cancel(tp, 0); |
| 2665 | goto std_return; |
| 2666 | } |
| 2667 | |
| 2668 | /* |
| 2669 | * Attach the dquots to the inodes |
| 2670 | */ |
| 2671 | error = xfs_qm_vop_rename_dqattach(inodes); |
| 2672 | if (error) { |
| 2673 | xfs_trans_cancel(tp, cancel_flags); |
| 2674 | goto std_return; |
| 2675 | } |
| 2676 | |
| 2677 | /* |
| 2678 | * Lock all the participating inodes. Depending upon whether |
| 2679 | * the target_name exists in the target directory, and |
| 2680 | * whether the target directory is the same as the source |
| 2681 | * directory, we can lock from 2 to 4 inodes. |
| 2682 | */ |
| 2683 | xfs_lock_inodes(inodes, num_inodes, XFS_ILOCK_EXCL); |
| 2684 | |
| 2685 | /* |
| 2686 | * Join all the inodes to the transaction. From this point on, |
| 2687 | * we can rely on either trans_commit or trans_cancel to unlock |
| 2688 | * them. |
| 2689 | */ |
| 2690 | xfs_trans_ijoin(tp, src_dp, XFS_ILOCK_EXCL); |
| 2691 | if (new_parent) |
| 2692 | xfs_trans_ijoin(tp, target_dp, XFS_ILOCK_EXCL); |
| 2693 | xfs_trans_ijoin(tp, src_ip, XFS_ILOCK_EXCL); |
| 2694 | if (target_ip) |
| 2695 | xfs_trans_ijoin(tp, target_ip, XFS_ILOCK_EXCL); |
| 2696 | |
| 2697 | /* |
| 2698 | * If we are using project inheritance, we only allow renames |
| 2699 | * into our tree when the project IDs are the same; else the |
| 2700 | * tree quota mechanism would be circumvented. |
| 2701 | */ |
| 2702 | if (unlikely((target_dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) && |
| 2703 | (xfs_get_projid(target_dp) != xfs_get_projid(src_ip)))) { |
| 2704 | error = XFS_ERROR(EXDEV); |
| 2705 | goto error_return; |
| 2706 | } |
| 2707 | |
| 2708 | /* |
| 2709 | * Set up the target. |
| 2710 | */ |
| 2711 | if (target_ip == NULL) { |
| 2712 | /* |
| 2713 | * If there's no space reservation, check the entry will |
| 2714 | * fit before actually inserting it. |
| 2715 | */ |
| 2716 | error = xfs_dir_canenter(tp, target_dp, target_name, spaceres); |
| 2717 | if (error) |
| 2718 | goto error_return; |
| 2719 | /* |
| 2720 | * If target does not exist and the rename crosses |
| 2721 | * directories, adjust the target directory link count |
| 2722 | * to account for the ".." reference from the new entry. |
| 2723 | */ |
| 2724 | error = xfs_dir_createname(tp, target_dp, target_name, |
| 2725 | src_ip->i_ino, &first_block, |
| 2726 | &free_list, spaceres); |
| 2727 | if (error == ENOSPC) |
| 2728 | goto error_return; |
| 2729 | if (error) |
| 2730 | goto abort_return; |
| 2731 | |
| 2732 | xfs_trans_ichgtime(tp, target_dp, |
| 2733 | XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); |
| 2734 | |
| 2735 | if (new_parent && src_is_directory) { |
| 2736 | error = xfs_bumplink(tp, target_dp); |
| 2737 | if (error) |
| 2738 | goto abort_return; |
| 2739 | } |
| 2740 | } else { /* target_ip != NULL */ |
| 2741 | /* |
| 2742 | * If target exists and it's a directory, check that both |
| 2743 | * target and source are directories and that target can be |
| 2744 | * destroyed, or that neither is a directory. |
| 2745 | */ |
| 2746 | if (S_ISDIR(target_ip->i_d.di_mode)) { |
| 2747 | /* |
| 2748 | * Make sure target dir is empty. |
| 2749 | */ |
| 2750 | if (!(xfs_dir_isempty(target_ip)) || |
| 2751 | (target_ip->i_d.di_nlink > 2)) { |
| 2752 | error = XFS_ERROR(EEXIST); |
| 2753 | goto error_return; |
| 2754 | } |
| 2755 | } |
| 2756 | |
| 2757 | /* |
| 2758 | * Link the source inode under the target name. |
| 2759 | * If the source inode is a directory and we are moving |
| 2760 | * it across directories, its ".." entry will be |
| 2761 | * inconsistent until we replace that down below. |
| 2762 | * |
| 2763 | * In case there is already an entry with the same |
| 2764 | * name at the destination directory, remove it first. |
| 2765 | */ |
| 2766 | error = xfs_dir_replace(tp, target_dp, target_name, |
| 2767 | src_ip->i_ino, |
| 2768 | &first_block, &free_list, spaceres); |
| 2769 | if (error) |
| 2770 | goto abort_return; |
| 2771 | |
| 2772 | xfs_trans_ichgtime(tp, target_dp, |
| 2773 | XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); |
| 2774 | |
| 2775 | /* |
| 2776 | * Decrement the link count on the target since the target |
| 2777 | * dir no longer points to it. |
| 2778 | */ |
| 2779 | error = xfs_droplink(tp, target_ip); |
| 2780 | if (error) |
| 2781 | goto abort_return; |
| 2782 | |
| 2783 | if (src_is_directory) { |
| 2784 | /* |
| 2785 | * Drop the link from the old "." entry. |
| 2786 | */ |
| 2787 | error = xfs_droplink(tp, target_ip); |
| 2788 | if (error) |
| 2789 | goto abort_return; |
| 2790 | } |
| 2791 | } /* target_ip != NULL */ |
| 2792 | |
| 2793 | /* |
| 2794 | * Remove the source. |
| 2795 | */ |
| 2796 | if (new_parent && src_is_directory) { |
| 2797 | /* |
| 2798 | * Rewrite the ".." entry to point to the new |
| 2799 | * directory. |
| 2800 | */ |
| 2801 | error = xfs_dir_replace(tp, src_ip, &xfs_name_dotdot, |
| 2802 | target_dp->i_ino, |
| 2803 | &first_block, &free_list, spaceres); |
| 2804 | ASSERT(error != EEXIST); |
| 2805 | if (error) |
| 2806 | goto abort_return; |
| 2807 | } |
| 2808 | |
| 2809 | /* |
| 2810 | * We always want to hit the ctime on the source inode. |
| 2811 | * |
| 2812 | * This isn't strictly required by the standards since the source |
| 2813 | * inode isn't really being changed, but old unix file systems did |
| 2814 | * it and some incremental backup programs won't work without it. |
| 2815 | */ |
| 2816 | xfs_trans_ichgtime(tp, src_ip, XFS_ICHGTIME_CHG); |
| 2817 | xfs_trans_log_inode(tp, src_ip, XFS_ILOG_CORE); |
| 2818 | |
| 2819 | /* |
| 2820 | * Adjust the link count on src_dp. This is necessary when |
| 2821 | * renaming a directory, either within one parent when |
| 2822 | * the target existed, or across two parent directories. |
| 2823 | */ |
| 2824 | if (src_is_directory && (new_parent || target_ip != NULL)) { |
| 2825 | |
| 2826 | /* |
| 2827 | * Decrement link count on src_directory since the |
| 2828 | * entry that's moved no longer points to it. |
| 2829 | */ |
| 2830 | error = xfs_droplink(tp, src_dp); |
| 2831 | if (error) |
| 2832 | goto abort_return; |
| 2833 | } |
| 2834 | |
| 2835 | error = xfs_dir_removename(tp, src_dp, src_name, src_ip->i_ino, |
| 2836 | &first_block, &free_list, spaceres); |
| 2837 | if (error) |
| 2838 | goto abort_return; |
| 2839 | |
| 2840 | xfs_trans_ichgtime(tp, src_dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); |
| 2841 | xfs_trans_log_inode(tp, src_dp, XFS_ILOG_CORE); |
| 2842 | if (new_parent) |
| 2843 | xfs_trans_log_inode(tp, target_dp, XFS_ILOG_CORE); |
| 2844 | |
| 2845 | /* |
| 2846 | * If this is a synchronous mount, make sure that the |
| 2847 | * rename transaction goes to disk before returning to |
| 2848 | * the user. |
| 2849 | */ |
| 2850 | if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) { |
| 2851 | xfs_trans_set_sync(tp); |
| 2852 | } |
| 2853 | |
| 2854 | error = xfs_bmap_finish(&tp, &free_list, &committed); |
| 2855 | if (error) { |
| 2856 | xfs_bmap_cancel(&free_list); |
| 2857 | xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES | |
| 2858 | XFS_TRANS_ABORT)); |
| 2859 | goto std_return; |
| 2860 | } |
| 2861 | |
| 2862 | /* |
| 2863 | * trans_commit will unlock src_ip, target_ip & decrement |
| 2864 | * the vnode references. |
| 2865 | */ |
| 2866 | return xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); |
| 2867 | |
| 2868 | abort_return: |
| 2869 | cancel_flags |= XFS_TRANS_ABORT; |
| 2870 | error_return: |
| 2871 | xfs_bmap_cancel(&free_list); |
| 2872 | xfs_trans_cancel(tp, cancel_flags); |
| 2873 | std_return: |
| 2874 | return error; |
| 2875 | } |
| 2876 | |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 2877 | STATIC int |
| 2878 | xfs_iflush_cluster( |
| 2879 | xfs_inode_t *ip, |
| 2880 | xfs_buf_t *bp) |
| 2881 | { |
| 2882 | xfs_mount_t *mp = ip->i_mount; |
Dave Chinner | 5017e97 | 2010-01-11 11:47:40 +0000 | [diff] [blame] | 2883 | struct xfs_perag *pag; |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 2884 | unsigned long first_index, mask; |
David Chinner | c8f5f12 | 2008-05-20 11:30:15 +1000 | [diff] [blame] | 2885 | unsigned long inodes_per_cluster; |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 2886 | int ilist_size; |
| 2887 | xfs_inode_t **ilist; |
| 2888 | xfs_inode_t *iq; |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 2889 | int nr_found; |
| 2890 | int clcount = 0; |
| 2891 | int bufwasdelwri; |
| 2892 | int i; |
| 2893 | |
Dave Chinner | 5017e97 | 2010-01-11 11:47:40 +0000 | [diff] [blame] | 2894 | 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] | 2895 | |
David Chinner | c8f5f12 | 2008-05-20 11:30:15 +1000 | [diff] [blame] | 2896 | inodes_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog; |
| 2897 | ilist_size = inodes_per_cluster * sizeof(xfs_inode_t *); |
David Chinner | 49383b0 | 2008-05-19 16:29:34 +1000 | [diff] [blame] | 2898 | ilist = kmem_alloc(ilist_size, KM_MAYFAIL|KM_NOFS); |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 2899 | if (!ilist) |
Dave Chinner | 44b56e0 | 2010-01-11 11:47:43 +0000 | [diff] [blame] | 2900 | goto out_put; |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 2901 | |
| 2902 | mask = ~(((XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog)) - 1); |
| 2903 | first_index = XFS_INO_TO_AGINO(mp, ip->i_ino) & mask; |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 2904 | rcu_read_lock(); |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 2905 | /* really need a gang lookup range call here */ |
| 2906 | nr_found = radix_tree_gang_lookup(&pag->pag_ici_root, (void**)ilist, |
David Chinner | c8f5f12 | 2008-05-20 11:30:15 +1000 | [diff] [blame] | 2907 | first_index, inodes_per_cluster); |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 2908 | if (nr_found == 0) |
| 2909 | goto out_free; |
| 2910 | |
| 2911 | for (i = 0; i < nr_found; i++) { |
| 2912 | iq = ilist[i]; |
| 2913 | if (iq == ip) |
| 2914 | continue; |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 2915 | |
| 2916 | /* |
| 2917 | * because this is an RCU protected lookup, we could find a |
| 2918 | * recently freed or even reallocated inode during the lookup. |
| 2919 | * We need to check under the i_flags_lock for a valid inode |
| 2920 | * here. Skip it if it is not valid or the wrong inode. |
| 2921 | */ |
| 2922 | spin_lock(&ip->i_flags_lock); |
| 2923 | if (!ip->i_ino || |
| 2924 | (XFS_INO_TO_AGINO(mp, iq->i_ino) & mask) != first_index) { |
| 2925 | spin_unlock(&ip->i_flags_lock); |
| 2926 | continue; |
| 2927 | } |
| 2928 | spin_unlock(&ip->i_flags_lock); |
| 2929 | |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 2930 | /* |
| 2931 | * Do an un-protected check to see if the inode is dirty and |
| 2932 | * is a candidate for flushing. These checks will be repeated |
| 2933 | * later after the appropriate locks are acquired. |
| 2934 | */ |
David Chinner | 3354040 | 2008-03-06 13:43:59 +1100 | [diff] [blame] | 2935 | if (xfs_inode_clean(iq) && xfs_ipincount(iq) == 0) |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 2936 | continue; |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 2937 | |
| 2938 | /* |
| 2939 | * Try to get locks. If any are unavailable or it is pinned, |
| 2940 | * then this inode cannot be flushed and is skipped. |
| 2941 | */ |
| 2942 | |
| 2943 | if (!xfs_ilock_nowait(iq, XFS_ILOCK_SHARED)) |
| 2944 | continue; |
| 2945 | if (!xfs_iflock_nowait(iq)) { |
| 2946 | xfs_iunlock(iq, XFS_ILOCK_SHARED); |
| 2947 | continue; |
| 2948 | } |
| 2949 | if (xfs_ipincount(iq)) { |
| 2950 | xfs_ifunlock(iq); |
| 2951 | xfs_iunlock(iq, XFS_ILOCK_SHARED); |
| 2952 | continue; |
| 2953 | } |
| 2954 | |
| 2955 | /* |
| 2956 | * arriving here means that this inode can be flushed. First |
| 2957 | * re-check that it's dirty before flushing. |
| 2958 | */ |
David Chinner | 3354040 | 2008-03-06 13:43:59 +1100 | [diff] [blame] | 2959 | if (!xfs_inode_clean(iq)) { |
| 2960 | int error; |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 2961 | error = xfs_iflush_int(iq, bp); |
| 2962 | if (error) { |
| 2963 | xfs_iunlock(iq, XFS_ILOCK_SHARED); |
| 2964 | goto cluster_corrupt_out; |
| 2965 | } |
| 2966 | clcount++; |
| 2967 | } else { |
| 2968 | xfs_ifunlock(iq); |
| 2969 | } |
| 2970 | xfs_iunlock(iq, XFS_ILOCK_SHARED); |
| 2971 | } |
| 2972 | |
| 2973 | if (clcount) { |
| 2974 | XFS_STATS_INC(xs_icluster_flushcnt); |
| 2975 | XFS_STATS_ADD(xs_icluster_flushinode, clcount); |
| 2976 | } |
| 2977 | |
| 2978 | out_free: |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 2979 | rcu_read_unlock(); |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 2980 | kmem_free(ilist); |
Dave Chinner | 44b56e0 | 2010-01-11 11:47:43 +0000 | [diff] [blame] | 2981 | out_put: |
| 2982 | xfs_perag_put(pag); |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 2983 | return 0; |
| 2984 | |
| 2985 | |
| 2986 | cluster_corrupt_out: |
| 2987 | /* |
| 2988 | * Corruption detected in the clustering loop. Invalidate the |
| 2989 | * inode buffer and shut down the filesystem. |
| 2990 | */ |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 2991 | rcu_read_unlock(); |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 2992 | /* |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 2993 | * Clean up the buffer. If it was delwri, just release it -- |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 2994 | * brelse can handle it with no problems. If not, shut down the |
| 2995 | * filesystem before releasing the buffer. |
| 2996 | */ |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 2997 | bufwasdelwri = (bp->b_flags & _XBF_DELWRI_Q); |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 2998 | if (bufwasdelwri) |
| 2999 | xfs_buf_relse(bp); |
| 3000 | |
| 3001 | xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); |
| 3002 | |
| 3003 | if (!bufwasdelwri) { |
| 3004 | /* |
| 3005 | * Just like incore_relse: if we have b_iodone functions, |
| 3006 | * mark the buffer as an error and call them. Otherwise |
| 3007 | * mark it as stale and brelse. |
| 3008 | */ |
Christoph Hellwig | cb669ca | 2011-07-13 13:43:49 +0200 | [diff] [blame] | 3009 | if (bp->b_iodone) { |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 3010 | XFS_BUF_UNDONE(bp); |
Christoph Hellwig | c867cb6 | 2011-10-10 16:52:46 +0000 | [diff] [blame] | 3011 | xfs_buf_stale(bp); |
Chandra Seetharaman | 5a52c2a58 | 2011-07-22 23:39:51 +0000 | [diff] [blame] | 3012 | xfs_buf_ioerror(bp, EIO); |
Christoph Hellwig | 1a1a3e9 | 2010-10-06 18:41:18 +0000 | [diff] [blame] | 3013 | xfs_buf_ioend(bp, 0); |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 3014 | } else { |
Christoph Hellwig | c867cb6 | 2011-10-10 16:52:46 +0000 | [diff] [blame] | 3015 | xfs_buf_stale(bp); |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 3016 | xfs_buf_relse(bp); |
| 3017 | } |
| 3018 | } |
| 3019 | |
| 3020 | /* |
| 3021 | * Unlocks the flush lock |
| 3022 | */ |
Dave Chinner | 04913fd | 2012-04-23 15:58:41 +1000 | [diff] [blame] | 3023 | xfs_iflush_abort(iq, false); |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 3024 | kmem_free(ilist); |
Dave Chinner | 44b56e0 | 2010-01-11 11:47:43 +0000 | [diff] [blame] | 3025 | xfs_perag_put(pag); |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 3026 | return XFS_ERROR(EFSCORRUPTED); |
| 3027 | } |
| 3028 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3029 | /* |
Christoph Hellwig | 4c46819 | 2012-04-23 15:58:36 +1000 | [diff] [blame] | 3030 | * Flush dirty inode metadata into the backing buffer. |
| 3031 | * |
| 3032 | * The caller must have the inode lock and the inode flush lock held. The |
| 3033 | * inode lock will still be held upon return to the caller, and the inode |
| 3034 | * flush lock will be released after the inode has reached the disk. |
| 3035 | * |
| 3036 | * 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] | 3037 | */ |
| 3038 | int |
| 3039 | xfs_iflush( |
Christoph Hellwig | 4c46819 | 2012-04-23 15:58:36 +1000 | [diff] [blame] | 3040 | struct xfs_inode *ip, |
| 3041 | struct xfs_buf **bpp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3042 | { |
Christoph Hellwig | 4c46819 | 2012-04-23 15:58:36 +1000 | [diff] [blame] | 3043 | struct xfs_mount *mp = ip->i_mount; |
| 3044 | struct xfs_buf *bp; |
| 3045 | struct xfs_dinode *dip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3046 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3047 | |
| 3048 | XFS_STATS_INC(xs_iflush_count); |
| 3049 | |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 3050 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)); |
Christoph Hellwig | 474fce0 | 2011-12-18 20:00:09 +0000 | [diff] [blame] | 3051 | ASSERT(xfs_isiflocked(ip)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3052 | ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE || |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 3053 | ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3054 | |
Christoph Hellwig | 4c46819 | 2012-04-23 15:58:36 +1000 | [diff] [blame] | 3055 | *bpp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3056 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3057 | xfs_iunpin_wait(ip); |
| 3058 | |
| 3059 | /* |
Dave Chinner | 4b6a468 | 2010-01-11 11:45:21 +0000 | [diff] [blame] | 3060 | * For stale inodes we cannot rely on the backing buffer remaining |
| 3061 | * 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] | 3062 | * 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] | 3063 | * inodes below. We have to check this after ensuring the inode is |
| 3064 | * unpinned so that it is safe to reclaim the stale inode after the |
| 3065 | * flush call. |
| 3066 | */ |
| 3067 | if (xfs_iflags_test(ip, XFS_ISTALE)) { |
| 3068 | xfs_ifunlock(ip); |
| 3069 | return 0; |
| 3070 | } |
| 3071 | |
| 3072 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3073 | * This may have been unpinned because the filesystem is shutting |
| 3074 | * 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] | 3075 | * to disk, because the log record didn't make it to disk. |
| 3076 | * |
| 3077 | * We also have to remove the log item from the AIL in this case, |
| 3078 | * 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] | 3079 | */ |
| 3080 | if (XFS_FORCED_SHUTDOWN(mp)) { |
Christoph Hellwig | 32ce90a | 2012-04-23 15:58:32 +1000 | [diff] [blame] | 3081 | error = XFS_ERROR(EIO); |
| 3082 | goto abort_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3083 | } |
| 3084 | |
| 3085 | /* |
David Chinner | a3f74ff | 2008-03-06 13:43:42 +1100 | [diff] [blame] | 3086 | * Get the buffer containing the on-disk inode. |
| 3087 | */ |
Christoph Hellwig | 475ee41 | 2012-07-03 12:21:22 -0400 | [diff] [blame] | 3088 | error = xfs_imap_to_bp(mp, NULL, &ip->i_imap, &dip, &bp, XBF_TRYLOCK, |
| 3089 | 0); |
David Chinner | a3f74ff | 2008-03-06 13:43:42 +1100 | [diff] [blame] | 3090 | if (error || !bp) { |
| 3091 | xfs_ifunlock(ip); |
| 3092 | return error; |
| 3093 | } |
| 3094 | |
| 3095 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3096 | * First flush out the inode that xfs_iflush was called with. |
| 3097 | */ |
| 3098 | error = xfs_iflush_int(ip, bp); |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 3099 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3100 | goto corrupt_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3101 | |
| 3102 | /* |
David Chinner | a3f74ff | 2008-03-06 13:43:42 +1100 | [diff] [blame] | 3103 | * If the buffer is pinned then push on the log now so we won't |
| 3104 | * get stuck waiting in the write for too long. |
| 3105 | */ |
Chandra Seetharaman | 811e64c | 2011-07-22 23:40:27 +0000 | [diff] [blame] | 3106 | if (xfs_buf_ispinned(bp)) |
Christoph Hellwig | a14a348 | 2010-01-19 09:56:46 +0000 | [diff] [blame] | 3107 | xfs_log_force(mp, 0); |
David Chinner | a3f74ff | 2008-03-06 13:43:42 +1100 | [diff] [blame] | 3108 | |
| 3109 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3110 | * inode clustering: |
| 3111 | * see if other inodes can be gathered into this write |
| 3112 | */ |
David Chinner | bad5584 | 2008-03-06 13:43:49 +1100 | [diff] [blame] | 3113 | error = xfs_iflush_cluster(ip, bp); |
| 3114 | if (error) |
| 3115 | goto cluster_corrupt_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3116 | |
Christoph Hellwig | 4c46819 | 2012-04-23 15:58:36 +1000 | [diff] [blame] | 3117 | *bpp = bp; |
| 3118 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3119 | |
| 3120 | corrupt_out: |
| 3121 | xfs_buf_relse(bp); |
Nathan Scott | 7d04a33 | 2006-06-09 14:58:38 +1000 | [diff] [blame] | 3122 | xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3123 | cluster_corrupt_out: |
Christoph Hellwig | 32ce90a | 2012-04-23 15:58:32 +1000 | [diff] [blame] | 3124 | error = XFS_ERROR(EFSCORRUPTED); |
| 3125 | abort_out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3126 | /* |
| 3127 | * Unlocks the flush lock |
| 3128 | */ |
Dave Chinner | 04913fd | 2012-04-23 15:58:41 +1000 | [diff] [blame] | 3129 | xfs_iflush_abort(ip, false); |
Christoph Hellwig | 32ce90a | 2012-04-23 15:58:32 +1000 | [diff] [blame] | 3130 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3131 | } |
| 3132 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3133 | STATIC int |
| 3134 | xfs_iflush_int( |
Christoph Hellwig | 93848a9 | 2013-04-03 16:11:17 +1100 | [diff] [blame] | 3135 | struct xfs_inode *ip, |
| 3136 | struct xfs_buf *bp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3137 | { |
Christoph Hellwig | 93848a9 | 2013-04-03 16:11:17 +1100 | [diff] [blame] | 3138 | struct xfs_inode_log_item *iip = ip->i_itemp; |
| 3139 | struct xfs_dinode *dip; |
| 3140 | struct xfs_mount *mp = ip->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3141 | |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 3142 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)); |
Christoph Hellwig | 474fce0 | 2011-12-18 20:00:09 +0000 | [diff] [blame] | 3143 | ASSERT(xfs_isiflocked(ip)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3144 | ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE || |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 3145 | ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK)); |
Christoph Hellwig | 93848a9 | 2013-04-03 16:11:17 +1100 | [diff] [blame] | 3146 | ASSERT(iip != NULL && iip->ili_fields != 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3147 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3148 | /* set *dip = inode's place in the buffer */ |
Christoph Hellwig | 92bfc6e | 2008-11-28 14:23:41 +1100 | [diff] [blame] | 3149 | 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] | 3150 | |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 3151 | 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] | 3152 | mp, XFS_ERRTAG_IFLUSH_1, XFS_RANDOM_IFLUSH_1)) { |
Dave Chinner | 6a19d93 | 2011-03-07 10:02:35 +1100 | [diff] [blame] | 3153 | xfs_alert_tag(mp, XFS_PTAG_IFLUSH, |
| 3154 | "%s: Bad inode %Lu magic number 0x%x, ptr 0x%p", |
| 3155 | __func__, ip->i_ino, be16_to_cpu(dip->di_magic), dip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3156 | goto corrupt_out; |
| 3157 | } |
| 3158 | if (XFS_TEST_ERROR(ip->i_d.di_magic != XFS_DINODE_MAGIC, |
| 3159 | mp, XFS_ERRTAG_IFLUSH_2, XFS_RANDOM_IFLUSH_2)) { |
Dave Chinner | 6a19d93 | 2011-03-07 10:02:35 +1100 | [diff] [blame] | 3160 | xfs_alert_tag(mp, XFS_PTAG_IFLUSH, |
| 3161 | "%s: Bad inode %Lu, ptr 0x%p, magic number 0x%x", |
| 3162 | __func__, ip->i_ino, ip, ip->i_d.di_magic); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3163 | goto corrupt_out; |
| 3164 | } |
Al Viro | abbede1 | 2011-07-26 02:31:30 -0400 | [diff] [blame] | 3165 | if (S_ISREG(ip->i_d.di_mode)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3166 | if (XFS_TEST_ERROR( |
| 3167 | (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) && |
| 3168 | (ip->i_d.di_format != XFS_DINODE_FMT_BTREE), |
| 3169 | mp, XFS_ERRTAG_IFLUSH_3, XFS_RANDOM_IFLUSH_3)) { |
Dave Chinner | 6a19d93 | 2011-03-07 10:02:35 +1100 | [diff] [blame] | 3170 | xfs_alert_tag(mp, XFS_PTAG_IFLUSH, |
| 3171 | "%s: Bad regular inode %Lu, ptr 0x%p", |
| 3172 | __func__, ip->i_ino, ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3173 | goto corrupt_out; |
| 3174 | } |
Al Viro | abbede1 | 2011-07-26 02:31:30 -0400 | [diff] [blame] | 3175 | } else if (S_ISDIR(ip->i_d.di_mode)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3176 | if (XFS_TEST_ERROR( |
| 3177 | (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) && |
| 3178 | (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) && |
| 3179 | (ip->i_d.di_format != XFS_DINODE_FMT_LOCAL), |
| 3180 | mp, XFS_ERRTAG_IFLUSH_4, XFS_RANDOM_IFLUSH_4)) { |
Dave Chinner | 6a19d93 | 2011-03-07 10:02:35 +1100 | [diff] [blame] | 3181 | xfs_alert_tag(mp, XFS_PTAG_IFLUSH, |
| 3182 | "%s: Bad directory inode %Lu, ptr 0x%p", |
| 3183 | __func__, ip->i_ino, ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3184 | goto corrupt_out; |
| 3185 | } |
| 3186 | } |
| 3187 | if (XFS_TEST_ERROR(ip->i_d.di_nextents + ip->i_d.di_anextents > |
| 3188 | ip->i_d.di_nblocks, mp, XFS_ERRTAG_IFLUSH_5, |
| 3189 | XFS_RANDOM_IFLUSH_5)) { |
Dave Chinner | 6a19d93 | 2011-03-07 10:02:35 +1100 | [diff] [blame] | 3190 | xfs_alert_tag(mp, XFS_PTAG_IFLUSH, |
| 3191 | "%s: detected corrupt incore inode %Lu, " |
| 3192 | "total extents = %d, nblocks = %Ld, ptr 0x%p", |
| 3193 | __func__, ip->i_ino, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3194 | ip->i_d.di_nextents + ip->i_d.di_anextents, |
Dave Chinner | 6a19d93 | 2011-03-07 10:02:35 +1100 | [diff] [blame] | 3195 | ip->i_d.di_nblocks, ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3196 | goto corrupt_out; |
| 3197 | } |
| 3198 | if (XFS_TEST_ERROR(ip->i_d.di_forkoff > mp->m_sb.sb_inodesize, |
| 3199 | mp, XFS_ERRTAG_IFLUSH_6, XFS_RANDOM_IFLUSH_6)) { |
Dave Chinner | 6a19d93 | 2011-03-07 10:02:35 +1100 | [diff] [blame] | 3200 | xfs_alert_tag(mp, XFS_PTAG_IFLUSH, |
| 3201 | "%s: bad inode %Lu, forkoff 0x%x, ptr 0x%p", |
| 3202 | __func__, ip->i_ino, ip->i_d.di_forkoff, ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3203 | goto corrupt_out; |
| 3204 | } |
Dave Chinner | e60896d | 2013-07-24 15:47:30 +1000 | [diff] [blame] | 3205 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3206 | /* |
Dave Chinner | e60896d | 2013-07-24 15:47:30 +1000 | [diff] [blame] | 3207 | * Inode item log recovery for v1/v2 inodes are dependent on the |
| 3208 | * di_flushiter count for correct sequencing. We bump the flush |
| 3209 | * iteration count so we can detect flushes which postdate a log record |
| 3210 | * during recovery. This is redundant as we now log every change and |
| 3211 | * hence this can't happen but we need to still do it to ensure |
| 3212 | * backwards compatibility with old kernels that predate logging all |
| 3213 | * inode changes. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3214 | */ |
Dave Chinner | e60896d | 2013-07-24 15:47:30 +1000 | [diff] [blame] | 3215 | if (ip->i_d.di_version < 3) |
| 3216 | ip->i_d.di_flushiter++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3217 | |
| 3218 | /* |
| 3219 | * Copy the dirty parts of the inode into the on-disk |
| 3220 | * inode. We always copy out the core of the inode, |
| 3221 | * because if the inode is dirty at all the core must |
| 3222 | * be. |
| 3223 | */ |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 3224 | xfs_dinode_to_disk(dip, &ip->i_d); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3225 | |
| 3226 | /* Wrap, we never let the log put out DI_MAX_FLUSH */ |
| 3227 | if (ip->i_d.di_flushiter == DI_MAX_FLUSH) |
| 3228 | ip->i_d.di_flushiter = 0; |
| 3229 | |
| 3230 | /* |
| 3231 | * If this is really an old format inode and the superblock version |
| 3232 | * has not been updated to support only new format inodes, then |
| 3233 | * convert back to the old inode format. If the superblock version |
| 3234 | * has been updated, then make the conversion permanent. |
| 3235 | */ |
Christoph Hellwig | 51ce16d | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 3236 | ASSERT(ip->i_d.di_version == 1 || xfs_sb_version_hasnlink(&mp->m_sb)); |
| 3237 | if (ip->i_d.di_version == 1) { |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 3238 | if (!xfs_sb_version_hasnlink(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3239 | /* |
| 3240 | * Convert it back. |
| 3241 | */ |
| 3242 | ASSERT(ip->i_d.di_nlink <= XFS_MAXLINK_1); |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 3243 | dip->di_onlink = cpu_to_be16(ip->i_d.di_nlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3244 | } else { |
| 3245 | /* |
| 3246 | * The superblock version has already been bumped, |
| 3247 | * so just make the conversion to the new inode |
| 3248 | * format permanent. |
| 3249 | */ |
Christoph Hellwig | 51ce16d | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 3250 | ip->i_d.di_version = 2; |
| 3251 | dip->di_version = 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3252 | ip->i_d.di_onlink = 0; |
Christoph Hellwig | 81591fe | 2008-11-28 14:23:39 +1100 | [diff] [blame] | 3253 | dip->di_onlink = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3254 | 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] | 3255 | memset(&(dip->di_pad[0]), 0, |
| 3256 | sizeof(dip->di_pad)); |
Arkadiusz Mi?kiewicz | 6743099 | 2010-09-26 06:10:18 +0000 | [diff] [blame] | 3257 | ASSERT(xfs_get_projid(ip) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3258 | } |
| 3259 | } |
| 3260 | |
David Chinner | e4ac967 | 2008-04-10 12:23:58 +1000 | [diff] [blame] | 3261 | xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK, bp); |
| 3262 | if (XFS_IFORK_Q(ip)) |
| 3263 | xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK, bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3264 | xfs_inobp_check(mp, bp); |
| 3265 | |
| 3266 | /* |
Christoph Hellwig | f5d8d5c | 2012-02-29 09:53:54 +0000 | [diff] [blame] | 3267 | * We've recorded everything logged in the inode, so we'd like to clear |
| 3268 | * the ili_fields bits so we don't log and flush things unnecessarily. |
| 3269 | * However, we can't stop logging all this information until the data |
| 3270 | * we've copied into the disk buffer is written to disk. If we did we |
| 3271 | * might overwrite the copy of the inode in the log with all the data |
| 3272 | * after re-logging only part of it, and in the face of a crash we |
| 3273 | * wouldn't have all the data we need to recover. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3274 | * |
Christoph Hellwig | f5d8d5c | 2012-02-29 09:53:54 +0000 | [diff] [blame] | 3275 | * What we do is move the bits to the ili_last_fields field. When |
| 3276 | * logging the inode, these bits are moved back to the ili_fields field. |
| 3277 | * In the xfs_iflush_done() routine we clear ili_last_fields, since we |
| 3278 | * know that the information those bits represent is permanently on |
| 3279 | * disk. As long as the flush completes before the inode is logged |
| 3280 | * again, then both ili_fields and ili_last_fields will be cleared. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3281 | * |
Christoph Hellwig | f5d8d5c | 2012-02-29 09:53:54 +0000 | [diff] [blame] | 3282 | * We can play with the ili_fields bits here, because the inode lock |
| 3283 | * must be held exclusively in order to set bits there and the flush |
| 3284 | * lock protects the ili_last_fields bits. Set ili_logged so the flush |
| 3285 | * done routine can tell whether or not to look in the AIL. Also, store |
| 3286 | * the current LSN of the inode so that we can tell whether the item has |
| 3287 | * moved in the AIL from xfs_iflush_done(). In order to read the lsn we |
| 3288 | * need the AIL lock, because it is a 64 bit value that cannot be read |
| 3289 | * atomically. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3290 | */ |
Christoph Hellwig | 93848a9 | 2013-04-03 16:11:17 +1100 | [diff] [blame] | 3291 | iip->ili_last_fields = iip->ili_fields; |
| 3292 | iip->ili_fields = 0; |
| 3293 | iip->ili_logged = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3294 | |
Christoph Hellwig | 93848a9 | 2013-04-03 16:11:17 +1100 | [diff] [blame] | 3295 | xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn, |
| 3296 | &iip->ili_item.li_lsn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3297 | |
Christoph Hellwig | 93848a9 | 2013-04-03 16:11:17 +1100 | [diff] [blame] | 3298 | /* |
| 3299 | * Attach the function xfs_iflush_done to the inode's |
| 3300 | * buffer. This will remove the inode from the AIL |
| 3301 | * and unlock the inode's flush lock when the inode is |
| 3302 | * completely written to disk. |
| 3303 | */ |
| 3304 | xfs_buf_attach_iodone(bp, xfs_iflush_done, &iip->ili_item); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3305 | |
Christoph Hellwig | 93848a9 | 2013-04-03 16:11:17 +1100 | [diff] [blame] | 3306 | /* update the lsn in the on disk inode if required */ |
| 3307 | if (ip->i_d.di_version == 3) |
| 3308 | dip->di_lsn = cpu_to_be64(iip->ili_item.li_lsn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3309 | |
Christoph Hellwig | 93848a9 | 2013-04-03 16:11:17 +1100 | [diff] [blame] | 3310 | /* generate the checksum. */ |
| 3311 | xfs_dinode_calc_crc(mp, dip); |
| 3312 | |
| 3313 | ASSERT(bp->b_fspriv != NULL); |
| 3314 | ASSERT(bp->b_iodone != NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3315 | return 0; |
| 3316 | |
| 3317 | corrupt_out: |
| 3318 | return XFS_ERROR(EFSCORRUPTED); |
| 3319 | } |